fs: btrfs: Use btrfs_iter_dir() to replace btrfs_readdir()

Use extent buffer based infrastructure to re-implement btrfs_readdir().

Along this rework, some small corner cases fixed:
- Subvolume tree mtime
  Mtime of a subvolume tree is recorded in its root item, since there is
  no INODE_ITEM for it.
  This needs extra search from tree root.

- Output the unknown type
  If the DIR_ITEM is corrupted, at least don't try to access the memory
  out of boundary.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
This commit is contained in:
Qu Wenruo 2020-06-24 18:03:06 +02:00 committed by Tom Rini
parent c921aa20c3
commit 325dd1f642
4 changed files with 145 additions and 92 deletions

View File

@ -16,67 +16,102 @@
struct btrfs_info btrfs_info; struct btrfs_info btrfs_info;
struct btrfs_fs_info *current_fs_info; struct btrfs_fs_info *current_fs_info;
static int readdir_callback(const struct __btrfs_root *root, static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,
struct btrfs_dir_item *item) struct btrfs_dir_item *di)
{ {
static const char typestr[BTRFS_FT_MAX][4] = { struct btrfs_fs_info *fs_info = root->fs_info;
[BTRFS_FT_UNKNOWN] = " ? ", struct btrfs_inode_item ii;
[BTRFS_FT_REG_FILE] = " ", struct btrfs_key key;
static const char* dir_item_str[] = {
[BTRFS_FT_REG_FILE] = "FILE",
[BTRFS_FT_DIR] = "DIR", [BTRFS_FT_DIR] = "DIR",
[BTRFS_FT_CHRDEV] = "CHR", [BTRFS_FT_CHRDEV] = "CHRDEV",
[BTRFS_FT_BLKDEV] = "BLK", [BTRFS_FT_BLKDEV] = "BLKDEV",
[BTRFS_FT_FIFO] = "FIF", [BTRFS_FT_FIFO] = "FIFO",
[BTRFS_FT_SOCK] = "SCK", [BTRFS_FT_SOCK] = "SOCK",
[BTRFS_FT_SYMLINK] = "SYM", [BTRFS_FT_SYMLINK] = "SYMLINK",
[BTRFS_FT_XATTR] = " ? ", [BTRFS_FT_XATTR] = "XATTR"
}; };
struct btrfs_inode_item inode; u8 type = btrfs_dir_type(eb, di);
const char *name = (const char *) (item + 1); char namebuf[BTRFS_NAME_LEN];
char filetime[32], *target = NULL; char *target = NULL;
char filetime[32];
time_t mtime; time_t mtime;
int ret;
if (__btrfs_lookup_inode(root, (struct btrfs_key *)&item->location, btrfs_dir_item_key_to_cpu(eb, di, &key);
&inode, NULL)) {
printf("%s: Cannot find inode item for directory entry %.*s!\n", if (key.type == BTRFS_ROOT_ITEM_KEY) {
__func__, item->name_len, name); struct btrfs_root *subvol;
return 0;
/* It's a subvolume, get its mtime from root item */
subvol = btrfs_read_fs_root(fs_info, &key);
if (IS_ERR(subvol)) {
ret = PTR_ERR(subvol);
error("Can't find root %llu", key.objectid);
return ret;
} }
mtime = btrfs_stack_timespec_sec(&subvol->root_item.otime);
} else {
struct btrfs_path path;
mtime = inode.mtime.sec; /* It's regular inode, get its mtime from inode item */
btrfs_init_path(&path);
ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
if (ret > 0)
ret = -ENOENT;
if (ret < 0) {
error("Can't find inode %llu", key.objectid);
btrfs_release_path(&path);
return ret;
}
read_extent_buffer(path.nodes[0], &ii,
btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
sizeof(ii));
btrfs_release_path(&path);
mtime = btrfs_stack_timespec_sec(&ii.mtime);
}
ctime_r(&mtime, filetime); ctime_r(&mtime, filetime);
if (item->type == BTRFS_FT_SYMLINK) { if (type == BTRFS_FT_SYMLINK) {
target = malloc(min(inode.size + 1, target = malloc(fs_info->sectorsize);
(u64) btrfs_info.sb.sectorsize)); if (!target) {
error("Can't alloc memory for symlink %llu",
if (target && __btrfs_readlink(root, item->location.objectid, key.objectid);
target)) { return -ENOMEM;
free(target); }
target = NULL; ret = btrfs_readlink(root, key.objectid, target);
if (ret < 0) {
error("Failed to read symlink %llu", key.objectid);
goto out;
}
target[ret] = '\0';
} }
if (!target) if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
printf("%s: Cannot read symlink target!\n", __func__); printf("<%s> ", dir_item_str[type]);
}
printf("<%s> ", typestr[item->type]);
if (item->type == BTRFS_FT_CHRDEV || item->type == BTRFS_FT_BLKDEV)
printf("%4u,%5u ", (unsigned int) (inode.rdev >> 20),
(unsigned int) (inode.rdev & 0xfffff));
else else
printf("%10llu ", inode.size); printf("DIR_ITEM.%u", type);
if (type == BTRFS_FT_CHRDEV || type == BTRFS_FT_BLKDEV) {
printf("%24.24s %.*s", filetime, item->name_len, name); ASSERT(key.type == BTRFS_INODE_ITEM_KEY);
printf("%4llu,%5llu ", btrfs_stack_inode_rdev(&ii) >> 20,
if (item->type == BTRFS_FT_SYMLINK) { btrfs_stack_inode_rdev(&ii) & 0xfffff);
printf(" -> %s", target ? target : "?"); } else {
if (target) if (key.type == BTRFS_INODE_ITEM_KEY)
free(target); printf("%10llu ", btrfs_stack_inode_size(&ii));
else
printf("%10llu ", 0ULL);
} }
read_extent_buffer(eb, namebuf, (unsigned long)(di + 1),
btrfs_dir_name_len(eb, di));
printf("%24.24s %.*s", filetime, btrfs_dir_name_len(eb, di), namebuf);
if (type == BTRFS_FT_SYMLINK)
printf(" -> %s", target ? target : "?");
printf("\n"); printf("\n");
out:
return 0; free(target);
return ret;
} }
int btrfs_probe(struct blk_desc *fs_dev_desc, int btrfs_probe(struct blk_desc *fs_dev_desc,
@ -125,27 +160,29 @@ int btrfs_probe(struct blk_desc *fs_dev_desc,
int btrfs_ls(const char *path) int btrfs_ls(const char *path)
{ {
struct __btrfs_root root = btrfs_info.fs_root; struct btrfs_fs_info *fs_info = current_fs_info;
u64 inr; struct btrfs_root *root = fs_info->fs_root;
u64 ino = BTRFS_FIRST_FREE_OBJECTID;
u8 type; u8 type;
int ret;
inr = __btrfs_lookup_path(&root, root.root_dirid, path, &type, NULL, 40); ASSERT(fs_info);
ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
if (inr == -1ULL) { path, &root, &ino, &type, 40);
if (ret < 0) {
printf("Cannot lookup path %s\n", path); printf("Cannot lookup path %s\n", path);
return -1; return ret;
} }
if (type != BTRFS_FT_DIR) { if (type != BTRFS_FT_DIR) {
printf("Not a directory: %s\n", path); error("Not a directory: %s", path);
return -1; return -ENOENT;
} }
ret = btrfs_iter_dir(root, ino, show_dir);
if (btrfs_readdir(&root, inr, readdir_callback)) { if (ret < 0) {
printf("An error occured while listing directory %s\n", path); error("An error occured while listing directory %s", path);
return -1; return ret;
} }
return 0; return 0;
} }

View File

@ -43,13 +43,8 @@ u32 btrfs_decompress(u8 type, const char *, u32, char *, u32);
int btrfs_read_superblock(void); int btrfs_read_superblock(void);
/* dir-item.c */ /* dir-item.c */
typedef int (*btrfs_readdir_callback_t)(const struct __btrfs_root *,
struct btrfs_dir_item *);
int __btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int, int __btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int,
struct btrfs_dir_item *); struct btrfs_dir_item *);
int btrfs_readdir(const struct __btrfs_root *, u64, btrfs_readdir_callback_t);
/* root.c */ /* root.c */
int btrfs_find_root(u64, struct __btrfs_root *, struct btrfs_root_item *); int btrfs_find_root(u64, struct __btrfs_root *, struct btrfs_root_item *);
u64 btrfs_lookup_root_ref(u64, struct btrfs_root_ref *, char *); u64 btrfs_lookup_root_ref(u64, struct btrfs_root_ref *, char *);

