Kelley Nielsen
2013-Nov-05 03:28 UTC
[PATCH v2 0/3] Bootstrap the btrfs_find_item interface
There are many btrfs functions that manually search the tree for an item. They all reimplement the same mechanism and differ in the conditions that they use to find the item. Zach Brown proposed creating a new core interface, btrfs_find_item, take the place of these functions, and standardize the search functionality. This patchset takes the first steps toward the implementation of this core interface. Create a starting point for the interface by moving one of the search functions, __inode_item, to ctree.c and renaming it with the core function name. With the next two patches, eliminate one similar helper function each by replacing their callers with calls to the new core function, and modifying the new core function to ensure it fulfills the purpose of the function being replaced. Kelley Nielsen (3): Bootstrap generic btrfs_find_item interface expand btrfs_find_item() to include find_root_ref functionality expand btrfs_find_item() to include find_orphan_item functionality fs/btrfs/backref.c | 40 ++++------------------------------------ fs/btrfs/ctree.c | 43 +++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ctree.h | 2 ++ fs/btrfs/disk-io.c | 3 ++- fs/btrfs/inode.c | 6 +++--- fs/btrfs/orphan.c | 20 -------------------- fs/btrfs/root-tree.c | 15 --------------- fs/btrfs/tree-log.c | 3 ++- 8 files changed, 56 insertions(+), 76 deletions(-) -- 1.8.1.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Kelley Nielsen
2013-Nov-05 03:33 UTC
[PATCH v2 1/3] Bootstrap generic btrfs_find_item interface
There are many btrfs functions that manually search the tree for an item. They all reimplement the same mechanism and differ in the conditions that they use to find the item. __inode_info() is one such example. Zach Brown proposed creating a new interface to take the place of these functions. This patch is the first step to creating the interface. A new function, btrfs_find_item, has been added to ctree.c and prototyped in ctree.h. It is identical to __inode_info, except that the order of the parameters has been rearranged to more closely those of similar functions elsewhere in the code (now, root and path come first, then the objectid, offset and type, and the key to be filled in last). __inode_info''s callers have been set to call this new function instead, and __inode_info itself has been removed. Signed-off-by: Kelley Nielsen <kelleynnn@gmail.com> Suggested-by: Zach Brown <zab@redhat.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org> --- Changes since v1: * reworded commit message to use the imperative rather than passive voice, and to mention Zach Brown in the body as well as in the suggested-by line * Changed parameter inum to iobjectid in btrfs_find_item() * Added a space before __inode_info() in the commit message fs/btrfs/backref.c | 40 ++++------------------------------------ fs/btrfs/ctree.c | 37 +++++++++++++++++++++++++++++++++++++ fs/btrfs/ctree.h | 2 ++ 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c index 3775947..595bd1f 100644 --- a/fs/btrfs/backref.c +++ b/fs/btrfs/backref.c @@ -1107,38 +1107,6 @@ int btrfs_find_all_roots(struct btrfs_trans_handle *trans, return 0; } - -static int __inode_info(u64 inum, u64 ioff, u8 key_type, - struct btrfs_root *fs_root, struct btrfs_path *path, - struct btrfs_key *found_key) -{ - int ret; - struct btrfs_key key; - struct extent_buffer *eb; - - key.type = key_type; - key.objectid = inum; - key.offset = ioff; - - ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); - if (ret < 0) - return ret; - - eb = path->nodes[0]; - if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { - ret = btrfs_next_leaf(fs_root, path); - if (ret) - return ret; - eb = path->nodes[0]; - } - - btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); - if (found_key->type != key.type || found_key->objectid != key.objectid) - return 1; - - return 0; -} - /* * this makes the path point to (inum INODE_ITEM ioff) */ @@ -1146,16 +1114,16 @@ int inode_item_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, struct btrfs_path *path) { struct btrfs_key key; - return __inode_info(inum, ioff, BTRFS_INODE_ITEM_KEY, fs_root, path, - &key); + return btrfs_find_item(fs_root, path, inum, ioff, + BTRFS_INODE_ITEM_KEY, &key); } static int inode_ref_info(u64 inum, u64 ioff, struct btrfs_root *fs_root, struct btrfs_path *path, struct btrfs_key *found_key) { - return __inode_info(inum, ioff, BTRFS_INODE_REF_KEY, fs_root, path, - found_key); + return btrfs_find_item(fs_root, path, inum, ioff, + BTRFS_INODE_REF_KEY, found_key); } int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 316136b..5969473 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -2462,6 +2462,43 @@ static int key_search(struct extent_buffer *b, struct btrfs_key *key, return 0; } +/* Proposed generic search function, meant to take the place of the +* various small search helper functions throughout the code and standardize +* the search interface. Right now, it only replaces the former __inode_info +* in backref.c. +*/ +int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, + u64 iobjectid, u64 ioff, u8 key_type, + struct btrfs_key *found_key) +{ + int ret; + struct btrfs_key key; + struct extent_buffer *eb; + + key.type = key_type; + key.objectid = iobjectid; + key.offset = ioff; + + ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); + if (ret < 0) + return ret; + + eb = path->nodes[0]; + if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { + ret = btrfs_next_leaf(fs_root, path); + if (ret) + return ret; + eb = path->nodes[0]; + } + + btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); + if (found_key->type != key.type || + found_key->objectid != key.objectid) + return 1; + + return 0; +} + /* * look for key in the tree. path is filled in with nodes along the way * if key is found, we return zero and you can find the item in the leaf diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index f013a31..0c337f5 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -3357,6 +3357,8 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, struct btrfs_key *new_key); +int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, + u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key); int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *key, struct btrfs_path *p, int ins_len, int cow); -- 1.8.1.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Kelley Nielsen
2013-Nov-05 03:35 UTC
[PATCH v2 2/3] expand btrfs_find_item() to include find_root_ref functionality
This patch is the second step in bootstrapping the btrfs_find_item interface. The btrfs_find_root_ref() is similar to the former __inode_info(); it accepts four of its parameters, and duplicates the first half of its functionality. Replace the one former call to btrfs_find_root_ref() with a call to btrfs_find_item(), along with the defined key type that was used internally by btrfs_find_root ref, and a null found key. In btrfs_find_item(), add a test for the null key at the place where the functionality of btrfs_find_root_ref() ends; btrfs_find_item() then returns if the test passes. Finally, remove btrfs_find_root_ref(). Signed-off-by: Kelley Nielsen <kelleynnn@gmail.com> Suggested-by: Zach Brown <zab@redhat.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org> --- Changes since v1: * reworded the commit message to use the imperative form instead of the passive voice, and to mention Zach Brown in the body fs/btrfs/ctree.c | 10 ++++++++-- fs/btrfs/inode.c | 6 +++--- fs/btrfs/root-tree.c | 15 --------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 5969473..7d2f71c 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -2465,7 +2465,13 @@ static int key_search(struct extent_buffer *b, struct btrfs_key *key, /* Proposed generic search function, meant to take the place of the * various small search helper functions throughout the code and standardize * the search interface. Right now, it only replaces the former __inode_info -* in backref.c. +* in backref.c, and the former btrfs_find_root_ref in root-tree.c. +* +* If a null key is passed, it returns immediately after running +* btrfs_search_slot, leaving the path filled as it is and passing its +* return value upward. If a real key is passed, it will set the caller''s +* path to point to the first item in the tree after its specified +* objectid, type, and offset for which objectid and type match the input. */ int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, u64 iobjectid, u64 ioff, u8 key_type, @@ -2480,7 +2486,7 @@ int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, key.offset = ioff; ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); - if (ret < 0) + if ((ret < 0) || (found_key == NULL)) return ret; eb = path->nodes[0]; diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index f167ced..27ee49b 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4672,9 +4672,9 @@ static int fixup_tree_root_location(struct btrfs_root *root, } err = -ENOENT; - ret = btrfs_find_root_ref(root->fs_info->tree_root, path, - BTRFS_I(dir)->root->root_key.objectid, - location->objectid); + ret = btrfs_find_item(root->fs_info->tree_root, path, + BTRFS_I(dir)->root->root_key.objectid, + location->objectid, BTRFS_ROOT_REF_KEY, NULL); if (ret) { if (ret < 0) err = ret; diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c index ec71ea4..fcc10eb 100644 --- a/fs/btrfs/root-tree.c +++ b/fs/btrfs/root-tree.c @@ -400,21 +400,6 @@ out: return err; } -int btrfs_find_root_ref(struct btrfs_root *tree_root, - struct btrfs_path *path, - u64 root_id, u64 ref_id) -{ - struct btrfs_key key; - int ret; - - key.objectid = root_id; - key.type = BTRFS_ROOT_REF_KEY; - key.offset = ref_id; - - ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); - return ret; -} - /* * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY * or BTRFS_ROOT_BACKREF_KEY. -- 1.8.1.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Kelley Nielsen
2013-Nov-05 03:37 UTC
[PATCH v2 3/3] expand btrfs_find_item() to include find_orphan_item functionality
This is the third step in bootstrapping the btrfs_find_item interface. The function find_orphan_item(), in orphan.c, is similar to the two functions already replaced by the new interface. It uses two parameters, which are already present in the interface, and is nearly identical to the function brought in in the previous patch. Replace the two calls to find_orphan_item() with calls to btrfs_find_item(), with the defined objectid and type that was used internally by find_orphan_item(), a null path, and a null key. Add a test for a null path to btrfs_find_item, and if it passes, allocate and free the path. Finally, remove find_orphan_item(). Signed-off-by: Kelley Nielsen <kelleynnn@gmail.com> --- Changes since v1: * Removed comment at the head of btrfs_find_item() * Reworded commit messasge to use imperative, not passive voice fs/btrfs/ctree.c | 26 +++++++++++++------------- fs/btrfs/disk-io.c | 3 ++- fs/btrfs/orphan.c | 20 -------------------- fs/btrfs/tree-log.c | 3 ++- 4 files changed, 17 insertions(+), 35 deletions(-) diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 7d2f71c..a528a14 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -2462,32 +2462,32 @@ static int key_search(struct extent_buffer *b, struct btrfs_key *key, return 0; } -/* Proposed generic search function, meant to take the place of the -* various small search helper functions throughout the code and standardize -* the search interface. Right now, it only replaces the former __inode_info -* in backref.c, and the former btrfs_find_root_ref in root-tree.c. -* -* If a null key is passed, it returns immediately after running -* btrfs_search_slot, leaving the path filled as it is and passing its -* return value upward. If a real key is passed, it will set the caller''s -* path to point to the first item in the tree after its specified -* objectid, type, and offset for which objectid and type match the input. -*/ -int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, +int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path, u64 iobjectid, u64 ioff, u8 key_type, struct btrfs_key *found_key) { int ret; struct btrfs_key key; struct extent_buffer *eb; + struct btrfs_path *path; key.type = key_type; key.objectid = iobjectid; key.offset = ioff; + if (found_path == NULL) { + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + } else + path = found_path; + ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); - if ((ret < 0) || (found_key == NULL)) + if ((ret < 0) || (found_key == NULL)) { + if (path != found_path) + btrfs_free_path(path); return ret; + } eb = path->nodes[0]; if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 4c4ed0b..bce90c9 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1616,7 +1616,8 @@ again: if (ret) goto fail; - ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); + ret = btrfs_find_item(fs_info->tree_root, NULL, BTRFS_ORPHAN_OBJECTID, + location->objectid, BTRFS_ORPHAN_ITEM_KEY, NULL); if (ret < 0) goto fail; if (ret == 0) diff --git a/fs/btrfs/orphan.c b/fs/btrfs/orphan.c index 24cad16..65793ed 100644 --- a/fs/btrfs/orphan.c +++ b/fs/btrfs/orphan.c @@ -69,23 +69,3 @@ out: btrfs_free_path(path); return ret; } - -int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset) -{ - struct btrfs_path *path; - struct btrfs_key key; - int ret; - - key.objectid = BTRFS_ORPHAN_OBJECTID; - key.type = BTRFS_ORPHAN_ITEM_KEY; - key.offset = offset; - - path = btrfs_alloc_path(); - if (!path) - return -ENOMEM; - - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); - - btrfs_free_path(path); - return ret; -} diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index a2c7b04..9972e2a 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -1238,7 +1238,8 @@ static int insert_orphan_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 offset) { int ret; - ret = btrfs_find_orphan_item(root, offset); + ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID, + offset, BTRFS_ORPHAN_ITEM_KEY, NULL); if (ret > 0) ret = btrfs_insert_orphan_item(trans, root, offset); return ret; -- 1.8.1.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html