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 | |
Qu Wenruo | a26a6be | 2020-06-24 18:03:09 +0200 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
Qu Wenruo | a26a6be | 2020-06-24 18:03:09 +0200 | [diff] [blame] | 10 | #include <memalign.h> |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 11 | #include "btrfs.h" |
| 12 | #include "disk-io.h" |
Qu Wenruo | a26a6be | 2020-06-24 18:03:09 +0200 | [diff] [blame] | 13 | #include "volumes.h" |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 14 | |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 15 | /* |
| 16 | * Read the content of symlink inode @ino of @root, into @target. |
| 17 | * NOTE: @target will not be \0 termiated, caller should handle it properly. |
| 18 | * |
| 19 | * Return the number of read data. |
| 20 | * Return <0 for error. |
| 21 | */ |
| 22 | int btrfs_readlink(struct btrfs_root *root, u64 ino, char *target) |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 23 | { |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 24 | struct btrfs_path path; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 25 | struct btrfs_key key; |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 26 | struct btrfs_file_extent_item *fi; |
| 27 | int ret; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 28 | |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 29 | key.objectid = ino; |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 30 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 31 | key.offset = 0; |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 32 | btrfs_init_path(&path); |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 33 | |
Qu Wenruo | 92bc179 | 2020-06-24 18:03:03 +0200 | [diff] [blame] | 34 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 35 | if (ret < 0) |
| 36 | return ret; |
| 37 | if (ret > 0) { |
| 38 | ret = -ENOENT; |
| 39 | goto out; |
| 40 | } |
| 41 | fi = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 42 | struct btrfs_file_extent_item); |
| 43 | if (btrfs_file_extent_type(path.nodes[0], fi) != |
| 44 | BTRFS_FILE_EXTENT_INLINE) { |
| 45 | ret = -EUCLEAN; |
| 46 | error("Extent for symlink %llu must be INLINE type!", ino); |
| 47 | goto out; |
| 48 | } |
| 49 | if (btrfs_file_extent_compression(path.nodes[0], fi) != |
| 50 | BTRFS_COMPRESS_NONE) { |
| 51 | ret = -EUCLEAN; |
| 52 | error("Extent for symlink %llu must not be compressed!", ino); |
| 53 | goto out; |
| 54 | } |
| 55 | if (btrfs_file_extent_ram_bytes(path.nodes[0], fi) >= |
| 56 | root->fs_info->sectorsize) { |
| 57 | ret = -EUCLEAN; |
| 58 | error("Symlink %llu extent data too large (%llu)!\n", |
| 59 | ino, btrfs_file_extent_ram_bytes(path.nodes[0], fi)); |
| 60 | goto out; |
| 61 | } |
| 62 | read_extent_buffer(path.nodes[0], target, |
| 63 | btrfs_file_extent_inline_start(fi), |
| 64 | btrfs_file_extent_ram_bytes(path.nodes[0], fi)); |
| 65 | ret = btrfs_file_extent_ram_bytes(path.nodes[0], fi); |
| 66 | out: |
| 67 | btrfs_release_path(&path); |
| 68 | return ret; |
| 69 | } |
| 70 | |
Qu Wenruo | c921aa2 | 2020-06-24 18:03:05 +0200 | [diff] [blame] | 71 | static int lookup_root_ref(struct btrfs_fs_info *fs_info, |
| 72 | u64 rootid, u64 *root_ret, u64 *dir_ret) |
| 73 | { |
| 74 | struct btrfs_root *root = fs_info->tree_root; |
| 75 | struct btrfs_root_ref *root_ref; |
| 76 | struct btrfs_path path; |
| 77 | struct btrfs_key key; |
| 78 | int ret; |
| 79 | |
| 80 | btrfs_init_path(&path); |
| 81 | key.objectid = rootid; |
| 82 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 83 | key.offset = (u64)-1; |
| 84 | |
| 85 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 86 | if (ret < 0) |
| 87 | return ret; |
| 88 | /* Should not happen */ |
| 89 | if (ret == 0) { |
| 90 | ret = -EUCLEAN; |
| 91 | goto out; |
| 92 | } |
| 93 | ret = btrfs_previous_item(root, &path, rootid, BTRFS_ROOT_BACKREF_KEY); |
| 94 | if (ret < 0) |
| 95 | goto out; |
| 96 | if (ret > 0) { |
| 97 | ret = -ENOENT; |
| 98 | goto out; |
| 99 | } |
| 100 | btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); |
| 101 | root_ref = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 102 | struct btrfs_root_ref); |
| 103 | *root_ret = key.offset; |
| 104 | *dir_ret = btrfs_root_ref_dirid(path.nodes[0], root_ref); |
| 105 | out: |
| 106 | btrfs_release_path(&path); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * To get the parent inode of @ino of @root. |
| 112 | * |
| 113 | * @root_ret and @ino_ret will be filled. |
| 114 | * |
| 115 | * NOTE: This function is not reliable. It can only get one parent inode. |
| 116 | * The get the proper parent inode, we need a full VFS inodes stack to |
| 117 | * resolve properly. |
| 118 | */ |
| 119 | static int get_parent_inode(struct btrfs_root *root, u64 ino, |
| 120 | struct btrfs_root **root_ret, u64 *ino_ret) |
| 121 | { |
| 122 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 123 | struct btrfs_path path; |
| 124 | struct btrfs_key key; |
| 125 | int ret; |
| 126 | |
| 127 | if (ino == BTRFS_FIRST_FREE_OBJECTID) { |
| 128 | u64 parent_root = -1; |
| 129 | |
| 130 | /* It's top level already, no more parent */ |
| 131 | if (root->root_key.objectid == BTRFS_FS_TREE_OBJECTID) { |
| 132 | *root_ret = fs_info->fs_root; |
| 133 | *ino_ret = BTRFS_FIRST_FREE_OBJECTID; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | ret = lookup_root_ref(fs_info, root->root_key.objectid, |
| 138 | &parent_root, ino_ret); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | key.objectid = parent_root; |
| 143 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 144 | key.offset = (u64)-1; |
| 145 | *root_ret = btrfs_read_fs_root(fs_info, &key); |
| 146 | if (IS_ERR(*root_ret)) |
| 147 | return PTR_ERR(*root_ret); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | btrfs_init_path(&path); |
| 153 | key.objectid = ino; |
| 154 | key.type = BTRFS_INODE_REF_KEY; |
| 155 | key.offset = (u64)-1; |
| 156 | |
| 157 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | /* Should not happen */ |
| 161 | if (ret == 0) { |
| 162 | ret = -EUCLEAN; |
| 163 | goto out; |
| 164 | } |
| 165 | ret = btrfs_previous_item(root, &path, ino, BTRFS_INODE_REF_KEY); |
| 166 | if (ret < 0) |
| 167 | goto out; |
| 168 | if (ret > 0) { |
| 169 | ret = -ENOENT; |
| 170 | goto out; |
| 171 | } |
| 172 | btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); |
| 173 | *root_ret = root; |
| 174 | *ino_ret = key.offset; |
| 175 | out: |
| 176 | btrfs_release_path(&path); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 180 | static inline int next_length(const char *path) |
| 181 | { |
| 182 | int res = 0; |
Qu Wenruo | 5bdcb37 | 2020-06-24 18:03:04 +0200 | [diff] [blame] | 183 | while (*path != '\0' && *path != '/') { |
| 184 | ++res; |
| 185 | ++path; |
| 186 | if (res > BTRFS_NAME_LEN) |
| 187 | break; |
| 188 | } |
Marek Behún | 21a14fa | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 189 | return res; |
| 190 | } |
| 191 | |
| 192 | static inline const char *skip_current_directories(const char *cur) |
| 193 | { |
| 194 | while (1) { |
| 195 | if (cur[0] == '/') |
| 196 | ++cur; |
| 197 | else if (cur[0] == '.' && cur[1] == '/') |
| 198 | cur += 2; |
| 199 | else |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | return cur; |
| 204 | } |
| 205 | |
Qu Wenruo | c921aa2 | 2020-06-24 18:03:05 +0200 | [diff] [blame] | 206 | /* |
| 207 | * Resolve one filename of @ino of @root. |
| 208 | * |
| 209 | * key_ret: The child key (either INODE_ITEM or ROOT_ITEM type) |
| 210 | * type_ret: BTRFS_FT_* of the child inode. |
| 211 | * |
| 212 | * Return 0 with above members filled. |
| 213 | * Return <0 for error. |
| 214 | */ |
| 215 | static int resolve_one_filename(struct btrfs_root *root, u64 ino, |
| 216 | const char *name, int namelen, |
| 217 | struct btrfs_key *key_ret, u8 *type_ret) |
| 218 | { |
| 219 | struct btrfs_dir_item *dir_item; |
| 220 | struct btrfs_path path; |
| 221 | int ret = 0; |
| 222 | |
| 223 | btrfs_init_path(&path); |
| 224 | |
| 225 | dir_item = btrfs_lookup_dir_item(NULL, root, &path, ino, name, |
| 226 | namelen, 0); |
| 227 | if (IS_ERR(dir_item)) { |
| 228 | ret = PTR_ERR(dir_item); |
| 229 | goto out; |
| 230 | } |
| 231 | |
| 232 | btrfs_dir_item_key_to_cpu(path.nodes[0], dir_item, key_ret); |
| 233 | *type_ret = btrfs_dir_type(path.nodes[0], dir_item); |
| 234 | out: |
| 235 | btrfs_release_path(&path); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Resolve a full path @filename. The start point is @ino of @root. |
| 241 | * |
| 242 | * The result will be filled into @root_ret, @ino_ret and @type_ret. |
| 243 | */ |
| 244 | int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename, |
| 245 | struct btrfs_root **root_ret, u64 *ino_ret, |
| 246 | u8 *type_ret, int symlink_limit) |
| 247 | { |
| 248 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 249 | struct btrfs_root *next_root; |
| 250 | struct btrfs_key key; |
| 251 | const char *cur = filename; |
| 252 | u64 next_ino; |
| 253 | u8 next_type; |
| 254 | u8 type; |
| 255 | int len; |
| 256 | int ret = 0; |
| 257 | |
| 258 | /* If the path is absolute path, also search from fs root */ |
| 259 | if (*cur == '/') { |
| 260 | root = fs_info->fs_root; |
| 261 | ino = btrfs_root_dirid(&root->root_item); |
| 262 | type = BTRFS_FT_DIR; |
| 263 | } |
| 264 | |
| 265 | while (*cur != '\0') { |
| 266 | cur = skip_current_directories(cur); |
| 267 | |
| 268 | len = next_length(cur); |
| 269 | if (len > BTRFS_NAME_LEN) { |
| 270 | error("%s: Name too long at \"%.*s\"", __func__, |
| 271 | BTRFS_NAME_LEN, cur); |
| 272 | return -ENAMETOOLONG; |
| 273 | } |
| 274 | |
| 275 | if (len == 1 && cur[0] == '.') |
| 276 | break; |
| 277 | |
| 278 | if (len == 2 && cur[0] == '.' && cur[1] == '.') { |
| 279 | /* Go one level up */ |
| 280 | ret = get_parent_inode(root, ino, &next_root, &next_ino); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | root = next_root; |
| 284 | ino = next_ino; |
| 285 | goto next; |
| 286 | } |
| 287 | |
| 288 | if (!*cur) |
| 289 | break; |
| 290 | |
| 291 | ret = resolve_one_filename(root, ino, cur, len, &key, &type); |
| 292 | if (ret < 0) |
| 293 | return ret; |
| 294 | |
| 295 | if (key.type == BTRFS_ROOT_ITEM_KEY) { |
| 296 | /* Child inode is a subvolume */ |
| 297 | |
| 298 | next_root = btrfs_read_fs_root(fs_info, &key); |
| 299 | if (IS_ERR(next_root)) |
| 300 | return PTR_ERR(next_root); |
| 301 | root = next_root; |
| 302 | ino = btrfs_root_dirid(&root->root_item); |
| 303 | } else if (type == BTRFS_FT_SYMLINK && symlink_limit >= 0) { |
| 304 | /* Child inode is a symlink */ |
| 305 | |
| 306 | char *target; |
| 307 | |
| 308 | if (symlink_limit == 0) { |
| 309 | error("%s: Too much symlinks!", __func__); |
| 310 | return -EMLINK; |
| 311 | } |
| 312 | target = malloc(fs_info->sectorsize); |
| 313 | if (!target) |
| 314 | return -ENOMEM; |
| 315 | ret = btrfs_readlink(root, key.objectid, target); |
| 316 | if (ret < 0) { |
| 317 | free(target); |
| 318 | return ret; |
| 319 | } |
| 320 | target[ret] = '\0'; |
| 321 | |
| 322 | ret = btrfs_lookup_path(root, ino, target, &next_root, |
| 323 | &next_ino, &next_type, |
| 324 | symlink_limit); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | root = next_root; |
| 328 | ino = next_ino; |
| 329 | type = next_type; |
| 330 | } else { |
| 331 | /* Child inode is an inode */ |
| 332 | ino = key.objectid; |
| 333 | } |
| 334 | next: |
| 335 | cur += len; |
| 336 | } |
| 337 | |
| 338 | if (!ret) { |
| 339 | *root_ret = root; |
| 340 | *ino_ret = ino; |
| 341 | *type_ret = type; |
| 342 | } |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
Qu Wenruo | a26a6be | 2020-06-24 18:03:09 +0200 | [diff] [blame] | 347 | /* |
| 348 | * Read out inline extent. |
| 349 | * |
| 350 | * Since inline extent should only exist for offset 0, no need for extra |
| 351 | * parameters. |
| 352 | * Truncating should be handled by the caller. |
| 353 | * |
| 354 | * Return the number of bytes read. |
| 355 | * Return <0 for error. |
| 356 | */ |
| 357 | int btrfs_read_extent_inline(struct btrfs_path *path, |
| 358 | struct btrfs_file_extent_item *fi, char *dest) |
| 359 | { |
| 360 | struct extent_buffer *leaf = path->nodes[0]; |
| 361 | int slot = path->slots[0]; |
| 362 | char *cbuf = NULL; |
| 363 | char *dbuf = NULL; |
| 364 | u32 csize; |
| 365 | u32 dsize; |
| 366 | int ret; |
| 367 | |
| 368 | csize = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(slot)); |
| 369 | if (btrfs_file_extent_compression(leaf, fi) == BTRFS_COMPRESS_NONE) { |
| 370 | /* Uncompressed, just read it out */ |
| 371 | read_extent_buffer(leaf, dest, |
| 372 | btrfs_file_extent_inline_start(fi), |
| 373 | csize); |
| 374 | return csize; |
| 375 | } |
| 376 | |
| 377 | /* Compressed extent, prepare the compressed and data buffer */ |
| 378 | dsize = btrfs_file_extent_ram_bytes(leaf, fi); |
| 379 | cbuf = malloc(csize); |
| 380 | dbuf = malloc(dsize); |
| 381 | if (!cbuf || !dbuf) { |
| 382 | ret = -ENOMEM; |
| 383 | goto out; |
| 384 | } |
| 385 | read_extent_buffer(leaf, cbuf, btrfs_file_extent_inline_start(fi), |
| 386 | csize); |
| 387 | ret = btrfs_decompress(btrfs_file_extent_compression(leaf, fi), |
| 388 | cbuf, csize, dbuf, dsize); |
| 389 | if (ret < 0 || ret != dsize) { |
| 390 | ret = -EIO; |
| 391 | goto out; |
| 392 | } |
| 393 | memcpy(dest, dbuf, dsize); |
| 394 | ret = dsize; |
| 395 | out: |
| 396 | free(cbuf); |
| 397 | free(dbuf); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Read out regular extent. |
| 403 | * |
| 404 | * Truncating should be handled by the caller. |
| 405 | * |
| 406 | * @offset and @len should not cross the extent boundary. |
| 407 | * Return the number of bytes read. |
| 408 | * Return <0 for error. |
| 409 | */ |
| 410 | int btrfs_read_extent_reg(struct btrfs_path *path, |
| 411 | struct btrfs_file_extent_item *fi, u64 offset, |
| 412 | int len, char *dest) |
| 413 | { |
| 414 | struct extent_buffer *leaf = path->nodes[0]; |
| 415 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
| 416 | struct btrfs_key key; |
| 417 | u64 extent_num_bytes; |
| 418 | u64 disk_bytenr; |
| 419 | u64 read; |
| 420 | char *cbuf = NULL; |
| 421 | char *dbuf = NULL; |
| 422 | u32 csize; |
| 423 | u32 dsize; |
| 424 | bool finished = false; |
| 425 | int num_copies; |
| 426 | int i; |
| 427 | int slot = path->slots[0]; |
| 428 | int ret; |
| 429 | |
| 430 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 431 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 432 | ASSERT(IS_ALIGNED(offset, fs_info->sectorsize) && |
| 433 | IS_ALIGNED(len, fs_info->sectorsize)); |
| 434 | ASSERT(offset >= key.offset && |
| 435 | offset + len <= key.offset + extent_num_bytes); |
| 436 | |
| 437 | /* Preallocated or hole , fill @dest with zero */ |
| 438 | if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_PREALLOC || |
| 439 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) { |
| 440 | memset(dest, 0, len); |
| 441 | return len; |
| 442 | } |
| 443 | |
| 444 | if (btrfs_file_extent_compression(leaf, fi) == BTRFS_COMPRESS_NONE) { |
| 445 | u64 logical; |
| 446 | |
| 447 | logical = btrfs_file_extent_disk_bytenr(leaf, fi) + |
| 448 | btrfs_file_extent_offset(leaf, fi) + |
| 449 | offset - key.offset; |
| 450 | read = len; |
| 451 | |
| 452 | num_copies = btrfs_num_copies(fs_info, logical, len); |
| 453 | for (i = 1; i <= num_copies; i++) { |
| 454 | ret = read_extent_data(fs_info, dest, logical, &read, i); |
| 455 | if (ret < 0 || read != len) |
| 456 | continue; |
| 457 | finished = true; |
| 458 | break; |
| 459 | } |
| 460 | if (!finished) |
| 461 | return -EIO; |
| 462 | return len; |
| 463 | } |
| 464 | |
| 465 | csize = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 466 | dsize = btrfs_file_extent_ram_bytes(leaf, fi); |
| 467 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 468 | num_copies = btrfs_num_copies(fs_info, disk_bytenr, csize); |
| 469 | |
| 470 | cbuf = malloc_cache_aligned(csize); |
| 471 | dbuf = malloc_cache_aligned(dsize); |
| 472 | if (!cbuf || !dbuf) { |
| 473 | ret = -ENOMEM; |
| 474 | goto out; |
| 475 | } |
| 476 | /* For compressed extent, we must read the whole on-disk extent */ |
| 477 | for (i = 1; i <= num_copies; i++) { |
| 478 | read = csize; |
| 479 | ret = read_extent_data(fs_info, cbuf, disk_bytenr, |
| 480 | &read, i); |
| 481 | if (ret < 0 || read != csize) |
| 482 | continue; |
| 483 | finished = true; |
| 484 | break; |
| 485 | } |
| 486 | if (!finished) { |
| 487 | ret = -EIO; |
| 488 | goto out; |
| 489 | } |
| 490 | |
| 491 | ret = btrfs_decompress(btrfs_file_extent_compression(leaf, fi), cbuf, |
| 492 | csize, dbuf, dsize); |
| 493 | if (ret != dsize) { |
| 494 | ret = -EIO; |
| 495 | goto out; |
| 496 | } |
| 497 | /* Then copy the needed part */ |
| 498 | memcpy(dest, dbuf + btrfs_file_extent_offset(leaf, fi), len); |
| 499 | ret = len; |
| 500 | out: |
| 501 | free(cbuf); |
| 502 | free(dbuf); |
| 503 | return ret; |
| 504 | } |
Qu Wenruo | 01347f6 | 2020-06-24 18:03:10 +0200 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Get the first file extent that covers bytenr @file_offset. |
| 508 | * |
| 509 | * @file_offset must be aligned to sectorsize. |
| 510 | * |
| 511 | * return 0 for found, and path points to the file extent. |
| 512 | * return >0 for not found, and fill @next_offset. |
| 513 | * @next_offset can be 0 if there is no next file extent. |
| 514 | * return <0 for error. |
| 515 | */ |
| 516 | static int lookup_data_extent(struct btrfs_root *root, struct btrfs_path *path, |
| 517 | u64 ino, u64 file_offset, u64 *next_offset) |
| 518 | { |
| 519 | struct btrfs_key key; |
| 520 | struct btrfs_file_extent_item *fi; |
| 521 | u8 extent_type; |
| 522 | int ret = 0; |
| 523 | |
| 524 | ASSERT(IS_ALIGNED(file_offset, root->fs_info->sectorsize)); |
| 525 | key.objectid = ino; |
| 526 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 527 | key.offset = file_offset; |
| 528 | |
| 529 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 530 | /* Error or we're already at the file extent */ |
| 531 | if (ret <= 0) |
| 532 | return ret; |
| 533 | if (ret > 0) { |
| 534 | /* Check previous file extent */ |
| 535 | ret = btrfs_previous_item(root, path, ino, |
| 536 | BTRFS_EXTENT_DATA_KEY); |
| 537 | if (ret < 0) |
| 538 | return ret; |
| 539 | if (ret > 0) |
| 540 | goto check_next; |
| 541 | } |
| 542 | /* Now the key.offset must be smaller than @file_offset */ |
| 543 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 544 | if (key.objectid != ino || |
| 545 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 546 | goto check_next; |
| 547 | |
| 548 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 549 | struct btrfs_file_extent_item); |
| 550 | extent_type = btrfs_file_extent_type(path->nodes[0], fi); |
| 551 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 552 | if (file_offset == 0) |
| 553 | return 0; |
| 554 | /* Inline extent should be the only extent, no next extent. */ |
| 555 | *next_offset = 0; |
| 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | /* This file extent covers @file_offset */ |
| 560 | if (key.offset <= file_offset && key.offset + |
| 561 | btrfs_file_extent_num_bytes(path->nodes[0], fi) > file_offset) |
| 562 | return 0; |
| 563 | check_next: |
| 564 | ret = btrfs_next_item(root, path); |
| 565 | if (ret < 0) |
| 566 | return ret; |
| 567 | if (ret > 0) { |
| 568 | *next_offset = 0; |
| 569 | return 1; |
| 570 | } |
| 571 | |
| 572 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 573 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 574 | struct btrfs_file_extent_item); |
| 575 | /* Next next data extent */ |
| 576 | if (key.objectid != ino || |
| 577 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 578 | *next_offset = 0; |
| 579 | return 1; |
| 580 | } |
| 581 | /* Current file extent already beyond @file_offset */ |
| 582 | if (key.offset > file_offset) { |
| 583 | *next_offset = key.offset; |
| 584 | return 1; |
| 585 | } |
| 586 | /* This file extent covers @file_offset */ |
| 587 | if (key.offset <= file_offset && key.offset + |
| 588 | btrfs_file_extent_num_bytes(path->nodes[0], fi) > file_offset) |
| 589 | return 0; |
| 590 | /* This file extent ends before @file_offset, check next */ |
| 591 | ret = btrfs_next_item(root, path); |
| 592 | if (ret < 0) |
| 593 | return ret; |
| 594 | if (ret > 0) { |
| 595 | *next_offset = 0; |
| 596 | return 1; |
| 597 | } |
| 598 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 599 | if (key.type != BTRFS_EXTENT_DATA_KEY || key.objectid != ino) { |
| 600 | *next_offset = 0; |
| 601 | return 1; |
| 602 | } |
| 603 | *next_offset = key.offset; |
| 604 | return 1; |
| 605 | } |
Qu Wenruo | e342718 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 606 | |
| 607 | static int read_and_truncate_page(struct btrfs_path *path, |
| 608 | struct btrfs_file_extent_item *fi, |
| 609 | int start, int len, char *dest) |
| 610 | { |
| 611 | struct extent_buffer *leaf = path->nodes[0]; |
| 612 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
| 613 | u64 aligned_start = round_down(start, fs_info->sectorsize); |
| 614 | u8 extent_type; |
| 615 | char *buf; |
| 616 | int page_off = start - aligned_start; |
| 617 | int page_len = fs_info->sectorsize - page_off; |
| 618 | int ret; |
| 619 | |
| 620 | ASSERT(start + len <= aligned_start + fs_info->sectorsize); |
| 621 | buf = malloc_cache_aligned(fs_info->sectorsize); |
| 622 | if (!buf) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 626 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 627 | ret = btrfs_read_extent_inline(path, fi, buf); |
| 628 | memcpy(dest, buf + page_off, min(page_len, ret)); |
| 629 | free(buf); |
| 630 | return len; |
| 631 | } |
| 632 | |
| 633 | ret = btrfs_read_extent_reg(path, fi, |
| 634 | round_down(start, fs_info->sectorsize), |
| 635 | fs_info->sectorsize, buf); |
| 636 | if (ret < 0) { |
| 637 | free(buf); |
| 638 | return ret; |
| 639 | } |
| 640 | memcpy(dest, buf + page_off, page_len); |
| 641 | free(buf); |
| 642 | return len; |
| 643 | } |
| 644 | |
| 645 | int btrfs_file_read(struct btrfs_root *root, u64 ino, u64 file_offset, u64 len, |
| 646 | char *dest) |
| 647 | { |
| 648 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 649 | struct btrfs_file_extent_item *fi; |
| 650 | struct btrfs_path path; |
| 651 | struct btrfs_key key; |
| 652 | u64 aligned_start = round_down(file_offset, fs_info->sectorsize); |
| 653 | u64 aligned_end = round_down(file_offset + len, fs_info->sectorsize); |
| 654 | u64 next_offset; |
| 655 | u64 cur = aligned_start; |
| 656 | int ret = 0; |
| 657 | |
| 658 | btrfs_init_path(&path); |
| 659 | |
| 660 | /* Set the whole dest all zero, so we won't need to bother holes */ |
| 661 | memset(dest, 0, len); |
| 662 | |
| 663 | /* Read out the leading unaligned part */ |
| 664 | if (aligned_start != file_offset) { |
| 665 | ret = lookup_data_extent(root, &path, ino, aligned_start, |
| 666 | &next_offset); |
| 667 | if (ret < 0) |
| 668 | goto out; |
| 669 | if (ret == 0) { |
| 670 | /* Read the unaligned part out*/ |
| 671 | fi = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 672 | struct btrfs_file_extent_item); |
| 673 | ret = read_and_truncate_page(&path, fi, file_offset, |
| 674 | round_up(file_offset, fs_info->sectorsize) - |
| 675 | file_offset, dest); |
| 676 | if (ret < 0) |
| 677 | goto out; |
| 678 | cur += fs_info->sectorsize; |
| 679 | } else { |
| 680 | /* The whole file is a hole */ |
| 681 | if (!next_offset) { |
| 682 | memset(dest, 0, len); |
| 683 | return len; |
| 684 | } |
| 685 | cur = next_offset; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* Read the aligned part */ |
| 690 | while (cur < aligned_end) { |
| 691 | u64 extent_num_bytes; |
| 692 | u8 type; |
| 693 | |
| 694 | btrfs_release_path(&path); |
| 695 | ret = lookup_data_extent(root, &path, ino, cur, &next_offset); |
| 696 | if (ret < 0) |
| 697 | goto out; |
| 698 | if (ret > 0) { |
| 699 | /* No next, direct exit */ |
| 700 | if (!next_offset) { |
| 701 | ret = 0; |
| 702 | goto out; |
| 703 | } |
| 704 | } |
| 705 | fi = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 706 | struct btrfs_file_extent_item); |
| 707 | btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); |
| 708 | type = btrfs_file_extent_type(path.nodes[0], fi); |
| 709 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 710 | ret = btrfs_read_extent_inline(&path, fi, dest); |
| 711 | goto out; |
| 712 | } |
| 713 | /* Skip holes, as we have zeroed the dest */ |
| 714 | if (type == BTRFS_FILE_EXTENT_PREALLOC || |
| 715 | btrfs_file_extent_disk_bytenr(path.nodes[0], fi) == 0) { |
| 716 | cur = key.offset + btrfs_file_extent_num_bytes( |
| 717 | path.nodes[0], fi); |
| 718 | continue; |
| 719 | } |
| 720 | |
| 721 | /* Read the remaining part of the extent */ |
| 722 | extent_num_bytes = btrfs_file_extent_num_bytes(path.nodes[0], |
| 723 | fi); |
| 724 | ret = btrfs_read_extent_reg(&path, fi, cur, |
| 725 | min(extent_num_bytes, aligned_end - cur), |
| 726 | dest + cur - file_offset); |
| 727 | if (ret < 0) |
| 728 | goto out; |
| 729 | cur += min(extent_num_bytes, aligned_end - cur); |
| 730 | } |
| 731 | |
| 732 | /* Read the tailing unaligned part*/ |
| 733 | if (file_offset + len != aligned_end) { |
| 734 | btrfs_release_path(&path); |
| 735 | ret = lookup_data_extent(root, &path, ino, aligned_end, |
| 736 | &next_offset); |
| 737 | /* <0 is error, >0 means no extent */ |
| 738 | if (ret) |
| 739 | goto out; |
| 740 | fi = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 741 | struct btrfs_file_extent_item); |
| 742 | ret = read_and_truncate_page(&path, fi, aligned_end, |
| 743 | file_offset + len - aligned_end, |
| 744 | dest + aligned_end - file_offset); |
| 745 | } |
| 746 | out: |
| 747 | btrfs_release_path(&path); |
| 748 | if (ret < 0) |
| 749 | return ret; |
| 750 | return len; |
| 751 | } |