1 // SPDX-License-Identifier: GPL-2.0
7 #include "btree_update.h"
13 #include "fs-common.h"
25 #include <linux/aio.h>
26 #include <linux/backing-dev.h>
27 #include <linux/exportfs.h>
28 #include <linux/fiemap.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/posix_acl.h>
32 #include <linux/random.h>
33 #include <linux/statfs.h>
34 #include <linux/string.h>
35 #include <linux/xattr.h>
37 static struct kmem_cache *bch2_inode_cache;
39 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
40 struct bch_inode_info *,
41 struct bch_inode_unpacked *,
42 struct bch_subvolume *);
44 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
46 BUG_ON(atomic_long_read(&lock->v) == 0);
48 if (atomic_long_sub_return_release(i, &lock->v) == 0)
49 wake_up_all(&lock->wait);
52 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
54 long v = atomic_long_read(&lock->v), old;
59 if (i > 0 ? v < 0 : v > 0)
61 } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
62 old, old + i)) != old);
66 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
68 wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
71 void bch2_pagecache_add_put(struct pagecache_lock *lock)
73 __pagecache_lock_put(lock, 1);
76 bool bch2_pagecache_add_tryget(struct pagecache_lock *lock)
78 return __pagecache_lock_tryget(lock, 1);
81 void bch2_pagecache_add_get(struct pagecache_lock *lock)
83 __pagecache_lock_get(lock, 1);
86 void bch2_pagecache_block_put(struct pagecache_lock *lock)
88 __pagecache_lock_put(lock, -1);
91 void bch2_pagecache_block_get(struct pagecache_lock *lock)
93 __pagecache_lock_get(lock, -1);
96 void bch2_inode_update_after_write(struct btree_trans *trans,
97 struct bch_inode_info *inode,
98 struct bch_inode_unpacked *bi,
101 struct bch_fs *c = trans->c;
103 BUG_ON(bi->bi_inum != inode->v.i_ino);
105 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
107 c->opts.inodes_use_key_cache);
109 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
110 i_uid_write(&inode->v, bi->bi_uid);
111 i_gid_write(&inode->v, bi->bi_gid);
112 inode->v.i_mode = bi->bi_mode;
114 if (fields & ATTR_ATIME)
115 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
116 if (fields & ATTR_MTIME)
117 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
118 if (fields & ATTR_CTIME)
119 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
121 inode->ei_inode = *bi;
123 bch2_inode_flags_to_vfs(inode);
126 int __must_check bch2_write_inode(struct bch_fs *c,
127 struct bch_inode_info *inode,
129 void *p, unsigned fields)
131 struct btree_trans trans;
132 struct btree_iter iter = { NULL };
133 struct bch_inode_unpacked inode_u;
136 bch2_trans_init(&trans, c, 0, 512);
138 bch2_trans_begin(&trans);
140 ret = bch2_inode_peek(&trans, &iter, &inode_u, inode_inum(inode),
141 BTREE_ITER_INTENT) ?:
142 (set ? set(inode, &inode_u, p) : 0) ?:
143 bch2_inode_write(&trans, &iter, &inode_u) ?:
144 bch2_trans_commit(&trans, NULL, NULL, BTREE_INSERT_NOFAIL);
147 * the btree node lock protects inode->ei_inode, not ei_update_lock;
148 * this is important for inode updates via bchfs_write_index_update
151 bch2_inode_update_after_write(&trans, inode, &inode_u, fields);
153 bch2_trans_iter_exit(&trans, &iter);
158 bch2_trans_exit(&trans);
159 return ret < 0 ? ret : 0;
162 int bch2_fs_quota_transfer(struct bch_fs *c,
163 struct bch_inode_info *inode,
164 struct bch_qid new_qid,
166 enum quota_acct_mode mode)
171 qtypes &= enabled_qtypes(c);
173 for (i = 0; i < QTYP_NR; i++)
174 if (new_qid.q[i] == inode->ei_qid.q[i])
175 qtypes &= ~(1U << i);
180 mutex_lock(&inode->ei_quota_lock);
182 ret = bch2_quota_transfer(c, qtypes, new_qid,
185 inode->ei_quota_reserved,
188 for (i = 0; i < QTYP_NR; i++)
189 if (qtypes & (1 << i))
190 inode->ei_qid.q[i] = new_qid.q[i];
192 mutex_unlock(&inode->ei_quota_lock);
197 static int bch2_iget5_test(struct inode *vinode, void *p)
199 struct bch_inode_info *inode = to_bch_ei(vinode);
200 subvol_inum *inum = p;
202 return inode->ei_subvol == inum->subvol &&
203 inode->ei_inode.bi_inum == inum->inum;
206 static int bch2_iget5_set(struct inode *vinode, void *p)
208 struct bch_inode_info *inode = to_bch_ei(vinode);
209 subvol_inum *inum = p;
211 inode->v.i_ino = inum->inum;
212 inode->ei_subvol = inum->subvol;
213 inode->ei_inode.bi_inum = inum->inum;
217 static unsigned bch2_inode_hash(subvol_inum inum)
219 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
222 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
224 struct bch_inode_unpacked inode_u;
225 struct bch_inode_info *inode;
226 struct btree_trans trans;
227 struct bch_subvolume subvol;
230 inode = to_bch_ei(iget5_locked(c->vfs_sb,
231 bch2_inode_hash(inum),
235 if (unlikely(!inode))
236 return ERR_PTR(-ENOMEM);
237 if (!(inode->v.i_state & I_NEW))
240 bch2_trans_init(&trans, c, 8, 0);
241 ret = lockrestart_do(&trans,
242 bch2_subvolume_get(&trans, inum.subvol, true, 0, &subvol) ?:
243 bch2_inode_find_by_inum_trans(&trans, inum, &inode_u));
246 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
247 bch2_trans_exit(&trans);
250 iget_failed(&inode->v);
254 unlock_new_inode(&inode->v);
259 struct bch_inode_info *
260 __bch2_create(struct user_namespace *mnt_userns,
261 struct bch_inode_info *dir, struct dentry *dentry,
262 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
265 struct bch_fs *c = dir->v.i_sb->s_fs_info;
266 struct btree_trans trans;
267 struct bch_inode_unpacked dir_u;
268 struct bch_inode_info *inode, *old;
269 struct bch_inode_unpacked inode_u;
270 struct posix_acl *default_acl = NULL, *acl = NULL;
272 struct bch_subvolume subvol;
277 * preallocate acls + vfs inode before btree transaction, so that
278 * nothing can fail after the transaction succeeds:
280 #ifdef CONFIG_BCACHEFS_POSIX_ACL
281 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
285 inode = to_bch_ei(new_inode(c->vfs_sb));
286 if (unlikely(!inode)) {
287 inode = ERR_PTR(-ENOMEM);
291 bch2_inode_init_early(c, &inode_u);
293 if (!(flags & BCH_CREATE_TMPFILE))
294 mutex_lock(&dir->ei_update_lock);
296 bch2_trans_init(&trans, c, 8,
297 2048 + (!(flags & BCH_CREATE_TMPFILE)
298 ? dentry->d_name.len : 0));
300 bch2_trans_begin(&trans);
302 ret = bch2_create_trans(&trans,
303 inode_inum(dir), &dir_u, &inode_u,
304 !(flags & BCH_CREATE_TMPFILE)
305 ? &dentry->d_name : NULL,
306 from_kuid(mnt_userns, current_fsuid()),
307 from_kgid(mnt_userns, current_fsgid()),
309 default_acl, acl, snapshot_src, flags) ?:
310 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
311 KEY_TYPE_QUOTA_PREALLOC);
313 goto err_before_quota;
315 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
316 inum.inum = inode_u.bi_inum;
318 ret = bch2_subvolume_get(&trans, inum.subvol, true,
319 BTREE_ITER_WITH_UPDATES, &subvol) ?:
320 bch2_trans_commit(&trans, NULL, &journal_seq, 0);
322 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
323 KEY_TYPE_QUOTA_WARN);
330 if (!(flags & BCH_CREATE_TMPFILE)) {
331 bch2_inode_update_after_write(&trans, dir, &dir_u,
332 ATTR_MTIME|ATTR_CTIME);
333 mutex_unlock(&dir->ei_update_lock);
336 bch2_iget5_set(&inode->v, &inum);
337 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
339 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
340 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
343 * we must insert the new inode into the inode cache before calling
344 * bch2_trans_exit() and dropping locks, else we could race with another
345 * thread pulling the inode in and modifying it:
348 inode->v.i_state |= I_CREATING;
350 old = to_bch_ei(inode_insert5(&inode->v,
351 bch2_inode_hash(inum),
357 if (unlikely(old != inode)) {
359 * We raced, another process pulled the new inode into cache
362 make_bad_inode(&inode->v);
368 * we really don't want insert_inode_locked2() to be setting
371 unlock_new_inode(&inode->v);
374 bch2_trans_exit(&trans);
376 posix_acl_release(default_acl);
377 posix_acl_release(acl);
380 if (!(flags & BCH_CREATE_TMPFILE))
381 mutex_unlock(&dir->ei_update_lock);
383 bch2_trans_exit(&trans);
384 make_bad_inode(&inode->v);
386 inode = ERR_PTR(ret);
392 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
395 struct bch_fs *c = vdir->i_sb->s_fs_info;
396 struct bch_inode_info *dir = to_bch_ei(vdir);
397 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
398 struct inode *vinode = NULL;
399 subvol_inum inum = { .subvol = 1 };
402 ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
403 &dentry->d_name, &inum);
406 vinode = bch2_vfs_inode_get(c, inum);
408 return d_splice_alias(vinode, dentry);
411 static int bch2_mknod(struct user_namespace *mnt_userns,
412 struct inode *vdir, struct dentry *dentry,
413 umode_t mode, dev_t rdev)
415 struct bch_inode_info *inode =
416 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, rdev,
417 (subvol_inum) { 0 }, 0);
420 return PTR_ERR(inode);
422 d_instantiate(dentry, &inode->v);
426 static int bch2_create(struct user_namespace *mnt_userns,
427 struct inode *vdir, struct dentry *dentry,
428 umode_t mode, bool excl)
430 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFREG, 0);
433 static int __bch2_link(struct bch_fs *c,
434 struct bch_inode_info *inode,
435 struct bch_inode_info *dir,
436 struct dentry *dentry)
438 struct btree_trans trans;
439 struct bch_inode_unpacked dir_u, inode_u;
442 mutex_lock(&inode->ei_update_lock);
443 bch2_trans_init(&trans, c, 4, 1024);
445 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
446 bch2_link_trans(&trans,
447 inode_inum(dir), &dir_u,
448 inode_inum(inode), &inode_u,
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, ATTR_CTIME);
457 bch2_trans_exit(&trans);
458 mutex_unlock(&inode->ei_update_lock);
462 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
463 struct dentry *dentry)
465 struct bch_fs *c = vdir->i_sb->s_fs_info;
466 struct bch_inode_info *dir = to_bch_ei(vdir);
467 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
470 lockdep_assert_held(&inode->v.i_rwsem);
472 ret = __bch2_link(c, inode, dir, dentry);
477 d_instantiate(dentry, &inode->v);
481 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
482 bool deleting_snapshot)
484 struct bch_fs *c = vdir->i_sb->s_fs_info;
485 struct bch_inode_info *dir = to_bch_ei(vdir);
486 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
487 struct bch_inode_unpacked dir_u, inode_u;
488 struct btree_trans trans;
491 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
492 bch2_trans_init(&trans, c, 4, 1024);
494 ret = __bch2_trans_do(&trans, NULL, NULL,
496 bch2_unlink_trans(&trans,
497 inode_inum(dir), &dir_u,
498 &inode_u, &dentry->d_name,
502 bch2_inode_update_after_write(&trans, dir, &dir_u,
503 ATTR_MTIME|ATTR_CTIME);
504 bch2_inode_update_after_write(&trans, inode, &inode_u,
508 bch2_trans_exit(&trans);
509 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
514 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
516 return __bch2_unlink(vdir, dentry, false);
519 static int bch2_symlink(struct user_namespace *mnt_userns,
520 struct inode *vdir, struct dentry *dentry,
523 struct bch_fs *c = vdir->i_sb->s_fs_info;
524 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
527 inode = __bch2_create(mnt_userns, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
528 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
529 if (unlikely(IS_ERR(inode)))
530 return PTR_ERR(inode);
532 inode_lock(&inode->v);
533 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
534 inode_unlock(&inode->v);
539 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
543 ret = __bch2_link(c, inode, dir, dentry);
547 d_instantiate(dentry, &inode->v);
554 static int bch2_mkdir(struct user_namespace *mnt_userns,
555 struct inode *vdir, struct dentry *dentry, umode_t mode)
557 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFDIR, 0);
560 static int bch2_rename2(struct user_namespace *mnt_userns,
561 struct inode *src_vdir, struct dentry *src_dentry,
562 struct inode *dst_vdir, struct dentry *dst_dentry,
565 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
566 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
567 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
568 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
569 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
570 struct bch_inode_unpacked dst_dir_u, src_dir_u;
571 struct bch_inode_unpacked src_inode_u, dst_inode_u;
572 struct btree_trans trans;
573 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
574 ? BCH_RENAME_EXCHANGE
575 : dst_dentry->d_inode
576 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
579 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
582 if (mode == BCH_RENAME_OVERWRITE) {
583 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
589 bch2_trans_init(&trans, c, 8, 2048);
591 bch2_lock_inodes(INODE_UPDATE_LOCK,
597 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
598 ret = bch2_fs_quota_transfer(c, src_inode,
601 KEY_TYPE_QUOTA_PREALLOC);
606 if (mode == BCH_RENAME_EXCHANGE &&
607 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
608 ret = bch2_fs_quota_transfer(c, dst_inode,
611 KEY_TYPE_QUOTA_PREALLOC);
616 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
617 bch2_rename_trans(&trans,
618 inode_inum(src_dir), &src_dir_u,
619 inode_inum(dst_dir), &dst_dir_u,
628 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
630 dst_inode->v.i_ino != dst_inode_u.bi_inum);
632 bch2_inode_update_after_write(&trans, src_dir, &src_dir_u,
633 ATTR_MTIME|ATTR_CTIME);
635 if (src_dir != dst_dir)
636 bch2_inode_update_after_write(&trans, dst_dir, &dst_dir_u,
637 ATTR_MTIME|ATTR_CTIME);
639 bch2_inode_update_after_write(&trans, src_inode, &src_inode_u,
643 bch2_inode_update_after_write(&trans, dst_inode, &dst_inode_u,
646 bch2_trans_exit(&trans);
648 bch2_fs_quota_transfer(c, src_inode,
649 bch_qid(&src_inode->ei_inode),
651 KEY_TYPE_QUOTA_NOCHECK);
653 bch2_fs_quota_transfer(c, dst_inode,
654 bch_qid(&dst_inode->ei_inode),
656 KEY_TYPE_QUOTA_NOCHECK);
658 bch2_unlock_inodes(INODE_UPDATE_LOCK,
667 static void bch2_setattr_copy(struct user_namespace *mnt_userns,
668 struct bch_inode_info *inode,
669 struct bch_inode_unpacked *bi,
672 struct bch_fs *c = inode->v.i_sb->s_fs_info;
673 unsigned int ia_valid = attr->ia_valid;
675 if (ia_valid & ATTR_UID)
676 bi->bi_uid = from_kuid(mnt_userns, attr->ia_uid);
677 if (ia_valid & ATTR_GID)
678 bi->bi_gid = from_kgid(mnt_userns, attr->ia_gid);
680 if (ia_valid & ATTR_SIZE)
681 bi->bi_size = attr->ia_size;
683 if (ia_valid & ATTR_ATIME)
684 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
685 if (ia_valid & ATTR_MTIME)
686 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
687 if (ia_valid & ATTR_CTIME)
688 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
690 if (ia_valid & ATTR_MODE) {
691 umode_t mode = attr->ia_mode;
692 kgid_t gid = ia_valid & ATTR_GID
696 if (!in_group_p(gid) &&
697 !capable_wrt_inode_uidgid(mnt_userns, &inode->v, CAP_FSETID))
703 int bch2_setattr_nonsize(struct user_namespace *mnt_userns,
704 struct bch_inode_info *inode,
707 struct bch_fs *c = inode->v.i_sb->s_fs_info;
709 struct btree_trans trans;
710 struct btree_iter inode_iter = { NULL };
711 struct bch_inode_unpacked inode_u;
712 struct posix_acl *acl = NULL;
715 mutex_lock(&inode->ei_update_lock);
719 if (attr->ia_valid & ATTR_UID)
720 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
722 if (attr->ia_valid & ATTR_GID)
723 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
725 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
726 KEY_TYPE_QUOTA_PREALLOC);
730 bch2_trans_init(&trans, c, 0, 0);
732 bch2_trans_begin(&trans);
736 ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode_inum(inode),
741 bch2_setattr_copy(mnt_userns, inode, &inode_u, attr);
743 if (attr->ia_valid & ATTR_MODE) {
744 ret = bch2_acl_chmod(&trans, inode_inum(inode), &inode_u,
745 inode_u.bi_mode, &acl);
750 ret = bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
751 bch2_trans_commit(&trans, NULL, NULL,
752 BTREE_INSERT_NOFAIL);
754 bch2_trans_iter_exit(&trans, &inode_iter);
761 bch2_inode_update_after_write(&trans, inode, &inode_u, attr->ia_valid);
764 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
766 bch2_trans_exit(&trans);
768 mutex_unlock(&inode->ei_update_lock);
773 static int bch2_getattr(struct user_namespace *mnt_userns,
774 const struct path *path, struct kstat *stat,
775 u32 request_mask, unsigned query_flags)
777 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
778 struct bch_fs *c = inode->v.i_sb->s_fs_info;
780 stat->dev = inode->v.i_sb->s_dev;
781 stat->ino = inode->v.i_ino;
782 stat->mode = inode->v.i_mode;
783 stat->nlink = inode->v.i_nlink;
784 stat->uid = inode->v.i_uid;
785 stat->gid = inode->v.i_gid;
786 stat->rdev = inode->v.i_rdev;
787 stat->size = i_size_read(&inode->v);
788 stat->atime = inode->v.i_atime;
789 stat->mtime = inode->v.i_mtime;
790 stat->ctime = inode->v.i_ctime;
791 stat->blksize = block_bytes(c);
792 stat->blocks = inode->v.i_blocks;
794 if (request_mask & STATX_BTIME) {
795 stat->result_mask |= STATX_BTIME;
796 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
799 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
800 stat->attributes |= STATX_ATTR_IMMUTABLE;
801 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
803 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
804 stat->attributes |= STATX_ATTR_APPEND;
805 stat->attributes_mask |= STATX_ATTR_APPEND;
807 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
808 stat->attributes |= STATX_ATTR_NODUMP;
809 stat->attributes_mask |= STATX_ATTR_NODUMP;
814 static int bch2_setattr(struct user_namespace *mnt_userns,
815 struct dentry *dentry, struct iattr *iattr)
817 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
820 lockdep_assert_held(&inode->v.i_rwsem);
822 ret = setattr_prepare(mnt_userns, dentry, iattr);
826 return iattr->ia_valid & ATTR_SIZE
827 ? bch2_truncate(mnt_userns, inode, iattr)
828 : bch2_setattr_nonsize(mnt_userns, inode, iattr);
831 static int bch2_tmpfile(struct user_namespace *mnt_userns,
832 struct inode *vdir, struct dentry *dentry, umode_t mode)
834 struct bch_inode_info *inode =
835 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, 0,
836 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
839 return PTR_ERR(inode);
841 d_mark_tmpfile(dentry, &inode->v);
842 d_instantiate(dentry, &inode->v);
846 static int bch2_fill_extent(struct bch_fs *c,
847 struct fiemap_extent_info *info,
848 struct bkey_s_c k, unsigned flags)
850 if (bkey_extent_is_direct_data(k.k)) {
851 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
852 const union bch_extent_entry *entry;
853 struct extent_ptr_decoded p;
856 if (k.k->type == KEY_TYPE_reflink_v)
857 flags |= FIEMAP_EXTENT_SHARED;
859 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
861 u64 offset = p.ptr.offset;
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 bch2_trans_init(&trans, c, 0, 0);
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 ((k = bch2_btree_iter_peek(&iter)).k &&
937 !(ret = bkey_err(k)) &&
938 bkey_cmp(iter.pos, end) < 0) {
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 ret = bch2_fill_extent(c, info,
973 bkey_i_to_s_c(prev.k), 0);
978 bkey_copy(prev.k, cur.k);
981 bch2_btree_iter_set_pos(&iter,
982 POS(iter.pos.inode, iter.pos.offset + sectors));
984 start = iter.pos.offset;
985 bch2_trans_iter_exit(&trans, &iter);
990 if (!ret && have_extent)
991 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
994 bch2_trans_exit(&trans);
995 bch2_bkey_buf_exit(&cur, c);
996 bch2_bkey_buf_exit(&prev, c);
997 return ret < 0 ? ret : 0;
1000 static const struct vm_operations_struct bch_vm_ops = {
1001 .fault = bch2_page_fault,
1002 .map_pages = filemap_map_pages,
1003 .page_mkwrite = bch2_page_mkwrite,
1006 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1008 file_accessed(file);
1010 vma->vm_ops = &bch_vm_ops;
1016 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1018 return generic_file_llseek_size(file, offset, whence,
1022 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1024 struct bch_inode_info *inode = file_bch_inode(file);
1025 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1027 if (!dir_emit_dots(file, ctx))
1030 return bch2_readdir(c, inode_inum(inode), ctx);
1033 static const struct file_operations bch_file_operations = {
1034 .llseek = bch2_llseek,
1035 .read_iter = bch2_read_iter,
1036 .write_iter = bch2_write_iter,
1038 .open = generic_file_open,
1039 .fsync = bch2_fsync,
1040 .splice_read = generic_file_splice_read,
1041 .splice_write = iter_file_splice_write,
1042 .fallocate = bch2_fallocate_dispatch,
1043 .unlocked_ioctl = bch2_fs_file_ioctl,
1044 #ifdef CONFIG_COMPAT
1045 .compat_ioctl = bch2_compat_fs_ioctl,
1047 .remap_file_range = bch2_remap_file_range,
1050 static const struct inode_operations bch_file_inode_operations = {
1051 .getattr = bch2_getattr,
1052 .setattr = bch2_setattr,
1053 .fiemap = bch2_fiemap,
1054 .listxattr = bch2_xattr_list,
1055 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1056 .get_acl = bch2_get_acl,
1057 .set_acl = bch2_set_acl,
1061 static const struct inode_operations bch_dir_inode_operations = {
1062 .lookup = bch2_lookup,
1063 .create = bch2_create,
1065 .unlink = bch2_unlink,
1066 .symlink = bch2_symlink,
1067 .mkdir = bch2_mkdir,
1068 .rmdir = bch2_unlink,
1069 .mknod = bch2_mknod,
1070 .rename = bch2_rename2,
1071 .getattr = bch2_getattr,
1072 .setattr = bch2_setattr,
1073 .tmpfile = bch2_tmpfile,
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 file_operations bch_dir_file_operations = {
1082 .llseek = bch2_dir_llseek,
1083 .read = generic_read_dir,
1084 .iterate_shared = bch2_vfs_readdir,
1085 .fsync = bch2_fsync,
1086 .unlocked_ioctl = bch2_fs_file_ioctl,
1087 #ifdef CONFIG_COMPAT
1088 .compat_ioctl = bch2_compat_fs_ioctl,
1092 static const struct inode_operations bch_symlink_inode_operations = {
1093 .get_link = page_get_link,
1094 .getattr = bch2_getattr,
1095 .setattr = bch2_setattr,
1096 .listxattr = bch2_xattr_list,
1097 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1098 .get_acl = bch2_get_acl,
1099 .set_acl = bch2_set_acl,
1103 static const struct inode_operations bch_special_inode_operations = {
1104 .getattr = bch2_getattr,
1105 .setattr = bch2_setattr,
1106 .listxattr = bch2_xattr_list,
1107 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1108 .get_acl = bch2_get_acl,
1109 .set_acl = bch2_set_acl,
1113 static const struct address_space_operations bch_address_space_operations = {
1114 .writepage = bch2_writepage,
1115 .readpage = bch2_readpage,
1116 .writepages = bch2_writepages,
1117 .readahead = bch2_readahead,
1118 .set_page_dirty = __set_page_dirty_nobuffers,
1119 .write_begin = bch2_write_begin,
1120 .write_end = bch2_write_end,
1121 .invalidatepage = bch2_invalidatepage,
1122 .releasepage = bch2_releasepage,
1123 .direct_IO = noop_direct_IO,
1124 #ifdef CONFIG_MIGRATION
1125 .migratepage = bch2_migrate_page,
1127 .error_remove_page = generic_error_remove_page,
1130 struct bcachefs_fid {
1136 struct bcachefs_fid_with_parent {
1137 struct bcachefs_fid fid;
1138 struct bcachefs_fid dir;
1141 static int bcachefs_fid_valid(int fh_len, int fh_type)
1144 case FILEID_BCACHEFS_WITHOUT_PARENT:
1145 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1146 case FILEID_BCACHEFS_WITH_PARENT:
1147 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1153 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1155 return (struct bcachefs_fid) {
1156 .inum = inode->ei_inode.bi_inum,
1157 .subvol = inode->ei_subvol,
1158 .gen = inode->ei_inode.bi_generation,
1162 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1165 struct bch_inode_info *inode = to_bch_ei(vinode);
1166 struct bch_inode_info *dir = to_bch_ei(vdir);
1168 if (*len < sizeof(struct bcachefs_fid_with_parent) / sizeof(u32))
1169 return FILEID_INVALID;
1171 if (!S_ISDIR(inode->v.i_mode) && dir) {
1172 struct bcachefs_fid_with_parent *fid = (void *) fh;
1174 fid->fid = bch2_inode_to_fid(inode);
1175 fid->dir = bch2_inode_to_fid(dir);
1177 *len = sizeof(*fid) / sizeof(u32);
1178 return FILEID_BCACHEFS_WITH_PARENT;
1180 struct bcachefs_fid *fid = (void *) fh;
1182 *fid = bch2_inode_to_fid(inode);
1184 *len = sizeof(*fid) / sizeof(u32);
1185 return FILEID_BCACHEFS_WITHOUT_PARENT;
1189 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1190 struct bcachefs_fid fid)
1192 struct bch_fs *c = sb->s_fs_info;
1193 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1194 .subvol = fid.subvol,
1197 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1199 vinode = ERR_PTR(-ESTALE);
1204 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1205 int fh_len, int fh_type)
1207 struct bcachefs_fid *fid = (void *) _fid;
1209 if (!bcachefs_fid_valid(fh_len, fh_type))
1212 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1215 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1216 int fh_len, int fh_type)
1218 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1220 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1221 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1224 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1227 static struct dentry *bch2_get_parent(struct dentry *child)
1229 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1230 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1231 subvol_inum parent_inum = {
1232 .subvol = inode->ei_inode.bi_parent_subvol ?:
1234 .inum = inode->ei_inode.bi_dir,
1237 if (!parent_inum.inum)
1240 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1243 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1245 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1246 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1247 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1248 struct btree_trans trans;
1249 struct btree_iter iter1;
1250 struct btree_iter iter2;
1252 struct bkey_s_c_dirent d;
1253 struct bch_inode_unpacked inode_u;
1259 if (!S_ISDIR(dir->v.i_mode))
1262 bch2_trans_init(&trans, c, 0, 0);
1264 bch2_trans_iter_init(&trans, &iter1, BTREE_ID_dirents,
1265 POS(dir->ei_inode.bi_inum, 0), 0);
1266 bch2_trans_iter_init(&trans, &iter2, BTREE_ID_dirents,
1267 POS(dir->ei_inode.bi_inum, 0), 0);
1269 bch2_trans_begin(&trans);
1271 ret = bch2_subvolume_get_snapshot(&trans, dir->ei_subvol, &snapshot);
1275 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1276 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1278 ret = bch2_inode_find_by_inum_trans(&trans, inode_inum(inode), &inode_u);
1282 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1283 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1285 k = bch2_btree_iter_peek_slot(&iter1);
1290 if (k.k->type != KEY_TYPE_dirent) {
1295 d = bkey_s_c_to_dirent(k);
1296 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1302 if (target.subvol == inode->ei_subvol &&
1303 target.inum == inode->ei_inode.bi_inum)
1307 * File with multiple hardlinks and our backref is to the wrong
1308 * directory - linear search:
1310 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1311 if (k.k->p.inode > dir->ei_inode.bi_inum)
1314 if (k.k->type != KEY_TYPE_dirent)
1317 d = bkey_s_c_to_dirent(k);
1318 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1324 if (target.subvol == inode->ei_subvol &&
1325 target.inum == inode->ei_inode.bi_inum)
1333 name_len = min_t(unsigned, bch2_dirent_name_bytes(d), NAME_MAX);
1335 memcpy(name, d.v->d_name, name_len);
1336 name[name_len] = '\0';
1341 bch2_trans_iter_exit(&trans, &iter1);
1342 bch2_trans_iter_exit(&trans, &iter2);
1343 bch2_trans_exit(&trans);
1348 static const struct export_operations bch_export_ops = {
1349 .encode_fh = bch2_encode_fh,
1350 .fh_to_dentry = bch2_fh_to_dentry,
1351 .fh_to_parent = bch2_fh_to_parent,
1352 .get_parent = bch2_get_parent,
1353 .get_name = bch2_get_name,
1356 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1357 struct bch_inode_info *inode,
1358 struct bch_inode_unpacked *bi,
1359 struct bch_subvolume *subvol)
1361 bch2_inode_update_after_write(trans, inode, bi, ~0);
1363 if (BCH_SUBVOLUME_SNAP(subvol))
1364 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1366 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1368 inode->v.i_blocks = bi->bi_sectors;
1369 inode->v.i_ino = bi->bi_inum;
1370 inode->v.i_rdev = bi->bi_dev;
1371 inode->v.i_generation = bi->bi_generation;
1372 inode->v.i_size = bi->bi_size;
1374 inode->ei_flags = 0;
1375 inode->ei_quota_reserved = 0;
1376 inode->ei_qid = bch_qid(bi);
1377 inode->ei_subvol = inum.subvol;
1379 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1381 switch (inode->v.i_mode & S_IFMT) {
1383 inode->v.i_op = &bch_file_inode_operations;
1384 inode->v.i_fop = &bch_file_operations;
1387 inode->v.i_op = &bch_dir_inode_operations;
1388 inode->v.i_fop = &bch_dir_file_operations;
1391 inode_nohighmem(&inode->v);
1392 inode->v.i_op = &bch_symlink_inode_operations;
1395 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1396 inode->v.i_op = &bch_special_inode_operations;
1401 static struct inode *bch2_alloc_inode(struct super_block *sb)
1403 struct bch_inode_info *inode;
1405 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1409 inode_init_once(&inode->v);
1410 mutex_init(&inode->ei_update_lock);
1411 pagecache_lock_init(&inode->ei_pagecache_lock);
1412 mutex_init(&inode->ei_quota_lock);
1417 static void bch2_i_callback(struct rcu_head *head)
1419 struct inode *vinode = container_of(head, struct inode, i_rcu);
1420 struct bch_inode_info *inode = to_bch_ei(vinode);
1422 kmem_cache_free(bch2_inode_cache, inode);
1425 static void bch2_destroy_inode(struct inode *vinode)
1427 call_rcu(&vinode->i_rcu, bch2_i_callback);
1430 static int inode_update_times_fn(struct bch_inode_info *inode,
1431 struct bch_inode_unpacked *bi,
1434 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1436 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1437 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1438 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1443 static int bch2_vfs_write_inode(struct inode *vinode,
1444 struct writeback_control *wbc)
1446 struct bch_fs *c = vinode->i_sb->s_fs_info;
1447 struct bch_inode_info *inode = to_bch_ei(vinode);
1450 mutex_lock(&inode->ei_update_lock);
1451 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1452 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1453 mutex_unlock(&inode->ei_update_lock);
1458 static void bch2_evict_inode(struct inode *vinode)
1460 struct bch_fs *c = vinode->i_sb->s_fs_info;
1461 struct bch_inode_info *inode = to_bch_ei(vinode);
1463 truncate_inode_pages_final(&inode->v.i_data);
1465 clear_inode(&inode->v);
1467 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1469 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1470 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1471 KEY_TYPE_QUOTA_WARN);
1472 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1473 KEY_TYPE_QUOTA_WARN);
1474 bch2_inode_rm(c, inode_inum(inode));
1478 void bch2_evict_subvolume_inodes(struct bch_fs *c,
1479 struct snapshot_id_list *s)
1481 struct super_block *sb = c->vfs_sb;
1482 struct inode *inode;
1484 spin_lock(&sb->s_inode_list_lock);
1485 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1486 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1487 (inode->i_state & I_FREEING))
1490 d_mark_dontcache(inode);
1491 d_prune_aliases(inode);
1493 spin_unlock(&sb->s_inode_list_lock);
1496 spin_lock(&sb->s_inode_list_lock);
1497 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1498 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1499 (inode->i_state & I_FREEING))
1502 if (!(inode->i_state & I_DONTCACHE)) {
1503 d_mark_dontcache(inode);
1504 d_prune_aliases(inode);
1507 spin_lock(&inode->i_lock);
1508 if (snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) &&
1509 !(inode->i_state & I_FREEING)) {
1510 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_NEW);
1511 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1512 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1513 spin_unlock(&inode->i_lock);
1514 spin_unlock(&sb->s_inode_list_lock);
1516 finish_wait(wq, &wait.wq_entry);
1520 spin_unlock(&inode->i_lock);
1522 spin_unlock(&sb->s_inode_list_lock);
1525 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1527 struct super_block *sb = dentry->d_sb;
1528 struct bch_fs *c = sb->s_fs_info;
1529 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1530 unsigned shift = sb->s_blocksize_bits - 9;
1532 * this assumes inodes take up 64 bytes, which is a decent average
1535 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1538 buf->f_type = BCACHEFS_STATFS_MAGIC;
1539 buf->f_bsize = sb->s_blocksize;
1540 buf->f_blocks = usage.capacity >> shift;
1541 buf->f_bfree = usage.free >> shift;
1542 buf->f_bavail = avail_factor(usage.free) >> shift;
1544 buf->f_files = usage.nr_inodes + avail_inodes;
1545 buf->f_ffree = avail_inodes;
1547 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1548 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1549 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1550 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1551 buf->f_namelen = BCH_NAME_MAX;
1556 static int bch2_sync_fs(struct super_block *sb, int wait)
1558 struct bch_fs *c = sb->s_fs_info;
1560 if (c->opts.journal_flush_disabled)
1564 bch2_journal_flush_async(&c->journal, NULL);
1568 return bch2_journal_flush(&c->journal);
1571 static struct bch_fs *bch2_path_to_fs(const char *path)
1577 ret = lookup_bdev(path, &dev);
1579 return ERR_PTR(ret);
1581 c = bch2_dev_to_fs(dev);
1583 closure_put(&c->cl);
1584 return c ?: ERR_PTR(-ENOENT);
1587 static char **split_devs(const char *_dev_name, unsigned *nr)
1589 char *dev_name = NULL, **devs = NULL, *s;
1590 size_t i, nr_devs = 0;
1592 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1596 for (s = dev_name; s; s = strchr(s + 1, ':'))
1599 devs = kcalloc(nr_devs + 1, sizeof(const char *), GFP_KERNEL);
1605 for (i = 0, s = dev_name;
1607 (s = strchr(s, ':')) && (*s++ = '\0'))
1614 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1616 struct bch_fs *c = sb->s_fs_info;
1617 struct bch_opts opts = bch2_opts_empty();
1620 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1622 ret = bch2_parse_mount_opts(c, &opts, data);
1626 if (opts.read_only != c->opts.read_only) {
1627 down_write(&c->state_lock);
1629 if (opts.read_only) {
1630 bch2_fs_read_only(c);
1632 sb->s_flags |= SB_RDONLY;
1634 ret = bch2_fs_read_write(c);
1636 bch_err(c, "error going rw: %i", ret);
1637 up_write(&c->state_lock);
1641 sb->s_flags &= ~SB_RDONLY;
1644 c->opts.read_only = opts.read_only;
1646 up_write(&c->state_lock);
1649 if (opts.errors >= 0)
1650 c->opts.errors = opts.errors;
1655 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1657 struct bch_fs *c = root->d_sb->s_fs_info;
1662 for_each_online_member(ca, c, i) {
1666 seq_puts(seq, "/dev/");
1667 seq_puts(seq, ca->name);
1673 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1675 struct bch_fs *c = root->d_sb->s_fs_info;
1679 for (i = 0; i < bch2_opts_nr; i++) {
1680 const struct bch_option *opt = &bch2_opt_table[i];
1681 u64 v = bch2_opt_get_by_id(&c->opts, i);
1683 if (!(opt->flags & OPT_MOUNT))
1686 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1689 bch2_opt_to_text(&PBUF(buf), c, opt, v,
1690 OPT_SHOW_MOUNT_STYLE);
1698 static void bch2_put_super(struct super_block *sb)
1700 struct bch_fs *c = sb->s_fs_info;
1705 static const struct super_operations bch_super_operations = {
1706 .alloc_inode = bch2_alloc_inode,
1707 .destroy_inode = bch2_destroy_inode,
1708 .write_inode = bch2_vfs_write_inode,
1709 .evict_inode = bch2_evict_inode,
1710 .sync_fs = bch2_sync_fs,
1711 .statfs = bch2_statfs,
1712 .show_devname = bch2_show_devname,
1713 .show_options = bch2_show_options,
1714 .remount_fs = bch2_remount,
1715 .put_super = bch2_put_super,
1717 .freeze_fs = bch2_freeze,
1718 .unfreeze_fs = bch2_unfreeze,
1722 static int bch2_set_super(struct super_block *s, void *data)
1724 s->s_fs_info = data;
1728 static int bch2_noset_super(struct super_block *s, void *data)
1733 static int bch2_test_super(struct super_block *s, void *data)
1735 struct bch_fs *c = s->s_fs_info;
1736 struct bch_fs **devs = data;
1742 for (i = 0; devs[i]; i++)
1748 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1749 int flags, const char *dev_name, void *data)
1753 struct super_block *sb;
1754 struct inode *vinode;
1755 struct bch_opts opts = bch2_opts_empty();
1757 struct bch_fs **devs_to_fs = NULL;
1758 unsigned i, nr_devs;
1761 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1763 ret = bch2_parse_mount_opts(NULL, &opts, data);
1765 return ERR_PTR(ret);
1767 if (!dev_name || strlen(dev_name) == 0)
1768 return ERR_PTR(-EINVAL);
1770 devs = split_devs(dev_name, &nr_devs);
1772 return ERR_PTR(-ENOMEM);
1774 devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
1776 sb = ERR_PTR(-ENOMEM);
1780 for (i = 0; i < nr_devs; i++)
1781 devs_to_fs[i] = bch2_path_to_fs(devs[i]);
1783 sb = sget(fs_type, bch2_test_super, bch2_noset_super,
1784 flags|SB_NOSEC, devs_to_fs);
1788 c = bch2_fs_open(devs, nr_devs, opts);
1794 /* Some options can't be parsed until after the fs is started: */
1795 ret = bch2_parse_mount_opts(c, &opts, data);
1802 bch2_opts_apply(&c->opts, opts);
1804 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1813 return ERR_CAST(sb);
1818 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1825 sb->s_blocksize = block_bytes(c);
1826 sb->s_blocksize_bits = ilog2(block_bytes(c));
1827 sb->s_maxbytes = MAX_LFS_FILESIZE;
1828 sb->s_op = &bch_super_operations;
1829 sb->s_export_op = &bch_export_ops;
1830 #ifdef CONFIG_BCACHEFS_QUOTA
1831 sb->s_qcop = &bch2_quotactl_operations;
1832 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1834 sb->s_xattr = bch2_xattr_handlers;
1835 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1836 sb->s_time_gran = c->sb.nsec_per_time_unit;
1837 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1838 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1840 strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1842 ret = super_setup_bdi(sb);
1846 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1848 for_each_online_member(ca, c, i) {
1849 struct block_device *bdev = ca->disk_sb.bdev;
1851 /* XXX: create an anonymous device for multi device filesystems */
1853 sb->s_dev = bdev->bd_dev;
1854 percpu_ref_put(&ca->io_ref);
1860 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1862 sb->s_flags |= SB_POSIXACL;
1865 sb->s_shrink.seeks = 0;
1867 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1868 if (IS_ERR(vinode)) {
1869 bch_err(c, "error mounting: error getting root inode %i",
1870 (int) PTR_ERR(vinode));
1871 ret = PTR_ERR(vinode);
1875 sb->s_root = d_make_root(vinode);
1877 bch_err(c, "error mounting: error allocating root dentry");
1882 sb->s_flags |= SB_ACTIVE;
1884 return dget(sb->s_root);
1887 deactivate_locked_super(sb);
1888 return ERR_PTR(ret);
1891 static void bch2_kill_sb(struct super_block *sb)
1893 struct bch_fs *c = sb->s_fs_info;
1895 generic_shutdown_super(sb);
1899 static struct file_system_type bcache_fs_type = {
1900 .owner = THIS_MODULE,
1902 .mount = bch2_mount,
1903 .kill_sb = bch2_kill_sb,
1904 .fs_flags = FS_REQUIRES_DEV,
1907 MODULE_ALIAS_FS("bcachefs");
1909 void bch2_vfs_exit(void)
1911 unregister_filesystem(&bcache_fs_type);
1912 if (bch2_inode_cache)
1913 kmem_cache_destroy(bch2_inode_cache);
1916 int __init bch2_vfs_init(void)
1920 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1921 if (!bch2_inode_cache)
1924 ret = register_filesystem(&bcache_fs_type);
1934 #endif /* NO_BCACHEFS_FS */