1 // SPDX-License-Identifier: GPL-2.0
7 #include "btree_update.h"
14 #include "fs-common.h"
17 #include "fs-io-buffered.h"
18 #include "fs-io-direct.h"
19 #include "fs-io-pagecache.h"
30 #include <linux/aio.h>
31 #include <linux/backing-dev.h>
32 #include <linux/exportfs.h>
33 #include <linux/fiemap.h>
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/posix_acl.h>
37 #include <linux/random.h>
38 #include <linux/seq_file.h>
39 #include <linux/statfs.h>
40 #include <linux/string.h>
41 #include <linux/xattr.h>
43 static struct kmem_cache *bch2_inode_cache;
45 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
46 struct bch_inode_info *,
47 struct bch_inode_unpacked *,
48 struct bch_subvolume *);
50 void bch2_inode_update_after_write(struct btree_trans *trans,
51 struct bch_inode_info *inode,
52 struct bch_inode_unpacked *bi,
55 struct bch_fs *c = trans->c;
57 BUG_ON(bi->bi_inum != inode->v.i_ino);
59 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
61 c->opts.inodes_use_key_cache);
63 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
64 i_uid_write(&inode->v, bi->bi_uid);
65 i_gid_write(&inode->v, bi->bi_gid);
66 inode->v.i_mode = bi->bi_mode;
68 if (fields & ATTR_ATIME)
69 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
70 if (fields & ATTR_MTIME)
71 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
72 if (fields & ATTR_CTIME)
73 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
75 inode->ei_inode = *bi;
77 bch2_inode_flags_to_vfs(inode);
80 int __must_check bch2_write_inode(struct bch_fs *c,
81 struct bch_inode_info *inode,
83 void *p, unsigned fields)
85 struct btree_trans *trans = bch2_trans_get(c);
86 struct btree_iter iter = { NULL };
87 struct bch_inode_unpacked inode_u;
90 bch2_trans_begin(trans);
92 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
94 (set ? set(trans, inode, &inode_u, p) : 0) ?:
95 bch2_inode_write(trans, &iter, &inode_u) ?:
96 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
99 * the btree node lock protects inode->ei_inode, not ei_update_lock;
100 * this is important for inode updates via bchfs_write_index_update
103 bch2_inode_update_after_write(trans, inode, &inode_u, fields);
105 bch2_trans_iter_exit(trans, &iter);
107 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
110 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
111 "inode %u:%llu not found when updating",
112 inode_inum(inode).subvol,
113 inode_inum(inode).inum);
115 bch2_trans_put(trans);
116 return ret < 0 ? ret : 0;
119 int bch2_fs_quota_transfer(struct bch_fs *c,
120 struct bch_inode_info *inode,
121 struct bch_qid new_qid,
123 enum quota_acct_mode mode)
128 qtypes &= enabled_qtypes(c);
130 for (i = 0; i < QTYP_NR; i++)
131 if (new_qid.q[i] == inode->ei_qid.q[i])
132 qtypes &= ~(1U << i);
137 mutex_lock(&inode->ei_quota_lock);
139 ret = bch2_quota_transfer(c, qtypes, new_qid,
142 inode->ei_quota_reserved,
145 for (i = 0; i < QTYP_NR; i++)
146 if (qtypes & (1 << i))
147 inode->ei_qid.q[i] = new_qid.q[i];
149 mutex_unlock(&inode->ei_quota_lock);
154 static int bch2_iget5_test(struct inode *vinode, void *p)
156 struct bch_inode_info *inode = to_bch_ei(vinode);
157 subvol_inum *inum = p;
159 return inode->ei_subvol == inum->subvol &&
160 inode->ei_inode.bi_inum == inum->inum;
163 static int bch2_iget5_set(struct inode *vinode, void *p)
165 struct bch_inode_info *inode = to_bch_ei(vinode);
166 subvol_inum *inum = p;
168 inode->v.i_ino = inum->inum;
169 inode->ei_subvol = inum->subvol;
170 inode->ei_inode.bi_inum = inum->inum;
174 static unsigned bch2_inode_hash(subvol_inum inum)
176 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
179 static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_inode_info *inode)
181 subvol_inum inum = inode_inum(inode);
182 struct bch_inode_info *old = to_bch_ei(inode_insert5(&inode->v,
183 bch2_inode_hash(inum),
189 if (unlikely(old != inode)) {
190 discard_new_inode(&inode->v);
193 mutex_lock(&c->vfs_inodes_lock);
194 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
195 mutex_unlock(&c->vfs_inodes_lock);
197 * we really don't want insert_inode_locked2() to be setting
200 unlock_new_inode(&inode->v);
206 #define memalloc_flags_do(_flags, _do) \
208 unsigned _saved_flags = memalloc_flags_save(_flags); \
209 typeof(_do) _ret = _do; \
210 memalloc_noreclaim_restore(_saved_flags); \
215 * Allocate a new inode, dropping/retaking btree locks if necessary:
217 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
219 struct bch_fs *c = trans->c;
221 struct bch_inode_info *inode =
222 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN,
223 to_bch_ei(new_inode(c->vfs_sb)));
225 if (unlikely(!inode)) {
226 int ret = drop_locks_do(trans, (inode = to_bch_ei(new_inode(c->vfs_sb))) ? 0 : -ENOMEM);
228 discard_new_inode(&inode->v);
236 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
238 struct bch_inode_info *inode =
239 to_bch_ei(ilookup5_nowait(c->vfs_sb,
240 bch2_inode_hash(inum),
246 struct btree_trans *trans = bch2_trans_get(c);
248 struct bch_inode_unpacked inode_u;
249 struct bch_subvolume subvol;
250 int ret = lockrestart_do(trans,
251 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
252 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
253 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
255 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
256 inode = bch2_inode_insert(c, inode);
258 bch2_trans_put(trans);
260 return ret ? ERR_PTR(ret) : &inode->v;
263 struct bch_inode_info *
264 __bch2_create(struct mnt_idmap *idmap,
265 struct bch_inode_info *dir, struct dentry *dentry,
266 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
269 struct bch_fs *c = dir->v.i_sb->s_fs_info;
270 struct btree_trans *trans;
271 struct bch_inode_unpacked dir_u;
272 struct bch_inode_info *inode;
273 struct bch_inode_unpacked inode_u;
274 struct posix_acl *default_acl = NULL, *acl = NULL;
276 struct bch_subvolume subvol;
281 * preallocate acls + vfs inode before btree transaction, so that
282 * nothing can fail after the transaction succeeds:
284 #ifdef CONFIG_BCACHEFS_POSIX_ACL
285 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
289 inode = to_bch_ei(new_inode(c->vfs_sb));
290 if (unlikely(!inode)) {
291 inode = ERR_PTR(-ENOMEM);
295 bch2_inode_init_early(c, &inode_u);
297 if (!(flags & BCH_CREATE_TMPFILE))
298 mutex_lock(&dir->ei_update_lock);
300 trans = bch2_trans_get(c);
302 bch2_trans_begin(trans);
304 ret = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?:
305 bch2_create_trans(trans,
306 inode_inum(dir), &dir_u, &inode_u,
307 !(flags & BCH_CREATE_TMPFILE)
308 ? &dentry->d_name : NULL,
309 from_kuid(i_user_ns(&dir->v), current_fsuid()),
310 from_kgid(i_user_ns(&dir->v), current_fsgid()),
312 default_acl, acl, snapshot_src, flags) ?:
313 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
314 KEY_TYPE_QUOTA_PREALLOC);
316 goto err_before_quota;
318 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
319 inum.inum = inode_u.bi_inum;
321 ret = bch2_subvolume_get(trans, inum.subvol, true,
322 BTREE_ITER_WITH_UPDATES, &subvol) ?:
323 bch2_trans_commit(trans, NULL, &journal_seq, 0);
325 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
326 KEY_TYPE_QUOTA_WARN);
328 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
333 if (!(flags & BCH_CREATE_TMPFILE)) {
334 bch2_inode_update_after_write(trans, dir, &dir_u,
335 ATTR_MTIME|ATTR_CTIME);
336 mutex_unlock(&dir->ei_update_lock);
339 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
341 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
342 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
345 * we must insert the new inode into the inode cache before calling
346 * bch2_trans_exit() and dropping locks, else we could race with another
347 * thread pulling the inode in and modifying it:
349 inode = bch2_inode_insert(c, inode);
350 bch2_trans_put(trans);
352 posix_acl_release(default_acl);
353 posix_acl_release(acl);
356 if (!(flags & BCH_CREATE_TMPFILE))
357 mutex_unlock(&dir->ei_update_lock);
359 bch2_trans_put(trans);
360 make_bad_inode(&inode->v);
362 inode = ERR_PTR(ret);
368 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
371 struct bch_fs *c = vdir->i_sb->s_fs_info;
372 struct bch_inode_info *dir = to_bch_ei(vdir);
373 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
374 struct inode *vinode = NULL;
375 subvol_inum inum = { .subvol = 1 };
378 ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
379 &dentry->d_name, &inum);
382 vinode = bch2_vfs_inode_get(c, inum);
384 return d_splice_alias(vinode, dentry);
387 static int bch2_mknod(struct mnt_idmap *idmap,
388 struct inode *vdir, struct dentry *dentry,
389 umode_t mode, dev_t rdev)
391 struct bch_inode_info *inode =
392 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
393 (subvol_inum) { 0 }, 0);
396 return bch2_err_class(PTR_ERR(inode));
398 d_instantiate(dentry, &inode->v);
402 static int bch2_create(struct mnt_idmap *idmap,
403 struct inode *vdir, struct dentry *dentry,
404 umode_t mode, bool excl)
406 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
409 static int __bch2_link(struct bch_fs *c,
410 struct bch_inode_info *inode,
411 struct bch_inode_info *dir,
412 struct dentry *dentry)
414 struct btree_trans *trans = bch2_trans_get(c);
415 struct bch_inode_unpacked dir_u, inode_u;
418 mutex_lock(&inode->ei_update_lock);
420 ret = commit_do(trans, NULL, NULL, 0,
421 bch2_link_trans(trans,
422 inode_inum(dir), &dir_u,
423 inode_inum(inode), &inode_u,
427 bch2_inode_update_after_write(trans, dir, &dir_u,
428 ATTR_MTIME|ATTR_CTIME);
429 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
432 bch2_trans_put(trans);
433 mutex_unlock(&inode->ei_update_lock);
437 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
438 struct dentry *dentry)
440 struct bch_fs *c = vdir->i_sb->s_fs_info;
441 struct bch_inode_info *dir = to_bch_ei(vdir);
442 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
445 lockdep_assert_held(&inode->v.i_rwsem);
447 ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
448 bch2_subvol_is_ro(c, inode->ei_subvol) ?:
449 __bch2_link(c, inode, dir, dentry);
451 return bch2_err_class(ret);
454 d_instantiate(dentry, &inode->v);
458 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
459 bool deleting_snapshot)
461 struct bch_fs *c = vdir->i_sb->s_fs_info;
462 struct bch_inode_info *dir = to_bch_ei(vdir);
463 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
464 struct bch_inode_unpacked dir_u, inode_u;
465 struct btree_trans *trans = bch2_trans_get(c);
468 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
470 ret = commit_do(trans, NULL, NULL,
471 BCH_TRANS_COMMIT_no_enospc,
472 bch2_unlink_trans(trans,
473 inode_inum(dir), &dir_u,
474 &inode_u, &dentry->d_name,
479 bch2_inode_update_after_write(trans, dir, &dir_u,
480 ATTR_MTIME|ATTR_CTIME);
481 bch2_inode_update_after_write(trans, inode, &inode_u,
484 if (inode_u.bi_subvol) {
486 * Subvolume deletion is asynchronous, but we still want to tell
487 * the VFS that it's been deleted here:
489 set_nlink(&inode->v, 0);
492 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
493 bch2_trans_put(trans);
498 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
500 struct bch_inode_info *dir= to_bch_ei(vdir);
501 struct bch_fs *c = dir->v.i_sb->s_fs_info;
503 int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
504 __bch2_unlink(vdir, dentry, false);
505 return bch2_err_class(ret);
508 static int bch2_symlink(struct mnt_idmap *idmap,
509 struct inode *vdir, struct dentry *dentry,
512 struct bch_fs *c = vdir->i_sb->s_fs_info;
513 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
516 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
517 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
519 return bch2_err_class(PTR_ERR(inode));
521 inode_lock(&inode->v);
522 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
523 inode_unlock(&inode->v);
528 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
532 ret = __bch2_link(c, inode, dir, dentry);
536 d_instantiate(dentry, &inode->v);
540 return bch2_err_class(ret);
543 static int bch2_mkdir(struct mnt_idmap *idmap,
544 struct inode *vdir, struct dentry *dentry, umode_t mode)
546 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
549 static int bch2_rename2(struct mnt_idmap *idmap,
550 struct inode *src_vdir, struct dentry *src_dentry,
551 struct inode *dst_vdir, struct dentry *dst_dentry,
554 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
555 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
556 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
557 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
558 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
559 struct bch_inode_unpacked dst_dir_u, src_dir_u;
560 struct bch_inode_unpacked src_inode_u, dst_inode_u;
561 struct btree_trans *trans;
562 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
563 ? BCH_RENAME_EXCHANGE
564 : dst_dentry->d_inode
565 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
568 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
571 if (mode == BCH_RENAME_OVERWRITE) {
572 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
578 trans = bch2_trans_get(c);
580 bch2_lock_inodes(INODE_UPDATE_LOCK,
586 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?:
587 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol);
591 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
592 ret = bch2_fs_quota_transfer(c, src_inode,
595 KEY_TYPE_QUOTA_PREALLOC);
600 if (mode == BCH_RENAME_EXCHANGE &&
601 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
602 ret = bch2_fs_quota_transfer(c, dst_inode,
605 KEY_TYPE_QUOTA_PREALLOC);
610 ret = commit_do(trans, NULL, NULL, 0,
611 bch2_rename_trans(trans,
612 inode_inum(src_dir), &src_dir_u,
613 inode_inum(dst_dir), &dst_dir_u,
622 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
624 dst_inode->v.i_ino != dst_inode_u.bi_inum);
626 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
627 ATTR_MTIME|ATTR_CTIME);
629 if (src_dir != dst_dir)
630 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
631 ATTR_MTIME|ATTR_CTIME);
633 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
637 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
640 bch2_trans_put(trans);
642 bch2_fs_quota_transfer(c, src_inode,
643 bch_qid(&src_inode->ei_inode),
645 KEY_TYPE_QUOTA_NOCHECK);
647 bch2_fs_quota_transfer(c, dst_inode,
648 bch_qid(&dst_inode->ei_inode),
650 KEY_TYPE_QUOTA_NOCHECK);
652 bch2_unlock_inodes(INODE_UPDATE_LOCK,
658 return bch2_err_class(ret);
661 static void bch2_setattr_copy(struct mnt_idmap *idmap,
662 struct bch_inode_info *inode,
663 struct bch_inode_unpacked *bi,
666 struct bch_fs *c = inode->v.i_sb->s_fs_info;
667 unsigned int ia_valid = attr->ia_valid;
669 if (ia_valid & ATTR_UID)
670 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
671 if (ia_valid & ATTR_GID)
672 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
674 if (ia_valid & ATTR_SIZE)
675 bi->bi_size = attr->ia_size;
677 if (ia_valid & ATTR_ATIME)
678 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
679 if (ia_valid & ATTR_MTIME)
680 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
681 if (ia_valid & ATTR_CTIME)
682 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
684 if (ia_valid & ATTR_MODE) {
685 umode_t mode = attr->ia_mode;
686 kgid_t gid = ia_valid & ATTR_GID
690 if (!in_group_p(gid) &&
691 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
697 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
698 struct bch_inode_info *inode,
701 struct bch_fs *c = inode->v.i_sb->s_fs_info;
703 struct btree_trans *trans;
704 struct btree_iter inode_iter = { NULL };
705 struct bch_inode_unpacked inode_u;
706 struct posix_acl *acl = NULL;
709 mutex_lock(&inode->ei_update_lock);
713 if (attr->ia_valid & ATTR_UID)
714 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
716 if (attr->ia_valid & ATTR_GID)
717 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
719 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
720 KEY_TYPE_QUOTA_PREALLOC);
724 trans = bch2_trans_get(c);
726 bch2_trans_begin(trans);
730 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
735 bch2_setattr_copy(idmap, inode, &inode_u, attr);
737 if (attr->ia_valid & ATTR_MODE) {
738 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
739 inode_u.bi_mode, &acl);
744 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
745 bch2_trans_commit(trans, NULL, NULL,
746 BCH_TRANS_COMMIT_no_enospc);
748 bch2_trans_iter_exit(trans, &inode_iter);
750 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
755 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
758 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
760 bch2_trans_put(trans);
762 mutex_unlock(&inode->ei_update_lock);
764 return bch2_err_class(ret);
767 static int bch2_getattr(struct mnt_idmap *idmap,
768 const struct path *path, struct kstat *stat,
769 u32 request_mask, unsigned query_flags)
771 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
772 struct bch_fs *c = inode->v.i_sb->s_fs_info;
774 stat->dev = inode->v.i_sb->s_dev;
775 stat->ino = inode->v.i_ino;
776 stat->mode = inode->v.i_mode;
777 stat->nlink = inode->v.i_nlink;
778 stat->uid = inode->v.i_uid;
779 stat->gid = inode->v.i_gid;
780 stat->rdev = inode->v.i_rdev;
781 stat->size = i_size_read(&inode->v);
782 stat->atime = inode_get_atime(&inode->v);
783 stat->mtime = inode_get_mtime(&inode->v);
784 stat->ctime = inode_get_ctime(&inode->v);
785 stat->blksize = block_bytes(c);
786 stat->blocks = inode->v.i_blocks;
788 if (request_mask & STATX_BTIME) {
789 stat->result_mask |= STATX_BTIME;
790 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
793 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
794 stat->attributes |= STATX_ATTR_IMMUTABLE;
795 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
797 if (inode->ei_inode.bi_flags & BCH_INODE_append)
798 stat->attributes |= STATX_ATTR_APPEND;
799 stat->attributes_mask |= STATX_ATTR_APPEND;
801 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
802 stat->attributes |= STATX_ATTR_NODUMP;
803 stat->attributes_mask |= STATX_ATTR_NODUMP;
808 static int bch2_setattr(struct mnt_idmap *idmap,
809 struct dentry *dentry, struct iattr *iattr)
811 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
812 struct bch_fs *c = inode->v.i_sb->s_fs_info;
815 lockdep_assert_held(&inode->v.i_rwsem);
817 ret = bch2_subvol_is_ro(c, inode->ei_subvol) ?:
818 setattr_prepare(idmap, dentry, iattr);
822 return iattr->ia_valid & ATTR_SIZE
823 ? bchfs_truncate(idmap, inode, iattr)
824 : bch2_setattr_nonsize(idmap, inode, iattr);
827 static int bch2_tmpfile(struct mnt_idmap *idmap,
828 struct inode *vdir, struct file *file, umode_t mode)
830 struct bch_inode_info *inode =
831 __bch2_create(idmap, to_bch_ei(vdir),
832 file->f_path.dentry, mode, 0,
833 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
836 return bch2_err_class(PTR_ERR(inode));
838 d_mark_tmpfile(file, &inode->v);
839 d_instantiate(file->f_path.dentry, &inode->v);
840 return finish_open_simple(file, 0);
843 static int bch2_fill_extent(struct bch_fs *c,
844 struct fiemap_extent_info *info,
845 struct bkey_s_c k, unsigned flags)
847 if (bkey_extent_is_direct_data(k.k)) {
848 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
849 const union bch_extent_entry *entry;
850 struct extent_ptr_decoded p;
853 if (k.k->type == KEY_TYPE_reflink_v)
854 flags |= FIEMAP_EXTENT_SHARED;
856 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
858 u64 offset = p.ptr.offset;
861 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
863 if (p.crc.compression_type)
864 flags2 |= FIEMAP_EXTENT_ENCODED;
866 offset += p.crc.offset;
868 if ((offset & (block_sectors(c) - 1)) ||
869 (k.k->size & (block_sectors(c) - 1)))
870 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
872 ret = fiemap_fill_next_extent(info,
873 bkey_start_offset(k.k) << 9,
875 k.k->size << 9, flags|flags2);
881 } else if (bkey_extent_is_inline_data(k.k)) {
882 return fiemap_fill_next_extent(info,
883 bkey_start_offset(k.k) << 9,
886 FIEMAP_EXTENT_DATA_INLINE);
887 } else if (k.k->type == KEY_TYPE_reservation) {
888 return fiemap_fill_next_extent(info,
889 bkey_start_offset(k.k) << 9,
892 FIEMAP_EXTENT_DELALLOC|
893 FIEMAP_EXTENT_UNWRITTEN);
899 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
902 struct bch_fs *c = vinode->i_sb->s_fs_info;
903 struct bch_inode_info *ei = to_bch_ei(vinode);
904 struct btree_trans *trans;
905 struct btree_iter iter;
907 struct bkey_buf cur, prev;
908 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
909 unsigned offset_into_extent, sectors;
910 bool have_extent = false;
914 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
918 if (start + len < start)
923 bch2_bkey_buf_init(&cur);
924 bch2_bkey_buf_init(&prev);
925 trans = bch2_trans_get(c);
927 bch2_trans_begin(trans);
929 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot);
933 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
934 SPOS(ei->v.i_ino, start, snapshot), 0);
936 while (!(ret = btree_trans_too_many_iters(trans)) &&
937 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
938 !(ret = bkey_err(k))) {
939 enum btree_id data_btree = BTREE_ID_extents;
941 if (!bkey_extent_is_data(k.k) &&
942 k.k->type != KEY_TYPE_reservation) {
943 bch2_btree_iter_advance(&iter);
947 offset_into_extent = iter.pos.offset -
948 bkey_start_offset(k.k);
949 sectors = k.k->size - offset_into_extent;
951 bch2_bkey_buf_reassemble(&cur, c, k);
953 ret = bch2_read_indirect_extent(trans, &data_btree,
954 &offset_into_extent, &cur);
958 k = bkey_i_to_s_c(cur.k);
959 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
961 sectors = min(sectors, k.k->size - offset_into_extent);
963 bch2_cut_front(POS(k.k->p.inode,
964 bkey_start_offset(k.k) +
967 bch2_key_resize(&cur.k->k, sectors);
968 cur.k->k.p = iter.pos;
969 cur.k->k.p.offset += cur.k->k.size;
972 bch2_trans_unlock(trans);
973 ret = bch2_fill_extent(c, info,
974 bkey_i_to_s_c(prev.k), 0);
979 bkey_copy(prev.k, cur.k);
982 bch2_btree_iter_set_pos(&iter,
983 POS(iter.pos.inode, iter.pos.offset + sectors));
985 start = iter.pos.offset;
986 bch2_trans_iter_exit(trans, &iter);
988 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
991 if (!ret && have_extent) {
992 bch2_trans_unlock(trans);
993 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
997 bch2_trans_put(trans);
998 bch2_bkey_buf_exit(&cur, c);
999 bch2_bkey_buf_exit(&prev, c);
1000 return ret < 0 ? ret : 0;
1003 static const struct vm_operations_struct bch_vm_ops = {
1004 .fault = bch2_page_fault,
1005 .map_pages = filemap_map_pages,
1006 .page_mkwrite = bch2_page_mkwrite,
1009 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1011 file_accessed(file);
1013 vma->vm_ops = &bch_vm_ops;
1019 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1021 return generic_file_llseek_size(file, offset, whence,
1025 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1027 struct bch_inode_info *inode = file_bch_inode(file);
1028 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1030 if (!dir_emit_dots(file, ctx))
1033 int ret = bch2_readdir(c, inode_inum(inode), ctx);
1036 return bch2_err_class(ret);
1039 static int bch2_open(struct inode *vinode, struct file *file)
1041 if (file->f_flags & (O_WRONLY|O_RDWR)) {
1042 struct bch_inode_info *inode = to_bch_ei(vinode);
1043 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1045 int ret = bch2_subvol_is_ro(c, inode->ei_subvol);
1050 return generic_file_open(vinode, file);
1053 static const struct file_operations bch_file_operations = {
1055 .llseek = bch2_llseek,
1056 .read_iter = bch2_read_iter,
1057 .write_iter = bch2_write_iter,
1059 .fsync = bch2_fsync,
1060 .splice_read = filemap_splice_read,
1061 .splice_write = iter_file_splice_write,
1062 .fallocate = bch2_fallocate_dispatch,
1063 .unlocked_ioctl = bch2_fs_file_ioctl,
1064 #ifdef CONFIG_COMPAT
1065 .compat_ioctl = bch2_compat_fs_ioctl,
1067 .remap_file_range = bch2_remap_file_range,
1070 static const struct inode_operations bch_file_inode_operations = {
1071 .getattr = bch2_getattr,
1072 .setattr = bch2_setattr,
1073 .fiemap = bch2_fiemap,
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_dir_inode_operations = {
1082 .lookup = bch2_lookup,
1083 .create = bch2_create,
1085 .unlink = bch2_unlink,
1086 .symlink = bch2_symlink,
1087 .mkdir = bch2_mkdir,
1088 .rmdir = bch2_unlink,
1089 .mknod = bch2_mknod,
1090 .rename = bch2_rename2,
1091 .getattr = bch2_getattr,
1092 .setattr = bch2_setattr,
1093 .tmpfile = bch2_tmpfile,
1094 .listxattr = bch2_xattr_list,
1095 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1096 .get_acl = bch2_get_acl,
1097 .set_acl = bch2_set_acl,
1101 static const struct file_operations bch_dir_file_operations = {
1102 .llseek = bch2_dir_llseek,
1103 .read = generic_read_dir,
1104 .iterate_shared = bch2_vfs_readdir,
1105 .fsync = bch2_fsync,
1106 .unlocked_ioctl = bch2_fs_file_ioctl,
1107 #ifdef CONFIG_COMPAT
1108 .compat_ioctl = bch2_compat_fs_ioctl,
1112 static const struct inode_operations bch_symlink_inode_operations = {
1113 .get_link = page_get_link,
1114 .getattr = bch2_getattr,
1115 .setattr = bch2_setattr,
1116 .listxattr = bch2_xattr_list,
1117 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1118 .get_acl = bch2_get_acl,
1119 .set_acl = bch2_set_acl,
1123 static const struct inode_operations bch_special_inode_operations = {
1124 .getattr = bch2_getattr,
1125 .setattr = bch2_setattr,
1126 .listxattr = bch2_xattr_list,
1127 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1128 .get_acl = bch2_get_acl,
1129 .set_acl = bch2_set_acl,
1133 static const struct address_space_operations bch_address_space_operations = {
1134 .read_folio = bch2_read_folio,
1135 .writepages = bch2_writepages,
1136 .readahead = bch2_readahead,
1137 .dirty_folio = filemap_dirty_folio,
1138 .write_begin = bch2_write_begin,
1139 .write_end = bch2_write_end,
1140 .invalidate_folio = bch2_invalidate_folio,
1141 .release_folio = bch2_release_folio,
1142 .direct_IO = noop_direct_IO,
1143 #ifdef CONFIG_MIGRATION
1144 .migrate_folio = filemap_migrate_folio,
1146 .error_remove_folio = generic_error_remove_folio,
1149 struct bcachefs_fid {
1155 struct bcachefs_fid_with_parent {
1156 struct bcachefs_fid fid;
1157 struct bcachefs_fid dir;
1160 static int bcachefs_fid_valid(int fh_len, int fh_type)
1163 case FILEID_BCACHEFS_WITHOUT_PARENT:
1164 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1165 case FILEID_BCACHEFS_WITH_PARENT:
1166 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1172 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1174 return (struct bcachefs_fid) {
1175 .inum = inode->ei_inode.bi_inum,
1176 .subvol = inode->ei_subvol,
1177 .gen = inode->ei_inode.bi_generation,
1181 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1184 struct bch_inode_info *inode = to_bch_ei(vinode);
1185 struct bch_inode_info *dir = to_bch_ei(vdir);
1188 if (!S_ISDIR(inode->v.i_mode) && dir) {
1189 struct bcachefs_fid_with_parent *fid = (void *) fh;
1191 min_len = sizeof(*fid) / sizeof(u32);
1192 if (*len < min_len) {
1194 return FILEID_INVALID;
1197 fid->fid = bch2_inode_to_fid(inode);
1198 fid->dir = bch2_inode_to_fid(dir);
1201 return FILEID_BCACHEFS_WITH_PARENT;
1203 struct bcachefs_fid *fid = (void *) fh;
1205 min_len = sizeof(*fid) / sizeof(u32);
1206 if (*len < min_len) {
1208 return FILEID_INVALID;
1210 *fid = bch2_inode_to_fid(inode);
1213 return FILEID_BCACHEFS_WITHOUT_PARENT;
1217 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1218 struct bcachefs_fid fid)
1220 struct bch_fs *c = sb->s_fs_info;
1221 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1222 .subvol = fid.subvol,
1225 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1227 vinode = ERR_PTR(-ESTALE);
1232 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1233 int fh_len, int fh_type)
1235 struct bcachefs_fid *fid = (void *) _fid;
1237 if (!bcachefs_fid_valid(fh_len, fh_type))
1240 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1243 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1244 int fh_len, int fh_type)
1246 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1248 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1249 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1252 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1255 static struct dentry *bch2_get_parent(struct dentry *child)
1257 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1258 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1259 subvol_inum parent_inum = {
1260 .subvol = inode->ei_inode.bi_parent_subvol ?:
1262 .inum = inode->ei_inode.bi_dir,
1265 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1268 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1270 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1271 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1272 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1273 struct btree_trans *trans;
1274 struct btree_iter iter1;
1275 struct btree_iter iter2;
1277 struct bkey_s_c_dirent d;
1278 struct bch_inode_unpacked inode_u;
1281 struct qstr dirent_name;
1282 unsigned name_len = 0;
1285 if (!S_ISDIR(dir->v.i_mode))
1288 trans = bch2_trans_get(c);
1290 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1291 POS(dir->ei_inode.bi_inum, 0), 0);
1292 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1293 POS(dir->ei_inode.bi_inum, 0), 0);
1295 bch2_trans_begin(trans);
1297 ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot);
1301 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1302 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1304 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1308 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1309 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1311 k = bch2_btree_iter_peek_slot(&iter1);
1316 if (k.k->type != KEY_TYPE_dirent) {
1317 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1321 d = bkey_s_c_to_dirent(k);
1322 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1324 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1328 if (target.subvol == inode->ei_subvol &&
1329 target.inum == inode->ei_inode.bi_inum)
1333 * File with multiple hardlinks and our backref is to the wrong
1334 * directory - linear search:
1336 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1337 if (k.k->p.inode > dir->ei_inode.bi_inum)
1340 if (k.k->type != KEY_TYPE_dirent)
1343 d = bkey_s_c_to_dirent(k);
1344 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1350 if (target.subvol == inode->ei_subvol &&
1351 target.inum == inode->ei_inode.bi_inum)
1359 dirent_name = bch2_dirent_get_name(d);
1361 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
1362 memcpy(name, dirent_name.name, name_len);
1363 name[name_len] = '\0';
1365 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1368 bch2_trans_iter_exit(trans, &iter1);
1369 bch2_trans_iter_exit(trans, &iter2);
1370 bch2_trans_put(trans);
1375 static const struct export_operations bch_export_ops = {
1376 .encode_fh = bch2_encode_fh,
1377 .fh_to_dentry = bch2_fh_to_dentry,
1378 .fh_to_parent = bch2_fh_to_parent,
1379 .get_parent = bch2_get_parent,
1380 .get_name = bch2_get_name,
1383 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1384 struct bch_inode_info *inode,
1385 struct bch_inode_unpacked *bi,
1386 struct bch_subvolume *subvol)
1388 bch2_iget5_set(&inode->v, &inum);
1389 bch2_inode_update_after_write(trans, inode, bi, ~0);
1391 if (BCH_SUBVOLUME_SNAP(subvol))
1392 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1394 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1396 inode->v.i_blocks = bi->bi_sectors;
1397 inode->v.i_ino = bi->bi_inum;
1398 inode->v.i_rdev = bi->bi_dev;
1399 inode->v.i_generation = bi->bi_generation;
1400 inode->v.i_size = bi->bi_size;
1402 inode->ei_flags = 0;
1403 inode->ei_quota_reserved = 0;
1404 inode->ei_qid = bch_qid(bi);
1405 inode->ei_subvol = inum.subvol;
1407 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1409 switch (inode->v.i_mode & S_IFMT) {
1411 inode->v.i_op = &bch_file_inode_operations;
1412 inode->v.i_fop = &bch_file_operations;
1415 inode->v.i_op = &bch_dir_inode_operations;
1416 inode->v.i_fop = &bch_dir_file_operations;
1419 inode_nohighmem(&inode->v);
1420 inode->v.i_op = &bch_symlink_inode_operations;
1423 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1424 inode->v.i_op = &bch_special_inode_operations;
1428 mapping_set_large_folios(inode->v.i_mapping);
1431 static struct inode *bch2_alloc_inode(struct super_block *sb)
1433 struct bch_inode_info *inode;
1435 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1439 inode_init_once(&inode->v);
1440 mutex_init(&inode->ei_update_lock);
1441 two_state_lock_init(&inode->ei_pagecache_lock);
1442 INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
1443 mutex_init(&inode->ei_quota_lock);
1448 static void bch2_i_callback(struct rcu_head *head)
1450 struct inode *vinode = container_of(head, struct inode, i_rcu);
1451 struct bch_inode_info *inode = to_bch_ei(vinode);
1453 kmem_cache_free(bch2_inode_cache, inode);
1456 static void bch2_destroy_inode(struct inode *vinode)
1458 call_rcu(&vinode->i_rcu, bch2_i_callback);
1461 static int inode_update_times_fn(struct btree_trans *trans,
1462 struct bch_inode_info *inode,
1463 struct bch_inode_unpacked *bi,
1466 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1468 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
1469 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
1470 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
1475 static int bch2_vfs_write_inode(struct inode *vinode,
1476 struct writeback_control *wbc)
1478 struct bch_fs *c = vinode->i_sb->s_fs_info;
1479 struct bch_inode_info *inode = to_bch_ei(vinode);
1482 mutex_lock(&inode->ei_update_lock);
1483 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1484 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1485 mutex_unlock(&inode->ei_update_lock);
1487 return bch2_err_class(ret);
1490 static void bch2_evict_inode(struct inode *vinode)
1492 struct bch_fs *c = vinode->i_sb->s_fs_info;
1493 struct bch_inode_info *inode = to_bch_ei(vinode);
1495 truncate_inode_pages_final(&inode->v.i_data);
1497 clear_inode(&inode->v);
1499 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1501 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1502 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1503 KEY_TYPE_QUOTA_WARN);
1504 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1505 KEY_TYPE_QUOTA_WARN);
1506 bch2_inode_rm(c, inode_inum(inode));
1509 mutex_lock(&c->vfs_inodes_lock);
1510 list_del_init(&inode->ei_vfs_inode_list);
1511 mutex_unlock(&c->vfs_inodes_lock);
1514 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
1516 struct bch_inode_info *inode;
1517 DARRAY(struct bch_inode_info *) grabbed;
1518 bool clean_pass = false, this_pass_clean;
1521 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
1522 * be pruned with d_mark_dontcache().
1524 * Once we've had a clean pass where we didn't find any inodes without
1525 * I_DONTCACHE, we wait for them to be freed:
1528 darray_init(&grabbed);
1529 darray_make_room(&grabbed, 1024);
1532 this_pass_clean = true;
1534 mutex_lock(&c->vfs_inodes_lock);
1535 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
1536 if (!snapshot_list_has_id(s, inode->ei_subvol))
1539 if (!(inode->v.i_state & I_DONTCACHE) &&
1540 !(inode->v.i_state & I_FREEING) &&
1542 this_pass_clean = false;
1544 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
1548 } else if (clean_pass && this_pass_clean) {
1549 wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW);
1550 DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW);
1552 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1553 mutex_unlock(&c->vfs_inodes_lock);
1556 finish_wait(wq, &wait.wq_entry);
1560 mutex_unlock(&c->vfs_inodes_lock);
1562 darray_for_each(grabbed, i) {
1564 d_mark_dontcache(&inode->v);
1565 d_prune_aliases(&inode->v);
1570 if (!clean_pass || !this_pass_clean) {
1571 clean_pass = this_pass_clean;
1575 darray_exit(&grabbed);
1578 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1580 struct super_block *sb = dentry->d_sb;
1581 struct bch_fs *c = sb->s_fs_info;
1582 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1583 unsigned shift = sb->s_blocksize_bits - 9;
1585 * this assumes inodes take up 64 bytes, which is a decent average
1588 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1590 buf->f_type = BCACHEFS_STATFS_MAGIC;
1591 buf->f_bsize = sb->s_blocksize;
1592 buf->f_blocks = usage.capacity >> shift;
1593 buf->f_bfree = usage.free >> shift;
1594 buf->f_bavail = avail_factor(usage.free) >> shift;
1596 buf->f_files = usage.nr_inodes + avail_inodes;
1597 buf->f_ffree = avail_inodes;
1599 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
1600 buf->f_namelen = BCH_NAME_MAX;
1605 static int bch2_sync_fs(struct super_block *sb, int wait)
1607 struct bch_fs *c = sb->s_fs_info;
1610 if (c->opts.journal_flush_disabled)
1614 bch2_journal_flush_async(&c->journal, NULL);
1618 ret = bch2_journal_flush(&c->journal);
1619 return bch2_err_class(ret);
1622 static struct bch_fs *bch2_path_to_fs(const char *path)
1628 ret = lookup_bdev(path, &dev);
1630 return ERR_PTR(ret);
1632 c = bch2_dev_to_fs(dev);
1634 closure_put(&c->cl);
1635 return c ?: ERR_PTR(-ENOENT);
1638 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1640 struct bch_fs *c = sb->s_fs_info;
1641 struct bch_opts opts = bch2_opts_empty();
1644 ret = bch2_parse_mount_opts(c, &opts, data);
1648 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1650 if (opts.read_only != c->opts.read_only) {
1651 down_write(&c->state_lock);
1653 if (opts.read_only) {
1654 bch2_fs_read_only(c);
1656 sb->s_flags |= SB_RDONLY;
1658 ret = bch2_fs_read_write(c);
1660 bch_err(c, "error going rw: %i", ret);
1661 up_write(&c->state_lock);
1666 sb->s_flags &= ~SB_RDONLY;
1669 c->opts.read_only = opts.read_only;
1671 up_write(&c->state_lock);
1674 if (opt_defined(opts, errors))
1675 c->opts.errors = opts.errors;
1677 return bch2_err_class(ret);
1680 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1682 struct bch_fs *c = root->d_sb->s_fs_info;
1685 for_each_online_member(c, ca) {
1689 seq_puts(seq, ca->disk_sb.sb_name);
1695 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1697 struct bch_fs *c = root->d_sb->s_fs_info;
1699 struct printbuf buf = PRINTBUF;
1702 for (i = 0; i < bch2_opts_nr; i++) {
1703 const struct bch_option *opt = &bch2_opt_table[i];
1704 u64 v = bch2_opt_get_by_id(&c->opts, i);
1706 if (!(opt->flags & OPT_MOUNT))
1709 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1712 printbuf_reset(&buf);
1713 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1714 OPT_SHOW_MOUNT_STYLE);
1716 seq_puts(seq, buf.buf);
1719 if (buf.allocation_failure)
1721 printbuf_exit(&buf);
1725 static void bch2_put_super(struct super_block *sb)
1727 struct bch_fs *c = sb->s_fs_info;
1733 * bcachefs doesn't currently integrate intwrite freeze protection but the
1734 * internal write references serve the same purpose. Therefore reuse the
1735 * read-only transition code to perform the quiesce. The caveat is that we don't
1736 * currently have the ability to block tasks that want a write reference while
1737 * the superblock is frozen. This is fine for now, but we should either add
1738 * blocking support or find a way to integrate sb_start_intwrite() and friends.
1740 static int bch2_freeze(struct super_block *sb)
1742 struct bch_fs *c = sb->s_fs_info;
1744 down_write(&c->state_lock);
1745 bch2_fs_read_only(c);
1746 up_write(&c->state_lock);
1750 static int bch2_unfreeze(struct super_block *sb)
1752 struct bch_fs *c = sb->s_fs_info;
1755 if (test_bit(BCH_FS_emergency_ro, &c->flags))
1758 down_write(&c->state_lock);
1759 ret = bch2_fs_read_write(c);
1760 up_write(&c->state_lock);
1764 static const struct super_operations bch_super_operations = {
1765 .alloc_inode = bch2_alloc_inode,
1766 .destroy_inode = bch2_destroy_inode,
1767 .write_inode = bch2_vfs_write_inode,
1768 .evict_inode = bch2_evict_inode,
1769 .sync_fs = bch2_sync_fs,
1770 .statfs = bch2_statfs,
1771 .show_devname = bch2_show_devname,
1772 .show_options = bch2_show_options,
1773 .remount_fs = bch2_remount,
1774 .put_super = bch2_put_super,
1775 .freeze_fs = bch2_freeze,
1776 .unfreeze_fs = bch2_unfreeze,
1779 static int bch2_set_super(struct super_block *s, void *data)
1781 s->s_fs_info = data;
1785 static int bch2_noset_super(struct super_block *s, void *data)
1790 typedef DARRAY(struct bch_fs *) darray_fs;
1792 static int bch2_test_super(struct super_block *s, void *data)
1794 struct bch_fs *c = s->s_fs_info;
1795 darray_fs *d = data;
1800 darray_for_each(*d, i)
1806 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1807 int flags, const char *dev_name, void *data)
1810 struct super_block *sb;
1811 struct inode *vinode;
1812 struct bch_opts opts = bch2_opts_empty();
1815 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1817 ret = bch2_parse_mount_opts(NULL, &opts, data);
1819 return ERR_PTR(ret);
1821 if (!dev_name || strlen(dev_name) == 0)
1822 return ERR_PTR(-EINVAL);
1825 ret = bch2_split_devs(dev_name, &devs);
1827 return ERR_PTR(ret);
1829 darray_fs devs_to_fs = {};
1830 darray_for_each(devs, i) {
1831 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
1838 sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs);
1842 c = bch2_fs_open(devs.data, devs.nr, opts);
1848 /* Some options can't be parsed until after the fs is started: */
1849 ret = bch2_parse_mount_opts(c, &opts, data);
1856 bch2_opts_apply(&c->opts, opts);
1858 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1862 darray_exit(&devs_to_fs);
1863 bch2_darray_str_exit(&devs);
1867 ret = bch2_err_class(ret);
1868 return ERR_PTR(ret);
1874 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1881 sb->s_blocksize = block_bytes(c);
1882 sb->s_blocksize_bits = ilog2(block_bytes(c));
1883 sb->s_maxbytes = MAX_LFS_FILESIZE;
1884 sb->s_op = &bch_super_operations;
1885 sb->s_export_op = &bch_export_ops;
1886 #ifdef CONFIG_BCACHEFS_QUOTA
1887 sb->s_qcop = &bch2_quotactl_operations;
1888 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1890 sb->s_xattr = bch2_xattr_handlers;
1891 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1892 sb->s_time_gran = c->sb.nsec_per_time_unit;
1893 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1894 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1895 sb->s_uuid = c->sb.user_uuid;
1897 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
1899 ret = super_setup_bdi(sb);
1903 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1905 for_each_online_member(c, ca) {
1906 struct block_device *bdev = ca->disk_sb.bdev;
1908 /* XXX: create an anonymous device for multi device filesystems */
1910 sb->s_dev = bdev->bd_dev;
1911 percpu_ref_put(&ca->io_ref);
1917 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1919 sb->s_flags |= SB_POSIXACL;
1922 sb->s_shrink->seeks = 0;
1924 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1925 ret = PTR_ERR_OR_ZERO(vinode);
1926 bch_err_msg(c, ret, "mounting: error getting root inode");
1930 sb->s_root = d_make_root(vinode);
1932 bch_err(c, "error mounting: error allocating root dentry");
1937 sb->s_flags |= SB_ACTIVE;
1939 return dget(sb->s_root);
1942 deactivate_locked_super(sb);
1943 return ERR_PTR(bch2_err_class(ret));
1946 static void bch2_kill_sb(struct super_block *sb)
1948 struct bch_fs *c = sb->s_fs_info;
1950 generic_shutdown_super(sb);
1954 static struct file_system_type bcache_fs_type = {
1955 .owner = THIS_MODULE,
1957 .mount = bch2_mount,
1958 .kill_sb = bch2_kill_sb,
1959 .fs_flags = FS_REQUIRES_DEV,
1962 MODULE_ALIAS_FS("bcachefs");
1964 void bch2_vfs_exit(void)
1966 unregister_filesystem(&bcache_fs_type);
1967 kmem_cache_destroy(bch2_inode_cache);
1970 int __init bch2_vfs_init(void)
1974 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT);
1975 if (!bch2_inode_cache)
1978 ret = register_filesystem(&bcache_fs_type);
1988 #endif /* NO_BCACHEFS_FS */