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/seq_file.h>
34 #include <linux/statfs.h>
35 #include <linux/string.h>
36 #include <linux/xattr.h>
38 static struct kmem_cache *bch2_inode_cache;
40 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
41 struct bch_inode_info *,
42 struct bch_inode_unpacked *,
43 struct bch_subvolume *);
45 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
47 BUG_ON(atomic_long_read(&lock->v) == 0);
49 if (atomic_long_sub_return_release(i, &lock->v) == 0)
50 wake_up_all(&lock->wait);
53 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
55 long v = atomic_long_read(&lock->v), old;
60 if (i > 0 ? v < 0 : v > 0)
62 } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
63 old, old + i)) != old);
67 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
69 wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
72 void bch2_pagecache_add_put(struct pagecache_lock *lock)
74 __pagecache_lock_put(lock, 1);
77 bool bch2_pagecache_add_tryget(struct pagecache_lock *lock)
79 return __pagecache_lock_tryget(lock, 1);
82 void bch2_pagecache_add_get(struct pagecache_lock *lock)
84 __pagecache_lock_get(lock, 1);
87 void bch2_pagecache_block_put(struct pagecache_lock *lock)
89 __pagecache_lock_put(lock, -1);
92 void bch2_pagecache_block_get(struct pagecache_lock *lock)
94 __pagecache_lock_get(lock, -1);
97 void bch2_inode_update_after_write(struct btree_trans *trans,
98 struct bch_inode_info *inode,
99 struct bch_inode_unpacked *bi,
102 struct bch_fs *c = trans->c;
104 BUG_ON(bi->bi_inum != inode->v.i_ino);
106 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
108 c->opts.inodes_use_key_cache);
110 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
111 i_uid_write(&inode->v, bi->bi_uid);
112 i_gid_write(&inode->v, bi->bi_gid);
113 inode->v.i_mode = bi->bi_mode;
115 if (fields & ATTR_ATIME)
116 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
117 if (fields & ATTR_MTIME)
118 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
119 if (fields & ATTR_CTIME)
120 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
122 inode->ei_inode = *bi;
124 bch2_inode_flags_to_vfs(inode);
127 int __must_check bch2_write_inode(struct bch_fs *c,
128 struct bch_inode_info *inode,
130 void *p, unsigned fields)
132 struct btree_trans trans;
133 struct btree_iter iter = { NULL };
134 struct bch_inode_unpacked inode_u;
137 bch2_trans_init(&trans, c, 0, 512);
139 bch2_trans_begin(&trans);
141 ret = bch2_inode_peek(&trans, &iter, &inode_u, inode_inum(inode),
142 BTREE_ITER_INTENT) ?:
143 (set ? set(inode, &inode_u, p) : 0) ?:
144 bch2_inode_write(&trans, &iter, &inode_u) ?:
145 bch2_trans_commit(&trans, NULL, NULL, BTREE_INSERT_NOFAIL);
148 * the btree node lock protects inode->ei_inode, not ei_update_lock;
149 * this is important for inode updates via bchfs_write_index_update
152 bch2_inode_update_after_write(&trans, inode, &inode_u, fields);
154 bch2_trans_iter_exit(&trans, &iter);
159 bch2_trans_exit(&trans);
160 return ret < 0 ? ret : 0;
163 int bch2_fs_quota_transfer(struct bch_fs *c,
164 struct bch_inode_info *inode,
165 struct bch_qid new_qid,
167 enum quota_acct_mode mode)
172 qtypes &= enabled_qtypes(c);
174 for (i = 0; i < QTYP_NR; i++)
175 if (new_qid.q[i] == inode->ei_qid.q[i])
176 qtypes &= ~(1U << i);
181 mutex_lock(&inode->ei_quota_lock);
183 ret = bch2_quota_transfer(c, qtypes, new_qid,
186 inode->ei_quota_reserved,
189 for (i = 0; i < QTYP_NR; i++)
190 if (qtypes & (1 << i))
191 inode->ei_qid.q[i] = new_qid.q[i];
193 mutex_unlock(&inode->ei_quota_lock);
198 static int bch2_iget5_test(struct inode *vinode, void *p)
200 struct bch_inode_info *inode = to_bch_ei(vinode);
201 subvol_inum *inum = p;
203 return inode->ei_subvol == inum->subvol &&
204 inode->ei_inode.bi_inum == inum->inum;
207 static int bch2_iget5_set(struct inode *vinode, void *p)
209 struct bch_inode_info *inode = to_bch_ei(vinode);
210 subvol_inum *inum = p;
212 inode->v.i_ino = inum->inum;
213 inode->ei_subvol = inum->subvol;
214 inode->ei_inode.bi_inum = inum->inum;
218 static unsigned bch2_inode_hash(subvol_inum inum)
220 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
223 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
225 struct bch_inode_unpacked inode_u;
226 struct bch_inode_info *inode;
227 struct btree_trans trans;
228 struct bch_subvolume subvol;
231 inode = to_bch_ei(iget5_locked(c->vfs_sb,
232 bch2_inode_hash(inum),
236 if (unlikely(!inode))
237 return ERR_PTR(-ENOMEM);
238 if (!(inode->v.i_state & I_NEW))
241 bch2_trans_init(&trans, c, 8, 0);
242 ret = lockrestart_do(&trans,
243 bch2_subvolume_get(&trans, inum.subvol, true, 0, &subvol) ?:
244 bch2_inode_find_by_inum_trans(&trans, inum, &inode_u));
247 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
248 bch2_trans_exit(&trans);
251 iget_failed(&inode->v);
255 unlock_new_inode(&inode->v);
260 struct bch_inode_info *
261 __bch2_create(struct user_namespace *mnt_userns,
262 struct bch_inode_info *dir, struct dentry *dentry,
263 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
266 struct bch_fs *c = dir->v.i_sb->s_fs_info;
267 struct btree_trans trans;
268 struct bch_inode_unpacked dir_u;
269 struct bch_inode_info *inode, *old;
270 struct bch_inode_unpacked inode_u;
271 struct posix_acl *default_acl = NULL, *acl = NULL;
273 struct bch_subvolume subvol;
278 * preallocate acls + vfs inode before btree transaction, so that
279 * nothing can fail after the transaction succeeds:
281 #ifdef CONFIG_BCACHEFS_POSIX_ACL
282 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
286 inode = to_bch_ei(new_inode(c->vfs_sb));
287 if (unlikely(!inode)) {
288 inode = ERR_PTR(-ENOMEM);
292 bch2_inode_init_early(c, &inode_u);
294 if (!(flags & BCH_CREATE_TMPFILE))
295 mutex_lock(&dir->ei_update_lock);
297 bch2_trans_init(&trans, c, 8,
298 2048 + (!(flags & BCH_CREATE_TMPFILE)
299 ? dentry->d_name.len : 0));
301 bch2_trans_begin(&trans);
303 ret = bch2_create_trans(&trans,
304 inode_inum(dir), &dir_u, &inode_u,
305 !(flags & BCH_CREATE_TMPFILE)
306 ? &dentry->d_name : NULL,
307 from_kuid(mnt_userns, current_fsuid()),
308 from_kgid(mnt_userns, current_fsgid()),
310 default_acl, acl, snapshot_src, flags) ?:
311 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
312 KEY_TYPE_QUOTA_PREALLOC);
314 goto err_before_quota;
316 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
317 inum.inum = inode_u.bi_inum;
319 ret = bch2_subvolume_get(&trans, inum.subvol, true,
320 BTREE_ITER_WITH_UPDATES, &subvol) ?:
321 bch2_trans_commit(&trans, NULL, &journal_seq, 0);
323 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
324 KEY_TYPE_QUOTA_WARN);
331 if (!(flags & BCH_CREATE_TMPFILE)) {
332 bch2_inode_update_after_write(&trans, dir, &dir_u,
333 ATTR_MTIME|ATTR_CTIME);
334 mutex_unlock(&dir->ei_update_lock);
337 bch2_iget5_set(&inode->v, &inum);
338 bch2_vfs_inode_init(&trans, inum, inode, &inode_u, &subvol);
340 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
341 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
344 * we must insert the new inode into the inode cache before calling
345 * bch2_trans_exit() and dropping locks, else we could race with another
346 * thread pulling the inode in and modifying it:
349 inode->v.i_state |= I_CREATING;
351 old = to_bch_ei(inode_insert5(&inode->v,
352 bch2_inode_hash(inum),
358 if (unlikely(old != inode)) {
360 * We raced, another process pulled the new inode into cache
363 make_bad_inode(&inode->v);
369 * we really don't want insert_inode_locked2() to be setting
372 unlock_new_inode(&inode->v);
375 bch2_trans_exit(&trans);
377 posix_acl_release(default_acl);
378 posix_acl_release(acl);
381 if (!(flags & BCH_CREATE_TMPFILE))
382 mutex_unlock(&dir->ei_update_lock);
384 bch2_trans_exit(&trans);
385 make_bad_inode(&inode->v);
387 inode = ERR_PTR(ret);
393 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
396 struct bch_fs *c = vdir->i_sb->s_fs_info;
397 struct bch_inode_info *dir = to_bch_ei(vdir);
398 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
399 struct inode *vinode = NULL;
400 subvol_inum inum = { .subvol = 1 };
403 ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
404 &dentry->d_name, &inum);
407 vinode = bch2_vfs_inode_get(c, inum);
409 return d_splice_alias(vinode, dentry);
412 static int bch2_mknod(struct user_namespace *mnt_userns,
413 struct inode *vdir, struct dentry *dentry,
414 umode_t mode, dev_t rdev)
416 struct bch_inode_info *inode =
417 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, rdev,
418 (subvol_inum) { 0 }, 0);
421 return PTR_ERR(inode);
423 d_instantiate(dentry, &inode->v);
427 static int bch2_create(struct user_namespace *mnt_userns,
428 struct inode *vdir, struct dentry *dentry,
429 umode_t mode, bool excl)
431 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFREG, 0);
434 static int __bch2_link(struct bch_fs *c,
435 struct bch_inode_info *inode,
436 struct bch_inode_info *dir,
437 struct dentry *dentry)
439 struct btree_trans trans;
440 struct bch_inode_unpacked dir_u, inode_u;
443 mutex_lock(&inode->ei_update_lock);
444 bch2_trans_init(&trans, c, 4, 1024);
446 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
447 bch2_link_trans(&trans,
448 inode_inum(dir), &dir_u,
449 inode_inum(inode), &inode_u,
453 bch2_inode_update_after_write(&trans, dir, &dir_u,
454 ATTR_MTIME|ATTR_CTIME);
455 bch2_inode_update_after_write(&trans, inode, &inode_u, ATTR_CTIME);
458 bch2_trans_exit(&trans);
459 mutex_unlock(&inode->ei_update_lock);
463 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
464 struct dentry *dentry)
466 struct bch_fs *c = vdir->i_sb->s_fs_info;
467 struct bch_inode_info *dir = to_bch_ei(vdir);
468 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
471 lockdep_assert_held(&inode->v.i_rwsem);
473 ret = __bch2_link(c, inode, dir, dentry);
478 d_instantiate(dentry, &inode->v);
482 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
483 bool deleting_snapshot)
485 struct bch_fs *c = vdir->i_sb->s_fs_info;
486 struct bch_inode_info *dir = to_bch_ei(vdir);
487 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
488 struct bch_inode_unpacked dir_u, inode_u;
489 struct btree_trans trans;
492 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
493 bch2_trans_init(&trans, c, 4, 1024);
495 ret = __bch2_trans_do(&trans, NULL, NULL,
497 bch2_unlink_trans(&trans,
498 inode_inum(dir), &dir_u,
499 &inode_u, &dentry->d_name,
503 bch2_inode_update_after_write(&trans, dir, &dir_u,
504 ATTR_MTIME|ATTR_CTIME);
505 bch2_inode_update_after_write(&trans, inode, &inode_u,
509 bch2_trans_exit(&trans);
510 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
515 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
517 return __bch2_unlink(vdir, dentry, false);
520 static int bch2_symlink(struct user_namespace *mnt_userns,
521 struct inode *vdir, struct dentry *dentry,
524 struct bch_fs *c = vdir->i_sb->s_fs_info;
525 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
528 inode = __bch2_create(mnt_userns, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
529 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
530 if (unlikely(IS_ERR(inode)))
531 return PTR_ERR(inode);
533 inode_lock(&inode->v);
534 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
535 inode_unlock(&inode->v);
540 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
544 ret = __bch2_link(c, inode, dir, dentry);
548 d_instantiate(dentry, &inode->v);
555 static int bch2_mkdir(struct user_namespace *mnt_userns,
556 struct inode *vdir, struct dentry *dentry, umode_t mode)
558 return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFDIR, 0);
561 static int bch2_rename2(struct user_namespace *mnt_userns,
562 struct inode *src_vdir, struct dentry *src_dentry,
563 struct inode *dst_vdir, struct dentry *dst_dentry,
566 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
567 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
568 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
569 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
570 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
571 struct bch_inode_unpacked dst_dir_u, src_dir_u;
572 struct bch_inode_unpacked src_inode_u, dst_inode_u;
573 struct btree_trans trans;
574 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
575 ? BCH_RENAME_EXCHANGE
576 : dst_dentry->d_inode
577 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
580 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
583 if (mode == BCH_RENAME_OVERWRITE) {
584 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
590 bch2_trans_init(&trans, c, 8, 2048);
592 bch2_lock_inodes(INODE_UPDATE_LOCK,
598 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
599 ret = bch2_fs_quota_transfer(c, src_inode,
602 KEY_TYPE_QUOTA_PREALLOC);
607 if (mode == BCH_RENAME_EXCHANGE &&
608 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
609 ret = bch2_fs_quota_transfer(c, dst_inode,
612 KEY_TYPE_QUOTA_PREALLOC);
617 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
618 bch2_rename_trans(&trans,
619 inode_inum(src_dir), &src_dir_u,
620 inode_inum(dst_dir), &dst_dir_u,
629 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
631 dst_inode->v.i_ino != dst_inode_u.bi_inum);
633 bch2_inode_update_after_write(&trans, src_dir, &src_dir_u,
634 ATTR_MTIME|ATTR_CTIME);
636 if (src_dir != dst_dir)
637 bch2_inode_update_after_write(&trans, dst_dir, &dst_dir_u,
638 ATTR_MTIME|ATTR_CTIME);
640 bch2_inode_update_after_write(&trans, src_inode, &src_inode_u,
644 bch2_inode_update_after_write(&trans, dst_inode, &dst_inode_u,
647 bch2_trans_exit(&trans);
649 bch2_fs_quota_transfer(c, src_inode,
650 bch_qid(&src_inode->ei_inode),
652 KEY_TYPE_QUOTA_NOCHECK);
654 bch2_fs_quota_transfer(c, dst_inode,
655 bch_qid(&dst_inode->ei_inode),
657 KEY_TYPE_QUOTA_NOCHECK);
659 bch2_unlock_inodes(INODE_UPDATE_LOCK,
668 static void bch2_setattr_copy(struct user_namespace *mnt_userns,
669 struct bch_inode_info *inode,
670 struct bch_inode_unpacked *bi,
673 struct bch_fs *c = inode->v.i_sb->s_fs_info;
674 unsigned int ia_valid = attr->ia_valid;
676 if (ia_valid & ATTR_UID)
677 bi->bi_uid = from_kuid(mnt_userns, attr->ia_uid);
678 if (ia_valid & ATTR_GID)
679 bi->bi_gid = from_kgid(mnt_userns, attr->ia_gid);
681 if (ia_valid & ATTR_SIZE)
682 bi->bi_size = attr->ia_size;
684 if (ia_valid & ATTR_ATIME)
685 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
686 if (ia_valid & ATTR_MTIME)
687 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
688 if (ia_valid & ATTR_CTIME)
689 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
691 if (ia_valid & ATTR_MODE) {
692 umode_t mode = attr->ia_mode;
693 kgid_t gid = ia_valid & ATTR_GID
697 if (!in_group_p(gid) &&
698 !capable_wrt_inode_uidgid(mnt_userns, &inode->v, CAP_FSETID))
704 int bch2_setattr_nonsize(struct user_namespace *mnt_userns,
705 struct bch_inode_info *inode,
708 struct bch_fs *c = inode->v.i_sb->s_fs_info;
710 struct btree_trans trans;
711 struct btree_iter inode_iter = { NULL };
712 struct bch_inode_unpacked inode_u;
713 struct posix_acl *acl = NULL;
716 mutex_lock(&inode->ei_update_lock);
720 if (attr->ia_valid & ATTR_UID)
721 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
723 if (attr->ia_valid & ATTR_GID)
724 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
726 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
727 KEY_TYPE_QUOTA_PREALLOC);
731 bch2_trans_init(&trans, c, 0, 0);
733 bch2_trans_begin(&trans);
737 ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode_inum(inode),
742 bch2_setattr_copy(mnt_userns, inode, &inode_u, attr);
744 if (attr->ia_valid & ATTR_MODE) {
745 ret = bch2_acl_chmod(&trans, inode_inum(inode), &inode_u,
746 inode_u.bi_mode, &acl);
751 ret = bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
752 bch2_trans_commit(&trans, NULL, NULL,
753 BTREE_INSERT_NOFAIL);
755 bch2_trans_iter_exit(&trans, &inode_iter);
762 bch2_inode_update_after_write(&trans, inode, &inode_u, attr->ia_valid);
765 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
767 bch2_trans_exit(&trans);
769 mutex_unlock(&inode->ei_update_lock);
774 static int bch2_getattr(struct user_namespace *mnt_userns,
775 const struct path *path, struct kstat *stat,
776 u32 request_mask, unsigned query_flags)
778 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
779 struct bch_fs *c = inode->v.i_sb->s_fs_info;
781 stat->dev = inode->v.i_sb->s_dev;
782 stat->ino = inode->v.i_ino;
783 stat->mode = inode->v.i_mode;
784 stat->nlink = inode->v.i_nlink;
785 stat->uid = inode->v.i_uid;
786 stat->gid = inode->v.i_gid;
787 stat->rdev = inode->v.i_rdev;
788 stat->size = i_size_read(&inode->v);
789 stat->atime = inode->v.i_atime;
790 stat->mtime = inode->v.i_mtime;
791 stat->ctime = inode->v.i_ctime;
792 stat->blksize = block_bytes(c);
793 stat->blocks = inode->v.i_blocks;
795 if (request_mask & STATX_BTIME) {
796 stat->result_mask |= STATX_BTIME;
797 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
800 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
801 stat->attributes |= STATX_ATTR_IMMUTABLE;
802 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
804 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
805 stat->attributes |= STATX_ATTR_APPEND;
806 stat->attributes_mask |= STATX_ATTR_APPEND;
808 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
809 stat->attributes |= STATX_ATTR_NODUMP;
810 stat->attributes_mask |= STATX_ATTR_NODUMP;
815 static int bch2_setattr(struct user_namespace *mnt_userns,
816 struct dentry *dentry, struct iattr *iattr)
818 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
821 lockdep_assert_held(&inode->v.i_rwsem);
823 ret = setattr_prepare(mnt_userns, dentry, iattr);
827 return iattr->ia_valid & ATTR_SIZE
828 ? bch2_truncate(mnt_userns, inode, iattr)
829 : bch2_setattr_nonsize(mnt_userns, inode, iattr);
832 static int bch2_tmpfile(struct user_namespace *mnt_userns,
833 struct inode *vdir, struct dentry *dentry, umode_t mode)
835 struct bch_inode_info *inode =
836 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, 0,
837 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
840 return PTR_ERR(inode);
842 d_mark_tmpfile(dentry, &inode->v);
843 d_instantiate(dentry, &inode->v);
847 static int bch2_fill_extent(struct bch_fs *c,
848 struct fiemap_extent_info *info,
849 struct bkey_s_c k, unsigned flags)
851 if (bkey_extent_is_direct_data(k.k)) {
852 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
853 const union bch_extent_entry *entry;
854 struct extent_ptr_decoded p;
857 if (k.k->type == KEY_TYPE_reflink_v)
858 flags |= FIEMAP_EXTENT_SHARED;
860 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
862 u64 offset = p.ptr.offset;
864 if (p.crc.compression_type)
865 flags2 |= FIEMAP_EXTENT_ENCODED;
867 offset += p.crc.offset;
869 if ((offset & (block_sectors(c) - 1)) ||
870 (k.k->size & (block_sectors(c) - 1)))
871 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
873 ret = fiemap_fill_next_extent(info,
874 bkey_start_offset(k.k) << 9,
876 k.k->size << 9, flags|flags2);
882 } else if (bkey_extent_is_inline_data(k.k)) {
883 return fiemap_fill_next_extent(info,
884 bkey_start_offset(k.k) << 9,
887 FIEMAP_EXTENT_DATA_INLINE);
888 } else if (k.k->type == KEY_TYPE_reservation) {
889 return fiemap_fill_next_extent(info,
890 bkey_start_offset(k.k) << 9,
893 FIEMAP_EXTENT_DELALLOC|
894 FIEMAP_EXTENT_UNWRITTEN);
900 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
903 struct bch_fs *c = vinode->i_sb->s_fs_info;
904 struct bch_inode_info *ei = to_bch_ei(vinode);
905 struct btree_trans trans;
906 struct btree_iter iter;
908 struct bkey_buf cur, prev;
909 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
910 unsigned offset_into_extent, sectors;
911 bool have_extent = false;
915 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
919 if (start + len < start)
924 bch2_bkey_buf_init(&cur);
925 bch2_bkey_buf_init(&prev);
926 bch2_trans_init(&trans, c, 0, 0);
928 bch2_trans_begin(&trans);
930 ret = bch2_subvolume_get_snapshot(&trans, ei->ei_subvol, &snapshot);
934 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
935 SPOS(ei->v.i_ino, start, snapshot), 0);
937 while (!(ret = btree_trans_too_many_iters(&trans)) &&
938 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
939 !(ret = bkey_err(k))) {
940 enum btree_id data_btree = BTREE_ID_extents;
942 if (!bkey_extent_is_data(k.k) &&
943 k.k->type != KEY_TYPE_reservation) {
944 bch2_btree_iter_advance(&iter);
948 offset_into_extent = iter.pos.offset -
949 bkey_start_offset(k.k);
950 sectors = k.k->size - offset_into_extent;
952 bch2_bkey_buf_reassemble(&cur, c, k);
954 ret = bch2_read_indirect_extent(&trans, &data_btree,
955 &offset_into_extent, &cur);
959 k = bkey_i_to_s_c(cur.k);
960 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
962 sectors = min(sectors, k.k->size - offset_into_extent);
964 bch2_cut_front(POS(k.k->p.inode,
965 bkey_start_offset(k.k) +
968 bch2_key_resize(&cur.k->k, sectors);
969 cur.k->k.p = iter.pos;
970 cur.k->k.p.offset += cur.k->k.size;
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);
991 if (!ret && have_extent)
992 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
995 bch2_trans_exit(&trans);
996 bch2_bkey_buf_exit(&cur, c);
997 bch2_bkey_buf_exit(&prev, c);
998 return ret < 0 ? ret : 0;
1001 static const struct vm_operations_struct bch_vm_ops = {
1002 .fault = bch2_page_fault,
1003 .map_pages = filemap_map_pages,
1004 .page_mkwrite = bch2_page_mkwrite,
1007 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1009 file_accessed(file);
1011 vma->vm_ops = &bch_vm_ops;
1017 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1019 return generic_file_llseek_size(file, offset, whence,
1023 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1025 struct bch_inode_info *inode = file_bch_inode(file);
1026 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1028 if (!dir_emit_dots(file, ctx))
1031 return bch2_readdir(c, inode_inum(inode), ctx);
1034 static const struct file_operations bch_file_operations = {
1035 .llseek = bch2_llseek,
1036 .read_iter = bch2_read_iter,
1037 .write_iter = bch2_write_iter,
1039 .open = generic_file_open,
1040 .fsync = bch2_fsync,
1041 .splice_read = generic_file_splice_read,
1042 .splice_write = iter_file_splice_write,
1043 .fallocate = bch2_fallocate_dispatch,
1044 .unlocked_ioctl = bch2_fs_file_ioctl,
1045 #ifdef CONFIG_COMPAT
1046 .compat_ioctl = bch2_compat_fs_ioctl,
1048 .remap_file_range = bch2_remap_file_range,
1051 static const struct inode_operations bch_file_inode_operations = {
1052 .getattr = bch2_getattr,
1053 .setattr = bch2_setattr,
1054 .fiemap = bch2_fiemap,
1055 .listxattr = bch2_xattr_list,
1056 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1057 .get_acl = bch2_get_acl,
1058 .set_acl = bch2_set_acl,
1062 static const struct inode_operations bch_dir_inode_operations = {
1063 .lookup = bch2_lookup,
1064 .create = bch2_create,
1066 .unlink = bch2_unlink,
1067 .symlink = bch2_symlink,
1068 .mkdir = bch2_mkdir,
1069 .rmdir = bch2_unlink,
1070 .mknod = bch2_mknod,
1071 .rename = bch2_rename2,
1072 .getattr = bch2_getattr,
1073 .setattr = bch2_setattr,
1074 .tmpfile = bch2_tmpfile,
1075 .listxattr = bch2_xattr_list,
1076 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1077 .get_acl = bch2_get_acl,
1078 .set_acl = bch2_set_acl,
1082 static const struct file_operations bch_dir_file_operations = {
1083 .llseek = bch2_dir_llseek,
1084 .read = generic_read_dir,
1085 .iterate_shared = bch2_vfs_readdir,
1086 .fsync = bch2_fsync,
1087 .unlocked_ioctl = bch2_fs_file_ioctl,
1088 #ifdef CONFIG_COMPAT
1089 .compat_ioctl = bch2_compat_fs_ioctl,
1093 static const struct inode_operations bch_symlink_inode_operations = {
1094 .get_link = page_get_link,
1095 .getattr = bch2_getattr,
1096 .setattr = bch2_setattr,
1097 .listxattr = bch2_xattr_list,
1098 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1099 .get_acl = bch2_get_acl,
1100 .set_acl = bch2_set_acl,
1104 static const struct inode_operations bch_special_inode_operations = {
1105 .getattr = bch2_getattr,
1106 .setattr = bch2_setattr,
1107 .listxattr = bch2_xattr_list,
1108 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1109 .get_acl = bch2_get_acl,
1110 .set_acl = bch2_set_acl,
1114 static const struct address_space_operations bch_address_space_operations = {
1115 .writepage = bch2_writepage,
1116 .readpage = bch2_readpage,
1117 .writepages = bch2_writepages,
1118 .readahead = bch2_readahead,
1119 .set_page_dirty = __set_page_dirty_nobuffers,
1120 .write_begin = bch2_write_begin,
1121 .write_end = bch2_write_end,
1122 .invalidatepage = bch2_invalidatepage,
1123 .releasepage = bch2_releasepage,
1124 .direct_IO = noop_direct_IO,
1125 #ifdef CONFIG_MIGRATION
1126 .migratepage = bch2_migrate_page,
1128 .error_remove_page = generic_error_remove_page,
1131 struct bcachefs_fid {
1137 struct bcachefs_fid_with_parent {
1138 struct bcachefs_fid fid;
1139 struct bcachefs_fid dir;
1142 static int bcachefs_fid_valid(int fh_len, int fh_type)
1145 case FILEID_BCACHEFS_WITHOUT_PARENT:
1146 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1147 case FILEID_BCACHEFS_WITH_PARENT:
1148 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1154 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1156 return (struct bcachefs_fid) {
1157 .inum = inode->ei_inode.bi_inum,
1158 .subvol = inode->ei_subvol,
1159 .gen = inode->ei_inode.bi_generation,
1163 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1166 struct bch_inode_info *inode = to_bch_ei(vinode);
1167 struct bch_inode_info *dir = to_bch_ei(vdir);
1169 if (*len < sizeof(struct bcachefs_fid_with_parent) / sizeof(u32))
1170 return FILEID_INVALID;
1172 if (!S_ISDIR(inode->v.i_mode) && dir) {
1173 struct bcachefs_fid_with_parent *fid = (void *) fh;
1175 fid->fid = bch2_inode_to_fid(inode);
1176 fid->dir = bch2_inode_to_fid(dir);
1178 *len = sizeof(*fid) / sizeof(u32);
1179 return FILEID_BCACHEFS_WITH_PARENT;
1181 struct bcachefs_fid *fid = (void *) fh;
1183 *fid = bch2_inode_to_fid(inode);
1185 *len = sizeof(*fid) / sizeof(u32);
1186 return FILEID_BCACHEFS_WITHOUT_PARENT;
1190 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1191 struct bcachefs_fid fid)
1193 struct bch_fs *c = sb->s_fs_info;
1194 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1195 .subvol = fid.subvol,
1198 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1200 vinode = ERR_PTR(-ESTALE);
1205 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1206 int fh_len, int fh_type)
1208 struct bcachefs_fid *fid = (void *) _fid;
1210 if (!bcachefs_fid_valid(fh_len, fh_type))
1213 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1216 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1217 int fh_len, int fh_type)
1219 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1221 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1222 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1225 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1228 static struct dentry *bch2_get_parent(struct dentry *child)
1230 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1231 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1232 subvol_inum parent_inum = {
1233 .subvol = inode->ei_inode.bi_parent_subvol ?:
1235 .inum = inode->ei_inode.bi_dir,
1238 if (!parent_inum.inum)
1241 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1244 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1246 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1247 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1248 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1249 struct btree_trans trans;
1250 struct btree_iter iter1;
1251 struct btree_iter iter2;
1253 struct bkey_s_c_dirent d;
1254 struct bch_inode_unpacked inode_u;
1260 if (!S_ISDIR(dir->v.i_mode))
1263 bch2_trans_init(&trans, c, 0, 0);
1265 bch2_trans_iter_init(&trans, &iter1, BTREE_ID_dirents,
1266 POS(dir->ei_inode.bi_inum, 0), 0);
1267 bch2_trans_iter_init(&trans, &iter2, BTREE_ID_dirents,
1268 POS(dir->ei_inode.bi_inum, 0), 0);
1270 bch2_trans_begin(&trans);
1272 ret = bch2_subvolume_get_snapshot(&trans, dir->ei_subvol, &snapshot);
1276 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1277 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1279 ret = bch2_inode_find_by_inum_trans(&trans, inode_inum(inode), &inode_u);
1283 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1284 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1286 k = bch2_btree_iter_peek_slot(&iter1);
1291 if (k.k->type != KEY_TYPE_dirent) {
1296 d = bkey_s_c_to_dirent(k);
1297 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1303 if (target.subvol == inode->ei_subvol &&
1304 target.inum == inode->ei_inode.bi_inum)
1308 * File with multiple hardlinks and our backref is to the wrong
1309 * directory - linear search:
1311 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1312 if (k.k->p.inode > dir->ei_inode.bi_inum)
1315 if (k.k->type != KEY_TYPE_dirent)
1318 d = bkey_s_c_to_dirent(k);
1319 ret = bch2_dirent_read_target(&trans, inode_inum(dir), d, &target);
1325 if (target.subvol == inode->ei_subvol &&
1326 target.inum == inode->ei_inode.bi_inum)
1334 name_len = min_t(unsigned, bch2_dirent_name_bytes(d), NAME_MAX);
1336 memcpy(name, d.v->d_name, name_len);
1337 name[name_len] = '\0';
1342 bch2_trans_iter_exit(&trans, &iter1);
1343 bch2_trans_iter_exit(&trans, &iter2);
1344 bch2_trans_exit(&trans);
1349 static const struct export_operations bch_export_ops = {
1350 .encode_fh = bch2_encode_fh,
1351 .fh_to_dentry = bch2_fh_to_dentry,
1352 .fh_to_parent = bch2_fh_to_parent,
1353 .get_parent = bch2_get_parent,
1354 .get_name = bch2_get_name,
1357 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1358 struct bch_inode_info *inode,
1359 struct bch_inode_unpacked *bi,
1360 struct bch_subvolume *subvol)
1362 bch2_inode_update_after_write(trans, inode, bi, ~0);
1364 if (BCH_SUBVOLUME_SNAP(subvol))
1365 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1367 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1369 inode->v.i_blocks = bi->bi_sectors;
1370 inode->v.i_ino = bi->bi_inum;
1371 inode->v.i_rdev = bi->bi_dev;
1372 inode->v.i_generation = bi->bi_generation;
1373 inode->v.i_size = bi->bi_size;
1375 inode->ei_flags = 0;
1376 inode->ei_quota_reserved = 0;
1377 inode->ei_qid = bch_qid(bi);
1378 inode->ei_subvol = inum.subvol;
1380 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1382 switch (inode->v.i_mode & S_IFMT) {
1384 inode->v.i_op = &bch_file_inode_operations;
1385 inode->v.i_fop = &bch_file_operations;
1388 inode->v.i_op = &bch_dir_inode_operations;
1389 inode->v.i_fop = &bch_dir_file_operations;
1392 inode_nohighmem(&inode->v);
1393 inode->v.i_op = &bch_symlink_inode_operations;
1396 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1397 inode->v.i_op = &bch_special_inode_operations;
1402 static struct inode *bch2_alloc_inode(struct super_block *sb)
1404 struct bch_inode_info *inode;
1406 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1410 inode_init_once(&inode->v);
1411 mutex_init(&inode->ei_update_lock);
1412 pagecache_lock_init(&inode->ei_pagecache_lock);
1413 mutex_init(&inode->ei_quota_lock);
1418 static void bch2_i_callback(struct rcu_head *head)
1420 struct inode *vinode = container_of(head, struct inode, i_rcu);
1421 struct bch_inode_info *inode = to_bch_ei(vinode);
1423 kmem_cache_free(bch2_inode_cache, inode);
1426 static void bch2_destroy_inode(struct inode *vinode)
1428 call_rcu(&vinode->i_rcu, bch2_i_callback);
1431 static int inode_update_times_fn(struct bch_inode_info *inode,
1432 struct bch_inode_unpacked *bi,
1435 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1437 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1438 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1439 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1444 static int bch2_vfs_write_inode(struct inode *vinode,
1445 struct writeback_control *wbc)
1447 struct bch_fs *c = vinode->i_sb->s_fs_info;
1448 struct bch_inode_info *inode = to_bch_ei(vinode);
1451 mutex_lock(&inode->ei_update_lock);
1452 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1453 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1454 mutex_unlock(&inode->ei_update_lock);
1459 static void bch2_evict_inode(struct inode *vinode)
1461 struct bch_fs *c = vinode->i_sb->s_fs_info;
1462 struct bch_inode_info *inode = to_bch_ei(vinode);
1464 truncate_inode_pages_final(&inode->v.i_data);
1466 clear_inode(&inode->v);
1468 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1470 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1471 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1472 KEY_TYPE_QUOTA_WARN);
1473 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1474 KEY_TYPE_QUOTA_WARN);
1475 bch2_inode_rm(c, inode_inum(inode));
1479 void bch2_evict_subvolume_inodes(struct bch_fs *c,
1480 struct snapshot_id_list *s)
1482 struct super_block *sb = c->vfs_sb;
1483 struct inode *inode;
1485 spin_lock(&sb->s_inode_list_lock);
1486 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1487 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1488 (inode->i_state & I_FREEING))
1491 d_mark_dontcache(inode);
1492 d_prune_aliases(inode);
1494 spin_unlock(&sb->s_inode_list_lock);
1497 spin_lock(&sb->s_inode_list_lock);
1498 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1499 if (!snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) ||
1500 (inode->i_state & I_FREEING))
1503 if (!(inode->i_state & I_DONTCACHE)) {
1504 d_mark_dontcache(inode);
1505 d_prune_aliases(inode);
1508 spin_lock(&inode->i_lock);
1509 if (snapshot_list_has_id(s, to_bch_ei(inode)->ei_subvol) &&
1510 !(inode->i_state & I_FREEING)) {
1511 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_NEW);
1512 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1513 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1514 spin_unlock(&inode->i_lock);
1515 spin_unlock(&sb->s_inode_list_lock);
1517 finish_wait(wq, &wait.wq_entry);
1521 spin_unlock(&inode->i_lock);
1523 spin_unlock(&sb->s_inode_list_lock);
1526 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1528 struct super_block *sb = dentry->d_sb;
1529 struct bch_fs *c = sb->s_fs_info;
1530 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1531 unsigned shift = sb->s_blocksize_bits - 9;
1533 * this assumes inodes take up 64 bytes, which is a decent average
1536 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1539 buf->f_type = BCACHEFS_STATFS_MAGIC;
1540 buf->f_bsize = sb->s_blocksize;
1541 buf->f_blocks = usage.capacity >> shift;
1542 buf->f_bfree = usage.free >> shift;
1543 buf->f_bavail = avail_factor(usage.free) >> shift;
1545 buf->f_files = usage.nr_inodes + avail_inodes;
1546 buf->f_ffree = avail_inodes;
1548 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1549 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1550 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1551 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1552 buf->f_namelen = BCH_NAME_MAX;
1557 static int bch2_sync_fs(struct super_block *sb, int wait)
1559 struct bch_fs *c = sb->s_fs_info;
1561 if (c->opts.journal_flush_disabled)
1565 bch2_journal_flush_async(&c->journal, NULL);
1569 return bch2_journal_flush(&c->journal);
1572 static struct bch_fs *bch2_path_to_fs(const char *path)
1578 ret = lookup_bdev(path, &dev);
1580 return ERR_PTR(ret);
1582 c = bch2_dev_to_fs(dev);
1584 closure_put(&c->cl);
1585 return c ?: ERR_PTR(-ENOENT);
1588 static char **split_devs(const char *_dev_name, unsigned *nr)
1590 char *dev_name = NULL, **devs = NULL, *s;
1591 size_t i, nr_devs = 0;
1593 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1597 for (s = dev_name; s; s = strchr(s + 1, ':'))
1600 devs = kcalloc(nr_devs + 1, sizeof(const char *), GFP_KERNEL);
1606 for (i = 0, s = dev_name;
1608 (s = strchr(s, ':')) && (*s++ = '\0'))
1615 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1617 struct bch_fs *c = sb->s_fs_info;
1618 struct bch_opts opts = bch2_opts_empty();
1621 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1623 ret = bch2_parse_mount_opts(c, &opts, data);
1627 if (opts.read_only != c->opts.read_only) {
1628 down_write(&c->state_lock);
1630 if (opts.read_only) {
1631 bch2_fs_read_only(c);
1633 sb->s_flags |= SB_RDONLY;
1635 ret = bch2_fs_read_write(c);
1637 bch_err(c, "error going rw: %i", ret);
1638 up_write(&c->state_lock);
1642 sb->s_flags &= ~SB_RDONLY;
1645 c->opts.read_only = opts.read_only;
1647 up_write(&c->state_lock);
1650 if (opts.errors >= 0)
1651 c->opts.errors = opts.errors;
1656 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1658 struct bch_fs *c = root->d_sb->s_fs_info;
1663 for_each_online_member(ca, c, i) {
1667 seq_puts(seq, "/dev/");
1668 seq_puts(seq, ca->name);
1674 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1676 struct bch_fs *c = root->d_sb->s_fs_info;
1678 struct printbuf buf = PRINTBUF;
1681 for (i = 0; i < bch2_opts_nr; i++) {
1682 const struct bch_option *opt = &bch2_opt_table[i];
1683 u64 v = bch2_opt_get_by_id(&c->opts, i);
1685 if (!(opt->flags & OPT_MOUNT))
1688 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1691 printbuf_reset(&buf);
1692 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1693 OPT_SHOW_MOUNT_STYLE);
1695 seq_puts(seq, buf.buf);
1698 if (buf.allocation_failure)
1700 printbuf_exit(&buf);
1704 static void bch2_put_super(struct super_block *sb)
1706 struct bch_fs *c = sb->s_fs_info;
1711 static const struct super_operations bch_super_operations = {
1712 .alloc_inode = bch2_alloc_inode,
1713 .destroy_inode = bch2_destroy_inode,
1714 .write_inode = bch2_vfs_write_inode,
1715 .evict_inode = bch2_evict_inode,
1716 .sync_fs = bch2_sync_fs,
1717 .statfs = bch2_statfs,
1718 .show_devname = bch2_show_devname,
1719 .show_options = bch2_show_options,
1720 .remount_fs = bch2_remount,
1721 .put_super = bch2_put_super,
1723 .freeze_fs = bch2_freeze,
1724 .unfreeze_fs = bch2_unfreeze,
1728 static int bch2_set_super(struct super_block *s, void *data)
1730 s->s_fs_info = data;
1734 static int bch2_noset_super(struct super_block *s, void *data)
1739 static int bch2_test_super(struct super_block *s, void *data)
1741 struct bch_fs *c = s->s_fs_info;
1742 struct bch_fs **devs = data;
1748 for (i = 0; devs[i]; i++)
1754 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1755 int flags, const char *dev_name, void *data)
1759 struct super_block *sb;
1760 struct inode *vinode;
1761 struct bch_opts opts = bch2_opts_empty();
1763 struct bch_fs **devs_to_fs = NULL;
1764 unsigned i, nr_devs;
1767 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1769 ret = bch2_parse_mount_opts(NULL, &opts, data);
1771 return ERR_PTR(ret);
1773 if (!dev_name || strlen(dev_name) == 0)
1774 return ERR_PTR(-EINVAL);
1776 devs = split_devs(dev_name, &nr_devs);
1778 return ERR_PTR(-ENOMEM);
1780 devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
1782 sb = ERR_PTR(-ENOMEM);
1786 for (i = 0; i < nr_devs; i++)
1787 devs_to_fs[i] = bch2_path_to_fs(devs[i]);
1789 sb = sget(fs_type, bch2_test_super, bch2_noset_super,
1790 flags|SB_NOSEC, devs_to_fs);
1794 c = bch2_fs_open(devs, nr_devs, opts);
1800 /* Some options can't be parsed until after the fs is started: */
1801 ret = bch2_parse_mount_opts(c, &opts, data);
1808 bch2_opts_apply(&c->opts, opts);
1810 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1819 return ERR_CAST(sb);
1824 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1831 sb->s_blocksize = block_bytes(c);
1832 sb->s_blocksize_bits = ilog2(block_bytes(c));
1833 sb->s_maxbytes = MAX_LFS_FILESIZE;
1834 sb->s_op = &bch_super_operations;
1835 sb->s_export_op = &bch_export_ops;
1836 #ifdef CONFIG_BCACHEFS_QUOTA
1837 sb->s_qcop = &bch2_quotactl_operations;
1838 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1840 sb->s_xattr = bch2_xattr_handlers;
1841 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1842 sb->s_time_gran = c->sb.nsec_per_time_unit;
1843 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1844 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1846 strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1848 ret = super_setup_bdi(sb);
1852 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1854 for_each_online_member(ca, c, i) {
1855 struct block_device *bdev = ca->disk_sb.bdev;
1857 /* XXX: create an anonymous device for multi device filesystems */
1859 sb->s_dev = bdev->bd_dev;
1860 percpu_ref_put(&ca->io_ref);
1866 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1868 sb->s_flags |= SB_POSIXACL;
1871 sb->s_shrink.seeks = 0;
1873 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1874 if (IS_ERR(vinode)) {
1875 bch_err(c, "error mounting: error getting root inode %i",
1876 (int) PTR_ERR(vinode));
1877 ret = PTR_ERR(vinode);
1881 sb->s_root = d_make_root(vinode);
1883 bch_err(c, "error mounting: error allocating root dentry");
1888 sb->s_flags |= SB_ACTIVE;
1890 return dget(sb->s_root);
1893 deactivate_locked_super(sb);
1894 return ERR_PTR(ret);
1897 static void bch2_kill_sb(struct super_block *sb)
1899 struct bch_fs *c = sb->s_fs_info;
1901 generic_shutdown_super(sb);
1905 static struct file_system_type bcache_fs_type = {
1906 .owner = THIS_MODULE,
1908 .mount = bch2_mount,
1909 .kill_sb = bch2_kill_sb,
1910 .fs_flags = FS_REQUIRES_DEV,
1913 MODULE_ALIAS_FS("bcachefs");
1915 void bch2_vfs_exit(void)
1917 unregister_filesystem(&bcache_fs_type);
1918 if (bch2_inode_cache)
1919 kmem_cache_destroy(bch2_inode_cache);
1922 int __init bch2_vfs_init(void)
1926 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1927 if (!bch2_inode_cache)
1930 ret = register_filesystem(&bcache_fs_type);
1940 #endif /* NO_BCACHEFS_FS */