Btrfs-progs: add show subcommand to subvol cli
[platform/upstream/btrfs-progs.git] / btrfs-list.h
1 /*
2  * Copyright (C) 2012 Fujitsu.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include "kerncompat.h"
20
21 #define BTRFS_LIST_LAYOUT_DEFAULT       0
22 #define BTRFS_LIST_LAYOUT_TABLE 1
23 #define BTRFS_LIST_LAYOUT_RAW           2
24
25 /*
26  * one of these for each root we find.
27  */
28 struct root_info {
29         struct rb_node rb_node;
30         struct rb_node sort_node;
31
32         /* this root's id */
33         u64 root_id;
34
35         /* equal the offset of the root's key */
36         u64 root_offset;
37
38         /* flags of the root */
39         u64 flags;
40
41         /* the id of the root that references this one */
42         u64 ref_tree;
43
44         /* the dir id we're in from ref_tree */
45         u64 dir_id;
46
47         u64 top_id;
48
49         /* generation when the root is created or last updated */
50         u64 gen;
51
52         /* creation generation of this root in sec*/
53         u64 ogen;
54
55         /* creation time of this root in sec*/
56         time_t otime;
57
58         u8 uuid[BTRFS_UUID_SIZE];
59         u8 puuid[BTRFS_UUID_SIZE];
60
61         /* path from the subvol we live in to this root, including the
62          * root's name.  This is null until we do the extra lookup ioctl.
63          */
64         char *path;
65
66         /* the name of this root in the directory it lives in */
67         char *name;
68
69         char *full_path;
70 };
71
72 typedef int (*btrfs_list_filter_func)(struct root_info *, u64);
73 typedef int (*btrfs_list_comp_func)(struct root_info *, struct root_info *,
74                                     int);
75
76 struct btrfs_list_filter {
77         btrfs_list_filter_func filter_func;
78         u64 data;
79 };
80
81 struct btrfs_list_comparer {
82         btrfs_list_comp_func comp_func;
83         int is_descending;
84 };
85
86 struct btrfs_list_filter_set {
87         int total;
88         int nfilters;
89         struct btrfs_list_filter filters[0];
90 };
91
92 struct btrfs_list_comparer_set {
93         int total;
94         int ncomps;
95         struct btrfs_list_comparer comps[0];
96 };
97
98 enum btrfs_list_column_enum {
99         BTRFS_LIST_OBJECTID,
100         BTRFS_LIST_GENERATION,
101         BTRFS_LIST_OGENERATION,
102         BTRFS_LIST_PARENT,
103         BTRFS_LIST_TOP_LEVEL,
104         BTRFS_LIST_OTIME,
105         BTRFS_LIST_PUUID,
106         BTRFS_LIST_UUID,
107         BTRFS_LIST_PATH,
108         BTRFS_LIST_ALL,
109 };
110
111 enum btrfs_list_filter_enum {
112         BTRFS_LIST_FILTER_ROOTID,
113         BTRFS_LIST_FILTER_SNAPSHOT_ONLY,
114         BTRFS_LIST_FILTER_FLAGS,
115         BTRFS_LIST_FILTER_GEN,
116         BTRFS_LIST_FILTER_GEN_EQUAL     =       BTRFS_LIST_FILTER_GEN,
117         BTRFS_LIST_FILTER_GEN_LESS,
118         BTRFS_LIST_FILTER_GEN_MORE,
119         BTRFS_LIST_FILTER_CGEN,
120         BTRFS_LIST_FILTER_CGEN_EQUAL    =       BTRFS_LIST_FILTER_CGEN,
121         BTRFS_LIST_FILTER_CGEN_LESS,
122         BTRFS_LIST_FILTER_CGEN_MORE,
123         BTRFS_LIST_FILTER_TOPID_EQUAL,
124         BTRFS_LIST_FILTER_FULL_PATH,
125         BTRFS_LIST_FILTER_BY_PARENT,
126         BTRFS_LIST_FILTER_MAX,
127 };
128
129 enum btrfs_list_comp_enum {
130         BTRFS_LIST_COMP_ROOTID,
131         BTRFS_LIST_COMP_OGEN,
132         BTRFS_LIST_COMP_GEN,
133         BTRFS_LIST_COMP_PATH,
134         BTRFS_LIST_COMP_MAX,
135 };
136
137 int btrfs_list_parse_sort_string(char *optarg,
138                                  struct btrfs_list_comparer_set **comps);
139 int btrfs_list_parse_filter_string(char *optarg,
140                                    struct btrfs_list_filter_set **filters,
141                                    enum btrfs_list_filter_enum type);
142 void btrfs_list_setup_print_column(enum btrfs_list_column_enum column);
143 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void);
144 void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set);
145 int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
146                             enum btrfs_list_filter_enum filter, u64 data);
147 struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void);
148 void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set);
149 int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
150                               enum btrfs_list_comp_enum comparer,
151                               int is_descending);
152
153 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
154                        struct btrfs_list_comparer_set *comp_set,
155                        int is_tab_result, int full_path, char *raw_prefix);
156 int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen);
157 int btrfs_list_get_default_subvolume(int fd, u64 *default_id);
158 char *btrfs_list_path_for_root(int fd, u64 root);
159 u64 btrfs_list_get_path_rootid(int fd);
160 int btrfs_get_subvol(int fd, struct root_info *the_ri);