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.
53 } btrfs_list_columns[] = {
66 .column_name = "CGen",
71 .column_name = "Parent",
76 .column_name = "Top Level",
81 .column_name = "OTime",
85 .name = "parent_uuid",
86 .column_name = "Parent UUID",
91 .column_name = "UUID",
96 .column_name = "Path",
106 static btrfs_list_filter_func all_filter_funcs[];
107 static btrfs_list_comp_func all_comp_funcs[];
109 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
113 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
115 if (column < BTRFS_LIST_ALL) {
116 btrfs_list_columns[column].need_print = 1;
120 for (i = 0; i < BTRFS_LIST_ALL; i++)
121 btrfs_list_columns[i].need_print = 1;
124 static void root_lookup_init(struct root_lookup *tree)
126 tree->root.rb_node = NULL;
129 static int comp_entry_with_rootid(struct root_info *entry1,
130 struct root_info *entry2,
135 if (entry1->root_id > entry2->root_id)
137 else if (entry1->root_id < entry2->root_id)
142 return is_descending ? -ret : ret;
145 static int comp_entry_with_gen(struct root_info *entry1,
146 struct root_info *entry2,
151 if (entry1->gen > entry2->gen)
153 else if (entry1->gen < entry2->gen)
158 return is_descending ? -ret : ret;
161 static int comp_entry_with_ogen(struct root_info *entry1,
162 struct root_info *entry2,
167 if (entry1->ogen > entry2->ogen)
169 else if (entry1->ogen < entry2->ogen)
174 return is_descending ? -ret : ret;
177 static int comp_entry_with_path(struct root_info *entry1,
178 struct root_info *entry2,
183 if (strcmp(entry1->full_path, entry2->full_path) > 0)
185 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
190 return is_descending ? -ret : ret;
193 static btrfs_list_comp_func all_comp_funcs[] = {
194 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
195 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
196 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
197 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
200 static char *all_sort_items[] = {
201 [BTRFS_LIST_COMP_ROOTID] = "rootid",
202 [BTRFS_LIST_COMP_OGEN] = "ogen",
203 [BTRFS_LIST_COMP_GEN] = "gen",
204 [BTRFS_LIST_COMP_PATH] = "path",
205 [BTRFS_LIST_COMP_MAX] = NULL,
208 static int btrfs_list_get_sort_item(char *sort_name)
212 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
213 if (strcmp(sort_name, all_sort_items[i]) == 0)
219 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
221 struct btrfs_list_comparer_set *set;
224 size = sizeof(struct btrfs_list_comparer_set) +
225 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
228 fprintf(stderr, "memory allocation failed\n");
232 memset(set, 0, size);
233 set->total = BTRFS_LIST_NCOMPS_INCREASE;
238 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
243 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
244 enum btrfs_list_comp_enum comparer, int is_descending)
246 struct btrfs_list_comparer_set *set = *comp_set;
250 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
251 BUG_ON(set->ncomps > set->total);
253 if (set->ncomps == set->total) {
254 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
255 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
256 set = realloc(set, size);
258 fprintf(stderr, "memory allocation failed\n");
262 memset(&set->comps[set->total], 0,
263 BTRFS_LIST_NCOMPS_INCREASE *
264 sizeof(struct btrfs_list_comparer));
265 set->total += BTRFS_LIST_NCOMPS_INCREASE;
269 BUG_ON(set->comps[set->ncomps].comp_func);
271 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
272 set->comps[set->ncomps].is_descending = is_descending;
277 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
278 struct btrfs_list_comparer_set *set)
280 int rootid_compared = 0;
283 if (!set || !set->ncomps)
286 for (i = 0; i < set->ncomps; i++) {
287 if (!set->comps[i].comp_func)
290 ret = set->comps[i].comp_func(entry1, entry2,
291 set->comps[i].is_descending);
295 if (set->comps[i].comp_func == comp_entry_with_rootid)
299 if (!rootid_compared) {
301 ret = comp_entry_with_rootid(entry1, entry2, 0);
307 static int sort_tree_insert(struct root_lookup *sort_tree,
308 struct root_info *ins,
309 struct btrfs_list_comparer_set *comp_set)
311 struct rb_node **p = &sort_tree->root.rb_node;
312 struct rb_node *parent = NULL;
313 struct root_info *curr;
318 curr = rb_entry(parent, struct root_info, sort_node);
320 ret = sort_comp(ins, curr, comp_set);
329 rb_link_node(&ins->sort_node, parent, p);
330 rb_insert_color(&ins->sort_node, &sort_tree->root);
335 * insert a new root into the tree. returns the existing root entry
336 * if one is already there. Both root_id and ref_tree are used
339 static int root_tree_insert(struct root_lookup *root_tree,
340 struct root_info *ins)
342 struct rb_node **p = &root_tree->root.rb_node;
343 struct rb_node * parent = NULL;
344 struct root_info *curr;
349 curr = rb_entry(parent, struct root_info, rb_node);
351 ret = comp_entry_with_rootid(ins, curr, 0);
360 rb_link_node(&ins->rb_node, parent, p);
361 rb_insert_color(&ins->rb_node, &root_tree->root);
366 * find a given root id in the tree. We return the smallest one,
367 * rb_next can be used to move forward looking for more if required
369 static struct root_info *root_tree_search(struct root_lookup *root_tree,
372 struct rb_node *n = root_tree->root.rb_node;
373 struct root_info *entry;
374 struct root_info tmp;
377 tmp.root_id = root_id;
380 entry = rb_entry(n, struct root_info, rb_node);
382 ret = comp_entry_with_rootid(&tmp, entry, 0);
393 static int update_root(struct root_lookup *root_lookup,
394 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
395 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
396 time_t ot, void *uuid, void *puuid)
398 struct root_info *ri;
400 ri = root_tree_search(root_lookup, root_id);
401 if (!ri || ri->root_id != root_id)
403 if (name && name_len > 0) {
407 ri->name = malloc(name_len + 1);
409 fprintf(stderr, "memory allocation failed\n");
412 strncpy(ri->name, name, name_len);
413 ri->name[name_len] = 0;
416 ri->ref_tree = ref_tree;
418 ri->root_offset = root_offset;
427 if (!ri->ogen && root_offset)
428 ri->ogen = root_offset;
432 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
434 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
440 * add_root - update the existed root, or allocate a new root and insert it
441 * into the lookup tree.
442 * root_id: object id of the root
443 * ref_tree: object id of the referring root.
444 * root_offset: offset value of the root'key
445 * dir_id: inode id of the directory in ref_tree where this root can be found.
446 * name: the name of root_id in that directory
447 * name_len: the length of name
448 * ogen: the original generation of the root
449 * gen: the current generation of the root
450 * ot: the original time(create time) of the root
451 * uuid: uuid of the root
452 * puuid: uuid of the root parent if any
454 static int add_root(struct root_lookup *root_lookup,
455 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
456 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
457 time_t ot, void *uuid, void *puuid)
459 struct root_info *ri;
462 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
463 dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
467 ri = malloc(sizeof(*ri));
469 printf("memory allocation failed\n");
472 memset(ri, 0, sizeof(*ri));
473 ri->root_id = root_id;
475 if (name && name_len > 0) {
476 ri->name = malloc(name_len + 1);
478 fprintf(stderr, "memory allocation failed\n");
481 strncpy(ri->name, name, name_len);
482 ri->name[name_len] = 0;
485 ri->ref_tree = ref_tree;
489 ri->root_offset = root_offset;
496 if (!ri->ogen && root_offset)
497 ri->ogen = root_offset;
502 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
505 memcpy(&ri->puuid, puuid, 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 static void __free_root_info(struct rb_node *node)
517 struct root_info *ri;
519 ri = rb_entry(node, struct root_info, rb_node);
532 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
534 rb_free_nodes(&root_tree->root, __free_root_info);
538 * for a given root_info, search through the root_lookup tree to construct
539 * the full path name to it.
541 * This can't be called until all the root_info->path fields are filled
542 * in by lookup_ino_path
544 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
547 char *full_path = NULL;
549 struct root_info *found;
552 * we go backwards from the root_info object and add pathnames
553 * from parent directories as we go.
562 * ref_tree = 0 indicates the subvolumes
565 if (!found->ref_tree) {
570 add_len = strlen(found->path);
573 /* room for / and for null */
574 tmp = malloc(add_len + 2 + len);
576 perror("malloc failed");
579 memcpy(tmp + add_len + 1, full_path, len);
581 memcpy(tmp, found->path, add_len);
582 tmp [add_len + len + 1] = '\0';
587 full_path = strdup(found->path);
591 next = found->ref_tree;
593 if (next == top_id) {
599 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
602 if (next == BTRFS_FS_TREE_OBJECTID) {
608 * if the ref_tree wasn't in our tree of roots, the
609 * subvolume was deleted.
611 found = root_tree_search(rl, next);
618 ri->full_path = full_path;
624 * for a single root_info, ask the kernel to give us a path name
625 * inside it's ref_root for the dir_id where it lives.
627 * This fills in root_info->path with the path to the directory and and
628 * appends this root's name.
630 static int lookup_ino_path(int fd, struct root_info *ri)
632 struct btrfs_ioctl_ino_lookup_args args;
641 memset(&args, 0, sizeof(args));
642 args.treeid = ri->ref_tree;
643 args.objectid = ri->dir_id;
645 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
652 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
653 (unsigned long long)ri->ref_tree,
660 * we're in a subdirectory of ref_tree, the kernel ioctl
661 * puts a / in there for us
663 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
665 perror("malloc failed");
668 strcpy(ri->path, args.name);
669 strcat(ri->path, ri->name);
671 /* we're at the root of ref_tree */
672 ri->path = strdup(ri->name);
674 perror("strdup failed");
681 /* finding the generation for a given path is a two step process.
682 * First we use the inode loookup routine to find out the root id
684 * Then we use the tree search ioctl to scan all the root items for a
685 * given root id and spit out the latest generation we can find
687 static u64 find_root_gen(int fd)
689 struct btrfs_ioctl_ino_lookup_args ino_args;
691 struct btrfs_ioctl_search_args args;
692 struct btrfs_ioctl_search_key *sk = &args.key;
693 struct btrfs_ioctl_search_header sh;
694 unsigned long off = 0;
699 memset(&ino_args, 0, sizeof(ino_args));
700 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
702 /* this ioctl fills in ino_args->treeid */
703 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
706 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
707 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
712 memset(&args, 0, sizeof(args));
717 * there may be more than one ROOT_ITEM key if there are
718 * snapshots pending deletion, we have to loop through
721 sk->min_objectid = ino_args.treeid;
722 sk->max_objectid = ino_args.treeid;
723 sk->max_type = BTRFS_ROOT_ITEM_KEY;
724 sk->min_type = BTRFS_ROOT_ITEM_KEY;
725 sk->max_offset = (u64)-1;
726 sk->max_transid = (u64)-1;
730 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
733 fprintf(stderr, "ERROR: can't perform the search - %s\n",
737 /* the ioctl returns the number of item it found in nr_items */
738 if (sk->nr_items == 0)
742 for (i = 0; i < sk->nr_items; i++) {
743 struct btrfs_root_item *item;
745 memcpy(&sh, args.buf + off, sizeof(sh));
747 item = (struct btrfs_root_item *)(args.buf + off);
750 sk->min_objectid = sh.objectid;
751 sk->min_type = sh.type;
752 sk->min_offset = sh.offset;
754 if (sh.objectid > ino_args.treeid)
757 if (sh.objectid == ino_args.treeid &&
758 sh.type == BTRFS_ROOT_ITEM_KEY) {
759 max_found = max(max_found,
760 btrfs_root_generation(item));
763 if (sk->min_offset < (u64)-1)
768 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
770 if (sk->min_objectid != ino_args.treeid)
776 /* pass in a directory id and this will return
777 * the full path of the parent directory inside its
780 * It may return NULL if it is in the root, or an ERR_PTR if things
783 static char *__ino_resolve(int fd, u64 dirid)
785 struct btrfs_ioctl_ino_lookup_args args;
790 memset(&args, 0, sizeof(args));
791 args.objectid = dirid;
793 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
796 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
797 (unsigned long long)dirid, strerror(e) );
803 * we're in a subdirectory of ref_tree, the kernel ioctl
804 * puts a / in there for us
806 full = strdup(args.name);
808 perror("malloc failed");
809 return ERR_PTR(-ENOMEM);
812 /* we're at the root of ref_tree */
819 * simple string builder, returning a new string with both
822 static char *build_name(char *dirid, char *name)
828 full = malloc(strlen(dirid) + strlen(name) + 1);
837 * given an inode number, this returns the full path name inside the subvolume
838 * to that file/directory. cache_dirid and cache_name are used to
839 * cache the results so we can avoid tree searches if a later call goes
840 * to the same directory or file name
842 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
850 struct btrfs_ioctl_search_args args;
851 struct btrfs_ioctl_search_key *sk = &args.key;
852 struct btrfs_ioctl_search_header *sh;
853 unsigned long off = 0;
857 memset(&args, 0, sizeof(args));
862 * step one, we search for the inode back ref. We just use the first
865 sk->min_objectid = ino;
866 sk->max_objectid = ino;
867 sk->max_type = BTRFS_INODE_REF_KEY;
868 sk->max_offset = (u64)-1;
869 sk->min_type = BTRFS_INODE_REF_KEY;
870 sk->max_transid = (u64)-1;
873 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
876 fprintf(stderr, "ERROR: can't perform the search - %s\n",
880 /* the ioctl returns the number of item it found in nr_items */
881 if (sk->nr_items == 0)
885 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
887 if (sh->type == BTRFS_INODE_REF_KEY) {
888 struct btrfs_inode_ref *ref;
891 ref = (struct btrfs_inode_ref *)(sh + 1);
892 namelen = btrfs_stack_inode_ref_name_len(ref);
894 name = (char *)(ref + 1);
895 name = strndup(name, namelen);
897 /* use our cached value */
898 if (dirid == *cache_dirid && *cache_name) {
899 dirname = *cache_name;
906 * the inode backref gives us the file name and the parent directory id.
907 * From here we use __ino_resolve to get the path to the parent
909 dirname = __ino_resolve(fd, dirid);
911 full = build_name(dirname, name);
912 if (*cache_name && dirname != *cache_name)
915 *cache_name = dirname;
916 *cache_dirid = dirid;
922 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
924 struct btrfs_ioctl_search_args args;
925 struct btrfs_ioctl_search_key *sk = &args.key;
926 struct btrfs_ioctl_search_header *sh;
930 memset(&args, 0, sizeof(args));
933 * search for a dir item with a name 'default' in the tree of
934 * tree roots, it should point us to a default root
938 /* don't worry about ancient format and request only one item */
941 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
942 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
943 sk->max_type = BTRFS_DIR_ITEM_KEY;
944 sk->min_type = BTRFS_DIR_ITEM_KEY;
945 sk->max_offset = (u64)-1;
946 sk->max_transid = (u64)-1;
948 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
952 /* the ioctl returns the number of items it found in nr_items */
953 if (sk->nr_items == 0)
956 sh = (struct btrfs_ioctl_search_header *)args.buf;
958 if (sh->type == BTRFS_DIR_ITEM_KEY) {
959 struct btrfs_dir_item *di;
963 di = (struct btrfs_dir_item *)(sh + 1);
964 name_len = btrfs_stack_dir_name_len(di);
965 name = (char *)(di + 1);
967 if (!strncmp("default", name, name_len))
968 found = btrfs_disk_key_objectid(&di->location);
976 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
979 struct btrfs_ioctl_search_args args;
980 struct btrfs_ioctl_search_key *sk = &args.key;
981 struct btrfs_ioctl_search_header sh;
982 struct btrfs_root_ref *ref;
983 struct btrfs_root_item *ri;
984 unsigned long off = 0;
993 u8 uuid[BTRFS_UUID_SIZE];
994 u8 puuid[BTRFS_UUID_SIZE];
996 root_lookup_init(root_lookup);
997 memset(&args, 0, sizeof(args));
999 /* search in the tree of tree roots */
1003 * set the min and max to backref keys. The search will
1004 * only send back this type of key now.
1006 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1007 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1009 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1012 * set all the other params to the max, we'll take any objectid
1015 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1016 sk->max_offset = (u64)-1;
1017 sk->max_transid = (u64)-1;
1019 /* just a big number, doesn't matter much */
1020 sk->nr_items = 4096;
1023 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1026 /* the ioctl returns the number of item it found in nr_items */
1027 if (sk->nr_items == 0)
1033 * for each item, pull the key out of the header and then
1034 * read the root_ref item it contains
1036 for (i = 0; i < sk->nr_items; i++) {
1037 memcpy(&sh, args.buf + off, sizeof(sh));
1039 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1040 ref = (struct btrfs_root_ref *)(args.buf + off);
1041 name_len = btrfs_stack_root_ref_name_len(ref);
1042 name = (char *)(ref + 1);
1043 dir_id = btrfs_stack_root_ref_dirid(ref);
1045 add_root(root_lookup, sh.objectid, sh.offset,
1046 0, 0, dir_id, name, name_len, 0, 0, 0,
1048 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1049 ri = (struct btrfs_root_item *)(args.buf + off);
1050 gen = btrfs_root_generation(ri);
1051 flags = btrfs_root_flags(ri);
1053 sizeof(struct btrfs_root_item_v0)) {
1054 t = btrfs_stack_timespec_sec(&ri->otime);
1055 ogen = btrfs_root_otransid(ri);
1056 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1057 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1061 memset(uuid, 0, BTRFS_UUID_SIZE);
1062 memset(puuid, 0, BTRFS_UUID_SIZE);
1065 add_root(root_lookup, sh.objectid, 0,
1066 sh.offset, flags, 0, NULL, 0, ogen,
1067 gen, t, uuid, puuid);
1073 * record the mins in sk so we can make sure the
1074 * next search doesn't repeat this root
1076 sk->min_objectid = sh.objectid;
1077 sk->min_type = sh.type;
1078 sk->min_offset = sh.offset;
1080 sk->nr_items = 4096;
1082 if (!sk->min_offset) /* overflow */
1087 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1088 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1093 if (sk->min_objectid > sk->max_objectid)
1100 static int filter_by_rootid(struct root_info *ri, u64 data)
1102 return ri->root_id == data;
1105 static int filter_snapshot(struct root_info *ri, u64 data)
1107 return !!ri->root_offset;
1110 static int filter_flags(struct root_info *ri, u64 flags)
1112 return ri->flags & flags;
1115 static int filter_gen_more(struct root_info *ri, u64 data)
1117 return ri->gen >= data;
1120 static int filter_gen_less(struct root_info *ri, u64 data)
1122 return ri->gen <= data;
1125 static int filter_gen_equal(struct root_info *ri, u64 data)
1127 return ri->gen == data;
1130 static int filter_cgen_more(struct root_info *ri, u64 data)
1132 return ri->ogen >= data;
1135 static int filter_cgen_less(struct root_info *ri, u64 data)
1137 return ri->ogen <= data;
1140 static int filter_cgen_equal(struct root_info *ri, u64 data)
1142 return ri->ogen == data;
1145 static int filter_topid_equal(struct root_info *ri, u64 data)
1147 return ri->top_id == data;
1150 static int filter_full_path(struct root_info *ri, u64 data)
1152 if (ri->full_path && ri->top_id != data) {
1154 char p[] = "<FS_TREE>";
1155 int add_len = strlen(p);
1156 int len = strlen(ri->full_path);
1158 tmp = malloc(len + add_len + 2);
1160 fprintf(stderr, "memory allocation failed\n");
1163 memcpy(tmp + add_len + 1, ri->full_path, len);
1164 tmp[len + add_len + 1] = '\0';
1166 memcpy(tmp, p, add_len);
1167 free(ri->full_path);
1168 ri->full_path = tmp;
1173 static int filter_by_parent(struct root_info *ri, u64 data)
1175 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1178 static btrfs_list_filter_func all_filter_funcs[] = {
1179 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1180 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1181 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1182 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1183 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1184 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1185 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1186 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1187 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1188 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1189 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1190 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1193 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1195 struct btrfs_list_filter_set *set;
1198 size = sizeof(struct btrfs_list_filter_set) +
1199 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1202 fprintf(stderr, "memory allocation failed\n");
1206 memset(set, 0, size);
1207 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1212 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1217 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1218 enum btrfs_list_filter_enum filter, u64 data)
1220 struct btrfs_list_filter_set *set = *filter_set;
1224 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1225 BUG_ON(set->nfilters > set->total);
1227 if (set->nfilters == set->total) {
1228 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1229 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1230 set = realloc(set, size);
1232 fprintf(stderr, "memory allocation failed\n");
1236 memset(&set->filters[set->total], 0,
1237 BTRFS_LIST_NFILTERS_INCREASE *
1238 sizeof(struct btrfs_list_filter));
1239 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1243 BUG_ON(set->filters[set->nfilters].filter_func);
1245 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1246 set->filters[set->nfilters].data = data;
1251 static int filter_root(struct root_info *ri,
1252 struct btrfs_list_filter_set *set)
1256 if (!set || !set->nfilters)
1259 for (i = 0; i < set->nfilters; i++) {
1260 if (!set->filters[i].filter_func)
1262 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1269 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1270 struct root_lookup *sort_tree,
1271 struct btrfs_list_filter_set *filter_set,
1272 struct btrfs_list_comparer_set *comp_set,
1276 struct root_info *entry;
1279 root_lookup_init(sort_tree);
1281 n = rb_last(&all_subvols->root);
1283 entry = rb_entry(n, struct root_info, rb_node);
1285 ret = resolve_root(all_subvols, entry, top_id);
1288 ret = filter_root(entry, filter_set);
1290 sort_tree_insert(sort_tree, entry, comp_set);
1296 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1300 n = rb_first(&root_lookup->root);
1302 struct root_info *entry;
1304 entry = rb_entry(n, struct root_info, rb_node);
1305 ret = lookup_ino_path(fd, entry);
1306 if (ret && ret != -ENOENT)
1314 static void print_subvolume_column(struct root_info *subv,
1315 enum btrfs_list_column_enum column)
1320 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1323 case BTRFS_LIST_OBJECTID:
1324 printf("%llu", subv->root_id);
1326 case BTRFS_LIST_GENERATION:
1327 printf("%llu", subv->gen);
1329 case BTRFS_LIST_OGENERATION:
1330 printf("%llu", subv->ogen);
1332 case BTRFS_LIST_PARENT:
1333 printf("%llu", subv->ref_tree);
1335 case BTRFS_LIST_TOP_LEVEL:
1336 printf("%llu", subv->top_id);
1338 case BTRFS_LIST_OTIME:
1342 localtime_r(&subv->otime, &tm);
1343 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1348 case BTRFS_LIST_UUID:
1349 if (uuid_is_null(subv->uuid))
1350 strcpy(uuidparse, "-");
1352 uuid_unparse(subv->uuid, uuidparse);
1353 printf("%s", uuidparse);
1355 case BTRFS_LIST_PUUID:
1356 if (uuid_is_null(subv->puuid))
1357 strcpy(uuidparse, "-");
1359 uuid_unparse(subv->puuid, uuidparse);
1360 printf("%s", uuidparse);
1362 case BTRFS_LIST_PATH:
1363 BUG_ON(!subv->full_path);
1364 printf("%s", subv->full_path);
1371 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1375 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1376 if (!btrfs_list_columns[i].need_print)
1380 printf("%s",raw_prefix);
1382 print_subvolume_column(subv, i);
1387 static void print_single_volume_info_table(struct root_info *subv)
1391 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1392 if (!btrfs_list_columns[i].need_print)
1395 print_subvolume_column(subv, i);
1397 if (i != BTRFS_LIST_PATH)
1400 if (i == BTRFS_LIST_TOP_LEVEL)
1406 static void print_single_volume_info_default(struct root_info *subv)
1410 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1411 if (!btrfs_list_columns[i].need_print)
1414 printf("%s ", btrfs_list_columns[i].name);
1415 print_subvolume_column(subv, i);
1417 if (i != BTRFS_LIST_PATH)
1423 static void print_all_volume_info_tab_head()
1429 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1430 if (btrfs_list_columns[i].need_print)
1431 printf("%s\t", btrfs_list_columns[i].name);
1433 if (i == BTRFS_LIST_ALL-1)
1437 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1438 memset(barrier, 0, sizeof(barrier));
1440 if (btrfs_list_columns[i].need_print) {
1441 len = strlen(btrfs_list_columns[i].name);
1443 strcat(barrier, "-");
1445 printf("%s\t", barrier);
1447 if (i == BTRFS_LIST_ALL-1)
1452 static void print_all_volume_info(struct root_lookup *sorted_tree,
1453 int layout, char *raw_prefix)
1456 struct root_info *entry;
1458 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1459 print_all_volume_info_tab_head();
1461 n = rb_first(&sorted_tree->root);
1463 entry = rb_entry(n, struct root_info, sort_node);
1465 case BTRFS_LIST_LAYOUT_DEFAULT:
1466 print_single_volume_info_default(entry);
1468 case BTRFS_LIST_LAYOUT_TABLE:
1469 print_single_volume_info_table(entry);
1471 case BTRFS_LIST_LAYOUT_RAW:
1472 print_single_volume_info_raw(entry, raw_prefix);
1479 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1483 ret = __list_subvol_search(fd, root_lookup);
1485 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1491 * now we have an rbtree full of root_info objects, but we need to fill
1492 * in their path names within the subvol that is referencing each one.
1494 ret = __list_subvol_fill_paths(fd, root_lookup);
1498 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1499 struct btrfs_list_comparer_set *comp_set,
1500 int layout, int full_path, char *raw_prefix)
1502 struct root_lookup root_lookup;
1503 struct root_lookup root_sort;
1508 ret = btrfs_list_get_path_rootid(fd, &top_id);
1512 ret = btrfs_list_subvols(fd, &root_lookup);
1515 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1518 print_all_volume_info(&root_sort, layout, raw_prefix);
1519 __free_all_subvolumn(&root_lookup);
1524 static char *strdup_or_null(const char *s)
1531 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1534 struct root_lookup rl;
1535 struct rb_node *rbn;
1536 struct root_info *ri;
1539 ret = btrfs_list_get_path_rootid(fd, &root_id);
1543 ret = btrfs_list_subvols(fd, &rl);
1547 rbn = rb_first(&rl.root);
1549 ri = rb_entry(rbn, struct root_info, rb_node);
1550 rr = resolve_root(&rl, ri, root_id);
1551 if (rr == -ENOENT) {
1556 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1557 memcpy(the_ri, ri, offsetof(struct root_info, path));
1558 the_ri->path = strdup_or_null(ri->path);
1559 the_ri->name = strdup_or_null(ri->name);
1560 the_ri->full_path = strdup_or_null(ri->full_path);
1566 __free_all_subvolumn(&rl);
1570 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1571 struct btrfs_file_extent_item *item,
1572 u64 found_gen, u64 *cache_dirid,
1573 char **cache_dir_name, u64 *cache_ino,
1574 char **cache_full_name)
1578 u64 disk_offset = 0;
1584 if (sh->objectid == *cache_ino) {
1585 name = *cache_full_name;
1586 } else if (*cache_full_name) {
1587 free(*cache_full_name);
1588 *cache_full_name = NULL;
1591 name = ino_resolve(fd, sh->objectid, cache_dirid,
1593 *cache_full_name = name;
1594 *cache_ino = sh->objectid;
1599 type = btrfs_stack_file_extent_type(item);
1600 compressed = btrfs_stack_file_extent_compression(item);
1602 if (type == BTRFS_FILE_EXTENT_REG ||
1603 type == BTRFS_FILE_EXTENT_PREALLOC) {
1604 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1605 disk_offset = btrfs_stack_file_extent_offset(item);
1606 len = btrfs_stack_file_extent_num_bytes(item);
1607 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1610 len = btrfs_stack_file_extent_ram_bytes(item);
1612 printf("unhandled extent type %d for inode %llu "
1613 "file offset %llu gen %llu\n",
1615 (unsigned long long)sh->objectid,
1616 (unsigned long long)sh->offset,
1617 (unsigned long long)found_gen);
1621 printf("inode %llu file offset %llu len %llu disk start %llu "
1622 "offset %llu gen %llu flags ",
1623 (unsigned long long)sh->objectid,
1624 (unsigned long long)sh->offset,
1625 (unsigned long long)len,
1626 (unsigned long long)disk_start,
1627 (unsigned long long)disk_offset,
1628 (unsigned long long)found_gen);
1634 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1635 printf("%sPREALLOC", flags ? "|" : "");
1638 if (type == BTRFS_FILE_EXTENT_INLINE) {
1639 printf("%sINLINE", flags ? "|" : "");
1645 printf(" %s\n", name);
1649 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1652 struct btrfs_ioctl_search_args args;
1653 struct btrfs_ioctl_search_key *sk = &args.key;
1654 struct btrfs_ioctl_search_header sh;
1655 struct btrfs_file_extent_item *item;
1656 unsigned long off = 0;
1661 u64 cache_dirid = 0;
1663 char *cache_dir_name = NULL;
1664 char *cache_full_name = NULL;
1665 struct btrfs_file_extent_item backup;
1667 memset(&backup, 0, sizeof(backup));
1668 memset(&args, 0, sizeof(args));
1670 sk->tree_id = root_id;
1673 * set all the other params to the max, we'll take any objectid
1676 sk->max_objectid = (u64)-1;
1677 sk->max_offset = (u64)-1;
1678 sk->max_transid = (u64)-1;
1679 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1680 sk->min_transid = oldest_gen;
1681 /* just a big number, doesn't matter much */
1682 sk->nr_items = 4096;
1684 max_found = find_root_gen(fd);
1686 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1689 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1693 /* the ioctl returns the number of item it found in nr_items */
1694 if (sk->nr_items == 0)
1700 * for each item, pull the key out of the header and then
1701 * read the root_ref item it contains
1703 for (i = 0; i < sk->nr_items; i++) {
1704 memcpy(&sh, args.buf + off, sizeof(sh));
1708 * just in case the item was too big, pass something other
1714 item = (struct btrfs_file_extent_item *)(args.buf +
1716 found_gen = btrfs_stack_file_extent_generation(item);
1717 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1718 found_gen >= oldest_gen) {
1719 print_one_extent(fd, &sh, item, found_gen,
1720 &cache_dirid, &cache_dir_name,
1721 &cache_ino, &cache_full_name);
1726 * record the mins in sk so we can make sure the
1727 * next search doesn't repeat this root
1729 sk->min_objectid = sh.objectid;
1730 sk->min_offset = sh.offset;
1731 sk->min_type = sh.type;
1733 sk->nr_items = 4096;
1734 if (sk->min_offset < (u64)-1)
1736 else if (sk->min_objectid < (u64)-1) {
1743 free(cache_dir_name);
1744 free(cache_full_name);
1745 printf("transid marker was %llu\n", (unsigned long long)max_found);
1749 char *btrfs_list_path_for_root(int fd, u64 root)
1751 struct root_lookup root_lookup;
1753 char *ret_path = NULL;
1757 ret = btrfs_list_get_path_rootid(fd, &top_id);
1759 return ERR_PTR(ret);
1761 ret = __list_subvol_search(fd, &root_lookup);
1763 return ERR_PTR(ret);
1765 ret = __list_subvol_fill_paths(fd, &root_lookup);
1767 return ERR_PTR(ret);
1769 n = rb_last(&root_lookup.root);
1771 struct root_info *entry;
1773 entry = rb_entry(n, struct root_info, rb_node);
1774 ret = resolve_root(&root_lookup, entry, top_id);
1775 if (ret == -ENOENT && entry->root_id == root) {
1779 if (entry->root_id == root) {
1780 ret_path = entry->full_path;
1781 entry->full_path = NULL;
1786 __free_all_subvolumn(&root_lookup);
1791 int btrfs_list_parse_sort_string(char *optarg,
1792 struct btrfs_list_comparer_set **comps)
1800 while ((p = strtok(optarg, ",")) != NULL) {
1802 ptr_argv = all_sort_items;
1805 if (strcmp(*ptr_argv, p) == 0) {
1810 if (strcmp(*ptr_argv, p) == 0) {
1827 } else if (*p == '-') {
1833 what_to_sort = btrfs_list_get_sort_item(p);
1834 btrfs_list_setup_comparer(comps, what_to_sort, order);
1843 * This function is used to parse the argument of filter condition.
1845 * type is the filter object.
1847 int btrfs_list_parse_filter_string(char *optarg,
1848 struct btrfs_list_filter_set **filters,
1849 enum btrfs_list_filter_enum type)
1853 char *ptr_parse_end = NULL;
1854 char *ptr_optarg_end = optarg + strlen(optarg);
1856 switch (*(optarg++)) {
1858 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1860 if (ptr_parse_end != ptr_optarg_end)
1863 btrfs_list_setup_filter(filters, type, arg);
1866 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1868 if (ptr_parse_end != ptr_optarg_end)
1871 btrfs_list_setup_filter(filters, type, arg);
1875 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1877 if (ptr_parse_end != ptr_optarg_end)
1879 btrfs_list_setup_filter(filters, type, arg);
1886 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1889 struct btrfs_ioctl_ino_lookup_args args;
1891 memset(&args, 0, sizeof(args));
1892 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1894 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1897 "ERROR: can't perform the search -%s\n",
1901 *treeid = args.treeid;