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"
36 #include "rbtree-utils.h"
38 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
39 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
41 /* we store all the roots we find in an rbtree so that we can
42 * search for them later.
52 } btrfs_list_columns[] = {
65 .column_name = "CGen",
70 .column_name = "Parent",
75 .column_name = "Top Level",
80 .column_name = "OTime",
84 .name = "parent_uuid",
85 .column_name = "Parent UUID",
89 .name = "received_uuid",
90 .column_name = "Received UUID",
95 .column_name = "UUID",
100 .column_name = "Path",
110 static btrfs_list_filter_func all_filter_funcs[];
111 static btrfs_list_comp_func all_comp_funcs[];
113 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
117 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
119 if (column < BTRFS_LIST_ALL) {
120 btrfs_list_columns[column].need_print = 1;
124 for (i = 0; i < BTRFS_LIST_ALL; i++)
125 btrfs_list_columns[i].need_print = 1;
128 static void root_lookup_init(struct root_lookup *tree)
130 tree->root.rb_node = NULL;
133 static int comp_entry_with_rootid(struct root_info *entry1,
134 struct root_info *entry2,
139 if (entry1->root_id > entry2->root_id)
141 else if (entry1->root_id < entry2->root_id)
146 return is_descending ? -ret : ret;
149 static int comp_entry_with_gen(struct root_info *entry1,
150 struct root_info *entry2,
155 if (entry1->gen > entry2->gen)
157 else if (entry1->gen < entry2->gen)
162 return is_descending ? -ret : ret;
165 static int comp_entry_with_ogen(struct root_info *entry1,
166 struct root_info *entry2,
171 if (entry1->ogen > entry2->ogen)
173 else if (entry1->ogen < entry2->ogen)
178 return is_descending ? -ret : ret;
181 static int comp_entry_with_path(struct root_info *entry1,
182 struct root_info *entry2,
187 if (strcmp(entry1->full_path, entry2->full_path) > 0)
189 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
194 return is_descending ? -ret : ret;
197 static btrfs_list_comp_func all_comp_funcs[] = {
198 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
199 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
200 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
201 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
204 static char *all_sort_items[] = {
205 [BTRFS_LIST_COMP_ROOTID] = "rootid",
206 [BTRFS_LIST_COMP_OGEN] = "ogen",
207 [BTRFS_LIST_COMP_GEN] = "gen",
208 [BTRFS_LIST_COMP_PATH] = "path",
209 [BTRFS_LIST_COMP_MAX] = NULL,
212 static int btrfs_list_get_sort_item(char *sort_name)
216 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
217 if (strcmp(sort_name, all_sort_items[i]) == 0)
223 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
225 struct btrfs_list_comparer_set *set;
228 size = sizeof(struct btrfs_list_comparer_set) +
229 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
232 fprintf(stderr, "memory allocation failed\n");
236 memset(set, 0, size);
237 set->total = BTRFS_LIST_NCOMPS_INCREASE;
242 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
247 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
248 enum btrfs_list_comp_enum comparer, int is_descending)
250 struct btrfs_list_comparer_set *set = *comp_set;
254 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
255 BUG_ON(set->ncomps > set->total);
257 if (set->ncomps == set->total) {
258 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
259 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
260 set = realloc(set, size);
262 fprintf(stderr, "memory allocation failed\n");
266 memset(&set->comps[set->total], 0,
267 BTRFS_LIST_NCOMPS_INCREASE *
268 sizeof(struct btrfs_list_comparer));
269 set->total += BTRFS_LIST_NCOMPS_INCREASE;
273 BUG_ON(set->comps[set->ncomps].comp_func);
275 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
276 set->comps[set->ncomps].is_descending = is_descending;
281 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
282 struct btrfs_list_comparer_set *set)
284 int rootid_compared = 0;
287 if (!set || !set->ncomps)
290 for (i = 0; i < set->ncomps; i++) {
291 if (!set->comps[i].comp_func)
294 ret = set->comps[i].comp_func(entry1, entry2,
295 set->comps[i].is_descending);
299 if (set->comps[i].comp_func == comp_entry_with_rootid)
303 if (!rootid_compared) {
305 ret = comp_entry_with_rootid(entry1, entry2, 0);
311 static int sort_tree_insert(struct root_lookup *sort_tree,
312 struct root_info *ins,
313 struct btrfs_list_comparer_set *comp_set)
315 struct rb_node **p = &sort_tree->root.rb_node;
316 struct rb_node *parent = NULL;
317 struct root_info *curr;
322 curr = rb_entry(parent, struct root_info, sort_node);
324 ret = sort_comp(ins, curr, comp_set);
333 rb_link_node(&ins->sort_node, parent, p);
334 rb_insert_color(&ins->sort_node, &sort_tree->root);
339 * insert a new root into the tree. returns the existing root entry
340 * if one is already there. Both root_id and ref_tree are used
343 static int root_tree_insert(struct root_lookup *root_tree,
344 struct root_info *ins)
346 struct rb_node **p = &root_tree->root.rb_node;
347 struct rb_node * parent = NULL;
348 struct root_info *curr;
353 curr = rb_entry(parent, struct root_info, rb_node);
355 ret = comp_entry_with_rootid(ins, curr, 0);
364 rb_link_node(&ins->rb_node, parent, p);
365 rb_insert_color(&ins->rb_node, &root_tree->root);
370 * find a given root id in the tree. We return the smallest one,
371 * rb_next can be used to move forward looking for more if required
373 static struct root_info *root_tree_search(struct root_lookup *root_tree,
376 struct rb_node *n = root_tree->root.rb_node;
377 struct root_info *entry;
378 struct root_info tmp;
381 tmp.root_id = root_id;
384 entry = rb_entry(n, struct root_info, rb_node);
386 ret = comp_entry_with_rootid(&tmp, entry, 0);
397 static int update_root(struct root_lookup *root_lookup,
398 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
399 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
400 time_t ot, void *uuid, void *puuid, void *ruuid)
402 struct root_info *ri;
404 ri = root_tree_search(root_lookup, root_id);
405 if (!ri || ri->root_id != root_id)
407 if (name && name_len > 0) {
410 ri->name = malloc(name_len + 1);
412 fprintf(stderr, "memory allocation failed\n");
415 strncpy(ri->name, name, name_len);
416 ri->name[name_len] = 0;
419 ri->ref_tree = ref_tree;
421 ri->root_offset = root_offset;
430 if (!ri->ogen && root_offset)
431 ri->ogen = root_offset;
435 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
437 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
439 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
445 * add_root - update the existed root, or allocate a new root and insert it
446 * into the lookup tree.
447 * root_id: object id of the root
448 * ref_tree: object id of the referring root.
449 * root_offset: offset value of the root'key
450 * dir_id: inode id of the directory in ref_tree where this root can be found.
451 * name: the name of root_id in that directory
452 * name_len: the length of name
453 * ogen: the original generation of the root
454 * gen: the current generation of the root
455 * ot: the original time(create time) of the root
456 * uuid: uuid of the root
457 * puuid: uuid of the root parent if any
458 * ruuid: uuid of the received subvol, if any
460 static int add_root(struct root_lookup *root_lookup,
461 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
462 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
463 time_t ot, void *uuid, void *puuid, void *ruuid)
465 struct root_info *ri;
468 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
469 dir_id, name, name_len, ogen, gen, ot,
474 ri = malloc(sizeof(*ri));
476 printf("memory allocation failed\n");
479 memset(ri, 0, sizeof(*ri));
480 ri->root_id = root_id;
482 if (name && name_len > 0) {
483 ri->name = malloc(name_len + 1);
485 fprintf(stderr, "memory allocation failed\n");
488 strncpy(ri->name, name, name_len);
489 ri->name[name_len] = 0;
492 ri->ref_tree = ref_tree;
496 ri->root_offset = root_offset;
503 if (!ri->ogen && root_offset)
504 ri->ogen = root_offset;
509 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
512 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
515 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
517 ret = root_tree_insert(root_lookup, ri);
519 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
525 static void __free_root_info(struct rb_node *node)
527 struct root_info *ri;
529 ri = rb_entry(node, struct root_info, rb_node);
536 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
538 rb_free_nodes(&root_tree->root, __free_root_info);
542 * for a given root_info, search through the root_lookup tree to construct
543 * the full path name to it.
545 * This can't be called until all the root_info->path fields are filled
546 * in by lookup_ino_path
548 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
551 char *full_path = NULL;
553 struct root_info *found;
556 * we go backwards from the root_info object and add pathnames
557 * from parent directories as we go.
566 * ref_tree = 0 indicates the subvolumes
569 if (!found->ref_tree) {
574 add_len = strlen(found->path);
577 /* room for / and for null */
578 tmp = malloc(add_len + 2 + len);
580 perror("malloc failed");
583 memcpy(tmp + add_len + 1, full_path, len);
585 memcpy(tmp, found->path, add_len);
586 tmp [add_len + len + 1] = '\0';
591 full_path = strdup(found->path);
595 ri->top_id = found->ref_tree;
597 next = found->ref_tree;
601 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
604 if (next == BTRFS_FS_TREE_OBJECTID)
607 * if the ref_tree wasn't in our tree of roots, the
608 * subvolume was deleted.
610 found = root_tree_search(rl, next);
617 ri->full_path = full_path;
623 * for a single root_info, ask the kernel to give us a path name
624 * inside it's ref_root for the dir_id where it lives.
626 * This fills in root_info->path with the path to the directory and and
627 * appends this root's name.
629 static int lookup_ino_path(int fd, struct root_info *ri)
631 struct btrfs_ioctl_ino_lookup_args args;
640 memset(&args, 0, sizeof(args));
641 args.treeid = ri->ref_tree;
642 args.objectid = ri->dir_id;
644 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
651 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
652 (unsigned long long)ri->ref_tree,
659 * we're in a subdirectory of ref_tree, the kernel ioctl
660 * puts a / in there for us
662 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
664 perror("malloc failed");
667 strcpy(ri->path, args.name);
668 strcat(ri->path, ri->name);
670 /* we're at the root of ref_tree */
671 ri->path = strdup(ri->name);
673 perror("strdup failed");
680 /* finding the generation for a given path is a two step process.
681 * First we use the inode loookup routine to find out the root id
683 * Then we use the tree search ioctl to scan all the root items for a
684 * given root id and spit out the latest generation we can find
686 static u64 find_root_gen(int fd)
688 struct btrfs_ioctl_ino_lookup_args ino_args;
690 struct btrfs_ioctl_search_args args;
691 struct btrfs_ioctl_search_key *sk = &args.key;
692 struct btrfs_ioctl_search_header sh;
693 unsigned long off = 0;
698 memset(&ino_args, 0, sizeof(ino_args));
699 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
701 /* this ioctl fills in ino_args->treeid */
702 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
705 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
706 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
711 memset(&args, 0, sizeof(args));
716 * there may be more than one ROOT_ITEM key if there are
717 * snapshots pending deletion, we have to loop through
720 sk->min_objectid = ino_args.treeid;
721 sk->max_objectid = ino_args.treeid;
722 sk->max_type = BTRFS_ROOT_ITEM_KEY;
723 sk->min_type = BTRFS_ROOT_ITEM_KEY;
724 sk->max_offset = (u64)-1;
725 sk->max_transid = (u64)-1;
729 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
732 fprintf(stderr, "ERROR: can't perform the search - %s\n",
736 /* the ioctl returns the number of item it found in nr_items */
737 if (sk->nr_items == 0)
741 for (i = 0; i < sk->nr_items; i++) {
742 struct btrfs_root_item *item;
744 memcpy(&sh, args.buf + off, sizeof(sh));
746 item = (struct btrfs_root_item *)(args.buf + off);
749 sk->min_objectid = sh.objectid;
750 sk->min_type = sh.type;
751 sk->min_offset = sh.offset;
753 if (sh.objectid > ino_args.treeid)
756 if (sh.objectid == ino_args.treeid &&
757 sh.type == BTRFS_ROOT_ITEM_KEY) {
758 max_found = max(max_found,
759 btrfs_root_generation(item));
762 if (sk->min_offset < (u64)-1)
767 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
769 if (sk->min_objectid != ino_args.treeid)
775 /* pass in a directory id and this will return
776 * the full path of the parent directory inside its
779 * It may return NULL if it is in the root, or an ERR_PTR if things
782 static char *__ino_resolve(int fd, u64 dirid)
784 struct btrfs_ioctl_ino_lookup_args args;
789 memset(&args, 0, sizeof(args));
790 args.objectid = dirid;
792 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
795 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
796 (unsigned long long)dirid, strerror(e) );
802 * we're in a subdirectory of ref_tree, the kernel ioctl
803 * puts a / in there for us
805 full = strdup(args.name);
807 perror("malloc failed");
808 return ERR_PTR(-ENOMEM);
811 /* we're at the root of ref_tree */
818 * simple string builder, returning a new string with both
821 static char *build_name(char *dirid, char *name)
827 full = malloc(strlen(dirid) + strlen(name) + 1);
836 * given an inode number, this returns the full path name inside the subvolume
837 * to that file/directory. cache_dirid and cache_name are used to
838 * cache the results so we can avoid tree searches if a later call goes
839 * to the same directory or file name
841 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
849 struct btrfs_ioctl_search_args args;
850 struct btrfs_ioctl_search_key *sk = &args.key;
851 struct btrfs_ioctl_search_header *sh;
852 unsigned long off = 0;
856 memset(&args, 0, sizeof(args));
861 * step one, we search for the inode back ref. We just use the first
864 sk->min_objectid = ino;
865 sk->max_objectid = ino;
866 sk->max_type = BTRFS_INODE_REF_KEY;
867 sk->max_offset = (u64)-1;
868 sk->min_type = BTRFS_INODE_REF_KEY;
869 sk->max_transid = (u64)-1;
872 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
875 fprintf(stderr, "ERROR: can't perform the search - %s\n",
879 /* the ioctl returns the number of item it found in nr_items */
880 if (sk->nr_items == 0)
884 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
886 if (sh->type == BTRFS_INODE_REF_KEY) {
887 struct btrfs_inode_ref *ref;
890 ref = (struct btrfs_inode_ref *)(sh + 1);
891 namelen = btrfs_stack_inode_ref_name_len(ref);
893 name = (char *)(ref + 1);
894 name = strndup(name, namelen);
896 /* use our cached value */
897 if (dirid == *cache_dirid && *cache_name) {
898 dirname = *cache_name;
905 * the inode backref gives us the file name and the parent directory id.
906 * From here we use __ino_resolve to get the path to the parent
908 dirname = __ino_resolve(fd, dirid);
910 full = build_name(dirname, name);
911 if (*cache_name && dirname != *cache_name)
914 *cache_name = dirname;
915 *cache_dirid = dirid;
921 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
923 struct btrfs_ioctl_search_args args;
924 struct btrfs_ioctl_search_key *sk = &args.key;
925 struct btrfs_ioctl_search_header *sh;
929 memset(&args, 0, sizeof(args));
932 * search for a dir item with a name 'default' in the tree of
933 * tree roots, it should point us to a default root
937 /* don't worry about ancient format and request only one item */
940 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
941 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
942 sk->max_type = BTRFS_DIR_ITEM_KEY;
943 sk->min_type = BTRFS_DIR_ITEM_KEY;
944 sk->max_offset = (u64)-1;
945 sk->max_transid = (u64)-1;
947 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
951 /* the ioctl returns the number of items it found in nr_items */
952 if (sk->nr_items == 0)
955 sh = (struct btrfs_ioctl_search_header *)args.buf;
957 if (sh->type == BTRFS_DIR_ITEM_KEY) {
958 struct btrfs_dir_item *di;
962 di = (struct btrfs_dir_item *)(sh + 1);
963 name_len = btrfs_stack_dir_name_len(di);
964 name = (char *)(di + 1);
966 if (!strncmp("default", name, name_len))
967 found = btrfs_disk_key_objectid(&di->location);
975 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
978 struct btrfs_ioctl_search_args args;
979 struct btrfs_ioctl_search_key *sk = &args.key;
980 struct btrfs_ioctl_search_header sh;
981 struct btrfs_root_ref *ref;
982 struct btrfs_root_item *ri;
983 unsigned long off = 0;
992 u8 uuid[BTRFS_UUID_SIZE];
993 u8 puuid[BTRFS_UUID_SIZE];
994 u8 ruuid[BTRFS_UUID_SIZE];
996 root_lookup_init(root_lookup);
997 memset(&args, 0, sizeof(args));
999 /* search in the tree of tree roots */
1003 * set the min and max to backref keys. The search will
1004 * only send back this type of key now.
1006 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1007 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1009 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1012 * set all the other params to the max, we'll take any objectid
1015 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1016 sk->max_offset = (u64)-1;
1017 sk->max_transid = (u64)-1;
1019 /* just a big number, doesn't matter much */
1020 sk->nr_items = 4096;
1023 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1026 /* the ioctl returns the number of item it found in nr_items */
1027 if (sk->nr_items == 0)
1033 * for each item, pull the key out of the header and then
1034 * read the root_ref item it contains
1036 for (i = 0; i < sk->nr_items; i++) {
1037 memcpy(&sh, args.buf + off, sizeof(sh));
1039 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1040 ref = (struct btrfs_root_ref *)(args.buf + off);
1041 name_len = btrfs_stack_root_ref_name_len(ref);
1042 name = (char *)(ref + 1);
1043 dir_id = btrfs_stack_root_ref_dirid(ref);
1045 add_root(root_lookup, sh.objectid, sh.offset,
1046 0, 0, dir_id, name, name_len, 0, 0, 0,
1048 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1049 ri = (struct btrfs_root_item *)(args.buf + off);
1050 gen = btrfs_root_generation(ri);
1051 flags = btrfs_root_flags(ri);
1053 sizeof(struct btrfs_root_item_v0)) {
1054 t = btrfs_stack_timespec_sec(&ri->otime);
1055 ogen = btrfs_root_otransid(ri);
1056 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1057 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1058 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1062 memset(uuid, 0, BTRFS_UUID_SIZE);
1063 memset(puuid, 0, BTRFS_UUID_SIZE);
1064 memset(ruuid, 0, BTRFS_UUID_SIZE);
1067 add_root(root_lookup, sh.objectid, 0,
1068 sh.offset, flags, 0, NULL, 0, ogen,
1069 gen, t, uuid, puuid, ruuid);
1075 * record the mins in sk so we can make sure the
1076 * next search doesn't repeat this root
1078 sk->min_objectid = sh.objectid;
1079 sk->min_type = sh.type;
1080 sk->min_offset = sh.offset;
1082 sk->nr_items = 4096;
1084 if (!sk->min_offset) /* overflow */
1089 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1090 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1095 if (sk->min_objectid > sk->max_objectid)
1102 static int filter_by_rootid(struct root_info *ri, u64 data)
1104 return ri->root_id == data;
1107 static int filter_snapshot(struct root_info *ri, u64 data)
1109 return !!ri->root_offset;
1112 static int filter_flags(struct root_info *ri, u64 flags)
1114 return ri->flags & flags;
1117 static int filter_gen_more(struct root_info *ri, u64 data)
1119 return ri->gen >= data;
1122 static int filter_gen_less(struct root_info *ri, u64 data)
1124 return ri->gen <= data;
1127 static int filter_gen_equal(struct root_info *ri, u64 data)
1129 return ri->gen == data;
1132 static int filter_cgen_more(struct root_info *ri, u64 data)
1134 return ri->ogen >= data;
1137 static int filter_cgen_less(struct root_info *ri, u64 data)
1139 return ri->ogen <= data;
1142 static int filter_cgen_equal(struct root_info *ri, u64 data)
1144 return ri->ogen == data;
1147 static int filter_topid_equal(struct root_info *ri, u64 data)
1149 return ri->top_id == data;
1152 static int filter_full_path(struct root_info *ri, u64 data)
1154 if (ri->full_path && ri->top_id != data) {
1156 char p[] = "<FS_TREE>";
1157 int add_len = strlen(p);
1158 int len = strlen(ri->full_path);
1160 tmp = malloc(len + add_len + 2);
1162 fprintf(stderr, "memory allocation failed\n");
1165 memcpy(tmp + add_len + 1, ri->full_path, len);
1166 tmp[len + add_len + 1] = '\0';
1168 memcpy(tmp, p, add_len);
1169 free(ri->full_path);
1170 ri->full_path = tmp;
1175 static int filter_by_parent(struct root_info *ri, u64 data)
1177 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1180 static int filter_deleted(struct root_info *ri, u64 data)
1185 static btrfs_list_filter_func all_filter_funcs[] = {
1186 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1187 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1188 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1189 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1190 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1191 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1192 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1193 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1194 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1195 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1196 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1197 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1198 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1201 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1203 struct btrfs_list_filter_set *set;
1206 size = sizeof(struct btrfs_list_filter_set) +
1207 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1210 fprintf(stderr, "memory allocation failed\n");
1214 memset(set, 0, size);
1215 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1220 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1225 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1226 enum btrfs_list_filter_enum filter, u64 data)
1228 struct btrfs_list_filter_set *set = *filter_set;
1232 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1233 BUG_ON(set->nfilters > set->total);
1235 if (set->nfilters == set->total) {
1236 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1237 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1238 set = realloc(set, size);
1240 fprintf(stderr, "memory allocation failed\n");
1244 memset(&set->filters[set->total], 0,
1245 BTRFS_LIST_NFILTERS_INCREASE *
1246 sizeof(struct btrfs_list_filter));
1247 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1251 BUG_ON(set->filters[set->nfilters].filter_func);
1253 if (filter == BTRFS_LIST_FILTER_DELETED)
1254 set->only_deleted = 1;
1256 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1257 set->filters[set->nfilters].data = data;
1262 static int filter_root(struct root_info *ri,
1263 struct btrfs_list_filter_set *set)
1270 if (set->only_deleted && !ri->deleted)
1273 if (!set->only_deleted && ri->deleted)
1276 for (i = 0; i < set->nfilters; i++) {
1277 if (!set->filters[i].filter_func)
1279 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1286 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1287 struct root_lookup *sort_tree,
1288 struct btrfs_list_filter_set *filter_set,
1289 struct btrfs_list_comparer_set *comp_set,
1293 struct root_info *entry;
1296 root_lookup_init(sort_tree);
1298 n = rb_last(&all_subvols->root);
1300 entry = rb_entry(n, struct root_info, rb_node);
1302 ret = resolve_root(all_subvols, entry, top_id);
1303 if (ret == -ENOENT) {
1304 entry->full_path = strdup("DELETED");
1307 ret = filter_root(entry, filter_set);
1309 sort_tree_insert(sort_tree, entry, comp_set);
1314 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1318 n = rb_first(&root_lookup->root);
1320 struct root_info *entry;
1322 entry = rb_entry(n, struct root_info, rb_node);
1323 ret = lookup_ino_path(fd, entry);
1324 if (ret && ret != -ENOENT)
1332 static void print_subvolume_column(struct root_info *subv,
1333 enum btrfs_list_column_enum column)
1336 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1338 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1341 case BTRFS_LIST_OBJECTID:
1342 printf("%llu", subv->root_id);
1344 case BTRFS_LIST_GENERATION:
1345 printf("%llu", subv->gen);
1347 case BTRFS_LIST_OGENERATION:
1348 printf("%llu", subv->ogen);
1350 case BTRFS_LIST_PARENT:
1351 printf("%llu", subv->ref_tree);
1353 case BTRFS_LIST_TOP_LEVEL:
1354 printf("%llu", subv->top_id);
1356 case BTRFS_LIST_OTIME:
1360 localtime_r(&subv->otime, &tm);
1361 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1366 case BTRFS_LIST_UUID:
1367 if (uuid_is_null(subv->uuid))
1368 strcpy(uuidparse, "-");
1370 uuid_unparse(subv->uuid, uuidparse);
1371 printf("%s", uuidparse);
1373 case BTRFS_LIST_PUUID:
1374 if (uuid_is_null(subv->puuid))
1375 strcpy(uuidparse, "-");
1377 uuid_unparse(subv->puuid, uuidparse);
1378 printf("%s", uuidparse);
1380 case BTRFS_LIST_RUUID:
1381 if (uuid_is_null(subv->ruuid))
1382 strcpy(uuidparse, "-");
1384 uuid_unparse(subv->ruuid, uuidparse);
1385 printf("%s", uuidparse);
1387 case BTRFS_LIST_PATH:
1388 BUG_ON(!subv->full_path);
1389 printf("%s", subv->full_path);
1396 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1400 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1401 if (!btrfs_list_columns[i].need_print)
1405 printf("%s",raw_prefix);
1407 print_subvolume_column(subv, i);
1412 static void print_single_volume_info_table(struct root_info *subv)
1416 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1417 if (!btrfs_list_columns[i].need_print)
1420 print_subvolume_column(subv, i);
1422 if (i != BTRFS_LIST_PATH)
1425 if (i == BTRFS_LIST_TOP_LEVEL)
1431 static void print_single_volume_info_default(struct root_info *subv)
1435 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1436 if (!btrfs_list_columns[i].need_print)
1439 printf("%s ", btrfs_list_columns[i].name);
1440 print_subvolume_column(subv, i);
1442 if (i != BTRFS_LIST_PATH)
1448 static void print_all_volume_info_tab_head(void)
1454 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1455 if (btrfs_list_columns[i].need_print)
1456 printf("%s\t", btrfs_list_columns[i].name);
1458 if (i == BTRFS_LIST_ALL-1)
1462 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1463 memset(barrier, 0, sizeof(barrier));
1465 if (btrfs_list_columns[i].need_print) {
1466 len = strlen(btrfs_list_columns[i].name);
1468 strcat(barrier, "-");
1470 printf("%s\t", barrier);
1472 if (i == BTRFS_LIST_ALL-1)
1477 static void print_all_volume_info(struct root_lookup *sorted_tree,
1478 int layout, char *raw_prefix)
1481 struct root_info *entry;
1483 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1484 print_all_volume_info_tab_head();
1486 n = rb_first(&sorted_tree->root);
1488 entry = rb_entry(n, struct root_info, sort_node);
1490 case BTRFS_LIST_LAYOUT_DEFAULT:
1491 print_single_volume_info_default(entry);
1493 case BTRFS_LIST_LAYOUT_TABLE:
1494 print_single_volume_info_table(entry);
1496 case BTRFS_LIST_LAYOUT_RAW:
1497 print_single_volume_info_raw(entry, raw_prefix);
1504 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1508 ret = __list_subvol_search(fd, root_lookup);
1510 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1516 * now we have an rbtree full of root_info objects, but we need to fill
1517 * in their path names within the subvol that is referencing each one.
1519 ret = __list_subvol_fill_paths(fd, root_lookup);
1523 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1524 struct btrfs_list_comparer_set *comp_set,
1525 int layout, int full_path, char *raw_prefix)
1527 struct root_lookup root_lookup;
1528 struct root_lookup root_sort;
1533 ret = btrfs_list_get_path_rootid(fd, &top_id);
1537 ret = btrfs_list_subvols(fd, &root_lookup);
1540 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1543 print_all_volume_info(&root_sort, layout, raw_prefix);
1544 __free_all_subvolumn(&root_lookup);
1549 static char *strdup_or_null(const char *s)
1556 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1559 struct root_lookup rl;
1560 struct rb_node *rbn;
1561 struct root_info *ri;
1564 ret = btrfs_list_get_path_rootid(fd, &root_id);
1568 ret = btrfs_list_subvols(fd, &rl);
1572 rbn = rb_first(&rl.root);
1574 ri = rb_entry(rbn, struct root_info, rb_node);
1575 rr = resolve_root(&rl, ri, root_id);
1576 if (rr == -ENOENT) {
1581 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1582 memcpy(the_ri, ri, offsetof(struct root_info, path));
1583 the_ri->path = strdup_or_null(ri->path);
1584 the_ri->name = strdup_or_null(ri->name);
1585 the_ri->full_path = strdup_or_null(ri->full_path);
1591 __free_all_subvolumn(&rl);
1595 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1596 struct btrfs_file_extent_item *item,
1597 u64 found_gen, u64 *cache_dirid,
1598 char **cache_dir_name, u64 *cache_ino,
1599 char **cache_full_name)
1603 u64 disk_offset = 0;
1609 if (sh->objectid == *cache_ino) {
1610 name = *cache_full_name;
1611 } else if (*cache_full_name) {
1612 free(*cache_full_name);
1613 *cache_full_name = NULL;
1616 name = ino_resolve(fd, sh->objectid, cache_dirid,
1618 *cache_full_name = name;
1619 *cache_ino = sh->objectid;
1624 type = btrfs_stack_file_extent_type(item);
1625 compressed = btrfs_stack_file_extent_compression(item);
1627 if (type == BTRFS_FILE_EXTENT_REG ||
1628 type == BTRFS_FILE_EXTENT_PREALLOC) {
1629 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1630 disk_offset = btrfs_stack_file_extent_offset(item);
1631 len = btrfs_stack_file_extent_num_bytes(item);
1632 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1635 len = btrfs_stack_file_extent_ram_bytes(item);
1637 printf("unhandled extent type %d for inode %llu "
1638 "file offset %llu gen %llu\n",
1640 (unsigned long long)sh->objectid,
1641 (unsigned long long)sh->offset,
1642 (unsigned long long)found_gen);
1646 printf("inode %llu file offset %llu len %llu disk start %llu "
1647 "offset %llu gen %llu flags ",
1648 (unsigned long long)sh->objectid,
1649 (unsigned long long)sh->offset,
1650 (unsigned long long)len,
1651 (unsigned long long)disk_start,
1652 (unsigned long long)disk_offset,
1653 (unsigned long long)found_gen);
1659 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1660 printf("%sPREALLOC", flags ? "|" : "");
1663 if (type == BTRFS_FILE_EXTENT_INLINE) {
1664 printf("%sINLINE", flags ? "|" : "");
1670 printf(" %s\n", name);
1674 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1677 struct btrfs_ioctl_search_args args;
1678 struct btrfs_ioctl_search_key *sk = &args.key;
1679 struct btrfs_ioctl_search_header sh;
1680 struct btrfs_file_extent_item *item;
1681 unsigned long off = 0;
1686 u64 cache_dirid = 0;
1688 char *cache_dir_name = NULL;
1689 char *cache_full_name = NULL;
1690 struct btrfs_file_extent_item backup;
1692 memset(&backup, 0, sizeof(backup));
1693 memset(&args, 0, sizeof(args));
1695 sk->tree_id = root_id;
1698 * set all the other params to the max, we'll take any objectid
1701 sk->max_objectid = (u64)-1;
1702 sk->max_offset = (u64)-1;
1703 sk->max_transid = (u64)-1;
1704 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1705 sk->min_transid = oldest_gen;
1706 /* just a big number, doesn't matter much */
1707 sk->nr_items = 4096;
1709 max_found = find_root_gen(fd);
1711 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1714 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1718 /* the ioctl returns the number of item it found in nr_items */
1719 if (sk->nr_items == 0)
1725 * for each item, pull the key out of the header and then
1726 * read the root_ref item it contains
1728 for (i = 0; i < sk->nr_items; i++) {
1729 memcpy(&sh, args.buf + off, sizeof(sh));
1733 * just in case the item was too big, pass something other
1739 item = (struct btrfs_file_extent_item *)(args.buf +
1741 found_gen = btrfs_stack_file_extent_generation(item);
1742 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1743 found_gen >= oldest_gen) {
1744 print_one_extent(fd, &sh, item, found_gen,
1745 &cache_dirid, &cache_dir_name,
1746 &cache_ino, &cache_full_name);
1751 * record the mins in sk so we can make sure the
1752 * next search doesn't repeat this root
1754 sk->min_objectid = sh.objectid;
1755 sk->min_offset = sh.offset;
1756 sk->min_type = sh.type;
1758 sk->nr_items = 4096;
1759 if (sk->min_offset < (u64)-1)
1761 else if (sk->min_objectid < (u64)-1) {
1768 free(cache_dir_name);
1769 free(cache_full_name);
1770 printf("transid marker was %llu\n", (unsigned long long)max_found);
1774 char *btrfs_list_path_for_root(int fd, u64 root)
1776 struct root_lookup root_lookup;
1778 char *ret_path = NULL;
1782 ret = btrfs_list_get_path_rootid(fd, &top_id);
1784 return ERR_PTR(ret);
1786 ret = __list_subvol_search(fd, &root_lookup);
1788 return ERR_PTR(ret);
1790 ret = __list_subvol_fill_paths(fd, &root_lookup);
1792 return ERR_PTR(ret);
1794 n = rb_last(&root_lookup.root);
1796 struct root_info *entry;
1798 entry = rb_entry(n, struct root_info, rb_node);
1799 ret = resolve_root(&root_lookup, entry, top_id);
1800 if (ret == -ENOENT && entry->root_id == root) {
1804 if (entry->root_id == root) {
1805 ret_path = entry->full_path;
1806 entry->full_path = NULL;
1811 __free_all_subvolumn(&root_lookup);
1816 int btrfs_list_parse_sort_string(char *opt_arg,
1817 struct btrfs_list_comparer_set **comps)
1825 while ((p = strtok(opt_arg, ",")) != NULL) {
1827 ptr_argv = all_sort_items;
1830 if (strcmp(*ptr_argv, p) == 0) {
1835 if (strcmp(*ptr_argv, p) == 0) {
1852 } else if (*p == '-') {
1858 what_to_sort = btrfs_list_get_sort_item(p);
1859 btrfs_list_setup_comparer(comps, what_to_sort, order);
1868 * This function is used to parse the argument of filter condition.
1870 * type is the filter object.
1872 int btrfs_list_parse_filter_string(char *opt_arg,
1873 struct btrfs_list_filter_set **filters,
1874 enum btrfs_list_filter_enum type)
1879 switch (*(opt_arg++)) {
1881 arg = arg_strtou64(opt_arg);
1884 btrfs_list_setup_filter(filters, type, arg);
1887 arg = arg_strtou64(opt_arg);
1890 btrfs_list_setup_filter(filters, type, arg);
1894 arg = arg_strtou64(opt_arg);
1896 btrfs_list_setup_filter(filters, type, arg);
1903 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1906 struct btrfs_ioctl_ino_lookup_args args;
1908 memset(&args, 0, sizeof(args));
1909 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1911 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1914 "ERROR: can't perform the search -%s\n",
1918 *treeid = args.treeid;