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.
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
25 #include <sys/types.h>
32 #include "transaction.h"
34 #include <uuid/uuid.h>
35 #include "btrfs-list.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 BUG_ON(column < 0 || 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);
231 fprintf(stderr, "memory allocation failed\n");
235 memset(set, 0, size);
236 set->total = BTRFS_LIST_NCOMPS_INCREASE;
241 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
246 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
247 enum btrfs_list_comp_enum comparer, int is_descending)
249 struct btrfs_list_comparer_set *set = *comp_set;
253 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
254 BUG_ON(set->ncomps > set->total);
256 if (set->ncomps == set->total) {
257 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
258 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
259 set = realloc(set, size);
261 fprintf(stderr, "memory allocation failed\n");
265 memset(&set->comps[set->total], 0,
266 BTRFS_LIST_NCOMPS_INCREASE *
267 sizeof(struct btrfs_list_comparer));
268 set->total += BTRFS_LIST_NCOMPS_INCREASE;
272 BUG_ON(set->comps[set->ncomps].comp_func);
274 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
275 set->comps[set->ncomps].is_descending = is_descending;
280 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
281 struct btrfs_list_comparer_set *set)
283 int rootid_compared = 0;
286 if (!set || !set->ncomps)
289 for (i = 0; i < set->ncomps; i++) {
290 if (!set->comps[i].comp_func)
293 ret = set->comps[i].comp_func(entry1, entry2,
294 set->comps[i].is_descending);
298 if (set->comps[i].comp_func == comp_entry_with_rootid)
302 if (!rootid_compared) {
304 ret = comp_entry_with_rootid(entry1, entry2, 0);
310 static int sort_tree_insert(struct root_lookup *sort_tree,
311 struct root_info *ins,
312 struct btrfs_list_comparer_set *comp_set)
314 struct rb_node **p = &sort_tree->root.rb_node;
315 struct rb_node *parent = NULL;
316 struct root_info *curr;
321 curr = rb_entry(parent, struct root_info, sort_node);
323 ret = sort_comp(ins, curr, comp_set);
332 rb_link_node(&ins->sort_node, parent, p);
333 rb_insert_color(&ins->sort_node, &sort_tree->root);
338 * insert a new root into the tree. returns the existing root entry
339 * if one is already there. Both root_id and ref_tree are used
342 static int root_tree_insert(struct root_lookup *root_tree,
343 struct root_info *ins)
345 struct rb_node **p = &root_tree->root.rb_node;
346 struct rb_node * parent = NULL;
347 struct root_info *curr;
352 curr = rb_entry(parent, struct root_info, rb_node);
354 ret = comp_entry_with_rootid(ins, curr, 0);
363 rb_link_node(&ins->rb_node, parent, p);
364 rb_insert_color(&ins->rb_node, &root_tree->root);
369 * find a given root id in the tree. We return the smallest one,
370 * rb_next can be used to move forward looking for more if required
372 static struct root_info *root_tree_search(struct root_lookup *root_tree,
375 struct rb_node *n = root_tree->root.rb_node;
376 struct root_info *entry;
377 struct root_info tmp;
380 tmp.root_id = root_id;
383 entry = rb_entry(n, struct root_info, rb_node);
385 ret = comp_entry_with_rootid(&tmp, entry, 0);
396 static int update_root(struct root_lookup *root_lookup,
397 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
398 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
399 time_t ot, void *uuid, void *puuid, void *ruuid)
401 struct root_info *ri;
403 ri = root_tree_search(root_lookup, root_id);
404 if (!ri || ri->root_id != root_id)
406 if (name && name_len > 0) {
409 ri->name = malloc(name_len + 1);
411 fprintf(stderr, "memory allocation failed\n");
414 strncpy(ri->name, name, name_len);
415 ri->name[name_len] = 0;
418 ri->ref_tree = ref_tree;
420 ri->root_offset = root_offset;
429 if (!ri->ogen && root_offset)
430 ri->ogen = root_offset;
434 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
436 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
438 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
444 * add_root - update the existed root, or allocate a new root and insert it
445 * into the lookup tree.
446 * root_id: object id of the root
447 * ref_tree: object id of the referring root.
448 * root_offset: offset value of the root'key
449 * dir_id: inode id of the directory in ref_tree where this root can be found.
450 * name: the name of root_id in that directory
451 * name_len: the length of name
452 * ogen: the original generation of the root
453 * gen: the current generation of the root
454 * ot: the original time(create time) of the root
455 * uuid: uuid of the root
456 * puuid: uuid of the root parent if any
457 * ruuid: uuid of the received subvol, if any
459 static int add_root(struct root_lookup *root_lookup,
460 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
461 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
462 time_t ot, void *uuid, void *puuid, void *ruuid)
464 struct root_info *ri;
467 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
468 dir_id, name, name_len, ogen, gen, ot,
473 ri = malloc(sizeof(*ri));
475 printf("memory allocation failed\n");
478 memset(ri, 0, sizeof(*ri));
479 ri->root_id = root_id;
481 if (name && name_len > 0) {
482 ri->name = malloc(name_len + 1);
484 fprintf(stderr, "memory allocation failed\n");
487 strncpy(ri->name, name, name_len);
488 ri->name[name_len] = 0;
491 ri->ref_tree = ref_tree;
495 ri->root_offset = root_offset;
502 if (!ri->ogen && root_offset)
503 ri->ogen = root_offset;
508 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
511 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
514 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
516 ret = root_tree_insert(root_lookup, ri);
518 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
524 static void __free_root_info(struct rb_node *node)
526 struct root_info *ri;
528 ri = rb_entry(node, struct root_info, rb_node);
535 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
537 rb_free_nodes(&root_tree->root, __free_root_info);
541 * for a given root_info, search through the root_lookup tree to construct
542 * the full path name to it.
544 * This can't be called until all the root_info->path fields are filled
545 * in by lookup_ino_path
547 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
550 char *full_path = NULL;
552 struct root_info *found;
555 * we go backwards from the root_info object and add pathnames
556 * from parent directories as we go.
565 * ref_tree = 0 indicates the subvolumes
568 if (!found->ref_tree) {
573 add_len = strlen(found->path);
576 /* room for / and for null */
577 tmp = malloc(add_len + 2 + len);
579 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 ri->top_id = found->ref_tree;
596 next = found->ref_tree;
600 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
603 if (next == BTRFS_FS_TREE_OBJECTID)
606 * if the ref_tree wasn't in our tree of roots, the
607 * subvolume was deleted.
609 found = root_tree_search(rl, next);
616 ri->full_path = full_path;
622 * for a single root_info, ask the kernel to give us a path name
623 * inside it's ref_root for the dir_id where it lives.
625 * This fills in root_info->path with the path to the directory and and
626 * appends this root's name.
628 static int lookup_ino_path(int fd, struct root_info *ri)
630 struct btrfs_ioctl_ino_lookup_args args;
639 memset(&args, 0, sizeof(args));
640 args.treeid = ri->ref_tree;
641 args.objectid = ri->dir_id;
643 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
650 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
651 (unsigned long long)ri->ref_tree,
658 * we're in a subdirectory of ref_tree, the kernel ioctl
659 * puts a / in there for us
661 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
663 perror("malloc failed");
666 strcpy(ri->path, args.name);
667 strcat(ri->path, ri->name);
669 /* we're at the root of ref_tree */
670 ri->path = strdup(ri->name);
672 perror("strdup failed");
679 /* finding the generation for a given path is a two step process.
680 * First we use the inode loookup routine to find out the root id
682 * Then we use the tree search ioctl to scan all the root items for a
683 * given root id and spit out the latest generation we can find
685 static u64 find_root_gen(int fd)
687 struct btrfs_ioctl_ino_lookup_args ino_args;
689 struct btrfs_ioctl_search_args args;
690 struct btrfs_ioctl_search_key *sk = &args.key;
691 struct btrfs_ioctl_search_header sh;
692 unsigned long off = 0;
697 memset(&ino_args, 0, sizeof(ino_args));
698 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
700 /* this ioctl fills in ino_args->treeid */
701 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
704 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
705 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
710 memset(&args, 0, sizeof(args));
715 * there may be more than one ROOT_ITEM key if there are
716 * snapshots pending deletion, we have to loop through
719 sk->min_objectid = ino_args.treeid;
720 sk->max_objectid = ino_args.treeid;
721 sk->max_type = BTRFS_ROOT_ITEM_KEY;
722 sk->min_type = BTRFS_ROOT_ITEM_KEY;
723 sk->max_offset = (u64)-1;
724 sk->max_transid = (u64)-1;
728 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
731 fprintf(stderr, "ERROR: can't perform the search - %s\n",
735 /* the ioctl returns the number of item it found in nr_items */
736 if (sk->nr_items == 0)
740 for (i = 0; i < sk->nr_items; i++) {
741 struct btrfs_root_item *item;
743 memcpy(&sh, args.buf + off, sizeof(sh));
745 item = (struct btrfs_root_item *)(args.buf + off);
748 sk->min_objectid = sh.objectid;
749 sk->min_type = sh.type;
750 sk->min_offset = sh.offset;
752 if (sh.objectid > ino_args.treeid)
755 if (sh.objectid == ino_args.treeid &&
756 sh.type == BTRFS_ROOT_ITEM_KEY) {
757 max_found = max(max_found,
758 btrfs_root_generation(item));
761 if (sk->min_offset < (u64)-1)
766 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
768 if (sk->min_objectid != ino_args.treeid)
774 /* pass in a directory id and this will return
775 * the full path of the parent directory inside its
778 * It may return NULL if it is in the root, or an ERR_PTR if things
781 static char *__ino_resolve(int fd, u64 dirid)
783 struct btrfs_ioctl_ino_lookup_args args;
788 memset(&args, 0, sizeof(args));
789 args.objectid = dirid;
791 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
794 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
795 (unsigned long long)dirid, strerror(e) );
801 * we're in a subdirectory of ref_tree, the kernel ioctl
802 * puts a / in there for us
804 full = strdup(args.name);
806 perror("malloc failed");
807 return ERR_PTR(-ENOMEM);
810 /* we're at the root of ref_tree */
817 * simple string builder, returning a new string with both
820 static char *build_name(char *dirid, char *name)
826 full = malloc(strlen(dirid) + strlen(name) + 1);
835 * given an inode number, this returns the full path name inside the subvolume
836 * to that file/directory. cache_dirid and cache_name are used to
837 * cache the results so we can avoid tree searches if a later call goes
838 * to the same directory or file name
840 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
848 struct btrfs_ioctl_search_args args;
849 struct btrfs_ioctl_search_key *sk = &args.key;
850 struct btrfs_ioctl_search_header *sh;
851 unsigned long off = 0;
855 memset(&args, 0, sizeof(args));
860 * step one, we search for the inode back ref. We just use the first
863 sk->min_objectid = ino;
864 sk->max_objectid = ino;
865 sk->max_type = BTRFS_INODE_REF_KEY;
866 sk->max_offset = (u64)-1;
867 sk->min_type = BTRFS_INODE_REF_KEY;
868 sk->max_transid = (u64)-1;
871 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
874 fprintf(stderr, "ERROR: can't perform the search - %s\n",
878 /* the ioctl returns the number of item it found in nr_items */
879 if (sk->nr_items == 0)
883 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
885 if (sh->type == BTRFS_INODE_REF_KEY) {
886 struct btrfs_inode_ref *ref;
889 ref = (struct btrfs_inode_ref *)(sh + 1);
890 namelen = btrfs_stack_inode_ref_name_len(ref);
892 name = (char *)(ref + 1);
893 name = strndup(name, namelen);
895 /* use our cached value */
896 if (dirid == *cache_dirid && *cache_name) {
897 dirname = *cache_name;
904 * the inode backref gives us the file name and the parent directory id.
905 * From here we use __ino_resolve to get the path to the parent
907 dirname = __ino_resolve(fd, dirid);
909 full = build_name(dirname, name);
910 if (*cache_name && dirname != *cache_name)
913 *cache_name = dirname;
914 *cache_dirid = dirid;
920 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
922 struct btrfs_ioctl_search_args args;
923 struct btrfs_ioctl_search_key *sk = &args.key;
924 struct btrfs_ioctl_search_header *sh;
928 memset(&args, 0, sizeof(args));
931 * search for a dir item with a name 'default' in the tree of
932 * tree roots, it should point us to a default root
936 /* don't worry about ancient format and request only one item */
939 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
940 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
941 sk->max_type = BTRFS_DIR_ITEM_KEY;
942 sk->min_type = BTRFS_DIR_ITEM_KEY;
943 sk->max_offset = (u64)-1;
944 sk->max_transid = (u64)-1;
946 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
950 /* the ioctl returns the number of items it found in nr_items */
951 if (sk->nr_items == 0)
954 sh = (struct btrfs_ioctl_search_header *)args.buf;
956 if (sh->type == BTRFS_DIR_ITEM_KEY) {
957 struct btrfs_dir_item *di;
961 di = (struct btrfs_dir_item *)(sh + 1);
962 name_len = btrfs_stack_dir_name_len(di);
963 name = (char *)(di + 1);
965 if (!strncmp("default", name, name_len))
966 found = btrfs_disk_key_objectid(&di->location);
974 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
977 struct btrfs_ioctl_search_args args;
978 struct btrfs_ioctl_search_key *sk = &args.key;
979 struct btrfs_ioctl_search_header sh;
980 struct btrfs_root_ref *ref;
981 struct btrfs_root_item *ri;
982 unsigned long off = 0;
991 u8 uuid[BTRFS_UUID_SIZE];
992 u8 puuid[BTRFS_UUID_SIZE];
993 u8 ruuid[BTRFS_UUID_SIZE];
995 root_lookup_init(root_lookup);
996 memset(&args, 0, sizeof(args));
998 /* search in the tree of tree roots */
1002 * set the min and max to backref keys. The search will
1003 * only send back this type of key now.
1005 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1006 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1008 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1011 * set all the other params to the max, we'll take any objectid
1014 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1015 sk->max_offset = (u64)-1;
1016 sk->max_transid = (u64)-1;
1018 /* just a big number, doesn't matter much */
1019 sk->nr_items = 4096;
1022 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1025 /* the ioctl returns the number of item it found in nr_items */
1026 if (sk->nr_items == 0)
1032 * for each item, pull the key out of the header and then
1033 * read the root_ref item it contains
1035 for (i = 0; i < sk->nr_items; i++) {
1036 memcpy(&sh, args.buf + off, sizeof(sh));
1038 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1039 ref = (struct btrfs_root_ref *)(args.buf + off);
1040 name_len = btrfs_stack_root_ref_name_len(ref);
1041 name = (char *)(ref + 1);
1042 dir_id = btrfs_stack_root_ref_dirid(ref);
1044 add_root(root_lookup, sh.objectid, sh.offset,
1045 0, 0, dir_id, name, name_len, 0, 0, 0,
1047 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1048 ri = (struct btrfs_root_item *)(args.buf + off);
1049 gen = btrfs_root_generation(ri);
1050 flags = btrfs_root_flags(ri);
1052 sizeof(struct btrfs_root_item_v0)) {
1053 t = btrfs_stack_timespec_sec(&ri->otime);
1054 ogen = btrfs_root_otransid(ri);
1055 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1056 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1057 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1061 memset(uuid, 0, BTRFS_UUID_SIZE);
1062 memset(puuid, 0, BTRFS_UUID_SIZE);
1063 memset(ruuid, 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, ruuid);
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 *)(unsigned long)data);
1179 static int filter_deleted(struct root_info *ri, u64 data)
1184 static btrfs_list_filter_func all_filter_funcs[] = {
1185 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1186 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1187 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1188 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1189 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1190 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1191 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1192 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1193 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1194 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1195 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1196 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1197 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1200 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1202 struct btrfs_list_filter_set *set;
1205 size = sizeof(struct btrfs_list_filter_set) +
1206 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1209 fprintf(stderr, "memory allocation failed\n");
1213 memset(set, 0, size);
1214 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1219 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1224 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1225 enum btrfs_list_filter_enum filter, u64 data)
1227 struct btrfs_list_filter_set *set = *filter_set;
1231 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1232 BUG_ON(set->nfilters > set->total);
1234 if (set->nfilters == set->total) {
1235 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1236 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1237 set = realloc(set, size);
1239 fprintf(stderr, "memory allocation failed\n");
1243 memset(&set->filters[set->total], 0,
1244 BTRFS_LIST_NFILTERS_INCREASE *
1245 sizeof(struct btrfs_list_filter));
1246 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1250 BUG_ON(set->filters[set->nfilters].filter_func);
1252 if (filter == BTRFS_LIST_FILTER_DELETED)
1253 set->only_deleted = 1;
1255 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1256 set->filters[set->nfilters].data = data;
1261 static int filter_root(struct root_info *ri,
1262 struct btrfs_list_filter_set *set)
1269 if (set->only_deleted && !ri->deleted)
1272 if (!set->only_deleted && ri->deleted)
1275 for (i = 0; i < set->nfilters; i++) {
1276 if (!set->filters[i].filter_func)
1278 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1285 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1286 struct root_lookup *sort_tree,
1287 struct btrfs_list_filter_set *filter_set,
1288 struct btrfs_list_comparer_set *comp_set,
1292 struct root_info *entry;
1295 root_lookup_init(sort_tree);
1297 n = rb_last(&all_subvols->root);
1299 entry = rb_entry(n, struct root_info, rb_node);
1301 ret = resolve_root(all_subvols, entry, top_id);
1302 if (ret == -ENOENT) {
1303 entry->full_path = strdup("DELETED");
1306 ret = filter_root(entry, filter_set);
1308 sort_tree_insert(sort_tree, entry, comp_set);
1313 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1317 n = rb_first(&root_lookup->root);
1319 struct root_info *entry;
1321 entry = rb_entry(n, struct root_info, rb_node);
1322 ret = lookup_ino_path(fd, entry);
1323 if (ret && ret != -ENOENT)
1331 static void print_subvolume_column(struct root_info *subv,
1332 enum btrfs_list_column_enum column)
1335 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1337 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1340 case BTRFS_LIST_OBJECTID:
1341 printf("%llu", subv->root_id);
1343 case BTRFS_LIST_GENERATION:
1344 printf("%llu", subv->gen);
1346 case BTRFS_LIST_OGENERATION:
1347 printf("%llu", subv->ogen);
1349 case BTRFS_LIST_PARENT:
1350 printf("%llu", subv->ref_tree);
1352 case BTRFS_LIST_TOP_LEVEL:
1353 printf("%llu", subv->top_id);
1355 case BTRFS_LIST_OTIME:
1359 localtime_r(&subv->otime, &tm);
1360 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1365 case BTRFS_LIST_UUID:
1366 if (uuid_is_null(subv->uuid))
1367 strcpy(uuidparse, "-");
1369 uuid_unparse(subv->uuid, uuidparse);
1370 printf("%s", uuidparse);
1372 case BTRFS_LIST_PUUID:
1373 if (uuid_is_null(subv->puuid))
1374 strcpy(uuidparse, "-");
1376 uuid_unparse(subv->puuid, uuidparse);
1377 printf("%s", uuidparse);
1379 case BTRFS_LIST_RUUID:
1380 if (uuid_is_null(subv->ruuid))
1381 strcpy(uuidparse, "-");
1383 uuid_unparse(subv->ruuid, uuidparse);
1384 printf("%s", uuidparse);
1386 case BTRFS_LIST_PATH:
1387 BUG_ON(!subv->full_path);
1388 printf("%s", subv->full_path);
1395 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1399 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1400 if (!btrfs_list_columns[i].need_print)
1404 printf("%s",raw_prefix);
1406 print_subvolume_column(subv, i);
1411 static void print_single_volume_info_table(struct root_info *subv)
1415 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1416 if (!btrfs_list_columns[i].need_print)
1419 print_subvolume_column(subv, i);
1421 if (i != BTRFS_LIST_PATH)
1424 if (i == BTRFS_LIST_TOP_LEVEL)
1430 static void print_single_volume_info_default(struct root_info *subv)
1434 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1435 if (!btrfs_list_columns[i].need_print)
1438 printf("%s ", btrfs_list_columns[i].name);
1439 print_subvolume_column(subv, i);
1441 if (i != BTRFS_LIST_PATH)
1447 static void print_all_volume_info_tab_head(void)
1453 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1454 if (btrfs_list_columns[i].need_print)
1455 printf("%s\t", btrfs_list_columns[i].name);
1457 if (i == BTRFS_LIST_ALL-1)
1461 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1462 memset(barrier, 0, sizeof(barrier));
1464 if (btrfs_list_columns[i].need_print) {
1465 len = strlen(btrfs_list_columns[i].name);
1467 strcat(barrier, "-");
1469 printf("%s\t", barrier);
1471 if (i == BTRFS_LIST_ALL-1)
1476 static void print_all_volume_info(struct root_lookup *sorted_tree,
1477 int layout, char *raw_prefix)
1480 struct root_info *entry;
1482 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1483 print_all_volume_info_tab_head();
1485 n = rb_first(&sorted_tree->root);
1487 entry = rb_entry(n, struct root_info, sort_node);
1489 case BTRFS_LIST_LAYOUT_DEFAULT:
1490 print_single_volume_info_default(entry);
1492 case BTRFS_LIST_LAYOUT_TABLE:
1493 print_single_volume_info_table(entry);
1495 case BTRFS_LIST_LAYOUT_RAW:
1496 print_single_volume_info_raw(entry, raw_prefix);
1503 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1507 ret = __list_subvol_search(fd, root_lookup);
1509 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1515 * now we have an rbtree full of root_info objects, but we need to fill
1516 * in their path names within the subvol that is referencing each one.
1518 ret = __list_subvol_fill_paths(fd, root_lookup);
1522 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1523 struct btrfs_list_comparer_set *comp_set,
1524 int layout, int full_path, char *raw_prefix)
1526 struct root_lookup root_lookup;
1527 struct root_lookup root_sort;
1532 ret = btrfs_list_get_path_rootid(fd, &top_id);
1536 ret = btrfs_list_subvols(fd, &root_lookup);
1539 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1542 print_all_volume_info(&root_sort, layout, raw_prefix);
1543 __free_all_subvolumn(&root_lookup);
1548 static char *strdup_or_null(const char *s)
1555 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1558 struct root_lookup rl;
1559 struct rb_node *rbn;
1560 struct root_info *ri;
1563 ret = btrfs_list_get_path_rootid(fd, &root_id);
1567 ret = btrfs_list_subvols(fd, &rl);
1571 rbn = rb_first(&rl.root);
1573 ri = rb_entry(rbn, struct root_info, rb_node);
1574 rr = resolve_root(&rl, ri, root_id);
1575 if (rr == -ENOENT) {
1580 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1581 memcpy(the_ri, ri, offsetof(struct root_info, path));
1582 the_ri->path = strdup_or_null(ri->path);
1583 the_ri->name = strdup_or_null(ri->name);
1584 the_ri->full_path = strdup_or_null(ri->full_path);
1590 __free_all_subvolumn(&rl);
1594 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1595 struct btrfs_file_extent_item *item,
1596 u64 found_gen, u64 *cache_dirid,
1597 char **cache_dir_name, u64 *cache_ino,
1598 char **cache_full_name)
1602 u64 disk_offset = 0;
1608 if (sh->objectid == *cache_ino) {
1609 name = *cache_full_name;
1610 } else if (*cache_full_name) {
1611 free(*cache_full_name);
1612 *cache_full_name = NULL;
1615 name = ino_resolve(fd, sh->objectid, cache_dirid,
1617 *cache_full_name = name;
1618 *cache_ino = sh->objectid;
1623 type = btrfs_stack_file_extent_type(item);
1624 compressed = btrfs_stack_file_extent_compression(item);
1626 if (type == BTRFS_FILE_EXTENT_REG ||
1627 type == BTRFS_FILE_EXTENT_PREALLOC) {
1628 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1629 disk_offset = btrfs_stack_file_extent_offset(item);
1630 len = btrfs_stack_file_extent_num_bytes(item);
1631 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1634 len = btrfs_stack_file_extent_ram_bytes(item);
1636 printf("unhandled extent type %d for inode %llu "
1637 "file offset %llu gen %llu\n",
1639 (unsigned long long)sh->objectid,
1640 (unsigned long long)sh->offset,
1641 (unsigned long long)found_gen);
1645 printf("inode %llu file offset %llu len %llu disk start %llu "
1646 "offset %llu gen %llu flags ",
1647 (unsigned long long)sh->objectid,
1648 (unsigned long long)sh->offset,
1649 (unsigned long long)len,
1650 (unsigned long long)disk_start,
1651 (unsigned long long)disk_offset,
1652 (unsigned long long)found_gen);
1658 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1659 printf("%sPREALLOC", flags ? "|" : "");
1662 if (type == BTRFS_FILE_EXTENT_INLINE) {
1663 printf("%sINLINE", flags ? "|" : "");
1669 printf(" %s\n", name);
1673 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1676 struct btrfs_ioctl_search_args args;
1677 struct btrfs_ioctl_search_key *sk = &args.key;
1678 struct btrfs_ioctl_search_header sh;
1679 struct btrfs_file_extent_item *item;
1680 unsigned long off = 0;
1685 u64 cache_dirid = 0;
1687 char *cache_dir_name = NULL;
1688 char *cache_full_name = NULL;
1689 struct btrfs_file_extent_item backup;
1691 memset(&backup, 0, sizeof(backup));
1692 memset(&args, 0, sizeof(args));
1694 sk->tree_id = root_id;
1697 * set all the other params to the max, we'll take any objectid
1700 sk->max_objectid = (u64)-1;
1701 sk->max_offset = (u64)-1;
1702 sk->max_transid = (u64)-1;
1703 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1704 sk->min_transid = oldest_gen;
1705 /* just a big number, doesn't matter much */
1706 sk->nr_items = 4096;
1708 max_found = find_root_gen(fd);
1710 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1713 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1717 /* the ioctl returns the number of item it found in nr_items */
1718 if (sk->nr_items == 0)
1724 * for each item, pull the key out of the header and then
1725 * read the root_ref item it contains
1727 for (i = 0; i < sk->nr_items; i++) {
1728 memcpy(&sh, args.buf + off, sizeof(sh));
1732 * just in case the item was too big, pass something other
1738 item = (struct btrfs_file_extent_item *)(args.buf +
1740 found_gen = btrfs_stack_file_extent_generation(item);
1741 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1742 found_gen >= oldest_gen) {
1743 print_one_extent(fd, &sh, item, found_gen,
1744 &cache_dirid, &cache_dir_name,
1745 &cache_ino, &cache_full_name);
1750 * record the mins in sk so we can make sure the
1751 * next search doesn't repeat this root
1753 sk->min_objectid = sh.objectid;
1754 sk->min_offset = sh.offset;
1755 sk->min_type = sh.type;
1757 sk->nr_items = 4096;
1758 if (sk->min_offset < (u64)-1)
1760 else if (sk->min_objectid < (u64)-1) {
1767 free(cache_dir_name);
1768 free(cache_full_name);
1769 printf("transid marker was %llu\n", (unsigned long long)max_found);
1773 char *btrfs_list_path_for_root(int fd, u64 root)
1775 struct root_lookup root_lookup;
1777 char *ret_path = NULL;
1781 ret = btrfs_list_get_path_rootid(fd, &top_id);
1783 return ERR_PTR(ret);
1785 ret = __list_subvol_search(fd, &root_lookup);
1787 return ERR_PTR(ret);
1789 ret = __list_subvol_fill_paths(fd, &root_lookup);
1791 return ERR_PTR(ret);
1793 n = rb_last(&root_lookup.root);
1795 struct root_info *entry;
1797 entry = rb_entry(n, struct root_info, rb_node);
1798 ret = resolve_root(&root_lookup, entry, top_id);
1799 if (ret == -ENOENT && entry->root_id == root) {
1803 if (entry->root_id == root) {
1804 ret_path = entry->full_path;
1805 entry->full_path = NULL;
1810 __free_all_subvolumn(&root_lookup);
1815 int btrfs_list_parse_sort_string(char *opt_arg,
1816 struct btrfs_list_comparer_set **comps)
1824 while ((p = strtok(opt_arg, ",")) != NULL) {
1826 ptr_argv = all_sort_items;
1829 if (strcmp(*ptr_argv, p) == 0) {
1834 if (strcmp(*ptr_argv, p) == 0) {
1851 } else if (*p == '-') {
1857 what_to_sort = btrfs_list_get_sort_item(p);
1858 btrfs_list_setup_comparer(comps, what_to_sort, order);
1867 * This function is used to parse the argument of filter condition.
1869 * type is the filter object.
1871 int btrfs_list_parse_filter_string(char *opt_arg,
1872 struct btrfs_list_filter_set **filters,
1873 enum btrfs_list_filter_enum type)
1878 switch (*(opt_arg++)) {
1880 arg = arg_strtou64(opt_arg);
1883 btrfs_list_setup_filter(filters, type, arg);
1886 arg = arg_strtou64(opt_arg);
1889 btrfs_list_setup_filter(filters, type, arg);
1893 arg = arg_strtou64(opt_arg);
1895 btrfs_list_setup_filter(filters, type, arg);
1902 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1905 struct btrfs_ioctl_ino_lookup_args args;
1907 memset(&args, 0, sizeof(args));
1908 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1910 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1913 "ERROR: can't perform the search -%s\n",
1917 *treeid = args.treeid;