1 // SPDX-License-Identifier: GPL-2.0+
3 * BTRFS filesystem implementation for U-Boot
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
11 u64 btrfs_read_extent_inline(struct btrfs_path *path,
12 struct btrfs_file_extent_item *extent, u64 offset,
15 u32 clen, dlen, orig_size = size, res;
18 const int data_off = offsetof(struct btrfs_file_extent_item,
21 clen = btrfs_path_item_size(path) - data_off;
22 cbuf = (const char *) extent + data_off;
23 dlen = extent->ram_bytes;
28 if (size > dlen - offset)
31 if (extent->compression == BTRFS_COMPRESS_NONE) {
32 memcpy(out, cbuf + offset, size);
36 if (dlen > orig_size) {
44 res = btrfs_decompress(extent->compression, cbuf, clen, dbuf, dlen);
45 if (res == -1 || res != dlen)
48 if (dlen > orig_size) {
49 memcpy(out, dbuf + offset, size);
52 memmove(out, dbuf + offset, size);
63 u64 btrfs_read_extent_reg(struct btrfs_path *path,
64 struct btrfs_file_extent_item *extent, u64 offset,
67 u64 physical, clen, dlen, orig_size = size;
71 clen = extent->disk_num_bytes;
72 dlen = extent->num_bytes;
77 if (size > dlen - offset)
80 physical = btrfs_map_logical_to_physical(extent->disk_bytenr);
81 if (physical == -1ULL)
84 if (extent->compression == BTRFS_COMPRESS_NONE) {
85 physical += extent->offset + offset;
86 if (!btrfs_devread(physical, size, out))
92 cbuf = malloc(dlen > size ? clen + dlen : clen);
101 if (!btrfs_devread(physical, clen, cbuf))
104 res = btrfs_decompress(extent->compression, cbuf, clen, dbuf, dlen);
108 if (dlen > orig_size)
109 memcpy(out, dbuf + offset, size);
111 memmove(out, dbuf + offset, size);