]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/cmd_fs.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / c_src / cmd_fs.c
1 #include <getopt.h>
2 #include <stdio.h>
3 #include <sys/ioctl.h>
4
5 #include <uuid/uuid.h>
6
7 #include "linux/sort.h"
8 #include "linux/rcupdate.h"
9
10 #include "libbcachefs/bcachefs_ioctl.h"
11 #include "libbcachefs/buckets.h"
12 #include "libbcachefs/opts.h"
13 #include "libbcachefs/super-io.h"
14
15 #include "cmds.h"
16 #include "libbcachefs.h"
17
18 #include "linux/darray.h"
19
20 static void __dev_usage_type_to_text(struct printbuf *out,
21                                      enum bch_data_type type,
22                                      unsigned bucket_size,
23                                      u64 buckets, u64 sectors, u64 frag)
24 {
25         bch2_prt_data_type(out, type);
26         prt_char(out, ':');
27         prt_tab(out);
28
29         prt_units_u64(out, sectors << 9);
30         prt_tab_rjust(out);
31
32         prt_printf(out, "%llu", buckets);
33         prt_tab_rjust(out);
34
35         if (frag) {
36                 prt_units_u64(out, frag << 9);
37                 prt_tab_rjust(out);
38         }
39         prt_newline(out);
40 }
41
42 static void dev_usage_type_to_text(struct printbuf *out,
43                                    struct bch_ioctl_dev_usage_v2 *u,
44                                    enum bch_data_type type)
45 {
46         u64 sectors = 0;
47         switch (type) {
48         case BCH_DATA_free:
49         case BCH_DATA_need_discard:
50         case BCH_DATA_need_gc_gens:
51                 /* sectors are 0 for these types so calculate sectors for them */
52                 sectors = u->d[type].buckets * u->bucket_size;
53                 break;
54         default:
55                 sectors = u->d[type].sectors;
56         }
57
58         __dev_usage_type_to_text(out, type,
59                         u->bucket_size,
60                         u->d[type].buckets,
61                         sectors,
62                         u->d[type].fragmented);
63 }
64
65 static void dev_usage_to_text(struct printbuf *out,
66                               struct bchfs_handle fs,
67                               struct dev_name *d)
68 {
69         struct bch_ioctl_dev_usage_v2 *u = bchu_dev_usage(fs, d->idx);
70
71         prt_newline(out);
72         prt_printf(out, "%s (device %u):", d->label ?: "(no label)", d->idx);
73         prt_tab(out);
74         prt_str(out, d->dev ?: "(device not found)");
75         prt_tab_rjust(out);
76
77         prt_str(out, bch2_member_states[u->state]);
78         prt_tab_rjust(out);
79
80         prt_newline(out);
81
82         printbuf_indent_add(out, 2);
83         prt_tab(out);
84
85         prt_str(out, "data");
86         prt_tab_rjust(out);
87
88         prt_str(out, "buckets");
89         prt_tab_rjust(out);
90
91         prt_str(out, "fragmented");
92         prt_tab_rjust(out);
93
94         prt_newline(out);
95
96         for (unsigned i = 0; i < u->nr_data_types; i++)
97                 dev_usage_type_to_text(out, u, i);
98
99         prt_str(out, "capacity:");
100         prt_tab(out);
101
102         prt_units_u64(out, (u->nr_buckets * u->bucket_size) << 9);
103         prt_tab_rjust(out);
104         prt_printf(out, "%llu", u->nr_buckets);
105         prt_tab_rjust(out);
106
107         printbuf_indent_sub(out, 2);
108
109         prt_newline(out);
110         free(u);
111 }
112
113 static int dev_by_label_cmp(const void *_l, const void *_r)
114 {
115         const struct dev_name *l = _l, *r = _r;
116
117         return  (l->label && r->label
118                  ? strcmp(l->label, r->label) : 0) ?:
119                 (l->dev && r->dev
120                  ? strcmp(l->dev, r->dev) : 0) ?:
121                 cmp_int(l->idx, r->idx);
122 }
123
124 static struct dev_name *dev_idx_to_name(dev_names *dev_names, unsigned idx)
125 {
126         darray_for_each(*dev_names, dev)
127                 if (dev->idx == idx)
128                         return dev;
129         return NULL;
130 }
131
132 static void replicas_usage_to_text(struct printbuf *out,
133                                    const struct bch_replicas_usage *r,
134                                    dev_names *dev_names)
135 {
136         if (!r->sectors)
137                 return;
138
139         char devs[4096], *d = devs;
140         *d++ = '[';
141
142         unsigned durability = 0;
143
144         for (unsigned i = 0; i < r->r.nr_devs; i++) {
145                 unsigned dev_idx = r->r.devs[i];
146                 struct dev_name *dev = dev_idx_to_name(dev_names, dev_idx);
147
148                 durability += dev->durability;
149
150                 if (i)
151                         *d++ = ' ';
152
153                 d += dev && dev->dev
154                         ? sprintf(d, "%s", dev->dev)
155                         : sprintf(d, "%u", dev_idx);
156         }
157         *d++ = ']';
158         *d++ = '\0';
159
160         bch2_prt_data_type(out, r->r.data_type);
161         prt_char(out, ':');
162         prt_tab(out);
163
164         prt_printf(out, "%u/%u ", r->r.nr_required, r->r.nr_devs);
165         prt_tab(out);
166
167         prt_printf(out, "%u ", durability);
168         prt_tab(out);
169
170         prt_printf(out, "%s ", devs);
171         prt_tab(out);
172
173         prt_units_u64(out, r->sectors << 9);
174         prt_tab_rjust(out);
175         prt_newline(out);
176 }
177
178 #define for_each_usage_replica(_u, _r)                                  \
179         for (_r = (_u)->replicas;                                       \
180              _r != (void *) (_u)->replicas + (_u)->replica_entries_bytes;\
181              _r = replicas_usage_next(_r),                              \
182              BUG_ON((void *) _r > (void *) (_u)->replicas + (_u)->replica_entries_bytes))
183
184 static void fs_usage_to_text(struct printbuf *out, const char *path)
185 {
186         unsigned i;
187
188         struct bchfs_handle fs = bcache_fs_open(path);
189
190         dev_names dev_names = bchu_fs_get_devices(fs);
191
192         struct bch_ioctl_fs_usage *u = bchu_fs_usage(fs);
193
194         prt_str(out, "Filesystem: ");
195         pr_uuid(out, fs.uuid.b);
196         prt_newline(out);
197
198         printbuf_tabstops_reset(out);
199         printbuf_tabstop_push(out, 20);
200         printbuf_tabstop_push(out, 16);
201
202         prt_str(out, "Size:");
203         prt_tab(out);
204         prt_units_u64(out, u->capacity << 9);
205         prt_tab_rjust(out);
206         prt_newline(out);
207
208         prt_str(out, "Used:");
209         prt_tab(out);
210         prt_units_u64(out, u->used << 9);
211         prt_tab_rjust(out);
212         prt_newline(out);
213
214         prt_str(out, "Online reserved:");
215         prt_tab(out);
216         prt_units_u64(out, u->online_reserved << 9);
217         prt_tab_rjust(out);
218         prt_newline(out);
219
220         prt_newline(out);
221
222         printbuf_tabstops_reset(out);
223
224         printbuf_tabstop_push(out, 16);
225         prt_str(out, "Data type");
226         prt_tab(out);
227
228         printbuf_tabstop_push(out, 16);
229         prt_str(out, "Required/total");
230         prt_tab(out);
231
232         printbuf_tabstop_push(out, 14);
233         prt_str(out, "Durability");
234         prt_tab(out);
235
236         printbuf_tabstop_push(out, 14);
237         prt_str(out, "Devices");
238         prt_newline(out);
239
240         printbuf_tabstop_push(out, 14);
241
242         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
243                 if (!u->persistent_reserved[i])
244                         continue;
245
246                 prt_str(out, "reserved:");
247                 prt_tab(out);
248                 prt_printf(out, "%u/%u ", 1, i);
249                 prt_tab(out);
250                 prt_str(out, "[] ");
251                 prt_units_u64(out, u->persistent_reserved[i] << 9);
252                 prt_tab_rjust(out);
253                 prt_newline(out);
254         }
255
256         struct bch_replicas_usage *r;
257
258         for_each_usage_replica(u, r)
259                 if (r->r.data_type < BCH_DATA_user)
260                         replicas_usage_to_text(out, r, &dev_names);
261
262         for_each_usage_replica(u, r)
263                 if (r->r.data_type == BCH_DATA_user &&
264                     r->r.nr_required <= 1)
265                         replicas_usage_to_text(out, r, &dev_names);
266
267         for_each_usage_replica(u, r)
268                 if (r->r.data_type == BCH_DATA_user &&
269                     r->r.nr_required > 1)
270                         replicas_usage_to_text(out, r, &dev_names);
271
272         for_each_usage_replica(u, r)
273                 if (r->r.data_type > BCH_DATA_user)
274                         replicas_usage_to_text(out, r, &dev_names);
275
276         free(u);
277
278         sort(dev_names.data, dev_names.nr,
279              sizeof(dev_names.data[0]), dev_by_label_cmp, NULL);
280
281         printbuf_tabstops_reset(out);
282         printbuf_tabstop_push(out, 16);
283         printbuf_tabstop_push(out, 20);
284         printbuf_tabstop_push(out, 16);
285         printbuf_tabstop_push(out, 14);
286
287         darray_for_each(dev_names, dev)
288                 dev_usage_to_text(out, fs, dev);
289
290         darray_for_each(dev_names, dev) {
291                 free(dev->dev);
292                 free(dev->label);
293         }
294         darray_exit(&dev_names);
295
296         bcache_fs_close(fs);
297 }
298
299 static void fs_usage_usage(void)
300 {
301         puts("bcachefs fs usage - display detailed filesystem usage\n"
302              "Usage: bcachefs fs usage [OPTION]... <mountpoint>\n"
303              "\n"
304              "Options:\n"
305              "  -h, --human-readable              Human readable units\n"
306              "  -H, --help                        Display this help and exit\n"
307              "Report bugs to <linux-bcachefs@vger.kernel.org>");
308 }
309
310 int cmd_fs_usage(int argc, char *argv[])
311 {
312         static const struct option longopts[] = {
313                 { "help",               no_argument,            NULL, 'H' },
314                 { "human-readable",     no_argument,            NULL, 'h' },
315                 { NULL }
316         };
317         bool human_readable = false;
318         struct printbuf buf = PRINTBUF;
319         char *fs;
320         int opt;
321
322         while ((opt = getopt_long(argc, argv, "h",
323                                   longopts, NULL)) != -1)
324                 switch (opt) {
325                 case 'h':
326                         human_readable = true;
327                         break;
328                 case 'H':
329                         fs_usage_usage();
330                         exit(EXIT_SUCCESS);
331                 default:
332                         fs_usage_usage();
333                         exit(EXIT_FAILURE);
334                 }
335         args_shift(optind);
336
337         if (!argc) {
338                 printbuf_reset(&buf);
339                 buf.human_readable_units = human_readable;
340                 fs_usage_to_text(&buf, ".");
341                 printf("%s", buf.buf);
342         } else {
343                 while ((fs = arg_pop())) {
344                         printbuf_reset(&buf);
345                         buf.human_readable_units = human_readable;
346                         fs_usage_to_text(&buf, fs);
347                         printf("%s", buf.buf);
348                 }
349         }
350
351         printbuf_exit(&buf);
352         return 0;
353 }