btrfs: rework directory logging to make it more efficient
cover btrfs: rework directory logging to make it more efficient
From: Filipe Manana <[email protected]>
This patchset changes directory logging to make it more efficient, by doing bulk inserts of directory items and avoiding tree searches on a item by item basis when they can be avoided. These decrease the amount of time we spend logging directory items and reduces lock contention on a log tree in case there are other tasks logging other inodes. The last patch mentions test results in its changelog, and the first 3 patches are just cleanups and preparatory work. This is also ground work for future changes to directory logging, but since these are already big enough, I'm sending these separately to get into integration/linux-next sooner rather than later.
Filipe Manana (5):
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
fs/btrfs/btrfs_inode.h | 39 ++-- fs/btrfs/inode.c | 6 +- fs/btrfs/tree-log.c | 419 ++++++++++++++++++++++++++++++----------- fs/btrfs/tree-log.h | 2 + 4 files changed, 340 insertions(+), 126 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/5 btrfs: remove root argument from btrfs_log_inode() and its callees
From: Filipe Manana <[email protected]>
The root argument passed to btrfs_log_inode() is unncessary, as it is always the root of the inode we are going to log. This root also gets unnecessarily propagated to several functions called by btrfs_log_inode(), and all of them take the inode as an argument as well. So just remove the root argument from these functions and have them get the root from the inode where needed.
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is patch 1/5. The change log of the last patch (5/5) has performance results.
--- fs/btrfs/tree-log.c | 52 +++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 28 deletions(-)
fs/btrfs/tree-log.c +24 -28
@@ -94,7 +94,7 @@ enum { };static int btrfs_log_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode, +struct btrfs_inode *inode,int inode_only,struct btrfs_log_ctx *ctx); static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
@@ -3621,13 +3621,14 @@ static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,* to replay anything deleted before the fsync */ static noinline int log_dir_items(struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode, +struct btrfs_inode *inode,struct btrfs_path *path,struct btrfs_path *dst_path,int key_type,struct btrfs_log_ctx *ctx,u64 min_offset,u64 *last_offset_ret) {struct btrfs_key min_key; +struct btrfs_root *root = inode->root;struct btrfs_root *log = root->log_root;struct extent_buffer *src;int err =0 ;
@@ -3828,7 +3829,7 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,* key logged by this transaction. */ static noinline int log_directory_changes(struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode, +struct btrfs_inode *inode,struct btrfs_path *path,struct btrfs_path *dst_path,struct btrfs_log_ctx *ctx)
@@ -3842,7 +3843,7 @@ static noinline int log_directory_changes(struct btrfs_trans_handle *trans, min_key =0 ; max_key =0 ;while (1 ) { - ret =log_dir_items (trans, root, inode, path, dst_path, key_type, + ret =log_dir_items (trans, inode, path, dst_path, key_type, ctx, min_key, &max_key);if (ret)return ret;
@@ -4339,13 +4340,13 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, }static int log_one_extent (struct btrfs_trans_handle *trans, -struct btrfs_inode *inode,struct btrfs_root *root, +struct btrfs_inode *inode,const struct extent_map *em,struct btrfs_path *path,struct btrfs_log_ctx *ctx) {struct btrfs_drop_extents_args drop_args = {0 }; -struct btrfs_root *log = root->log_root; +struct btrfs_root *log = inode->root->log_root;struct btrfs_file_extent_item *fi;struct extent_buffer *leaf;struct btrfs_map_token token;
@@ -4563,7 +4564,6 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans, }static int btrfs_log_changed_extents (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode,struct btrfs_path *path,struct btrfs_log_ctx *ctx)
@@ -4628,7 +4628,7 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,write_unlock (&tree->lock); - ret =log_one_extent (trans, inode, root, em, path, ctx); + ret =log_one_extent (trans, inode, em, path, ctx);write_lock (&tree->lock);clear_em_logging (tree, em);free_extent_map (em);
@@ -4717,11 +4717,11 @@ static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,* with a journal, ext3/4, xfs, f2fs, etc). */ static int btrfs_log_all_xattrs (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode,struct btrfs_path *path,struct btrfs_path *dst_path) { +struct btrfs_root *root = inode->root;int ret;struct btrfs_key key;const u64 ino =btrfs_ino (inode);
@@ -4795,10 +4795,10 @@ static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,* truncate operation that changes the inode's size. */ static int btrfs_log_holes (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode,struct btrfs_path *path) { +struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_key key;const u64 ino =btrfs_ino (inode);
@@ -5075,7 +5075,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,if (IS_ERR (inode)) { ret =PTR_ERR (inode); }else { - ret =btrfs_log_inode (trans, root, + ret =btrfs_log_inode (trans,BTRFS_I (inode),LOG_OTHER_INODE_ALL , ctx);
@@ -5135,8 +5135,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,* well because during a rename we pin the log and update the * log with the new name before we unpin it. */ - ret =btrfs_log_inode (trans, root,BTRFS_I (inode), -LOG_OTHER_INODE , ctx); + ret =btrfs_log_inode (trans,BTRFS_I (inode),LOG_OTHER_INODE , ctx);if (ret) {btrfs_add_delayed_iput (inode);continue ;
@@ -5347,7 +5346,7 @@ static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,* This handles both files and directories. */ static int btrfs_log_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root,struct btrfs_inode *inode, +struct btrfs_inode *inode,int inode_only,struct btrfs_log_ctx *ctx) {
@@ -5355,7 +5354,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,struct btrfs_path *dst_path;struct btrfs_key min_key;struct btrfs_key max_key; -struct btrfs_root *log = root->log_root; +struct btrfs_root *log = inode->root->log_root;int err =0 ;int ret =0 ;bool fast_search = false;
@@ -5505,14 +5504,14 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,btrfs_release_path (path);btrfs_release_path (dst_path); - err =btrfs_log_all_xattrs (trans, root, inode, path, dst_path); + err =btrfs_log_all_xattrs (trans, inode, path, dst_path);if (err) goto out_unlock; xattrs_logged = true;if (max_key.type >=BTRFS_EXTENT_DATA_KEY && !fast_search) {btrfs_release_path (path);btrfs_release_path (dst_path); - err =btrfs_log_holes (trans, root, inode, path); + err =btrfs_log_holes (trans, inode, path);if (err) goto out_unlock; }
@@ -5532,16 +5531,14 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,* BTRFS_INODE_COPY_EVERYTHING set. */ if (!xattrs_logged && inode->logged_trans < trans->transid) { - err =btrfs_log_all_xattrs (trans, root, inode, path, - dst_path); + err =btrfs_log_all_xattrs (trans, inode, path, dst_path);if (err) goto out_unlock;btrfs_release_path (path); } }if (fast_search) { - ret =btrfs_log_changed_extents (trans, root, inode, dst_path, - ctx); + ret =btrfs_log_changed_extents (trans, inode, dst_path, ctx);if (ret) { err = ret; goto out_unlock;
@@ -5556,8 +5553,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, }if (inode_only ==LOG_INODE_ALL &&S_ISDIR (inode->vfs_inode.i_mode)) { - ret =log_directory_changes (trans, root, inode, path, dst_path, - ctx); + ret =log_directory_changes (trans, inode, path, dst_path, ctx);if (ret) { err = ret; goto out_unlock;
@@ -5786,7 +5782,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans, ctx->log_new_dentries = false;if (type ==BTRFS_FT_DIR || type ==BTRFS_FT_SYMLINK ) log_mode =LOG_INODE_ALL ; - ret =btrfs_log_inode (trans, root,BTRFS_I (di_inode), + ret =btrfs_log_inode (trans,BTRFS_I (di_inode), log_mode, ctx);btrfs_add_delayed_iput (di_inode);if (ret)
@@ -5931,7 +5927,7 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans, } ctx->log_new_dentries = false; - ret =btrfs_log_inode (trans, root,BTRFS_I (dir_inode), + ret =btrfs_log_inode (trans,BTRFS_I (dir_inode),LOG_INODE_ALL , ctx);if (!ret && ctx->log_new_dentries) ret = log_new_dir_dentries(trans, root,
@@ -5979,7 +5975,7 @@ static int log_new_ancestors(struct btrfs_trans_handle *trans,if (BTRFS_I (inode)->generation >= trans->transid &&need_log_inode (trans,BTRFS_I (inode))) - ret =btrfs_log_inode (trans, root,BTRFS_I (inode), + ret =btrfs_log_inode (trans,BTRFS_I (inode),LOG_INODE_EXISTS , ctx);btrfs_add_delayed_iput (inode);if (ret)
@@ -6034,7 +6030,7 @@ static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,if (inode->generation >= trans->transid &&need_log_inode (trans, inode)) { - ret =btrfs_log_inode (trans, root, inode, + ret =btrfs_log_inode (trans, inode,LOG_INODE_EXISTS , ctx);if (ret)break ;
@@ -6177,7 +6173,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,if (ret) goto end_no_trans; - ret =btrfs_log_inode (trans, root, inode, inode_only, ctx); + ret =btrfs_log_inode (trans, inode, inode_only, ctx);if (ret) goto end_trans;
Signature · 1 line
2.33.0
Re: [PATCH 1/5] btrfs: remove root argument from btrfs_log_inode() and its callees
Quoted 9 lines · On Thu, Sep 16, 2021 at 11:32:10AM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
The root argument passed to
btrfs_log_inode()is unncessary, as it is always the root of the inode we are going to log. This root also gets unnecessarily propagated to several functions called bybtrfs_log_inode(), and all of them take the inode as an argument as well. So just remove the root argument from these functions and have them get the root from the inode where needed.
Quoted 7 lines
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from
btrfs_log_inode()and its callees
btrfs: remove redundant log root assignment fromlog_dir_items()
btrfs: factor out the copying loop of dir items fromlog_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is a nice description, in all the patches, though I think you could make it less tedious for yourself to just reference the patch with results or a significant change. Up to you.
Re: [PATCH 1/5] btrfs: remove root argument from btrfs_log_inode() and its callees
Quoted 19 lines · David Sterba
Quoted 9 lines · On Thu, Sep 16, 2021 at 11:32:10AM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
The root argument passed to
btrfs_log_inode()is unncessary, as it is always the root of the inode we are going to log. This root also gets unnecessarily propagated to several functions called bybtrfs_log_inode(), and all of them take the inode as an argument as well. So just remove the root argument from these functions and have them get the root from the inode where needed.Quoted 7 lines
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from
btrfs_log_inode()and its callees
btrfs: remove redundant log root assignment fromlog_dir_items()
btrfs: factor out the copying loop of dir items fromlog_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
Quoted 3 lines
This is a nice description, in all the patches, though I think you could make it less tedious for yourself to just reference the patch with results or a significant change. Up to you.
It's just copy paste, it doesn't add any significant work for me. Btw, I see that patch 2/5 is missing in misc-next, was that intentional?
Re: [PATCH 1/5] btrfs: remove root argument from btrfs_log_inode() and its callees
Quoted 20 lines · Filipe Manana
Quoted 19 lines · David Sterba
Quoted 16 lines · On Thu, Sep 16, 2021 at 11:32:10AM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
The root argument passed to
btrfs_log_inode()is unncessary, as it is always the root of the inode we are going to log. This root also gets unnecessarily propagated to several functions called bybtrfs_log_inode(), and all of them take the inode as an argument as well. So just remove the root argument from these functions and have them get the root from the inode where needed.This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from
btrfs_log_inode()and its callees
btrfs: remove redundant log root assignment fromlog_dir_items()
btrfs: factor out the copying loop of dir items fromlog_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
Quoted 6 lines
Quoted 3 lines
This is a nice description, in all the patches, though I think you could make it less tedious for yourself to just reference the patch with results or a significant change. Up to you.
It's just copy paste, it doesn't add any significant work for me. Btw, I see that patch 2/5 is missing in misc-next, was that intentional?
Unintentional. My bad, sorry.
PATCH 2/5 btrfs: remove redundant log root assignment from log_dir_items()
In reply to [email protected]From: Filipe Manana <[email protected]>
At log_dir_items() we are assigning the exact same value to the local variable 'log', once when it's declared and once again shortly after. Remove the later assignment as it's pointless.
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is patch 2/5. The change log of the last patch (5/5) has performance results.
--- fs/btrfs/tree-log.c | 2 -- 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
@@ -3639,8 +3639,6 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,u64 last_offset = (u64)-1 ;u64 ino =btrfs_ino (inode); - log = root->log_root; - min_key.objectid = ino; min_key.type = key_type; min_key.offset = min_offset;
Signature · 1 line
2.33.0
PATCH 3/5 btrfs: factor out the copying loop of dir items from log_dir_items()
In reply to [email protected]From: Filipe Manana <[email protected]>
In preparation for the next change, move the loop that processes a leaf and copies its directory items to the log, into a separate helper function. This makes the next change simpler and it also helps making log_dir_items() a bit shorter (specially after the next change).
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is patch 3/5. The change log of the last patch (5/5) has performance results.
--- fs/btrfs/tree-log.c | 135 ++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 60 deletions(-)
fs/btrfs/tree-log.c +75 -60
@@ -3615,6 +3615,66 @@ static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,return 0 ; } +static int process_dir_items_leaf (struct btrfs_trans_handle *trans, +struct btrfs_inode *inode, +struct btrfs_path *path, +struct btrfs_path *dst_path, +int key_type, +struct btrfs_log_ctx *ctx) +{ +struct btrfs_root *log = inode->root->log_root; +struct extent_buffer *src = path->nodes[0 ]; +const int nritems =btrfs_header_nritems (src); +const u64 ino =btrfs_ino (inode); +int i; + +for (i = path->slots[0 ]; i < nritems; i++) { +struct btrfs_key key; +struct btrfs_dir_item *di; +int ret; + +btrfs_item_key_to_cpu (src, &key, i); + +if (key.objectid != ino || key.type != key_type) +return 1 ; + + ret =overwrite_item (trans, log, dst_path, src, i, &key); +if (ret <0 ) +return ret; + +/* +* We must make sure that when we log a directory entry, the +* corresponding inode, after log replay, has a matching link +* count. For example: +* +* touch foo +* mkdir mydir +* sync +* ln foo mydir/bar +* xfs_io -c "fsync" mydir +* <crash> +* <mount fs and log replay> +* +* Would result in a fsync log that when replayed, our file inode +* would have a link count of 1, but we get two directory entries +* pointing to the same inode. After removing one of the names, +* it would not be possible to remove the other name, which +* resulted always in stale file handle errors, and would not be +* possible to rmdir the parent directory, since its i_size could +* never be decremented to the value BTRFS_EMPTY_DIR_SIZE, +* resulting in -ENOTEMPTY errors. +*/ + di = btrfs_item_ptr(src, i, struct btrfs_dir_item); +btrfs_dir_item_key_to_cpu (src, di, &key); +if ((btrfs_dir_transid (src, di) == trans->transid || +btrfs_dir_type (src, di) ==BTRFS_FT_DIR ) && + key.type !=BTRFS_ROOT_ITEM_KEY ) + ctx->log_new_dentries = true; + } + +return 0 ; +} +/* * log all the items included in the current transaction for a given * directory. This also creates the range items in the log tree required
@@ -3630,11 +3690,8 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,struct btrfs_key min_key;struct btrfs_root *root = inode->root;struct btrfs_root *log = root->log_root; -struct extent_buffer *src;int err =0 ;int ret; -int i; -int nritems;u64 first_offset = min_offset;u64 last_offset = (u64)-1 ;u64 ino =btrfs_ino (inode);
@@ -3712,61 +3769,14 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,* from our directory */ while (1 ) { -struct btrfs_key tmp; - src = path->nodes[0 ]; - nritems =btrfs_header_nritems (src); -for (i = path->slots[0 ]; i < nritems; i++) { -struct btrfs_dir_item *di; - -btrfs_item_key_to_cpu (src, &min_key, i); - -if (min_key.objectid != ino || min_key.type != key_type) - goto done; - -if (need_resched ()) { -btrfs_release_path (path); -cond_resched (); - goto search; - } - - ret =overwrite_item (trans, log, dst_path, src, i, - &min_key); -if (ret) { + ret =process_dir_items_leaf (trans, inode, path, dst_path, + key_type, ctx); +if (ret !=0 ) { +if (ret <0 ) err = ret; - goto done; - } - -/* -* We must make sure that when we log a directory entry, -* the corresponding inode, after log replay, has a -* matching link count. For example: -* -* touch foo -* mkdir mydir -* sync -* ln foo mydir/bar -* xfs_io -c "fsync" mydir -* <crash> -* <mount fs and log replay> -* -* Would result in a fsync log that when replayed, our -* file inode would have a link count of 1, but we get -* two directory entries pointing to the same inode. -* After removing one of the names, it would not be -* possible to remove the other name, which resulted -* always in stale file handle errors, and would not -* be possible to rmdir the parent directory, since -* its i_size could never decrement to the value -* BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors. -*/ - di = btrfs_item_ptr(src, i, struct btrfs_dir_item); -btrfs_dir_item_key_to_cpu (src, di, &tmp); -if ((btrfs_dir_transid (src, di) == trans->transid || -btrfs_dir_type (src, di) ==BTRFS_FT_DIR ) && - tmp.type !=BTRFS_ROOT_ITEM_KEY ) - ctx->log_new_dentries = true; + goto done; } - path->slots[0 ] = nritems; + path->slots[0 ] =btrfs_header_nritems (path->nodes[0 ]);/* * look ahead to the next item and see if it is also
@@ -3780,21 +3790,26 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans, err = ret; goto done; } -btrfs_item_key_to_cpu (path->nodes[0 ], &tmp, path->slots[0 ]); -if (tmp.objectid != ino || tmp.type != key_type) { +btrfs_item_key_to_cpu (path->nodes[0 ], &min_key, path->slots[0 ]); +if (min_key.objectid != ino || min_key.type != key_type) { last_offset = (u64)-1 ; goto done; }if (btrfs_header_generation (path->nodes[0 ]) != trans->transid) { ret =overwrite_item (trans, log, dst_path, path->nodes[0 ], path->slots[0 ], - &tmp); + &min_key);if (ret) err = ret;else - last_offset = tmp.offset; + last_offset = min_key.offset; goto done; } +if (need_resched ()) { +btrfs_release_path (path); +cond_resched (); + goto search; + } } done:btrfs_release_path (path);
Signature · 1 line
2.33.0
PATCH 4/5 btrfs: insert items in batches when logging a directory when possible
In reply to [email protected]From: Filipe Manana <[email protected]>
When logging a directory, we scan its directory items from the subvolume tree and then copy one by one into the log tree. This is not efficient since we generally are able to insert several items in a batch, using a single btree operation for adding several items at once. The reason we copy items one by one is that we must check if each item was previously logged in the current transaction, and if it was we either overwrite it or skip it in case its content did not change in the subvolume tree (this can happen only for dir item keys, but not for dir index keys), and doing such check makes it a bit cumbersome to attempt batch insertions.
However the chances for doing batch insertions are very frequent and always happen when:
1) Logging the directory for the first time in the current transaction, as none of the items exist in the log tree yet;
2) Logging new dir index keys, because the offset for new dir index keys comes from a monotonically increasing counter. This means if we keep adding dentries to a directory, through creation of new files and sub-directories or by adding new links or renaming from some other directory into the one we are logging, all the new dir index keys have a new offset that is greater than the offset of any previously logged index keys, so we can insert them in batches into the log tree.
For dir item keys, since their offset depends on the result of an hash function against the dentry's name, unless the directory is being logged for the first time in the current transaction, the chances being able to insert the items in the log using batches is pretty much random and not predictable, as it depends on the names of the dentries, but still happens often enough.
So change directory logging to keep track of consecutive directory items that don't exist yet in the log and batch insert them.
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is patch 4/5. The change log of the last patch (5/5) has performance results.
--- fs/btrfs/tree-log.c | 217 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 180 insertions(+), 37 deletions(-)
fs/btrfs/tree-log.c +180 -37
@@ -368,25 +368,11 @@ static int process_one_buffer(struct btrfs_root *log,return ret; } -/* -* Item overwrite used by replay and tree logging. eb, slot and key all refer -* to the src data we are copying out. -* -* root is the tree we are copying into, and path is a scratch -* path for use in this function (it should be released on entry and -* will be released on exit). -* -* If the key is already in the destination tree the existing item is -* overwritten. If the existing item isn't big enough, it is extended. -* If it is too large, it is truncated. -* -* If the key isn't in the destination yet, a new item is inserted. -*/ -static noinline int overwrite_item(struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct btrfs_path *path, -struct extent_buffer *eb,int slot, -struct btrfs_key *key) +static int do_overwrite_item (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_path *path, +struct extent_buffer *eb,int slot, +struct btrfs_key *key) {int ret;u32 item_size;
@@ -403,10 +389,22 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, item_size =btrfs_item_size_nr (eb, slot); src_ptr =btrfs_item_ptr_offset (eb, slot); -/* look for the key in the destination tree */ - ret =btrfs_search_slot (NULL , root, key, path,0 ,0 ); -if (ret <0 ) -return ret; +/* Our caller must have done a search for the key for us. */ +ASSERT (path->nodes[0 ] !=NULL ); + +/* +* And the slot must point to the exact key or the slot where the key +* should be at (the first item with a key greater than 'key') +*/ +if (path->slots[0 ] <btrfs_header_nritems (path->nodes[0 ])) { +struct btrfs_key found_key; + +btrfs_item_key_to_cpu (path->nodes[0 ], &found_key, path->slots[0 ]); + ret =btrfs_comp_cpu_keys (&found_key, key); +ASSERT (ret >=0 ); + }else { + ret =1 ; + }if (ret ==0 ) {char *src_copy;
@@ -584,6 +582,36 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,return 0 ; } +/* +* Item overwrite used by replay and tree logging. eb, slot and key all refer +* to the src data we are copying out. +* +* root is the tree we are copying into, and path is a scratch +* path for use in this function (it should be released on entry and +* will be released on exit). +* +* If the key is already in the destination tree the existing item is +* overwritten. If the existing item isn't big enough, it is extended. +* If it is too large, it is truncated. +* +* If the key isn't in the destination yet, a new item is inserted. +*/ +static int overwrite_item (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_path *path, +struct extent_buffer *eb,int slot, +struct btrfs_key *key) +{ +int ret; + +/* Look for the key in the destination tree. */ + ret =btrfs_search_slot (NULL , root, key, path,0 ,0 ); +if (ret <0 ) +return ret; + +return do_overwrite_item (trans, root, path, eb, slot, key); +} +/* * simple helper to read an inode off the disk from a given root * This can only be called for subvolume roots and not for the log
@@ -3615,6 +3643,68 @@ static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,return 0 ; } +static int flush_dir_items_batch (struct btrfs_trans_handle *trans, +struct btrfs_root *log, +struct extent_buffer *src, +struct btrfs_path *dst_path, +int start_slot, +int count) +{ +char *ins_data =NULL ; +struct btrfs_key *ins_keys; +u32 *ins_sizes; +struct extent_buffer *dst; +struct btrfs_key key; +u32 item_size; +int ret; +int i; + +ASSERT (count >0 ); + +if (count ==1 ) { +btrfs_item_key_to_cpu (src, &key, start_slot); + item_size =btrfs_item_size_nr (src, start_slot); + ins_keys = &key; + ins_sizes = &item_size; + }else { + ins_data =kmalloc (count *sizeof (u32) + + count *sizeof (struct btrfs_key ),GFP_NOFS ); +if (!ins_data) +return -ENOMEM ; + + ins_sizes = (u32 *)ins_data; + ins_keys = (struct btrfs_key *)(ins_data + count *sizeof (u32)); + +for (i =0 ; i < count; i++) { +const int slot = start_slot + i; + +btrfs_item_key_to_cpu (src, &ins_keys[i], slot); + ins_sizes[i] =btrfs_item_size_nr (src, slot); + } + } + + ret =btrfs_insert_empty_items (trans, log, dst_path, ins_keys, ins_sizes, + count); +if (ret) + goto out; + + dst = dst_path->nodes[0 ]; +for (i =0 ; i < count; i++) { +unsigned long src_offset; +unsigned long dst_offset; + + dst_offset =btrfs_item_ptr_offset (dst, dst_path->slots[0 ]); + src_offset =btrfs_item_ptr_offset (src, start_slot + i); +copy_extent_buffer (dst, src, dst_offset, src_offset, ins_sizes[i]); + dst_path->slots[0 ]++; + } +btrfs_release_path (dst_path); +out: +kfree (ins_data); + +return ret; +} + static int process_dir_items_leaf(struct btrfs_trans_handle *trans, struct btrfs_inode *inode, struct btrfs_path *path,
@@ -3626,21 +3716,22 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,struct extent_buffer *src = path->nodes[0 ];const int nritems =btrfs_header_nritems (src);const u64 ino =btrfs_ino (inode); +const bool inode_logged_before =inode_logged (trans, inode); +bool last_found = false; +int batch_start =0 ; +int batch_size =0 ;int i;for (i = path->slots[0 ]; i < nritems; i++) {struct btrfs_key key; -struct btrfs_dir_item *di;int ret;btrfs_item_key_to_cpu (src, &key, i); -if (key.objectid != ino || key.type != key_type) -return 1 ; - - ret =overwrite_item (trans, log, dst_path, src, i, &key); -if (ret <0 ) -return ret; +if (key.objectid != ino || key.type != key_type) { + last_found = true; +break ; + }/* * We must make sure that when we log a directory entry, the
@@ -3664,15 +3755,67 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,* never be decremented to the value BTRFS_EMPTY_DIR_SIZE, * resulting in -ENOTEMPTY errors. */ - di = btrfs_item_ptr(src, i, struct btrfs_dir_item); -btrfs_dir_item_key_to_cpu (src, di, &key); -if ((btrfs_dir_transid (src, di) == trans->transid || -btrfs_dir_type (src, di) ==BTRFS_FT_DIR ) && - key.type !=BTRFS_ROOT_ITEM_KEY ) - ctx->log_new_dentries = true; +if (!ctx->log_new_dentries) { +struct btrfs_dir_item *di; +struct btrfs_key di_key; + + di = btrfs_item_ptr(src, i, struct btrfs_dir_item); +btrfs_dir_item_key_to_cpu (src, di, &di_key); +if ((btrfs_dir_transid (src, di) == trans->transid || +btrfs_dir_type (src, di) ==BTRFS_FT_DIR ) && + di_key.type !=BTRFS_ROOT_ITEM_KEY ) + ctx->log_new_dentries = true; + } + +if (!inode_logged_before) + goto add_to_batch; +/* +* Check if the key was already logged before. If not we can add +* it to a batch for bulk insertion. +*/ + ret =btrfs_search_slot (NULL , log, &key, dst_path,0 ,0 ); +if (ret <0 ) { +return ret; + }else if (ret >0 ) { +btrfs_release_path (dst_path); + goto add_to_batch; + } + +/* +* Item exists in the log. Overwrite the item in the log if it +* has different content or do nothing if it has exactly the same +* content. And then flush the current batch if any - do it after +* overwriting the current item, or we would deadlock otherwise, +* since we are holding a path for the existing item. +*/ + ret =do_overwrite_item (trans, log, dst_path, src, i, &key); +if (ret <0 ) +return ret; + +if (batch_size >0 ) { + ret =flush_dir_items_batch (trans, log, src, dst_path, + batch_start, batch_size); +if (ret <0 ) +return ret; + batch_size =0 ; + } +continue ; +add_to_batch: +if (batch_size ==0 ) + batch_start = i; + batch_size++; } -return 0 ; +if (batch_size >0 ) { +int ret; + + ret =flush_dir_items_batch (trans, log, src, dst_path, + batch_start, batch_size); +if (ret <0 ) +return ret; + } + +return last_found ?1 :0 ; }/*
Signature · 1 line
2.33.0
PATCH 5/5 btrfs: keep track of the last logged keys when logging a directory
In reply to [email protected]From: Filipe Manana <[email protected]>
After the first time we log a directory in the current transaction, for each directory item in a changed leaf of the subvolume tree, we have to check if we previously logged the item, in order to overwrite it in case its data changed or skip it in case its data hasn't changed.
Checking if we have logged each item before not only wastes times, but it also adds lock contention on the log tree. So in order to minimize the number of times we do such checks, keep track of the offset of the last key we logged for a directory and, on the next time we log the directory, skip the checks for any new keys that have an offset greater than the offset we have previously saved. This is specially effective for index keys, because the offset for these keys comes from a monotonically increasing counter.
This patch is part of a patchset comprised of the following 5 patches:
btrfs: remove root argument from btrfs_log_inode() and its callees
btrfs: remove redundant log root assignment from log_dir_items()
btrfs: factor out the copying loop of dir items from log_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
This is patch 5/5.
The following test was used on a non-debug kernel to measure the impact it has on a directory fsync:
$ cat test-dir-fsync.sh #!/bin/bash
DEV=/dev/nvme0n1
MNT=/mnt/nvme0n1
NUM_NEW_FILES=100000
NUM_FILE_DELETES=1000
mkfs.btrfs -f $DEV
mount -o ssd $DEV $MNT
mkdir $MNT/testdir
for ((i = 1; i <= $NUM_NEW_FILES; i++)); do
echo -n > $MNT/testdir/file_$i
done
# fsync the directory, this will log the new dir items and the inodes # they point to, because these are new inodes. start=$(date +%s%N) xfs_io -c "fsync" $MNT/testdir end=$(date +%s%N)
dur=$(( (end - start) / 1000000 ))
echo "dir fsync took $dur ms after adding $NUM_NEW_FILES files"
# sync to force transaction commit and wipeout the log. sync
del_inc=$(( $NUM_NEW_FILES / $NUM_FILE_DELETES ))
for ((i = 1; i <= $NUM_NEW_FILES; i += $del_inc)); do
rm -f $MNT/testdir/file_$i
done
# fsync the directory, this will only log dir items, there are no
# dentries pointing to new inodes.
start=$(date +%s%N)
xfs_io -c "fsync" $MNT/testdir
end=$(date +%s%N) dur=$(( (end - start) / 1000000 ))
echo "dir fsync took $dur ms after deleting $NUM_FILE_DELETES files"
umount $MNT
Test results with NUM_NEW_FILES set to 100 000 and 1 000 000:
**** before patchset, 100 000 files, 1000 deletes ****
dir fsync took 848 ms after adding 100000 files
dir fsync took 175 ms after deleting 1000 files
**** after patchset, 100 000 files, 1000 deletes ****
dir fsync took 758 ms after adding 100000 files (-11.2%)
dir fsync took 63 ms after deleting 1000 files (-94.1%)
**** before patchset, 1 000 000 files, 1000 deletes ****
dir fsync took 9945 ms after adding 1000000 files
dir fsync took 473 ms after deleting 1000 files
**** after patchset, 1 000 000 files, 1000 deletes ****
dir fsync took 8677 ms after adding 1000000 files (-13.6%)
dir fsync took 146 ms after deleting 1000 files (-105.6%)
--- fs/btrfs/btrfs_inode.h | 39 ++++++++++++++++++++++++++++----------- fs/btrfs/inode.c | 6 ++++-- fs/btrfs/tree-log.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/tree-log.h | 2 ++ 4 files changed, 75 insertions(+), 13 deletions(-)
fs/btrfs/btrfs_inode.h +28 -11
@@ -138,17 +138,34 @@ struct btrfs_inode {/* a local copy of root's last_log_commit */ int last_log_commit; -/* total number of bytes pending delalloc, used by stat to calc the -* real block usage of the file -*/ -u64 delalloc_bytes; - -/* -* Total number of bytes pending delalloc that fall within a file -* range that is either a hole or beyond EOF (and no prealloc extent -* exists in the range). This is always <= delalloc_bytes. -*/ -u64 new_delalloc_bytes; +union { +/* +* Total number of bytes pending delalloc, used by stat to +* calculate the real block usage of the file. This is used +* only for files. +*/ +u64 delalloc_bytes; +/* +* The offset of the last dir item key that was logged. +* This is used only for directories. +*/ +u64 last_dir_item_offset; + }; + +union { +/* +* Total number of bytes pending delalloc that fall within a file +* range that is either a hole or beyond EOF (and no prealloc extent +* exists in the range). This is always <= delalloc_bytes and this +* is used only for files. +*/ +u64 new_delalloc_bytes; +/* +* The offset of the last dir index key that was logged. +* This is used only for directories. +*/ +u64 last_dir_index_offset; + };/* * total number of bytes pending defrag, used by stat to check whether
fs/btrfs/inode.c +4 -2
@@ -9155,8 +9155,10 @@ void btrfs_destroy_inode(struct inode *vfs_inode)WARN_ON (inode->block_rsv.reserved);WARN_ON (inode->block_rsv.size);WARN_ON (inode->outstanding_extents); -WARN_ON (inode->delalloc_bytes); -WARN_ON (inode->new_delalloc_bytes); +if (!S_ISDIR (vfs_inode->i_mode)) { +WARN_ON (inode->delalloc_bytes); +WARN_ON (inode->new_delalloc_bytes); + }WARN_ON (inode->csum_bytes);WARN_ON (inode->defrag_bytes);
fs/btrfs/tree-log.c +41 -0
81 lines not on this page · raw message
fs/btrfs/tree-log.h +2 -0
@@ -17,6 +17,8 @@ struct btrfs_log_ctx {int log_transid;bool log_new_dentries;bool logging_new_name; +/* Tracks the last logged dir item/index key offset. */ +u64 last_dir_item_offset;struct inode *inode;struct list_head list;/* Only used for fast fsyncs. */
Signature · 1 line
2.33.0
Quoted 14 lines · On Thu, Sep 16, 2021 at 11:32:09AM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
This patchset changes directory logging to make it more efficient, by doing bulk inserts of directory items and avoiding tree searches on a item by item basis when they can be avoided. These decrease the amount of time we spend logging directory items and reduces lock contention on a log tree in case there are other tasks logging other inodes. The last patch mentions test results in its changelog, and the first 3 patches are just cleanups and preparatory work. This is also ground work for future changes to directory logging, but since these are already big enough, I'm sending these separately to get into integration/linux-next sooner rather than later.
Quoted 6 lines
Filipe Manana (5):
btrfs: remove root argument frombtrfs_log_inode()and its callees
btrfs: remove redundant log root assignment fromlog_dir_items()
btrfs: factor out the copying loop of dir items fromlog_dir_items()
btrfs: insert items in batches when logging a directory when possible
btrfs: keep track of the last logged keys when logging a directory
Added to misc-next, thanks.