blob: 52a243a659ffbb27af3982ec01d0c54aed881bf2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún0c936ee2017-09-03 17:00:29 +02002/*
3 * BTRFS filesystem implementation for U-Boot
4 *
5 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
Marek Behún0c936ee2017-09-03 17:00:29 +02006 */
7
Marek Behún0c936ee2017-09-03 17:00:29 +02008#include <config.h>
9#include <malloc.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060010#include <uuid.h>
Marek Behún0c936ee2017-09-03 17:00:29 +020011#include <linux/time.h>
Qu Wenruo565a4142020-06-24 18:02:48 +020012#include "btrfs.h"
13#include "crypto/hash.h"
Qu Wenruo4aebb992020-06-24 18:02:49 +020014#include "disk-io.h"
Marek Behún0c936ee2017-09-03 17:00:29 +020015
Qu Wenruof06bfcf2020-06-24 18:03:01 +020016struct btrfs_fs_info *current_fs_info;
Marek Behún0c936ee2017-09-03 17:00:29 +020017
Qu Wenruo325dd1f2020-06-24 18:03:06 +020018static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,
19 struct btrfs_dir_item *di)
Marek Behún0c936ee2017-09-03 17:00:29 +020020{
Qu Wenruo325dd1f2020-06-24 18:03:06 +020021 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únc65365d2021-02-09 19:05:08 +010025 [BTRFS_FT_REG_FILE] = " ",
Qu Wenruo325dd1f2020-06-24 18:03:06 +020026 [BTRFS_FT_DIR] = "DIR",
Marek Behúnc65365d2021-02-09 19:05:08 +010027 [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ún0c936ee2017-09-03 17:00:29 +020032 };
Qu Wenruo325dd1f2020-06-24 18:03:06 +020033 u8 type = btrfs_dir_type(eb, di);
34 char namebuf[BTRFS_NAME_LEN];
35 char *target = NULL;
36 char filetime[32];
Marek Behún0c936ee2017-09-03 17:00:29 +020037 time_t mtime;
Qu Wenruo4891c4e2020-10-31 09:07:51 +080038 int ret = 0;
Marek Behún0c936ee2017-09-03 17:00:29 +020039
Marek Behúne4cecb562021-02-09 19:05:07 +010040 /* skip XATTRs in directory listing */
41 if (type == BTRFS_FT_XATTR)
42 return 0;
43
Qu Wenruo325dd1f2020-06-24 18:03:06 +020044 btrfs_dir_item_key_to_cpu(eb, di, &key);
45
46 if (key.type == BTRFS_ROOT_ITEM_KEY) {
47 struct btrfs_root *subvol;
48
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;
59
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;
69 }
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);
Marek Behún0c936ee2017-09-03 17:00:29 +020075 }
Marek Behún0c936ee2017-09-03 17:00:29 +020076 ctime_r(&mtime, filetime);
77
Qu Wenruo325dd1f2020-06-24 18:03:06 +020078 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;
Marek Behún0c936ee2017-09-03 17:00:29 +020084 }
Qu Wenruo325dd1f2020-06-24 18:03:06 +020085 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ún0c936ee2017-09-03 17:00:29 +020091 }
92
Qu Wenruo325dd1f2020-06-24 18:03:06 +020093 if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
94 printf("<%s> ", dir_item_str[type]);
Marek Behún0c936ee2017-09-03 17:00:29 +020095 else
Marek Behúnc65365d2021-02-09 19:05:08 +010096 printf("?%3u? ", type);
Qu Wenruo325dd1f2020-06-24 18:03:06 +020097 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ún0c936ee2017-09-03 17:00:29 +0200106 }
107
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200108 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ún0c936ee2017-09-03 17:00:29 +0200113 printf("\n");
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200114out:
115 free(target);
116 return ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200117}
118
Simon Glass05289792020-05-10 11:39:57 -0600119int btrfs_probe(struct blk_desc *fs_dev_desc,
120 struct disk_partition *fs_partition)
Marek Behún0c936ee2017-09-03 17:00:29 +0200121{
Qu Wenruof06bfcf2020-06-24 18:03:01 +0200122 struct btrfs_fs_info *fs_info;
123 int ret = -1;
124
Marek Behún0c936ee2017-09-03 17:00:29 +0200125 btrfs_hash_init();
Qu Wenruof06bfcf2020-06-24 18:03:01 +0200126 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ún0c936ee2017-09-03 17:00:29 +0200132}
133
134int btrfs_ls(const char *path)
135{
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200136 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ún0c936ee2017-09-03 17:00:29 +0200139 u8 type;
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200140 int ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200141
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200142 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ún0c936ee2017-09-03 17:00:29 +0200146 printf("Cannot lookup path %s\n", path);
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200147 return ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200148 }
149
150 if (type != BTRFS_FT_DIR) {
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200151 error("Not a directory: %s", path);
152 return -ENOENT;
Marek Behún0c936ee2017-09-03 17:00:29 +0200153 }
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200154 ret = btrfs_iter_dir(root, ino, show_dir);
155 if (ret < 0) {
Naoki Hayama09f21072020-10-12 18:35:33 +0900156 error("An error occurred while listing directory %s", path);
Qu Wenruo325dd1f2020-06-24 18:03:06 +0200157 return ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200158 }
Marek Behún0c936ee2017-09-03 17:00:29 +0200159 return 0;
160}
161
162int btrfs_exists(const char *file)
163{
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200164 struct btrfs_fs_info *fs_info = current_fs_info;
165 struct btrfs_root *root;
166 u64 ino;
Marek Behún0c936ee2017-09-03 17:00:29 +0200167 u8 type;
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200168 int ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200169
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200170 ASSERT(fs_info);
Marek Behún0c936ee2017-09-03 17:00:29 +0200171
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200172 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;
176
177 if (type == BTRFS_FT_REG_FILE)
178 return 1;
179 return 0;
Marek Behún0c936ee2017-09-03 17:00:29 +0200180}
181
182int btrfs_size(const char *file, loff_t *size)
183{
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200184 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ún0c936ee2017-09-03 17:00:29 +0200190 u8 type;
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200191 int ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200192
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200193 ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
194 file, &root, &ino, &type, 40);
195 if (ret < 0) {
Marek Behún0c936ee2017-09-03 17:00:29 +0200196 printf("Cannot lookup file %s\n", file);
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200197 return ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200198 }
Marek Behún0c936ee2017-09-03 17:00:29 +0200199 if (type != BTRFS_FT_REG_FILE) {
200 printf("Not a regular file: %s\n", file);
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200201 return -ENOENT;
Marek Behún0c936ee2017-09-03 17:00:29 +0200202 }
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200203 btrfs_init_path(&path);
204 key.objectid = ino;
205 key.type = BTRFS_INODE_ITEM_KEY;
206 key.offset = 0;
Marek Behún0c936ee2017-09-03 17:00:29 +0200207
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200208 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);
221out:
222 btrfs_release_path(&path);
223 return ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200224}
225
226int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
227 loff_t *actread)
228{
Qu Wenruoe3427182020-06-24 18:03:11 +0200229 struct btrfs_fs_info *fs_info = current_fs_info;
230 struct btrfs_root *root;
231 loff_t real_size = 0;
232 u64 ino;
Marek Behún0c936ee2017-09-03 17:00:29 +0200233 u8 type;
Qu Wenruoe3427182020-06-24 18:03:11 +0200234 int ret;
Marek Behún0c936ee2017-09-03 17:00:29 +0200235
Qu Wenruoe3427182020-06-24 18:03:11 +0200236 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ún0c936ee2017-09-03 17:00:29 +0200242 }
243
244 if (type != BTRFS_FT_REG_FILE) {
Qu Wenruoe3427182020-06-24 18:03:11 +0200245 error("Not a regular file: %s", file);
246 return -EINVAL;
Marek Behún0c936ee2017-09-03 17:00:29 +0200247 }
248
Qu Wenruoe3427182020-06-24 18:03:11 +0200249 if (!len) {
250 ret = btrfs_size(file, &real_size);
251 if (ret < 0) {
252 error("Failed to get inode size: %s", file);
253 return ret;
254 }
255 len = real_size;
Marek Behún0c936ee2017-09-03 17:00:29 +0200256 }
257
Qu Wenruoe3427182020-06-24 18:03:11 +0200258 if (len > real_size - offset)
259 len = real_size - offset;
260
261 ret = btrfs_file_read(root, ino, offset, len, buf);
262 if (ret < 0) {
Naoki Hayama09f21072020-10-12 18:35:33 +0900263 error("An error occurred while reading file %s", file);
Qu Wenruoe3427182020-06-24 18:03:11 +0200264 return ret;
265 }
266
267 *actread = len;
Marek Behún0c936ee2017-09-03 17:00:29 +0200268 return 0;
269}
270
271void btrfs_close(void)
272{
Qu Wenruof06bfcf2020-06-24 18:03:01 +0200273 if (current_fs_info) {
274 close_ctree_fs_info(current_fs_info);
275 current_fs_info = NULL;
276 }
Marek Behún0c936ee2017-09-03 17:00:29 +0200277}
278
279int btrfs_uuid(char *uuid_str)
280{
281#ifdef CONFIG_LIB_UUID
Qu Wenruo5bbb68d2020-06-24 18:03:07 +0200282 if (current_fs_info)
283 uuid_bin_to_str(current_fs_info->super_copy->fsid, uuid_str,
284 UUID_STR_FORMAT_STD);
Marek Behún0c936ee2017-09-03 17:00:29 +0200285 return 0;
286#endif
287 return -ENOSYS;
288}