Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 04/15] btrfs: free list element sooner at log_new_dir_dentries()
Date: Mon, 22 Aug 2022 11:51:33 +0100	[thread overview]
Message-ID: <bcfff559fa2ceb5d935de5f71fc5ae346bd75787.1661165149.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1661165149.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

At log_new_dir_dentries(), there's no need to keep the current list
element allocated while processing the leaves with directory items for
the current directory, and while logging other inodes. Plus in case we
find a subdirectory, we also end up allocating a new list element while
the current one is still allocated, temporarily using more memory than
necessary.

So free the current list element early on, before processing leaves.
Also make the removal and release of all list elements in case of an
error more simple by eliminating the label and goto, adding an explicit
loop to release all list elements in case an error happens.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/tree-log.c | 52 ++++++++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 22 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 9625707bfa8a..bd50509e9839 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -6002,25 +6002,28 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 	while (!list_empty(&dir_list)) {
 		struct extent_buffer *leaf;
 		struct btrfs_key min_key;
+		u64 ino;
+		bool continue_curr_inode = true;
 		int nritems;
 		int i;
 
 		dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
 					    list);
-		if (ret)
-			goto next_dir_inode;
+		ino = dir_elem->ino;
+		list_del(&dir_elem->list);
+		kfree(dir_elem);
 
-		min_key.objectid = dir_elem->ino;
+		min_key.objectid = ino;
 		min_key.type = BTRFS_DIR_INDEX_KEY;
 		min_key.offset = 0;
 again:
 		btrfs_release_path(path);
 		ret = btrfs_search_forward(root, &min_key, path, trans->transid);
 		if (ret < 0) {
-			goto next_dir_inode;
+			break;
 		} else if (ret > 0) {
 			ret = 0;
-			goto next_dir_inode;
+			continue;
 		}
 
 		leaf = path->nodes[0];
@@ -6029,14 +6032,15 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 			struct btrfs_dir_item *di;
 			struct btrfs_key di_key;
 			struct inode *di_inode;
-			struct btrfs_dir_list *new_dir_elem;
 			int log_mode = LOG_INODE_EXISTS;
 			int type;
 
 			btrfs_item_key_to_cpu(leaf, &min_key, i);
-			if (min_key.objectid != dir_elem->ino ||
-			    min_key.type != BTRFS_DIR_INDEX_KEY)
-				goto next_dir_inode;
+			if (min_key.objectid != ino ||
+			    min_key.type != BTRFS_DIR_INDEX_KEY) {
+				continue_curr_inode = false;
+				break;
+			}
 
 			di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
 			type = btrfs_dir_type(leaf, di);
@@ -6050,7 +6054,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 			di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
 			if (IS_ERR(di_inode)) {
 				ret = PTR_ERR(di_inode);
-				goto next_dir_inode;
+				goto out;
 			}
 
 			if (!need_log_inode(trans, BTRFS_I(di_inode))) {
@@ -6065,29 +6069,33 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 					      log_mode, ctx);
 			btrfs_add_delayed_iput(di_inode);
 			if (ret)
-				goto next_dir_inode;
+				goto out;
 			if (ctx->log_new_dentries) {
-				new_dir_elem = kmalloc(sizeof(*new_dir_elem),
-						       GFP_NOFS);
-				if (!new_dir_elem) {
+				dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
+				if (!dir_elem) {
 					ret = -ENOMEM;
-					goto next_dir_inode;
+					goto out;
 				}
-				new_dir_elem->ino = di_key.objectid;
-				list_add_tail(&new_dir_elem->list, &dir_list);
+				dir_elem->ino = di_key.objectid;
+				list_add_tail(&dir_elem->list, &dir_list);
 			}
 			break;
 		}
-		if (min_key.offset < (u64)-1) {
+
+		if (continue_curr_inode && min_key.offset < (u64)-1) {
 			min_key.offset++;
 			goto again;
 		}
-next_dir_inode:
-		list_del(&dir_elem->list);
-		kfree(dir_elem);
 	}
-
+out:
 	btrfs_free_path(path);
+	if (ret) {
+		struct btrfs_dir_list *next;
+
+		list_for_each_entry_safe(dir_elem, next, &dir_list, list)
+			kfree(dir_elem);
+	}
+
 	return ret;
 }
 
