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.
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
27 #include <sys/types.h>
34 #include "transaction.h"
36 #include <uuid/uuid.h>
37 #include "btrfs-list.h"
39 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
40 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
42 /* we store all the roots we find in an rbtree so that we can
43 * search for them later.
50 * one of these for each root we find.
53 struct rb_node rb_node;
54 struct rb_node sort_node;
59 /* equal the offset of the root's key */
62 /* flags of the root */
65 /* the id of the root that references this one */
68 /* the dir id we're in from ref_tree */
73 /* generation when the root is created or last updated */
76 /* creation generation of this root in sec*/
79 /* creation time of this root in sec*/
82 u8 uuid[BTRFS_UUID_SIZE];
83 u8 puuid[BTRFS_UUID_SIZE];
85 /* path from the subvol we live in to this root, including the
86 * root's name. This is null until we do the extra lookup ioctl.
90 /* the name of this root in the directory it lives in */
100 } btrfs_list_columns[] = {
108 .column_name = "Gen",
113 .column_name = "CGen",
118 .column_name = "Parent",
123 .column_name = "Top Level",
128 .column_name = "OTime",
132 .name = "parent_uuid",
133 .column_name = "Parent UUID",
138 .column_name = "UUID",
143 .column_name = "Path",
153 static btrfs_list_filter_func all_filter_funcs[];
154 static btrfs_list_comp_func all_comp_funcs[];
156 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
160 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
162 if (column < BTRFS_LIST_ALL) {
163 btrfs_list_columns[column].need_print = 1;
167 for (i = 0; i < BTRFS_LIST_ALL; i++)
168 btrfs_list_columns[i].need_print = 1;
171 static void root_lookup_init(struct root_lookup *tree)
173 tree->root.rb_node = NULL;
176 static int comp_entry_with_rootid(struct root_info *entry1,
177 struct root_info *entry2,
182 if (entry1->root_id > entry2->root_id)
184 else if (entry1->root_id < entry2->root_id)
189 return is_descending ? -ret : ret;
192 static int comp_entry_with_gen(struct root_info *entry1,
193 struct root_info *entry2,
198 if (entry1->gen > entry2->gen)
200 else if (entry1->gen < entry2->gen)
205 return is_descending ? -ret : ret;
208 static int comp_entry_with_ogen(struct root_info *entry1,
209 struct root_info *entry2,
214 if (entry1->ogen > entry2->ogen)
216 else if (entry1->ogen < entry2->ogen)
221 return is_descending ? -ret : ret;
224 static int comp_entry_with_path(struct root_info *entry1,
225 struct root_info *entry2,
230 if (strcmp(entry1->full_path, entry2->full_path) > 0)
232 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
237 return is_descending ? -ret : ret;
240 static btrfs_list_comp_func all_comp_funcs[] = {
241 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
242 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
243 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
244 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
247 static char *all_sort_items[] = {
248 [BTRFS_LIST_COMP_ROOTID] = "rootid",
249 [BTRFS_LIST_COMP_OGEN] = "ogen",
250 [BTRFS_LIST_COMP_GEN] = "gen",
251 [BTRFS_LIST_COMP_PATH] = "path",
252 [BTRFS_LIST_COMP_MAX] = NULL,
255 static int btrfs_list_get_sort_item(char *sort_name)
259 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
260 if (strcmp(sort_name, all_sort_items[i]) == 0)
266 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
268 struct btrfs_list_comparer_set *set;
271 size = sizeof(struct btrfs_list_comparer_set) +
272 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
275 fprintf(stderr, "memory allocation failed\n");
279 memset(set, 0, size);
280 set->total = BTRFS_LIST_NCOMPS_INCREASE;
285 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
290 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
291 enum btrfs_list_comp_enum comparer,
294 struct btrfs_list_comparer_set *set = *comp_set;
298 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
299 BUG_ON(set->ncomps > set->total);
301 if (set->ncomps == set->total) {
302 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
303 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
304 set = realloc(set, size);
306 fprintf(stderr, "memory allocation failed\n");
310 memset(&set->comps[set->total], 0,
311 BTRFS_LIST_NCOMPS_INCREASE *
312 sizeof(struct btrfs_list_comparer));
313 set->total += BTRFS_LIST_NCOMPS_INCREASE;
317 BUG_ON(set->comps[set->ncomps].comp_func);
319 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
320 set->comps[set->ncomps].is_descending = is_descending;
325 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
326 struct btrfs_list_comparer_set *set)
328 int rootid_compared = 0;
331 if (!set || !set->ncomps)
334 for (i = 0; i < set->ncomps; i++) {
335 if (!set->comps[i].comp_func)
338 ret = set->comps[i].comp_func(entry1, entry2,
339 set->comps[i].is_descending);
343 if (set->comps[i].comp_func == comp_entry_with_rootid)
347 if (!rootid_compared) {
349 ret = comp_entry_with_rootid(entry1, entry2, 0);
355 static int sort_tree_insert(struct root_lookup *sort_tree,
356 struct root_info *ins,
357 struct btrfs_list_comparer_set *comp_set)
359 struct rb_node **p = &sort_tree->root.rb_node;
360 struct rb_node *parent = NULL;
361 struct root_info *curr;
366 curr = rb_entry(parent, struct root_info, sort_node);
368 ret = sort_comp(ins, curr, comp_set);
377 rb_link_node(&ins->sort_node, parent, p);
378 rb_insert_color(&ins->sort_node, &sort_tree->root);
383 * insert a new root into the tree. returns the existing root entry
384 * if one is already there. Both root_id and ref_tree are used
387 static int root_tree_insert(struct root_lookup *root_tree,
388 struct root_info *ins)
390 struct rb_node **p = &root_tree->root.rb_node;
391 struct rb_node * parent = NULL;
392 struct root_info *curr;
397 curr = rb_entry(parent, struct root_info, rb_node);
399 ret = comp_entry_with_rootid(ins, curr, 0);
408 rb_link_node(&ins->rb_node, parent, p);
409 rb_insert_color(&ins->rb_node, &root_tree->root);
414 * find a given root id in the tree. We return the smallest one,
415 * rb_next can be used to move forward looking for more if required
417 static struct root_info *root_tree_search(struct root_lookup *root_tree,
420 struct rb_node *n = root_tree->root.rb_node;
421 struct root_info *entry;
422 struct root_info tmp;
425 tmp.root_id = root_id;
428 entry = rb_entry(n, struct root_info, rb_node);
430 ret = comp_entry_with_rootid(&tmp, entry, 0);
441 static int update_root(struct root_lookup *root_lookup,
442 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
443 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
444 time_t ot, void *uuid, void *puuid)
446 struct root_info *ri;
448 ri = root_tree_search(root_lookup, root_id);
449 if (!ri || ri->root_id != root_id)
451 if (name && name_len > 0) {
455 ri->name = malloc(name_len + 1);
457 fprintf(stderr, "memory allocation failed\n");
460 strncpy(ri->name, name, name_len);
461 ri->name[name_len] = 0;
464 ri->ref_tree = ref_tree;
466 ri->root_offset = root_offset;
475 if (!ri->ogen && root_offset)
476 ri->ogen = root_offset;
480 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
482 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
488 * add_root - update the existed root, or allocate a new root and insert it
489 * into the lookup tree.
490 * root_id: object id of the root
491 * ref_tree: object id of the referring root.
492 * root_offset: offset value of the root'key
493 * dir_id: inode id of the directory in ref_tree where this root can be found.
494 * name: the name of root_id in that directory
495 * name_len: the length of name
496 * ogen: the original generation of the root
497 * gen: the current generation of the root
498 * ot: the original time(create time) of the root
499 * uuid: uuid of the root
500 * puuid: uuid of the root parent if any
502 static int add_root(struct root_lookup *root_lookup,
503 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
504 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
505 time_t ot, void *uuid, void *puuid)
507 struct root_info *ri;
510 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
511 dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
515 ri = malloc(sizeof(*ri));
517 printf("memory allocation failed\n");
520 memset(ri, 0, sizeof(*ri));
521 ri->root_id = root_id;
523 if (name && name_len > 0) {
524 ri->name = malloc(name_len + 1);
526 fprintf(stderr, "memory allocation failed\n");
529 strncpy(ri->name, name, name_len);
530 ri->name[name_len] = 0;
533 ri->ref_tree = ref_tree;
537 ri->root_offset = root_offset;
544 if (!ri->ogen && root_offset)
545 ri->ogen = root_offset;
550 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
553 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
555 ret = root_tree_insert(root_lookup, ri);
557 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
563 void __free_root_info(struct root_info *ri)
577 void __free_all_subvolumn(struct root_lookup *root_tree)
579 struct root_info *entry;
582 n = rb_first(&root_tree->root);
584 entry = rb_entry(n, struct root_info, rb_node);
585 rb_erase(n, &root_tree->root);
586 __free_root_info(entry);
588 n = rb_first(&root_tree->root);
593 * for a given root_info, search through the root_lookup tree to construct
594 * the full path name to it.
596 * This can't be called until all the root_info->path fields are filled
597 * in by lookup_ino_path
599 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
602 char *full_path = NULL;
604 struct root_info *found;
607 * we go backwards from the root_info object and add pathnames
608 * from parent directories as we go.
614 int add_len = strlen(found->path);
616 /* room for / and for null */
617 tmp = malloc(add_len + 2 + len);
619 perror("malloc failed");
623 memcpy(tmp + add_len + 1, full_path, len);
625 memcpy(tmp, found->path, add_len);
626 tmp [add_len + len + 1] = '\0';
631 full_path = strdup(found->path);
635 next = found->ref_tree;
637 if (next == top_id) {
642 if (next == BTRFS_FS_TREE_OBJECTID) {
648 * if the ref_tree wasn't in our tree of roots, we're
651 found = root_tree_search(rl, next);
658 ri->full_path = full_path;
664 * for a single root_info, ask the kernel to give us a path name
665 * inside it's ref_root for the dir_id where it lives.
667 * This fills in root_info->path with the path to the directory and and
668 * appends this root's name.
670 static int lookup_ino_path(int fd, struct root_info *ri)
672 struct btrfs_ioctl_ino_lookup_args args;
678 memset(&args, 0, sizeof(args));
679 args.treeid = ri->ref_tree;
680 args.objectid = ri->dir_id;
682 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
685 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
686 (unsigned long long)ri->ref_tree,
693 * we're in a subdirectory of ref_tree, the kernel ioctl
694 * puts a / in there for us
696 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
698 perror("malloc failed");
701 strcpy(ri->path, args.name);
702 strcat(ri->path, ri->name);
704 /* we're at the root of ref_tree */
705 ri->path = strdup(ri->name);
707 perror("strdup failed");
714 /* finding the generation for a given path is a two step process.
715 * First we use the inode loookup routine to find out the root id
717 * Then we use the tree search ioctl to scan all the root items for a
718 * given root id and spit out the latest generation we can find
720 static u64 find_root_gen(int fd)
722 struct btrfs_ioctl_ino_lookup_args ino_args;
724 struct btrfs_ioctl_search_args args;
725 struct btrfs_ioctl_search_key *sk = &args.key;
726 struct btrfs_ioctl_search_header sh;
727 unsigned long off = 0;
732 memset(&ino_args, 0, sizeof(ino_args));
733 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
735 /* this ioctl fills in ino_args->treeid */
736 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
739 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
740 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
745 memset(&args, 0, sizeof(args));
750 * there may be more than one ROOT_ITEM key if there are
751 * snapshots pending deletion, we have to loop through
754 sk->min_objectid = ino_args.treeid;
755 sk->max_objectid = ino_args.treeid;
756 sk->max_type = BTRFS_ROOT_ITEM_KEY;
757 sk->min_type = BTRFS_ROOT_ITEM_KEY;
758 sk->max_offset = (u64)-1;
759 sk->max_transid = (u64)-1;
763 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
766 fprintf(stderr, "ERROR: can't perform the search - %s\n",
770 /* the ioctl returns the number of item it found in nr_items */
771 if (sk->nr_items == 0)
775 for (i = 0; i < sk->nr_items; i++) {
776 struct btrfs_root_item *item;
778 memcpy(&sh, args.buf + off, sizeof(sh));
780 item = (struct btrfs_root_item *)(args.buf + off);
783 sk->min_objectid = sh.objectid;
784 sk->min_type = sh.type;
785 sk->min_offset = sh.offset;
787 if (sh.objectid > ino_args.treeid)
790 if (sh.objectid == ino_args.treeid &&
791 sh.type == BTRFS_ROOT_ITEM_KEY) {
792 max_found = max(max_found,
793 btrfs_root_generation(item));
796 if (sk->min_offset < (u64)-1)
801 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
803 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
809 /* pass in a directory id and this will return
810 * the full path of the parent directory inside its
813 * It may return NULL if it is in the root, or an ERR_PTR if things
816 static char *__ino_resolve(int fd, u64 dirid)
818 struct btrfs_ioctl_ino_lookup_args args;
823 memset(&args, 0, sizeof(args));
824 args.objectid = dirid;
826 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
829 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
830 (unsigned long long)dirid, strerror(e) );
836 * we're in a subdirectory of ref_tree, the kernel ioctl
837 * puts a / in there for us
839 full = strdup(args.name);
841 perror("malloc failed");
842 return ERR_PTR(-ENOMEM);
845 /* we're at the root of ref_tree */
852 * simple string builder, returning a new string with both
855 char *build_name(char *dirid, char *name)
861 full = malloc(strlen(dirid) + strlen(name) + 1);
870 * given an inode number, this returns the full path name inside the subvolume
871 * to that file/directory. cache_dirid and cache_name are used to
872 * cache the results so we can avoid tree searches if a later call goes
873 * to the same directory or file name
875 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
883 struct btrfs_ioctl_search_args args;
884 struct btrfs_ioctl_search_key *sk = &args.key;
885 struct btrfs_ioctl_search_header *sh;
886 unsigned long off = 0;
890 memset(&args, 0, sizeof(args));
895 * step one, we search for the inode back ref. We just use the first
898 sk->min_objectid = ino;
899 sk->max_objectid = ino;
900 sk->max_type = BTRFS_INODE_REF_KEY;
901 sk->max_offset = (u64)-1;
902 sk->min_type = BTRFS_INODE_REF_KEY;
903 sk->max_transid = (u64)-1;
906 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
909 fprintf(stderr, "ERROR: can't perform the search - %s\n",
913 /* the ioctl returns the number of item it found in nr_items */
914 if (sk->nr_items == 0)
918 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
920 if (sh->type == BTRFS_INODE_REF_KEY) {
921 struct btrfs_inode_ref *ref;
924 ref = (struct btrfs_inode_ref *)(sh + 1);
925 namelen = btrfs_stack_inode_ref_name_len(ref);
927 name = (char *)(ref + 1);
928 name = strndup(name, namelen);
930 /* use our cached value */
931 if (dirid == *cache_dirid && *cache_name) {
932 dirname = *cache_name;
939 * the inode backref gives us the file name and the parent directory id.
940 * From here we use __ino_resolve to get the path to the parent
942 dirname = __ino_resolve(fd, dirid);
944 full = build_name(dirname, name);
945 if (*cache_name && dirname != *cache_name)
948 *cache_name = dirname;
949 *cache_dirid = dirid;
955 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
957 struct btrfs_ioctl_search_args args;
958 struct btrfs_ioctl_search_key *sk = &args.key;
959 struct btrfs_ioctl_search_header *sh;
963 memset(&args, 0, sizeof(args));
966 * search for a dir item with a name 'default' in the tree of
967 * tree roots, it should point us to a default root
971 /* don't worry about ancient format and request only one item */
974 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
975 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
976 sk->max_type = BTRFS_DIR_ITEM_KEY;
977 sk->min_type = BTRFS_DIR_ITEM_KEY;
978 sk->max_offset = (u64)-1;
979 sk->max_transid = (u64)-1;
981 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
985 /* the ioctl returns the number of items it found in nr_items */
986 if (sk->nr_items == 0)
989 sh = (struct btrfs_ioctl_search_header *)args.buf;
991 if (sh->type == BTRFS_DIR_ITEM_KEY) {
992 struct btrfs_dir_item *di;
996 di = (struct btrfs_dir_item *)(sh + 1);
997 name_len = btrfs_stack_dir_name_len(di);
998 name = (char *)(di + 1);
1000 if (!strncmp("default", name, name_len))
1001 found = btrfs_disk_key_objectid(&di->location);
1005 *default_id = found;
1009 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
1012 struct btrfs_ioctl_search_args args;
1013 struct btrfs_ioctl_search_key *sk = &args.key;
1014 struct btrfs_ioctl_search_header sh;
1015 struct btrfs_root_ref *ref;
1016 struct btrfs_root_item *ri;
1017 unsigned long off = 0;
1026 u8 uuid[BTRFS_UUID_SIZE];
1027 u8 puuid[BTRFS_UUID_SIZE];
1029 root_lookup_init(root_lookup);
1030 memset(&args, 0, sizeof(args));
1032 /* search in the tree of tree roots */
1036 * set the min and max to backref keys. The search will
1037 * only send back this type of key now.
1039 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1040 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1042 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1045 * set all the other params to the max, we'll take any objectid
1048 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1049 sk->max_offset = (u64)-1;
1050 sk->max_transid = (u64)-1;
1052 /* just a big number, doesn't matter much */
1053 sk->nr_items = 4096;
1056 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1059 /* the ioctl returns the number of item it found in nr_items */
1060 if (sk->nr_items == 0)
1066 * for each item, pull the key out of the header and then
1067 * read the root_ref item it contains
1069 for (i = 0; i < sk->nr_items; i++) {
1070 memcpy(&sh, args.buf + off, sizeof(sh));
1072 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1073 ref = (struct btrfs_root_ref *)(args.buf + off);
1074 name_len = btrfs_stack_root_ref_name_len(ref);
1075 name = (char *)(ref + 1);
1076 dir_id = btrfs_stack_root_ref_dirid(ref);
1078 add_root(root_lookup, sh.objectid, sh.offset,
1079 0, 0, dir_id, name, name_len, 0, 0, 0,
1081 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1082 ri = (struct btrfs_root_item *)(args.buf + off);
1083 gen = btrfs_root_generation(ri);
1084 flags = btrfs_root_flags(ri);
1086 sizeof(struct btrfs_root_item_v0)) {
1088 ogen = btrfs_root_otransid(ri);
1089 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1090 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1094 memset(uuid, 0, BTRFS_UUID_SIZE);
1095 memset(puuid, 0, BTRFS_UUID_SIZE);
1098 add_root(root_lookup, sh.objectid, 0,
1099 sh.offset, flags, 0, NULL, 0, ogen,
1100 gen, t, uuid, puuid);
1106 * record the mins in sk so we can make sure the
1107 * next search doesn't repeat this root
1109 sk->min_objectid = sh.objectid;
1110 sk->min_type = sh.type;
1111 sk->min_offset = sh.offset;
1113 sk->nr_items = 4096;
1115 if (!sk->min_offset) /* overflow */
1120 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1121 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1126 if (sk->min_objectid > sk->max_objectid)
1133 static int filter_by_rootid(struct root_info *ri, u64 data)
1135 return ri->root_id == data;
1138 static int filter_snapshot(struct root_info *ri, u64 data)
1140 return !!ri->root_offset;
1143 static int filter_flags(struct root_info *ri, u64 flags)
1145 return ri->flags & flags;
1148 static int filter_gen_more(struct root_info *ri, u64 data)
1150 return ri->gen >= data;
1153 static int filter_gen_less(struct root_info *ri, u64 data)
1155 return ri->gen <= data;
1158 static int filter_gen_equal(struct root_info *ri, u64 data)
1160 return ri->gen == data;
1163 static int filter_cgen_more(struct root_info *ri, u64 data)
1165 return ri->ogen >= data;
1168 static int filter_cgen_less(struct root_info *ri, u64 data)
1170 return ri->ogen <= data;
1173 static int filter_cgen_equal(struct root_info *ri, u64 data)
1175 return ri->ogen == data;
1178 static int filter_topid_equal(struct root_info *ri, u64 data)
1180 return ri->top_id == data;
1183 static int filter_full_path(struct root_info *ri, u64 data)
1185 if (ri->full_path && ri->top_id != data) {
1187 char p[] = "<FS_TREE>";
1188 int add_len = strlen(p);
1189 int len = strlen(ri->full_path);
1191 tmp = malloc(len + add_len + 2);
1193 fprintf(stderr, "memory allocation failed\n");
1196 memcpy(tmp + add_len + 1, ri->full_path, len);
1197 tmp[len + add_len + 1] = '\0';
1199 memcpy(tmp, p, add_len);
1200 free(ri->full_path);
1201 ri->full_path = tmp;
1206 static btrfs_list_filter_func all_filter_funcs[] = {
1207 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1208 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1209 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1210 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1211 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1212 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1213 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1214 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1215 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1216 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1217 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1220 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1222 struct btrfs_list_filter_set *set;
1225 size = sizeof(struct btrfs_list_filter_set) +
1226 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1229 fprintf(stderr, "memory allocation failed\n");
1233 memset(set, 0, size);
1234 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1239 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1244 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1245 enum btrfs_list_filter_enum filter, u64 data)
1247 struct btrfs_list_filter_set *set = *filter_set;
1251 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1252 BUG_ON(set->nfilters > set->total);
1254 if (set->nfilters == set->total) {
1255 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1256 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1257 set = realloc(set, size);
1259 fprintf(stderr, "memory allocation failed\n");
1263 memset(&set->filters[set->total], 0,
1264 BTRFS_LIST_NFILTERS_INCREASE *
1265 sizeof(struct btrfs_list_filter));
1266 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1270 BUG_ON(set->filters[set->nfilters].filter_func);
1272 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1273 set->filters[set->nfilters].data = data;
1278 static int filter_root(struct root_info *ri,
1279 struct btrfs_list_filter_set *set)
1283 if (!set || !set->nfilters)
1286 for (i = 0; i < set->nfilters; i++) {
1287 if (!set->filters[i].filter_func)
1289 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1296 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1297 struct root_lookup *sort_tree,
1298 struct btrfs_list_filter_set *filter_set,
1299 struct btrfs_list_comparer_set *comp_set,
1303 struct root_info *entry;
1306 root_lookup_init(sort_tree);
1308 n = rb_last(&all_subvols->root);
1310 entry = rb_entry(n, struct root_info, rb_node);
1312 resolve_root(all_subvols, entry, top_id);
1313 ret = filter_root(entry, filter_set);
1315 sort_tree_insert(sort_tree, entry, comp_set);
1320 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1324 n = rb_first(&root_lookup->root);
1326 struct root_info *entry;
1328 entry = rb_entry(n, struct root_info, rb_node);
1329 ret = lookup_ino_path(fd, entry);
1338 static void print_subvolume_column(struct root_info *subv,
1339 enum btrfs_list_column_enum column)
1344 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1347 case BTRFS_LIST_OBJECTID:
1348 printf("%llu", subv->root_id);
1350 case BTRFS_LIST_GENERATION:
1351 printf("%llu", subv->gen);
1353 case BTRFS_LIST_OGENERATION:
1354 printf("%llu", subv->ogen);
1356 case BTRFS_LIST_PARENT:
1357 printf("%llu", subv->ref_tree);
1359 case BTRFS_LIST_TOP_LEVEL:
1360 printf("%llu", subv->top_id);
1362 case BTRFS_LIST_OTIME:
1364 strftime(tstr, 256, "%Y-%m-%d %X",
1365 localtime(&subv->otime));
1370 case BTRFS_LIST_UUID:
1371 if (uuid_is_null(subv->uuid))
1372 strcpy(uuidparse, "-");
1374 uuid_unparse(subv->uuid, uuidparse);
1375 printf("%s", uuidparse);
1377 case BTRFS_LIST_PUUID:
1378 if (uuid_is_null(subv->puuid))
1379 strcpy(uuidparse, "-");
1381 uuid_unparse(subv->puuid, uuidparse);
1382 printf("%s", uuidparse);
1384 case BTRFS_LIST_PATH:
1385 BUG_ON(!subv->full_path);
1386 printf("%s", subv->full_path);
1393 static void print_single_volume_info_table(struct root_info *subv)
1397 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1398 if (!btrfs_list_columns[i].need_print)
1401 print_subvolume_column(subv, i);
1403 if (i != BTRFS_LIST_PATH)
1406 if (i == BTRFS_LIST_TOP_LEVEL)
1412 static void print_single_volume_info_default(struct root_info *subv)
1416 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1417 if (!btrfs_list_columns[i].need_print)
1420 printf("%s ", btrfs_list_columns[i].name);
1421 print_subvolume_column(subv, i);
1423 if (i != BTRFS_LIST_PATH)
1429 static void print_all_volume_info_tab_head()
1435 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1436 if (btrfs_list_columns[i].need_print)
1437 printf("%s\t", btrfs_list_columns[i].name);
1439 if (i == BTRFS_LIST_ALL-1)
1443 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1444 memset(barrier, 0, sizeof(barrier));
1446 if (btrfs_list_columns[i].need_print) {
1447 len = strlen(btrfs_list_columns[i].name);
1449 strcat(barrier, "-");
1451 printf("%s\t", barrier);
1453 if (i == BTRFS_LIST_ALL-1)
1458 static void print_all_volume_info(struct root_lookup *sorted_tree,
1462 struct root_info *entry;
1465 print_all_volume_info_tab_head();
1467 n = rb_first(&sorted_tree->root);
1469 entry = rb_entry(n, struct root_info, sort_node);
1471 print_single_volume_info_table(entry);
1473 print_single_volume_info_default(entry);
1478 int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1482 ret = __list_subvol_search(fd, root_lookup);
1484 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1490 * now we have an rbtree full of root_info objects, but we need to fill
1491 * in their path names within the subvol that is referencing each one.
1493 ret = __list_subvol_fill_paths(fd, root_lookup);
1497 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1498 struct btrfs_list_comparer_set *comp_set,
1499 int is_tab_result, int full_path)
1501 struct root_lookup root_lookup;
1502 struct root_lookup root_sort;
1504 u64 top_id = (full_path ? 0 : btrfs_list_get_path_rootid(fd));
1506 ret = btrfs_list_subvols(fd, &root_lookup);
1509 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1512 print_all_volume_info(&root_sort, is_tab_result);
1513 __free_all_subvolumn(&root_lookup);
1518 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1519 struct btrfs_file_extent_item *item,
1520 u64 found_gen, u64 *cache_dirid,
1521 char **cache_dir_name, u64 *cache_ino,
1522 char **cache_full_name)
1526 u64 disk_offset = 0;
1532 if (sh->objectid == *cache_ino) {
1533 name = *cache_full_name;
1534 } else if (*cache_full_name) {
1535 free(*cache_full_name);
1536 *cache_full_name = NULL;
1539 name = ino_resolve(fd, sh->objectid, cache_dirid,
1541 *cache_full_name = name;
1542 *cache_ino = sh->objectid;
1547 type = btrfs_stack_file_extent_type(item);
1548 compressed = btrfs_stack_file_extent_compression(item);
1550 if (type == BTRFS_FILE_EXTENT_REG ||
1551 type == BTRFS_FILE_EXTENT_PREALLOC) {
1552 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1553 disk_offset = btrfs_stack_file_extent_offset(item);
1554 len = btrfs_stack_file_extent_num_bytes(item);
1555 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1558 len = btrfs_stack_file_extent_ram_bytes(item);
1560 printf("unhandled extent type %d for inode %llu "
1561 "file offset %llu gen %llu\n",
1563 (unsigned long long)sh->objectid,
1564 (unsigned long long)sh->offset,
1565 (unsigned long long)found_gen);
1569 printf("inode %llu file offset %llu len %llu disk start %llu "
1570 "offset %llu gen %llu flags ",
1571 (unsigned long long)sh->objectid,
1572 (unsigned long long)sh->offset,
1573 (unsigned long long)len,
1574 (unsigned long long)disk_start,
1575 (unsigned long long)disk_offset,
1576 (unsigned long long)found_gen);
1582 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1583 printf("%sPREALLOC", flags ? "|" : "");
1586 if (type == BTRFS_FILE_EXTENT_INLINE) {
1587 printf("%sINLINE", flags ? "|" : "");
1593 printf(" %s\n", name);
1597 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1600 struct btrfs_ioctl_search_args args;
1601 struct btrfs_ioctl_search_key *sk = &args.key;
1602 struct btrfs_ioctl_search_header sh;
1603 struct btrfs_file_extent_item *item;
1604 unsigned long off = 0;
1609 u64 cache_dirid = 0;
1611 char *cache_dir_name = NULL;
1612 char *cache_full_name = NULL;
1613 struct btrfs_file_extent_item backup;
1615 memset(&backup, 0, sizeof(backup));
1616 memset(&args, 0, sizeof(args));
1618 sk->tree_id = root_id;
1621 * set all the other params to the max, we'll take any objectid
1624 sk->max_objectid = (u64)-1;
1625 sk->max_offset = (u64)-1;
1626 sk->max_transid = (u64)-1;
1627 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1628 sk->min_transid = oldest_gen;
1629 /* just a big number, doesn't matter much */
1630 sk->nr_items = 4096;
1632 max_found = find_root_gen(fd);
1634 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1637 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1641 /* the ioctl returns the number of item it found in nr_items */
1642 if (sk->nr_items == 0)
1648 * for each item, pull the key out of the header and then
1649 * read the root_ref item it contains
1651 for (i = 0; i < sk->nr_items; i++) {
1652 memcpy(&sh, args.buf + off, sizeof(sh));
1656 * just in case the item was too big, pass something other
1662 item = (struct btrfs_file_extent_item *)(args.buf +
1664 found_gen = btrfs_stack_file_extent_generation(item);
1665 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1666 found_gen >= oldest_gen) {
1667 print_one_extent(fd, &sh, item, found_gen,
1668 &cache_dirid, &cache_dir_name,
1669 &cache_ino, &cache_full_name);
1674 * record the mins in sk so we can make sure the
1675 * next search doesn't repeat this root
1677 sk->min_objectid = sh.objectid;
1678 sk->min_offset = sh.offset;
1679 sk->min_type = sh.type;
1681 sk->nr_items = 4096;
1682 if (sk->min_offset < (u64)-1)
1684 else if (sk->min_objectid < (u64)-1) {
1691 free(cache_dir_name);
1692 free(cache_full_name);
1693 printf("transid marker was %llu\n", (unsigned long long)max_found);
1697 char *btrfs_list_path_for_root(int fd, u64 root)
1699 struct root_lookup root_lookup;
1701 char *ret_path = NULL;
1703 u64 top_id = btrfs_list_get_path_rootid(fd);
1705 ret = __list_subvol_search(fd, &root_lookup);
1707 return ERR_PTR(ret);
1709 ret = __list_subvol_fill_paths(fd, &root_lookup);
1711 return ERR_PTR(ret);
1713 n = rb_last(&root_lookup.root);
1715 struct root_info *entry;
1717 entry = rb_entry(n, struct root_info, rb_node);
1718 resolve_root(&root_lookup, entry, top_id);
1719 if (entry->root_id == root) {
1720 ret_path = entry->full_path;
1721 entry->full_path = NULL;
1726 __free_all_subvolumn(&root_lookup);
1731 int btrfs_list_parse_sort_string(char *optarg,
1732 struct btrfs_list_comparer_set **comps)
1740 while ((p = strtok(optarg, ",")) != NULL) {
1742 ptr_argv = all_sort_items;
1745 if (strcmp(*ptr_argv, p) == 0) {
1750 if (strcmp(*ptr_argv, p) == 0) {
1767 } else if (*p == '-') {
1773 what_to_sort = btrfs_list_get_sort_item(p);
1774 btrfs_list_setup_comparer(comps, what_to_sort, order);
1783 * This function is used to parse the argument of filter condition.
1785 * type is the filter object.
1787 int btrfs_list_parse_filter_string(char *optarg,
1788 struct btrfs_list_filter_set **filters,
1789 enum btrfs_list_filter_enum type)
1793 char *ptr_parse_end = NULL;
1794 char *ptr_optarg_end = optarg + strlen(optarg);
1796 switch (*(optarg++)) {
1798 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1800 if (ptr_parse_end != ptr_optarg_end)
1803 btrfs_list_setup_filter(filters, type, arg);
1806 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1808 if (ptr_parse_end != ptr_optarg_end)
1811 btrfs_list_setup_filter(filters, type, arg);
1815 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1817 if (ptr_parse_end != ptr_optarg_end)
1819 btrfs_list_setup_filter(filters, type, arg);
1826 u64 btrfs_list_get_path_rootid(int fd)
1829 struct btrfs_ioctl_ino_lookup_args args;
1831 memset(&args, 0, sizeof(args));
1832 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1834 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1837 "ERROR: can't perform the search -%s\n",