btrfs-progs: Fix infinite loop of btrfs subvolumn sync
[btrfs-progs-unstable/devel.git] / btrfs-list.c
blobd54de61aec018ea1d9d8d63710046bffa666c826
1 /*
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>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <libgen.h>
29 #include "ctree.h"
30 #include "transaction.h"
31 #include "utils.h"
32 #include "ioctl.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.
43 struct root_lookup {
44 struct rb_root root;
47 static struct {
48 char *name;
49 char *column_name;
50 int need_print;
51 } btrfs_list_columns[] = {
53 .name = "ID",
54 .column_name = "ID",
55 .need_print = 0,
58 .name = "gen",
59 .column_name = "Gen",
60 .need_print = 0,
63 .name = "cgen",
64 .column_name = "CGen",
65 .need_print = 0,
68 .name = "parent",
69 .column_name = "Parent",
70 .need_print = 0,
73 .name = "top level",
74 .column_name = "Top Level",
75 .need_print = 0,
78 .name = "otime",
79 .column_name = "OTime",
80 .need_print = 0,
83 .name = "parent_uuid",
84 .column_name = "Parent UUID",
85 .need_print = 0,
88 .name = "received_uuid",
89 .column_name = "Received UUID",
90 .need_print = 0,
93 .name = "uuid",
94 .column_name = "UUID",
95 .need_print = 0,
98 .name = "path",
99 .column_name = "Path",
100 .need_print = 0,
103 .name = NULL,
104 .column_name = NULL,
105 .need_print = 0,
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)
114 int i;
116 BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
118 if (column < BTRFS_LIST_ALL) {
119 btrfs_list_columns[column].need_print = 1;
120 return;
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,
134 int is_descending)
136 int ret;
138 if (entry1->root_id > entry2->root_id)
139 ret = 1;
140 else if (entry1->root_id < entry2->root_id)
141 ret = -1;
142 else
143 ret = 0;
145 return is_descending ? -ret : ret;
148 static int comp_entry_with_gen(struct root_info *entry1,
149 struct root_info *entry2,
150 int is_descending)
152 int ret;
154 if (entry1->gen > entry2->gen)
155 ret = 1;
156 else if (entry1->gen < entry2->gen)
157 ret = -1;
158 else
159 ret = 0;
161 return is_descending ? -ret : ret;
164 static int comp_entry_with_ogen(struct root_info *entry1,
165 struct root_info *entry2,
166 int is_descending)
168 int ret;
170 if (entry1->ogen > entry2->ogen)
171 ret = 1;
172 else if (entry1->ogen < entry2->ogen)
173 ret = -1;
174 else
175 ret = 0;
177 return is_descending ? -ret : ret;
180 static int comp_entry_with_path(struct root_info *entry1,
181 struct root_info *entry2,
182 int is_descending)
184 int ret;
186 if (strcmp(entry1->full_path, entry2->full_path) > 0)
187 ret = 1;
188 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
189 ret = -1;
190 else
191 ret = 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)
213 int i;
215 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
216 if (strcmp(sort_name, all_sort_items[i]) == 0)
217 return i;
219 return -1;
222 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
224 struct btrfs_list_comparer_set *set;
225 int size;
227 size = sizeof(struct btrfs_list_comparer_set) +
228 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
229 set = malloc(size);
230 if (!set) {
231 fprintf(stderr, "memory allocation failed\n");
232 exit(1);
235 memset(set, 0, size);
236 set->total = BTRFS_LIST_NCOMPS_INCREASE;
238 return set;
241 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
243 free(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;
250 int size;
252 BUG_ON(!set);
253 BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
254 BUG_ON(set->ncomps > set->total);
256 if (set->ncomps == set->total) {
257 void *tmp;
259 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
260 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
261 tmp = set;
262 set = realloc(set, size);
263 if (!set) {
264 fprintf(stderr, "memory allocation failed\n");
265 free(tmp);
266 exit(1);
269 memset(&set->comps[set->total], 0,
270 BTRFS_LIST_NCOMPS_INCREASE *
271 sizeof(struct btrfs_list_comparer));
272 set->total += BTRFS_LIST_NCOMPS_INCREASE;
273 *comp_set = set;
276 BUG_ON(set->comps[set->ncomps].comp_func);
278 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
279 set->comps[set->ncomps].is_descending = is_descending;
280 set->ncomps++;
281 return 0;
284 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
285 struct btrfs_list_comparer_set *set)
287 int rootid_compared = 0;
288 int i, ret = 0;
290 if (!set || !set->ncomps)
291 goto comp_rootid;
293 for (i = 0; i < set->ncomps; i++) {
294 if (!set->comps[i].comp_func)
295 break;
297 ret = set->comps[i].comp_func(entry1, entry2,
298 set->comps[i].is_descending);
299 if (ret)
300 return ret;
302 if (set->comps[i].comp_func == comp_entry_with_rootid)
303 rootid_compared = 1;
306 if (!rootid_compared) {
307 comp_rootid:
308 ret = comp_entry_with_rootid(entry1, entry2, 0);
311 return ret;
314 static int sort_tree_insert(struct root_lookup *sort_tree,
315 struct root_info *ins,
316 struct btrfs_list_comparer_set *comp_set)
318 struct rb_node **p = &sort_tree->root.rb_node;
319 struct rb_node *parent = NULL;
320 struct root_info *curr;
321 int ret;
323 while (*p) {
324 parent = *p;
325 curr = rb_entry(parent, struct root_info, sort_node);
327 ret = sort_comp(ins, curr, comp_set);
328 if (ret < 0)
329 p = &(*p)->rb_left;
330 else if (ret > 0)
331 p = &(*p)->rb_right;
332 else
333 return -EEXIST;
336 rb_link_node(&ins->sort_node, parent, p);
337 rb_insert_color(&ins->sort_node, &sort_tree->root);
338 return 0;
342 * insert a new root into the tree. returns the existing root entry
343 * if one is already there. Both root_id and ref_tree are used
344 * as the key
346 static int root_tree_insert(struct root_lookup *root_tree,
347 struct root_info *ins)
349 struct rb_node **p = &root_tree->root.rb_node;
350 struct rb_node * parent = NULL;
351 struct root_info *curr;
352 int ret;
354 while(*p) {
355 parent = *p;
356 curr = rb_entry(parent, struct root_info, rb_node);
358 ret = comp_entry_with_rootid(ins, curr, 0);
359 if (ret < 0)
360 p = &(*p)->rb_left;
361 else if (ret > 0)
362 p = &(*p)->rb_right;
363 else
364 return -EEXIST;
367 rb_link_node(&ins->rb_node, parent, p);
368 rb_insert_color(&ins->rb_node, &root_tree->root);
369 return 0;
373 * find a given root id in the tree. We return the smallest one,
374 * rb_next can be used to move forward looking for more if required
376 static struct root_info *root_tree_search(struct root_lookup *root_tree,
377 u64 root_id)
379 struct rb_node *n = root_tree->root.rb_node;
380 struct root_info *entry;
381 struct root_info tmp;
382 int ret;
384 tmp.root_id = root_id;
386 while(n) {
387 entry = rb_entry(n, struct root_info, rb_node);
389 ret = comp_entry_with_rootid(&tmp, entry, 0);
390 if (ret < 0)
391 n = n->rb_left;
392 else if (ret > 0)
393 n = n->rb_right;
394 else
395 return entry;
397 return NULL;
400 static int update_root(struct root_lookup *root_lookup,
401 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
402 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
403 time_t ot, void *uuid, void *puuid, void *ruuid)
405 struct root_info *ri;
407 ri = root_tree_search(root_lookup, root_id);
408 if (!ri || ri->root_id != root_id)
409 return -ENOENT;
410 if (name && name_len > 0) {
411 free(ri->name);
413 ri->name = malloc(name_len + 1);
414 if (!ri->name) {
415 fprintf(stderr, "memory allocation failed\n");
416 exit(1);
418 strncpy(ri->name, name, name_len);
419 ri->name[name_len] = 0;
421 if (ref_tree)
422 ri->ref_tree = ref_tree;
423 if (root_offset)
424 ri->root_offset = root_offset;
425 if (flags)
426 ri->flags = flags;
427 if (dir_id)
428 ri->dir_id = dir_id;
429 if (gen)
430 ri->gen = gen;
431 if (ogen)
432 ri->ogen = ogen;
433 if (!ri->ogen && root_offset)
434 ri->ogen = root_offset;
435 if (ot)
436 ri->otime = ot;
437 if (uuid)
438 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
439 if (puuid)
440 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
441 if (ruuid)
442 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
444 return 0;
448 * add_root - update the existed root, or allocate a new root and insert it
449 * into the lookup tree.
450 * root_id: object id of the root
451 * ref_tree: object id of the referring root.
452 * root_offset: offset value of the root'key
453 * dir_id: inode id of the directory in ref_tree where this root can be found.
454 * name: the name of root_id in that directory
455 * name_len: the length of name
456 * ogen: the original generation of the root
457 * gen: the current generation of the root
458 * ot: the original time(create time) of the root
459 * uuid: uuid of the root
460 * puuid: uuid of the root parent if any
461 * ruuid: uuid of the received subvol, if any
463 static int add_root(struct root_lookup *root_lookup,
464 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
465 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
466 time_t ot, void *uuid, void *puuid, void *ruuid)
468 struct root_info *ri;
469 int ret;
471 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
472 dir_id, name, name_len, ogen, gen, ot,
473 uuid, puuid, ruuid);
474 if (!ret)
475 return 0;
477 ri = malloc(sizeof(*ri));
478 if (!ri) {
479 printf("memory allocation failed\n");
480 exit(1);
482 memset(ri, 0, sizeof(*ri));
483 ri->root_id = root_id;
485 if (name && name_len > 0) {
486 ri->name = malloc(name_len + 1);
487 if (!ri->name) {
488 fprintf(stderr, "memory allocation failed\n");
489 exit(1);
491 strncpy(ri->name, name, name_len);
492 ri->name[name_len] = 0;
494 if (ref_tree)
495 ri->ref_tree = ref_tree;
496 if (dir_id)
497 ri->dir_id = dir_id;
498 if (root_offset)
499 ri->root_offset = root_offset;
500 if (flags)
501 ri->flags = flags;
502 if (gen)
503 ri->gen = gen;
504 if (ogen)
505 ri->ogen = ogen;
506 if (!ri->ogen && root_offset)
507 ri->ogen = root_offset;
508 if (ot)
509 ri->otime = ot;
511 if (uuid)
512 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
514 if (puuid)
515 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
517 if (ruuid)
518 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
520 ret = root_tree_insert(root_lookup, ri);
521 if (ret) {
522 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
523 exit(1);
525 return 0;
528 static void __free_root_info(struct rb_node *node)
530 struct root_info *ri;
532 ri = rb_entry(node, struct root_info, rb_node);
533 free(ri->name);
534 free(ri->path);
535 free(ri->full_path);
536 free(ri);
539 static inline void __free_all_subvolumn(struct root_lookup *root_tree)
541 rb_free_nodes(&root_tree->root, __free_root_info);
545 * for a given root_info, search through the root_lookup tree to construct
546 * the full path name to it.
548 * This can't be called until all the root_info->path fields are filled
549 * in by lookup_ino_path
551 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
552 u64 top_id)
554 char *full_path = NULL;
555 int len = 0;
556 struct root_info *found;
559 * we go backwards from the root_info object and add pathnames
560 * from parent directories as we go.
562 found = ri;
563 while (1) {
564 char *tmp;
565 u64 next;
566 int add_len;
569 * ref_tree = 0 indicates the subvolumes
570 * has been deleted.
572 if (!found->ref_tree) {
573 free(full_path);
574 return -ENOENT;
577 add_len = strlen(found->path);
579 if (full_path) {
580 /* room for / and for null */
581 tmp = malloc(add_len + 2 + len);
582 if (!tmp) {
583 perror("malloc failed");
584 exit(1);
586 memcpy(tmp + add_len + 1, full_path, len);
587 tmp[add_len] = '/';
588 memcpy(tmp, found->path, add_len);
589 tmp [add_len + len + 1] = '\0';
590 free(full_path);
591 full_path = tmp;
592 len += add_len + 1;
593 } else {
594 full_path = strdup(found->path);
595 len = add_len;
597 if (!ri->top_id)
598 ri->top_id = found->ref_tree;
600 next = found->ref_tree;
601 if (next == top_id)
602 break;
604 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
605 * we are at the top
607 if (next == BTRFS_FS_TREE_OBJECTID)
608 break;
610 * if the ref_tree wasn't in our tree of roots, the
611 * subvolume was deleted.
613 found = root_tree_search(rl, next);
614 if (!found) {
615 free(full_path);
616 return -ENOENT;
620 ri->full_path = full_path;
622 return 0;
626 * for a single root_info, ask the kernel to give us a path name
627 * inside it's ref_root for the dir_id where it lives.
629 * This fills in root_info->path with the path to the directory and and
630 * appends this root's name.
632 static int lookup_ino_path(int fd, struct root_info *ri)
634 struct btrfs_ioctl_ino_lookup_args args;
635 int ret, e;
637 if (ri->path)
638 return 0;
640 if (!ri->ref_tree)
641 return -ENOENT;
643 memset(&args, 0, sizeof(args));
644 args.treeid = ri->ref_tree;
645 args.objectid = ri->dir_id;
647 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
648 e = errno;
649 if (ret) {
650 if (e == ENOENT) {
651 ri->ref_tree = 0;
652 return -ENOENT;
654 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
655 (unsigned long long)ri->ref_tree,
656 strerror(e));
657 return ret;
660 if (args.name[0]) {
662 * we're in a subdirectory of ref_tree, the kernel ioctl
663 * puts a / in there for us
665 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
666 if (!ri->path) {
667 perror("malloc failed");
668 exit(1);
670 strcpy(ri->path, args.name);
671 strcat(ri->path, ri->name);
672 } else {
673 /* we're at the root of ref_tree */
674 ri->path = strdup(ri->name);
675 if (!ri->path) {
676 perror("strdup failed");
677 exit(1);
680 return 0;
683 /* finding the generation for a given path is a two step process.
684 * First we use the inode loookup routine to find out the root id
686 * Then we use the tree search ioctl to scan all the root items for a
687 * given root id and spit out the latest generation we can find
689 static u64 find_root_gen(int fd)
691 struct btrfs_ioctl_ino_lookup_args ino_args;
692 int ret;
693 struct btrfs_ioctl_search_args args;
694 struct btrfs_ioctl_search_key *sk = &args.key;
695 struct btrfs_ioctl_search_header sh;
696 unsigned long off = 0;
697 u64 max_found = 0;
698 int i;
699 int e;
701 memset(&ino_args, 0, sizeof(ino_args));
702 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
704 /* this ioctl fills in ino_args->treeid */
705 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
706 e = errno;
707 if (ret) {
708 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
709 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
710 strerror(e));
711 return 0;
714 memset(&args, 0, sizeof(args));
716 sk->tree_id = 1;
719 * there may be more than one ROOT_ITEM key if there are
720 * snapshots pending deletion, we have to loop through
721 * them.
723 sk->min_objectid = ino_args.treeid;
724 sk->max_objectid = ino_args.treeid;
725 sk->max_type = BTRFS_ROOT_ITEM_KEY;
726 sk->min_type = BTRFS_ROOT_ITEM_KEY;
727 sk->max_offset = (u64)-1;
728 sk->max_transid = (u64)-1;
729 sk->nr_items = 4096;
731 while (1) {
732 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
733 e = errno;
734 if (ret < 0) {
735 fprintf(stderr, "ERROR: can't perform the search - %s\n",
736 strerror(e));
737 return 0;
739 /* the ioctl returns the number of item it found in nr_items */
740 if (sk->nr_items == 0)
741 break;
743 off = 0;
744 for (i = 0; i < sk->nr_items; i++) {
745 struct btrfs_root_item *item;
747 memcpy(&sh, args.buf + off, sizeof(sh));
748 off += sizeof(sh);
749 item = (struct btrfs_root_item *)(args.buf + off);
750 off += sh.len;
752 sk->min_objectid = sh.objectid;
753 sk->min_type = sh.type;
754 sk->min_offset = sh.offset;
756 if (sh.objectid > ino_args.treeid)
757 break;
759 if (sh.objectid == ino_args.treeid &&
760 sh.type == BTRFS_ROOT_ITEM_KEY) {
761 max_found = max(max_found,
762 btrfs_root_generation(item));
765 if (sk->min_offset < (u64)-1)
766 sk->min_offset++;
767 else
768 break;
770 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
771 break;
772 if (sk->min_objectid != ino_args.treeid)
773 break;
775 return max_found;
778 /* pass in a directory id and this will return
779 * the full path of the parent directory inside its
780 * subvolume root.
782 * It may return NULL if it is in the root, or an ERR_PTR if things
783 * go badly.
785 static char *__ino_resolve(int fd, u64 dirid)
787 struct btrfs_ioctl_ino_lookup_args args;
788 int ret;
789 char *full;
790 int e;
792 memset(&args, 0, sizeof(args));
793 args.objectid = dirid;
795 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
796 e = errno;
797 if (ret) {
798 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
799 (unsigned long long)dirid, strerror(e) );
800 return ERR_PTR(ret);
803 if (args.name[0]) {
805 * we're in a subdirectory of ref_tree, the kernel ioctl
806 * puts a / in there for us
808 full = strdup(args.name);
809 if (!full) {
810 perror("malloc failed");
811 return ERR_PTR(-ENOMEM);
813 } else {
814 /* we're at the root of ref_tree */
815 full = NULL;
817 return full;
821 * simple string builder, returning a new string with both
822 * dirid and name
824 static char *build_name(char *dirid, char *name)
826 char *full;
827 if (!dirid)
828 return strdup(name);
830 full = malloc(strlen(dirid) + strlen(name) + 1);
831 if (!full)
832 return NULL;
833 strcpy(full, dirid);
834 strcat(full, name);
835 return full;
839 * given an inode number, this returns the full path name inside the subvolume
840 * to that file/directory. cache_dirid and cache_name are used to
841 * cache the results so we can avoid tree searches if a later call goes
842 * to the same directory or file name
844 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
847 u64 dirid;
848 char *dirname;
849 char *name;
850 char *full;
851 int ret;
852 struct btrfs_ioctl_search_args args;
853 struct btrfs_ioctl_search_key *sk = &args.key;
854 struct btrfs_ioctl_search_header *sh;
855 unsigned long off = 0;
856 int namelen;
857 int e;
859 memset(&args, 0, sizeof(args));
861 sk->tree_id = 0;
864 * step one, we search for the inode back ref. We just use the first
865 * one
867 sk->min_objectid = ino;
868 sk->max_objectid = ino;
869 sk->max_type = BTRFS_INODE_REF_KEY;
870 sk->max_offset = (u64)-1;
871 sk->min_type = BTRFS_INODE_REF_KEY;
872 sk->max_transid = (u64)-1;
873 sk->nr_items = 1;
875 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
876 e = errno;
877 if (ret < 0) {
878 fprintf(stderr, "ERROR: can't perform the search - %s\n",
879 strerror(e));
880 return NULL;
882 /* the ioctl returns the number of item it found in nr_items */
883 if (sk->nr_items == 0)
884 return NULL;
886 off = 0;
887 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
889 if (sh->type == BTRFS_INODE_REF_KEY) {
890 struct btrfs_inode_ref *ref;
891 dirid = sh->offset;
893 ref = (struct btrfs_inode_ref *)(sh + 1);
894 namelen = btrfs_stack_inode_ref_name_len(ref);
896 name = (char *)(ref + 1);
897 name = strndup(name, namelen);
899 /* use our cached value */
900 if (dirid == *cache_dirid && *cache_name) {
901 dirname = *cache_name;
902 goto build;
904 } else {
905 return NULL;
908 * the inode backref gives us the file name and the parent directory id.
909 * From here we use __ino_resolve to get the path to the parent
911 dirname = __ino_resolve(fd, dirid);
912 build:
913 full = build_name(dirname, name);
914 if (*cache_name && dirname != *cache_name)
915 free(*cache_name);
917 *cache_name = dirname;
918 *cache_dirid = dirid;
919 free(name);
921 return full;
924 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
926 struct btrfs_ioctl_search_args args;
927 struct btrfs_ioctl_search_key *sk = &args.key;
928 struct btrfs_ioctl_search_header *sh;
929 u64 found = 0;
930 int ret;
932 memset(&args, 0, sizeof(args));
935 * search for a dir item with a name 'default' in the tree of
936 * tree roots, it should point us to a default root
938 sk->tree_id = 1;
940 /* don't worry about ancient format and request only one item */
941 sk->nr_items = 1;
943 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
944 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
945 sk->max_type = BTRFS_DIR_ITEM_KEY;
946 sk->min_type = BTRFS_DIR_ITEM_KEY;
947 sk->max_offset = (u64)-1;
948 sk->max_transid = (u64)-1;
950 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
951 if (ret < 0)
952 return ret;
954 /* the ioctl returns the number of items it found in nr_items */
955 if (sk->nr_items == 0)
956 goto out;
958 sh = (struct btrfs_ioctl_search_header *)args.buf;
960 if (sh->type == BTRFS_DIR_ITEM_KEY) {
961 struct btrfs_dir_item *di;
962 int name_len;
963 char *name;
965 di = (struct btrfs_dir_item *)(sh + 1);
966 name_len = btrfs_stack_dir_name_len(di);
967 name = (char *)(di + 1);
969 if (!strncmp("default", name, name_len))
970 found = btrfs_disk_key_objectid(&di->location);
973 out:
974 *default_id = found;
975 return 0;
978 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
980 int ret;
981 struct btrfs_ioctl_search_args args;
982 struct btrfs_ioctl_search_key *sk = &args.key;
983 struct btrfs_ioctl_search_header sh;
984 struct btrfs_root_ref *ref;
985 struct btrfs_root_item *ri;
986 unsigned long off = 0;
987 int name_len;
988 char *name;
989 u64 dir_id;
990 u64 gen = 0;
991 u64 ogen;
992 u64 flags;
993 int i;
994 time_t t;
995 u8 uuid[BTRFS_UUID_SIZE];
996 u8 puuid[BTRFS_UUID_SIZE];
997 u8 ruuid[BTRFS_UUID_SIZE];
999 root_lookup_init(root_lookup);
1000 memset(&args, 0, sizeof(args));
1002 /* search in the tree of tree roots */
1003 sk->tree_id = 1;
1006 * set the min and max to backref keys. The search will
1007 * only send back this type of key now.
1009 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1010 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1012 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1015 * set all the other params to the max, we'll take any objectid
1016 * and any trans
1018 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1019 sk->max_offset = (u64)-1;
1020 sk->max_transid = (u64)-1;
1022 /* just a big number, doesn't matter much */
1023 sk->nr_items = 4096;
1025 while(1) {
1026 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1027 if (ret < 0)
1028 return ret;
1029 /* the ioctl returns the number of item it found in nr_items */
1030 if (sk->nr_items == 0)
1031 break;
1033 off = 0;
1036 * for each item, pull the key out of the header and then
1037 * read the root_ref item it contains
1039 for (i = 0; i < sk->nr_items; i++) {
1040 memcpy(&sh, args.buf + off, sizeof(sh));
1041 off += sizeof(sh);
1042 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1043 ref = (struct btrfs_root_ref *)(args.buf + off);
1044 name_len = btrfs_stack_root_ref_name_len(ref);
1045 name = (char *)(ref + 1);
1046 dir_id = btrfs_stack_root_ref_dirid(ref);
1048 add_root(root_lookup, sh.objectid, sh.offset,
1049 0, 0, dir_id, name, name_len, 0, 0, 0,
1050 NULL, NULL, NULL);
1051 } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1052 ri = (struct btrfs_root_item *)(args.buf + off);
1053 gen = btrfs_root_generation(ri);
1054 flags = btrfs_root_flags(ri);
1055 if(sh.len >
1056 sizeof(struct btrfs_root_item_v0)) {
1057 t = btrfs_stack_timespec_sec(&ri->otime);
1058 ogen = btrfs_root_otransid(ri);
1059 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1060 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1061 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1062 } else {
1063 t = 0;
1064 ogen = 0;
1065 memset(uuid, 0, BTRFS_UUID_SIZE);
1066 memset(puuid, 0, BTRFS_UUID_SIZE);
1067 memset(ruuid, 0, BTRFS_UUID_SIZE);
1070 add_root(root_lookup, sh.objectid, 0,
1071 sh.offset, flags, 0, NULL, 0, ogen,
1072 gen, t, uuid, puuid, ruuid);
1075 off += sh.len;
1078 * record the mins in sk so we can make sure the
1079 * next search doesn't repeat this root
1081 sk->min_objectid = sh.objectid;
1082 sk->min_type = sh.type;
1083 sk->min_offset = sh.offset;
1085 sk->nr_items = 4096;
1086 sk->min_offset++;
1087 if (!sk->min_offset) /* overflow */
1088 sk->min_type++;
1089 else
1090 continue;
1092 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1093 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1094 sk->min_objectid++;
1095 } else
1096 continue;
1098 if (sk->min_objectid > sk->max_objectid)
1099 break;
1102 return 0;
1105 static int filter_by_rootid(struct root_info *ri, u64 data)
1107 return ri->root_id == data;
1110 static int filter_snapshot(struct root_info *ri, u64 data)
1112 return !!ri->root_offset;
1115 static int filter_flags(struct root_info *ri, u64 flags)
1117 return ri->flags & flags;
1120 static int filter_gen_more(struct root_info *ri, u64 data)
1122 return ri->gen >= data;
1125 static int filter_gen_less(struct root_info *ri, u64 data)
1127 return ri->gen <= data;
1130 static int filter_gen_equal(struct root_info *ri, u64 data)
1132 return ri->gen == data;
1135 static int filter_cgen_more(struct root_info *ri, u64 data)
1137 return ri->ogen >= data;
1140 static int filter_cgen_less(struct root_info *ri, u64 data)
1142 return ri->ogen <= data;
1145 static int filter_cgen_equal(struct root_info *ri, u64 data)
1147 return ri->ogen == data;
1150 static int filter_topid_equal(struct root_info *ri, u64 data)
1152 return ri->top_id == data;
1155 static int filter_full_path(struct root_info *ri, u64 data)
1157 if (ri->full_path && ri->top_id != data) {
1158 char *tmp;
1159 char p[] = "<FS_TREE>";
1160 int add_len = strlen(p);
1161 int len = strlen(ri->full_path);
1163 tmp = malloc(len + add_len + 2);
1164 if (!tmp) {
1165 fprintf(stderr, "memory allocation failed\n");
1166 exit(1);
1168 memcpy(tmp + add_len + 1, ri->full_path, len);
1169 tmp[len + add_len + 1] = '\0';
1170 tmp[add_len] = '/';
1171 memcpy(tmp, p, add_len);
1172 free(ri->full_path);
1173 ri->full_path = tmp;
1175 return 1;
1178 static int filter_by_parent(struct root_info *ri, u64 data)
1180 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1183 static int filter_deleted(struct root_info *ri, u64 data)
1185 return ri->deleted;
1188 static btrfs_list_filter_func all_filter_funcs[] = {
1189 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1190 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1191 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1192 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1193 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1194 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1195 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1196 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1197 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1198 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1199 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1200 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1201 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1204 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1206 struct btrfs_list_filter_set *set;
1207 int size;
1209 size = sizeof(struct btrfs_list_filter_set) +
1210 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1211 set = malloc(size);
1212 if (!set) {
1213 fprintf(stderr, "memory allocation failed\n");
1214 exit(1);
1217 memset(set, 0, size);
1218 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1220 return set;
1223 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1225 free(filter_set);
1228 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1229 enum btrfs_list_filter_enum filter, u64 data)
1231 struct btrfs_list_filter_set *set = *filter_set;
1232 int size;
1234 BUG_ON(!set);
1235 BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1236 BUG_ON(set->nfilters > set->total);
1238 if (set->nfilters == set->total) {
1239 void *tmp;
1241 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1242 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1243 tmp = set;
1244 set = realloc(set, size);
1245 if (!set) {
1246 fprintf(stderr, "memory allocation failed\n");
1247 free(tmp);
1248 exit(1);
1251 memset(&set->filters[set->total], 0,
1252 BTRFS_LIST_NFILTERS_INCREASE *
1253 sizeof(struct btrfs_list_filter));
1254 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1255 *filter_set = set;
1258 BUG_ON(set->filters[set->nfilters].filter_func);
1260 if (filter == BTRFS_LIST_FILTER_DELETED)
1261 set->only_deleted = 1;
1263 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1264 set->filters[set->nfilters].data = data;
1265 set->nfilters++;
1266 return 0;
1269 static int filter_root(struct root_info *ri,
1270 struct btrfs_list_filter_set *set)
1272 int i, ret;
1274 if (!set)
1275 return 1;
1277 if (set->only_deleted && !ri->deleted)
1278 return 0;
1280 if (!set->only_deleted && ri->deleted)
1281 return 0;
1283 for (i = 0; i < set->nfilters; i++) {
1284 if (!set->filters[i].filter_func)
1285 break;
1286 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1287 if (!ret)
1288 return 0;
1290 return 1;
1293 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1294 struct root_lookup *sort_tree,
1295 struct btrfs_list_filter_set *filter_set,
1296 struct btrfs_list_comparer_set *comp_set,
1297 u64 top_id)
1299 struct rb_node *n;
1300 struct root_info *entry;
1301 int ret;
1303 root_lookup_init(sort_tree);
1305 n = rb_last(&all_subvols->root);
1306 while (n) {
1307 entry = rb_entry(n, struct root_info, rb_node);
1309 ret = resolve_root(all_subvols, entry, top_id);
1310 if (ret == -ENOENT) {
1311 entry->full_path = strdup("DELETED");
1312 entry->deleted = 1;
1314 ret = filter_root(entry, filter_set);
1315 if (ret)
1316 sort_tree_insert(sort_tree, entry, comp_set);
1317 n = rb_prev(n);
1321 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1323 struct rb_node *n;
1325 n = rb_first(&root_lookup->root);
1326 while (n) {
1327 struct root_info *entry;
1328 int ret;
1329 entry = rb_entry(n, struct root_info, rb_node);
1330 ret = lookup_ino_path(fd, entry);
1331 if (ret && ret != -ENOENT)
1332 return ret;
1333 n = rb_next(n);
1336 return 0;
1339 static void print_subvolume_column(struct root_info *subv,
1340 enum btrfs_list_column_enum column)
1342 char tstr[256];
1343 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1345 BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1347 switch (column) {
1348 case BTRFS_LIST_OBJECTID:
1349 printf("%llu", subv->root_id);
1350 break;
1351 case BTRFS_LIST_GENERATION:
1352 printf("%llu", subv->gen);
1353 break;
1354 case BTRFS_LIST_OGENERATION:
1355 printf("%llu", subv->ogen);
1356 break;
1357 case BTRFS_LIST_PARENT:
1358 printf("%llu", subv->ref_tree);
1359 break;
1360 case BTRFS_LIST_TOP_LEVEL:
1361 printf("%llu", subv->top_id);
1362 break;
1363 case BTRFS_LIST_OTIME:
1364 if (subv->otime) {
1365 struct tm tm;
1367 localtime_r(&subv->otime, &tm);
1368 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1369 } else
1370 strcpy(tstr, "-");
1371 printf("%s", tstr);
1372 break;
1373 case BTRFS_LIST_UUID:
1374 if (uuid_is_null(subv->uuid))
1375 strcpy(uuidparse, "-");
1376 else
1377 uuid_unparse(subv->uuid, uuidparse);
1378 printf("%s", uuidparse);
1379 break;
1380 case BTRFS_LIST_PUUID:
1381 if (uuid_is_null(subv->puuid))
1382 strcpy(uuidparse, "-");
1383 else
1384 uuid_unparse(subv->puuid, uuidparse);
1385 printf("%s", uuidparse);
1386 break;
1387 case BTRFS_LIST_RUUID:
1388 if (uuid_is_null(subv->ruuid))
1389 strcpy(uuidparse, "-");
1390 else
1391 uuid_unparse(subv->ruuid, uuidparse);
1392 printf("%s", uuidparse);
1393 break;
1394 case BTRFS_LIST_PATH:
1395 BUG_ON(!subv->full_path);
1396 printf("%s", subv->full_path);
1397 break;
1398 default:
1399 break;
1403 static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
1405 int i;
1407 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1408 if (!btrfs_list_columns[i].need_print)
1409 continue;
1411 if (raw_prefix)
1412 printf("%s",raw_prefix);
1414 print_subvolume_column(subv, i);
1416 printf("\n");
1419 static void print_single_volume_info_table(struct root_info *subv)
1421 int i;
1423 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1424 if (!btrfs_list_columns[i].need_print)
1425 continue;
1427 print_subvolume_column(subv, i);
1429 if (i != BTRFS_LIST_PATH)
1430 printf("\t");
1432 if (i == BTRFS_LIST_TOP_LEVEL)
1433 printf("\t");
1435 printf("\n");
1438 static void print_single_volume_info_default(struct root_info *subv)
1440 int i;
1442 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1443 if (!btrfs_list_columns[i].need_print)
1444 continue;
1446 printf("%s ", btrfs_list_columns[i].name);
1447 print_subvolume_column(subv, i);
1449 if (i != BTRFS_LIST_PATH)
1450 printf(" ");
1452 printf("\n");
1455 static void print_all_volume_info_tab_head(void)
1457 int i;
1458 int len;
1459 char barrier[20];
1461 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1462 if (btrfs_list_columns[i].need_print)
1463 printf("%s\t", btrfs_list_columns[i].name);
1465 if (i == BTRFS_LIST_ALL-1)
1466 printf("\n");
1469 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1470 memset(barrier, 0, sizeof(barrier));
1472 if (btrfs_list_columns[i].need_print) {
1473 len = strlen(btrfs_list_columns[i].name);
1474 while (len--)
1475 strcat(barrier, "-");
1477 printf("%s\t", barrier);
1479 if (i == BTRFS_LIST_ALL-1)
1480 printf("\n");
1484 static void print_all_volume_info(struct root_lookup *sorted_tree,
1485 int layout, char *raw_prefix)
1487 struct rb_node *n;
1488 struct root_info *entry;
1490 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1491 print_all_volume_info_tab_head();
1493 n = rb_first(&sorted_tree->root);
1494 while (n) {
1495 entry = rb_entry(n, struct root_info, sort_node);
1496 switch (layout) {
1497 case BTRFS_LIST_LAYOUT_DEFAULT:
1498 print_single_volume_info_default(entry);
1499 break;
1500 case BTRFS_LIST_LAYOUT_TABLE:
1501 print_single_volume_info_table(entry);
1502 break;
1503 case BTRFS_LIST_LAYOUT_RAW:
1504 print_single_volume_info_raw(entry, raw_prefix);
1505 break;
1507 n = rb_next(n);
1511 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1513 int ret;
1515 ret = __list_subvol_search(fd, root_lookup);
1516 if (ret) {
1517 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1518 strerror(errno));
1519 return ret;
1523 * now we have an rbtree full of root_info objects, but we need to fill
1524 * in their path names within the subvol that is referencing each one.
1526 ret = __list_subvol_fill_paths(fd, root_lookup);
1527 return ret;
1530 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1531 struct btrfs_list_comparer_set *comp_set,
1532 int layout, int full_path, char *raw_prefix)
1534 struct root_lookup root_lookup;
1535 struct root_lookup root_sort;
1536 int ret = 0;
1537 u64 top_id = 0;
1539 if (full_path)
1540 ret = btrfs_list_get_path_rootid(fd, &top_id);
1541 if (ret)
1542 return ret;
1544 ret = btrfs_list_subvols(fd, &root_lookup);
1545 if (ret)
1546 return ret;
1547 __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1548 comp_set, top_id);
1550 print_all_volume_info(&root_sort, layout, raw_prefix);
1551 __free_all_subvolumn(&root_lookup);
1553 return 0;
1556 static char *strdup_or_null(const char *s)
1558 if (!s)
1559 return NULL;
1560 return strdup(s);
1563 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1565 int ret, rr;
1566 struct root_lookup rl;
1567 struct rb_node *rbn;
1568 struct root_info *ri;
1569 u64 root_id;
1571 ret = btrfs_list_get_path_rootid(fd, &root_id);
1572 if (ret)
1573 return ret;
1575 ret = btrfs_list_subvols(fd, &rl);
1576 if (ret)
1577 return ret;
1579 rbn = rb_first(&rl.root);
1580 while(rbn) {
1581 ri = rb_entry(rbn, struct root_info, rb_node);
1582 rr = resolve_root(&rl, ri, root_id);
1583 if (rr == -ENOENT) {
1584 ret = -ENOENT;
1585 rbn = rb_next(rbn);
1586 continue;
1588 if (!comp_entry_with_rootid(the_ri, ri, 0)) {
1589 memcpy(the_ri, ri, offsetof(struct root_info, path));
1590 the_ri->path = strdup_or_null(ri->path);
1591 the_ri->name = strdup_or_null(ri->name);
1592 the_ri->full_path = strdup_or_null(ri->full_path);
1593 ret = 0;
1594 break;
1596 rbn = rb_next(rbn);
1598 __free_all_subvolumn(&rl);
1599 return ret;
1602 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1603 struct btrfs_file_extent_item *item,
1604 u64 found_gen, u64 *cache_dirid,
1605 char **cache_dir_name, u64 *cache_ino,
1606 char **cache_full_name)
1608 u64 len = 0;
1609 u64 disk_start = 0;
1610 u64 disk_offset = 0;
1611 u8 type;
1612 int compressed = 0;
1613 int flags = 0;
1614 char *name = NULL;
1616 if (sh->objectid == *cache_ino) {
1617 name = *cache_full_name;
1618 } else if (*cache_full_name) {
1619 free(*cache_full_name);
1620 *cache_full_name = NULL;
1622 if (!name) {
1623 name = ino_resolve(fd, sh->objectid, cache_dirid,
1624 cache_dir_name);
1625 *cache_full_name = name;
1626 *cache_ino = sh->objectid;
1628 if (!name)
1629 return -EIO;
1631 type = btrfs_stack_file_extent_type(item);
1632 compressed = btrfs_stack_file_extent_compression(item);
1634 if (type == BTRFS_FILE_EXTENT_REG ||
1635 type == BTRFS_FILE_EXTENT_PREALLOC) {
1636 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1637 disk_offset = btrfs_stack_file_extent_offset(item);
1638 len = btrfs_stack_file_extent_num_bytes(item);
1639 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1640 disk_start = 0;
1641 disk_offset = 0;
1642 len = btrfs_stack_file_extent_ram_bytes(item);
1643 } else {
1644 printf("unhandled extent type %d for inode %llu "
1645 "file offset %llu gen %llu\n",
1646 type,
1647 (unsigned long long)sh->objectid,
1648 (unsigned long long)sh->offset,
1649 (unsigned long long)found_gen);
1651 return -EIO;
1653 printf("inode %llu file offset %llu len %llu disk start %llu "
1654 "offset %llu gen %llu flags ",
1655 (unsigned long long)sh->objectid,
1656 (unsigned long long)sh->offset,
1657 (unsigned long long)len,
1658 (unsigned long long)disk_start,
1659 (unsigned long long)disk_offset,
1660 (unsigned long long)found_gen);
1662 if (compressed) {
1663 printf("COMPRESS");
1664 flags++;
1666 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1667 printf("%sPREALLOC", flags ? "|" : "");
1668 flags++;
1670 if (type == BTRFS_FILE_EXTENT_INLINE) {
1671 printf("%sINLINE", flags ? "|" : "");
1672 flags++;
1674 if (!flags)
1675 printf("NONE");
1677 printf(" %s\n", name);
1678 return 0;
1681 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1683 int ret;
1684 struct btrfs_ioctl_search_args args;
1685 struct btrfs_ioctl_search_key *sk = &args.key;
1686 struct btrfs_ioctl_search_header sh;
1687 struct btrfs_file_extent_item *item;
1688 unsigned long off = 0;
1689 u64 found_gen;
1690 u64 max_found = 0;
1691 int i;
1692 int e;
1693 u64 cache_dirid = 0;
1694 u64 cache_ino = 0;
1695 char *cache_dir_name = NULL;
1696 char *cache_full_name = NULL;
1697 struct btrfs_file_extent_item backup;
1699 memset(&backup, 0, sizeof(backup));
1700 memset(&args, 0, sizeof(args));
1702 sk->tree_id = root_id;
1705 * set all the other params to the max, we'll take any objectid
1706 * and any trans
1708 sk->max_objectid = (u64)-1;
1709 sk->max_offset = (u64)-1;
1710 sk->max_transid = (u64)-1;
1711 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1712 sk->min_transid = oldest_gen;
1713 /* just a big number, doesn't matter much */
1714 sk->nr_items = 4096;
1716 max_found = find_root_gen(fd);
1717 while(1) {
1718 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1719 e = errno;
1720 if (ret < 0) {
1721 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1722 strerror(e));
1723 break;
1725 /* the ioctl returns the number of item it found in nr_items */
1726 if (sk->nr_items == 0)
1727 break;
1729 off = 0;
1732 * for each item, pull the key out of the header and then
1733 * read the root_ref item it contains
1735 for (i = 0; i < sk->nr_items; i++) {
1736 memcpy(&sh, args.buf + off, sizeof(sh));
1737 off += sizeof(sh);
1740 * just in case the item was too big, pass something other
1741 * than garbage
1743 if (sh.len == 0)
1744 item = &backup;
1745 else
1746 item = (struct btrfs_file_extent_item *)(args.buf +
1747 off);
1748 found_gen = btrfs_stack_file_extent_generation(item);
1749 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1750 found_gen >= oldest_gen) {
1751 print_one_extent(fd, &sh, item, found_gen,
1752 &cache_dirid, &cache_dir_name,
1753 &cache_ino, &cache_full_name);
1755 off += sh.len;
1758 * record the mins in sk so we can make sure the
1759 * next search doesn't repeat this root
1761 sk->min_objectid = sh.objectid;
1762 sk->min_offset = sh.offset;
1763 sk->min_type = sh.type;
1765 sk->nr_items = 4096;
1766 if (sk->min_offset < (u64)-1)
1767 sk->min_offset++;
1768 else if (sk->min_objectid < (u64)-1) {
1769 sk->min_objectid++;
1770 sk->min_offset = 0;
1771 sk->min_type = 0;
1772 } else
1773 break;
1775 free(cache_dir_name);
1776 free(cache_full_name);
1777 printf("transid marker was %llu\n", (unsigned long long)max_found);
1778 return ret;
1781 char *btrfs_list_path_for_root(int fd, u64 root)
1783 struct root_lookup root_lookup;
1784 struct rb_node *n;
1785 char *ret_path = NULL;
1786 int ret;
1787 u64 top_id;
1789 ret = btrfs_list_get_path_rootid(fd, &top_id);
1790 if (ret)
1791 return ERR_PTR(ret);
1793 ret = __list_subvol_search(fd, &root_lookup);
1794 if (ret < 0)
1795 return ERR_PTR(ret);
1797 ret = __list_subvol_fill_paths(fd, &root_lookup);
1798 if (ret < 0)
1799 return ERR_PTR(ret);
1801 n = rb_last(&root_lookup.root);
1802 while (n) {
1803 struct root_info *entry;
1805 entry = rb_entry(n, struct root_info, rb_node);
1806 ret = resolve_root(&root_lookup, entry, top_id);
1807 if (ret == -ENOENT && entry->root_id == root) {
1808 ret_path = NULL;
1809 break;
1811 if (entry->root_id == root) {
1812 ret_path = entry->full_path;
1813 entry->full_path = NULL;
1816 n = rb_prev(n);
1818 __free_all_subvolumn(&root_lookup);
1820 return ret_path;
1823 int btrfs_list_parse_sort_string(char *opt_arg,
1824 struct btrfs_list_comparer_set **comps)
1826 int order;
1827 int flag;
1828 char *p;
1829 char **ptr_argv;
1830 int what_to_sort;
1832 while ((p = strtok(opt_arg, ",")) != NULL) {
1833 flag = 0;
1834 ptr_argv = all_sort_items;
1836 while (*ptr_argv) {
1837 if (strcmp(*ptr_argv, p) == 0) {
1838 flag = 1;
1839 break;
1840 } else {
1841 p++;
1842 if (strcmp(*ptr_argv, p) == 0) {
1843 flag = 1;
1844 p--;
1845 break;
1847 p--;
1849 ptr_argv++;
1852 if (flag == 0)
1853 return -1;
1855 else {
1856 if (*p == '+') {
1857 order = 0;
1858 p++;
1859 } else if (*p == '-') {
1860 order = 1;
1861 p++;
1862 } else
1863 order = 0;
1865 what_to_sort = btrfs_list_get_sort_item(p);
1866 btrfs_list_setup_comparer(comps, what_to_sort, order);
1868 opt_arg = NULL;
1871 return 0;
1875 * This function is used to parse the argument of filter condition.
1877 * type is the filter object.
1879 int btrfs_list_parse_filter_string(char *opt_arg,
1880 struct btrfs_list_filter_set **filters,
1881 enum btrfs_list_filter_enum type)
1884 u64 arg;
1886 switch (*(opt_arg++)) {
1887 case '+':
1888 arg = arg_strtou64(opt_arg);
1889 type += 2;
1891 btrfs_list_setup_filter(filters, type, arg);
1892 break;
1893 case '-':
1894 arg = arg_strtou64(opt_arg);
1895 type += 1;
1897 btrfs_list_setup_filter(filters, type, arg);
1898 break;
1899 default:
1900 opt_arg--;
1901 arg = arg_strtou64(opt_arg);
1903 btrfs_list_setup_filter(filters, type, arg);
1904 break;
1907 return 0;
1910 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1912 int ret;
1913 struct btrfs_ioctl_ino_lookup_args args;
1915 memset(&args, 0, sizeof(args));
1916 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1918 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1919 if (ret < 0) {
1920 fprintf(stderr,
1921 "ERROR: can't perform the search - %s\n",
1922 strerror(errno));
1923 return ret;
1925 *treeid = args.treeid;
1926 return 0;