summaryrefslogtreecommitdiff
path: root/fs/btrfs/subvolume.c
blob: 6fc28d53e55224dbe6bd88241eb8993aa294e307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// SPDX-License-Identifier: GPL-2.0+
/*
 * BTRFS filesystem implementation for U-Boot
 *
 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
 */

#include <malloc.h>
#include "ctree.h"
#include "btrfs.h"
#include "disk-io.h"

/*
 * Resolve the path of ino inside subvolume @root into @path_ret.
 *
 * @path_ret must be at least PATH_MAX size.
 */
static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret)
{
	struct btrfs_path path;
	struct btrfs_key key;
	char *tmp;
	u64 cur = ino;
	int ret = 0;

	tmp = malloc(PATH_MAX);
	if (!tmp)
		return -ENOMEM;
	tmp[0] = '\0';

	btrfs_init_path(&path);
	while (cur != BTRFS_FIRST_FREE_OBJECTID) {
		struct btrfs_inode_ref *iref;
		int name_len;

		btrfs_release_path(&path);
		key.objectid = cur;
		key.type = BTRFS_INODE_REF_KEY;
		key.offset = (u64)-1;

		ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
		/* Impossible */
		if (ret == 0)
			ret = -EUCLEAN;
		if (ret < 0)
			goto out;
		ret = btrfs_previous_item(root, &path, cur,
					  BTRFS_INODE_REF_KEY);
		if (ret > 0)
			ret = -ENOENT;
		if (ret < 0)
			goto out;

		strncpy(tmp, path_ret, PATH_MAX);
		iref = btrfs_item_ptr(path.nodes[0], path.slots[0],
				      struct btrfs_inode_ref);
		name_len = btrfs_inode_ref_name_len(path.nodes[0],
						    iref);
		if (name_len > BTRFS_NAME_LEN) {
			ret = -ENAMETOOLONG;
			goto out;
		}
		read_extent_buffer(path.nodes[0], path_ret,
				   (unsigned long)(iref + 1), name_len);
		path_ret[name_len] = '/';
		path_ret[name_len + 1] = '\0';
		strncat(path_ret, tmp, PATH_MAX);

		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
		cur = key.offset;
	}
out:
	btrfs_release_path(&path);
	free(tmp);
	return ret;
}

static int list_one_subvol(struct btrfs_root *root, char *path_ret)
{
	struct btrfs_fs_info *fs_info = root->fs_info;
	struct btrfs_root *tree_root = fs_info->tree_root;
	struct btrfs_path path;
	struct btrfs_key key;
	char *tmp;
	u64 cur = root->root_key.objectid;
	int ret = 0;

	tmp = malloc(PATH_MAX);
	if (!tmp)
		return -ENOMEM;
	tmp[0] = '\0';
	path_ret[0] = '\0';
	btrfs_init_path(&path);
	while (cur != BTRFS_FS_TREE_OBJECTID) {
		struct btrfs_root_ref *rr;
		struct btrfs_key location;
		int name_len;
		u64 ino;

		key.objectid = cur;
		key.type = BTRFS_ROOT_BACKREF_KEY;
		key.offset = (u64)-1;
		btrfs_release_path(&path);

		ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
		if (ret == 0)
			ret = -EUCLEAN;
		if (ret < 0)
			goto out;
		ret = btrfs_previous_item(tree_root, &path, cur,
					  BTRFS_ROOT_BACKREF_KEY);
		if (ret > 0)
			ret = -ENOENT;
		if (ret < 0)
			goto out;

		/* Get the subvolume name */
		rr = btrfs_item_ptr(path.nodes[0], path.slots[0],
				    struct btrfs_root_ref);
		strncpy(tmp, path_ret, PATH_MAX);
		name_len = btrfs_root_ref_name_len(path.nodes[0], rr);
		if (name_len > BTRFS_NAME_LEN) {
			ret = -ENAMETOOLONG;
			goto out;
		}
		ino = btrfs_root_ref_dirid(path.nodes[0], rr);
		read_extent_buffer(path.nodes[0], path_ret,
				   (unsigned long)(rr + 1), name_len);
		path_ret[name_len] = '/';
		path_ret[name_len + 1] = '\0';
		strncat(path_ret, tmp, PATH_MAX);

		/* Get the path inside the parent subvolume */
		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
		location.objectid = key.offset;
		location.type = BTRFS_ROOT_ITEM_KEY;
		location.offset = (u64)-1;
		root = btrfs_read_fs_root(fs_info, &location);
		if (IS_ERR(root)) {
			ret = PTR_ERR(root);
			goto out;
		}
		ret = get_path_in_subvol(root, ino, path_ret);
		if (ret < 0)
			goto out;
		cur = key.offset;
	}
	/* Add the leading '/' */
	strncpy(tmp, path_ret, PATH_MAX);
	strncpy(path_ret, "/", PATH_MAX);
	strncat(path_ret, tmp, PATH_MAX);
out:
	btrfs_release_path(&path);
	free(tmp);
	return ret;
}

static int list_subvolums(struct btrfs_fs_info *fs_info)
{
	struct btrfs_root *tree_root = fs_info->tree_root;
	struct btrfs_root *root;
	struct btrfs_path path;
	struct btrfs_key key;
	char *result;
	int ret = 0;

	result = malloc(PATH_MAX);
	if (!result)
		return -ENOMEM;

	ret = list_one_subvol(fs_info->fs_root, result);
	if (ret < 0)
		goto out;
	root = fs_info->fs_root;
	printf("ID %llu gen %llu path %.*s\n",
		root->root_key.objectid, btrfs_root_generation(&root->root_item),
		PATH_MAX, result);

	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
	key.type = BTRFS_ROOT_ITEM_KEY;
	key.offset = 0;
	btrfs_init_path(&path);
	ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
	if (ret < 0)
		goto out;
	while (1) {
		if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
			goto next;

		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
		if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
			break;
		if (key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
		    key.type != BTRFS_ROOT_ITEM_KEY)
			goto next;
		key.offset = (u64)-1;
		root = btrfs_read_fs_root(fs_info, &key);
		if (IS_ERR(root)) {
			ret = PTR_ERR(root);
			if (ret == -ENOENT)
				goto next;
		}
		ret = list_one_subvol(root, result);
		if (ret < 0)
			goto out;
		printf("ID %llu gen %llu path %.*s\n",
			root->root_key.objectid,
			btrfs_root_generation(&root->root_item),
			PATH_MAX, result);
next:
		ret = btrfs_next_item(tree_root, &path);
		if (ret < 0)
			goto out;
		if (ret > 0) {
			ret = 0;
			break;
		}
	}
out:
	free(result);
	return ret;
}

static int get_subvol_name(u64 subvolid, char *name, int max_len)
{
	struct btrfs_root_ref rref;
	struct btrfs_inode_ref iref;
	struct __btrfs_root root;
	u64 dir;
	char tmp[BTRFS_NAME_LEN];
	char *ptr;

	ptr = name + max_len - 1;
	*ptr = '\0';

	while (subvolid != BTRFS_FS_TREE_OBJECTID) {
		subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp);

		if (subvolid == -1ULL)
			return -1;

		ptr -= rref.name_len + 1;
		if (ptr < name)
			goto too_long;

		memcpy(ptr + 1, tmp, rref.name_len);
		*ptr = '/';

		if (btrfs_find_root(subvolid, &root, NULL))
			return -1;

		dir = rref.dirid;

		while (dir != BTRFS_FIRST_FREE_OBJECTID) {
			dir = __btrfs_lookup_inode_ref(&root, dir, &iref, tmp);

			if (dir == -1ULL)
				return -1;

			ptr -= iref.name_len + 1;
			if (ptr < name)
				goto too_long;

			memcpy(ptr + 1, tmp, iref.name_len);
			*ptr = '/';
		}
	}

	if (ptr == name + max_len - 1) {
		name[0] = '/';
		name[1] = '\0';
	} else {
		memmove(name, ptr, name + max_len - ptr);
	}

	return 0;

too_long:
	printf("%s: subvolume name too long\n", __func__);
	return -1;
}

u64 btrfs_get_default_subvol_objectid(void)
{
	struct btrfs_dir_item item;

	if (__btrfs_lookup_dir_item(&btrfs_info.tree_root,
				  btrfs_info.sb.root_dir_objectid, "default", 7,
				  &item))
		return BTRFS_FS_TREE_OBJECTID;
	return item.location.objectid;
}

static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
{
	struct btrfs_key key, *found_key;
	struct __btrfs_path path;
	struct btrfs_root_ref *ref;
	int res;

	key.objectid = tree;
	key.type = BTRFS_ROOT_REF_KEY;
	key.offset = 0;

	if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path))
		return;

	do {
		found_key = btrfs_path_leaf_key(&path);
		if (btrfs_comp_keys_type(&key, found_key))
			break;

		ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref);
		btrfs_root_ref_to_cpu(ref);

		printf("ID %llu parent %llu name ", found_key->offset, tree);
		if (nameptr && !get_subvol_name(found_key->offset, nameptr,
						max_name_len))
			printf("%s\n", nameptr);
		else
			printf("%.*s\n", (int) ref->name_len,
			       (const char *) (ref + 1));

		if (level > 0)
			list_subvols(found_key->offset, nameptr, max_name_len,
				     level - 1);
		else
			printf("%s: Too much recursion, maybe skipping some "
			       "subvolumes\n", __func__);
	} while (!(res = btrfs_next_slot(&path)));

	__btrfs_free_path(&path);
}

void btrfs_list_subvols(void)
{
	struct btrfs_fs_info *fs_info = current_fs_info;
	int ret;

	if (!fs_info)
		return;
	ret = list_subvolums(fs_info);
	if (ret < 0)
		error("failed to list subvolume: %d", ret);
}