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 void root_lookup_init(struct root_lookup *tree)
129 tree->root.rb_node = NULL;
132 static int comp_entry_with_rootid(struct root_info *entry1,
133 struct root_info *entry2,
138 if (entry1->root_id > entry2->root_id)
140 else if (entry1->root_id < entry2->root_id)
145 return is_descending ? -ret : ret;
148 static int comp_entry_with_gen(struct root_info *entry1,
149 struct root_info *entry2,
154 if (entry1->gen > entry2->gen)
156 else if (entry1->gen < entry2->gen)
161 return is_descending ? -ret : ret;
164 static int comp_entry_with_ogen(struct root_info *entry1,
165 struct root_info *entry2,
170 if (entry1->ogen > entry2->ogen)
172 else if (entry1->ogen < entry2->ogen)
177 return is_descending ? -ret : ret;
180 static int comp_entry_with_path(struct root_info *entry1,
181 struct root_info *entry2,
186 if (strcmp(entry1->full_path, entry2->full_path) > 0)
188 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
193 return is_descending ? -ret : ret;
196 static btrfs_list_comp_func all_comp_funcs[] = {
197 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
198 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
199 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
200 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
203 static char *all_sort_items[] = {
204 [BTRFS_LIST_COMP_ROOTID] = "rootid",
205 [BTRFS_LIST_COMP_OGEN] = "ogen",
206 [BTRFS_LIST_COMP_GEN] = "gen",
207 [BTRFS_LIST_COMP_PATH] = "path",
208 [BTRFS_LIST_COMP_MAX] = NULL,
211 static int btrfs_list_get_sort_item(char *sort_name)
215 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
216 if (strcmp(sort_name, all_sort_items[i]) == 0)
222 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
224 struct btrfs_list_comparer_set *set;
227 size = sizeof(struct btrfs_list_comparer_set) +
228 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
229 set = calloc(1, size);
231 fprintf(stderr, "memory allocation failed\n");
235 set->total = BTRFS_LIST_NCOMPS_INCREASE;
240 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
241 enum btrfs_list_comp_enum comparer, int is_descending)
243 struct btrfs_list_comparer_set *set = *comp_set;
247 ASSERT(comparer < BTRFS_LIST_COMP_MAX);
248 ASSERT(set->ncomps <= set->total);
250 if (set->ncomps == set->total) {
253 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
254 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
256 set = realloc(set, size);
258 fprintf(stderr, "memory allocation failed\n");
263 memset(&set->comps[set->total], 0,
264 BTRFS_LIST_NCOMPS_INCREASE *
265 sizeof(struct btrfs_list_comparer));
266 set->total += BTRFS_LIST_NCOMPS_INCREASE;
270 ASSERT(set->comps[set->ncomps].comp_func == NULL);
272 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
273 set->comps[set->ncomps].is_descending = is_descending;
278 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
279 struct btrfs_list_comparer_set *set)
281 int rootid_compared = 0;
284 if (!set || !set->ncomps)
287 for (i = 0; i < set->ncomps; i++) {
288 if (!set->comps[i].comp_func)
291 ret = set->comps[i].comp_func(entry1, entry2,
292 set->comps[i].is_descending);
296 if (set->comps[i].comp_func == comp_entry_with_rootid)
300 if (!rootid_compared) {
302 ret = comp_entry_with_rootid(entry1, entry2, 0);
308 static int sort_tree_insert(struct root_lookup *sort_tree,
309 struct root_info *ins,
310 struct btrfs_list_comparer_set *comp_set)
312 struct rb_node **p = &sort_tree->root.rb_node;
313 struct rb_node *parent = NULL;
314 struct root_info *curr;
319 curr = rb_entry(parent, struct root_info, sort_node);
321 ret = sort_comp(ins, curr, comp_set);
330 rb_link_node(&ins->sort_node, parent, p);
331 rb_insert_color(&ins->sort_node, &sort_tree->root);
336 * insert a new root into the tree. returns the existing root entry
337 * if one is already there. Both root_id and ref_tree are used
340 static int root_tree_insert(struct root_lookup *root_tree,
341 struct root_info *ins)
343 struct rb_node **p = &root_tree->root.rb_node;
344 struct rb_node * parent = NULL;
345 struct root_info *curr;
350 curr = rb_entry(parent, struct root_info, rb_node);
352 ret = comp_entry_with_rootid(ins, curr, 0);
361 rb_link_node(&ins->rb_node, parent, p);
362 rb_insert_color(&ins->rb_node, &root_tree->root);
367 * find a given root id in the tree. We return the smallest one,
368 * rb_next can be used to move forward looking for more if required
370 static struct root_info *root_tree_search(struct root_lookup *root_tree,
373 struct rb_node *n = root_tree->root.rb_node;
374 struct root_info *entry;
375 struct root_info tmp;
378 tmp.root_id = root_id;
381 entry = rb_entry(n, struct root_info, rb_node);
383 ret = comp_entry_with_rootid(&tmp, entry, 0);
394 static int update_root(struct root_lookup *root_lookup,
395 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
396 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
397 time_t ot, void *uuid, void *puuid, void *ruuid)
399 struct root_info *ri;
401 ri = root_tree_search(root_lookup, root_id);
402 if (!ri || ri->root_id != root_id)
404 if (name && name_len > 0) {
407 ri->name = malloc(name_len + 1);
409 fprintf(stderr, "memory allocation failed\n");
412 strncpy(ri->name, name, name_len);
413 ri->name[name_len] = 0;
416 ri->ref_tree = ref_tree;
418 ri->root_offset = root_offset;
427 if (!ri->ogen && root_offset)
428 ri->ogen = root_offset;
432 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
434 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
436 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
442 * add_root - update the existed root, or allocate a new root and insert it
443 * into the lookup tree.
444 * root_id: object id of the root
445 * ref_tree: object id of the referring root.
446 * root_offset: offset value of the root'key
447 * dir_id: inode id of the directory in ref_tree where this root can be found.
448 * name: the name of root_id in that directory
449 * name_len: the length of name
450 * ogen: the original generation of the root
451 * gen: the current generation of the root
452 * ot: the original time(create time) of the root
453 * uuid: uuid of the root
454 * puuid: uuid of the root parent if any
455 * ruuid: uuid of the received subvol, if any
457 static int add_root(struct root_lookup *root_lookup,
458 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
459 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
460 time_t ot, void *uuid, void *puuid, void *ruuid)
462 struct root_info *ri;
465 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
466 dir_id, name, name_len, ogen, gen, ot,
471 ri = calloc(1, sizeof(*ri));
473 printf("memory allocation failed\n");
476 ri->root_id = root_id;
478 if (name && name_len > 0) {
479 ri->name = malloc(name_len + 1);
481 fprintf(stderr, "memory allocation failed\n");
484 strncpy(ri->name, name, name_len);
485 ri->name[name_len] = 0;
488 ri->ref_tree = ref_tree;
492 ri->root_offset = root_offset;
499 if (!ri->ogen && root_offset)
500 ri->ogen = root_offset;
505 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
508 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
511 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
513 ret = root_tree_insert(root_lookup, ri);
515 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
521 static void free_root_info(struct rb_node *node)
523 struct root_info *ri;
525 ri = rb_entry(node, struct root_info, rb_node);
533 * for a given root_info, search through the root_lookup tree to construct
534 * the full path name to it.
536 * This can't be called until all the root_info->path fields are filled
537 * in by lookup_ino_path
539 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
542 char *full_path = NULL;
544 struct root_info *found;
547 * we go backwards from the root_info object and add pathnames
548 * from parent directories as we go.
557 * ref_tree = 0 indicates the subvolume
560 if (!found->ref_tree) {
565 add_len = strlen(found->path);
568 /* room for / and for null */
569 tmp = malloc(add_len + 2 + len);
571 perror("malloc failed");
574 memcpy(tmp + add_len + 1, full_path, len);
576 memcpy(tmp, found->path, add_len);
577 tmp [add_len + len + 1] = '\0';
582 full_path = strdup(found->path);
586 ri->top_id = found->ref_tree;
588 next = found->ref_tree;
592 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
595 if (next == BTRFS_FS_TREE_OBJECTID)
598 * if the ref_tree wasn't in our tree of roots, the
599 * subvolume was deleted.
601 found = root_tree_search(rl, next);
608 ri->full_path = full_path;
614 * for a single root_info, ask the kernel to give us a path name
615 * inside it's ref_root for the dir_id where it lives.
617 * This fills in root_info->path with the path to the directory and and
618 * appends this root's name.
620 static int lookup_ino_path(int fd, struct root_info *ri)
622 struct btrfs_ioctl_ino_lookup_args args;
631 memset(&args, 0, sizeof(args));
632 args.treeid = ri->ref_tree;
633 args.objectid = ri->dir_id;
635 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
637 if (errno == ENOENT) {
641 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
642 (unsigned long long)ri->ref_tree,
649 * we're in a subdirectory of ref_tree, the kernel ioctl
650 * puts a / in there for us
652 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
654 perror("malloc failed");
657 strcpy(ri->path, args.name);
658 strcat(ri->path, ri->name);
660 /* we're at the root of ref_tree */
661 ri->path = strdup(ri->name);
663 perror("strdup failed");
670 /* finding the generation for a given path is a two step process.
671 * First we use the inode lookup routine to find out the root id
673 * Then we use the tree search ioctl to scan all the root items for a
674 * given root id and spit out the latest generation we can find
676 static u64 find_root_gen(int fd)
678 struct btrfs_ioctl_ino_lookup_args ino_args;
680 struct btrfs_ioctl_search_args args;
681 struct btrfs_ioctl_search_key *sk = &args.key;
682 struct btrfs_ioctl_search_header sh;
683 unsigned long off = 0;
687 memset(&ino_args, 0, sizeof(ino_args));
688 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
690 /* this ioctl fills in ino_args->treeid */
691 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
693 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
694 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
699 memset(&args, 0, sizeof(args));
701 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
704 * there may be more than one ROOT_ITEM key if there are
705 * snapshots pending deletion, we have to loop through
708 sk->min_objectid = ino_args.treeid;
709 sk->max_objectid = ino_args.treeid;
710 sk->max_type = BTRFS_ROOT_ITEM_KEY;
711 sk->min_type = BTRFS_ROOT_ITEM_KEY;
712 sk->max_offset = (u64)-1;
713 sk->max_transid = (u64)-1;
717 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
719 fprintf(stderr, "ERROR: can't perform the search - %s\n",
723 /* the ioctl returns the number of item it found in nr_items */
724 if (sk->nr_items == 0)
728 for (i = 0; i < sk->nr_items; i++) {
729 struct btrfs_root_item *item;
731 memcpy(&sh, args.buf + off, sizeof(sh));
733 item = (struct btrfs_root_item *)(args.buf + off);
736 sk->min_objectid = sh.objectid;
737 sk->min_type = sh.type;
738 sk->min_offset = sh.offset;
740 if (sh.objectid > ino_args.treeid)
743 if (sh.objectid == ino_args.treeid &&
744 sh.type == BTRFS_ROOT_ITEM_KEY) {
745 max_found = max(max_found,
746 btrfs_root_generation(item));
749 if (sk->min_offset < (u64)-1)
754 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
756 if (sk->min_objectid != ino_args.treeid)
762 /* pass in a directory id and this will return
763 * the full path of the parent directory inside its
766 * It may return NULL if it is in the root, or an ERR_PTR if things
769 static char *__ino_resolve(int fd, u64 dirid)
771 struct btrfs_ioctl_ino_lookup_args args;
775 memset(&args, 0, sizeof(args));
776 args.objectid = dirid;
778 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
780 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
781 (unsigned long long)dirid, strerror(errno));
787 * we're in a subdirectory of ref_tree, the kernel ioctl
788 * puts a / in there for us
790 full = strdup(args.name);
792 perror("malloc failed");
793 return ERR_PTR(-ENOMEM);
796 /* we're at the root of ref_tree */
803 * simple string builder, returning a new string with both
806 static char *build_name(const char *dirid, const char *name)
813 full = malloc(strlen(dirid) + strlen(name) + 1);
822 * given an inode number, this returns the full path name inside the subvolume
823 * to that file/directory. cache_dirid and cache_name are used to
824 * cache the results so we can avoid tree searches if a later call goes
825 * to the same directory or file name
827 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
835 struct btrfs_ioctl_search_args args;
836 struct btrfs_ioctl_search_key *sk = &args.key;
837 struct btrfs_ioctl_search_header *sh;
838 unsigned long off = 0;
841 memset(&args, 0, sizeof(args));
846 * step one, we search for the inode back ref. We just use the first
849 sk->min_objectid = ino;
850 sk->max_objectid = ino;
851 sk->max_type = BTRFS_INODE_REF_KEY;
852 sk->max_offset = (u64)-1;
853 sk->min_type = BTRFS_INODE_REF_KEY;
854 sk->max_transid = (u64)-1;
857 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
859 fprintf(stderr, "ERROR: can't perform the search - %s\n",
863 /* the ioctl returns the number of item it found in nr_items */
864 if (sk->nr_items == 0)
868 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
870 if (btrfs_search_header_type(sh) == BTRFS_INODE_REF_KEY) {
871 struct btrfs_inode_ref *ref;
872 dirid = btrfs_search_header_offset(sh);
874 ref = (struct btrfs_inode_ref *)(sh + 1);
875 namelen = btrfs_stack_inode_ref_name_len(ref);
877 name = (char *)(ref + 1);
878 name = strndup(name, namelen);
880 /* use our cached value */
881 if (dirid == *cache_dirid && *cache_name) {
882 dirname = *cache_name;
889 * the inode backref gives us the file name and the parent directory id.
890 * From here we use __ino_resolve to get the path to the parent
892 dirname = __ino_resolve(fd, dirid);
894 full = build_name(dirname, name);
895 if (*cache_name && dirname != *cache_name)
898 *cache_name = dirname;
899 *cache_dirid = dirid;
905 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
907 struct btrfs_ioctl_search_args args;
908 struct btrfs_ioctl_search_key *sk = &args.key;
909 struct btrfs_ioctl_search_header *sh;
913 memset(&args, 0, sizeof(args));
916 * search for a dir item with a name 'default' in the tree of
917 * tree roots, it should point us to a default root
919 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
921 /* don't worry about ancient format and request only one item */
924 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
925 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
926 sk->max_type = BTRFS_DIR_ITEM_KEY;
927 sk->min_type = BTRFS_DIR_ITEM_KEY;
928 sk->max_offset = (u64)-1;
929 sk->max_transid = (u64)-1;
931 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
935 /* the ioctl returns the number of items it found in nr_items */
936 if (sk->nr_items == 0)
939 sh = (struct btrfs_ioctl_search_header *)args.buf;
941 if (btrfs_search_header_type(sh) == BTRFS_DIR_ITEM_KEY) {
942 struct btrfs_dir_item *di;
946 di = (struct btrfs_dir_item *)(sh + 1);
947 name_len = btrfs_stack_dir_name_len(di);
948 name = (char *)(di + 1);
950 if (!strncmp("default", name, name_len))
951 found = btrfs_disk_key_objectid(&di->location);
959 static int list_subvol_search(int fd, struct root_lookup *root_lookup)
962 struct btrfs_ioctl_search_args args;
963 struct btrfs_ioctl_search_key *sk = &args.key;
964 struct btrfs_ioctl_search_header sh;
965 struct btrfs_root_ref *ref;
966 struct btrfs_root_item *ri;
967 unsigned long off = 0;
976 u8 uuid[BTRFS_UUID_SIZE];
977 u8 puuid[BTRFS_UUID_SIZE];
978 u8 ruuid[BTRFS_UUID_SIZE];
980 root_lookup_init(root_lookup);
981 memset(&args, 0, sizeof(args));
983 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
986 * set the min and max to backref keys. The search will
987 * only send back this type of key now.
989 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
990 sk->min_type = BTRFS_ROOT_ITEM_KEY;
992 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
995 * set all the other params to the max, we'll take any objectid
998 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
999 sk->max_offset = (u64)-1;
1000 sk->max_transid = (u64)-1;
1002 /* just a big number, doesn't matter much */
1003 sk->nr_items = 4096;
1006 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1009 /* the ioctl returns the number of item it found in nr_items */
1010 if (sk->nr_items == 0)
1016 * for each item, pull the key out of the header and then
1017 * read the root_ref item it contains
1019 for (i = 0; i < sk->nr_items; i++) {
1020 memcpy(&sh, args.buf + off, sizeof(sh));
1022 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1023 ref = (struct btrfs_root_ref *)(args.buf + off);
1024 name_len = btrfs_stack_root_ref_name_len(ref);
1025 name = (char *)(ref + 1);
1026 dir_id = btrfs_stack_root_ref_dirid(ref);
1028 add_root(root_lookup, sh.objectid, sh.offset,
1029 0, 0, dir_id, name, name_len, 0, 0, 0,
1031 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1032 ri = (struct btrfs_root_item *)(args.buf + off);
1033 gen = btrfs_root_generation(ri);
1034 flags = btrfs_root_flags(ri);
1036 sizeof(struct btrfs_root_item_v0)) {
1037 t = btrfs_stack_timespec_sec(&ri->otime);
1038 ogen = btrfs_root_otransid(ri);
1039 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1040 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1041 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1045 memset(uuid, 0, BTRFS_UUID_SIZE);
1046 memset(puuid, 0, BTRFS_UUID_SIZE);
1047 memset(ruuid, 0, BTRFS_UUID_SIZE);
1050 add_root(root_lookup, sh.objectid, 0,
1051 sh.offset, flags, 0, NULL, 0, ogen,
1052 gen, t, uuid, puuid, ruuid);
1058 * record the mins in sk so we can make sure the
1059 * next search doesn't repeat this root
1061 sk->min_objectid = sh.objectid;
1062 sk->min_type = sh.type;
1063 sk->min_offset = sh.offset;
1065 sk->nr_items = 4096;
1067 if (!sk->min_offset) /* overflow */
1072 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1073 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1078 if (sk->min_objectid > sk->max_objectid)
1085 static int filter_by_rootid(struct root_info *ri, u64 data)
1087 return ri->root_id == data;
1090 static int filter_snapshot(struct root_info *ri, u64 data)
1092 return !!ri->root_offset;
1095 static int filter_flags(struct root_info *ri, u64 flags)
1097 return ri->flags & flags;
1100 static int filter_gen_more(struct root_info *ri, u64 data)
1102 return ri->gen >= data;
1105 static int filter_gen_less(struct root_info *ri, u64 data)
1107 return ri->gen <= data;
1110 static int filter_gen_equal(struct root_info *ri, u64 data)
1112 return ri->gen == data;
1115 static int filter_cgen_more(struct root_info *ri, u64 data)
1117 return ri->ogen >= data;
1120 static int filter_cgen_less(struct root_info *ri, u64 data)
1122 return ri->ogen <= data;
1125 static int filter_cgen_equal(struct root_info *ri, u64 data)
1127 return ri->ogen == data;
1130 static int filter_topid_equal(struct root_info *ri, u64 data)
1132 return ri->top_id == data;
1135 static int filter_full_path(struct root_info *ri, u64 data)
1137 if (ri->full_path && ri->top_id != data) {
1139 char p[] = "<FS_TREE>";
1140 int add_len = strlen(p);
1141 int len = strlen(ri->full_path);
1143 tmp = malloc(len + add_len + 2);
1145 fprintf(stderr, "memory allocation failed\n");
1148 memcpy(tmp + add_len + 1, ri->full_path, len);
1149 tmp[len + add_len + 1] = '\0';
1151 memcpy(tmp, p, add_len);
1152 free(ri->full_path);
1153 ri->full_path = tmp;
1158 static int filter_by_parent(struct root_info *ri, u64 data)
1160 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1163 static int filter_deleted(struct root_info *ri, u64 data)
1168 static btrfs_list_filter_func all_filter_funcs[] = {
1169 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1170 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1171 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1172 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1173 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1174 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1175 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1176 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1177 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1178 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1179 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1180 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1181 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1184 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1186 struct btrfs_list_filter_set *set;
1189 size = sizeof(struct btrfs_list_filter_set) +
1190 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1191 set = calloc(1, size);
1193 fprintf(stderr, "memory allocation failed\n");
1197 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1202 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1203 enum btrfs_list_filter_enum filter, u64 data)
1205 struct btrfs_list_filter_set *set = *filter_set;
1208 ASSERT(set != NULL);
1209 ASSERT(filter < BTRFS_LIST_FILTER_MAX);
1210 ASSERT(set->nfilters <= set->total);
1212 if (set->nfilters == set->total) {
1215 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1216 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1218 set = realloc(set, size);
1220 fprintf(stderr, "memory allocation failed\n");
1225 memset(&set->filters[set->total], 0,
1226 BTRFS_LIST_NFILTERS_INCREASE *
1227 sizeof(struct btrfs_list_filter));
1228 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1232 ASSERT(set->filters[set->nfilters].filter_func == NULL);
1234 if (filter == BTRFS_LIST_FILTER_DELETED)
1235 set->only_deleted = 1;
1237 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1238 set->filters[set->nfilters].data = data;
1243 static int filter_root(struct root_info *ri,
1244 struct btrfs_list_filter_set *set)
1251 if (set->only_deleted && !ri->deleted)
1254 if (!set->only_deleted && ri->deleted)
1257 for (i = 0; i < set->nfilters; i++) {
1258 if (!set->filters[i].filter_func)
1260 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1267 static void filter_and_sort_subvol(struct root_lookup *all_subvols,
1268 struct root_lookup *sort_tree,
1269 struct btrfs_list_filter_set *filter_set,
1270 struct btrfs_list_comparer_set *comp_set,
1274 struct root_info *entry;
1277 root_lookup_init(sort_tree);
1279 n = rb_last(&all_subvols->root);
1281 entry = rb_entry(n, struct root_info, rb_node);
1283 ret = resolve_root(all_subvols, entry, top_id);
1284 if (ret == -ENOENT) {
1285 entry->full_path = strdup("DELETED");
1288 ret = filter_root(entry, filter_set);
1290 sort_tree_insert(sort_tree, entry, comp_set);
1295 static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1299 n = rb_first(&root_lookup->root);
1301 struct root_info *entry;
1303 entry = rb_entry(n, struct root_info, rb_node);
1304 ret = lookup_ino_path(fd, entry);
1305 if (ret && ret != -ENOENT)
1313 static void print_subvolume_column(struct root_info *subv,
1314 enum btrfs_list_column_enum column)
1317 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1319 ASSERT(0 <= column && column < BTRFS_LIST_ALL);
1322 case BTRFS_LIST_OBJECTID:
1323 printf("%llu", subv->root_id);
1325 case BTRFS_LIST_GENERATION:
1326 printf("%llu", subv->gen);
1328 case BTRFS_LIST_OGENERATION:
1329 printf("%llu", subv->ogen);
1331 case BTRFS_LIST_PARENT:
1332 printf("%llu", subv->ref_tree);
1334 case BTRFS_LIST_TOP_LEVEL:
1335 printf("%llu", subv->top_id);
1337 case BTRFS_LIST_OTIME:
1341 localtime_r(&subv->otime, &tm);
1342 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1347 case BTRFS_LIST_UUID:
1348 if (uuid_is_null(subv->uuid))
1349 strcpy(uuidparse, "-");
1351 uuid_unparse(subv->uuid, uuidparse);
1352 printf("%s", uuidparse);
1354 case BTRFS_LIST_PUUID:
1355 if (uuid_is_null(subv->puuid))
1356 strcpy(uuidparse, "-");
1358 uuid_unparse(subv->puuid, uuidparse);
1359 printf("%s", uuidparse);
1361 case BTRFS_LIST_RUUID:
1362 if (uuid_is_null(subv->ruuid))
1363 strcpy(uuidparse, "-");
1365 uuid_unparse(subv->ruuid, uuidparse);
1366 printf("%s", uuidparse);
1368 case BTRFS_LIST_PATH:
1369 BUG_ON(!subv->full_path);
1370 printf("%s", subv->full_path);
1377 static void print_one_subvol_info_raw(struct root_info *subv,
1378 const char *raw_prefix)
1382 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1383 if (!btrfs_list_columns[i].need_print)
1387 printf("%s",raw_prefix);
1389 print_subvolume_column(subv, i);
1394 static void print_one_subvol_info_table(struct root_info *subv)
1398 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1399 if (!btrfs_list_columns[i].need_print)
1402 print_subvolume_column(subv, i);
1404 if (i != BTRFS_LIST_PATH)
1407 if (i == BTRFS_LIST_TOP_LEVEL)
1413 static void print_one_subvol_info_default(struct root_info *subv)
1417 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1418 if (!btrfs_list_columns[i].need_print)
1421 printf("%s ", btrfs_list_columns[i].name);
1422 print_subvolume_column(subv, i);
1424 if (i != BTRFS_LIST_PATH)
1430 static void print_all_subvol_info_tab_head(void)
1436 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1437 if (btrfs_list_columns[i].need_print)
1438 printf("%s\t", btrfs_list_columns[i].name);
1440 if (i == BTRFS_LIST_ALL-1)
1444 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1445 memset(barrier, 0, sizeof(barrier));
1447 if (btrfs_list_columns[i].need_print) {
1448 len = strlen(btrfs_list_columns[i].name);
1450 strcat(barrier, "-");
1452 printf("%s\t", barrier);
1454 if (i == BTRFS_LIST_ALL-1)
1459 static void print_all_subvol_info(struct root_lookup *sorted_tree,
1460 enum btrfs_list_layout layout, const char *raw_prefix)
1463 struct root_info *entry;
1465 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1466 print_all_subvol_info_tab_head();
1468 n = rb_first(&sorted_tree->root);
1470 entry = rb_entry(n, struct root_info, sort_node);
1472 case BTRFS_LIST_LAYOUT_DEFAULT:
1473 print_one_subvol_info_default(entry);
1475 case BTRFS_LIST_LAYOUT_TABLE:
1476 print_one_subvol_info_table(entry);
1478 case BTRFS_LIST_LAYOUT_RAW:
1479 print_one_subvol_info_raw(entry, raw_prefix);
1486 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1490 ret = list_subvol_search(fd, root_lookup);
1492 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1498 * now we have an rbtree full of root_info objects, but we need to fill
1499 * in their path names within the subvol that is referencing each one.
1501 ret = list_subvol_fill_paths(fd, root_lookup);
1505 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1506 struct btrfs_list_comparer_set *comp_set,
1507 enum btrfs_list_layout layout, int full_path,
1508 const char *raw_prefix)
1510 struct root_lookup root_lookup;
1511 struct root_lookup root_sort;
1516 ret = btrfs_list_get_path_rootid(fd, &top_id);
1520 ret = btrfs_list_subvols(fd, &root_lookup);
1523 filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1526 print_all_subvol_info(&root_sort, layout, raw_prefix);
1527 rb_free_nodes(&root_lookup.root, free_root_info);
1532 static char *strdup_or_null(const char *s)
1539 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1542 struct root_lookup rl;
1543 struct rb_node *rbn;
1544 struct root_info *ri;
1547 ret = btrfs_list_get_path_rootid(fd, &root_id);
1551 ret = btrfs_list_subvols(fd, &rl);
1555 rbn = rb_first(&rl.root);
1557 ri = rb_entry(rbn, struct root_info, rb_node);
1558 rr = resolve_root(&rl, ri, root_id);
1559 if (rr == -ENOENT) {
1564 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1565 memcpy(the_ri, ri, offsetof(struct root_info, path));
1566 the_ri->path = strdup_or_null(ri->path);
1567 the_ri->name = strdup_or_null(ri->name);
1568 the_ri->full_path = strdup_or_null(ri->full_path);
1574 rb_free_nodes(&rl.root, free_root_info);
1578 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1579 struct btrfs_file_extent_item *item,
1580 u64 found_gen, u64 *cache_dirid,
1581 char **cache_dir_name, u64 *cache_ino,
1582 char **cache_full_name)
1586 u64 disk_offset = 0;
1592 if (btrfs_search_header_objectid(sh) == *cache_ino) {
1593 name = *cache_full_name;
1594 } else if (*cache_full_name) {
1595 free(*cache_full_name);
1596 *cache_full_name = NULL;
1599 name = ino_resolve(fd, btrfs_search_header_objectid(sh),
1602 *cache_full_name = name;
1603 *cache_ino = btrfs_search_header_objectid(sh);
1608 type = btrfs_stack_file_extent_type(item);
1609 compressed = btrfs_stack_file_extent_compression(item);
1611 if (type == BTRFS_FILE_EXTENT_REG ||
1612 type == BTRFS_FILE_EXTENT_PREALLOC) {
1613 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1614 disk_offset = btrfs_stack_file_extent_offset(item);
1615 len = btrfs_stack_file_extent_num_bytes(item);
1616 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1619 len = btrfs_stack_file_extent_ram_bytes(item);
1621 printf("unhandled extent type %d for inode %llu "
1622 "file offset %llu gen %llu\n",
1624 (unsigned long long)btrfs_search_header_objectid(sh),
1625 (unsigned long long)btrfs_search_header_offset(sh),
1626 (unsigned long long)found_gen);
1630 printf("inode %llu file offset %llu len %llu disk start %llu "
1631 "offset %llu gen %llu flags ",
1632 (unsigned long long)btrfs_search_header_objectid(sh),
1633 (unsigned long long)btrfs_search_header_offset(sh),
1634 (unsigned long long)len,
1635 (unsigned long long)disk_start,
1636 (unsigned long long)disk_offset,
1637 (unsigned long long)found_gen);
1643 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1644 printf("%sPREALLOC", flags ? "|" : "");
1647 if (type == BTRFS_FILE_EXTENT_INLINE) {
1648 printf("%sINLINE", flags ? "|" : "");
1654 printf(" %s\n", name);
1658 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1661 struct btrfs_ioctl_search_args args;
1662 struct btrfs_ioctl_search_key *sk = &args.key;
1663 struct btrfs_ioctl_search_header sh;
1664 struct btrfs_file_extent_item *item;
1665 unsigned long off = 0;
1669 u64 cache_dirid = 0;
1671 char *cache_dir_name = NULL;
1672 char *cache_full_name = NULL;
1673 struct btrfs_file_extent_item backup;
1675 memset(&backup, 0, sizeof(backup));
1676 memset(&args, 0, sizeof(args));
1678 sk->tree_id = root_id;
1681 * set all the other params to the max, we'll take any objectid
1684 sk->max_objectid = (u64)-1;
1685 sk->max_offset = (u64)-1;
1686 sk->max_transid = (u64)-1;
1687 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1688 sk->min_transid = oldest_gen;
1689 /* just a big number, doesn't matter much */
1690 sk->nr_items = 4096;
1692 max_found = find_root_gen(fd);
1694 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1696 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1700 /* the ioctl returns the number of item it found in nr_items */
1701 if (sk->nr_items == 0)
1707 * for each item, pull the key out of the header and then
1708 * read the root_ref item it contains
1710 for (i = 0; i < sk->nr_items; i++) {
1711 memcpy(&sh, args.buf + off, sizeof(sh));
1715 * just in case the item was too big, pass something other
1721 item = (struct btrfs_file_extent_item *)(args.buf +
1723 found_gen = btrfs_stack_file_extent_generation(item);
1724 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1725 found_gen >= oldest_gen) {
1726 print_one_extent(fd, &sh, item, found_gen,
1727 &cache_dirid, &cache_dir_name,
1728 &cache_ino, &cache_full_name);
1733 * record the mins in sk so we can make sure the
1734 * next search doesn't repeat this root
1736 sk->min_objectid = sh.objectid;
1737 sk->min_offset = sh.offset;
1738 sk->min_type = sh.type;
1740 sk->nr_items = 4096;
1741 if (sk->min_offset < (u64)-1)
1743 else if (sk->min_objectid < (u64)-1) {
1750 free(cache_dir_name);
1751 free(cache_full_name);
1752 printf("transid marker was %llu\n", (unsigned long long)max_found);
1756 char *btrfs_list_path_for_root(int fd, u64 root)
1758 struct root_lookup root_lookup;
1760 char *ret_path = NULL;
1764 ret = btrfs_list_get_path_rootid(fd, &top_id);
1766 return ERR_PTR(ret);
1768 ret = list_subvol_search(fd, &root_lookup);
1770 return ERR_PTR(ret);
1772 ret = list_subvol_fill_paths(fd, &root_lookup);
1774 return ERR_PTR(ret);
1776 n = rb_last(&root_lookup.root);
1778 struct root_info *entry;
1780 entry = rb_entry(n, struct root_info, rb_node);
1781 ret = resolve_root(&root_lookup, entry, top_id);
1782 if (ret == -ENOENT && entry->root_id == root) {
1786 if (entry->root_id == root) {
1787 ret_path = entry->full_path;
1788 entry->full_path = NULL;
1793 rb_free_nodes(&root_lookup.root, free_root_info);
1798 int btrfs_list_parse_sort_string(char *opt_arg,
1799 struct btrfs_list_comparer_set **comps)
1807 while ((p = strtok(opt_arg, ",")) != NULL) {
1809 ptr_argv = all_sort_items;
1812 if (strcmp(*ptr_argv, p) == 0) {
1817 if (strcmp(*ptr_argv, p) == 0) {
1834 } else if (*p == '-') {
1840 what_to_sort = btrfs_list_get_sort_item(p);
1841 btrfs_list_setup_comparer(comps, what_to_sort, order);
1850 * This function is used to parse the argument of filter condition.
1852 * type is the filter object.
1854 int btrfs_list_parse_filter_string(char *opt_arg,
1855 struct btrfs_list_filter_set **filters,
1856 enum btrfs_list_filter_enum type)
1861 switch (*(opt_arg++)) {
1863 arg = arg_strtou64(opt_arg);
1866 btrfs_list_setup_filter(filters, type, arg);
1869 arg = arg_strtou64(opt_arg);
1872 btrfs_list_setup_filter(filters, type, arg);
1876 arg = arg_strtou64(opt_arg);
1878 btrfs_list_setup_filter(filters, type, arg);
1885 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1889 ret = lookup_path_rootid(fd, treeid);
1891 error("cannot resolve rootid for path: %s",