-- 
2.35.1


  parent reply	other threads:[~2022-08-22 10:51 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-17 11:22 [PATCH 00/15] btrfs: some updates to delayed items and inode logging fdmanana
2022-08-17 11:22 ` [PATCH 01/15] btrfs: don't drop dir index range items when logging a directory fdmanana
2022-08-17 11:22 ` [PATCH 02/15] btrfs: remove the root argument from log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 03/15] btrfs: update stale comment for log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 04/15] btrfs: free list element sooner at log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 05/15] btrfs: avoid memory allocation at log_new_dir_dentries() for common case fdmanana
2022-08-17 11:22 ` [PATCH 06/15] btrfs: remove root argument from btrfs_delayed_item_reserve_metadata() fdmanana
2022-08-17 11:22 ` [PATCH 07/15] btrfs: store index number instead of key in struct btrfs_delayed_item fdmanana
2022-08-17 11:22 ` [PATCH 08/15] btrfs: remove unused logic when looking up delayed items fdmanana
2022-08-17 11:22 ` [PATCH 09/15] btrfs: shrink the size of struct btrfs_delayed_item fdmanana
2022-08-22 13:43   ` David Sterba
2022-08-22 14:15     ` Filipe Manana
2022-08-22 15:29       ` David Sterba
2022-08-22 16:00         ` Filipe Manana
2022-08-17 11:22 ` [PATCH 10/15] btrfs: search for last logged dir index if it's not cached in the inode fdmanana
2022-08-17 11:22 ` [PATCH 11/15] btrfs: move need_log_inode() to above log_conflicting_inodes() fdmanana
2022-08-17 11:22 ` [PATCH 12/15] btrfs: move log_new_dir_dentries() above btrfs_log_inode() fdmanana
2022-08-17 11:22 ` [PATCH 13/15] btrfs: log conflicting inodes without holding log mutex of the initial inode fdmanana
2022-08-17 11:22 ` [PATCH 14/15] btrfs: skip logging parent dir when conflicting inode is not a dir fdmanana
2022-08-17 11:22 ` [PATCH 15/15] btrfs: use delayed items when logging a directory fdmanana
2022-08-22 10:51 ` [PATCH v2 00/15] btrfs: some updates to delayed items and inode logging fdmanana
2022-08-22 10:51   ` [PATCH v2 01/15] btrfs: don't drop dir index range items when logging a directory fdmanana
2022-08-22 10:51   ` [PATCH v2 02/15] btrfs: remove the root argument from log_new_dir_dentries() fdmanana
2022-08-22 10:51   ` [PATCH v2 03/15] btrfs: update stale comment for log_new_dir_dentries() fdmanana
2022-08-22 10:51   ` fdmanana [this message]
2022-08-22 10:51   ` [PATCH v2 05/15] btrfs: avoid memory allocation at log_new_dir_dentries() for common case fdmanana
2022-08-22 10:51   ` [PATCH v2 06/15] btrfs: remove root argument from btrfs_delayed_item_reserve_metadata() fdmanana
2022-08-22 10:51   ` [PATCH v2 07/15] btrfs: store index number instead of key in struct btrfs_delayed_item fdmanana
2022-08-22 10:51   ` [PATCH v2 08/15] btrfs: remove unused logic when looking up delayed items fdmanana
2022-08-22 10:51   ` [PATCH v2 09/15] btrfs: shrink the size of struct btrfs_delayed_item fdmanana
2022-08-22 10:51   ` [PATCH v2 10/15] btrfs: search for last logged dir index if it's not cached in the inode fdmanana
2022-08-22 10:51   ` [PATCH v2 11/15] btrfs: move need_log_inode() to above log_conflicting_inodes() fdmanana
2022-08-22 10:51   ` [PATCH v2 12/15] btrfs: move log_new_dir_dentries() above btrfs_log_inode() fdmanana
2022-08-22 10:51   ` [PATCH v2 13/15] btrfs: log conflicting inodes without holding log mutex of the initial inode fdmanana
2022-08-22 10:51   ` [PATCH v2 14/15] btrfs: skip logging parent dir when conflicting inode is not a dir fdmanana
2022-08-22 10:51   ` [PATCH v2 15/15] btrfs: use delayed items when logging a directory fdmanana
2022-08-22 15:45   ` [PATCH v2 00/15] btrfs: some updates to delayed items and inode logging David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bcfff559fa2ceb5d935de5f71fc5ae346bd75787.1661165149.git.fdmanana@suse.com \
    --to=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox