1 // SPDX-License-Identifier: GPL-2.0
7 #include "btree_update.h"
14 #include "fs-common.h"
26 #include <linux/aio.h>
27 #include <linux/backing-dev.h>
28 #include <linux/exportfs.h>
29 #include <linux/fiemap.h>
30 #include <linux/module.h>
31 #include <linux/pagemap.h>
32 #include <linux/posix_acl.h>
33 #include <linux/random.h>
34 #include <linux/seq_file.h>
35 #include <linux/statfs.h>
36 #include <linux/string.h>
37 #include <linux/xattr.h>
39 static struct kmem_cache *bch2_inode_cache;
41 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
42 struct bch_inode_info *,
43 struct bch_inode_unpacked *,
44 struct bch_subvolume *);
46 void bch2_inode_update_after_write(struct btree_trans *trans,
47 struct bch_inode_info *inode,
48 struct bch_inode_unpacked *bi,
51 struct bch_fs *c = trans->c;
53 BUG_ON(bi->bi_inum != inode->v.i_ino);
55 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
57 c->opts.inodes_use_key_cache);
59 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
60 i_uid_write(&inode->v, bi->bi_uid);
61 i_gid_write(&inode->v, bi->bi_gid);
62 inode->v.i_mode = bi->bi_mode;
64 if (fields & ATTR_ATIME)
65 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
66 if (fields & ATTR_MTIME)
67 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
68 if (fields & ATTR_CTIME)
69 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
71 inode->ei_inode = *bi;
73 bch2_inode_flags_to_vfs(inode);
76 int __must_check bch2_write_inode(struct bch_fs *c,
77 struct bch_inode_info *inode,
79 void *p, unsigned fields)
81 struct btree_trans trans;
82 struct btree_iter iter = { NULL };
83 struct bch_inode_unpacked inode_u;
86 bch2_trans_init(&trans, c, 0, 512);
88 bch2_trans_begin(&trans);
90 ret = bch2_inode_peek(&trans, &iter, &inode_u, inode_inum(inode),
92 (set ? set(inode, &inode_u, p) : 0) ?:
93 bch2_inode_write(&trans, &iter, &inode_u) ?:
94 bch2_trans_commit(&trans, NULL, NULL, BTREE_INSERT_NOFAIL);
97 * the btree node lock protects inode->ei_inode, not ei_update_lock;
98 * this is important for inode updates via bchfs_write_index_update
101 bch2_inode_update_after_write(&trans, inode, &inode_u, fields);
103 bch2_trans_iter_exit(&trans, &iter);
105 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
108 bch2_trans_exit(&trans);
109 return ret < 0 ? ret : 0;
112 int bch2_fs_quota_transfer(struct bch_fs *c,
113 struct bch_inode_info *inode,
114 struct bch_qid new_qid,
116 enum quota_acct_mode mode)
121 qtypes &= enabled_qtypes(c);
123 for (i = 0; i < QTYP_NR; i++)
124 if (new_qid.q[i] == inode->ei_qid.q[i])
125 qtypes &= ~(1U << i);
130 mutex_lock(&inode->ei_quota_lock);
132 ret = bch2_quota_transfer(c, qtypes, new_qid,
135 inode->ei_quota_reserved,
138 for (i = 0; i < QTYP_NR; i++)
139 if (qtypes & (1 << i))
140 inode->ei_qid.q[i] = new_qid.q[i];
142 mutex_unlock(&inode->ei_quota_lock);
147 static int bch2_iget5_test(struct inode *vinode, void *p)
149 struct bch_inode_info *inode = to_bch_ei(vinode);
150 subvol_inum *inum = p;
152 return inode->ei_subvol == inum->subvol &&
153 inode->ei_inode.bi_inum == inum->inum;
156 static int bch2_iget5_set(struct inode *vinode, void *p)
158 struct bch_inode_info *inode = to_bch_ei(vinode);
159 subvol_inum *inum = p;
161 inode->v.i_ino = inum->inum;
162 inode->ei_subvol = inum->subvol;
163 inode->ei_inode.bi_inum = inum->inum;
167 static unsigned bch2_inode_hash(subvol_inum inum)
169 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
172 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
174 struct bch_inode_unpacked inode_u;
175 struct bch_inode_info *inode;
176 struct btree_trans trans;
177 struct bch_subvolume subvol;
180 inode = to_bch_ei(iget5_locked(c->vfs_sb,
181 bch2_inode_hash(inum),
185 if (unlikely(!inode))
186 return ERR_PTR(-ENOMEM);
187 if (!(inode->v.i_state & I_NEW))
190 bch2_trans_init(&trans, c, 8, 0);
191 ret = lockrestart_do(&trans,
192 bch2_subvolume_get(&trans, inum.subvol, true, 0, &subvol) ?:
193 bch2_inode_find_by_inum_trans(&trans, inum, &inode_u));
196 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
197 bch2_trans_exit(&trans);
200 iget_failed(&inode->v);
204 unlock_new_inode(&inode->v);
209 struct bch_inode_info *
210 __bch2_create(struct user_namespace *mnt_userns,
211 struct bch_inode_info *dir, struct dentry *dentry,
212 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
215 struct bch_fs *c = dir->v.i_sb->s_fs_info;
216 struct btree_trans trans;
217 struct bch_inode_unpacked dir_u;
218 struct bch_inode_info *inode, *old;
219 struct bch_inode_unpacked inode_u;
220 struct posix_acl *default_acl = NULL, *acl = NULL;
222 struct bch_subvolume subvol;
227 * preallocate acls + vfs inode before btree transaction, so that
228 * nothing can fail after the transaction succeeds:
230 #ifdef CONFIG_BCACHEFS_POSIX_ACL
231 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
235 inode = to_bch_ei(new_inode(c->vfs_sb));
236 if (unlikely(!inode)) {
237 inode = ERR_PTR(-ENOMEM);
241 bch2_inode_init_early(c, &inode_u);
243 if (!(flags & BCH_CREATE_TMPFILE))
244 mutex_lock(&dir->ei_update_lock);
246 bch2_trans_init(&trans, c, 8,
247 2048 + (!(flags & BCH_CREATE_TMPFILE)
248 ? dentry->d_name.len : 0));
250 bch2_trans_begin(&trans);
252 ret = bch2_create_trans(&trans,
253 inode_inum(dir), &dir_u, &inode_u,
254 !(flags & BCH_CREATE_TMPFILE)
255 ? &dentry->d_name : NULL,
256 from_kuid(mnt_userns, current_fsuid()),
257 from_kgid(mnt_userns, current_fsgid()),
259 default_acl, acl, snapshot_src, flags) ?:
260 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
261 KEY_TYPE_QUOTA_PREALLOC);
263 goto err_before_quota;
265 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
266 inum.inum = inode_u.bi_inum;
268 ret = bch2_subvolume_get(&trans, inum.subvol, true,
269 BTREE_ITER_WITH_UPDATES, &subvol) ?:
270 bch2_trans_commit(&trans, NULL, &journal_seq, 0);
272 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
273 KEY_TYPE_QUOTA_WARN);
275 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
280 if (!(flags & BCH_CREATE_TMPFILE)) {
281 bch2_inode_update_after_write(&trans, dir, &dir_u,
282 ATTR_MTIME|ATTR_CTIME);
283 mutex_unlock(&dir->ei_update_lock);
286 bch2_iget5_set(&inode->v, &inum);
287 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
289 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
290 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
293 * we must insert the new inode into the inode cache before calling
294 * bch2_trans_exit() and dropping locks, else we could race with another
295 * thread pulling the inode in and modifying it:
298 inode->v.i_state |= I_CREATING;
300 old = to_bch_ei(inode_insert5(&inode->v,
301 bch2_inode_hash(inum),
307 if (unlikely(old != inode)) {
309 * We raced, another process pulled the new inode into cache
312 make_bad_inode(&inode->v);
318 * we really don't want insert_inode_locked2() to be setting
321 unlock_new_inode(&inode->v);
324 bch2_trans_exit(&trans);
326 posix_acl_release(default_acl);
327 posix_acl_release(acl);
330 if (!(flags & BCH_CREATE_TMPFILE))
331 mutex_unlock(&dir->ei_update_lock);
333 bch2_trans_exit(&trans);
334 make_bad_inode(&inode->v);
336 inode = ERR_PTR(ret);
342 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
345 struct bch_fs *c = vdir->i_sb->s_fs_info;
346 struct bch_inode_info *dir = to_bch_ei(vdir);
347 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
348 struct inode *vinode = NULL;
349 subvol_inum inum = { .subvol = 1 };
352 ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
353 &dentry->d_name, &inum);
356 vinode = bch2_vfs_inode_get(c, inum);
358 return d_splice_alias(vinode, dentry);
361 static int bch2_mknod(struct user_namespace *mnt_userns,
362 struct inode *vdir, struct dentry *dentry,
363 umode_t mode, dev_t rdev)
365 struct bch_inode_info *inode =
366 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, rdev,
367 (subvol_inum) { 0 }, 0);
370 return bch2_err_class(PTR_ERR(inode));
372 d_instantiate(dentry, &inode->v);
376 static int bch2_create(struct user_namespace *mnt_userns,
377 struct inode *vdir, struct dentry *dentry,
378 umode_t mode, bool excl)
380 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFREG, 0);
383 static int __bch2_link(struct bch_fs *c,
384 struct bch_inode_info *inode,
385 struct bch_inode_info *dir,
386 struct dentry *dentry)
388 struct btree_trans trans;
389 struct bch_inode_unpacked dir_u, inode_u;
392 mutex_lock(&inode->ei_update_lock);
393 bch2_trans_init(&trans, c, 4, 1024);
395 ret = commit_do(&trans, NULL, NULL, 0,
396 bch2_link_trans(&trans,
397 inode_inum(dir), &dir_u,
398 inode_inum(inode), &inode_u,
402 bch2_inode_update_after_write(&trans, dir, &dir_u,
403 ATTR_MTIME|ATTR_CTIME);
404 bch2_inode_update_after_write(&trans, inode, &inode_u, ATTR_CTIME);
407 bch2_trans_exit(&trans);
408 mutex_unlock(&inode->ei_update_lock);
412 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
413 struct dentry *dentry)
415 struct bch_fs *c = vdir->i_sb->s_fs_info;
416 struct bch_inode_info *dir = to_bch_ei(vdir);
417 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
420 lockdep_assert_held(&inode->v.i_rwsem);
422 ret = __bch2_link(c, inode, dir, dentry);
427 d_instantiate(dentry, &inode->v);
431 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
432 bool deleting_snapshot)
434 struct bch_fs *c = vdir->i_sb->s_fs_info;
435 struct bch_inode_info *dir = to_bch_ei(vdir);
436 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
437 struct bch_inode_unpacked dir_u, inode_u;
438 struct btree_trans trans;
441 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
442 bch2_trans_init(&trans, c, 4, 1024);
444 ret = commit_do(&trans, NULL, NULL,
446 bch2_unlink_trans(&trans,
447 inode_inum(dir), &dir_u,
448 &inode_u, &dentry->d_name,
452 bch2_inode_update_after_write(&trans, dir, &dir_u,
453 ATTR_MTIME|ATTR_CTIME);
454 bch2_inode_update_after_write(&trans, inode, &inode_u,
458 bch2_trans_exit(&trans);
459 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
464 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
466 return __bch2_unlink(vdir, dentry, false);
469 static int bch2_symlink(struct user_namespace *mnt_userns,
470 struct inode *vdir, struct dentry *dentry,
473 struct bch_fs *c = vdir->i_sb->s_fs_info;
474 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
477 inode = __bch2_create(mnt_userns, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
478 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
480 return bch2_err_class(PTR_ERR(inode));
482 inode_lock(&inode->v);
483 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
484 inode_unlock(&inode->v);
489 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
493 ret = __bch2_link(c, inode, dir, dentry);
497 d_instantiate(dentry, &inode->v);
504 static int bch2_mkdir(struct user_namespace *mnt_userns,
505 struct inode *vdir, struct dentry *dentry, umode_t mode)
507 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFDIR, 0);
510 static int bch2_rename2(struct user_namespace *mnt_userns,
511 struct inode *src_vdir, struct dentry *src_dentry,
512 struct inode *dst_vdir, struct dentry *dst_dentry,
515 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
516 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
517 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
518 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
519 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
520 struct bch_inode_unpacked dst_dir_u, src_dir_u;
521 struct bch_inode_unpacked src_inode_u, dst_inode_u;
522 struct btree_trans trans;
523 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
524 ? BCH_RENAME_EXCHANGE
525 : dst_dentry->d_inode
526 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
529 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
532 if (mode == BCH_RENAME_OVERWRITE) {
533 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
539 bch2_trans_init(&trans, c, 8, 2048);
541 bch2_lock_inodes(INODE_UPDATE_LOCK,
547 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
548 ret = bch2_fs_quota_transfer(c, src_inode,
551 KEY_TYPE_QUOTA_PREALLOC);
556 if (mode == BCH_RENAME_EXCHANGE &&
557 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
558 ret = bch2_fs_quota_transfer(c, dst_inode,
561 KEY_TYPE_QUOTA_PREALLOC);
566 ret = commit_do(&trans, NULL, NULL, 0,
567 bch2_rename_trans(&trans,
568 inode_inum(src_dir), &src_dir_u,
569 inode_inum(dst_dir), &dst_dir_u,
578 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
580 dst_inode->v.i_ino != dst_inode_u.bi_inum);
582 bch2_inode_update_after_write(&trans, src_dir, &src_dir_u,
583 ATTR_MTIME|ATTR_CTIME);
585 if (src_dir != dst_dir)
586 bch2_inode_update_after_write(&trans, dst_dir, &dst_dir_u,
587 ATTR_MTIME|ATTR_CTIME);
589 bch2_inode_update_after_write(&trans, src_inode, &src_inode_u,
593 bch2_inode_update_after_write(&trans, dst_inode, &dst_inode_u,
596 bch2_trans_exit(&trans);
598 bch2_fs_quota_transfer(c, src_inode,
599 bch_qid(&src_inode->ei_inode),
601 KEY_TYPE_QUOTA_NOCHECK);
603 bch2_fs_quota_transfer(c, dst_inode,
604 bch_qid(&dst_inode->ei_inode),
606 KEY_TYPE_QUOTA_NOCHECK);
608 bch2_unlock_inodes(INODE_UPDATE_LOCK,
617 static void bch2_setattr_copy(struct user_namespace *mnt_userns,
618 struct bch_inode_info *inode,
619 struct bch_inode_unpacked *bi,
622 struct bch_fs *c = inode->v.i_sb->s_fs_info;
623 unsigned int ia_valid = attr->ia_valid;
625 if (ia_valid & ATTR_UID)
626 bi->bi_uid = from_kuid(mnt_userns, attr->ia_uid);
627 if (ia_valid & ATTR_GID)
628 bi->bi_gid = from_kgid(mnt_userns, attr->ia_gid);
630 if (ia_valid & ATTR_SIZE)
631 bi->bi_size = attr->ia_size;
633 if (ia_valid & ATTR_ATIME)
634 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
635 if (ia_valid & ATTR_MTIME)
636 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
637 if (ia_valid & ATTR_CTIME)
638 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
640 if (ia_valid & ATTR_MODE) {
641 umode_t mode = attr->ia_mode;
642 kgid_t gid = ia_valid & ATTR_GID
646 if (!in_group_p(gid) &&
647 !capable_wrt_inode_uidgid(mnt_userns, &inode->v, CAP_FSETID))
653 int bch2_setattr_nonsize(struct user_namespace *mnt_userns,
654 struct bch_inode_info *inode,
657 struct bch_fs *c = inode->v.i_sb->s_fs_info;
659 struct btree_trans trans;
660 struct btree_iter inode_iter = { NULL };
661 struct bch_inode_unpacked inode_u;
662 struct posix_acl *acl = NULL;
665 mutex_lock(&inode->ei_update_lock);
669 if (attr->ia_valid & ATTR_UID)
670 qid.q[QTYP_USR] = from_kuid(mnt_userns, attr->ia_uid);
672 if (attr->ia_valid & ATTR_GID)
673 qid.q[QTYP_GRP] = from_kgid(mnt_userns, attr->ia_gid);
675 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
676 KEY_TYPE_QUOTA_PREALLOC);
680 bch2_trans_init(&trans, c, 0, 0);
682 bch2_trans_begin(&trans);
686 ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode_inum(inode),
691 bch2_setattr_copy(mnt_userns, inode, &inode_u, attr);
693 if (attr->ia_valid & ATTR_MODE) {
694 ret = bch2_acl_chmod(&trans, inode_inum(inode), &inode_u,
695 inode_u.bi_mode, &acl);
700 ret = bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
701 bch2_trans_commit(&trans, NULL, NULL,
702 BTREE_INSERT_NOFAIL);
704 bch2_trans_iter_exit(&trans, &inode_iter);
706 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
711 bch2_inode_update_after_write(&trans, inode, &inode_u, attr->ia_valid);
714 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
716 bch2_trans_exit(&trans);
718 mutex_unlock(&inode->ei_update_lock);
720 return bch2_err_class(ret);
723 static int bch2_getattr(struct user_namespace *mnt_userns,
724 const struct path *path, struct kstat *stat,
725 u32 request_mask, unsigned query_flags)
727 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
728 struct bch_fs *c = inode->v.i_sb->s_fs_info;
730 stat->dev = inode->v.i_sb->s_dev;
731 stat->ino = inode->v.i_ino;
732 stat->mode = inode->v.i_mode;
733 stat->nlink = inode->v.i_nlink;
734 stat->uid = inode->v.i_uid;
735 stat->gid = inode->v.i_gid;
736 stat->rdev = inode->v.i_rdev;
737 stat->size = i_size_read(&inode->v);
738 stat->atime = inode->v.i_atime;
739 stat->mtime = inode->v.i_mtime;
740 stat->ctime = inode->v.i_ctime;
741 stat->blksize = block_bytes(c);
742 stat->blocks = inode->v.i_blocks;
744 if (request_mask & STATX_BTIME) {
745 stat->result_mask |= STATX_BTIME;
746 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
749 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
750 stat->attributes |= STATX_ATTR_IMMUTABLE;
751 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
753 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
754 stat->attributes |= STATX_ATTR_APPEND;
755 stat->attributes_mask |= STATX_ATTR_APPEND;
757 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
758 stat->attributes |= STATX_ATTR_NODUMP;
759 stat->attributes_mask |= STATX_ATTR_NODUMP;
764 static int bch2_setattr(struct user_namespace *mnt_userns,
765 struct dentry *dentry, struct iattr *iattr)
767 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
770 lockdep_assert_held(&inode->v.i_rwsem);
772 ret = setattr_prepare(mnt_userns, dentry, iattr);
776 return iattr->ia_valid & ATTR_SIZE
777 ? bch2_truncate(mnt_userns, inode, iattr)
778 : bch2_setattr_nonsize(mnt_userns, inode, iattr);
781 static int bch2_tmpfile(struct user_namespace *mnt_userns,
782 struct inode *vdir, struct file *file, umode_t mode)
784 struct bch_inode_info *inode =
785 __bch2_create(mnt_userns, to_bch_ei(vdir),
786 file->f_path.dentry, mode, 0,
787 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
790 return bch2_err_class(PTR_ERR(inode));
792 d_mark_tmpfile(file, &inode->v);
793 d_instantiate(file->f_path.dentry, &inode->v);
794 return finish_open_simple(file, 0);
797 static int bch2_fill_extent(struct bch_fs *c,
798 struct fiemap_extent_info *info,
799 struct bkey_s_c k, unsigned flags)
801 if (bkey_extent_is_direct_data(k.k)) {
802 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
803 const union bch_extent_entry *entry;
804 struct extent_ptr_decoded p;
807 if (k.k->type == KEY_TYPE_reflink_v)
808 flags |= FIEMAP_EXTENT_SHARED;
810 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
812 u64 offset = p.ptr.offset;
815 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
817 if (p.crc.compression_type)
818 flags2 |= FIEMAP_EXTENT_ENCODED;
820 offset += p.crc.offset;
822 if ((offset & (block_sectors(c) - 1)) ||
823 (k.k->size & (block_sectors(c) - 1)))
824 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
826 ret = fiemap_fill_next_extent(info,
827 bkey_start_offset(k.k) << 9,
829 k.k->size << 9, flags|flags2);
835 } else if (bkey_extent_is_inline_data(k.k)) {
836 return fiemap_fill_next_extent(info,
837 bkey_start_offset(k.k) << 9,
840 FIEMAP_EXTENT_DATA_INLINE);
841 } else if (k.k->type == KEY_TYPE_reservation) {
842 return fiemap_fill_next_extent(info,
843 bkey_start_offset(k.k) << 9,
846 FIEMAP_EXTENT_DELALLOC|
847 FIEMAP_EXTENT_UNWRITTEN);
853 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
856 struct bch_fs *c = vinode->i_sb->s_fs_info;
857 struct bch_inode_info *ei = to_bch_ei(vinode);
858 struct btree_trans trans;
859 struct btree_iter iter;
861 struct bkey_buf cur, prev;
862 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
863 unsigned offset_into_extent, sectors;
864 bool have_extent = false;
868 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
872 if (start + len < start)
877 bch2_bkey_buf_init(&cur);
878 bch2_bkey_buf_init(&prev);
879 bch2_trans_init(&trans, c, 0, 0);
881 bch2_trans_begin(&trans);
883 ret = bch2_subvolume_get_snapshot(&trans, ei->ei_subvol, &snapshot);
887 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
888 SPOS(ei->v.i_ino, start, snapshot), 0);
890 while (!(ret = btree_trans_too_many_iters(&trans)) &&
891 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
892 !(ret = bkey_err(k))) {
893 enum btree_id data_btree = BTREE_ID_extents;
895 if (!bkey_extent_is_data(k.k) &&
896 k.k->type != KEY_TYPE_reservation) {
897 bch2_btree_iter_advance(&iter);
901 offset_into_extent = iter.pos.offset -
902 bkey_start_offset(k.k);
903 sectors = k.k->size - offset_into_extent;
905 bch2_bkey_buf_reassemble(&cur, c, k);
907 ret = bch2_read_indirect_extent(&trans, &data_btree,
908 &offset_into_extent, &cur);
912 k = bkey_i_to_s_c(cur.k);
913 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
915 sectors = min(sectors, k.k->size - offset_into_extent);
917 bch2_cut_front(POS(k.k->p.inode,
918 bkey_start_offset(k.k) +
921 bch2_key_resize(&cur.k->k, sectors);
922 cur.k->k.p = iter.pos;
923 cur.k->k.p.offset += cur.k->k.size;
926 ret = bch2_fill_extent(c, info,
927 bkey_i_to_s_c(prev.k), 0);
932 bkey_copy(prev.k, cur.k);
935 bch2_btree_iter_set_pos(&iter,
936 POS(iter.pos.inode, iter.pos.offset + sectors));
938 start = iter.pos.offset;
939 bch2_trans_iter_exit(&trans, &iter);
941 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
944 if (!ret && have_extent)
945 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
948 bch2_trans_exit(&trans);
949 bch2_bkey_buf_exit(&cur, c);
950 bch2_bkey_buf_exit(&prev, c);
951 return ret < 0 ? ret : 0;
954 static const struct vm_operations_struct bch_vm_ops = {
955 .fault = bch2_page_fault,
956 .map_pages = filemap_map_pages,
957 .page_mkwrite = bch2_page_mkwrite,
960 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
964 vma->vm_ops = &bch_vm_ops;
970 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
972 return generic_file_llseek_size(file, offset, whence,
976 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
978 struct bch_inode_info *inode = file_bch_inode(file);
979 struct bch_fs *c = inode->v.i_sb->s_fs_info;
981 if (!dir_emit_dots(file, ctx))
984 return bch2_readdir(c, inode_inum(inode), ctx);
987 static const struct file_operations bch_file_operations = {
988 .llseek = bch2_llseek,
989 .read_iter = bch2_read_iter,
990 .write_iter = bch2_write_iter,
992 .open = generic_file_open,
994 .splice_read = generic_file_splice_read,
995 .splice_write = iter_file_splice_write,
996 .fallocate = bch2_fallocate_dispatch,
997 .unlocked_ioctl = bch2_fs_file_ioctl,
999 .compat_ioctl = bch2_compat_fs_ioctl,
1001 .remap_file_range = bch2_remap_file_range,
1004 static const struct inode_operations bch_file_inode_operations = {
1005 .getattr = bch2_getattr,
1006 .setattr = bch2_setattr,
1007 .fiemap = bch2_fiemap,
1008 .listxattr = bch2_xattr_list,
1009 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1010 .get_acl = bch2_get_acl,
1011 .set_acl = bch2_set_acl,
1015 static const struct inode_operations bch_dir_inode_operations = {
1016 .lookup = bch2_lookup,
1017 .create = bch2_create,
1019 .unlink = bch2_unlink,
1020 .symlink = bch2_symlink,
1021 .mkdir = bch2_mkdir,
1022 .rmdir = bch2_unlink,
1023 .mknod = bch2_mknod,
1024 .rename = bch2_rename2,
1025 .getattr = bch2_getattr,
1026 .setattr = bch2_setattr,
1027 .tmpfile = bch2_tmpfile,
1028 .listxattr = bch2_xattr_list,
1029 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1030 .get_acl = bch2_get_acl,
1031 .set_acl = bch2_set_acl,
1035 static const struct file_operations bch_dir_file_operations = {
1036 .llseek = bch2_dir_llseek,
1037 .read = generic_read_dir,
1038 .iterate_shared = bch2_vfs_readdir,
1039 .fsync = bch2_fsync,
1040 .unlocked_ioctl = bch2_fs_file_ioctl,
1041 #ifdef CONFIG_COMPAT
1042 .compat_ioctl = bch2_compat_fs_ioctl,
1046 static const struct inode_operations bch_symlink_inode_operations = {
1047 .get_link = page_get_link,
1048 .getattr = bch2_getattr,
1049 .setattr = bch2_setattr,
1050 .listxattr = bch2_xattr_list,
1051 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1052 .get_acl = bch2_get_acl,
1053 .set_acl = bch2_set_acl,
1057 static const struct inode_operations bch_special_inode_operations = {
1058 .getattr = bch2_getattr,
1059 .setattr = bch2_setattr,
1060 .listxattr = bch2_xattr_list,
1061 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1062 .get_acl = bch2_get_acl,
1063 .set_acl = bch2_set_acl,
1067 static const struct address_space_operations bch_address_space_operations = {
1068 .read_folio = bch2_read_folio,
1069 .writepages = bch2_writepages,
1070 .readahead = bch2_readahead,
1071 .dirty_folio = filemap_dirty_folio,
1072 .write_begin = bch2_write_begin,
1073 .write_end = bch2_write_end,
1074 .invalidate_folio = bch2_invalidate_folio,
1075 .release_folio = bch2_release_folio,
1076 .direct_IO = noop_direct_IO,
1077 #ifdef CONFIG_MIGRATION
1078 .migrate_folio = filemap_migrate_folio,
1080 .error_remove_page = generic_error_remove_page,
1083 struct bcachefs_fid {
1089 struct bcachefs_fid_with_parent {
1090 struct bcachefs_fid fid;
1091 struct bcachefs_fid dir;
1094 static int bcachefs_fid_valid(int fh_len, int fh_type)
1097 case FILEID_BCACHEFS_WITHOUT_PARENT:
1098 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1099 case FILEID_BCACHEFS_WITH_PARENT:
1100 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1106 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1108 return (struct bcachefs_fid) {
1109 .inum = inode->ei_inode.bi_inum,
1110 .subvol = inode->ei_subvol,
1111 .gen = inode->ei_inode.bi_generation,
1115 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1118 struct bch_inode_info *inode = to_bch_ei(vinode);
1119 struct bch_inode_info *dir = to_bch_ei(vdir);
1121 if (*len < sizeof(struct bcachefs_fid_with_parent) / sizeof(u32))
1122 return FILEID_INVALID;
1124 if (!S_ISDIR(inode->v.i_mode) && dir) {
1125 struct bcachefs_fid_with_parent *fid = (void *) fh;
1127 fid->fid = bch2_inode_to_fid(inode);
1128 fid->dir = bch2_inode_to_fid(dir);
1130 *len = sizeof(*fid) / sizeof(u32);
1131 return FILEID_BCACHEFS_WITH_PARENT;
1133 struct bcachefs_fid *fid = (void *) fh;
1135 *fid = bch2_inode_to_fid(inode);
1137 *len = sizeof(*fid) / sizeof(u32);
1138 return FILEID_BCACHEFS_WITHOUT_PARENT;
1142 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1143 struct bcachefs_fid fid)
1145 struct bch_fs *c = sb->s_fs_info;
1146 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1147 .subvol = fid.subvol,
1150 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1152 vinode = ERR_PTR(-ESTALE);
1157 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1158 int fh_len, int fh_type)
1160 struct bcachefs_fid *fid = (void *) _fid;
1162 if (!bcachefs_fid_valid(fh_len, fh_type))
1165 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1168 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1169 int fh_len, int fh_type)
1171 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1173 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1174 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1177 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1180 static struct dentry *bch2_get_parent(struct dentry *child)
1182 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1183 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1184 subvol_inum parent_inum = {
1185 .subvol = inode->ei_inode.bi_parent_subvol ?:
1187 .inum = inode->ei_inode.bi_dir,
1190 if (!parent_inum.inum)
1193 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1196 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1198 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1199 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1200 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1201 struct btree_trans trans;
1202 struct btree_iter iter1;
1203 struct btree_iter iter2;
1205 struct bkey_s_c_dirent d;
1206 struct bch_inode_unpacked inode_u;
1212 if (!S_ISDIR(dir->v.i_mode))
1215 bch2_trans_init(&trans, c, 0, 0);
1217 bch2_trans_iter_init(&trans, &iter1, BTREE_ID_dirents,
1218 POS(dir->ei_inode.bi_inum, 0), 0);
1219 bch2_trans_iter_init(&trans, &iter2, BTREE_ID_dirents,
1220 POS(dir->ei_inode.bi_inum, 0), 0);
1222 bch2_trans_begin(&trans);
1224 ret = bch2_subvolume_get_snapshot(&trans, dir->ei_subvol, &snapshot);
1228 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1229 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1231 ret = bch2_inode_find_by_inum_trans(&trans, inode_inum(inode), &inode_u);
1235 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1236 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1238 k = bch2_btree_iter_peek_slot(&iter1);
1243 if (k.k->type != KEY_TYPE_dirent) {
1248 d = bkey_s_c_to_dirent(k);
1249 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1255 if (target.subvol == inode->ei_subvol &&
1256 target.inum == inode->ei_inode.bi_inum)
1260 * File with multiple hardlinks and our backref is to the wrong
1261 * directory - linear search:
1263 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1264 if (k.k->p.inode > dir->ei_inode.bi_inum)
1267 if (k.k->type != KEY_TYPE_dirent)
1270 d = bkey_s_c_to_dirent(k);
1271 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1277 if (target.subvol == inode->ei_subvol &&
1278 target.inum == inode->ei_inode.bi_inum)
1286 name_len = min_t(unsigned, bch2_dirent_name_bytes(d), NAME_MAX);
1288 memcpy(name, d.v->d_name, name_len);
1289 name[name_len] = '\0';
1291 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1294 bch2_trans_iter_exit(&trans, &iter1);
1295 bch2_trans_iter_exit(&trans, &iter2);
1296 bch2_trans_exit(&trans);
1301 static const struct export_operations bch_export_ops = {
1302 .encode_fh = bch2_encode_fh,
1303 .fh_to_dentry = bch2_fh_to_dentry,
1304 .fh_to_parent = bch2_fh_to_parent,
1305 .get_parent = bch2_get_parent,
1306 .get_name = bch2_get_name,
1309 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1310 struct bch_inode_info *inode,
1311 struct bch_inode_unpacked *bi,
1312 struct bch_subvolume *subvol)
1314 bch2_inode_update_after_write(trans, inode, bi, ~0);
1316 if (BCH_SUBVOLUME_SNAP(subvol))
1317 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1319 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1321 inode->v.i_blocks = bi->bi_sectors;
1322 inode->v.i_ino = bi->bi_inum;
1323 inode->v.i_rdev = bi->bi_dev;
1324 inode->v.i_generation = bi->bi_generation;
1325 inode->v.i_size = bi->bi_size;
1327 inode->ei_flags = 0;
1328 inode->ei_quota_reserved = 0;
1329 inode->ei_qid = bch_qid(bi);
1330 inode->ei_subvol = inum.subvol;
1332 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1334 switch (inode->v.i_mode & S_IFMT) {
1336 inode->v.i_op = &bch_file_inode_operations;
1337 inode->v.i_fop = &bch_file_operations;
1340 inode->v.i_op = &bch_dir_inode_operations;
1341 inode->v.i_fop = &bch_dir_file_operations;
1344 inode_nohighmem(&inode->v);
1345 inode->v.i_op = &bch_symlink_inode_operations;
1348 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1349 inode->v.i_op = &bch_special_inode_operations;
1354 static struct inode *bch2_alloc_inode(struct super_block *sb)
1356 struct bch_inode_info *inode;
1358 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1362 inode_init_once(&inode->v);
1363 mutex_init(&inode->ei_update_lock);
1364 two_state_lock_init(&inode->ei_pagecache_lock);
1365 mutex_init(&inode->ei_quota_lock);
1370 static void bch2_i_callback(struct rcu_head *head)
1372 struct inode *vinode = container_of(head, struct inode, i_rcu);
1373 struct bch_inode_info *inode = to_bch_ei(vinode);
1375 kmem_cache_free(bch2_inode_cache, inode);
1378 static void bch2_destroy_inode(struct inode *vinode)
1380 call_rcu(&vinode->i_rcu, bch2_i_callback);
1383 static int inode_update_times_fn(struct bch_inode_info *inode,
1384 struct bch_inode_unpacked *bi,
1387 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1389 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1390 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1391 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1396 static int bch2_vfs_write_inode(struct inode *vinode,
1397 struct writeback_control *wbc)
1399 struct bch_fs *c = vinode->i_sb->s_fs_info;
1400 struct bch_inode_info *inode = to_bch_ei(vinode);
1403 mutex_lock(&inode->ei_update_lock);
1404 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1405 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1406 mutex_unlock(&inode->ei_update_lock);
1408 return bch2_err_class(ret);
1411 static void bch2_evict_inode(struct inode *vinode)
1413 struct bch_fs *c = vinode->i_sb->s_fs_info;
1414 struct bch_inode_info *inode = to_bch_ei(vinode);
1416 truncate_inode_pages_final(&inode->v.i_data);
1418 clear_inode(&inode->v);
1420 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1422 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1423 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1424 KEY_TYPE_QUOTA_WARN);
1425 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1426 KEY_TYPE_QUOTA_WARN);
1427 bch2_inode_rm(c, inode_inum(inode));
1431 void bch2_evict_subvolume_inodes(struct bch_fs *c,
1432 snapshot_id_list *s)
1434 struct super_block *sb = c->vfs_sb;
1435 struct inode *inode;
1437 spin_lock(&sb->s_inode_list_lock);
1438 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1439 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1440 (inode->i_state & I_FREEING))
1443 d_mark_dontcache(inode);
1444 d_prune_aliases(inode);
1446 spin_unlock(&sb->s_inode_list_lock);
1449 spin_lock(&sb->s_inode_list_lock);
1450 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1451 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1452 (inode->i_state & I_FREEING))
1455 if (!(inode->i_state & I_DONTCACHE)) {
1456 d_mark_dontcache(inode);
1457 d_prune_aliases(inode);
1460 spin_lock(&inode->i_lock);
1461 if (snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) &&
1462 !(inode->i_state & I_FREEING)) {
1463 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_NEW);
1464 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1465 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1466 spin_unlock(&inode->i_lock);
1467 spin_unlock(&sb->s_inode_list_lock);
1469 finish_wait(wq, &wait.wq_entry);
1473 spin_unlock(&inode->i_lock);
1475 spin_unlock(&sb->s_inode_list_lock);
1478 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1480 struct super_block *sb = dentry->d_sb;
1481 struct bch_fs *c = sb->s_fs_info;
1482 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1483 unsigned shift = sb->s_blocksize_bits - 9;
1485 * this assumes inodes take up 64 bytes, which is a decent average
1488 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1491 buf->f_type = BCACHEFS_STATFS_MAGIC;
1492 buf->f_bsize = sb->s_blocksize;
1493 buf->f_blocks = usage.capacity >> shift;
1494 buf->f_bfree = usage.free >> shift;
1495 buf->f_bavail = avail_factor(usage.free) >> shift;
1497 buf->f_files = usage.nr_inodes + avail_inodes;
1498 buf->f_ffree = avail_inodes;
1500 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1501 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1502 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1503 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1504 buf->f_namelen = BCH_NAME_MAX;
1509 static int bch2_sync_fs(struct super_block *sb, int wait)
1511 struct bch_fs *c = sb->s_fs_info;
1514 if (c->opts.journal_flush_disabled)
1518 bch2_journal_flush_async(&c->journal, NULL);
1522 ret = bch2_journal_flush(&c->journal);
1523 return bch2_err_class(ret);
1526 static struct bch_fs *bch2_path_to_fs(const char *path)
1532 ret = lookup_bdev(path, &dev);
1534 return ERR_PTR(ret);
1536 c = bch2_dev_to_fs(dev);
1538 closure_put(&c->cl);
1539 return c ?: ERR_PTR(-ENOENT);
1542 static char **split_devs(const char *_dev_name, unsigned *nr)
1544 char *dev_name = NULL, **devs = NULL, *s;
1545 size_t i, nr_devs = 0;
1547 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1551 for (s = dev_name; s; s = strchr(s + 1, ':'))
1554 devs = kcalloc(nr_devs + 1, sizeof(const char *), GFP_KERNEL);
1560 for (i = 0, s = dev_name;
1562 (s = strchr(s, ':')) && (*s++ = '\0'))
1569 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1571 struct bch_fs *c = sb->s_fs_info;
1572 struct bch_opts opts = bch2_opts_empty();
1575 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1577 ret = bch2_parse_mount_opts(c, &opts, data);
1581 if (opts.read_only != c->opts.read_only) {
1582 down_write(&c->state_lock);
1584 if (opts.read_only) {
1585 bch2_fs_read_only(c);
1587 sb->s_flags |= SB_RDONLY;
1589 ret = bch2_fs_read_write(c);
1591 bch_err(c, "error going rw: %i", ret);
1592 up_write(&c->state_lock);
1597 sb->s_flags &= ~SB_RDONLY;
1600 c->opts.read_only = opts.read_only;
1602 up_write(&c->state_lock);
1605 if (opts.errors >= 0)
1606 c->opts.errors = opts.errors;
1608 return bch2_err_class(ret);
1611 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1613 struct bch_fs *c = root->d_sb->s_fs_info;
1618 for_each_online_member(ca, c, i) {
1622 seq_puts(seq, "/dev/");
1623 seq_puts(seq, ca->name);
1629 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1631 struct bch_fs *c = root->d_sb->s_fs_info;
1633 struct printbuf buf = PRINTBUF;
1636 for (i = 0; i < bch2_opts_nr; i++) {
1637 const struct bch_option *opt = &bch2_opt_table[i];
1638 u64 v = bch2_opt_get_by_id(&c->opts, i);
1640 if (!(opt->flags & OPT_MOUNT))
1643 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1646 printbuf_reset(&buf);
1647 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1648 OPT_SHOW_MOUNT_STYLE);
1650 seq_puts(seq, buf.buf);
1653 if (buf.allocation_failure)
1655 printbuf_exit(&buf);
1659 static void bch2_put_super(struct super_block *sb)
1661 struct bch_fs *c = sb->s_fs_info;
1666 static const struct super_operations bch_super_operations = {
1667 .alloc_inode = bch2_alloc_inode,
1668 .destroy_inode = bch2_destroy_inode,
1669 .write_inode = bch2_vfs_write_inode,
1670 .evict_inode = bch2_evict_inode,
1671 .sync_fs = bch2_sync_fs,
1672 .statfs = bch2_statfs,
1673 .show_devname = bch2_show_devname,
1674 .show_options = bch2_show_options,
1675 .remount_fs = bch2_remount,
1676 .put_super = bch2_put_super,
1678 .freeze_fs = bch2_freeze,
1679 .unfreeze_fs = bch2_unfreeze,
1683 static int bch2_set_super(struct super_block *s, void *data)
1685 s->s_fs_info = data;
1689 static int bch2_noset_super(struct super_block *s, void *data)
1694 static int bch2_test_super(struct super_block *s, void *data)
1696 struct bch_fs *c = s->s_fs_info;
1697 struct bch_fs **devs = data;
1703 for (i = 0; devs[i]; i++)
1709 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1710 int flags, const char *dev_name, void *data)
1714 struct super_block *sb;
1715 struct inode *vinode;
1716 struct bch_opts opts = bch2_opts_empty();
1718 struct bch_fs **devs_to_fs = NULL;
1719 unsigned i, nr_devs;
1722 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1724 ret = bch2_parse_mount_opts(NULL, &opts, data);
1726 return ERR_PTR(ret);
1728 if (!dev_name || strlen(dev_name) == 0)
1729 return ERR_PTR(-EINVAL);
1731 devs = split_devs(dev_name, &nr_devs);
1733 return ERR_PTR(-ENOMEM);
1735 devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
1737 sb = ERR_PTR(-ENOMEM);
1741 for (i = 0; i < nr_devs; i++)
1742 devs_to_fs[i] = bch2_path_to_fs(devs[i]);
1744 sb = sget(fs_type, bch2_test_super, bch2_noset_super,
1745 flags|SB_NOSEC, devs_to_fs);
1749 c = bch2_fs_open(devs, nr_devs, opts);
1755 /* Some options can't be parsed until after the fs is started: */
1756 ret = bch2_parse_mount_opts(c, &opts, data);
1763 bch2_opts_apply(&c->opts, opts);
1765 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1775 ret = bch2_err_class(ret);
1776 return ERR_PTR(ret);
1782 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1789 sb->s_blocksize = block_bytes(c);
1790 sb->s_blocksize_bits = ilog2(block_bytes(c));
1791 sb->s_maxbytes = MAX_LFS_FILESIZE;
1792 sb->s_op = &bch_super_operations;
1793 sb->s_export_op = &bch_export_ops;
1794 #ifdef CONFIG_BCACHEFS_QUOTA
1795 sb->s_qcop = &bch2_quotactl_operations;
1796 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1798 sb->s_xattr = bch2_xattr_handlers;
1799 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1800 sb->s_time_gran = c->sb.nsec_per_time_unit;
1801 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1802 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1804 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
1806 ret = super_setup_bdi(sb);
1810 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1812 for_each_online_member(ca, c, i) {
1813 struct block_device *bdev = ca->disk_sb.bdev;
1815 /* XXX: create an anonymous device for multi device filesystems */
1817 sb->s_dev = bdev->bd_dev;
1818 percpu_ref_put(&ca->io_ref);
1824 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1826 sb->s_flags |= SB_POSIXACL;
1829 sb->s_shrink.seeks = 0;
1831 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1832 ret = PTR_ERR_OR_ZERO(vinode);
1834 bch_err(c, "error mounting: error getting root inode: %s", bch2_err_str(ret));
1838 sb->s_root = d_make_root(vinode);
1840 bch_err(c, "error mounting: error allocating root dentry");
1845 sb->s_flags |= SB_ACTIVE;
1847 return dget(sb->s_root);
1850 deactivate_locked_super(sb);
1851 return ERR_PTR(ret);
1854 static void bch2_kill_sb(struct super_block *sb)
1856 struct bch_fs *c = sb->s_fs_info;
1858 generic_shutdown_super(sb);
1862 static struct file_system_type bcache_fs_type = {
1863 .owner = THIS_MODULE,
1865 .mount = bch2_mount,
1866 .kill_sb = bch2_kill_sb,
1867 .fs_flags = FS_REQUIRES_DEV,
1870 MODULE_ALIAS_FS("bcachefs");
1872 void bch2_vfs_exit(void)
1874 unregister_filesystem(&bcache_fs_type);
1875 kmem_cache_destroy(bch2_inode_cache);
1878 int __init bch2_vfs_init(void)
1882 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1883 if (!bch2_inode_cache)
1886 ret = register_filesystem(&bcache_fs_type);
1896 #endif /* NO_BCACHEFS_FS */