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.
19 #include <sys/ioctl.h>
20 #include <sys/mount.h>
23 #include <sys/types.h>
30 #include "transaction.h"
33 #include <uuid/uuid.h>
34 #include "btrfs-list.h"
35 #include "rbtree-utils.h"
37 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
38 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
40 /* we store all the roots we find in an rbtree so that we can
41 * search for them later.
51 } btrfs_list_columns[] = {
64 .column_name = "CGen",
69 .column_name = "Parent",
74 .column_name = "Top Level",
79 .column_name = "OTime",
83 .name = "parent_uuid",
84 .column_name = "Parent UUID",
88 .name = "received_uuid",
89 .column_name = "Received UUID",
94 .column_name = "UUID",
99 .column_name = "Path",
109 static btrfs_list_filter_func all_filter_funcs[];
110 static btrfs_list_comp_func all_comp_funcs[];
112 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
116 ASSERT(0 <= column && column <= BTRFS_LIST_ALL);
118 if (column < BTRFS_LIST_ALL) {
119 btrfs_list_columns[column].need_print = 1;
123 for (i = 0; i < BTRFS_LIST_ALL; i++)
124 btrfs_list_columns[i].need_print = 1;
127 static int comp_entry_with_rootid(struct root_info *entry1,
128 struct root_info *entry2,
133 if (entry1->root_id > entry2->root_id)
135 else if (entry1->root_id < entry2->root_id)
140 return is_descending ? -ret : ret;
143 static int comp_entry_with_gen(struct root_info *entry1,
144 struct root_info *entry2,
149 if (entry1->gen > entry2->gen)
151 else if (entry1->gen < entry2->gen)
156 return is_descending ? -ret : ret;
159 static int comp_entry_with_ogen(struct root_info *entry1,
160 struct root_info *entry2,
165 if (entry1->ogen > entry2->ogen)
167 else if (entry1->ogen < entry2->ogen)
172 return is_descending ? -ret : ret;
175 static int comp_entry_with_path(struct root_info *entry1,
176 struct root_info *entry2,
181 if (strcmp(entry1->full_path, entry2->full_path) > 0)
183 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
188 return is_descending ? -ret : ret;
191 static btrfs_list_comp_func all_comp_funcs[] = {
192 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
193 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
194 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
195 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
198 static char *all_sort_items[] = {
199 [BTRFS_LIST_COMP_ROOTID] = "rootid",
200 [BTRFS_LIST_COMP_OGEN] = "ogen",
201 [BTRFS_LIST_COMP_GEN] = "gen",
202 [BTRFS_LIST_COMP_PATH] = "path",
203 [BTRFS_LIST_COMP_MAX] = NULL,
206 static int btrfs_list_get_sort_item(char *sort_name)
210 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
211 if (strcmp(sort_name, all_sort_items[i]) == 0)
217 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
219 struct btrfs_list_comparer_set *set;
222 size = sizeof(struct btrfs_list_comparer_set) +
223 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
224 set = calloc(1, size);
226 fprintf(stderr, "memory allocation failed\n");
230 set->total = BTRFS_LIST_NCOMPS_INCREASE;
235 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
236 enum btrfs_list_comp_enum comparer, int is_descending)
238 struct btrfs_list_comparer_set *set = *comp_set;
242 ASSERT(comparer < BTRFS_LIST_COMP_MAX);
243 ASSERT(set->ncomps <= set->total);
245 if (set->ncomps == set->total) {
248 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
249 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
251 set = realloc(set, size);
253 fprintf(stderr, "memory allocation failed\n");
258 memset(&set->comps[set->total], 0,
259 BTRFS_LIST_NCOMPS_INCREASE *
260 sizeof(struct btrfs_list_comparer));
261 set->total += BTRFS_LIST_NCOMPS_INCREASE;
265 ASSERT(set->comps[set->ncomps].comp_func == NULL);
267 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
268 set->comps[set->ncomps].is_descending = is_descending;
273 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
274 struct btrfs_list_comparer_set *set)
276 int rootid_compared = 0;
279 if (!set || !set->ncomps)
280 return comp_entry_with_rootid(entry1, entry2, 0);
282 for (i = 0; i < set->ncomps; i++) {
283 if (!set->comps[i].comp_func)
286 ret = set->comps[i].comp_func(entry1, entry2,
287 set->comps[i].is_descending);
291 if (set->comps[i].comp_func == comp_entry_with_rootid)
295 if (!rootid_compared)
296 ret = comp_entry_with_rootid(entry1, entry2, 0);
301 static int sort_tree_insert(struct root_lookup *sort_tree,
302 struct root_info *ins,
303 struct btrfs_list_comparer_set *comp_set)
305 struct rb_node **p = &sort_tree->root.rb_node;
306 struct rb_node *parent = NULL;
307 struct root_info *curr;
312 curr = rb_entry(parent, struct root_info, sort_node);
314 ret = sort_comp(ins, curr, comp_set);
323 rb_link_node(&ins->sort_node, parent, p);
324 rb_insert_color(&ins->sort_node, &sort_tree->root);
329 * insert a new root into the tree. returns the existing root entry
330 * if one is already there. Both root_id and ref_tree are used
333 static int root_tree_insert(struct root_lookup *root_tree,
334 struct root_info *ins)
336 struct rb_node **p = &root_tree->root.rb_node;
337 struct rb_node * parent = NULL;
338 struct root_info *curr;
343 curr = rb_entry(parent, struct root_info, rb_node);
345 ret = comp_entry_with_rootid(ins, curr, 0);
354 rb_link_node(&ins->rb_node, parent, p);
355 rb_insert_color(&ins->rb_node, &root_tree->root);
360 * find a given root id in the tree. We return the smallest one,
361 * rb_next can be used to move forward looking for more if required
363 static struct root_info *root_tree_search(struct root_lookup *root_tree,
366 struct rb_node *n = root_tree->root.rb_node;
367 struct root_info *entry;
368 struct root_info tmp;
371 tmp.root_id = root_id;
374 entry = rb_entry(n, struct root_info, rb_node);
376 ret = comp_entry_with_rootid(&tmp, entry, 0);
387 static int update_root(struct root_lookup *root_lookup,
388 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
389 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
390 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
392 struct root_info *ri;
394 ri = root_tree_search(root_lookup, root_id);
395 if (!ri || ri->root_id != root_id)
397 if (name && name_len > 0) {
400 ri->name = malloc(name_len + 1);
402 fprintf(stderr, "memory allocation failed\n");
405 strncpy(ri->name, name, name_len);
406 ri->name[name_len] = 0;
409 ri->ref_tree = ref_tree;
411 ri->root_offset = root_offset;
420 if (!ri->ogen && root_offset)
421 ri->ogen = root_offset;
425 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
427 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
429 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
435 * add_root - update the existed root, or allocate a new root and insert it
436 * into the lookup tree.
437 * root_id: object id of the root
438 * ref_tree: object id of the referring root.
439 * root_offset: offset value of the root'key
440 * dir_id: inode id of the directory in ref_tree where this root can be found.
441 * name: the name of root_id in that directory
442 * name_len: the length of name
443 * ogen: the original generation of the root
444 * gen: the current generation of the root
445 * otime: the original time (creation time) of the root
446 * uuid: uuid of the root
447 * puuid: uuid of the root parent if any
448 * ruuid: uuid of the received subvol, if any
450 static int add_root(struct root_lookup *root_lookup,
451 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
452 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
453 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
455 struct root_info *ri;
458 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
459 dir_id, name, name_len, ogen, gen, otime,
464 ri = calloc(1, sizeof(*ri));
466 printf("memory allocation failed\n");
469 ri->root_id = root_id;
471 if (name && name_len > 0) {
472 ri->name = malloc(name_len + 1);
474 fprintf(stderr, "memory allocation failed\n");
477 strncpy(ri->name, name, name_len);
478 ri->name[name_len] = 0;
481 ri->ref_tree = ref_tree;
485 ri->root_offset = root_offset;
492 if (!ri->ogen && root_offset)
493 ri->ogen = root_offset;
498 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
501 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
504 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
506 ret = root_tree_insert(root_lookup, ri);
508 error("failed to insert subvolume %llu to tree: %s",
509 (unsigned long long)root_id, strerror(-ret));
516 * Simplified add_root for back references, omits the uuid and original info
517 * parameters, root offset and flags.
519 static int add_root_backref(struct root_lookup *root_lookup, u64 root_id,
520 u64 ref_tree, u64 dir_id, char *name, int name_len)
522 return add_root(root_lookup, root_id, ref_tree, 0, 0, dir_id, name,
523 name_len, 0, 0, 0, NULL, NULL, NULL);
527 static void free_root_info(struct rb_node *node)
529 struct root_info *ri;
531 ri = rb_entry(node, struct root_info, rb_node);
539 * for a given root_info, search through the root_lookup tree to construct
540 * the full path name to it.
542 * This can't be called until all the root_info->path fields are filled
543 * in by lookup_ino_path
545 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
548 char *full_path = NULL;
550 struct root_info *found;
553 * we go backwards from the root_info object and add pathnames
554 * from parent directories as we go.
563 * ref_tree = 0 indicates the subvolume
566 if (!found->ref_tree) {
571 add_len = strlen(found->path);
574 /* room for / and for null */
575 tmp = malloc(add_len + 2 + len);
577 perror("malloc failed");
580 memcpy(tmp + add_len + 1, full_path, len);
582 memcpy(tmp, found->path, add_len);
583 tmp [add_len + len + 1] = '\0';
588 full_path = strdup(found->path);
592 ri->top_id = found->ref_tree;
594 next = found->ref_tree;
598 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
601 if (next == BTRFS_FS_TREE_OBJECTID)
604 * if the ref_tree wasn't in our tree of roots, the
605 * subvolume was deleted.
607 found = root_tree_search(rl, next);
614 ri->full_path = full_path;
620 * for a single root_info, ask the kernel to give us a path name
621 * inside it's ref_root for the dir_id where it lives.
623 * This fills in root_info->path with the path to the directory and and
624 * appends this root's name.
626 static int lookup_ino_path(int fd, struct root_info *ri)
628 struct btrfs_ioctl_ino_lookup_args args;
637 memset(&args, 0, sizeof(args));
638 args.treeid = ri->ref_tree;
639 args.objectid = ri->dir_id;
641 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
643 if (errno == ENOENT) {
647 error("failed to lookup path for root %llu: %s",
648 (unsigned long long)ri->ref_tree, strerror(errno));
654 * we're in a subdirectory of ref_tree, the kernel ioctl
655 * puts a / in there for us
657 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
659 perror("malloc failed");
662 strcpy(ri->path, args.name);
663 strcat(ri->path, ri->name);
665 /* we're at the root of ref_tree */
666 ri->path = strdup(ri->name);
668 perror("strdup failed");
675 /* finding the generation for a given path is a two step process.
676 * First we use the inode lookup routine to find out the root id
678 * Then we use the tree search ioctl to scan all the root items for a
679 * given root id and spit out the latest generation we can find
681 static u64 find_root_gen(int fd)
683 struct btrfs_ioctl_ino_lookup_args ino_args;
685 struct btrfs_ioctl_search_args args;
686 struct btrfs_ioctl_search_key *sk = &args.key;
687 struct btrfs_ioctl_search_header sh;
688 unsigned long off = 0;
692 memset(&ino_args, 0, sizeof(ino_args));
693 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
695 /* this ioctl fills in ino_args->treeid */
696 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
698 error("failed to lookup path for dirid %llu: %s",
699 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
704 memset(&args, 0, sizeof(args));
706 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
709 * there may be more than one ROOT_ITEM key if there are
710 * snapshots pending deletion, we have to loop through
713 sk->min_objectid = ino_args.treeid;
714 sk->max_objectid = ino_args.treeid;
715 sk->max_type = BTRFS_ROOT_ITEM_KEY;
716 sk->min_type = BTRFS_ROOT_ITEM_KEY;
717 sk->max_offset = (u64)-1;
718 sk->max_transid = (u64)-1;
722 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
724 error("can't perform the search: %s", strerror(errno));
727 /* the ioctl returns the number of item it found in nr_items */
728 if (sk->nr_items == 0)
732 for (i = 0; i < sk->nr_items; i++) {
733 struct btrfs_root_item *item;
735 memcpy(&sh, args.buf + off, sizeof(sh));
737 item = (struct btrfs_root_item *)(args.buf + off);
740 sk->min_objectid = sh.objectid;
741 sk->min_type = sh.type;
742 sk->min_offset = sh.offset;
744 if (sh.objectid > ino_args.treeid)
747 if (sh.objectid == ino_args.treeid &&
748 sh.type == BTRFS_ROOT_ITEM_KEY) {
749 max_found = max(max_found,
750 btrfs_root_generation(item));
753 if (sk->min_offset < (u64)-1)
758 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
760 if (sk->min_objectid != ino_args.treeid)
766 /* pass in a directory id and this will return
767 * the full path of the parent directory inside its
770 * It may return NULL if it is in the root, or an ERR_PTR if things
773 static char *__ino_resolve(int fd, u64 dirid)
775 struct btrfs_ioctl_ino_lookup_args args;
779 memset(&args, 0, sizeof(args));
780 args.objectid = dirid;
782 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
784 error("failed to lookup path for dirid %llu: %s",
785 (unsigned long long)dirid, strerror(errno));
791 * we're in a subdirectory of ref_tree, the kernel ioctl
792 * puts a / in there for us
794 full = strdup(args.name);
796 perror("malloc failed");
797 return ERR_PTR(-ENOMEM);
800 /* we're at the root of ref_tree */
807 * simple string builder, returning a new string with both
810 static char *build_name(const char *dirid, const char *name)
817 full = malloc(strlen(dirid) + strlen(name) + 1);
826 * given an inode number, this returns the full path name inside the subvolume
827 * to that file/directory. cache_dirid and cache_name are used to
828 * cache the results so we can avoid tree searches if a later call goes
829 * to the same directory or file name
831 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
839 struct btrfs_ioctl_search_args args;
840 struct btrfs_ioctl_search_key *sk = &args.key;
841 struct btrfs_ioctl_search_header *sh;
842 unsigned long off = 0;
845 memset(&args, 0, sizeof(args));
850 * step one, we search for the inode back ref. We just use the first
853 sk->min_objectid = ino;
854 sk->max_objectid = ino;
855 sk->max_type = BTRFS_INODE_REF_KEY;
856 sk->max_offset = (u64)-1;
857 sk->min_type = BTRFS_INODE_REF_KEY;
858 sk->max_transid = (u64)-1;
861 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
863 error("can't perform the search: %s", strerror(errno));
866 /* the ioctl returns the number of item it found in nr_items */
867 if (sk->nr_items == 0)
871 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
873 if (btrfs_search_header_type(sh) == BTRFS_INODE_REF_KEY) {
874 struct btrfs_inode_ref *ref;
875 dirid = btrfs_search_header_offset(sh);
877 ref = (struct btrfs_inode_ref *)(sh + 1);
878 namelen = btrfs_stack_inode_ref_name_len(ref);
880 name = (char *)(ref + 1);
881 name = strndup(name, namelen);
883 /* use our cached value */
884 if (dirid == *cache_dirid && *cache_name) {
885 dirname = *cache_name;
892 * the inode backref gives us the file name and the parent directory id.
893 * From here we use __ino_resolve to get the path to the parent
895 dirname = __ino_resolve(fd, dirid);
897 full = build_name(dirname, name);
898 if (*cache_name && dirname != *cache_name)
901 *cache_name = dirname;
902 *cache_dirid = dirid;
908 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
910 struct btrfs_ioctl_search_args args;
911 struct btrfs_ioctl_search_key *sk = &args.key;
912 struct btrfs_ioctl_search_header *sh;
916 memset(&args, 0, sizeof(args));
919 * search for a dir item with a name 'default' in the tree of
920 * tree roots, it should point us to a default root
922 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
924 /* don't worry about ancient format and request only one item */
927 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
928 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
929 sk->max_type = BTRFS_DIR_ITEM_KEY;
930 sk->min_type = BTRFS_DIR_ITEM_KEY;
931 sk->max_offset = (u64)-1;
932 sk->max_transid = (u64)-1;
934 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
938 /* the ioctl returns the number of items it found in nr_items */
939 if (sk->nr_items == 0)
942 sh = (struct btrfs_ioctl_search_header *)args.buf;
944 if (btrfs_search_header_type(sh) == BTRFS_DIR_ITEM_KEY) {
945 struct btrfs_dir_item *di;
949 di = (struct btrfs_dir_item *)(sh + 1);
950 name_len = btrfs_stack_dir_name_len(di);
951 name = (char *)(di + 1);
953 if (!strncmp("default", name, name_len))
954 found = btrfs_disk_key_objectid(&di->location);
962 static int list_subvol_search(int fd, struct root_lookup *root_lookup)
965 struct btrfs_ioctl_search_args args;
966 struct btrfs_ioctl_search_key *sk = &args.key;
967 struct btrfs_ioctl_search_header sh;
968 struct btrfs_root_ref *ref;
969 struct btrfs_root_item *ri;
979 root_lookup->root.rb_node = NULL;
980 memset(&args, 0, sizeof(args));
982 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
983 /* Search both live and deleted subvolumes */
984 sk->min_type = BTRFS_ROOT_ITEM_KEY;
985 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
986 sk->min_objectid = BTRFS_FS_TREE_OBJECTID;
987 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
988 sk->max_offset = (u64)-1;
989 sk->max_transid = (u64)-1;
993 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
996 if (sk->nr_items == 0)
1002 * for each item, pull the key out of the header and then
1003 * read the root_ref item it contains
1005 for (i = 0; i < sk->nr_items; i++) {
1006 memcpy(&sh, args.buf + off, sizeof(sh));
1008 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1009 ref = (struct btrfs_root_ref *)(args.buf + off);
1010 name_len = btrfs_stack_root_ref_name_len(ref);
1011 name = (char *)(ref + 1);
1012 dir_id = btrfs_stack_root_ref_dirid(ref);
1014 add_root_backref(root_lookup, sh.objectid,
1015 sh.offset, dir_id, name,
1017 } else if (sh.type == BTRFS_ROOT_ITEM_KEY &&
1018 (sh.objectid >= BTRFS_FIRST_FREE_OBJECTID ||
1019 sh.objectid == BTRFS_FS_TREE_OBJECTID)) {
1021 u8 uuid[BTRFS_UUID_SIZE];
1022 u8 puuid[BTRFS_UUID_SIZE];
1023 u8 ruuid[BTRFS_UUID_SIZE];
1025 ri = (struct btrfs_root_item *)(args.buf + off);
1026 gen = btrfs_root_generation(ri);
1027 flags = btrfs_root_flags(ri);
1029 sizeof(struct btrfs_root_item_v0)) {
1030 otime = btrfs_stack_timespec_sec(&ri->otime);
1031 ogen = btrfs_root_otransid(ri);
1032 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1033 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1034 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1038 memset(uuid, 0, BTRFS_UUID_SIZE);
1039 memset(puuid, 0, BTRFS_UUID_SIZE);
1040 memset(ruuid, 0, BTRFS_UUID_SIZE);
1043 add_root(root_lookup, sh.objectid, 0,
1044 sh.offset, flags, 0, NULL, 0, ogen,
1045 gen, otime, uuid, puuid, ruuid);
1050 sk->min_objectid = sh.objectid;
1051 sk->min_type = sh.type;
1052 sk->min_offset = sh.offset;
1055 if (!sk->min_offset)
1060 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1061 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1066 if (sk->min_objectid > sk->max_objectid)
1073 static int filter_by_rootid(struct root_info *ri, u64 data)
1075 return ri->root_id == data;
1078 static int filter_snapshot(struct root_info *ri, u64 data)
1080 return !!ri->root_offset;
1083 static int filter_flags(struct root_info *ri, u64 flags)
1085 return ri->flags & flags;
1088 static int filter_gen_more(struct root_info *ri, u64 data)
1090 return ri->gen >= data;
1093 static int filter_gen_less(struct root_info *ri, u64 data)
1095 return ri->gen <= data;
1098 static int filter_gen_equal(struct root_info *ri, u64 data)
1100 return ri->gen == data;
1103 static int filter_cgen_more(struct root_info *ri, u64 data)
1105 return ri->ogen >= data;
1108 static int filter_cgen_less(struct root_info *ri, u64 data)
1110 return ri->ogen <= data;
1113 static int filter_cgen_equal(struct root_info *ri, u64 data)
1115 return ri->ogen == data;
1118 static int filter_topid_equal(struct root_info *ri, u64 data)
1120 return ri->top_id == data;
1123 static int filter_full_path(struct root_info *ri, u64 data)
1125 if (ri->full_path && ri->top_id != data) {
1127 char p[] = "<FS_TREE>";
1128 int add_len = strlen(p);
1129 int len = strlen(ri->full_path);
1131 tmp = malloc(len + add_len + 2);
1133 fprintf(stderr, "memory allocation failed\n");
1136 memcpy(tmp + add_len + 1, ri->full_path, len);
1137 tmp[len + add_len + 1] = '\0';
1139 memcpy(tmp, p, add_len);
1140 free(ri->full_path);
1141 ri->full_path = tmp;
1146 static int filter_by_parent(struct root_info *ri, u64 data)
1148 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1151 static int filter_deleted(struct root_info *ri, u64 data)
1156 static btrfs_list_filter_func all_filter_funcs[] = {
1157 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1158 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1159 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1160 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1161 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1162 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1163 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1164 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1165 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1166 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1167 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1168 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1169 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1172 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1174 struct btrfs_list_filter_set *set;
1177 size = sizeof(struct btrfs_list_filter_set) +
1178 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1179 set = calloc(1, size);
1181 fprintf(stderr, "memory allocation failed\n");
1185 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1191 * Setup list filters. Exit if there's not enough memory, as we can't continue
1192 * without the structures set up properly.
1194 void btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1195 enum btrfs_list_filter_enum filter, u64 data)
1197 struct btrfs_list_filter_set *set = *filter_set;
1200 ASSERT(set != NULL);
1201 ASSERT(filter < BTRFS_LIST_FILTER_MAX);
1202 ASSERT(set->nfilters <= set->total);
1204 if (set->nfilters == set->total) {
1207 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1208 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1210 set = realloc(set, size);
1212 fprintf(stderr, "memory allocation failed\n");
1217 memset(&set->filters[set->total], 0,
1218 BTRFS_LIST_NFILTERS_INCREASE *
1219 sizeof(struct btrfs_list_filter));
1220 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1224 ASSERT(set->filters[set->nfilters].filter_func == NULL);
1226 if (filter == BTRFS_LIST_FILTER_DELETED)
1227 set->only_deleted = 1;
1229 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1230 set->filters[set->nfilters].data = data;
1234 static int filter_root(struct root_info *ri,
1235 struct btrfs_list_filter_set *set)
1242 if (set->only_deleted && !ri->deleted)
1245 if (!set->only_deleted && ri->deleted)
1248 for (i = 0; i < set->nfilters; i++) {
1249 if (!set->filters[i].filter_func)
1251 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1258 static void filter_and_sort_subvol(struct root_lookup *all_subvols,
1259 struct root_lookup *sort_tree,
1260 struct btrfs_list_filter_set *filter_set,
1261 struct btrfs_list_comparer_set *comp_set,
1265 struct root_info *entry;
1268 sort_tree->root.rb_node = NULL;
1270 n = rb_last(&all_subvols->root);
1272 entry = rb_entry(n, struct root_info, rb_node);
1274 ret = resolve_root(all_subvols, entry, top_id);
1275 if (ret == -ENOENT) {
1276 entry->full_path = strdup("DELETED");
1279 ret = filter_root(entry, filter_set);
1281 sort_tree_insert(sort_tree, entry, comp_set);
1286 static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1290 n = rb_first(&root_lookup->root);
1292 struct root_info *entry;
1294 entry = rb_entry(n, struct root_info, rb_node);
1295 ret = lookup_ino_path(fd, entry);
1296 if (ret && ret != -ENOENT)
1304 static void print_subvolume_column(struct root_info *subv,
1305 enum btrfs_list_column_enum column)
1308 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1310 ASSERT(0 <= column && column < BTRFS_LIST_ALL);
1313 case BTRFS_LIST_OBJECTID:
1314 printf("%llu", subv->root_id);
1316 case BTRFS_LIST_GENERATION:
1317 printf("%llu", subv->gen);
1319 case BTRFS_LIST_OGENERATION:
1320 printf("%llu", subv->ogen);
1322 case BTRFS_LIST_PARENT:
1323 printf("%llu", subv->ref_tree);
1325 case BTRFS_LIST_TOP_LEVEL:
1326 printf("%llu", subv->top_id);
1328 case BTRFS_LIST_OTIME:
1332 localtime_r(&subv->otime, &tm);
1333 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1338 case BTRFS_LIST_UUID:
1339 if (uuid_is_null(subv->uuid))
1340 strcpy(uuidparse, "-");
1342 uuid_unparse(subv->uuid, uuidparse);
1343 printf("%s", uuidparse);
1345 case BTRFS_LIST_PUUID:
1346 if (uuid_is_null(subv->puuid))
1347 strcpy(uuidparse, "-");
1349 uuid_unparse(subv->puuid, uuidparse);
1350 printf("%s", uuidparse);
1352 case BTRFS_LIST_RUUID:
1353 if (uuid_is_null(subv->ruuid))
1354 strcpy(uuidparse, "-");
1356 uuid_unparse(subv->ruuid, uuidparse);
1357 printf("%s", uuidparse);
1359 case BTRFS_LIST_PATH:
1360 BUG_ON(!subv->full_path);
1361 printf("%s", subv->full_path);
1368 static void print_one_subvol_info_raw(struct root_info *subv,
1369 const char *raw_prefix)
1373 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1374 if (!btrfs_list_columns[i].need_print)
1378 printf("%s",raw_prefix);
1380 print_subvolume_column(subv, i);
1385 static void print_one_subvol_info_table(struct root_info *subv)
1389 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1390 if (!btrfs_list_columns[i].need_print)
1393 print_subvolume_column(subv, i);
1395 if (i != BTRFS_LIST_PATH)
1398 if (i == BTRFS_LIST_TOP_LEVEL)
1404 static void print_one_subvol_info_default(struct root_info *subv)
1408 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1409 if (!btrfs_list_columns[i].need_print)
1412 printf("%s ", btrfs_list_columns[i].name);
1413 print_subvolume_column(subv, i);
1415 if (i != BTRFS_LIST_PATH)
1421 static void print_all_subvol_info_tab_head(void)
1427 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1428 if (btrfs_list_columns[i].need_print)
1429 printf("%s\t", btrfs_list_columns[i].name);
1431 if (i == BTRFS_LIST_ALL-1)
1435 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1436 memset(barrier, 0, sizeof(barrier));
1438 if (btrfs_list_columns[i].need_print) {
1439 len = strlen(btrfs_list_columns[i].name);
1441 strcat(barrier, "-");
1443 printf("%s\t", barrier);
1445 if (i == BTRFS_LIST_ALL-1)
1450 static void print_all_subvol_info(struct root_lookup *sorted_tree,
1451 enum btrfs_list_layout layout, const char *raw_prefix)
1454 struct root_info *entry;
1456 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1457 print_all_subvol_info_tab_head();
1459 n = rb_first(&sorted_tree->root);
1461 entry = rb_entry(n, struct root_info, sort_node);
1463 case BTRFS_LIST_LAYOUT_DEFAULT:
1464 print_one_subvol_info_default(entry);
1466 case BTRFS_LIST_LAYOUT_TABLE:
1467 print_one_subvol_info_table(entry);
1469 case BTRFS_LIST_LAYOUT_RAW:
1470 print_one_subvol_info_raw(entry, raw_prefix);
1477 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1481 ret = list_subvol_search(fd, root_lookup);
1483 error("can't perform the search: %s", strerror(errno));
1488 * now we have an rbtree full of root_info objects, but we need to fill
1489 * in their path names within the subvol that is referencing each one.
1491 ret = list_subvol_fill_paths(fd, root_lookup);
1495 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1496 struct btrfs_list_comparer_set *comp_set,
1497 enum btrfs_list_layout layout, int full_path,
1498 const char *raw_prefix)
1500 struct root_lookup root_lookup;
1501 struct root_lookup root_sort;
1506 ret = btrfs_list_get_path_rootid(fd, &top_id);
1510 ret = btrfs_list_subvols(fd, &root_lookup);
1513 filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1516 print_all_subvol_info(&root_sort, layout, raw_prefix);
1517 rb_free_nodes(&root_lookup.root, free_root_info);
1522 static char *strdup_or_null(const char *s)
1529 int btrfs_get_toplevel_subvol(int fd, struct root_info *the_ri)
1532 struct root_lookup rl;
1533 struct rb_node *rbn;
1534 struct root_info *ri;
1537 ret = btrfs_list_get_path_rootid(fd, &root_id);
1541 ret = btrfs_list_subvols(fd, &rl);
1545 rbn = rb_first(&rl.root);
1546 ri = rb_entry(rbn, struct root_info, rb_node);
1548 if (ri->root_id != BTRFS_FS_TREE_OBJECTID)
1551 memcpy(the_ri, ri, offsetof(struct root_info, path));
1552 the_ri->path = strdup_or_null("/");
1553 the_ri->name = strdup_or_null("<FS_TREE>");
1554 the_ri->full_path = strdup_or_null("/");
1555 rb_free_nodes(&rl.root, free_root_info);
1560 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1563 struct root_lookup rl;
1564 struct rb_node *rbn;
1565 struct root_info *ri;
1568 ret = btrfs_list_get_path_rootid(fd, &root_id);
1572 ret = btrfs_list_subvols(fd, &rl);
1576 rbn = rb_first(&rl.root);
1578 ri = rb_entry(rbn, struct root_info, rb_node);
1579 rr = resolve_root(&rl, ri, root_id);
1580 if (rr == -ENOENT) {
1586 if (!comp_entry_with_rootid(the_ri, ri, 0) ||
1587 !uuid_compare(the_ri->uuid, ri->uuid)) {
1588 memcpy(the_ri, ri, offsetof(struct root_info, path));
1589 the_ri->path = strdup_or_null(ri->path);
1590 the_ri->name = strdup_or_null(ri->name);
1591 the_ri->full_path = strdup_or_null(ri->full_path);
1597 rb_free_nodes(&rl.root, free_root_info);
1601 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1602 struct btrfs_file_extent_item *item,
1603 u64 found_gen, u64 *cache_dirid,
1604 char **cache_dir_name, u64 *cache_ino,
1605 char **cache_full_name)
1609 u64 disk_offset = 0;
1615 if (btrfs_search_header_objectid(sh) == *cache_ino) {
1616 name = *cache_full_name;
1617 } else if (*cache_full_name) {
1618 free(*cache_full_name);
1619 *cache_full_name = NULL;
1622 name = ino_resolve(fd, btrfs_search_header_objectid(sh),
1625 *cache_full_name = name;
1626 *cache_ino = btrfs_search_header_objectid(sh);
1631 type = btrfs_stack_file_extent_type(item);
1632 compressed = btrfs_stack_file_extent_compression(item);
1634 if (type == BTRFS_FILE_EXTENT_REG ||
1635 type == BTRFS_FILE_EXTENT_PREALLOC) {
1636 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1637 disk_offset = btrfs_stack_file_extent_offset(item);
1638 len = btrfs_stack_file_extent_num_bytes(item);
1639 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1642 len = btrfs_stack_file_extent_ram_bytes(item);
1645 "unhandled extent type %d for inode %llu file offset %llu gen %llu",
1647 (unsigned long long)btrfs_search_header_objectid(sh),
1648 (unsigned long long)btrfs_search_header_offset(sh),
1649 (unsigned long long)found_gen);
1653 printf("inode %llu file offset %llu len %llu disk start %llu "
1654 "offset %llu gen %llu flags ",
1655 (unsigned long long)btrfs_search_header_objectid(sh),
1656 (unsigned long long)btrfs_search_header_offset(sh),
1657 (unsigned long long)len,
1658 (unsigned long long)disk_start,
1659 (unsigned long long)disk_offset,
1660 (unsigned long long)found_gen);
1666 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1667 printf("%sPREALLOC", flags ? "|" : "");
1670 if (type == BTRFS_FILE_EXTENT_INLINE) {
1671 printf("%sINLINE", flags ? "|" : "");
1677 printf(" %s\n", name);
1681 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1684 struct btrfs_ioctl_search_args args;
1685 struct btrfs_ioctl_search_key *sk = &args.key;
1686 struct btrfs_ioctl_search_header sh;
1687 struct btrfs_file_extent_item *item;
1688 unsigned long off = 0;
1692 u64 cache_dirid = 0;
1694 char *cache_dir_name = NULL;
1695 char *cache_full_name = NULL;
1696 struct btrfs_file_extent_item backup;
1698 memset(&backup, 0, sizeof(backup));
1699 memset(&args, 0, sizeof(args));
1701 sk->tree_id = root_id;
1704 * set all the other params to the max, we'll take any objectid
1707 sk->max_objectid = (u64)-1;
1708 sk->max_offset = (u64)-1;
1709 sk->max_transid = (u64)-1;
1710 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1711 sk->min_transid = oldest_gen;
1712 /* just a big number, doesn't matter much */
1713 sk->nr_items = 4096;
1715 max_found = find_root_gen(fd);
1717 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1719 error("can't perform the search: %s", strerror(errno));
1722 /* the ioctl returns the number of item it found in nr_items */
1723 if (sk->nr_items == 0)
1729 * for each item, pull the key out of the header and then
1730 * read the root_ref item it contains
1732 for (i = 0; i < sk->nr_items; i++) {
1733 memcpy(&sh, args.buf + off, sizeof(sh));
1737 * just in case the item was too big, pass something other
1743 item = (struct btrfs_file_extent_item *)(args.buf +
1745 found_gen = btrfs_stack_file_extent_generation(item);
1746 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1747 found_gen >= oldest_gen) {
1748 print_one_extent(fd, &sh, item, found_gen,
1749 &cache_dirid, &cache_dir_name,
1750 &cache_ino, &cache_full_name);
1755 * record the mins in sk so we can make sure the
1756 * next search doesn't repeat this root
1758 sk->min_objectid = sh.objectid;
1759 sk->min_offset = sh.offset;
1760 sk->min_type = sh.type;
1762 sk->nr_items = 4096;
1763 if (sk->min_offset < (u64)-1)
1765 else if (sk->min_objectid < (u64)-1) {
1772 free(cache_dir_name);
1773 free(cache_full_name);
1774 printf("transid marker was %llu\n", (unsigned long long)max_found);
1778 char *btrfs_list_path_for_root(int fd, u64 root)
1780 struct root_lookup root_lookup;
1782 char *ret_path = NULL;
1786 ret = btrfs_list_get_path_rootid(fd, &top_id);
1788 return ERR_PTR(ret);
1790 ret = list_subvol_search(fd, &root_lookup);
1792 return ERR_PTR(ret);
1794 ret = list_subvol_fill_paths(fd, &root_lookup);
1796 return ERR_PTR(ret);
1798 n = rb_last(&root_lookup.root);
1800 struct root_info *entry;
1802 entry = rb_entry(n, struct root_info, rb_node);
1803 ret = resolve_root(&root_lookup, entry, top_id);
1804 if (ret == -ENOENT && entry->root_id == root) {
1808 if (entry->root_id == root) {
1809 ret_path = entry->full_path;
1810 entry->full_path = NULL;
1815 rb_free_nodes(&root_lookup.root, free_root_info);
1820 int btrfs_list_parse_sort_string(char *opt_arg,
1821 struct btrfs_list_comparer_set **comps)
1829 while ((p = strtok(opt_arg, ",")) != NULL) {
1831 ptr_argv = all_sort_items;
1834 if (strcmp(*ptr_argv, p) == 0) {
1839 if (strcmp(*ptr_argv, p) == 0) {
1856 } else if (*p == '-') {
1862 what_to_sort = btrfs_list_get_sort_item(p);
1863 btrfs_list_setup_comparer(comps, what_to_sort, order);
1872 * This function is used to parse the argument of filter condition.
1874 * type is the filter object.
1876 int btrfs_list_parse_filter_string(char *opt_arg,
1877 struct btrfs_list_filter_set **filters,
1878 enum btrfs_list_filter_enum type)
1883 switch (*(opt_arg++)) {
1885 arg = arg_strtou64(opt_arg);
1888 btrfs_list_setup_filter(filters, type, arg);
1891 arg = arg_strtou64(opt_arg);
1894 btrfs_list_setup_filter(filters, type, arg);
1898 arg = arg_strtou64(opt_arg);
1900 btrfs_list_setup_filter(filters, type, arg);
1907 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1911 ret = lookup_path_rootid(fd, treeid);
1913 error("cannot resolve rootid for path: %s",