View File

@ -1285,6 +1285,11 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
struct btrfs_path *path, u64 dir, struct btrfs_path *path, u64 dir,
const char *name, int name_len, const char *name, int name_len,
int mod); int mod);
typedef int (*btrfs_iter_dir_callback_t)(struct btrfs_root *root,
struct extent_buffer *eb,
struct btrfs_dir_item *di);
int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
btrfs_iter_dir_callback_t callback);
/* inode.c */ /* inode.c */
int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename, int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
struct btrfs_root **root_ret, u64 *ino_ret, struct btrfs_root **root_ret, u64 *ino_ret,

View File

@ -193,39 +193,55 @@ out:
return res ? 0 : -1; return res ? 0 : -1;
} }
int btrfs_readdir(const struct __btrfs_root *root, u64 dir, int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
btrfs_readdir_callback_t callback) btrfs_iter_dir_callback_t callback)
{ {
struct __btrfs_path path; struct btrfs_path path;
struct btrfs_key key, *found_key; struct btrfs_key key;
struct btrfs_dir_item *item; int ret;
int res = 0;
key.objectid = dir; btrfs_init_path(&path);
key.objectid = ino;
key.type = BTRFS_DIR_INDEX_KEY; key.type = BTRFS_DIR_INDEX_KEY;
key.offset = 0; key.offset = 0;
if (btrfs_search_tree(root, &key, &path)) ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
return -1; if (ret < 0)
return ret;
do { /* Should not happen */
found_key = btrfs_path_leaf_key(&path); if (ret == 0) {
if (btrfs_comp_keys_type(&key, found_key)) ret = -EUCLEAN;
break; goto out;
}
item = btrfs_path_item_ptr(&path, struct btrfs_dir_item); if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
btrfs_dir_item_to_cpu(item); ret = btrfs_next_leaf(root, &path);
if (ret < 0)
if (__verify_dir_item(item, 0, sizeof(*item) + item->name_len)) goto out;
continue; if (ret > 0) {
if (item->type == BTRFS_FT_XATTR) ret = 0;
continue; goto out;
}
if (callback(root, item)) }
break; do {
} while (!(res = btrfs_next_slot(&path))); struct btrfs_dir_item *di;
__btrfs_free_path(&path); btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY)
return res < 0 ? -1 : 0; break;
di = btrfs_item_ptr(path.nodes[0], path.slots[0],
struct btrfs_dir_item);
if (verify_dir_item(root, path.nodes[0], di)) {
ret = -EUCLEAN;
goto out;
}
ret = callback(root, path.nodes[0], di);
if (ret < 0)
goto out;
} while (!(ret = btrfs_next_item(root, &path)));
if (ret > 0)
ret = 0;
out:
btrfs_release_path(&path);
return ret;
} }