2 * Copyright (C) 2010 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
26 #include <sys/types.h>
32 #include "kerncompat.h"
34 #include "transaction.h"
38 /* we store all the roots we find in an rbtree so that we can
39 * search for them later.
46 * one of these for each root we find.
49 struct rb_node rb_node;
54 /* the id of the root that references this one */
57 /* the dir id we're in from ref_tree */
60 /* path from the subvol we live in to this root, including the
61 * root's name. This is null until we do the extra lookup ioctl.
65 /* the name of this root in the directory it lives in */
69 void root_lookup_init(struct root_lookup *tree)
71 tree->root.rb_node = NULL;
74 static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree)
76 if (entry->root_id > root_id)
78 if (entry->root_id < root_id)
80 if (entry->ref_tree > ref_tree)
82 if (entry->ref_tree < ref_tree)
88 * insert a new root into the tree. returns the existing root entry
89 * if one is already there. Both root_id and ref_tree are used
92 static struct rb_node *tree_insert(struct rb_root *root, u64 root_id,
93 u64 ref_tree, struct rb_node *node)
95 struct rb_node ** p = &root->rb_node;
96 struct rb_node * parent = NULL;
97 struct root_info *entry;
102 entry = rb_entry(parent, struct root_info, rb_node);
104 comp = comp_entry(entry, root_id, ref_tree);
114 entry = rb_entry(parent, struct root_info, rb_node);
115 rb_link_node(node, parent, p);
116 rb_insert_color(node, root);
121 * find a given root id in the tree. We return the smallest one,
122 * rb_next can be used to move forward looking for more if required
124 static struct root_info *tree_search(struct rb_root *root, u64 root_id)
126 struct rb_node * n = root->rb_node;
127 struct root_info *entry;
130 entry = rb_entry(n, struct root_info, rb_node);
132 if (entry->root_id < root_id)
134 else if (entry->root_id > root_id)
137 struct root_info *prev;
138 struct rb_node *prev_n;
143 prev = rb_entry(prev_n, struct root_info,
145 if (prev->root_id != root_id)
157 * this allocates a new root in the lookup tree.
159 * root_id should be the object id of the root
161 * ref_tree is the objectid of the referring root.
163 * dir_id is the directory in ref_tree where this root_id can be found.
165 * name is the name of root_id in that directory
167 * name_len is the length of name
169 static int add_root(struct root_lookup *root_lookup,
170 u64 root_id, u64 ref_tree, u64 dir_id, char *name,
173 struct root_info *ri;
175 ri = malloc(sizeof(*ri) + name_len + 1);
177 printf("memory allocation failed\n");
180 memset(ri, 0, sizeof(*ri) + name_len + 1);
183 ri->root_id = root_id;
184 ri->ref_tree = ref_tree;
185 strncpy(ri->name, name, name_len);
187 ret = tree_insert(&root_lookup->root, root_id, ref_tree, &ri->rb_node);
189 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
196 * for a given root_info, search through the root_lookup tree to construct
197 * the full path name to it.
199 * This can't be called until all the root_info->path fields are filled
200 * in by lookup_ino_path
202 static int resolve_root(struct root_lookup *rl, struct root_info *ri)
205 char *full_path = NULL;
207 struct root_info *found;
210 * we go backwards from the root_info object and add pathnames
211 * from parent directories as we go.
217 int add_len = strlen(found->path);
219 /* room for / and for null */
220 tmp = malloc(add_len + 2 + len);
222 memcpy(tmp + add_len + 1, full_path, len);
224 memcpy(tmp, found->path, add_len);
225 tmp [add_len + len + 1] = '\0';
230 full_path = strdup(found->path);
234 next = found->ref_tree;
235 /* if the ref_tree refers to ourselves, we're at the top */
236 if (next == found->root_id) {
242 * if the ref_tree wasn't in our tree of roots, we're
245 found = tree_search(&rl->root, next);
251 printf("ID %llu top level %llu path %s\n", ri->root_id, top_id,
258 * for a single root_info, ask the kernel to give us a path name
259 * inside it's ref_root for the dir_id where it lives.
261 * This fills in root_info->path with the path to the directory and and
262 * appends this root's name.
264 static int lookup_ino_path(int fd, struct root_info *ri)
266 struct btrfs_ioctl_ino_lookup_args args;
272 memset(&args, 0, sizeof(args));
273 args.treeid = ri->ref_tree;
274 args.objectid = ri->dir_id;
276 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
278 fprintf(stderr, "Failed to lookup path for root %llu\n",
279 (unsigned long long)ri->ref_tree);
285 * we're in a subdirectory of ref_tree, the kernel ioctl
286 * puts a / in there for us
288 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
290 perror("malloc failed");
293 strcpy(ri->path, args.name);
294 strcat(ri->path, ri->name);
296 /* we're at the root of ref_tree */
297 ri->path = strdup(ri->name);
299 perror("strdup failed");
306 int list_subvols(int fd)
308 struct root_lookup root_lookup;
311 struct btrfs_ioctl_search_args args;
312 struct btrfs_ioctl_search_key *sk = &args.key;
313 struct btrfs_ioctl_search_header *sh;
314 struct btrfs_root_ref *ref;
315 unsigned long off = 0;
321 root_lookup_init(&root_lookup);
323 memset(&args, 0, sizeof(args));
325 /* search in the tree of tree roots */
329 * set the min and max to backref keys. The search will
330 * only send back this type of key now.
332 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
333 sk->min_type = BTRFS_ROOT_BACKREF_KEY;
336 * set all the other params to the max, we'll take any objectid
339 sk->max_objectid = (u64)-1;
340 sk->max_offset = (u64)-1;
341 sk->max_transid = (u64)-1;
343 /* just a big number, doesn't matter much */
347 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
352 /* the ioctl returns the number of item it found in nr_items */
353 if (sk->nr_items == 0)
359 * for each item, pull the key out of the header and then
360 * read the root_ref item it contains
362 for (i = 0; i < sk->nr_items; i++) {
363 sh = (struct btrfs_ioctl_search_header *)(args.buf +
367 ref = (struct btrfs_root_ref *)(args.buf + off);
368 name_len = btrfs_stack_root_ref_name_len(ref);
369 name = (char *)(ref + 1);
370 dir_id = btrfs_stack_root_ref_dirid(ref);
372 add_root(&root_lookup, sh->objectid, sh->offset,
373 dir_id, name, name_len);
378 * record the mins in sk so we can make sure the
379 * next search doesn't repeat this root
381 sk->min_objectid = sh->objectid;
382 sk->min_type = sh->type;
383 sk->min_offset = sh->offset;
386 /* this iteration is done, step forward one root for the next
389 if (sk->min_objectid < (u64)-1)
395 * now we have an rbtree full of root_info objects, but we need to fill
396 * in their path names within the subvol that is referencing each one.
398 n = rb_first(&root_lookup.root);
400 struct root_info *entry;
401 entry = rb_entry(n, struct root_info, rb_node);
402 lookup_ino_path(fd, entry);
406 /* now that we have all the subvol-relative paths filled in,
407 * we have to string the subvols together so that we can get
408 * a path all the way back to the FS root
410 n = rb_last(&root_lookup.root);
412 struct root_info *entry;
413 entry = rb_entry(n, struct root_info, rb_node);
414 resolve_root(&root_lookup, entry);