Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 2 | /* |
| 3 | * BTRFS filesystem implementation for U-Boot |
| 4 | * |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 5 | * 2017 Marek Behún, CZ.NIC, kabel@kernel.org |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 8 | #include <config.h> |
| 9 | #include <malloc.h> |
Caleb Connolly | 29cab7c | 2024-08-30 13:34:37 +0100 | [diff] [blame] | 10 | #include <u-boot/uuid.h> |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 11 | #include <linux/time.h> |
Qu Wenruo | 0e7b854 | 2020-06-24 18:02:48 +0200 | [diff] [blame] | 12 | #include "btrfs.h" |
| 13 | #include "crypto/hash.h" |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 14 | #include "disk-io.h" |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 15 | |
Qu Wenruo | 1d5a7b7 | 2020-06-24 18:03:01 +0200 | [diff] [blame] | 16 | struct btrfs_fs_info *current_fs_info; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 17 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 18 | static int show_dir(struct btrfs_root *root, struct extent_buffer *eb, |
| 19 | struct btrfs_dir_item *di) |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 20 | { |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 21 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 22 | struct btrfs_inode_item ii; |
| 23 | struct btrfs_key key; |
| 24 | static const char* dir_item_str[] = { |
Marek Behún | b4c2773 | 2021-02-09 19:05:08 +0100 | [diff] [blame] | 25 | [BTRFS_FT_REG_FILE] = " ", |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 26 | [BTRFS_FT_DIR] = "DIR", |
Marek Behún | b4c2773 | 2021-02-09 19:05:08 +0100 | [diff] [blame] | 27 | [BTRFS_FT_CHRDEV] = "CHR", |
| 28 | [BTRFS_FT_BLKDEV] = "BLK", |
| 29 | [BTRFS_FT_FIFO] = "FIF", |
| 30 | [BTRFS_FT_SOCK] = "SCK", |
| 31 | [BTRFS_FT_SYMLINK] = "SYM", |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 32 | }; |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 33 | u8 type = btrfs_dir_type(eb, di); |
| 34 | char namebuf[BTRFS_NAME_LEN]; |
| 35 | char *target = NULL; |
| 36 | char filetime[32]; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 37 | time_t mtime; |
Qu Wenruo | bbad1d7 | 2020-10-31 09:07:51 +0800 | [diff] [blame] | 38 | int ret = 0; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 39 | |
Marek Behún | b0cbb52 | 2021-02-09 19:05:07 +0100 | [diff] [blame] | 40 | /* skip XATTRs in directory listing */ |
| 41 | if (type == BTRFS_FT_XATTR) |
| 42 | return 0; |
| 43 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 44 | btrfs_dir_item_key_to_cpu(eb, di, &key); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 45 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 46 | if (key.type == BTRFS_ROOT_ITEM_KEY) { |
| 47 | struct btrfs_root *subvol; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 48 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 49 | /* It's a subvolume, get its mtime from root item */ |
| 50 | subvol = btrfs_read_fs_root(fs_info, &key); |
| 51 | if (IS_ERR(subvol)) { |
| 52 | ret = PTR_ERR(subvol); |
| 53 | error("Can't find root %llu", key.objectid); |
| 54 | return ret; |
| 55 | } |
| 56 | mtime = btrfs_stack_timespec_sec(&subvol->root_item.otime); |
| 57 | } else { |
| 58 | struct btrfs_path path; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 59 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 60 | /* It's regular inode, get its mtime from inode item */ |
| 61 | btrfs_init_path(&path); |
| 62 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 63 | if (ret > 0) |
| 64 | ret = -ENOENT; |
| 65 | if (ret < 0) { |
| 66 | error("Can't find inode %llu", key.objectid); |
| 67 | btrfs_release_path(&path); |
| 68 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 69 | } |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 70 | read_extent_buffer(path.nodes[0], &ii, |
| 71 | btrfs_item_ptr_offset(path.nodes[0], path.slots[0]), |
| 72 | sizeof(ii)); |
| 73 | btrfs_release_path(&path); |
| 74 | mtime = btrfs_stack_timespec_sec(&ii.mtime); |
| 75 | } |
| 76 | ctime_r(&mtime, filetime); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 77 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 78 | if (type == BTRFS_FT_SYMLINK) { |
| 79 | target = malloc(fs_info->sectorsize); |
| 80 | if (!target) { |
| 81 | error("Can't alloc memory for symlink %llu", |
| 82 | key.objectid); |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | ret = btrfs_readlink(root, key.objectid, target); |
| 86 | if (ret < 0) { |
| 87 | error("Failed to read symlink %llu", key.objectid); |
| 88 | goto out; |
| 89 | } |
| 90 | target[ret] = '\0'; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 93 | if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type]) |
| 94 | printf("<%s> ", dir_item_str[type]); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 95 | else |
Marek Behún | b4c2773 | 2021-02-09 19:05:08 +0100 | [diff] [blame] | 96 | printf("?%3u? ", type); |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 97 | if (type == BTRFS_FT_CHRDEV || type == BTRFS_FT_BLKDEV) { |
| 98 | ASSERT(key.type == BTRFS_INODE_ITEM_KEY); |
| 99 | printf("%4llu,%5llu ", btrfs_stack_inode_rdev(&ii) >> 20, |
| 100 | btrfs_stack_inode_rdev(&ii) & 0xfffff); |
| 101 | } else { |
| 102 | if (key.type == BTRFS_INODE_ITEM_KEY) |
| 103 | printf("%10llu ", btrfs_stack_inode_size(&ii)); |
| 104 | else |
| 105 | printf("%10llu ", 0ULL); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 108 | read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), |
| 109 | btrfs_dir_name_len(eb, di)); |
| 110 | printf("%24.24s %.*s", filetime, btrfs_dir_name_len(eb, di), namebuf); |
| 111 | if (type == BTRFS_FT_SYMLINK) |
| 112 | printf(" -> %s", target ? target : "?"); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 113 | printf("\n"); |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 114 | out: |
| 115 | free(target); |
| 116 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 119 | int btrfs_probe(struct blk_desc *fs_dev_desc, |
| 120 | struct disk_partition *fs_partition) |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 121 | { |
Qu Wenruo | 1d5a7b7 | 2020-06-24 18:03:01 +0200 | [diff] [blame] | 122 | struct btrfs_fs_info *fs_info; |
| 123 | int ret = -1; |
| 124 | |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 125 | btrfs_hash_init(); |
Qu Wenruo | 1d5a7b7 | 2020-06-24 18:03:01 +0200 | [diff] [blame] | 126 | fs_info = open_ctree_fs_info(fs_dev_desc, fs_partition); |
| 127 | if (fs_info) { |
| 128 | current_fs_info = fs_info; |
| 129 | ret = 0; |
| 130 | } |
| 131 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int btrfs_ls(const char *path) |
| 135 | { |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 136 | struct btrfs_fs_info *fs_info = current_fs_info; |
| 137 | struct btrfs_root *root = fs_info->fs_root; |
| 138 | u64 ino = BTRFS_FIRST_FREE_OBJECTID; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 139 | u8 type; |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 140 | int ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 141 | |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 142 | ASSERT(fs_info); |
| 143 | ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID, |
| 144 | path, &root, &ino, &type, 40); |
| 145 | if (ret < 0) { |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 146 | printf("Cannot lookup path %s\n", path); |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 147 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if (type != BTRFS_FT_DIR) { |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 151 | error("Not a directory: %s", path); |
| 152 | return -ENOENT; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 153 | } |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 154 | ret = btrfs_iter_dir(root, ino, show_dir); |
| 155 | if (ret < 0) { |
Naoki Hayama | c080b51 | 2020-10-12 18:35:33 +0900 | [diff] [blame] | 156 | error("An error occurred while listing directory %s", path); |
Qu Wenruo | 348848a | 2020-06-24 18:03:06 +0200 | [diff] [blame] | 157 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 158 | } |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int btrfs_exists(const char *file) |
| 163 | { |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 164 | struct btrfs_fs_info *fs_info = current_fs_info; |
| 165 | struct btrfs_root *root; |
| 166 | u64 ino; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 167 | u8 type; |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | ASSERT(fs_info); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 171 | |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 172 | ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID, |
| 173 | file, &root, &ino, &type, 40); |
| 174 | if (ret < 0) |
| 175 | return 0; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 176 | |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 177 | if (type == BTRFS_FT_REG_FILE) |
| 178 | return 1; |
| 179 | return 0; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int btrfs_size(const char *file, loff_t *size) |
| 183 | { |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 184 | struct btrfs_fs_info *fs_info = current_fs_info; |
| 185 | struct btrfs_inode_item *ii; |
| 186 | struct btrfs_root *root; |
| 187 | struct btrfs_path path; |
| 188 | struct btrfs_key key; |
| 189 | u64 ino; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 190 | u8 type; |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 191 | int ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 192 | |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 193 | ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID, |
| 194 | file, &root, &ino, &type, 40); |
| 195 | if (ret < 0) { |
Dominique Martinet | 0e00310 | 2024-11-15 11:15:47 +0900 | [diff] [blame] | 196 | debug("Cannot lookup file %s\n", file); |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 197 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 198 | } |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 199 | if (type != BTRFS_FT_REG_FILE) { |
| 200 | printf("Not a regular file: %s\n", file); |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 201 | return -ENOENT; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 202 | } |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 203 | btrfs_init_path(&path); |
| 204 | key.objectid = ino; |
| 205 | key.type = BTRFS_INODE_ITEM_KEY; |
| 206 | key.offset = 0; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 207 | |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 208 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 209 | if (ret < 0) { |
| 210 | printf("Cannot lookup ino %llu\n", ino); |
| 211 | return ret; |
| 212 | } |
| 213 | if (ret > 0) { |
| 214 | printf("Ino %llu does not exist\n", ino); |
| 215 | ret = -ENOENT; |
| 216 | goto out; |
| 217 | } |
| 218 | ii = btrfs_item_ptr(path.nodes[0], path.slots[0], |
| 219 | struct btrfs_inode_item); |
| 220 | *size = btrfs_inode_size(path.nodes[0], ii); |
| 221 | out: |
| 222 | btrfs_release_path(&path); |
| 223 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len, |
| 227 | loff_t *actread) |
| 228 | { |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 229 | struct btrfs_fs_info *fs_info = current_fs_info; |
| 230 | struct btrfs_root *root; |
Sam Edwards | 9d1c1b4 | 2023-11-11 08:19:04 -0700 | [diff] [blame] | 231 | loff_t real_size; |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 232 | u64 ino; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 233 | u8 type; |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 234 | int ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 235 | |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 236 | ASSERT(fs_info); |
| 237 | ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID, |
| 238 | file, &root, &ino, &type, 40); |
| 239 | if (ret < 0) { |
| 240 | error("Cannot lookup file %s", file); |
| 241 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (type != BTRFS_FT_REG_FILE) { |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 245 | error("Not a regular file: %s", file); |
| 246 | return -EINVAL; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Sam Edwards | 9d1c1b4 | 2023-11-11 08:19:04 -0700 | [diff] [blame] | 249 | ret = btrfs_size(file, &real_size); |
| 250 | if (ret < 0) { |
| 251 | error("Failed to get inode size: %s", file); |
| 252 | return ret; |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 253 | } |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 254 | |
Sam Edwards | 9d1c1b4 | 2023-11-11 08:19:04 -0700 | [diff] [blame] | 255 | if (!len || len > real_size - offset) |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 256 | len = real_size - offset; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 257 | |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 258 | ret = btrfs_file_read(root, ino, offset, len, buf); |
| 259 | if (ret < 0) { |
Naoki Hayama | c080b51 | 2020-10-12 18:35:33 +0900 | [diff] [blame] | 260 | error("An error occurred while reading file %s", file); |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 261 | return ret; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Qu Wenruo | cf5a3a0 | 2020-06-24 18:03:11 +0200 | [diff] [blame] | 264 | *actread = len; |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | void btrfs_close(void) |
| 269 | { |
Qu Wenruo | 1d5a7b7 | 2020-06-24 18:03:01 +0200 | [diff] [blame] | 270 | if (current_fs_info) { |
| 271 | close_ctree_fs_info(current_fs_info); |
| 272 | current_fs_info = NULL; |
| 273 | } |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int btrfs_uuid(char *uuid_str) |
| 277 | { |
| 278 | #ifdef CONFIG_LIB_UUID |
Qu Wenruo | f9344c3 | 2020-06-24 18:03:07 +0200 | [diff] [blame] | 279 | if (current_fs_info) |
| 280 | uuid_bin_to_str(current_fs_info->super_copy->fsid, uuid_str, |
| 281 | UUID_STR_FORMAT_STD); |
Marek Behún | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | #endif |
| 284 | return -ENOSYS; |
| 285 | } |