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>
25 #include <sys/types.h>
32 #include "transaction.h"
34 #include <uuid/uuid.h>
35 #include "btrfs-list.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",
89 .column_name = "UUID",
94 .column_name = "Path",
104 static btrfs_list_filter_func all_filter_funcs[];
105 static btrfs_list_comp_func all_comp_funcs[];
107 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
111 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
113 if (column < BTRFS_LIST_ALL) {
114 btrfs_list_columns[column].need_print = 1;
118 for (i = 0; i < BTRFS_LIST_ALL; i++)
119 btrfs_list_columns[i].need_print = 1;
122 static void root_lookup_init(struct root_lookup *tree)
124 tree->root.rb_node = NULL;
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);
226 fprintf(stderr, "memory allocation failed\n");
230 memset(set, 0, size);
231 set->total = BTRFS_LIST_NCOMPS_INCREASE;
236 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
241 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
242 enum btrfs_list_comp_enum comparer, int is_descending)
244 struct btrfs_list_comparer_set *set = *comp_set;
248 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
249 BUG_ON(set->ncomps > set->total);
251 if (set->ncomps == set->total) {
252 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
253 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
254 set = realloc(set, size);
256 fprintf(stderr, "memory allocation failed\n");
260 memset(&set->comps[set->total], 0,
261 BTRFS_LIST_NCOMPS_INCREASE *
262 sizeof(struct btrfs_list_comparer));
263 set->total += BTRFS_LIST_NCOMPS_INCREASE;
267 BUG_ON(set->comps[set->ncomps].comp_func);
269 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
270 set->comps[set->ncomps].is_descending = is_descending;
275 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
276 struct btrfs_list_comparer_set *set)
278 int rootid_compared = 0;
281 if (!set || !set->ncomps)
284 for (i = 0; i < set->ncomps; i++) {
285 if (!set->comps[i].comp_func)
288 ret = set->comps[i].comp_func(entry1, entry2,
289 set->comps[i].is_descending);
293 if (set->comps[i].comp_func == comp_entry_with_rootid)
297 if (!rootid_compared) {
299 ret = comp_entry_with_rootid(entry1, entry2, 0);
305 static int sort_tree_insert(struct root_lookup *sort_tree,
306 struct root_info *ins,
307 struct btrfs_list_comparer_set *comp_set)
309 struct rb_node **p = &sort_tree->root.rb_node;
310 struct rb_node *parent = NULL;
311 struct root_info *curr;
316 curr = rb_entry(parent, struct root_info, sort_node);
318 ret = sort_comp(ins, curr, comp_set);
327 rb_link_node(&ins->sort_node, parent, p);
328 rb_insert_color(&ins->sort_node, &sort_tree->root);
333 * insert a new root into the tree. returns the existing root entry
334 * if one is already there. Both root_id and ref_tree are used
337 static int root_tree_insert(struct root_lookup *root_tree,
338 struct root_info *ins)
340 struct rb_node **p = &root_tree->root.rb_node;
341 struct rb_node * parent = NULL;
342 struct root_info *curr;
347 curr = rb_entry(parent, struct root_info, rb_node);
349 ret = comp_entry_with_rootid(ins, curr, 0);
358 rb_link_node(&ins->rb_node, parent, p);
359 rb_insert_color(&ins->rb_node, &root_tree->root);
364 * find a given root id in the tree. We return the smallest one,
365 * rb_next can be used to move forward looking for more if required
367 static struct root_info *root_tree_search(struct root_lookup *root_tree,
370 struct rb_node *n = root_tree->root.rb_node;
371 struct root_info *entry;
372 struct root_info tmp;
375 tmp.root_id = root_id;
378 entry = rb_entry(n, struct root_info, rb_node);
380 ret = comp_entry_with_rootid(&tmp, entry, 0);
391 static int update_root(struct root_lookup *root_lookup,
392 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
393 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
394 time_t ot, void *uuid, void *puuid)
396 struct root_info *ri;
398 ri = root_tree_search(root_lookup, root_id);
399 if (!ri || ri->root_id != root_id)
401 if (name && name_len > 0) {
404 ri->name = malloc(name_len + 1);
406 fprintf(stderr, "memory allocation failed\n");
409 strncpy(ri->name, name, name_len);
410 ri->name[name_len] = 0;
413 ri->ref_tree = ref_tree;
415 ri->root_offset = root_offset;
424 if (!ri->ogen && root_offset)
425 ri->ogen = root_offset;
429 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
431 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
437 * add_root - update the existed root, or allocate a new root and insert it
438 * into the lookup tree.
439 * root_id: object id of the root
440 * ref_tree: object id of the referring root.
441 * root_offset: offset value of the root'key
442 * dir_id: inode id of the directory in ref_tree where this root can be found.
443 * name: the name of root_id in that directory
444 * name_len: the length of name
445 * ogen: the original generation of the root
446 * gen: the current generation of the root
447 * ot: the original time(create time) of the root
448 * uuid: uuid of the root
449 * puuid: uuid of the root parent if any
451 static int add_root(struct root_lookup *root_lookup,
452 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
453 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
454 time_t ot, void *uuid, void *puuid)
456 struct root_info *ri;
459 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
460 dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
464 ri = malloc(sizeof(*ri));
466 printf("memory allocation failed\n");
469 memset(ri, 0, sizeof(*ri));
470 ri->root_id = root_id;
472 if (name && name_len > 0) {
473 ri->name = malloc(name_len + 1);
475 fprintf(stderr, "memory allocation failed\n");
478 strncpy(ri->name, name, name_len);
479 ri->name[name_len] = 0;
482 ri->ref_tree = ref_tree;
486 ri->root_offset = root_offset;
493 if (!ri->ogen && root_offset)
494 ri->ogen = root_offset;
499 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
502 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
504 ret = root_tree_insert(root_lookup, ri);
506 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
512 static void __free_root_info(struct rb_node *node)
514 struct root_info *ri;
516 ri = rb_entry(node, struct root_info, rb_node);
523 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
525 rb_free_nodes(&root_tree->root, __free_root_info);
529 * for a given root_info, search through the root_lookup tree to construct
530 * the full path name to it.
532 * This can't be called until all the root_info->path fields are filled
533 * in by lookup_ino_path
535 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
538 char *full_path = NULL;
540 struct root_info *found;
543 * we go backwards from the root_info object and add pathnames
544 * from parent directories as we go.
553 * ref_tree = 0 indicates the subvolumes
556 if (!found->ref_tree) {
561 add_len = strlen(found->path);
564 /* room for / and for null */
565 tmp = malloc(add_len + 2 + len);
567 perror("malloc failed");
570 memcpy(tmp + add_len + 1, full_path, len);
572 memcpy(tmp, found->path, add_len);
573 tmp [add_len + len + 1] = '\0';
578 full_path = strdup(found->path);
582 ri->top_id = found->ref_tree;
584 next = found->ref_tree;
588 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
591 if (next == BTRFS_FS_TREE_OBJECTID)
594 * if the ref_tree wasn't in our tree of roots, the
595 * subvolume was deleted.
597 found = root_tree_search(rl, next);
604 ri->full_path = full_path;
610 * for a single root_info, ask the kernel to give us a path name
611 * inside it's ref_root for the dir_id where it lives.
613 * This fills in root_info->path with the path to the directory and and
614 * appends this root's name.
616 static int lookup_ino_path(int fd, struct root_info *ri)
618 struct btrfs_ioctl_ino_lookup_args args;
627 memset(&args, 0, sizeof(args));
628 args.treeid = ri->ref_tree;
629 args.objectid = ri->dir_id;
631 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
638 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
639 (unsigned long long)ri->ref_tree,
646 * we're in a subdirectory of ref_tree, the kernel ioctl
647 * puts a / in there for us
649 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
651 perror("malloc failed");
654 strcpy(ri->path, args.name);
655 strcat(ri->path, ri->name);
657 /* we're at the root of ref_tree */
658 ri->path = strdup(ri->name);
660 perror("strdup failed");
667 /* finding the generation for a given path is a two step process.
668 * First we use the inode loookup routine to find out the root id
670 * Then we use the tree search ioctl to scan all the root items for a
671 * given root id and spit out the latest generation we can find
673 static u64 find_root_gen(int fd)
675 struct btrfs_ioctl_ino_lookup_args ino_args;
677 struct btrfs_ioctl_search_args args;
678 struct btrfs_ioctl_search_key *sk = &args.key;
679 struct btrfs_ioctl_search_header sh;
680 unsigned long off = 0;
685 memset(&ino_args, 0, sizeof(ino_args));
686 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
688 /* this ioctl fills in ino_args->treeid */
689 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
692 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
693 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
698 memset(&args, 0, sizeof(args));
703 * there may be more than one ROOT_ITEM key if there are
704 * snapshots pending deletion, we have to loop through
707 sk->min_objectid = ino_args.treeid;
708 sk->max_objectid = ino_args.treeid;
709 sk->max_type = BTRFS_ROOT_ITEM_KEY;
710 sk->min_type = BTRFS_ROOT_ITEM_KEY;
711 sk->max_offset = (u64)-1;
712 sk->max_transid = (u64)-1;
716 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;
776 memset(&args, 0, sizeof(args));
777 args.objectid = dirid;
779 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
782 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
783 (unsigned long long)dirid, strerror(e) );
789 * we're in a subdirectory of ref_tree, the kernel ioctl
790 * puts a / in there for us
792 full = strdup(args.name);
794 perror("malloc failed");
795 return ERR_PTR(-ENOMEM);
798 /* we're at the root of ref_tree */
805 * simple string builder, returning a new string with both
808 static char *build_name(char *dirid, char *name)
814 full = malloc(strlen(dirid) + strlen(name) + 1);
823 * given an inode number, this returns the full path name inside the subvolume
824 * to that file/directory. cache_dirid and cache_name are used to
825 * cache the results so we can avoid tree searches if a later call goes
826 * to the same directory or file name
828 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
836 struct btrfs_ioctl_search_args args;
837 struct btrfs_ioctl_search_key *sk = &args.key;
838 struct btrfs_ioctl_search_header *sh;
839 unsigned long off = 0;
843 memset(&args, 0, sizeof(args));
848 * step one, we search for the inode back ref. We just use the first
851 sk->min_objectid = ino;
852 sk->max_objectid = ino;
853 sk->max_type = BTRFS_INODE_REF_KEY;
854 sk->max_offset = (u64)-1;
855 sk->min_type = BTRFS_INODE_REF_KEY;
856 sk->max_transid = (u64)-1;
859 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
862 fprintf(stderr, "ERROR: can't perform the search - %s\n",
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 (sh->type == BTRFS_INODE_REF_KEY) {
874 struct btrfs_inode_ref *ref;
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
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 (sh->type == 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;
970 unsigned long off = 0;
979 u8 uuid[BTRFS_UUID_SIZE];
980 u8 puuid[BTRFS_UUID_SIZE];
982 root_lookup_init(root_lookup);
983 memset(&args, 0, sizeof(args));
985 /* search in the tree of tree roots */
989 * set the min and max to backref keys. The search will
990 * only send back this type of key now.
992 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
993 sk->min_type = BTRFS_ROOT_ITEM_KEY;
995 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
998 * set all the other params to the max, we'll take any objectid
1001 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1002 sk->max_offset = (u64)-1;
1003 sk->max_transid = (u64)-1;
1005 /* just a big number, doesn't matter much */
1006 sk->nr_items = 4096;
1009 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1012 /* the ioctl returns the number of item it found in nr_items */
1013 if (sk->nr_items == 0)
1019 * for each item, pull the key out of the header and then
1020 * read the root_ref item it contains
1022 for (i = 0; i < sk->nr_items; i++) {
1023 memcpy(&sh, args.buf + off, sizeof(sh));
1025 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1026 ref = (struct btrfs_root_ref *)(args.buf + off);
1027 name_len = btrfs_stack_root_ref_name_len(ref);
1028 name = (char *)(ref + 1);
1029 dir_id = btrfs_stack_root_ref_dirid(ref);
1031 add_root(root_lookup, sh.objectid, sh.offset,
1032 0, 0, dir_id, name, name_len, 0, 0, 0,
1034 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1035 ri = (struct btrfs_root_item *)(args.buf + off);
1036 gen = btrfs_root_generation(ri);
1037 flags = btrfs_root_flags(ri);
1039 sizeof(struct btrfs_root_item_v0)) {
1040 t = btrfs_stack_timespec_sec(&ri->otime);
1041 ogen = btrfs_root_otransid(ri);
1042 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1043 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1047 memset(uuid, 0, BTRFS_UUID_SIZE);
1048 memset(puuid, 0, BTRFS_UUID_SIZE);
1051 add_root(root_lookup, sh.objectid, 0,
1052 sh.offset, flags, 0, NULL, 0, ogen,
1053 gen, t, uuid, puuid);
1059 * record the mins in sk so we can make sure the
1060 * next search doesn't repeat this root
1062 sk->min_objectid = sh.objectid;
1063 sk->min_type = sh.type;
1064 sk->min_offset = sh.offset;
1066 sk->nr_items = 4096;
1068 if (!sk->min_offset) /* overflow */
1073 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1074 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1079 if (sk->min_objectid > sk->max_objectid)
1086 static int filter_by_rootid(struct root_info *ri, u64 data)
1088 return ri->root_id == data;
1091 static int filter_snapshot(struct root_info *ri, u64 data)
1093 return !!ri->root_offset;
1096 static int filter_flags(struct root_info *ri, u64 flags)
1098 return ri->flags & flags;
1101 static int filter_gen_more(struct root_info *ri, u64 data)
1103 return ri->gen >= data;
1106 static int filter_gen_less(struct root_info *ri, u64 data)
1108 return ri->gen <= data;
1111 static int filter_gen_equal(struct root_info *ri, u64 data)
1113 return ri->gen == data;
1116 static int filter_cgen_more(struct root_info *ri, u64 data)
1118 return ri->ogen >= data;
1121 static int filter_cgen_less(struct root_info *ri, u64 data)
1123 return ri->ogen <= data;
1126 static int filter_cgen_equal(struct root_info *ri, u64 data)
1128 return ri->ogen == data;
1131 static int filter_topid_equal(struct root_info *ri, u64 data)
1133 return ri->top_id == data;
1136 static int filter_full_path(struct root_info *ri, u64 data)
1138 if (ri->full_path && ri->top_id != data) {
1140 char p[] = "<FS_TREE>";
1141 int add_len = strlen(p);
1142 int len = strlen(ri->full_path);
1144 tmp = malloc(len + add_len + 2);
1146 fprintf(stderr, "memory allocation failed\n");
1149 memcpy(tmp + add_len + 1, ri->full_path, len);
1150 tmp[len + add_len + 1] = '\0';
1152 memcpy(tmp, p, add_len);
1153 free(ri->full_path);
1154 ri->full_path = tmp;
1159 static int filter_by_parent(struct root_info *ri, u64 data)
1161 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1164 static int filter_deleted(struct root_info *ri, u64 data)
1169 static btrfs_list_filter_func all_filter_funcs[] = {
1170 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1171 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1172 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1173 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1174 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1175 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1176 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1177 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1178 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1179 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1180 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1181 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1182 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1185 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1187 struct btrfs_list_filter_set *set;
1190 size = sizeof(struct btrfs_list_filter_set) +
1191 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1194 fprintf(stderr, "memory allocation failed\n");
1198 memset(set, 0, size);
1199 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1204 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1209 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1210 enum btrfs_list_filter_enum filter, u64 data)
1212 struct btrfs_list_filter_set *set = *filter_set;
1216 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1217 BUG_ON(set->nfilters > set->total);
1219 if (filter == BTRFS_LIST_FILTER_DELETED) {
1220 set->only_deleted = 1;
1224 if (set->nfilters == set->total) {
1225 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1226 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1227 set = realloc(set, size);
1229 fprintf(stderr, "memory allocation failed\n");
1233 memset(&set->filters[set->total], 0,
1234 BTRFS_LIST_NFILTERS_INCREASE *
1235 sizeof(struct btrfs_list_filter));
1236 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1240 BUG_ON(set->filters[set->nfilters].filter_func);
1242 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1243 set->filters[set->nfilters].data = data;
1248 static int filter_root(struct root_info *ri,
1249 struct btrfs_list_filter_set *set)
1253 if (!set || !set->nfilters)
1256 if (set->only_deleted && !ri->deleted)
1259 if (!set->only_deleted && ri->deleted)
1262 for (i = 0; i < set->nfilters; i++) {
1263 if (!set->filters[i].filter_func)
1265 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1272 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1273 struct root_lookup *sort_tree,
1274 struct btrfs_list_filter_set *filter_set,
1275 struct btrfs_list_comparer_set *comp_set,
1279 struct root_info *entry;
1282 root_lookup_init(sort_tree);
1284 n = rb_last(&all_subvols->root);
1286 entry = rb_entry(n, struct root_info, rb_node);
1288 ret = resolve_root(all_subvols, entry, top_id);
1289 if (ret == -ENOENT) {
1290 entry->full_path = strdup("DELETED");
1293 ret = filter_root(entry, filter_set);
1295 sort_tree_insert(sort_tree, entry, comp_set);
1300 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1304 n = rb_first(&root_lookup->root);
1306 struct root_info *entry;
1308 entry = rb_entry(n, struct root_info, rb_node);
1309 ret = lookup_ino_path(fd, entry);
1310 if (ret && ret != -ENOENT)
1318 static void print_subvolume_column(struct root_info *subv,
1319 enum btrfs_list_column_enum column)
1322 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1324 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1327 case BTRFS_LIST_OBJECTID:
1328 printf("%llu", subv->root_id);
1330 case BTRFS_LIST_GENERATION:
1331 printf("%llu", subv->gen);
1333 case BTRFS_LIST_OGENERATION:
1334 printf("%llu", subv->ogen);
1336 case BTRFS_LIST_PARENT:
1337 printf("%llu", subv->ref_tree);
1339 case BTRFS_LIST_TOP_LEVEL:
1340 printf("%llu", subv->top_id);
1342 case BTRFS_LIST_OTIME:
1346 localtime_r(&subv->otime, &tm);
1347 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1352 case BTRFS_LIST_UUID:
1353 if (uuid_is_null(subv->uuid))
1354 strcpy(uuidparse, "-");
1356 uuid_unparse(subv->uuid, uuidparse);
1357 printf("%s", uuidparse);
1359 case BTRFS_LIST_PUUID:
1360 if (uuid_is_null(subv->puuid))
1361 strcpy(uuidparse, "-");
1363 uuid_unparse(subv->puuid, uuidparse);
1364 printf("%s", uuidparse);
1366 case BTRFS_LIST_PATH:
1367 BUG_ON(!subv->full_path);
1368 printf("%s", subv->full_path);
1375 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1379 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1380 if (!btrfs_list_columns[i].need_print)
1384 printf("%s",raw_prefix);
1386 print_subvolume_column(subv, i);
1391 static void print_single_volume_info_table(struct root_info *subv)
1395 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1396 if (!btrfs_list_columns[i].need_print)
1399 print_subvolume_column(subv, i);
1401 if (i != BTRFS_LIST_PATH)
1404 if (i == BTRFS_LIST_TOP_LEVEL)
1410 static void print_single_volume_info_default(struct root_info *subv)
1414 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1415 if (!btrfs_list_columns[i].need_print)
1418 printf("%s ", btrfs_list_columns[i].name);
1419 print_subvolume_column(subv, i);
1421 if (i != BTRFS_LIST_PATH)
1427 static void print_all_volume_info_tab_head(void)
1433 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1434 if (btrfs_list_columns[i].need_print)
1435 printf("%s\t", btrfs_list_columns[i].name);
1437 if (i == BTRFS_LIST_ALL-1)
1441 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1442 memset(barrier, 0, sizeof(barrier));
1444 if (btrfs_list_columns[i].need_print) {
1445 len = strlen(btrfs_list_columns[i].name);
1447 strcat(barrier, "-");
1449 printf("%s\t", barrier);
1451 if (i == BTRFS_LIST_ALL-1)
1456 static void print_all_volume_info(struct root_lookup *sorted_tree,
1457 int layout, char *raw_prefix)
1460 struct root_info *entry;
1462 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1463 print_all_volume_info_tab_head();
1465 n = rb_first(&sorted_tree->root);
1467 entry = rb_entry(n, struct root_info, sort_node);
1469 case BTRFS_LIST_LAYOUT_DEFAULT:
1470 print_single_volume_info_default(entry);
1472 case BTRFS_LIST_LAYOUT_TABLE:
1473 print_single_volume_info_table(entry);
1475 case BTRFS_LIST_LAYOUT_RAW:
1476 print_single_volume_info_raw(entry, raw_prefix);
1483 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1487 ret = __list_subvol_search(fd, root_lookup);
1489 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1495 * now we have an rbtree full of root_info objects, but we need to fill
1496 * in their path names within the subvol that is referencing each one.
1498 ret = __list_subvol_fill_paths(fd, root_lookup);
1502 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1503 struct btrfs_list_comparer_set *comp_set,
1504 int layout, int full_path, char *raw_prefix)
1506 struct root_lookup root_lookup;
1507 struct root_lookup root_sort;
1512 ret = btrfs_list_get_path_rootid(fd, &top_id);
1516 ret = btrfs_list_subvols(fd, &root_lookup);
1519 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1522 print_all_volume_info(&root_sort, layout, raw_prefix);
1523 __free_all_subvolumn(&root_lookup);
1528 static char *strdup_or_null(const char *s)
1535 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1538 struct root_lookup rl;
1539 struct rb_node *rbn;
1540 struct root_info *ri;
1543 ret = btrfs_list_get_path_rootid(fd, &root_id);
1547 ret = btrfs_list_subvols(fd, &rl);
1551 rbn = rb_first(&rl.root);
1553 ri = rb_entry(rbn, struct root_info, rb_node);
1554 rr = resolve_root(&rl, ri, root_id);
1555 if (rr == -ENOENT) {
1560 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1561 memcpy(the_ri, ri, offsetof(struct root_info, path));
1562 the_ri->path = strdup_or_null(ri->path);
1563 the_ri->name = strdup_or_null(ri->name);
1564 the_ri->full_path = strdup_or_null(ri->full_path);
1570 __free_all_subvolumn(&rl);
1574 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1575 struct btrfs_file_extent_item *item,
1576 u64 found_gen, u64 *cache_dirid,
1577 char **cache_dir_name, u64 *cache_ino,
1578 char **cache_full_name)
1582 u64 disk_offset = 0;
1588 if (sh->objectid == *cache_ino) {
1589 name = *cache_full_name;
1590 } else if (*cache_full_name) {
1591 free(*cache_full_name);
1592 *cache_full_name = NULL;
1595 name = ino_resolve(fd, sh->objectid, cache_dirid,
1597 *cache_full_name = name;
1598 *cache_ino = sh->objectid;
1603 type = btrfs_stack_file_extent_type(item);
1604 compressed = btrfs_stack_file_extent_compression(item);
1606 if (type == BTRFS_FILE_EXTENT_REG ||
1607 type == BTRFS_FILE_EXTENT_PREALLOC) {
1608 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1609 disk_offset = btrfs_stack_file_extent_offset(item);
1610 len = btrfs_stack_file_extent_num_bytes(item);
1611 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1614 len = btrfs_stack_file_extent_ram_bytes(item);
1616 printf("unhandled extent type %d for inode %llu "
1617 "file offset %llu gen %llu\n",
1619 (unsigned long long)sh->objectid,
1620 (unsigned long long)sh->offset,
1621 (unsigned long long)found_gen);
1625 printf("inode %llu file offset %llu len %llu disk start %llu "
1626 "offset %llu gen %llu flags ",
1627 (unsigned long long)sh->objectid,
1628 (unsigned long long)sh->offset,
1629 (unsigned long long)len,
1630 (unsigned long long)disk_start,
1631 (unsigned long long)disk_offset,
1632 (unsigned long long)found_gen);
1638 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1639 printf("%sPREALLOC", flags ? "|" : "");
1642 if (type == BTRFS_FILE_EXTENT_INLINE) {
1643 printf("%sINLINE", flags ? "|" : "");
1649 printf(" %s\n", name);
1653 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1656 struct btrfs_ioctl_search_args args;
1657 struct btrfs_ioctl_search_key *sk = &args.key;
1658 struct btrfs_ioctl_search_header sh;
1659 struct btrfs_file_extent_item *item;
1660 unsigned long off = 0;
1665 u64 cache_dirid = 0;
1667 char *cache_dir_name = NULL;
1668 char *cache_full_name = NULL;
1669 struct btrfs_file_extent_item backup;
1671 memset(&backup, 0, sizeof(backup));
1672 memset(&args, 0, sizeof(args));
1674 sk->tree_id = root_id;
1677 * set all the other params to the max, we'll take any objectid
1680 sk->max_objectid = (u64)-1;
1681 sk->max_offset = (u64)-1;
1682 sk->max_transid = (u64)-1;
1683 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1684 sk->min_transid = oldest_gen;
1685 /* just a big number, doesn't matter much */
1686 sk->nr_items = 4096;
1688 max_found = find_root_gen(fd);
1690 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1693 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1697 /* the ioctl returns the number of item it found in nr_items */
1698 if (sk->nr_items == 0)
1704 * for each item, pull the key out of the header and then
1705 * read the root_ref item it contains
1707 for (i = 0; i < sk->nr_items; i++) {
1708 memcpy(&sh, args.buf + off, sizeof(sh));
1712 * just in case the item was too big, pass something other
1718 item = (struct btrfs_file_extent_item *)(args.buf +
1720 found_gen = btrfs_stack_file_extent_generation(item);
1721 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1722 found_gen >= oldest_gen) {
1723 print_one_extent(fd, &sh, item, found_gen,
1724 &cache_dirid, &cache_dir_name,
1725 &cache_ino, &cache_full_name);
1730 * record the mins in sk so we can make sure the
1731 * next search doesn't repeat this root
1733 sk->min_objectid = sh.objectid;
1734 sk->min_offset = sh.offset;
1735 sk->min_type = sh.type;
1737 sk->nr_items = 4096;
1738 if (sk->min_offset < (u64)-1)
1740 else if (sk->min_objectid < (u64)-1) {
1747 free(cache_dir_name);
1748 free(cache_full_name);
1749 printf("transid marker was %llu\n", (unsigned long long)max_found);
1753 char *btrfs_list_path_for_root(int fd, u64 root)
1755 struct root_lookup root_lookup;
1757 char *ret_path = NULL;
1761 ret = btrfs_list_get_path_rootid(fd, &top_id);
1763 return ERR_PTR(ret);
1765 ret = __list_subvol_search(fd, &root_lookup);
1767 return ERR_PTR(ret);
1769 ret = __list_subvol_fill_paths(fd, &root_lookup);
1771 return ERR_PTR(ret);
1773 n = rb_last(&root_lookup.root);
1775 struct root_info *entry;
1777 entry = rb_entry(n, struct root_info, rb_node);
1778 ret = resolve_root(&root_lookup, entry, top_id);
1779 if (ret == -ENOENT && entry->root_id == root) {
1783 if (entry->root_id == root) {
1784 ret_path = entry->full_path;
1785 entry->full_path = NULL;
1790 __free_all_subvolumn(&root_lookup);
1795 int btrfs_list_parse_sort_string(char *opt_arg,
1796 struct btrfs_list_comparer_set **comps)
1804 while ((p = strtok(opt_arg, ",")) != NULL) {
1806 ptr_argv = all_sort_items;
1809 if (strcmp(*ptr_argv, p) == 0) {
1814 if (strcmp(*ptr_argv, p) == 0) {
1831 } else if (*p == '-') {
1837 what_to_sort = btrfs_list_get_sort_item(p);
1838 btrfs_list_setup_comparer(comps, what_to_sort, order);
1847 * This function is used to parse the argument of filter condition.
1849 * type is the filter object.
1851 int btrfs_list_parse_filter_string(char *opt_arg,
1852 struct btrfs_list_filter_set **filters,
1853 enum btrfs_list_filter_enum type)
1857 char *ptr_parse_end = NULL;
1858 char *ptr_opt_arg_end = opt_arg + strlen(opt_arg);
1860 switch (*(opt_arg++)) {
1862 arg = (u64)strtol(opt_arg, &ptr_parse_end, 10);
1864 if (ptr_parse_end != ptr_opt_arg_end)
1867 btrfs_list_setup_filter(filters, type, arg);
1870 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1872 if (ptr_parse_end != ptr_opt_arg_end)
1875 btrfs_list_setup_filter(filters, type, arg);
1879 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1881 if (ptr_parse_end != ptr_opt_arg_end)
1883 btrfs_list_setup_filter(filters, type, arg);
1890 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1893 struct btrfs_ioctl_ino_lookup_args args;
1895 memset(&args, 0, sizeof(args));
1896 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1898 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1901 "ERROR: can't perform the search -%s\n",
1905 *treeid = args.treeid;