f079d998fb28a73737e00259a6c887fc37f85b41
[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 the ref_tree refers to ourselves, we're at the top */
631                 if (next == found->root_id) {
632                         ri->top_id = next;
633                         break;
634                 }
635
636                 /*
637                  * if the ref_tree wasn't in our tree of roots, we're
638                  * at the top
639                  */
640                 found = root_tree_search(rl, next);
641                 if (!found) {
642                         ri->top_id = next;
643                         break;
644                 }
645         }
646
647         ri->full_path = full_path;
648
649         return 0;
650 }
651
652 /*
653  * for a single root_info, ask the kernel to give us a path name
654  * inside it's ref_root for the dir_id where it lives.
655  *
656  * This fills in root_info->path with the path to the directory and and
657  * appends this root's name.
658  */
659 static int lookup_ino_path(int fd, struct root_info *ri)
660 {
661         struct btrfs_ioctl_ino_lookup_args args;
662         int ret, e;
663
664         if (ri->path)
665                 return 0;
666
667         memset(&args, 0, sizeof(args));
668         args.treeid = ri->ref_tree;
669         args.objectid = ri->dir_id;
670
671         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
672         e = errno;
673         if (ret) {
674                 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
675                         (unsigned long long)ri->ref_tree,
676                         strerror(e));
677                 return ret;
678         }
679
680         if (args.name[0]) {
681                 /*
682                  * we're in a subdirectory of ref_tree, the kernel ioctl
683                  * puts a / in there for us
684                  */
685                 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
686                 if (!ri->path) {
687                         perror("malloc failed");
688                         exit(1);
689                 }
690                 strcpy(ri->path, args.name);
691                 strcat(ri->path, ri->name);
692         } else {
693                 /* we're at the root of ref_tree */
694                 ri->path = strdup(ri->name);
695                 if (!ri->path) {
696                         perror("strdup failed");
697                         exit(1);
698                 }
699         }
700         return 0;
701 }
702
703 /* finding the generation for a given path is a two step process.
704  * First we use the inode loookup routine to find out the root id
705  *
706  * Then we use the tree search ioctl to scan all the root items for a
707  * given root id and spit out the latest generation we can find
708  */
709 static u64 find_root_gen(int fd)
710 {
711         struct btrfs_ioctl_ino_lookup_args ino_args;
712         int ret;
713         struct btrfs_ioctl_search_args args;
714         struct btrfs_ioctl_search_key *sk = &args.key;
715         struct btrfs_ioctl_search_header *sh;
716         unsigned long off = 0;
717         u64 max_found = 0;
718         int i;
719         int e;
720
721         memset(&ino_args, 0, sizeof(ino_args));
722         ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
723
724         /* this ioctl fills in ino_args->treeid */
725         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
726         e = errno;
727         if (ret) {
728                 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
729                         (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
730                         strerror(e));
731                 return 0;
732         }
733
734         memset(&args, 0, sizeof(args));
735
736         sk->tree_id = 1;
737
738         /*
739          * there may be more than one ROOT_ITEM key if there are
740          * snapshots pending deletion, we have to loop through
741          * them.
742          */
743         sk->min_objectid = ino_args.treeid;
744         sk->max_objectid = ino_args.treeid;
745         sk->max_type = BTRFS_ROOT_ITEM_KEY;
746         sk->min_type = BTRFS_ROOT_ITEM_KEY;
747         sk->max_offset = (u64)-1;
748         sk->max_transid = (u64)-1;
749         sk->nr_items = 4096;
750
751         while (1) {
752                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
753                 e = errno;
754                 if (ret < 0) {
755                         fprintf(stderr, "ERROR: can't perform the search - %s\n",
756                                 strerror(e));
757                         return 0;
758                 }
759                 /* the ioctl returns the number of item it found in nr_items */
760                 if (sk->nr_items == 0)
761                         break;
762
763                 off = 0;
764                 for (i = 0; i < sk->nr_items; i++) {
765                         struct btrfs_root_item *item;
766                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
767                                                                   off);
768
769                         off += sizeof(*sh);
770                         item = (struct btrfs_root_item *)(args.buf + off);
771                         off += sh->len;
772
773                         sk->min_objectid = sh->objectid;
774                         sk->min_type = sh->type;
775                         sk->min_offset = sh->offset;
776
777                         if (sh->objectid > ino_args.treeid)
778                                 break;
779
780                         if (sh->objectid == ino_args.treeid &&
781                             sh->type == BTRFS_ROOT_ITEM_KEY) {
782                                 max_found = max(max_found,
783                                                 btrfs_root_generation(item));
784                         }
785                 }
786                 if (sk->min_offset < (u64)-1)
787                         sk->min_offset++;
788                 else
789                         break;
790
791                 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
792                         break;
793                 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
794                         break;
795         }
796         return max_found;
797 }
798
799 /* pass in a directory id and this will return
800  * the full path of the parent directory inside its
801  * subvolume root.
802  *
803  * It may return NULL if it is in the root, or an ERR_PTR if things
804  * go badly.
805  */
806 static char *__ino_resolve(int fd, u64 dirid)
807 {
808         struct btrfs_ioctl_ino_lookup_args args;
809         int ret;
810         char *full;
811         int e;
812
813         memset(&args, 0, sizeof(args));
814         args.objectid = dirid;
815
816         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
817         e = errno;
818         if (ret) {
819                 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
820                         (unsigned long long)dirid, strerror(e) );
821                 return ERR_PTR(ret);
822         }
823
824         if (args.name[0]) {
825                 /*
826                  * we're in a subdirectory of ref_tree, the kernel ioctl
827                  * puts a / in there for us
828                  */
829                 full = strdup(args.name);
830                 if (!full) {
831                         perror("malloc failed");
832                         return ERR_PTR(-ENOMEM);
833                 }
834         } else {
835                 /* we're at the root of ref_tree */
836                 full = NULL;
837         }
838         return full;
839 }
840
841 /*
842  * simple string builder, returning a new string with both
843  * dirid and name
844  */
845 char *build_name(char *dirid, char *name)
846 {
847         char *full;
848         if (!dirid)
849                 return strdup(name);
850
851         full = malloc(strlen(dirid) + strlen(name) + 1);
852         if (!full)
853                 return NULL;
854         strcpy(full, dirid);
855         strcat(full, name);
856         return full;
857 }
858
859 /*
860  * given an inode number, this returns the full path name inside the subvolume
861  * to that file/directory.  cache_dirid and cache_name are used to
862  * cache the results so we can avoid tree searches if a later call goes
863  * to the same directory or file name
864  */
865 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
866
867 {
868         u64 dirid;
869         char *dirname;
870         char *name;
871         char *full;
872         int ret;
873         struct btrfs_ioctl_search_args args;
874         struct btrfs_ioctl_search_key *sk = &args.key;
875         struct btrfs_ioctl_search_header *sh;
876         unsigned long off = 0;
877         int namelen;
878         int e;
879
880         memset(&args, 0, sizeof(args));
881
882         sk->tree_id = 0;
883
884         /*
885          * step one, we search for the inode back ref.  We just use the first
886          * one
887          */
888         sk->min_objectid = ino;
889         sk->max_objectid = ino;
890         sk->max_type = BTRFS_INODE_REF_KEY;
891         sk->max_offset = (u64)-1;
892         sk->min_type = BTRFS_INODE_REF_KEY;
893         sk->max_transid = (u64)-1;
894         sk->nr_items = 1;
895
896         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
897         e = errno;
898         if (ret < 0) {
899                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
900                         strerror(e));
901                 return NULL;
902         }
903         /* the ioctl returns the number of item it found in nr_items */
904         if (sk->nr_items == 0)
905                 return NULL;
906
907         off = 0;
908         sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
909
910         if (sh->type == BTRFS_INODE_REF_KEY) {
911                 struct btrfs_inode_ref *ref;
912                 dirid = sh->offset;
913
914                 ref = (struct btrfs_inode_ref *)(sh + 1);
915                 namelen = btrfs_stack_inode_ref_name_len(ref);
916
917                 name = (char *)(ref + 1);
918                 name = strndup(name, namelen);
919
920                 /* use our cached value */
921                 if (dirid == *cache_dirid && *cache_name) {
922                         dirname = *cache_name;
923                         goto build;
924                 }
925         } else {
926                 return NULL;
927         }
928         /*
929          * the inode backref gives us the file name and the parent directory id.
930          * From here we use __ino_resolve to get the path to the parent
931          */
932         dirname = __ino_resolve(fd, dirid);
933 build:
934         full = build_name(dirname, name);
935         if (*cache_name && dirname != *cache_name)
936                 free(*cache_name);
937
938         *cache_name = dirname;
939         *cache_dirid = dirid;
940         free(name);
941
942         return full;
943 }
944
945 int btrfs_list_get_default_subvolume(int fd, u64 *default_id)
946 {
947         struct btrfs_ioctl_search_args args;
948         struct btrfs_ioctl_search_key *sk = &args.key;
949         struct btrfs_ioctl_search_header *sh;
950         u64 found = 0;
951         int ret;
952
953         memset(&args, 0, sizeof(args));
954
955         /*
956          * search for a dir item with a name 'default' in the tree of
957          * tree roots, it should point us to a default root
958          */
959         sk->tree_id = 1;
960
961         /* don't worry about ancient format and request only one item */
962         sk->nr_items = 1;
963
964         sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
965         sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
966         sk->max_type = BTRFS_DIR_ITEM_KEY;
967         sk->min_type = BTRFS_DIR_ITEM_KEY;
968         sk->max_offset = (u64)-1;
969         sk->max_transid = (u64)-1;
970
971         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
972         if (ret < 0)
973                 return ret;
974
975         /* the ioctl returns the number of items it found in nr_items */
976         if (sk->nr_items == 0)
977                 goto out;
978
979         sh = (struct btrfs_ioctl_search_header *)args.buf;
980
981         if (sh->type == BTRFS_DIR_ITEM_KEY) {
982                 struct btrfs_dir_item *di;
983                 int name_len;
984                 char *name;
985
986                 di = (struct btrfs_dir_item *)(sh + 1);
987                 name_len = btrfs_stack_dir_name_len(di);
988                 name = (char *)(di + 1);
989
990                 if (!strncmp("default", name, name_len))
991                         found = btrfs_disk_key_objectid(&di->location);
992         }
993
994 out:
995         *default_id = found;
996         return 0;
997 }
998
999 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
1000 {
1001         int ret;
1002         struct btrfs_ioctl_search_args args;
1003         struct btrfs_ioctl_search_key *sk = &args.key;
1004         struct btrfs_ioctl_search_header *sh;
1005         struct btrfs_root_ref *ref;
1006         struct btrfs_root_item *ri;
1007         unsigned long off = 0;
1008         int name_len;
1009         char *name;
1010         u64 dir_id;
1011         u64 gen = 0;
1012         u64 ogen;
1013         u64 flags;
1014         int i;
1015         time_t t;
1016         u8 uuid[BTRFS_UUID_SIZE];
1017
1018         root_lookup_init(root_lookup);
1019         memset(&args, 0, sizeof(args));
1020
1021         /* search in the tree of tree roots */
1022         sk->tree_id = 1;
1023
1024         /*
1025          * set the min and max to backref keys.  The search will
1026          * only send back this type of key now.
1027          */
1028         sk->max_type = BTRFS_ROOT_BACKREF_KEY;
1029         sk->min_type = BTRFS_ROOT_ITEM_KEY;
1030
1031         sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
1032
1033         /*
1034          * set all the other params to the max, we'll take any objectid
1035          * and any trans
1036          */
1037         sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
1038         sk->max_offset = (u64)-1;
1039         sk->max_transid = (u64)-1;
1040
1041         /* just a big number, doesn't matter much */
1042         sk->nr_items = 4096;
1043
1044         while(1) {
1045                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1046                 if (ret < 0)
1047                         return ret;
1048                 /* the ioctl returns the number of item it found in nr_items */
1049                 if (sk->nr_items == 0)
1050                         break;
1051
1052                 off = 0;
1053
1054                 /*
1055                  * for each item, pull the key out of the header and then
1056                  * read the root_ref item it contains
1057                  */
1058                 for (i = 0; i < sk->nr_items; i++) {
1059                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
1060                                                                   off);
1061                         off += sizeof(*sh);
1062                         if (sh->type == BTRFS_ROOT_BACKREF_KEY) {
1063                                 ref = (struct btrfs_root_ref *)(args.buf + off);
1064                                 name_len = btrfs_stack_root_ref_name_len(ref);
1065                                 name = (char *)(ref + 1);
1066                                 dir_id = btrfs_stack_root_ref_dirid(ref);
1067
1068                                 add_root(root_lookup, sh->objectid, sh->offset,
1069                                          0, 0, dir_id, name, name_len, 0, 0, 0,
1070                                          NULL);
1071                         } else if (sh->type == BTRFS_ROOT_ITEM_KEY) {
1072                                 ri = (struct btrfs_root_item *)(args.buf + off);
1073                                 gen = btrfs_root_generation(ri);
1074                                 flags = btrfs_root_flags(ri);
1075                                 if(sh->len >
1076                                    sizeof(struct btrfs_root_item_v0)) {
1077                                         t = ri->otime.sec;
1078                                         ogen = btrfs_root_otransid(ri);
1079                                         memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
1080                                 } else {
1081                                         t = 0;
1082                                         ogen = 0;
1083                                         memset(uuid, 0, BTRFS_UUID_SIZE);
1084                                 }
1085
1086                                 add_root(root_lookup, sh->objectid, 0,
1087                                          sh->offset, flags, 0, NULL, 0, ogen,
1088                                          gen, t, uuid);
1089                         }
1090
1091                         off += sh->len;
1092
1093                         /*
1094                          * record the mins in sk so we can make sure the
1095                          * next search doesn't repeat this root
1096                          */
1097                         sk->min_objectid = sh->objectid;
1098                         sk->min_type = sh->type;
1099                         sk->min_offset = sh->offset;
1100                 }
1101                 sk->nr_items = 4096;
1102                 sk->min_offset++;
1103                 if (!sk->min_offset)    /* overflow */
1104                         sk->min_type++;
1105                 else
1106                         continue;
1107
1108                 if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) {
1109                         sk->min_type = BTRFS_ROOT_ITEM_KEY;
1110                         sk->min_objectid++;
1111                 } else
1112                         continue;
1113
1114                 if (sk->min_objectid > sk->max_objectid)
1115                         break;
1116         }
1117
1118         return 0;
1119 }
1120
1121 static int filter_by_rootid(struct root_info *ri, u64 data)
1122 {
1123         return ri->root_id == data;
1124 }
1125
1126 static int filter_snapshot(struct root_info *ri, u64 data)
1127 {
1128         return !!ri->root_offset;
1129 }
1130
1131 static int filter_flags(struct root_info *ri, u64 flags)
1132 {
1133         return ri->flags & flags;
1134 }
1135
1136 static int filter_gen_more(struct root_info *ri, u64 data)
1137 {
1138         return ri->gen >= data;
1139 }
1140
1141 static int filter_gen_less(struct root_info *ri, u64 data)
1142 {
1143         return ri->gen <= data;
1144 }
1145
1146 static int filter_gen_equal(struct root_info  *ri, u64 data)
1147 {
1148         return ri->gen == data;
1149 }
1150
1151 static int filter_cgen_more(struct root_info *ri, u64 data)
1152 {
1153         return ri->ogen >= data;
1154 }
1155
1156 static int filter_cgen_less(struct root_info *ri, u64 data)
1157 {
1158         return ri->ogen <= data;
1159 }
1160
1161 static int filter_cgen_equal(struct root_info *ri, u64 data)
1162 {
1163         return ri->ogen == data;
1164 }
1165
1166 static int filter_topid_equal(struct root_info *ri, u64 data)
1167 {
1168         return ri->top_id == data;
1169 }
1170
1171 static btrfs_list_filter_func all_filter_funcs[] = {
1172         [BTRFS_LIST_FILTER_ROOTID]              = filter_by_rootid,
1173         [BTRFS_LIST_FILTER_SNAPSHOT_ONLY]       = filter_snapshot,
1174         [BTRFS_LIST_FILTER_FLAGS]               = filter_flags,
1175         [BTRFS_LIST_FILTER_GEN_MORE]            = filter_gen_more,
1176         [BTRFS_LIST_FILTER_GEN_LESS]            = filter_gen_less,
1177         [BTRFS_LIST_FILTER_GEN_EQUAL]           = filter_gen_equal,
1178         [BTRFS_LIST_FILTER_CGEN_MORE]           = filter_cgen_more,
1179         [BTRFS_LIST_FILTER_CGEN_LESS]           = filter_cgen_less,
1180         [BTRFS_LIST_FILTER_CGEN_EQUAL]          = filter_cgen_equal,
1181         [BTRFS_LIST_FILTER_TOPID_EQUAL]         = filter_topid_equal,
1182 };
1183
1184 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
1185 {
1186         struct btrfs_list_filter_set *set;
1187         int size;
1188
1189         size = sizeof(struct btrfs_list_filter_set) +
1190                BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
1191         set = malloc(size);
1192         if (!set) {
1193                 fprintf(stderr, "memory allocation failed\n");
1194                 exit(1);
1195         }
1196
1197         memset(set, 0, size);
1198         set->total = BTRFS_LIST_NFILTERS_INCREASE;
1199
1200         return set;
1201 }
1202
1203 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
1204 {
1205         free(filter_set);
1206 }
1207
1208 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
1209                             enum btrfs_list_filter_enum filter, u64 data)
1210 {
1211         struct btrfs_list_filter_set *set = *filter_set;
1212         int size;
1213
1214         BUG_ON(!set);
1215         BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
1216         BUG_ON(set->nfilters > set->total);
1217
1218         if (set->nfilters == set->total) {
1219                 size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
1220                 size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
1221                 set = realloc(set, size);
1222                 if (!set) {
1223                         fprintf(stderr, "memory allocation failed\n");
1224                         exit(1);
1225                 }
1226
1227                 memset(&set->filters[set->total], 0,
1228                        BTRFS_LIST_NFILTERS_INCREASE *
1229                        sizeof(struct btrfs_list_filter));
1230                 set->total += BTRFS_LIST_NFILTERS_INCREASE;
1231                 *filter_set = set;
1232         }
1233
1234         BUG_ON(set->filters[set->nfilters].filter_func);
1235
1236         set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
1237         set->filters[set->nfilters].data = data;
1238         set->nfilters++;
1239         return 0;
1240 }
1241
1242 static int filter_root(struct root_info *ri,
1243                        struct btrfs_list_filter_set *set)
1244 {
1245         int i, ret;
1246
1247         if (!set || !set->nfilters)
1248                 return 1;
1249
1250         for (i = 0; i < set->nfilters; i++) {
1251                 if (!set->filters[i].filter_func)
1252                         break;
1253                 ret = set->filters[i].filter_func(ri, set->filters[i].data);
1254                 if (!ret)
1255                         return 0;
1256         }
1257         return 1;
1258 }
1259
1260 static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
1261                                     struct root_lookup *sort_tree,
1262                                     struct btrfs_list_filter_set *filter_set,
1263                                     struct btrfs_list_comparer_set *comp_set,
1264                                     int fd)
1265 {
1266         struct rb_node *n;
1267         struct root_info *entry;
1268         int ret;
1269         u64 top_id = btrfs_list_get_path_rootid(fd);
1270
1271         root_lookup_init(sort_tree);
1272
1273         n = rb_last(&all_subvols->root);
1274         while (n) {
1275                 entry = rb_entry(n, struct root_info, rb_node);
1276
1277                 resolve_root(all_subvols, entry, top_id);
1278                 ret = filter_root(entry, filter_set);
1279                 if (ret)
1280                         sort_tree_insert(sort_tree, entry, comp_set);
1281                 n = rb_prev(n);
1282         }
1283 }
1284
1285 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
1286 {
1287         struct rb_node *n;
1288
1289         n = rb_first(&root_lookup->root);
1290         while (n) {
1291                 struct root_info *entry;
1292                 int ret;
1293                 entry = rb_entry(n, struct root_info, rb_node);
1294                 ret = lookup_ino_path(fd, entry);
1295                 if(ret < 0)
1296                         return ret;
1297                 n = rb_next(n);
1298         }
1299
1300         return 0;
1301 }
1302
1303 static void print_subvolume_column(struct root_info *subv,
1304                                    enum btrfs_list_column_enum column)
1305 {
1306         char tstr[256];
1307         char uuidparse[37];
1308
1309         BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
1310
1311         switch (column) {
1312         case BTRFS_LIST_OBJECTID:
1313                 printf("%llu", subv->root_id);
1314                 break;
1315         case BTRFS_LIST_GENERATION:
1316                 printf("%llu", subv->gen);
1317                 break;
1318         case BTRFS_LIST_OGENERATION:
1319                 printf("%llu", subv->ogen);
1320                 break;
1321         case BTRFS_LIST_PARENT:
1322                 printf("%llu", subv->ref_tree);
1323                 break;
1324         case BTRFS_LIST_TOP_LEVEL:
1325                 printf("%llu", subv->top_id);
1326                 break;
1327         case BTRFS_LIST_OTIME:
1328                 if (subv->otime)
1329                         strftime(tstr, 256, "%Y-%m-%d %X",
1330                                  localtime(&subv->otime));
1331                 else
1332                         strcpy(tstr, "-");
1333                 printf("%s", tstr);
1334                 break;
1335         case BTRFS_LIST_UUID:
1336                 if (uuid_is_null(subv->uuid))
1337                         strcpy(uuidparse, "-");
1338                 else
1339                         uuid_unparse(subv->uuid, uuidparse);
1340                 printf("%s", uuidparse);
1341                 break;
1342         case BTRFS_LIST_PATH:
1343                 BUG_ON(!subv->full_path);
1344                 printf("%s", subv->full_path);
1345                 break;
1346         default:
1347                 break;
1348         }
1349 }
1350
1351 static void print_single_volume_info_table(struct root_info *subv)
1352 {
1353         int i;
1354
1355         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1356                 if (!btrfs_list_columns[i].need_print)
1357                         continue;
1358
1359                 print_subvolume_column(subv, i);
1360
1361                 if (i != BTRFS_LIST_PATH)
1362                         printf("\t");
1363
1364                 if (i == BTRFS_LIST_TOP_LEVEL)
1365                         printf("\t");
1366         }
1367         printf("\n");
1368 }
1369
1370 static void print_single_volume_info_default(struct root_info *subv)
1371 {
1372         int i;
1373
1374         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1375                 if (!btrfs_list_columns[i].need_print)
1376                         continue;
1377
1378                 printf("%s ", btrfs_list_columns[i].name);
1379                 print_subvolume_column(subv, i);
1380
1381                 if (i != BTRFS_LIST_PATH)
1382                         printf(" ");
1383         }
1384         printf("\n");
1385 }
1386
1387 static void print_all_volume_info_tab_head()
1388 {
1389         int i;
1390         int len;
1391         char barrier[20];
1392
1393         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1394                 if (btrfs_list_columns[i].need_print)
1395                         printf("%s\t", btrfs_list_columns[i].name);
1396
1397                 if (i == BTRFS_LIST_ALL-1)
1398                         printf("\n");
1399         }
1400
1401         for (i = 0; i < BTRFS_LIST_ALL; i++) {
1402                 memset(barrier, 0, sizeof(barrier));
1403
1404                 if (btrfs_list_columns[i].need_print) {
1405                         len = strlen(btrfs_list_columns[i].name);
1406                         while (len--)
1407                                 strcat(barrier, "-");
1408
1409                         printf("%s\t", barrier);
1410                 }
1411                 if (i == BTRFS_LIST_ALL-1)
1412                         printf("\n");
1413         }
1414 }
1415
1416 static void print_all_volume_info(struct root_lookup *sorted_tree,
1417                                   int is_tab_result)
1418 {
1419         struct rb_node *n;
1420         struct root_info *entry;
1421
1422         if (is_tab_result)
1423                 print_all_volume_info_tab_head();
1424
1425         n = rb_first(&sorted_tree->root);
1426         while (n) {
1427                 entry = rb_entry(n, struct root_info, sort_node);
1428                 if (is_tab_result)
1429                         print_single_volume_info_table(entry);
1430                 else
1431                         print_single_volume_info_default(entry);
1432                 n = rb_next(n);
1433         }
1434 }
1435
1436 int btrfs_list_subvols(int fd, struct btrfs_list_filter_set *filter_set,
1437                        struct btrfs_list_comparer_set *comp_set,
1438                        int is_tab_result)
1439 {
1440         struct root_lookup root_lookup;
1441         struct root_lookup root_sort;
1442         int ret;
1443
1444         ret = __list_subvol_search(fd, &root_lookup);
1445         if (ret) {
1446                 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1447                                 strerror(errno));
1448                 return ret;
1449         }
1450
1451         /*
1452          * now we have an rbtree full of root_info objects, but we need to fill
1453          * in their path names within the subvol that is referencing each one.
1454          */
1455         ret = __list_subvol_fill_paths(fd, &root_lookup);
1456         if (ret < 0)
1457                 return ret;
1458
1459         __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
1460                                  comp_set, fd);
1461
1462         print_all_volume_info(&root_sort, is_tab_result);
1463         __free_all_subvolumn(&root_lookup);
1464         return ret;
1465 }
1466
1467 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1468                             struct btrfs_file_extent_item *item,
1469                             u64 found_gen, u64 *cache_dirid,
1470                             char **cache_dir_name, u64 *cache_ino,
1471                             char **cache_full_name)
1472 {
1473         u64 len = 0;
1474         u64 disk_start = 0;
1475         u64 disk_offset = 0;
1476         u8 type;
1477         int compressed = 0;
1478         int flags = 0;
1479         char *name = NULL;
1480
1481         if (sh->objectid == *cache_ino) {
1482                 name = *cache_full_name;
1483         } else if (*cache_full_name) {
1484                 free(*cache_full_name);
1485                 *cache_full_name = NULL;
1486         }
1487         if (!name) {
1488                 name = ino_resolve(fd, sh->objectid, cache_dirid,
1489                                    cache_dir_name);
1490                 *cache_full_name = name;
1491                 *cache_ino = sh->objectid;
1492         }
1493         if (!name)
1494                 return -EIO;
1495
1496         type = btrfs_stack_file_extent_type(item);
1497         compressed = btrfs_stack_file_extent_compression(item);
1498
1499         if (type == BTRFS_FILE_EXTENT_REG ||
1500             type == BTRFS_FILE_EXTENT_PREALLOC) {
1501                 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1502                 disk_offset = btrfs_stack_file_extent_offset(item);
1503                 len = btrfs_stack_file_extent_num_bytes(item);
1504         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1505                 disk_start = 0;
1506                 disk_offset = 0;
1507                 len = btrfs_stack_file_extent_ram_bytes(item);
1508         } else {
1509                 printf("unhandled extent type %d for inode %llu "
1510                        "file offset %llu gen %llu\n",
1511                         type,
1512                         (unsigned long long)sh->objectid,
1513                         (unsigned long long)sh->offset,
1514                         (unsigned long long)found_gen);
1515
1516                 return -EIO;
1517         }
1518         printf("inode %llu file offset %llu len %llu disk start %llu "
1519                "offset %llu gen %llu flags ",
1520                (unsigned long long)sh->objectid,
1521                (unsigned long long)sh->offset,
1522                (unsigned long long)len,
1523                (unsigned long long)disk_start,
1524                (unsigned long long)disk_offset,
1525                (unsigned long long)found_gen);
1526
1527         if (compressed) {
1528                 printf("COMPRESS");
1529                 flags++;
1530         }
1531         if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1532                 printf("%sPREALLOC", flags ? "|" : "");
1533                 flags++;
1534         }
1535         if (type == BTRFS_FILE_EXTENT_INLINE) {
1536                 printf("%sINLINE", flags ? "|" : "");
1537                 flags++;
1538         }
1539         if (!flags)
1540                 printf("NONE");
1541
1542         printf(" %s\n", name);
1543         return 0;
1544 }
1545
1546 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1547 {
1548         int ret;
1549         struct btrfs_ioctl_search_args args;
1550         struct btrfs_ioctl_search_key *sk = &args.key;
1551         struct btrfs_ioctl_search_header *sh;
1552         struct btrfs_file_extent_item *item;
1553         unsigned long off = 0;
1554         u64 found_gen;
1555         u64 max_found = 0;
1556         int i;
1557         int e;
1558         u64 cache_dirid = 0;
1559         u64 cache_ino = 0;
1560         char *cache_dir_name = NULL;
1561         char *cache_full_name = NULL;
1562         struct btrfs_file_extent_item backup;
1563
1564         memset(&backup, 0, sizeof(backup));
1565         memset(&args, 0, sizeof(args));
1566
1567         sk->tree_id = root_id;
1568
1569         /*
1570          * set all the other params to the max, we'll take any objectid
1571          * and any trans
1572          */
1573         sk->max_objectid = (u64)-1;
1574         sk->max_offset = (u64)-1;
1575         sk->max_transid = (u64)-1;
1576         sk->max_type = BTRFS_EXTENT_DATA_KEY;
1577         sk->min_transid = oldest_gen;
1578         /* just a big number, doesn't matter much */
1579         sk->nr_items = 4096;
1580
1581         max_found = find_root_gen(fd);
1582         while(1) {
1583                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1584                 e = errno;
1585                 if (ret < 0) {
1586                         fprintf(stderr, "ERROR: can't perform the search- %s\n",
1587                                 strerror(e));
1588                         return ret;
1589                 }
1590                 /* the ioctl returns the number of item it found in nr_items */
1591                 if (sk->nr_items == 0)
1592                         break;
1593
1594                 off = 0;
1595
1596                 /*
1597                  * for each item, pull the key out of the header and then
1598                  * read the root_ref item it contains
1599                  */
1600                 for (i = 0; i < sk->nr_items; i++) {
1601                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
1602                                                                   off);
1603                         off += sizeof(*sh);
1604
1605                         /*
1606                          * just in case the item was too big, pass something other
1607                          * than garbage
1608                          */
1609                         if (sh->len == 0)
1610                                 item = &backup;
1611                         else
1612                                 item = (struct btrfs_file_extent_item *)(args.buf +
1613                                                                  off);
1614                         found_gen = btrfs_stack_file_extent_generation(item);
1615                         if (sh->type == BTRFS_EXTENT_DATA_KEY &&
1616                             found_gen >= oldest_gen) {
1617                                 print_one_extent(fd, sh, item, found_gen,
1618                                                  &cache_dirid, &cache_dir_name,
1619                                                  &cache_ino, &cache_full_name);
1620                         }
1621                         off += sh->len;
1622
1623                         /*
1624                          * record the mins in sk so we can make sure the
1625                          * next search doesn't repeat this root
1626                          */
1627                         sk->min_objectid = sh->objectid;
1628                         sk->min_offset = sh->offset;
1629                         sk->min_type = sh->type;
1630                 }
1631                 sk->nr_items = 4096;
1632                 if (sk->min_offset < (u64)-1)
1633                         sk->min_offset++;
1634                 else if (sk->min_objectid < (u64)-1) {
1635                         sk->min_objectid++;
1636                         sk->min_offset = 0;
1637                         sk->min_type = 0;
1638                 } else
1639                         break;
1640         }
1641         free(cache_dir_name);
1642         free(cache_full_name);
1643         printf("transid marker was %llu\n", (unsigned long long)max_found);
1644         return ret;
1645 }
1646
1647 char *btrfs_list_path_for_root(int fd, u64 root)
1648 {
1649         struct root_lookup root_lookup;
1650         struct rb_node *n;
1651         char *ret_path = NULL;
1652         int ret;
1653         u64 top_id = btrfs_list_get_path_rootid(fd);
1654
1655         ret = __list_subvol_search(fd, &root_lookup);
1656         if (ret < 0)
1657                 return ERR_PTR(ret);
1658
1659         ret = __list_subvol_fill_paths(fd, &root_lookup);
1660         if (ret < 0)
1661                 return ERR_PTR(ret);
1662
1663         n = rb_last(&root_lookup.root);
1664         while (n) {
1665                 struct root_info *entry;
1666
1667                 entry = rb_entry(n, struct root_info, rb_node);
1668                 resolve_root(&root_lookup, entry, top_id);
1669                 if (entry->root_id == root) {
1670                         ret_path = entry->full_path;
1671                         entry->full_path = NULL;
1672                 }
1673
1674                 n = rb_prev(n);
1675         }
1676         __free_all_subvolumn(&root_lookup);
1677
1678         return ret_path;
1679 }
1680
1681 int btrfs_list_parse_sort_string(char *optarg,
1682                                  struct btrfs_list_comparer_set **comps)
1683 {
1684         int order;
1685         int flag;
1686         char *p;
1687         char **ptr_argv;
1688         int what_to_sort;
1689
1690         while ((p = strtok(optarg, ",")) != NULL) {
1691                 flag = 0;
1692                 ptr_argv = all_sort_items;
1693
1694                 while (*ptr_argv) {
1695                         if (strcmp(*ptr_argv, p) == 0) {
1696                                 flag = 1;
1697                                 break;
1698                         } else {
1699                                 p++;
1700                                 if (strcmp(*ptr_argv, p) == 0) {
1701                                         flag = 1;
1702                                         p--;
1703                                         break;
1704                                 }
1705                                 p--;
1706                         }
1707                         ptr_argv++;
1708                 }
1709
1710                 if (flag == 0)
1711                         return -1;
1712
1713                 else {
1714                         if (*p == '+') {
1715                                 order = 0;
1716                                 p++;
1717                         } else if (*p == '-') {
1718                                 order = 1;
1719                                 p++;
1720                         } else
1721                                 order = 0;
1722
1723                         what_to_sort = btrfs_list_get_sort_item(p);
1724                         btrfs_list_setup_comparer(comps, what_to_sort, order);
1725                 }
1726                 optarg = NULL;
1727         }
1728
1729         return 0;
1730 }
1731
1732 /*
1733  * This function is used to parse the argument of filter condition.
1734  *
1735  * type is the filter object.
1736  */
1737 int btrfs_list_parse_filter_string(char *optarg,
1738                                    struct btrfs_list_filter_set **filters,
1739                                    enum btrfs_list_filter_enum type)
1740 {
1741
1742         u64 arg;
1743         char *ptr_parse_end = NULL;
1744         char *ptr_optarg_end = optarg + strlen(optarg);
1745
1746         switch (*(optarg++)) {
1747         case '+':
1748                 arg = (u64)strtol(optarg, &ptr_parse_end, 10);
1749                 type += 2;
1750                 if (ptr_parse_end != ptr_optarg_end)
1751                         return -1;
1752
1753                 btrfs_list_setup_filter(filters, type, arg);
1754                 break;
1755         case '-':
1756                 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1757                 type += 1;
1758                 if (ptr_parse_end != ptr_optarg_end)
1759                         return -1;
1760
1761                 btrfs_list_setup_filter(filters, type, arg);
1762                 break;
1763         default:
1764                 optarg--;
1765                 arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
1766
1767                 if (ptr_parse_end != ptr_optarg_end)
1768                         return -1;
1769                 btrfs_list_setup_filter(filters, type, arg);
1770                 break;
1771         }
1772
1773         return 0;
1774 }
1775
1776 u64 btrfs_list_get_path_rootid(int fd)
1777 {
1778         int  ret;
1779         struct btrfs_ioctl_ino_lookup_args args;
1780
1781         memset(&args, 0, sizeof(args));
1782         args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1783
1784         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1785         if (ret < 0) {
1786                 fprintf(stderr,
1787                         "ERROR: can't perform the search -%s\n",
1788                         strerror(errno));
1789                 return ret;
1790         }
1791         return args.treeid;
1792 }