Commit 40084813 authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba

btrfs: free list element sooner at log_new_dir_dentries()

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: default avatarFilipe Manana <fdmanana@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent b96c552b
...@@ -6142,25 +6142,28 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans, ...@@ -6142,25 +6142,28 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
while (!list_empty(&dir_list)) { while (!list_empty(&dir_list)) {
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_key min_key; struct btrfs_key min_key;
u64 ino;
bool continue_curr_inode = true;
int nritems; int nritems;
int i; int i;
dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
list); list);
if (ret) ino = dir_elem->ino;
goto next_dir_inode; 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.type = BTRFS_DIR_INDEX_KEY;
min_key.offset = 0; min_key.offset = 0;
again: again:
btrfs_release_path(path); btrfs_release_path(path);
ret = btrfs_search_forward(root, &min_key, path, trans->transid); ret = btrfs_search_forward(root, &min_key, path, trans->transid);
if (ret < 0) { if (ret < 0) {
goto next_dir_inode; break;
} else if (ret > 0) { } else if (ret > 0) {
ret = 0; ret = 0;
goto next_dir_inode; continue;
} }
leaf = path->nodes[0]; leaf = path->nodes[0];
...@@ -6169,14 +6172,15 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans, ...@@ -6169,14 +6172,15 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
struct btrfs_dir_item *di; struct btrfs_dir_item *di;
struct btrfs_key di_key; struct btrfs_key di_key;
struct inode *di_inode; struct inode *di_inode;
struct btrfs_dir_list *new_dir_elem;
int log_mode = LOG_INODE_EXISTS; int log_mode = LOG_INODE_EXISTS;
int type; int type;
btrfs_item_key_to_cpu(leaf, &min_key, i); btrfs_item_key_to_cpu(leaf, &min_key, i);
if (min_key.objectid != dir_elem->ino || if (min_key.objectid != ino ||
min_key.type != BTRFS_DIR_INDEX_KEY) min_key.type != BTRFS_DIR_INDEX_KEY) {
goto next_dir_inode; continue_curr_inode = false;
break;
}
di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item); di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
type = btrfs_dir_type(leaf, di); type = btrfs_dir_type(leaf, di);
...@@ -6190,7 +6194,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans, ...@@ -6190,7 +6194,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root); di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
if (IS_ERR(di_inode)) { if (IS_ERR(di_inode)) {
ret = PTR_ERR(di_inode); ret = PTR_ERR(di_inode);
goto next_dir_inode; goto out;
} }
if (!need_log_inode(trans, BTRFS_I(di_inode))) { if (!need_log_inode(trans, BTRFS_I(di_inode))) {
...@@ -6205,29 +6209,33 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans, ...@@ -6205,29 +6209,33 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
log_mode, ctx); log_mode, ctx);
btrfs_add_delayed_iput(di_inode); btrfs_add_delayed_iput(di_inode);
if (ret) if (ret)
goto next_dir_inode; goto out;
if (ctx->log_new_dentries) { if (ctx->log_new_dentries) {
new_dir_elem = kmalloc(sizeof(*new_dir_elem), dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
GFP_NOFS); if (!dir_elem) {
if (!new_dir_elem) {
ret = -ENOMEM; ret = -ENOMEM;
goto next_dir_inode; goto out;
} }
new_dir_elem->ino = di_key.objectid; dir_elem->ino = di_key.objectid;
list_add_tail(&new_dir_elem->list, &dir_list); list_add_tail(&dir_elem->list, &dir_list);
} }
break; break;
} }
if (min_key.offset < (u64)-1) {
if (continue_curr_inode && min_key.offset < (u64)-1) {
min_key.offset++; min_key.offset++;
goto again; goto again;
} }
next_dir_inode:
list_del(&dir_elem->list);
kfree(dir_elem);
} }
out:
btrfs_free_path(path); 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; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment