xref: /openbmc/u-boot/fs/btrfs/subvolume.c (revision e8f80a5a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * BTRFS filesystem implementation for U-Boot
4  *
5  * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
6  */
7 
8 #include "btrfs.h"
9 #include <malloc.h>
10 
get_subvol_name(u64 subvolid,char * name,int max_len)11 static int get_subvol_name(u64 subvolid, char *name, int max_len)
12 {
13 	struct btrfs_root_ref rref;
14 	struct btrfs_inode_ref iref;
15 	struct btrfs_root root;
16 	u64 dir;
17 	char tmp[max(BTRFS_VOL_NAME_MAX, BTRFS_NAME_MAX)];
18 	char *ptr;
19 
20 	ptr = name + max_len - 1;
21 	*ptr = '\0';
22 
23 	while (subvolid != BTRFS_FS_TREE_OBJECTID) {
24 		subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp);
25 
26 		if (subvolid == -1ULL)
27 			return -1;
28 
29 		ptr -= rref.name_len + 1;
30 		if (ptr < name)
31 			goto too_long;
32 
33 		memcpy(ptr + 1, tmp, rref.name_len);
34 		*ptr = '/';
35 
36 		if (btrfs_find_root(subvolid, &root, NULL))
37 			return -1;
38 
39 		dir = rref.dirid;
40 
41 		while (dir != BTRFS_FIRST_FREE_OBJECTID) {
42 			dir = btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
43 
44 			if (dir == -1ULL)
45 				return -1;
46 
47 			ptr -= iref.name_len + 1;
48 			if (ptr < name)
49 				goto too_long;
50 
51 			memcpy(ptr + 1, tmp, iref.name_len);
52 			*ptr = '/';
53 		}
54 	}
55 
56 	if (ptr == name + max_len - 1) {
57 		name[0] = '/';
58 		name[1] = '\0';
59 	} else {
60 		memmove(name, ptr, name + max_len - ptr);
61 	}
62 
63 	return 0;
64 
65 too_long:
66 	printf("%s: subvolume name too long\n", __func__);
67 	return -1;
68 }
69 
btrfs_get_default_subvol_objectid(void)70 u64 btrfs_get_default_subvol_objectid(void)
71 {
72 	struct btrfs_dir_item item;
73 
74 	if (btrfs_lookup_dir_item(&btrfs_info.tree_root,
75 				  btrfs_info.sb.root_dir_objectid, "default", 7,
76 				  &item))
77 		return BTRFS_FS_TREE_OBJECTID;
78 	return item.location.objectid;
79 }
80 
list_subvols(u64 tree,char * nameptr,int max_name_len,int level)81 static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
82 {
83 	struct btrfs_key key, *found_key;
84 	struct btrfs_path path;
85 	struct btrfs_root_ref *ref;
86 	int res;
87 
88 	key.objectid = tree;
89 	key.type = BTRFS_ROOT_REF_KEY;
90 	key.offset = 0;
91 
92 	if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path))
93 		return;
94 
95 	do {
96 		found_key = btrfs_path_leaf_key(&path);
97 		if (btrfs_comp_keys_type(&key, found_key))
98 			break;
99 
100 		ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
101 		btrfs_root_ref_to_cpu(ref);
102 
103 		printf("ID %llu parent %llu name ", found_key->offset, tree);
104 		if (nameptr && !get_subvol_name(found_key->offset, nameptr,
105 						max_name_len))
106 			printf("%s\n", nameptr);
107 		else
108 			printf("%.*s\n", (int) ref->name_len,
109 			       (const char *) (ref + 1));
110 
111 		if (level > 0)
112 			list_subvols(found_key->offset, nameptr, max_name_len,
113 				     level - 1);
114 		else
115 			printf("%s: Too much recursion, maybe skipping some "
116 			       "subvolumes\n", __func__);
117 	} while (!(res = btrfs_next_slot(&path)));
118 
119 	btrfs_free_path(&path);
120 }
121 
btrfs_list_subvols(void)122 void btrfs_list_subvols(void)
123 {
124 	char *nameptr = malloc(4096);
125 
126 	list_subvols(BTRFS_FS_TREE_OBJECTID, nameptr, nameptr ? 4096 : 0, 40);
127 
128 	if (nameptr)
129 		free(nameptr);
130 }
131