Btrfs-progs: move path modification to filters
[platform/upstream/btrfs-progs.git] / btrfs-list.c
1 /*
2  * Copyright (C) 2010 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _GNU_SOURCE
20 #ifndef __CHECKER__
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include "ioctl.h"
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #include <libgen.h>
33 #include "ctree.h"
34 #include "transaction.h"
35 #include "utils.h"
36 #include <uuid/uuid.h>
37 #include "btrfs-list.h"
38
39 #define BTRFS_LIST_NFILTERS_INCREASE    (2 * BTRFS_LIST_FILTER_MAX)
40 #define BTRFS_LIST_NCOMPS_INCREASE      (2 * BTRFS_LIST_COMP_MAX)
41
42 /* we store all the roots we find in an rbtree so that we can
43  * search for them later.
44  */
45 struct root_lookup {
46         struct rb_root root;
47 };
48
49 /*
50  * one of these for each root we find.
51  */
52 struct root_info {
53         struct rb_node rb_node;
54         struct rb_node sort_node;
55
56         /* this root's id */
57         u64 root_id;
58
59         /* equal the offset of the root's key */
60         u64 root_offset;
61
62         /* flags of the root */
63         u64 flags;
64
65         /* the id of the root that references this one */
66         u64 ref_tree;
67
68         /* the dir id we're in from ref_tree */
69         u64 dir_id;
70
71         u64 top_id;
72
73         /* generation when the root is created or last updated */
74         u64 gen;
75
76         /* creation generation of this root in sec*/
77         u64 ogen;
78
79         /* creation time of this root in sec*/
80         time_t otime;
81
82         u8 uuid[BTRFS_UUID_SIZE];
83
84         /* path from the subvol we live in to this root, including the
85          * root's name.  This is null until we do the extra lookup ioctl.
86          */
87         char *path;
88
89         /* the name of this root in the directory it lives in */
90         char *name;
91
92         char *full_path;
93 };
94
95 struct {
96         char    *name;
97         char    *column_name;
98         int     need_print;
99 } btrfs_list_columns[] = {
100         {
101                 .name           = "ID",
102                 .column_name    = "ID",
103                 .need_print     = 1,
104         },
105         {
106                 .name           = "gen",
107                 .column_name    = "Gen",
108                 .need_print     = 1,
109         },
110         {
111                 .name           = "cgen",
112                 .column_name    = "CGen",
113                 .need_print     = 0,
114         },
115         {
116                 .name           = "parent",
117                 .column_name    = "Parent",
118                 .need_print     = 0,
119         },
120         {
121                 .name           = "top level",
122                 .column_name    = "Top Level",
123                 .need_print     = 1,
124         },
125         {
126                 .name           = "otime",
127                 .column_name    = "OTime",
128                 .need_print     = 0,
129         },
130         {
131                 .name           = "uuid",
132                 .column_name    = "UUID",
133                 .need_print     = 0,
134         },
135         {
136                 .name           = "path",
137                 .column_name    = "Path",
138                 .need_print     = 1,
139         },
140         {
141                 .name           = NULL,
142                 .column_name    = NULL,
143                 .need_print     = 0,
144         },
145 };
146
147 static btrfs_list_filter_func all_filter_funcs[];
148 static btrfs_list_comp_func all_comp_funcs[];
149
150 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column)
151 {
152         int i;
153
154         BUG_ON(column < 0 || column > BTRFS_LIST_ALL);
155
156         if (column < BTRFS_LIST_ALL) {
157                 btrfs_list_columns[column].need_print = 1;
158                 return;
159         }
160
161         for (i = 0; i < BTRFS_LIST_ALL; i++)
162                 btrfs_list_columns[i].need_print = 1;
163 }
164
165 static void root_lookup_init(struct root_lookup *tree)
166 {
167         tree->root.rb_node = NULL;
168 }
169
170 static int comp_entry_with_rootid(struct root_info *entry1,
171                                   struct root_info *entry2,
172                                   int is_descending)
173 {
174         int ret;
175
176         if (entry1->root_id > entry2->root_id)
177                 ret = 1;
178         else if (entry1->root_id < entry2->root_id)
179                 ret = -1;
180         else
181                 ret = 0;
182
183         return is_descending ? -ret : ret;
184 }
185
186 static int comp_entry_with_gen(struct root_info *entry1,
187                                struct root_info *entry2,
188                                int is_descending)
189 {
190         int ret;
191
192         if (entry1->gen > entry2->gen)
193                 ret = 1;
194         else if (entry1->gen < entry2->gen)
195                 ret = -1;
196         else
197                 ret = 0;
198
199         return is_descending ? -ret : ret;
200 }
201
202 static int comp_entry_with_ogen(struct root_info *entry1,
203                                 struct root_info *entry2,
204                                 int is_descending)
205 {
206         int ret;
207
208         if (entry1->ogen > entry2->ogen)
209                 ret = 1;
210         else if (entry1->ogen < entry2->ogen)
211                 ret = -1;
212         else
213                 ret = 0;
214
215         return is_descending ? -ret : ret;
216 }
217
218 static int comp_entry_with_path(struct root_info *entry1,
219                                 struct root_info *entry2,
220                                 int is_descending)
221 {
222         int ret;
223
224         if (strcmp(entry1->full_path, entry2->full_path) > 0)
225                 ret = 1;
226         else if (strcmp(entry1->full_path, entry2->full_path) < 0)
227                 ret = -1;
228         else
229                 ret = 0;
230
231         return is_descending ? -ret : ret;
232 }
233
234 static btrfs_list_comp_func all_comp_funcs[] = {
235         [BTRFS_LIST_COMP_ROOTID]        = comp_entry_with_rootid,
236         [BTRFS_LIST_COMP_OGEN]          = comp_entry_with_ogen,
237         [BTRFS_LIST_COMP_GEN]           = comp_entry_with_gen,
238         [BTRFS_LIST_COMP_PATH]          = comp_entry_with_path,
239 };
240
241 static char *all_sort_items[] = {
242         [BTRFS_LIST_COMP_ROOTID]        = "rootid",
243         [BTRFS_LIST_COMP_OGEN]          = "ogen",
244         [BTRFS_LIST_COMP_GEN]           = "gen",
245         [BTRFS_LIST_COMP_PATH]          = "path",
246         [BTRFS_LIST_COMP_MAX]           = NULL,
247 };
248
249 static int  btrfs_list_get_sort_item(char *sort_name)
250 {
251         int i;
252
253         for (i = 0; i < BTRFS_LIST_COMP_MAX; i++) {
254                 if (strcmp(sort_name, all_sort_items[i]) == 0)
255                         return i;
256         }
257         return -1;
258 }
259
260 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
261 {
262         struct btrfs_list_comparer_set *set;
263         int size;
264
265         size = sizeof(struct btrfs_list_comparer_set) +
266                BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
267         set = malloc(size);
268         if (!set) {
269                 fprintf(stderr, "memory allocation failed\n");
270                 exit(1);
271         }
272
273         memset(set, 0, size);
274         set->total = BTRFS_LIST_NCOMPS_INCREASE;
275
276         return set;
277 }
278
279 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
280 {
281         free(comp_set);
282 }
283
284 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set  **comp_set,
285                               enum btrfs_list_comp_enum comparer,
286                               int is_descending)
287 {
288         struct btrfs_list_comparer_set *set = *comp_set;
289         int size;
290
291         BUG_ON(!set);
292         BUG_ON(comparer >= BTRFS_LIST_COMP_MAX);
293         BUG_ON(set->ncomps > set->total);
294
295         if (set->ncomps == set->total) {
296                 size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
297                 size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
298                 set = realloc(set, size);
299                 if (!set) {
300                         fprintf(stderr, "memory allocation failed\n");
301                         exit(1);
302                 }
303
304                 memset(&set->comps[set->total], 0,
305                        BTRFS_LIST_NCOMPS_INCREASE *
306                        sizeof(struct btrfs_list_comparer));
307                 set->total += BTRFS_LIST_NCOMPS_INCREASE;
308                 *comp_set = set;
309         }
310
311         BUG_ON(set->comps[set->ncomps].comp_func);
312
313         set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
314         set->comps[set->ncomps].is_descending = is_descending;
315         set->ncomps++;
316         return 0;
317 }
318
319 static int sort_comp(struct root_info *entry1, struct root_info *entry2,
320                      struct btrfs_list_comparer_set *set)
321 {
322         int rootid_compared = 0;
323         int i, ret = 0;
324
325         if (!set || !set->ncomps)
326                 goto comp_rootid;
327
328         for (i = 0; i < set->ncomps; i++) {
329                 if (!set->comps[i].comp_func)
330                         break;
331
332                 ret = set->comps[i].comp_func(entry1, entry2,
333                                               set->comps[i].is_descending);
334                 if (ret)
335                         return ret;
336
337                 if (set->comps[i].comp_func == comp_entry_with_rootid)
338                         rootid_compared = 1;
339         }
340
341         if (!rootid_compared) {
342 comp_rootid:
343                 ret = comp_entry_with_rootid(entry1, entry2, 0);
344         }
345
346         return ret;
347 }
348
349 static int sort_tree_insert(struct root_lookup *sort_tree,
350                             struct root_info *ins,
351                             struct btrfs_list_comparer_set *comp_set)
352 {
353         struct rb_node **p = &sort_tree->root.rb_node;
354         struct rb_node *parent = NULL;
355         struct root_info *curr;
356         int ret;
357
358         while (*p) {
359                 parent = *p;
360                 curr = rb_entry(parent, struct root_info, sort_node);
361
362                 ret = sort_comp(ins, curr, comp_set);
363                 if (ret < 0)
364                         p = &(*p)->rb_left;
365                 else if (ret > 0)
366                         p = &(*p)->rb_right;
367                 else
368                         return -EEXIST;
369         }
370
371         rb_link_node(&ins->sort_node, parent, p);
372         rb_insert_color(&ins->sort_node, &sort_tree->root);
373         return 0;
374 }
375
376 /*
377  * insert a new root into the tree.  returns the existing root entry
378  * if one is already there.  Both root_id and ref_tree are used
379  * as the key
380  */
381 static int root_tree_insert(struct root_lookup *root_tree,
382                             struct root_info *ins)
383 {
384         struct rb_node **p = &root_tree->root.rb_node;
385         struct rb_node * parent = NULL;
386         struct root_info *curr;
387         int ret;
388
389         while(*p) {
390                 parent = *p;
391                 curr = rb_entry(parent, struct root_info, rb_node);
392
393                 ret = comp_entry_with_rootid(ins, curr, 0);
394                 if (ret < 0)
395                         p = &(*p)->rb_left;
396                 else if (ret > 0)
397                         p = &(*p)->rb_right;
398                 else
399                         return -EEXIST;
400         }
401
402         rb_link_node(&ins->rb_node, parent, p);
403         rb_insert_color(&ins->rb_node, &root_tree->root);
404         return 0;
405 }
406
407 /*
408  * find a given root id in the tree.  We return the smallest one,
409  * rb_next can be used to move forward looking for more if required
410  */
411 static struct root_info *root_tree_search(struct root_lookup *root_tree,
412                                           u64 root_id)
413 {
414         struct rb_node *n = root_tree->root.rb_node;
415         struct root_info *entry;
416         struct root_info tmp;
417         int ret;
418
419         tmp.root_id = root_id;
420
421         while(n) {
422                 entry = rb_entry(n, struct root_info, rb_node);
423
424                 ret = comp_entry_with_rootid(&tmp, entry, 0);
425                 if (ret < 0)
426                         n = n->rb_left;
427                 else if (ret > 0)
428                         n = n->rb_right;
429                 else
430                         return entry;
431         }
432         return NULL;
433 }
434
435 static int update_root(struct root_lookup *root_lookup,
436                        u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
437                        u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
438                        time_t ot, void *uuid)
439 {
440         struct root_info *ri;
441
442         ri = root_tree_search(root_lookup, root_id);
443         if (!ri || ri->root_id != root_id)
444                 return -ENOENT;
445         if (name && name_len > 0) {
446                 if (ri->name)
447                         free(ri->name);
448
449                 ri->name = malloc(name_len + 1);
450                 if (!ri->name) {
451                         fprintf(stderr, "memory allocation failed\n");
452                         exit(1);
453                 }
454                 strncpy(ri->name, name, name_len);
455                 ri->name[name_len] = 0;
456         }
457         if (ref_tree)
458                 ri->ref_tree = ref_tree;
459         if (root_offset)
460                 ri->root_offset = root_offset;
461         if (flags)
462                 ri->flags = flags;
463         if (dir_id)
464                 ri->dir_id = dir_id;
465         if (gen)
466                 ri->gen = gen;
467         if (ogen)
468                 ri->ogen = ogen;
469         if (!ri->ogen && root_offset)
470                 ri->ogen = root_offset;
471         if (ot)
472                 ri->otime = ot;
473         if (uuid)
474                 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
475
476         return 0;
477 }
478
479 /*
480  * add_root - update the existed root, or allocate a new root and insert it
481  *            into the lookup tree.
482  * root_id: object id of the root
483  * ref_tree: object id of the referring root.
484  * root_offset: offset value of the root'key
485  * dir_id: inode id of the directory in ref_tree where this root can be found.
486  * name: the name of root_id in that directory
487  * name_len: the length of name
488  * ogen: the original generation of the root
489  * gen: the current generation of the root
490  * ot: the original time(create time) of the root
491  * uuid: uuid of the root
492  */
493 static int add_root(struct root_lookup *root_lookup,
494                     u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
495                     u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
496                     time_t ot, void *uuid)
497 {
498         struct root_info *ri;
499         int ret;
500
501         ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
502                           dir_id, name, name_len, ogen, gen, ot, uuid);
503         if (!ret)
504                 return 0;
505
506         ri = malloc(sizeof(*ri));
507         if (!ri) {
508                 printf("memory allocation failed\n");
509                 exit(1);
510         }
511         memset(ri, 0, sizeof(*ri));
512         ri->root_id = root_id;
513
514         if (name && name_len > 0) {
515                 ri->name = malloc(name_len + 1);
516                 if (!ri->name) {
517                         fprintf(stderr, "memory allocation failed\n");
518                         exit(1);
519                 }
520                 strncpy(ri->name, name, name_len);
521                 ri->name[name_len] = 0;
522         }
523         if (ref_tree)
524                 ri->ref_tree = ref_tree;
525         if (dir_id)
526                 ri->dir_id = dir_id;
527         if (root_offset)
528                 ri->root_offset = root_offset;
529         if (flags)
530                 ri->flags = flags;
531         if (gen)
532                 ri->gen = gen;
533         if (ogen)
534                 ri->ogen = ogen;
535         if (!ri->ogen && root_offset)
536                 ri->ogen = root_offset;
537         if (ot)
538                 ri->otime = ot;
539
540         if (uuid) 
541                 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
542
543         ret = root_tree_insert(root_lookup, ri);
544         if (ret) {
545                 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
546                 exit(1);
547         }
548         return 0;
549 }
550
551 void __free_root_info(struct root_info *ri)
552 {
553         if (ri->name)
554                 free(ri->name);
555
556         if (ri->path)
557                 free(ri->path);
558
559         if (ri->full_path)
560                 free(ri->full_path);
561
562         free(ri);
563 }
564
565 void __free_all_subvolumn(struct root_lookup *root_tree)
566 {
567         struct root_info *entry;
568         struct rb_node *n;
569
570         n = rb_first(&root_tree->root);
571         while (n) {
572                 entry = rb_entry(n, struct root_info, rb_node);
573                 rb_erase(n, &root_tree->root);
574                 __free_root_info(entry);
575
576                 n = rb_first(&root_tree->root);
577         }
578 }
579
580 /*
581  * for a given root_info, search through the root_lookup tree to construct
582  * the full path name to it.
583  *
584  * This can't be called until all the root_info->path fields are filled
585  * in by lookup_ino_path
586  */
587 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
588                        u64 top_id)
589 {
590         char *full_path = NULL;
591         int len = 0;
592         struct root_info *found;
593
594         /*
595          * we go backwards from the root_info object and add pathnames
596          * from parent directories as we go.
597          */
598         found = ri;
599         while (1) {
600                 char *tmp;
601                 u64 next;
602                 int add_len = strlen(found->path);
603
604                 /* room for / and for null */
605                 tmp = malloc(add_len + 2 + len);
606                 if (!tmp) {
607                         perror("malloc failed");
608                         exit(1);
609                 }
610                 if (full_path) {
611                         memcpy(tmp + add_len + 1, full_path, len);
612                         tmp[add_len] = '/';
613                         memcpy(tmp, found->path, add_len);
614                         tmp [add_len + len + 1] = '\0';
615                         free(full_path);
616                         full_path = tmp;
617                         len += add_len + 1;
618                 } else {
619                         full_path = strdup(found->path);
620                         len = add_len;
621                 }
622
623                 next = found->ref_tree;
624
625                 if (next ==  top_id) {
626                         ri->top_id = top_id;
627                         break;
628                 }
629
630                 if (next == BTRFS_FS_TREE_OBJECTID) {
631                         ri->top_id = next;
632                         break;
633                 }
634
635                 /*
636                  * if the ref_tree wasn't in our tree of roots, we're
637                  * at the top
638                  */
639                 found = root_tree_search(rl, next);
640                 if (!found) {
641                         ri->top_id = next;
642                         break;
643                 }
644         }
645
646         ri->full_path = full_path;
647
648         return 0;
649 }
650
651 /*
652  * for a single root_info, ask the kernel to give us a path name
653  * inside it's ref_root for the dir_id where it lives.
654  *
655  * This fills in root_info->path with the path to the directory and and
656  * appends this root's name.
657  */
658 static int lookup_ino_path(int fd, struct root_info *ri)
659 {
660         struct btrfs_ioctl_ino_lookup_args args;
661         int ret, e;
662
663         if (ri->path)
664                 return 0;
665
666         memset(&args, 0, sizeof(args));
667         args.treeid = ri->ref_tree;
668         args.objectid = ri->dir_id;
669
670         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
671         e = errno;
672         if (ret) {
673                 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
674                         (unsigned long long)ri->ref_tree,
675                         strerror(e));
676                 return ret;
677         }
678
679         if (args.name[0]) {
680                 /*
681                  * we're in a subdirectory of ref_tree, the kernel ioctl
682                  * puts a / in there for us
683                  */
684                 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
685                 if (!ri->path) {
686                         perror("malloc failed");
687                         exit(1);
688                 }
689                 strcpy(ri->path, args.name);
690                 strcat(ri->path, ri->name);
691         } else {
692                 /* we're at the root of ref_tree */
693                 ri->path = strdup(ri->name);
694                 if (!ri->path) {
695                         perror("strdup failed");
696                         exit(1);
697                 }
698         }
699         return 0;
700 }
701
702 /* finding the generation for a given path is a two step process.
703  * First we use the inode loookup routine to find out the root id
704  *
705  * Then we use the tree search ioctl to scan all the root items for a
706  * given root id and spit out the latest generation we can find
707  */
708 static u64 find_root_gen(int fd)
709 {
710         struct btrfs_ioctl_ino_lookup_args ino_args;
711         int ret;
712         struct btrfs_ioctl_search_args args;
713         struct btrfs_ioctl_search_key *sk = &args.key;
714         struct btrfs_ioctl_search_header sh;
715         unsigned long off = 0;
716         u64 max_found = 0;
717         int i;
718         int e;
719
720         memset(&ino_args, 0, sizeof(ino_args));
721         ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
722
723         /* this ioctl fills in ino_args->treeid */
724         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
725         e = errno;
726         if (ret) {
727                 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
728                         (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
729                         strerror(e));
730                 return 0;
731         }
732
733         memset(&args, 0, sizeof(args));
734
735         sk->tree_id = 1;
736
737         /*
738          * there may be more than one ROOT_ITEM key if there are
739          * snapshots pending deletion, we have to loop through
740          * them.
741          */
742         sk->min_objectid = ino_args.treeid;
743         sk->max_objectid = ino_args.treeid;
744         sk->max_type = BTRFS_ROOT_ITEM_KEY;
745         sk->min_type = BTRFS_ROOT_ITEM_KEY;
746         sk->max_offset = (u64)-1;
747         sk->max_transid = (u64)-1;
748         sk->nr_items = 4096;
749
750         while (1) {
751                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
752                 e = errno;
753                 if (ret < 0) {
754                         fprintf(stderr, "ERROR: can't perform the search - %s\n",
755                                 strerror(e));
756                         return 0;
757                 }
758                 /* the ioctl returns the number of item it found in nr_items */
759                 if (sk->nr_items == 0)
760                         break;
761
762                 off = 0;
763                 for (i = 0; i < sk->nr_items; i++) {
764                         struct btrfs_root_item *item;
765
766                         memcpy(&sh, args.buf + off, sizeof(sh));
767                         off += sizeof(sh);
768                         item = (struct btrfs_root_item *)(args.buf + off);
769                         off += sh.len;
770
771                         sk->min_objectid = sh.objectid;
772                         sk->min_type = sh.type;
773                         sk->min_offset = sh.offset;
774
775                         if (sh.objectid > ino_args.treeid)
776                                 break;
777
778                         if (sh.objectid == ino_args.treeid &&
779                             sh.type == BTRFS_ROOT_ITEM_KEY) {
780                                 max_found = max(max_found,
781                                                 btrfs_root_generation(item));
782                         }
783                 }
784                 if (sk->min_offset < (u64)-1)
785                         sk->min_offset++;
786                 else
787                         break;
788
789                 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
790                         break;
791                 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
792                         break;
793         }
794         return max_found;
795 }
796
797 /* pass in a directory id and this will return
798  * the full path of the parent directory inside its
799  * subvolume root.
800  *
801  * It may return NULL if it is in the root, or an ERR_PTR if things
802  * go badly.
803  */
804 static char *__ino_resolve(int fd, u64 dirid)
805 {
806         struct btrfs_ioctl_ino_lookup_args args;
807         int ret;
808         char *full;
809         int e;
810
811         memset(&args, 0, sizeof(args));
812         args.objectid = dirid;
813
814         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
815         e = errno;
816         if (ret) {
817                 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
818                         (unsigned long long)dirid, strerror(e) );
819                 return ERR_PTR(ret);
820         }
821
822         if (args.name[0]) {
823                 /*
824                  * we're in a subdirectory of ref_tree, the kernel ioctl
825                  * puts a / in there for us
826                  */
827                 full = strdup(args.name);
828                 if (!full) {
829                         perror("malloc failed");
830                         return ERR_PTR(-ENOMEM);
831                 }
832         } else {
833                 /* we're at the root of ref_tree */
834                 full = NULL;
835         }
836         return full;
837 }
838
839 /*
840  * simple string builder, returning a new string with both
841  * dirid and name
842  */
843 char *build_name(char *dirid, char *name)
844 {
845         char *full;
846         if (!dirid)
847                 return strdup(name);
848
849         full = malloc(strlen(dirid) + strlen(name) + 1);
850         if (!full)
851                 return NULL;
852         strcpy(full, dirid);
853         strcat(full, name);
854         return full;
855 }
856
857 /*
858  * given an inode number, this returns the full path name inside the subvolume
859  * to that file/directory.  cache_dirid and cache_name are used to
860  * cache the results so we can avoid tree searches if a later call goes
861  * to the same directory or file name
862  */
863 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
864
865 {
866         u64 dirid;
867         char *dirname;
868         char *name;
869         char *full;
870         int ret;
871         struct btrfs_ioctl_search_args args;
872         struct btrfs_ioctl_search_key *sk = &args.key;
873         struct btrfs_ioctl_search_header *sh;
874         unsigned long off = 0;
875         int namelen;
876         int e;
877
878         memset(&args, 0, sizeof(args));
879
880         sk->tree_id = 0;
881
882         /*
883          * step one, we search for the inode back ref.  We just use the first
884          * one
885          */
886         sk->min_objectid = ino;
887         sk->max_objectid = ino;
888         sk->max_type = BTRFS_INODE_REF_KEY;
889         sk->max_offset = (u64)-1;
890         sk->min_type = BTRFS_INODE_REF_KEY;
891         sk->max_transid = (u64)-1;
892         sk->nr_items = 1;
893
894         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
895         e = errno;
896         if (ret < 0) {
897                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
898                         strerror(e));
899                 return NULL;
900         }
901         /* the ioctl returns the number of item it found in nr_items */
902         if (sk->nr_items == 0)
903                 return NULL;
904
905         off = 0;
906         sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
907
908         if (sh->type == BTRFS_INODE_REF_KEY) {
909                 struct btrfs_inode_ref *ref;
910                 dirid = sh->offset;
911
912                 ref = (struct btrfs_inode_ref *)(sh + 1);
913                 namelen = btrfs_stack_inode_ref_name_len(ref);
914
915                 name = (char *)(ref + 1);
916                 name = strndup(name, namelen);
917
918                 /* use our cached value */
919                 if (dirid == *cache_dirid && *cache_name) {
920                         dirname = *cache_name;
921                         goto build;
922                 }
923         } else {
924                 return NULL;
925         }
926         /*
927          * the inode backref gives us the file name and the parent directory id.
928          * From here we use __ino_resolve to get the path to the parent
929          */
930         dirname = __ino_resolve(fd, dirid);
931 build:
932         full = build_name(dirname, name);
933         if (*cache_name && dirname != *cache_name)
934                 free(*cache_name);
935
936         *cache_name = dirname;
937         *cache_dirid = dirid;
938         free(name);
939
940         return full;
941 }
942
943 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
944 {
945         struct btrfs_ioctl_search_args args;
946         struct btrfs_ioctl_search_key *sk = &args.key;
947         struct btrfs_ioctl_search_header *sh;
948         u64 found = 0;
949         int ret;
950
951         memset(&args, 0, sizeof(args));
952
953         /*
954          * search for a dir item with a name 'default' in the tree of
955          * tree roots, it should point us to a default root
956          */
957         sk->tree_id = 1;
958
959         /* don't worry about ancient format and request only one item */
960         sk->nr_items = 1;
961
962         sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
963         sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
964         sk->max_type = BTRFS_DIR_ITEM_KEY;
965         sk->min_type = BTRFS_DIR_ITEM_KEY;
966         sk->max_offset = (u64)-1;
967         sk->max_transid = (u64)-1;
968
969         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
970         if (ret < 0)
971                 return ret;
972
973         /* the ioctl returns the number of items it found in nr_items */
974         if (sk->nr_items == 0)
975                 goto out;
976
977         sh = (struct btrfs_ioctl_search_header *)args.buf;
978
979         if (sh->type == BTRFS_DIR_ITEM_KEY) {
980                 struct btrfs_dir_item *di;
981                 int name_len;
982                 char *name;
983
984                 di = (struct btrfs_dir_item *)(sh + 1);
985                 name_len = btrfs_stack_dir_name_len(di);
986                 name = (char *)(di + 1);
987
988                 if (!strncmp("default", name, name_len))
989                         found = btrfs_disk_key_objectid(&di->location);
990         }
991
992 out:
993         *default_id = found;
994         return 0;
995 }
996
997 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
998 {
999         int ret;
1000         struct btrfs_ioctl_search_args args;
1001         struct btrfs_ioctl_search_key *sk = &args.key;
1002         struct btrfs_ioctl_search_header sh;
1003         struct btrfs_root_ref *ref;
1004         struct btrfs_root_item *ri;
1005         unsigned long off = 0;
1006         int name_len;
1007         char *name;
1008         u64 dir_id;
1009         u64 gen = 0;
1010         u64 ogen;
1011         u64 flags;
1012         int i;
1013         time_t t;
1014         u8 uuid[BTRFS_UUID_SIZE];
1015
1016         root_lookup_init(root_lookup);
1017         memset(&args, 0, sizeof(args));
1018
1019         /* search in the tree of tree roots */
1020         sk->tree_id = 1;
1021
1022         /*
1023          * set the min and max to backref keys.  The search will
1024          * only send back this type of key now.
1025          */
1026         sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1027         sk->min_type = BTRFS_ROOT_ITEM_KEY;
1028
1029         sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1030
1031         /*
1032          * set all the other params to the max, we'll take any objectid
1033          * and any trans
1034          */
1035         sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1036         sk->max_offset = (u64)-1;
1037         sk->max_transid = (u64)-1;
1038
1039         /* just a big number, doesn't matter much */
1040         sk->nr_items = 4096;
1041
1042         while(1) {
1043                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1044                 if (ret < 0)
1045                         return ret;
1046                 /* the ioctl returns the number of item it found in nr_items */
1047                 if (sk->nr_items == 0)
1048                         break;
1049
1050                 off = 0;
1051
1052                 /*
1053                  * for each item, pull the key out of the header and then
1054                  * read the root_ref item it contains
1055                  */
1056                 for (i = 0; i < sk->nr_items; i++) {
1057                         memcpy(&sh, args.buf + off, sizeof(sh));
1058                         off += sizeof(sh);
1059                         if (sh.type == BTRFS_ROOT_BACKREF_KEY) {
1060                                 ref = (struct btrfs_root_ref *)(args.buf + off);
1061                                 name_len = btrfs_stack_root_ref_name_len(ref);
1062                                 name = (char *)(ref + 1);
1063                                 dir_id = btrfs_stack_root_ref_dirid(ref);
1064
1065                                 add_root(root_lookup, sh.objectid, sh.offset,
1066                                          0, 0, dir_id, name, name_len, 0, 0, 0,
1067                                          NULL);
1068                         } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
1069                                 ri = (struct btrfs_root_item *)(args.buf + off);
1070                                 gen = btrfs_root_generation(ri);
1071                                 flags = btrfs_root_flags(ri);
1072                                 if(sh.len >
1073                                    sizeof(struct btrfs_root_item_v0)) {
1074                                         t = ri->otime.sec;
1075                                         ogen = btrfs_root_otransid(ri);
1076                                         memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1077                                 } else {
1078                                         t = 0;
1079                                         ogen = 0;
1080                                         memset(uuid, 0, BTRFS_UUID_SIZE);
1081                                 }
1082
1083                                 add_root(root_lookup, sh.objectid, 0,
1084                                          sh.offset, flags, 0, NULL, 0, ogen,
1085                                          gen, t, uuid);
1086                         }
1087
1088                         off += sh.len;
1089
1090                         /*
1091                          * record the mins in sk so we can make sure the
1092                          * next search doesn't repeat this root
1093                          */
1094                         sk->min_objectid = sh.objectid;
1095                         sk->min_type = sh.type;
1096                         sk->min_offset = sh.offset;
1097                 }
1098                 sk->nr_items = 4096;
1099                 sk->min_offset++;
1100                 if (!sk->min_offset)    /* overflow */
1101                         sk->min_type++;
1102                 else
1103                         continue;
1104
1105                 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1106                         sk->min_type = BTRFS_ROOT_ITEM_KEY;
1107                         sk->min_objectid++;
1108                 } else
1109                         continue;
1110
1111                 if (sk->min_objectid > sk->max_objectid)
1112                         break;
1113         }
1114
1115         return 0;
1116 }
1117
1118 static int filter_by_rootid(struct root_info *ri, u64 data)
1119 {
1120         return ri->root_id == data;
1121 }
1122
1123 static int filter_snapshot(struct root_info *ri, u64 data)
1124 {
1125         return !!ri->root_offset;
1126 }
1127
1128 static int filter_flags(struct root_info *ri, u64 flags)
1129 {
1130         return ri->flags & flags;
1131 }
1132
1133 static int filter_gen_more(struct root_info *ri, u64 data)
1134 {
1135         return ri->gen >= data;
1136 }
1137
1138 static int filter_gen_less(struct root_info *ri, u64 data)
1139 {
1140         return ri->gen <= data;
1141 }
1142
1143 static int filter_gen_equal(struct root_info  *ri, u64 data)
1144 {
1145         return ri->gen == data;
1146 }
1147
1148 static int filter_cgen_more(struct root_info *ri, u64 data)
1149 {
1150         return ri->ogen >= data;
1151 }
1152
1153 static int filter_cgen_less(struct root_info *ri, u64 data)
1154 {
1155         return ri->ogen <= data;
1156 }
1157
1158 static int filter_cgen_equal(struct root_info *ri, u64 data)
1159 {
1160         return ri->ogen == data;
1161 }
1162
1163 static int filter_topid_equal(struct root_info *ri, u64 data)
1164 {
1165         return ri->top_id == data;
1166 }
1167
1168 static int filter_full_path(struct root_info *ri, u64 data)
1169 {
1170         if (ri->full_path && ri->top_id != data) {
1171                 char *tmp;
1172                 char p[] = "<FS_TREE>";
1173                 int add_len = strlen(p);
1174                 int len = strlen(ri->full_path);
1175
1176                 tmp = malloc(len + add_len + 2);
1177                 if (!tmp) {
1178                         fprintf(stderr, "memory allocation failed\n");
1179                         exit(1);
1180                 }
1181                 memcpy(tmp + add_len + 1, ri->full_path, len);
1182                 tmp[len + add_len + 1] = '\0';
1183                 tmp[add_len] = '/';
1184                 memcpy(tmp, p, add_len);
1185                 free(ri->full_path);
1186                 ri->full_path = tmp;
1187         }
1188         return 1;
1189 }
1190
1191 static btrfs_list_filter_func all_filter_funcs[] = {
1192         [BTRFS_LIST_FILTER_ROOTID]              = filter_by_rootid,
1193         [BTRFS_LIST_FILTER_SNAPSHOT_ONLY]       = filter_snapshot,
1194         [BTRFS_LIST_FILTER_FLAGS]               = filter_flags,
1195         [BTRFS_LIST_FILTER_GEN_MORE]            = filter_gen_more,
1196         [BTRFS_LIST_FILTER_GEN_LESS]            = filter_gen_less,
1197         [BTRFS_LIST_FILTER_GEN_EQUAL]           = filter_gen_equal,
1198         [BTRFS_LIST_FILTER_CGEN_MORE]           = filter_cgen_more,
1199         [BTRFS_LIST_FILTER_CGEN_LESS]           = filter_cgen_less,
1200         [BTRFS_LIST_FILTER_CGEN_EQUAL]          = filter_cgen_equal,
1201         [BTRFS_LIST_FILTER_TOPID_EQUAL]         = filter_topid_equal,
1202         [BTRFS_LIST_FILTER_FULL_PATH]           = filter_full_path,
1203 };
1204
1205 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1206 {
1207         struct btrfs_list_filter_set *set;
1208         int size;
1209
1210         size = sizeof(struct btrfs_list_filter_set) +
1211                BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1212         set = malloc(size);
1213         if (!set) {
1214                 fprintf(stderr, "memory allocation failed\n");
1215                 exit(1);
1216         }
1217
1218         memset(set, 0, size);
1219         set->total = BTRFS_LIST_NFILTERS_INCREASE;
1220
1221         return set;
1222 }
1223
1224 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1225 {
1226         free(filter_set);
1227 }
1228
1229 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1230                             enum btrfs_list_filter_enum filter, u64 data)
1231 {
1232         struct btrfs_list_filter_set *set = *filter_set;
1233         int size;
1234
1235         BUG_ON(!set);
1236         BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1237         BUG_ON(set->nfilters > set->total);
1238
1239         if (set->nfilters == set->total) {
1240                 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1241                 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1242                 set = realloc(set, size);
1243                 if (!set) {
1244                         fprintf(stderr, "memory allocation failed\n");
1245                         exit(1);
1246                 }
1247
1248                 memset(&set->filters[set->total], 0,
1249                        BTRFS_LIST_NFILTERS_INCREASE *
1250                        sizeof(struct btrfs_list_filter));
1251                 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1252                 *filter_set = set;
1253         }
1254
1255         BUG_ON(set->filters[set->nfilters].filter_func);
1256
1257         set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1258         set->filters[set->nfilters].data = data;
1259         set->nfilters++;
1260         return 0;
1261 }
1262
1263 static int filter_root(struct root_info *ri,
1264                        struct btrfs_list_filter_set *set)
1265 {
1266         int i, ret;
1267
1268         if (!set || !set->nfilters)
1269                 return 1;
1270
1271         for (i = 0; i < set->nfilters; i++) {
1272                 if (!set->filters[i].filter_func)
1273                         break;
1274                 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1275                 if (!ret)
1276                         return 0;
1277         }
1278         return 1;
1279 }
1280
1281 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1282                                     struct root_lookup *sort_tree,
1283                                     struct btrfs_list_filter_set *filter_set,
1284                                     struct btrfs_list_comparer_set *comp_set,
1285                                     int fd)
1286 {
1287         struct rb_node *n;
1288         struct root_info *entry;
1289         int ret;
1290         u64 top_id = btrfs_list_get_path_rootid(fd);
1291
1292         root_lookup_init(sort_tree);
1293
1294         n = rb_last(&all_subvols->root);
1295         while (n) {
1296                 entry = rb_entry(n, struct root_info, rb_node);
1297
1298                 resolve_root(all_subvols, entry, top_id);
1299                 ret = filter_root(entry, filter_set);
1300                 if (ret)
1301                         sort_tree_insert(sort_tree, entry, comp_set);
1302                 n = rb_prev(n);
1303         }
1304 }
1305
1306 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1307 {
1308         struct rb_node *n;
1309
1310         n = rb_first(&root_lookup->root);
1311         while (n) {
1312                 struct root_info *entry;
1313                 int ret;
1314                 entry = rb_entry(n, struct root_info, rb_node);
1315                 ret = lookup_ino_path(fd, entry);
1316                 if(ret < 0)
1317                         return ret;
1318                 n = rb_next(n);
1319         }
1320
1321         return 0;
1322 }
1323
1324 static void print_subvolume_column(struct root_info *subv,
1325                                    enum btrfs_list_column_enum column)
1326 {
1327         char tstr[256];
1328         char uuidparse[37];
1329
1330         BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1331
1332         switch (column) {
1333         case BTRFS_LIST_OBJECTID:
1334                 printf("%llu", subv->root_id);
1335                 break;
1336         case BTRFS_LIST_GENERATION:
1337                 printf("%llu", subv->gen);
1338                 break;
1339         case BTRFS_LIST_OGENERATION:
1340                 printf("%llu", subv->ogen);
1341                 break;
1342         case BTRFS_LIST_PARENT:
1343                 printf("%llu", subv->ref_tree);
1344                 break;
1345         case BTRFS_LIST_TOP_LEVEL:
1346                 printf("%llu", subv->top_id);
1347                 break;
1348         case BTRFS_LIST_OTIME:
1349                 if (subv->otime)
1350                         strftime(tstr, 256, "%Y-%m-%d %X",
1351                                  localtime(&subv->otime));
1352                 else
1353                         strcpy(tstr, "-");
1354                 printf("%s", tstr);
1355                 break;
1356         case BTRFS_LIST_UUID:
1357                 if (uuid_is_null(subv->uuid))
1358                         strcpy(uuidparse, "-");
1359                 else
1360                         uuid_unparse(subv->uuid, uuidparse);
1361                 printf("%s", uuidparse);
1362                 break;
1363         case BTRFS_LIST_PATH:
1364                 BUG_ON(!subv->full_path);
1365                 printf("%s", subv->full_path);
1366                 break;
1367         default:
1368                 break;
1369         }
1370 }
1371
1372 static void print_single_volume_info_table(struct root_info *subv)
1373 {
1374         int i;
1375
1376         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1377                 if (!btrfs_list_columns[i].need_print)
1378                         continue;
1379
1380                 print_subvolume_column(subv, i);
1381
1382                 if (i != BTRFS_LIST_PATH)
1383                         printf("\t");
1384
1385                 if (i == BTRFS_LIST_TOP_LEVEL)
1386                         printf("\t");
1387         }
1388         printf("\n");
1389 }
1390
1391 static void print_single_volume_info_default(struct root_info *subv)
1392 {
1393         int i;
1394
1395         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1396                 if (!btrfs_list_columns[i].need_print)
1397                         continue;
1398
1399                 printf("%s ", btrfs_list_columns[i].name);
1400                 print_subvolume_column(subv, i);
1401
1402                 if (i != BTRFS_LIST_PATH)
1403                         printf(" ");
1404         }
1405         printf("\n");
1406 }
1407
1408 static void print_all_volume_info_tab_head()
1409 {
1410         int i;
1411         int len;
1412         char barrier[20];
1413
1414         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1415                 if (btrfs_list_columns[i].need_print)
1416                         printf("%s\t", btrfs_list_columns[i].name);
1417
1418                 if (i == BTRFS_LIST_ALL-1)
1419                         printf("\n");
1420         }
1421
1422         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1423                 memset(barrier, 0, sizeof(barrier));
1424
1425                 if (btrfs_list_columns[i].need_print) {
1426                         len = strlen(btrfs_list_columns[i].name);
1427                         while (len--)
1428                                 strcat(barrier, "-");
1429
1430                         printf("%s\t", barrier);
1431                 }
1432                 if (i == BTRFS_LIST_ALL-1)
1433                         printf("\n");
1434         }
1435 }
1436
1437 static void print_all_volume_info(struct root_lookup *sorted_tree,
1438                                   int is_tab_result)
1439 {
1440         struct rb_node *n;
1441         struct root_info *entry;
1442
1443         if (is_tab_result)
1444                 print_all_volume_info_tab_head();
1445
1446         n = rb_first(&sorted_tree->root);
1447         while (n) {
1448                 entry = rb_entry(n, struct root_info, sort_node);
1449                 if (is_tab_result)
1450                         print_single_volume_info_table(entry);
1451                 else
1452                         print_single_volume_info_default(entry);
1453                 n = rb_next(n);
1454         }
1455 }
1456
1457 int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set,
1458                        struct btrfs_list_comparer_set *comp_set,
1459                        int is_tab_result)
1460 {
1461         struct root_lookup root_lookup;
1462         struct root_lookup root_sort;
1463         int ret;
1464
1465         ret = __list_subvol_search(fd, &root_lookup);
1466         if (ret) {
1467                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1468                                 strerror(errno));
1469                 return ret;
1470         }
1471
1472         /*
1473          * now we have an rbtree full of root_info objects, but we need to fill
1474          * in their path names within the subvol that is referencing each one.
1475          */
1476         ret = __list_subvol_fill_paths(fd, &root_lookup);
1477         if (ret < 0)
1478                 return ret;
1479
1480         __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1481                                  comp_set, fd);
1482
1483         print_all_volume_info(&root_sort, is_tab_result);
1484         __free_all_subvolumn(&root_lookup);
1485         return ret;
1486 }
1487
1488 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1489                             struct btrfs_file_extent_item *item,
1490                             u64 found_gen, u64 *cache_dirid,
1491                             char **cache_dir_name, u64 *cache_ino,
1492                             char **cache_full_name)
1493 {
1494         u64 len = 0;
1495         u64 disk_start = 0;
1496         u64 disk_offset = 0;
1497         u8 type;
1498         int compressed = 0;
1499         int flags = 0;
1500         char *name = NULL;
1501
1502         if (sh->objectid == *cache_ino) {
1503                 name = *cache_full_name;
1504         } else if (*cache_full_name) {
1505                 free(*cache_full_name);
1506                 *cache_full_name = NULL;
1507         }
1508         if (!name) {
1509                 name = ino_resolve(fd, sh->objectid, cache_dirid,
1510                                    cache_dir_name);
1511                 *cache_full_name = name;
1512                 *cache_ino = sh->objectid;
1513         }
1514         if (!name)
1515                 return -EIO;
1516
1517         type = btrfs_stack_file_extent_type(item);
1518         compressed = btrfs_stack_file_extent_compression(item);
1519
1520         if (type == BTRFS_FILE_EXTENT_REG ||
1521             type == BTRFS_FILE_EXTENT_PREALLOC) {
1522                 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1523                 disk_offset = btrfs_stack_file_extent_offset(item);
1524                 len = btrfs_stack_file_extent_num_bytes(item);
1525         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1526                 disk_start = 0;
1527                 disk_offset = 0;
1528                 len = btrfs_stack_file_extent_ram_bytes(item);
1529         } else {
1530                 printf("unhandled extent type %d for inode %llu "
1531                        "file offset %llu gen %llu\n",
1532                         type,
1533                         (unsigned long long)sh->objectid,
1534                         (unsigned long long)sh->offset,
1535                         (unsigned long long)found_gen);
1536
1537                 return -EIO;
1538         }
1539         printf("inode %llu file offset %llu len %llu disk start %llu "
1540                "offset %llu gen %llu flags ",
1541                (unsigned long long)sh->objectid,
1542                (unsigned long long)sh->offset,
1543                (unsigned long long)len,
1544                (unsigned long long)disk_start,
1545                (unsigned long long)disk_offset,
1546                (unsigned long long)found_gen);
1547
1548         if (compressed) {
1549                 printf("COMPRESS");
1550                 flags++;
1551         }
1552         if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1553                 printf("%sPREALLOC", flags ? "|" : "");
1554                 flags++;
1555         }
1556         if (type == BTRFS_FILE_EXTENT_INLINE) {
1557                 printf("%sINLINE", flags ? "|" : "");
1558                 flags++;
1559         }
1560         if (!flags)
1561                 printf("NONE");
1562
1563         printf(" %s\n", name);
1564         return 0;
1565 }
1566
1567 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1568 {
1569         int ret;
1570         struct btrfs_ioctl_search_args args;
1571         struct btrfs_ioctl_search_key *sk = &args.key;
1572         struct btrfs_ioctl_search_header sh;
1573         struct btrfs_file_extent_item *item;
1574         unsigned long off = 0;
1575         u64 found_gen;
1576         u64 max_found = 0;
1577         int i;
1578         int e;
1579         u64 cache_dirid = 0;
1580         u64 cache_ino = 0;
1581         char *cache_dir_name = NULL;
1582         char *cache_full_name = NULL;
1583         struct btrfs_file_extent_item backup;
1584
1585         memset(&backup, 0, sizeof(backup));
1586         memset(&args, 0, sizeof(args));
1587
1588         sk->tree_id = root_id;
1589
1590         /*
1591          * set all the other params to the max, we'll take any objectid
1592          * and any trans
1593          */
1594         sk->max_objectid = (u64)-1;
1595         sk->max_offset = (u64)-1;
1596         sk->max_transid = (u64)-1;
1597         sk->max_type = BTRFS_EXTENT_DATA_KEY;
1598         sk->min_transid = oldest_gen;
1599         /* just a big number, doesn't matter much */
1600         sk->nr_items = 4096;
1601
1602         max_found = find_root_gen(fd);
1603         while(1) {
1604                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1605                 e = errno;
1606                 if (ret < 0) {
1607                         fprintf(stderr, "ERROR: can't perform the search- %s\n",
1608                                 strerror(e));
1609                         return ret;
1610                 }
1611                 /* the ioctl returns the number of item it found in nr_items */
1612                 if (sk->nr_items == 0)
1613                         break;
1614
1615                 off = 0;
1616
1617                 /*
1618                  * for each item, pull the key out of the header and then
1619                  * read the root_ref item it contains
1620                  */
1621                 for (i = 0; i < sk->nr_items; i++) {
1622                         memcpy(&sh, args.buf + off, sizeof(sh));
1623                         off += sizeof(sh);
1624
1625                         /*
1626                          * just in case the item was too big, pass something other
1627                          * than garbage
1628                          */
1629                         if (sh.len == 0)
1630                                 item = &backup;
1631                         else
1632                                 item = (struct btrfs_file_extent_item *)(args.buf +
1633                                                                  off);
1634                         found_gen = btrfs_stack_file_extent_generation(item);
1635                         if (sh.type == BTRFS_EXTENT_DATA_KEY &&
1636                             found_gen >= oldest_gen) {
1637                                 print_one_extent(fd, &sh, item, found_gen,
1638                                                  &cache_dirid, &cache_dir_name,
1639                                                  &cache_ino, &cache_full_name);
1640                         }
1641                         off += sh.len;
1642
1643                         /*
1644                          * record the mins in sk so we can make sure the
1645                          * next search doesn't repeat this root
1646                          */
1647                         sk->min_objectid = sh.objectid;
1648                         sk->min_offset = sh.offset;
1649                         sk->min_type = sh.type;
1650                 }
1651                 sk->nr_items = 4096;
1652                 if (sk->min_offset < (u64)-1)
1653                         sk->min_offset++;
1654                 else if (sk->min_objectid < (u64)-1) {
1655                         sk->min_objectid++;
1656                         sk->min_offset = 0;
1657                         sk->min_type = 0;
1658                 } else
1659                         break;
1660         }
1661         free(cache_dir_name);
1662         free(cache_full_name);
1663         printf("transid marker was %llu\n", (unsigned long long)max_found);
1664         return ret;
1665 }
1666
1667 char *btrfs_list_path_for_root(int fd, u64 root)
1668 {
1669         struct root_lookup root_lookup;
1670         struct rb_node *n;
1671         char *ret_path = NULL;
1672         int ret;
1673         u64 top_id = btrfs_list_get_path_rootid(fd);
1674
1675         ret = __list_subvol_search(fd, &root_lookup);
1676         if (ret < 0)
1677                 return ERR_PTR(ret);
1678
1679         ret = __list_subvol_fill_paths(fd, &root_lookup);
1680         if (ret < 0)
1681                 return ERR_PTR(ret);
1682
1683         n = rb_last(&root_lookup.root);
1684         while (n) {
1685                 struct root_info *entry;
1686
1687                 entry = rb_entry(n, struct root_info, rb_node);
1688                 resolve_root(&root_lookup, entry, top_id);
1689                 if (entry->root_id == root) {
1690                         ret_path = entry->full_path;
1691                         entry->full_path = NULL;
1692                 }
1693
1694                 n = rb_prev(n);
1695         }
1696         __free_all_subvolumn(&root_lookup);
1697
1698         return ret_path;
1699 }
1700
1701 int btrfs_list_parse_sort_string(char *optarg,
1702                                  struct btrfs_list_comparer_set **comps)
1703 {
1704         int order;
1705         int flag;
1706         char *p;
1707         char **ptr_argv;
1708         int what_to_sort;
1709
1710         while ((p = strtok(optarg, ",")) != NULL) {
1711                 flag = 0;
1712                 ptr_argv = all_sort_items;
1713
1714                 while (*ptr_argv) {
1715                         if (strcmp(*ptr_argv, p) == 0) {
1716                                 flag = 1;
1717                                 break;
1718                         } else {
1719                                 p++;
1720                                 if (strcmp(*ptr_argv, p) == 0) {
1721                                         flag = 1;
1722                                         p--;
1723                                         break;
1724                                 }
1725                                 p--;
1726                         }
1727                         ptr_argv++;
1728                 }
1729
1730                 if (flag == 0)
1731                         return -1;
1732
1733                 else {
1734                         if (*p == '+') {
1735                                 order = 0;
1736                                 p++;
1737                         } else if (*p == '-') {
1738                                 order = 1;
1739                                 p++;
1740                         } else
1741                                 order = 0;
1742
1743                         what_to_sort = btrfs_list_get_sort_item(p);
1744                         btrfs_list_setup_comparer(comps, what_to_sort, order);
1745                 }
1746                 optarg = NULL;
1747         }
1748
1749         return 0;
1750 }
1751
1752 /*
1753  * This function is used to parse the argument of filter condition.
1754  *
1755  * type is the filter object.
1756  */
1757 int btrfs_list_parse_filter_string(char *optarg,
1758                                    struct btrfs_list_filter_set **filters,
1759                                    enum btrfs_list_filter_enum type)
1760 {
1761
1762         u64 arg;
1763         char *ptr_parse_end = NULL;
1764         char *ptr_optarg_end = optarg + strlen(optarg);
1765
1766         switch (*(optarg++)) {
1767         case '+':
1768                 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1769                 type += 2;
1770                 if (ptr_parse_end != ptr_optarg_end)
1771                         return -1;
1772
1773                 btrfs_list_setup_filter(filters, type, arg);
1774                 break;
1775         case '-':
1776                 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1777                 type += 1;
1778                 if (ptr_parse_end != ptr_optarg_end)
1779                         return -1;
1780
1781                 btrfs_list_setup_filter(filters, type, arg);
1782                 break;
1783         default:
1784                 optarg--;
1785                 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1786
1787                 if (ptr_parse_end != ptr_optarg_end)
1788                         return -1;
1789                 btrfs_list_setup_filter(filters, type, arg);
1790                 break;
1791         }
1792
1793         return 0;
1794 }
1795
1796 u64 btrfs_list_get_path_rootid(int fd)
1797 {
1798         int  ret;
1799         struct btrfs_ioctl_ino_lookup_args args;
1800
1801         memset(&args, 0, sizeof(args));
1802         args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1803
1804         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1805         if (ret < 0) {
1806                 fprintf(stderr,
1807                         "ERROR: can't perform the search -%s\n",
1808                         strerror(errno));
1809                 return ret;
1810         }
1811         return args.treeid;
1812 }