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 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
244 enum btrfs_list_comp_enum comparer,
247 struct btrfs_list_comparer_set *set = *comp_set;
251 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
252 BUG_ON(set->ncomps > set->total);
254 if (set->ncomps == set->total) {
255 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
256 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
257 set = realloc(set, size);
259 fprintf(stderr, "memory allocation failed\n");
263 memset(&set->comps[set->total], 0,
264 BTRFS_LIST_NCOMPS_INCREASE *
265 sizeof(struct btrfs_list_comparer));
266 set->total += BTRFS_LIST_NCOMPS_INCREASE;
270 BUG_ON(set->comps[set->ncomps].comp_func);
272 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
273 set->comps[set->ncomps].is_descending = is_descending;
278 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
279 struct btrfs_list_comparer_set *set)
281 int rootid_compared = 0;
284 if (!set || !set->ncomps)
287 for (i = 0; i < set->ncomps; i++) {
288 if (!set->comps[i].comp_func)
291 ret = set->comps[i].comp_func(entry1, entry2,
292 set->comps[i].is_descending);
296 if (set->comps[i].comp_func == comp_entry_with_rootid)
300 if (!rootid_compared) {
302 ret = comp_entry_with_rootid(entry1, entry2, 0);
308 static int sort_tree_insert(struct root_lookup *sort_tree,
309 struct root_info *ins,
310 struct btrfs_list_comparer_set *comp_set)
312 struct rb_node **p = &sort_tree->root.rb_node;
313 struct rb_node *parent = NULL;
314 struct root_info *curr;
319 curr = rb_entry(parent, struct root_info, sort_node);
321 ret = sort_comp(ins, curr, comp_set);
330 rb_link_node(&ins->sort_node, parent, p);
331 rb_insert_color(&ins->sort_node, &sort_tree->root);
336 * insert a new root into the tree. returns the existing root entry
337 * if one is already there. Both root_id and ref_tree are used
340 static int root_tree_insert(struct root_lookup *root_tree,
341 struct root_info *ins)
343 struct rb_node **p = &root_tree->root.rb_node;
344 struct rb_node * parent = NULL;
345 struct root_info *curr;
350 curr = rb_entry(parent, struct root_info, rb_node);
352 ret = comp_entry_with_rootid(ins, curr, 0);
361 rb_link_node(&ins->rb_node, parent, p);
362 rb_insert_color(&ins->rb_node, &root_tree->root);
367 * find a given root id in the tree. We return the smallest one,
368 * rb_next can be used to move forward looking for more if required
370 static struct root_info *root_tree_search(struct root_lookup *root_tree,
373 struct rb_node *n = root_tree->root.rb_node;
374 struct root_info *entry;
375 struct root_info tmp;
378 tmp.root_id = root_id;
381 entry = rb_entry(n, struct root_info, rb_node);
383 ret = comp_entry_with_rootid(&tmp, entry, 0);
394 static int update_root(struct root_lookup *root_lookup,
395 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
396 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
397 time_t ot, void *uuid, void *puuid)
399 struct root_info *ri;
401 ri = root_tree_search(root_lookup, root_id);
402 if (!ri || ri->root_id != root_id)
404 if (name && name_len > 0) {
408 ri->name = malloc(name_len + 1);
410 fprintf(stderr, "memory allocation failed\n");
413 strncpy(ri->name, name, name_len);
414 ri->name[name_len] = 0;
417 ri->ref_tree = ref_tree;
419 ri->root_offset = root_offset;
428 if (!ri->ogen && root_offset)
429 ri->ogen = root_offset;
433 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
435 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
441 * add_root - update the existed root, or allocate a new root and insert it
442 * into the lookup tree.
443 * root_id: object id of the root
444 * ref_tree: object id of the referring root.
445 * root_offset: offset value of the root'key
446 * dir_id: inode id of the directory in ref_tree where this root can be found.
447 * name: the name of root_id in that directory
448 * name_len: the length of name
449 * ogen: the original generation of the root
450 * gen: the current generation of the root
451 * ot: the original time(create time) of the root
452 * uuid: uuid of the root
453 * puuid: uuid of the root parent if any
455 static int add_root(struct root_lookup *root_lookup,
456 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
457 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
458 time_t ot, void *uuid, void *puuid)
460 struct root_info *ri;
463 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
464 dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
468 ri = malloc(sizeof(*ri));
470 printf("memory allocation failed\n");
473 memset(ri, 0, sizeof(*ri));
474 ri->root_id = root_id;
476 if (name && name_len > 0) {
477 ri->name = malloc(name_len + 1);
479 fprintf(stderr, "memory allocation failed\n");
482 strncpy(ri->name, name, name_len);
483 ri->name[name_len] = 0;
486 ri->ref_tree = ref_tree;
490 ri->root_offset = root_offset;
497 if (!ri->ogen && root_offset)
498 ri->ogen = root_offset;
503 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
506 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
508 ret = root_tree_insert(root_lookup, ri);
510 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
516 void __free_root_info(struct root_info *ri)
530 void __free_all_subvolumn(struct root_lookup *root_tree)
532 struct root_info *entry;
535 n = rb_first(&root_tree->root);
537 entry = rb_entry(n, struct root_info, rb_node);
538 rb_erase(n, &root_tree->root);
539 __free_root_info(entry);
541 n = rb_first(&root_tree->root);
546 * for a given root_info, search through the root_lookup tree to construct
547 * the full path name to it.
549 * This can't be called until all the root_info->path fields are filled
550 * in by lookup_ino_path
552 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
555 char *full_path = NULL;
557 struct root_info *found;
560 * we go backwards from the root_info object and add pathnames
561 * from parent directories as we go.
568 * ref_tree = 0 indicates the subvolumes
571 if (!found->ref_tree)
573 int add_len = strlen(found->path);
575 /* room for / and for null */
576 tmp = malloc(add_len + 2 + len);
578 perror("malloc failed");
582 memcpy(tmp + add_len + 1, full_path, len);
584 memcpy(tmp, found->path, add_len);
585 tmp [add_len + len + 1] = '\0';
590 full_path = strdup(found->path);
594 next = found->ref_tree;
596 if (next == top_id) {
602 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
605 if (next == BTRFS_FS_TREE_OBJECTID) {
611 * if the ref_tree wasn't in our tree of roots, the
612 * subvolume was deleted.
614 found = root_tree_search(rl, next);
619 ri->full_path = full_path;
625 * for a single root_info, ask the kernel to give us a path name
626 * inside it's ref_root for the dir_id where it lives.
628 * This fills in root_info->path with the path to the directory and and
629 * appends this root's name.
631 static int lookup_ino_path(int fd, struct root_info *ri)
633 struct btrfs_ioctl_ino_lookup_args args;
642 memset(&args, 0, sizeof(args));
643 args.treeid = ri->ref_tree;
644 args.objectid = ri->dir_id;
646 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
653 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
654 (unsigned long long)ri->ref_tree,
661 * we're in a subdirectory of ref_tree, the kernel ioctl
662 * puts a / in there for us
664 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
666 perror("malloc failed");
669 strcpy(ri->path, args.name);
670 strcat(ri->path, ri->name);
672 /* we're at the root of ref_tree */
673 ri->path = strdup(ri->name);
675 perror("strdup failed");
682 /* finding the generation for a given path is a two step process.
683 * First we use the inode loookup routine to find out the root id
685 * Then we use the tree search ioctl to scan all the root items for a
686 * given root id and spit out the latest generation we can find
688 static u64 find_root_gen(int fd)
690 struct btrfs_ioctl_ino_lookup_args ino_args;
692 struct btrfs_ioctl_search_args args;
693 struct btrfs_ioctl_search_key *sk = &args.key;
694 struct btrfs_ioctl_search_header sh;
695 unsigned long off = 0;
700 memset(&ino_args, 0, sizeof(ino_args));
701 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
703 /* this ioctl fills in ino_args->treeid */
704 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
707 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
708 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
713 memset(&args, 0, sizeof(args));
718 * there may be more than one ROOT_ITEM key if there are
719 * snapshots pending deletion, we have to loop through
722 sk->min_objectid = ino_args.treeid;
723 sk->max_objectid = ino_args.treeid;
724 sk->max_type = BTRFS_ROOT_ITEM_KEY;
725 sk->min_type = BTRFS_ROOT_ITEM_KEY;
726 sk->max_offset = (u64)-1;
727 sk->max_transid = (u64)-1;
731 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
734 fprintf(stderr, "ERROR: can't perform the search - %s\n",
738 /* the ioctl returns the number of item it found in nr_items */
739 if (sk->nr_items == 0)
743 for (i = 0; i < sk->nr_items; i++) {
744 struct btrfs_root_item *item;
746 memcpy(&sh, args.buf + off, sizeof(sh));
748 item = (struct btrfs_root_item *)(args.buf + off);
751 sk->min_objectid = sh.objectid;
752 sk->min_type = sh.type;
753 sk->min_offset = sh.offset;
755 if (sh.objectid > ino_args.treeid)
758 if (sh.objectid == ino_args.treeid &&
759 sh.type == BTRFS_ROOT_ITEM_KEY) {
760 max_found = max(max_found,
761 btrfs_root_generation(item));
764 if (sk->min_offset < (u64)-1)
769 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
771 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
777 /* pass in a directory id and this will return
778 * the full path of the parent directory inside its
781 * It may return NULL if it is in the root, or an ERR_PTR if things
784 static char *__ino_resolve(int fd, u64 dirid)
786 struct btrfs_ioctl_ino_lookup_args args;
791 memset(&args, 0, sizeof(args));
792 args.objectid = dirid;
794 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
797 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
798 (unsigned long long)dirid, strerror(e) );
804 * we're in a subdirectory of ref_tree, the kernel ioctl
805 * puts a / in there for us
807 full = strdup(args.name);
809 perror("malloc failed");
810 return ERR_PTR(-ENOMEM);
813 /* we're at the root of ref_tree */
820 * simple string builder, returning a new string with both
823 char *build_name(char *dirid, char *name)
829 full = malloc(strlen(dirid) + strlen(name) + 1);
838 * given an inode number, this returns the full path name inside the subvolume
839 * to that file/directory. cache_dirid and cache_name are used to
840 * cache the results so we can avoid tree searches if a later call goes
841 * to the same directory or file name
843 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
851 struct btrfs_ioctl_search_args args;
852 struct btrfs_ioctl_search_key *sk = &args.key;
853 struct btrfs_ioctl_search_header *sh;
854 unsigned long off = 0;
858 memset(&args, 0, sizeof(args));
863 * step one, we search for the inode back ref. We just use the first
866 sk->min_objectid = ino;
867 sk->max_objectid = ino;
868 sk->max_type = BTRFS_INODE_REF_KEY;
869 sk->max_offset = (u64)-1;
870 sk->min_type = BTRFS_INODE_REF_KEY;
871 sk->max_transid = (u64)-1;
874 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
877 fprintf(stderr, "ERROR: can't perform the search - %s\n",
881 /* the ioctl returns the number of item it found in nr_items */
882 if (sk->nr_items == 0)
886 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
888 if (sh->type == BTRFS_INODE_REF_KEY) {
889 struct btrfs_inode_ref *ref;
892 ref = (struct btrfs_inode_ref *)(sh + 1);
893 namelen = btrfs_stack_inode_ref_name_len(ref);
895 name = (char *)(ref + 1);
896 name = strndup(name, namelen);
898 /* use our cached value */
899 if (dirid == *cache_dirid && *cache_name) {
900 dirname = *cache_name;
907 * the inode backref gives us the file name and the parent directory id.
908 * From here we use __ino_resolve to get the path to the parent
910 dirname = __ino_resolve(fd, dirid);
912 full = build_name(dirname, name);
913 if (*cache_name && dirname != *cache_name)
916 *cache_name = dirname;
917 *cache_dirid = dirid;
923 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
925 struct btrfs_ioctl_search_args args;
926 struct btrfs_ioctl_search_key *sk = &args.key;
927 struct btrfs_ioctl_search_header *sh;
931 memset(&args, 0, sizeof(args));
934 * search for a dir item with a name 'default' in the tree of
935 * tree roots, it should point us to a default root
939 /* don't worry about ancient format and request only one item */
942 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
943 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
944 sk->max_type = BTRFS_DIR_ITEM_KEY;
945 sk->min_type = BTRFS_DIR_ITEM_KEY;
946 sk->max_offset = (u64)-1;
947 sk->max_transid = (u64)-1;
949 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
953 /* the ioctl returns the number of items it found in nr_items */
954 if (sk->nr_items == 0)
957 sh = (struct btrfs_ioctl_search_header *)args.buf;
959 if (sh->type == BTRFS_DIR_ITEM_KEY) {
960 struct btrfs_dir_item *di;
964 di = (struct btrfs_dir_item *)(sh + 1);
965 name_len = btrfs_stack_dir_name_len(di);
966 name = (char *)(di + 1);
968 if (!strncmp("default", name, name_len))
969 found = btrfs_disk_key_objectid(&di->location);
977 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
980 struct btrfs_ioctl_search_args args;
981 struct btrfs_ioctl_search_key *sk = &args.key;
982 struct btrfs_ioctl_search_header sh;
983 struct btrfs_root_ref *ref;
984 struct btrfs_root_item *ri;
985 unsigned long off = 0;
994 u8 uuid[BTRFS_UUID_SIZE];
995 u8 puuid[BTRFS_UUID_SIZE];
997 root_lookup_init(root_lookup);
998 memset(&args, 0, sizeof(args));
1000 /* search in the tree of tree roots */
1004 * set the min and max to backref keys. The search will
1005 * only send back this type of key now.
1007 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1008 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1010 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1013 * set all the other params to the max, we'll take any objectid
1016 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1017 sk->max_offset = (u64)-1;
1018 sk->max_transid = (u64)-1;
1020 /* just a big number, doesn't matter much */
1021 sk->nr_items = 4096;
1024 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1027 /* the ioctl returns the number of item it found in nr_items */
1028 if (sk->nr_items == 0)
1034 * for each item, pull the key out of the header and then
1035 * read the root_ref item it contains
1037 for (i = 0; i < sk->nr_items; i++) {
1038 memcpy(&sh, args.buf + off, sizeof(sh));
1040 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1041 ref = (struct btrfs_root_ref *)(args.buf + off);
1042 name_len = btrfs_stack_root_ref_name_len(ref);
1043 name = (char *)(ref + 1);
1044 dir_id = btrfs_stack_root_ref_dirid(ref);
1046 add_root(root_lookup, sh.objectid, sh.offset,
1047 0, 0, dir_id, name, name_len, 0, 0, 0,
1049 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1050 ri = (struct btrfs_root_item *)(args.buf + off);
1051 gen = btrfs_root_generation(ri);
1052 flags = btrfs_root_flags(ri);
1054 sizeof(struct btrfs_root_item_v0)) {
1056 ogen = btrfs_root_otransid(ri);
1057 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1058 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1062 memset(uuid, 0, BTRFS_UUID_SIZE);
1063 memset(puuid, 0, BTRFS_UUID_SIZE);
1066 add_root(root_lookup, sh.objectid, 0,
1067 sh.offset, flags, 0, NULL, 0, ogen,
1068 gen, t, uuid, puuid);
1074 * record the mins in sk so we can make sure the
1075 * next search doesn't repeat this root
1077 sk->min_objectid = sh.objectid;
1078 sk->min_type = sh.type;
1079 sk->min_offset = sh.offset;
1081 sk->nr_items = 4096;
1083 if (!sk->min_offset) /* overflow */
1088 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1089 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1094 if (sk->min_objectid > sk->max_objectid)
1101 static int filter_by_rootid(struct root_info *ri, u64 data)
1103 return ri->root_id == data;
1106 static int filter_snapshot(struct root_info *ri, u64 data)
1108 return !!ri->root_offset;
1111 static int filter_flags(struct root_info *ri, u64 flags)
1113 return ri->flags & flags;
1116 static int filter_gen_more(struct root_info *ri, u64 data)
1118 return ri->gen >= data;
1121 static int filter_gen_less(struct root_info *ri, u64 data)
1123 return ri->gen <= data;
1126 static int filter_gen_equal(struct root_info *ri, u64 data)
1128 return ri->gen == data;
1131 static int filter_cgen_more(struct root_info *ri, u64 data)
1133 return ri->ogen >= data;
1136 static int filter_cgen_less(struct root_info *ri, u64 data)
1138 return ri->ogen <= data;
1141 static int filter_cgen_equal(struct root_info *ri, u64 data)
1143 return ri->ogen == data;
1146 static int filter_topid_equal(struct root_info *ri, u64 data)
1148 return ri->top_id == data;
1151 static int filter_full_path(struct root_info *ri, u64 data)
1153 if (ri->full_path && ri->top_id != data) {
1155 char p[] = "<FS_TREE>";
1156 int add_len = strlen(p);
1157 int len = strlen(ri->full_path);
1159 tmp = malloc(len + add_len + 2);
1161 fprintf(stderr, "memory allocation failed\n");
1164 memcpy(tmp + add_len + 1, ri->full_path, len);
1165 tmp[len + add_len + 1] = '\0';
1167 memcpy(tmp, p, add_len);
1168 free(ri->full_path);
1169 ri->full_path = tmp;
1174 static int filter_by_parent(struct root_info *ri, u64 data)
1176 return !uuid_compare(ri->puuid, (u8 *)data);
1179 static btrfs_list_filter_func all_filter_funcs[] = {
1180 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1181 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1182 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1183 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1184 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1185 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1186 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1187 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1188 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1189 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1190 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1191 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1194 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1196 struct btrfs_list_filter_set *set;
1199 size = sizeof(struct btrfs_list_filter_set) +
1200 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1203 fprintf(stderr, "memory allocation failed\n");
1207 memset(set, 0, size);
1208 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1213 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1218 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1219 enum btrfs_list_filter_enum filter, u64 data)
1221 struct btrfs_list_filter_set *set = *filter_set;
1225 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1226 BUG_ON(set->nfilters > set->total);
1228 if (set->nfilters == set->total) {
1229 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1230 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1231 set = realloc(set, size);
1233 fprintf(stderr, "memory allocation failed\n");
1237 memset(&set->filters[set->total], 0,
1238 BTRFS_LIST_NFILTERS_INCREASE *
1239 sizeof(struct btrfs_list_filter));
1240 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1244 BUG_ON(set->filters[set->nfilters].filter_func);
1246 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1247 set->filters[set->nfilters].data = data;
1252 static int filter_root(struct root_info *ri,
1253 struct btrfs_list_filter_set *set)
1257 if (!set || !set->nfilters)
1260 for (i = 0; i < set->nfilters; i++) {
1261 if (!set->filters[i].filter_func)
1263 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1270 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1271 struct root_lookup *sort_tree,
1272 struct btrfs_list_filter_set *filter_set,
1273 struct btrfs_list_comparer_set *comp_set,
1277 struct root_info *entry;
1280 root_lookup_init(sort_tree);
1282 n = rb_last(&all_subvols->root);
1284 entry = rb_entry(n, struct root_info, rb_node);
1286 ret = resolve_root(all_subvols, entry, top_id);
1289 ret = filter_root(entry, filter_set);
1291 sort_tree_insert(sort_tree, entry, comp_set);
1297 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1301 n = rb_first(&root_lookup->root);
1303 struct root_info *entry;
1305 entry = rb_entry(n, struct root_info, rb_node);
1306 ret = lookup_ino_path(fd, entry);
1307 if (ret && ret != -ENOENT)
1315 static void print_subvolume_column(struct root_info *subv,
1316 enum btrfs_list_column_enum column)
1321 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1324 case BTRFS_LIST_OBJECTID:
1325 printf("%llu", subv->root_id);
1327 case BTRFS_LIST_GENERATION:
1328 printf("%llu", subv->gen);
1330 case BTRFS_LIST_OGENERATION:
1331 printf("%llu", subv->ogen);
1333 case BTRFS_LIST_PARENT:
1334 printf("%llu", subv->ref_tree);
1336 case BTRFS_LIST_TOP_LEVEL:
1337 printf("%llu", subv->top_id);
1339 case BTRFS_LIST_OTIME:
1341 strftime(tstr, 256, "%Y-%m-%d %X",
1342 localtime(&subv->otime));
1347 case BTRFS_LIST_UUID:
1348 if (uuid_is_null(subv->uuid))
1349 strcpy(uuidparse, "-");
1351 uuid_unparse(subv->uuid, uuidparse);
1352 printf("%s", uuidparse);
1354 case BTRFS_LIST_PUUID:
1355 if (uuid_is_null(subv->puuid))
1356 strcpy(uuidparse, "-");
1358 uuid_unparse(subv->puuid, uuidparse);
1359 printf("%s", uuidparse);
1361 case BTRFS_LIST_PATH:
1362 BUG_ON(!subv->full_path);
1363 printf("%s", subv->full_path);
1370 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1374 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1375 if (!btrfs_list_columns[i].need_print)
1379 printf("%s",raw_prefix);
1381 print_subvolume_column(subv, i);
1386 static void print_single_volume_info_table(struct root_info *subv)
1390 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1391 if (!btrfs_list_columns[i].need_print)
1394 print_subvolume_column(subv, i);
1396 if (i != BTRFS_LIST_PATH)
1399 if (i == BTRFS_LIST_TOP_LEVEL)
1405 static void print_single_volume_info_default(struct root_info *subv)
1409 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1410 if (!btrfs_list_columns[i].need_print)
1413 printf("%s ", btrfs_list_columns[i].name);
1414 print_subvolume_column(subv, i);
1416 if (i != BTRFS_LIST_PATH)
1422 static void print_all_volume_info_tab_head()
1428 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1429 if (btrfs_list_columns[i].need_print)
1430 printf("%s\t", btrfs_list_columns[i].name);
1432 if (i == BTRFS_LIST_ALL-1)
1436 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1437 memset(barrier, 0, sizeof(barrier));
1439 if (btrfs_list_columns[i].need_print) {
1440 len = strlen(btrfs_list_columns[i].name);
1442 strcat(barrier, "-");
1444 printf("%s\t", barrier);
1446 if (i == BTRFS_LIST_ALL-1)
1451 static void print_all_volume_info(struct root_lookup *sorted_tree,
1452 int layout, char *raw_prefix)
1455 struct root_info *entry;
1457 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1458 print_all_volume_info_tab_head();
1460 n = rb_first(&sorted_tree->root);
1462 entry = rb_entry(n, struct root_info, sort_node);
1464 case BTRFS_LIST_LAYOUT_DEFAULT:
1465 print_single_volume_info_default(entry);
1467 case BTRFS_LIST_LAYOUT_TABLE:
1468 print_single_volume_info_table(entry);
1470 case BTRFS_LIST_LAYOUT_RAW:
1471 print_single_volume_info_raw(entry, raw_prefix);
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 layout, int full_path, char *raw_prefix)
1501 struct root_lookup root_lookup;
1502 struct root_lookup root_sort;
1507 ret = btrfs_list_get_path_rootid(fd, &top_id);
1511 ret = btrfs_list_subvols(fd, &root_lookup);
1514 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1517 print_all_volume_info(&root_sort, layout, raw_prefix);
1518 __free_all_subvolumn(&root_lookup);
1523 char *strdup_or_null(const char *s)
1530 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1533 struct root_lookup rl;
1534 struct rb_node *rbn;
1535 struct root_info *ri;
1538 ret = btrfs_list_get_path_rootid(fd, &root_id);
1542 ret = btrfs_list_subvols(fd, &rl);
1546 rbn = rb_first(&rl.root);
1548 ri = rb_entry(rbn, struct root_info, rb_node);
1549 rr = resolve_root(&rl, ri, root_id);
1550 if (rr == -ENOENT) {
1555 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1556 memcpy(the_ri, ri, offsetof(struct root_info, path));
1557 the_ri->path = strdup_or_null(ri->path);
1558 the_ri->name = strdup_or_null(ri->name);
1559 the_ri->full_path = strdup_or_null(ri->full_path);
1565 __free_all_subvolumn(&rl);
1569 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1570 struct btrfs_file_extent_item *item,
1571 u64 found_gen, u64 *cache_dirid,
1572 char **cache_dir_name, u64 *cache_ino,
1573 char **cache_full_name)
1577 u64 disk_offset = 0;
1583 if (sh->objectid == *cache_ino) {
1584 name = *cache_full_name;
1585 } else if (*cache_full_name) {
1586 free(*cache_full_name);
1587 *cache_full_name = NULL;
1590 name = ino_resolve(fd, sh->objectid, cache_dirid,
1592 *cache_full_name = name;
1593 *cache_ino = sh->objectid;
1598 type = btrfs_stack_file_extent_type(item);
1599 compressed = btrfs_stack_file_extent_compression(item);
1601 if (type == BTRFS_FILE_EXTENT_REG ||
1602 type == BTRFS_FILE_EXTENT_PREALLOC) {
1603 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1604 disk_offset = btrfs_stack_file_extent_offset(item);
1605 len = btrfs_stack_file_extent_num_bytes(item);
1606 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1609 len = btrfs_stack_file_extent_ram_bytes(item);
1611 printf("unhandled extent type %d for inode %llu "
1612 "file offset %llu gen %llu\n",
1614 (unsigned long long)sh->objectid,
1615 (unsigned long long)sh->offset,
1616 (unsigned long long)found_gen);
1620 printf("inode %llu file offset %llu len %llu disk start %llu "
1621 "offset %llu gen %llu flags ",
1622 (unsigned long long)sh->objectid,
1623 (unsigned long long)sh->offset,
1624 (unsigned long long)len,
1625 (unsigned long long)disk_start,
1626 (unsigned long long)disk_offset,
1627 (unsigned long long)found_gen);
1633 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1634 printf("%sPREALLOC", flags ? "|" : "");
1637 if (type == BTRFS_FILE_EXTENT_INLINE) {
1638 printf("%sINLINE", flags ? "|" : "");
1644 printf(" %s\n", name);
1648 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1651 struct btrfs_ioctl_search_args args;
1652 struct btrfs_ioctl_search_key *sk = &args.key;
1653 struct btrfs_ioctl_search_header sh;
1654 struct btrfs_file_extent_item *item;
1655 unsigned long off = 0;
1660 u64 cache_dirid = 0;
1662 char *cache_dir_name = NULL;
1663 char *cache_full_name = NULL;
1664 struct btrfs_file_extent_item backup;
1666 memset(&backup, 0, sizeof(backup));
1667 memset(&args, 0, sizeof(args));
1669 sk->tree_id = root_id;
1672 * set all the other params to the max, we'll take any objectid
1675 sk->max_objectid = (u64)-1;
1676 sk->max_offset = (u64)-1;
1677 sk->max_transid = (u64)-1;
1678 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1679 sk->min_transid = oldest_gen;
1680 /* just a big number, doesn't matter much */
1681 sk->nr_items = 4096;
1683 max_found = find_root_gen(fd);
1685 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1688 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1692 /* the ioctl returns the number of item it found in nr_items */
1693 if (sk->nr_items == 0)
1699 * for each item, pull the key out of the header and then
1700 * read the root_ref item it contains
1702 for (i = 0; i < sk->nr_items; i++) {
1703 memcpy(&sh, args.buf + off, sizeof(sh));
1707 * just in case the item was too big, pass something other
1713 item = (struct btrfs_file_extent_item *)(args.buf +
1715 found_gen = btrfs_stack_file_extent_generation(item);
1716 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1717 found_gen >= oldest_gen) {
1718 print_one_extent(fd, &sh, item, found_gen,
1719 &cache_dirid, &cache_dir_name,
1720 &cache_ino, &cache_full_name);
1725 * record the mins in sk so we can make sure the
1726 * next search doesn't repeat this root
1728 sk->min_objectid = sh.objectid;
1729 sk->min_offset = sh.offset;
1730 sk->min_type = sh.type;
1732 sk->nr_items = 4096;
1733 if (sk->min_offset < (u64)-1)
1735 else if (sk->min_objectid < (u64)-1) {
1742 free(cache_dir_name);
1743 free(cache_full_name);
1744 printf("transid marker was %llu\n", (unsigned long long)max_found);
1748 char *btrfs_list_path_for_root(int fd, u64 root)
1750 struct root_lookup root_lookup;
1752 char *ret_path = NULL;
1756 ret = btrfs_list_get_path_rootid(fd, &top_id);
1758 return ERR_PTR(ret);
1760 ret = __list_subvol_search(fd, &root_lookup);
1762 return ERR_PTR(ret);
1764 ret = __list_subvol_fill_paths(fd, &root_lookup);
1766 return ERR_PTR(ret);
1768 n = rb_last(&root_lookup.root);
1770 struct root_info *entry;
1772 entry = rb_entry(n, struct root_info, rb_node);
1773 ret = resolve_root(&root_lookup, entry, top_id);
1774 if (ret == -ENOENT && entry->root_id == root) {
1778 if (entry->root_id == root) {
1779 ret_path = entry->full_path;
1780 entry->full_path = NULL;
1785 __free_all_subvolumn(&root_lookup);
1790 int btrfs_list_parse_sort_string(char *optarg,
1791 struct btrfs_list_comparer_set **comps)
1799 while ((p = strtok(optarg, ",")) != NULL) {
1801 ptr_argv = all_sort_items;
1804 if (strcmp(*ptr_argv, p) == 0) {
1809 if (strcmp(*ptr_argv, p) == 0) {
1826 } else if (*p == '-') {
1832 what_to_sort = btrfs_list_get_sort_item(p);
1833 btrfs_list_setup_comparer(comps, what_to_sort, order);
1842 * This function is used to parse the argument of filter condition.
1844 * type is the filter object.
1846 int btrfs_list_parse_filter_string(char *optarg,
1847 struct btrfs_list_filter_set **filters,
1848 enum btrfs_list_filter_enum type)
1852 char *ptr_parse_end = NULL;
1853 char *ptr_optarg_end = optarg + strlen(optarg);
1855 switch (*(optarg++)) {
1857 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1859 if (ptr_parse_end != ptr_optarg_end)
1862 btrfs_list_setup_filter(filters, type, arg);
1865 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1867 if (ptr_parse_end != ptr_optarg_end)
1870 btrfs_list_setup_filter(filters, type, arg);
1874 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1876 if (ptr_parse_end != ptr_optarg_end)
1878 btrfs_list_setup_filter(filters, type, arg);
1885 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1888 struct btrfs_ioctl_ino_lookup_args args;
1890 memset(&args, 0, sizeof(args));
1891 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1893 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1896 "ERROR: can't perform the search -%s\n",
1900 *treeid = args.treeid;