blob: aea621c72bb3eb5d81a092fce669b83ce1b61701 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún21a14fa2017-09-03 17:00:28 +02002/*
3 * BTRFS filesystem implementation for U-Boot
4 *
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
Marek Behún21a14fa2017-09-03 17:00:28 +02006 */
7
8#include "btrfs.h"
Qu Wenruo565a4142020-06-24 18:02:48 +02009#include "disk-io.h"
Marek Behún21a14fa2017-09-03 17:00:28 +020010
Qu Wenruocafffc52020-06-24 18:03:02 +020011static int __verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
Marek Behún21a14fa2017-09-03 17:00:28 +020012{
13 u16 max_len = BTRFS_NAME_LEN;
14 u32 end;
15
16 if (item->type >= BTRFS_FT_MAX) {
17 printf("%s: invalid dir item type: %i\n", __func__, item->type);
18 return 1;
19 }
20
21 if (item->type == BTRFS_FT_XATTR)
22 max_len = 255; /* XATTR_NAME_MAX */
23
24 end = start + sizeof(*item) + item->name_len;
25 if (item->name_len > max_len || end > total) {
26 printf("%s: invalid dir item name len: %u\n", __func__,
27 item->name_len);
28 return 1;
29 }
30
31 return 0;
32}
33
34static struct btrfs_dir_item *
Qu Wenruocafffc52020-06-24 18:03:02 +020035__btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
Marek Behún21a14fa2017-09-03 17:00:28 +020036 int name_len)
37{
38 struct btrfs_dir_item *item;
39 u32 total_len, cur = 0, this_len;
40 const char *name_ptr;
41
42 item = btrfs_path_item_ptr(path, struct btrfs_dir_item);
43
44 total_len = btrfs_path_item_size(path);
45
46 while (cur < total_len) {
47 btrfs_dir_item_to_cpu(item);
48 this_len = sizeof(*item) + item->name_len + item->data_len;
49 name_ptr = (const char *) (item + 1);
50
Qu Wenruocafffc52020-06-24 18:03:02 +020051 if (__verify_dir_item(item, cur, total_len))
Marek Behún21a14fa2017-09-03 17:00:28 +020052 return NULL;
53 if (item->name_len == name_len && !memcmp(name_ptr, name,
54 name_len))
55 return item;
56
57 cur += this_len;
58 item = (struct btrfs_dir_item *) ((u8 *) item + this_len);
59 }
60
61 return NULL;
62}
63
Qu Wenruocafffc52020-06-24 18:03:02 +020064int __btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir,
Marek Behún21a14fa2017-09-03 17:00:28 +020065 const char *name, int name_len,
66 struct btrfs_dir_item *item)
67{
Qu Wenruo33966de2020-06-24 18:02:56 +020068 struct __btrfs_path path;
Marek Behún21a14fa2017-09-03 17:00:28 +020069 struct btrfs_key key;
70 struct btrfs_dir_item *res = NULL;
71
72 key.objectid = dir;
73 key.type = BTRFS_DIR_ITEM_KEY;
74 key.offset = btrfs_name_hash(name, name_len);
75
76 if (btrfs_search_tree(root, &key, &path))
77 return -1;
78
79 if (btrfs_comp_keys_type(&key, btrfs_path_leaf_key(&path)))
80 goto out;
81
Qu Wenruocafffc52020-06-24 18:03:02 +020082 res = __btrfs_match_dir_item_name(&path, name, name_len);
Marek Behún21a14fa2017-09-03 17:00:28 +020083 if (res)
84 *item = *res;
85out:
Qu Wenruo33966de2020-06-24 18:02:56 +020086 __btrfs_free_path(&path);
Marek Behún21a14fa2017-09-03 17:00:28 +020087 return res ? 0 : -1;
88}
89
Qu Wenruo207011b2020-06-24 18:02:57 +020090int btrfs_readdir(const struct __btrfs_root *root, u64 dir,
Marek Behún21a14fa2017-09-03 17:00:28 +020091 btrfs_readdir_callback_t callback)
92{
Qu Wenruo33966de2020-06-24 18:02:56 +020093 struct __btrfs_path path;
Marek Behún21a14fa2017-09-03 17:00:28 +020094 struct btrfs_key key, *found_key;
95 struct btrfs_dir_item *item;
Marek Behúnecab8812017-10-06 15:04:57 +020096 int res = 0;
Marek Behún21a14fa2017-09-03 17:00:28 +020097
98 key.objectid = dir;
99 key.type = BTRFS_DIR_INDEX_KEY;
100 key.offset = 0;
101
102 if (btrfs_search_tree(root, &key, &path))
103 return -1;
104
105 do {
106 found_key = btrfs_path_leaf_key(&path);
107 if (btrfs_comp_keys_type(&key, found_key))
108 break;
109
110 item = btrfs_path_item_ptr(&path, struct btrfs_dir_item);
111 btrfs_dir_item_to_cpu(item);
112
Qu Wenruocafffc52020-06-24 18:03:02 +0200113 if (__verify_dir_item(item, 0, sizeof(*item) + item->name_len))
Marek Behún21a14fa2017-09-03 17:00:28 +0200114 continue;
115 if (item->type == BTRFS_FT_XATTR)
116 continue;
117
118 if (callback(root, item))
119 break;
120 } while (!(res = btrfs_next_slot(&path)));
121
Qu Wenruo33966de2020-06-24 18:02:56 +0200122 __btrfs_free_path(&path);
Marek Behún21a14fa2017-09-03 17:00:28 +0200123
124 return res < 0 ? -1 : 0;
125}