struct rb_root root;
};
-/*
- * one of these for each root we find.
- */
-struct root_info {
- struct rb_node rb_node;
- struct rb_node sort_node;
-
- /* this root's id */
- u64 root_id;
-
- /* equal the offset of the root's key */
- u64 root_offset;
-
- /* flags of the root */
- u64 flags;
-
- /* the id of the root that references this one */
- u64 ref_tree;
-
- /* the dir id we're in from ref_tree */
- u64 dir_id;
-
- u64 top_id;
-
- /* generation when the root is created or last updated */
- u64 gen;
-
- /* creation generation of this root in sec*/
- u64 ogen;
-
- /* creation time of this root in sec*/
- time_t otime;
-
- u8 uuid[BTRFS_UUID_SIZE];
- u8 puuid[BTRFS_UUID_SIZE];
-
- /* path from the subvol we live in to this root, including the
- * root's name. This is null until we do the extra lookup ioctl.
- */
- char *path;
-
- /* the name of this root in the directory it lives in */
- char *name;
-
- char *full_path;
-};
-
struct {
char *name;
char *column_name;
{
.name = "ID",
.column_name = "ID",
- .need_print = 1,
+ .need_print = 0,
},
{
.name = "gen",
.column_name = "Gen",
- .need_print = 1,
+ .need_print = 0,
},
{
.name = "cgen",
{
.name = "top level",
.column_name = "Top Level",
- .need_print = 1,
+ .need_print = 0,
},
{
.name = "otime",
{
.name = "path",
.column_name = "Path",
- .need_print = 1,
+ .need_print = 0,
},
{
.name = NULL,
while (1) {
char *tmp;
u64 next;
- int add_len = strlen(found->path);
+ int add_len;
+
+ /*
+ * ref_tree = 0 indicates the subvolumes
+ * has been deleted.
+ */
+ if (!found->ref_tree) {
+ free(full_path);
+ return -ENOENT;
+ }
+
+ add_len = strlen(found->path);
/* room for / and for null */
tmp = malloc(add_len + 2 + len);
next = found->ref_tree;
- if (next == top_id) {
+ if (next == top_id) {
ri->top_id = top_id;
break;
}
+ /*
+ * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
+ * we are at the top
+ */
if (next == BTRFS_FS_TREE_OBJECTID) {
ri->top_id = next;
break;
}
/*
- * if the ref_tree wasn't in our tree of roots, we're
- * at the top
- */
+ * if the ref_tree wasn't in our tree of roots, the
+ * subvolume was deleted.
+ */
found = root_tree_search(rl, next);
if (!found) {
- ri->top_id = next;
- break;
+ free(full_path);
+ return -ENOENT;
}
}
if (ri->path)
return 0;
+ if (!ri->ref_tree)
+ return -ENOENT;
+
memset(&args, 0, sizeof(args));
args.treeid = ri->ref_tree;
args.objectid = ri->dir_id;
ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
e = errno;
if (ret) {
+ if (e == ENOENT) {
+ ri->ref_tree = 0;
+ return -ENOENT;
+ }
fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
(unsigned long long)ri->ref_tree,
strerror(e));
return 1;
}
+static int filter_by_parent(struct root_info *ri, u64 data)
+{
+ return !uuid_compare(ri->puuid, (u8 *)data);
+}
+
static btrfs_list_filter_func all_filter_funcs[] = {
[BTRFS_LIST_FILTER_ROOTID] = filter_by_rootid,
[BTRFS_LIST_FILTER_SNAPSHOT_ONLY] = filter_snapshot,
[BTRFS_LIST_FILTER_CGEN_EQUAL] = filter_cgen_equal,
[BTRFS_LIST_FILTER_TOPID_EQUAL] = filter_topid_equal,
[BTRFS_LIST_FILTER_FULL_PATH] = filter_full_path,
+ [BTRFS_LIST_FILTER_BY_PARENT] = filter_by_parent,
};
struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
while (n) {
entry = rb_entry(n, struct root_info, rb_node);
- resolve_root(all_subvols, entry, top_id);
+ ret = resolve_root(all_subvols, entry, top_id);
+ if (ret == -ENOENT)
+ goto skip;
ret = filter_root(entry, filter_set);
if (ret)
sort_tree_insert(sort_tree, entry, comp_set);
+skip:
n = rb_prev(n);
}
}
int ret;
entry = rb_entry(n, struct root_info, rb_node);
ret = lookup_ino_path(fd, entry);
- if(ret < 0)
+ if (ret && ret != -ENOENT)
return ret;
n = rb_next(n);
}
}
}
+static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
+{
+ int i;
+
+ for (i = 0; i < BTRFS_LIST_ALL; i++) {
+ if (!btrfs_list_columns[i].need_print)
+ continue;
+
+ if (raw_prefix)
+ printf("%s",raw_prefix);
+
+ print_subvolume_column(subv, i);
+ }
+ printf("\n");
+}
+
static void print_single_volume_info_table(struct root_info *subv)
{
int i;
}
static void print_all_volume_info(struct root_lookup *sorted_tree,
- int is_tab_result)
+ int layout, char *raw_prefix)
{
struct rb_node *n;
struct root_info *entry;
- if (is_tab_result)
+ if (layout == BTRFS_LIST_LAYOUT_TABLE)
print_all_volume_info_tab_head();
n = rb_first(&sorted_tree->root);
while (n) {
entry = rb_entry(n, struct root_info, sort_node);
- if (is_tab_result)
- print_single_volume_info_table(entry);
- else
+ switch (layout) {
+ case BTRFS_LIST_LAYOUT_DEFAULT:
print_single_volume_info_default(entry);
+ break;
+ case BTRFS_LIST_LAYOUT_TABLE:
+ print_single_volume_info_table(entry);
+ break;
+ case BTRFS_LIST_LAYOUT_RAW:
+ print_single_volume_info_raw(entry, raw_prefix);
+ break;
+ }
n = rb_next(n);
}
}
int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
struct btrfs_list_comparer_set *comp_set,
- int is_tab_result, int full_path)
+ int layout, int full_path, char *raw_prefix)
{
struct root_lookup root_lookup;
struct root_lookup root_sort;
- int ret;
- u64 top_id = (full_path ? 0 : btrfs_list_get_path_rootid(fd));
+ int ret = 0;
+ u64 top_id = 0;
+
+ if (full_path)
+ ret = btrfs_list_get_path_rootid(fd, &top_id);
+ if (ret)
+ return ret;
ret = btrfs_list_subvols(fd, &root_lookup);
if (ret)
__filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
comp_set, top_id);
- print_all_volume_info(&root_sort, is_tab_result);
+ print_all_volume_info(&root_sort, layout, raw_prefix);
__free_all_subvolumn(&root_lookup);
return 0;
}
+char *strdup_or_null(const char *s)
+{
+ if (!s)
+ return NULL;
+ return strdup(s);
+}
+
+int btrfs_get_subvol(int fd, struct root_info *the_ri)
+{
+ int ret, rr;
+ struct root_lookup rl;
+ struct rb_node *rbn;
+ struct root_info *ri;
+ u64 root_id;
+
+ ret = btrfs_list_get_path_rootid(fd, &root_id);
+ if (ret)
+ return ret;
+
+ ret = btrfs_list_subvols(fd, &rl);
+ if (ret)
+ return ret;
+
+ rbn = rb_first(&rl.root);
+ while(rbn) {
+ ri = rb_entry(rbn, struct root_info, rb_node);
+ rr = resolve_root(&rl, ri, root_id);
+ if (rr == -ENOENT) {
+ ret = -ENOENT;
+ rbn = rb_next(rbn);
+ continue;
+ }
+ if (!comp_entry_with_rootid(the_ri, ri, 0)) {
+ memcpy(the_ri, ri, offsetof(struct root_info, path));
+ the_ri->path = strdup_or_null(ri->path);
+ the_ri->name = strdup_or_null(ri->name);
+ the_ri->full_path = strdup_or_null(ri->full_path);
+ ret = 0;
+ break;
+ }
+ rbn = rb_next(rbn);
+ }
+ __free_all_subvolumn(&rl);
+ return ret;
+}
+
static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
struct btrfs_file_extent_item *item,
u64 found_gen, u64 *cache_dirid,
struct rb_node *n;
char *ret_path = NULL;
int ret;
- u64 top_id = btrfs_list_get_path_rootid(fd);
+ u64 top_id;
+
+ ret = btrfs_list_get_path_rootid(fd, &top_id);
+ if (ret)
+ return ERR_PTR(ret);
ret = __list_subvol_search(fd, &root_lookup);
if (ret < 0)
struct root_info *entry;
entry = rb_entry(n, struct root_info, rb_node);
- resolve_root(&root_lookup, entry, top_id);
+ ret = resolve_root(&root_lookup, entry, top_id);
+ if (ret == -ENOENT && entry->root_id == root) {
+ ret_path = NULL;
+ break;
+ }
if (entry->root_id == root) {
ret_path = entry->full_path;
entry->full_path = NULL;
return 0;
}
-u64 btrfs_list_get_path_rootid(int fd)
+int btrfs_list_get_path_rootid(int fd, u64 *treeid)
{
int ret;
struct btrfs_ioctl_ino_lookup_args args;
strerror(errno));
return ret;
}
- return args.treeid;
+ *treeid = args.treeid;
+ return 0;
}