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 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
181 struct bch_inode_unpacked inode_u;
182 struct bch_inode_info *inode;
183 struct btree_trans *trans;
184 struct bch_subvolume subvol;
187 inode = to_bch_ei(iget5_locked(c->vfs_sb,
188 bch2_inode_hash(inum),
192 if (unlikely(!inode))
193 return ERR_PTR(-ENOMEM);
194 if (!(inode->v.i_state & I_NEW))
197 trans = bch2_trans_get(c);
198 ret = lockrestart_do(trans,
199 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
200 bch2_inode_find_by_inum_trans(trans, inum, &inode_u));
203 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
204 bch2_trans_put(trans);
207 iget_failed(&inode->v);
208 return ERR_PTR(bch2_err_class(ret));
211 mutex_lock(&c->vfs_inodes_lock);
212 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
213 mutex_unlock(&c->vfs_inodes_lock);
215 unlock_new_inode(&inode->v);
220 struct bch_inode_info *
221 __bch2_create(struct mnt_idmap *idmap,
222 struct bch_inode_info *dir, struct dentry *dentry,
223 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
226 struct bch_fs *c = dir->v.i_sb->s_fs_info;
227 struct btree_trans *trans;
228 struct bch_inode_unpacked dir_u;
229 struct bch_inode_info *inode, *old;
230 struct bch_inode_unpacked inode_u;
231 struct posix_acl *default_acl = NULL, *acl = NULL;
233 struct bch_subvolume subvol;
238 * preallocate acls + vfs inode before btree transaction, so that
239 * nothing can fail after the transaction succeeds:
241 #ifdef CONFIG_BCACHEFS_POSIX_ACL
242 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
246 inode = to_bch_ei(new_inode(c->vfs_sb));
247 if (unlikely(!inode)) {
248 inode = ERR_PTR(-ENOMEM);
252 bch2_inode_init_early(c, &inode_u);
254 if (!(flags & BCH_CREATE_TMPFILE))
255 mutex_lock(&dir->ei_update_lock);
257 trans = bch2_trans_get(c);
259 bch2_trans_begin(trans);
261 ret = bch2_create_trans(trans,
262 inode_inum(dir), &dir_u, &inode_u,
263 !(flags & BCH_CREATE_TMPFILE)
264 ? &dentry->d_name : NULL,
265 from_kuid(i_user_ns(&dir->v), current_fsuid()),
266 from_kgid(i_user_ns(&dir->v), current_fsgid()),
268 default_acl, acl, snapshot_src, flags) ?:
269 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
270 KEY_TYPE_QUOTA_PREALLOC);
272 goto err_before_quota;
274 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
275 inum.inum = inode_u.bi_inum;
277 ret = bch2_subvolume_get(trans, inum.subvol, true,
278 BTREE_ITER_WITH_UPDATES, &subvol) ?:
279 bch2_trans_commit(trans, NULL, &journal_seq, 0);
281 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
282 KEY_TYPE_QUOTA_WARN);
284 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
289 if (!(flags & BCH_CREATE_TMPFILE)) {
290 bch2_inode_update_after_write(trans, dir, &dir_u,
291 ATTR_MTIME|ATTR_CTIME);
292 mutex_unlock(&dir->ei_update_lock);
295 bch2_iget5_set(&inode->v, &inum);
296 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
298 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
299 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
302 * we must insert the new inode into the inode cache before calling
303 * bch2_trans_exit() and dropping locks, else we could race with another
304 * thread pulling the inode in and modifying it:
307 inode->v.i_state |= I_CREATING;
309 old = to_bch_ei(inode_insert5(&inode->v,
310 bch2_inode_hash(inum),
316 if (unlikely(old != inode)) {
318 * We raced, another process pulled the new inode into cache
321 make_bad_inode(&inode->v);
326 mutex_lock(&c->vfs_inodes_lock);
327 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
328 mutex_unlock(&c->vfs_inodes_lock);
330 * we really don't want insert_inode_locked2() to be setting
333 unlock_new_inode(&inode->v);
336 bch2_trans_put(trans);
338 posix_acl_release(default_acl);
339 posix_acl_release(acl);
342 if (!(flags & BCH_CREATE_TMPFILE))
343 mutex_unlock(&dir->ei_update_lock);
345 bch2_trans_put(trans);
346 make_bad_inode(&inode->v);
348 inode = ERR_PTR(ret);
354 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
357 struct bch_fs *c = vdir->i_sb->s_fs_info;
358 struct bch_inode_info *dir = to_bch_ei(vdir);
359 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
360 struct inode *vinode = NULL;
361 subvol_inum inum = { .subvol = 1 };
364 ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
365 &dentry->d_name, &inum);
368 vinode = bch2_vfs_inode_get(c, inum);
370 return d_splice_alias(vinode, dentry);
373 static int bch2_mknod(struct mnt_idmap *idmap,
374 struct inode *vdir, struct dentry *dentry,
375 umode_t mode, dev_t rdev)
377 struct bch_inode_info *inode =
378 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
379 (subvol_inum) { 0 }, 0);
382 return bch2_err_class(PTR_ERR(inode));
384 d_instantiate(dentry, &inode->v);
388 static int bch2_create(struct mnt_idmap *idmap,
389 struct inode *vdir, struct dentry *dentry,
390 umode_t mode, bool excl)
392 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
395 static int __bch2_link(struct bch_fs *c,
396 struct bch_inode_info *inode,
397 struct bch_inode_info *dir,
398 struct dentry *dentry)
400 struct btree_trans *trans = bch2_trans_get(c);
401 struct bch_inode_unpacked dir_u, inode_u;
404 mutex_lock(&inode->ei_update_lock);
406 ret = commit_do(trans, NULL, NULL, 0,
407 bch2_link_trans(trans,
408 inode_inum(dir), &dir_u,
409 inode_inum(inode), &inode_u,
413 bch2_inode_update_after_write(trans, dir, &dir_u,
414 ATTR_MTIME|ATTR_CTIME);
415 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
418 bch2_trans_put(trans);
419 mutex_unlock(&inode->ei_update_lock);
423 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
424 struct dentry *dentry)
426 struct bch_fs *c = vdir->i_sb->s_fs_info;
427 struct bch_inode_info *dir = to_bch_ei(vdir);
428 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
431 lockdep_assert_held(&inode->v.i_rwsem);
433 ret = __bch2_link(c, inode, dir, dentry);
438 d_instantiate(dentry, &inode->v);
442 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
443 bool deleting_snapshot)
445 struct bch_fs *c = vdir->i_sb->s_fs_info;
446 struct bch_inode_info *dir = to_bch_ei(vdir);
447 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
448 struct bch_inode_unpacked dir_u, inode_u;
449 struct btree_trans *trans = bch2_trans_get(c);
452 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
454 ret = commit_do(trans, NULL, NULL,
455 BCH_TRANS_COMMIT_no_enospc,
456 bch2_unlink_trans(trans,
457 inode_inum(dir), &dir_u,
458 &inode_u, &dentry->d_name,
463 bch2_inode_update_after_write(trans, dir, &dir_u,
464 ATTR_MTIME|ATTR_CTIME);
465 bch2_inode_update_after_write(trans, inode, &inode_u,
468 if (inode_u.bi_subvol) {
470 * Subvolume deletion is asynchronous, but we still want to tell
471 * the VFS that it's been deleted here:
473 set_nlink(&inode->v, 0);
476 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
477 bch2_trans_put(trans);
482 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
484 return __bch2_unlink(vdir, dentry, false);
487 static int bch2_symlink(struct mnt_idmap *idmap,
488 struct inode *vdir, struct dentry *dentry,
491 struct bch_fs *c = vdir->i_sb->s_fs_info;
492 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
495 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
496 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
498 return bch2_err_class(PTR_ERR(inode));
500 inode_lock(&inode->v);
501 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
502 inode_unlock(&inode->v);
507 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
511 ret = __bch2_link(c, inode, dir, dentry);
515 d_instantiate(dentry, &inode->v);
522 static int bch2_mkdir(struct mnt_idmap *idmap,
523 struct inode *vdir, struct dentry *dentry, umode_t mode)
525 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
528 static int bch2_rename2(struct mnt_idmap *idmap,
529 struct inode *src_vdir, struct dentry *src_dentry,
530 struct inode *dst_vdir, struct dentry *dst_dentry,
533 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
534 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
535 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
536 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
537 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
538 struct bch_inode_unpacked dst_dir_u, src_dir_u;
539 struct bch_inode_unpacked src_inode_u, dst_inode_u;
540 struct btree_trans *trans;
541 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
542 ? BCH_RENAME_EXCHANGE
543 : dst_dentry->d_inode
544 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
547 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
550 if (mode == BCH_RENAME_OVERWRITE) {
551 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
557 trans = bch2_trans_get(c);
559 bch2_lock_inodes(INODE_UPDATE_LOCK,
565 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
566 ret = bch2_fs_quota_transfer(c, src_inode,
569 KEY_TYPE_QUOTA_PREALLOC);
574 if (mode == BCH_RENAME_EXCHANGE &&
575 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
576 ret = bch2_fs_quota_transfer(c, dst_inode,
579 KEY_TYPE_QUOTA_PREALLOC);
584 ret = commit_do(trans, NULL, NULL, 0,
585 bch2_rename_trans(trans,
586 inode_inum(src_dir), &src_dir_u,
587 inode_inum(dst_dir), &dst_dir_u,
596 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
598 dst_inode->v.i_ino != dst_inode_u.bi_inum);
600 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
601 ATTR_MTIME|ATTR_CTIME);
603 if (src_dir != dst_dir)
604 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
605 ATTR_MTIME|ATTR_CTIME);
607 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
611 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
614 bch2_trans_put(trans);
616 bch2_fs_quota_transfer(c, src_inode,
617 bch_qid(&src_inode->ei_inode),
619 KEY_TYPE_QUOTA_NOCHECK);
621 bch2_fs_quota_transfer(c, dst_inode,
622 bch_qid(&dst_inode->ei_inode),
624 KEY_TYPE_QUOTA_NOCHECK);
626 bch2_unlock_inodes(INODE_UPDATE_LOCK,
635 static void bch2_setattr_copy(struct mnt_idmap *idmap,
636 struct bch_inode_info *inode,
637 struct bch_inode_unpacked *bi,
640 struct bch_fs *c = inode->v.i_sb->s_fs_info;
641 unsigned int ia_valid = attr->ia_valid;
643 if (ia_valid & ATTR_UID)
644 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
645 if (ia_valid & ATTR_GID)
646 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
648 if (ia_valid & ATTR_SIZE)
649 bi->bi_size = attr->ia_size;
651 if (ia_valid & ATTR_ATIME)
652 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
653 if (ia_valid & ATTR_MTIME)
654 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
655 if (ia_valid & ATTR_CTIME)
656 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
658 if (ia_valid & ATTR_MODE) {
659 umode_t mode = attr->ia_mode;
660 kgid_t gid = ia_valid & ATTR_GID
664 if (!in_group_p(gid) &&
665 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
671 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
672 struct bch_inode_info *inode,
675 struct bch_fs *c = inode->v.i_sb->s_fs_info;
677 struct btree_trans *trans;
678 struct btree_iter inode_iter = { NULL };
679 struct bch_inode_unpacked inode_u;
680 struct posix_acl *acl = NULL;
683 mutex_lock(&inode->ei_update_lock);
687 if (attr->ia_valid & ATTR_UID)
688 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
690 if (attr->ia_valid & ATTR_GID)
691 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
693 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
694 KEY_TYPE_QUOTA_PREALLOC);
698 trans = bch2_trans_get(c);
700 bch2_trans_begin(trans);
704 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
709 bch2_setattr_copy(idmap, inode, &inode_u, attr);
711 if (attr->ia_valid & ATTR_MODE) {
712 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
713 inode_u.bi_mode, &acl);
718 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
719 bch2_trans_commit(trans, NULL, NULL,
720 BCH_TRANS_COMMIT_no_enospc);
722 bch2_trans_iter_exit(trans, &inode_iter);
724 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
729 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
732 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
734 bch2_trans_put(trans);
736 mutex_unlock(&inode->ei_update_lock);
738 return bch2_err_class(ret);
741 static int bch2_getattr(struct mnt_idmap *idmap,
742 const struct path *path, struct kstat *stat,
743 u32 request_mask, unsigned query_flags)
745 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
746 struct bch_fs *c = inode->v.i_sb->s_fs_info;
748 stat->dev = inode->v.i_sb->s_dev;
749 stat->ino = inode->v.i_ino;
750 stat->mode = inode->v.i_mode;
751 stat->nlink = inode->v.i_nlink;
752 stat->uid = inode->v.i_uid;
753 stat->gid = inode->v.i_gid;
754 stat->rdev = inode->v.i_rdev;
755 stat->size = i_size_read(&inode->v);
756 stat->atime = inode_get_atime(&inode->v);
757 stat->mtime = inode_get_mtime(&inode->v);
758 stat->ctime = inode_get_ctime(&inode->v);
759 stat->blksize = block_bytes(c);
760 stat->blocks = inode->v.i_blocks;
762 if (request_mask & STATX_BTIME) {
763 stat->result_mask |= STATX_BTIME;
764 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
767 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
768 stat->attributes |= STATX_ATTR_IMMUTABLE;
769 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
771 if (inode->ei_inode.bi_flags & BCH_INODE_append)
772 stat->attributes |= STATX_ATTR_APPEND;
773 stat->attributes_mask |= STATX_ATTR_APPEND;
775 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
776 stat->attributes |= STATX_ATTR_NODUMP;
777 stat->attributes_mask |= STATX_ATTR_NODUMP;
782 static int bch2_setattr(struct mnt_idmap *idmap,
783 struct dentry *dentry, struct iattr *iattr)
785 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
788 lockdep_assert_held(&inode->v.i_rwsem);
790 ret = setattr_prepare(idmap, dentry, iattr);
794 return iattr->ia_valid & ATTR_SIZE
795 ? bchfs_truncate(idmap, inode, iattr)
796 : bch2_setattr_nonsize(idmap, inode, iattr);
799 static int bch2_tmpfile(struct mnt_idmap *idmap,
800 struct inode *vdir, struct file *file, umode_t mode)
802 struct bch_inode_info *inode =
803 __bch2_create(idmap, to_bch_ei(vdir),
804 file->f_path.dentry, mode, 0,
805 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
808 return bch2_err_class(PTR_ERR(inode));
810 d_mark_tmpfile(file, &inode->v);
811 d_instantiate(file->f_path.dentry, &inode->v);
812 return finish_open_simple(file, 0);
815 static int bch2_fill_extent(struct bch_fs *c,
816 struct fiemap_extent_info *info,
817 struct bkey_s_c k, unsigned flags)
819 if (bkey_extent_is_direct_data(k.k)) {
820 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
821 const union bch_extent_entry *entry;
822 struct extent_ptr_decoded p;
825 if (k.k->type == KEY_TYPE_reflink_v)
826 flags |= FIEMAP_EXTENT_SHARED;
828 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
830 u64 offset = p.ptr.offset;
833 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
835 if (p.crc.compression_type)
836 flags2 |= FIEMAP_EXTENT_ENCODED;
838 offset += p.crc.offset;
840 if ((offset & (block_sectors(c) - 1)) ||
841 (k.k->size & (block_sectors(c) - 1)))
842 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
844 ret = fiemap_fill_next_extent(info,
845 bkey_start_offset(k.k) << 9,
847 k.k->size << 9, flags|flags2);
853 } else if (bkey_extent_is_inline_data(k.k)) {
854 return fiemap_fill_next_extent(info,
855 bkey_start_offset(k.k) << 9,
858 FIEMAP_EXTENT_DATA_INLINE);
859 } else if (k.k->type == KEY_TYPE_reservation) {
860 return fiemap_fill_next_extent(info,
861 bkey_start_offset(k.k) << 9,
864 FIEMAP_EXTENT_DELALLOC|
865 FIEMAP_EXTENT_UNWRITTEN);
871 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
874 struct bch_fs *c = vinode->i_sb->s_fs_info;
875 struct bch_inode_info *ei = to_bch_ei(vinode);
876 struct btree_trans *trans;
877 struct btree_iter iter;
879 struct bkey_buf cur, prev;
880 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
881 unsigned offset_into_extent, sectors;
882 bool have_extent = false;
886 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
890 if (start + len < start)
895 bch2_bkey_buf_init(&cur);
896 bch2_bkey_buf_init(&prev);
897 trans = bch2_trans_get(c);
899 bch2_trans_begin(trans);
901 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot);
905 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
906 SPOS(ei->v.i_ino, start, snapshot), 0);
908 while (!(ret = btree_trans_too_many_iters(trans)) &&
909 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
910 !(ret = bkey_err(k))) {
911 enum btree_id data_btree = BTREE_ID_extents;
913 if (!bkey_extent_is_data(k.k) &&
914 k.k->type != KEY_TYPE_reservation) {
915 bch2_btree_iter_advance(&iter);
919 offset_into_extent = iter.pos.offset -
920 bkey_start_offset(k.k);
921 sectors = k.k->size - offset_into_extent;
923 bch2_bkey_buf_reassemble(&cur, c, k);
925 ret = bch2_read_indirect_extent(trans, &data_btree,
926 &offset_into_extent, &cur);
930 k = bkey_i_to_s_c(cur.k);
931 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
933 sectors = min(sectors, k.k->size - offset_into_extent);
935 bch2_cut_front(POS(k.k->p.inode,
936 bkey_start_offset(k.k) +
939 bch2_key_resize(&cur.k->k, sectors);
940 cur.k->k.p = iter.pos;
941 cur.k->k.p.offset += cur.k->k.size;
944 bch2_trans_unlock(trans);
945 ret = bch2_fill_extent(c, info,
946 bkey_i_to_s_c(prev.k), 0);
951 bkey_copy(prev.k, cur.k);
954 bch2_btree_iter_set_pos(&iter,
955 POS(iter.pos.inode, iter.pos.offset + sectors));
957 start = iter.pos.offset;
958 bch2_trans_iter_exit(trans, &iter);
960 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
963 if (!ret && have_extent) {
964 bch2_trans_unlock(trans);
965 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
969 bch2_trans_put(trans);
970 bch2_bkey_buf_exit(&cur, c);
971 bch2_bkey_buf_exit(&prev, c);
972 return ret < 0 ? ret : 0;
975 static const struct vm_operations_struct bch_vm_ops = {
976 .fault = bch2_page_fault,
977 .map_pages = filemap_map_pages,
978 .page_mkwrite = bch2_page_mkwrite,
981 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
985 vma->vm_ops = &bch_vm_ops;
991 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
993 return generic_file_llseek_size(file, offset, whence,
997 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
999 struct bch_inode_info *inode = file_bch_inode(file);
1000 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1002 if (!dir_emit_dots(file, ctx))
1005 int ret = bch2_readdir(c, inode_inum(inode), ctx);
1008 return bch2_err_class(ret);
1011 static const struct file_operations bch_file_operations = {
1012 .llseek = bch2_llseek,
1013 .read_iter = bch2_read_iter,
1014 .write_iter = bch2_write_iter,
1016 .open = generic_file_open,
1017 .fsync = bch2_fsync,
1018 .splice_read = filemap_splice_read,
1019 .splice_write = iter_file_splice_write,
1020 .fallocate = bch2_fallocate_dispatch,
1021 .unlocked_ioctl = bch2_fs_file_ioctl,
1022 #ifdef CONFIG_COMPAT
1023 .compat_ioctl = bch2_compat_fs_ioctl,
1025 .remap_file_range = bch2_remap_file_range,
1028 static const struct inode_operations bch_file_inode_operations = {
1029 .getattr = bch2_getattr,
1030 .setattr = bch2_setattr,
1031 .fiemap = bch2_fiemap,
1032 .listxattr = bch2_xattr_list,
1033 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1034 .get_acl = bch2_get_acl,
1035 .set_acl = bch2_set_acl,
1039 static const struct inode_operations bch_dir_inode_operations = {
1040 .lookup = bch2_lookup,
1041 .create = bch2_create,
1043 .unlink = bch2_unlink,
1044 .symlink = bch2_symlink,
1045 .mkdir = bch2_mkdir,
1046 .rmdir = bch2_unlink,
1047 .mknod = bch2_mknod,
1048 .rename = bch2_rename2,
1049 .getattr = bch2_getattr,
1050 .setattr = bch2_setattr,
1051 .tmpfile = bch2_tmpfile,
1052 .listxattr = bch2_xattr_list,
1053 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1054 .get_acl = bch2_get_acl,
1055 .set_acl = bch2_set_acl,
1059 static const struct file_operations bch_dir_file_operations = {
1060 .llseek = bch2_dir_llseek,
1061 .read = generic_read_dir,
1062 .iterate_shared = bch2_vfs_readdir,
1063 .fsync = bch2_fsync,
1064 .unlocked_ioctl = bch2_fs_file_ioctl,
1065 #ifdef CONFIG_COMPAT
1066 .compat_ioctl = bch2_compat_fs_ioctl,
1070 static const struct inode_operations bch_symlink_inode_operations = {
1071 .get_link = page_get_link,
1072 .getattr = bch2_getattr,
1073 .setattr = bch2_setattr,
1074 .listxattr = bch2_xattr_list,
1075 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1076 .get_acl = bch2_get_acl,
1077 .set_acl = bch2_set_acl,
1081 static const struct inode_operations bch_special_inode_operations = {
1082 .getattr = bch2_getattr,
1083 .setattr = bch2_setattr,
1084 .listxattr = bch2_xattr_list,
1085 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1086 .get_acl = bch2_get_acl,
1087 .set_acl = bch2_set_acl,
1091 static const struct address_space_operations bch_address_space_operations = {
1092 .read_folio = bch2_read_folio,
1093 .writepages = bch2_writepages,
1094 .readahead = bch2_readahead,
1095 .dirty_folio = filemap_dirty_folio,
1096 .write_begin = bch2_write_begin,
1097 .write_end = bch2_write_end,
1098 .invalidate_folio = bch2_invalidate_folio,
1099 .release_folio = bch2_release_folio,
1100 .direct_IO = noop_direct_IO,
1101 #ifdef CONFIG_MIGRATION
1102 .migrate_folio = filemap_migrate_folio,
1104 .error_remove_page = generic_error_remove_page,
1107 struct bcachefs_fid {
1113 struct bcachefs_fid_with_parent {
1114 struct bcachefs_fid fid;
1115 struct bcachefs_fid dir;
1118 static int bcachefs_fid_valid(int fh_len, int fh_type)
1121 case FILEID_BCACHEFS_WITHOUT_PARENT:
1122 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1123 case FILEID_BCACHEFS_WITH_PARENT:
1124 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1130 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1132 return (struct bcachefs_fid) {
1133 .inum = inode->ei_inode.bi_inum,
1134 .subvol = inode->ei_subvol,
1135 .gen = inode->ei_inode.bi_generation,
1139 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1142 struct bch_inode_info *inode = to_bch_ei(vinode);
1143 struct bch_inode_info *dir = to_bch_ei(vdir);
1146 if (!S_ISDIR(inode->v.i_mode) && dir) {
1147 struct bcachefs_fid_with_parent *fid = (void *) fh;
1149 min_len = sizeof(*fid) / sizeof(u32);
1150 if (*len < min_len) {
1152 return FILEID_INVALID;
1155 fid->fid = bch2_inode_to_fid(inode);
1156 fid->dir = bch2_inode_to_fid(dir);
1159 return FILEID_BCACHEFS_WITH_PARENT;
1161 struct bcachefs_fid *fid = (void *) fh;
1163 min_len = sizeof(*fid) / sizeof(u32);
1164 if (*len < min_len) {
1166 return FILEID_INVALID;
1168 *fid = bch2_inode_to_fid(inode);
1171 return FILEID_BCACHEFS_WITHOUT_PARENT;
1175 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1176 struct bcachefs_fid fid)
1178 struct bch_fs *c = sb->s_fs_info;
1179 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1180 .subvol = fid.subvol,
1183 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1185 vinode = ERR_PTR(-ESTALE);
1190 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1191 int fh_len, int fh_type)
1193 struct bcachefs_fid *fid = (void *) _fid;
1195 if (!bcachefs_fid_valid(fh_len, fh_type))
1198 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1201 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1202 int fh_len, int fh_type)
1204 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1206 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1207 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1210 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1213 static struct dentry *bch2_get_parent(struct dentry *child)
1215 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1216 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1217 subvol_inum parent_inum = {
1218 .subvol = inode->ei_inode.bi_parent_subvol ?:
1220 .inum = inode->ei_inode.bi_dir,
1223 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1226 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1228 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1229 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1230 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1231 struct btree_trans *trans;
1232 struct btree_iter iter1;
1233 struct btree_iter iter2;
1235 struct bkey_s_c_dirent d;
1236 struct bch_inode_unpacked inode_u;
1239 struct qstr dirent_name;
1240 unsigned name_len = 0;
1243 if (!S_ISDIR(dir->v.i_mode))
1246 trans = bch2_trans_get(c);
1248 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1249 POS(dir->ei_inode.bi_inum, 0), 0);
1250 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1251 POS(dir->ei_inode.bi_inum, 0), 0);
1253 bch2_trans_begin(trans);
1255 ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot);
1259 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1260 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1262 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1266 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1267 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1269 k = bch2_btree_iter_peek_slot(&iter1);
1274 if (k.k->type != KEY_TYPE_dirent) {
1275 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1279 d = bkey_s_c_to_dirent(k);
1280 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1282 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1286 if (target.subvol == inode->ei_subvol &&
1287 target.inum == inode->ei_inode.bi_inum)
1291 * File with multiple hardlinks and our backref is to the wrong
1292 * directory - linear search:
1294 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1295 if (k.k->p.inode > dir->ei_inode.bi_inum)
1298 if (k.k->type != KEY_TYPE_dirent)
1301 d = bkey_s_c_to_dirent(k);
1302 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1308 if (target.subvol == inode->ei_subvol &&
1309 target.inum == inode->ei_inode.bi_inum)
1317 dirent_name = bch2_dirent_get_name(d);
1319 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
1320 memcpy(name, dirent_name.name, name_len);
1321 name[name_len] = '\0';
1323 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1326 bch2_trans_iter_exit(trans, &iter1);
1327 bch2_trans_iter_exit(trans, &iter2);
1328 bch2_trans_put(trans);
1333 static const struct export_operations bch_export_ops = {
1334 .encode_fh = bch2_encode_fh,
1335 .fh_to_dentry = bch2_fh_to_dentry,
1336 .fh_to_parent = bch2_fh_to_parent,
1337 .get_parent = bch2_get_parent,
1338 .get_name = bch2_get_name,
1341 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1342 struct bch_inode_info *inode,
1343 struct bch_inode_unpacked *bi,
1344 struct bch_subvolume *subvol)
1346 bch2_inode_update_after_write(trans, inode, bi, ~0);
1348 if (BCH_SUBVOLUME_SNAP(subvol))
1349 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1351 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1353 inode->v.i_blocks = bi->bi_sectors;
1354 inode->v.i_ino = bi->bi_inum;
1355 inode->v.i_rdev = bi->bi_dev;
1356 inode->v.i_generation = bi->bi_generation;
1357 inode->v.i_size = bi->bi_size;
1359 inode->ei_flags = 0;
1360 inode->ei_quota_reserved = 0;
1361 inode->ei_qid = bch_qid(bi);
1362 inode->ei_subvol = inum.subvol;
1364 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1366 switch (inode->v.i_mode & S_IFMT) {
1368 inode->v.i_op = &bch_file_inode_operations;
1369 inode->v.i_fop = &bch_file_operations;
1372 inode->v.i_op = &bch_dir_inode_operations;
1373 inode->v.i_fop = &bch_dir_file_operations;
1376 inode_nohighmem(&inode->v);
1377 inode->v.i_op = &bch_symlink_inode_operations;
1380 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1381 inode->v.i_op = &bch_special_inode_operations;
1385 mapping_set_large_folios(inode->v.i_mapping);
1388 static struct inode *bch2_alloc_inode(struct super_block *sb)
1390 struct bch_inode_info *inode;
1392 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1396 inode_init_once(&inode->v);
1397 mutex_init(&inode->ei_update_lock);
1398 two_state_lock_init(&inode->ei_pagecache_lock);
1399 INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
1400 mutex_init(&inode->ei_quota_lock);
1405 static void bch2_i_callback(struct rcu_head *head)
1407 struct inode *vinode = container_of(head, struct inode, i_rcu);
1408 struct bch_inode_info *inode = to_bch_ei(vinode);
1410 kmem_cache_free(bch2_inode_cache, inode);
1413 static void bch2_destroy_inode(struct inode *vinode)
1415 call_rcu(&vinode->i_rcu, bch2_i_callback);
1418 static int inode_update_times_fn(struct btree_trans *trans,
1419 struct bch_inode_info *inode,
1420 struct bch_inode_unpacked *bi,
1423 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1425 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
1426 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
1427 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
1432 static int bch2_vfs_write_inode(struct inode *vinode,
1433 struct writeback_control *wbc)
1435 struct bch_fs *c = vinode->i_sb->s_fs_info;
1436 struct bch_inode_info *inode = to_bch_ei(vinode);
1439 mutex_lock(&inode->ei_update_lock);
1440 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1441 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1442 mutex_unlock(&inode->ei_update_lock);
1444 return bch2_err_class(ret);
1447 static void bch2_evict_inode(struct inode *vinode)
1449 struct bch_fs *c = vinode->i_sb->s_fs_info;
1450 struct bch_inode_info *inode = to_bch_ei(vinode);
1452 truncate_inode_pages_final(&inode->v.i_data);
1454 clear_inode(&inode->v);
1456 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1458 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1459 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1460 KEY_TYPE_QUOTA_WARN);
1461 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1462 KEY_TYPE_QUOTA_WARN);
1463 bch2_inode_rm(c, inode_inum(inode));
1466 mutex_lock(&c->vfs_inodes_lock);
1467 list_del_init(&inode->ei_vfs_inode_list);
1468 mutex_unlock(&c->vfs_inodes_lock);
1471 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
1473 struct bch_inode_info *inode;
1474 DARRAY(struct bch_inode_info *) grabbed;
1475 bool clean_pass = false, this_pass_clean;
1478 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
1479 * be pruned with d_mark_dontcache().
1481 * Once we've had a clean pass where we didn't find any inodes without
1482 * I_DONTCACHE, we wait for them to be freed:
1485 darray_init(&grabbed);
1486 darray_make_room(&grabbed, 1024);
1489 this_pass_clean = true;
1491 mutex_lock(&c->vfs_inodes_lock);
1492 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
1493 if (!snapshot_list_has_id(s, inode->ei_subvol))
1496 if (!(inode->v.i_state & I_DONTCACHE) &&
1497 !(inode->v.i_state & I_FREEING) &&
1499 this_pass_clean = false;
1501 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
1505 } else if (clean_pass && this_pass_clean) {
1506 wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW);
1507 DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW);
1509 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1510 mutex_unlock(&c->vfs_inodes_lock);
1513 finish_wait(wq, &wait.wq_entry);
1517 mutex_unlock(&c->vfs_inodes_lock);
1519 darray_for_each(grabbed, i) {
1521 d_mark_dontcache(&inode->v);
1522 d_prune_aliases(&inode->v);
1527 if (!clean_pass || !this_pass_clean) {
1528 clean_pass = this_pass_clean;
1532 darray_exit(&grabbed);
1535 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1537 struct super_block *sb = dentry->d_sb;
1538 struct bch_fs *c = sb->s_fs_info;
1539 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1540 unsigned shift = sb->s_blocksize_bits - 9;
1542 * this assumes inodes take up 64 bytes, which is a decent average
1545 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1548 buf->f_type = BCACHEFS_STATFS_MAGIC;
1549 buf->f_bsize = sb->s_blocksize;
1550 buf->f_blocks = usage.capacity >> shift;
1551 buf->f_bfree = usage.free >> shift;
1552 buf->f_bavail = avail_factor(usage.free) >> shift;
1554 buf->f_files = usage.nr_inodes + avail_inodes;
1555 buf->f_ffree = avail_inodes;
1557 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1558 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1559 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1560 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1561 buf->f_namelen = BCH_NAME_MAX;
1566 static int bch2_sync_fs(struct super_block *sb, int wait)
1568 struct bch_fs *c = sb->s_fs_info;
1571 if (c->opts.journal_flush_disabled)
1575 bch2_journal_flush_async(&c->journal, NULL);
1579 ret = bch2_journal_flush(&c->journal);
1580 return bch2_err_class(ret);
1583 static struct bch_fs *bch2_path_to_fs(const char *path)
1589 ret = lookup_bdev(path, &dev);
1591 return ERR_PTR(ret);
1593 c = bch2_dev_to_fs(dev);
1595 closure_put(&c->cl);
1596 return c ?: ERR_PTR(-ENOENT);
1599 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1601 struct bch_fs *c = sb->s_fs_info;
1602 struct bch_opts opts = bch2_opts_empty();
1605 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1607 ret = bch2_parse_mount_opts(c, &opts, data);
1611 if (opts.read_only != c->opts.read_only) {
1612 down_write(&c->state_lock);
1614 if (opts.read_only) {
1615 bch2_fs_read_only(c);
1617 sb->s_flags |= SB_RDONLY;
1619 ret = bch2_fs_read_write(c);
1621 bch_err(c, "error going rw: %i", ret);
1622 up_write(&c->state_lock);
1627 sb->s_flags &= ~SB_RDONLY;
1630 c->opts.read_only = opts.read_only;
1632 up_write(&c->state_lock);
1635 if (opt_defined(opts, errors))
1636 c->opts.errors = opts.errors;
1638 return bch2_err_class(ret);
1641 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1643 struct bch_fs *c = root->d_sb->s_fs_info;
1646 for_each_online_member(c, ca) {
1650 seq_puts(seq, ca->disk_sb.sb_name);
1656 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1658 struct bch_fs *c = root->d_sb->s_fs_info;
1660 struct printbuf buf = PRINTBUF;
1663 for (i = 0; i < bch2_opts_nr; i++) {
1664 const struct bch_option *opt = &bch2_opt_table[i];
1665 u64 v = bch2_opt_get_by_id(&c->opts, i);
1667 if (!(opt->flags & OPT_MOUNT))
1670 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1673 printbuf_reset(&buf);
1674 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1675 OPT_SHOW_MOUNT_STYLE);
1677 seq_puts(seq, buf.buf);
1680 if (buf.allocation_failure)
1682 printbuf_exit(&buf);
1686 static void bch2_put_super(struct super_block *sb)
1688 struct bch_fs *c = sb->s_fs_info;
1694 * bcachefs doesn't currently integrate intwrite freeze protection but the
1695 * internal write references serve the same purpose. Therefore reuse the
1696 * read-only transition code to perform the quiesce. The caveat is that we don't
1697 * currently have the ability to block tasks that want a write reference while
1698 * the superblock is frozen. This is fine for now, but we should either add
1699 * blocking support or find a way to integrate sb_start_intwrite() and friends.
1701 static int bch2_freeze(struct super_block *sb)
1703 struct bch_fs *c = sb->s_fs_info;
1705 down_write(&c->state_lock);
1706 bch2_fs_read_only(c);
1707 up_write(&c->state_lock);
1711 static int bch2_unfreeze(struct super_block *sb)
1713 struct bch_fs *c = sb->s_fs_info;
1716 if (test_bit(BCH_FS_emergency_ro, &c->flags))
1719 down_write(&c->state_lock);
1720 ret = bch2_fs_read_write(c);
1721 up_write(&c->state_lock);
1725 static const struct super_operations bch_super_operations = {
1726 .alloc_inode = bch2_alloc_inode,
1727 .destroy_inode = bch2_destroy_inode,
1728 .write_inode = bch2_vfs_write_inode,
1729 .evict_inode = bch2_evict_inode,
1730 .sync_fs = bch2_sync_fs,
1731 .statfs = bch2_statfs,
1732 .show_devname = bch2_show_devname,
1733 .show_options = bch2_show_options,
1734 .remount_fs = bch2_remount,
1735 .put_super = bch2_put_super,
1736 .freeze_fs = bch2_freeze,
1737 .unfreeze_fs = bch2_unfreeze,
1740 static int bch2_set_super(struct super_block *s, void *data)
1742 s->s_fs_info = data;
1746 static int bch2_noset_super(struct super_block *s, void *data)
1751 typedef DARRAY(struct bch_fs *) darray_fs;
1753 static int bch2_test_super(struct super_block *s, void *data)
1755 struct bch_fs *c = s->s_fs_info;
1756 darray_fs *d = data;
1761 darray_for_each(*d, i)
1767 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1768 int flags, const char *dev_name, void *data)
1771 struct super_block *sb;
1772 struct inode *vinode;
1773 struct bch_opts opts = bch2_opts_empty();
1776 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1778 ret = bch2_parse_mount_opts(NULL, &opts, data);
1780 return ERR_PTR(ret);
1782 if (!dev_name || strlen(dev_name) == 0)
1783 return ERR_PTR(-EINVAL);
1786 ret = bch2_split_devs(dev_name, &devs);
1788 return ERR_PTR(ret);
1790 darray_fs devs_to_fs = {};
1791 darray_for_each(devs, i) {
1792 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
1799 sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs);
1803 c = bch2_fs_open(devs.data, devs.nr, opts);
1809 /* Some options can't be parsed until after the fs is started: */
1810 ret = bch2_parse_mount_opts(c, &opts, data);
1817 bch2_opts_apply(&c->opts, opts);
1819 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1823 darray_exit(&devs_to_fs);
1824 bch2_darray_str_exit(&devs);
1828 ret = bch2_err_class(ret);
1829 return ERR_PTR(ret);
1835 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1842 sb->s_blocksize = block_bytes(c);
1843 sb->s_blocksize_bits = ilog2(block_bytes(c));
1844 sb->s_maxbytes = MAX_LFS_FILESIZE;
1845 sb->s_op = &bch_super_operations;
1846 sb->s_export_op = &bch_export_ops;
1847 #ifdef CONFIG_BCACHEFS_QUOTA
1848 sb->s_qcop = &bch2_quotactl_operations;
1849 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1851 sb->s_xattr = bch2_xattr_handlers;
1852 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1853 sb->s_time_gran = c->sb.nsec_per_time_unit;
1854 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1855 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1857 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
1859 ret = super_setup_bdi(sb);
1863 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1865 for_each_online_member(c, ca) {
1866 struct block_device *bdev = ca->disk_sb.bdev;
1868 /* XXX: create an anonymous device for multi device filesystems */
1870 sb->s_dev = bdev->bd_dev;
1871 percpu_ref_put(&ca->io_ref);
1877 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1879 sb->s_flags |= SB_POSIXACL;
1882 sb->s_shrink->seeks = 0;
1884 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1885 ret = PTR_ERR_OR_ZERO(vinode);
1886 bch_err_msg(c, ret, "mounting: error getting root inode");
1890 sb->s_root = d_make_root(vinode);
1892 bch_err(c, "error mounting: error allocating root dentry");
1897 sb->s_flags |= SB_ACTIVE;
1899 return dget(sb->s_root);
1902 deactivate_locked_super(sb);
1903 return ERR_PTR(bch2_err_class(ret));
1906 static void bch2_kill_sb(struct super_block *sb)
1908 struct bch_fs *c = sb->s_fs_info;
1910 generic_shutdown_super(sb);
1914 static struct file_system_type bcache_fs_type = {
1915 .owner = THIS_MODULE,
1917 .mount = bch2_mount,
1918 .kill_sb = bch2_kill_sb,
1919 .fs_flags = FS_REQUIRES_DEV,
1922 MODULE_ALIAS_FS("bcachefs");
1924 void bch2_vfs_exit(void)
1926 unregister_filesystem(&bcache_fs_type);
1927 kmem_cache_destroy(bch2_inode_cache);
1930 int __init bch2_vfs_init(void)
1934 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT);
1935 if (!bch2_inode_cache)
1938 ret = register_filesystem(&bcache_fs_type);
1948 #endif /* NO_BCACHEFS_FS */