2 * Copyright (C) 2010 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
27 #include <sys/types.h>
34 #include "transaction.h"
36 #include <uuid/uuid.h>
37 #include "btrfs-list.h"
39 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
40 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
42 /* we store all the roots we find in an rbtree so that we can
43 * search for them later.
50 * one of these for each root we find.
53 struct rb_node rb_node;
54 struct rb_node sort_node;
59 /* equal the offset of the root's key */
62 /* flags of the root */
65 /* the id of the root that references this one */
68 /* the dir id we're in from ref_tree */
73 /* generation when the root is created or last updated */
76 /* creation generation of this root in sec*/
79 /* creation time of this root in sec*/
82 u8 uuid[BTRFS_UUID_SIZE];
84 /* path from the subvol we live in to this root, including the
85 * root's name. This is null until we do the extra lookup ioctl.
89 /* the name of this root in the directory it lives in */
99 } btrfs_list_columns[] = {
107 .column_name = "Gen",
112 .column_name = "CGen",
117 .column_name = "Parent",
122 .column_name = "Top Level",
127 .column_name = "OTime",
132 .column_name = "UUID",
137 .column_name = "Path",
147 static btrfs_list_filter_func all_filter_funcs[];
148 static btrfs_list_comp_func all_comp_funcs[];
150 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
154 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
156 if (column < BTRFS_LIST_ALL) {
157 btrfs_list_columns[column].need_print = 1;
161 for (i = 0; i < BTRFS_LIST_ALL; i++)
162 btrfs_list_columns[i].need_print = 1;
165 static void root_lookup_init(struct root_lookup *tree)
167 tree->root.rb_node = NULL;
170 static int comp_entry_with_rootid(struct root_info *entry1,
171 struct root_info *entry2,
176 if (entry1->root_id > entry2->root_id)
178 else if (entry1->root_id < entry2->root_id)
183 return is_descending ? -ret : ret;
186 static int comp_entry_with_gen(struct root_info *entry1,
187 struct root_info *entry2,
192 if (entry1->gen > entry2->gen)
194 else if (entry1->gen < entry2->gen)
199 return is_descending ? -ret : ret;
202 static int comp_entry_with_ogen(struct root_info *entry1,
203 struct root_info *entry2,
208 if (entry1->ogen > entry2->ogen)
210 else if (entry1->ogen < entry2->ogen)
215 return is_descending ? -ret : ret;
218 static int comp_entry_with_path(struct root_info *entry1,
219 struct root_info *entry2,
224 if (strcmp(entry1->full_path, entry2->full_path) > 0)
226 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
231 return is_descending ? -ret : ret;
234 static btrfs_list_comp_func all_comp_funcs[] = {
235 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
236 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
237 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
238 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
241 static char *all_sort_items[] = {
242 [BTRFS_LIST_COMP_ROOTID] = "rootid",
243 [BTRFS_LIST_COMP_OGEN] = "ogen",
244 [BTRFS_LIST_COMP_GEN] = "gen",
245 [BTRFS_LIST_COMP_PATH] = "path",
246 [BTRFS_LIST_COMP_MAX] = NULL,
249 static int btrfs_list_get_sort_item(char *sort_name)
253 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
254 if (strcmp(sort_name, all_sort_items[i]) == 0)
260 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
262 struct btrfs_list_comparer_set *set;
265 size = sizeof(struct btrfs_list_comparer_set) +
266 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
269 fprintf(stderr, "memory allocation failed\n");
273 memset(set, 0, size);
274 set->total = BTRFS_LIST_NCOMPS_INCREASE;
279 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
284 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
285 enum btrfs_list_comp_enum comparer,
288 struct btrfs_list_comparer_set *set = *comp_set;
292 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
293 BUG_ON(set->ncomps > set->total);
295 if (set->ncomps == set->total) {
296 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
297 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
298 set = realloc(set, size);
300 fprintf(stderr, "memory allocation failed\n");
304 memset(&set->comps[set->total], 0,
305 BTRFS_LIST_NCOMPS_INCREASE *
306 sizeof(struct btrfs_list_comparer));
307 set->total += BTRFS_LIST_NCOMPS_INCREASE;
311 BUG_ON(set->comps[set->ncomps].comp_func);
313 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
314 set->comps[set->ncomps].is_descending = is_descending;
319 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
320 struct btrfs_list_comparer_set *set)
322 int rootid_compared = 0;
325 if (!set || !set->ncomps)
328 for (i = 0; i < set->ncomps; i++) {
329 if (!set->comps[i].comp_func)
332 ret = set->comps[i].comp_func(entry1, entry2,
333 set->comps[i].is_descending);
337 if (set->comps[i].comp_func == comp_entry_with_rootid)
341 if (!rootid_compared) {
343 ret = comp_entry_with_rootid(entry1, entry2, 0);
349 static int sort_tree_insert(struct root_lookup *sort_tree,
350 struct root_info *ins,
351 struct btrfs_list_comparer_set *comp_set)
353 struct rb_node **p = &sort_tree->root.rb_node;
354 struct rb_node *parent = NULL;
355 struct root_info *curr;
360 curr = rb_entry(parent, struct root_info, sort_node);
362 ret = sort_comp(ins, curr, comp_set);
371 rb_link_node(&ins->sort_node, parent, p);
372 rb_insert_color(&ins->sort_node, &sort_tree->root);
377 * insert a new root into the tree. returns the existing root entry
378 * if one is already there. Both root_id and ref_tree are used
381 static int root_tree_insert(struct root_lookup *root_tree,
382 struct root_info *ins)
384 struct rb_node **p = &root_tree->root.rb_node;
385 struct rb_node * parent = NULL;
386 struct root_info *curr;
391 curr = rb_entry(parent, struct root_info, rb_node);
393 ret = comp_entry_with_rootid(ins, curr, 0);
402 rb_link_node(&ins->rb_node, parent, p);
403 rb_insert_color(&ins->rb_node, &root_tree->root);
408 * find a given root id in the tree. We return the smallest one,
409 * rb_next can be used to move forward looking for more if required
411 static struct root_info *root_tree_search(struct root_lookup *root_tree,
414 struct rb_node *n = root_tree->root.rb_node;
415 struct root_info *entry;
416 struct root_info tmp;
419 tmp.root_id = root_id;
422 entry = rb_entry(n, struct root_info, rb_node);
424 ret = comp_entry_with_rootid(&tmp, entry, 0);
435 static int update_root(struct root_lookup *root_lookup,
436 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
437 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
438 time_t ot, void *uuid)
440 struct root_info *ri;
442 ri = root_tree_search(root_lookup, root_id);
443 if (!ri || ri->root_id != root_id)
445 if (name && name_len > 0) {
449 ri->name = malloc(name_len + 1);
451 fprintf(stderr, "memory allocation failed\n");
454 strncpy(ri->name, name, name_len);
455 ri->name[name_len] = 0;
458 ri->ref_tree = ref_tree;
460 ri->root_offset = root_offset;
469 if (!ri->ogen && root_offset)
470 ri->ogen = root_offset;
474 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
480 * add_root - update the existed root, or allocate a new root and insert it
481 * into the lookup tree.
482 * root_id: object id of the root
483 * ref_tree: object id of the referring root.
484 * root_offset: offset value of the root'key
485 * dir_id: inode id of the directory in ref_tree where this root can be found.
486 * name: the name of root_id in that directory
487 * name_len: the length of name
488 * ogen: the original generation of the root
489 * gen: the current generation of the root
490 * ot: the original time(create time) of the root
491 * uuid: uuid of the root
493 static int add_root(struct root_lookup *root_lookup,
494 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
495 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
496 time_t ot, void *uuid)
498 struct root_info *ri;
501 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
502 dir_id, name, name_len, ogen, gen, ot, uuid);
506 ri = malloc(sizeof(*ri));
508 printf("memory allocation failed\n");
511 memset(ri, 0, sizeof(*ri));
512 ri->root_id = root_id;
514 if (name && name_len > 0) {
515 ri->name = malloc(name_len + 1);
517 fprintf(stderr, "memory allocation failed\n");
520 strncpy(ri->name, name, name_len);
521 ri->name[name_len] = 0;
524 ri->ref_tree = ref_tree;
528 ri->root_offset = root_offset;
535 if (!ri->ogen && root_offset)
536 ri->ogen = root_offset;
541 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
543 ret = root_tree_insert(root_lookup, ri);
545 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
551 void __free_root_info(struct root_info *ri)
565 void __free_all_subvolumn(struct root_lookup *root_tree)
567 struct root_info *entry;
570 n = rb_first(&root_tree->root);
572 entry = rb_entry(n, struct root_info, rb_node);
573 rb_erase(n, &root_tree->root);
574 __free_root_info(entry);
576 n = rb_first(&root_tree->root);
581 * for a given root_info, search through the root_lookup tree to construct
582 * the full path name to it.
584 * This can't be called until all the root_info->path fields are filled
585 * in by lookup_ino_path
587 static int resolve_root(struct root_lookup *rl, struct root_info *ri)
589 char *full_path = NULL;
591 struct root_info *found;
594 * we go backwards from the root_info object and add pathnames
595 * from parent directories as we go.
601 int add_len = strlen(found->path);
603 /* room for / and for null */
604 tmp = malloc(add_len + 2 + len);
606 perror("malloc failed");
610 memcpy(tmp + add_len + 1, full_path, len);
612 memcpy(tmp, found->path, add_len);
613 tmp [add_len + len + 1] = '\0';
618 full_path = strdup(found->path);
622 next = found->ref_tree;
624 /* if the ref_tree refers to ourselves, we're at the top */
625 if (next == found->root_id) {
631 * if the ref_tree wasn't in our tree of roots, we're
634 found = root_tree_search(rl, next);
641 ri->full_path = full_path;
647 * for a single root_info, ask the kernel to give us a path name
648 * inside it's ref_root for the dir_id where it lives.
650 * This fills in root_info->path with the path to the directory and and
651 * appends this root's name.
653 static int lookup_ino_path(int fd, struct root_info *ri)
655 struct btrfs_ioctl_ino_lookup_args args;
661 memset(&args, 0, sizeof(args));
662 args.treeid = ri->ref_tree;
663 args.objectid = ri->dir_id;
665 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
668 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
669 (unsigned long long)ri->ref_tree,
676 * we're in a subdirectory of ref_tree, the kernel ioctl
677 * puts a / in there for us
679 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
681 perror("malloc failed");
684 strcpy(ri->path, args.name);
685 strcat(ri->path, ri->name);
687 /* we're at the root of ref_tree */
688 ri->path = strdup(ri->name);
690 perror("strdup failed");
697 /* finding the generation for a given path is a two step process.
698 * First we use the inode loookup routine to find out the root id
700 * Then we use the tree search ioctl to scan all the root items for a
701 * given root id and spit out the latest generation we can find
703 static u64 find_root_gen(int fd)
705 struct btrfs_ioctl_ino_lookup_args ino_args;
707 struct btrfs_ioctl_search_args args;
708 struct btrfs_ioctl_search_key *sk = &args.key;
709 struct btrfs_ioctl_search_header *sh;
710 unsigned long off = 0;
715 memset(&ino_args, 0, sizeof(ino_args));
716 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
718 /* this ioctl fills in ino_args->treeid */
719 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
722 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
723 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
728 memset(&args, 0, sizeof(args));
733 * there may be more than one ROOT_ITEM key if there are
734 * snapshots pending deletion, we have to loop through
737 sk->min_objectid = ino_args.treeid;
738 sk->max_objectid = ino_args.treeid;
739 sk->max_type = BTRFS_ROOT_ITEM_KEY;
740 sk->min_type = BTRFS_ROOT_ITEM_KEY;
741 sk->max_offset = (u64)-1;
742 sk->max_transid = (u64)-1;
746 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
749 fprintf(stderr, "ERROR: can't perform the search - %s\n",
753 /* the ioctl returns the number of item it found in nr_items */
754 if (sk->nr_items == 0)
758 for (i = 0; i < sk->nr_items; i++) {
759 struct btrfs_root_item *item;
760 sh = (struct btrfs_ioctl_search_header *)(args.buf +
764 item = (struct btrfs_root_item *)(args.buf + off);
767 sk->min_objectid = sh->objectid;
768 sk->min_type = sh->type;
769 sk->min_offset = sh->offset;
771 if (sh->objectid > ino_args.treeid)
774 if (sh->objectid == ino_args.treeid &&
775 sh->type == BTRFS_ROOT_ITEM_KEY) {
776 max_found = max(max_found,
777 btrfs_root_generation(item));
780 if (sk->min_offset < (u64)-1)
785 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
787 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
793 /* pass in a directory id and this will return
794 * the full path of the parent directory inside its
797 * It may return NULL if it is in the root, or an ERR_PTR if things
800 static char *__ino_resolve(int fd, u64 dirid)
802 struct btrfs_ioctl_ino_lookup_args args;
807 memset(&args, 0, sizeof(args));
808 args.objectid = dirid;
810 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
813 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
814 (unsigned long long)dirid, strerror(e) );
820 * we're in a subdirectory of ref_tree, the kernel ioctl
821 * puts a / in there for us
823 full = strdup(args.name);
825 perror("malloc failed");
826 return ERR_PTR(-ENOMEM);
829 /* we're at the root of ref_tree */
836 * simple string builder, returning a new string with both
839 char *build_name(char *dirid, char *name)
845 full = malloc(strlen(dirid) + strlen(name) + 1);
854 * given an inode number, this returns the full path name inside the subvolume
855 * to that file/directory. cache_dirid and cache_name are used to
856 * cache the results so we can avoid tree searches if a later call goes
857 * to the same directory or file name
859 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
867 struct btrfs_ioctl_search_args args;
868 struct btrfs_ioctl_search_key *sk = &args.key;
869 struct btrfs_ioctl_search_header *sh;
870 unsigned long off = 0;
874 memset(&args, 0, sizeof(args));
879 * step one, we search for the inode back ref. We just use the first
882 sk->min_objectid = ino;
883 sk->max_objectid = ino;
884 sk->max_type = BTRFS_INODE_REF_KEY;
885 sk->max_offset = (u64)-1;
886 sk->min_type = BTRFS_INODE_REF_KEY;
887 sk->max_transid = (u64)-1;
890 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
893 fprintf(stderr, "ERROR: can't perform the search - %s\n",
897 /* the ioctl returns the number of item it found in nr_items */
898 if (sk->nr_items == 0)
902 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
904 if (sh->type == BTRFS_INODE_REF_KEY) {
905 struct btrfs_inode_ref *ref;
908 ref = (struct btrfs_inode_ref *)(sh + 1);
909 namelen = btrfs_stack_inode_ref_name_len(ref);
911 name = (char *)(ref + 1);
912 name = strndup(name, namelen);
914 /* use our cached value */
915 if (dirid == *cache_dirid && *cache_name) {
916 dirname = *cache_name;
923 * the inode backref gives us the file name and the parent directory id.
924 * From here we use __ino_resolve to get the path to the parent
926 dirname = __ino_resolve(fd, dirid);
928 full = build_name(dirname, name);
929 if (*cache_name && dirname != *cache_name)
932 *cache_name = dirname;
933 *cache_dirid = dirid;
939 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
941 struct btrfs_ioctl_search_args args;
942 struct btrfs_ioctl_search_key *sk = &args.key;
943 struct btrfs_ioctl_search_header *sh;
947 memset(&args, 0, sizeof(args));
950 * search for a dir item with a name 'default' in the tree of
951 * tree roots, it should point us to a default root
955 /* don't worry about ancient format and request only one item */
958 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
959 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
960 sk->max_type = BTRFS_DIR_ITEM_KEY;
961 sk->min_type = BTRFS_DIR_ITEM_KEY;
962 sk->max_offset = (u64)-1;
963 sk->max_transid = (u64)-1;
965 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
969 /* the ioctl returns the number of items it found in nr_items */
970 if (sk->nr_items == 0)
973 sh = (struct btrfs_ioctl_search_header *)args.buf;
975 if (sh->type == BTRFS_DIR_ITEM_KEY) {
976 struct btrfs_dir_item *di;
980 di = (struct btrfs_dir_item *)(sh + 1);
981 name_len = btrfs_stack_dir_name_len(di);
982 name = (char *)(di + 1);
984 if (!strncmp("default", name, name_len))
985 found = btrfs_disk_key_objectid(&di->location);
993 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
996 struct btrfs_ioctl_search_args args;
997 struct btrfs_ioctl_search_key *sk = &args.key;
998 struct btrfs_ioctl_search_header *sh;
999 struct btrfs_root_ref *ref;
1000 struct btrfs_root_item *ri;
1001 unsigned long off = 0;
1010 u8 uuid[BTRFS_UUID_SIZE];
1012 root_lookup_init(root_lookup);
1013 memset(&args, 0, sizeof(args));
1015 /* search in the tree of tree roots */
1019 * set the min and max to backref keys. The search will
1020 * only send back this type of key now.
1022 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1023 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1025 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1028 * set all the other params to the max, we'll take any objectid
1031 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1032 sk->max_offset = (u64)-1;
1033 sk->max_transid = (u64)-1;
1035 /* just a big number, doesn't matter much */
1036 sk->nr_items = 4096;
1039 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1042 /* the ioctl returns the number of item it found in nr_items */
1043 if (sk->nr_items == 0)
1049 * for each item, pull the key out of the header and then
1050 * read the root_ref item it contains
1052 for (i = 0; i < sk->nr_items; i++) {
1053 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1056 if (sh->type == BTRFS_ROOT_BACKREF_KEY) {
1057 ref = (struct btrfs_root_ref *)(args.buf + off);
1058 name_len = btrfs_stack_root_ref_name_len(ref);
1059 name = (char *)(ref + 1);
1060 dir_id = btrfs_stack_root_ref_dirid(ref);
1062 add_root(root_lookup, sh->objectid, sh->offset,
1063 0, 0, dir_id, name, name_len, 0, 0, 0,
1065 } else if (sh->type == BTRFS_ROOT_ITEM_KEY) {
1066 ri = (struct btrfs_root_item *)(args.buf + off);
1067 gen = btrfs_root_generation(ri);
1068 flags = btrfs_root_flags(ri);
1070 sizeof(struct btrfs_root_item_v0)) {
1072 ogen = btrfs_root_otransid(ri);
1073 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1077 memset(uuid, 0, BTRFS_UUID_SIZE);
1080 add_root(root_lookup, sh->objectid, 0,
1081 sh->offset, flags, 0, NULL, 0, ogen,
1088 * record the mins in sk so we can make sure the
1089 * next search doesn't repeat this root
1091 sk->min_objectid = sh->objectid;
1092 sk->min_type = sh->type;
1093 sk->min_offset = sh->offset;
1095 sk->nr_items = 4096;
1097 if (!sk->min_offset) /* overflow */
1102 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1103 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1108 if (sk->min_objectid > sk->max_objectid)
1115 static int filter_by_rootid(struct root_info *ri, u64 data)
1117 return ri->root_id == data;
1120 static int filter_snapshot(struct root_info *ri, u64 data)
1122 return !!ri->root_offset;
1125 static int filter_flags(struct root_info *ri, u64 flags)
1127 return ri->flags & flags;
1130 static int filter_gen_more(struct root_info *ri, u64 data)
1132 return ri->gen >= data;
1135 static int filter_gen_less(struct root_info *ri, u64 data)
1137 return ri->gen <= data;
1140 static int filter_gen_equal(struct root_info *ri, u64 data)
1142 return ri->gen == data;
1145 static int filter_cgen_more(struct root_info *ri, u64 data)
1147 return ri->ogen >= data;
1150 static int filter_cgen_less(struct root_info *ri, u64 data)
1152 return ri->ogen <= data;
1155 static int filter_cgen_equal(struct root_info *ri, u64 data)
1157 return ri->ogen == data;
1160 static btrfs_list_filter_func all_filter_funcs[] = {
1161 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1162 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1163 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1164 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1165 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1166 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1167 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1168 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1169 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1172 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1174 struct btrfs_list_filter_set *set;
1177 size = sizeof(struct btrfs_list_filter_set) +
1178 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1181 fprintf(stderr, "memory allocation failed\n");
1185 memset(set, 0, size);
1186 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1191 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1196 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1197 enum btrfs_list_filter_enum filter, u64 data)
1199 struct btrfs_list_filter_set *set = *filter_set;
1203 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1204 BUG_ON(set->nfilters > set->total);
1206 if (set->nfilters == set->total) {
1207 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1208 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1209 set = realloc(set, size);
1211 fprintf(stderr, "memory allocation failed\n");
1215 memset(&set->filters[set->total], 0,
1216 BTRFS_LIST_NFILTERS_INCREASE *
1217 sizeof(struct btrfs_list_filter));
1218 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1222 BUG_ON(set->filters[set->nfilters].filter_func);
1224 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1225 set->filters[set->nfilters].data = data;
1230 static int filter_root(struct root_info *ri,
1231 struct btrfs_list_filter_set *set)
1235 if (!set || !set->nfilters)
1238 for (i = 0; i < set->nfilters; i++) {
1239 if (!set->filters[i].filter_func)
1241 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1248 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1249 struct root_lookup *sort_tree,
1250 struct btrfs_list_filter_set *filter_set,
1251 struct btrfs_list_comparer_set *comp_set)
1254 struct root_info *entry;
1257 root_lookup_init(sort_tree);
1259 n = rb_last(&all_subvols->root);
1261 entry = rb_entry(n, struct root_info, rb_node);
1263 resolve_root(all_subvols, entry);
1264 ret = filter_root(entry, filter_set);
1266 sort_tree_insert(sort_tree, entry, comp_set);
1271 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1275 n = rb_first(&root_lookup->root);
1277 struct root_info *entry;
1279 entry = rb_entry(n, struct root_info, rb_node);
1280 ret = lookup_ino_path(fd, entry);
1289 static void print_subvolume_column(struct root_info *subv,
1290 enum btrfs_list_column_enum column)
1295 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1298 case BTRFS_LIST_OBJECTID:
1299 printf("%llu", subv->root_id);
1301 case BTRFS_LIST_GENERATION:
1302 printf("%llu", subv->gen);
1304 case BTRFS_LIST_OGENERATION:
1305 printf("%llu", subv->ogen);
1307 case BTRFS_LIST_PARENT:
1308 printf("%llu", subv->ref_tree);
1310 case BTRFS_LIST_TOP_LEVEL:
1311 printf("%llu", subv->top_id);
1313 case BTRFS_LIST_OTIME:
1315 strftime(tstr, 256, "%Y-%m-%d %X",
1316 localtime(&subv->otime));
1321 case BTRFS_LIST_UUID:
1322 if (uuid_is_null(subv->uuid))
1323 strcpy(uuidparse, "-");
1325 uuid_unparse(subv->uuid, uuidparse);
1326 printf("%s", uuidparse);
1328 case BTRFS_LIST_PATH:
1329 BUG_ON(!subv->full_path);
1330 printf("%s", subv->full_path);
1337 static void print_single_volume_info_table(struct root_info *subv)
1341 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1342 if (!btrfs_list_columns[i].need_print)
1345 print_subvolume_column(subv, i);
1347 if (i != BTRFS_LIST_PATH)
1350 if (i == BTRFS_LIST_TOP_LEVEL)
1356 static void print_single_volume_info_default(struct root_info *subv)
1360 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1361 if (!btrfs_list_columns[i].need_print)
1364 printf("%s ", btrfs_list_columns[i].name);
1365 print_subvolume_column(subv, i);
1367 if (i != BTRFS_LIST_PATH)
1373 static void print_all_volume_info_tab_head()
1379 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1380 if (btrfs_list_columns[i].need_print)
1381 printf("%s\t", btrfs_list_columns[i].name);
1383 if (i == BTRFS_LIST_ALL-1)
1387 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1388 memset(barrier, 0, sizeof(barrier));
1390 if (btrfs_list_columns[i].need_print) {
1391 len = strlen(btrfs_list_columns[i].name);
1393 strcat(barrier, "-");
1395 printf("%s\t", barrier);
1397 if (i == BTRFS_LIST_ALL-1)
1402 static void print_all_volume_info(struct root_lookup *sorted_tree,
1406 struct root_info *entry;
1409 print_all_volume_info_tab_head();
1411 n = rb_first(&sorted_tree->root);
1413 entry = rb_entry(n, struct root_info, sort_node);
1415 print_single_volume_info_table(entry);
1417 print_single_volume_info_default(entry);
1422 int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set,
1423 struct btrfs_list_comparer_set *comp_set,
1426 struct root_lookup root_lookup;
1427 struct root_lookup root_sort;
1430 ret = __list_subvol_search(fd, &root_lookup);
1432 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1438 * now we have an rbtree full of root_info objects, but we need to fill
1439 * in their path names within the subvol that is referencing each one.
1441 ret = __list_subvol_fill_paths(fd, &root_lookup);
1445 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1448 print_all_volume_info(&root_sort, is_tab_result);
1449 __free_all_subvolumn(&root_lookup);
1453 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1454 struct btrfs_file_extent_item *item,
1455 u64 found_gen, u64 *cache_dirid,
1456 char **cache_dir_name, u64 *cache_ino,
1457 char **cache_full_name)
1461 u64 disk_offset = 0;
1467 if (sh->objectid == *cache_ino) {
1468 name = *cache_full_name;
1469 } else if (*cache_full_name) {
1470 free(*cache_full_name);
1471 *cache_full_name = NULL;
1474 name = ino_resolve(fd, sh->objectid, cache_dirid,
1476 *cache_full_name = name;
1477 *cache_ino = sh->objectid;
1482 type = btrfs_stack_file_extent_type(item);
1483 compressed = btrfs_stack_file_extent_compression(item);
1485 if (type == BTRFS_FILE_EXTENT_REG ||
1486 type == BTRFS_FILE_EXTENT_PREALLOC) {
1487 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1488 disk_offset = btrfs_stack_file_extent_offset(item);
1489 len = btrfs_stack_file_extent_num_bytes(item);
1490 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1493 len = btrfs_stack_file_extent_ram_bytes(item);
1495 printf("unhandled extent type %d for inode %llu "
1496 "file offset %llu gen %llu\n",
1498 (unsigned long long)sh->objectid,
1499 (unsigned long long)sh->offset,
1500 (unsigned long long)found_gen);
1504 printf("inode %llu file offset %llu len %llu disk start %llu "
1505 "offset %llu gen %llu flags ",
1506 (unsigned long long)sh->objectid,
1507 (unsigned long long)sh->offset,
1508 (unsigned long long)len,
1509 (unsigned long long)disk_start,
1510 (unsigned long long)disk_offset,
1511 (unsigned long long)found_gen);
1517 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1518 printf("%sPREALLOC", flags ? "|" : "");
1521 if (type == BTRFS_FILE_EXTENT_INLINE) {
1522 printf("%sINLINE", flags ? "|" : "");
1528 printf(" %s\n", name);
1532 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1535 struct btrfs_ioctl_search_args args;
1536 struct btrfs_ioctl_search_key *sk = &args.key;
1537 struct btrfs_ioctl_search_header *sh;
1538 struct btrfs_file_extent_item *item;
1539 unsigned long off = 0;
1544 u64 cache_dirid = 0;
1546 char *cache_dir_name = NULL;
1547 char *cache_full_name = NULL;
1548 struct btrfs_file_extent_item backup;
1550 memset(&backup, 0, sizeof(backup));
1551 memset(&args, 0, sizeof(args));
1553 sk->tree_id = root_id;
1556 * set all the other params to the max, we'll take any objectid
1559 sk->max_objectid = (u64)-1;
1560 sk->max_offset = (u64)-1;
1561 sk->max_transid = (u64)-1;
1562 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1563 sk->min_transid = oldest_gen;
1564 /* just a big number, doesn't matter much */
1565 sk->nr_items = 4096;
1567 max_found = find_root_gen(fd);
1569 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1572 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1576 /* the ioctl returns the number of item it found in nr_items */
1577 if (sk->nr_items == 0)
1583 * for each item, pull the key out of the header and then
1584 * read the root_ref item it contains
1586 for (i = 0; i < sk->nr_items; i++) {
1587 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1592 * just in case the item was too big, pass something other
1598 item = (struct btrfs_file_extent_item *)(args.buf +
1600 found_gen = btrfs_stack_file_extent_generation(item);
1601 if (sh->type == BTRFS_EXTENT_DATA_KEY &&
1602 found_gen >= oldest_gen) {
1603 print_one_extent(fd, sh, item, found_gen,
1604 &cache_dirid, &cache_dir_name,
1605 &cache_ino, &cache_full_name);
1610 * record the mins in sk so we can make sure the
1611 * next search doesn't repeat this root
1613 sk->min_objectid = sh->objectid;
1614 sk->min_offset = sh->offset;
1615 sk->min_type = sh->type;
1617 sk->nr_items = 4096;
1618 if (sk->min_offset < (u64)-1)
1620 else if (sk->min_objectid < (u64)-1) {
1627 free(cache_dir_name);
1628 free(cache_full_name);
1629 printf("transid marker was %llu\n", (unsigned long long)max_found);
1633 char *btrfs_list_path_for_root(int fd, u64 root)
1635 struct root_lookup root_lookup;
1637 char *ret_path = NULL;
1640 ret = __list_subvol_search(fd, &root_lookup);
1642 return ERR_PTR(ret);
1644 ret = __list_subvol_fill_paths(fd, &root_lookup);
1646 return ERR_PTR(ret);
1648 n = rb_last(&root_lookup.root);
1650 struct root_info *entry;
1652 entry = rb_entry(n, struct root_info, rb_node);
1653 resolve_root(&root_lookup, entry);
1654 if (entry->root_id == root) {
1655 ret_path = entry->full_path;
1656 entry->full_path = NULL;
1661 __free_all_subvolumn(&root_lookup);
1666 int btrfs_list_parse_sort_string(char *optarg,
1667 struct btrfs_list_comparer_set **comps)
1675 while ((p = strtok(optarg, ",")) != NULL) {
1677 ptr_argv = all_sort_items;
1680 if (strcmp(*ptr_argv, p) == 0) {
1685 if (strcmp(*ptr_argv, p) == 0) {
1702 } else if (*p == '-') {
1708 what_to_sort = btrfs_list_get_sort_item(p);
1709 btrfs_list_setup_comparer(comps, what_to_sort, order);
1718 * This function is used to parse the argument of filter condition.
1720 * type is the filter object.
1722 int btrfs_list_parse_filter_string(char *optarg,
1723 struct btrfs_list_filter_set **filters,
1724 enum btrfs_list_filter_enum type)
1728 char *ptr_parse_end = NULL;
1729 char *ptr_optarg_end = optarg + strlen(optarg);
1731 switch (*(optarg++)) {
1733 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1735 if (ptr_parse_end != ptr_optarg_end)
1738 btrfs_list_setup_filter(filters, type, arg);
1741 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1743 if (ptr_parse_end != ptr_optarg_end)
1746 btrfs_list_setup_filter(filters, type, arg);
1750 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1752 if (ptr_parse_end != ptr_optarg_end)
1754 btrfs_list_setup_filter(filters, type, arg);