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 subvolumes
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
);
652 fprintf(stderr
, "ERROR: Failed to lookup path for root %llu - %s\n",
653 (unsigned long long)ri
->ref_tree
,
660 * we're in a subdirectory of ref_tree, the kernel ioctl
661 * puts a / in there for us
663 ri
->path
= malloc(strlen(ri
->name
) + strlen(args
.name
) + 1);
665 perror("malloc failed");
668 strcpy(ri
->path
, args
.name
);
669 strcat(ri
->path
, ri
->name
);
671 /* we're at the root of ref_tree */
672 ri
->path
= strdup(ri
->name
);
674 perror("strdup failed");
681 /* finding the generation for a given path is a two step process.
682 * First we use the inode loookup routine to find out the root id
684 * Then we use the tree search ioctl to scan all the root items for a
685 * given root id and spit out the latest generation we can find
687 static u64
find_root_gen(int fd
)
689 struct btrfs_ioctl_ino_lookup_args ino_args
;
691 struct btrfs_ioctl_search_args args
;
692 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
693 struct btrfs_ioctl_search_header sh
;
694 unsigned long off
= 0;
699 memset(&ino_args
, 0, sizeof(ino_args
));
700 ino_args
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
702 /* this ioctl fills in ino_args->treeid */
703 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
);
706 fprintf(stderr
, "ERROR: Failed to lookup path for dirid %llu - %s\n",
707 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID
,
712 memset(&args
, 0, sizeof(args
));
717 * there may be more than one ROOT_ITEM key if there are
718 * snapshots pending deletion, we have to loop through
721 sk
->min_objectid
= ino_args
.treeid
;
722 sk
->max_objectid
= ino_args
.treeid
;
723 sk
->max_type
= BTRFS_ROOT_ITEM_KEY
;
724 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
725 sk
->max_offset
= (u64
)-1;
726 sk
->max_transid
= (u64
)-1;
730 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
733 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
737 /* the ioctl returns the number of item it found in nr_items */
738 if (sk
->nr_items
== 0)
742 for (i
= 0; i
< sk
->nr_items
; i
++) {
743 struct btrfs_root_item
*item
;
745 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
747 item
= (struct btrfs_root_item
*)(args
.buf
+ off
);
750 sk
->min_objectid
= sh
.objectid
;
751 sk
->min_type
= sh
.type
;
752 sk
->min_offset
= sh
.offset
;
754 if (sh
.objectid
> ino_args
.treeid
)
757 if (sh
.objectid
== ino_args
.treeid
&&
758 sh
.type
== BTRFS_ROOT_ITEM_KEY
) {
759 max_found
= max(max_found
,
760 btrfs_root_generation(item
));
763 if (sk
->min_offset
< (u64
)-1)
768 if (sk
->min_type
!= BTRFS_ROOT_ITEM_KEY
)
770 if (sk
->min_objectid
!= ino_args
.treeid
)
776 /* pass in a directory id and this will return
777 * the full path of the parent directory inside its
780 * It may return NULL if it is in the root, or an ERR_PTR if things
783 static char *__ino_resolve(int fd
, u64 dirid
)
785 struct btrfs_ioctl_ino_lookup_args args
;
790 memset(&args
, 0, sizeof(args
));
791 args
.objectid
= dirid
;
793 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
796 fprintf(stderr
, "ERROR: Failed to lookup path for dirid %llu - %s\n",
797 (unsigned long long)dirid
, strerror(e
) );
803 * we're in a subdirectory of ref_tree, the kernel ioctl
804 * puts a / in there for us
806 full
= strdup(args
.name
);
808 perror("malloc failed");
809 return ERR_PTR(-ENOMEM
);
812 /* we're at the root of ref_tree */
819 * simple string builder, returning a new string with both
822 static char *build_name(char *dirid
, char *name
)
828 full
= malloc(strlen(dirid
) + strlen(name
) + 1);
837 * given an inode number, this returns the full path name inside the subvolume
838 * to that file/directory. cache_dirid and cache_name are used to
839 * cache the results so we can avoid tree searches if a later call goes
840 * to the same directory or file name
842 static char *ino_resolve(int fd
, u64 ino
, u64
*cache_dirid
, char **cache_name
)
850 struct btrfs_ioctl_search_args args
;
851 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
852 struct btrfs_ioctl_search_header
*sh
;
853 unsigned long off
= 0;
857 memset(&args
, 0, sizeof(args
));
862 * step one, we search for the inode back ref. We just use the first
865 sk
->min_objectid
= ino
;
866 sk
->max_objectid
= ino
;
867 sk
->max_type
= BTRFS_INODE_REF_KEY
;
868 sk
->max_offset
= (u64
)-1;
869 sk
->min_type
= BTRFS_INODE_REF_KEY
;
870 sk
->max_transid
= (u64
)-1;
873 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
876 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
880 /* the ioctl returns the number of item it found in nr_items */
881 if (sk
->nr_items
== 0)
885 sh
= (struct btrfs_ioctl_search_header
*)(args
.buf
+ off
);
887 if (sh
->type
== BTRFS_INODE_REF_KEY
) {
888 struct btrfs_inode_ref
*ref
;
891 ref
= (struct btrfs_inode_ref
*)(sh
+ 1);
892 namelen
= btrfs_stack_inode_ref_name_len(ref
);
894 name
= (char *)(ref
+ 1);
895 name
= strndup(name
, namelen
);
897 /* use our cached value */
898 if (dirid
== *cache_dirid
&& *cache_name
) {
899 dirname
= *cache_name
;
906 * the inode backref gives us the file name and the parent directory id.
907 * From here we use __ino_resolve to get the path to the parent
909 dirname
= __ino_resolve(fd
, dirid
);
911 full
= build_name(dirname
, name
);
912 if (*cache_name
&& dirname
!= *cache_name
)
915 *cache_name
= dirname
;
916 *cache_dirid
= dirid
;
922 int btrfs_list_get_default_subvolume(int fd
, u64
*default_id
)
924 struct btrfs_ioctl_search_args args
;
925 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
926 struct btrfs_ioctl_search_header
*sh
;
930 memset(&args
, 0, sizeof(args
));
933 * search for a dir item with a name 'default' in the tree of
934 * tree roots, it should point us to a default root
938 /* don't worry about ancient format and request only one item */
941 sk
->max_objectid
= BTRFS_ROOT_TREE_DIR_OBJECTID
;
942 sk
->min_objectid
= BTRFS_ROOT_TREE_DIR_OBJECTID
;
943 sk
->max_type
= BTRFS_DIR_ITEM_KEY
;
944 sk
->min_type
= BTRFS_DIR_ITEM_KEY
;
945 sk
->max_offset
= (u64
)-1;
946 sk
->max_transid
= (u64
)-1;
948 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
952 /* the ioctl returns the number of items it found in nr_items */
953 if (sk
->nr_items
== 0)
956 sh
= (struct btrfs_ioctl_search_header
*)args
.buf
;
958 if (sh
->type
== BTRFS_DIR_ITEM_KEY
) {
959 struct btrfs_dir_item
*di
;
963 di
= (struct btrfs_dir_item
*)(sh
+ 1);
964 name_len
= btrfs_stack_dir_name_len(di
);
965 name
= (char *)(di
+ 1);
967 if (!strncmp("default", name
, name_len
))
968 found
= btrfs_disk_key_objectid(&di
->location
);
976 static int __list_subvol_search(int fd
, struct root_lookup
*root_lookup
)
979 struct btrfs_ioctl_search_args args
;
980 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
981 struct btrfs_ioctl_search_header sh
;
982 struct btrfs_root_ref
*ref
;
983 struct btrfs_root_item
*ri
;
984 unsigned long off
= 0;
993 u8 uuid
[BTRFS_UUID_SIZE
];
994 u8 puuid
[BTRFS_UUID_SIZE
];
995 u8 ruuid
[BTRFS_UUID_SIZE
];
997 root_lookup_init(root_lookup
);
998 memset(&args
, 0, sizeof(args
));
1000 /* search in the tree of tree roots */
1004 * set the min and max to backref keys. The search will
1005 * only send back this type of key now.
1007 sk
->max_type
= BTRFS_ROOT_BACKREF_KEY
;
1008 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
1010 sk
->min_objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1013 * set all the other params to the max, we'll take any objectid
1016 sk
->max_objectid
= BTRFS_LAST_FREE_OBJECTID
;
1017 sk
->max_offset
= (u64
)-1;
1018 sk
->max_transid
= (u64
)-1;
1020 /* just a big number, doesn't matter much */
1021 sk
->nr_items
= 4096;
1024 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
1027 /* the ioctl returns the number of item it found in nr_items */
1028 if (sk
->nr_items
== 0)
1034 * for each item, pull the key out of the header and then
1035 * read the root_ref item it contains
1037 for (i
= 0; i
< sk
->nr_items
; i
++) {
1038 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
1040 if (sh
.type
== BTRFS_ROOT_BACKREF_KEY
) {
1041 ref
= (struct btrfs_root_ref
*)(args
.buf
+ off
);
1042 name_len
= btrfs_stack_root_ref_name_len(ref
);
1043 name
= (char *)(ref
+ 1);
1044 dir_id
= btrfs_stack_root_ref_dirid(ref
);
1046 add_root(root_lookup
, sh
.objectid
, sh
.offset
,
1047 0, 0, dir_id
, name
, name_len
, 0, 0, 0,
1049 } else if (sh
.type
== BTRFS_ROOT_ITEM_KEY
) {
1050 ri
= (struct btrfs_root_item
*)(args
.buf
+ off
);
1051 gen
= btrfs_root_generation(ri
);
1052 flags
= btrfs_root_flags(ri
);
1054 sizeof(struct btrfs_root_item_v0
)) {
1055 t
= btrfs_stack_timespec_sec(&ri
->otime
);
1056 ogen
= btrfs_root_otransid(ri
);
1057 memcpy(uuid
, ri
->uuid
, BTRFS_UUID_SIZE
);
1058 memcpy(puuid
, ri
->parent_uuid
, BTRFS_UUID_SIZE
);
1059 memcpy(ruuid
, ri
->received_uuid
, BTRFS_UUID_SIZE
);
1063 memset(uuid
, 0, BTRFS_UUID_SIZE
);
1064 memset(puuid
, 0, BTRFS_UUID_SIZE
);
1065 memset(ruuid
, 0, BTRFS_UUID_SIZE
);
1068 add_root(root_lookup
, sh
.objectid
, 0,
1069 sh
.offset
, flags
, 0, NULL
, 0, ogen
,
1070 gen
, t
, uuid
, puuid
, ruuid
);
1076 * record the mins in sk so we can make sure the
1077 * next search doesn't repeat this root
1079 sk
->min_objectid
= sh
.objectid
;
1080 sk
->min_type
= sh
.type
;
1081 sk
->min_offset
= sh
.offset
;
1083 sk
->nr_items
= 4096;
1085 if (!sk
->min_offset
) /* overflow */
1090 if (sk
->min_type
> BTRFS_ROOT_BACKREF_KEY
) {
1091 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
1096 if (sk
->min_objectid
> sk
->max_objectid
)
1103 static int filter_by_rootid(struct root_info
*ri
, u64 data
)
1105 return ri
->root_id
== data
;
1108 static int filter_snapshot(struct root_info
*ri
, u64 data
)
1110 return !!ri
->root_offset
;
1113 static int filter_flags(struct root_info
*ri
, u64 flags
)
1115 return ri
->flags
& flags
;
1118 static int filter_gen_more(struct root_info
*ri
, u64 data
)
1120 return ri
->gen
>= data
;
1123 static int filter_gen_less(struct root_info
*ri
, u64 data
)
1125 return ri
->gen
<= data
;
1128 static int filter_gen_equal(struct root_info
*ri
, u64 data
)
1130 return ri
->gen
== data
;
1133 static int filter_cgen_more(struct root_info
*ri
, u64 data
)
1135 return ri
->ogen
>= data
;
1138 static int filter_cgen_less(struct root_info
*ri
, u64 data
)
1140 return ri
->ogen
<= data
;
1143 static int filter_cgen_equal(struct root_info
*ri
, u64 data
)
1145 return ri
->ogen
== data
;
1148 static int filter_topid_equal(struct root_info
*ri
, u64 data
)
1150 return ri
->top_id
== data
;
1153 static int filter_full_path(struct root_info
*ri
, u64 data
)
1155 if (ri
->full_path
&& ri
->top_id
!= data
) {
1157 char p
[] = "<FS_TREE>";
1158 int add_len
= strlen(p
);
1159 int len
= strlen(ri
->full_path
);
1161 tmp
= malloc(len
+ add_len
+ 2);
1163 fprintf(stderr
, "memory allocation failed\n");
1166 memcpy(tmp
+ add_len
+ 1, ri
->full_path
, len
);
1167 tmp
[len
+ add_len
+ 1] = '\0';
1169 memcpy(tmp
, p
, add_len
);
1170 free(ri
->full_path
);
1171 ri
->full_path
= tmp
;
1176 static int filter_by_parent(struct root_info
*ri
, u64 data
)
1178 return !uuid_compare(ri
->puuid
, (u8
*)(unsigned long)data
);
1181 static int filter_deleted(struct root_info
*ri
, u64 data
)
1186 static btrfs_list_filter_func all_filter_funcs
[] = {
1187 [BTRFS_LIST_FILTER_ROOTID
] = filter_by_rootid
,
1188 [BTRFS_LIST_FILTER_SNAPSHOT_ONLY
] = filter_snapshot
,
1189 [BTRFS_LIST_FILTER_FLAGS
] = filter_flags
,
1190 [BTRFS_LIST_FILTER_GEN_MORE
] = filter_gen_more
,
1191 [BTRFS_LIST_FILTER_GEN_LESS
] = filter_gen_less
,
1192 [BTRFS_LIST_FILTER_GEN_EQUAL
] = filter_gen_equal
,
1193 [BTRFS_LIST_FILTER_CGEN_MORE
] = filter_cgen_more
,
1194 [BTRFS_LIST_FILTER_CGEN_LESS
] = filter_cgen_less
,
1195 [BTRFS_LIST_FILTER_CGEN_EQUAL
] = filter_cgen_equal
,
1196 [BTRFS_LIST_FILTER_TOPID_EQUAL
] = filter_topid_equal
,
1197 [BTRFS_LIST_FILTER_FULL_PATH
] = filter_full_path
,
1198 [BTRFS_LIST_FILTER_BY_PARENT
] = filter_by_parent
,
1199 [BTRFS_LIST_FILTER_DELETED
] = filter_deleted
,
1202 struct btrfs_list_filter_set
*btrfs_list_alloc_filter_set(void)
1204 struct btrfs_list_filter_set
*set
;
1207 size
= sizeof(struct btrfs_list_filter_set
) +
1208 BTRFS_LIST_NFILTERS_INCREASE
* sizeof(struct btrfs_list_filter
);
1209 set
= calloc(1, size
);
1211 fprintf(stderr
, "memory allocation failed\n");
1215 set
->total
= BTRFS_LIST_NFILTERS_INCREASE
;
1220 void btrfs_list_free_filter_set(struct btrfs_list_filter_set
*filter_set
)
1225 int btrfs_list_setup_filter(struct btrfs_list_filter_set
**filter_set
,
1226 enum btrfs_list_filter_enum filter
, u64 data
)
1228 struct btrfs_list_filter_set
*set
= *filter_set
;
1232 BUG_ON(filter
>= BTRFS_LIST_FILTER_MAX
);
1233 BUG_ON(set
->nfilters
> set
->total
);
1235 if (set
->nfilters
== set
->total
) {
1238 size
= set
->total
+ BTRFS_LIST_NFILTERS_INCREASE
;
1239 size
= sizeof(*set
) + size
* sizeof(struct btrfs_list_filter
);
1241 set
= realloc(set
, size
);
1243 fprintf(stderr
, "memory allocation failed\n");
1248 memset(&set
->filters
[set
->total
], 0,
1249 BTRFS_LIST_NFILTERS_INCREASE
*
1250 sizeof(struct btrfs_list_filter
));
1251 set
->total
+= BTRFS_LIST_NFILTERS_INCREASE
;
1255 BUG_ON(set
->filters
[set
->nfilters
].filter_func
);
1257 if (filter
== BTRFS_LIST_FILTER_DELETED
)
1258 set
->only_deleted
= 1;
1260 set
->filters
[set
->nfilters
].filter_func
= all_filter_funcs
[filter
];
1261 set
->filters
[set
->nfilters
].data
= data
;
1266 static int filter_root(struct root_info
*ri
,
1267 struct btrfs_list_filter_set
*set
)
1274 if (set
->only_deleted
&& !ri
->deleted
)
1277 if (!set
->only_deleted
&& ri
->deleted
)
1280 for (i
= 0; i
< set
->nfilters
; i
++) {
1281 if (!set
->filters
[i
].filter_func
)
1283 ret
= set
->filters
[i
].filter_func(ri
, set
->filters
[i
].data
);
1290 static void __filter_and_sort_subvol(struct root_lookup
*all_subvols
,
1291 struct root_lookup
*sort_tree
,
1292 struct btrfs_list_filter_set
*filter_set
,
1293 struct btrfs_list_comparer_set
*comp_set
,
1297 struct root_info
*entry
;
1300 root_lookup_init(sort_tree
);
1302 n
= rb_last(&all_subvols
->root
);
1304 entry
= rb_entry(n
, struct root_info
, rb_node
);
1306 ret
= resolve_root(all_subvols
, entry
, top_id
);
1307 if (ret
== -ENOENT
) {
1308 entry
->full_path
= strdup("DELETED");
1311 ret
= filter_root(entry
, filter_set
);
1313 sort_tree_insert(sort_tree
, entry
, comp_set
);
1318 static int __list_subvol_fill_paths(int fd
, struct root_lookup
*root_lookup
)
1322 n
= rb_first(&root_lookup
->root
);
1324 struct root_info
*entry
;
1326 entry
= rb_entry(n
, struct root_info
, rb_node
);
1327 ret
= lookup_ino_path(fd
, entry
);
1328 if (ret
&& ret
!= -ENOENT
)
1336 static void print_subvolume_column(struct root_info
*subv
,
1337 enum btrfs_list_column_enum column
)
1340 char uuidparse
[BTRFS_UUID_UNPARSED_SIZE
];
1342 BUG_ON(column
>= BTRFS_LIST_ALL
|| column
< 0);
1345 case BTRFS_LIST_OBJECTID
:
1346 printf("%llu", subv
->root_id
);
1348 case BTRFS_LIST_GENERATION
:
1349 printf("%llu", subv
->gen
);
1351 case BTRFS_LIST_OGENERATION
:
1352 printf("%llu", subv
->ogen
);
1354 case BTRFS_LIST_PARENT
:
1355 printf("%llu", subv
->ref_tree
);
1357 case BTRFS_LIST_TOP_LEVEL
:
1358 printf("%llu", subv
->top_id
);
1360 case BTRFS_LIST_OTIME
:
1364 localtime_r(&subv
->otime
, &tm
);
1365 strftime(tstr
, 256, "%Y-%m-%d %X", &tm
);
1370 case BTRFS_LIST_UUID
:
1371 if (uuid_is_null(subv
->uuid
))
1372 strcpy(uuidparse
, "-");
1374 uuid_unparse(subv
->uuid
, uuidparse
);
1375 printf("%s", uuidparse
);
1377 case BTRFS_LIST_PUUID
:
1378 if (uuid_is_null(subv
->puuid
))
1379 strcpy(uuidparse
, "-");
1381 uuid_unparse(subv
->puuid
, uuidparse
);
1382 printf("%s", uuidparse
);
1384 case BTRFS_LIST_RUUID
:
1385 if (uuid_is_null(subv
->ruuid
))
1386 strcpy(uuidparse
, "-");
1388 uuid_unparse(subv
->ruuid
, uuidparse
);
1389 printf("%s", uuidparse
);
1391 case BTRFS_LIST_PATH
:
1392 BUG_ON(!subv
->full_path
);
1393 printf("%s", subv
->full_path
);
1400 static void print_single_volume_info_raw(struct root_info
*subv
, char *raw_prefix
)
1404 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1405 if (!btrfs_list_columns
[i
].need_print
)
1409 printf("%s",raw_prefix
);
1411 print_subvolume_column(subv
, i
);
1416 static void print_single_volume_info_table(struct root_info
*subv
)
1420 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1421 if (!btrfs_list_columns
[i
].need_print
)
1424 print_subvolume_column(subv
, i
);
1426 if (i
!= BTRFS_LIST_PATH
)
1429 if (i
== BTRFS_LIST_TOP_LEVEL
)
1435 static void print_single_volume_info_default(struct root_info
*subv
)
1439 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1440 if (!btrfs_list_columns
[i
].need_print
)
1443 printf("%s ", btrfs_list_columns
[i
].name
);
1444 print_subvolume_column(subv
, i
);
1446 if (i
!= BTRFS_LIST_PATH
)
1452 static void print_all_volume_info_tab_head(void)
1458 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1459 if (btrfs_list_columns
[i
].need_print
)
1460 printf("%s\t", btrfs_list_columns
[i
].name
);
1462 if (i
== BTRFS_LIST_ALL
-1)
1466 for (i
= 0; i
< BTRFS_LIST_ALL
; i
++) {
1467 memset(barrier
, 0, sizeof(barrier
));
1469 if (btrfs_list_columns
[i
].need_print
) {
1470 len
= strlen(btrfs_list_columns
[i
].name
);
1472 strcat(barrier
, "-");
1474 printf("%s\t", barrier
);
1476 if (i
== BTRFS_LIST_ALL
-1)
1481 static void print_all_volume_info(struct root_lookup
*sorted_tree
,
1482 int layout
, char *raw_prefix
)
1485 struct root_info
*entry
;
1487 if (layout
== BTRFS_LIST_LAYOUT_TABLE
)
1488 print_all_volume_info_tab_head();
1490 n
= rb_first(&sorted_tree
->root
);
1492 entry
= rb_entry(n
, struct root_info
, sort_node
);
1494 case BTRFS_LIST_LAYOUT_DEFAULT
:
1495 print_single_volume_info_default(entry
);
1497 case BTRFS_LIST_LAYOUT_TABLE
:
1498 print_single_volume_info_table(entry
);
1500 case BTRFS_LIST_LAYOUT_RAW
:
1501 print_single_volume_info_raw(entry
, raw_prefix
);
1508 static int btrfs_list_subvols(int fd
, struct root_lookup
*root_lookup
)
1512 ret
= __list_subvol_search(fd
, root_lookup
);
1514 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
1520 * now we have an rbtree full of root_info objects, but we need to fill
1521 * in their path names within the subvol that is referencing each one.
1523 ret
= __list_subvol_fill_paths(fd
, root_lookup
);
1527 int btrfs_list_subvols_print(int fd
, struct btrfs_list_filter_set
*filter_set
,
1528 struct btrfs_list_comparer_set
*comp_set
,
1529 int layout
, int full_path
, char *raw_prefix
)
1531 struct root_lookup root_lookup
;
1532 struct root_lookup root_sort
;
1537 ret
= btrfs_list_get_path_rootid(fd
, &top_id
);
1541 ret
= btrfs_list_subvols(fd
, &root_lookup
);
1544 __filter_and_sort_subvol(&root_lookup
, &root_sort
, filter_set
,
1547 print_all_volume_info(&root_sort
, layout
, raw_prefix
);
1548 __free_all_subvolumn(&root_lookup
);
1553 static char *strdup_or_null(const char *s
)
1560 int btrfs_get_subvol(int fd
, struct root_info
*the_ri
)
1563 struct root_lookup rl
;
1564 struct rb_node
*rbn
;
1565 struct root_info
*ri
;
1568 ret
= btrfs_list_get_path_rootid(fd
, &root_id
);
1572 ret
= btrfs_list_subvols(fd
, &rl
);
1576 rbn
= rb_first(&rl
.root
);
1578 ri
= rb_entry(rbn
, struct root_info
, rb_node
);
1579 rr
= resolve_root(&rl
, ri
, root_id
);
1580 if (rr
== -ENOENT
) {
1585 if (!comp_entry_with_rootid(the_ri
, ri
, 0)) {
1586 memcpy(the_ri
, ri
, offsetof(struct root_info
, path
));
1587 the_ri
->path
= strdup_or_null(ri
->path
);
1588 the_ri
->name
= strdup_or_null(ri
->name
);
1589 the_ri
->full_path
= strdup_or_null(ri
->full_path
);
1595 __free_all_subvolumn(&rl
);
1599 static int print_one_extent(int fd
, struct btrfs_ioctl_search_header
*sh
,
1600 struct btrfs_file_extent_item
*item
,
1601 u64 found_gen
, u64
*cache_dirid
,
1602 char **cache_dir_name
, u64
*cache_ino
,
1603 char **cache_full_name
)
1607 u64 disk_offset
= 0;
1613 if (sh
->objectid
== *cache_ino
) {
1614 name
= *cache_full_name
;
1615 } else if (*cache_full_name
) {
1616 free(*cache_full_name
);
1617 *cache_full_name
= NULL
;
1620 name
= ino_resolve(fd
, sh
->objectid
, cache_dirid
,
1622 *cache_full_name
= name
;
1623 *cache_ino
= sh
->objectid
;
1628 type
= btrfs_stack_file_extent_type(item
);
1629 compressed
= btrfs_stack_file_extent_compression(item
);
1631 if (type
== BTRFS_FILE_EXTENT_REG
||
1632 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1633 disk_start
= btrfs_stack_file_extent_disk_bytenr(item
);
1634 disk_offset
= btrfs_stack_file_extent_offset(item
);
1635 len
= btrfs_stack_file_extent_num_bytes(item
);
1636 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1639 len
= btrfs_stack_file_extent_ram_bytes(item
);
1641 printf("unhandled extent type %d for inode %llu "
1642 "file offset %llu gen %llu\n",
1644 (unsigned long long)sh
->objectid
,
1645 (unsigned long long)sh
->offset
,
1646 (unsigned long long)found_gen
);
1650 printf("inode %llu file offset %llu len %llu disk start %llu "
1651 "offset %llu gen %llu flags ",
1652 (unsigned long long)sh
->objectid
,
1653 (unsigned long long)sh
->offset
,
1654 (unsigned long long)len
,
1655 (unsigned long long)disk_start
,
1656 (unsigned long long)disk_offset
,
1657 (unsigned long long)found_gen
);
1663 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1664 printf("%sPREALLOC", flags
? "|" : "");
1667 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1668 printf("%sINLINE", flags
? "|" : "");
1674 printf(" %s\n", name
);
1678 int btrfs_list_find_updated_files(int fd
, u64 root_id
, u64 oldest_gen
)
1681 struct btrfs_ioctl_search_args args
;
1682 struct btrfs_ioctl_search_key
*sk
= &args
.key
;
1683 struct btrfs_ioctl_search_header sh
;
1684 struct btrfs_file_extent_item
*item
;
1685 unsigned long off
= 0;
1690 u64 cache_dirid
= 0;
1692 char *cache_dir_name
= NULL
;
1693 char *cache_full_name
= NULL
;
1694 struct btrfs_file_extent_item backup
;
1696 memset(&backup
, 0, sizeof(backup
));
1697 memset(&args
, 0, sizeof(args
));
1699 sk
->tree_id
= root_id
;
1702 * set all the other params to the max, we'll take any objectid
1705 sk
->max_objectid
= (u64
)-1;
1706 sk
->max_offset
= (u64
)-1;
1707 sk
->max_transid
= (u64
)-1;
1708 sk
->max_type
= BTRFS_EXTENT_DATA_KEY
;
1709 sk
->min_transid
= oldest_gen
;
1710 /* just a big number, doesn't matter much */
1711 sk
->nr_items
= 4096;
1713 max_found
= find_root_gen(fd
);
1715 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
1718 fprintf(stderr
, "ERROR: can't perform the search - %s\n",
1722 /* the ioctl returns the number of item it found in nr_items */
1723 if (sk
->nr_items
== 0)
1729 * for each item, pull the key out of the header and then
1730 * read the root_ref item it contains
1732 for (i
= 0; i
< sk
->nr_items
; i
++) {
1733 memcpy(&sh
, args
.buf
+ off
, sizeof(sh
));
1737 * just in case the item was too big, pass something other
1743 item
= (struct btrfs_file_extent_item
*)(args
.buf
+
1745 found_gen
= btrfs_stack_file_extent_generation(item
);
1746 if (sh
.type
== BTRFS_EXTENT_DATA_KEY
&&
1747 found_gen
>= oldest_gen
) {
1748 print_one_extent(fd
, &sh
, item
, found_gen
,
1749 &cache_dirid
, &cache_dir_name
,
1750 &cache_ino
, &cache_full_name
);
1755 * record the mins in sk so we can make sure the
1756 * next search doesn't repeat this root
1758 sk
->min_objectid
= sh
.objectid
;
1759 sk
->min_offset
= sh
.offset
;
1760 sk
->min_type
= sh
.type
;
1762 sk
->nr_items
= 4096;
1763 if (sk
->min_offset
< (u64
)-1)
1765 else if (sk
->min_objectid
< (u64
)-1) {
1772 free(cache_dir_name
);
1773 free(cache_full_name
);
1774 printf("transid marker was %llu\n", (unsigned long long)max_found
);
1778 char *btrfs_list_path_for_root(int fd
, u64 root
)
1780 struct root_lookup root_lookup
;
1782 char *ret_path
= NULL
;
1786 ret
= btrfs_list_get_path_rootid(fd
, &top_id
);
1788 return ERR_PTR(ret
);
1790 ret
= __list_subvol_search(fd
, &root_lookup
);
1792 return ERR_PTR(ret
);
1794 ret
= __list_subvol_fill_paths(fd
, &root_lookup
);
1796 return ERR_PTR(ret
);
1798 n
= rb_last(&root_lookup
.root
);
1800 struct root_info
*entry
;
1802 entry
= rb_entry(n
, struct root_info
, rb_node
);
1803 ret
= resolve_root(&root_lookup
, entry
, top_id
);
1804 if (ret
== -ENOENT
&& entry
->root_id
== root
) {
1808 if (entry
->root_id
== root
) {
1809 ret_path
= entry
->full_path
;
1810 entry
->full_path
= NULL
;
1815 __free_all_subvolumn(&root_lookup
);
1820 int btrfs_list_parse_sort_string(char *opt_arg
,
1821 struct btrfs_list_comparer_set
**comps
)
1829 while ((p
= strtok(opt_arg
, ",")) != NULL
) {
1831 ptr_argv
= all_sort_items
;
1834 if (strcmp(*ptr_argv
, p
) == 0) {
1839 if (strcmp(*ptr_argv
, p
) == 0) {
1856 } else if (*p
== '-') {
1862 what_to_sort
= btrfs_list_get_sort_item(p
);
1863 btrfs_list_setup_comparer(comps
, what_to_sort
, order
);
1872 * This function is used to parse the argument of filter condition.
1874 * type is the filter object.
1876 int btrfs_list_parse_filter_string(char *opt_arg
,
1877 struct btrfs_list_filter_set
**filters
,
1878 enum btrfs_list_filter_enum type
)
1883 switch (*(opt_arg
++)) {
1885 arg
= arg_strtou64(opt_arg
);
1888 btrfs_list_setup_filter(filters
, type
, arg
);
1891 arg
= arg_strtou64(opt_arg
);
1894 btrfs_list_setup_filter(filters
, type
, arg
);
1898 arg
= arg_strtou64(opt_arg
);
1900 btrfs_list_setup_filter(filters
, type
, arg
);
1907 int btrfs_list_get_path_rootid(int fd
, u64
*treeid
)
1910 struct btrfs_ioctl_ino_lookup_args args
;
1912 memset(&args
, 0, sizeof(args
));
1913 args
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1915 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
1918 "ERROR: can't perform the search - %s\n",
1922 *treeid
= args
.treeid
;