Re: [PATCH 5/8] btrfs: inode: use btrfs_for_each_slot in btrfs_read_readdir
From: Nikolay Borisov <hidden>
Date: 2021-08-30 13:05:34
On 26.08.21 г. 19:40, Marcos Paulo de Souza wrote:
quoted hunk ↗ jump to hunk
The function can be simplified by using the macro. No functional changes. Signed-off-by: Marcos Paulo de Souza <redacted> --- fs/btrfs/inode.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-)diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 2aa9646bce56..12bee0107015 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c@@ -6109,8 +6109,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) struct list_head ins_list; struct list_head del_list; int ret; - struct extent_buffer *leaf; - int slot; + int iter_ret; char *name_ptr; int name_len; int entries = 0;@@ -6137,35 +6136,19 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) key.offset = ctx->pos; key.objectid = btrfs_ino(BTRFS_I(inode)); - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); - if (ret < 0) - goto err; - - while (1) { + btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
I don't think it's necessary to use iter_ret, instead you can use ret directly. Because if either btrfs_search_slot return an error or btrfs_valid_slot then ret would be set to the respective return value and the body of the loop won't be executed at all, no?
quoted hunk ↗ jump to hunk
struct dir_entry *entry; + struct extent_buffer *leaf = path->nodes[0]; - leaf = path->nodes[0]; - slot = path->slots[0]; - if (slot >= btrfs_header_nritems(leaf)) { - ret = btrfs_next_leaf(root, path); - if (ret < 0) - goto err; - else if (ret > 0) - break; - continue; - } + if (found_key.objectid != key.objectid || + found_key.type != BTRFS_DIR_INDEX_KEY) + break; - btrfs_item_key_to_cpu(leaf, &found_key, slot); + if (found_key.offset < ctx->pos || + btrfs_should_delete_dir_index(&del_list, found_key.offset)) + continue; - if (found_key.objectid != key.objectid) - break; - if (found_key.type != BTRFS_DIR_INDEX_KEY) - break; - if (found_key.offset < ctx->pos) - goto next; - if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) - goto next; - di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); + di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); name_len = btrfs_dir_name_len(leaf, di); if ((total_len + sizeof(struct dir_entry) + name_len) >= PAGE_SIZE) {@@ -6192,9 +6175,14 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) entries++; addr += sizeof(struct dir_entry) + name_len; total_len += sizeof(struct dir_entry) + name_len; -next: - path->slots[0]++; } + + /* Error found while searching. */ + if (iter_ret < 0) { + ret = iter_ret; + goto err; + } + btrfs_release_path(path); ret = btrfs_filldir(private->filldir_buf, entries, ctx);