1 // SPDX-License-Identifier: GPL-2.0
6 #include "btree_update.h"
23 #include <linux/aio.h>
24 #include <linux/backing-dev.h>
25 #include <linux/exportfs.h>
26 #include <linux/module.h>
27 #include <linux/posix_acl.h>
28 #include <linux/random.h>
29 #include <linux/statfs.h>
30 #include <linux/xattr.h>
32 static struct kmem_cache *bch2_inode_cache;
34 static void bch2_vfs_inode_init(struct bch_fs *,
35 struct bch_inode_info *,
36 struct bch_inode_unpacked *);
38 static void journal_seq_copy(struct bch_inode_info *dst,
41 u64 old, v = READ_ONCE(dst->ei_journal_seq);
46 if (old >= journal_seq)
48 } while ((v = cmpxchg(&dst->ei_journal_seq, old, journal_seq)) != old);
52 * I_SIZE_DIRTY requires special handling:
54 * To the recovery code, the flag means that there is stale data past i_size
55 * that needs to be deleted; it's used for implementing atomic appends and
58 * On append, we set I_SIZE_DIRTY before doing the write, then after the write
59 * we clear I_SIZE_DIRTY atomically with updating i_size to the new larger size
60 * that exposes the data we just wrote.
62 * On truncate, it's the reverse: We set I_SIZE_DIRTY atomically with setting
63 * i_size to the new smaller size, then we delete the data that we just made
64 * invisible, and then we clear I_SIZE_DIRTY.
66 * Because there can be multiple appends in flight at a time, we need a refcount
67 * (i_size_dirty_count) instead of manipulating the flag directly. Nonzero
68 * refcount means I_SIZE_DIRTY is set, zero means it's cleared.
70 * Because write_inode() can be called at any time, i_size_dirty_count means
71 * something different to the runtime code - it means to write_inode() "don't
74 * We don't clear I_SIZE_DIRTY directly, we let write_inode() clear it when
75 * i_size_dirty_count is zero - but the reverse is not true, I_SIZE_DIRTY must
79 void bch2_inode_update_after_write(struct bch_fs *c,
80 struct bch_inode_info *inode,
81 struct bch_inode_unpacked *bi,
84 set_nlink(&inode->v, bi->bi_flags & BCH_INODE_UNLINKED
86 : bi->bi_nlink + nlink_bias(inode->v.i_mode));
87 i_uid_write(&inode->v, bi->bi_uid);
88 i_gid_write(&inode->v, bi->bi_gid);
89 inode->v.i_mode = bi->bi_mode;
91 if (fields & ATTR_ATIME)
92 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
93 if (fields & ATTR_MTIME)
94 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
95 if (fields & ATTR_CTIME)
96 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
98 inode->ei_inode = *bi;
100 bch2_inode_flags_to_vfs(inode);
103 int __must_check bch2_write_inode_trans(struct btree_trans *trans,
104 struct bch_inode_info *inode,
105 struct bch_inode_unpacked *inode_u,
109 struct bch_fs *c = trans->c;
110 struct btree_iter *iter = NULL;
111 struct bkey_inode_buf *inode_p;
114 lockdep_assert_held(&inode->ei_update_lock);
116 if (c->opts.new_inode_updates) {
117 /* XXX: Don't do this with btree locks held */
118 if (!inode->ei_inode_update)
119 inode->ei_inode_update =
120 bch2_deferred_update_alloc(c, BTREE_ID_INODES, 64);
122 iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
123 POS(inode->v.i_ino, 0),
124 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
126 return PTR_ERR(iter);
128 /* The btree node lock is our lock on the inode: */
129 ret = bch2_btree_iter_traverse(iter);
134 *inode_u = inode->ei_inode;
137 ret = set(inode, inode_u, p);
142 inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
144 return PTR_ERR(inode_p);
146 bch2_inode_pack(inode_p, inode_u);
148 if (!inode->ei_inode_update)
149 bch2_trans_update(trans,
150 BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
152 bch2_trans_update(trans,
153 BTREE_INSERT_DEFERRED(inode->ei_inode_update,
154 &inode_p->inode.k_i));
159 int __must_check bch2_write_inode(struct bch_fs *c,
160 struct bch_inode_info *inode,
162 void *p, unsigned fields)
164 struct btree_trans trans;
165 struct bch_inode_unpacked inode_u;
168 bch2_trans_init(&trans, c, 0, 0);
170 bch2_trans_begin(&trans);
172 ret = bch2_write_inode_trans(&trans, inode, &inode_u, set, p) ?:
173 bch2_trans_commit(&trans, NULL,
174 &inode->ei_journal_seq,
176 BTREE_INSERT_NOUNLOCK|
177 BTREE_INSERT_NOFAIL);
182 * the btree node lock protects inode->ei_inode, not ei_update_lock;
183 * this is important for inode updates via bchfs_write_index_update
186 bch2_inode_update_after_write(c, inode, &inode_u, fields);
188 bch2_trans_exit(&trans);
189 return ret < 0 ? ret : 0;
192 int bch2_fs_quota_transfer(struct bch_fs *c,
193 struct bch_inode_info *inode,
194 struct bch_qid new_qid,
196 enum quota_acct_mode mode)
201 qtypes &= enabled_qtypes(c);
203 for (i = 0; i < QTYP_NR; i++)
204 if (new_qid.q[i] == inode->ei_qid.q[i])
205 qtypes &= ~(1U << i);
210 mutex_lock(&inode->ei_quota_lock);
212 ret = bch2_quota_transfer(c, qtypes, new_qid,
215 inode->ei_quota_reserved,
218 for (i = 0; i < QTYP_NR; i++)
219 if (qtypes & (1 << i))
220 inode->ei_qid.q[i] = new_qid.q[i];
222 mutex_unlock(&inode->ei_quota_lock);
227 int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
228 struct bch_inode_unpacked *bi,
231 struct bch_inode_info *dir = p;
236 for (id = 0; id < Inode_opt_nr; id++) {
237 if (bi->bi_fields_set & (1 << id))
240 src = bch2_inode_opt_get(&dir->ei_inode, id);
241 dst = bch2_inode_opt_get(bi, id);
246 bch2_inode_opt_set(bi, id, src);
253 struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
255 struct bch_inode_unpacked inode_u;
256 struct bch_inode_info *inode;
259 inode = to_bch_ei(iget_locked(c->vfs_sb, inum));
260 if (unlikely(!inode))
261 return ERR_PTR(-ENOMEM);
262 if (!(inode->v.i_state & I_NEW))
265 ret = bch2_inode_find_by_inum(c, inum, &inode_u);
267 iget_failed(&inode->v);
271 bch2_vfs_inode_init(c, inode, &inode_u);
273 inode->ei_journal_seq = bch2_inode_journal_seq(&c->journal, inum);
275 unlock_new_inode(&inode->v);
280 static void bch2_inode_init_owner(struct bch_inode_unpacked *inode_u,
281 const struct inode *dir, umode_t mode)
283 kuid_t uid = current_fsuid();
286 if (dir && dir->i_mode & S_ISGID) {
291 gid = current_fsgid();
293 inode_u->bi_uid = from_kuid(dir->i_sb->s_user_ns, uid);
294 inode_u->bi_gid = from_kgid(dir->i_sb->s_user_ns, gid);
295 inode_u->bi_mode = mode;
298 static int inode_update_for_create_fn(struct bch_inode_info *inode,
299 struct bch_inode_unpacked *bi,
302 struct bch_fs *c = inode->v.i_sb->s_fs_info;
303 struct bch_inode_unpacked *new_inode = p;
305 bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
307 if (S_ISDIR(new_inode->bi_mode))
313 static struct bch_inode_info *
314 __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
315 umode_t mode, dev_t rdev, bool tmpfile)
317 struct bch_fs *c = dir->v.i_sb->s_fs_info;
318 struct btree_trans trans;
319 struct bch_inode_unpacked dir_u;
320 struct bch_inode_info *inode, *old;
321 struct bch_inode_unpacked inode_u;
322 struct bch_hash_info hash_info;
323 struct posix_acl *default_acl = NULL, *acl = NULL;
327 bch2_inode_init(c, &inode_u, 0, 0, 0, rdev, &dir->ei_inode);
328 bch2_inode_init_owner(&inode_u, &dir->v, mode);
330 hash_info = bch2_hash_info_init(c, &inode_u);
333 inode_u.bi_flags |= BCH_INODE_UNLINKED;
335 ret = bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
336 KEY_TYPE_QUOTA_PREALLOC);
340 #ifdef CONFIG_BCACHEFS_POSIX_ACL
341 ret = posix_acl_create(&dir->v, &inode_u.bi_mode, &default_acl, &acl);
347 * preallocate vfs inode before btree transaction, so that nothing can
348 * fail after the transaction succeeds:
350 inode = to_bch_ei(new_inode(c->vfs_sb));
351 if (unlikely(!inode)) {
357 mutex_lock(&dir->ei_update_lock);
359 bch2_trans_init(&trans, c, 8, 1024);
361 bch2_trans_begin(&trans);
363 ret = __bch2_inode_create(&trans, &inode_u,
364 BLOCKDEV_INODE_MAX, 0,
365 &c->unused_inode_hint) ?:
367 ? bch2_set_acl_trans(&trans, &inode_u, &hash_info,
368 default_acl, ACL_TYPE_DEFAULT)
371 ? bch2_set_acl_trans(&trans, &inode_u, &hash_info,
372 acl, ACL_TYPE_ACCESS)
375 ? __bch2_dirent_create(&trans, dir->v.i_ino,
380 BCH_HASH_SET_MUST_CREATE)
383 ? bch2_write_inode_trans(&trans, dir, &dir_u,
384 inode_update_for_create_fn,
387 bch2_trans_commit(&trans, NULL,
390 BTREE_INSERT_NOUNLOCK);
397 bch2_inode_update_after_write(c, dir, &dir_u,
398 ATTR_MTIME|ATTR_CTIME);
399 journal_seq_copy(dir, journal_seq);
400 mutex_unlock(&dir->ei_update_lock);
403 bch2_vfs_inode_init(c, inode, &inode_u);
404 journal_seq_copy(inode, journal_seq);
406 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
407 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
410 * we must insert the new inode into the inode cache before calling
411 * bch2_trans_exit() and dropping locks, else we could race with another
412 * thread pulling the inode in and modifying it:
415 old = to_bch_ei(insert_inode_locked2(&inode->v));
418 * We raced, another process pulled the new inode into cache
421 old->ei_journal_seq = inode->ei_journal_seq;
422 make_bad_inode(&inode->v);
428 * we really don't want insert_inode_locked2() to be setting
431 unlock_new_inode(&inode->v);
434 bch2_trans_exit(&trans);
436 posix_acl_release(default_acl);
437 posix_acl_release(acl);
441 mutex_unlock(&dir->ei_update_lock);
443 bch2_trans_exit(&trans);
444 make_bad_inode(&inode->v);
447 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1, KEY_TYPE_QUOTA_WARN);
448 inode = ERR_PTR(ret);
454 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
457 struct bch_fs *c = vdir->i_sb->s_fs_info;
458 struct bch_inode_info *dir = to_bch_ei(vdir);
459 struct inode *vinode = NULL;
462 inum = bch2_dirent_lookup(c, dir->v.i_ino,
467 vinode = bch2_vfs_inode_get(c, inum);
469 return d_splice_alias(vinode, dentry);
472 static int bch2_create(struct inode *vdir, struct dentry *dentry,
473 umode_t mode, bool excl)
475 struct bch_inode_info *inode =
476 __bch2_create(to_bch_ei(vdir), dentry, mode|S_IFREG, 0, false);
479 return PTR_ERR(inode);
481 d_instantiate(dentry, &inode->v);
485 static int inode_update_for_link_fn(struct bch_inode_info *inode,
486 struct bch_inode_unpacked *bi,
489 struct bch_fs *c = inode->v.i_sb->s_fs_info;
491 bi->bi_ctime = bch2_current_time(c);
493 if (bi->bi_flags & BCH_INODE_UNLINKED)
494 bi->bi_flags &= ~BCH_INODE_UNLINKED;
501 static int __bch2_link(struct bch_fs *c,
502 struct bch_inode_info *inode,
503 struct bch_inode_info *dir,
504 struct dentry *dentry)
506 struct btree_trans trans;
507 struct bch_inode_unpacked inode_u;
510 mutex_lock(&inode->ei_update_lock);
511 bch2_trans_init(&trans, c, 4, 1024);
513 bch2_trans_begin(&trans);
515 ret = __bch2_dirent_create(&trans, dir->v.i_ino,
517 mode_to_type(inode->v.i_mode),
520 BCH_HASH_SET_MUST_CREATE) ?:
521 bch2_write_inode_trans(&trans, inode, &inode_u,
522 inode_update_for_link_fn,
524 bch2_trans_commit(&trans, NULL,
525 &inode->ei_journal_seq,
527 BTREE_INSERT_NOUNLOCK);
533 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
535 bch2_trans_exit(&trans);
536 mutex_unlock(&inode->ei_update_lock);
540 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
541 struct dentry *dentry)
543 struct bch_fs *c = vdir->i_sb->s_fs_info;
544 struct bch_inode_info *dir = to_bch_ei(vdir);
545 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
548 lockdep_assert_held(&inode->v.i_rwsem);
550 ret = __bch2_link(c, inode, dir, dentry);
555 d_instantiate(dentry, &inode->v);
559 static int inode_update_dir_for_unlink_fn(struct bch_inode_info *inode,
560 struct bch_inode_unpacked *bi,
563 struct bch_fs *c = inode->v.i_sb->s_fs_info;
564 struct bch_inode_info *unlink_inode = p;
566 bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
568 bi->bi_nlink -= S_ISDIR(unlink_inode->v.i_mode);
573 static int inode_update_for_unlink_fn(struct bch_inode_info *inode,
574 struct bch_inode_unpacked *bi,
577 struct bch_fs *c = inode->v.i_sb->s_fs_info;
579 bi->bi_ctime = bch2_current_time(c);
583 bi->bi_flags |= BCH_INODE_UNLINKED;
588 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
590 struct bch_fs *c = vdir->i_sb->s_fs_info;
591 struct bch_inode_info *dir = to_bch_ei(vdir);
592 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
593 struct bch_inode_unpacked dir_u, inode_u;
594 struct btree_trans trans;
597 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
598 bch2_trans_init(&trans, c, 4, 1024);
600 bch2_trans_begin(&trans);
602 ret = __bch2_dirent_delete(&trans, dir->v.i_ino,
605 bch2_write_inode_trans(&trans, dir, &dir_u,
606 inode_update_dir_for_unlink_fn,
608 bch2_write_inode_trans(&trans, inode, &inode_u,
609 inode_update_for_unlink_fn,
611 bch2_trans_commit(&trans, NULL,
612 &dir->ei_journal_seq,
614 BTREE_INSERT_NOUNLOCK|
615 BTREE_INSERT_NOFAIL);
621 if (dir->ei_journal_seq > inode->ei_journal_seq)
622 inode->ei_journal_seq = dir->ei_journal_seq;
624 bch2_inode_update_after_write(c, dir, &dir_u,
625 ATTR_MTIME|ATTR_CTIME);
626 bch2_inode_update_after_write(c, inode, &inode_u,
629 bch2_trans_exit(&trans);
630 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
635 static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
638 struct bch_fs *c = vdir->i_sb->s_fs_info;
639 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
642 inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
643 if (unlikely(IS_ERR(inode)))
644 return PTR_ERR(inode);
646 inode_lock(&inode->v);
647 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
648 inode_unlock(&inode->v);
653 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
657 journal_seq_copy(dir, inode->ei_journal_seq);
659 ret = __bch2_link(c, inode, dir, dentry);
663 d_instantiate(dentry, &inode->v);
670 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
672 struct bch_inode_info *inode =
673 __bch2_create(to_bch_ei(vdir), dentry, mode|S_IFDIR, 0, false);
676 return PTR_ERR(inode);
678 d_instantiate(dentry, &inode->v);
682 static int bch2_rmdir(struct inode *vdir, struct dentry *dentry)
684 struct bch_fs *c = vdir->i_sb->s_fs_info;
686 if (bch2_empty_dir(c, dentry->d_inode->i_ino))
689 return bch2_unlink(vdir, dentry);
692 static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
693 umode_t mode, dev_t rdev)
695 struct bch_inode_info *inode =
696 __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
699 return PTR_ERR(inode);
701 d_instantiate(dentry, &inode->v);
707 struct bch_inode_info *src_dir;
708 struct bch_inode_info *dst_dir;
709 struct bch_inode_info *src_inode;
710 struct bch_inode_info *dst_inode;
711 enum bch_rename_mode mode;
714 static int inode_update_for_rename_fn(struct bch_inode_info *inode,
715 struct bch_inode_unpacked *bi,
718 struct rename_info *info = p;
721 if (inode == info->src_dir) {
722 bi->bi_nlink -= S_ISDIR(info->src_inode->v.i_mode);
723 bi->bi_nlink += info->dst_inode &&
724 S_ISDIR(info->dst_inode->v.i_mode) &&
725 info->mode == BCH_RENAME_EXCHANGE;
728 if (inode == info->dst_dir) {
729 bi->bi_nlink += S_ISDIR(info->src_inode->v.i_mode);
730 bi->bi_nlink -= info->dst_inode &&
731 S_ISDIR(info->dst_inode->v.i_mode);
734 if (inode == info->src_inode) {
735 ret = bch2_reinherit_attrs_fn(inode, bi, info->dst_dir);
737 BUG_ON(!ret && S_ISDIR(info->src_inode->v.i_mode));
740 if (inode == info->dst_inode &&
741 info->mode == BCH_RENAME_EXCHANGE) {
742 ret = bch2_reinherit_attrs_fn(inode, bi, info->src_dir);
744 BUG_ON(!ret && S_ISDIR(info->dst_inode->v.i_mode));
747 if (inode == info->dst_inode &&
748 info->mode == BCH_RENAME_OVERWRITE) {
749 BUG_ON(bi->bi_nlink &&
750 S_ISDIR(info->dst_inode->v.i_mode));
755 bi->bi_flags |= BCH_INODE_UNLINKED;
758 if (inode == info->src_dir ||
759 inode == info->dst_dir)
760 bi->bi_mtime = info->now;
761 bi->bi_ctime = info->now;
766 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
767 struct inode *dst_vdir, struct dentry *dst_dentry,
770 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
771 struct rename_info i = {
772 .src_dir = to_bch_ei(src_vdir),
773 .dst_dir = to_bch_ei(dst_vdir),
774 .src_inode = to_bch_ei(src_dentry->d_inode),
775 .dst_inode = to_bch_ei(dst_dentry->d_inode),
776 .mode = flags & RENAME_EXCHANGE
777 ? BCH_RENAME_EXCHANGE
778 : dst_dentry->d_inode
779 ? BCH_RENAME_OVERWRITE : BCH_RENAME,
781 struct btree_trans trans;
782 struct bch_inode_unpacked dst_dir_u, src_dir_u;
783 struct bch_inode_unpacked src_inode_u, dst_inode_u;
787 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
790 if (i.mode == BCH_RENAME_OVERWRITE) {
791 if (S_ISDIR(i.src_inode->v.i_mode) !=
792 S_ISDIR(i.dst_inode->v.i_mode))
795 if (S_ISDIR(i.src_inode->v.i_mode) &&
796 bch2_empty_dir(c, i.dst_inode->v.i_ino))
799 ret = filemap_write_and_wait_range(i.src_inode->v.i_mapping,
805 bch2_trans_init(&trans, c, 8, 2048);
807 bch2_lock_inodes(INODE_UPDATE_LOCK,
813 if (S_ISDIR(i.src_inode->v.i_mode) &&
814 inode_attrs_changing(i.dst_dir, i.src_inode)) {
819 if (i.mode == BCH_RENAME_EXCHANGE &&
820 S_ISDIR(i.dst_inode->v.i_mode) &&
821 inode_attrs_changing(i.src_dir, i.dst_inode)) {
826 if (inode_attr_changing(i.dst_dir, i.src_inode, Inode_opt_project)) {
827 ret = bch2_fs_quota_transfer(c, i.src_inode,
830 KEY_TYPE_QUOTA_PREALLOC);
835 if (i.mode == BCH_RENAME_EXCHANGE &&
836 inode_attr_changing(i.src_dir, i.dst_inode, Inode_opt_project)) {
837 ret = bch2_fs_quota_transfer(c, i.dst_inode,
840 KEY_TYPE_QUOTA_PREALLOC);
846 bch2_trans_begin(&trans);
847 i.now = bch2_current_time(c);
849 ret = bch2_dirent_rename(&trans,
850 i.src_dir, &src_dentry->d_name,
851 i.dst_dir, &dst_dentry->d_name,
853 bch2_write_inode_trans(&trans, i.src_dir, &src_dir_u,
854 inode_update_for_rename_fn, &i) ?:
855 (i.src_dir != i.dst_dir
856 ? bch2_write_inode_trans(&trans, i.dst_dir, &dst_dir_u,
857 inode_update_for_rename_fn, &i)
859 bch2_write_inode_trans(&trans, i.src_inode, &src_inode_u,
860 inode_update_for_rename_fn, &i) ?:
862 ? bch2_write_inode_trans(&trans, i.dst_inode, &dst_inode_u,
863 inode_update_for_rename_fn, &i)
865 bch2_trans_commit(&trans, NULL,
868 BTREE_INSERT_NOUNLOCK);
874 bch2_inode_update_after_write(c, i.src_dir, &src_dir_u,
875 ATTR_MTIME|ATTR_CTIME);
876 journal_seq_copy(i.src_dir, journal_seq);
878 if (i.src_dir != i.dst_dir) {
879 bch2_inode_update_after_write(c, i.dst_dir, &dst_dir_u,
880 ATTR_MTIME|ATTR_CTIME);
881 journal_seq_copy(i.dst_dir, journal_seq);
884 journal_seq_copy(i.src_inode, journal_seq);
886 journal_seq_copy(i.dst_inode, journal_seq);
888 bch2_inode_update_after_write(c, i.src_inode, &src_inode_u,
891 bch2_inode_update_after_write(c, i.dst_inode, &dst_inode_u,
894 bch2_trans_exit(&trans);
896 bch2_fs_quota_transfer(c, i.src_inode,
897 bch_qid(&i.src_inode->ei_inode),
899 KEY_TYPE_QUOTA_NOCHECK);
901 bch2_fs_quota_transfer(c, i.dst_inode,
902 bch_qid(&i.dst_inode->ei_inode),
904 KEY_TYPE_QUOTA_NOCHECK);
906 bch2_unlock_inodes(INODE_UPDATE_LOCK,
915 static int inode_update_for_setattr_fn(struct bch_inode_info *inode,
916 struct bch_inode_unpacked *bi,
919 struct bch_fs *c = inode->v.i_sb->s_fs_info;
920 struct iattr *attr = p;
921 unsigned int ia_valid = attr->ia_valid;
923 if (ia_valid & ATTR_UID)
924 bi->bi_uid = from_kuid(inode->v.i_sb->s_user_ns, attr->ia_uid);
925 if (ia_valid & ATTR_GID)
926 bi->bi_gid = from_kgid(inode->v.i_sb->s_user_ns, attr->ia_gid);
928 if (ia_valid & ATTR_ATIME)
929 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
930 if (ia_valid & ATTR_MTIME)
931 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
932 if (ia_valid & ATTR_CTIME)
933 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
935 if (ia_valid & ATTR_MODE) {
936 umode_t mode = attr->ia_mode;
937 kgid_t gid = ia_valid & ATTR_GID
941 if (!in_group_p(gid) &&
942 !capable_wrt_inode_uidgid(&inode->v, CAP_FSETID))
950 static int bch2_setattr_nonsize(struct bch_inode_info *inode, struct iattr *iattr)
952 struct bch_fs *c = inode->v.i_sb->s_fs_info;
954 struct btree_trans trans;
955 struct bch_inode_unpacked inode_u;
956 struct posix_acl *acl = NULL;
959 mutex_lock(&inode->ei_update_lock);
963 if (iattr->ia_valid & ATTR_UID)
964 qid.q[QTYP_USR] = from_kuid(&init_user_ns, iattr->ia_uid);
966 if (iattr->ia_valid & ATTR_GID)
967 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, iattr->ia_gid);
969 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
970 KEY_TYPE_QUOTA_PREALLOC);
974 bch2_trans_init(&trans, c, 0, 0);
976 bch2_trans_begin(&trans);
980 ret = bch2_write_inode_trans(&trans, inode, &inode_u,
981 inode_update_for_setattr_fn, iattr) ?:
982 (iattr->ia_valid & ATTR_MODE
983 ? bch2_acl_chmod(&trans, inode, iattr->ia_mode, &acl)
985 bch2_trans_commit(&trans, NULL,
986 &inode->ei_journal_seq,
988 BTREE_INSERT_NOUNLOCK|
989 BTREE_INSERT_NOFAIL);
995 bch2_inode_update_after_write(c, inode, &inode_u, iattr->ia_valid);
998 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
1000 bch2_trans_exit(&trans);
1002 mutex_unlock(&inode->ei_update_lock);
1007 static int bch2_getattr(const struct path *path, struct kstat *stat,
1008 u32 request_mask, unsigned query_flags)
1010 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
1011 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1013 stat->dev = inode->v.i_sb->s_dev;
1014 stat->ino = inode->v.i_ino;
1015 stat->mode = inode->v.i_mode;
1016 stat->nlink = inode->v.i_nlink;
1017 stat->uid = inode->v.i_uid;
1018 stat->gid = inode->v.i_gid;
1019 stat->rdev = inode->v.i_rdev;
1020 stat->size = i_size_read(&inode->v);
1021 stat->atime = inode->v.i_atime;
1022 stat->mtime = inode->v.i_mtime;
1023 stat->ctime = inode->v.i_ctime;
1024 stat->blksize = block_bytes(c);
1025 stat->blocks = inode->v.i_blocks;
1027 if (request_mask & STATX_BTIME) {
1028 stat->result_mask |= STATX_BTIME;
1029 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
1032 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
1033 stat->attributes |= STATX_ATTR_IMMUTABLE;
1034 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
1035 stat->attributes |= STATX_ATTR_APPEND;
1036 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
1037 stat->attributes |= STATX_ATTR_NODUMP;
1042 static int bch2_setattr(struct dentry *dentry, struct iattr *iattr)
1044 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
1047 lockdep_assert_held(&inode->v.i_rwsem);
1049 ret = setattr_prepare(dentry, iattr);
1053 return iattr->ia_valid & ATTR_SIZE
1054 ? bch2_truncate(inode, iattr)
1055 : bch2_setattr_nonsize(inode, iattr);
1058 static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
1060 struct bch_inode_info *inode =
1061 __bch2_create(to_bch_ei(vdir), dentry, mode, 0, true);
1064 return PTR_ERR(inode);
1066 d_mark_tmpfile(dentry, &inode->v);
1067 d_instantiate(dentry, &inode->v);
1071 static int bch2_fill_extent(struct fiemap_extent_info *info,
1072 const struct bkey_i *k, unsigned flags)
1074 if (bkey_extent_is_data(&k->k)) {
1075 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
1076 const union bch_extent_entry *entry;
1077 struct extent_ptr_decoded p;
1080 extent_for_each_ptr_decode(e, p, entry) {
1082 u64 offset = p.ptr.offset;
1084 if (p.crc.compression_type)
1085 flags2 |= FIEMAP_EXTENT_ENCODED;
1087 offset += p.crc.offset;
1089 if ((offset & (PAGE_SECTORS - 1)) ||
1090 (e.k->size & (PAGE_SECTORS - 1)))
1091 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
1093 ret = fiemap_fill_next_extent(info,
1094 bkey_start_offset(e.k) << 9,
1096 e.k->size << 9, flags|flags2);
1102 } else if (k->k.type == KEY_TYPE_reservation) {
1103 return fiemap_fill_next_extent(info,
1104 bkey_start_offset(&k->k) << 9,
1107 FIEMAP_EXTENT_DELALLOC|
1108 FIEMAP_EXTENT_UNWRITTEN);
1114 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
1117 struct bch_fs *c = vinode->i_sb->s_fs_info;
1118 struct bch_inode_info *ei = to_bch_ei(vinode);
1119 struct btree_trans trans;
1120 struct btree_iter *iter;
1123 bool have_extent = false;
1126 if (start + len < start)
1129 bch2_trans_init(&trans, c, 0, 0);
1131 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS,
1132 POS(ei->v.i_ino, start >> 9), 0, k, ret)
1133 if (bkey_extent_is_data(k.k) ||
1134 k.k->type == KEY_TYPE_reservation) {
1135 if (bkey_cmp(bkey_start_pos(k.k),
1136 POS(ei->v.i_ino, (start + len) >> 9)) >= 0)
1140 ret = bch2_fill_extent(info, &tmp.k, 0);
1145 bkey_reassemble(&tmp.k, k);
1149 if (!ret && have_extent)
1150 ret = bch2_fill_extent(info, &tmp.k, FIEMAP_EXTENT_LAST);
1152 ret = bch2_trans_exit(&trans) ?: ret;
1153 return ret < 0 ? ret : 0;
1156 static const struct vm_operations_struct bch_vm_ops = {
1157 .fault = filemap_fault,
1158 .map_pages = filemap_map_pages,
1159 .page_mkwrite = bch2_page_mkwrite,
1162 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1164 file_accessed(file);
1166 vma->vm_ops = &bch_vm_ops;
1172 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1174 return generic_file_llseek_size(file, offset, whence,
1178 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1180 struct bch_fs *c = file_inode(file)->i_sb->s_fs_info;
1182 return bch2_readdir(c, file, ctx);
1185 static const struct file_operations bch_file_operations = {
1186 .llseek = bch2_llseek,
1187 .read_iter = generic_file_read_iter,
1188 .write_iter = bch2_write_iter,
1190 .open = generic_file_open,
1191 .fsync = bch2_fsync,
1192 .splice_read = generic_file_splice_read,
1193 .splice_write = iter_file_splice_write,
1194 .fallocate = bch2_fallocate_dispatch,
1195 .unlocked_ioctl = bch2_fs_file_ioctl,
1196 #ifdef CONFIG_COMPAT
1197 .compat_ioctl = bch2_compat_fs_ioctl,
1201 static const struct inode_operations bch_file_inode_operations = {
1202 .getattr = bch2_getattr,
1203 .setattr = bch2_setattr,
1204 .fiemap = bch2_fiemap,
1205 .listxattr = bch2_xattr_list,
1206 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1207 .get_acl = bch2_get_acl,
1208 .set_acl = bch2_set_acl,
1212 static const struct inode_operations bch_dir_inode_operations = {
1213 .lookup = bch2_lookup,
1214 .create = bch2_create,
1216 .unlink = bch2_unlink,
1217 .symlink = bch2_symlink,
1218 .mkdir = bch2_mkdir,
1219 .rmdir = bch2_rmdir,
1220 .mknod = bch2_mknod,
1221 .rename = bch2_rename2,
1222 .getattr = bch2_getattr,
1223 .setattr = bch2_setattr,
1224 .tmpfile = bch2_tmpfile,
1225 .listxattr = bch2_xattr_list,
1226 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1227 .get_acl = bch2_get_acl,
1228 .set_acl = bch2_set_acl,
1232 static const struct file_operations bch_dir_file_operations = {
1233 .llseek = bch2_dir_llseek,
1234 .read = generic_read_dir,
1235 .iterate = bch2_vfs_readdir,
1236 .fsync = bch2_fsync,
1237 .unlocked_ioctl = bch2_fs_file_ioctl,
1238 #ifdef CONFIG_COMPAT
1239 .compat_ioctl = bch2_compat_fs_ioctl,
1243 static const struct inode_operations bch_symlink_inode_operations = {
1244 .get_link = page_get_link,
1245 .getattr = bch2_getattr,
1246 .setattr = bch2_setattr,
1247 .listxattr = bch2_xattr_list,
1248 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1249 .get_acl = bch2_get_acl,
1250 .set_acl = bch2_set_acl,
1254 static const struct inode_operations bch_special_inode_operations = {
1255 .getattr = bch2_getattr,
1256 .setattr = bch2_setattr,
1257 .listxattr = bch2_xattr_list,
1258 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1259 .get_acl = bch2_get_acl,
1260 .set_acl = bch2_set_acl,
1264 static const struct address_space_operations bch_address_space_operations = {
1265 .writepage = bch2_writepage,
1266 .readpage = bch2_readpage,
1267 .writepages = bch2_writepages,
1268 .readpages = bch2_readpages,
1269 .set_page_dirty = __set_page_dirty_nobuffers,
1270 .write_begin = bch2_write_begin,
1271 .write_end = bch2_write_end,
1272 .invalidatepage = bch2_invalidatepage,
1273 .releasepage = bch2_releasepage,
1274 .direct_IO = bch2_direct_IO,
1275 #ifdef CONFIG_MIGRATION
1276 .migratepage = bch2_migrate_page,
1278 .error_remove_page = generic_error_remove_page,
1281 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1282 u64 ino, u32 generation)
1284 struct bch_fs *c = sb->s_fs_info;
1285 struct inode *vinode;
1287 if (ino < BCACHEFS_ROOT_INO)
1288 return ERR_PTR(-ESTALE);
1290 vinode = bch2_vfs_inode_get(c, ino);
1292 return ERR_CAST(vinode);
1293 if (generation && vinode->i_generation != generation) {
1294 /* we didn't find the right inode.. */
1296 return ERR_PTR(-ESTALE);
1301 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *fid,
1302 int fh_len, int fh_type)
1304 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1305 bch2_nfs_get_inode);
1308 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *fid,
1309 int fh_len, int fh_type)
1311 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1312 bch2_nfs_get_inode);
1315 static const struct export_operations bch_export_ops = {
1316 .fh_to_dentry = bch2_fh_to_dentry,
1317 .fh_to_parent = bch2_fh_to_parent,
1318 //.get_parent = bch2_get_parent,
1321 static void bch2_vfs_inode_init(struct bch_fs *c,
1322 struct bch_inode_info *inode,
1323 struct bch_inode_unpacked *bi)
1325 bch2_inode_update_after_write(c, inode, bi, ~0);
1327 inode->v.i_blocks = bi->bi_sectors;
1328 inode->v.i_ino = bi->bi_inum;
1329 inode->v.i_rdev = bi->bi_dev;
1330 inode->v.i_generation = bi->bi_generation;
1331 inode->v.i_size = bi->bi_size;
1333 inode->ei_journal_seq = 0;
1334 inode->ei_quota_reserved = 0;
1335 inode->ei_str_hash = bch2_hash_info_init(c, bi);
1336 inode->ei_qid = bch_qid(bi);
1338 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1340 switch (inode->v.i_mode & S_IFMT) {
1342 inode->v.i_op = &bch_file_inode_operations;
1343 inode->v.i_fop = &bch_file_operations;
1346 inode->v.i_op = &bch_dir_inode_operations;
1347 inode->v.i_fop = &bch_dir_file_operations;
1350 inode_nohighmem(&inode->v);
1351 inode->v.i_op = &bch_symlink_inode_operations;
1354 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1355 inode->v.i_op = &bch_special_inode_operations;
1360 static struct inode *bch2_alloc_inode(struct super_block *sb)
1362 struct bch_inode_info *inode;
1364 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1368 inode_init_once(&inode->v);
1369 mutex_init(&inode->ei_update_lock);
1370 mutex_init(&inode->ei_quota_lock);
1371 inode->ei_inode_update = NULL;
1372 inode->ei_journal_seq = 0;
1377 static void bch2_i_callback(struct rcu_head *head)
1379 struct inode *vinode = container_of(head, struct inode, i_rcu);
1380 struct bch_inode_info *inode = to_bch_ei(vinode);
1382 kmem_cache_free(bch2_inode_cache, inode);
1385 static void bch2_destroy_inode(struct inode *vinode)
1387 call_rcu(&vinode->i_rcu, bch2_i_callback);
1390 static int inode_update_times_fn(struct bch_inode_info *inode,
1391 struct bch_inode_unpacked *bi,
1394 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1396 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1397 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1398 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1403 static int bch2_vfs_write_inode(struct inode *vinode,
1404 struct writeback_control *wbc)
1406 struct bch_fs *c = vinode->i_sb->s_fs_info;
1407 struct bch_inode_info *inode = to_bch_ei(vinode);
1410 mutex_lock(&inode->ei_update_lock);
1411 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1412 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1413 mutex_unlock(&inode->ei_update_lock);
1415 if (c->opts.journal_flush_disabled)
1418 if (!ret && wbc->sync_mode == WB_SYNC_ALL)
1419 ret = bch2_journal_flush_seq(&c->journal, inode->ei_journal_seq);
1424 static void bch2_evict_inode(struct inode *vinode)
1426 struct bch_fs *c = vinode->i_sb->s_fs_info;
1427 struct bch_inode_info *inode = to_bch_ei(vinode);
1429 truncate_inode_pages_final(&inode->v.i_data);
1431 clear_inode(&inode->v);
1433 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1435 if (inode->ei_inode_update)
1436 bch2_deferred_update_free(c, inode->ei_inode_update);
1437 inode->ei_inode_update = NULL;
1439 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1440 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1441 KEY_TYPE_QUOTA_WARN);
1442 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1443 KEY_TYPE_QUOTA_WARN);
1444 bch2_inode_rm(c, inode->v.i_ino);
1448 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1450 struct super_block *sb = dentry->d_sb;
1451 struct bch_fs *c = sb->s_fs_info;
1452 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1453 unsigned shift = sb->s_blocksize_bits - 9;
1456 buf->f_type = BCACHEFS_STATFS_MAGIC;
1457 buf->f_bsize = sb->s_blocksize;
1458 buf->f_blocks = usage.capacity >> shift;
1459 buf->f_bfree = (usage.capacity - usage.used) >> shift;
1460 buf->f_bavail = buf->f_bfree;
1461 buf->f_files = usage.nr_inodes;
1462 buf->f_ffree = U64_MAX;
1464 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1465 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1466 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1467 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1468 buf->f_namelen = BCH_NAME_MAX;
1473 static int bch2_sync_fs(struct super_block *sb, int wait)
1475 struct bch_fs *c = sb->s_fs_info;
1478 bch2_journal_flush_async(&c->journal, NULL);
1482 return bch2_journal_flush(&c->journal);
1485 static struct bch_fs *bch2_path_to_fs(const char *dev)
1488 struct block_device *bdev = lookup_bdev(dev);
1491 return ERR_CAST(bdev);
1493 c = bch2_bdev_to_fs(bdev);
1495 return c ?: ERR_PTR(-ENOENT);
1498 static struct bch_fs *__bch2_open_as_blockdevs(const char *dev_name, char * const *devs,
1499 unsigned nr_devs, struct bch_opts opts)
1501 struct bch_fs *c, *c1, *c2;
1505 return ERR_PTR(-EINVAL);
1507 c = bch2_fs_open(devs, nr_devs, opts);
1509 if (IS_ERR(c) && PTR_ERR(c) == -EBUSY) {
1512 * Look up each block device, make sure they all belong to a
1513 * filesystem and they all belong to the _same_ filesystem
1516 c1 = bch2_path_to_fs(devs[0]);
1520 for (i = 1; i < nr_devs; i++) {
1521 c2 = bch2_path_to_fs(devs[i]);
1523 closure_put(&c2->cl);
1526 closure_put(&c1->cl);
1537 mutex_lock(&c->state_lock);
1539 if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1540 mutex_unlock(&c->state_lock);
1541 closure_put(&c->cl);
1542 pr_err("err mounting %s: incomplete filesystem", dev_name);
1543 return ERR_PTR(-EINVAL);
1546 mutex_unlock(&c->state_lock);
1548 set_bit(BCH_FS_BDEV_MOUNTED, &c->flags);
1552 static struct bch_fs *bch2_open_as_blockdevs(const char *_dev_name,
1553 struct bch_opts opts)
1555 char *dev_name = NULL, **devs = NULL, *s;
1556 struct bch_fs *c = ERR_PTR(-ENOMEM);
1557 size_t i, nr_devs = 0;
1559 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1563 for (s = dev_name; s; s = strchr(s + 1, ':'))
1566 devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1570 for (i = 0, s = dev_name;
1572 (s = strchr(s, ':')) && (*s++ = '\0'))
1575 c = __bch2_open_as_blockdevs(_dev_name, devs, nr_devs, opts);
1582 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1584 struct bch_fs *c = sb->s_fs_info;
1585 struct bch_opts opts = bch2_opts_empty();
1588 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1590 ret = bch2_parse_mount_opts(&opts, data);
1594 if (opts.read_only != c->opts.read_only) {
1595 mutex_lock(&c->state_lock);
1597 if (opts.read_only) {
1598 bch2_fs_read_only(c);
1600 sb->s_flags |= SB_RDONLY;
1602 ret = bch2_fs_read_write(c);
1604 bch_err(c, "error going rw: %i", ret);
1605 mutex_unlock(&c->state_lock);
1609 sb->s_flags &= ~SB_RDONLY;
1612 c->opts.read_only = opts.read_only;
1614 mutex_unlock(&c->state_lock);
1617 if (opts.errors >= 0)
1618 c->opts.errors = opts.errors;
1623 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1625 struct bch_fs *c = root->d_sb->s_fs_info;
1629 for (i = 0; i < bch2_opts_nr; i++) {
1630 const struct bch_option *opt = &bch2_opt_table[i];
1631 u64 v = bch2_opt_get_by_id(&c->opts, i);
1633 if (!(opt->mode & OPT_MOUNT))
1636 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1639 bch2_opt_to_text(&PBUF(buf), c, opt, v,
1640 OPT_SHOW_MOUNT_STYLE);
1649 static const struct super_operations bch_super_operations = {
1650 .alloc_inode = bch2_alloc_inode,
1651 .destroy_inode = bch2_destroy_inode,
1652 .write_inode = bch2_vfs_write_inode,
1653 .evict_inode = bch2_evict_inode,
1654 .sync_fs = bch2_sync_fs,
1655 .statfs = bch2_statfs,
1656 .show_options = bch2_show_options,
1657 .remount_fs = bch2_remount,
1659 .put_super = bch2_put_super,
1660 .freeze_fs = bch2_freeze,
1661 .unfreeze_fs = bch2_unfreeze,
1665 static int bch2_test_super(struct super_block *s, void *data)
1667 return s->s_fs_info == data;
1670 static int bch2_set_super(struct super_block *s, void *data)
1672 s->s_fs_info = data;
1676 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1677 int flags, const char *dev_name, void *data)
1681 struct super_block *sb;
1682 struct inode *vinode;
1683 struct bch_opts opts = bch2_opts_empty();
1687 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1689 ret = bch2_parse_mount_opts(&opts, data);
1691 return ERR_PTR(ret);
1693 c = bch2_open_as_blockdevs(dev_name, opts);
1697 sb = sget(fs_type, bch2_test_super, bch2_set_super, flags|SB_NOSEC, c);
1699 closure_put(&c->cl);
1700 return ERR_CAST(sb);
1703 BUG_ON(sb->s_fs_info != c);
1706 closure_put(&c->cl);
1708 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1715 /* XXX: blocksize */
1716 sb->s_blocksize = PAGE_SIZE;
1717 sb->s_blocksize_bits = PAGE_SHIFT;
1718 sb->s_maxbytes = MAX_LFS_FILESIZE;
1719 sb->s_op = &bch_super_operations;
1720 sb->s_export_op = &bch_export_ops;
1721 #ifdef CONFIG_BCACHEFS_QUOTA
1722 sb->s_qcop = &bch2_quotactl_operations;
1723 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1725 sb->s_xattr = bch2_xattr_handlers;
1726 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1727 sb->s_time_gran = c->sb.time_precision;
1729 strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1731 ret = super_setup_bdi(sb);
1735 sb->s_bdi->congested_fn = bch2_congested;
1736 sb->s_bdi->congested_data = c;
1737 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1739 for_each_online_member(ca, c, i) {
1740 struct block_device *bdev = ca->disk_sb.bdev;
1742 /* XXX: create an anonymous device for multi device filesystems */
1744 sb->s_dev = bdev->bd_dev;
1745 percpu_ref_put(&ca->io_ref);
1749 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1751 sb->s_flags |= SB_POSIXACL;
1754 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
1755 if (IS_ERR(vinode)) {
1756 bch_err(c, "error mounting: error getting root inode %i",
1757 (int) PTR_ERR(vinode));
1758 ret = PTR_ERR(vinode);
1762 sb->s_root = d_make_root(vinode);
1764 bch_err(c, "error mounting: error allocating root dentry");
1769 sb->s_flags |= SB_ACTIVE;
1771 return dget(sb->s_root);
1774 deactivate_locked_super(sb);
1775 return ERR_PTR(ret);
1778 static void bch2_kill_sb(struct super_block *sb)
1780 struct bch_fs *c = sb->s_fs_info;
1782 generic_shutdown_super(sb);
1784 if (test_bit(BCH_FS_BDEV_MOUNTED, &c->flags))
1787 closure_put(&c->cl);
1790 static struct file_system_type bcache_fs_type = {
1791 .owner = THIS_MODULE,
1793 .mount = bch2_mount,
1794 .kill_sb = bch2_kill_sb,
1795 .fs_flags = FS_REQUIRES_DEV,
1798 MODULE_ALIAS_FS("bcachefs");
1800 void bch2_vfs_exit(void)
1802 unregister_filesystem(&bcache_fs_type);
1803 if (bch2_inode_cache)
1804 kmem_cache_destroy(bch2_inode_cache);
1807 int __init bch2_vfs_init(void)
1811 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1812 if (!bch2_inode_cache)
1815 ret = register_filesystem(&bcache_fs_type);
1825 #endif /* NO_BCACHEFS_FS */