]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
btrfs: avoid transaction commit on any fsync after subvolume creation
authorFilipe Manana <fdmanana@suse.com>
Tue, 11 Jun 2024 10:44:33 +0000 (11:44 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 11 Jul 2024 13:33:24 +0000 (15:33 +0200)
As of commit 1b53e51a4a8f ("btrfs: don't commit transaction for every
subvol create") we started to make any fsync after creating a subvolume
to fallback to a transaction commit if the fsync is performed in the
same transaction that was used to create the subvolume. This happens
with the following at ioctl.c:create_subvol():

  $ cat fs/btrfs/ioctl.c
  (...)
      /* Tree log can't currently deal with an inode which is a new root. */
      btrfs_set_log_full_commit(trans);
  (...)

Note that the comment is misleading as the problem is not that fsync can
not deal with the root inode of a new root, but that we can not log any
inode that belongs to a root that was not yet persisted because that would
make log replay fail since the root doesn't exist at log replay time.

The above simply makes any fsync fallback to a full transaction commit if
it happens in the same transaction used to create the subvolume - even if
it's an inode that belongs to any other subvolume. This is a brute force
solution and it doesn't necessarily improve performance for every workload
out there - it just moves a full transaction commit from one place, the
subvolume creation, to another - an fsync for any inode.

Just improve on this by making the fallback to a transaction commit only
for an fsync against an inode of the new subvolume, or for the directory
that contains the dentry that points to the new subvolume (in case anyone
attempts to fsync the directory in the same transaction).

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c
fs/btrfs/tree-log.c
fs/btrfs/tree-log.h

index d00d49338ecb7afa9ae5c11aed7e536da8fa3476..1dca986943f0be756363b6542e9c7335b3e0f960 100644 (file)
@@ -662,8 +662,6 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
        qgroup_reserved = 0;
        trans->block_rsv = &block_rsv;
        trans->bytes_reserved = block_rsv.size;
-       /* Tree log can't currently deal with an inode which is a new root. */
-       btrfs_set_log_full_commit(trans);
 
        ret = btrfs_qgroup_inherit(trans, 0, objectid, btrfs_root_id(root), inherit);
        if (ret)
@@ -764,6 +762,8 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
                goto out;
        }
 
+       btrfs_record_new_subvolume(trans, BTRFS_I(dir));
+
        d_instantiate_new(dentry, new_inode_args.inode);
        new_inode_args.inode = NULL;
 
index 57df8506a952ff5b32bcda2eee2bd12cc8e831dd..4c9cc8eecb30b9879a959e0214fead6191a80559 100644 (file)
@@ -7078,6 +7078,15 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
                goto end_no_trans;
        }
 
+       /*
+        * If we're logging an inode from a subvolume created in the current
+        * transaction we must force a commit since the root is not persisted.
+        */
+       if (btrfs_root_generation(&root->root_item) == trans->transid) {
+               ret = BTRFS_LOG_FORCE_COMMIT;
+               goto end_no_trans;
+       }
+
        /*
         * Skip already logged inodes or inodes corresponding to tmpfiles
         * (since logging them is pointless, a link count of 0 means they
@@ -7458,6 +7467,24 @@ void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
        mutex_unlock(&dir->log_mutex);
 }
 
+/*
+ * Call this when creating a subvolume in a directory.
+ * Because we don't commit a transaction when creating a subvolume, we can't
+ * allow the directory pointing to the subvolume to be logged with an entry that
+ * points to an unpersisted root if we are still in the transaction used to
+ * create the subvolume, so make any attempt to log the directory to result in a
+ * full log sync.
+ * Also we don't need to worry with renames, since btrfs_rename() marks the log
+ * for full commit when renaming a subvolume.
+ */
+void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
+                               struct btrfs_inode *dir)
+{
+       mutex_lock(&dir->log_mutex);
+       dir->last_unlink_trans = trans->transid;
+       mutex_unlock(&dir->log_mutex);
+}
+
 /*
  * Update the log after adding a new name for an inode.
  *
index fa0a689259b12f8d29c2f3ebe070c8acf5ad0b9e..dc313e6bb2faa11e7cbdc6ce25cd0e44b3fdca96 100644 (file)
@@ -94,6 +94,8 @@ void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
                             bool for_rename);
 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
                                   struct btrfs_inode *dir);
+void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
+                               struct btrfs_inode *dir);
 void btrfs_log_new_name(struct btrfs_trans_handle *trans,
                        struct dentry *old_dentry, struct btrfs_inode *old_dir,
                        u64 old_dir_index, struct dentry *parent);