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];
84 /* path from the subvol we live in to this root, including the
85 * root's name. This is null until we do the extra lookup ioctl.
89 /* the name of this root in the directory it lives in */
99 } btrfs_list_columns[] = {
107 .column_name = "Gen",
112 .column_name = "CGen",
117 .column_name = "Parent",
122 .column_name = "Top Level",
127 .column_name = "OTime",
132 .column_name = "UUID",
137 .column_name = "Path",
147 static btrfs_list_filter_func all_filter_funcs[];
148 static btrfs_list_comp_func all_comp_funcs[];
150 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
154 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
156 if (column < BTRFS_LIST_ALL) {
157 btrfs_list_columns[column].need_print = 1;
161 for (i = 0; i < BTRFS_LIST_ALL; i++)
162 btrfs_list_columns[i].need_print = 1;
165 static void root_lookup_init(struct root_lookup *tree)
167 tree->root.rb_node = NULL;
170 static int comp_entry_with_rootid(struct root_info *entry1,
171 struct root_info *entry2,
176 if (entry1->root_id > entry2->root_id)
178 else if (entry1->root_id < entry2->root_id)
183 return is_descending ? -ret : ret;
186 static int comp_entry_with_gen(struct root_info *entry1,
187 struct root_info *entry2,
192 if (entry1->gen > entry2->gen)
194 else if (entry1->gen < entry2->gen)
199 return is_descending ? -ret : ret;
202 static int comp_entry_with_ogen(struct root_info *entry1,
203 struct root_info *entry2,
208 if (entry1->ogen > entry2->ogen)
210 else if (entry1->ogen < entry2->ogen)
215 return is_descending ? -ret : ret;
218 static btrfs_list_comp_func all_comp_funcs[] = {
219 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
220 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
221 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
224 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
226 struct btrfs_list_comparer_set *set;
229 size = sizeof(struct btrfs_list_comparer_set) +
230 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
233 fprintf(stderr, "memory allocation failed\n");
237 memset(set, 0, size);
238 set->total = BTRFS_LIST_NCOMPS_INCREASE;
243 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
248 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
249 enum btrfs_list_comp_enum comparer,
252 struct btrfs_list_comparer_set *set = *comp_set;
256 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
257 BUG_ON(set->ncomps > set->total);
259 if (set->ncomps == set->total) {
260 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
261 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
262 set = realloc(set, size);
264 fprintf(stderr, "memory allocation failed\n");
268 memset(&set->comps[set->total], 0,
269 BTRFS_LIST_NCOMPS_INCREASE *
270 sizeof(struct btrfs_list_comparer));
271 set->total += BTRFS_LIST_NCOMPS_INCREASE;
275 BUG_ON(set->comps[set->ncomps].comp_func);
277 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
278 set->comps[set->ncomps].is_descending = is_descending;
283 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
284 struct btrfs_list_comparer_set *set)
286 int rootid_compared = 0;
289 if (!set || !set->ncomps)
292 for (i = 0; i < set->ncomps; i++) {
293 if (!set->comps[i].comp_func)
296 ret = set->comps[i].comp_func(entry1, entry2,
297 set->comps[i].is_descending);
301 if (set->comps[i].comp_func == comp_entry_with_rootid)
305 if (!rootid_compared) {
307 ret = comp_entry_with_rootid(entry1, entry2, 0);
313 static int sort_tree_insert(struct root_lookup *sort_tree,
314 struct root_info *ins,
315 struct btrfs_list_comparer_set *comp_set)
317 struct rb_node **p = &sort_tree->root.rb_node;
318 struct rb_node *parent = NULL;
319 struct root_info *curr;
324 curr = rb_entry(parent, struct root_info, sort_node);
326 ret = sort_comp(ins, curr, comp_set);
335 rb_link_node(&ins->sort_node, parent, p);
336 rb_insert_color(&ins->sort_node, &sort_tree->root);
341 * insert a new root into the tree. returns the existing root entry
342 * if one is already there. Both root_id and ref_tree are used
345 static int root_tree_insert(struct root_lookup *root_tree,
346 struct root_info *ins)
348 struct rb_node **p = &root_tree->root.rb_node;
349 struct rb_node * parent = NULL;
350 struct root_info *curr;
355 curr = rb_entry(parent, struct root_info, rb_node);
357 ret = comp_entry_with_rootid(ins, curr, 0);
366 rb_link_node(&ins->rb_node, parent, p);
367 rb_insert_color(&ins->rb_node, &root_tree->root);
372 * find a given root id in the tree. We return the smallest one,
373 * rb_next can be used to move forward looking for more if required
375 static struct root_info *root_tree_search(struct root_lookup *root_tree,
378 struct rb_node *n = root_tree->root.rb_node;
379 struct root_info *entry;
380 struct root_info tmp;
383 tmp.root_id = root_id;
386 entry = rb_entry(n, struct root_info, rb_node);
388 ret = comp_entry_with_rootid(&tmp, entry, 0);
399 static int update_root(struct root_lookup *root_lookup,
400 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
401 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
402 time_t ot, void *uuid)
404 struct root_info *ri;
406 ri = root_tree_search(root_lookup, root_id);
407 if (!ri || ri->root_id != root_id)
409 if (name && name_len > 0) {
413 ri->name = malloc(name_len + 1);
415 fprintf(stderr, "memory allocation failed\n");
418 strncpy(ri->name, name, name_len);
419 ri->name[name_len] = 0;
422 ri->ref_tree = ref_tree;
424 ri->root_offset = root_offset;
433 if (!ri->ogen && root_offset)
434 ri->ogen = root_offset;
438 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
444 * add_root - update the existed root, or allocate a new root and insert it
445 * into the lookup tree.
446 * root_id: object id of the root
447 * ref_tree: object id of the referring root.
448 * root_offset: offset value of the root'key
449 * dir_id: inode id of the directory in ref_tree where this root can be found.
450 * name: the name of root_id in that directory
451 * name_len: the length of name
452 * ogen: the original generation of the root
453 * gen: the current generation of the root
454 * ot: the original time(create time) of the root
455 * uuid: uuid of the root
457 static int add_root(struct root_lookup *root_lookup,
458 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
459 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
460 time_t ot, void *uuid)
462 struct root_info *ri;
465 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
466 dir_id, name, name_len, ogen, gen, ot, uuid);
470 ri = malloc(sizeof(*ri));
472 printf("memory allocation failed\n");
475 memset(ri, 0, sizeof(*ri));
476 ri->root_id = root_id;
478 if (name && name_len > 0) {
479 ri->name = malloc(name_len + 1);
481 fprintf(stderr, "memory allocation failed\n");
484 strncpy(ri->name, name, name_len);
485 ri->name[name_len] = 0;
488 ri->ref_tree = ref_tree;
492 ri->root_offset = root_offset;
499 if (!ri->ogen && root_offset)
500 ri->ogen = root_offset;
505 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
507 ret = root_tree_insert(root_lookup, ri);
509 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
515 void __free_root_info(struct root_info *ri)
529 void __free_all_subvolumn(struct root_lookup *root_tree)
531 struct root_info *entry;
534 n = rb_first(&root_tree->root);
536 entry = rb_entry(n, struct root_info, rb_node);
537 rb_erase(n, &root_tree->root);
538 __free_root_info(entry);
540 n = rb_first(&root_tree->root);
545 * for a given root_info, search through the root_lookup tree to construct
546 * the full path name to it.
548 * This can't be called until all the root_info->path fields are filled
549 * in by lookup_ino_path
551 static int resolve_root(struct root_lookup *rl, struct root_info *ri)
553 char *full_path = NULL;
555 struct root_info *found;
558 * we go backwards from the root_info object and add pathnames
559 * from parent directories as we go.
565 int add_len = strlen(found->path);
567 /* room for / and for null */
568 tmp = malloc(add_len + 2 + len);
570 perror("malloc failed");
574 memcpy(tmp + add_len + 1, full_path, len);
576 memcpy(tmp, found->path, add_len);
577 tmp [add_len + len + 1] = '\0';
582 full_path = strdup(found->path);
586 next = found->ref_tree;
588 /* if the ref_tree refers to ourselves, we're at the top */
589 if (next == found->root_id) {
595 * if the ref_tree wasn't in our tree of roots, we're
598 found = root_tree_search(rl, next);
605 ri->full_path = full_path;
611 * for a single root_info, ask the kernel to give us a path name
612 * inside it's ref_root for the dir_id where it lives.
614 * This fills in root_info->path with the path to the directory and and
615 * appends this root's name.
617 static int lookup_ino_path(int fd, struct root_info *ri)
619 struct btrfs_ioctl_ino_lookup_args args;
625 memset(&args, 0, sizeof(args));
626 args.treeid = ri->ref_tree;
627 args.objectid = ri->dir_id;
629 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
632 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
633 (unsigned long long)ri->ref_tree,
640 * we're in a subdirectory of ref_tree, the kernel ioctl
641 * puts a / in there for us
643 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
645 perror("malloc failed");
648 strcpy(ri->path, args.name);
649 strcat(ri->path, ri->name);
651 /* we're at the root of ref_tree */
652 ri->path = strdup(ri->name);
654 perror("strdup failed");
661 /* finding the generation for a given path is a two step process.
662 * First we use the inode loookup routine to find out the root id
664 * Then we use the tree search ioctl to scan all the root items for a
665 * given root id and spit out the latest generation we can find
667 static u64 find_root_gen(int fd)
669 struct btrfs_ioctl_ino_lookup_args ino_args;
671 struct btrfs_ioctl_search_args args;
672 struct btrfs_ioctl_search_key *sk = &args.key;
673 struct btrfs_ioctl_search_header *sh;
674 unsigned long off = 0;
679 memset(&ino_args, 0, sizeof(ino_args));
680 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
682 /* this ioctl fills in ino_args->treeid */
683 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
686 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
687 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
692 memset(&args, 0, sizeof(args));
697 * there may be more than one ROOT_ITEM key if there are
698 * snapshots pending deletion, we have to loop through
701 sk->min_objectid = ino_args.treeid;
702 sk->max_objectid = ino_args.treeid;
703 sk->max_type = BTRFS_ROOT_ITEM_KEY;
704 sk->min_type = BTRFS_ROOT_ITEM_KEY;
705 sk->max_offset = (u64)-1;
706 sk->max_transid = (u64)-1;
710 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
713 fprintf(stderr, "ERROR: can't perform the search - %s\n",
717 /* the ioctl returns the number of item it found in nr_items */
718 if (sk->nr_items == 0)
722 for (i = 0; i < sk->nr_items; i++) {
723 struct btrfs_root_item *item;
724 sh = (struct btrfs_ioctl_search_header *)(args.buf +
728 item = (struct btrfs_root_item *)(args.buf + off);
731 sk->min_objectid = sh->objectid;
732 sk->min_type = sh->type;
733 sk->min_offset = sh->offset;
735 if (sh->objectid > ino_args.treeid)
738 if (sh->objectid == ino_args.treeid &&
739 sh->type == BTRFS_ROOT_ITEM_KEY) {
740 max_found = max(max_found,
741 btrfs_root_generation(item));
744 if (sk->min_offset < (u64)-1)
749 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
751 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
757 /* pass in a directory id and this will return
758 * the full path of the parent directory inside its
761 * It may return NULL if it is in the root, or an ERR_PTR if things
764 static char *__ino_resolve(int fd, u64 dirid)
766 struct btrfs_ioctl_ino_lookup_args args;
771 memset(&args, 0, sizeof(args));
772 args.objectid = dirid;
774 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
777 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
778 (unsigned long long)dirid, strerror(e) );
784 * we're in a subdirectory of ref_tree, the kernel ioctl
785 * puts a / in there for us
787 full = strdup(args.name);
789 perror("malloc failed");
790 return ERR_PTR(-ENOMEM);
793 /* we're at the root of ref_tree */
800 * simple string builder, returning a new string with both
803 char *build_name(char *dirid, char *name)
809 full = malloc(strlen(dirid) + strlen(name) + 1);
818 * given an inode number, this returns the full path name inside the subvolume
819 * to that file/directory. cache_dirid and cache_name are used to
820 * cache the results so we can avoid tree searches if a later call goes
821 * to the same directory or file name
823 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
831 struct btrfs_ioctl_search_args args;
832 struct btrfs_ioctl_search_key *sk = &args.key;
833 struct btrfs_ioctl_search_header *sh;
834 unsigned long off = 0;
838 memset(&args, 0, sizeof(args));
843 * step one, we search for the inode back ref. We just use the first
846 sk->min_objectid = ino;
847 sk->max_objectid = ino;
848 sk->max_type = BTRFS_INODE_REF_KEY;
849 sk->max_offset = (u64)-1;
850 sk->min_type = BTRFS_INODE_REF_KEY;
851 sk->max_transid = (u64)-1;
854 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
857 fprintf(stderr, "ERROR: can't perform the search - %s\n",
861 /* the ioctl returns the number of item it found in nr_items */
862 if (sk->nr_items == 0)
866 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
868 if (sh->type == BTRFS_INODE_REF_KEY) {
869 struct btrfs_inode_ref *ref;
872 ref = (struct btrfs_inode_ref *)(sh + 1);
873 namelen = btrfs_stack_inode_ref_name_len(ref);
875 name = (char *)(ref + 1);
876 name = strndup(name, namelen);
878 /* use our cached value */
879 if (dirid == *cache_dirid && *cache_name) {
880 dirname = *cache_name;
887 * the inode backref gives us the file name and the parent directory id.
888 * From here we use __ino_resolve to get the path to the parent
890 dirname = __ino_resolve(fd, dirid);
892 full = build_name(dirname, name);
893 if (*cache_name && dirname != *cache_name)
896 *cache_name = dirname;
897 *cache_dirid = dirid;
903 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
905 struct btrfs_ioctl_search_args args;
906 struct btrfs_ioctl_search_key *sk = &args.key;
907 struct btrfs_ioctl_search_header *sh;
911 memset(&args, 0, sizeof(args));
914 * search for a dir item with a name 'default' in the tree of
915 * tree roots, it should point us to a default root
919 /* don't worry about ancient format and request only one item */
922 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
923 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
924 sk->max_type = BTRFS_DIR_ITEM_KEY;
925 sk->min_type = BTRFS_DIR_ITEM_KEY;
926 sk->max_offset = (u64)-1;
927 sk->max_transid = (u64)-1;
929 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
933 /* the ioctl returns the number of items it found in nr_items */
934 if (sk->nr_items == 0)
937 sh = (struct btrfs_ioctl_search_header *)args.buf;
939 if (sh->type == BTRFS_DIR_ITEM_KEY) {
940 struct btrfs_dir_item *di;
944 di = (struct btrfs_dir_item *)(sh + 1);
945 name_len = btrfs_stack_dir_name_len(di);
946 name = (char *)(di + 1);
948 if (!strncmp("default", name, name_len))
949 found = btrfs_disk_key_objectid(&di->location);
957 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
960 struct btrfs_ioctl_search_args args;
961 struct btrfs_ioctl_search_key *sk = &args.key;
962 struct btrfs_ioctl_search_header *sh;
963 struct btrfs_root_ref *ref;
964 struct btrfs_root_item *ri;
965 unsigned long off = 0;
974 u8 uuid[BTRFS_UUID_SIZE];
976 root_lookup_init(root_lookup);
977 memset(&args, 0, sizeof(args));
979 /* search in the tree of tree roots */
983 * set the min and max to backref keys. The search will
984 * only send back this type of key now.
986 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
987 sk->min_type = BTRFS_ROOT_ITEM_KEY;
989 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
992 * set all the other params to the max, we'll take any objectid
995 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
996 sk->max_offset = (u64)-1;
997 sk->max_transid = (u64)-1;
999 /* just a big number, doesn't matter much */
1000 sk->nr_items = 4096;
1003 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1006 /* the ioctl returns the number of item it found in nr_items */
1007 if (sk->nr_items == 0)
1013 * for each item, pull the key out of the header and then
1014 * read the root_ref item it contains
1016 for (i = 0; i < sk->nr_items; i++) {
1017 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1020 if (sh->type == BTRFS_ROOT_BACKREF_KEY) {
1021 ref = (struct btrfs_root_ref *)(args.buf + off);
1022 name_len = btrfs_stack_root_ref_name_len(ref);
1023 name = (char *)(ref + 1);
1024 dir_id = btrfs_stack_root_ref_dirid(ref);
1026 add_root(root_lookup, sh->objectid, sh->offset,
1027 0, 0, dir_id, name, name_len, 0, 0, 0,
1029 } else if (sh->type == BTRFS_ROOT_ITEM_KEY) {
1030 ri = (struct btrfs_root_item *)(args.buf + off);
1031 gen = btrfs_root_generation(ri);
1032 flags = btrfs_root_flags(ri);
1034 sizeof(struct btrfs_root_item_v0)) {
1036 ogen = btrfs_root_otransid(ri);
1037 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1041 memset(uuid, 0, BTRFS_UUID_SIZE);
1044 add_root(root_lookup, sh->objectid, 0,
1045 sh->offset, flags, 0, NULL, 0, ogen,
1052 * record the mins in sk so we can make sure the
1053 * next search doesn't repeat this root
1055 sk->min_objectid = sh->objectid;
1056 sk->min_type = sh->type;
1057 sk->min_offset = sh->offset;
1059 sk->nr_items = 4096;
1061 if (!sk->min_offset) /* overflow */
1066 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1067 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1072 if (sk->min_objectid > sk->max_objectid)
1079 static int filter_by_rootid(struct root_info *ri, u64 data)
1081 return ri->root_id == data;
1084 static int filter_snapshot(struct root_info *ri, u64 data)
1086 return !!ri->root_offset;
1089 static int filter_flags(struct root_info *ri, u64 flags)
1091 return ri->flags & flags;
1094 static btrfs_list_filter_func all_filter_funcs[] = {
1095 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1096 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1097 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1100 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1102 struct btrfs_list_filter_set *set;
1105 size = sizeof(struct btrfs_list_filter_set) +
1106 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1109 fprintf(stderr, "memory allocation failed\n");
1113 memset(set, 0, size);
1114 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1119 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1124 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1125 enum btrfs_list_filter_enum filter, u64 data)
1127 struct btrfs_list_filter_set *set = *filter_set;
1131 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1132 BUG_ON(set->nfilters > set->total);
1134 if (set->nfilters == set->total) {
1135 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1136 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1137 set = realloc(set, size);
1139 fprintf(stderr, "memory allocation failed\n");
1143 memset(&set->filters[set->total], 0,
1144 BTRFS_LIST_NFILTERS_INCREASE *
1145 sizeof(struct btrfs_list_filter));
1146 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1150 BUG_ON(set->filters[set->nfilters].filter_func);
1152 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1153 set->filters[set->nfilters].data = data;
1158 static int filter_root(struct root_info *ri,
1159 struct btrfs_list_filter_set *set)
1163 if (!set || !set->nfilters)
1166 for (i = 0; i < set->nfilters; i++) {
1167 if (!set->filters[i].filter_func)
1169 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1176 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1177 struct root_lookup *sort_tree,
1178 struct btrfs_list_filter_set *filter_set,
1179 struct btrfs_list_comparer_set *comp_set)
1182 struct root_info *entry;
1185 root_lookup_init(sort_tree);
1187 n = rb_last(&all_subvols->root);
1189 entry = rb_entry(n, struct root_info, rb_node);
1191 resolve_root(all_subvols, entry);
1192 ret = filter_root(entry, filter_set);
1194 sort_tree_insert(sort_tree, entry, comp_set);
1199 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1203 n = rb_first(&root_lookup->root);
1205 struct root_info *entry;
1207 entry = rb_entry(n, struct root_info, rb_node);
1208 ret = lookup_ino_path(fd, entry);
1217 static void print_subvolume_column(struct root_info *subv,
1218 enum btrfs_list_column_enum column)
1223 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1226 case BTRFS_LIST_OBJECTID:
1227 printf("%llu", subv->root_id);
1229 case BTRFS_LIST_GENERATION:
1230 printf("%llu", subv->gen);
1232 case BTRFS_LIST_OGENERATION:
1233 printf("%llu", subv->ogen);
1235 case BTRFS_LIST_PARENT:
1236 printf("%llu", subv->ref_tree);
1238 case BTRFS_LIST_TOP_LEVEL:
1239 printf("%llu", subv->top_id);
1241 case BTRFS_LIST_OTIME:
1243 strftime(tstr, 256, "%Y-%m-%d %X",
1244 localtime(&subv->otime));
1249 case BTRFS_LIST_UUID:
1250 if (uuid_is_null(subv->uuid))
1251 strcpy(uuidparse, "-");
1253 uuid_unparse(subv->uuid, uuidparse);
1254 printf("%s", uuidparse);
1256 case BTRFS_LIST_PATH:
1257 BUG_ON(!subv->full_path);
1258 printf("%s", subv->full_path);
1265 static void print_single_volume_info_default(struct root_info *subv)
1269 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1270 if (!btrfs_list_columns[i].need_print)
1273 printf("%s ", btrfs_list_columns[i].name);
1274 print_subvolume_column(subv, i);
1276 if (i != BTRFS_LIST_PATH)
1282 static void print_all_volume_info_default(struct root_lookup *sorted_tree)
1285 struct root_info *entry;
1287 n = rb_first(&sorted_tree->root);
1289 entry = rb_entry(n, struct root_info, sort_node);
1290 print_single_volume_info_default(entry);
1295 int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set,
1296 struct btrfs_list_comparer_set *comp_set)
1298 struct root_lookup root_lookup;
1299 struct root_lookup root_sort;
1302 ret = __list_subvol_search(fd, &root_lookup);
1304 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1310 * now we have an rbtree full of root_info objects, but we need to fill
1311 * in their path names within the subvol that is referencing each one.
1313 ret = __list_subvol_fill_paths(fd, &root_lookup);
1317 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1320 print_all_volume_info_default(&root_sort);
1321 __free_all_subvolumn(&root_lookup);
1325 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1326 struct btrfs_file_extent_item *item,
1327 u64 found_gen, u64 *cache_dirid,
1328 char **cache_dir_name, u64 *cache_ino,
1329 char **cache_full_name)
1333 u64 disk_offset = 0;
1339 if (sh->objectid == *cache_ino) {
1340 name = *cache_full_name;
1341 } else if (*cache_full_name) {
1342 free(*cache_full_name);
1343 *cache_full_name = NULL;
1346 name = ino_resolve(fd, sh->objectid, cache_dirid,
1348 *cache_full_name = name;
1349 *cache_ino = sh->objectid;
1354 type = btrfs_stack_file_extent_type(item);
1355 compressed = btrfs_stack_file_extent_compression(item);
1357 if (type == BTRFS_FILE_EXTENT_REG ||
1358 type == BTRFS_FILE_EXTENT_PREALLOC) {
1359 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1360 disk_offset = btrfs_stack_file_extent_offset(item);
1361 len = btrfs_stack_file_extent_num_bytes(item);
1362 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1365 len = btrfs_stack_file_extent_ram_bytes(item);
1367 printf("unhandled extent type %d for inode %llu "
1368 "file offset %llu gen %llu\n",
1370 (unsigned long long)sh->objectid,
1371 (unsigned long long)sh->offset,
1372 (unsigned long long)found_gen);
1376 printf("inode %llu file offset %llu len %llu disk start %llu "
1377 "offset %llu gen %llu flags ",
1378 (unsigned long long)sh->objectid,
1379 (unsigned long long)sh->offset,
1380 (unsigned long long)len,
1381 (unsigned long long)disk_start,
1382 (unsigned long long)disk_offset,
1383 (unsigned long long)found_gen);
1389 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1390 printf("%sPREALLOC", flags ? "|" : "");
1393 if (type == BTRFS_FILE_EXTENT_INLINE) {
1394 printf("%sINLINE", flags ? "|" : "");
1400 printf(" %s\n", name);
1404 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1407 struct btrfs_ioctl_search_args args;
1408 struct btrfs_ioctl_search_key *sk = &args.key;
1409 struct btrfs_ioctl_search_header *sh;
1410 struct btrfs_file_extent_item *item;
1411 unsigned long off = 0;
1416 u64 cache_dirid = 0;
1418 char *cache_dir_name = NULL;
1419 char *cache_full_name = NULL;
1420 struct btrfs_file_extent_item backup;
1422 memset(&backup, 0, sizeof(backup));
1423 memset(&args, 0, sizeof(args));
1425 sk->tree_id = root_id;
1428 * set all the other params to the max, we'll take any objectid
1431 sk->max_objectid = (u64)-1;
1432 sk->max_offset = (u64)-1;
1433 sk->max_transid = (u64)-1;
1434 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1435 sk->min_transid = oldest_gen;
1436 /* just a big number, doesn't matter much */
1437 sk->nr_items = 4096;
1439 max_found = find_root_gen(fd);
1441 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1444 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1448 /* the ioctl returns the number of item it found in nr_items */
1449 if (sk->nr_items == 0)
1455 * for each item, pull the key out of the header and then
1456 * read the root_ref item it contains
1458 for (i = 0; i < sk->nr_items; i++) {
1459 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1464 * just in case the item was too big, pass something other
1470 item = (struct btrfs_file_extent_item *)(args.buf +
1472 found_gen = btrfs_stack_file_extent_generation(item);
1473 if (sh->type == BTRFS_EXTENT_DATA_KEY &&
1474 found_gen >= oldest_gen) {
1475 print_one_extent(fd, sh, item, found_gen,
1476 &cache_dirid, &cache_dir_name,
1477 &cache_ino, &cache_full_name);
1482 * record the mins in sk so we can make sure the
1483 * next search doesn't repeat this root
1485 sk->min_objectid = sh->objectid;
1486 sk->min_offset = sh->offset;
1487 sk->min_type = sh->type;
1489 sk->nr_items = 4096;
1490 if (sk->min_offset < (u64)-1)
1492 else if (sk->min_objectid < (u64)-1) {
1499 free(cache_dir_name);
1500 free(cache_full_name);
1501 printf("transid marker was %llu\n", (unsigned long long)max_found);
1505 char *btrfs_list_path_for_root(int fd, u64 root)
1507 struct root_lookup root_lookup;
1509 char *ret_path = NULL;
1512 ret = __list_subvol_search(fd, &root_lookup);
1514 return ERR_PTR(ret);
1516 ret = __list_subvol_fill_paths(fd, &root_lookup);
1518 return ERR_PTR(ret);
1520 n = rb_last(&root_lookup.root);
1522 struct root_info *entry;
1524 entry = rb_entry(n, struct root_info, rb_node);
1525 resolve_root(&root_lookup, entry);
1526 if (entry->root_id == root) {
1527 ret_path = entry->full_path;
1528 entry->full_path = NULL;
1533 __free_all_subvolumn(&root_lookup);