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 static int get_subvol_name(u64 subvolid, char *name, int max_len)
13 struct btrfs_root_ref rref;
14 struct btrfs_inode_ref iref;
15 struct btrfs_root root;
17 char tmp[max(BTRFS_VOL_NAME_MAX, BTRFS_NAME_MAX)];
20 ptr = name + max_len - 1;
23 while (subvolid != BTRFS_FS_TREE_OBJECTID) {
24 subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp);
26 if (subvolid == -1ULL)
29 ptr -= rref.name_len + 1;
33 memcpy(ptr + 1, tmp, rref.name_len);
36 if (btrfs_find_root(subvolid, &root, NULL))
41 while (dir != BTRFS_FIRST_FREE_OBJECTID) {
42 dir = btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
47 ptr -= iref.name_len + 1;
51 memcpy(ptr + 1, tmp, iref.name_len);
56 if (ptr == name + max_len - 1) {
60 memmove(name, ptr, name + max_len - ptr);
66 printf("%s: subvolume name too long\n", __func__);
70 u64 btrfs_get_default_subvol_objectid(void)
72 struct btrfs_dir_item item;
74 if (btrfs_lookup_dir_item(&btrfs_info.tree_root,
75 btrfs_info.sb.root_dir_objectid, "default", 7,
77 return BTRFS_FS_TREE_OBJECTID;
78 return item.location.objectid;
81 static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
83 struct btrfs_key key, *found_key;
84 struct btrfs_path path;
85 struct btrfs_root_ref *ref;
89 key.type = BTRFS_ROOT_REF_KEY;
92 if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path))
96 found_key = btrfs_path_leaf_key(&path);
97 if (btrfs_comp_keys_type(&key, found_key))
100 ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
101 btrfs_root_ref_to_cpu(ref);
103 printf("ID %llu parent %llu name ", found_key->offset, tree);
104 if (nameptr && !get_subvol_name(found_key->offset, nameptr,
106 printf("%s\n", nameptr);
108 printf("%.*s\n", (int) ref->name_len,
109 (const char *) (ref + 1));
112 list_subvols(found_key->offset, nameptr, max_name_len,
115 printf("%s: Too much recursion, maybe skipping some "
116 "subvolumes\n", __func__);
117 } while (!(res = btrfs_next_slot(&path)));
119 btrfs_free_path(&path);
122 void btrfs_list_subvols(void)
124 char *nameptr = malloc(4096);
126 list_subvols(BTRFS_FS_TREE_OBJECTID, nameptr, nameptr ? 4096 : 0, 40);