btrfs-progs: check/original: Don't overwrite return value when we failed to repair
[btrfs-progs-unstable/devel.git] / btrfs-list.c
blob90c98be11aea36af776f62f26021c6ac0e5f6a00
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 inline struct root_info *to_root_info(struct rb_node *node)
49 return rb_entry(node, struct root_info, rb_node);
52 static inline struct root_info *to_root_info_sorted(struct rb_node *node)
54 return rb_entry(node, struct root_info, sort_node);
57 static struct {
58 char *name;
59 char *column_name;
60 int need_print;
61 } btrfs_list_columns[] = {
63 .name = "ID",
64 .column_name = "ID",
65 .need_print = 0,
68 .name = "gen",
69 .column_name = "Gen",
70 .need_print = 0,
73 .name = "cgen",
74 .column_name = "CGen",
75 .need_print = 0,
78 .name = "parent",
79 .column_name = "Parent",
80 .need_print = 0,
83 .name = "top level",
84 .column_name = "Top Level",
85 .need_print = 0,
88 .name = "otime",
89 .column_name = "OTime",
90 .need_print = 0,
93 .name = "parent_uuid",
94 .column_name = "Parent UUID",
95 .need_print = 0,
98 .name = "received_uuid",
99 .column_name = "Received UUID",
100 .need_print = 0,
103 .name = "uuid",
104 .column_name = "UUID",
105 .need_print = 0,
108 .name = "path",
109 .column_name = "Path",
110 .need_print = 0,
113 .name = NULL,
114 .column_name = NULL,
115 .need_print = 0,
119 static btrfs_list_filter_func all_filter_funcs[];
120 static btrfs_list_comp_func all_comp_funcs[];
122 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
124 int i;
126 ASSERT(0 <= column && column <= BTRFS_LIST_ALL);
128 if (column < BTRFS_LIST_ALL) {
129 btrfs_list_columns[column].need_print = 1;
130 return;
133 for (i = 0; i < BTRFS_LIST_ALL; i++)
134 btrfs_list_columns[i].need_print = 1;
137 static int comp_entry_with_rootid(struct root_info *entry1,
138 struct root_info *entry2,
139 int is_descending)
141 int ret;
143 if (entry1->root_id > entry2->root_id)
144 ret = 1;
145 else if (entry1->root_id < entry2->root_id)
146 ret = -1;
147 else
148 ret = 0;
150 return is_descending ? -ret : ret;
153 static int comp_entry_with_gen(struct root_info *entry1,
154 struct root_info *entry2,
155 int is_descending)
157 int ret;
159 if (entry1->gen > entry2->gen)
160 ret = 1;
161 else if (entry1->gen < entry2->gen)
162 ret = -1;
163 else
164 ret = 0;
166 return is_descending ? -ret : ret;
169 static int comp_entry_with_ogen(struct root_info *entry1,
170 struct root_info *entry2,
171 int is_descending)
173 int ret;
175 if (entry1->ogen > entry2->ogen)
176 ret = 1;
177 else if (entry1->ogen < entry2->ogen)
178 ret = -1;
179 else
180 ret = 0;
182 return is_descending ? -ret : ret;
185 static int comp_entry_with_path(struct root_info *entry1,
186 struct root_info *entry2,
187 int is_descending)
189 int ret;
191 if (strcmp(entry1->full_path, entry2->full_path) > 0)
192 ret = 1;
193 else if (strcmp(entry1->full_path, entry2->full_path) < 0)
194 ret = -1;
195 else
196 ret = 0;
198 return is_descending ? -ret : ret;
201 static btrfs_list_comp_func all_comp_funcs[] = {
202 [BTRFS_LIST_COMP_ROOTID] = comp_entry_with_rootid,
203 [BTRFS_LIST_COMP_OGEN] = comp_entry_with_ogen,
204 [BTRFS_LIST_COMP_GEN] = comp_entry_with_gen,
205 [BTRFS_LIST_COMP_PATH] = comp_entry_with_path,
208 static char *all_sort_items[] = {
209 [BTRFS_LIST_COMP_ROOTID] = "rootid",
210 [BTRFS_LIST_COMP_OGEN] = "ogen",
211 [BTRFS_LIST_COMP_GEN] = "gen",
212 [BTRFS_LIST_COMP_PATH] = "path",
213 [BTRFS_LIST_COMP_MAX] = NULL,
216 static int btrfs_list_get_sort_item(char *sort_name)
218 int i;
220 for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
221 if (strcmp(sort_name, all_sort_items[i]) == 0)
222 return i;
224 return -1;
227 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
229 struct btrfs_list_comparer_set *set;
230 int size;
232 size = sizeof(struct btrfs_list_comparer_set) +
233 BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
234 set = calloc(1, size);
235 if (!set) {
236 fprintf(stderr, "memory allocation failed\n");
237 exit(1);
240 set->total = BTRFS_LIST_NCOMPS_INCREASE;
242 return set;
245 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
246 enum btrfs_list_comp_enum comparer, int is_descending)
248 struct btrfs_list_comparer_set *set = *comp_set;
249 int size;
251 ASSERT(set != NULL);
252 ASSERT(comparer < BTRFS_LIST_COMP_MAX);
253 ASSERT(set->ncomps <= set->total);
255 if (set->ncomps == set->total) {
256 void *tmp;
258 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
259 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
260 tmp = set;
261 set = realloc(set, size);
262 if (!set) {
263 fprintf(stderr, "memory allocation failed\n");
264 free(tmp);
265 exit(1);
268 memset(&set->comps[set->total], 0,
269 BTRFS_LIST_NCOMPS_INCREASE *
270 sizeof(struct btrfs_list_comparer));
271 set->total += BTRFS_LIST_NCOMPS_INCREASE;
272 *comp_set = set;
275 ASSERT(set->comps[set->ncomps].comp_func == NULL);
277 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
278 set->comps[set->ncomps].is_descending = is_descending;
279 set->ncomps++;
280 return 0;
283 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
284 struct btrfs_list_comparer_set *set)
286 int rootid_compared = 0;
287 int i, ret = 0;
289 if (!set || !set->ncomps)
290 return comp_entry_with_rootid(entry1, entry2, 0);
292 for (i = 0; i < set->ncomps; i++) {
293 if (!set->comps[i].comp_func)
294 break;
296 ret = set->comps[i].comp_func(entry1, entry2,
297 set->comps[i].is_descending);
298 if (ret)
299 return ret;
301 if (set->comps[i].comp_func == comp_entry_with_rootid)
302 rootid_compared = 1;
305 if (!rootid_compared)
306 ret = comp_entry_with_rootid(entry1, entry2, 0);
308 return ret;
311 static int sort_tree_insert(struct root_lookup *sort_tree,
312 struct root_info *ins,
313 struct btrfs_list_comparer_set *comp_set)
315 struct rb_node **p = &sort_tree->root.rb_node;
316 struct rb_node *parent = NULL;
317 struct root_info *curr;
318 int ret;
320 while (*p) {
321 parent = *p;
322 curr = to_root_info_sorted(parent);
324 ret = sort_comp(ins, curr, comp_set);
325 if (ret < 0)
326 p = &(*p)->rb_left;
327 else if (ret > 0)
328 p = &(*p)->rb_right;
329 else
330 return -EEXIST;
333 rb_link_node(&ins->sort_node, parent, p);
334 rb_insert_color(&ins->sort_node, &sort_tree->root);
335 return 0;
339 * insert a new root into the tree. returns the existing root entry
340 * if one is already there. Both root_id and ref_tree are used
341 * as the key
343 static int root_tree_insert(struct root_lookup *root_tree,
344 struct root_info *ins)
346 struct rb_node **p = &root_tree->root.rb_node;
347 struct rb_node * parent = NULL;
348 struct root_info *curr;
349 int ret;
351 while(*p) {
352 parent = *p;
353 curr = to_root_info(parent);
355 ret = comp_entry_with_rootid(ins, curr, 0);
356 if (ret < 0)
357 p = &(*p)->rb_left;
358 else if (ret > 0)
359 p = &(*p)->rb_right;
360 else
361 return -EEXIST;
364 rb_link_node(&ins->rb_node, parent, p);
365 rb_insert_color(&ins->rb_node, &root_tree->root);
366 return 0;
370 * find a given root id in the tree. We return the smallest one,
371 * rb_next can be used to move forward looking for more if required
373 static struct root_info *root_tree_search(struct root_lookup *root_tree,
374 u64 root_id)
376 struct rb_node *n = root_tree->root.rb_node;
377 struct root_info *entry;
378 struct root_info tmp;
379 int ret;
381 tmp.root_id = root_id;
383 while(n) {
384 entry = to_root_info(n);
386 ret = comp_entry_with_rootid(&tmp, entry, 0);
387 if (ret < 0)
388 n = n->rb_left;
389 else if (ret > 0)
390 n = n->rb_right;
391 else
392 return entry;
394 return NULL;
397 static int update_root(struct root_lookup *root_lookup,
398 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
399 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
400 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
402 struct root_info *ri;
404 ri = root_tree_search(root_lookup, root_id);
405 if (!ri || ri->root_id != root_id)
406 return -ENOENT;
407 if (name && name_len > 0) {
408 free(ri->name);
410 ri->name = malloc(name_len + 1);
411 if (!ri->name) {
412 fprintf(stderr, "memory allocation failed\n");
413 exit(1);
415 strncpy(ri->name, name, name_len);
416 ri->name[name_len] = 0;
418 if (ref_tree)
419 ri->ref_tree = ref_tree;
420 if (root_offset)
421 ri->root_offset = root_offset;
422 if (flags)
423 ri->flags = flags;
424 if (dir_id)
425 ri->dir_id = dir_id;
426 if (gen)
427 ri->gen = gen;
428 if (ogen)
429 ri->ogen = ogen;
430 if (!ri->ogen && root_offset)
431 ri->ogen = root_offset;
432 if (otime)
433 ri->otime = otime;
434 if (uuid)
435 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
436 if (puuid)
437 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
438 if (ruuid)
439 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
441 return 0;
445 * add_root - update the existed root, or allocate a new root and insert it
446 * into the lookup tree.
447 * root_id: object id of the root
448 * ref_tree: object id of the referring root.
449 * root_offset: offset value of the root'key
450 * dir_id: inode id of the directory in ref_tree where this root can be found.
451 * name: the name of root_id in that directory
452 * name_len: the length of name
453 * ogen: the original generation of the root
454 * gen: the current generation of the root
455 * otime: the original time (creation time) of the root
456 * uuid: uuid of the root
457 * puuid: uuid of the root parent if any
458 * ruuid: uuid of the received subvol, if any
460 static int add_root(struct root_lookup *root_lookup,
461 u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
462 u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
463 time_t otime, u8 *uuid, u8 *puuid, u8 *ruuid)
465 struct root_info *ri;
466 int ret;
468 ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
469 dir_id, name, name_len, ogen, gen, otime,
470 uuid, puuid, ruuid);
471 if (!ret)
472 return 0;
474 ri = calloc(1, sizeof(*ri));
475 if (!ri) {
476 printf("memory allocation failed\n");
477 exit(1);
479 ri->root_id = root_id;
481 if (name && name_len > 0) {
482 ri->name = malloc(name_len + 1);
483 if (!ri->name) {
484 fprintf(stderr, "memory allocation failed\n");
485 exit(1);
487 strncpy(ri->name, name, name_len);
488 ri->name[name_len] = 0;
490 if (ref_tree)
491 ri->ref_tree = ref_tree;
492 if (dir_id)
493 ri->dir_id = dir_id;
494 if (root_offset)
495 ri->root_offset = root_offset;
496 if (flags)
497 ri->flags = flags;
498 if (gen)
499 ri->gen = gen;
500 if (ogen)
501 ri->ogen = ogen;
502 if (!ri->ogen && root_offset)
503 ri->ogen = root_offset;
504 if (otime)
505 ri->otime = otime;
507 if (uuid)
508 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
510 if (puuid)
511 memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
513 if (ruuid)
514 memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
516 ret = root_tree_insert(root_lookup, ri);
517 if (ret < 0) {
518 error("failed to insert subvolume %llu to tree: %s",
519 (unsigned long long)root_id, strerror(-ret));
520 exit(1);
522 return 0;
526 * Simplified add_root for back references, omits the uuid and original info
527 * parameters, root offset and flags.
529 static int add_root_backref(struct root_lookup *root_lookup, u64 root_id,
530 u64 ref_tree, u64 dir_id, char *name, int name_len)
532 return add_root(root_lookup, root_id, ref_tree, 0, 0, dir_id, name,
533 name_len, 0, 0, 0, NULL, NULL, NULL);
537 static void free_root_info(struct rb_node *node)
539 struct root_info *ri;
541 ri = to_root_info(node);
542 free(ri->name);
543 free(ri->path);
544 free(ri->full_path);
545 free(ri);
549 * for a given root_info, search through the root_lookup tree to construct
550 * the full path name to it.
552 * This can't be called until all the root_info->path fields are filled
553 * in by lookup_ino_path
555 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
556 u64 top_id)
558 char *full_path = NULL;
559 int len = 0;
560 struct root_info *found;
563 * we go backwards from the root_info object and add pathnames
564 * from parent directories as we go.
566 found = ri;
567 while (1) {
568 char *tmp;
569 u64 next;
570 int add_len;
573 * ref_tree = 0 indicates the subvolume
574 * has been deleted.
576 if (!found->ref_tree) {
577 free(full_path);
578 return -ENOENT;
581 add_len = strlen(found->path);
583 if (full_path) {
584 /* room for / and for null */
585 tmp = malloc(add_len + 2 + len);
586 if (!tmp) {
587 perror("malloc failed");
588 exit(1);
590 memcpy(tmp + add_len + 1, full_path, len);
591 tmp[add_len] = '/';
592 memcpy(tmp, found->path, add_len);
593 tmp [add_len + len + 1] = '\0';
594 free(full_path);
595 full_path = tmp;
596 len += add_len + 1;
597 } else {
598 full_path = strdup(found->path);
599 len = add_len;
601 if (!ri->top_id)
602 ri->top_id = found->ref_tree;
604 next = found->ref_tree;
605 if (next == top_id)
606 break;
608 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
609 * we are at the top
611 if (next == BTRFS_FS_TREE_OBJECTID)
612 break;
614 * if the ref_tree wasn't in our tree of roots, the
615 * subvolume was deleted.
617 found = root_tree_search(rl, next);
618 if (!found) {
619 free(full_path);
620 return -ENOENT;
624 ri->full_path = full_path;
626 return 0;
630 * for a single root_info, ask the kernel to give us a path name
631 * inside it's ref_root for the dir_id where it lives.
633 * This fills in root_info->path with the path to the directory and and
634 * appends this root's name.
636 static int lookup_ino_path(int fd, struct root_info *ri)
638 struct btrfs_ioctl_ino_lookup_args args;
639 int ret;
641 if (ri->path)
642 return 0;
644 if (!ri->ref_tree)
645 return -ENOENT;
647 memset(&args, 0, sizeof(args));
648 args.treeid = ri->ref_tree;
649 args.objectid = ri->dir_id;
651 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
652 if (ret < 0) {
653 if (errno == ENOENT) {
654 ri->ref_tree = 0;
655 return -ENOENT;
657 error("failed to lookup path for root %llu: %m",
658 (unsigned long long)ri->ref_tree);
659 return ret;
662 if (args.name[0]) {
664 * we're in a subdirectory of ref_tree, the kernel ioctl
665 * puts a / in there for us
667 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
668 if (!ri->path) {
669 perror("malloc failed");
670 exit(1);
672 strcpy(ri->path, args.name);
673 strcat(ri->path, ri->name);
674 } else {
675 /* we're at the root of ref_tree */
676 ri->path = strdup(ri->name);
677 if (!ri->path) {
678 perror("strdup failed");
679 exit(1);
682 return 0;
685 /* finding the generation for a given path is a two step process.
686 * First we use the inode lookup routine to find out the root id
688 * Then we use the tree search ioctl to scan all the root items for a
689 * given root id and spit out the latest generation we can find
691 static u64 find_root_gen(int fd)
693 struct btrfs_ioctl_ino_lookup_args ino_args;
694 int ret;
695 struct btrfs_ioctl_search_args args;
696 struct btrfs_ioctl_search_key *sk = &args.key;
697 struct btrfs_ioctl_search_header sh;
698 unsigned long off = 0;
699 u64 max_found = 0;
700 int i;
702 memset(&ino_args, 0, sizeof(ino_args));
703 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
705 /* this ioctl fills in ino_args->treeid */
706 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
707 if (ret < 0) {
708 error("failed to lookup path for dirid %llu: %m",
709 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
710 return 0;
713 memset(&args, 0, sizeof(args));
715 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
718 * there may be more than one ROOT_ITEM key if there are
719 * snapshots pending deletion, we have to loop through
720 * them.
722 sk->min_objectid = ino_args.treeid;
723 sk->max_objectid = ino_args.treeid;
724 sk->max_type = BTRFS_ROOT_ITEM_KEY;
725 sk->min_type = BTRFS_ROOT_ITEM_KEY;
726 sk->max_offset = (u64)-1;
727 sk->max_transid = (u64)-1;
728 sk->nr_items = 4096;
730 while (1) {
731 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
732 if (ret < 0) {
733 error("can't perform the search: %m");
734 return 0;
736 /* the ioctl returns the number of item it found in nr_items */
737 if (sk->nr_items == 0)
738 break;
740 off = 0;
741 for (i = 0; i < sk->nr_items; i++) {
742 struct btrfs_root_item *item;
744 memcpy(&sh, args.buf + off, sizeof(sh));
745 off += sizeof(sh);
746 item = (struct btrfs_root_item *)(args.buf + off);
747 off += sh.len;
749 sk->min_objectid = sh.objectid;
750 sk->min_type = sh.type;
751 sk->min_offset = sh.offset;
753 if (sh.objectid > ino_args.treeid)
754 break;
756 if (sh.objectid == ino_args.treeid &&
757 sh.type == BTRFS_ROOT_ITEM_KEY) {
758 max_found = max(max_found,
759 btrfs_root_generation(item));
762 if (sk->min_offset < (u64)-1)
763 sk->min_offset++;
764 else
765 break;
767 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
768 break;
769 if (sk->min_objectid != ino_args.treeid)
770 break;
772 return max_found;
775 /* pass in a directory id and this will return
776 * the full path of the parent directory inside its
777 * subvolume root.
779 * It may return NULL if it is in the root, or an ERR_PTR if things
780 * go badly.
782 static char *__ino_resolve(int fd, u64 dirid)
784 struct btrfs_ioctl_ino_lookup_args args;
785 int ret;
786 char *full;
788 memset(&args, 0, sizeof(args));
789 args.objectid = dirid;
791 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
792 if (ret < 0) {
793 error("failed to lookup path for dirid %llu: %m",
794 (unsigned long long)dirid);
795 return ERR_PTR(ret);
798 if (args.name[0]) {
800 * we're in a subdirectory of ref_tree, the kernel ioctl
801 * puts a / in there for us
803 full = strdup(args.name);
804 if (!full) {
805 perror("malloc failed");
806 return ERR_PTR(-ENOMEM);
808 } else {
809 /* we're at the root of ref_tree */
810 full = NULL;
812 return full;
816 * simple string builder, returning a new string with both
817 * dirid and name
819 static char *build_name(const char *dirid, const char *name)
821 char *full;
823 if (!dirid)
824 return strdup(name);
826 full = malloc(strlen(dirid) + strlen(name) + 1);
827 if (!full)
828 return NULL;
829 strcpy(full, dirid);
830 strcat(full, name);
831 return full;
835 * given an inode number, this returns the full path name inside the subvolume
836 * to that file/directory. cache_dirid and cache_name are used to
837 * cache the results so we can avoid tree searches if a later call goes
838 * to the same directory or file name
840 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
843 u64 dirid;
844 char *dirname;
845 char *name;
846 char *full;
847 int ret;
848 struct btrfs_ioctl_search_args args;
849 struct btrfs_ioctl_search_key *sk = &args.key;
850 struct btrfs_ioctl_search_header *sh;
851 unsigned long off = 0;
852 int namelen;
854 memset(&args, 0, sizeof(args));
856 sk->tree_id = 0;
859 * step one, we search for the inode back ref. We just use the first
860 * one
862 sk->min_objectid = ino;
863 sk->max_objectid = ino;
864 sk->max_type = BTRFS_INODE_REF_KEY;
865 sk->max_offset = (u64)-1;
866 sk->min_type = BTRFS_INODE_REF_KEY;
867 sk->max_transid = (u64)-1;
868 sk->nr_items = 1;
870 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
871 if (ret < 0) {
872 error("can't perform the search: %m");
873 return NULL;
875 /* the ioctl returns the number of item it found in nr_items */
876 if (sk->nr_items == 0)
877 return NULL;
879 off = 0;
880 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
882 if (btrfs_search_header_type(sh) == BTRFS_INODE_REF_KEY) {
883 struct btrfs_inode_ref *ref;
884 dirid = btrfs_search_header_offset(sh);
886 ref = (struct btrfs_inode_ref *)(sh + 1);
887 namelen = btrfs_stack_inode_ref_name_len(ref);
889 name = (char *)(ref + 1);
890 name = strndup(name, namelen);
892 /* use our cached value */
893 if (dirid == *cache_dirid && *cache_name) {
894 dirname = *cache_name;
895 goto build;
897 } else {
898 return NULL;
901 * the inode backref gives us the file name and the parent directory id.
902 * From here we use __ino_resolve to get the path to the parent
904 dirname = __ino_resolve(fd, dirid);
905 build:
906 full = build_name(dirname, name);
907 if (*cache_name && dirname != *cache_name)
908 free(*cache_name);
910 *cache_name = dirname;
911 *cache_dirid = dirid;
912 free(name);
914 return full;
917 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
919 struct btrfs_ioctl_search_args args;
920 struct btrfs_ioctl_search_key *sk = &args.key;
921 struct btrfs_ioctl_search_header *sh;
922 u64 found = 0;
923 int ret;
925 memset(&args, 0, sizeof(args));
928 * search for a dir item with a name 'default' in the tree of
929 * tree roots, it should point us to a default root
931 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
933 /* don't worry about ancient format and request only one item */
934 sk->nr_items = 1;
936 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
937 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
938 sk->max_type = BTRFS_DIR_ITEM_KEY;
939 sk->min_type = BTRFS_DIR_ITEM_KEY;
940 sk->max_offset = (u64)-1;
941 sk->max_transid = (u64)-1;
943 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
944 if (ret < 0)
945 return ret;
947 /* the ioctl returns the number of items it found in nr_items */
948 if (sk->nr_items == 0)
949 goto out;
951 sh = (struct btrfs_ioctl_search_header *)args.buf;
953 if (btrfs_search_header_type(sh) == BTRFS_DIR_ITEM_KEY) {
954 struct btrfs_dir_item *di;
955 int name_len;
956 char *name;
958 di = (struct btrfs_dir_item *)(sh + 1);
959 name_len = btrfs_stack_dir_name_len(di);
960 name = (char *)(di + 1);
962 if (!strncmp("default", name, name_len))
963 found = btrfs_disk_key_objectid(&di->location);
966 out:
967 *default_id = found;
968 return 0;
971 static int list_subvol_search(int fd, struct root_lookup *root_lookup)
973 int ret;
974 struct btrfs_ioctl_search_args args;
975 struct btrfs_ioctl_search_key *sk = &args.key;
976 struct btrfs_ioctl_search_header sh;
977 struct btrfs_root_ref *ref;
978 struct btrfs_root_item *ri;
979 unsigned long off;
980 int name_len;
981 char *name;
982 u64 dir_id;
983 u64 gen = 0;
984 u64 ogen;
985 u64 flags;
986 int i;
988 root_lookup->root.rb_node = NULL;
989 memset(&args, 0, sizeof(args));
991 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
992 /* Search both live and deleted subvolumes */
993 sk->min_type = BTRFS_ROOT_ITEM_KEY;
994 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
995 sk->min_objectid = BTRFS_FS_TREE_OBJECTID;
996 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
997 sk->max_offset = (u64)-1;
998 sk->max_transid = (u64)-1;
1000 while(1) {
1001 sk->nr_items = 4096;
1002 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1003 if (ret < 0)
1004 return ret;
1005 if (sk->nr_items == 0)
1006 break;
1008 off = 0;
1011 * for each item, pull the key out of the header and then
1012 * read the root_ref item it contains
1014 for (i = 0; i < sk->nr_items; i++) {
1015 memcpy(&sh, args.buf + off, sizeof(sh));
1016 off += sizeof(sh);
1017 if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1018 ref = (struct btrfs_root_ref *)(args.buf + off);
1019 name_len = btrfs_stack_root_ref_name_len(ref);
1020 name = (char *)(ref + 1);
1021 dir_id = btrfs_stack_root_ref_dirid(ref);
1023 add_root_backref(root_lookup, sh.objectid,
1024 sh.offset, dir_id, name,
1025 name_len);
1026 } else if (sh.type == BTRFS_ROOT_ITEM_KEY &&
1027 (sh.objectid >= BTRFS_FIRST_FREE_OBJECTID ||
1028 sh.objectid == BTRFS_FS_TREE_OBJECTID)) {
1029 time_t otime;
1030 u8 uuid[BTRFS_UUID_SIZE];
1031 u8 puuid[BTRFS_UUID_SIZE];
1032 u8 ruuid[BTRFS_UUID_SIZE];
1034 ri = (struct btrfs_root_item *)(args.buf + off);
1035 gen = btrfs_root_generation(ri);
1036 flags = btrfs_root_flags(ri);
1037 if(sh.len >
1038 sizeof(struct btrfs_root_item_v0)) {
1039 otime = btrfs_stack_timespec_sec(&ri->otime);
1040 ogen = btrfs_root_otransid(ri);
1041 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1042 memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
1043 memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
1044 } else {
1045 otime = 0;
1046 ogen = 0;
1047 memset(uuid, 0, BTRFS_UUID_SIZE);
1048 memset(puuid, 0, BTRFS_UUID_SIZE);
1049 memset(ruuid, 0, BTRFS_UUID_SIZE);
1052 add_root(root_lookup, sh.objectid, 0,
1053 sh.offset, flags, 0, NULL, 0, ogen,
1054 gen, otime, uuid, puuid, ruuid);
1057 off += sh.len;
1059 sk->min_objectid = sh.objectid;
1060 sk->min_type = sh.type;
1061 sk->min_offset = sh.offset;
1063 sk->min_offset++;
1064 if (!sk->min_offset)
1065 sk->min_type++;
1066 else
1067 continue;
1069 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1070 sk->min_type = BTRFS_ROOT_ITEM_KEY;
1071 sk->min_objectid++;
1072 } else
1073 continue;
1075 if (sk->min_objectid > sk->max_objectid)
1076 break;
1079 return 0;
1082 static int filter_by_rootid(struct root_info *ri, u64 data)
1084 return ri->root_id == data;
1087 static int filter_snapshot(struct root_info *ri, u64 data)
1089 return !!ri->root_offset;
1092 static int filter_flags(struct root_info *ri, u64 flags)
1094 return ri->flags & flags;
1097 static int filter_gen_more(struct root_info *ri, u64 data)
1099 return ri->gen >= data;
1102 static int filter_gen_less(struct root_info *ri, u64 data)
1104 return ri->gen <= data;
1107 static int filter_gen_equal(struct root_info *ri, u64 data)
1109 return ri->gen == data;
1112 static int filter_cgen_more(struct root_info *ri, u64 data)
1114 return ri->ogen >= data;
1117 static int filter_cgen_less(struct root_info *ri, u64 data)
1119 return ri->ogen <= data;
1122 static int filter_cgen_equal(struct root_info *ri, u64 data)
1124 return ri->ogen == data;
1127 static int filter_topid_equal(struct root_info *ri, u64 data)
1129 return ri->top_id == data;
1132 static int filter_full_path(struct root_info *ri, u64 data)
1134 if (ri->full_path && ri->top_id != data) {
1135 char *tmp;
1136 char p[] = "<FS_TREE>";
1137 int add_len = strlen(p);
1138 int len = strlen(ri->full_path);
1140 tmp = malloc(len + add_len + 2);
1141 if (!tmp) {
1142 fprintf(stderr, "memory allocation failed\n");
1143 exit(1);
1145 memcpy(tmp + add_len + 1, ri->full_path, len);
1146 tmp[len + add_len + 1] = '\0';
1147 tmp[add_len] = '/';
1148 memcpy(tmp, p, add_len);
1149 free(ri->full_path);
1150 ri->full_path = tmp;
1152 return 1;
1155 static int filter_by_parent(struct root_info *ri, u64 data)
1157 return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
1160 static int filter_deleted(struct root_info *ri, u64 data)
1162 return ri->deleted;
1165 static btrfs_list_filter_func all_filter_funcs[] = {
1166 [BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
1167 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
1168 [BTRFS_LIST_FILTER_FLAGS] = filter_flags,
1169 [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
1170 [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
1171 [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
1172 [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
1173 [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
1174 [BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
1175 [BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
1176 [BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
1177 [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
1178 [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
1181 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1183 struct btrfs_list_filter_set *set;
1184 int size;
1186 size = sizeof(struct btrfs_list_filter_set) +
1187 BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1188 set = calloc(1, size);
1189 if (!set) {
1190 fprintf(stderr, "memory allocation failed\n");
1191 exit(1);
1194 set->total = BTRFS_LIST_NFILTERS_INCREASE;
1196 return set;
1200 * Setup list filters. Exit if there's not enough memory, as we can't continue
1201 * without the structures set up properly.
1203 void btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1204 enum btrfs_list_filter_enum filter, u64 data)
1206 struct btrfs_list_filter_set *set = *filter_set;
1207 int size;
1209 ASSERT(set != NULL);
1210 ASSERT(filter < BTRFS_LIST_FILTER_MAX);
1211 ASSERT(set->nfilters <= set->total);
1213 if (set->nfilters == set->total) {
1214 void *tmp;
1216 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1217 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1218 tmp = set;
1219 set = realloc(set, size);
1220 if (!set) {
1221 fprintf(stderr, "memory allocation failed\n");
1222 free(tmp);
1223 exit(1);
1226 memset(&set->filters[set->total], 0,
1227 BTRFS_LIST_NFILTERS_INCREASE *
1228 sizeof(struct btrfs_list_filter));
1229 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1230 *filter_set = set;
1233 ASSERT(set->filters[set->nfilters].filter_func == NULL);
1235 if (filter == BTRFS_LIST_FILTER_DELETED)
1236 set->only_deleted = 1;
1238 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1239 set->filters[set->nfilters].data = data;
1240 set->nfilters++;
1243 static int filter_root(struct root_info *ri,
1244 struct btrfs_list_filter_set *set)
1246 int i, ret;
1248 if (!set)
1249 return 1;
1251 if (set->only_deleted && !ri->deleted)
1252 return 0;
1254 if (!set->only_deleted && ri->deleted)
1255 return 0;
1257 for (i = 0; i < set->nfilters; i++) {
1258 if (!set->filters[i].filter_func)
1259 break;
1260 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1261 if (!ret)
1262 return 0;
1264 return 1;
1267 static void filter_and_sort_subvol(struct root_lookup *all_subvols,
1268 struct root_lookup *sort_tree,
1269 struct btrfs_list_filter_set *filter_set,
1270 struct btrfs_list_comparer_set *comp_set,
1271 u64 top_id)
1273 struct rb_node *n;
1274 struct root_info *entry;
1275 int ret;
1277 sort_tree->root.rb_node = NULL;
1279 n = rb_last(&all_subvols->root);
1280 while (n) {
1281 entry = to_root_info(n);
1283 ret = resolve_root(all_subvols, entry, top_id);
1284 if (ret == -ENOENT) {
1285 if (entry->root_id != BTRFS_FS_TREE_OBJECTID) {
1286 entry->full_path = strdup("DELETED");
1287 entry->deleted = 1;
1288 } else {
1290 * The full path is not supposed to be printed,
1291 * but we don't want to print an empty string,
1292 * in case it appears somewhere.
1294 entry->full_path = strdup("TOPLEVEL");
1295 entry->deleted = 0;
1298 ret = filter_root(entry, filter_set);
1299 if (ret)
1300 sort_tree_insert(sort_tree, entry, comp_set);
1301 n = rb_prev(n);
1305 static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1307 struct rb_node *n;
1309 n = rb_first(&root_lookup->root);
1310 while (n) {
1311 struct root_info *entry;
1312 int ret;
1313 entry = to_root_info(n);
1314 ret = lookup_ino_path(fd, entry);
1315 if (ret && ret != -ENOENT)
1316 return ret;
1317 n = rb_next(n);
1320 return 0;
1323 static void print_subvolume_column(struct root_info *subv,
1324 enum btrfs_list_column_enum column)
1326 char tstr[256];
1327 char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
1329 ASSERT(0 <= column && column < BTRFS_LIST_ALL);
1331 switch (column) {
1332 case BTRFS_LIST_OBJECTID:
1333 printf("%llu", subv->root_id);
1334 break;
1335 case BTRFS_LIST_GENERATION:
1336 printf("%llu", subv->gen);
1337 break;
1338 case BTRFS_LIST_OGENERATION:
1339 printf("%llu", subv->ogen);
1340 break;
1341 case BTRFS_LIST_PARENT:
1342 printf("%llu", subv->ref_tree);
1343 break;
1344 case BTRFS_LIST_TOP_LEVEL:
1345 printf("%llu", subv->top_id);
1346 break;
1347 case BTRFS_LIST_OTIME:
1348 if (subv->otime) {
1349 struct tm tm;
1351 localtime_r(&subv->otime, &tm);
1352 strftime(tstr, 256, "%Y-%m-%d %X", &tm);
1353 } else
1354 strcpy(tstr, "-");
1355 printf("%s", tstr);
1356 break;
1357 case BTRFS_LIST_UUID:
1358 if (uuid_is_null(subv->uuid))
1359 strcpy(uuidparse, "-");
1360 else
1361 uuid_unparse(subv->uuid, uuidparse);
1362 printf("%-36s", uuidparse);
1363 break;
1364 case BTRFS_LIST_PUUID:
1365 if (uuid_is_null(subv->puuid))
1366 strcpy(uuidparse, "-");
1367 else
1368 uuid_unparse(subv->puuid, uuidparse);
1369 printf("%-36s", uuidparse);
1370 break;
1371 case BTRFS_LIST_RUUID:
1372 if (uuid_is_null(subv->ruuid))
1373 strcpy(uuidparse, "-");
1374 else
1375 uuid_unparse(subv->ruuid, uuidparse);
1376 printf("%-36s", uuidparse);
1377 break;
1378 case BTRFS_LIST_PATH:
1379 BUG_ON(!subv->full_path);
1380 printf("%s", subv->full_path);
1381 break;
1382 default:
1383 break;
1387 static void print_one_subvol_info_raw(struct root_info *subv,
1388 const char *raw_prefix)
1390 int i;
1392 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1393 if (!btrfs_list_columns[i].need_print)
1394 continue;
1396 if (raw_prefix)
1397 printf("%s",raw_prefix);
1399 print_subvolume_column(subv, i);
1401 printf("\n");
1404 static void print_one_subvol_info_table(struct root_info *subv)
1406 int i;
1408 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1409 if (!btrfs_list_columns[i].need_print)
1410 continue;
1412 print_subvolume_column(subv, i);
1414 if (i != BTRFS_LIST_PATH)
1415 printf("\t");
1417 if (i == BTRFS_LIST_TOP_LEVEL)
1418 printf("\t");
1420 printf("\n");
1423 static void print_one_subvol_info_default(struct root_info *subv)
1425 int i;
1427 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1428 if (!btrfs_list_columns[i].need_print)
1429 continue;
1431 printf("%s ", btrfs_list_columns[i].name);
1432 print_subvolume_column(subv, i);
1434 if (i != BTRFS_LIST_PATH)
1435 printf(" ");
1437 printf("\n");
1440 static void print_all_subvol_info_tab_head(void)
1442 int i;
1443 int len;
1444 char barrier[20];
1446 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1447 if (btrfs_list_columns[i].need_print)
1448 printf("%s\t", btrfs_list_columns[i].name);
1450 if (i == BTRFS_LIST_ALL-1)
1451 printf("\n");
1454 for (i = 0; i < BTRFS_LIST_ALL; i++) {
1455 memset(barrier, 0, sizeof(barrier));
1457 if (btrfs_list_columns[i].need_print) {
1458 len = strlen(btrfs_list_columns[i].name);
1459 while (len--)
1460 strcat(barrier, "-");
1462 printf("%s\t", barrier);
1464 if (i == BTRFS_LIST_ALL-1)
1465 printf("\n");
1469 static void print_all_subvol_info(struct root_lookup *sorted_tree,
1470 enum btrfs_list_layout layout, const char *raw_prefix)
1472 struct rb_node *n;
1473 struct root_info *entry;
1475 if (layout == BTRFS_LIST_LAYOUT_TABLE)
1476 print_all_subvol_info_tab_head();
1478 n = rb_first(&sorted_tree->root);
1479 while (n) {
1480 entry = to_root_info_sorted(n);
1482 /* The toplevel subvolume is not listed by default */
1483 if (entry->root_id == BTRFS_FS_TREE_OBJECTID)
1484 goto next;
1486 switch (layout) {
1487 case BTRFS_LIST_LAYOUT_DEFAULT:
1488 print_one_subvol_info_default(entry);
1489 break;
1490 case BTRFS_LIST_LAYOUT_TABLE:
1491 print_one_subvol_info_table(entry);
1492 break;
1493 case BTRFS_LIST_LAYOUT_RAW:
1494 print_one_subvol_info_raw(entry, raw_prefix);
1495 break;
1497 next:
1498 n = rb_next(n);
1502 static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
1504 int ret;
1506 ret = list_subvol_search(fd, root_lookup);
1507 if (ret) {
1508 error("can't perform the search: %m");
1509 return ret;
1513 * now we have an rbtree full of root_info objects, but we need to fill
1514 * in their path names within the subvol that is referencing each one.
1516 ret = list_subvol_fill_paths(fd, root_lookup);
1517 return ret;
1520 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
1521 struct btrfs_list_comparer_set *comp_set,
1522 enum btrfs_list_layout layout, int full_path,
1523 const char *raw_prefix)
1525 struct root_lookup root_lookup;
1526 struct root_lookup root_sort;
1527 int ret = 0;
1528 u64 top_id = 0;
1530 if (full_path)
1531 ret = btrfs_list_get_path_rootid(fd, &top_id);
1532 if (ret)
1533 return ret;
1535 ret = btrfs_list_subvols(fd, &root_lookup);
1536 if (ret)
1537 return ret;
1538 filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1539 comp_set, top_id);
1541 print_all_subvol_info(&root_sort, layout, raw_prefix);
1542 rb_free_nodes(&root_lookup.root, free_root_info);
1544 return 0;
1547 static char *strdup_or_null(const char *s)
1549 if (!s)
1550 return NULL;
1551 return strdup(s);
1554 int btrfs_get_toplevel_subvol(int fd, struct root_info *the_ri)
1556 int ret;
1557 struct root_lookup rl;
1558 struct rb_node *rbn;
1559 struct root_info *ri;
1560 u64 root_id;
1562 ret = btrfs_list_get_path_rootid(fd, &root_id);
1563 if (ret)
1564 return ret;
1566 ret = btrfs_list_subvols(fd, &rl);
1567 if (ret)
1568 return ret;
1570 rbn = rb_first(&rl.root);
1571 ri = to_root_info(rbn);
1573 if (ri->root_id != BTRFS_FS_TREE_OBJECTID)
1574 return -ENOENT;
1576 memcpy(the_ri, ri, offsetof(struct root_info, path));
1577 the_ri->path = strdup_or_null("/");
1578 the_ri->name = strdup_or_null("<FS_TREE>");
1579 the_ri->full_path = strdup_or_null("/");
1580 rb_free_nodes(&rl.root, free_root_info);
1582 return ret;
1585 int btrfs_get_subvol(int fd, struct root_info *the_ri)
1587 int ret, rr;
1588 struct root_lookup rl;
1589 struct rb_node *rbn;
1590 struct root_info *ri;
1591 u64 root_id;
1593 ret = btrfs_list_get_path_rootid(fd, &root_id);
1594 if (ret)
1595 return ret;
1597 ret = btrfs_list_subvols(fd, &rl);
1598 if (ret)
1599 return ret;
1601 rbn = rb_first(&rl.root);
1602 while(rbn) {
1603 ri = to_root_info(rbn);
1604 rr = resolve_root(&rl, ri, root_id);
1605 if (rr == -ENOENT) {
1606 ret = -ENOENT;
1607 rbn = rb_next(rbn);
1608 continue;
1611 if (!comp_entry_with_rootid(the_ri, ri, 0) ||
1612 !uuid_compare(the_ri->uuid, ri->uuid)) {
1613 memcpy(the_ri, ri, offsetof(struct root_info, path));
1614 the_ri->path = strdup_or_null(ri->path);
1615 the_ri->name = strdup_or_null(ri->name);
1616 the_ri->full_path = strdup_or_null(ri->full_path);
1617 ret = 0;
1618 break;
1620 rbn = rb_next(rbn);
1622 rb_free_nodes(&rl.root, free_root_info);
1623 return ret;
1626 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1627 struct btrfs_file_extent_item *item,
1628 u64 found_gen, u64 *cache_dirid,
1629 char **cache_dir_name, u64 *cache_ino,
1630 char **cache_full_name)
1632 u64 len = 0;
1633 u64 disk_start = 0;
1634 u64 disk_offset = 0;
1635 u8 type;
1636 int compressed = 0;
1637 int flags = 0;
1638 char *name = NULL;
1640 if (btrfs_search_header_objectid(sh) == *cache_ino) {
1641 name = *cache_full_name;
1642 } else if (*cache_full_name) {
1643 free(*cache_full_name);
1644 *cache_full_name = NULL;
1646 if (!name) {
1647 name = ino_resolve(fd, btrfs_search_header_objectid(sh),
1648 cache_dirid,
1649 cache_dir_name);
1650 *cache_full_name = name;
1651 *cache_ino = btrfs_search_header_objectid(sh);
1653 if (!name)
1654 return -EIO;
1656 type = btrfs_stack_file_extent_type(item);
1657 compressed = btrfs_stack_file_extent_compression(item);
1659 if (type == BTRFS_FILE_EXTENT_REG ||
1660 type == BTRFS_FILE_EXTENT_PREALLOC) {
1661 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1662 disk_offset = btrfs_stack_file_extent_offset(item);
1663 len = btrfs_stack_file_extent_num_bytes(item);
1664 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1665 disk_start = 0;
1666 disk_offset = 0;
1667 len = btrfs_stack_file_extent_ram_bytes(item);
1668 } else {
1669 error(
1670 "unhandled extent type %d for inode %llu file offset %llu gen %llu",
1671 type,
1672 (unsigned long long)btrfs_search_header_objectid(sh),
1673 (unsigned long long)btrfs_search_header_offset(sh),
1674 (unsigned long long)found_gen);
1676 return -EIO;
1678 printf("inode %llu file offset %llu len %llu disk start %llu "
1679 "offset %llu gen %llu flags ",
1680 (unsigned long long)btrfs_search_header_objectid(sh),
1681 (unsigned long long)btrfs_search_header_offset(sh),
1682 (unsigned long long)len,
1683 (unsigned long long)disk_start,
1684 (unsigned long long)disk_offset,
1685 (unsigned long long)found_gen);
1687 if (compressed) {
1688 printf("COMPRESS");
1689 flags++;
1691 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1692 printf("%sPREALLOC", flags ? "|" : "");
1693 flags++;
1695 if (type == BTRFS_FILE_EXTENT_INLINE) {
1696 printf("%sINLINE", flags ? "|" : "");
1697 flags++;
1699 if (!flags)
1700 printf("NONE");
1702 printf(" %s\n", name);
1703 return 0;
1706 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1708 int ret;
1709 struct btrfs_ioctl_search_args args;
1710 struct btrfs_ioctl_search_key *sk = &args.key;
1711 struct btrfs_ioctl_search_header sh;
1712 struct btrfs_file_extent_item *item;
1713 unsigned long off = 0;
1714 u64 found_gen;
1715 u64 max_found = 0;
1716 int i;
1717 u64 cache_dirid = 0;
1718 u64 cache_ino = 0;
1719 char *cache_dir_name = NULL;
1720 char *cache_full_name = NULL;
1721 struct btrfs_file_extent_item backup;
1723 memset(&backup, 0, sizeof(backup));
1724 memset(&args, 0, sizeof(args));
1726 sk->tree_id = root_id;
1729 * set all the other params to the max, we'll take any objectid
1730 * and any trans
1732 sk->max_objectid = (u64)-1;
1733 sk->max_offset = (u64)-1;
1734 sk->max_transid = (u64)-1;
1735 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1736 sk->min_transid = oldest_gen;
1737 /* just a big number, doesn't matter much */
1738 sk->nr_items = 4096;
1740 max_found = find_root_gen(fd);
1741 while(1) {
1742 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1743 if (ret < 0) {
1744 error("can't perform the search: %m");
1745 break;
1747 /* the ioctl returns the number of item it found in nr_items */
1748 if (sk->nr_items == 0)
1749 break;
1751 off = 0;
1754 * for each item, pull the key out of the header and then
1755 * read the root_ref item it contains
1757 for (i = 0; i < sk->nr_items; i++) {
1758 memcpy(&sh, args.buf + off, sizeof(sh));
1759 off += sizeof(sh);
1762 * just in case the item was too big, pass something other
1763 * than garbage
1765 if (sh.len == 0)
1766 item = &backup;
1767 else
1768 item = (struct btrfs_file_extent_item *)(args.buf +
1769 off);
1770 found_gen = btrfs_stack_file_extent_generation(item);
1771 if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1772 found_gen >= oldest_gen) {
1773 print_one_extent(fd, &sh, item, found_gen,
1774 &cache_dirid, &cache_dir_name,
1775 &cache_ino, &cache_full_name);
1777 off += sh.len;
1780 * record the mins in sk so we can make sure the
1781 * next search doesn't repeat this root
1783 sk->min_objectid = sh.objectid;
1784 sk->min_offset = sh.offset;
1785 sk->min_type = sh.type;
1787 sk->nr_items = 4096;
1788 if (sk->min_offset < (u64)-1)
1789 sk->min_offset++;
1790 else if (sk->min_objectid < (u64)-1) {
1791 sk->min_objectid++;
1792 sk->min_offset = 0;
1793 sk->min_type = 0;
1794 } else
1795 break;
1797 free(cache_dir_name);
1798 free(cache_full_name);
1799 printf("transid marker was %llu\n", (unsigned long long)max_found);
1800 return ret;
1803 char *btrfs_list_path_for_root(int fd, u64 root)
1805 struct root_lookup root_lookup;
1806 struct rb_node *n;
1807 char *ret_path = NULL;
1808 int ret;
1809 u64 top_id;
1811 ret = btrfs_list_get_path_rootid(fd, &top_id);
1812 if (ret)
1813 return ERR_PTR(ret);
1815 ret = list_subvol_search(fd, &root_lookup);
1816 if (ret < 0)
1817 return ERR_PTR(ret);
1819 ret = list_subvol_fill_paths(fd, &root_lookup);
1820 if (ret < 0)
1821 return ERR_PTR(ret);
1823 n = rb_last(&root_lookup.root);
1824 while (n) {
1825 struct root_info *entry;
1827 entry = to_root_info(n);
1828 ret = resolve_root(&root_lookup, entry, top_id);
1829 if (ret == -ENOENT && entry->root_id == root) {
1830 ret_path = NULL;
1831 break;
1833 if (entry->root_id == root) {
1834 ret_path = entry->full_path;
1835 entry->full_path = NULL;
1838 n = rb_prev(n);
1840 rb_free_nodes(&root_lookup.root, free_root_info);
1842 return ret_path;
1845 int btrfs_list_parse_sort_string(char *opt_arg,
1846 struct btrfs_list_comparer_set **comps)
1848 int order;
1849 int flag;
1850 char *p;
1851 char **ptr_argv;
1852 int what_to_sort;
1854 while ((p = strtok(opt_arg, ",")) != NULL) {
1855 flag = 0;
1856 ptr_argv = all_sort_items;
1858 while (*ptr_argv) {
1859 if (strcmp(*ptr_argv, p) == 0) {
1860 flag = 1;
1861 break;
1862 } else {
1863 p++;
1864 if (strcmp(*ptr_argv, p) == 0) {
1865 flag = 1;
1866 p--;
1867 break;
1869 p--;
1871 ptr_argv++;
1874 if (flag == 0)
1875 return -1;
1877 else {
1878 if (*p == '+') {
1879 order = 0;
1880 p++;
1881 } else if (*p == '-') {
1882 order = 1;
1883 p++;
1884 } else
1885 order = 0;
1887 what_to_sort = btrfs_list_get_sort_item(p);
1888 btrfs_list_setup_comparer(comps, what_to_sort, order);
1890 opt_arg = NULL;
1893 return 0;
1897 * This function is used to parse the argument of filter condition.
1899 * type is the filter object.
1901 int btrfs_list_parse_filter_string(char *opt_arg,
1902 struct btrfs_list_filter_set **filters,
1903 enum btrfs_list_filter_enum type)
1906 u64 arg;
1908 switch (*(opt_arg++)) {
1909 case '+':
1910 arg = arg_strtou64(opt_arg);
1911 type += 2;
1913 btrfs_list_setup_filter(filters, type, arg);
1914 break;
1915 case '-':
1916 arg = arg_strtou64(opt_arg);
1917 type += 1;
1919 btrfs_list_setup_filter(filters, type, arg);
1920 break;
1921 default:
1922 opt_arg--;
1923 arg = arg_strtou64(opt_arg);
1925 btrfs_list_setup_filter(filters, type, arg);
1926 break;
1929 return 0;
1932 int btrfs_list_get_path_rootid(int fd, u64 *treeid)
1934 int ret;
1936 ret = lookup_path_rootid(fd, treeid);
1937 if (ret < 0)
1938 error("cannot resolve rootid for path: %m");
1940 return ret;