libbtrfsutil: always build libbtrfsutil.so.$MAJOR
[btrfs-progs-unstable/devel.git] / btrfs-list.c
blobe01c58991dad9e16f46ec00fcc2546b73642a612
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 ASSERT(0 <= column && 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 int comp_entry_with_rootid(struct root_info *entry1,
128 struct root_info *entry2,
129 int is_descending)
131 int ret;
133 if (entry1->root_id > entry2->root_id)
134 ret = 1;
135 else if (entry1->root_id < entry2->root_id)
136 ret = -1;
137 else
138 ret = 0;
140 return is_descending ? -ret : ret;
143 static int comp_entry_with_gen(struct root_info *entry1,
144 struct root_info *entry2,
145 int is_descending)
147 int ret;
149 if (entry1->gen > entry2->gen)
150 ret = 1;
151 else if (entry1->gen < entry2->gen)
152 ret = -1;
153 else
154 ret = 0;
156 return is_descending ? -ret : ret;
159 static int comp_entry_with_ogen(struct root_info *entry1,
160 struct root_info *entry2,
161 int is_descending)
163 int ret;
165 if (entry1->ogen > entry2->ogen)
166 ret = 1;
167 else if (entry1->ogen < entry2->ogen)
168 ret = -1;
169 else
170 ret = 0;
172 return is_descending ? -ret : ret;
175 static int comp_entry_with_path(struct root_info *entry1,
176 struct root_info *entry2,
177 int is_descending)
179 int ret;
181 if (strcmp(entry1->full_path, entry2->full_path) > 0)
182 ret = 1;
183 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
184 ret = -1;
185 else
186 ret = 0;
188 return is_descending ? -ret : ret;
191 static btrfs_list_comp_func all_comp_funcs[] = {
192 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
193 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
194 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
195 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
198 static char *all_sort_items[] = {
199 [BTRFS_LIST_COMP_ROOTID] = "rootid",
200 [BTRFS_LIST_COMP_OGEN] = "ogen",
201 [BTRFS_LIST_COMP_GEN] = "gen",
202 [BTRFS_LIST_COMP_PATH] = "path",
203 [BTRFS_LIST_COMP_MAX] = NULL,
206 static int btrfs_list_get_sort_item(char *sort_name)
208 int i;
210 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
211 if (strcmp(sort_name, all_sort_items[i]) == 0)
212 return i;
214 return -1;
217 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
219 struct btrfs_list_comparer_set *set;
220 int size;
222 size = sizeof(struct btrfs_list_comparer_set) +
223 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
224 set = calloc(1, size);
225 if (!set) {
226 fprintf(stderr, "memory allocation failed\n");
227 exit(1);
230 set->total = BTRFS_LIST_NCOMPS_INCREASE;
232 return set;
235 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
236 enum btrfs_list_comp_enum comparer, int is_descending)
238 struct btrfs_list_comparer_set *set = *comp_set;
239 int size;
241 ASSERT(set != NULL);
242 ASSERT(comparer < BTRFS_LIST_COMP_MAX);
243 ASSERT(set->ncomps <= set->total);
245 if (set->ncomps == set->total) {
246 void *tmp;
248 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
249 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
250 tmp = set;
251 set = realloc(set, size);
252 if (!set) {
253 fprintf(stderr, "memory allocation failed\n");
254 free(tmp);
255 exit(1);
258 memset(&set->comps[set->total], 0,
259 BTRFS_LIST_NCOMPS_INCREASE *
260 sizeof(struct btrfs_list_comparer));
261 set->total += BTRFS_LIST_NCOMPS_INCREASE;
262 *comp_set = set;
265 ASSERT(set->comps[set->ncomps].comp_func == NULL);
267 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
268 set->comps[set->ncomps].is_descending = is_descending;
269 set->ncomps++;
270 return 0;
273 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
274 struct btrfs_list_comparer_set *set)
276 int rootid_compared = 0;
277 int i, ret = 0;
279 if (!set || !set->ncomps)
280 return comp_entry_with_rootid(entry1, entry2, 0);
282 for (i = 0; i < set->ncomps; i++) {
283 if (!set->comps[i].comp_func)
284 break;
286 ret = set->comps[i].comp_func(entry1, entry2,
287 set->comps[i].is_descending);
288 if (ret)
289 return ret;
291 if (set->comps[i].comp_func == comp_entry_with_rootid)
292 rootid_compared = 1;
295 if (!rootid_compared)
296 ret = comp_entry_with_rootid(entry1, entry2, 0);
298 return ret;
301 static int sort_tree_insert(struct root_lookup *sort_tree,
302 struct root_info *ins,
303 struct btrfs_list_comparer_set *comp_set)
305 struct rb_node **p = &sort_tree->root.rb_node;
306 struct rb_node *parent = NULL;
307 struct root_info *curr;
308 int ret;
310 while (*p) {
311 parent = *p;
312 curr = rb_entry(parent, struct root_info, sort_node);
314 ret = sort_comp(ins, curr, comp_set);
315 if (ret < 0)
316 p = &(*p)->rb_left;
317 else if (ret > 0)
318 p = &(*p)->rb_right;
319 else
320 return -EEXIST;
323 rb_link_node(&ins->sort_node, parent, p);
324 rb_insert_color(&ins->sort_node, &sort_tree->root);
325 return 0;
329 * insert a new root into the tree. returns the existing root entry
330 * if one is already there. Both root_id and ref_tree are used
331 * as the key
333 static int root_tree_insert(struct root_lookup *root_tree,
334 struct root_info *ins)
336 struct rb_node **p = &root_tree->root.rb_node;
337 struct rb_node * parent = NULL;
338 struct root_info *curr;
339 int ret;
341 while(*p) {
342 parent = *p;
343 curr = rb_entry(parent, struct root_info, rb_node);
345 ret = comp_entry_with_rootid(ins, curr, 0);
346 if (ret < 0)
347 p = &(*p)->rb_left;
348 else if (ret > 0)
349 p = &(*p)->rb_right;
350 else
351 return -EEXIST;
354 rb_link_node(&ins->rb_node, parent, p);
355 rb_insert_color(&ins->rb_node, &root_tree->root);
356 return 0;
360 * find a given root id in the tree. We return the smallest one,
361 * rb_next can be used to move forward looking for more if required
363 static struct root_info *root_tree_search(struct root_lookup *root_tree,
364 u64 root_id)
366 struct rb_node *n = root_tree->root.rb_node;
367 struct root_info *entry;
368 struct root_info tmp;
369 int ret;
371 tmp.root_id = root_id;
373 while(n) {
374 entry = rb_entry(n, struct root_info, rb_node);
376 ret = comp_entry_with_rootid(&tmp, entry, 0);
377 if (ret < 0)
378 n = n->rb_left;
379 else if (ret > 0)
380 n = n->rb_right;
381 else
382 return entry;
384 return NULL;
387 static int update_root(struct root_lookup *root_lookup,
388 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
389 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
390 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
392 struct root_info *ri;
394 ri = root_tree_search(root_lookup, root_id);
395 if (!ri || ri->root_id != root_id)
396 return -ENOENT;
397 if (name && name_len > 0) {
398 free(ri->name);
400 ri->name = malloc(name_len + 1);
401 if (!ri->name) {
402 fprintf(stderr, "memory allocation failed\n");
403 exit(1);
405 strncpy(ri->name, name, name_len);
406 ri->name[name_len] = 0;
408 if (ref_tree)
409 ri->ref_tree = ref_tree;
410 if (root_offset)
411 ri->root_offset = root_offset;
412 if (flags)
413 ri->flags = flags;
414 if (dir_id)
415 ri->dir_id = dir_id;
416 if (gen)
417 ri->gen = gen;
418 if (ogen)
419 ri->ogen = ogen;
420 if (!ri->ogen && root_offset)
421 ri->ogen = root_offset;
422 if (otime)
423 ri->otime = otime;
424 if (uuid)
425 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
426 if (puuid)
427 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
428 if (ruuid)
429 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
431 return 0;
435 * add_root - update the existed root, or allocate a new root and insert it
436 * into the lookup tree.
437 * root_id: object id of the root
438 * ref_tree: object id of the referring root.
439 * root_offset: offset value of the root'key
440 * dir_id: inode id of the directory in ref_tree where this root can be found.
441 * name: the name of root_id in that directory
442 * name_len: the length of name
443 * ogen: the original generation of the root
444 * gen: the current generation of the root
445 * otime: the original time (creation time) of the root
446 * uuid: uuid of the root
447 * puuid: uuid of the root parent if any
448 * ruuid: uuid of the received subvol, if any
450 static int add_root(struct root_lookup *root_lookup,
451 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
452 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
453 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
455 struct root_info *ri;
456 int ret;
458 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
459 dir_id, name, name_len, ogen, gen, otime,
460 uuid, puuid, ruuid);
461 if (!ret)
462 return 0;
464 ri = calloc(1, sizeof(*ri));
465 if (!ri) {
466 printf("memory allocation failed\n");
467 exit(1);
469 ri->root_id = root_id;
471 if (name && name_len > 0) {
472 ri->name = malloc(name_len + 1);
473 if (!ri->name) {
474 fprintf(stderr, "memory allocation failed\n");
475 exit(1);
477 strncpy(ri->name, name, name_len);
478 ri->name[name_len] = 0;
480 if (ref_tree)
481 ri->ref_tree = ref_tree;
482 if (dir_id)
483 ri->dir_id = dir_id;
484 if (root_offset)
485 ri->root_offset = root_offset;
486 if (flags)
487 ri->flags = flags;
488 if (gen)
489 ri->gen = gen;
490 if (ogen)
491 ri->ogen = ogen;
492 if (!ri->ogen && root_offset)
493 ri->ogen = root_offset;
494 if (otime)
495 ri->otime = otime;
497 if (uuid)
498 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
500 if (puuid)
501 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
503 if (ruuid)
504 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
506 ret = root_tree_insert(root_lookup, ri);
507 if (ret < 0) {
508 error("failed to insert subvolume %llu to tree: %s",
509 (unsigned long long)root_id, strerror(-ret));
510 exit(1);
512 return 0;
516 * Simplified add_root for back references, omits the uuid and original info
517 * parameters, root offset and flags.
519 static int add_root_backref(struct root_lookup *root_lookup, u64 root_id,
520 u64 ref_tree, u64 dir_id, char *name, int name_len)
522 return add_root(root_lookup, root_id, ref_tree, 0, 0, dir_id, name,
523 name_len, 0, 0, 0, NULL, NULL, NULL);
527 static void free_root_info(struct rb_node *node)
529 struct root_info *ri;
531 ri = rb_entry(node, struct root_info, rb_node);
532 free(ri->name);
533 free(ri->path);
534 free(ri->full_path);
535 free(ri);
539 * for a given root_info, search through the root_lookup tree to construct
540 * the full path name to it.
542 * This can't be called until all the root_info->path fields are filled
543 * in by lookup_ino_path
545 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
546 u64 top_id)
548 char *full_path = NULL;
549 int len = 0;
550 struct root_info *found;
553 * we go backwards from the root_info object and add pathnames
554 * from parent directories as we go.
556 found = ri;
557 while (1) {
558 char *tmp;
559 u64 next;
560 int add_len;
563 * ref_tree = 0 indicates the subvolume
564 * has been deleted.
566 if (!found->ref_tree) {
567 free(full_path);
568 return -ENOENT;
571 add_len = strlen(found->path);
573 if (full_path) {
574 /* room for / and for null */
575 tmp = malloc(add_len + 2 + len);
576 if (!tmp) {
577 perror("malloc failed");
578 exit(1);
580 memcpy(tmp + add_len + 1, full_path, len);
581 tmp[add_len] = '/';
582 memcpy(tmp, found->path, add_len);
583 tmp [add_len + len + 1] = '\0';
584 free(full_path);
585 full_path = tmp;
586 len += add_len + 1;
587 } else {
588 full_path = strdup(found->path);
589 len = add_len;
591 if (!ri->top_id)
592 ri->top_id = found->ref_tree;
594 next = found->ref_tree;
595 if (next == top_id)
596 break;
598 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
599 * we are at the top
601 if (next == BTRFS_FS_TREE_OBJECTID)
602 break;
604 * if the ref_tree wasn't in our tree of roots, the
605 * subvolume was deleted.
607 found = root_tree_search(rl, next);
608 if (!found) {
609 free(full_path);
610 return -ENOENT;
614 ri->full_path = full_path;
616 return 0;
620 * for a single root_info, ask the kernel to give us a path name
621 * inside it's ref_root for the dir_id where it lives.
623 * This fills in root_info->path with the path to the directory and and
624 * appends this root's name.
626 static int lookup_ino_path(int fd, struct root_info *ri)
628 struct btrfs_ioctl_ino_lookup_args args;
629 int ret;
631 if (ri->path)
632 return 0;
634 if (!ri->ref_tree)
635 return -ENOENT;
637 memset(&args, 0, sizeof(args));
638 args.treeid = ri->ref_tree;
639 args.objectid = ri->dir_id;
641 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
642 if (ret < 0) {
643 if (errno == ENOENT) {
644 ri->ref_tree = 0;
645 return -ENOENT;
647 error("failed to lookup path for root %llu: %m",
648 (unsigned long long)ri->ref_tree);
649 return ret;
652 if (args.name[0]) {
654 * we're in a subdirectory of ref_tree, the kernel ioctl
655 * puts a / in there for us
657 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
658 if (!ri->path) {
659 perror("malloc failed");
660 exit(1);
662 strcpy(ri->path, args.name);
663 strcat(ri->path, ri->name);
664 } else {
665 /* we're at the root of ref_tree */
666 ri->path = strdup(ri->name);
667 if (!ri->path) {
668 perror("strdup failed");
669 exit(1);
672 return 0;
675 /* finding the generation for a given path is a two step process.
676 * First we use the inode lookup routine to find out the root id
678 * Then we use the tree search ioctl to scan all the root items for a
679 * given root id and spit out the latest generation we can find
681 static u64 find_root_gen(int fd)
683 struct btrfs_ioctl_ino_lookup_args ino_args;
684 int ret;
685 struct btrfs_ioctl_search_args args;
686 struct btrfs_ioctl_search_key *sk = &args.key;
687 struct btrfs_ioctl_search_header sh;
688 unsigned long off = 0;
689 u64 max_found = 0;
690 int i;
692 memset(&ino_args, 0, sizeof(ino_args));
693 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
695 /* this ioctl fills in ino_args->treeid */
696 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
697 if (ret < 0) {
698 error("failed to lookup path for dirid %llu: %m",
699 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
700 return 0;
703 memset(&args, 0, sizeof(args));
705 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
708 * there may be more than one ROOT_ITEM key if there are
709 * snapshots pending deletion, we have to loop through
710 * them.
712 sk->min_objectid = ino_args.treeid;
713 sk->max_objectid = ino_args.treeid;
714 sk->max_type = BTRFS_ROOT_ITEM_KEY;
715 sk->min_type = BTRFS_ROOT_ITEM_KEY;
716 sk->max_offset = (u64)-1;
717 sk->max_transid = (u64)-1;
718 sk->nr_items = 4096;
720 while (1) {
721 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
722 if (ret < 0) {
723 error("can't perform the search: %m");
724 return 0;
726 /* the ioctl returns the number of item it found in nr_items */
727 if (sk->nr_items == 0)
728 break;
730 off = 0;
731 for (i = 0; i < sk->nr_items; i++) {
732 struct btrfs_root_item *item;
734 memcpy(&sh, args.buf + off, sizeof(sh));
735 off += sizeof(sh);
736 item = (struct btrfs_root_item *)(args.buf + off);
737 off += sh.len;
739 sk->min_objectid = sh.objectid;
740 sk->min_type = sh.type;
741 sk->min_offset = sh.offset;
743 if (sh.objectid > ino_args.treeid)
744 break;
746 if (sh.objectid == ino_args.treeid &&
747 sh.type == BTRFS_ROOT_ITEM_KEY) {
748 max_found = max(max_found,
749 btrfs_root_generation(item));
752 if (sk->min_offset < (u64)-1)
753 sk->min_offset++;
754 else
755 break;
757 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
758 break;
759 if (sk->min_objectid != ino_args.treeid)
760 break;
762 return max_found;
765 /* pass in a directory id and this will return
766 * the full path of the parent directory inside its
767 * subvolume root.
769 * It may return NULL if it is in the root, or an ERR_PTR if things
770 * go badly.
772 static char *__ino_resolve(int fd, u64 dirid)
774 struct btrfs_ioctl_ino_lookup_args args;
775 int ret;
776 char *full;
778 memset(&args, 0, sizeof(args));
779 args.objectid = dirid;
781 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
782 if (ret < 0) {
783 error("failed to lookup path for dirid %llu: %m",
784 (unsigned long long)dirid);
785 return ERR_PTR(ret);
788 if (args.name[0]) {
790 * we're in a subdirectory of ref_tree, the kernel ioctl
791 * puts a / in there for us
793 full = strdup(args.name);
794 if (!full) {
795 perror("malloc failed");
796 return ERR_PTR(-ENOMEM);
798 } else {
799 /* we're at the root of ref_tree */
800 full = NULL;
802 return full;
806 * simple string builder, returning a new string with both
807 * dirid and name
809 static char *build_name(const char *dirid, const char *name)
811 char *full;
813 if (!dirid)
814 return strdup(name);
816 full = malloc(strlen(dirid) + strlen(name) + 1);
817 if (!full)
818 return NULL;
819 strcpy(full, dirid);
820 strcat(full, name);
821 return full;
825 * given an inode number, this returns the full path name inside the subvolume
826 * to that file/directory. cache_dirid and cache_name are used to
827 * cache the results so we can avoid tree searches if a later call goes
828 * to the same directory or file name
830 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
833 u64 dirid;
834 char *dirname;
835 char *name;
836 char *full;
837 int ret;
838 struct btrfs_ioctl_search_args args;
839 struct btrfs_ioctl_search_key *sk = &args.key;
840 struct btrfs_ioctl_search_header *sh;
841 unsigned long off = 0;
842 int namelen;
844 memset(&args, 0, sizeof(args));
846 sk->tree_id = 0;
849 * step one, we search for the inode back ref. We just use the first
850 * one
852 sk->min_objectid = ino;
853 sk->max_objectid = ino;
854 sk->max_type = BTRFS_INODE_REF_KEY;
855 sk->max_offset = (u64)-1;
856 sk->min_type = BTRFS_INODE_REF_KEY;
857 sk->max_transid = (u64)-1;
858 sk->nr_items = 1;
860 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
861 if (ret < 0) {
862 error("can't perform the search: %m");
863 return NULL;
865 /* the ioctl returns the number of item it found in nr_items */
866 if (sk->nr_items == 0)
867 return NULL;
869 off = 0;
870 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
872 if (btrfs_search_header_type(sh) == BTRFS_INODE_REF_KEY) {
873 struct btrfs_inode_ref *ref;
874 dirid = btrfs_search_header_offset(sh);
876 ref = (struct btrfs_inode_ref *)(sh + 1);
877 namelen = btrfs_stack_inode_ref_name_len(ref);
879 name = (char *)(ref + 1);
880 name = strndup(name, namelen);
882 /* use our cached value */
883 if (dirid == *cache_dirid && *cache_name) {
884 dirname = *cache_name;
885 goto build;
887 } else {
888 return NULL;
891 * the inode backref gives us the file name and the parent directory id.
892 * From here we use __ino_resolve to get the path to the parent
894 dirname = __ino_resolve(fd, dirid);
895 build:
896 full = build_name(dirname, name);
897 if (*cache_name && dirname != *cache_name)
898 free(*cache_name);
900 *cache_name = dirname;
901 *cache_dirid = dirid;
902 free(name);
904 return full;
907 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
909 struct btrfs_ioctl_search_args args;
910 struct btrfs_ioctl_search_key *sk = &args.key;
911 struct btrfs_ioctl_search_header *sh;
912 u64 found = 0;
913 int ret;
915 memset(&args, 0, sizeof(args));
918 * search for a dir item with a name 'default' in the tree of
919 * tree roots, it should point us to a default root
921 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
923 /* don't worry about ancient format and request only one item */
924 sk->nr_items = 1;
926 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
927 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
928 sk->max_type = BTRFS_DIR_ITEM_KEY;
929 sk->min_type = BTRFS_DIR_ITEM_KEY;
930 sk->max_offset = (u64)-1;
931 sk->max_transid = (u64)-1;
933 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
934 if (ret < 0)
935 return ret;
937 /* the ioctl returns the number of items it found in nr_items */
938 if (sk->nr_items == 0)
939 goto out;
941 sh = (struct btrfs_ioctl_search_header *)args.buf;
943 if (btrfs_search_header_type(sh) == BTRFS_DIR_ITEM_KEY) {
944 struct btrfs_dir_item *di;
945 int name_len;
946 char *name;
948 di = (struct btrfs_dir_item *)(sh + 1);
949 name_len = btrfs_stack_dir_name_len(di);
950 name = (char *)(di + 1);
952 if (!strncmp("default", name, name_len))
953 found = btrfs_disk_key_objectid(&di->location);
956 out:
957 *default_id = found;
958 return 0;
961 static int list_subvol_search(int fd, struct root_lookup *root_lookup)
963 int ret;
964 struct btrfs_ioctl_search_args args;
965 struct btrfs_ioctl_search_key *sk = &args.key;
966 struct btrfs_ioctl_search_header sh;
967 struct btrfs_root_ref *ref;
968 struct btrfs_root_item *ri;
969 unsigned long off;
970 int name_len;
971 char *name;
972 u64 dir_id;
973 u64 gen = 0;
974 u64 ogen;
975 u64 flags;
976 int i;
978 root_lookup->root.rb_node = NULL;
979 memset(&args, 0, sizeof(args));
981 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
982 /* Search both live and deleted subvolumes */
983 sk->min_type = BTRFS_ROOT_ITEM_KEY;
984 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
985 sk->min_objectid = BTRFS_FS_TREE_OBJECTID;
986 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
987 sk->max_offset = (u64)-1;
988 sk->max_transid = (u64)-1;
990 while(1) {
991 sk->nr_items = 4096;
992 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
993 if (ret < 0)
994 return ret;
995 if (sk->nr_items == 0)
996 break;
998 off = 0;
1001 * for each item, pull the key out of the header and then
1002 * read the root_ref item it contains
1004 for (i = 0; i < sk->nr_items; i++) {
1005 memcpy(&sh, args.buf + off, sizeof(sh));
1006 off += sizeof(sh);
1007 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1008 ref = (struct btrfs_root_ref *)(args.buf + off);
1009 name_len = btrfs_stack_root_ref_name_len(ref);
1010 name = (char *)(ref + 1);
1011 dir_id = btrfs_stack_root_ref_dirid(ref);
1013 add_root_backref(root_lookup, sh.objectid,
1014 sh.offset, dir_id, name,
1015 name_len);
1016 } else if (sh.type == BTRFS_ROOT_ITEM_KEY &&
1017 (sh.objectid >= BTRFS_FIRST_FREE_OBJECTID ||
1018 sh.objectid == BTRFS_FS_TREE_OBJECTID)) {
1019 time_t otime;
1020 u8 uuid[BTRFS_UUID_SIZE];
1021 u8 puuid[BTRFS_UUID_SIZE];
1022 u8 ruuid[BTRFS_UUID_SIZE];
1024 ri = (struct btrfs_root_item *)(args.buf + off);
1025 gen = btrfs_root_generation(ri);
1026 flags = btrfs_root_flags(ri);
1027 if(sh.len >
1028 sizeof(struct btrfs_root_item_v0)) {
1029 otime = btrfs_stack_timespec_sec(&ri->otime);
1030 ogen = btrfs_root_otransid(ri);
1031 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1032 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1033 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1034 } else {
1035 otime = 0;
1036 ogen = 0;
1037 memset(uuid, 0, BTRFS_UUID_SIZE);
1038 memset(puuid, 0, BTRFS_UUID_SIZE);
1039 memset(ruuid, 0, BTRFS_UUID_SIZE);
1042 add_root(root_lookup, sh.objectid, 0,
1043 sh.offset, flags, 0, NULL, 0, ogen,
1044 gen, otime, uuid, puuid, ruuid);
1047 off += sh.len;
1049 sk->min_objectid = sh.objectid;
1050 sk->min_type = sh.type;
1051 sk->min_offset = sh.offset;
1053 sk->min_offset++;
1054 if (!sk->min_offset)
1055 sk->min_type++;
1056 else
1057 continue;
1059 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1060 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1061 sk->min_objectid++;
1062 } else
1063 continue;
1065 if (sk->min_objectid > sk->max_objectid)
1066 break;
1069 return 0;
1072 static int filter_by_rootid(struct root_info *ri, u64 data)
1074 return ri->root_id == data;
1077 static int filter_snapshot(struct root_info *ri, u64 data)
1079 return !!ri->root_offset;
1082 static int filter_flags(struct root_info *ri, u64 flags)
1084 return ri->flags & flags;
1087 static int filter_gen_more(struct root_info *ri, u64 data)
1089 return ri->gen >= data;
1092 static int filter_gen_less(struct root_info *ri, u64 data)
1094 return ri->gen <= data;
1097 static int filter_gen_equal(struct root_info *ri, u64 data)
1099 return ri->gen == data;
1102 static int filter_cgen_more(struct root_info *ri, u64 data)
1104 return ri->ogen >= data;
1107 static int filter_cgen_less(struct root_info *ri, u64 data)
1109 return ri->ogen <= data;
1112 static int filter_cgen_equal(struct root_info *ri, u64 data)
1114 return ri->ogen == data;
1117 static int filter_topid_equal(struct root_info *ri, u64 data)
1119 return ri->top_id == data;
1122 static int filter_full_path(struct root_info *ri, u64 data)
1124 if (ri->full_path && ri->top_id != data) {
1125 char *tmp;
1126 char p[] = "<FS_TREE>";
1127 int add_len = strlen(p);
1128 int len = strlen(ri->full_path);
1130 tmp = malloc(len + add_len + 2);
1131 if (!tmp) {
1132 fprintf(stderr, "memory allocation failed\n");
1133 exit(1);
1135 memcpy(tmp + add_len + 1, ri->full_path, len);
1136 tmp[len + add_len + 1] = '\0';
1137 tmp[add_len] = '/';
1138 memcpy(tmp, p, add_len);
1139 free(ri->full_path);
1140 ri->full_path = tmp;
1142 return 1;
1145 static int filter_by_parent(struct root_info *ri, u64 data)
1147 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1150 static int filter_deleted(struct root_info *ri, u64 data)
1152 return ri->deleted;
1155 static btrfs_list_filter_func all_filter_funcs[] = {
1156 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1157 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1158 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1159 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1160 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1161 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1162 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1163 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1164 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1165 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1166 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1167 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1168 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1171 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1173 struct btrfs_list_filter_set *set;
1174 int size;
1176 size = sizeof(struct btrfs_list_filter_set) +
1177 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1178 set = calloc(1, size);
1179 if (!set) {
1180 fprintf(stderr, "memory allocation failed\n");
1181 exit(1);
1184 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1186 return set;
1190 * Setup list filters. Exit if there's not enough memory, as we can't continue
1191 * without the structures set up properly.
1193 void btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1194 enum btrfs_list_filter_enum filter, u64 data)
1196 struct btrfs_list_filter_set *set = *filter_set;
1197 int size;
1199 ASSERT(set != NULL);
1200 ASSERT(filter < BTRFS_LIST_FILTER_MAX);
1201 ASSERT(set->nfilters <= set->total);
1203 if (set->nfilters == set->total) {
1204 void *tmp;
1206 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1207 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1208 tmp = set;
1209 set = realloc(set, size);
1210 if (!set) {
1211 fprintf(stderr, "memory allocation failed\n");
1212 free(tmp);
1213 exit(1);
1216 memset(&set->filters[set->total], 0,
1217 BTRFS_LIST_NFILTERS_INCREASE *
1218 sizeof(struct btrfs_list_filter));
1219 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1220 *filter_set = set;
1223 ASSERT(set->filters[set->nfilters].filter_func == NULL);
1225 if (filter == BTRFS_LIST_FILTER_DELETED)
1226 set->only_deleted = 1;
1228 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1229 set->filters[set->nfilters].data = data;
1230 set->nfilters++;
1233 static int filter_root(struct root_info *ri,
1234 struct btrfs_list_filter_set *set)
1236 int i, ret;
1238 if (!set)
1239 return 1;
1241 if (set->only_deleted && !ri->deleted)
1242 return 0;
1244 if (!set->only_deleted && ri->deleted)
1245 return 0;
1247 for (i = 0; i < set->nfilters; i++) {
1248 if (!set->filters[i].filter_func)
1249 break;
1250 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1251 if (!ret)
1252 return 0;
1254 return 1;
1257 static void filter_and_sort_subvol(struct root_lookup *all_subvols,
1258 struct root_lookup *sort_tree,
1259 struct btrfs_list_filter_set *filter_set,
1260 struct btrfs_list_comparer_set *comp_set,
1261 u64 top_id)
1263 struct rb_node *n;
1264 struct root_info *entry;
1265 int ret;
1267 sort_tree->root.rb_node = NULL;
1269 n = rb_last(&all_subvols->root);
1270 while (n) {
1271 entry = rb_entry(n, struct root_info, rb_node);
1273 ret = resolve_root(all_subvols, entry, top_id);
1274 if (ret == -ENOENT) {
1275 if (entry->root_id != BTRFS_FS_TREE_OBJECTID) {
1276 entry->full_path = strdup("DELETED");
1277 entry->deleted = 1;
1278 } else {
1280 * The full path is not supposed to be printed,
1281 * but we don't want to print an empty string,
1282 * in case it appears somewhere.
1284 entry->full_path = strdup("TOPLEVEL");
1285 entry->deleted = 0;
1288 ret = filter_root(entry, filter_set);
1289 if (ret)
1290 sort_tree_insert(sort_tree, entry, comp_set);
1291 n = rb_prev(n);
1295 static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1297 struct rb_node *n;
1299 n = rb_first(&root_lookup->root);
1300 while (n) {
1301 struct root_info *entry;
1302 int ret;
1303 entry = rb_entry(n, struct root_info, rb_node);
1304 ret = lookup_ino_path(fd, entry);
1305 if (ret && ret != -ENOENT)
1306 return ret;
1307 n = rb_next(n);
1310 return 0;
1313 static void print_subvolume_column(struct root_info *subv,
1314 enum btrfs_list_column_enum column)
1316 char tstr[256];
1317 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1319 ASSERT(0 <= column && column < BTRFS_LIST_ALL);
1321 switch (column) {
1322 case BTRFS_LIST_OBJECTID:
1323 printf("%llu", subv->root_id);
1324 break;
1325 case BTRFS_LIST_GENERATION:
1326 printf("%llu", subv->gen);
1327 break;
1328 case BTRFS_LIST_OGENERATION:
1329 printf("%llu", subv->ogen);
1330 break;
1331 case BTRFS_LIST_PARENT:
1332 printf("%llu", subv->ref_tree);
1333 break;
1334 case BTRFS_LIST_TOP_LEVEL:
1335 printf("%llu", subv->top_id);
1336 break;
1337 case BTRFS_LIST_OTIME:
1338 if (subv->otime) {
1339 struct tm tm;
1341 localtime_r(&subv->otime, &tm);
1342 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1343 } else
1344 strcpy(tstr, "-");
1345 printf("%s", tstr);
1346 break;
1347 case BTRFS_LIST_UUID:
1348 if (uuid_is_null(subv->uuid))
1349 strcpy(uuidparse, "-");
1350 else
1351 uuid_unparse(subv->uuid, uuidparse);
1352 printf("%-36s", uuidparse);
1353 break;
1354 case BTRFS_LIST_PUUID:
1355 if (uuid_is_null(subv->puuid))
1356 strcpy(uuidparse, "-");
1357 else
1358 uuid_unparse(subv->puuid, uuidparse);
1359 printf("%-36s", uuidparse);
1360 break;
1361 case BTRFS_LIST_RUUID:
1362 if (uuid_is_null(subv->ruuid))
1363 strcpy(uuidparse, "-");
1364 else
1365 uuid_unparse(subv->ruuid, uuidparse);
1366 printf("%-36s", uuidparse);
1367 break;
1368 case BTRFS_LIST_PATH:
1369 BUG_ON(!subv->full_path);
1370 printf("%s", subv->full_path);
1371 break;
1372 default:
1373 break;
1377 static void print_one_subvol_info_raw(struct root_info *subv,
1378 const char *raw_prefix)
1380 int i;
1382 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1383 if (!btrfs_list_columns[i].need_print)
1384 continue;
1386 if (raw_prefix)
1387 printf("%s",raw_prefix);
1389 print_subvolume_column(subv, i);
1391 printf("\n");
1394 static void print_one_subvol_info_table(struct root_info *subv)
1396 int i;
1398 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1399 if (!btrfs_list_columns[i].need_print)
1400 continue;
1402 print_subvolume_column(subv, i);
1404 if (i != BTRFS_LIST_PATH)
1405 printf("\t");
1407 if (i == BTRFS_LIST_TOP_LEVEL)
1408 printf("\t");
1410 printf("\n");
1413 static void print_one_subvol_info_default(struct root_info *subv)
1415 int i;
1417 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1418 if (!btrfs_list_columns[i].need_print)
1419 continue;
1421 printf("%s ", btrfs_list_columns[i].name);
1422 print_subvolume_column(subv, i);
1424 if (i != BTRFS_LIST_PATH)
1425 printf(" ");
1427 printf("\n");
1430 static void print_all_subvol_info_tab_head(void)
1432 int i;
1433 int len;
1434 char barrier[20];
1436 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1437 if (btrfs_list_columns[i].need_print)
1438 printf("%s\t", btrfs_list_columns[i].name);
1440 if (i == BTRFS_LIST_ALL-1)
1441 printf("\n");
1444 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1445 memset(barrier, 0, sizeof(barrier));
1447 if (btrfs_list_columns[i].need_print) {
1448 len = strlen(btrfs_list_columns[i].name);
1449 while (len--)
1450 strcat(barrier, "-");
1452 printf("%s\t", barrier);
1454 if (i == BTRFS_LIST_ALL-1)
1455 printf("\n");
1459 static void print_all_subvol_info(struct root_lookup *sorted_tree,
1460 enum btrfs_list_layout layout, const char *raw_prefix)
1462 struct rb_node *n;
1463 struct root_info *entry;
1465 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1466 print_all_subvol_info_tab_head();
1468 n = rb_first(&sorted_tree->root);
1469 while (n) {
1470 entry = rb_entry(n, struct root_info, sort_node);
1472 /* The toplevel subvolume is not listed by default */
1473 if (entry->root_id == BTRFS_FS_TREE_OBJECTID)
1474 goto next;
1476 switch (layout) {
1477 case BTRFS_LIST_LAYOUT_DEFAULT:
1478 print_one_subvol_info_default(entry);
1479 break;
1480 case BTRFS_LIST_LAYOUT_TABLE:
1481 print_one_subvol_info_table(entry);
1482 break;
1483 case BTRFS_LIST_LAYOUT_RAW:
1484 print_one_subvol_info_raw(entry, raw_prefix);
1485 break;
1487 next:
1488 n = rb_next(n);
1492 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1494 int ret;
1496 ret = list_subvol_search(fd, root_lookup);
1497 if (ret) {
1498 error("can't perform the search: %m");
1499 return ret;
1503 * now we have an rbtree full of root_info objects, but we need to fill
1504 * in their path names within the subvol that is referencing each one.
1506 ret = list_subvol_fill_paths(fd, root_lookup);
1507 return ret;
1510 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1511 struct btrfs_list_comparer_set *comp_set,
1512 enum btrfs_list_layout layout, int full_path,
1513 const char *raw_prefix)
1515 struct root_lookup root_lookup;
1516 struct root_lookup root_sort;
1517 int ret = 0;
1518 u64 top_id = 0;
1520 if (full_path)
1521 ret = btrfs_list_get_path_rootid(fd, &top_id);
1522 if (ret)
1523 return ret;
1525 ret = btrfs_list_subvols(fd, &root_lookup);
1526 if (ret)
1527 return ret;
1528 filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1529 comp_set, top_id);
1531 print_all_subvol_info(&root_sort, layout, raw_prefix);
1532 rb_free_nodes(&root_lookup.root, free_root_info);
1534 return 0;
1537 static char *strdup_or_null(const char *s)
1539 if (!s)
1540 return NULL;
1541 return strdup(s);
1544 int btrfs_get_toplevel_subvol(int fd, struct root_info *the_ri)
1546 int ret;
1547 struct root_lookup rl;
1548 struct rb_node *rbn;
1549 struct root_info *ri;
1550 u64 root_id;
1552 ret = btrfs_list_get_path_rootid(fd, &root_id);
1553 if (ret)
1554 return ret;
1556 ret = btrfs_list_subvols(fd, &rl);
1557 if (ret)
1558 return ret;
1560 rbn = rb_first(&rl.root);
1561 ri = rb_entry(rbn, struct root_info, rb_node);
1563 if (ri->root_id != BTRFS_FS_TREE_OBJECTID)
1564 return -ENOENT;
1566 memcpy(the_ri, ri, offsetof(struct root_info, path));
1567 the_ri->path = strdup_or_null("/");
1568 the_ri->name = strdup_or_null("<FS_TREE>");
1569 the_ri->full_path = strdup_or_null("/");
1570 rb_free_nodes(&rl.root, free_root_info);
1572 return ret;
1575 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1577 int ret, rr;
1578 struct root_lookup rl;
1579 struct rb_node *rbn;
1580 struct root_info *ri;
1581 u64 root_id;
1583 ret = btrfs_list_get_path_rootid(fd, &root_id);
1584 if (ret)
1585 return ret;
1587 ret = btrfs_list_subvols(fd, &rl);
1588 if (ret)
1589 return ret;
1591 rbn = rb_first(&rl.root);
1592 while(rbn) {
1593 ri = rb_entry(rbn, struct root_info, rb_node);
1594 rr = resolve_root(&rl, ri, root_id);
1595 if (rr == -ENOENT) {
1596 ret = -ENOENT;
1597 rbn = rb_next(rbn);
1598 continue;
1601 if (!comp_entry_with_rootid(the_ri, ri, 0) ||
1602 !uuid_compare(the_ri->uuid, ri->uuid)) {
1603 memcpy(the_ri, ri, offsetof(struct root_info, path));
1604 the_ri->path = strdup_or_null(ri->path);
1605 the_ri->name = strdup_or_null(ri->name);
1606 the_ri->full_path = strdup_or_null(ri->full_path);
1607 ret = 0;
1608 break;
1610 rbn = rb_next(rbn);
1612 rb_free_nodes(&rl.root, free_root_info);
1613 return ret;
1616 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1617 struct btrfs_file_extent_item *item,
1618 u64 found_gen, u64 *cache_dirid,
1619 char **cache_dir_name, u64 *cache_ino,
1620 char **cache_full_name)
1622 u64 len = 0;
1623 u64 disk_start = 0;
1624 u64 disk_offset = 0;
1625 u8 type;
1626 int compressed = 0;
1627 int flags = 0;
1628 char *name = NULL;
1630 if (btrfs_search_header_objectid(sh) == *cache_ino) {
1631 name = *cache_full_name;
1632 } else if (*cache_full_name) {
1633 free(*cache_full_name);
1634 *cache_full_name = NULL;
1636 if (!name) {
1637 name = ino_resolve(fd, btrfs_search_header_objectid(sh),
1638 cache_dirid,
1639 cache_dir_name);
1640 *cache_full_name = name;
1641 *cache_ino = btrfs_search_header_objectid(sh);
1643 if (!name)
1644 return -EIO;
1646 type = btrfs_stack_file_extent_type(item);
1647 compressed = btrfs_stack_file_extent_compression(item);
1649 if (type == BTRFS_FILE_EXTENT_REG ||
1650 type == BTRFS_FILE_EXTENT_PREALLOC) {
1651 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1652 disk_offset = btrfs_stack_file_extent_offset(item);
1653 len = btrfs_stack_file_extent_num_bytes(item);
1654 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1655 disk_start = 0;
1656 disk_offset = 0;
1657 len = btrfs_stack_file_extent_ram_bytes(item);
1658 } else {
1659 error(
1660 "unhandled extent type %d for inode %llu file offset %llu gen %llu",
1661 type,
1662 (unsigned long long)btrfs_search_header_objectid(sh),
1663 (unsigned long long)btrfs_search_header_offset(sh),
1664 (unsigned long long)found_gen);
1666 return -EIO;
1668 printf("inode %llu file offset %llu len %llu disk start %llu "
1669 "offset %llu gen %llu flags ",
1670 (unsigned long long)btrfs_search_header_objectid(sh),
1671 (unsigned long long)btrfs_search_header_offset(sh),
1672 (unsigned long long)len,
1673 (unsigned long long)disk_start,
1674 (unsigned long long)disk_offset,
1675 (unsigned long long)found_gen);
1677 if (compressed) {
1678 printf("COMPRESS");
1679 flags++;
1681 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1682 printf("%sPREALLOC", flags ? "|" : "");
1683 flags++;
1685 if (type == BTRFS_FILE_EXTENT_INLINE) {
1686 printf("%sINLINE", flags ? "|" : "");
1687 flags++;
1689 if (!flags)
1690 printf("NONE");
1692 printf(" %s\n", name);
1693 return 0;
1696 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1698 int ret;
1699 struct btrfs_ioctl_search_args args;
1700 struct btrfs_ioctl_search_key *sk = &args.key;
1701 struct btrfs_ioctl_search_header sh;
1702 struct btrfs_file_extent_item *item;
1703 unsigned long off = 0;
1704 u64 found_gen;
1705 u64 max_found = 0;
1706 int i;
1707 u64 cache_dirid = 0;
1708 u64 cache_ino = 0;
1709 char *cache_dir_name = NULL;
1710 char *cache_full_name = NULL;
1711 struct btrfs_file_extent_item backup;
1713 memset(&backup, 0, sizeof(backup));
1714 memset(&args, 0, sizeof(args));
1716 sk->tree_id = root_id;
1719 * set all the other params to the max, we'll take any objectid
1720 * and any trans
1722 sk->max_objectid = (u64)-1;
1723 sk->max_offset = (u64)-1;
1724 sk->max_transid = (u64)-1;
1725 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1726 sk->min_transid = oldest_gen;
1727 /* just a big number, doesn't matter much */
1728 sk->nr_items = 4096;
1730 max_found = find_root_gen(fd);
1731 while(1) {
1732 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1733 if (ret < 0) {
1734 error("can't perform the search: %m");
1735 break;
1737 /* the ioctl returns the number of item it found in nr_items */
1738 if (sk->nr_items == 0)
1739 break;
1741 off = 0;
1744 * for each item, pull the key out of the header and then
1745 * read the root_ref item it contains
1747 for (i = 0; i < sk->nr_items; i++) {
1748 memcpy(&sh, args.buf + off, sizeof(sh));
1749 off += sizeof(sh);
1752 * just in case the item was too big, pass something other
1753 * than garbage
1755 if (sh.len == 0)
1756 item = &backup;
1757 else
1758 item = (struct btrfs_file_extent_item *)(args.buf +
1759 off);
1760 found_gen = btrfs_stack_file_extent_generation(item);
1761 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1762 found_gen >= oldest_gen) {
1763 print_one_extent(fd, &sh, item, found_gen,
1764 &cache_dirid, &cache_dir_name,
1765 &cache_ino, &cache_full_name);
1767 off += sh.len;
1770 * record the mins in sk so we can make sure the
1771 * next search doesn't repeat this root
1773 sk->min_objectid = sh.objectid;
1774 sk->min_offset = sh.offset;
1775 sk->min_type = sh.type;
1777 sk->nr_items = 4096;
1778 if (sk->min_offset < (u64)-1)
1779 sk->min_offset++;
1780 else if (sk->min_objectid < (u64)-1) {
1781 sk->min_objectid++;
1782 sk->min_offset = 0;
1783 sk->min_type = 0;
1784 } else
1785 break;
1787 free(cache_dir_name);
1788 free(cache_full_name);
1789 printf("transid marker was %llu\n", (unsigned long long)max_found);
1790 return ret;
1793 char *btrfs_list_path_for_root(int fd, u64 root)
1795 struct root_lookup root_lookup;
1796 struct rb_node *n;
1797 char *ret_path = NULL;
1798 int ret;
1799 u64 top_id;
1801 ret = btrfs_list_get_path_rootid(fd, &top_id);
1802 if (ret)
1803 return ERR_PTR(ret);
1805 ret = list_subvol_search(fd, &root_lookup);
1806 if (ret < 0)
1807 return ERR_PTR(ret);
1809 ret = list_subvol_fill_paths(fd, &root_lookup);
1810 if (ret < 0)
1811 return ERR_PTR(ret);
1813 n = rb_last(&root_lookup.root);
1814 while (n) {
1815 struct root_info *entry;
1817 entry = rb_entry(n, struct root_info, rb_node);
1818 ret = resolve_root(&root_lookup, entry, top_id);
1819 if (ret == -ENOENT && entry->root_id == root) {
1820 ret_path = NULL;
1821 break;
1823 if (entry->root_id == root) {
1824 ret_path = entry->full_path;
1825 entry->full_path = NULL;
1828 n = rb_prev(n);
1830 rb_free_nodes(&root_lookup.root, free_root_info);
1832 return ret_path;
1835 int btrfs_list_parse_sort_string(char *opt_arg,
1836 struct btrfs_list_comparer_set **comps)
1838 int order;
1839 int flag;
1840 char *p;
1841 char **ptr_argv;
1842 int what_to_sort;
1844 while ((p = strtok(opt_arg, ",")) != NULL) {
1845 flag = 0;
1846 ptr_argv = all_sort_items;
1848 while (*ptr_argv) {
1849 if (strcmp(*ptr_argv, p) == 0) {
1850 flag = 1;
1851 break;
1852 } else {
1853 p++;
1854 if (strcmp(*ptr_argv, p) == 0) {
1855 flag = 1;
1856 p--;
1857 break;
1859 p--;
1861 ptr_argv++;
1864 if (flag == 0)
1865 return -1;
1867 else {
1868 if (*p == '+') {
1869 order = 0;
1870 p++;
1871 } else if (*p == '-') {
1872 order = 1;
1873 p++;
1874 } else
1875 order = 0;
1877 what_to_sort = btrfs_list_get_sort_item(p);
1878 btrfs_list_setup_comparer(comps, what_to_sort, order);
1880 opt_arg = NULL;
1883 return 0;
1887 * This function is used to parse the argument of filter condition.
1889 * type is the filter object.
1891 int btrfs_list_parse_filter_string(char *opt_arg,
1892 struct btrfs_list_filter_set **filters,
1893 enum btrfs_list_filter_enum type)
1896 u64 arg;
1898 switch (*(opt_arg++)) {
1899 case '+':
1900 arg = arg_strtou64(opt_arg);
1901 type += 2;
1903 btrfs_list_setup_filter(filters, type, arg);
1904 break;
1905 case '-':
1906 arg = arg_strtou64(opt_arg);
1907 type += 1;
1909 btrfs_list_setup_filter(filters, type, arg);
1910 break;
1911 default:
1912 opt_arg--;
1913 arg = arg_strtou64(opt_arg);
1915 btrfs_list_setup_filter(filters, type, arg);
1916 break;
1919 return 0;
1922 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1924 int ret;
1926 ret = lookup_path_rootid(fd, treeid);
1927 if (ret < 0)
1928 error("cannot resolve rootid for path: %m");
1930 return ret;