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