From cb689481947a36c4b0e885d21a315ff9ffbb87ab Mon Sep 17 00:00:00 2001 From: Filipe Manana Date: Wed, 11 Jan 2023 11:36:03 +0000 Subject: [PATCH] btrfs: send: avoid unnecessary generation search at did_overwrite_ref() At did_overwrite_ref() we always call get_inode_gen() to find out the generation of the inode 'ow_inode'. However we don't always need to use that generation, and in fact it's very common to not use it, so we end up doing a b+tree search on the send root, allocating a path, etc, for nothing. So improve on this by getting the generation only if we need to use it. This patch is part of a larger patchset and the changelog of the last patch in the series contains a sample performance test and results. The patches that comprise the patchset are the following: btrfs: send: directly return from did_overwrite_ref() and simplify it btrfs: send: avoid unnecessary generation search at did_overwrite_ref() btrfs: send: directly return from will_overwrite_ref() and simplify it btrfs: send: avoid extra b+tree searches when checking reference overrides btrfs: send: remove send_progress argument from can_rmdir() btrfs: send: avoid duplicated orphan dir allocation and initialization btrfs: send: avoid unnecessary orphan dir rbtree search at can_rmdir() btrfs: send: reduce searches on parent root when checking if dir can be removed btrfs: send: iterate waiting dir move rbtree only once when processing refs btrfs: send: initialize all the red black trees earlier btrfs: send: genericize the backref cache to allow it to be reused btrfs: adapt lru cache to allow for 64 bits keys on 32 bits systems btrfs: send: cache information about created directories btrfs: allow a generation number to be associated with lru cache entries btrfs: add an api to delete a specific entry from the lru cache btrfs: send: use the lru cache to implement the name cache btrfs: send: update size of roots array for backref cache entries btrfs: send: cache utimes operations for directories if possible Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba --- fs/btrfs/send.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c index c77508b7ed68..73017be632ed 100644 --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -2193,8 +2193,8 @@ static int did_overwrite_ref(struct send_ctx *sctx, const char *name, int name_len) { int ret; - u64 gen; u64 ow_inode; + u64 ow_gen = 0; if (!sctx->parent_root) return 0; @@ -2204,6 +2204,8 @@ static int did_overwrite_ref(struct send_ctx *sctx, return ret; if (dir != BTRFS_FIRST_FREE_OBJECTID) { + u64 gen; + ret = get_inode_gen(sctx->send_root, dir, &gen); if (ret == -ENOENT) return 0; @@ -2224,12 +2226,15 @@ static int did_overwrite_ref(struct send_ctx *sctx, return ret; } - ret = get_inode_gen(sctx->send_root, ow_inode, &gen); - if (ret < 0) - return ret; + if (ow_inode == ino) { + ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen); + if (ret < 0) + return ret; - if (ow_inode == ino && gen == ino_gen) - return 0; + /* It's the same inode, so no overwrite happened. */ + if (ow_gen == ino_gen) + return 0; + } /* * We know that it is or will be overwritten. Check this now. @@ -2237,11 +2242,19 @@ static int did_overwrite_ref(struct send_ctx *sctx, * inode 'ino' to be orphanized, therefore check if ow_inode matches * the current inode being processed. */ - if ((ow_inode < sctx->send_progress) || - (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && - gen == sctx->cur_inode_gen)) + if (ow_inode < sctx->send_progress) return 1; + if (ino != sctx->cur_ino && ow_inode == sctx->cur_ino) { + if (ow_gen == 0) { + ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen); + if (ret < 0) + return ret; + } + if (ow_gen == sctx->cur_inode_gen) + return 1; + } + return 0; }