2 * Copyright (C) 2010 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <sys/ioctl.h>
20 #include <sys/mount.h>
23 #include <sys/types.h>
30 #include "transaction.h"
33 #include <uuid/uuid.h>
34 #include "btrfs-list.h"
35 #include "rbtree-utils.h"
37 #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX)
38 #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX)
40 /* we store all the roots we find in an rbtree so that we can
41 * search for them later.
51 } btrfs_list_columns
[] = {
64 .column_name
= "CGen",
69 .column_name
= "Parent",
74 .column_name
= "Top Level",
79 .column_name
= "OTime",
83 .name
= "parent_uuid",
84 .column_name
= "Parent UUID",
88 .name
= "received_uuid",
89 .column_name
= "Received UUID",
94 .column_name
= "UUID",
99 .column_name
= "Path",
109 static btrfs_list_filter_func all_filter_funcs
[];
110 static btrfs_list_comp_func all_comp_funcs
[];
112 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column
)
116 BUG_ON(column
< 0 || column
> BTRFS_LIST_ALL
);
118 if (column
< BTRFS_LIST_ALL
) {
119 btrfs_list_columns
[column
].need_print
= 1;
123 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++)
124 btrfs_list_columns
[i
].need_print
= 1;
127 static void root_lookup_init(struct root_lookup
*tree
)
129 tree
->root
.rb_node
= NULL
;
132 static int comp_entry_with_rootid(struct root_info
*entry1
,
133 struct root_info
*entry2
,
138 if (entry1
->root_id
> entry2
->root_id
)
140 else if (entry1
->root_id
< entry2
->root_id
)
145 return is_descending
? -ret
: ret
;
148 static int comp_entry_with_gen(struct root_info
*entry1
,
149 struct root_info
*entry2
,
154 if (entry1
->gen
> entry2
->gen
)
156 else if (entry1
->gen
< entry2
->gen
)
161 return is_descending
? -ret
: ret
;
164 static int comp_entry_with_ogen(struct root_info
*entry1
,
165 struct root_info
*entry2
,
170 if (entry1
->ogen
> entry2
->ogen
)
172 else if (entry1
->ogen
< entry2
->ogen
)
177 return is_descending
? -ret
: ret
;
180 static int comp_entry_with_path(struct root_info
*entry1
,
181 struct root_info
*entry2
,
186 if (strcmp(entry1
->full_path
, entry2
->full_path
) > 0)
188 else if (strcmp(entry1
->full_path
, entry2
->full_path
) < 0)
193 return is_descending
? -ret
: ret
;
196 static btrfs_list_comp_func all_comp_funcs
[] = {
197 [BTRFS_LIST_COMP_ROOTID
] = comp_entry_with_rootid
,
198 [BTRFS_LIST_COMP_OGEN
] = comp_entry_with_ogen
,
199 [BTRFS_LIST_COMP_GEN
] = comp_entry_with_gen
,
200 [BTRFS_LIST_COMP_PATH
] = comp_entry_with_path
,
203 static char *all_sort_items
[] = {
204 [BTRFS_LIST_COMP_ROOTID
] = "rootid",
205 [BTRFS_LIST_COMP_OGEN
] = "ogen",
206 [BTRFS_LIST_COMP_GEN
] = "gen",
207 [BTRFS_LIST_COMP_PATH
] = "path",
208 [BTRFS_LIST_COMP_MAX
] = NULL
,
211 static int btrfs_list_get_sort_item(char *sort_name
)
215 for (i
= 0; i
< BTRFS_LIST_COMP_MAX
; i
++) {
216 if (strcmp(sort_name
, all_sort_items
[i
]) == 0)
222 struct btrfs_list_comparer_set
*btrfs_list_alloc_comparer_set(void)
224 struct btrfs_list_comparer_set
*set
;
227 size
= sizeof(struct btrfs_list_comparer_set
) +
228 BTRFS_LIST_NCOMPS_INCREASE
* sizeof(struct btrfs_list_comparer
);
229 set
= calloc(1, size
);
231 fprintf(stderr
, "memory allocation failed\n");
235 set
->total
= BTRFS_LIST_NCOMPS_INCREASE
;
240 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set
*comp_set
)
245 static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set
**comp_set
,
246 enum btrfs_list_comp_enum comparer
, int is_descending
)
248 struct btrfs_list_comparer_set
*set
= *comp_set
;
252 BUG_ON(comparer
>= BTRFS_LIST_COMP_MAX
);
253 BUG_ON(set
->ncomps
> set
->total
);
255 if (set
->ncomps
== set
->total
) {
258 size
= set
->total
+ BTRFS_LIST_NCOMPS_INCREASE
;
259 size
= sizeof(*set
) + size
* sizeof(struct btrfs_list_comparer
);
261 set
= realloc(set
, size
);
263 fprintf(stderr
, "memory allocation failed\n");
268 memset(&set
->comps
[set
->total
], 0,
269 BTRFS_LIST_NCOMPS_INCREASE
*
270 sizeof(struct btrfs_list_comparer
));
271 set
->total
+= BTRFS_LIST_NCOMPS_INCREASE
;
275 BUG_ON(set
->comps
[set
->ncomps
].comp_func
);
277 set
->comps
[set
->ncomps
].comp_func
= all_comp_funcs
[comparer
];
278 set
->comps
[set
->ncomps
].is_descending
= is_descending
;
283 static int sort_comp(struct root_info
*entry1
, struct root_info
*entry2
,
284 struct btrfs_list_comparer_set
*set
)
286 int rootid_compared
= 0;
289 if (!set
|| !set
->ncomps
)
292 for (i
= 0; i
< set
->ncomps
; i
++) {
293 if (!set
->comps
[i
].comp_func
)
296 ret
= set
->comps
[i
].comp_func(entry1
, entry2
,
297 set
->comps
[i
].is_descending
);
301 if (set
->comps
[i
].comp_func
== comp_entry_with_rootid
)
305 if (!rootid_compared
) {
307 ret
= comp_entry_with_rootid(entry1
, entry2
, 0);
313 static int sort_tree_insert(struct root_lookup
*sort_tree
,
314 struct root_info
*ins
,
315 struct btrfs_list_comparer_set
*comp_set
)
317 struct rb_node
**p
= &sort_tree
->root
.rb_node
;
318 struct rb_node
*parent
= NULL
;
319 struct root_info
*curr
;
324 curr
= rb_entry(parent
, struct root_info
, sort_node
);
326 ret
= sort_comp(ins
, curr
, comp_set
);
335 rb_link_node(&ins
->sort_node
, parent
, p
);
336 rb_insert_color(&ins
->sort_node
, &sort_tree
->root
);
341 * insert a new root into the tree. returns the existing root entry
342 * if one is already there. Both root_id and ref_tree are used
345 static int root_tree_insert(struct root_lookup
*root_tree
,
346 struct root_info
*ins
)
348 struct rb_node
**p
= &root_tree
->root
.rb_node
;
349 struct rb_node
* parent
= NULL
;
350 struct root_info
*curr
;
355 curr
= rb_entry(parent
, struct root_info
, rb_node
);
357 ret
= comp_entry_with_rootid(ins
, curr
, 0);
366 rb_link_node(&ins
->rb_node
, parent
, p
);
367 rb_insert_color(&ins
->rb_node
, &root_tree
->root
);
372 * find a given root id in the tree. We return the smallest one,
373 * rb_next can be used to move forward looking for more if required
375 static struct root_info
*root_tree_search(struct root_lookup
*root_tree
,
378 struct rb_node
*n
= root_tree
->root
.rb_node
;
379 struct root_info
*entry
;
380 struct root_info tmp
;
383 tmp
.root_id
= root_id
;
386 entry
= rb_entry(n
, struct root_info
, rb_node
);
388 ret
= comp_entry_with_rootid(&tmp
, entry
, 0);
399 static int update_root(struct root_lookup
*root_lookup
,
400 u64 root_id
, u64 ref_tree
, u64 root_offset
, u64 flags
,
401 u64 dir_id
, char *name
, int name_len
, u64 ogen
, u64 gen
,
402 time_t ot
, void *uuid
, void *puuid
, void *ruuid
)
404 struct root_info
*ri
;
406 ri
= root_tree_search(root_lookup
, root_id
);
407 if (!ri
|| ri
->root_id
!= root_id
)
409 if (name
&& name_len
> 0) {
412 ri
->name
= malloc(name_len
+ 1);
414 fprintf(stderr
, "memory allocation failed\n");
417 strncpy(ri
->name
, name
, name_len
);
418 ri
->name
[name_len
] = 0;
421 ri
->ref_tree
= ref_tree
;
423 ri
->root_offset
= root_offset
;
432 if (!ri
->ogen
&& root_offset
)
433 ri
->ogen
= root_offset
;
437 memcpy(&ri
->uuid
, uuid
, BTRFS_UUID_SIZE
);
439 memcpy(&ri
->puuid
, puuid
, BTRFS_UUID_SIZE
);
441 memcpy(&ri
->ruuid
, ruuid
, BTRFS_UUID_SIZE
);
447 * add_root - update the existed root, or allocate a new root and insert it
448 * into the lookup tree.
449 * root_id: object id of the root
450 * ref_tree: object id of the referring root.
451 * root_offset: offset value of the root'key
452 * dir_id: inode id of the directory in ref_tree where this root can be found.
453 * name: the name of root_id in that directory
454 * name_len: the length of name
455 * ogen: the original generation of the root
456 * gen: the current generation of the root
457 * ot: the original time(create time) of the root
458 * uuid: uuid of the root
459 * puuid: uuid of the root parent if any
460 * ruuid: uuid of the received subvol, if any
462 static int add_root(struct root_lookup
*root_lookup
,
463 u64 root_id
, u64 ref_tree
, u64 root_offset
, u64 flags
,
464 u64 dir_id
, char *name
, int name_len
, u64 ogen
, u64 gen
,
465 time_t ot
, void *uuid
, void *puuid
, void *ruuid
)
467 struct root_info
*ri
;
470 ret
= update_root(root_lookup
, root_id
, ref_tree
, root_offset
, flags
,
471 dir_id
, name
, name_len
, ogen
, gen
, ot
,
476 ri
= calloc(1, sizeof(*ri
));
478 printf("memory allocation failed\n");
481 ri
->root_id
= root_id
;
483 if (name
&& name_len
> 0) {
484 ri
->name
= malloc(name_len
+ 1);
486 fprintf(stderr
, "memory allocation failed\n");
489 strncpy(ri
->name
, name
, name_len
);
490 ri
->name
[name_len
] = 0;
493 ri
->ref_tree
= ref_tree
;
497 ri
->root_offset
= root_offset
;
504 if (!ri
->ogen
&& root_offset
)
505 ri
->ogen
= root_offset
;
510 memcpy(&ri
->uuid
, uuid
, BTRFS_UUID_SIZE
);
513 memcpy(&ri
->puuid
, puuid
, BTRFS_UUID_SIZE
);
516 memcpy(&ri
->ruuid
, ruuid
, BTRFS_UUID_SIZE
);
518 ret
= root_tree_insert(root_lookup
, ri
);
520 printf("failed to insert tree %llu\n", (unsigned long long)root_id
);
526 static void __free_root_info(struct rb_node
*node
)
528 struct root_info
*ri
;
530 ri
= rb_entry(node
, struct root_info
, rb_node
);
537 static inline void __free_all_subvolumn(struct root_lookup
*root_tree
)
539 rb_free_nodes(&root_tree
->root
, __free_root_info
);
543 * for a given root_info, search through the root_lookup tree to construct
544 * the full path name to it.
546 * This can't be called until all the root_info->path fields are filled
547 * in by lookup_ino_path
549 static int resolve_root(struct root_lookup
*rl
, struct root_info
*ri
,
552 char *full_path
= NULL
;
554 struct root_info
*found
;
557 * we go backwards from the root_info object and add pathnames
558 * from parent directories as we go.
567 * ref_tree = 0 indicates the subvolume
570 if (!found
->ref_tree
) {
575 add_len
= strlen(found
->path
);
578 /* room for / and for null */
579 tmp
= malloc(add_len
+ 2 + len
);
581 perror("malloc failed");
584 memcpy(tmp
+ add_len
+ 1, full_path
, len
);
586 memcpy(tmp
, found
->path
, add_len
);
587 tmp
[add_len
+ len
+ 1] = '\0';
592 full_path
= strdup(found
->path
);
596 ri
->top_id
= found
->ref_tree
;
598 next
= found
->ref_tree
;
602 * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
605 if (next
== BTRFS_FS_TREE_OBJECTID
)
608 * if the ref_tree wasn't in our tree of roots, the
609 * subvolume was deleted.
611 found
= root_tree_search(rl
, next
);
618 ri
->full_path
= full_path
;
624 * for a single root_info, ask the kernel to give us a path name
625 * inside it's ref_root for the dir_id where it lives.
627 * This fills in root_info->path with the path to the directory and and
628 * appends this root's name.
630 static int lookup_ino_path(int fd
, struct root_info
*ri
)
632 struct btrfs_ioctl_ino_lookup_args args
;
641 memset(&args
, 0, sizeof(args
));
642 args
.treeid
= ri
->ref_tree
;
643 args
.objectid
= ri
->dir_id
;
645 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
647 if (errno
== ENOENT
) {
651 fprintf(stderr
, "ERROR: Failed to lookup path for root %llu - %s\n",
652 (unsigned long long)ri
->ref_tree
,
659 * we're in a subdirectory of ref_tree, the kernel ioctl
660 * puts a / in there for us
662 ri
->path
= malloc(strlen(ri
->name
) + strlen(args
.name
) + 1);
664 perror("malloc failed");
667 strcpy(ri
->path
, args
.name
);
668 strcat(ri
->path
, ri
->name
);
670 /* we're at the root of ref_tree */
671 ri
->path
= strdup(ri
->name
);
673 perror("strdup failed");
680 /* finding the generation for a given path is a two step process.
681 * First we use the inode lookup routine to find out the root id
683 * Then we use the tree search ioctl to scan all the root items for a
684 * given root id and spit out the latest generation we can find
686 static u64
find_root_gen(int fd
)
688 struct btrfs_ioctl_ino_lookup_args ino_args
;
690 struct btrfs_ioctl_search_args args
;
691 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
692 struct btrfs_ioctl_search_header sh
;
693 unsigned long off
= 0;
697 memset(&ino_args
, 0, sizeof(ino_args
));
698 ino_args
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
700 /* this ioctl fills in ino_args->treeid */
701 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
);
703 fprintf(stderr
, "ERROR: Failed to lookup path for dirid %llu - %s\n",
704 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID
,
709 memset(&args
, 0, sizeof(args
));
714 * there may be more than one ROOT_ITEM key if there are
715 * snapshots pending deletion, we have to loop through
718 sk
->min_objectid
= ino_args
.treeid
;
719 sk
->max_objectid
= ino_args
.treeid
;
720 sk
->max_type
= BTRFS_ROOT_ITEM_KEY
;
721 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
722 sk
->max_offset
= (u64
)-1;
723 sk
->max_transid
= (u64
)-1;
727 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
729 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
733 /* the ioctl returns the number of item it found in nr_items */
734 if (sk
->nr_items
== 0)
738 for (i
= 0; i
< sk
->nr_items
; i
++) {
739 struct btrfs_root_item
*item
;
741 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
743 item
= (struct btrfs_root_item
*)(args
.buf
+ off
);
746 sk
->min_objectid
= sh
.objectid
;
747 sk
->min_type
= sh
.type
;
748 sk
->min_offset
= sh
.offset
;
750 if (sh
.objectid
> ino_args
.treeid
)
753 if (sh
.objectid
== ino_args
.treeid
&&
754 sh
.type
== BTRFS_ROOT_ITEM_KEY
) {
755 max_found
= max(max_found
,
756 btrfs_root_generation(item
));
759 if (sk
->min_offset
< (u64
)-1)
764 if (sk
->min_type
!= BTRFS_ROOT_ITEM_KEY
)
766 if (sk
->min_objectid
!= ino_args
.treeid
)
772 /* pass in a directory id and this will return
773 * the full path of the parent directory inside its
776 * It may return NULL if it is in the root, or an ERR_PTR if things
779 static char *__ino_resolve(int fd
, u64 dirid
)
781 struct btrfs_ioctl_ino_lookup_args args
;
785 memset(&args
, 0, sizeof(args
));
786 args
.objectid
= dirid
;
788 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
790 fprintf(stderr
, "ERROR: Failed to lookup path for dirid %llu - %s\n",
791 (unsigned long long)dirid
, strerror(errno
));
797 * we're in a subdirectory of ref_tree, the kernel ioctl
798 * puts a / in there for us
800 full
= strdup(args
.name
);
802 perror("malloc failed");
803 return ERR_PTR(-ENOMEM
);
806 /* we're at the root of ref_tree */
813 * simple string builder, returning a new string with both
816 static char *build_name(char *dirid
, char *name
)
822 full
= malloc(strlen(dirid
) + strlen(name
) + 1);
831 * given an inode number, this returns the full path name inside the subvolume
832 * to that file/directory. cache_dirid and cache_name are used to
833 * cache the results so we can avoid tree searches if a later call goes
834 * to the same directory or file name
836 static char *ino_resolve(int fd
, u64 ino
, u64
*cache_dirid
, char **cache_name
)
844 struct btrfs_ioctl_search_args args
;
845 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
846 struct btrfs_ioctl_search_header
*sh
;
847 unsigned long off
= 0;
850 memset(&args
, 0, sizeof(args
));
855 * step one, we search for the inode back ref. We just use the first
858 sk
->min_objectid
= ino
;
859 sk
->max_objectid
= ino
;
860 sk
->max_type
= BTRFS_INODE_REF_KEY
;
861 sk
->max_offset
= (u64
)-1;
862 sk
->min_type
= BTRFS_INODE_REF_KEY
;
863 sk
->max_transid
= (u64
)-1;
866 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
868 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
872 /* the ioctl returns the number of item it found in nr_items */
873 if (sk
->nr_items
== 0)
877 sh
= (struct btrfs_ioctl_search_header
*)(args
.buf
+ off
);
879 if (btrfs_search_header_type(sh
) == BTRFS_INODE_REF_KEY
) {
880 struct btrfs_inode_ref
*ref
;
881 dirid
= btrfs_search_header_offset(sh
);
883 ref
= (struct btrfs_inode_ref
*)(sh
+ 1);
884 namelen
= btrfs_stack_inode_ref_name_len(ref
);
886 name
= (char *)(ref
+ 1);
887 name
= strndup(name
, namelen
);
889 /* use our cached value */
890 if (dirid
== *cache_dirid
&& *cache_name
) {
891 dirname
= *cache_name
;
898 * the inode backref gives us the file name and the parent directory id.
899 * From here we use __ino_resolve to get the path to the parent
901 dirname
= __ino_resolve(fd
, dirid
);
903 full
= build_name(dirname
, name
);
904 if (*cache_name
&& dirname
!= *cache_name
)
907 *cache_name
= dirname
;
908 *cache_dirid
= dirid
;
914 int btrfs_list_get_default_subvolume(int fd
, u64
*default_id
)
916 struct btrfs_ioctl_search_args args
;
917 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
918 struct btrfs_ioctl_search_header
*sh
;
922 memset(&args
, 0, sizeof(args
));
925 * search for a dir item with a name 'default' in the tree of
926 * tree roots, it should point us to a default root
930 /* don't worry about ancient format and request only one item */
933 sk
->max_objectid
= BTRFS_ROOT_TREE_DIR_OBJECTID
;
934 sk
->min_objectid
= BTRFS_ROOT_TREE_DIR_OBJECTID
;
935 sk
->max_type
= BTRFS_DIR_ITEM_KEY
;
936 sk
->min_type
= BTRFS_DIR_ITEM_KEY
;
937 sk
->max_offset
= (u64
)-1;
938 sk
->max_transid
= (u64
)-1;
940 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
944 /* the ioctl returns the number of items it found in nr_items */
945 if (sk
->nr_items
== 0)
948 sh
= (struct btrfs_ioctl_search_header
*)args
.buf
;
950 if (btrfs_search_header_type(sh
) == BTRFS_DIR_ITEM_KEY
) {
951 struct btrfs_dir_item
*di
;
955 di
= (struct btrfs_dir_item
*)(sh
+ 1);
956 name_len
= btrfs_stack_dir_name_len(di
);
957 name
= (char *)(di
+ 1);
959 if (!strncmp("default", name
, name_len
))
960 found
= btrfs_disk_key_objectid(&di
->location
);
968 static int __list_subvol_search(int fd
, struct root_lookup
*root_lookup
)
971 struct btrfs_ioctl_search_args args
;
972 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
973 struct btrfs_ioctl_search_header sh
;
974 struct btrfs_root_ref
*ref
;
975 struct btrfs_root_item
*ri
;
976 unsigned long off
= 0;
985 u8 uuid
[BTRFS_UUID_SIZE
];
986 u8 puuid
[BTRFS_UUID_SIZE
];
987 u8 ruuid
[BTRFS_UUID_SIZE
];
989 root_lookup_init(root_lookup
);
990 memset(&args
, 0, sizeof(args
));
992 /* search in the tree of tree roots */
996 * set the min and max to backref keys. The search will
997 * only send back this type of key now.
999 sk
->max_type
= BTRFS_ROOT_BACKREF_KEY
;
1000 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
1002 sk
->min_objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1005 * set all the other params to the max, we'll take any objectid
1008 sk
->max_objectid
= BTRFS_LAST_FREE_OBJECTID
;
1009 sk
->max_offset
= (u64
)-1;
1010 sk
->max_transid
= (u64
)-1;
1012 /* just a big number, doesn't matter much */
1013 sk
->nr_items
= 4096;
1016 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
1019 /* the ioctl returns the number of item it found in nr_items */
1020 if (sk
->nr_items
== 0)
1026 * for each item, pull the key out of the header and then
1027 * read the root_ref item it contains
1029 for (i
= 0; i
< sk
->nr_items
; i
++) {
1030 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
1032 if (sh
.type
== BTRFS_ROOT_BACKREF_KEY
) {
1033 ref
= (struct btrfs_root_ref
*)(args
.buf
+ off
);
1034 name_len
= btrfs_stack_root_ref_name_len(ref
);
1035 name
= (char *)(ref
+ 1);
1036 dir_id
= btrfs_stack_root_ref_dirid(ref
);
1038 add_root(root_lookup
, sh
.objectid
, sh
.offset
,
1039 0, 0, dir_id
, name
, name_len
, 0, 0, 0,
1041 } else if (sh
.type
== BTRFS_ROOT_ITEM_KEY
) {
1042 ri
= (struct btrfs_root_item
*)(args
.buf
+ off
);
1043 gen
= btrfs_root_generation(ri
);
1044 flags
= btrfs_root_flags(ri
);
1046 sizeof(struct btrfs_root_item_v0
)) {
1047 t
= btrfs_stack_timespec_sec(&ri
->otime
);
1048 ogen
= btrfs_root_otransid(ri
);
1049 memcpy(uuid
, ri
->uuid
, BTRFS_UUID_SIZE
);
1050 memcpy(puuid
, ri
->parent_uuid
, BTRFS_UUID_SIZE
);
1051 memcpy(ruuid
, ri
->received_uuid
, BTRFS_UUID_SIZE
);
1055 memset(uuid
, 0, BTRFS_UUID_SIZE
);
1056 memset(puuid
, 0, BTRFS_UUID_SIZE
);
1057 memset(ruuid
, 0, BTRFS_UUID_SIZE
);
1060 add_root(root_lookup
, sh
.objectid
, 0,
1061 sh
.offset
, flags
, 0, NULL
, 0, ogen
,
1062 gen
, t
, uuid
, puuid
, ruuid
);
1068 * record the mins in sk so we can make sure the
1069 * next search doesn't repeat this root
1071 sk
->min_objectid
= sh
.objectid
;
1072 sk
->min_type
= sh
.type
;
1073 sk
->min_offset
= sh
.offset
;
1075 sk
->nr_items
= 4096;
1077 if (!sk
->min_offset
) /* overflow */
1082 if (sk
->min_type
> BTRFS_ROOT_BACKREF_KEY
) {
1083 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
1088 if (sk
->min_objectid
> sk
->max_objectid
)
1095 static int filter_by_rootid(struct root_info
*ri
, u64 data
)
1097 return ri
->root_id
== data
;
1100 static int filter_snapshot(struct root_info
*ri
, u64 data
)
1102 return !!ri
->root_offset
;
1105 static int filter_flags(struct root_info
*ri
, u64 flags
)
1107 return ri
->flags
& flags
;
1110 static int filter_gen_more(struct root_info
*ri
, u64 data
)
1112 return ri
->gen
>= data
;
1115 static int filter_gen_less(struct root_info
*ri
, u64 data
)
1117 return ri
->gen
<= data
;
1120 static int filter_gen_equal(struct root_info
*ri
, u64 data
)
1122 return ri
->gen
== data
;
1125 static int filter_cgen_more(struct root_info
*ri
, u64 data
)
1127 return ri
->ogen
>= data
;
1130 static int filter_cgen_less(struct root_info
*ri
, u64 data
)
1132 return ri
->ogen
<= data
;
1135 static int filter_cgen_equal(struct root_info
*ri
, u64 data
)
1137 return ri
->ogen
== data
;
1140 static int filter_topid_equal(struct root_info
*ri
, u64 data
)
1142 return ri
->top_id
== data
;
1145 static int filter_full_path(struct root_info
*ri
, u64 data
)
1147 if (ri
->full_path
&& ri
->top_id
!= data
) {
1149 char p
[] = "<FS_TREE>";
1150 int add_len
= strlen(p
);
1151 int len
= strlen(ri
->full_path
);
1153 tmp
= malloc(len
+ add_len
+ 2);
1155 fprintf(stderr
, "memory allocation failed\n");
1158 memcpy(tmp
+ add_len
+ 1, ri
->full_path
, len
);
1159 tmp
[len
+ add_len
+ 1] = '\0';
1161 memcpy(tmp
, p
, add_len
);
1162 free(ri
->full_path
);
1163 ri
->full_path
= tmp
;
1168 static int filter_by_parent(struct root_info
*ri
, u64 data
)
1170 return !uuid_compare(ri
->puuid
, (u8
*)(unsigned long)data
);
1173 static int filter_deleted(struct root_info
*ri
, u64 data
)
1178 static btrfs_list_filter_func all_filter_funcs
[] = {
1179 [BTRFS_LIST_FILTER_ROOTID
] = filter_by_rootid
,
1180 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY
] = filter_snapshot
,
1181 [BTRFS_LIST_FILTER_FLAGS
] = filter_flags
,
1182 [BTRFS_LIST_FILTER_GEN_MORE
] = filter_gen_more
,
1183 [BTRFS_LIST_FILTER_GEN_LESS
] = filter_gen_less
,
1184 [BTRFS_LIST_FILTER_GEN_EQUAL
] = filter_gen_equal
,
1185 [BTRFS_LIST_FILTER_CGEN_MORE
] = filter_cgen_more
,
1186 [BTRFS_LIST_FILTER_CGEN_LESS
] = filter_cgen_less
,
1187 [BTRFS_LIST_FILTER_CGEN_EQUAL
] = filter_cgen_equal
,
1188 [BTRFS_LIST_FILTER_TOPID_EQUAL
] = filter_topid_equal
,
1189 [BTRFS_LIST_FILTER_FULL_PATH
] = filter_full_path
,
1190 [BTRFS_LIST_FILTER_BY_PARENT
] = filter_by_parent
,
1191 [BTRFS_LIST_FILTER_DELETED
] = filter_deleted
,
1194 struct btrfs_list_filter_set
*btrfs_list_alloc_filter_set(void)
1196 struct btrfs_list_filter_set
*set
;
1199 size
= sizeof(struct btrfs_list_filter_set
) +
1200 BTRFS_LIST_NFILTERS_INCREASE
* sizeof(struct btrfs_list_filter
);
1201 set
= calloc(1, size
);
1203 fprintf(stderr
, "memory allocation failed\n");
1207 set
->total
= BTRFS_LIST_NFILTERS_INCREASE
;
1212 void btrfs_list_free_filter_set(struct btrfs_list_filter_set
*filter_set
)
1217 int btrfs_list_setup_filter(struct btrfs_list_filter_set
**filter_set
,
1218 enum btrfs_list_filter_enum filter
, u64 data
)
1220 struct btrfs_list_filter_set
*set
= *filter_set
;
1224 BUG_ON(filter
>= BTRFS_LIST_FILTER_MAX
);
1225 BUG_ON(set
->nfilters
> set
->total
);
1227 if (set
->nfilters
== set
->total
) {
1230 size
= set
->total
+ BTRFS_LIST_NFILTERS_INCREASE
;
1231 size
= sizeof(*set
) + size
* sizeof(struct btrfs_list_filter
);
1233 set
= realloc(set
, size
);
1235 fprintf(stderr
, "memory allocation failed\n");
1240 memset(&set
->filters
[set
->total
], 0,
1241 BTRFS_LIST_NFILTERS_INCREASE
*
1242 sizeof(struct btrfs_list_filter
));
1243 set
->total
+= BTRFS_LIST_NFILTERS_INCREASE
;
1247 BUG_ON(set
->filters
[set
->nfilters
].filter_func
);
1249 if (filter
== BTRFS_LIST_FILTER_DELETED
)
1250 set
->only_deleted
= 1;
1252 set
->filters
[set
->nfilters
].filter_func
= all_filter_funcs
[filter
];
1253 set
->filters
[set
->nfilters
].data
= data
;
1258 static int filter_root(struct root_info
*ri
,
1259 struct btrfs_list_filter_set
*set
)
1266 if (set
->only_deleted
&& !ri
->deleted
)
1269 if (!set
->only_deleted
&& ri
->deleted
)
1272 for (i
= 0; i
< set
->nfilters
; i
++) {
1273 if (!set
->filters
[i
].filter_func
)
1275 ret
= set
->filters
[i
].filter_func(ri
, set
->filters
[i
].data
);
1282 static void __filter_and_sort_subvol(struct root_lookup
*all_subvols
,
1283 struct root_lookup
*sort_tree
,
1284 struct btrfs_list_filter_set
*filter_set
,
1285 struct btrfs_list_comparer_set
*comp_set
,
1289 struct root_info
*entry
;
1292 root_lookup_init(sort_tree
);
1294 n
= rb_last(&all_subvols
->root
);
1296 entry
= rb_entry(n
, struct root_info
, rb_node
);
1298 ret
= resolve_root(all_subvols
, entry
, top_id
);
1299 if (ret
== -ENOENT
) {
1300 entry
->full_path
= strdup("DELETED");
1303 ret
= filter_root(entry
, filter_set
);
1305 sort_tree_insert(sort_tree
, entry
, comp_set
);
1310 static int __list_subvol_fill_paths(int fd
, struct root_lookup
*root_lookup
)
1314 n
= rb_first(&root_lookup
->root
);
1316 struct root_info
*entry
;
1318 entry
= rb_entry(n
, struct root_info
, rb_node
);
1319 ret
= lookup_ino_path(fd
, entry
);
1320 if (ret
&& ret
!= -ENOENT
)
1328 static void print_subvolume_column(struct root_info
*subv
,
1329 enum btrfs_list_column_enum column
)
1332 char uuidparse
[BTRFS_UUID_UNPARSED_SIZE
];
1334 BUG_ON(column
>= BTRFS_LIST_ALL
|| column
< 0);
1337 case BTRFS_LIST_OBJECTID
:
1338 printf("%llu", subv
->root_id
);
1340 case BTRFS_LIST_GENERATION
:
1341 printf("%llu", subv
->gen
);
1343 case BTRFS_LIST_OGENERATION
:
1344 printf("%llu", subv
->ogen
);
1346 case BTRFS_LIST_PARENT
:
1347 printf("%llu", subv
->ref_tree
);
1349 case BTRFS_LIST_TOP_LEVEL
:
1350 printf("%llu", subv
->top_id
);
1352 case BTRFS_LIST_OTIME
:
1356 localtime_r(&subv
->otime
, &tm
);
1357 strftime(tstr
, 256, "%Y-%m-%d %X", &tm
);
1362 case BTRFS_LIST_UUID
:
1363 if (uuid_is_null(subv
->uuid
))
1364 strcpy(uuidparse
, "-");
1366 uuid_unparse(subv
->uuid
, uuidparse
);
1367 printf("%s", uuidparse
);
1369 case BTRFS_LIST_PUUID
:
1370 if (uuid_is_null(subv
->puuid
))
1371 strcpy(uuidparse
, "-");
1373 uuid_unparse(subv
->puuid
, uuidparse
);
1374 printf("%s", uuidparse
);
1376 case BTRFS_LIST_RUUID
:
1377 if (uuid_is_null(subv
->ruuid
))
1378 strcpy(uuidparse
, "-");
1380 uuid_unparse(subv
->ruuid
, uuidparse
);
1381 printf("%s", uuidparse
);
1383 case BTRFS_LIST_PATH
:
1384 BUG_ON(!subv
->full_path
);
1385 printf("%s", subv
->full_path
);
1392 static void print_single_volume_info_raw(struct root_info
*subv
, char *raw_prefix
)
1396 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1397 if (!btrfs_list_columns
[i
].need_print
)
1401 printf("%s",raw_prefix
);
1403 print_subvolume_column(subv
, i
);
1408 static void print_single_volume_info_table(struct root_info
*subv
)
1412 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1413 if (!btrfs_list_columns
[i
].need_print
)
1416 print_subvolume_column(subv
, i
);
1418 if (i
!= BTRFS_LIST_PATH
)
1421 if (i
== BTRFS_LIST_TOP_LEVEL
)
1427 static void print_single_volume_info_default(struct root_info
*subv
)
1431 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1432 if (!btrfs_list_columns
[i
].need_print
)
1435 printf("%s ", btrfs_list_columns
[i
].name
);
1436 print_subvolume_column(subv
, i
);
1438 if (i
!= BTRFS_LIST_PATH
)
1444 static void print_all_volume_info_tab_head(void)
1450 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1451 if (btrfs_list_columns
[i
].need_print
)
1452 printf("%s\t", btrfs_list_columns
[i
].name
);
1454 if (i
== BTRFS_LIST_ALL
-1)
1458 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1459 memset(barrier
, 0, sizeof(barrier
));
1461 if (btrfs_list_columns
[i
].need_print
) {
1462 len
= strlen(btrfs_list_columns
[i
].name
);
1464 strcat(barrier
, "-");
1466 printf("%s\t", barrier
);
1468 if (i
== BTRFS_LIST_ALL
-1)
1473 static void print_all_volume_info(struct root_lookup
*sorted_tree
,
1474 int layout
, char *raw_prefix
)
1477 struct root_info
*entry
;
1479 if (layout
== BTRFS_LIST_LAYOUT_TABLE
)
1480 print_all_volume_info_tab_head();
1482 n
= rb_first(&sorted_tree
->root
);
1484 entry
= rb_entry(n
, struct root_info
, sort_node
);
1486 case BTRFS_LIST_LAYOUT_DEFAULT
:
1487 print_single_volume_info_default(entry
);
1489 case BTRFS_LIST_LAYOUT_TABLE
:
1490 print_single_volume_info_table(entry
);
1492 case BTRFS_LIST_LAYOUT_RAW
:
1493 print_single_volume_info_raw(entry
, raw_prefix
);
1500 static int btrfs_list_subvols(int fd
, struct root_lookup
*root_lookup
)
1504 ret
= __list_subvol_search(fd
, root_lookup
);
1506 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
1512 * now we have an rbtree full of root_info objects, but we need to fill
1513 * in their path names within the subvol that is referencing each one.
1515 ret
= __list_subvol_fill_paths(fd
, root_lookup
);
1519 int btrfs_list_subvols_print(int fd
, struct btrfs_list_filter_set
*filter_set
,
1520 struct btrfs_list_comparer_set
*comp_set
,
1521 int layout
, int full_path
, char *raw_prefix
)
1523 struct root_lookup root_lookup
;
1524 struct root_lookup root_sort
;
1529 ret
= btrfs_list_get_path_rootid(fd
, &top_id
);
1533 ret
= btrfs_list_subvols(fd
, &root_lookup
);
1536 __filter_and_sort_subvol(&root_lookup
, &root_sort
, filter_set
,
1539 print_all_volume_info(&root_sort
, layout
, raw_prefix
);
1540 __free_all_subvolumn(&root_lookup
);
1545 static char *strdup_or_null(const char *s
)
1552 int btrfs_get_subvol(int fd
, struct root_info
*the_ri
)
1555 struct root_lookup rl
;
1556 struct rb_node
*rbn
;
1557 struct root_info
*ri
;
1560 ret
= btrfs_list_get_path_rootid(fd
, &root_id
);
1564 ret
= btrfs_list_subvols(fd
, &rl
);
1568 rbn
= rb_first(&rl
.root
);
1570 ri
= rb_entry(rbn
, struct root_info
, rb_node
);
1571 rr
= resolve_root(&rl
, ri
, root_id
);
1572 if (rr
== -ENOENT
) {
1577 if (!comp_entry_with_rootid(the_ri
, ri
, 0)) {
1578 memcpy(the_ri
, ri
, offsetof(struct root_info
, path
));
1579 the_ri
->path
= strdup_or_null(ri
->path
);
1580 the_ri
->name
= strdup_or_null(ri
->name
);
1581 the_ri
->full_path
= strdup_or_null(ri
->full_path
);
1587 __free_all_subvolumn(&rl
);
1591 static int print_one_extent(int fd
, struct btrfs_ioctl_search_header
*sh
,
1592 struct btrfs_file_extent_item
*item
,
1593 u64 found_gen
, u64
*cache_dirid
,
1594 char **cache_dir_name
, u64
*cache_ino
,
1595 char **cache_full_name
)
1599 u64 disk_offset
= 0;
1605 if (btrfs_search_header_objectid(sh
) == *cache_ino
) {
1606 name
= *cache_full_name
;
1607 } else if (*cache_full_name
) {
1608 free(*cache_full_name
);
1609 *cache_full_name
= NULL
;
1612 name
= ino_resolve(fd
, btrfs_search_header_objectid(sh
),
1615 *cache_full_name
= name
;
1616 *cache_ino
= btrfs_search_header_objectid(sh
);
1621 type
= btrfs_stack_file_extent_type(item
);
1622 compressed
= btrfs_stack_file_extent_compression(item
);
1624 if (type
== BTRFS_FILE_EXTENT_REG
||
1625 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1626 disk_start
= btrfs_stack_file_extent_disk_bytenr(item
);
1627 disk_offset
= btrfs_stack_file_extent_offset(item
);
1628 len
= btrfs_stack_file_extent_num_bytes(item
);
1629 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1632 len
= btrfs_stack_file_extent_ram_bytes(item
);
1634 printf("unhandled extent type %d for inode %llu "
1635 "file offset %llu gen %llu\n",
1637 (unsigned long long)btrfs_search_header_objectid(sh
),
1638 (unsigned long long)btrfs_search_header_offset(sh
),
1639 (unsigned long long)found_gen
);
1643 printf("inode %llu file offset %llu len %llu disk start %llu "
1644 "offset %llu gen %llu flags ",
1645 (unsigned long long)btrfs_search_header_objectid(sh
),
1646 (unsigned long long)btrfs_search_header_offset(sh
),
1647 (unsigned long long)len
,
1648 (unsigned long long)disk_start
,
1649 (unsigned long long)disk_offset
,
1650 (unsigned long long)found_gen
);
1656 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1657 printf("%sPREALLOC", flags
? "|" : "");
1660 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1661 printf("%sINLINE", flags
? "|" : "");
1667 printf(" %s\n", name
);
1671 int btrfs_list_find_updated_files(int fd
, u64 root_id
, u64 oldest_gen
)
1674 struct btrfs_ioctl_search_args args
;
1675 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
1676 struct btrfs_ioctl_search_header sh
;
1677 struct btrfs_file_extent_item
*item
;
1678 unsigned long off
= 0;
1682 u64 cache_dirid
= 0;
1684 char *cache_dir_name
= NULL
;
1685 char *cache_full_name
= NULL
;
1686 struct btrfs_file_extent_item backup
;
1688 memset(&backup
, 0, sizeof(backup
));
1689 memset(&args
, 0, sizeof(args
));
1691 sk
->tree_id
= root_id
;
1694 * set all the other params to the max, we'll take any objectid
1697 sk
->max_objectid
= (u64
)-1;
1698 sk
->max_offset
= (u64
)-1;
1699 sk
->max_transid
= (u64
)-1;
1700 sk
->max_type
= BTRFS_EXTENT_DATA_KEY
;
1701 sk
->min_transid
= oldest_gen
;
1702 /* just a big number, doesn't matter much */
1703 sk
->nr_items
= 4096;
1705 max_found
= find_root_gen(fd
);
1707 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
1709 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
1713 /* the ioctl returns the number of item it found in nr_items */
1714 if (sk
->nr_items
== 0)
1720 * for each item, pull the key out of the header and then
1721 * read the root_ref item it contains
1723 for (i
= 0; i
< sk
->nr_items
; i
++) {
1724 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
1728 * just in case the item was too big, pass something other
1734 item
= (struct btrfs_file_extent_item
*)(args
.buf
+
1736 found_gen
= btrfs_stack_file_extent_generation(item
);
1737 if (sh
.type
== BTRFS_EXTENT_DATA_KEY
&&
1738 found_gen
>= oldest_gen
) {
1739 print_one_extent(fd
, &sh
, item
, found_gen
,
1740 &cache_dirid
, &cache_dir_name
,
1741 &cache_ino
, &cache_full_name
);
1746 * record the mins in sk so we can make sure the
1747 * next search doesn't repeat this root
1749 sk
->min_objectid
= sh
.objectid
;
1750 sk
->min_offset
= sh
.offset
;
1751 sk
->min_type
= sh
.type
;
1753 sk
->nr_items
= 4096;
1754 if (sk
->min_offset
< (u64
)-1)
1756 else if (sk
->min_objectid
< (u64
)-1) {
1763 free(cache_dir_name
);
1764 free(cache_full_name
);
1765 printf("transid marker was %llu\n", (unsigned long long)max_found
);
1769 char *btrfs_list_path_for_root(int fd
, u64 root
)
1771 struct root_lookup root_lookup
;
1773 char *ret_path
= NULL
;
1777 ret
= btrfs_list_get_path_rootid(fd
, &top_id
);
1779 return ERR_PTR(ret
);
1781 ret
= __list_subvol_search(fd
, &root_lookup
);
1783 return ERR_PTR(ret
);
1785 ret
= __list_subvol_fill_paths(fd
, &root_lookup
);
1787 return ERR_PTR(ret
);
1789 n
= rb_last(&root_lookup
.root
);
1791 struct root_info
*entry
;
1793 entry
= rb_entry(n
, struct root_info
, rb_node
);
1794 ret
= resolve_root(&root_lookup
, entry
, top_id
);
1795 if (ret
== -ENOENT
&& entry
->root_id
== root
) {
1799 if (entry
->root_id
== root
) {
1800 ret_path
= entry
->full_path
;
1801 entry
->full_path
= NULL
;
1806 __free_all_subvolumn(&root_lookup
);
1811 int btrfs_list_parse_sort_string(char *opt_arg
,
1812 struct btrfs_list_comparer_set
**comps
)
1820 while ((p
= strtok(opt_arg
, ",")) != NULL
) {
1822 ptr_argv
= all_sort_items
;
1825 if (strcmp(*ptr_argv
, p
) == 0) {
1830 if (strcmp(*ptr_argv
, p
) == 0) {
1847 } else if (*p
== '-') {
1853 what_to_sort
= btrfs_list_get_sort_item(p
);
1854 btrfs_list_setup_comparer(comps
, what_to_sort
, order
);
1863 * This function is used to parse the argument of filter condition.
1865 * type is the filter object.
1867 int btrfs_list_parse_filter_string(char *opt_arg
,
1868 struct btrfs_list_filter_set
**filters
,
1869 enum btrfs_list_filter_enum type
)
1874 switch (*(opt_arg
++)) {
1876 arg
= arg_strtou64(opt_arg
);
1879 btrfs_list_setup_filter(filters
, type
, arg
);
1882 arg
= arg_strtou64(opt_arg
);
1885 btrfs_list_setup_filter(filters
, type
, arg
);
1889 arg
= arg_strtou64(opt_arg
);
1891 btrfs_list_setup_filter(filters
, type
, arg
);
1898 int btrfs_list_get_path_rootid(int fd
, u64
*treeid
)
1901 struct btrfs_ioctl_ino_lookup_args args
;
1903 memset(&args
, 0, sizeof(args
));
1904 args
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1906 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
1909 "ERROR: can't perform the search - %s\n",
1913 *treeid
= args
.treeid
;