]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/fs.c
Update bcachefs sources to 0a9f0fc68a bcachefs: Don't unconditially version_upgrade...
[bcachefs-tools-debian] / libbcachefs / fs.c
index 13670a638e0a3c7abfde55bf7e9a1843fec66db7..1cca02f0aa3de2cf3ddb0e714e79e4b685cda278 100644 (file)
@@ -1,13 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
 #ifndef NO_BCACHEFS_FS
 
 #include "bcachefs.h"
 #include "acl.h"
+#include "bkey_buf.h"
 #include "btree_update.h"
 #include "buckets.h"
 #include "chardev.h"
 #include "dirent.h"
 #include "extents.h"
 #include "fs.h"
+#include "fs-common.h"
 #include "fs-io.h"
 #include "fs-ioctl.h"
 #include "fsck.h"
@@ -22,6 +25,7 @@
 #include <linux/aio.h>
 #include <linux/backing-dev.h>
 #include <linux/exportfs.h>
+#include <linux/fiemap.h>
 #include <linux/module.h>
 #include <linux/posix_acl.h>
 #include <linux/random.h>
@@ -34,9 +38,15 @@ static void bch2_vfs_inode_init(struct bch_fs *,
                                struct bch_inode_info *,
                                struct bch_inode_unpacked *);
 
-static void journal_seq_copy(struct bch_inode_info *dst,
+static void journal_seq_copy(struct bch_fs *c,
+                            struct bch_inode_info *dst,
                             u64 journal_seq)
 {
+       /*
+        * atomic64_cmpxchg has a fallback for archs that don't support it,
+        * cmpxchg does not:
+        */
+       atomic64_t *dst_seq = (void *) &dst->ei_journal_seq;
        u64 old, v = READ_ONCE(dst->ei_journal_seq);
 
        do {
@@ -44,69 +54,69 @@ static void journal_seq_copy(struct bch_inode_info *dst,
 
                if (old >= journal_seq)
                        break;
-       } while ((v = cmpxchg(&dst->ei_journal_seq, old, journal_seq)) != old);
+       } while ((v = atomic64_cmpxchg(dst_seq, old, journal_seq)) != old);
+
+       bch2_journal_set_has_inum(&c->journal, dst->v.i_ino, journal_seq);
+}
+
+static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
+{
+       BUG_ON(atomic_long_read(&lock->v) == 0);
+
+       if (atomic_long_sub_return_release(i, &lock->v) == 0)
+               wake_up_all(&lock->wait);
+}
+
+static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
+{
+       long v = atomic_long_read(&lock->v), old;
+
+       do {
+               old = v;
+
+               if (i > 0 ? v < 0 : v > 0)
+                       return false;
+       } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
+                                       old, old + i)) != old);
+       return true;
+}
+
+static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
+{
+       wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
+}
+
+void bch2_pagecache_add_put(struct pagecache_lock *lock)
+{
+       __pagecache_lock_put(lock, 1);
+}
+
+bool bch2_pagecache_add_tryget(struct pagecache_lock *lock)
+{
+       return __pagecache_lock_tryget(lock, 1);
+}
+
+void bch2_pagecache_add_get(struct pagecache_lock *lock)
+{
+       __pagecache_lock_get(lock, 1);
 }
 
-static inline int ptrcmp(void *l, void *r)
+void bch2_pagecache_block_put(struct pagecache_lock *lock)
 {
-       return (l > r) - (l < r);
+       __pagecache_lock_put(lock, -1);
 }
 
