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 next = found->ref_tree;
584 if (next == top_id) {
590 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
593 if (next == BTRFS_FS_TREE_OBJECTID) {
599 * if the ref_tree wasn't in our tree of roots, the
600 * subvolume was deleted.
602 found = root_tree_search(rl, next);
609 ri->full_path = full_path;
615 * for a single root_info, ask the kernel to give us a path name
616 * inside it's ref_root for the dir_id where it lives.
618 * This fills in root_info->path with the path to the directory and and
619 * appends this root's name.
621 static int lookup_ino_path(int fd, struct root_info *ri)
623 struct btrfs_ioctl_ino_lookup_args args;
632 memset(&args, 0, sizeof(args));
633 args.treeid = ri->ref_tree;
634 args.objectid = ri->dir_id;
636 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
643 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
644 (unsigned long long)ri->ref_tree,
651 * we're in a subdirectory of ref_tree, the kernel ioctl
652 * puts a / in there for us
654 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
656 perror("malloc failed");
659 strcpy(ri->path, args.name);
660 strcat(ri->path, ri->name);
662 /* we're at the root of ref_tree */
663 ri->path = strdup(ri->name);
665 perror("strdup failed");
672 /* finding the generation for a given path is a two step process.
673 * First we use the inode loookup routine to find out the root id
675 * Then we use the tree search ioctl to scan all the root items for a
676 * given root id and spit out the latest generation we can find
678 static u64 find_root_gen(int fd)
680 struct btrfs_ioctl_ino_lookup_args ino_args;
682 struct btrfs_ioctl_search_args args;
683 struct btrfs_ioctl_search_key *sk = &args.key;
684 struct btrfs_ioctl_search_header sh;
685 unsigned long off = 0;
690 memset(&ino_args, 0, sizeof(ino_args));
691 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
693 /* this ioctl fills in ino_args->treeid */
694 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
697 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
698 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
703 memset(&args, 0, sizeof(args));
708 * there may be more than one ROOT_ITEM key if there are
709 * snapshots pending deletion, we have to loop through
712 sk->min_objectid = ino_args.treeid;
713 sk->max_objectid = ino_args.treeid;
714 sk->max_type = BTRFS_ROOT_ITEM_KEY;
715 sk->min_type = BTRFS_ROOT_ITEM_KEY;
716 sk->max_offset = (u64)-1;
717 sk->max_transid = (u64)-1;
721 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
724 fprintf(stderr, "ERROR: can't perform the search - %s\n",
728 /* the ioctl returns the number of item it found in nr_items */
729 if (sk->nr_items == 0)
733 for (i = 0; i < sk->nr_items; i++) {
734 struct btrfs_root_item *item;
736 memcpy(&sh, args.buf + off, sizeof(sh));
738 item = (struct btrfs_root_item *)(args.buf + off);
741 sk->min_objectid = sh.objectid;
742 sk->min_type = sh.type;
743 sk->min_offset = sh.offset;
745 if (sh.objectid > ino_args.treeid)
748 if (sh.objectid == ino_args.treeid &&
749 sh.type == BTRFS_ROOT_ITEM_KEY) {
750 max_found = max(max_found,
751 btrfs_root_generation(item));
754 if (sk->min_offset < (u64)-1)
759 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
761 if (sk->min_objectid != ino_args.treeid)
767 /* pass in a directory id and this will return
768 * the full path of the parent directory inside its
771 * It may return NULL if it is in the root, or an ERR_PTR if things
774 static char *__ino_resolve(int fd, u64 dirid)
776 struct btrfs_ioctl_ino_lookup_args args;
781 memset(&args, 0, sizeof(args));
782 args.objectid = dirid;
784 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
787 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
788 (unsigned long long)dirid, strerror(e) );
794 * we're in a subdirectory of ref_tree, the kernel ioctl
795 * puts a / in there for us
797 full = strdup(args.name);
799 perror("malloc failed");
800 return ERR_PTR(-ENOMEM);
803 /* we're at the root of ref_tree */
810 * simple string builder, returning a new string with both
813 static char *build_name(char *dirid, char *name)
819 full = malloc(strlen(dirid) + strlen(name) + 1);
828 * given an inode number, this returns the full path name inside the subvolume
829 * to that file/directory. cache_dirid and cache_name are used to
830 * cache the results so we can avoid tree searches if a later call goes
831 * to the same directory or file name
833 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
841 struct btrfs_ioctl_search_args args;
842 struct btrfs_ioctl_search_key *sk = &args.key;
843 struct btrfs_ioctl_search_header *sh;
844 unsigned long off = 0;
848 memset(&args, 0, sizeof(args));
853 * step one, we search for the inode back ref. We just use the first
856 sk->min_objectid = ino;
857 sk->max_objectid = ino;
858 sk->max_type = BTRFS_INODE_REF_KEY;
859 sk->max_offset = (u64)-1;
860 sk->min_type = BTRFS_INODE_REF_KEY;
861 sk->max_transid = (u64)-1;
864 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
867 fprintf(stderr, "ERROR: can't perform the search - %s\n",
871 /* the ioctl returns the number of item it found in nr_items */
872 if (sk->nr_items == 0)
876 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
878 if (sh->type == BTRFS_INODE_REF_KEY) {
879 struct btrfs_inode_ref *ref;
882 ref = (struct btrfs_inode_ref *)(sh + 1);
883 namelen = btrfs_stack_inode_ref_name_len(ref);
885 name = (char *)(ref + 1);
886 name = strndup(name, namelen);
888 /* use our cached value */
889 if (dirid == *cache_dirid && *cache_name) {
890 dirname = *cache_name;
897 * the inode backref gives us the file name and the parent directory id.
898 * From here we use __ino_resolve to get the path to the parent
900 dirname = __ino_resolve(fd, dirid);
902 full = build_name(dirname, name);
903 if (*cache_name && dirname != *cache_name)
906 *cache_name = dirname;
907 *cache_dirid = dirid;
913 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
915 struct btrfs_ioctl_search_args args;
916 struct btrfs_ioctl_search_key *sk = &args.key;
917 struct btrfs_ioctl_search_header *sh;
921 memset(&args, 0, sizeof(args));
924 * search for a dir item with a name 'default' in the tree of
925 * tree roots, it should point us to a default root
929 /* don't worry about ancient format and request only one item */
932 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
933 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
934 sk->max_type = BTRFS_DIR_ITEM_KEY;
935 sk->min_type = BTRFS_DIR_ITEM_KEY;
936 sk->max_offset = (u64)-1;
937 sk->max_transid = (u64)-1;
939 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
943 /* the ioctl returns the number of items it found in nr_items */
944 if (sk->nr_items == 0)
947 sh = (struct btrfs_ioctl_search_header *)args.buf;
949 if (sh->type == BTRFS_DIR_ITEM_KEY) {
950 struct btrfs_dir_item *di;
954 di = (struct btrfs_dir_item *)(sh + 1);
955 name_len = btrfs_stack_dir_name_len(di);
956 name = (char *)(di + 1);
958 if (!strncmp("default", name, name_len))
959 found = btrfs_disk_key_objectid(&di->location);
967 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
970 struct btrfs_ioctl_search_args args;
971 struct btrfs_ioctl_search_key *sk = &args.key;
972 struct btrfs_ioctl_search_header sh;
973 struct btrfs_root_ref *ref;
974 struct btrfs_root_item *ri;
975 unsigned long off = 0;
984 u8 uuid[BTRFS_UUID_SIZE];
985 u8 puuid[BTRFS_UUID_SIZE];
987 root_lookup_init(root_lookup);
988 memset(&args, 0, sizeof(args));
990 /* search in the tree of tree roots */
994 * set the min and max to backref keys. The search will
995 * only send back this type of key now.
997 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
998 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1000 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1003 * set all the other params to the max, we'll take any objectid
1006 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1007 sk->max_offset = (u64)-1;
1008 sk->max_transid = (u64)-1;
1010 /* just a big number, doesn't matter much */
1011 sk->nr_items = 4096;
1014 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1017 /* the ioctl returns the number of item it found in nr_items */
1018 if (sk->nr_items == 0)
1024 * for each item, pull the key out of the header and then
1025 * read the root_ref item it contains
1027 for (i = 0; i < sk->nr_items; i++) {
1028 memcpy(&sh, args.buf + off, sizeof(sh));
1030 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1031 ref = (struct btrfs_root_ref *)(args.buf + off);
1032 name_len = btrfs_stack_root_ref_name_len(ref);
1033 name = (char *)(ref + 1);
1034 dir_id = btrfs_stack_root_ref_dirid(ref);
1036 add_root(root_lookup, sh.objectid, sh.offset,
1037 0, 0, dir_id, name, name_len, 0, 0, 0,
1039 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1040 ri = (struct btrfs_root_item *)(args.buf + off);
1041 gen = btrfs_root_generation(ri);
1042 flags = btrfs_root_flags(ri);
1044 sizeof(struct btrfs_root_item_v0)) {
1045 t = btrfs_stack_timespec_sec(&ri->otime);
1046 ogen = btrfs_root_otransid(ri);
1047 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1048 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1052 memset(uuid, 0, BTRFS_UUID_SIZE);
1053 memset(puuid, 0, BTRFS_UUID_SIZE);
1056 add_root(root_lookup, sh.objectid, 0,
1057 sh.offset, flags, 0, NULL, 0, ogen,
1058 gen, t, uuid, puuid);
1064 * record the mins in sk so we can make sure the
1065 * next search doesn't repeat this root
1067 sk->min_objectid = sh.objectid;
1068 sk->min_type = sh.type;
1069 sk->min_offset = sh.offset;
1071 sk->nr_items = 4096;
1073 if (!sk->min_offset) /* overflow */
1078 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1079 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1084 if (sk->min_objectid > sk->max_objectid)
1091 static int filter_by_rootid(struct root_info *ri, u64 data)
1093 return ri->root_id == data;
1096 static int filter_snapshot(struct root_info *ri, u64 data)
1098 return !!ri->root_offset;
1101 static int filter_flags(struct root_info *ri, u64 flags)
1103 return ri->flags & flags;
1106 static int filter_gen_more(struct root_info *ri, u64 data)
1108 return ri->gen >= data;
1111 static int filter_gen_less(struct root_info *ri, u64 data)
1113 return ri->gen <= data;
1116 static int filter_gen_equal(struct root_info *ri, u64 data)
1118 return ri->gen == data;
1121 static int filter_cgen_more(struct root_info *ri, u64 data)
1123 return ri->ogen >= data;
1126 static int filter_cgen_less(struct root_info *ri, u64 data)
1128 return ri->ogen <= data;
1131 static int filter_cgen_equal(struct root_info *ri, u64 data)
1133 return ri->ogen == data;
1136 static int filter_topid_equal(struct root_info *ri, u64 data)
1138 return ri->top_id == data;
1141 static int filter_full_path(struct root_info *ri, u64 data)
1143 if (ri->full_path && ri->top_id != data) {
1145 char p[] = "<FS_TREE>";
1146 int add_len = strlen(p);
1147 int len = strlen(ri->full_path);
1149 tmp = malloc(len + add_len + 2);
1151 fprintf(stderr, "memory allocation failed\n");
1154 memcpy(tmp + add_len + 1, ri->full_path, len);
1155 tmp[len + add_len + 1] = '\0';
1157 memcpy(tmp, p, add_len);
1158 free(ri->full_path);
1159 ri->full_path = tmp;
1164 static int filter_by_parent(struct root_info *ri, u64 data)
1166 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1169 static int filter_deleted(struct root_info *ri, u64 data)
1174 static btrfs_list_filter_func all_filter_funcs[] = {
1175 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1176 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1177 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1178 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1179 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1180 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1181 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1182 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1183 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1184 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1185 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1186 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1187 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1190 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1192 struct btrfs_list_filter_set *set;
1195 size = sizeof(struct btrfs_list_filter_set) +
1196 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1199 fprintf(stderr, "memory allocation failed\n");
1203 memset(set, 0, size);
1204 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1209 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1214 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1215 enum btrfs_list_filter_enum filter, u64 data)
1217 struct btrfs_list_filter_set *set = *filter_set;
1221 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1222 BUG_ON(set->nfilters > set->total);
1224 if (filter == BTRFS_LIST_FILTER_DELETED) {
1225 set->only_deleted = 1;
1229 if (set->nfilters == set->total) {
1230 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1231 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1232 set = realloc(set, size);
1234 fprintf(stderr, "memory allocation failed\n");
1238 memset(&set->filters[set->total], 0,
1239 BTRFS_LIST_NFILTERS_INCREASE *
1240 sizeof(struct btrfs_list_filter));
1241 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1245 BUG_ON(set->filters[set->nfilters].filter_func);
1247 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1248 set->filters[set->nfilters].data = data;
1253 static int filter_root(struct root_info *ri,
1254 struct btrfs_list_filter_set *set)
1258 if (!set || !set->nfilters)
1261 if (set->only_deleted && !ri->deleted)
1264 if (!set->only_deleted && ri->deleted)
1267 for (i = 0; i < set->nfilters; i++) {
1268 if (!set->filters[i].filter_func)
1270 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1277 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1278 struct root_lookup *sort_tree,
1279 struct btrfs_list_filter_set *filter_set,
1280 struct btrfs_list_comparer_set *comp_set,
1284 struct root_info *entry;
1287 root_lookup_init(sort_tree);
1289 n = rb_last(&all_subvols->root);
1291 entry = rb_entry(n, struct root_info, rb_node);
1293 ret = resolve_root(all_subvols, entry, top_id);
1294 if (ret == -ENOENT) {
1295 entry->full_path = strdup("DELETED");
1298 ret = filter_root(entry, filter_set);
1300 sort_tree_insert(sort_tree, entry, comp_set);
1305 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1309 n = rb_first(&root_lookup->root);
1311 struct root_info *entry;
1313 entry = rb_entry(n, struct root_info, rb_node);
1314 ret = lookup_ino_path(fd, entry);
1315 if (ret && ret != -ENOENT)
1323 static void print_subvolume_column(struct root_info *subv,
1324 enum btrfs_list_column_enum column)
1327 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1329 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1332 case BTRFS_LIST_OBJECTID:
1333 printf("%llu", subv->root_id);
1335 case BTRFS_LIST_GENERATION:
1336 printf("%llu", subv->gen);
1338 case BTRFS_LIST_OGENERATION:
1339 printf("%llu", subv->ogen);
1341 case BTRFS_LIST_PARENT:
1342 printf("%llu", subv->ref_tree);
1344 case BTRFS_LIST_TOP_LEVEL:
1345 printf("%llu", subv->top_id);
1347 case BTRFS_LIST_OTIME:
1351 localtime_r(&subv->otime, &tm);
1352 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1357 case BTRFS_LIST_UUID:
1358 if (uuid_is_null(subv->uuid))
1359 strcpy(uuidparse, "-");
1361 uuid_unparse(subv->uuid, uuidparse);
1362 printf("%s", uuidparse);
1364 case BTRFS_LIST_PUUID:
1365 if (uuid_is_null(subv->puuid))
1366 strcpy(uuidparse, "-");
1368 uuid_unparse(subv->puuid, uuidparse);
1369 printf("%s", uuidparse);
1371 case BTRFS_LIST_PATH:
1372 BUG_ON(!subv->full_path);
1373 printf("%s", subv->full_path);
1380 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1384 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1385 if (!btrfs_list_columns[i].need_print)
1389 printf("%s",raw_prefix);
1391 print_subvolume_column(subv, i);
1396 static void print_single_volume_info_table(struct root_info *subv)
1400 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1401 if (!btrfs_list_columns[i].need_print)
1404 print_subvolume_column(subv, i);
1406 if (i != BTRFS_LIST_PATH)
1409 if (i == BTRFS_LIST_TOP_LEVEL)
1415 static void print_single_volume_info_default(struct root_info *subv)
1419 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1420 if (!btrfs_list_columns[i].need_print)
1423 printf("%s ", btrfs_list_columns[i].name);
1424 print_subvolume_column(subv, i);
1426 if (i != BTRFS_LIST_PATH)
1432 static void print_all_volume_info_tab_head(void)
1438 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1439 if (btrfs_list_columns[i].need_print)
1440 printf("%s\t", btrfs_list_columns[i].name);
1442 if (i == BTRFS_LIST_ALL-1)
1446 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1447 memset(barrier, 0, sizeof(barrier));
1449 if (btrfs_list_columns[i].need_print) {
1450 len = strlen(btrfs_list_columns[i].name);
1452 strcat(barrier, "-");
1454 printf("%s\t", barrier);
1456 if (i == BTRFS_LIST_ALL-1)
1461 static void print_all_volume_info(struct root_lookup *sorted_tree,
1462 int layout, char *raw_prefix)
1465 struct root_info *entry;
1467 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1468 print_all_volume_info_tab_head();
1470 n = rb_first(&sorted_tree->root);
1472 entry = rb_entry(n, struct root_info, sort_node);
1474 case BTRFS_LIST_LAYOUT_DEFAULT:
1475 print_single_volume_info_default(entry);
1477 case BTRFS_LIST_LAYOUT_TABLE:
1478 print_single_volume_info_table(entry);
1480 case BTRFS_LIST_LAYOUT_RAW:
1481 print_single_volume_info_raw(entry, raw_prefix);
1488 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1492 ret = __list_subvol_search(fd, root_lookup);
1494 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1500 * now we have an rbtree full of root_info objects, but we need to fill
1501 * in their path names within the subvol that is referencing each one.
1503 ret = __list_subvol_fill_paths(fd, root_lookup);
1507 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1508 struct btrfs_list_comparer_set *comp_set,
1509 int layout, int full_path, char *raw_prefix)
1511 struct root_lookup root_lookup;
1512 struct root_lookup root_sort;
1517 ret = btrfs_list_get_path_rootid(fd, &top_id);
1521 ret = btrfs_list_subvols(fd, &root_lookup);
1524 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1527 print_all_volume_info(&root_sort, layout, raw_prefix);
1528 __free_all_subvolumn(&root_lookup);
1533 static char *strdup_or_null(const char *s)
1540 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1543 struct root_lookup rl;
1544 struct rb_node *rbn;
1545 struct root_info *ri;
1548 ret = btrfs_list_get_path_rootid(fd, &root_id);
1552 ret = btrfs_list_subvols(fd, &rl);
1556 rbn = rb_first(&rl.root);
1558 ri = rb_entry(rbn, struct root_info, rb_node);
1559 rr = resolve_root(&rl, ri, root_id);
1560 if (rr == -ENOENT) {
1565 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1566 memcpy(the_ri, ri, offsetof(struct root_info, path));
1567 the_ri->path = strdup_or_null(ri->path);
1568 the_ri->name = strdup_or_null(ri->name);
1569 the_ri->full_path = strdup_or_null(ri->full_path);
1575 __free_all_subvolumn(&rl);
1579 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1580 struct btrfs_file_extent_item *item,
1581 u64 found_gen, u64 *cache_dirid,
1582 char **cache_dir_name, u64 *cache_ino,
1583 char **cache_full_name)
1587 u64 disk_offset = 0;
1593 if (sh->objectid == *cache_ino) {
1594 name = *cache_full_name;
1595 } else if (*cache_full_name) {
1596 free(*cache_full_name);
1597 *cache_full_name = NULL;
1600 name = ino_resolve(fd, sh->objectid, cache_dirid,
1602 *cache_full_name = name;
1603 *cache_ino = sh->objectid;
1608 type = btrfs_stack_file_extent_type(item);
1609 compressed = btrfs_stack_file_extent_compression(item);
1611 if (type == BTRFS_FILE_EXTENT_REG ||
1612 type == BTRFS_FILE_EXTENT_PREALLOC) {
1613 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1614 disk_offset = btrfs_stack_file_extent_offset(item);
1615 len = btrfs_stack_file_extent_num_bytes(item);
1616 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1619 len = btrfs_stack_file_extent_ram_bytes(item);
1621 printf("unhandled extent type %d for inode %llu "
1622 "file offset %llu gen %llu\n",
1624 (unsigned long long)sh->objectid,
1625 (unsigned long long)sh->offset,
1626 (unsigned long long)found_gen);
1630 printf("inode %llu file offset %llu len %llu disk start %llu "
1631 "offset %llu gen %llu flags ",
1632 (unsigned long long)sh->objectid,
1633 (unsigned long long)sh->offset,
1634 (unsigned long long)len,
1635 (unsigned long long)disk_start,
1636 (unsigned long long)disk_offset,
1637 (unsigned long long)found_gen);
1643 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1644 printf("%sPREALLOC", flags ? "|" : "");
1647 if (type == BTRFS_FILE_EXTENT_INLINE) {
1648 printf("%sINLINE", flags ? "|" : "");
1654 printf(" %s\n", name);
1658 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1661 struct btrfs_ioctl_search_args args;
1662 struct btrfs_ioctl_search_key *sk = &args.key;
1663 struct btrfs_ioctl_search_header sh;
1664 struct btrfs_file_extent_item *item;
1665 unsigned long off = 0;
1670 u64 cache_dirid = 0;
1672 char *cache_dir_name = NULL;
1673 char *cache_full_name = NULL;
1674 struct btrfs_file_extent_item backup;
1676 memset(&backup, 0, sizeof(backup));
1677 memset(&args, 0, sizeof(args));
1679 sk->tree_id = root_id;
1682 * set all the other params to the max, we'll take any objectid
1685 sk->max_objectid = (u64)-1;
1686 sk->max_offset = (u64)-1;
1687 sk->max_transid = (u64)-1;
1688 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1689 sk->min_transid = oldest_gen;
1690 /* just a big number, doesn't matter much */
1691 sk->nr_items = 4096;
1693 max_found = find_root_gen(fd);
1695 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1698 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1702 /* the ioctl returns the number of item it found in nr_items */
1703 if (sk->nr_items == 0)
1709 * for each item, pull the key out of the header and then
1710 * read the root_ref item it contains
1712 for (i = 0; i < sk->nr_items; i++) {
1713 memcpy(&sh, args.buf + off, sizeof(sh));
1717 * just in case the item was too big, pass something other
1723 item = (struct btrfs_file_extent_item *)(args.buf +
1725 found_gen = btrfs_stack_file_extent_generation(item);
1726 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1727 found_gen >= oldest_gen) {
1728 print_one_extent(fd, &sh, item, found_gen,
1729 &cache_dirid, &cache_dir_name,
1730 &cache_ino, &cache_full_name);
1735 * record the mins in sk so we can make sure the
1736 * next search doesn't repeat this root
1738 sk->min_objectid = sh.objectid;
1739 sk->min_offset = sh.offset;
1740 sk->min_type = sh.type;
1742 sk->nr_items = 4096;
1743 if (sk->min_offset < (u64)-1)
1745 else if (sk->min_objectid < (u64)-1) {
1752 free(cache_dir_name);
1753 free(cache_full_name);
1754 printf("transid marker was %llu\n", (unsigned long long)max_found);
1758 char *btrfs_list_path_for_root(int fd, u64 root)
1760 struct root_lookup root_lookup;
1762 char *ret_path = NULL;
1766 ret = btrfs_list_get_path_rootid(fd, &top_id);
1768 return ERR_PTR(ret);
1770 ret = __list_subvol_search(fd, &root_lookup);
1772 return ERR_PTR(ret);
1774 ret = __list_subvol_fill_paths(fd, &root_lookup);
1776 return ERR_PTR(ret);
1778 n = rb_last(&root_lookup.root);
1780 struct root_info *entry;
1782 entry = rb_entry(n, struct root_info, rb_node);
1783 ret = resolve_root(&root_lookup, entry, top_id);
1784 if (ret == -ENOENT && entry->root_id == root) {
1788 if (entry->root_id == root) {
1789 ret_path = entry->full_path;
1790 entry->full_path = NULL;
1795 __free_all_subvolumn(&root_lookup);
1800 int btrfs_list_parse_sort_string(char *opt_arg,
1801 struct btrfs_list_comparer_set **comps)
1809 while ((p = strtok(opt_arg, ",")) != NULL) {
1811 ptr_argv = all_sort_items;
1814 if (strcmp(*ptr_argv, p) == 0) {
1819 if (strcmp(*ptr_argv, p) == 0) {
1836 } else if (*p == '-') {
1842 what_to_sort = btrfs_list_get_sort_item(p);
1843 btrfs_list_setup_comparer(comps, what_to_sort, order);
1852 * This function is used to parse the argument of filter condition.
1854 * type is the filter object.
1856 int btrfs_list_parse_filter_string(char *opt_arg,
1857 struct btrfs_list_filter_set **filters,
1858 enum btrfs_list_filter_enum type)
1862 char *ptr_parse_end = NULL;
1863 char *ptr_opt_arg_end = opt_arg + strlen(opt_arg);
1865 switch (*(opt_arg++)) {
1867 arg = (u64)strtol(opt_arg, &ptr_parse_end, 10);
1869 if (ptr_parse_end != ptr_opt_arg_end)
1872 btrfs_list_setup_filter(filters, type, arg);
1875 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1877 if (ptr_parse_end != ptr_opt_arg_end)
1880 btrfs_list_setup_filter(filters, type, arg);
1884 arg = (u64)strtoll(opt_arg, &ptr_parse_end, 10);
1886 if (ptr_parse_end != ptr_opt_arg_end)
1888 btrfs_list_setup_filter(filters, type, arg);
1895 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1898 struct btrfs_ioctl_ino_lookup_args args;
1900 memset(&args, 0, sizeof(args));
1901 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1903 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1906 "ERROR: can't perform the search -%s\n",
1910 *treeid = args.treeid;