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.
19 #include <sys/ioctl.h>
20 #include <sys/mount.h>
23 #include <sys/types.h>
30 #include "transaction.h"
33 #include <uuid/uuid.h>
34 #include "btrfs-list.h"
35 #include "rbtree-utils.h"
37 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
38 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
40 /* we store all the roots we find in an rbtree so that we can
41 * search for them later.
51 } btrfs_list_columns[] = {
64 .column_name = "CGen",
69 .column_name = "Parent",
74 .column_name = "Top Level",
79 .column_name = "OTime",
83 .name = "parent_uuid",
84 .column_name = "Parent UUID",
88 .name = "received_uuid",
89 .column_name = "Received UUID",
94 .column_name = "UUID",
99 .column_name = "Path",
109 static btrfs_list_filter_func all_filter_funcs[];
110 static btrfs_list_comp_func all_comp_funcs[];
112 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
116 ASSERT(0 <= column && column <= BTRFS_LIST_ALL);
118 if (column < BTRFS_LIST_ALL) {
119 btrfs_list_columns[column].need_print = 1;
123 for (i = 0; i < BTRFS_LIST_ALL; i++)
124 btrfs_list_columns[i].need_print = 1;
127 static void root_lookup_init(struct root_lookup *tree)
129 tree->root.rb_node = NULL;
132 static int comp_entry_with_rootid(struct root_info *entry1,
133 struct root_info *entry2,
138 if (entry1->root_id > entry2->root_id)
140 else if (entry1->root_id < entry2->root_id)
145 return is_descending ? -ret : ret;
148 static int comp_entry_with_gen(struct root_info *entry1,
149 struct root_info *entry2,
154 if (entry1->gen > entry2->gen)
156 else if (entry1->gen < entry2->gen)
161 return is_descending ? -ret : ret;
164 static int comp_entry_with_ogen(struct root_info *entry1,
165 struct root_info *entry2,
170 if (entry1->ogen > entry2->ogen)
172 else if (entry1->ogen < entry2->ogen)
177 return is_descending ? -ret : ret;
180 static int comp_entry_with_path(struct root_info *entry1,
181 struct root_info *entry2,
186 if (strcmp(entry1->full_path, entry2->full_path) > 0)
188 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
193 return is_descending ? -ret : ret;
196 static btrfs_list_comp_func all_comp_funcs[] = {
197 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
198 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
199 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
200 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
203 static char *all_sort_items[] = {
204 [BTRFS_LIST_COMP_ROOTID] = "rootid",
205 [BTRFS_LIST_COMP_OGEN] = "ogen",
206 [BTRFS_LIST_COMP_GEN] = "gen",
207 [BTRFS_LIST_COMP_PATH] = "path",
208 [BTRFS_LIST_COMP_MAX] = NULL,
211 static int btrfs_list_get_sort_item(char *sort_name)
215 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
216 if (strcmp(sort_name, all_sort_items[i]) == 0)
222 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
224 struct btrfs_list_comparer_set *set;
227 size = sizeof(struct btrfs_list_comparer_set) +
228 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
229 set = calloc(1, size);
231 fprintf(stderr, "memory allocation failed\n");
235 set->total = BTRFS_LIST_NCOMPS_INCREASE;
240 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
241 enum btrfs_list_comp_enum comparer, int is_descending)
243 struct btrfs_list_comparer_set *set = *comp_set;
247 ASSERT(comparer < BTRFS_LIST_COMP_MAX);
248 ASSERT(set->ncomps <= set->total);
250 if (set->ncomps == set->total) {
253 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
254 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
256 set = realloc(set, size);
258 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 ASSERT(set->comps[set->ncomps].comp_func == NULL);
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 otime, u8 *uuid, u8 *puuid, u8 *ruuid)
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) {
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);
436 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
442 * add_root - update the existed root, or allocate a new root and insert it
443 * into the lookup tree.
444 * root_id: object id of the root
445 * ref_tree: object id of the referring root.
446 * root_offset: offset value of the root'key
447 * dir_id: inode id of the directory in ref_tree where this root can be found.
448 * name: the name of root_id in that directory
449 * name_len: the length of name
450 * ogen: the original generation of the root
451 * gen: the current generation of the root
452 * otime: the original time (creation time) of the root
453 * uuid: uuid of the root
454 * puuid: uuid of the root parent if any
455 * ruuid: uuid of the received subvol, if any
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 otime, u8 *uuid, u8 *puuid, u8 *ruuid)
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, otime,
471 ri = calloc(1, sizeof(*ri));
473 printf("memory allocation failed\n");
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);
508 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
511 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
513 ret = root_tree_insert(root_lookup, ri);
515 error("failed to insert tree %llu",
516 (unsigned long long)root_id);
523 * Simplified add_root for back references, omits the uuid and original info
524 * parameters, root offset and flags.
526 static int add_root_backref(struct root_lookup *root_lookup, u64 root_id,
527 u64 ref_tree, u64 dir_id, char *name, int name_len)
529 return add_root(root_lookup, root_id, ref_tree, 0, 0, dir_id, name,
530 name_len, 0, 0, 0, NULL, NULL, NULL);
534 static void free_root_info(struct rb_node *node)
536 struct root_info *ri;
538 ri = rb_entry(node, struct root_info, rb_node);
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.
570 * ref_tree = 0 indicates the subvolume
573 if (!found->ref_tree) {
578 add_len = strlen(found->path);
581 /* room for / and for null */
582 tmp = malloc(add_len + 2 + len);
584 perror("malloc failed");
587 memcpy(tmp + add_len + 1, full_path, len);
589 memcpy(tmp, found->path, add_len);
590 tmp [add_len + len + 1] = '\0';
595 full_path = strdup(found->path);
599 ri->top_id = found->ref_tree;
601 next = found->ref_tree;
605 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
608 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);
621 ri->full_path = full_path;
627 * for a single root_info, ask the kernel to give us a path name
628 * inside it's ref_root for the dir_id where it lives.
630 * This fills in root_info->path with the path to the directory and and
631 * appends this root's name.
633 static int lookup_ino_path(int fd, struct root_info *ri)
635 struct btrfs_ioctl_ino_lookup_args args;
644 memset(&args, 0, sizeof(args));
645 args.treeid = ri->ref_tree;
646 args.objectid = ri->dir_id;
648 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
650 if (errno == ENOENT) {
654 error("failed to lookup path for root %llu: %s",
655 (unsigned long long)ri->ref_tree, strerror(errno));
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 lookup 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;
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);
705 error("failed to lookup path for dirid %llu: %s",
706 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
711 memset(&args, 0, sizeof(args));
713 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
716 * there may be more than one ROOT_ITEM key if there are
717 * snapshots pending deletion, we have to loop through
720 sk->min_objectid = ino_args.treeid;
721 sk->max_objectid = ino_args.treeid;
722 sk->max_type = BTRFS_ROOT_ITEM_KEY;
723 sk->min_type = BTRFS_ROOT_ITEM_KEY;
724 sk->max_offset = (u64)-1;
725 sk->max_transid = (u64)-1;
729 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
731 error("can't perform the search: %s", strerror(errno));
734 /* the ioctl returns the number of item it found in nr_items */
735 if (sk->nr_items == 0)
739 for (i = 0; i < sk->nr_items; i++) {
740 struct btrfs_root_item *item;
742 memcpy(&sh, args.buf + off, sizeof(sh));
744 item = (struct btrfs_root_item *)(args.buf + off);
747 sk->min_objectid = sh.objectid;
748 sk->min_type = sh.type;
749 sk->min_offset = sh.offset;
751 if (sh.objectid > ino_args.treeid)
754 if (sh.objectid == ino_args.treeid &&
755 sh.type == BTRFS_ROOT_ITEM_KEY) {
756 max_found = max(max_found,
757 btrfs_root_generation(item));
760 if (sk->min_offset < (u64)-1)
765 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
767 if (sk->min_objectid != ino_args.treeid)
773 /* pass in a directory id and this will return
774 * the full path of the parent directory inside its
777 * It may return NULL if it is in the root, or an ERR_PTR if things
780 static char *__ino_resolve(int fd, u64 dirid)
782 struct btrfs_ioctl_ino_lookup_args args;
786 memset(&args, 0, sizeof(args));
787 args.objectid = dirid;
789 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
791 error("failed to lookup path for dirid %llu: %s",
792 (unsigned long long)dirid, strerror(errno));
798 * we're in a subdirectory of ref_tree, the kernel ioctl
799 * puts a / in there for us
801 full = strdup(args.name);
803 perror("malloc failed");
804 return ERR_PTR(-ENOMEM);
807 /* we're at the root of ref_tree */
814 * simple string builder, returning a new string with both
817 static char *build_name(const char *dirid, const char *name)
824 full = malloc(strlen(dirid) + strlen(name) + 1);
833 * given an inode number, this returns the full path name inside the subvolume
834 * to that file/directory. cache_dirid and cache_name are used to
835 * cache the results so we can avoid tree searches if a later call goes
836 * to the same directory or file name
838 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
846 struct btrfs_ioctl_search_args args;
847 struct btrfs_ioctl_search_key *sk = &args.key;
848 struct btrfs_ioctl_search_header *sh;
849 unsigned long off = 0;
852 memset(&args, 0, sizeof(args));
857 * step one, we search for the inode back ref. We just use the first
860 sk->min_objectid = ino;
861 sk->max_objectid = ino;
862 sk->max_type = BTRFS_INODE_REF_KEY;
863 sk->max_offset = (u64)-1;
864 sk->min_type = BTRFS_INODE_REF_KEY;
865 sk->max_transid = (u64)-1;
868 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
870 error("can't perform the search: %s", strerror(errno));
873 /* the ioctl returns the number of item it found in nr_items */
874 if (sk->nr_items == 0)
878 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
880 if (btrfs_search_header_type(sh) == BTRFS_INODE_REF_KEY) {
881 struct btrfs_inode_ref *ref;
882 dirid = btrfs_search_header_offset(sh);
884 ref = (struct btrfs_inode_ref *)(sh + 1);
885 namelen = btrfs_stack_inode_ref_name_len(ref);
887 name = (char *)(ref + 1);
888 name = strndup(name, namelen);
890 /* use our cached value */
891 if (dirid == *cache_dirid && *cache_name) {
892 dirname = *cache_name;
899 * the inode backref gives us the file name and the parent directory id.
900 * From here we use __ino_resolve to get the path to the parent
902 dirname = __ino_resolve(fd, dirid);
904 full = build_name(dirname, name);
905 if (*cache_name && dirname != *cache_name)
908 *cache_name = dirname;
909 *cache_dirid = dirid;
915 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
917 struct btrfs_ioctl_search_args args;
918 struct btrfs_ioctl_search_key *sk = &args.key;
919 struct btrfs_ioctl_search_header *sh;
923 memset(&args, 0, sizeof(args));
926 * search for a dir item with a name 'default' in the tree of
927 * tree roots, it should point us to a default root
929 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
931 /* don't worry about ancient format and request only one item */
934 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
935 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
936 sk->max_type = BTRFS_DIR_ITEM_KEY;
937 sk->min_type = BTRFS_DIR_ITEM_KEY;
938 sk->max_offset = (u64)-1;
939 sk->max_transid = (u64)-1;
941 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
945 /* the ioctl returns the number of items it found in nr_items */
946 if (sk->nr_items == 0)
949 sh = (struct btrfs_ioctl_search_header *)args.buf;
951 if (btrfs_search_header_type(sh) == BTRFS_DIR_ITEM_KEY) {
952 struct btrfs_dir_item *di;
956 di = (struct btrfs_dir_item *)(sh + 1);
957 name_len = btrfs_stack_dir_name_len(di);
958 name = (char *)(di + 1);
960 if (!strncmp("default", name, name_len))
961 found = btrfs_disk_key_objectid(&di->location);
969 static int list_subvol_search(int fd, struct root_lookup *root_lookup)
972 struct btrfs_ioctl_search_args args;
973 struct btrfs_ioctl_search_key *sk = &args.key;
974 struct btrfs_ioctl_search_header sh;
975 struct btrfs_root_ref *ref;
976 struct btrfs_root_item *ri;
986 root_lookup_init(root_lookup);
987 memset(&args, 0, sizeof(args));
989 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
990 /* Search both live and deleted subvolumes */
991 sk->min_type = BTRFS_ROOT_ITEM_KEY;
992 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
993 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
994 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
995 sk->max_offset = (u64)-1;
996 sk->max_transid = (u64)-1;
1000 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1003 if (sk->nr_items == 0)
1009 * for each item, pull the key out of the header and then
1010 * read the root_ref item it contains
1012 for (i = 0; i < sk->nr_items; i++) {
1013 memcpy(&sh, args.buf + off, sizeof(sh));
1015 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1016 ref = (struct btrfs_root_ref *)(args.buf + off);
1017 name_len = btrfs_stack_root_ref_name_len(ref);
1018 name = (char *)(ref + 1);
1019 dir_id = btrfs_stack_root_ref_dirid(ref);
1021 add_root_backref(root_lookup, sh.objectid,
1022 sh.offset, dir_id, name,
1024 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1026 u8 uuid[BTRFS_UUID_SIZE];
1027 u8 puuid[BTRFS_UUID_SIZE];
1028 u8 ruuid[BTRFS_UUID_SIZE];
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)) {
1035 otime = btrfs_stack_timespec_sec(&ri->otime);
1036 ogen = btrfs_root_otransid(ri);
1037 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1038 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1039 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1043 memset(uuid, 0, BTRFS_UUID_SIZE);
1044 memset(puuid, 0, BTRFS_UUID_SIZE);
1045 memset(ruuid, 0, BTRFS_UUID_SIZE);
1048 add_root(root_lookup, sh.objectid, 0,
1049 sh.offset, flags, 0, NULL, 0, ogen,
1050 gen, otime, uuid, puuid, ruuid);
1055 sk->min_objectid = sh.objectid;
1056 sk->min_type = sh.type;
1057 sk->min_offset = sh.offset;
1060 if (!sk->min_offset)
1065 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1066 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1071 if (sk->min_objectid > sk->max_objectid)
1078 static int filter_by_rootid(struct root_info *ri, u64 data)
1080 return ri->root_id == data;
1083 static int filter_snapshot(struct root_info *ri, u64 data)
1085 return !!ri->root_offset;
1088 static int filter_flags(struct root_info *ri, u64 flags)
1090 return ri->flags & flags;
1093 static int filter_gen_more(struct root_info *ri, u64 data)
1095 return ri->gen >= data;
1098 static int filter_gen_less(struct root_info *ri, u64 data)
1100 return ri->gen <= data;
1103 static int filter_gen_equal(struct root_info *ri, u64 data)
1105 return ri->gen == data;
1108 static int filter_cgen_more(struct root_info *ri, u64 data)
1110 return ri->ogen >= data;
1113 static int filter_cgen_less(struct root_info *ri, u64 data)
1115 return ri->ogen <= data;
1118 static int filter_cgen_equal(struct root_info *ri, u64 data)
1120 return ri->ogen == data;
1123 static int filter_topid_equal(struct root_info *ri, u64 data)
1125 return ri->top_id == data;
1128 static int filter_full_path(struct root_info *ri, u64 data)
1130 if (ri->full_path && ri->top_id != data) {
1132 char p[] = "<FS_TREE>";
1133 int add_len = strlen(p);
1134 int len = strlen(ri->full_path);
1136 tmp = malloc(len + add_len + 2);
1138 fprintf(stderr, "memory allocation failed\n");
1141 memcpy(tmp + add_len + 1, ri->full_path, len);
1142 tmp[len + add_len + 1] = '\0';
1144 memcpy(tmp, p, add_len);
1145 free(ri->full_path);
1146 ri->full_path = tmp;
1151 static int filter_by_parent(struct root_info *ri, u64 data)
1153 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1156 static int filter_deleted(struct root_info *ri, u64 data)
1161 static btrfs_list_filter_func all_filter_funcs[] = {
1162 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1163 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1164 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1165 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1166 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1167 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1168 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1169 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1170 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1171 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1172 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1173 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1174 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1177 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1179 struct btrfs_list_filter_set *set;
1182 size = sizeof(struct btrfs_list_filter_set) +
1183 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1184 set = calloc(1, size);
1186 fprintf(stderr, "memory allocation failed\n");
1190 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1196 * Setup list filters. Exit if there's not enough memory, as we can't continue
1197 * without the structures set up properly.
1199 void btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1200 enum btrfs_list_filter_enum filter, u64 data)
1202 struct btrfs_list_filter_set *set = *filter_set;
1205 ASSERT(set != NULL);
1206 ASSERT(filter < BTRFS_LIST_FILTER_MAX);
1207 ASSERT(set->nfilters <= set->total);
1209 if (set->nfilters == set->total) {
1212 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1213 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1215 set = realloc(set, size);
1217 fprintf(stderr, "memory allocation failed\n");
1222 memset(&set->filters[set->total], 0,
1223 BTRFS_LIST_NFILTERS_INCREASE *
1224 sizeof(struct btrfs_list_filter));
1225 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1229 ASSERT(set->filters[set->nfilters].filter_func == NULL);
1231 if (filter == BTRFS_LIST_FILTER_DELETED)
1232 set->only_deleted = 1;
1234 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1235 set->filters[set->nfilters].data = data;
1239 static int filter_root(struct root_info *ri,
1240 struct btrfs_list_filter_set *set)
1247 if (set->only_deleted && !ri->deleted)
1250 if (!set->only_deleted && ri->deleted)
1253 for (i = 0; i < set->nfilters; i++) {
1254 if (!set->filters[i].filter_func)
1256 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1263 static void filter_and_sort_subvol(struct root_lookup *all_subvols,
1264 struct root_lookup *sort_tree,
1265 struct btrfs_list_filter_set *filter_set,
1266 struct btrfs_list_comparer_set *comp_set,
1270 struct root_info *entry;
1273 root_lookup_init(sort_tree);
1275 n = rb_last(&all_subvols->root);
1277 entry = rb_entry(n, struct root_info, rb_node);
1279 ret = resolve_root(all_subvols, entry, top_id);
1280 if (ret == -ENOENT) {
1281 entry->full_path = strdup("DELETED");
1284 ret = filter_root(entry, filter_set);
1286 sort_tree_insert(sort_tree, entry, comp_set);
1291 static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1295 n = rb_first(&root_lookup->root);
1297 struct root_info *entry;
1299 entry = rb_entry(n, struct root_info, rb_node);
1300 ret = lookup_ino_path(fd, entry);
1301 if (ret && ret != -ENOENT)
1309 static void print_subvolume_column(struct root_info *subv,
1310 enum btrfs_list_column_enum column)
1313 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1315 ASSERT(0 <= column && column < BTRFS_LIST_ALL);
1318 case BTRFS_LIST_OBJECTID:
1319 printf("%llu", subv->root_id);
1321 case BTRFS_LIST_GENERATION:
1322 printf("%llu", subv->gen);
1324 case BTRFS_LIST_OGENERATION:
1325 printf("%llu", subv->ogen);
1327 case BTRFS_LIST_PARENT:
1328 printf("%llu", subv->ref_tree);
1330 case BTRFS_LIST_TOP_LEVEL:
1331 printf("%llu", subv->top_id);
1333 case BTRFS_LIST_OTIME:
1337 localtime_r(&subv->otime, &tm);
1338 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1343 case BTRFS_LIST_UUID:
1344 if (uuid_is_null(subv->uuid))
1345 strcpy(uuidparse, "-");
1347 uuid_unparse(subv->uuid, uuidparse);
1348 printf("%s", uuidparse);
1350 case BTRFS_LIST_PUUID:
1351 if (uuid_is_null(subv->puuid))
1352 strcpy(uuidparse, "-");
1354 uuid_unparse(subv->puuid, uuidparse);
1355 printf("%s", uuidparse);
1357 case BTRFS_LIST_RUUID:
1358 if (uuid_is_null(subv->ruuid))
1359 strcpy(uuidparse, "-");
1361 uuid_unparse(subv->ruuid, uuidparse);
1362 printf("%s", uuidparse);
1364 case BTRFS_LIST_PATH:
1365 BUG_ON(!subv->full_path);
1366 printf("%s", subv->full_path);
1373 static void print_one_subvol_info_raw(struct root_info *subv,
1374 const char *raw_prefix)
1378 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1379 if (!btrfs_list_columns[i].need_print)
1383 printf("%s",raw_prefix);
1385 print_subvolume_column(subv, i);
1390 static void print_one_subvol_info_table(struct root_info *subv)
1394 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1395 if (!btrfs_list_columns[i].need_print)
1398 print_subvolume_column(subv, i);
1400 if (i != BTRFS_LIST_PATH)
1403 if (i == BTRFS_LIST_TOP_LEVEL)
1409 static void print_one_subvol_info_default(struct root_info *subv)
1413 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1414 if (!btrfs_list_columns[i].need_print)
1417 printf("%s ", btrfs_list_columns[i].name);
1418 print_subvolume_column(subv, i);
1420 if (i != BTRFS_LIST_PATH)
1426 static void print_all_subvol_info_tab_head(void)
1432 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1433 if (btrfs_list_columns[i].need_print)
1434 printf("%s\t", btrfs_list_columns[i].name);
1436 if (i == BTRFS_LIST_ALL-1)
1440 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1441 memset(barrier, 0, sizeof(barrier));
1443 if (btrfs_list_columns[i].need_print) {
1444 len = strlen(btrfs_list_columns[i].name);
1446 strcat(barrier, "-");
1448 printf("%s\t", barrier);
1450 if (i == BTRFS_LIST_ALL-1)
1455 static void print_all_subvol_info(struct root_lookup *sorted_tree,
1456 enum btrfs_list_layout layout, const char *raw_prefix)
1459 struct root_info *entry;
1461 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1462 print_all_subvol_info_tab_head();
1464 n = rb_first(&sorted_tree->root);
1466 entry = rb_entry(n, struct root_info, sort_node);
1468 case BTRFS_LIST_LAYOUT_DEFAULT:
1469 print_one_subvol_info_default(entry);
1471 case BTRFS_LIST_LAYOUT_TABLE:
1472 print_one_subvol_info_table(entry);
1474 case BTRFS_LIST_LAYOUT_RAW:
1475 print_one_subvol_info_raw(entry, raw_prefix);
1482 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1486 ret = list_subvol_search(fd, root_lookup);
1488 error("can't perform the search: %s", strerror(errno));
1493 * now we have an rbtree full of root_info objects, but we need to fill
1494 * in their path names within the subvol that is referencing each one.
1496 ret = list_subvol_fill_paths(fd, root_lookup);
1500 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1501 struct btrfs_list_comparer_set *comp_set,
1502 enum btrfs_list_layout layout, int full_path,
1503 const char *raw_prefix)
1505 struct root_lookup root_lookup;
1506 struct root_lookup root_sort;
1511 ret = btrfs_list_get_path_rootid(fd, &top_id);
1515 ret = btrfs_list_subvols(fd, &root_lookup);
1518 filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1521 print_all_subvol_info(&root_sort, layout, raw_prefix);
1522 rb_free_nodes(&root_lookup.root, free_root_info);
1527 static char *strdup_or_null(const char *s)
1534 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1537 struct root_lookup rl;
1538 struct rb_node *rbn;
1539 struct root_info *ri;
1542 ret = btrfs_list_get_path_rootid(fd, &root_id);
1546 ret = btrfs_list_subvols(fd, &rl);
1550 rbn = rb_first(&rl.root);
1552 ri = rb_entry(rbn, struct root_info, rb_node);
1553 rr = resolve_root(&rl, ri, root_id);
1554 if (rr == -ENOENT) {
1559 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1560 memcpy(the_ri, ri, offsetof(struct root_info, path));
1561 the_ri->path = strdup_or_null(ri->path);
1562 the_ri->name = strdup_or_null(ri->name);
1563 the_ri->full_path = strdup_or_null(ri->full_path);
1569 rb_free_nodes(&rl.root, free_root_info);
1573 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1574 struct btrfs_file_extent_item *item,
1575 u64 found_gen, u64 *cache_dirid,
1576 char **cache_dir_name, u64 *cache_ino,
1577 char **cache_full_name)
1581 u64 disk_offset = 0;
1587 if (btrfs_search_header_objectid(sh) == *cache_ino) {
1588 name = *cache_full_name;
1589 } else if (*cache_full_name) {
1590 free(*cache_full_name);
1591 *cache_full_name = NULL;
1594 name = ino_resolve(fd, btrfs_search_header_objectid(sh),
1597 *cache_full_name = name;
1598 *cache_ino = btrfs_search_header_objectid(sh);
1603 type = btrfs_stack_file_extent_type(item);
1604 compressed = btrfs_stack_file_extent_compression(item);
1606 if (type == BTRFS_FILE_EXTENT_REG ||
1607 type == BTRFS_FILE_EXTENT_PREALLOC) {
1608 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1609 disk_offset = btrfs_stack_file_extent_offset(item);
1610 len = btrfs_stack_file_extent_num_bytes(item);
1611 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1614 len = btrfs_stack_file_extent_ram_bytes(item);
1617 "unhandled extent type %d for inode %llu file offset %llu gen %llu",
1619 (unsigned long long)btrfs_search_header_objectid(sh),
1620 (unsigned long long)btrfs_search_header_offset(sh),
1621 (unsigned long long)found_gen);
1625 printf("inode %llu file offset %llu len %llu disk start %llu "
1626 "offset %llu gen %llu flags ",
1627 (unsigned long long)btrfs_search_header_objectid(sh),
1628 (unsigned long long)btrfs_search_header_offset(sh),
1629 (unsigned long long)len,
1630 (unsigned long long)disk_start,
1631 (unsigned long long)disk_offset,
1632 (unsigned long long)found_gen);
1638 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1639 printf("%sPREALLOC", flags ? "|" : "");
1642 if (type == BTRFS_FILE_EXTENT_INLINE) {
1643 printf("%sINLINE", flags ? "|" : "");
1649 printf(" %s\n", name);
1653 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1656 struct btrfs_ioctl_search_args args;
1657 struct btrfs_ioctl_search_key *sk = &args.key;
1658 struct btrfs_ioctl_search_header sh;
1659 struct btrfs_file_extent_item *item;
1660 unsigned long off = 0;
1664 u64 cache_dirid = 0;
1666 char *cache_dir_name = NULL;
1667 char *cache_full_name = NULL;
1668 struct btrfs_file_extent_item backup;
1670 memset(&backup, 0, sizeof(backup));
1671 memset(&args, 0, sizeof(args));
1673 sk->tree_id = root_id;
1676 * set all the other params to the max, we'll take any objectid
1679 sk->max_objectid = (u64)-1;
1680 sk->max_offset = (u64)-1;
1681 sk->max_transid = (u64)-1;
1682 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1683 sk->min_transid = oldest_gen;
1684 /* just a big number, doesn't matter much */
1685 sk->nr_items = 4096;
1687 max_found = find_root_gen(fd);
1689 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1691 error("can't perform the search: %s", strerror(errno));
1694 /* the ioctl returns the number of item it found in nr_items */
1695 if (sk->nr_items == 0)
1701 * for each item, pull the key out of the header and then
1702 * read the root_ref item it contains
1704 for (i = 0; i < sk->nr_items; i++) {
1705 memcpy(&sh, args.buf + off, sizeof(sh));
1709 * just in case the item was too big, pass something other
1715 item = (struct btrfs_file_extent_item *)(args.buf +
1717 found_gen = btrfs_stack_file_extent_generation(item);
1718 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1719 found_gen >= oldest_gen) {
1720 print_one_extent(fd, &sh, item, found_gen,
1721 &cache_dirid, &cache_dir_name,
1722 &cache_ino, &cache_full_name);
1727 * record the mins in sk so we can make sure the
1728 * next search doesn't repeat this root
1730 sk->min_objectid = sh.objectid;
1731 sk->min_offset = sh.offset;
1732 sk->min_type = sh.type;
1734 sk->nr_items = 4096;
1735 if (sk->min_offset < (u64)-1)
1737 else if (sk->min_objectid < (u64)-1) {
1744 free(cache_dir_name);
1745 free(cache_full_name);
1746 printf("transid marker was %llu\n", (unsigned long long)max_found);
1750 char *btrfs_list_path_for_root(int fd, u64 root)
1752 struct root_lookup root_lookup;
1754 char *ret_path = NULL;
1758 ret = btrfs_list_get_path_rootid(fd, &top_id);
1760 return ERR_PTR(ret);
1762 ret = list_subvol_search(fd, &root_lookup);
1764 return ERR_PTR(ret);
1766 ret = list_subvol_fill_paths(fd, &root_lookup);
1768 return ERR_PTR(ret);
1770 n = rb_last(&root_lookup.root);
1772 struct root_info *entry;
1774 entry = rb_entry(n, struct root_info, rb_node);
1775 ret = resolve_root(&root_lookup, entry, top_id);
1776 if (ret == -ENOENT && entry->root_id == root) {
1780 if (entry->root_id == root) {
1781 ret_path = entry->full_path;
1782 entry->full_path = NULL;
1787 rb_free_nodes(&root_lookup.root, free_root_info);
1792 int btrfs_list_parse_sort_string(char *opt_arg,
1793 struct btrfs_list_comparer_set **comps)
1801 while ((p = strtok(opt_arg, ",")) != NULL) {
1803 ptr_argv = all_sort_items;
1806 if (strcmp(*ptr_argv, p) == 0) {
1811 if (strcmp(*ptr_argv, p) == 0) {
1828 } else if (*p == '-') {
1834 what_to_sort = btrfs_list_get_sort_item(p);
1835 btrfs_list_setup_comparer(comps, what_to_sort, order);
1844 * This function is used to parse the argument of filter condition.
1846 * type is the filter object.
1848 int btrfs_list_parse_filter_string(char *opt_arg,
1849 struct btrfs_list_filter_set **filters,
1850 enum btrfs_list_filter_enum type)
1855 switch (*(opt_arg++)) {
1857 arg = arg_strtou64(opt_arg);
1860 btrfs_list_setup_filter(filters, type, arg);
1863 arg = arg_strtou64(opt_arg);
1866 btrfs_list_setup_filter(filters, type, arg);
1870 arg = arg_strtou64(opt_arg);
1872 btrfs_list_setup_filter(filters, type, arg);
1879 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1883 ret = lookup_path_rootid(fd, treeid);
1885 error("cannot resolve rootid for path: %s",