Skip to content

Commit 8dcbc26

Browse files
fdmananakdave
authored andcommitted
btrfs: unify lookup return value when dir entry is missing
btrfs_lookup_dir_index_item() and btrfs_lookup_dir_item() lookup for dir entries and both are used during log replay or when updating a log tree during an unlink. However when the dir item does not exists, btrfs_lookup_dir_item() returns NULL while btrfs_lookup_dir_index_item() returns PTR_ERR(-ENOENT), and if the dir item exists but there is no matching entry for a given name or index, both return NULL. This makes the call sites during log replay to be more verbose than necessary and it makes it easy to miss this slight difference. Since we don't need to distinguish between those two cases, make btrfs_lookup_dir_index_item() always return NULL when there is no matching directory entry - either because there isn't any dir entry or because there is one but it does not match the given name and index. Also rename the argument 'objectid' of btrfs_lookup_dir_index_item() to 'index' since it is supposed to match an index number, and the name 'objectid' is not very good because it can easily be confused with an inode number (like the inode number a dir entry points to). CC: [email protected] # 4.14+ Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 52db777 commit 8dcbc26

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

fs/btrfs/ctree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,7 @@ struct btrfs_dir_item *
30303030
btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
30313031
struct btrfs_root *root,
30323032
struct btrfs_path *path, u64 dir,
3033-
u64 objectid, const char *name, int name_len,
3033+
u64 index, const char *name, int name_len,
30343034
int mod);
30353035
struct btrfs_dir_item *
30363036
btrfs_search_dir_index_item(struct btrfs_root *root,

fs/btrfs/dir-item.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,20 @@ static struct btrfs_dir_item *btrfs_lookup_match_dir(
190190
}
191191

192192
/*
193-
* lookup a directory item based on name. 'dir' is the objectid
194-
* we're searching in, and 'mod' tells us if you plan on deleting the
195-
* item (use mod < 0) or changing the options (use mod > 0)
193+
* Lookup for a directory item by name.
194+
*
195+
* @trans: The transaction handle to use. Can be NULL if @mod is 0.
196+
* @root: The root of the target tree.
197+
* @path: Path to use for the search.
198+
* @dir: The inode number (objectid) of the directory.
199+
* @name: The name associated to the directory entry we are looking for.
200+
* @name_len: The length of the name.
201+
* @mod: Used to indicate if the tree search is meant for a read only
202+
* lookup, for a modification lookup or for a deletion lookup, so
203+
* its value should be 0, 1 or -1, respectively.
204+
*
205+
* Returns: NULL if the dir item does not exists, an error pointer if an error
206+
* happened, or a pointer to a dir item if a dir item exists for the given name.
196207
*/
197208
struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
198209
struct btrfs_root *root,
@@ -273,27 +284,42 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
273284
}
274285

275286
/*
276-
* lookup a directory item based on index. 'dir' is the objectid
277-
* we're searching in, and 'mod' tells us if you plan on deleting the
278-
* item (use mod < 0) or changing the options (use mod > 0)
287+
* Lookup for a directory index item by name and index number.
279288
*
280-
* The name is used to make sure the index really points to the name you were
281-
* looking for.
289+
* @trans: The transaction handle to use. Can be NULL if @mod is 0.
290+
* @root: The root of the target tree.
291+
* @path: Path to use for the search.
292+
* @dir: The inode number (objectid) of the directory.
293+
* @index: The index number.
294+
* @name: The name associated to the directory entry we are looking for.
295+
* @name_len: The length of the name.
296+
* @mod: Used to indicate if the tree search is meant for a read only
297+
* lookup, for a modification lookup or for a deletion lookup, so
298+
* its value should be 0, 1 or -1, respectively.
299+
*
300+
* Returns: NULL if the dir index item does not exists, an error pointer if an
301+
* error happened, or a pointer to a dir item if the dir index item exists and
302+
* matches the criteria (name and index number).
282303
*/
283304
struct btrfs_dir_item *
284305
btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
285306
struct btrfs_root *root,
286307
struct btrfs_path *path, u64 dir,
287-
u64 objectid, const char *name, int name_len,
308+
u64 index, const char *name, int name_len,
288309
int mod)
289310
{
311+
struct btrfs_dir_item *di;
290312
struct btrfs_key key;
291313

292314
key.objectid = dir;
293315
key.type = BTRFS_DIR_INDEX_KEY;
294-
key.offset = objectid;
316+
key.offset = index;
295317

296-
return btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
318+
di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
319+
if (di == ERR_PTR(-ENOENT))
320+
return NULL;
321+
322+
return di;
297323
}
298324

299325
struct btrfs_dir_item *

fs/btrfs/tree-log.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,7 @@ static noinline int inode_in_dir(struct btrfs_root *root,
957957
di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
958958
index, name, name_len, 0);
959959
if (IS_ERR(di)) {
960-
if (PTR_ERR(di) != -ENOENT)
961-
ret = PTR_ERR(di);
960+
ret = PTR_ERR(di);
962961
goto out;
963962
} else if (di) {
964963
btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
@@ -1191,8 +1190,7 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
11911190
di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
11921191
ref_index, name, namelen, 0);
11931192
if (IS_ERR(di)) {
1194-
if (PTR_ERR(di) != -ENOENT)
1195-
return PTR_ERR(di);
1193+
return PTR_ERR(di);
11961194
} else if (di) {
11971195
ret = drop_one_dir_item(trans, root, path, dir, di);
11981196
if (ret)
@@ -1994,9 +1992,6 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans,
19941992
goto out;
19951993
}
19961994

1997-
if (dst_di == ERR_PTR(-ENOENT))
1998-
dst_di = NULL;
1999-
20001995
if (IS_ERR(dst_di)) {
20011996
ret = PTR_ERR(dst_di);
20021997
goto out;
@@ -2304,7 +2299,7 @@ static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
23042299
dir_key->offset,
23052300
name, name_len, 0);
23062301
}
2307-
if (!log_di || log_di == ERR_PTR(-ENOENT)) {
2302+
if (!log_di) {
23082303
btrfs_dir_item_key_to_cpu(eb, di, &location);
23092304
btrfs_release_path(path);
23102305
btrfs_release_path(log_path);
@@ -3563,8 +3558,7 @@ int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
35633558
if (err == -ENOSPC) {
35643559
btrfs_set_log_full_commit(trans);
35653560
err = 0;
3566-
} else if (err < 0 && err != -ENOENT) {
3567-
/* ENOENT can be returned if the entry hasn't been fsynced yet */
3561+
} else if (err < 0) {
35683562
btrfs_abort_transaction(trans, err);
35693563
}
35703564

0 commit comments

Comments
 (0)