+ return !!ri->root_offset;
+}
+
+static int filter_flags(struct root_info *ri, u64 flags)
+{
+ return ri->flags & flags;
+}
+
+static int filter_gen_more(struct root_info *ri, u64 data)
+{
+ return ri->gen >= data;
+}
+
+static int filter_gen_less(struct root_info *ri, u64 data)
+{
+ return ri->gen <= data;
+}
+
+static int filter_gen_equal(struct root_info *ri, u64 data)
+{
+ return ri->gen == data;
+}
+
+static int filter_cgen_more(struct root_info *ri, u64 data)
+{
+ return ri->ogen >= data;
+}
+
+static int filter_cgen_less(struct root_info *ri, u64 data)
+{
+ return ri->ogen <= data;
+}
+
+static int filter_cgen_equal(struct root_info *ri, u64 data)
+{
+ return ri->ogen == data;
+}
+
+static int filter_topid_equal(struct root_info *ri, u64 data)
+{
+ return ri->top_id == data;
+}
+
+static int filter_full_path(struct root_info *ri, u64 data)
+{
+ if (ri->full_path && ri->top_id != data) {
+ char *tmp;
+ char p[] = "<FS_TREE>";
+ int add_len = strlen(p);
+ int len = strlen(ri->full_path);
+
+ tmp = malloc(len + add_len + 2);
+ if (!tmp) {
+ fprintf(stderr, "memory allocation failed\n");
+ exit(1);
+ }
+ memcpy(tmp + add_len + 1, ri->full_path, len);
+ tmp[len + add_len + 1] = '\0';
+ tmp[add_len] = '/';
+ memcpy(tmp, p, add_len);
+ free(ri->full_path);
+ ri->full_path = tmp;
+ }
+ return 1;
+}
+
+static int filter_by_parent(struct root_info *ri, u64 data)
+{
+ return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
+}
+
+static int filter_deleted(struct root_info *ri, u64 data)
+{
+ return ri->deleted;
+}
+
+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_FLAGS] = filter_flags,
+ [BTRFS_LIST_FILTER_GEN_MORE] = filter_gen_more,
+ [BTRFS_LIST_FILTER_GEN_LESS] = filter_gen_less,
+ [BTRFS_LIST_FILTER_GEN_EQUAL] = filter_gen_equal,
+ [BTRFS_LIST_FILTER_CGEN_MORE] = filter_cgen_more,
+ [BTRFS_LIST_FILTER_CGEN_LESS] = filter_cgen_less,
+ [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,
+ [BTRFS_LIST_FILTER_DELETED] = filter_deleted,
+};
+
+struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
+{
+ struct btrfs_list_filter_set *set;
+ int size;
+
+ size = sizeof(struct btrfs_list_filter_set) +
+ BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
+ set = calloc(1, size);
+ if (!set) {
+ fprintf(stderr, "memory allocation failed\n");
+ exit(1);
+ }
+
+ set->total = BTRFS_LIST_NFILTERS_INCREASE;
+
+ return set;
+}
+
+/*
+ * Setup list filters. Exit if there's not enough memory, as we can't continue
+ * without the structures set up properly.
+ */
+void btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
+ enum btrfs_list_filter_enum filter, u64 data)
+{
+ struct btrfs_list_filter_set *set = *filter_set;
+ int size;
+
+ ASSERT(set != NULL);
+ ASSERT(filter < BTRFS_LIST_FILTER_MAX);
+ ASSERT(set->nfilters <= set->total);
+
+ if (set->nfilters == set->total) {
+ void *tmp;
+
+ size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
+ size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
+ tmp = set;
+ set = realloc(set, size);
+ if (!set) {
+ fprintf(stderr, "memory allocation failed\n");
+ free(tmp);
+ exit(1);
+ }
+
+ memset(&set->filters[set->total], 0,
+ BTRFS_LIST_NFILTERS_INCREASE *
+ sizeof(struct btrfs_list_filter));
+ set->total += BTRFS_LIST_NFILTERS_INCREASE;
+ *filter_set = set;
+ }
+
+ ASSERT(set->filters[set->nfilters].filter_func == NULL);
+
+ if (filter == BTRFS_LIST_FILTER_DELETED)
+ set->only_deleted = 1;
+
+ set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
+ set->filters[set->nfilters].data = data;
+ set->nfilters++;
+}
+
+static int filter_root(struct root_info *ri,
+ struct btrfs_list_filter_set *set)
+{
+ int i, ret;
+
+ if (!set)
+ return 1;
+
+ if (set->only_deleted && !ri->deleted)
+ return 0;
+
+ if (!set->only_deleted && ri->deleted)
+ return 0;
+
+ for (i = 0; i < set->nfilters; i++) {
+ if (!set->filters[i].filter_func)
+ break;
+ ret = set->filters[i].filter_func(ri, set->filters[i].data);
+ if (!ret)
+ return 0;
+ }
+ return 1;
+}
+
+static void filter_and_sort_subvol(struct root_lookup *all_subvols,
+ struct root_lookup *sort_tree,
+ struct btrfs_list_filter_set *filter_set,
+ struct btrfs_list_comparer_set *comp_set,
+ u64 top_id)
+{
+ struct rb_node *n;
+ struct root_info *entry;
+ int ret;
+
+ sort_tree->root.rb_node = NULL;
+
+ n = rb_last(&all_subvols->root);
+ while (n) {
+ entry = rb_entry(n, struct root_info, rb_node);
+
+ ret = resolve_root(all_subvols, entry, top_id);
+ if (ret == -ENOENT) {
+ if (entry->root_id != BTRFS_FS_TREE_OBJECTID) {
+ entry->full_path = strdup("DELETED");
+ entry->deleted = 1;
+ } else {
+ /*
+ * The full path is not supposed to be printed,
+ * but we don't want to print an empty string,
+ * in case it appears somewhere.
+ */
+ entry->full_path = strdup("TOPLEVEL");
+ entry->deleted = 0;
+ }
+ }
+ ret = filter_root(entry, filter_set);
+ if (ret)
+ sort_tree_insert(sort_tree, entry, comp_set);
+ n = rb_prev(n);
+ }
+}
+
+static int list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
+{
+ struct rb_node *n;
+
+ n = rb_first(&root_lookup->root);
+ while (n) {
+ struct root_info *entry;
+ int ret;
+ entry = rb_entry(n, struct root_info, rb_node);
+ ret = lookup_ino_path(fd, entry);
+ if (ret && ret != -ENOENT)
+ return ret;
+ n = rb_next(n);
+ }
+
+ return 0;
+}
+
+static void print_subvolume_column(struct root_info *subv,
+ enum btrfs_list_column_enum column)
+{
+ char tstr[256];
+ char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
+
+ ASSERT(0 <= column && column < BTRFS_LIST_ALL);
+
+ switch (column) {
+ case BTRFS_LIST_OBJECTID:
+ printf("%llu", subv->root_id);
+ break;
+ case BTRFS_LIST_GENERATION:
+ printf("%llu", subv->gen);
+ break;
+ case BTRFS_LIST_OGENERATION:
+ printf("%llu", subv->ogen);
+ break;
+ case BTRFS_LIST_PARENT:
+ printf("%llu", subv->ref_tree);
+ break;
+ case BTRFS_LIST_TOP_LEVEL:
+ printf("%llu", subv->top_id);
+ break;
+ case BTRFS_LIST_OTIME:
+ if (subv->otime) {
+ struct tm tm;
+
+ localtime_r(&subv->otime, &tm);
+ strftime(tstr, 256, "%Y-%m-%d %X", &tm);
+ } else
+ strcpy(tstr, "-");
+ printf("%s", tstr);
+ break;
+ case BTRFS_LIST_UUID:
+ if (uuid_is_null(subv->uuid))
+ strcpy(uuidparse, "-");
+ else
+ uuid_unparse(subv->uuid, uuidparse);
+ printf("%-36s", uuidparse);
+ break;
+ case BTRFS_LIST_PUUID:
+ if (uuid_is_null(subv->puuid))
+ strcpy(uuidparse, "-");
+ else
+ uuid_unparse(subv->puuid, uuidparse);
+ printf("%-36s", uuidparse);
+ break;
+ case BTRFS_LIST_RUUID:
+ if (uuid_is_null(subv->ruuid))
+ strcpy(uuidparse, "-");
+ else
+ uuid_unparse(subv->ruuid, uuidparse);
+ printf("%-36s", uuidparse);
+ break;
+ case BTRFS_LIST_PATH:
+ BUG_ON(!subv->full_path);
+ printf("%s", subv->full_path);
+ break;
+ default:
+ break;
+ }
+}
+
+static void print_one_subvol_info_raw(struct root_info *subv,
+ const 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_one_subvol_info_table(struct root_info *subv)
+{
+ int i;
+
+ for (i = 0; i < BTRFS_LIST_ALL; i++) {
+ if (!btrfs_list_columns[i].need_print)
+ continue;
+
+ print_subvolume_column(subv, i);
+
+ if (i != BTRFS_LIST_PATH)
+ printf("\t");
+
+ if (i == BTRFS_LIST_TOP_LEVEL)
+ printf("\t");
+ }
+ printf("\n");
+}
+
+static void print_one_subvol_info_default(struct root_info *subv)
+{
+ int i;
+
+ for (i = 0; i < BTRFS_LIST_ALL; i++) {
+ if (!btrfs_list_columns[i].need_print)
+ continue;
+
+ printf("%s ", btrfs_list_columns[i].name);
+ print_subvolume_column(subv, i);
+
+ if (i != BTRFS_LIST_PATH)
+ printf(" ");
+ }
+ printf("\n");
+}
+
+static void print_all_subvol_info_tab_head(void)
+{
+ int i;
+ int len;
+ char barrier[20];
+
+ for (i = 0; i < BTRFS_LIST_ALL; i++) {
+ if (btrfs_list_columns[i].need_print)
+ printf("%s\t", btrfs_list_columns[i].name);
+
+ if (i == BTRFS_LIST_ALL-1)
+ printf("\n");
+ }
+
+ for (i = 0; i < BTRFS_LIST_ALL; i++) {
+ memset(barrier, 0, sizeof(barrier));
+
+ if (btrfs_list_columns[i].need_print) {
+ len = strlen(btrfs_list_columns[i].name);
+ while (len--)
+ strcat(barrier, "-");
+
+ printf("%s\t", barrier);
+ }
+ if (i == BTRFS_LIST_ALL-1)
+ printf("\n");
+ }
+}
+
+static void print_all_subvol_info(struct root_lookup *sorted_tree,
+ enum btrfs_list_layout layout, const char *raw_prefix)
+{
+ struct rb_node *n;
+ struct root_info *entry;
+
+ if (layout == BTRFS_LIST_LAYOUT_TABLE)
+ print_all_subvol_info_tab_head();
+
+ n = rb_first(&sorted_tree->root);
+ while (n) {
+ entry = rb_entry(n, struct root_info, sort_node);
+
+ /* The toplevel subvolume is not listed by default */
+ if (entry->root_id == BTRFS_FS_TREE_OBJECTID)
+ goto next;
+
+ switch (layout) {
+ case BTRFS_LIST_LAYOUT_DEFAULT:
+ print_one_subvol_info_default(entry);
+ break;
+ case BTRFS_LIST_LAYOUT_TABLE:
+ print_one_subvol_info_table(entry);
+ break;
+ case BTRFS_LIST_LAYOUT_RAW:
+ print_one_subvol_info_raw(entry, raw_prefix);
+ break;
+ }
+next:
+ n = rb_next(n);
+ }
+}
+
+static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
+{
+ int ret;
+
+ ret = list_subvol_search(fd, root_lookup);
+ if (ret) {
+ error("can't perform the search: %m");
+ return ret;
+ }
+
+ /*
+ * now we have an rbtree full of root_info objects, but we need to fill
+ * in their path names within the subvol that is referencing each one.
+ */
+ ret = list_subvol_fill_paths(fd, root_lookup);
+ return ret;
+}
+
+int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
+ struct btrfs_list_comparer_set *comp_set,
+ enum btrfs_list_layout layout, int full_path,
+ const char *raw_prefix)
+{
+ struct root_lookup root_lookup;
+ struct root_lookup root_sort;
+ 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)
+ return ret;
+ filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
+ comp_set, top_id);
+
+ print_all_subvol_info(&root_sort, layout, raw_prefix);
+ rb_free_nodes(&root_lookup.root, free_root_info);
+
+ return 0;
+}
+
+static char *strdup_or_null(const char *s)
+{
+ if (!s)
+ return NULL;
+ return strdup(s);
+}
+
+int btrfs_get_toplevel_subvol(int fd, struct root_info *the_ri)
+{
+ int ret;
+ 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);
+ ri = rb_entry(rbn, struct root_info, rb_node);
+
+ if (ri->root_id != BTRFS_FS_TREE_OBJECTID)
+ return -ENOENT;
+
+ memcpy(the_ri, ri, offsetof(struct root_info, path));
+ the_ri->path = strdup_or_null("/");
+ the_ri->name = strdup_or_null("<FS_TREE>");
+ the_ri->full_path = strdup_or_null("/");
+ rb_free_nodes(&rl.root, free_root_info);
+
+ return ret;
+}
+
+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) ||
+ !uuid_compare(the_ri->uuid, ri->uuid)) {
+ 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);
+ }
+ rb_free_nodes(&rl.root, free_root_info);
+ 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,
+ char **cache_dir_name, u64 *cache_ino,
+ char **cache_full_name)
+{
+ u64 len = 0;
+ u64 disk_start = 0;
+ u64 disk_offset = 0;
+ u8 type;
+ int compressed = 0;
+ int flags = 0;