Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * BTRFS filesystem implementation for U-Boot |
| 4 | * |
| 5 | * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "btrfs.h" |
Qu Wenruo | 565a414 | 2020-06-24 18:02:48 +0200 | [diff] [blame] | 9 | #include "disk-io.h" |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 10 | |
Qu Wenruo | c921aa2 | 2020-06-24 18:03:05 +0200 | [diff] [blame] | 11 | static int verify_dir_item(struct btrfs_root *root, |
| 12 | struct extent_buffer *leaf, |
| 13 | struct btrfs_dir_item *dir_item) |
| 14 | { |
| 15 | u16 namelen = BTRFS_NAME_LEN; |
| 16 | u8 type = btrfs_dir_type(leaf, dir_item); |
| 17 | |
| 18 | if (type == BTRFS_FT_XATTR) |
| 19 | namelen = XATTR_NAME_MAX; |
| 20 | |
| 21 | if (btrfs_dir_name_len(leaf, dir_item) > namelen) { |
| 22 | fprintf(stderr, "invalid dir item name len: %u\n", |
| 23 | (unsigned)btrfs_dir_data_len(leaf, dir_item)); |
| 24 | return 1; |
| 25 | } |
| 26 | |
| 27 | /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */ |
| 28 | if ((btrfs_dir_data_len(leaf, dir_item) + |
| 29 | btrfs_dir_name_len(leaf, dir_item)) > |
| 30 | BTRFS_MAX_XATTR_SIZE(root->fs_info)) { |
| 31 | fprintf(stderr, "invalid dir item name + data len: %u + %u\n", |
| 32 | (unsigned)btrfs_dir_name_len(leaf, dir_item), |
| 33 | (unsigned)btrfs_dir_data_len(leaf, dir_item)); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, |
| 41 | struct btrfs_path *path, |
| 42 | const char *name, int name_len) |
| 43 | { |
| 44 | struct btrfs_dir_item *dir_item; |
| 45 | unsigned long name_ptr; |
| 46 | u32 total_len; |
| 47 | u32 cur = 0; |
| 48 | u32 this_len; |
| 49 | struct extent_buffer *leaf; |
| 50 | |
| 51 | leaf = path->nodes[0]; |
| 52 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
| 53 | total_len = btrfs_item_size_nr(leaf, path->slots[0]); |
| 54 | if (verify_dir_item(root, leaf, dir_item)) |
| 55 | return NULL; |
| 56 | |
| 57 | while(cur < total_len) { |
| 58 | this_len = sizeof(*dir_item) + |
| 59 | btrfs_dir_name_len(leaf, dir_item) + |
| 60 | btrfs_dir_data_len(leaf, dir_item); |
| 61 | if (this_len > (total_len - cur)) { |
| 62 | fprintf(stderr, "invalid dir item size\n"); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | name_ptr = (unsigned long)(dir_item + 1); |
| 67 | |
| 68 | if (btrfs_dir_name_len(leaf, dir_item) == name_len && |
| 69 | memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
| 70 | return dir_item; |
| 71 | |
| 72 | cur += this_len; |
| 73 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + |
| 74 | this_len); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
| 80 | struct btrfs_root *root, |
| 81 | struct btrfs_path *path, u64 dir, |
| 82 | const char *name, int name_len, |
| 83 | int mod) |
| 84 | { |
| 85 | int ret; |
| 86 | struct btrfs_key key; |
| 87 | int ins_len = mod < 0 ? -1 : 0; |
| 88 | int cow = mod != 0; |
| 89 | struct btrfs_key found_key; |
| 90 | struct extent_buffer *leaf; |
| 91 | |
| 92 | key.objectid = dir; |
| 93 | key.type = BTRFS_DIR_ITEM_KEY; |
| 94 | |
| 95 | key.offset = btrfs_name_hash(name, name_len); |
| 96 | |
| 97 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 98 | if (ret < 0) |
| 99 | return ERR_PTR(ret); |
| 100 | if (ret > 0) { |
| 101 | if (path->slots[0] == 0) |
| 102 | return NULL; |
| 103 | path->slots[0]--; |
| 104 | } |
| 105 | |
| 106 | leaf = path->nodes[0]; |
| 107 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 108 | |
| 109 | if (found_key.objectid != dir || |
| 110 | found_key.type != BTRFS_DIR_ITEM_KEY || |
| 111 | found_key.offset != key.offset) |
| 112 | return NULL; |
| 113 | |
| 114 | return btrfs_match_dir_item_name(root, path, name, name_len); |
| 115 | } |
| 116 | |
Qu Wenruo | cafffc5 | 2020-06-24 18:03:02 +0200 | [diff] [blame] | 117 | static int __verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total) |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 118 | { |
| 119 | u16 max_len = BTRFS_NAME_LEN; |
| 120 | u32 end; |
| 121 | |
| 122 | if (item->type >= BTRFS_FT_MAX) { |
| 123 | printf("%s: invalid dir item type: %i\n", __func__, item->type); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | if (item->type == BTRFS_FT_XATTR) |
| 128 | max_len = 255; /* XATTR_NAME_MAX */ |
| 129 | |
| 130 | end = start + sizeof(*item) + item->name_len; |
| 131 | if (item->name_len > max_len || end > total) { |
| 132 | printf("%s: invalid dir item name len: %u\n", __func__, |
| 133 | item->name_len); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static struct btrfs_dir_item * |
Qu Wenruo | cafffc5 | 2020-06-24 18:03:02 +0200 | [diff] [blame] | 141 | __btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name, |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 142 | int name_len) |
| 143 | { |
| 144 | struct btrfs_dir_item *item; |
| 145 | u32 total_len, cur = 0, this_len; |
| 146 | const char *name_ptr; |
| 147 | |
| 148 | item = btrfs_path_item_ptr(path, struct btrfs_dir_item); |
| 149 | |
| 150 | total_len = btrfs_path_item_size(path); |
| 151 | |
| 152 | while (cur < total_len) { |
| 153 | btrfs_dir_item_to_cpu(item); |
| 154 | this_len = sizeof(*item) + item->name_len + item->data_len; |
| 155 | name_ptr = (const char *) (item + 1); |
| 156 | |
Qu Wenruo | cafffc5 | 2020-06-24 18:03:02 +0200 | [diff] [blame] | 157 | if (__verify_dir_item(item, cur, total_len)) |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 158 | return NULL; |
| 159 | if (item->name_len == name_len && !memcmp(name_ptr, name, |
| 160 | name_len)) |
| 161 | return item; |
| 162 | |
| 163 | cur += this_len; |
| 164 | item = (struct btrfs_dir_item *) ((u8 *) item + this_len); |
| 165 | } |
| 166 | |
| 167 | return NULL; |
| 168 | } |
| 169 | |
Qu Wenruo | cafffc5 | 2020-06-24 18:03:02 +0200 | [diff] [blame] | 170 | int __btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir, |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 171 | const char *name, int name_len, |
| 172 | struct btrfs_dir_item *item) |
| 173 | { |
Qu Wenruo | 33966de | 2020-06-24 18:02:56 +0200 | [diff] [blame] | 174 | struct __btrfs_path path; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 175 | struct btrfs_key key; |
| 176 | struct btrfs_dir_item *res = NULL; |
| 177 | |
| 178 | key.objectid = dir; |
| 179 | key.type = BTRFS_DIR_ITEM_KEY; |
| 180 | key.offset = btrfs_name_hash(name, name_len); |
| 181 | |
| 182 | if (btrfs_search_tree(root, &key, &path)) |
| 183 | return -1; |
| 184 | |
| 185 | if (btrfs_comp_keys_type(&key, btrfs_path_leaf_key(&path))) |
| 186 | goto out; |
| 187 | |
Qu Wenruo | cafffc5 | 2020-06-24 18:03:02 +0200 | [diff] [blame] | 188 | res = __btrfs_match_dir_item_name(&path, name, name_len); |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 189 | if (res) |
| 190 | *item = *res; |
| 191 | out: |
Qu Wenruo | 33966de | 2020-06-24 18:02:56 +0200 | [diff] [blame] | 192 | __btrfs_free_path(&path); |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 193 | return res ? 0 : -1; |
| 194 | } |
| 195 | |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 196 | int btrfs_iter_dir(struct btrfs_root *root, u64 ino, |
| 197 | btrfs_iter_dir_callback_t callback) |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 198 | { |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 199 | struct btrfs_path path; |
| 200 | struct btrfs_key key; |
| 201 | int ret; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 202 | |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 203 | btrfs_init_path(&path); |
| 204 | key.objectid = ino; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 205 | key.type = BTRFS_DIR_INDEX_KEY; |
| 206 | key.offset = 0; |
| 207 | |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 208 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 209 | if (ret < 0) |
| 210 | return ret; |
| 211 | /* Should not happen */ |
| 212 | if (ret == 0) { |
| 213 | ret = -EUCLEAN; |
| 214 | goto out; |
| 215 | } |
| 216 | if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) { |
| 217 | ret = btrfs_next_leaf(root, &path); |
| 218 | if (ret < 0) |
| 219 | goto out; |
| 220 | if (ret > 0) { |
| 221 | ret = 0; |
| 222 | goto out; |
| 223 | } |
| 224 | } |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 225 | do { |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 226 | struct btrfs_dir_item *di; |
| 227 | |
| 228 | btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); |
| 229 | if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 230 | break; |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 231 | di = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 232 | struct btrfs_dir_item); |
| 233 | if (verify_dir_item(root, path.nodes[0], di)) { |
| 234 | ret = -EUCLEAN; |
| 235 | goto out; |
| 236 | } |
| 237 | ret = callback(root, path.nodes[0], di); |
| 238 | if (ret < 0) |
| 239 | goto out; |
| 240 | } while (!(ret = btrfs_next_item(root, &path))); |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 241 | |
Qu Wenruo | 325dd1f | 2020-06-24 18:03:06 +0200 | [diff] [blame^] | 242 | if (ret > 0) |
| 243 | ret = 0; |
| 244 | out: |
| 245 | btrfs_release_path(&path); |
| 246 | return ret; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 247 | } |