-#define __bch2_lock_inodes(_lock, ...)                                 \
-do {                                                                   \
-       struct bch_inode_info *a[] = { NULL, __VA_ARGS__ };             \
-       unsigned i;                                                     \
-                                                                       \
-       bubble_sort(&a[1], ARRAY_SIZE(a) - 1 , ptrcmp);                 \
-                                                                       \
-       for (i = ARRAY_SIZE(a) - 1; a[i]; --i)                          \
-               if (a[i] != a[i - 1]) {                                 \
-                       if (_lock)                                      \
-                               mutex_lock_nested(&a[i]->ei_update_lock, i);\
-                       else                                            \
-                               mutex_unlock(&a[i]->ei_update_lock);    \
-               }                                                       \
-} while (0)
-
-#define bch2_lock_inodes(...)  __bch2_lock_inodes(true, __VA_ARGS__)
-#define bch2_unlock_inodes(...)        __bch2_lock_inodes(false, __VA_ARGS__)
-
-/*
- * I_SIZE_DIRTY requires special handling:
- *
- * To the recovery code, the flag means that there is stale data past i_size
- * that needs to be deleted; it's used for implementing atomic appends and
- * truncates.
- *
- * On append, we set I_SIZE_DIRTY before doing the write, then after the write
- * we clear I_SIZE_DIRTY atomically with updating i_size to the new larger size
- * that exposes the data we just wrote.
- *
- * On truncate, it's the reverse: We set I_SIZE_DIRTY atomically with setting
- * i_size to the new smaller size, then we delete the data that we just made
- * invisible, and then we clear I_SIZE_DIRTY.
- *
- * Because there can be multiple appends in flight at a time, we need a refcount
- * (i_size_dirty_count) instead of manipulating the flag directly. Nonzero
- * refcount means I_SIZE_DIRTY is set, zero means it's cleared.
- *
- * Because write_inode() can be called at any time, i_size_dirty_count means
- * something different to the runtime code - it means to write_inode() "don't
- * update i_size yet".
- *
- * We don't clear I_SIZE_DIRTY directly, we let write_inode() clear it when
- * i_size_dirty_count is zero - but the reverse is not true, I_SIZE_DIRTY must
- * be set explicitly.
- */
+void bch2_pagecache_block_get(struct pagecache_lock *lock)
+{
+       __pagecache_lock_get(lock, -1);
+}
 
 void bch2_inode_update_after_write(struct bch_fs *c,
                                   struct bch_inode_info *inode,
                                   struct bch_inode_unpacked *bi,
                                   unsigned fields)
 {
-       set_nlink(&inode->v, bi->bi_flags & BCH_INODE_UNLINKED
-                 ? 0
-                 : bi->bi_nlink + nlink_bias(inode->v.i_mode));
+       set_nlink(&inode->v, bch2_inode_nlink_get(bi));
        i_uid_write(&inode->v, bi->bi_uid);
        i_gid_write(&inode->v, bi->bi_gid);
        inode->v.i_mode = bi->bi_mode;
@@ -119,72 +129,33 @@ void bch2_inode_update_after_write(struct bch_fs *c,
                inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
 
        inode->ei_inode         = *bi;
-       inode->ei_qid           = bch_qid(bi);
 
        bch2_inode_flags_to_vfs(inode);
 }
 
-int __must_check bch2_write_inode_trans(struct btree_trans *trans,
-                               struct bch_inode_info *inode,
-                               struct bch_inode_unpacked *inode_u,
-                               inode_set_fn set,
-                               void *p)
-{
-       struct btree_iter *iter;
-       struct bkey_inode_buf *inode_p;
-       int ret;
-
-       lockdep_assert_held(&inode->ei_update_lock);
-
-       iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
-                       POS(inode->v.i_ino, 0),
-                       BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
-       if (IS_ERR(iter))
-               return PTR_ERR(iter);
-
-       /* The btree node lock is our lock on the inode: */
-       ret = bch2_btree_iter_traverse(iter);
-       if (ret)
-               return ret;
-
-       *inode_u = inode->ei_inode;
-
-       if (set) {
-               ret = set(inode, inode_u, p);
-               if (ret)
-                       return ret;
-       }
-
-       inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
-       if (IS_ERR(inode_p))
-               return PTR_ERR(inode_p);
-
-       bch2_inode_pack(inode_p, inode_u);
-       bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
-       return 0;
-}
-
 int __must_check bch2_write_inode(struct bch_fs *c,
                                  struct bch_inode_info *inode,
                                  inode_set_fn set,
                                  void *p, unsigned fields)
 {
        struct btree_trans trans;
+       struct btree_iter *iter;
        struct bch_inode_unpacked inode_u;
        int ret;
 
-       bch2_trans_init(&trans, c);
+       bch2_trans_init(&trans, c, 0, 0);
 retry:
        bch2_trans_begin(&trans);
 
-       ret = bch2_write_inode_trans(&trans, inode, &inode_u, set, p) ?:
+       iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
+                              BTREE_ITER_INTENT);
+       ret   = PTR_ERR_OR_ZERO(iter) ?:
+               (set ? set(inode, &inode_u, p) : 0) ?:
+               bch2_inode_write(&trans, iter, &inode_u) ?:
                bch2_trans_commit(&trans, NULL,
                                  &inode->ei_journal_seq,
-                                 BTREE_INSERT_ATOMIC|
                                  BTREE_INSERT_NOUNLOCK|
                                  BTREE_INSERT_NOFAIL);
-       if (ret == -EINTR)
-               goto retry;
 
        /*
         * the btree node lock protects inode->ei_inode, not ei_update_lock;
@@ -193,11 +164,51 @@ retry:
        if (!ret)
                bch2_inode_update_after_write(c, inode, &inode_u, fields);
 
+       bch2_trans_iter_put(&trans, iter);
+
+       if (ret == -EINTR)
+               goto retry;
+
        bch2_trans_exit(&trans);
        return ret < 0 ? ret : 0;
 }
 
-static struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
+int bch2_fs_quota_transfer(struct bch_fs *c,
+                          struct bch_inode_info *inode,
+                          struct bch_qid new_qid,
+                          unsigned qtypes,
+                          enum quota_acct_mode mode)
+{
+       unsigned i;
+       int ret;
+
+       qtypes &= enabled_qtypes(c);
+
+       for (i = 0; i < QTYP_NR; i++)
+               if (new_qid.q[i] == inode->ei_qid.q[i])
+                       qtypes &= ~(1U << i);
+
+       if (!qtypes)
+               return 0;
+
+       mutex_lock(&inode->ei_quota_lock);
+
+       ret = bch2_quota_transfer(c, qtypes, new_qid,
+                                 inode->ei_qid,
+                                 inode->v.i_blocks +
+                                 inode->ei_quota_reserved,
+                                 mode);
+       if (!ret)
+               for (i = 0; i < QTYP_NR; i++)
+                       if (qtypes & (1 << i))
+                               inode->ei_qid.q[i] = new_qid.q[i];
+
+       mutex_unlock(&inode->ei_quota_lock);
+
+       return ret;
+}
+
+struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
 {
        struct bch_inode_unpacked inode_u;
        struct bch_inode_info *inode;
@@ -224,37 +235,11 @@ static struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
        return &inode->v;
 }
 
-static void bch2_inode_init_owner(struct bch_inode_unpacked *inode_u,
-                                 const struct inode *dir, umode_t mode)
+static int inum_test(struct inode *inode, void *p)
 {
-       kuid_t uid = current_fsuid();
-       kgid_t gid;
-
-       if (dir && dir->i_mode & S_ISGID) {
-               gid = dir->i_gid;
-               if (S_ISDIR(mode))
-                       mode |= S_ISGID;
-       } else
-               gid = current_fsgid();
-
-       inode_u->bi_uid         = from_kuid(dir->i_sb->s_user_ns, uid);
-       inode_u->bi_gid         = from_kgid(dir->i_sb->s_user_ns, gid);
-       inode_u->bi_mode        = mode;
-}
+       unsigned long *ino = p;
 
-static int inode_update_for_create_fn(struct bch_inode_info *inode,
-                                     struct bch_inode_unpacked *bi,
-                                     void *p)
-{
-       struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct bch_inode_unpacked *new_inode = p;
-
-       bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
-
-       if (S_ISDIR(new_inode->bi_mode))
-               bi->bi_nlink++;
-
-       return 0;
+       return *ino == inode->i_ino;
 }
 
 static struct bch_inode_info *
@@ -262,94 +247,71 @@ __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
              umode_t mode, dev_t rdev, bool tmpfile)
 {
        struct bch_fs *c = dir->v.i_sb->s_fs_info;
+       struct user_namespace *ns = dir->v.i_sb->s_user_ns;
        struct btree_trans trans;
        struct bch_inode_unpacked dir_u;
        struct bch_inode_info *inode, *old;
        struct bch_inode_unpacked inode_u;
-       struct bch_hash_info hash_info;
        struct posix_acl *default_acl = NULL, *acl = NULL;
        u64 journal_seq = 0;
        int ret;
 
-       bch2_inode_init(c, &inode_u, 0, 0, 0, rdev, &dir->ei_inode);
-       bch2_inode_init_owner(&inode_u, &dir->v, mode);
-
-       inode_u.bi_project = dir->ei_qid.q[QTYP_PRJ];
-
-       hash_info = bch2_hash_info_init(c, &inode_u);
-
-       if (tmpfile)
-               inode_u.bi_flags |= BCH_INODE_UNLINKED;
-
-       ret = bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1, KEY_TYPE_QUOTA_PREALLOC);
-       if (ret)
-               return ERR_PTR(ret);
-
+       /*
+        * preallocate acls + vfs inode before btree transaction, so that
+        * nothing can fail after the transaction succeeds:
+        */
 #ifdef CONFIG_BCACHEFS_POSIX_ACL
-       ret = posix_acl_create(&dir->v, &inode_u.bi_mode, &default_acl, &acl);
+       ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
        if (ret)
-               goto err;
+               return ERR_PTR(ret);
 #endif
-
-       /*
-        * preallocate vfs inode before btree transaction, so that nothing can
-        * fail after the transaction succeeds:
-        */
        inode = to_bch_ei(new_inode(c->vfs_sb));
        if (unlikely(!inode)) {
-               ret = -ENOMEM;
+               inode = ERR_PTR(-ENOMEM);
                goto err;
        }
 
+       bch2_inode_init_early(c, &inode_u);
+
        if (!tmpfile)
                mutex_lock(&dir->ei_update_lock);
 
-       bch2_trans_init(&trans, c);
+       bch2_trans_init(&trans, c, 8,
+                       2048 + (!tmpfile ? dentry->d_name.len : 0));
 retry:
        bch2_trans_begin(&trans);
 
-       ret   = __bch2_inode_create(&trans, &inode_u,
-                                   BLOCKDEV_INODE_MAX, 0,
-                                   &c->unused_inode_hint) ?:
-               (default_acl
-                ? bch2_set_acl_trans(&trans, &inode_u, &hash_info,
-                                     default_acl, ACL_TYPE_DEFAULT)
-                : 0) ?:
-               (acl
-                ? bch2_set_acl_trans(&trans, &inode_u, &hash_info,
-                                     acl, ACL_TYPE_ACCESS)
-                : 0) ?:
-               (!tmpfile
-                ? __bch2_dirent_create(&trans, dir->v.i_ino,
-                                       &dir->ei_str_hash,
-                                       mode_to_type(mode),
-                                       &dentry->d_name,
-                                       inode_u.bi_inum,
-                                       BCH_HASH_SET_MUST_CREATE)
-               : 0) ?:
-               (!tmpfile
-                ? bch2_write_inode_trans(&trans, dir, &dir_u,
-                                         inode_update_for_create_fn,
-                                         &inode_u)
-                : 0) ?:
-               bch2_trans_commit(&trans, NULL,
-                                 &journal_seq,
-                                 BTREE_INSERT_ATOMIC|
-                                 BTREE_INSERT_NOUNLOCK);
-       if (ret == -EINTR)
-               goto retry;
+       ret   = bch2_create_trans(&trans, dir->v.i_ino, &dir_u, &inode_u,
+                                 !tmpfile ? &dentry->d_name : NULL,
+                                 from_kuid(ns, current_fsuid()),
+                                 from_kgid(ns, current_fsgid()),
+                                 mode, rdev,
+                                 default_acl, acl) ?:
+               bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
+                               KEY_TYPE_QUOTA_PREALLOC);
        if (unlikely(ret))
+               goto err_before_quota;
+
+       ret   = bch2_trans_commit(&trans, NULL, &journal_seq,
+                                 BTREE_INSERT_NOUNLOCK);
+       if (unlikely(ret)) {
+               bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
+                               KEY_TYPE_QUOTA_WARN);
+err_before_quota:
+               if (ret == -EINTR)
+                       goto retry;
                goto err_trans;
+       }
 
        if (!tmpfile) {
                bch2_inode_update_after_write(c, dir, &dir_u,
                                              ATTR_MTIME|ATTR_CTIME);
-               journal_seq_copy(dir, inode->ei_journal_seq);
+               journal_seq_copy(c, dir, journal_seq);
                mutex_unlock(&dir->ei_update_lock);
        }
 
        bch2_vfs_inode_init(c, inode, &inode_u);
