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) {
405 ri->name = malloc(name_len + 1);
407 fprintf(stderr, "memory allocation failed\n");
410 strncpy(ri->name, name, name_len);
411 ri->name[name_len] = 0;
414 ri->ref_tree = ref_tree;
416 ri->root_offset = root_offset;
425 if (!ri->ogen && root_offset)
426 ri->ogen = root_offset;
430 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
432 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
438 * add_root - update the existed root, or allocate a new root and insert it
439 * into the lookup tree.
440 * root_id: object id of the root
441 * ref_tree: object id of the referring root.
442 * root_offset: offset value of the root'key
443 * dir_id: inode id of the directory in ref_tree where this root can be found.
444 * name: the name of root_id in that directory
445 * name_len: the length of name
446 * ogen: the original generation of the root
447 * gen: the current generation of the root
448 * ot: the original time(create time) of the root
449 * uuid: uuid of the root
450 * puuid: uuid of the root parent if any
452 static int add_root(struct root_lookup *root_lookup,
453 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
454 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
455 time_t ot, void *uuid, void *puuid)
457 struct root_info *ri;
460 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
461 dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
465 ri = malloc(sizeof(*ri));
467 printf("memory allocation failed\n");
470 memset(ri, 0, sizeof(*ri));
471 ri->root_id = root_id;
473 if (name && name_len > 0) {
474 ri->name = malloc(name_len + 1);
476 fprintf(stderr, "memory allocation failed\n");
479 strncpy(ri->name, name, name_len);
480 ri->name[name_len] = 0;
483 ri->ref_tree = ref_tree;
487 ri->root_offset = root_offset;
494 if (!ri->ogen && root_offset)
495 ri->ogen = root_offset;
500 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
503 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
505 ret = root_tree_insert(root_lookup, ri);
507 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
513 static void __free_root_info(struct rb_node *node)
515 struct root_info *ri;
517 ri = rb_entry(node, struct root_info, rb_node);
530 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
532 rb_free_nodes(&root_tree->root, __free_root_info);
536 * for a given root_info, search through the root_lookup tree to construct
537 * the full path name to it.
539 * This can't be called until all the root_info->path fields are filled
540 * in by lookup_ino_path
542 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
545 char *full_path = NULL;
547 struct root_info *found;
550 * we go backwards from the root_info object and add pathnames
551 * from parent directories as we go.
560 * ref_tree = 0 indicates the subvolumes
563 if (!found->ref_tree) {
568 add_len = strlen(found->path);
571 /* room for / and for null */
572 tmp = malloc(add_len + 2 + len);
574 perror("malloc failed");
577 memcpy(tmp + add_len + 1, full_path, len);
579 memcpy(tmp, found->path, add_len);
580 tmp [add_len + len + 1] = '\0';
585 full_path = strdup(found->path);
589 next = found->ref_tree;
591 if (next == top_id) {
597 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
600 if (next == BTRFS_FS_TREE_OBJECTID) {
606 * if the ref_tree wasn't in our tree of roots, the
607 * subvolume was deleted.
609 found = root_tree_search(rl, next);
616 ri->full_path = full_path;
622 * for a single root_info, ask the kernel to give us a path name
623 * inside it's ref_root for the dir_id where it lives.
625 * This fills in root_info->path with the path to the directory and and
626 * appends this root's name.
628 static int lookup_ino_path(int fd, struct root_info *ri)
630 struct btrfs_ioctl_ino_lookup_args args;
639 memset(&args, 0, sizeof(args));
640 args.treeid = ri->ref_tree;
641 args.objectid = ri->dir_id;
643 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
650 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
651 (unsigned long long)ri->ref_tree,
658 * we're in a subdirectory of ref_tree, the kernel ioctl
659 * puts a / in there for us
661 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
663 perror("malloc failed");
666 strcpy(ri->path, args.name);
667 strcat(ri->path, ri->name);
669 /* we're at the root of ref_tree */
670 ri->path = strdup(ri->name);
672 perror("strdup failed");
679 /* finding the generation for a given path is a two step process.
680 * First we use the inode loookup routine to find out the root id
682 * Then we use the tree search ioctl to scan all the root items for a
683 * given root id and spit out the latest generation we can find
685 static u64 find_root_gen(int fd)
687 struct btrfs_ioctl_ino_lookup_args ino_args;
689 struct btrfs_ioctl_search_args args;
690 struct btrfs_ioctl_search_key *sk = &args.key;
691 struct btrfs_ioctl_search_header sh;
692 unsigned long off = 0;
697 memset(&ino_args, 0, sizeof(ino_args));
698 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
700 /* this ioctl fills in ino_args->treeid */
701 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
704 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
705 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
710 memset(&args, 0, sizeof(args));
715 * there may be more than one ROOT_ITEM key if there are
716 * snapshots pending deletion, we have to loop through
719 sk->min_objectid = ino_args.treeid;
720 sk->max_objectid = ino_args.treeid;
721 sk->max_type = BTRFS_ROOT_ITEM_KEY;
722 sk->min_type = BTRFS_ROOT_ITEM_KEY;
723 sk->max_offset = (u64)-1;
724 sk->max_transid = (u64)-1;
728 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
731 fprintf(stderr, "ERROR: can't perform the search - %s\n",
735 /* the ioctl returns the number of item it found in nr_items */
736 if (sk->nr_items == 0)
740 for (i = 0; i < sk->nr_items; i++) {
741 struct btrfs_root_item *item;
743 memcpy(&sh, args.buf + off, sizeof(sh));
745 item = (struct btrfs_root_item *)(args.buf + off);
748 sk->min_objectid = sh.objectid;
749 sk->min_type = sh.type;
750 sk->min_offset = sh.offset;
752 if (sh.objectid > ino_args.treeid)
755 if (sh.objectid == ino_args.treeid &&
756 sh.type == BTRFS_ROOT_ITEM_KEY) {
757 max_found = max(max_found,
758 btrfs_root_generation(item));
761 if (sk->min_offset < (u64)-1)
766 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
768 if (sk->min_objectid != ino_args.treeid)
774 /* pass in a directory id and this will return
775 * the full path of the parent directory inside its
778 * It may return NULL if it is in the root, or an ERR_PTR if things
781 static char *__ino_resolve(int fd, u64 dirid)
783 struct btrfs_ioctl_ino_lookup_args args;
788 memset(&args, 0, sizeof(args));
789 args.objectid = dirid;
791 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
794 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
795 (unsigned long long)dirid, strerror(e) );
801 * we're in a subdirectory of ref_tree, the kernel ioctl
802 * puts a / in there for us
804 full = strdup(args.name);
806 perror("malloc failed");
807 return ERR_PTR(-ENOMEM);
810 /* we're at the root of ref_tree */
817 * simple string builder, returning a new string with both
820 static char *build_name(char *dirid, char *name)
826 full = malloc(strlen(dirid) + strlen(name) + 1);
835 * given an inode number, this returns the full path name inside the subvolume
836 * to that file/directory. cache_dirid and cache_name are used to
837 * cache the results so we can avoid tree searches if a later call goes
838 * to the same directory or file name
840 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
848 struct btrfs_ioctl_search_args args;
849 struct btrfs_ioctl_search_key *sk = &args.key;
850 struct btrfs_ioctl_search_header *sh;
851 unsigned long off = 0;
855 memset(&args, 0, sizeof(args));
860 * step one, we search for the inode back ref. We just use the first
863 sk->min_objectid = ino;
864 sk->max_objectid = ino;
865 sk->max_type = BTRFS_INODE_REF_KEY;
866 sk->max_offset = (u64)-1;
867 sk->min_type = BTRFS_INODE_REF_KEY;
868 sk->max_transid = (u64)-1;
871 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
874 fprintf(stderr, "ERROR: can't perform the search - %s\n",
878 /* the ioctl returns the number of item it found in nr_items */
879 if (sk->nr_items == 0)
883 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
885 if (sh->type == BTRFS_INODE_REF_KEY) {
886 struct btrfs_inode_ref *ref;
889 ref = (struct btrfs_inode_ref *)(sh + 1);
890 namelen = btrfs_stack_inode_ref_name_len(ref);
892 name = (char *)(ref + 1);
893 name = strndup(name, namelen);
895 /* use our cached value */
896 if (dirid == *cache_dirid && *cache_name) {
897 dirname = *cache_name;
904 * the inode backref gives us the file name and the parent directory id.
905 * From here we use __ino_resolve to get the path to the parent
907 dirname = __ino_resolve(fd, dirid);
909 full = build_name(dirname, name);
910 if (*cache_name && dirname != *cache_name)
913 *cache_name = dirname;
914 *cache_dirid = dirid;
920 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
922 struct btrfs_ioctl_search_args args;
923 struct btrfs_ioctl_search_key *sk = &args.key;
924 struct btrfs_ioctl_search_header *sh;
928 memset(&args, 0, sizeof(args));
931 * search for a dir item with a name 'default' in the tree of
932 * tree roots, it should point us to a default root
936 /* don't worry about ancient format and request only one item */
939 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
940 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
941 sk->max_type = BTRFS_DIR_ITEM_KEY;
942 sk->min_type = BTRFS_DIR_ITEM_KEY;
943 sk->max_offset = (u64)-1;
944 sk->max_transid = (u64)-1;
946 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
950 /* the ioctl returns the number of items it found in nr_items */
951 if (sk->nr_items == 0)
954 sh = (struct btrfs_ioctl_search_header *)args.buf;
956 if (sh->type == BTRFS_DIR_ITEM_KEY) {
957 struct btrfs_dir_item *di;
961 di = (struct btrfs_dir_item *)(sh + 1);
962 name_len = btrfs_stack_dir_name_len(di);
963 name = (char *)(di + 1);
965 if (!strncmp("default", name, name_len))
966 found = btrfs_disk_key_objectid(&di->location);
974 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
977 struct btrfs_ioctl_search_args args;
978 struct btrfs_ioctl_search_key *sk = &args.key;
979 struct btrfs_ioctl_search_header sh;
980 struct btrfs_root_ref *ref;
981 struct btrfs_root_item *ri;
982 unsigned long off = 0;
991 u8 uuid[BTRFS_UUID_SIZE];
992 u8 puuid[BTRFS_UUID_SIZE];
994 root_lookup_init(root_lookup);
995 memset(&args, 0, sizeof(args));
997 /* search in the tree of tree roots */
1001 * set the min and max to backref keys. The search will
1002 * only send back this type of key now.
1004 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1005 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1007 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1010 * set all the other params to the max, we'll take any objectid
1013 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1014 sk->max_offset = (u64)-1;
1015 sk->max_transid = (u64)-1;
1017 /* just a big number, doesn't matter much */
1018 sk->nr_items = 4096;
1021 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1024 /* the ioctl returns the number of item it found in nr_items */
1025 if (sk->nr_items == 0)
1031 * for each item, pull the key out of the header and then
1032 * read the root_ref item it contains
1034 for (i = 0; i < sk->nr_items; i++) {
1035 memcpy(&sh, args.buf + off, sizeof(sh));
1037 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1038 ref = (struct btrfs_root_ref *)(args.buf + off);
1039 name_len = btrfs_stack_root_ref_name_len(ref);
1040 name = (char *)(ref + 1);
1041 dir_id = btrfs_stack_root_ref_dirid(ref);
1043 add_root(root_lookup, sh.objectid, sh.offset,
1044 0, 0, dir_id, name, name_len, 0, 0, 0,
1046 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1047 ri = (struct btrfs_root_item *)(args.buf + off);
1048 gen = btrfs_root_generation(ri);
1049 flags = btrfs_root_flags(ri);
1051 sizeof(struct btrfs_root_item_v0)) {
1052 t = btrfs_stack_timespec_sec(&ri->otime);
1053 ogen = btrfs_root_otransid(ri);
1054 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1055 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1059 memset(uuid, 0, BTRFS_UUID_SIZE);
1060 memset(puuid, 0, BTRFS_UUID_SIZE);
1063 add_root(root_lookup, sh.objectid, 0,
1064 sh.offset, flags, 0, NULL, 0, ogen,
1065 gen, t, uuid, puuid);
1071 * record the mins in sk so we can make sure the
1072 * next search doesn't repeat this root
1074 sk->min_objectid = sh.objectid;
1075 sk->min_type = sh.type;
1076 sk->min_offset = sh.offset;
1078 sk->nr_items = 4096;
1080 if (!sk->min_offset) /* overflow */
1085 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1086 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1091 if (sk->min_objectid > sk->max_objectid)
1098 static int filter_by_rootid(struct root_info *ri, u64 data)
1100 return ri->root_id == data;
1103 static int filter_snapshot(struct root_info *ri, u64 data)
1105 return !!ri->root_offset;
1108 static int filter_flags(struct root_info *ri, u64 flags)
1110 return ri->flags & flags;
1113 static int filter_gen_more(struct root_info *ri, u64 data)
1115 return ri->gen >= data;
1118 static int filter_gen_less(struct root_info *ri, u64 data)
1120 return ri->gen <= data;
1123 static int filter_gen_equal(struct root_info *ri, u64 data)
1125 return ri->gen == data;
1128 static int filter_cgen_more(struct root_info *ri, u64 data)
1130 return ri->ogen >= data;
1133 static int filter_cgen_less(struct root_info *ri, u64 data)
1135 return ri->ogen <= data;
1138 static int filter_cgen_equal(struct root_info *ri, u64 data)
1140 return ri->ogen == data;
1143 static int filter_topid_equal(struct root_info *ri, u64 data)
1145 return ri->top_id == data;
1148 static int filter_full_path(struct root_info *ri, u64 data)
1150 if (ri->full_path && ri->top_id != data) {
1152 char p[] = "<FS_TREE>";
1153 int add_len = strlen(p);
1154 int len = strlen(ri->full_path);
1156 tmp = malloc(len + add_len + 2);
1158 fprintf(stderr, "memory allocation failed\n");
1161 memcpy(tmp + add_len + 1, ri->full_path, len);
1162 tmp[len + add_len + 1] = '\0';
1164 memcpy(tmp, p, add_len);
1165 free(ri->full_path);
1166 ri->full_path = tmp;
1171 static int filter_by_parent(struct root_info *ri, u64 data)
1173 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1176 static int filter_deleted(struct root_info *ri, u64 data)
1181 static btrfs_list_filter_func all_filter_funcs[] = {
1182 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1183 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1184 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1185 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1186 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1187 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1188 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1189 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1190 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1191 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1192 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1193 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1194 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1197 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1199 struct btrfs_list_filter_set *set;
1202 size = sizeof(struct btrfs_list_filter_set) +
1203 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1206 fprintf(stderr, "memory allocation failed\n");
1210 memset(set, 0, size);
1211 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1216 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1221 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1222 enum btrfs_list_filter_enum filter, u64 data)
1224 struct btrfs_list_filter_set *set = *filter_set;
1228 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1229 BUG_ON(set->nfilters > set->total);
1231 if (filter == BTRFS_LIST_FILTER_DELETED) {
1232 set->only_deleted = 1;
1236 if (set->nfilters == set->total) {
1237 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1238 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1239 set = realloc(set, size);
1241 fprintf(stderr, "memory allocation failed\n");
1245 memset(&set->filters[set->total], 0,
1246 BTRFS_LIST_NFILTERS_INCREASE *
1247 sizeof(struct btrfs_list_filter));
1248 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1252 BUG_ON(set->filters[set->nfilters].filter_func);
1254 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1255 set->filters[set->nfilters].data = data;
1260 static int filter_root(struct root_info *ri,
1261 struct btrfs_list_filter_set *set)
1265 if (!set || !set->nfilters)
1268 if (set->only_deleted && !ri->deleted)
1271 if (!set->only_deleted && ri->deleted)
1274 for (i = 0; i < set->nfilters; i++) {
1275 if (!set->filters[i].filter_func)
1277 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1284 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1285 struct root_lookup *sort_tree,
1286 struct btrfs_list_filter_set *filter_set,
1287 struct btrfs_list_comparer_set *comp_set,
1291 struct root_info *entry;
1294 root_lookup_init(sort_tree);
1296 n = rb_last(&all_subvols->root);
1298 entry = rb_entry(n, struct root_info, rb_node);
1300 ret = resolve_root(all_subvols, entry, top_id);
1301 if (ret == -ENOENT) {
1302 entry->full_path = strdup("DELETED");
1305 ret = filter_root(entry, filter_set);
1307 sort_tree_insert(sort_tree, entry, comp_set);
1312 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1316 n = rb_first(&root_lookup->root);
1318 struct root_info *entry;
1320 entry = rb_entry(n, struct root_info, rb_node);
1321 ret = lookup_ino_path(fd, entry);
1322 if (ret && ret != -ENOENT)
1330 static void print_subvolume_column(struct root_info *subv,
1331 enum btrfs_list_column_enum column)
1336 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1339 case BTRFS_LIST_OBJECTID:
1340 printf("%llu", subv->root_id);
1342 case BTRFS_LIST_GENERATION:
1343 printf("%llu", subv->gen);
1345 case BTRFS_LIST_OGENERATION:
1346 printf("%llu", subv->ogen);
1348 case BTRFS_LIST_PARENT:
1349 printf("%llu", subv->ref_tree);
1351 case BTRFS_LIST_TOP_LEVEL:
1352 printf("%llu", subv->top_id);
1354 case BTRFS_LIST_OTIME:
1358 localtime_r(&subv->otime, &tm);
1359 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1364 case BTRFS_LIST_UUID:
1365 if (uuid_is_null(subv->uuid))
1366 strcpy(uuidparse, "-");
1368 uuid_unparse(subv->uuid, uuidparse);
1369 printf("%s", uuidparse);
1371 case BTRFS_LIST_PUUID:
1372 if (uuid_is_null(subv->puuid))
1373 strcpy(uuidparse, "-");
1375 uuid_unparse(subv->puuid, uuidparse);
1376 printf("%s", uuidparse);
1378 case BTRFS_LIST_PATH:
1379 BUG_ON(!subv->full_path);
1380 printf("%s", subv->full_path);
1387 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1391 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1392 if (!btrfs_list_columns[i].need_print)
1396 printf("%s",raw_prefix);
1398 print_subvolume_column(subv, i);
1403 static void print_single_volume_info_table(struct root_info *subv)
1407 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1408 if (!btrfs_list_columns[i].need_print)
1411 print_subvolume_column(subv, i);
1413 if (i != BTRFS_LIST_PATH)
1416 if (i == BTRFS_LIST_TOP_LEVEL)
1422 static void print_single_volume_info_default(struct root_info *subv)
1426 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1427 if (!btrfs_list_columns[i].need_print)
1430 printf("%s ", btrfs_list_columns[i].name);
1431 print_subvolume_column(subv, i);
1433 if (i != BTRFS_LIST_PATH)
1439 static void print_all_volume_info_tab_head(void)
1445 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1446 if (btrfs_list_columns[i].need_print)
1447 printf("%s\t", btrfs_list_columns[i].name);
1449 if (i == BTRFS_LIST_ALL-1)
1453 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1454 memset(barrier, 0, sizeof(barrier));
1456 if (btrfs_list_columns[i].need_print) {
1457 len = strlen(btrfs_list_columns[i].name);
1459 strcat(barrier, "-");
1461 printf("%s\t", barrier);
1463 if (i == BTRFS_LIST_ALL-1)
1468 static void print_all_volume_info(struct root_lookup *sorted_tree,
1469 int layout, char *raw_prefix)
1472 struct root_info *entry;
1474 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1475 print_all_volume_info_tab_head();
1477 n = rb_first(&sorted_tree->root);
1479 entry = rb_entry(n, struct root_info, sort_node);
1481 case BTRFS_LIST_LAYOUT_DEFAULT:
1482 print_single_volume_info_default(entry);
1484 case BTRFS_LIST_LAYOUT_TABLE:
1485 print_single_volume_info_table(entry);
1487 case BTRFS_LIST_LAYOUT_RAW:
1488 print_single_volume_info_raw(entry, raw_prefix);
1495 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1499 ret = __list_subvol_search(fd, root_lookup);
1501 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1507 * now we have an rbtree full of root_info objects, but we need to fill
1508 * in their path names within the subvol that is referencing each one.
1510 ret = __list_subvol_fill_paths(fd, root_lookup);
1514 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1515 struct btrfs_list_comparer_set *comp_set,
1516 int layout, int full_path, char *raw_prefix)
1518 struct root_lookup root_lookup;
1519 struct root_lookup root_sort;
1524 ret = btrfs_list_get_path_rootid(fd, &top_id);
1528 ret = btrfs_list_subvols(fd, &root_lookup);
1531 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1534 print_all_volume_info(&root_sort, layout, raw_prefix);
1535 __free_all_subvolumn(&root_lookup);
1540 static char *strdup_or_null(const char *s)
1547 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1550 struct root_lookup rl;
1551 struct rb_node *rbn;
1552 struct root_info *ri;
1555 ret = btrfs_list_get_path_rootid(fd, &root_id);
1559 ret = btrfs_list_subvols(fd, &rl);
1563 rbn = rb_first(&rl.root);
1565 ri = rb_entry(rbn, struct root_info, rb_node);
1566 rr = resolve_root(&rl, ri, root_id);
1567 if (rr == -ENOENT) {
1572 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1573 memcpy(the_ri, ri, offsetof(struct root_info, path));
1574 the_ri->path = strdup_or_null(ri->path);
1575 the_ri->name = strdup_or_null(ri->name);
1576 the_ri->full_path = strdup_or_null(ri->full_path);
1582 __free_all_subvolumn(&rl);
1586 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1587 struct btrfs_file_extent_item *item,
1588 u64 found_gen, u64 *cache_dirid,
1589 char **cache_dir_name, u64 *cache_ino,
1590 char **cache_full_name)
1594 u64 disk_offset = 0;
1600 if (sh->objectid == *cache_ino) {
1601 name = *cache_full_name;
1602 } else if (*cache_full_name) {
1603 free(*cache_full_name);
1604 *cache_full_name = NULL;
1607 name = ino_resolve(fd, sh->objectid, cache_dirid,
1609 *cache_full_name = name;
1610 *cache_ino = sh->objectid;
1615 type = btrfs_stack_file_extent_type(item);
1616 compressed = btrfs_stack_file_extent_compression(item);
1618 if (type == BTRFS_FILE_EXTENT_REG ||
1619 type == BTRFS_FILE_EXTENT_PREALLOC) {
1620 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1621 disk_offset = btrfs_stack_file_extent_offset(item);
1622 len = btrfs_stack_file_extent_num_bytes(item);
1623 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1626 len = btrfs_stack_file_extent_ram_bytes(item);
1628 printf("unhandled extent type %d for inode %llu "
1629 "file offset %llu gen %llu\n",
1631 (unsigned long long)sh->objectid,
1632 (unsigned long long)sh->offset,
1633 (unsigned long long)found_gen);
1637 printf("inode %llu file offset %llu len %llu disk start %llu "
1638 "offset %llu gen %llu flags ",
1639 (unsigned long long)sh->objectid,
1640 (unsigned long long)sh->offset,
1641 (unsigned long long)len,
1642 (unsigned long long)disk_start,
1643 (unsigned long long)disk_offset,
1644 (unsigned long long)found_gen);
1650 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1651 printf("%sPREALLOC", flags ? "|" : "");
1654 if (type == BTRFS_FILE_EXTENT_INLINE) {
1655 printf("%sINLINE", flags ? "|" : "");
1661 printf(" %s\n", name);
1665 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1668 struct btrfs_ioctl_search_args args;
1669 struct btrfs_ioctl_search_key *sk = &args.key;
1670 struct btrfs_ioctl_search_header sh;
1671 struct btrfs_file_extent_item *item;
1672 unsigned long off = 0;
1677 u64 cache_dirid = 0;
1679 char *cache_dir_name = NULL;
1680 char *cache_full_name = NULL;
1681 struct btrfs_file_extent_item backup;
1683 memset(&backup, 0, sizeof(backup));
1684 memset(&args, 0, sizeof(args));
1686 sk->tree_id = root_id;
1689 * set all the other params to the max, we'll take any objectid
1692 sk->max_objectid = (u64)-1;
1693 sk->max_offset = (u64)-1;
1694 sk->max_transid = (u64)-1;
1695 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1696 sk->min_transid = oldest_gen;
1697 /* just a big number, doesn't matter much */
1698 sk->nr_items = 4096;
1700 max_found = find_root_gen(fd);
1702 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1705 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1709 /* the ioctl returns the number of item it found in nr_items */
1710 if (sk->nr_items == 0)
1716 * for each item, pull the key out of the header and then
1717 * read the root_ref item it contains
1719 for (i = 0; i < sk->nr_items; i++) {
1720 memcpy(&sh, args.buf + off, sizeof(sh));
1724 * just in case the item was too big, pass something other
1730 item = (struct btrfs_file_extent_item *)(args.buf +
1732 found_gen = btrfs_stack_file_extent_generation(item);
1733 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1734 found_gen >= oldest_gen) {
1735 print_one_extent(fd, &sh, item, found_gen,
1736 &cache_dirid, &cache_dir_name,
1737 &cache_ino, &cache_full_name);
1742 * record the mins in sk so we can make sure the
1743 * next search doesn't repeat this root
1745 sk->min_objectid = sh.objectid;
1746 sk->min_offset = sh.offset;
1747 sk->min_type = sh.type;
1749 sk->nr_items = 4096;
1750 if (sk->min_offset < (u64)-1)
1752 else if (sk->min_objectid < (u64)-1) {
1759 free(cache_dir_name);
1760 free(cache_full_name);
1761 printf("transid marker was %llu\n", (unsigned long long)max_found);
1765 char *btrfs_list_path_for_root(int fd, u64 root)
1767 struct root_lookup root_lookup;
1769 char *ret_path = NULL;
1773 ret = btrfs_list_get_path_rootid(fd, &top_id);
1775 return ERR_PTR(ret);
1777 ret = __list_subvol_search(fd, &root_lookup);
1779 return ERR_PTR(ret);
1781 ret = __list_subvol_fill_paths(fd, &root_lookup);
1783 return ERR_PTR(ret);
1785 n = rb_last(&root_lookup.root);
1787 struct root_info *entry;
1789 entry = rb_entry(n, struct root_info, rb_node);
1790 ret = resolve_root(&root_lookup, entry, top_id);
1791 if (ret == -ENOENT && entry->root_id == root) {
1795 if (entry->root_id == root) {
1796 ret_path = entry->full_path;
1797 entry->full_path = NULL;
1802 __free_all_subvolumn(&root_lookup);
1807 int btrfs_list_parse_sort_string(char *opt_arg,
1808 struct btrfs_list_comparer_set **comps)
1816 while ((p = strtok(opt_arg, ",")) != NULL) {
1818 ptr_argv = all_sort_items;
1821 if (strcmp(*ptr_argv, p) == 0) {
1826 if (strcmp(*ptr_argv, p) == 0) {
1843 } else if (*p == '-') {
1849 what_to_sort = btrfs_list_get_sort_item(p);
1850 btrfs_list_setup_comparer(comps, what_to_sort, order);
1859 * This function is used to parse the argument of filter condition.
1861 * type is the filter object.
1863 int btrfs_list_parse_filter_string(char *opt_arg,
1864 struct btrfs_list_filter_set **filters,
1865 enum btrfs_list_filter_enum type)
1869 char *ptr_parse_end = NULL;
1870 char *ptr_opt_arg_end = opt_arg + strlen(opt_arg);
1872 switch (*(opt_arg++)) {
1874 arg = (u64)strtol(opt_arg, &ptr_parse_end, 10);
1876 if (ptr_parse_end != ptr_opt_arg_end)
1879 btrfs_list_setup_filter(filters, type, arg);
1882 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1884 if (ptr_parse_end != ptr_opt_arg_end)
1887 btrfs_list_setup_filter(filters, type, arg);
1891 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1893 if (ptr_parse_end != ptr_opt_arg_end)
1895 btrfs_list_setup_filter(filters, type, arg);
1902 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1905 struct btrfs_ioctl_ino_lookup_args args;
1907 memset(&args, 0, sizeof(args));
1908 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1910 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1913 "ERROR: can't perform the search -%s\n",
1917 *treeid = args.treeid;