1 // SPDX-License-Identifier: GPL-2.0
7 #include "btree_update.h"
14 #include "fs-common.h"
17 #include "fs-io-buffered.h"
18 #include "fs-io-direct.h"
19 #include "fs-io-pagecache.h"
30 #include <linux/aio.h>
31 #include <linux/backing-dev.h>
32 #include <linux/exportfs.h>
33 #include <linux/fiemap.h>
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/posix_acl.h>
37 #include <linux/random.h>
38 #include <linux/seq_file.h>
39 #include <linux/statfs.h>
40 #include <linux/string.h>
41 #include <linux/xattr.h>
43 static struct kmem_cache *bch2_inode_cache;
45 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
46 struct bch_inode_info *,
47 struct bch_inode_unpacked *,
48 struct bch_subvolume *);
50 void bch2_inode_update_after_write(struct btree_trans *trans,
51 struct bch_inode_info *inode,
52 struct bch_inode_unpacked *bi,
55 struct bch_fs *c = trans->c;
57 BUG_ON(bi->bi_inum != inode->v.i_ino);
59 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
61 c->opts.inodes_use_key_cache);
63 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
64 i_uid_write(&inode->v, bi->bi_uid);
65 i_gid_write(&inode->v, bi->bi_gid);
66 inode->v.i_mode = bi->bi_mode;
68 if (fields & ATTR_ATIME)
69 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
70 if (fields & ATTR_MTIME)
71 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
72 if (fields & ATTR_CTIME)
73 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
75 inode->ei_inode = *bi;
77 bch2_inode_flags_to_vfs(inode);
80 int __must_check bch2_write_inode(struct bch_fs *c,
81 struct bch_inode_info *inode,
83 void *p, unsigned fields)
85 struct btree_trans *trans = bch2_trans_get(c);
86 struct btree_iter iter = { NULL };
87 struct bch_inode_unpacked inode_u;
90 bch2_trans_begin(trans);
92 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
94 (set ? set(trans, inode, &inode_u, p) : 0) ?:
95 bch2_inode_write(trans, &iter, &inode_u) ?:
96 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
99 * the btree node lock protects inode->ei_inode, not ei_update_lock;
100 * this is important for inode updates via bchfs_write_index_update
103 bch2_inode_update_after_write(trans, inode, &inode_u, fields);
105 bch2_trans_iter_exit(trans, &iter);
107 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
110 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
111 "inode %u:%llu not found when updating",
112 inode_inum(inode).subvol,
113 inode_inum(inode).inum);
115 bch2_trans_put(trans);
116 return ret < 0 ? ret : 0;
119 int bch2_fs_quota_transfer(struct bch_fs *c,
120 struct bch_inode_info *inode,
121 struct bch_qid new_qid,
123 enum quota_acct_mode mode)
128 qtypes &= enabled_qtypes(c);
130 for (i = 0; i < QTYP_NR; i++)
131 if (new_qid.q[i] == inode->ei_qid.q[i])
132 qtypes &= ~(1U << i);
137 mutex_lock(&inode->ei_quota_lock);
139 ret = bch2_quota_transfer(c, qtypes, new_qid,
142 inode->ei_quota_reserved,
145 for (i = 0; i < QTYP_NR; i++)
146 if (qtypes & (1 << i))
147 inode->ei_qid.q[i] = new_qid.q[i];
149 mutex_unlock(&inode->ei_quota_lock);
154 static int bch2_iget5_test(struct inode *vinode, void *p)
156 struct bch_inode_info *inode = to_bch_ei(vinode);
157 subvol_inum *inum = p;
159 return inode->ei_subvol == inum->subvol &&
160 inode->ei_inode.bi_inum == inum->inum;
163 static int bch2_iget5_set(struct inode *vinode, void *p)
165 struct bch_inode_info *inode = to_bch_ei(vinode);
166 subvol_inum *inum = p;
168 inode->v.i_ino = inum->inum;
169 inode->ei_subvol = inum->subvol;
170 inode->ei_inode.bi_inum = inum->inum;
174 static unsigned bch2_inode_hash(subvol_inum inum)
176 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
179 static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_inode_info *inode)
181 subvol_inum inum = inode_inum(inode);
182 struct bch_inode_info *old = to_bch_ei(inode_insert5(&inode->v,
183 bch2_inode_hash(inum),
189 if (unlikely(old != inode)) {
190 discard_new_inode(&inode->v);
193 mutex_lock(&c->vfs_inodes_lock);
194 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
195 mutex_unlock(&c->vfs_inodes_lock);
197 * we really don't want insert_inode_locked2() to be setting
200 unlock_new_inode(&inode->v);
206 #define memalloc_flags_do(_flags, _do) \
208 unsigned _saved_flags = memalloc_flags_save(_flags); \
209 typeof(_do) _ret = _do; \
210 memalloc_noreclaim_restore(_saved_flags); \
215 * Allocate a new inode, dropping/retaking btree locks if necessary:
217 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
219 struct bch_fs *c = trans->c;
221 struct bch_inode_info *inode =
222 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN,
223 to_bch_ei(new_inode(c->vfs_sb)));
225 if (unlikely(!inode)) {
226 int ret = drop_locks_do(trans, (inode = to_bch_ei(new_inode(c->vfs_sb))) ? 0 : -ENOMEM);
228 discard_new_inode(&inode->v);
236 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
238 struct bch_inode_info *inode =
239 to_bch_ei(ilookup5_nowait(c->vfs_sb,
240 bch2_inode_hash(inum),
246 struct btree_trans *trans = bch2_trans_get(c);
248 struct bch_inode_unpacked inode_u;
249 struct bch_subvolume subvol;
250 int ret = lockrestart_do(trans,
251 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
252 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
253 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
255 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
256 inode = bch2_inode_insert(c, inode);
258 bch2_trans_put(trans);
260 return ret ? ERR_PTR(ret) : &inode->v;
263 struct bch_inode_info *
264 __bch2_create(struct mnt_idmap *idmap,
265 struct bch_inode_info *dir, struct dentry *dentry,
266 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
269 struct bch_fs *c = dir->v.i_sb->s_fs_info;
270 struct btree_trans *trans;
271 struct bch_inode_unpacked dir_u;
272 struct bch_inode_info *inode;
273 struct bch_inode_unpacked inode_u;
274 struct posix_acl *default_acl = NULL, *acl = NULL;
276 struct bch_subvolume subvol;
281 * preallocate acls + vfs inode before btree transaction, so that
282 * nothing can fail after the transaction succeeds:
284 #ifdef CONFIG_BCACHEFS_POSIX_ACL
285 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
289 inode = to_bch_ei(new_inode(c->vfs_sb));
290 if (unlikely(!inode)) {
291 inode = ERR_PTR(-ENOMEM);
295 bch2_inode_init_early(c, &inode_u);
297 if (!(flags & BCH_CREATE_TMPFILE))
298 mutex_lock(&dir->ei_update_lock);
300 trans = bch2_trans_get(c);
302 bch2_trans_begin(trans);
304 ret = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?:
305 bch2_create_trans(trans,
306 inode_inum(dir), &dir_u, &inode_u,
307 !(flags & BCH_CREATE_TMPFILE)
308 ? &dentry->d_name : NULL,
309 from_kuid(i_user_ns(&dir->v), current_fsuid()),
310 from_kgid(i_user_ns(&dir->v), current_fsgid()),
312 default_acl, acl, snapshot_src, flags) ?:
313 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
314 KEY_TYPE_QUOTA_PREALLOC);
316 goto err_before_quota;
318 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
319 inum.inum = inode_u.bi_inum;
321 ret = bch2_subvolume_get(trans, inum.subvol, true,
322 BTREE_ITER_WITH_UPDATES, &subvol) ?:
323 bch2_trans_commit(trans, NULL, &journal_seq, 0);
325 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
326 KEY_TYPE_QUOTA_WARN);
328 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
333 if (!(flags & BCH_CREATE_TMPFILE)) {
334 bch2_inode_update_after_write(trans, dir, &dir_u,
335 ATTR_MTIME|ATTR_CTIME);
336 mutex_unlock(&dir->ei_update_lock);
339 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
341 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
342 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
345 * we must insert the new inode into the inode cache before calling
346 * bch2_trans_exit() and dropping locks, else we could race with another
347 * thread pulling the inode in and modifying it:
349 inode = bch2_inode_insert(c, inode);
350 bch2_trans_put(trans);
352 posix_acl_release(default_acl);
353 posix_acl_release(acl);
356 if (!(flags & BCH_CREATE_TMPFILE))
357 mutex_unlock(&dir->ei_update_lock);
359 bch2_trans_put(trans);
360 make_bad_inode(&inode->v);
362 inode = ERR_PTR(ret);
368 static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans,
369 subvol_inum dir, struct bch_hash_info *dir_hash_info,
370 const struct qstr *name)
372 struct bch_fs *c = trans->c;
373 struct btree_iter dirent_iter = {};
374 subvol_inum inum = {};
376 int ret = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc,
377 dir_hash_info, dir, name, 0);
381 struct bkey_s_c k = bch2_btree_iter_peek_slot(&dirent_iter);
386 ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum);
392 struct bch_inode_info *inode =
393 to_bch_ei(ilookup5_nowait(c->vfs_sb,
394 bch2_inode_hash(inum),
400 struct bch_subvolume subvol;
401 struct bch_inode_unpacked inode_u;
402 ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
403 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?:
404 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
405 if (bch2_err_matches(ret, ENOENT)) {
406 struct printbuf buf = PRINTBUF;
408 bch2_bkey_val_to_text(&buf, c, k);
409 bch_err(c, "%s points to missing inode", buf.buf);
415 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
416 inode = bch2_inode_insert(c, inode);
418 bch2_trans_iter_exit(trans, &dirent_iter);
421 inode = ERR_PTR(ret);
425 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
428 struct bch_fs *c = vdir->i_sb->s_fs_info;
429 struct bch_inode_info *dir = to_bch_ei(vdir);
430 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
432 struct bch_inode_info *inode;
433 bch2_trans_do(c, NULL, NULL, 0,
434 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir),
435 &hash, &dentry->d_name)));
439 return d_splice_alias(&inode->v, dentry);
442 static int bch2_mknod(struct mnt_idmap *idmap,
443 struct inode *vdir, struct dentry *dentry,
444 umode_t mode, dev_t rdev)
446 struct bch_inode_info *inode =
447 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
448 (subvol_inum) { 0 }, 0);
451 return bch2_err_class(PTR_ERR(inode));
453 d_instantiate(dentry, &inode->v);
457 static int bch2_create(struct mnt_idmap *idmap,
458 struct inode *vdir, struct dentry *dentry,
459 umode_t mode, bool excl)
461 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
464 static int __bch2_link(struct bch_fs *c,
465 struct bch_inode_info *inode,
466 struct bch_inode_info *dir,
467 struct dentry *dentry)
469 struct btree_trans *trans = bch2_trans_get(c);
470 struct bch_inode_unpacked dir_u, inode_u;
473 mutex_lock(&inode->ei_update_lock);
475 ret = commit_do(trans, NULL, NULL, 0,
476 bch2_link_trans(trans,
477 inode_inum(dir), &dir_u,
478 inode_inum(inode), &inode_u,
482 bch2_inode_update_after_write(trans, dir, &dir_u,
483 ATTR_MTIME|ATTR_CTIME);
484 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
487 bch2_trans_put(trans);
488 mutex_unlock(&inode->ei_update_lock);
492 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
493 struct dentry *dentry)
495 struct bch_fs *c = vdir->i_sb->s_fs_info;
496 struct bch_inode_info *dir = to_bch_ei(vdir);
497 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
500 lockdep_assert_held(&inode->v.i_rwsem);
502 ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
503 bch2_subvol_is_ro(c, inode->ei_subvol) ?:
504 __bch2_link(c, inode, dir, dentry);
506 return bch2_err_class(ret);
509 d_instantiate(dentry, &inode->v);
513 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
514 bool deleting_snapshot)
516 struct bch_fs *c = vdir->i_sb->s_fs_info;
517 struct bch_inode_info *dir = to_bch_ei(vdir);
518 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
519 struct bch_inode_unpacked dir_u, inode_u;
520 struct btree_trans *trans = bch2_trans_get(c);
523 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
525 ret = commit_do(trans, NULL, NULL,
526 BCH_TRANS_COMMIT_no_enospc,
527 bch2_unlink_trans(trans,
528 inode_inum(dir), &dir_u,
529 &inode_u, &dentry->d_name,
534 bch2_inode_update_after_write(trans, dir, &dir_u,
535 ATTR_MTIME|ATTR_CTIME);
536 bch2_inode_update_after_write(trans, inode, &inode_u,
539 if (inode_u.bi_subvol) {
541 * Subvolume deletion is asynchronous, but we still want to tell
542 * the VFS that it's been deleted here:
544 set_nlink(&inode->v, 0);
547 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
548 bch2_trans_put(trans);
553 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
555 struct bch_inode_info *dir= to_bch_ei(vdir);
556 struct bch_fs *c = dir->v.i_sb->s_fs_info;
558 int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
559 __bch2_unlink(vdir, dentry, false);
560 return bch2_err_class(ret);
563 static int bch2_symlink(struct mnt_idmap *idmap,
564 struct inode *vdir, struct dentry *dentry,
567 struct bch_fs *c = vdir->i_sb->s_fs_info;
568 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
571 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
572 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
574 return bch2_err_class(PTR_ERR(inode));
576 inode_lock(&inode->v);
577 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
578 inode_unlock(&inode->v);
583 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
587 ret = __bch2_link(c, inode, dir, dentry);
591 d_instantiate(dentry, &inode->v);
595 return bch2_err_class(ret);
598 static int bch2_mkdir(struct mnt_idmap *idmap,
599 struct inode *vdir, struct dentry *dentry, umode_t mode)
601 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
604 static int bch2_rename2(struct mnt_idmap *idmap,
605 struct inode *src_vdir, struct dentry *src_dentry,
606 struct inode *dst_vdir, struct dentry *dst_dentry,
609 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
610 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
611 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
612 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
613 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
614 struct bch_inode_unpacked dst_dir_u, src_dir_u;
615 struct bch_inode_unpacked src_inode_u, dst_inode_u;
616 struct btree_trans *trans;
617 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
618 ? BCH_RENAME_EXCHANGE
619 : dst_dentry->d_inode
620 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
623 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
626 if (mode == BCH_RENAME_OVERWRITE) {
627 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
633 trans = bch2_trans_get(c);
635 bch2_lock_inodes(INODE_UPDATE_LOCK,
641 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?:
642 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol);
646 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
647 ret = bch2_fs_quota_transfer(c, src_inode,
650 KEY_TYPE_QUOTA_PREALLOC);
655 if (mode == BCH_RENAME_EXCHANGE &&
656 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
657 ret = bch2_fs_quota_transfer(c, dst_inode,
660 KEY_TYPE_QUOTA_PREALLOC);
665 ret = commit_do(trans, NULL, NULL, 0,
666 bch2_rename_trans(trans,
667 inode_inum(src_dir), &src_dir_u,
668 inode_inum(dst_dir), &dst_dir_u,
677 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
679 dst_inode->v.i_ino != dst_inode_u.bi_inum);
681 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
682 ATTR_MTIME|ATTR_CTIME);
684 if (src_dir != dst_dir)
685 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
686 ATTR_MTIME|ATTR_CTIME);
688 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
692 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
695 bch2_trans_put(trans);
697 bch2_fs_quota_transfer(c, src_inode,
698 bch_qid(&src_inode->ei_inode),
700 KEY_TYPE_QUOTA_NOCHECK);
702 bch2_fs_quota_transfer(c, dst_inode,
703 bch_qid(&dst_inode->ei_inode),
705 KEY_TYPE_QUOTA_NOCHECK);
707 bch2_unlock_inodes(INODE_UPDATE_LOCK,
713 return bch2_err_class(ret);
716 static void bch2_setattr_copy(struct mnt_idmap *idmap,
717 struct bch_inode_info *inode,
718 struct bch_inode_unpacked *bi,
721 struct bch_fs *c = inode->v.i_sb->s_fs_info;
722 unsigned int ia_valid = attr->ia_valid;
724 if (ia_valid & ATTR_UID)
725 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
726 if (ia_valid & ATTR_GID)
727 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
729 if (ia_valid & ATTR_SIZE)
730 bi->bi_size = attr->ia_size;
732 if (ia_valid & ATTR_ATIME)
733 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
734 if (ia_valid & ATTR_MTIME)
735 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
736 if (ia_valid & ATTR_CTIME)
737 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
739 if (ia_valid & ATTR_MODE) {
740 umode_t mode = attr->ia_mode;
741 kgid_t gid = ia_valid & ATTR_GID
745 if (!in_group_p(gid) &&
746 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
752 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
753 struct bch_inode_info *inode,
756 struct bch_fs *c = inode->v.i_sb->s_fs_info;
758 struct btree_trans *trans;
759 struct btree_iter inode_iter = { NULL };
760 struct bch_inode_unpacked inode_u;
761 struct posix_acl *acl = NULL;
764 mutex_lock(&inode->ei_update_lock);
768 if (attr->ia_valid & ATTR_UID)
769 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
771 if (attr->ia_valid & ATTR_GID)
772 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
774 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
775 KEY_TYPE_QUOTA_PREALLOC);
779 trans = bch2_trans_get(c);
781 bch2_trans_begin(trans);
785 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
790 bch2_setattr_copy(idmap, inode, &inode_u, attr);
792 if (attr->ia_valid & ATTR_MODE) {
793 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
794 inode_u.bi_mode, &acl);
799 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
800 bch2_trans_commit(trans, NULL, NULL,
801 BCH_TRANS_COMMIT_no_enospc);
803 bch2_trans_iter_exit(trans, &inode_iter);
805 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
810 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
813 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
815 bch2_trans_put(trans);
817 mutex_unlock(&inode->ei_update_lock);
819 return bch2_err_class(ret);
822 static int bch2_getattr(struct mnt_idmap *idmap,
823 const struct path *path, struct kstat *stat,
824 u32 request_mask, unsigned query_flags)
826 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
827 struct bch_fs *c = inode->v.i_sb->s_fs_info;
829 stat->dev = inode->v.i_sb->s_dev;
830 stat->ino = inode->v.i_ino;
831 stat->mode = inode->v.i_mode;
832 stat->nlink = inode->v.i_nlink;
833 stat->uid = inode->v.i_uid;
834 stat->gid = inode->v.i_gid;
835 stat->rdev = inode->v.i_rdev;
836 stat->size = i_size_read(&inode->v);
837 stat->atime = inode_get_atime(&inode->v);
838 stat->mtime = inode_get_mtime(&inode->v);
839 stat->ctime = inode_get_ctime(&inode->v);
840 stat->blksize = block_bytes(c);
841 stat->blocks = inode->v.i_blocks;
843 if (request_mask & STATX_BTIME) {
844 stat->result_mask |= STATX_BTIME;
845 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
848 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
849 stat->attributes |= STATX_ATTR_IMMUTABLE;
850 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
852 if (inode->ei_inode.bi_flags & BCH_INODE_append)
853 stat->attributes |= STATX_ATTR_APPEND;
854 stat->attributes_mask |= STATX_ATTR_APPEND;
856 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
857 stat->attributes |= STATX_ATTR_NODUMP;
858 stat->attributes_mask |= STATX_ATTR_NODUMP;
863 static int bch2_setattr(struct mnt_idmap *idmap,
864 struct dentry *dentry, struct iattr *iattr)
866 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
867 struct bch_fs *c = inode->v.i_sb->s_fs_info;
870 lockdep_assert_held(&inode->v.i_rwsem);
872 ret = bch2_subvol_is_ro(c, inode->ei_subvol) ?:
873 setattr_prepare(idmap, dentry, iattr);
877 return iattr->ia_valid & ATTR_SIZE
878 ? bchfs_truncate(idmap, inode, iattr)
879 : bch2_setattr_nonsize(idmap, inode, iattr);
882 static int bch2_tmpfile(struct mnt_idmap *idmap,
883 struct inode *vdir, struct file *file, umode_t mode)
885 struct bch_inode_info *inode =
886 __bch2_create(idmap, to_bch_ei(vdir),
887 file->f_path.dentry, mode, 0,
888 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
891 return bch2_err_class(PTR_ERR(inode));
893 d_mark_tmpfile(file, &inode->v);
894 d_instantiate(file->f_path.dentry, &inode->v);
895 return finish_open_simple(file, 0);
898 static int bch2_fill_extent(struct bch_fs *c,
899 struct fiemap_extent_info *info,
900 struct bkey_s_c k, unsigned flags)
902 if (bkey_extent_is_direct_data(k.k)) {
903 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
904 const union bch_extent_entry *entry;
905 struct extent_ptr_decoded p;
908 if (k.k->type == KEY_TYPE_reflink_v)
909 flags |= FIEMAP_EXTENT_SHARED;
911 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
913 u64 offset = p.ptr.offset;
916 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
918 if (p.crc.compression_type)
919 flags2 |= FIEMAP_EXTENT_ENCODED;
921 offset += p.crc.offset;
923 if ((offset & (block_sectors(c) - 1)) ||
924 (k.k->size & (block_sectors(c) - 1)))
925 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
927 ret = fiemap_fill_next_extent(info,
928 bkey_start_offset(k.k) << 9,
930 k.k->size << 9, flags|flags2);
936 } else if (bkey_extent_is_inline_data(k.k)) {
937 return fiemap_fill_next_extent(info,
938 bkey_start_offset(k.k) << 9,
941 FIEMAP_EXTENT_DATA_INLINE);
942 } else if (k.k->type == KEY_TYPE_reservation) {
943 return fiemap_fill_next_extent(info,
944 bkey_start_offset(k.k) << 9,
947 FIEMAP_EXTENT_DELALLOC|
948 FIEMAP_EXTENT_UNWRITTEN);
954 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
957 struct bch_fs *c = vinode->i_sb->s_fs_info;
958 struct bch_inode_info *ei = to_bch_ei(vinode);
959 struct btree_trans *trans;
960 struct btree_iter iter;
962 struct bkey_buf cur, prev;
963 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
964 unsigned offset_into_extent, sectors;
965 bool have_extent = false;
969 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
973 if (start + len < start)
978 bch2_bkey_buf_init(&cur);
979 bch2_bkey_buf_init(&prev);
980 trans = bch2_trans_get(c);
982 bch2_trans_begin(trans);
984 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot);
988 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
989 SPOS(ei->v.i_ino, start, snapshot), 0);
991 while (!(ret = btree_trans_too_many_iters(trans)) &&
992 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
993 !(ret = bkey_err(k))) {
994 enum btree_id data_btree = BTREE_ID_extents;
996 if (!bkey_extent_is_data(k.k) &&
997 k.k->type != KEY_TYPE_reservation) {
998 bch2_btree_iter_advance(&iter);
1002 offset_into_extent = iter.pos.offset -
1003 bkey_start_offset(k.k);
1004 sectors = k.k->size - offset_into_extent;
1006 bch2_bkey_buf_reassemble(&cur, c, k);
1008 ret = bch2_read_indirect_extent(trans, &data_btree,
1009 &offset_into_extent, &cur);
1013 k = bkey_i_to_s_c(cur.k);
1014 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
1016 sectors = min(sectors, k.k->size - offset_into_extent);
1018 bch2_cut_front(POS(k.k->p.inode,
1019 bkey_start_offset(k.k) +
1020 offset_into_extent),
1022 bch2_key_resize(&cur.k->k, sectors);
1023 cur.k->k.p = iter.pos;
1024 cur.k->k.p.offset += cur.k->k.size;
1027 bch2_trans_unlock(trans);
1028 ret = bch2_fill_extent(c, info,
1029 bkey_i_to_s_c(prev.k), 0);
1034 bkey_copy(prev.k, cur.k);
1037 bch2_btree_iter_set_pos(&iter,
1038 POS(iter.pos.inode, iter.pos.offset + sectors));
1040 start = iter.pos.offset;
1041 bch2_trans_iter_exit(trans, &iter);
1043 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1046 if (!ret && have_extent) {
1047 bch2_trans_unlock(trans);
1048 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
1049 FIEMAP_EXTENT_LAST);
1052 bch2_trans_put(trans);
1053 bch2_bkey_buf_exit(&cur, c);
1054 bch2_bkey_buf_exit(&prev, c);
1055 return ret < 0 ? ret : 0;
1058 static const struct vm_operations_struct bch_vm_ops = {
1059 .fault = bch2_page_fault,
1060 .map_pages = filemap_map_pages,
1061 .page_mkwrite = bch2_page_mkwrite,
1064 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1066 file_accessed(file);
1068 vma->vm_ops = &bch_vm_ops;
1074 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1076 return generic_file_llseek_size(file, offset, whence,
1080 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1082 struct bch_inode_info *inode = file_bch_inode(file);
1083 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1085 if (!dir_emit_dots(file, ctx))
1088 int ret = bch2_readdir(c, inode_inum(inode), ctx);
1091 return bch2_err_class(ret);
1094 static int bch2_open(struct inode *vinode, struct file *file)
1096 if (file->f_flags & (O_WRONLY|O_RDWR)) {
1097 struct bch_inode_info *inode = to_bch_ei(vinode);
1098 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1100 int ret = bch2_subvol_is_ro(c, inode->ei_subvol);
1105 return generic_file_open(vinode, file);
1108 static const struct file_operations bch_file_operations = {
1110 .llseek = bch2_llseek,
1111 .read_iter = bch2_read_iter,
1112 .write_iter = bch2_write_iter,
1114 .fsync = bch2_fsync,
1115 .splice_read = filemap_splice_read,
1116 .splice_write = iter_file_splice_write,
1117 .fallocate = bch2_fallocate_dispatch,
1118 .unlocked_ioctl = bch2_fs_file_ioctl,
1119 #ifdef CONFIG_COMPAT
1120 .compat_ioctl = bch2_compat_fs_ioctl,
1122 .remap_file_range = bch2_remap_file_range,
1125 static const struct inode_operations bch_file_inode_operations = {
1126 .getattr = bch2_getattr,
1127 .setattr = bch2_setattr,
1128 .fiemap = bch2_fiemap,
1129 .listxattr = bch2_xattr_list,
1130 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1131 .get_acl = bch2_get_acl,
1132 .set_acl = bch2_set_acl,
1136 static const struct inode_operations bch_dir_inode_operations = {
1137 .lookup = bch2_lookup,
1138 .create = bch2_create,
1140 .unlink = bch2_unlink,
1141 .symlink = bch2_symlink,
1142 .mkdir = bch2_mkdir,
1143 .rmdir = bch2_unlink,
1144 .mknod = bch2_mknod,
1145 .rename = bch2_rename2,
1146 .getattr = bch2_getattr,
1147 .setattr = bch2_setattr,
1148 .tmpfile = bch2_tmpfile,
1149 .listxattr = bch2_xattr_list,
1150 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1151 .get_acl = bch2_get_acl,
1152 .set_acl = bch2_set_acl,
1156 static const struct file_operations bch_dir_file_operations = {
1157 .llseek = bch2_dir_llseek,
1158 .read = generic_read_dir,
1159 .iterate_shared = bch2_vfs_readdir,
1160 .fsync = bch2_fsync,
1161 .unlocked_ioctl = bch2_fs_file_ioctl,
1162 #ifdef CONFIG_COMPAT
1163 .compat_ioctl = bch2_compat_fs_ioctl,
1167 static const struct inode_operations bch_symlink_inode_operations = {
1168 .get_link = page_get_link,
1169 .getattr = bch2_getattr,
1170 .setattr = bch2_setattr,
1171 .listxattr = bch2_xattr_list,
1172 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1173 .get_acl = bch2_get_acl,
1174 .set_acl = bch2_set_acl,
1178 static const struct inode_operations bch_special_inode_operations = {
1179 .getattr = bch2_getattr,
1180 .setattr = bch2_setattr,
1181 .listxattr = bch2_xattr_list,
1182 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1183 .get_acl = bch2_get_acl,
1184 .set_acl = bch2_set_acl,
1188 static const struct address_space_operations bch_address_space_operations = {
1189 .read_folio = bch2_read_folio,
1190 .writepages = bch2_writepages,
1191 .readahead = bch2_readahead,
1192 .dirty_folio = filemap_dirty_folio,
1193 .write_begin = bch2_write_begin,
1194 .write_end = bch2_write_end,
1195 .invalidate_folio = bch2_invalidate_folio,
1196 .release_folio = bch2_release_folio,
1197 .direct_IO = noop_direct_IO,
1198 #ifdef CONFIG_MIGRATION
1199 .migrate_folio = filemap_migrate_folio,
1201 .error_remove_folio = generic_error_remove_folio,
1204 struct bcachefs_fid {
1210 struct bcachefs_fid_with_parent {
1211 struct bcachefs_fid fid;
1212 struct bcachefs_fid dir;
1215 static int bcachefs_fid_valid(int fh_len, int fh_type)
1218 case FILEID_BCACHEFS_WITHOUT_PARENT:
1219 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1220 case FILEID_BCACHEFS_WITH_PARENT:
1221 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1227 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1229 return (struct bcachefs_fid) {
1230 .inum = inode->ei_inode.bi_inum,
1231 .subvol = inode->ei_subvol,
1232 .gen = inode->ei_inode.bi_generation,
1236 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1239 struct bch_inode_info *inode = to_bch_ei(vinode);
1240 struct bch_inode_info *dir = to_bch_ei(vdir);
1243 if (!S_ISDIR(inode->v.i_mode) && dir) {
1244 struct bcachefs_fid_with_parent *fid = (void *) fh;
1246 min_len = sizeof(*fid) / sizeof(u32);
1247 if (*len < min_len) {
1249 return FILEID_INVALID;
1252 fid->fid = bch2_inode_to_fid(inode);
1253 fid->dir = bch2_inode_to_fid(dir);
1256 return FILEID_BCACHEFS_WITH_PARENT;
1258 struct bcachefs_fid *fid = (void *) fh;
1260 min_len = sizeof(*fid) / sizeof(u32);
1261 if (*len < min_len) {
1263 return FILEID_INVALID;
1265 *fid = bch2_inode_to_fid(inode);
1268 return FILEID_BCACHEFS_WITHOUT_PARENT;
1272 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1273 struct bcachefs_fid fid)
1275 struct bch_fs *c = sb->s_fs_info;
1276 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1277 .subvol = fid.subvol,
1280 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1282 vinode = ERR_PTR(-ESTALE);
1287 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1288 int fh_len, int fh_type)
1290 struct bcachefs_fid *fid = (void *) _fid;
1292 if (!bcachefs_fid_valid(fh_len, fh_type))
1295 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1298 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1299 int fh_len, int fh_type)
1301 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1303 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1304 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1307 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1310 static struct dentry *bch2_get_parent(struct dentry *child)
1312 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1313 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1314 subvol_inum parent_inum = {
1315 .subvol = inode->ei_inode.bi_parent_subvol ?:
1317 .inum = inode->ei_inode.bi_dir,
1320 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1323 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1325 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1326 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1327 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1328 struct btree_trans *trans;
1329 struct btree_iter iter1;
1330 struct btree_iter iter2;
1332 struct bkey_s_c_dirent d;
1333 struct bch_inode_unpacked inode_u;
1336 struct qstr dirent_name;
1337 unsigned name_len = 0;
1340 if (!S_ISDIR(dir->v.i_mode))
1343 trans = bch2_trans_get(c);
1345 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1346 POS(dir->ei_inode.bi_inum, 0), 0);
1347 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1348 POS(dir->ei_inode.bi_inum, 0), 0);
1350 bch2_trans_begin(trans);
1352 ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot);
1356 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1357 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1359 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1363 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1364 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1366 k = bch2_btree_iter_peek_slot(&iter1);
1371 if (k.k->type != KEY_TYPE_dirent) {
1372 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1376 d = bkey_s_c_to_dirent(k);
1377 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1379 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1383 if (target.subvol == inode->ei_subvol &&
1384 target.inum == inode->ei_inode.bi_inum)
1388 * File with multiple hardlinks and our backref is to the wrong
1389 * directory - linear search:
1391 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1392 if (k.k->p.inode > dir->ei_inode.bi_inum)
1395 if (k.k->type != KEY_TYPE_dirent)
1398 d = bkey_s_c_to_dirent(k);
1399 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1405 if (target.subvol == inode->ei_subvol &&
1406 target.inum == inode->ei_inode.bi_inum)
1414 dirent_name = bch2_dirent_get_name(d);
1416 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
1417 memcpy(name, dirent_name.name, name_len);
1418 name[name_len] = '\0';
1420 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1423 bch2_trans_iter_exit(trans, &iter1);
1424 bch2_trans_iter_exit(trans, &iter2);
1425 bch2_trans_put(trans);
1430 static const struct export_operations bch_export_ops = {
1431 .encode_fh = bch2_encode_fh,
1432 .fh_to_dentry = bch2_fh_to_dentry,
1433 .fh_to_parent = bch2_fh_to_parent,
1434 .get_parent = bch2_get_parent,
1435 .get_name = bch2_get_name,
1438 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1439 struct bch_inode_info *inode,
1440 struct bch_inode_unpacked *bi,
1441 struct bch_subvolume *subvol)
1443 bch2_iget5_set(&inode->v, &inum);
1444 bch2_inode_update_after_write(trans, inode, bi, ~0);
1446 if (BCH_SUBVOLUME_SNAP(subvol))
1447 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1449 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1451 inode->v.i_blocks = bi->bi_sectors;
1452 inode->v.i_ino = bi->bi_inum;
1453 inode->v.i_rdev = bi->bi_dev;
1454 inode->v.i_generation = bi->bi_generation;
1455 inode->v.i_size = bi->bi_size;
1457 inode->ei_flags = 0;
1458 inode->ei_quota_reserved = 0;
1459 inode->ei_qid = bch_qid(bi);
1460 inode->ei_subvol = inum.subvol;
1462 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1464 switch (inode->v.i_mode & S_IFMT) {
1466 inode->v.i_op = &bch_file_inode_operations;
1467 inode->v.i_fop = &bch_file_operations;
1470 inode->v.i_op = &bch_dir_inode_operations;
1471 inode->v.i_fop = &bch_dir_file_operations;
1474 inode_nohighmem(&inode->v);
1475 inode->v.i_op = &bch_symlink_inode_operations;
1478 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1479 inode->v.i_op = &bch_special_inode_operations;
1483 mapping_set_large_folios(inode->v.i_mapping);
1486 static struct inode *bch2_alloc_inode(struct super_block *sb)
1488 struct bch_inode_info *inode;
1490 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1494 inode_init_once(&inode->v);
1495 mutex_init(&inode->ei_update_lock);
1496 two_state_lock_init(&inode->ei_pagecache_lock);
1497 INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
1498 mutex_init(&inode->ei_quota_lock);
1503 static void bch2_i_callback(struct rcu_head *head)
1505 struct inode *vinode = container_of(head, struct inode, i_rcu);
1506 struct bch_inode_info *inode = to_bch_ei(vinode);
1508 kmem_cache_free(bch2_inode_cache, inode);
1511 static void bch2_destroy_inode(struct inode *vinode)
1513 call_rcu(&vinode->i_rcu, bch2_i_callback);
1516 static int inode_update_times_fn(struct btree_trans *trans,
1517 struct bch_inode_info *inode,
1518 struct bch_inode_unpacked *bi,
1521 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1523 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
1524 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
1525 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
1530 static int bch2_vfs_write_inode(struct inode *vinode,
1531 struct writeback_control *wbc)
1533 struct bch_fs *c = vinode->i_sb->s_fs_info;
1534 struct bch_inode_info *inode = to_bch_ei(vinode);
1537 mutex_lock(&inode->ei_update_lock);
1538 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1539 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1540 mutex_unlock(&inode->ei_update_lock);
1542 return bch2_err_class(ret);
1545 static void bch2_evict_inode(struct inode *vinode)
1547 struct bch_fs *c = vinode->i_sb->s_fs_info;
1548 struct bch_inode_info *inode = to_bch_ei(vinode);
1550 truncate_inode_pages_final(&inode->v.i_data);
1552 clear_inode(&inode->v);
1554 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1556 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1557 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1558 KEY_TYPE_QUOTA_WARN);
1559 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1560 KEY_TYPE_QUOTA_WARN);
1561 bch2_inode_rm(c, inode_inum(inode));
1564 mutex_lock(&c->vfs_inodes_lock);
1565 list_del_init(&inode->ei_vfs_inode_list);
1566 mutex_unlock(&c->vfs_inodes_lock);
1569 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
1571 struct bch_inode_info *inode;
1572 DARRAY(struct bch_inode_info *) grabbed;
1573 bool clean_pass = false, this_pass_clean;
1576 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
1577 * be pruned with d_mark_dontcache().
1579 * Once we've had a clean pass where we didn't find any inodes without
1580 * I_DONTCACHE, we wait for them to be freed:
1583 darray_init(&grabbed);
1584 darray_make_room(&grabbed, 1024);
1587 this_pass_clean = true;
1589 mutex_lock(&c->vfs_inodes_lock);
1590 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
1591 if (!snapshot_list_has_id(s, inode->ei_subvol))
1594 if (!(inode->v.i_state & I_DONTCACHE) &&
1595 !(inode->v.i_state & I_FREEING) &&
1597 this_pass_clean = false;
1599 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
1603 } else if (clean_pass && this_pass_clean) {
1604 wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW);
1605 DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW);
1607 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1608 mutex_unlock(&c->vfs_inodes_lock);
1611 finish_wait(wq, &wait.wq_entry);
1615 mutex_unlock(&c->vfs_inodes_lock);
1617 darray_for_each(grabbed, i) {
1619 d_mark_dontcache(&inode->v);
1620 d_prune_aliases(&inode->v);
1625 if (!clean_pass || !this_pass_clean) {
1626 clean_pass = this_pass_clean;
1630 darray_exit(&grabbed);
1633 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1635 struct super_block *sb = dentry->d_sb;
1636 struct bch_fs *c = sb->s_fs_info;
1637 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1638 unsigned shift = sb->s_blocksize_bits - 9;
1640 * this assumes inodes take up 64 bytes, which is a decent average
1643 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1645 buf->f_type = BCACHEFS_STATFS_MAGIC;
1646 buf->f_bsize = sb->s_blocksize;
1647 buf->f_blocks = usage.capacity >> shift;
1648 buf->f_bfree = usage.free >> shift;
1649 buf->f_bavail = avail_factor(usage.free) >> shift;
1651 buf->f_files = usage.nr_inodes + avail_inodes;
1652 buf->f_ffree = avail_inodes;
1654 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
1655 buf->f_namelen = BCH_NAME_MAX;
1660 static int bch2_sync_fs(struct super_block *sb, int wait)
1662 struct bch_fs *c = sb->s_fs_info;
1665 if (c->opts.journal_flush_disabled)
1669 bch2_journal_flush_async(&c->journal, NULL);
1673 ret = bch2_journal_flush(&c->journal);
1674 return bch2_err_class(ret);
1677 static struct bch_fs *bch2_path_to_fs(const char *path)
1683 ret = lookup_bdev(path, &dev);
1685 return ERR_PTR(ret);
1687 c = bch2_dev_to_fs(dev);
1689 closure_put(&c->cl);
1690 return c ?: ERR_PTR(-ENOENT);
1693 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1695 struct bch_fs *c = sb->s_fs_info;
1696 struct bch_opts opts = bch2_opts_empty();
1699 ret = bch2_parse_mount_opts(c, &opts, data);
1703 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1705 if (opts.read_only != c->opts.read_only) {
1706 down_write(&c->state_lock);
1708 if (opts.read_only) {
1709 bch2_fs_read_only(c);
1711 sb->s_flags |= SB_RDONLY;
1713 ret = bch2_fs_read_write(c);
1715 bch_err(c, "error going rw: %i", ret);
1716 up_write(&c->state_lock);
1721 sb->s_flags &= ~SB_RDONLY;
1724 c->opts.read_only = opts.read_only;
1726 up_write(&c->state_lock);
1729 if (opt_defined(opts, errors))
1730 c->opts.errors = opts.errors;
1732 return bch2_err_class(ret);
1735 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1737 struct bch_fs *c = root->d_sb->s_fs_info;
1740 for_each_online_member(c, ca) {
1744 seq_puts(seq, ca->disk_sb.sb_name);
1750 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1752 struct bch_fs *c = root->d_sb->s_fs_info;
1754 struct printbuf buf = PRINTBUF;
1757 for (i = 0; i < bch2_opts_nr; i++) {
1758 const struct bch_option *opt = &bch2_opt_table[i];
1759 u64 v = bch2_opt_get_by_id(&c->opts, i);
1761 if (!(opt->flags & OPT_MOUNT))
1764 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1767 printbuf_reset(&buf);
1768 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1769 OPT_SHOW_MOUNT_STYLE);
1771 seq_puts(seq, buf.buf);
1774 if (buf.allocation_failure)
1776 printbuf_exit(&buf);
1780 static void bch2_put_super(struct super_block *sb)
1782 struct bch_fs *c = sb->s_fs_info;
1788 * bcachefs doesn't currently integrate intwrite freeze protection but the
1789 * internal write references serve the same purpose. Therefore reuse the
1790 * read-only transition code to perform the quiesce. The caveat is that we don't
1791 * currently have the ability to block tasks that want a write reference while
1792 * the superblock is frozen. This is fine for now, but we should either add
1793 * blocking support or find a way to integrate sb_start_intwrite() and friends.
1795 static int bch2_freeze(struct super_block *sb)
1797 struct bch_fs *c = sb->s_fs_info;
1799 down_write(&c->state_lock);
1800 bch2_fs_read_only(c);
1801 up_write(&c->state_lock);
1805 static int bch2_unfreeze(struct super_block *sb)
1807 struct bch_fs *c = sb->s_fs_info;
1810 if (test_bit(BCH_FS_emergency_ro, &c->flags))
1813 down_write(&c->state_lock);
1814 ret = bch2_fs_read_write(c);
1815 up_write(&c->state_lock);
1819 static const struct super_operations bch_super_operations = {
1820 .alloc_inode = bch2_alloc_inode,
1821 .destroy_inode = bch2_destroy_inode,
1822 .write_inode = bch2_vfs_write_inode,
1823 .evict_inode = bch2_evict_inode,
1824 .sync_fs = bch2_sync_fs,
1825 .statfs = bch2_statfs,
1826 .show_devname = bch2_show_devname,
1827 .show_options = bch2_show_options,
1828 .remount_fs = bch2_remount,
1829 .put_super = bch2_put_super,
1830 .freeze_fs = bch2_freeze,
1831 .unfreeze_fs = bch2_unfreeze,
1834 static int bch2_set_super(struct super_block *s, void *data)
1836 s->s_fs_info = data;
1840 static int bch2_noset_super(struct super_block *s, void *data)
1845 typedef DARRAY(struct bch_fs *) darray_fs;
1847 static int bch2_test_super(struct super_block *s, void *data)
1849 struct bch_fs *c = s->s_fs_info;
1850 darray_fs *d = data;
1855 darray_for_each(*d, i)
1861 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1862 int flags, const char *dev_name, void *data)
1865 struct super_block *sb;
1866 struct inode *vinode;
1867 struct bch_opts opts = bch2_opts_empty();
1870 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1872 ret = bch2_parse_mount_opts(NULL, &opts, data);
1874 return ERR_PTR(ret);
1876 if (!dev_name || strlen(dev_name) == 0)
1877 return ERR_PTR(-EINVAL);
1880 ret = bch2_split_devs(dev_name, &devs);
1882 return ERR_PTR(ret);
1884 darray_fs devs_to_fs = {};
1885 darray_for_each(devs, i) {
1886 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
1893 sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs);
1897 c = bch2_fs_open(devs.data, devs.nr, opts);
1903 /* Some options can't be parsed until after the fs is started: */
1904 ret = bch2_parse_mount_opts(c, &opts, data);
1911 bch2_opts_apply(&c->opts, opts);
1913 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1917 darray_exit(&devs_to_fs);
1918 bch2_darray_str_exit(&devs);
1922 ret = bch2_err_class(ret);
1923 return ERR_PTR(ret);
1929 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1936 sb->s_blocksize = block_bytes(c);
1937 sb->s_blocksize_bits = ilog2(block_bytes(c));
1938 sb->s_maxbytes = MAX_LFS_FILESIZE;
1939 sb->s_op = &bch_super_operations;
1940 sb->s_export_op = &bch_export_ops;
1941 #ifdef CONFIG_BCACHEFS_QUOTA
1942 sb->s_qcop = &bch2_quotactl_operations;
1943 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1945 sb->s_xattr = bch2_xattr_handlers;
1946 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1947 sb->s_time_gran = c->sb.nsec_per_time_unit;
1948 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1949 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1950 sb->s_uuid = c->sb.user_uuid;
1952 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
1954 ret = super_setup_bdi(sb);
1958 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1960 for_each_online_member(c, ca) {
1961 struct block_device *bdev = ca->disk_sb.bdev;
1963 /* XXX: create an anonymous device for multi device filesystems */
1965 sb->s_dev = bdev->bd_dev;
1966 percpu_ref_put(&ca->io_ref);
1972 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1974 sb->s_flags |= SB_POSIXACL;
1977 sb->s_shrink->seeks = 0;
1979 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1980 ret = PTR_ERR_OR_ZERO(vinode);
1981 bch_err_msg(c, ret, "mounting: error getting root inode");
1985 sb->s_root = d_make_root(vinode);
1987 bch_err(c, "error mounting: error allocating root dentry");
1992 sb->s_flags |= SB_ACTIVE;
1994 return dget(sb->s_root);
1997 deactivate_locked_super(sb);
1998 return ERR_PTR(bch2_err_class(ret));
2001 static void bch2_kill_sb(struct super_block *sb)
2003 struct bch_fs *c = sb->s_fs_info;
2005 generic_shutdown_super(sb);
2009 static struct file_system_type bcache_fs_type = {
2010 .owner = THIS_MODULE,
2012 .mount = bch2_mount,
2013 .kill_sb = bch2_kill_sb,
2014 .fs_flags = FS_REQUIRES_DEV,
2017 MODULE_ALIAS_FS("bcachefs");
2019 void bch2_vfs_exit(void)
2021 unregister_filesystem(&bcache_fs_type);
2022 kmem_cache_destroy(bch2_inode_cache);
2025 int __init bch2_vfs_init(void)
2029 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT);
2030 if (!bch2_inode_cache)
2033 ret = register_filesystem(&bcache_fs_type);
2043 #endif /* NO_BCACHEFS_FS */