-       journal_seq_copy(inode, journal_seq);
+       journal_seq_copy(c, inode, journal_seq);
 
        set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
        set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
@@ -360,13 +322,17 @@ retry:
         * thread pulling the inode in and modifying it:
         */
 
-       old = to_bch_ei(insert_inode_locked2(&inode->v));
-       if (unlikely(old)) {
+       inode->v.i_state |= I_CREATING;
+       old = to_bch_ei(inode_insert5(&inode->v, inode->v.i_ino,
+                                     inum_test, NULL, &inode->v.i_ino));
+       BUG_ON(!old);
+
+       if (unlikely(old != inode)) {
                /*
                 * We raced, another process pulled the new inode into cache
                 * before us:
                 */
-               old->ei_journal_seq = inode->ei_journal_seq;
+               journal_seq_copy(c, old, journal_seq);
                make_bad_inode(&inode->v);
                iput(&inode->v);
 
@@ -380,7 +346,7 @@ retry:
        }
 
        bch2_trans_exit(&trans);
-out:
+err:
        posix_acl_release(default_acl);
        posix_acl_release(acl);
        return inode;
@@ -391,10 +357,8 @@ err_trans:
        bch2_trans_exit(&trans);
        make_bad_inode(&inode->v);
        iput(&inode->v);
-err:
-       bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1, KEY_TYPE_QUOTA_WARN);
        inode = ERR_PTR(ret);
-       goto out;
+       goto err;
 }
 
 /* methods */
