1 // SPDX-License-Identifier: GPL-2.0
6 #include "bkey_on_stack.h"
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/module.h>
29 #include <linux/posix_acl.h>
30 #include <linux/random.h>
31 #include <linux/statfs.h>
32 #include <linux/xattr.h>
34 static struct kmem_cache *bch2_inode_cache;
36 static void bch2_vfs_inode_init(struct bch_fs *,
37 struct bch_inode_info *,
38 struct bch_inode_unpacked *);
40 static void journal_seq_copy(struct bch_inode_info *dst,
43 u64 old, v = READ_ONCE(dst->ei_journal_seq);
48 if (old >= journal_seq)
50 } while ((v = cmpxchg(&dst->ei_journal_seq, old, journal_seq)) != old);
53 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
55 BUG_ON(atomic_long_read(&lock->v) == 0);
57 if (atomic_long_sub_return_release(i, &lock->v) == 0)
58 wake_up_all(&lock->wait);
61 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
63 long v = atomic_long_read(&lock->v), old;
68 if (i > 0 ? v < 0 : v > 0)
70 } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
71 old, old + i)) != old);
75 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
77 wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
80 void bch2_pagecache_add_put(struct pagecache_lock *lock)
82 __pagecache_lock_put(lock, 1);
85 void bch2_pagecache_add_get(struct pagecache_lock *lock)
87 __pagecache_lock_get(lock, 1);
90 void bch2_pagecache_block_put(struct pagecache_lock *lock)
92 __pagecache_lock_put(lock, -1);
95 void bch2_pagecache_block_get(struct pagecache_lock *lock)
97 __pagecache_lock_get(lock, -1);
100 void bch2_inode_update_after_write(struct bch_fs *c,
101 struct bch_inode_info *inode,
102 struct bch_inode_unpacked *bi,
105 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
106 i_uid_write(&inode->v, bi->bi_uid);
107 i_gid_write(&inode->v, bi->bi_gid);
108 inode->v.i_mode = bi->bi_mode;
110 if (fields & ATTR_ATIME)
111 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
112 if (fields & ATTR_MTIME)
113 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
114 if (fields & ATTR_CTIME)
115 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
117 inode->ei_inode = *bi;
119 bch2_inode_flags_to_vfs(inode);
122 int __must_check bch2_write_inode(struct bch_fs *c,
123 struct bch_inode_info *inode,
125 void *p, unsigned fields)
127 struct btree_trans trans;
128 struct btree_iter *iter;
129 struct bch_inode_unpacked inode_u;
132 bch2_trans_init(&trans, c, 0, 0);
134 bch2_trans_begin(&trans);
136 iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
138 ret = PTR_ERR_OR_ZERO(iter) ?:
139 (set ? set(inode, &inode_u, p) : 0) ?:
140 bch2_inode_write(&trans, iter, &inode_u) ?:
141 bch2_trans_commit(&trans, NULL,
142 &inode->ei_journal_seq,
143 BTREE_INSERT_NOUNLOCK|
144 BTREE_INSERT_NOFAIL);
149 * the btree node lock protects inode->ei_inode, not ei_update_lock;
150 * this is important for inode updates via bchfs_write_index_update
153 bch2_inode_update_after_write(c, inode, &inode_u, fields);
155 bch2_trans_exit(&trans);
156 return ret < 0 ? ret : 0;
159 int bch2_fs_quota_transfer(struct bch_fs *c,
160 struct bch_inode_info *inode,
161 struct bch_qid new_qid,
163 enum quota_acct_mode mode)
168 qtypes &= enabled_qtypes(c);
170 for (i = 0; i < QTYP_NR; i++)
171 if (new_qid.q[i] == inode->ei_qid.q[i])
172 qtypes &= ~(1U << i);
177 mutex_lock(&inode->ei_quota_lock);
179 ret = bch2_quota_transfer(c, qtypes, new_qid,
182 inode->ei_quota_reserved,
185 for (i = 0; i < QTYP_NR; i++)
186 if (qtypes & (1 << i))
187 inode->ei_qid.q[i] = new_qid.q[i];
189 mutex_unlock(&inode->ei_quota_lock);
194 struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
196 struct bch_inode_unpacked inode_u;
197 struct bch_inode_info *inode;
200 inode = to_bch_ei(iget_locked(c->vfs_sb, inum));
201 if (unlikely(!inode))
202 return ERR_PTR(-ENOMEM);
203 if (!(inode->v.i_state & I_NEW))
206 ret = bch2_inode_find_by_inum(c, inum, &inode_u);
208 iget_failed(&inode->v);
212 bch2_vfs_inode_init(c, inode, &inode_u);
214 inode->ei_journal_seq = bch2_inode_journal_seq(&c->journal, inum);
216 unlock_new_inode(&inode->v);
221 static struct bch_inode_info *
222 __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
223 umode_t mode, dev_t rdev, bool tmpfile)
225 struct bch_fs *c = dir->v.i_sb->s_fs_info;
226 struct user_namespace *ns = dir->v.i_sb->s_user_ns;
227 struct btree_trans trans;
228 struct bch_inode_unpacked dir_u;
229 struct bch_inode_info *inode, *old;
230 struct bch_inode_unpacked inode_u;
231 struct posix_acl *default_acl = NULL, *acl = NULL;
236 * preallocate acls + vfs inode before btree transaction, so that
237 * nothing can fail after the transaction succeeds:
239 #ifdef CONFIG_BCACHEFS_POSIX_ACL
240 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
244 inode = to_bch_ei(new_inode(c->vfs_sb));
245 if (unlikely(!inode)) {
246 inode = ERR_PTR(-ENOMEM);
250 bch2_inode_init_early(c, &inode_u);
253 mutex_lock(&dir->ei_update_lock);
255 bch2_trans_init(&trans, c, 8, 1024);
257 bch2_trans_begin(&trans);
259 ret = bch2_create_trans(&trans, dir->v.i_ino, &dir_u, &inode_u,
260 !tmpfile ? &dentry->d_name : NULL,
261 from_kuid(ns, current_fsuid()),
262 from_kgid(ns, current_fsgid()),
265 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
266 KEY_TYPE_QUOTA_PREALLOC);
268 goto err_before_quota;
270 ret = bch2_trans_commit(&trans, NULL, &journal_seq,
271 BTREE_INSERT_NOUNLOCK);
273 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
274 KEY_TYPE_QUOTA_WARN);
282 bch2_inode_update_after_write(c, dir, &dir_u,
283 ATTR_MTIME|ATTR_CTIME);
284 journal_seq_copy(dir, journal_seq);
285 mutex_unlock(&dir->ei_update_lock);
288 bch2_vfs_inode_init(c, inode, &inode_u);
289 journal_seq_copy(inode, journal_seq);
291 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
292 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
295 * we must insert the new inode into the inode cache before calling
296 * bch2_trans_exit() and dropping locks, else we could race with another
297 * thread pulling the inode in and modifying it:
300 old = to_bch_ei(insert_inode_locked2(&inode->v));
303 * We raced, another process pulled the new inode into cache
306 journal_seq_copy(old, journal_seq);
307 make_bad_inode(&inode->v);
313 * we really don't want insert_inode_locked2() to be setting
316 unlock_new_inode(&inode->v);
319 bch2_trans_exit(&trans);
321 posix_acl_release(default_acl);
322 posix_acl_release(acl);
326 mutex_unlock(&dir->ei_update_lock);
328 bch2_trans_exit(&trans);
329 make_bad_inode(&inode->v);
331 inode = ERR_PTR(ret);
337 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
340 struct bch_fs *c = vdir->i_sb->s_fs_info;
341 struct bch_inode_info *dir = to_bch_ei(vdir);
342 struct inode *vinode = NULL;
345 inum = bch2_dirent_lookup(c, dir->v.i_ino,
350 vinode = bch2_vfs_inode_get(c, inum);
352 return d_splice_alias(vinode, dentry);
355 static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
356 umode_t mode, dev_t rdev)
358 struct bch_inode_info *inode =
359 __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
362 return PTR_ERR(inode);
364 d_instantiate(dentry, &inode->v);
368 static int bch2_create(struct inode *vdir, struct dentry *dentry,
369 umode_t mode, bool excl)
371 return bch2_mknod(vdir, dentry, mode|S_IFREG, 0);
374 static int __bch2_link(struct bch_fs *c,
375 struct bch_inode_info *inode,
376 struct bch_inode_info *dir,
377 struct dentry *dentry)
379 struct btree_trans trans;
380 struct bch_inode_unpacked dir_u, inode_u;
383 mutex_lock(&inode->ei_update_lock);
384 bch2_trans_init(&trans, c, 4, 1024);
387 bch2_trans_begin(&trans);
388 ret = bch2_link_trans(&trans,
390 inode->v.i_ino, &dir_u, &inode_u,
392 bch2_trans_commit(&trans, NULL,
393 &inode->ei_journal_seq,
394 BTREE_INSERT_NOUNLOCK);
395 } while (ret == -EINTR);
398 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
400 journal_seq_copy(inode, dir->ei_journal_seq);
401 bch2_inode_update_after_write(c, dir, &dir_u,
402 ATTR_MTIME|ATTR_CTIME);
403 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
406 bch2_trans_exit(&trans);
407 mutex_unlock(&inode->ei_update_lock);
411 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
412 struct dentry *dentry)
414 struct bch_fs *c = vdir->i_sb->s_fs_info;
415 struct bch_inode_info *dir = to_bch_ei(vdir);
416 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
419 lockdep_assert_held(&inode->v.i_rwsem);
421 ret = __bch2_link(c, inode, dir, dentry);
426 d_instantiate(dentry, &inode->v);
430 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
432 struct bch_fs *c = vdir->i_sb->s_fs_info;
433 struct bch_inode_info *dir = to_bch_ei(vdir);
434 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
435 struct bch_inode_unpacked dir_u, inode_u;
436 struct btree_trans trans;
439 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
440 bch2_trans_init(&trans, c, 4, 1024);
443 bch2_trans_begin(&trans);
445 ret = bch2_unlink_trans(&trans,
446 dir->v.i_ino, &dir_u,
447 &inode_u, &dentry->d_name) ?:
448 bch2_trans_commit(&trans, NULL,
449 &dir->ei_journal_seq,
450 BTREE_INSERT_NOUNLOCK|
451 BTREE_INSERT_NOFAIL);
452 } while (ret == -EINTR);
455 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
457 journal_seq_copy(inode, dir->ei_journal_seq);
458 bch2_inode_update_after_write(c, dir, &dir_u,
459 ATTR_MTIME|ATTR_CTIME);
460 bch2_inode_update_after_write(c, inode, &inode_u,
464 bch2_trans_exit(&trans);
465 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
470 static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
473 struct bch_fs *c = vdir->i_sb->s_fs_info;
474 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
477 inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
478 if (unlikely(IS_ERR(inode)))
479 return PTR_ERR(inode);
481 inode_lock(&inode->v);
482 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
483 inode_unlock(&inode->v);
488 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
492 journal_seq_copy(dir, inode->ei_journal_seq);
494 ret = __bch2_link(c, inode, dir, dentry);
498 d_instantiate(dentry, &inode->v);
505 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
507 return bch2_mknod(vdir, dentry, mode|S_IFDIR, 0);
510 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
511 struct inode *dst_vdir, struct dentry *dst_dentry,
514 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
515 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
516 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
517 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
518 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
519 struct bch_inode_unpacked dst_dir_u, src_dir_u;
520 struct bch_inode_unpacked src_inode_u, dst_inode_u;
521 struct btree_trans trans;
522 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
523 ? BCH_RENAME_EXCHANGE
524 : dst_dentry->d_inode
525 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
529 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
532 if (mode == BCH_RENAME_OVERWRITE) {
533 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
539 bch2_trans_init(&trans, c, 8, 2048);
541 bch2_lock_inodes(INODE_UPDATE_LOCK,
547 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
548 ret = bch2_fs_quota_transfer(c, src_inode,
551 KEY_TYPE_QUOTA_PREALLOC);
556 if (mode == BCH_RENAME_EXCHANGE &&
557 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
558 ret = bch2_fs_quota_transfer(c, dst_inode,
561 KEY_TYPE_QUOTA_PREALLOC);
567 bch2_trans_begin(&trans);
568 ret = bch2_rename_trans(&trans,
569 src_dir->v.i_ino, &src_dir_u,
570 dst_dir->v.i_ino, &dst_dir_u,
576 bch2_trans_commit(&trans, NULL,
578 BTREE_INSERT_NOUNLOCK);
584 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
586 dst_inode->v.i_ino != dst_inode_u.bi_inum);
588 bch2_inode_update_after_write(c, src_dir, &src_dir_u,
589 ATTR_MTIME|ATTR_CTIME);
590 journal_seq_copy(src_dir, journal_seq);
592 if (src_dir != dst_dir) {
593 bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
594 ATTR_MTIME|ATTR_CTIME);
595 journal_seq_copy(dst_dir, journal_seq);
598 bch2_inode_update_after_write(c, src_inode, &src_inode_u,
600 journal_seq_copy(src_inode, journal_seq);
603 bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
605 journal_seq_copy(dst_inode, journal_seq);
608 bch2_trans_exit(&trans);
610 bch2_fs_quota_transfer(c, src_inode,
611 bch_qid(&src_inode->ei_inode),
613 KEY_TYPE_QUOTA_NOCHECK);
615 bch2_fs_quota_transfer(c, dst_inode,
616 bch_qid(&dst_inode->ei_inode),
618 KEY_TYPE_QUOTA_NOCHECK);
620 bch2_unlock_inodes(INODE_UPDATE_LOCK,
629 void bch2_setattr_copy(struct bch_inode_info *inode,
630 struct bch_inode_unpacked *bi,
633 struct bch_fs *c = inode->v.i_sb->s_fs_info;
634 unsigned int ia_valid = attr->ia_valid;
636 if (ia_valid & ATTR_UID)
637 bi->bi_uid = from_kuid(c->vfs_sb->s_user_ns, attr->ia_uid);
638 if (ia_valid & ATTR_GID)
639 bi->bi_gid = from_kgid(c->vfs_sb->s_user_ns, attr->ia_gid);
641 if (ia_valid & ATTR_ATIME)
642 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
643 if (ia_valid & ATTR_MTIME)
644 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
645 if (ia_valid & ATTR_CTIME)
646 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
648 if (ia_valid & ATTR_MODE) {
649 umode_t mode = attr->ia_mode;
650 kgid_t gid = ia_valid & ATTR_GID
654 if (!in_group_p(gid) &&
655 !capable_wrt_inode_uidgid(&inode->v, CAP_FSETID))
661 static int bch2_setattr_nonsize(struct bch_inode_info *inode,
664 struct bch_fs *c = inode->v.i_sb->s_fs_info;
666 struct btree_trans trans;
667 struct btree_iter *inode_iter;
668 struct bch_inode_unpacked inode_u;
669 struct posix_acl *acl = NULL;
672 mutex_lock(&inode->ei_update_lock);
676 if (attr->ia_valid & ATTR_UID)
677 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
679 if (attr->ia_valid & ATTR_GID)
680 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
682 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
683 KEY_TYPE_QUOTA_PREALLOC);
687 bch2_trans_init(&trans, c, 0, 0);
689 bch2_trans_begin(&trans);
693 inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
695 ret = PTR_ERR_OR_ZERO(inode_iter);
699 bch2_setattr_copy(inode, &inode_u, attr);
701 if (attr->ia_valid & ATTR_MODE) {
702 ret = bch2_acl_chmod(&trans, inode, inode_u.bi_mode, &acl);
707 ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
708 bch2_trans_commit(&trans, NULL,
709 &inode->ei_journal_seq,
710 BTREE_INSERT_NOUNLOCK|
711 BTREE_INSERT_NOFAIL);
718 bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
721 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
723 bch2_trans_exit(&trans);
725 mutex_unlock(&inode->ei_update_lock);
730 static int bch2_getattr(const struct path *path, struct kstat *stat,
731 u32 request_mask, unsigned query_flags)
733 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
734 struct bch_fs *c = inode->v.i_sb->s_fs_info;
736 stat->dev = inode->v.i_sb->s_dev;
737 stat->ino = inode->v.i_ino;
738 stat->mode = inode->v.i_mode;
739 stat->nlink = inode->v.i_nlink;
740 stat->uid = inode->v.i_uid;
741 stat->gid = inode->v.i_gid;
742 stat->rdev = inode->v.i_rdev;
743 stat->size = i_size_read(&inode->v);
744 stat->atime = inode->v.i_atime;
745 stat->mtime = inode->v.i_mtime;
746 stat->ctime = inode->v.i_ctime;
747 stat->blksize = block_bytes(c);
748 stat->blocks = inode->v.i_blocks;
750 if (request_mask & STATX_BTIME) {
751 stat->result_mask |= STATX_BTIME;
752 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
755 if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
756 stat->attributes |= STATX_ATTR_IMMUTABLE;
757 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
759 if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
760 stat->attributes |= STATX_ATTR_APPEND;
761 stat->attributes_mask |= STATX_ATTR_APPEND;
763 if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
764 stat->attributes |= STATX_ATTR_NODUMP;
765 stat->attributes_mask |= STATX_ATTR_NODUMP;
770 static int bch2_setattr(struct dentry *dentry, struct iattr *iattr)
772 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
775 lockdep_assert_held(&inode->v.i_rwsem);
777 ret = setattr_prepare(dentry, iattr);
781 return iattr->ia_valid & ATTR_SIZE
782 ? bch2_truncate(inode, iattr)
783 : bch2_setattr_nonsize(inode, iattr);
786 static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
788 struct bch_inode_info *inode =
789 __bch2_create(to_bch_ei(vdir), dentry, mode, 0, true);
792 return PTR_ERR(inode);
794 d_mark_tmpfile(dentry, &inode->v);
795 d_instantiate(dentry, &inode->v);
799 static int bch2_fill_extent(struct bch_fs *c,
800 struct fiemap_extent_info *info,
801 struct bkey_s_c k, unsigned flags)
803 if (bkey_extent_is_data(k.k)) {
804 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
805 const union bch_extent_entry *entry;
806 struct extent_ptr_decoded p;
809 if (k.k->type == KEY_TYPE_reflink_v)
810 flags |= FIEMAP_EXTENT_SHARED;
812 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
814 u64 offset = p.ptr.offset;
816 if (p.crc.compression_type)
817 flags2 |= FIEMAP_EXTENT_ENCODED;
819 offset += p.crc.offset;
821 if ((offset & (c->opts.block_size - 1)) ||
822 (k.k->size & (c->opts.block_size - 1)))
823 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
825 ret = fiemap_fill_next_extent(info,
826 bkey_start_offset(k.k) << 9,
828 k.k->size << 9, flags|flags2);
834 } else if (k.k->type == KEY_TYPE_reservation) {
835 return fiemap_fill_next_extent(info,
836 bkey_start_offset(k.k) << 9,
839 FIEMAP_EXTENT_DELALLOC|
840 FIEMAP_EXTENT_UNWRITTEN);
846 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
849 struct bch_fs *c = vinode->i_sb->s_fs_info;
850 struct bch_inode_info *ei = to_bch_ei(vinode);
851 struct btree_trans trans;
852 struct btree_iter *iter;
854 struct bkey_on_stack cur, prev;
855 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
856 unsigned offset_into_extent, sectors;
857 bool have_extent = false;
860 if (start + len < start)
863 bkey_on_stack_init(&cur);
864 bkey_on_stack_init(&prev);
865 bch2_trans_init(&trans, c, 0, 0);
867 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
868 POS(ei->v.i_ino, start >> 9), 0);
870 while ((k = bch2_btree_iter_peek(iter)).k &&
871 !(ret = bkey_err(k)) &&
872 bkey_cmp(iter->pos, end) < 0) {
873 if (!bkey_extent_is_data(k.k) &&
874 k.k->type != KEY_TYPE_reservation) {
875 bch2_btree_iter_next(iter);
879 bkey_on_stack_realloc(&cur, c, k.k->u64s);
880 bkey_on_stack_realloc(&prev, c, k.k->u64s);
881 bkey_reassemble(cur.k, k);
882 k = bkey_i_to_s_c(cur.k);
884 offset_into_extent = iter->pos.offset -
885 bkey_start_offset(k.k);
886 sectors = k.k->size - offset_into_extent;
888 ret = bch2_read_indirect_extent(&trans,
889 &offset_into_extent, cur.k);
893 sectors = min(sectors, k.k->size - offset_into_extent);
895 if (offset_into_extent)
896 bch2_cut_front(POS(k.k->p.inode,
897 bkey_start_offset(k.k) +
900 bch2_key_resize(&cur.k->k, sectors);
901 cur.k->k.p = iter->pos;
902 cur.k->k.p.offset += cur.k->k.size;
905 ret = bch2_fill_extent(c, info,
906 bkey_i_to_s_c(prev.k), 0);
911 bkey_copy(prev.k, cur.k);
914 if (k.k->type == KEY_TYPE_reflink_v)
915 bch2_btree_iter_set_pos(iter, k.k->p);
917 bch2_btree_iter_next(iter);
923 if (!ret && have_extent)
924 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
927 ret = bch2_trans_exit(&trans) ?: ret;
928 bkey_on_stack_exit(&cur, c);
929 bkey_on_stack_exit(&prev, c);
930 return ret < 0 ? ret : 0;
933 static const struct vm_operations_struct bch_vm_ops = {
934 .fault = bch2_page_fault,
935 .map_pages = filemap_map_pages,
936 .page_mkwrite = bch2_page_mkwrite,
939 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
943 vma->vm_ops = &bch_vm_ops;
949 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
951 return generic_file_llseek_size(file, offset, whence,
955 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
957 struct bch_inode_info *inode = file_bch_inode(file);
958 struct bch_fs *c = inode->v.i_sb->s_fs_info;
960 if (!dir_emit_dots(file, ctx))
963 return bch2_readdir(c, inode->v.i_ino, ctx);
966 static const struct file_operations bch_file_operations = {
967 .llseek = bch2_llseek,
968 .read_iter = bch2_read_iter,
969 .write_iter = bch2_write_iter,
971 .open = generic_file_open,
973 .splice_read = generic_file_splice_read,
976 .splice_write = iter_file_splice_write,
978 .fallocate = bch2_fallocate_dispatch,
979 .unlocked_ioctl = bch2_fs_file_ioctl,
981 .compat_ioctl = bch2_compat_fs_ioctl,
983 .remap_file_range = bch2_remap_file_range,
986 static const struct inode_operations bch_file_inode_operations = {
987 .getattr = bch2_getattr,
988 .setattr = bch2_setattr,
989 .fiemap = bch2_fiemap,
990 .listxattr = bch2_xattr_list,
991 #ifdef CONFIG_BCACHEFS_POSIX_ACL
992 .get_acl = bch2_get_acl,
993 .set_acl = bch2_set_acl,
997 static const struct inode_operations bch_dir_inode_operations = {
998 .lookup = bch2_lookup,
999 .create = bch2_create,
1001 .unlink = bch2_unlink,
1002 .symlink = bch2_symlink,
1003 .mkdir = bch2_mkdir,
1004 .rmdir = bch2_unlink,
1005 .mknod = bch2_mknod,
1006 .rename = bch2_rename2,
1007 .getattr = bch2_getattr,
1008 .setattr = bch2_setattr,
1009 .tmpfile = bch2_tmpfile,
1010 .listxattr = bch2_xattr_list,
1011 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1012 .get_acl = bch2_get_acl,
1013 .set_acl = bch2_set_acl,
1017 static const struct file_operations bch_dir_file_operations = {
1018 .llseek = bch2_dir_llseek,
1019 .read = generic_read_dir,
1020 .iterate_shared = bch2_vfs_readdir,
1021 .fsync = bch2_fsync,
1022 .unlocked_ioctl = bch2_fs_file_ioctl,
1023 #ifdef CONFIG_COMPAT
1024 .compat_ioctl = bch2_compat_fs_ioctl,
1028 static const struct inode_operations bch_symlink_inode_operations = {
1029 .get_link = page_get_link,
1030 .getattr = bch2_getattr,
1031 .setattr = bch2_setattr,
1032 .listxattr = bch2_xattr_list,
1033 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1034 .get_acl = bch2_get_acl,
1035 .set_acl = bch2_set_acl,
1039 static const struct inode_operations bch_special_inode_operations = {
1040 .getattr = bch2_getattr,
1041 .setattr = bch2_setattr,
1042 .listxattr = bch2_xattr_list,
1043 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1044 .get_acl = bch2_get_acl,
1045 .set_acl = bch2_set_acl,
1049 static const struct address_space_operations bch_address_space_operations = {
1050 .writepage = bch2_writepage,
1051 .readpage = bch2_readpage,
1052 .writepages = bch2_writepages,
1053 .readpages = bch2_readpages,
1054 .set_page_dirty = __set_page_dirty_nobuffers,
1055 .write_begin = bch2_write_begin,
1056 .write_end = bch2_write_end,
1057 .invalidatepage = bch2_invalidatepage,
1058 .releasepage = bch2_releasepage,
1059 .direct_IO = noop_direct_IO,
1060 #ifdef CONFIG_MIGRATION
1061 .migratepage = bch2_migrate_page,
1063 .error_remove_page = generic_error_remove_page,
1066 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1067 u64 ino, u32 generation)
1069 struct bch_fs *c = sb->s_fs_info;
1070 struct inode *vinode;
1072 if (ino < BCACHEFS_ROOT_INO)
1073 return ERR_PTR(-ESTALE);
1075 vinode = bch2_vfs_inode_get(c, ino);
1077 return ERR_CAST(vinode);
1078 if (generation && vinode->i_generation != generation) {
1079 /* we didn't find the right inode.. */
1081 return ERR_PTR(-ESTALE);
1086 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *fid,
1087 int fh_len, int fh_type)
1089 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1090 bch2_nfs_get_inode);
1093 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *fid,
1094 int fh_len, int fh_type)
1096 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1097 bch2_nfs_get_inode);
1100 static const struct export_operations bch_export_ops = {
1101 .fh_to_dentry = bch2_fh_to_dentry,
1102 .fh_to_parent = bch2_fh_to_parent,
1103 //.get_parent = bch2_get_parent,
1106 static void bch2_vfs_inode_init(struct bch_fs *c,
1107 struct bch_inode_info *inode,
1108 struct bch_inode_unpacked *bi)
1110 bch2_inode_update_after_write(c, inode, bi, ~0);
1112 inode->v.i_blocks = bi->bi_sectors;
1113 inode->v.i_ino = bi->bi_inum;
1114 inode->v.i_rdev = bi->bi_dev;
1115 inode->v.i_generation = bi->bi_generation;
1116 inode->v.i_size = bi->bi_size;
1118 inode->ei_journal_seq = 0;
1119 inode->ei_quota_reserved = 0;
1120 inode->ei_str_hash = bch2_hash_info_init(c, bi);
1121 inode->ei_qid = bch_qid(bi);
1123 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1125 switch (inode->v.i_mode & S_IFMT) {
1127 inode->v.i_op = &bch_file_inode_operations;
1128 inode->v.i_fop = &bch_file_operations;
1131 inode->v.i_op = &bch_dir_inode_operations;
1132 inode->v.i_fop = &bch_dir_file_operations;
1135 inode_nohighmem(&inode->v);
1136 inode->v.i_op = &bch_symlink_inode_operations;
1139 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1140 inode->v.i_op = &bch_special_inode_operations;
1145 static struct inode *bch2_alloc_inode(struct super_block *sb)
1147 struct bch_inode_info *inode;
1149 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1153 inode_init_once(&inode->v);
1154 mutex_init(&inode->ei_update_lock);
1155 pagecache_lock_init(&inode->ei_pagecache_lock);
1156 mutex_init(&inode->ei_quota_lock);
1157 inode->ei_journal_seq = 0;
1162 static void bch2_i_callback(struct rcu_head *head)
1164 struct inode *vinode = container_of(head, struct inode, i_rcu);
1165 struct bch_inode_info *inode = to_bch_ei(vinode);
1167 kmem_cache_free(bch2_inode_cache, inode);
1170 static void bch2_destroy_inode(struct inode *vinode)
1172 call_rcu(&vinode->i_rcu, bch2_i_callback);
1175 static int inode_update_times_fn(struct bch_inode_info *inode,
1176 struct bch_inode_unpacked *bi,
1179 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1181 bi->bi_atime = timespec_to_bch2_time(c, inode->v.i_atime);
1182 bi->bi_mtime = timespec_to_bch2_time(c, inode->v.i_mtime);
1183 bi->bi_ctime = timespec_to_bch2_time(c, inode->v.i_ctime);
1188 static int bch2_vfs_write_inode(struct inode *vinode,
1189 struct writeback_control *wbc)
1191 struct bch_fs *c = vinode->i_sb->s_fs_info;
1192 struct bch_inode_info *inode = to_bch_ei(vinode);
1195 mutex_lock(&inode->ei_update_lock);
1196 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1197 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1198 mutex_unlock(&inode->ei_update_lock);
1203 static void bch2_evict_inode(struct inode *vinode)
1205 struct bch_fs *c = vinode->i_sb->s_fs_info;
1206 struct bch_inode_info *inode = to_bch_ei(vinode);
1208 truncate_inode_pages_final(&inode->v.i_data);
1210 clear_inode(&inode->v);
1212 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1214 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1215 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1216 KEY_TYPE_QUOTA_WARN);
1217 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1218 KEY_TYPE_QUOTA_WARN);
1219 bch2_inode_rm(c, inode->v.i_ino);
1223 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1225 struct super_block *sb = dentry->d_sb;
1226 struct bch_fs *c = sb->s_fs_info;
1227 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1228 unsigned shift = sb->s_blocksize_bits - 9;
1231 buf->f_type = BCACHEFS_STATFS_MAGIC;
1232 buf->f_bsize = sb->s_blocksize;
1233 buf->f_blocks = usage.capacity >> shift;
1234 buf->f_bfree = (usage.capacity - usage.used) >> shift;
1235 buf->f_bavail = buf->f_bfree;
1236 buf->f_files = usage.nr_inodes;
1237 buf->f_ffree = U64_MAX;
1239 fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1240 le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1241 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1242 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1243 buf->f_namelen = BCH_NAME_MAX;
1248 static int bch2_sync_fs(struct super_block *sb, int wait)
1250 struct bch_fs *c = sb->s_fs_info;
1252 if (c->opts.journal_flush_disabled)
1256 bch2_journal_flush_async(&c->journal, NULL);
1260 return bch2_journal_flush(&c->journal);
1263 static struct bch_fs *bch2_path_to_fs(const char *dev)
1266 struct block_device *bdev = lookup_bdev(dev);
1269 return ERR_CAST(bdev);
1271 c = bch2_bdev_to_fs(bdev);
1273 return c ?: ERR_PTR(-ENOENT);
1276 static struct bch_fs *__bch2_open_as_blockdevs(const char *dev_name, char * const *devs,
1277 unsigned nr_devs, struct bch_opts opts)
1279 struct bch_fs *c, *c1, *c2;
1283 return ERR_PTR(-EINVAL);
1285 c = bch2_fs_open(devs, nr_devs, opts);
1287 if (IS_ERR(c) && PTR_ERR(c) == -EBUSY) {
1290 * Look up each block device, make sure they all belong to a
1291 * filesystem and they all belong to the _same_ filesystem
1294 c1 = bch2_path_to_fs(devs[0]);
1298 for (i = 1; i < nr_devs; i++) {
1299 c2 = bch2_path_to_fs(devs[i]);
1301 closure_put(&c2->cl);
1304 closure_put(&c1->cl);
1315 mutex_lock(&c->state_lock);
1317 if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1318 mutex_unlock(&c->state_lock);
1319 closure_put(&c->cl);
1320 pr_err("err mounting %s: incomplete filesystem", dev_name);
1321 return ERR_PTR(-EINVAL);
1324 mutex_unlock(&c->state_lock);
1326 set_bit(BCH_FS_BDEV_MOUNTED, &c->flags);
1330 static struct bch_fs *bch2_open_as_blockdevs(const char *_dev_name,
1331 struct bch_opts opts)
1333 char *dev_name = NULL, **devs = NULL, *s;
1334 struct bch_fs *c = ERR_PTR(-ENOMEM);
1335 size_t i, nr_devs = 0;
1337 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1341 for (s = dev_name; s; s = strchr(s + 1, ':'))
1344 devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1348 for (i = 0, s = dev_name;
1350 (s = strchr(s, ':')) && (*s++ = '\0'))
1353 c = __bch2_open_as_blockdevs(_dev_name, devs, nr_devs, opts);
1360 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1362 struct bch_fs *c = sb->s_fs_info;
1363 struct bch_opts opts = bch2_opts_empty();
1366 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1368 ret = bch2_parse_mount_opts(&opts, data);
1372 if (opts.read_only != c->opts.read_only) {
1373 mutex_lock(&c->state_lock);
1375 if (opts.read_only) {
1376 bch2_fs_read_only(c);
1378 sb->s_flags |= SB_RDONLY;
1380 ret = bch2_fs_read_write(c);
1382 bch_err(c, "error going rw: %i", ret);
1383 mutex_unlock(&c->state_lock);
1387 sb->s_flags &= ~SB_RDONLY;
1390 c->opts.read_only = opts.read_only;
1392 mutex_unlock(&c->state_lock);
1395 if (opts.errors >= 0)
1396 c->opts.errors = opts.errors;
1401 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1403 struct bch_fs *c = root->d_sb->s_fs_info;
1407 for (i = 0; i < bch2_opts_nr; i++) {
1408 const struct bch_option *opt = &bch2_opt_table[i];
1409 u64 v = bch2_opt_get_by_id(&c->opts, i);
1411 if (!(opt->mode & OPT_MOUNT))
1414 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1417 bch2_opt_to_text(&PBUF(buf), c, opt, v,
1418 OPT_SHOW_MOUNT_STYLE);
1427 static const struct super_operations bch_super_operations = {
1428 .alloc_inode = bch2_alloc_inode,
1429 .destroy_inode = bch2_destroy_inode,
1430 .write_inode = bch2_vfs_write_inode,
1431 .evict_inode = bch2_evict_inode,
1432 .sync_fs = bch2_sync_fs,
1433 .statfs = bch2_statfs,
1434 .show_options = bch2_show_options,
1435 .remount_fs = bch2_remount,
1437 .put_super = bch2_put_super,
1438 .freeze_fs = bch2_freeze,
1439 .unfreeze_fs = bch2_unfreeze,
1443 static int bch2_test_super(struct super_block *s, void *data)
1445 return s->s_fs_info == data;
1448 static int bch2_set_super(struct super_block *s, void *data)
1450 s->s_fs_info = data;
1454 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1455 int flags, const char *dev_name, void *data)
1459 struct super_block *sb;
1460 struct inode *vinode;
1461 struct bch_opts opts = bch2_opts_empty();
1465 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1467 ret = bch2_parse_mount_opts(&opts, data);
1469 return ERR_PTR(ret);
1471 c = bch2_open_as_blockdevs(dev_name, opts);
1475 sb = sget(fs_type, bch2_test_super, bch2_set_super, flags|SB_NOSEC, c);
1477 closure_put(&c->cl);
1478 return ERR_CAST(sb);
1481 BUG_ON(sb->s_fs_info != c);
1484 closure_put(&c->cl);
1486 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1493 sb->s_blocksize = block_bytes(c);
1494 sb->s_blocksize_bits = ilog2(block_bytes(c));
1495 sb->s_maxbytes = MAX_LFS_FILESIZE;
1496 sb->s_op = &bch_super_operations;
1497 sb->s_export_op = &bch_export_ops;
1498 #ifdef CONFIG_BCACHEFS_QUOTA
1499 sb->s_qcop = &bch2_quotactl_operations;
1500 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1502 sb->s_xattr = bch2_xattr_handlers;
1503 sb->s_magic = BCACHEFS_STATFS_MAGIC;
1504 sb->s_time_gran = c->sb.time_precision;
1506 strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1508 ret = super_setup_bdi(sb);
1512 sb->s_bdi->congested_fn = bch2_congested;
1513 sb->s_bdi->congested_data = c;
1514 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1516 for_each_online_member(ca, c, i) {
1517 struct block_device *bdev = ca->disk_sb.bdev;
1519 /* XXX: create an anonymous device for multi device filesystems */
1521 sb->s_dev = bdev->bd_dev;
1522 percpu_ref_put(&ca->io_ref);
1526 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1528 sb->s_flags |= SB_POSIXACL;
1531 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
1532 if (IS_ERR(vinode)) {
1533 bch_err(c, "error mounting: error getting root inode %i",
1534 (int) PTR_ERR(vinode));
1535 ret = PTR_ERR(vinode);
1539 sb->s_root = d_make_root(vinode);
1541 bch_err(c, "error mounting: error allocating root dentry");
1546 sb->s_flags |= SB_ACTIVE;
1548 return dget(sb->s_root);
1551 deactivate_locked_super(sb);
1552 return ERR_PTR(ret);
1555 static void bch2_kill_sb(struct super_block *sb)
1557 struct bch_fs *c = sb->s_fs_info;
1559 generic_shutdown_super(sb);
1561 if (test_bit(BCH_FS_BDEV_MOUNTED, &c->flags))
1564 closure_put(&c->cl);
1567 static struct file_system_type bcache_fs_type = {
1568 .owner = THIS_MODULE,
1570 .mount = bch2_mount,
1571 .kill_sb = bch2_kill_sb,
1572 .fs_flags = FS_REQUIRES_DEV,
1575 MODULE_ALIAS_FS("bcachefs");
1577 void bch2_vfs_exit(void)
1579 unregister_filesystem(&bcache_fs_type);
1580 if (bch2_inode_cache)
1581 kmem_cache_destroy(bch2_inode_cache);
1584 int __init bch2_vfs_init(void)
1588 bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1589 if (!bch2_inode_cache)
1592 ret = register_filesystem(&bcache_fs_type);
1602 #endif /* NO_BCACHEFS_FS */