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/posix_acl.h>
31 #include <linux/random.h>
32 #include <linux/statfs.h>
33 #include <linux/string.h>
34 #include <linux/xattr.h>
36 static struct kmem_cache *bch2_inode_cache;
38 static void bch2_vfs_inode_init(struct bch_fs *,
39 struct bch_inode_info *,
40 struct bch_inode_unpacked *);
42 static void journal_seq_copy(struct bch_fs *c,
43 struct bch_inode_info *dst,
47 * atomic64_cmpxchg has a fallback for archs that don't support it,
50 atomic64_t *dst_seq = (void *) &dst->ei_journal_seq;
51 u64 old, v = READ_ONCE(dst->ei_journal_seq);
56 if (old >= journal_seq)
58 } while ((v = atomic64_cmpxchg(dst_seq, old, journal_seq)) != old);
60 bch2_journal_set_has_inum(&c->journal, dst->v.i_ino, journal_seq);
63 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
65 BUG_ON(atomic_long_read(&lock->v) == 0);
67 if (atomic_long_sub_return_release(i, &lock->v) == 0)
68 wake_up_all(&lock->wait);
71 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
73 long v = atomic_long_read(&lock->v), old;
78 if (i > 0 ? v < 0 : v > 0)
80 } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
81 old, old + i)) != old);
85 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
87 wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
90 void bch2_pagecache_add_put(struct pagecache_lock *lock)
92 __pagecache_lock_put(lock, 1);
95 bool bch2_pagecache_add_tryget(struct pagecache_lock *lock)
97 return __pagecache_lock_tryget(lock, 1);
100 void bch2_pagecache_add_get(struct pagecache_lock *lock)
102 __pagecache_lock_get(lock, 1);
105 void bch2_pagecache_block_put(struct pagecache_lock *lock)
107 __pagecache_lock_put(lock, -1);
110 void bch2_pagecache_block_get(struct pagecache_lock *lock)
112 __pagecache_lock_get(lock, -1);
115 void bch2_inode_update_after_write(struct bch_fs *c,
116 struct bch_inode_info *inode,
117 struct bch_inode_unpacked *bi,
120 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
121 i_uid_write(&inode->v, bi->bi_uid);
122 i_gid_write(&inode->v, bi->bi_gid);
123 inode->v.i_mode = bi->bi_mode;
125 if (fields & ATTR_ATIME)
126 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
127 if (fields & ATTR_MTIME)
128 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
129 if (fields & ATTR_CTIME)
130 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
132 inode->ei_inode = *bi;
134 bch2_inode_flags_to_vfs(inode);
137 int __must_check bch2_write_inode(struct bch_fs *c,
138 struct bch_inode_info *inode,
140 void *p, unsigned fields)
142 struct btree_trans trans;
143 struct btree_iter *iter;
144 struct bch_inode_unpacked inode_u;
147 bch2_trans_init(&trans, c, 0, 256);
149 bch2_trans_begin(&trans);
151 iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
153 ret = PTR_ERR_OR_ZERO(iter) ?:
154 (set ? set(inode, &inode_u, p) : 0) ?:
155 bch2_inode_write(&trans, iter, &inode_u) ?:
156 bch2_trans_commit(&trans, NULL,
157 &inode->ei_journal_seq,
158 BTREE_INSERT_NOUNLOCK|
159 BTREE_INSERT_NOFAIL);
162 * the btree node lock protects inode->ei_inode, not ei_update_lock;
163 * this is important for inode updates via bchfs_write_index_update
166 bch2_inode_update_after_write(c, inode, &inode_u, fields);
168 bch2_trans_iter_put(&trans, iter);
173 bch2_trans_exit(&trans);
174 return ret < 0 ? ret : 0;
177 int bch2_fs_quota_transfer(struct bch_fs *c,
178 struct bch_inode_info *inode,
179 struct bch_qid new_qid,
181 enum quota_acct_mode mode)
186 qtypes &= enabled_qtypes(c);
188 for (i = 0; i < QTYP_NR; i++)
189 if (new_qid.q[i] == inode->ei_qid.q[i])
190 qtypes &= ~(1U << i);
195 mutex_lock(&inode->ei_quota_lock);
197 ret = bch2_quota_transfer(c, qtypes, new_qid,
200 inode->ei_quota_reserved,
203 for (i = 0; i < QTYP_NR; i++)
204 if (qtypes & (1 << i))
205 inode->ei_qid.q[i] = new_qid.q[i];
207 mutex_unlock(&inode->ei_quota_lock);
212 struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
214 struct bch_inode_unpacked inode_u;
215 struct bch_inode_info *inode;
218 inode = to_bch_ei(iget_locked(c->vfs_sb, inum));
219 if (unlikely(!inode))
220 return ERR_PTR(-ENOMEM);
221 if (!(inode->v.i_state & I_NEW))
224 ret = bch2_inode_find_by_inum(c, inum, &inode_u);
226 iget_failed(&inode->v);
230 bch2_vfs_inode_init(c, inode, &inode_u);
232 inode->ei_journal_seq = bch2_inode_journal_seq(&c->journal, inum);
234 unlock_new_inode(&inode->v);
239 static int inum_test(struct inode *inode, void *p)
241 unsigned long *ino = p;
243 return *ino == inode->i_ino;
246 static struct bch_inode_info *
247 __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
248 umode_t mode, dev_t rdev, bool tmpfile)
250 struct bch_fs *c = dir->v.i_sb->s_fs_info;
251 struct user_namespace *ns = dir->v.i_sb->s_user_ns;
252 struct btree_trans trans;
253 struct bch_inode_unpacked dir_u;
254 struct bch_inode_info *inode, *old;
255 struct bch_inode_unpacked inode_u;
256 struct posix_acl *default_acl = NULL, *acl = NULL;
261 * preallocate acls + vfs inode before btree transaction, so that
262 * nothing can fail after the transaction succeeds:
264 #ifdef CONFIG_BCACHEFS_POSIX_ACL
265 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
269 inode = to_bch_ei(new_inode(c->vfs_sb));
270 if (unlikely(!inode)) {
271 inode = ERR_PTR(-ENOMEM);
275 bch2_inode_init_early(c, &inode_u);
278 mutex_lock(&dir->ei_update_lock);
280 bch2_trans_init(&trans, c, 8,
281 2048 + (!tmpfile ? dentry->d_name.len : 0));
283 bch2_trans_begin(&trans);
285 ret = bch2_create_trans(&trans, dir->v.i_ino, &dir_u, &inode_u,
286 !tmpfile ? &dentry->d_name : NULL,
287 from_kuid(ns, current_fsuid()),
288 from_kgid(ns, current_fsgid()),
291 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
292 KEY_TYPE_QUOTA_PREALLOC);
294 goto err_before_quota;
296 ret = bch2_trans_commit(&trans, NULL, &journal_seq,
297 BTREE_INSERT_NOUNLOCK);
299 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
300 KEY_TYPE_QUOTA_WARN);
308 bch2_inode_update_after_write(c, dir, &dir_u,
309 ATTR_MTIME|ATTR_CTIME);
310 journal_seq_copy(c, dir, journal_seq);
311 mutex_unlock(&dir->ei_update_lock);
314 bch2_vfs_inode_init(c, inode, &inode_u);
315 journal_seq_copy(c, inode, journal_seq);
317 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
318 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
321 * we must insert the new inode into the inode cache before calling
322 * bch2_trans_exit() and dropping locks, else we could race with another
323 * thread pulling the inode in and modifying it:
326 inode->v.i_state |= I_CREATING;
327 old = to_bch_ei(inode_insert5(&inode->v, inode->v.i_ino,
328 inum_test, NULL, &inode->v.i_ino));
331 if (unlikely(old != inode)) {
333 * We raced, another process pulled the new inode into cache
336 journal_seq_copy(c, old, journal_seq);
337 make_bad_inode(&inode->v);
343 * we really don't want insert_inode_locked2() to be setting
346 unlock_new_inode(&inode->v);
349 bch2_trans_exit(&trans);
351 posix_acl_release(default_acl);
352 posix_acl_release(acl);
356 mutex_unlock(&dir->ei_update_lock);
358 bch2_trans_exit(&trans);
359 make_bad_inode(&inode->v);
361 inode = ERR_PTR(ret);
367 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
370 struct bch_fs *c = vdir->i_sb->s_fs_info;
371 struct bch_inode_info *dir = to_bch_ei(vdir);
372 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
373 struct inode *vinode = NULL;
376 inum = bch2_dirent_lookup(c, dir->v.i_ino, &hash,
380 vinode = bch2_vfs_inode_get(c, inum);
382 return d_splice_alias(vinode, dentry);
385 static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
386 umode_t mode, dev_t rdev)
388 struct bch_inode_info *inode =
389 __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
392 return PTR_ERR(inode);
394 d_instantiate(dentry, &inode->v);
398 static int bch2_create(struct inode *vdir, struct dentry *dentry,
399 umode_t mode, bool excl)
401 return bch2_mknod(vdir, dentry, mode|S_IFREG, 0);
404 static int __bch2_link(struct bch_fs *c,
405 struct bch_inode_info *inode,
406 struct bch_inode_info *dir,
407 struct dentry *dentry)
409 struct btree_trans trans;
410 struct bch_inode_unpacked dir_u, inode_u;
413 mutex_lock(&inode->ei_update_lock);
414 bch2_trans_init(&trans, c, 4, 1024);
416 ret = __bch2_trans_do(&trans, NULL, &inode->ei_journal_seq,
417 BTREE_INSERT_NOUNLOCK,
418 bch2_link_trans(&trans,
420 inode->v.i_ino, &dir_u, &inode_u,
424 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
426 journal_seq_copy(c, inode, dir->ei_journal_seq);
427 bch2_inode_update_after_write(c, dir, &dir_u,
428 ATTR_MTIME|ATTR_CTIME);
429 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
432 bch2_trans_exit(&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_link(c, inode, dir, dentry);
452 d_instantiate(dentry, &inode->v);
456 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
458 struct bch_fs *c = vdir->i_sb->s_fs_info;
459 struct bch_inode_info *dir = to_bch_ei(vdir);
460 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
461 struct bch_inode_unpacked dir_u, inode_u;
462 struct btree_trans trans;
465 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
466 bch2_trans_init(&trans, c, 4, 1024);
468 ret = __bch2_trans_do(&trans, NULL, &dir->ei_journal_seq,
469 BTREE_INSERT_NOUNLOCK|
471 bch2_unlink_trans(&trans,
472 dir->v.i_ino, &dir_u,
473 &inode_u, &dentry->d_name));
476 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
478 journal_seq_copy(c, inode, dir->ei_journal_seq);
479 bch2_inode_update_after_write(c, dir, &dir_u,
480 ATTR_MTIME|ATTR_CTIME);
481 bch2_inode_update_after_write(c, inode, &inode_u,
485 bch2_trans_exit(&trans);
486 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
491 static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
494 struct bch_fs *c = vdir->i_sb->s_fs_info;
495 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
498 inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
499 if (unlikely(IS_ERR(inode)))
500 return PTR_ERR(inode);
502 inode_lock(&inode->v);
503 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
504 inode_unlock(&inode->v);
509 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
513 journal_seq_copy(c, dir, inode->ei_journal_seq);
515 ret = __bch2_link(c, inode, dir, dentry);
519 d_instantiate(dentry, &inode->v);
526 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
528 return bch2_mknod(vdir, dentry, mode|S_IFDIR, 0);
531 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
532 struct inode *dst_vdir, struct dentry *dst_dentry,
535 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
536 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
537 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
538 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
539 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
540 struct bch_inode_unpacked dst_dir_u, src_dir_u;
541 struct bch_inode_unpacked src_inode_u, dst_inode_u;
542 struct btree_trans trans;
543 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
544 ? BCH_RENAME_EXCHANGE
545 : dst_dentry->d_inode
546 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
550 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
553 if (mode == BCH_RENAME_OVERWRITE) {
554 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
560 bch2_trans_init(&trans, c, 8, 2048);
562 bch2_lock_inodes(INODE_UPDATE_LOCK,
568 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
569 ret = bch2_fs_quota_transfer(c, src_inode,
572 KEY_TYPE_QUOTA_PREALLOC);
577 if (mode == BCH_RENAME_EXCHANGE &&
578 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
579 ret = bch2_fs_quota_transfer(c, dst_inode,
582 KEY_TYPE_QUOTA_PREALLOC);
587 ret = __bch2_trans_do(&trans, NULL, &journal_seq,
588 BTREE_INSERT_NOUNLOCK,
589 bch2_rename_trans(&trans,
590 src_dir->v.i_ino, &src_dir_u,
591 dst_dir->v.i_ino, &dst_dir_u,
600 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
602 dst_inode->v.i_ino != dst_inode_u.bi_inum);
604 bch2_inode_update_after_write(c, src_dir, &src_dir_u,
605 ATTR_MTIME|ATTR_CTIME);
606 journal_seq_copy(c, src_dir, journal_seq);
608 if (src_dir != dst_dir) {
609 bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
610 ATTR_MTIME|ATTR_CTIME);
611 journal_seq_copy(c, dst_dir, journal_seq);
614 bch2_inode_update_after_write(c, src_inode, &src_inode_u,
616 journal_seq_copy(c, src_inode, journal_seq);
619 bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
621 journal_seq_copy(c, dst_inode, journal_seq);
624 bch2_trans_exit(&trans);
626 bch2_fs_quota_transfer(c, src_inode,
627 bch_qid(&src_inode->ei_inode),
629 KEY_TYPE_QUOTA_NOCHECK);
631 bch2_fs_quota_transfer(c, dst_inode,
632 bch_qid(&dst_inode->ei_inode),
634 KEY_TYPE_QUOTA_NOCHECK);
636 bch2_unlock_inodes(INODE_UPDATE_LOCK,
645 void bch2_setattr_copy(struct bch_inode_info *inode,
646 struct bch_inode_unpacked *bi,
649 struct bch_fs *c = inode->v.i_sb->s_fs_info;
650 unsigned int ia_valid = attr->ia_valid;
652 if (ia_valid & ATTR_UID)
653 bi->bi_uid = from_kuid(c->vfs_sb->s_user_ns, attr->ia_uid);
654 if (ia_valid & ATTR_GID)
655 bi->bi_gid = from_kgid(c->vfs_sb->s_user_ns, attr->ia_gid);
657 if (ia_valid & ATTR_ATIME)
658 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
659 if (ia_valid & ATTR_MTIME)
660 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
661 if (ia_valid & ATTR_CTIME)
662 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
664 if (ia_valid & ATTR_MODE) {
665 umode_t mode = attr->ia_mode;
666 kgid_t gid = ia_valid & ATTR_GID
670 if (!in_group_p(gid) &&
671 !capable_wrt_inode_uidgid(&inode->v, CAP_FSETID))
677 static int bch2_setattr_nonsize(struct bch_inode_info *inode,
680 struct bch_fs *c = inode->v.i_sb->s_fs_info;
682 struct btree_trans trans;
683 struct btree_iter *inode_iter;
684 struct bch_inode_unpacked inode_u;
685 struct posix_acl *acl = NULL;
688 mutex_lock(&inode->ei_update_lock);
692 if (attr->ia_valid & ATTR_UID)
693 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
695 if (attr->ia_valid & ATTR_GID)
696 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
698 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
699 KEY_TYPE_QUOTA_PREALLOC);
703 bch2_trans_init(&trans, c, 0, 0);
705 bch2_trans_begin(&trans);
709 inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
711 ret = PTR_ERR_OR_ZERO(inode_iter);
715 bch2_setattr_copy(inode, &inode_u, attr);
717 if (attr->ia_valid & ATTR_MODE) {
718 ret = bch2_acl_chmod(&trans, &inode_u, inode_u.bi_mode, &acl);
723 ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
724 bch2_trans_commit(&trans, NULL,
725 &inode->ei_journal_seq,
726 BTREE_INSERT_NOUNLOCK|
727 BTREE_INSERT_NOFAIL);
729 bch2_trans_iter_put(&trans, inode_iter);
736 bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
739 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
741 bch2_trans_exit(&trans);
743 mutex_unlock(&inode->ei_update_lock);
748 static int bch2_getattr(const struct path *path, struct kstat *stat,
749 u32 request_mask, unsigned query_flags)
751 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
752 struct bch_fs *c = inode->v.i_sb->s_fs_info;
754 stat->dev = inode->v.i_sb->s_dev;
755 stat->ino = inode->v.i_ino;
756 stat->mode = inode->v.i_mode;
757 stat->nlink = inode->v.i_nlink;
758 stat->uid = inode->v.i_uid;
759 stat->gid = inode->v.i_gid;
760 stat->rdev = inode->v.i_rdev;
761 stat->size = i_size_read(&inode->v);
762 stat->atime = inode->v.i_atime;
763 stat->mtime = inode->v.i_mtime;
764 stat->ctime = inode->v.i_ctime;
765 stat->blksize = block_bytes(c);
766 stat->blocks = inode->v.i_blocks;
768 if (request_mask & STATX_BTIME) {
769 stat->result_mask |= STATX_BTIME;
770 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
773 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
774 stat->attributes |= STATX_ATTR_IMMUTABLE;
775 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
777 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
778 stat->attributes |= STATX_ATTR_APPEND;
779 stat->attributes_mask |= STATX_ATTR_APPEND;
781 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
782 stat->attributes |= STATX_ATTR_NODUMP;
783 stat->attributes_mask |= STATX_ATTR_NODUMP;
788 static int bch2_setattr(struct dentry *dentry, struct iattr *iattr)
790 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
793 lockdep_assert_held(&inode->v.i_rwsem);
795 ret = setattr_prepare(dentry, iattr);
799 return iattr->ia_valid & ATTR_SIZE
800 ? bch2_truncate(inode, iattr)
801 : bch2_setattr_nonsize(inode, iattr);
804 static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
806 struct bch_inode_info *inode =
807 __bch2_create(to_bch_ei(vdir), dentry, mode, 0, true);
810 return PTR_ERR(inode);
812 d_mark_tmpfile(dentry, &inode->v);
813 d_instantiate(dentry, &inode->v);
817 static int bch2_fill_extent(struct bch_fs *c,
818 struct fiemap_extent_info *info,
819 struct bkey_s_c k, unsigned flags)
821 if (bkey_extent_is_direct_data(k.k)) {
822 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
823 const union bch_extent_entry *entry;
824 struct extent_ptr_decoded p;
827 if (k.k->type == KEY_TYPE_reflink_v)
828 flags |= FIEMAP_EXTENT_SHARED;
830 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
832 u64 offset = p.ptr.offset;
834 if (p.crc.compression_type)
835 flags2 |= FIEMAP_EXTENT_ENCODED;
837 offset += p.crc.offset;
839 if ((offset & (c->opts.block_size - 1)) ||
840 (k.k->size & (c->opts.block_size - 1)))
841 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
843 ret = fiemap_fill_next_extent(info,
844 bkey_start_offset(k.k) << 9,
846 k.k->size << 9, flags|flags2);
852 } else if (bkey_extent_is_inline_data(k.k)) {
853 return fiemap_fill_next_extent(info,
854 bkey_start_offset(k.k) << 9,
857 FIEMAP_EXTENT_DATA_INLINE);
858 } else if (k.k->type == KEY_TYPE_reservation) {
859 return fiemap_fill_next_extent(info,
860 bkey_start_offset(k.k) << 9,
863 FIEMAP_EXTENT_DELALLOC|
864 FIEMAP_EXTENT_UNWRITTEN);
870 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
873 struct bch_fs *c = vinode->i_sb->s_fs_info;
874 struct bch_inode_info *ei = to_bch_ei(vinode);
875 struct btree_trans trans;
876 struct btree_iter *iter;
878 struct bkey_buf cur, prev;
879 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
880 unsigned offset_into_extent, sectors;
881 bool have_extent = false;
884 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
888 if (start + len < start)
891 bch2_bkey_buf_init(&cur);
892 bch2_bkey_buf_init(&prev);
893 bch2_trans_init(&trans, c, 0, 0);
895 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
896 POS(ei->v.i_ino, start >> 9), 0);
898 while ((k = bch2_btree_iter_peek(iter)).k &&
899 !(ret = bkey_err(k)) &&
900 bkey_cmp(iter->pos, end) < 0) {
901 enum btree_id data_btree = BTREE_ID_extents;
903 if (!bkey_extent_is_data(k.k) &&
904 k.k->type != KEY_TYPE_reservation) {
905 bch2_btree_iter_advance(iter);
909 offset_into_extent = iter->pos.offset -
910 bkey_start_offset(k.k);
911 sectors = k.k->size - offset_into_extent;
913 bch2_bkey_buf_reassemble(&cur, c, k);
915 ret = bch2_read_indirect_extent(&trans, &data_btree,
916 &offset_into_extent, &cur);
920 k = bkey_i_to_s_c(cur.k);
921 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
923 sectors = min(sectors, k.k->size - offset_into_extent);
925 bch2_cut_front(POS(k.k->p.inode,
926 bkey_start_offset(k.k) +
929 bch2_key_resize(&cur.k->k, sectors);
930 cur.k->k.p = iter->pos;
931 cur.k->k.p.offset += cur.k->k.size;
934 ret = bch2_fill_extent(c, info,
935 bkey_i_to_s_c(prev.k), 0);
940 bkey_copy(prev.k, cur.k);
943 bch2_btree_iter_set_pos(iter,
944 POS(iter->pos.inode, iter->pos.offset + sectors));
950 if (!ret && have_extent)
951 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
954 bch2_trans_iter_put(&trans, iter);
955 ret = bch2_trans_exit(&trans) ?: ret;
956 bch2_bkey_buf_exit(&cur, c);
957 bch2_bkey_buf_exit(&prev, c);
958 return ret < 0 ? ret : 0;
961 static const struct vm_operations_struct bch_vm_ops = {
962 .fault = bch2_page_fault,
963 .map_pages = filemap_map_pages,
964 .page_mkwrite = bch2_page_mkwrite,
967 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
971 vma->vm_ops = &bch_vm_ops;
977 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
979 return generic_file_llseek_size(file, offset, whence,
983 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
985 struct bch_inode_info *inode = file_bch_inode(file);
986 struct bch_fs *c = inode->v.i_sb->s_fs_info;
988 if (!dir_emit_dots(file, ctx))
991 return bch2_readdir(c, inode->v.i_ino, ctx);
994 static const struct file_operations bch_file_operations = {
995 .llseek = bch2_llseek,
996 .read_iter = bch2_read_iter,
997 .write_iter = bch2_write_iter,
999 .open = generic_file_open,
1000 .fsync = bch2_fsync,
1001 .splice_read = generic_file_splice_read,
1002 .splice_write = iter_file_splice_write,
1003 .fallocate = bch2_fallocate_dispatch,
1004 .unlocked_ioctl = bch2_fs_file_ioctl,
1005 #ifdef CONFIG_COMPAT
1006 .compat_ioctl = bch2_compat_fs_ioctl,
1008 .remap_file_range = bch2_remap_file_range,
1011 static const struct inode_operations bch_file_inode_operations = {
1012 .getattr = bch2_getattr,
1013 .setattr = bch2_setattr,
1014 .fiemap = bch2_fiemap,
1015 .listxattr = bch2_xattr_list,
1016 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1017 .get_acl = bch2_get_acl,
1018 .set_acl = bch2_set_acl,
1022 static const struct inode_operations bch_dir_inode_operations = {
1023 .lookup = bch2_lookup,
1024 .create = bch2_create,
1026 .unlink = bch2_unlink,
1027 .symlink = bch2_symlink,
1028 .mkdir = bch2_mkdir,
1029 .rmdir = bch2_unlink,
1030 .mknod = bch2_mknod,
1031 .rename = bch2_rename2,
1032 .getattr = bch2_getattr,
1033 .setattr = bch2_setattr,
1034 .tmpfile = bch2_tmpfile,
1035 .listxattr = bch2_xattr_list,
1036 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1037 .get_acl = bch2_get_acl,
1038 .set_acl = bch2_set_acl,
1042 static const struct file_operations bch_dir_file_operations = {
1043 .llseek = bch2_dir_llseek,
1044 .read = generic_read_dir,
1045 .iterate_shared = bch2_vfs_readdir,
1046 .fsync = bch2_fsync,
1047 .unlocked_ioctl = bch2_fs_file_ioctl,
1048 #ifdef CONFIG_COMPAT
1049 .compat_ioctl = bch2_compat_fs_ioctl,
1053 static const struct inode_operations bch_symlink_inode_operations = {
1054 .get_link = page_get_link,
1055 .getattr = bch2_getattr,
1056 .setattr = bch2_setattr,
1057 .listxattr = bch2_xattr_list,
1058 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1059 .get_acl = bch2_get_acl,
1060 .set_acl = bch2_set_acl,
1064 static const struct inode_operations bch_special_inode_operations = {
1065 .getattr = bch2_getattr,
1066 .setattr = bch2_setattr,
1067 .listxattr = bch2_xattr_list,
1068 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1069 .get_acl = bch2_get_acl,
1070 .set_acl = bch2_set_acl,
1074 static const struct address_space_operations bch_address_space_operations = {
1075 .writepage = bch2_writepage,
1076 .readpage = bch2_readpage,
1077 .writepages = bch2_writepages,
1078 .readahead = bch2_readahead,
1079 .set_page_dirty = __set_page_dirty_nobuffers,
1080 .write_begin = bch2_write_begin,
1081 .write_end = bch2_write_end,
1082 .invalidatepage = bch2_invalidatepage,
1083 .releasepage = bch2_releasepage,
1084 .direct_IO = noop_direct_IO,
1085 #ifdef CONFIG_MIGRATION
1086 .migratepage = bch2_migrate_page,
1088 .error_remove_page = generic_error_remove_page,
1091 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1092 u64 ino, u32 generation)
1094 struct bch_fs *c = sb->s_fs_info;
1095 struct inode *vinode;
1097 if (ino < BCACHEFS_ROOT_INO)
1098 return ERR_PTR(-ESTALE);
1100 vinode = bch2_vfs_inode_get(c, ino);
1102 return ERR_CAST(vinode);
1103 if (generation && vinode->i_generation != generation) {
1104 /* we didn't find the right inode.. */
1106 return ERR_PTR(-ESTALE);
1111 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *fid,
1112 int fh_len, int fh_type)
1114 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1115 bch2_nfs_get_inode);
1118 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *fid,
1119 int fh_len, int fh_type)
1121 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1122 bch2_nfs_get_inode);
1125 static const struct export_operations bch_export_ops = {
1126 .fh_to_dentry = bch2_fh_to_dentry,
1127 .fh_to_parent = bch2_fh_to_parent,
1128 //.get_parent = bch2_get_parent,
1131 static void bch2_vfs_inode_init(struct bch_fs *c,
1132 struct bch_inode_info *inode,
1133 struct bch_inode_unpacked *bi)
1135 bch2_inode_update_after_write(c, inode, bi, ~0);
1137 inode->v.i_blocks = bi->bi_sectors;
1138 inode->v.i_ino = bi->bi_inum;
1139 inode->v.i_rdev = bi->bi_dev;
1140 inode->v.i_generation = bi->bi_generation;
1141 inode->v.i_size = bi->bi_size;
1143 inode->ei_flags = 0;
1144 inode->ei_journal_seq = 0;
1145 inode->ei_quota_reserved = 0;
1146 inode->ei_qid = bch_qid(bi);
1148 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1150 switch (inode->v.i_mode & S_IFMT) {
1152 inode->v.i_op = &bch_file_inode_operations;
1153 inode->v.i_fop = &bch_file_operations;
1156 inode->v.i_op = &bch_dir_inode_operations;
1157 inode->v.i_fop = &bch_dir_file_operations;
1160 inode_nohighmem(&inode->v);
1161 inode->v.i_op = &bch_symlink_inode_operations;
1164 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1165 inode->v.i_op = &bch_special_inode_operations;
1170 static struct inode *bch2_alloc_inode(struct super_block *sb)
1172 struct bch_inode_info *inode;
1174 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1178 inode_init_once(&inode->v);
1179 mutex_init(&inode->ei_update_lock);
1180 pagecache_lock_init(&inode->ei_pagecache_lock);
1181 mutex_init(&inode->ei_quota_lock);
1182 inode->ei_journal_seq = 0;
1187 static void bch2_i_callback(struct rcu_head *head)
1189 struct inode *vinode = container_of(head, struct inode, i_rcu);
1190 struct bch_inode_info *inode = to_bch_ei(vinode);
1192 kmem_cache_free(bch2_inode_cache, inode);
1195 static void bch2_destroy_inode(struct inode *vinode)
1197 call_rcu(&vinode->i_rcu, bch2_i_callback);
1200 static int inode_update_times_fn(struct bch_inode_info *inode,
1201 struct bch_inode_unpacked *bi,
1204 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1206 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1207 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1208 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1213 static int bch2_vfs_write_inode(struct inode *vinode,
1214 struct writeback_control *wbc)
1216 struct bch_fs *c = vinode->i_sb->s_fs_info;
1217 struct bch_inode_info *inode = to_bch_ei(vinode);
1220 mutex_lock(&inode->ei_update_lock);
1221 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1222 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1223 mutex_unlock(&inode->ei_update_lock);
1228 static void bch2_evict_inode(struct inode *vinode)
1230 struct bch_fs *c = vinode->i_sb->s_fs_info;
1231 struct bch_inode_info *inode = to_bch_ei(vinode);
1233 truncate_inode_pages_final(&inode->v.i_data);
1235 clear_inode(&inode->v);
1237 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1239 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1240 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1241 KEY_TYPE_QUOTA_WARN);
1242 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1243 KEY_TYPE_QUOTA_WARN);
1244 bch2_inode_rm(c, inode->v.i_ino, true);
1248 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1250 struct super_block *sb = dentry->d_sb;
1251 struct bch_fs *c = sb->s_fs_info;
1252 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1253 unsigned shift = sb->s_blocksize_bits - 9;
1255 * this assumes inodes take up 64 bytes, which is a decent average
1258 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1261 buf->f_type = BCACHEFS_STATFS_MAGIC;
1262 buf->f_bsize = sb->s_blocksize;
1263 buf->f_blocks = usage.capacity >> shift;
1264 buf->f_bfree = (usage.capacity - usage.used) >> shift;
1265 buf->f_bavail = buf->f_bfree;
1267 buf->f_files = usage.nr_inodes + avail_inodes;
1268 buf->f_ffree = avail_inodes;
1270 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1271 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1272 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1273 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1274 buf->f_namelen = BCH_NAME_MAX;
1279 static int bch2_sync_fs(struct super_block *sb, int wait)
1281 struct bch_fs *c = sb->s_fs_info;
1283 if (c->opts.journal_flush_disabled)
1287 bch2_journal_flush_async(&c->journal, NULL);
1291 return bch2_journal_flush(&c->journal);
1294 static struct bch_fs *bch2_path_to_fs(const char *path)
1300 ret = lookup_bdev(path, &dev);
1302 return ERR_PTR(ret);
1304 c = bch2_dev_to_fs(dev);
1306 closure_put(&c->cl);
1307 return c ?: ERR_PTR(-ENOENT);
1310 static char **split_devs(const char *_dev_name, unsigned *nr)
1312 char *dev_name = NULL, **devs = NULL, *s;
1313 size_t i, nr_devs = 0;
1315 if (strlen(_dev_name) == 0)
1318 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1322 for (s = dev_name; s; s = strchr(s + 1, ':'))
1325 devs = kcalloc(nr_devs + 1, sizeof(const char *), GFP_KERNEL);
1331 for (i = 0, s = dev_name;
1333 (s = strchr(s, ':')) && (*s++ = '\0'))
1340 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1342 struct bch_fs *c = sb->s_fs_info;
1343 struct bch_opts opts = bch2_opts_empty();
1346 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1348 ret = bch2_parse_mount_opts(c, &opts, data);
1352 if (opts.read_only != c->opts.read_only) {
1353 down_write(&c->state_lock);
1355 if (opts.read_only) {
1356 bch2_fs_read_only(c);
1358 sb->s_flags |= SB_RDONLY;
1360 ret = bch2_fs_read_write(c);
1362 bch_err(c, "error going rw: %i", ret);
1363 up_write(&c->state_lock);
1367 sb->s_flags &= ~SB_RDONLY;
1370 c->opts.read_only = opts.read_only;
1372 up_write(&c->state_lock);
1375 if (opts.errors >= 0)
1376 c->opts.errors = opts.errors;
1381 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1383 struct bch_fs *c = root->d_sb->s_fs_info;
1388 for_each_online_member(ca, c, i) {
1392 seq_puts(seq, "/dev/");
1393 seq_puts(seq, ca->name);
1399 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1401 struct bch_fs *c = root->d_sb->s_fs_info;
1405 for (i = 0; i < bch2_opts_nr; i++) {
1406 const struct bch_option *opt = &bch2_opt_table[i];
1407 u64 v = bch2_opt_get_by_id(&c->opts, i);
1409 if (!(opt->mode & OPT_MOUNT))
1412 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1415 bch2_opt_to_text(&PBUF(buf), c, opt, v,
1416 OPT_SHOW_MOUNT_STYLE);
1424 static void bch2_put_super(struct super_block *sb)
1426 struct bch_fs *c = sb->s_fs_info;
1431 static const struct super_operations bch_super_operations = {
1432 .alloc_inode = bch2_alloc_inode,
1433 .destroy_inode = bch2_destroy_inode,
1434 .write_inode = bch2_vfs_write_inode,
1435 .evict_inode = bch2_evict_inode,
1436 .sync_fs = bch2_sync_fs,
1437 .statfs = bch2_statfs,
1438 .show_devname = bch2_show_devname,
1439 .show_options = bch2_show_options,
1440 .remount_fs = bch2_remount,
1441 .put_super = bch2_put_super,
1443 .freeze_fs = bch2_freeze,
1444 .unfreeze_fs = bch2_unfreeze,
1448 static int bch2_set_super(struct super_block *s, void *data)
1450 s->s_fs_info = data;
1454 static int bch2_noset_super(struct super_block *s, void *data)
1459 static int bch2_test_super(struct super_block *s, void *data)
1461 struct bch_fs *c = s->s_fs_info;
1462 struct bch_fs **devs = data;
1468 for (i = 0; devs[i]; i++)
1474 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1475 int flags, const char *dev_name, void *data)
1479 struct super_block *sb;
1480 struct inode *vinode;
1481 struct bch_opts opts = bch2_opts_empty();
1483 struct bch_fs **devs_to_fs = NULL;
1484 unsigned i, nr_devs;
1487 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1489 ret = bch2_parse_mount_opts(NULL, &opts, data);
1491 return ERR_PTR(ret);
1493 devs = split_devs(dev_name, &nr_devs);
1495 return ERR_PTR(-ENOMEM);
1497 devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
1499 sb = ERR_PTR(-ENOMEM);
1503 for (i = 0; i < nr_devs; i++)
1504 devs_to_fs[i] = bch2_path_to_fs(devs[i]);
1506 sb = sget(fs_type, bch2_test_super, bch2_noset_super,
1507 flags|SB_NOSEC, devs_to_fs);
1511 c = bch2_fs_open(devs, nr_devs, opts);
1517 /* Some options can't be parsed until after the fs is started: */
1518 ret = bch2_parse_mount_opts(c, &opts, data);
1525 bch2_opts_apply(&c->opts, opts);
1527 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1536 return ERR_CAST(sb);
1541 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1548 sb->s_blocksize = block_bytes(c);
1549 sb->s_blocksize_bits = ilog2(block_bytes(c));
1550 sb->s_maxbytes = MAX_LFS_FILESIZE;
1551 sb->s_op = &bch_super_operations;
1552 sb->s_export_op = &bch_export_ops;
1553 #ifdef CONFIG_BCACHEFS_QUOTA
1554 sb->s_qcop = &bch2_quotactl_operations;
1555 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1557 sb->s_xattr = bch2_xattr_handlers;
1558 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1559 sb->s_time_gran = c->sb.nsec_per_time_unit;
1560 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1561 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
1563 strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1565 ret = super_setup_bdi(sb);
1569 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1571 for_each_online_member(ca, c, i) {
1572 struct block_device *bdev = ca->disk_sb.bdev;
1574 /* XXX: create an anonymous device for multi device filesystems */
1576 sb->s_dev = bdev->bd_dev;
1577 percpu_ref_put(&ca->io_ref);
1581 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1583 sb->s_flags |= SB_POSIXACL;
1586 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
1587 if (IS_ERR(vinode)) {
1588 bch_err(c, "error mounting: error getting root inode %i",
1589 (int) PTR_ERR(vinode));
1590 ret = PTR_ERR(vinode);
1594 sb->s_root = d_make_root(vinode);
1596 bch_err(c, "error mounting: error allocating root dentry");
1601 sb->s_flags |= SB_ACTIVE;
1603 return dget(sb->s_root);
1606 deactivate_locked_super(sb);
1607 return ERR_PTR(ret);
1610 static void bch2_kill_sb(struct super_block *sb)
1612 struct bch_fs *c = sb->s_fs_info;
1614 generic_shutdown_super(sb);
1618 static struct file_system_type bcache_fs_type = {
1619 .owner = THIS_MODULE,
1621 .mount = bch2_mount,
1622 .kill_sb = bch2_kill_sb,
1623 .fs_flags = FS_REQUIRES_DEV,
1626 MODULE_ALIAS_FS("bcachefs");
1628 void bch2_vfs_exit(void)
1630 unregister_filesystem(&bcache_fs_type);
1631 if (bch2_inode_cache)
1632 kmem_cache_destroy(bch2_inode_cache);
1635 int __init bch2_vfs_init(void)
1639 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1640 if (!bch2_inode_cache)
1643 ret = register_filesystem(&bcache_fs_type);
1653 #endif /* NO_BCACHEFS_FS */