@@ -404,11 +368,11 @@ static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
 {
        struct bch_fs *c = vdir->i_sb->s_fs_info;
        struct bch_inode_info *dir = to_bch_ei(vdir);
+       struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
        struct inode *vinode = NULL;
        u64 inum;
 
-       inum = bch2_dirent_lookup(c, dir->v.i_ino,
-                                 &dir->ei_str_hash,
+       inum = bch2_dirent_lookup(c, dir->v.i_ino, &hash,
                                  &dentry->d_name);
 
        if (inum)
@@ -417,11 +381,11 @@ static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
        return d_splice_alias(vinode, dentry);
 }
 
-static int bch2_create(struct inode *vdir, struct dentry *dentry,
-                      umode_t mode, bool excl)
+static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
+                     umode_t mode, dev_t rdev)
 {
        struct bch_inode_info *inode =
-               __bch2_create(to_bch_ei(vdir), dentry, mode|S_IFREG, 0, false);
+               __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
 
        if (IS_ERR(inode))
                return PTR_ERR(inode);
@@ -430,20 +394,10 @@ static int bch2_create(struct inode *vdir, struct dentry *dentry,
        return 0;
 }
 
-static int inode_update_for_link_fn(struct bch_inode_info *inode,
-                                   struct bch_inode_unpacked *bi,
-                                   void *p)
+static int bch2_create(struct inode *vdir, struct dentry *dentry,
+                      umode_t mode, bool excl)
 {
-       struct bch_fs *c = inode->v.i_sb->s_fs_info;
-
-       bi->bi_ctime = bch2_current_time(c);
-
-       if (bi->bi_flags & BCH_INODE_UNLINKED)
-               bi->bi_flags &= ~BCH_INODE_UNLINKED;
-       else
-               bi->bi_nlink++;
-
-       return 0;
+       return bch2_mknod(vdir, dentry, mode|S_IFREG, 0);
 }
 
 static int __bch2_link(struct bch_fs *c,
@@ -452,33 +406,27 @@ static int __bch2_link(struct bch_fs *c,
                       struct dentry *dentry)
 {
        struct btree_trans trans;
-       struct bch_inode_unpacked inode_u;
+       struct bch_inode_unpacked dir_u, inode_u;
        int ret;
 
        mutex_lock(&inode->ei_update_lock);
-       bch2_trans_init(&trans, c);
-retry:
-       bch2_trans_begin(&trans);
+       bch2_trans_init(&trans, c, 4, 1024);
 
-       ret   = __bch2_dirent_create(&trans, dir->v.i_ino,
-                                    &dir->ei_str_hash,
-                                    mode_to_type(inode->v.i_mode),
-                                    &dentry->d_name,
-                                    inode->v.i_ino,
-                                    BCH_HASH_SET_MUST_CREATE) ?:
-               bch2_write_inode_trans(&trans, inode, &inode_u,
-                                      inode_update_for_link_fn,
-                                      NULL) ?:
-               bch2_trans_commit(&trans, NULL,
-                                 &inode->ei_journal_seq,
-                                 BTREE_INSERT_ATOMIC|
-                                 BTREE_INSERT_NOUNLOCK);
+       ret = __bch2_trans_do(&trans, NULL, &inode->ei_journal_seq,
+                             BTREE_INSERT_NOUNLOCK,
+                       bch2_link_trans(&trans,
+                                       dir->v.i_ino,
+                                       inode->v.i_ino, &dir_u, &inode_u,
+                                       &dentry->d_name));
 
-       if (ret == -EINTR)
-               goto retry;
+       if (likely(!ret)) {
+               BUG_ON(inode_u.bi_inum != inode->v.i_ino);
 
-       if (likely(!ret))
+               journal_seq_copy(c, inode, dir->ei_journal_seq);
+               bch2_inode_update_after_write(c, dir, &dir_u,
+                                             ATTR_MTIME|ATTR_CTIME);
                bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
+       }
 
        bch2_trans_exit(&trans);
        mutex_unlock(&inode->ei_update_lock);
@@ -504,35 +452,6 @@ static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
        return 0;
 }
 
-static int inode_update_dir_for_unlink_fn(struct bch_inode_info *inode,
-                                         struct bch_inode_unpacked *bi,
-                                         void *p)
-{
-       struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct bch_inode_info *unlink_inode = p;
-
-       bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
-
-       bi->bi_nlink -= S_ISDIR(unlink_inode->v.i_mode);
-
-       return 0;
-}
-
-static int inode_update_for_unlink_fn(struct bch_inode_info *inode,
-                                     struct bch_inode_unpacked *bi,
-                                     void *p)
-{
-       struct bch_fs *c = inode->v.i_sb->s_fs_info;
-
-       bi->bi_ctime = bch2_current_time(c);
-       if (bi->bi_nlink)
-               bi->bi_nlink--;
-       else
-               bi->bi_flags |= BCH_INODE_UNLINKED;
-
-       return 0;
-}
-
 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
 {
        struct bch_fs *c = vdir->i_sb->s_fs_info;
@@ -542,40 +461,28 @@ static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
        struct btree_trans trans;
        int ret;
 
-       bch2_lock_inodes(dir, inode);
-       bch2_trans_init(&trans, c);
-retry:
-       bch2_trans_begin(&trans);
+       bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
+       bch2_trans_init(&trans, c, 4, 1024);
 
-       ret   = __bch2_dirent_delete(&trans, dir->v.i_ino,
-                                    &dir->ei_str_hash,
-                                    &dentry->d_name) ?:
-               bch2_write_inode_trans(&trans, dir, &dir_u,
-                                      inode_update_dir_for_unlink_fn,
-                                      inode) ?:
-               bch2_write_inode_trans(&trans, inode, &inode_u,
-                                      inode_update_for_unlink_fn,
-                                      NULL) ?:
-               bch2_trans_commit(&trans, NULL,
-                                 &dir->ei_journal_seq,
-                                 BTREE_INSERT_ATOMIC|
-                                 BTREE_INSERT_NOUNLOCK|
-                                 BTREE_INSERT_NOFAIL);
-       if (ret == -EINTR)
-               goto retry;
-       if (ret)
-               goto err;
+       ret = __bch2_trans_do(&trans, NULL, &dir->ei_journal_seq,
+                             BTREE_INSERT_NOUNLOCK|
+                             BTREE_INSERT_NOFAIL,
+                       bch2_unlink_trans(&trans,
+                                         dir->v.i_ino, &dir_u,
+                                         &inode_u, &dentry->d_name));
 
-       if (dir->ei_journal_seq > inode->ei_journal_seq)
-               inode->ei_journal_seq = dir->ei_journal_seq;
+       if (likely(!ret)) {
+               BUG_ON(inode_u.bi_inum != inode->v.i_ino);
+
+               journal_seq_copy(c, inode, dir->ei_journal_seq);
+               bch2_inode_update_after_write(c, dir, &dir_u,
+                                             ATTR_MTIME|ATTR_CTIME);
+               bch2_inode_update_after_write(c, inode, &inode_u,
+                                             ATTR_MTIME);
+       }
 
-       bch2_inode_update_after_write(c, dir, &dir_u,
-                                     ATTR_MTIME|ATTR_CTIME);
-       bch2_inode_update_after_write(c, inode, &inode_u,
-                                     ATTR_MTIME);
-err:
        bch2_trans_exit(&trans);
-       bch2_unlock_inodes(dir, inode);
+       bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
 
        return ret;
 }
@@ -602,7 +509,7 @@ static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
        if (unlikely(ret))
                goto err;
 
-       journal_seq_copy(dir, inode->ei_journal_seq);
+       journal_seq_copy(c, dir, inode->ei_journal_seq);
 
        ret = __bch2_link(c, inode, dir, dentry);
        if (unlikely(ret))
@@ -617,84 +524,7 @@ err:
 
 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
 {
-       struct bch_inode_info *inode =
-               __bch2_create(to_bch_ei(vdir), dentry, mode|S_IFDIR, 0, false);
-
-       if (IS_ERR(inode))
-               return PTR_ERR(inode);
-
-       d_instantiate(dentry, &inode->v);
-       return 0;
-}
-
-static int bch2_rmdir(struct inode *vdir, struct dentry *dentry)
-{
-       struct bch_fs *c = vdir->i_sb->s_fs_info;
-
-       if (bch2_empty_dir(c, dentry->d_inode->i_ino))
-               return -ENOTEMPTY;
-
-       return bch2_unlink(vdir, dentry);
-}
-
-static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
-                     umode_t mode, dev_t rdev)
-{
-       struct bch_inode_info *inode =
-               __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
-
-       if (IS_ERR(inode))
-               return PTR_ERR(inode);
-
-       d_instantiate(dentry, &inode->v);
-       return 0;
-}
-
-struct rename_info {
-       u64                     now;
-       struct bch_inode_info   *src_dir;
-       struct bch_inode_info   *dst_dir;
-       struct bch_inode_info   *src_inode;
-       struct bch_inode_info   *dst_inode;
-       enum bch_rename_mode    mode;
-};
-
-static int inode_update_for_rename_fn(struct bch_inode_info *inode,
-                                     struct bch_inode_unpacked *bi,
-                                     void *p)
-{
-       struct rename_info *info = p;
-
-       if (inode == info->src_dir) {
-               bi->bi_nlink -= S_ISDIR(info->src_inode->v.i_mode);
-               bi->bi_nlink += info->dst_inode &&
-                       S_ISDIR(info->dst_inode->v.i_mode) &&
-                       info->mode == BCH_RENAME_EXCHANGE;
-       }
-
-       if (inode == info->dst_dir) {
-               bi->bi_nlink += S_ISDIR(info->src_inode->v.i_mode);
-               bi->bi_nlink -= info->dst_inode &&
-                       S_ISDIR(info->dst_inode->v.i_mode);
-       }
-
-       if (inode == info->dst_inode &&
-           info->mode == BCH_RENAME_OVERWRITE) {
-               BUG_ON(bi->bi_nlink &&
-                      S_ISDIR(info->dst_inode->v.i_mode));
-
-               if (bi->bi_nlink)
-                       bi->bi_nlink--;
-               else
-                       bi->bi_flags |= BCH_INODE_UNLINKED;
-       }
-
-       if (inode == info->src_dir ||
-           inode == info->dst_dir)
-               bi->bi_mtime = info->now;
-       bi->bi_ctime = info->now;
-
-       return 0;
+       return bch2_mknod(vdir, dentry, mode|S_IFDIR, 0);
 }
 
 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
@@ -702,116 +532,126 @@ static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
                        unsigned flags)
 {
        struct bch_fs *c = src_vdir->i_sb->s_fs_info;
-       struct rename_info i = {
-               .src_dir        = to_bch_ei(src_vdir),
-               .dst_dir        = to_bch_ei(dst_vdir),
-               .src_inode      = to_bch_ei(src_dentry->d_inode),
-               .dst_inode      = to_bch_ei(dst_dentry->d_inode),
-               .mode           = flags & RENAME_EXCHANGE
-                               ? BCH_RENAME_EXCHANGE
-                       : dst_dentry->d_inode
-                               ? BCH_RENAME_OVERWRITE : BCH_RENAME,
-       };
-       struct btree_trans trans;
+       struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
+       struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
+       struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
+       struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
        struct bch_inode_unpacked dst_dir_u, src_dir_u;
        struct bch_inode_unpacked src_inode_u, dst_inode_u;
+       struct btree_trans trans;
+       enum bch_rename_mode mode = flags & RENAME_EXCHANGE
+               ? BCH_RENAME_EXCHANGE
+               : dst_dentry->d_inode
+               ? BCH_RENAME_OVERWRITE : BCH_RENAME;
        u64 journal_seq = 0;
        int ret;
 
        if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
                return -EINVAL;
 
-       if (i.mode == BCH_RENAME_OVERWRITE) {
-               if (S_ISDIR(i.src_inode->v.i_mode) !=
-                   S_ISDIR(i.dst_inode->v.i_mode))
-                       return -ENOTDIR;
-
-               if (S_ISDIR(i.src_inode->v.i_mode) &&
-                   bch2_empty_dir(c, i.dst_inode->v.i_ino))
-                       return -ENOTEMPTY;
-
-               ret = filemap_write_and_wait_range(i.src_inode->v.i_mapping,
+       if (mode == BCH_RENAME_OVERWRITE) {
+               ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
                                                   0, LLONG_MAX);
                if (ret)
                        return ret;
        }
 
-       bch2_lock_inodes(i.src_dir,
-                        i.dst_dir,
-                        i.src_inode,
-                        i.dst_inode);
+       bch2_trans_init(&trans, c, 8, 2048);
 
-       bch2_trans_init(&trans, c);
-retry:
-       bch2_trans_begin(&trans);
-       i.now = bch2_current_time(c);
-
-       ret   = bch2_dirent_rename(&trans,
-                                  i.src_dir, &src_dentry->d_name,
-                                  i.dst_dir, &dst_dentry->d_name,
-                                  i.mode) ?:
-               bch2_write_inode_trans(&trans, i.src_dir, &src_dir_u,
-                                      inode_update_for_rename_fn, &i) ?:
-               (i.src_dir != i.dst_dir
-                ? bch2_write_inode_trans(&trans, i.dst_dir, &dst_dir_u,
-                                      inode_update_for_rename_fn, &i)
-                : 0 ) ?:
-               bch2_write_inode_trans(&trans, i.src_inode, &src_inode_u,
-                                      inode_update_for_rename_fn, &i) ?:
-               (i.dst_inode
-                ? bch2_write_inode_trans(&trans, i.dst_inode, &dst_inode_u,
-                                      inode_update_for_rename_fn, &i)
-                : 0 ) ?:
-               bch2_trans_commit(&trans, NULL,
-                                 &journal_seq,
-                                 BTREE_INSERT_ATOMIC|
-                                 BTREE_INSERT_NOUNLOCK);
-       if (ret == -EINTR)
-               goto retry;
+       bch2_lock_inodes(INODE_UPDATE_LOCK,
+                        src_dir,
+                        dst_dir,
+                        src_inode,
+                        dst_inode);
+
+       if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
+               ret = bch2_fs_quota_transfer(c, src_inode,
+                                            dst_dir->ei_qid,
+                                            1 << QTYP_PRJ,
+                                            KEY_TYPE_QUOTA_PREALLOC);
+               if (ret)
+                       goto err;
+       }
+
+       if (mode == BCH_RENAME_EXCHANGE &&
+           inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
+               ret = bch2_fs_quota_transfer(c, dst_inode,
+                                            src_dir->ei_qid,
+                                            1 << QTYP_PRJ,
+                                            KEY_TYPE_QUOTA_PREALLOC);
+               if (ret)
+                       goto err;
+       }
+
+       ret = __bch2_trans_do(&trans, NULL, &journal_seq,
+                             BTREE_INSERT_NOUNLOCK,
+                       bch2_rename_trans(&trans,
+                                         src_dir->v.i_ino, &src_dir_u,
+                                         dst_dir->v.i_ino, &dst_dir_u,
+                                         &src_inode_u,
+                                         &dst_inode_u,
+                                         &src_dentry->d_name,
+                                         &dst_dentry->d_name,
+                                         mode));
        if (unlikely(ret))
                goto err;
 
-       bch2_inode_update_after_write(c, i.src_dir, &src_dir_u,
+       BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
+       BUG_ON(dst_inode &&
+              dst_inode->v.i_ino != dst_inode_u.bi_inum);
+
+       bch2_inode_update_after_write(c, src_dir, &src_dir_u,
                                      ATTR_MTIME|ATTR_CTIME);
-       journal_seq_copy(i.src_dir, journal_seq);
+       journal_seq_copy(c, src_dir, journal_seq);
 
-       if (i.src_dir != i.dst_dir) {
-               bch2_inode_update_after_write(c, i.dst_dir, &dst_dir_u,
+       if (src_dir != dst_dir) {
+               bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
                                              ATTR_MTIME|ATTR_CTIME);
-               journal_seq_copy(i.dst_dir, journal_seq);
+               journal_seq_copy(c, dst_dir, journal_seq);
        }
 
-       journal_seq_copy(i.src_inode, journal_seq);
-       if (i.dst_inode)
-               journal_seq_copy(i.dst_inode, journal_seq);
-
-       bch2_inode_update_after_write(c, i.src_inode, &src_inode_u,
+       bch2_inode_update_after_write(c, src_inode, &src_inode_u,
                                      ATTR_CTIME);
-       if (i.dst_inode)
-               bch2_inode_update_after_write(c, i.dst_inode, &dst_inode_u,
+       journal_seq_copy(c, src_inode, journal_seq);
+
+       if (dst_inode) {
+               bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
                                              ATTR_CTIME);
+               journal_seq_copy(c, dst_inode, journal_seq);
+       }
 err:
        bch2_trans_exit(&trans);
-       bch2_unlock_inodes(i.src_dir,
-                          i.dst_dir,
-                          i.src_inode,
-                          i.dst_inode);
+
+       bch2_fs_quota_transfer(c, src_inode,
+                              bch_qid(&src_inode->ei_inode),
+                              1 << QTYP_PRJ,
+                              KEY_TYPE_QUOTA_NOCHECK);
+       if (dst_inode)
+               bch2_fs_quota_transfer(c, dst_inode,
+                                      bch_qid(&dst_inode->ei_inode),
+                                      1 << QTYP_PRJ,
+                                      KEY_TYPE_QUOTA_NOCHECK);
+
+       bch2_unlock_inodes(INODE_UPDATE_LOCK,
+                          src_dir,
+                          dst_dir,
+                          src_inode,
+                          dst_inode);
 
        return ret;
 }
 
-static int inode_update_for_setattr_fn(struct bch_inode_info *inode,
-                                      struct bch_inode_unpacked *bi,
-                                      void *p)
+void bch2_setattr_copy(struct bch_inode_info *inode,
+                      struct bch_inode_unpacked *bi,
+                      struct iattr *attr)
 {
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct iattr *attr = p;
        unsigned int ia_valid = attr->ia_valid;
 
        if (ia_valid & ATTR_UID)
-               bi->bi_uid = from_kuid(inode->v.i_sb->s_user_ns, attr->ia_uid);
+               bi->bi_uid = from_kuid(c->vfs_sb->s_user_ns, attr->ia_uid);
        if (ia_valid & ATTR_GID)
-               bi->bi_gid = from_kgid(inode->v.i_sb->s_user_ns, attr->ia_gid);
+               bi->bi_gid = from_kgid(c->vfs_sb->s_user_ns, attr->ia_gid);
 
        if (ia_valid & ATTR_ATIME)
                bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
@@ -831,66 +671,68 @@ static int inode_update_for_setattr_fn(struct bch_inode_info *inode,
                        mode &= ~S_ISGID;
                bi->bi_mode = mode;
        }
-
-       return 0;
 }
 
-static int bch2_setattr_nonsize(struct bch_inode_info *inode, struct iattr *iattr)
+static int bch2_setattr_nonsize(struct bch_inode_info *inode,
+                               struct iattr *attr)
 {
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct bch_qid qid = inode->ei_qid;
+       struct bch_qid qid;
        struct btree_trans trans;
+       struct btree_iter *inode_iter;
        struct bch_inode_unpacked inode_u;
        struct posix_acl *acl = NULL;
-       unsigned qtypes = 0;
        int ret;
 
        mutex_lock(&inode->ei_update_lock);
 
-       if (c->opts.usrquota &&
-           (iattr->ia_valid & ATTR_UID) &&
-           !uid_eq(iattr->ia_uid, inode->v.i_uid)) {
-               qid.q[QTYP_USR] = from_kuid(&init_user_ns, iattr->ia_uid),
-               qtypes |= 1 << QTYP_USR;
-       }
+       qid = inode->ei_qid;
 
-       if (c->opts.grpquota &&
-           (iattr->ia_valid & ATTR_GID) &&
-           !gid_eq(iattr->ia_gid, inode->v.i_gid)) {
-               qid.q[QTYP_GRP] = from_kgid(&init_user_ns, iattr->ia_gid);
-               qtypes |= 1 << QTYP_GRP;
-       }
+       if (attr->ia_valid & ATTR_UID)
+               qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
 
-       if (qtypes) {
-               ret = bch2_quota_transfer(c, qtypes, qid, inode->ei_qid,
-                                         inode->v.i_blocks +
-                                         inode->ei_quota_reserved);
-               if (ret)
-                       goto err;
-       }
+       if (attr->ia_valid & ATTR_GID)
+               qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
+
+       ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
+                                    KEY_TYPE_QUOTA_PREALLOC);
+       if (ret)
+               goto err;
 
-       bch2_trans_init(&trans, c);
+       bch2_trans_init(&trans, c, 0, 0);
 retry:
        bch2_trans_begin(&trans);
        kfree(acl);
        acl = NULL;
 
-       ret = bch2_write_inode_trans(&trans, inode, &inode_u,
-                               inode_update_for_setattr_fn, iattr) ?:
-               (iattr->ia_valid & ATTR_MODE
-                ? bch2_acl_chmod(&trans, inode, iattr->ia_mode, &acl)
-                : 0) ?:
+       inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
+                                    BTREE_ITER_INTENT);
+       ret = PTR_ERR_OR_ZERO(inode_iter);
+       if (ret)
+               goto btree_err;
+
+       bch2_setattr_copy(inode, &inode_u, attr);
+
+       if (attr->ia_valid & ATTR_MODE) {
+               ret = bch2_acl_chmod(&trans, &inode_u, inode_u.bi_mode, &acl);
+               if (ret)
+                       goto btree_err;
+       }
+
+       ret =   bch2_inode_write(&trans, inode_iter, &inode_u) ?:
                bch2_trans_commit(&trans, NULL,
                                  &inode->ei_journal_seq,
-                                 BTREE_INSERT_ATOMIC|
                                  BTREE_INSERT_NOUNLOCK|
                                  BTREE_INSERT_NOFAIL);
+btree_err:
+       bch2_trans_iter_put(&trans, inode_iter);
+
        if (ret == -EINTR)
                goto retry;
        if (unlikely(ret))
                goto err_trans;
 
-       bch2_inode_update_after_write(c, inode, &inode_u, iattr->ia_valid);
+       bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
 
        if (acl)
                set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
@@ -929,10 +771,15 @@ static int bch2_getattr(const struct path *path, struct kstat *stat,
 
        if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
                stat->attributes |= STATX_ATTR_IMMUTABLE;
+       stat->attributes_mask    |= STATX_ATTR_IMMUTABLE;
+
        if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
                stat->attributes |= STATX_ATTR_APPEND;
+       stat->attributes_mask    |= STATX_ATTR_APPEND;
+
        if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
                stat->attributes |= STATX_ATTR_NODUMP;
+       stat->attributes_mask    |= STATX_ATTR_NODUMP;
 
        return 0;
 }
@@ -966,16 +813,20 @@ static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
        return 0;
 }
 
-static int bch2_fill_extent(struct fiemap_extent_info *info,
-                           const struct bkey_i *k, unsigned flags)
+static int bch2_fill_extent(struct bch_fs *c,
+                           struct fiemap_extent_info *info,
+                           struct bkey_s_c k, unsigned flags)
 {
-       if (bkey_extent_is_data(&k->k)) {
-               struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
+       if (bkey_extent_is_direct_data(k.k)) {
+               struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
                const union bch_extent_entry *entry;
                struct extent_ptr_decoded p;
                int ret;
 
-               extent_for_each_ptr_decode(e, p, entry) {
+               if (k.k->type == KEY_TYPE_reflink_v)
+                       flags |= FIEMAP_EXTENT_SHARED;
+
+               bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
                        int flags2 = 0;
                        u64 offset = p.ptr.offset;
 
@@ -984,23 +835,29 @@ static int bch2_fill_extent(struct fiemap_extent_info *info,
                        else
                                offset += p.crc.offset;
 
-                       if ((offset & (PAGE_SECTORS - 1)) ||
-                           (e.k->size & (PAGE_SECTORS - 1)))
+                       if ((offset & (c->opts.block_size - 1)) ||
+                           (k.k->size & (c->opts.block_size - 1)))
                                flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
 
                        ret = fiemap_fill_next_extent(info,
-                                               bkey_start_offset(e.k) << 9,
+                                               bkey_start_offset(k.k) << 9,
                                                offset << 9,
-                                               e.k->size << 9, flags|flags2);
+                                               k.k->size << 9, flags|flags2);
                        if (ret)
                                return ret;
                }
 
                return 0;
-       } else if (k->k.type == KEY_TYPE_reservation) {
+       } else if (bkey_extent_is_inline_data(k.k)) {
                return fiemap_fill_next_extent(info,
-                                              bkey_start_offset(&k->k) << 9,
-                                              0, k->k.size << 9,
+                                              bkey_start_offset(k.k) << 9,
+                                              0, k.k->size << 9,
+                                              flags|
+                                              FIEMAP_EXTENT_DATA_INLINE);
+       } else if (k.k->type == KEY_TYPE_reservation) {
+               return fiemap_fill_next_extent(info,
+                                              bkey_start_offset(k.k) << 9,
+                                              0, k.k->size << 9,
                                               flags|
                                               FIEMAP_EXTENT_DELALLOC|
                                               FIEMAP_EXTENT_UNWRITTEN);
@@ -1014,42 +871,94 @@ static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
 {
        struct bch_fs *c = vinode->i_sb->s_fs_info;
        struct bch_inode_info *ei = to_bch_ei(vinode);
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
        struct bkey_s_c k;
-       BKEY_PADDED(k) tmp;
+       struct bkey_buf cur, prev;
+       struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
+       unsigned offset_into_extent, sectors;
        bool have_extent = false;
        int ret = 0;
 
+       ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
+       if (ret)
+               return ret;
+
        if (start + len < start)
                return -EINVAL;
 
-       for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
-                          POS(ei->v.i_ino, start >> 9), 0, k)
-               if (bkey_extent_is_data(k.k) ||
-                   k.k->type == KEY_TYPE_reservation) {
-                       if (bkey_cmp(bkey_start_pos(k.k),
-                                    POS(ei->v.i_ino, (start + len) >> 9)) >= 0)
-                               break;
+       bch2_bkey_buf_init(&cur);
+       bch2_bkey_buf_init(&prev);
+       bch2_trans_init(&trans, c, 0, 0);
 
-                       if (have_extent) {
-                               ret = bch2_fill_extent(info, &tmp.k, 0);
-                               if (ret)
-                                       goto out;
-                       }
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
+                                  POS(ei->v.i_ino, start >> 9), 0);
+retry:
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              !(ret = bkey_err(k)) &&
+              bkey_cmp(iter->pos, end) < 0) {
+               enum btree_id data_btree = BTREE_ID_extents;
+
+               if (!bkey_extent_is_data(k.k) &&
+                   k.k->type != KEY_TYPE_reservation) {
+                       bch2_btree_iter_next(iter);
+                       continue;
+               }
+
+               offset_into_extent      = iter->pos.offset -
+                       bkey_start_offset(k.k);
+               sectors                 = k.k->size - offset_into_extent;
+
+               bch2_bkey_buf_reassemble(&cur, c, k);
+
+               ret = bch2_read_indirect_extent(&trans, &data_btree,
+                                       &offset_into_extent, &cur);
+               if (ret)
+                       break;
+
+               k = bkey_i_to_s_c(cur.k);
+               bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
 
-                       bkey_reassemble(&tmp.k, k);
-                       have_extent = true;
+               sectors = min(sectors, k.k->size - offset_into_extent);
+
+               bch2_cut_front(POS(k.k->p.inode,
+                                  bkey_start_offset(k.k) +
+                                  offset_into_extent),
+                              cur.k);
+               bch2_key_resize(&cur.k->k, sectors);
+               cur.k->k.p = iter->pos;
+               cur.k->k.p.offset += cur.k->k.size;
+
+               if (have_extent) {
+                       ret = bch2_fill_extent(c, info,
+                                       bkey_i_to_s_c(prev.k), 0);
+                       if (ret)
+                               break;
                }
 
-       if (have_extent)
-               ret = bch2_fill_extent(info, &tmp.k, FIEMAP_EXTENT_LAST);
-out:
-       bch2_btree_iter_unlock(&iter);
+               bkey_copy(prev.k, cur.k);
+               have_extent = true;
+
+               bch2_btree_iter_set_pos(iter,
+                       POS(iter->pos.inode, iter->pos.offset + sectors));
+       }
+
+       if (ret == -EINTR)
+               goto retry;
+
+       if (!ret && have_extent)
+               ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
+                                      FIEMAP_EXTENT_LAST);
+
+       bch2_trans_iter_put(&trans, iter);
+       ret = bch2_trans_exit(&trans) ?: ret;
+       bch2_bkey_buf_exit(&cur, c);
+       bch2_bkey_buf_exit(&prev, c);
        return ret < 0 ? ret : 0;
 }
 
 static const struct vm_operations_struct bch_vm_ops = {
-       .fault          = filemap_fault,
+       .fault          = bch2_page_fault,
        .map_pages      = filemap_map_pages,
        .page_mkwrite   = bch2_page_mkwrite,
 };
@@ -1072,25 +981,33 @@ static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
 
 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
 {
-       struct bch_fs *c = file_inode(file)->i_sb->s_fs_info;
+       struct bch_inode_info *inode = file_bch_inode(file);
+       struct bch_fs *c = inode->v.i_sb->s_fs_info;
+
+       if (!dir_emit_dots(file, ctx))
+               return 0;
 
-       return bch2_readdir(c, file, ctx);
+       return bch2_readdir(c, inode->v.i_ino, ctx);
 }
 
 static const struct file_operations bch_file_operations = {
        .llseek         = bch2_llseek,
-       .read_iter      = generic_file_read_iter,
+       .read_iter      = bch2_read_iter,
        .write_iter     = bch2_write_iter,
        .mmap           = bch2_mmap,
        .open           = generic_file_open,
        .fsync          = bch2_fsync,
        .splice_read    = generic_file_splice_read,
+#if 0
+       /* Busted: */
        .splice_write   = iter_file_splice_write,
+#endif
        .fallocate      = bch2_fallocate_dispatch,
        .unlocked_ioctl = bch2_fs_file_ioctl,
 #ifdef CONFIG_COMPAT
        .compat_ioctl   = bch2_compat_fs_ioctl,
 #endif
+       .remap_file_range = bch2_remap_file_range,
 };
 
 static const struct inode_operations bch_file_inode_operations = {
@@ -1111,7 +1028,7 @@ static const struct inode_operations bch_dir_inode_operations = {
        .unlink         = bch2_unlink,
        .symlink        = bch2_symlink,
        .mkdir          = bch2_mkdir,
-       .rmdir          = bch2_rmdir,
+       .rmdir          = bch2_unlink,
        .mknod          = bch2_mknod,
        .rename         = bch2_rename2,
        .getattr        = bch2_getattr,
@@ -1127,7 +1044,7 @@ static const struct inode_operations bch_dir_inode_operations = {
 static const struct file_operations bch_dir_file_operations = {
        .llseek         = bch2_dir_llseek,
        .read           = generic_read_dir,
-       .iterate        = bch2_vfs_readdir,
+       .iterate_shared = bch2_vfs_readdir,
        .fsync          = bch2_fsync,
        .unlocked_ioctl = bch2_fs_file_ioctl,
 #ifdef CONFIG_COMPAT
@@ -1160,13 +1077,13 @@ static const struct address_space_operations bch_address_space_operations = {
        .writepage      = bch2_writepage,
        .readpage       = bch2_readpage,
        .writepages     = bch2_writepages,
-       .readpages      = bch2_readpages,
-       .set_page_dirty = bch2_set_page_dirty,
+       .readahead      = bch2_readahead,
+       .set_page_dirty = __set_page_dirty_nobuffers,
        .write_begin    = bch2_write_begin,
        .write_end      = bch2_write_end,
        .invalidatepage = bch2_invalidatepage,
        .releasepage    = bch2_releasepage,
-       .direct_IO      = bch2_direct_IO,
+       .direct_IO      = noop_direct_IO,
 #ifdef CONFIG_MIGRATION
        .migratepage    = bch2_migrate_page,
 #endif
@@ -1225,9 +1142,10 @@ static void bch2_vfs_inode_init(struct bch_fs *c,
        inode->v.i_generation   = bi->bi_generation;
        inode->v.i_size         = bi->bi_size;
 
+       inode->ei_flags         = 0;
        inode->ei_journal_seq   = 0;
        inode->ei_quota_reserved = 0;
-       inode->ei_str_hash      = bch2_hash_info_init(c, bi);
+       inode->ei_qid           = bch_qid(bi);
 
        inode->v.i_mapping->a_ops = &bch_address_space_operations;
 
@@ -1261,6 +1179,7 @@ static struct inode *bch2_alloc_inode(struct super_block *sb)
 
        inode_init_once(&inode->v);
        mutex_init(&inode->ei_update_lock);
+       pagecache_lock_init(&inode->ei_pagecache_lock);
        mutex_init(&inode->ei_quota_lock);
        inode->ei_journal_seq = 0;
 
@@ -1305,12 +1224,6 @@ static int bch2_vfs_write_inode(struct inode *vinode,
                               ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
        mutex_unlock(&inode->ei_update_lock);
 
-       if (c->opts.journal_flush_disabled)
-               return ret;
-
-       if (!ret && wbc->sync_mode == WB_SYNC_ALL)
-               ret = bch2_journal_flush_seq(&c->journal, inode->ei_journal_seq);
-
        return ret;
 }
 
@@ -1330,7 +1243,7 @@ static void bch2_evict_inode(struct inode *vinode)
                                KEY_TYPE_QUOTA_WARN);
                bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
                                KEY_TYPE_QUOTA_WARN);
-               bch2_inode_rm(c, inode->v.i_ino);
+               bch2_inode_rm(c, inode->v.i_ino, true);
        }
 }
 
@@ -1340,6 +1253,11 @@ static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
        struct bch_fs *c = sb->s_fs_info;
        struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
        unsigned shift = sb->s_blocksize_bits - 9;
+       /*
+        * this assumes inodes take up 64 bytes, which is a decent average
+        * number:
+        */
+       u64 avail_inodes = ((usage.capacity - usage.used) << 3);
        u64 fsid;
 
        buf->f_type     = BCACHEFS_STATFS_MAGIC;
@@ -1347,8 +1265,9 @@ static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
        buf->f_blocks   = usage.capacity >> shift;
        buf->f_bfree    = (usage.capacity - usage.used) >> shift;
        buf->f_bavail   = buf->f_bfree;
-       buf->f_files    = usage.nr_inodes;
-       buf->f_ffree    = U64_MAX;
+
+       buf->f_files    = usage.nr_inodes + avail_inodes;
+       buf->f_ffree    = avail_inodes;
 
        fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
               le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
@@ -1363,6 +1282,9 @@ static int bch2_sync_fs(struct super_block *sb, int wait)
 {
        struct bch_fs *c = sb->s_fs_info;
 
+       if (c->opts.journal_flush_disabled)
+               return 0;
+
        if (!wait) {
                bch2_journal_flush_async(&c->journal, NULL);
                return 0;
@@ -1381,91 +1303,36 @@ static struct bch_fs *bch2_path_to_fs(const char *dev)
 
        c = bch2_bdev_to_fs(bdev);
        bdput(bdev);
-       return c ?: ERR_PTR(-ENOENT);
-}
-
-static struct bch_fs *__bch2_open_as_blockdevs(const char *dev_name, char * const *devs,
-                                              unsigned nr_devs, struct bch_opts opts)
-{
-       struct bch_fs *c, *c1, *c2;
-       size_t i;
-
-       if (!nr_devs)
-               return ERR_PTR(-EINVAL);
-
-       c = bch2_fs_open(devs, nr_devs, opts);
-
-       if (IS_ERR(c) && PTR_ERR(c) == -EBUSY) {
-               /*
-                * Already open?
-                * Look up each block device, make sure they all belong to a
-                * filesystem and they all belong to the _same_ filesystem
-                */
-
-               c1 = bch2_path_to_fs(devs[0]);
-               if (!c1)
-                       return c;
-
-               for (i = 1; i < nr_devs; i++) {
-                       c2 = bch2_path_to_fs(devs[i]);
-                       if (!IS_ERR(c2))
-                               closure_put(&c2->cl);
-
-                       if (c1 != c2) {
-                               closure_put(&c1->cl);
-                               return c;
-                       }
-               }
-
-               c = c1;
-       }
-
-       if (IS_ERR(c))
-               return c;
-
-       mutex_lock(&c->state_lock);
-
-       if (!bch2_fs_running(c)) {
-               mutex_unlock(&c->state_lock);
+       if (c)
                closure_put(&c->cl);
-               pr_err("err mounting %s: incomplete filesystem", dev_name);
-               return ERR_PTR(-EINVAL);
-       }
-
-       mutex_unlock(&c->state_lock);
-
-       set_bit(BCH_FS_BDEV_MOUNTED, &c->flags);
-       return c;
+       return c ?: ERR_PTR(-ENOENT);
 }
 
-static struct bch_fs *bch2_open_as_blockdevs(const char *_dev_name,
-                                            struct bch_opts opts)
+static char **split_devs(const char *_dev_name, unsigned *nr)
 {
        char *dev_name = NULL, **devs = NULL, *s;
-       struct bch_fs *c = ERR_PTR(-ENOMEM);
        size_t i, nr_devs = 0;
 
        dev_name = kstrdup(_dev_name, GFP_KERNEL);
        if (!dev_name)
-               goto err;
+               return NULL;
 
        for (s = dev_name; s; s = strchr(s + 1, ':'))
                nr_devs++;
 
-       devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
-       if (!devs)
-               goto err;
+       devs = kcalloc(nr_devs + 1, sizeof(const char *), GFP_KERNEL);
+       if (!devs) {
+               kfree(dev_name);
+               return NULL;
+       }
 
        for (i = 0, s = dev_name;
             s;
             (s = strchr(s, ':')) && (*s++ = '\0'))
                devs[i++] = s;
 
-       c = __bch2_open_as_blockdevs(_dev_name, devs, nr_devs, opts);
-err:
-       kfree(devs);
-       kfree(dev_name);
-       return c;
+       *nr = nr_devs;
+       return devs;
 }
 
 static int bch2_remount(struct super_block *sb, int *flags, char *data)
@@ -1474,34 +1341,33 @@ static int bch2_remount(struct super_block *sb, int *flags, char *data)
        struct bch_opts opts = bch2_opts_empty();
        int ret;
 
-       opt_set(opts, read_only, (*flags & MS_RDONLY) != 0);
+       opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
 
-       ret = bch2_parse_mount_opts(&opts, data);
+       ret = bch2_parse_mount_opts(c, &opts, data);
        if (ret)
                return ret;
 
        if (opts.read_only != c->opts.read_only) {
-               const char *err = NULL;
-
-               mutex_lock(&c->state_lock);
+               down_write(&c->state_lock);
 
                if (opts.read_only) {
                        bch2_fs_read_only(c);
 
-                       sb->s_flags |= MS_RDONLY;
+                       sb->s_flags |= SB_RDONLY;
                } else {
-                       err = bch2_fs_read_write(c);
-                       if (err) {
-                               bch_err(c, "error going rw: %s", err);
+                       ret = bch2_fs_read_write(c);
+                       if (ret) {
+                               bch_err(c, "error going rw: %i", ret);
+                               up_write(&c->state_lock);
                                return -EINVAL;
                        }
 
-                       sb->s_flags &= ~MS_RDONLY;
+                       sb->s_flags &= ~SB_RDONLY;
                }
 
                c->opts.read_only = opts.read_only;
 
-               mutex_unlock(&c->state_lock);
+               up_write(&c->state_lock);
        }
 
        if (opts.errors >= 0)
@@ -1510,6 +1376,24 @@ static int bch2_remount(struct super_block *sb, int *flags, char *data)
        return ret;
 }
 
+static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
+{
+       struct bch_fs *c = root->d_sb->s_fs_info;
+       struct bch_dev *ca;
+       unsigned i;
+       bool first = true;
+
+       for_each_online_member(ca, c, i) {
+               if (!first)
+                       seq_putc(seq, ':');
+               first = false;
+               seq_puts(seq, "/dev/");
+               seq_puts(seq, ca->name);
+       }
+
+       return 0;
+}
+
 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
 {
        struct bch_fs *c = root->d_sb->s_fs_info;
@@ -1520,7 +1404,7 @@ static int bch2_show_options(struct seq_file *seq, struct dentry *root)
                const struct bch_option *opt = &bch2_opt_table[i];
                u64 v = bch2_opt_get_by_id(&c->opts, i);
 
-               if (opt->mode < OPT_MOUNT)
+               if (!(opt->mode & OPT_MOUNT))
                        continue;
 
                if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
@@ -1533,7 +1417,13 @@ static int bch2_show_options(struct seq_file *seq, struct dentry *root)
        }
 
        return 0;
+}
+
+static void bch2_put_super(struct super_block *sb)
+{
+       struct bch_fs *c = sb->s_fs_info;
 
+       __bch2_fs_stop(c);
 }
 
 static const struct super_operations bch_super_operations = {
@@ -1543,26 +1433,42 @@ static const struct super_operations bch_super_operations = {
        .evict_inode    = bch2_evict_inode,
        .sync_fs        = bch2_sync_fs,
        .statfs         = bch2_statfs,
+       .show_devname   = bch2_show_devname,
        .show_options   = bch2_show_options,
        .remount_fs     = bch2_remount,
-#if 0
        .put_super      = bch2_put_super,
+#if 0
        .freeze_fs      = bch2_freeze,
        .unfreeze_fs    = bch2_unfreeze,
 #endif
 };
 
-static int bch2_test_super(struct super_block *s, void *data)
-{
-       return s->s_fs_info == data;
-}
-
 static int bch2_set_super(struct super_block *s, void *data)
 {
        s->s_fs_info = data;
        return 0;
 }
 
+static int bch2_noset_super(struct super_block *s, void *data)
+{
+       return -EBUSY;
+}
+
+static int bch2_test_super(struct super_block *s, void *data)
+{
+       struct bch_fs *c = s->s_fs_info;
+       struct bch_fs **devs = data;
+       unsigned i;
+
+       if (!c)
+               return false;
+
+       for (i = 0; devs[i]; i++)
+               if (c != devs[i])
+                       return false;
+       return true;
+}
+
 static struct dentry *bch2_mount(struct file_system_type *fs_type,
                                 int flags, const char *dev_name, void *data)
 {
@@ -1571,40 +1477,74 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
        struct super_block *sb;
        struct inode *vinode;
        struct bch_opts opts = bch2_opts_empty();
-       unsigned i;
+       char **devs;
+       struct bch_fs **devs_to_fs = NULL;
+       unsigned i, nr_devs;
        int ret;
 
-       opt_set(opts, read_only, (flags & MS_RDONLY) != 0);
+       opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
 
-       ret = bch2_parse_mount_opts(&opts, data);
+       ret = bch2_parse_mount_opts(NULL, &opts, data);
        if (ret)
                return ERR_PTR(ret);
 
-       c = bch2_open_as_blockdevs(dev_name, opts);
-       if (IS_ERR(c))
-               return ERR_CAST(c);
+       devs = split_devs(dev_name, &nr_devs);
+       if (!devs)
+               return ERR_PTR(-ENOMEM);
 
-       sb = sget(fs_type, bch2_test_super, bch2_set_super, flags|MS_NOSEC, c);
-       if (IS_ERR(sb)) {
-               closure_put(&c->cl);
-               return ERR_CAST(sb);
+       devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
+       if (!devs_to_fs) {
+               sb = ERR_PTR(-ENOMEM);
+               goto got_sb;
        }
 
-       BUG_ON(sb->s_fs_info != c);
+       for (i = 0; i < nr_devs; i++)
+               devs_to_fs[i] = bch2_path_to_fs(devs[i]);
 
-       if (sb->s_root) {
-               closure_put(&c->cl);
+       sb = sget(fs_type, bch2_test_super, bch2_noset_super,
+                 flags|SB_NOSEC, devs_to_fs);
+       if (!IS_ERR(sb))
+               goto got_sb;
+
+       c = bch2_fs_open(devs, nr_devs, opts);
+       if (IS_ERR(c)) {
+               sb = ERR_CAST(c);
+               goto got_sb;
+       }
+
+       /* Some options can't be parsed until after the fs is started: */
+       ret = bch2_parse_mount_opts(c, &opts, data);
+       if (ret) {
+               bch2_fs_stop(c);
+               sb = ERR_PTR(ret);
+               goto got_sb;
+       }
 
-               if ((flags ^ sb->s_flags) & MS_RDONLY) {
+       bch2_opts_apply(&c->opts, opts);
+
+       sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
+       if (IS_ERR(sb))
+               bch2_fs_stop(c);
+got_sb:
+       kfree(devs_to_fs);
+       kfree(devs[0]);
+       kfree(devs);
+
+       if (IS_ERR(sb))
+               return ERR_CAST(sb);
+
+       c = sb->s_fs_info;
+
+       if (sb->s_root) {
+               if ((flags ^ sb->s_flags) & SB_RDONLY) {
                        ret = -EBUSY;
                        goto err_put_super;
                }
                goto out;
        }
 
-       /* XXX: blocksize */
-       sb->s_blocksize         = PAGE_SIZE;
-       sb->s_blocksize_bits    = PAGE_SHIFT;
+       sb->s_blocksize         = block_bytes(c);
+       sb->s_blocksize_bits    = ilog2(block_bytes(c));
        sb->s_maxbytes          = MAX_LFS_FILESIZE;
        sb->s_op                = &bch_super_operations;
        sb->s_export_op         = &bch_export_ops;
@@ -1622,9 +1562,7 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
        if (ret)
                goto err_put_super;
 
-       sb->s_bdi->congested_fn         = bch2_congested;
-       sb->s_bdi->congested_data       = c;
-       sb->s_bdi->ra_pages             = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
+       sb->s_bdi->ra_pages             = VM_READAHEAD_PAGES;
 
        for_each_online_member(ca, c, i) {
                struct block_device *bdev = ca->disk_sb.bdev;
@@ -1638,22 +1576,25 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
 
 #ifdef CONFIG_BCACHEFS_POSIX_ACL
        if (c->opts.acl)
-               sb->s_flags     |= MS_POSIXACL;
+               sb->s_flags     |= SB_POSIXACL;
 #endif
 
        vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
        if (IS_ERR(vinode)) {
+               bch_err(c, "error mounting: error getting root inode %i",
+                       (int) PTR_ERR(vinode));
                ret = PTR_ERR(vinode);
                goto err_put_super;
        }
 
        sb->s_root = d_make_root(vinode);
        if (!sb->s_root) {
+               bch_err(c, "error mounting: error allocating root dentry");
                ret = -ENOMEM;
                goto err_put_super;
        }
 
-       sb->s_flags |= MS_ACTIVE;
+       sb->s_flags |= SB_ACTIVE;
 out:
        return dget(sb->s_root);
 
@@ -1667,11 +1608,7 @@ static void bch2_kill_sb(struct super_block *sb)
        struct bch_fs *c = sb->s_fs_info;
 
        generic_shutdown_super(sb);
-
-       if (test_bit(BCH_FS_BDEV_MOUNTED, &c->flags))
-               bch2_fs_stop(c);
-       else
-               closure_put(&c->cl);
+       bch2_fs_free(c);
 }
 
 static struct file_system_type bcache_fs_type = {