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.
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
27 #include <sys/types.h>
33 #include "kerncompat.h"
35 #include "transaction.h"
37 #include <uuid/uuid.h>
39 /* we store all the roots we find in an rbtree so that we can
40 * search for them later.
47 * one of these for each root we find.
50 struct rb_node rb_node;
55 /* the id of the root that references this one */
58 /* the dir id we're in from ref_tree */
61 /* generation when the root is created or last updated */
64 /* creation time of this root in sec*/
67 u8 uuid[BTRFS_UUID_SIZE];
69 /* path from the subvol we live in to this root, including the
70 * root's name. This is null until we do the extra lookup ioctl.
74 /* the name of this root in the directory it lives in */
78 static void root_lookup_init(struct root_lookup *tree)
80 tree->root.rb_node = NULL;
83 static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree)
85 if (entry->root_id > root_id)
87 if (entry->root_id < root_id)
89 if (entry->ref_tree > ref_tree)
91 if (entry->ref_tree < ref_tree)
96 static int comp_entry_with_gen(struct root_info *entry, u64 root_id,
97 u64 ref_tree, u64 gen)
101 if (entry->gen > gen)
103 return comp_entry(entry, root_id, ref_tree);
107 * insert a new root into the tree. returns the existing root entry
108 * if one is already there. Both root_id and ref_tree are used
111 static struct rb_node *tree_insert(struct rb_root *root, u64 root_id,
112 u64 ref_tree, u64 *gen, struct rb_node *node)
114 struct rb_node ** p = &root->rb_node;
115 struct rb_node * parent = NULL;
116 struct root_info *entry;
121 entry = rb_entry(parent, struct root_info, rb_node);
124 comp = comp_entry(entry, root_id, ref_tree);
126 comp = comp_entry_with_gen(entry, root_id, ref_tree,
137 entry = rb_entry(parent, struct root_info, rb_node);
138 rb_link_node(node, parent, p);
139 rb_insert_color(node, root);
144 * find a given root id in the tree. We return the smallest one,
145 * rb_next can be used to move forward looking for more if required
147 static struct root_info *tree_search(struct rb_root *root, u64 root_id)
149 struct rb_node * n = root->rb_node;
150 struct root_info *entry;
153 entry = rb_entry(n, struct root_info, rb_node);
155 if (entry->root_id < root_id)
157 else if (entry->root_id > root_id)
160 struct root_info *prev;
161 struct rb_node *prev_n;
166 prev = rb_entry(prev_n, struct root_info,
168 if (prev->root_id != root_id)
180 * this allocates a new root in the lookup tree.
182 * root_id should be the object id of the root
184 * ref_tree is the objectid of the referring root.
186 * dir_id is the directory in ref_tree where this root_id can be found.
188 * name is the name of root_id in that directory
190 * name_len is the length of name
192 static int add_root(struct root_lookup *root_lookup,
193 u64 root_id, u64 ref_tree, u64 dir_id, char *name,
194 int name_len, u64 *gen, time_t ot, void *uuid)
196 struct root_info *ri;
198 ri = malloc(sizeof(*ri) + name_len + 1);
200 printf("memory allocation failed\n");
203 memset(ri, 0, sizeof(*ri) + name_len + 1);
206 ri->root_id = root_id;
207 ri->ref_tree = ref_tree;
209 strncpy(ri->name, name, name_len);
211 ri->name[name_len] = 0;
217 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
219 memset(&ri->uuid, 0, BTRFS_UUID_SIZE);
221 ret = tree_insert(&root_lookup->root, root_id, ref_tree, gen,
224 printf("failed to insert tree %llu\n", (unsigned long long)root_id);
230 static int update_root(struct root_lookup *root_lookup, u64 root_id, u64 gen,
231 time_t ot, void *uuid)
233 struct root_info *ri;
235 ri = tree_search(&root_lookup->root, root_id);
236 if (!ri || ri->root_id != root_id) {
237 fprintf(stderr, "could not find subvol %llu\n", root_id);
243 memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
245 memset(&ri->uuid, 0, BTRFS_UUID_SIZE);
251 * for a given root_info, search through the root_lookup tree to construct
252 * the full path name to it.
254 * This can't be called until all the root_info->path fields are filled
255 * in by lookup_ino_path
257 static int resolve_root(struct root_lookup *rl, struct root_info *ri,
258 u64 *parent_id, u64 *top_id, char **path)
260 char *full_path = NULL;
262 struct root_info *found;
265 * we go backwards from the root_info object and add pathnames
266 * from parent directories as we go.
273 int add_len = strlen(found->path);
275 /* room for / and for null */
276 tmp = malloc(add_len + 2 + len);
278 memcpy(tmp + add_len + 1, full_path, len);
280 memcpy(tmp, found->path, add_len);
281 tmp [add_len + len + 1] = '\0';
286 full_path = strdup(found->path);
290 next = found->ref_tree;
291 /* record the first parent */
295 /* if the ref_tree refers to ourselves, we're at the top */
296 if (next == found->root_id) {
302 * if the ref_tree wasn't in our tree of roots, we're
305 found = tree_search(&rl->root, next);
318 * for a single root_info, ask the kernel to give us a path name
319 * inside it's ref_root for the dir_id where it lives.
321 * This fills in root_info->path with the path to the directory and and
322 * appends this root's name.
324 static int lookup_ino_path(int fd, struct root_info *ri)
326 struct btrfs_ioctl_ino_lookup_args args;
332 memset(&args, 0, sizeof(args));
333 args.treeid = ri->ref_tree;
334 args.objectid = ri->dir_id;
336 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
339 fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
340 (unsigned long long)ri->ref_tree,
347 * we're in a subdirectory of ref_tree, the kernel ioctl
348 * puts a / in there for us
350 ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
352 perror("malloc failed");
355 strcpy(ri->path, args.name);
356 strcat(ri->path, ri->name);
358 /* we're at the root of ref_tree */
359 ri->path = strdup(ri->name);
361 perror("strdup failed");
368 /* finding the generation for a given path is a two step process.
369 * First we use the inode loookup routine to find out the root id
371 * Then we use the tree search ioctl to scan all the root items for a
372 * given root id and spit out the latest generation we can find
374 static u64 find_root_gen(int fd)
376 struct btrfs_ioctl_ino_lookup_args ino_args;
378 struct btrfs_ioctl_search_args args;
379 struct btrfs_ioctl_search_key *sk = &args.key;
380 struct btrfs_ioctl_search_header *sh;
381 unsigned long off = 0;
386 memset(&ino_args, 0, sizeof(ino_args));
387 ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
389 /* this ioctl fills in ino_args->treeid */
390 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
393 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
394 (unsigned long long)BTRFS_FIRST_FREE_OBJECTID,
399 memset(&args, 0, sizeof(args));
404 * there may be more than one ROOT_ITEM key if there are
405 * snapshots pending deletion, we have to loop through
408 sk->min_objectid = ino_args.treeid;
409 sk->max_objectid = ino_args.treeid;
410 sk->max_type = BTRFS_ROOT_ITEM_KEY;
411 sk->min_type = BTRFS_ROOT_ITEM_KEY;
412 sk->max_offset = (u64)-1;
413 sk->max_transid = (u64)-1;
417 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
420 fprintf(stderr, "ERROR: can't perform the search - %s\n",
424 /* the ioctl returns the number of item it found in nr_items */
425 if (sk->nr_items == 0)
429 for (i = 0; i < sk->nr_items; i++) {
430 struct btrfs_root_item *item;
431 sh = (struct btrfs_ioctl_search_header *)(args.buf +
435 item = (struct btrfs_root_item *)(args.buf + off);
438 sk->min_objectid = sh->objectid;
439 sk->min_type = sh->type;
440 sk->min_offset = sh->offset;
442 if (sh->objectid > ino_args.treeid)
445 if (sh->objectid == ino_args.treeid &&
446 sh->type == BTRFS_ROOT_ITEM_KEY) {
447 max_found = max(max_found,
448 btrfs_root_generation(item));
451 if (sk->min_offset < (u64)-1)
456 if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
458 if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
464 /* pass in a directory id and this will return
465 * the full path of the parent directory inside its
468 * It may return NULL if it is in the root, or an ERR_PTR if things
471 static char *__ino_resolve(int fd, u64 dirid)
473 struct btrfs_ioctl_ino_lookup_args args;
478 memset(&args, 0, sizeof(args));
479 args.objectid = dirid;
481 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
484 fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n",
485 (unsigned long long)dirid, strerror(e) );
491 * we're in a subdirectory of ref_tree, the kernel ioctl
492 * puts a / in there for us
494 full = strdup(args.name);
496 perror("malloc failed");
497 return ERR_PTR(-ENOMEM);
500 /* we're at the root of ref_tree */
507 * simple string builder, returning a new string with both
510 char *build_name(char *dirid, char *name)
516 full = malloc(strlen(dirid) + strlen(name) + 1);
525 * given an inode number, this returns the full path name inside the subvolume
526 * to that file/directory. cache_dirid and cache_name are used to
527 * cache the results so we can avoid tree searches if a later call goes
528 * to the same directory or file name
530 static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
538 struct btrfs_ioctl_search_args args;
539 struct btrfs_ioctl_search_key *sk = &args.key;
540 struct btrfs_ioctl_search_header *sh;
541 unsigned long off = 0;
545 memset(&args, 0, sizeof(args));
550 * step one, we search for the inode back ref. We just use the first
553 sk->min_objectid = ino;
554 sk->max_objectid = ino;
555 sk->max_type = BTRFS_INODE_REF_KEY;
556 sk->max_offset = (u64)-1;
557 sk->min_type = BTRFS_INODE_REF_KEY;
558 sk->max_transid = (u64)-1;
561 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
564 fprintf(stderr, "ERROR: can't perform the search - %s\n",
568 /* the ioctl returns the number of item it found in nr_items */
569 if (sk->nr_items == 0)
573 sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
575 if (sh->type == BTRFS_INODE_REF_KEY) {
576 struct btrfs_inode_ref *ref;
579 ref = (struct btrfs_inode_ref *)(sh + 1);
580 namelen = btrfs_stack_inode_ref_name_len(ref);
582 name = (char *)(ref + 1);
583 name = strndup(name, namelen);
585 /* use our cached value */
586 if (dirid == *cache_dirid && *cache_name) {
587 dirname = *cache_name;
594 * the inode backref gives us the file name and the parent directory id.
595 * From here we use __ino_resolve to get the path to the parent
597 dirname = __ino_resolve(fd, dirid);
599 full = build_name(dirname, name);
600 if (*cache_name && dirname != *cache_name)
603 *cache_name = dirname;
604 *cache_dirid = dirid;
610 static int get_default_subvolid(int fd, u64 *default_id)
612 struct btrfs_ioctl_search_args args;
613 struct btrfs_ioctl_search_key *sk = &args.key;
614 struct btrfs_ioctl_search_header *sh;
618 memset(&args, 0, sizeof(args));
621 * search for a dir item with a name 'default' in the tree of
622 * tree roots, it should point us to a default root
626 /* don't worry about ancient format and request only one item */
629 sk->max_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
630 sk->min_objectid = BTRFS_ROOT_TREE_DIR_OBJECTID;
631 sk->max_type = BTRFS_DIR_ITEM_KEY;
632 sk->min_type = BTRFS_DIR_ITEM_KEY;
633 sk->max_offset = (u64)-1;
634 sk->max_transid = (u64)-1;
636 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
640 /* the ioctl returns the number of items it found in nr_items */
641 if (sk->nr_items == 0)
644 sh = (struct btrfs_ioctl_search_header *)args.buf;
646 if (sh->type == BTRFS_DIR_ITEM_KEY) {
647 struct btrfs_dir_item *di;
651 di = (struct btrfs_dir_item *)(sh + 1);
652 name_len = btrfs_stack_dir_name_len(di);
653 name = (char *)(di + 1);
655 if (!strncmp("default", name, name_len))
656 found = btrfs_disk_key_objectid(&di->location);
664 static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
667 struct btrfs_ioctl_search_args args;
668 struct btrfs_ioctl_search_key *sk = &args.key;
669 struct btrfs_ioctl_search_header *sh;
670 struct btrfs_root_ref *ref;
671 struct btrfs_root_item *ri;
672 unsigned long off = 0;
681 u8 uuid[BTRFS_UUID_SIZE];
683 root_lookup_init(root_lookup);
684 memset(&args, 0, sizeof(args));
686 /* search in the tree of tree roots */
690 * set the min and max to backref keys. The search will
691 * only send back this type of key now.
693 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
694 sk->min_type = BTRFS_ROOT_BACKREF_KEY;
696 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
699 * set all the other params to the max, we'll take any objectid
702 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
703 sk->max_offset = (u64)-1;
704 sk->max_transid = (u64)-1;
707 /* just a big number, doesn't matter much */
711 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
714 /* the ioctl returns the number of item it found in nr_items */
715 if (sk->nr_items == 0)
721 * for each item, pull the key out of the header and then
722 * read the root_ref item it contains
724 for (i = 0; i < sk->nr_items; i++) {
725 sh = (struct btrfs_ioctl_search_header *)(args.buf +
728 if (!get_gen && sh->type == BTRFS_ROOT_BACKREF_KEY) {
729 ref = (struct btrfs_root_ref *)(args.buf + off);
730 name_len = btrfs_stack_root_ref_name_len(ref);
731 name = (char *)(ref + 1);
732 dir_id = btrfs_stack_root_ref_dirid(ref);
734 add_root(root_lookup, sh->objectid, sh->offset,
735 dir_id, name, name_len, NULL, 0, NULL);
736 } else if (get_gen && sh->type == BTRFS_ROOT_ITEM_KEY) {
737 ri = (struct btrfs_root_item *)(args.buf + off);
738 gen = btrfs_root_generation(ri);
739 if(ri->generation == ri->generation_v2) {
741 memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
744 memset(uuid, 0, BTRFS_UUID_SIZE);
747 update_root(root_lookup, sh->objectid, gen, t,
754 * record the mins in sk so we can make sure the
755 * next search doesn't repeat this root
757 sk->min_objectid = sh->objectid;
758 sk->min_type = sh->type;
759 sk->min_offset = sh->offset;
762 /* this iteration is done, step forward one root for the next
766 type = BTRFS_ROOT_ITEM_KEY;
768 type = BTRFS_ROOT_BACKREF_KEY;
770 if (sk->min_type < type) {
773 } else if (sk->min_objectid < BTRFS_LAST_FREE_OBJECTID) {
782 memset(&args, 0, sizeof(args));
785 sk->max_type = BTRFS_ROOT_ITEM_KEY;
786 sk->min_type = BTRFS_ROOT_ITEM_KEY;
788 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
790 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
791 sk->max_offset = (u64)-1;
792 sk->max_transid = (u64)-1;
800 static int __list_snapshot_search(int fd, struct root_lookup *root_lookup)
803 struct btrfs_ioctl_search_args args;
804 struct btrfs_ioctl_search_key *sk = &args.key;
805 struct btrfs_ioctl_search_header *sh;
806 unsigned long off = 0;
810 root_lookup_init(root_lookup);
811 memset(&args, 0, sizeof(args));
814 sk->max_type = BTRFS_ROOT_ITEM_KEY;
815 sk->min_type = BTRFS_ROOT_ITEM_KEY;
816 sk->min_objectid = BTRFS_FIRST_FREE_OBJECTID;
817 sk->max_objectid = BTRFS_LAST_FREE_OBJECTID;
818 sk->max_offset = (u64)-1;
819 sk->max_transid = (u64)-1;
823 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
826 /* the ioctl returns the number of item it found in nr_items */
827 if (sk->nr_items == 0)
833 * for each item, pull the key out of the header and then
834 * read the root_ref item it contains
836 for (i = 0; i < sk->nr_items; i++) {
837 struct btrfs_root_item *item;
839 u8 uuid[BTRFS_UUID_SIZE];
841 sh = (struct btrfs_ioctl_search_header *)(args.buf +
844 if (sh->type == BTRFS_ROOT_ITEM_KEY && sh->offset) {
845 item = (struct btrfs_root_item *)(args.buf + off);
846 if(item->generation == item->generation_v2) {
848 memcpy(uuid, item->uuid, BTRFS_UUID_SIZE);
851 memset(uuid, 0, BTRFS_UUID_SIZE);
855 add_root(root_lookup, sh->objectid, 0,
856 0, NULL, 0, &gen, t, uuid);
861 * record the mins in sk so we can make sure the
862 * next search doesn't repeat this root
864 sk->min_objectid = sh->objectid;
865 sk->min_type = sh->type;
866 sk->min_offset = sh->offset;
869 /* this iteration is done, step forward one root for the next
872 if (sk->min_type < BTRFS_ROOT_ITEM_KEY) {
873 sk->min_type = BTRFS_ROOT_ITEM_KEY;
875 } else if (sk->min_objectid < BTRFS_LAST_FREE_OBJECTID) {
877 sk->min_type = BTRFS_ROOT_ITEM_KEY;
885 static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
889 n = rb_first(&root_lookup->root);
891 struct root_info *entry;
893 entry = rb_entry(n, struct root_info, rb_node);
894 ret = lookup_ino_path(fd, entry);
903 int list_subvols(int fd, int print_parent, int get_default, int print_uuid)
905 struct root_lookup root_lookup;
912 ret = get_default_subvolid(fd, &default_id);
914 fprintf(stderr, "ERROR: can't perform the search - %s\n",
918 if (default_id == 0) {
919 fprintf(stderr, "ERROR: 'default' dir item not found\n");
923 /* no need to resolve roots if FS_TREE is default */
924 if (default_id == BTRFS_FS_TREE_OBJECTID) {
925 printf("ID 5 (FS_TREE)\n");
930 ret = __list_subvol_search(fd, &root_lookup);
932 fprintf(stderr, "ERROR: can't perform the search - %s\n",
938 * now we have an rbtree full of root_info objects, but we need to fill
939 * in their path names within the subvol that is referencing each one.
941 ret = __list_subvol_fill_paths(fd, &root_lookup);
945 /* now that we have all the subvol-relative paths filled in,
946 * we have to string the subvols together so that we can get
947 * a path all the way back to the FS root
949 n = rb_last(&root_lookup.root);
951 struct root_info *entry;
956 entry = rb_entry(n, struct root_info, rb_node);
957 if (get_default && entry->root_id != default_id) {
962 resolve_root(&root_lookup, entry, &parent_id, &level, &path);
965 if (uuid_is_null(entry->uuid))
966 strcpy(uuidparse, "-");
968 uuid_unparse(entry->uuid, uuidparse);
969 printf("ID %llu gen %llu parent %llu top level %llu"
970 " uuid %s path %s\n",
971 (unsigned long long)entry->root_id,
972 (unsigned long long)entry->gen,
973 (unsigned long long)parent_id,
974 (unsigned long long)level,
977 printf("ID %llu gen %llu parent %llu top level"
979 (unsigned long long)entry->root_id,
980 (unsigned long long)entry->gen,
981 (unsigned long long)parent_id,
982 (unsigned long long)level, path);
986 if (uuid_is_null(entry->uuid))
987 strcpy(uuidparse, "-");
989 uuid_unparse(entry->uuid, uuidparse);
990 printf("ID %llu gen %llu top level %llu"
991 " uuid %s path %s\n",
992 (unsigned long long)entry->root_id,
993 (unsigned long long)entry->gen,
994 (unsigned long long)level,
997 printf("ID %llu gen %llu top level %llu path %s\n",
998 (unsigned long long)entry->root_id,
999 (unsigned long long)entry->gen,
1000 (unsigned long long)level, path);
1011 int list_snapshots(int fd, int print_parent, int order, int print_uuid)
1013 struct root_lookup root_lookup;
1014 struct root_lookup root_lookup_snap;
1018 ret = __list_snapshot_search(fd, &root_lookup_snap);
1020 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1025 ret = __list_subvol_search(fd, &root_lookup);
1027 fprintf(stderr, "ERROR: can't perform the search - %s\n",
1033 * now we have an rbtree full of root_info objects, but we need to fill
1034 * in their path names within the subvol that is referencing each one.
1036 ret = __list_subvol_fill_paths(fd, &root_lookup);
1040 /* now that we have all the subvol-relative paths filled in,
1041 * we have to string the subvols together so that we can get
1042 * a path all the way back to the FS root
1045 n = rb_last(&root_lookup_snap.root);
1047 n = rb_first(&root_lookup_snap.root);
1049 struct root_info *entry_snap;
1050 struct root_info *entry;
1058 entry_snap = rb_entry(n, struct root_info, rb_node);
1059 entry = tree_search(&root_lookup.root, entry_snap->root_id);
1061 resolve_root(&root_lookup, entry, &parent_id, &level, &path);
1064 strftime(tstr,256,"%Y-%m-%d %X",localtime(&t));
1069 if (uuid_is_null(entry->uuid))
1070 strcpy(uuidparse, "-");
1072 uuid_unparse(entry->uuid, uuidparse);
1073 printf("ID %llu gen %llu cgen %llu parent %llu"
1074 " top level %llu otime %s uuid %s path %s\n",
1075 (unsigned long long)entry->root_id,
1076 (unsigned long long)entry->gen,
1077 (unsigned long long)entry_snap->gen,
1078 (unsigned long long)parent_id,
1079 (unsigned long long)level,
1080 tstr, uuidparse, path);
1082 printf("ID %llu gen %llu cgen %llu parent %llu"
1083 " top level %llu otime %s path %s\n",
1084 (unsigned long long)entry->root_id,
1085 (unsigned long long)entry->gen,
1086 (unsigned long long)entry_snap->gen,
1087 (unsigned long long)parent_id,
1088 (unsigned long long)level, tstr, path);
1092 if (uuid_is_null(entry->uuid))
1093 strcpy(uuidparse, "-");
1095 uuid_unparse(entry->uuid, uuidparse);
1096 printf("ID %llu gen %llu cgen %llu top level %llu "
1097 "otime %s uuid %s path %s\n",
1098 (unsigned long long)entry->root_id,
1099 (unsigned long long)entry->gen,
1100 (unsigned long long)entry_snap->gen,
1101 (unsigned long long)level,
1102 tstr, uuidparse, path);
1104 printf("ID %llu gen %llu cgen %llu top level %llu "
1105 "otime %s path %s\n",
1106 (unsigned long long)entry->root_id,
1107 (unsigned long long)entry->gen,
1108 (unsigned long long)entry_snap->gen,
1109 (unsigned long long)level, tstr, path);
1123 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
1124 struct btrfs_file_extent_item *item,
1125 u64 found_gen, u64 *cache_dirid,
1126 char **cache_dir_name, u64 *cache_ino,
1127 char **cache_full_name)
1131 u64 disk_offset = 0;
1137 if (sh->objectid == *cache_ino) {
1138 name = *cache_full_name;
1139 } else if (*cache_full_name) {
1140 free(*cache_full_name);
1141 *cache_full_name = NULL;
1144 name = ino_resolve(fd, sh->objectid, cache_dirid,
1146 *cache_full_name = name;
1147 *cache_ino = sh->objectid;
1152 type = btrfs_stack_file_extent_type(item);
1153 compressed = btrfs_stack_file_extent_compression(item);
1155 if (type == BTRFS_FILE_EXTENT_REG ||
1156 type == BTRFS_FILE_EXTENT_PREALLOC) {
1157 disk_start = btrfs_stack_file_extent_disk_bytenr(item);
1158 disk_offset = btrfs_stack_file_extent_offset(item);
1159 len = btrfs_stack_file_extent_num_bytes(item);
1160 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1163 len = btrfs_stack_file_extent_ram_bytes(item);
1165 printf("unhandled extent type %d for inode %llu "
1166 "file offset %llu gen %llu\n",
1168 (unsigned long long)sh->objectid,
1169 (unsigned long long)sh->offset,
1170 (unsigned long long)found_gen);
1174 printf("inode %llu file offset %llu len %llu disk start %llu "
1175 "offset %llu gen %llu flags ",
1176 (unsigned long long)sh->objectid,
1177 (unsigned long long)sh->offset,
1178 (unsigned long long)len,
1179 (unsigned long long)disk_start,
1180 (unsigned long long)disk_offset,
1181 (unsigned long long)found_gen);
1187 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
1188 printf("%sPREALLOC", flags ? "|" : "");
1191 if (type == BTRFS_FILE_EXTENT_INLINE) {
1192 printf("%sINLINE", flags ? "|" : "");
1198 printf(" %s\n", name);
1202 int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
1205 struct btrfs_ioctl_search_args args;
1206 struct btrfs_ioctl_search_key *sk = &args.key;
1207 struct btrfs_ioctl_search_header *sh;
1208 struct btrfs_file_extent_item *item;
1209 unsigned long off = 0;
1214 u64 cache_dirid = 0;
1216 char *cache_dir_name = NULL;
1217 char *cache_full_name = NULL;
1218 struct btrfs_file_extent_item backup;
1220 memset(&backup, 0, sizeof(backup));
1221 memset(&args, 0, sizeof(args));
1223 sk->tree_id = root_id;
1226 * set all the other params to the max, we'll take any objectid
1229 sk->max_objectid = (u64)-1;
1230 sk->max_offset = (u64)-1;
1231 sk->max_transid = (u64)-1;
1232 sk->max_type = BTRFS_EXTENT_DATA_KEY;
1233 sk->min_transid = oldest_gen;
1234 /* just a big number, doesn't matter much */
1235 sk->nr_items = 4096;
1237 max_found = find_root_gen(fd);
1239 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1242 fprintf(stderr, "ERROR: can't perform the search- %s\n",
1246 /* the ioctl returns the number of item it found in nr_items */
1247 if (sk->nr_items == 0)
1253 * for each item, pull the key out of the header and then
1254 * read the root_ref item it contains
1256 for (i = 0; i < sk->nr_items; i++) {
1257 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1262 * just in case the item was too big, pass something other
1268 item = (struct btrfs_file_extent_item *)(args.buf +
1270 found_gen = btrfs_stack_file_extent_generation(item);
1271 if (sh->type == BTRFS_EXTENT_DATA_KEY &&
1272 found_gen >= oldest_gen) {
1273 print_one_extent(fd, sh, item, found_gen,
1274 &cache_dirid, &cache_dir_name,
1275 &cache_ino, &cache_full_name);
1280 * record the mins in sk so we can make sure the
1281 * next search doesn't repeat this root
1283 sk->min_objectid = sh->objectid;
1284 sk->min_offset = sh->offset;
1285 sk->min_type = sh->type;
1287 sk->nr_items = 4096;
1288 if (sk->min_offset < (u64)-1)
1290 else if (sk->min_objectid < (u64)-1) {
1297 free(cache_dir_name);
1298 free(cache_full_name);
1299 printf("transid marker was %llu\n", (unsigned long long)max_found);
1303 char *path_for_root(int fd, u64 root)
1305 struct root_lookup root_lookup;
1307 char *ret_path = NULL;
1310 ret = __list_subvol_search(fd, &root_lookup);
1312 return ERR_PTR(ret);
1314 ret = __list_subvol_fill_paths(fd, &root_lookup);
1316 return ERR_PTR(ret);
1318 n = rb_last(&root_lookup.root);
1320 struct root_info *entry;
1325 entry = rb_entry(n, struct root_info, rb_node);
1326 resolve_root(&root_lookup, entry, &parent_id, &level, &path);
1327 if (entry->root_id == root)