7 #include "linux/sort.h"
8 #include "linux/rcupdate.h"
10 #include "libbcachefs/bcachefs_ioctl.h"
11 #include "libbcachefs/buckets.h"
12 #include "libbcachefs/opts.h"
13 #include "libbcachefs/super-io.h"
16 #include "libbcachefs.h"
18 #include "linux/darray.h"
20 static void __dev_usage_type_to_text(struct printbuf *out,
21 enum bch_data_type type,
23 u64 buckets, u64 sectors, u64 frag)
25 bch2_prt_data_type(out, type);
29 prt_units_u64(out, sectors << 9);
32 prt_printf(out, "%llu", buckets);
36 prt_units_u64(out, frag << 9);
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)
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;
55 sectors = u->d[type].sectors;
58 __dev_usage_type_to_text(out, type,
62 u->d[type].fragmented);
65 static void dev_usage_to_text(struct printbuf *out,
66 struct bchfs_handle fs,
69 struct bch_ioctl_dev_usage_v2 *u = bchu_dev_usage(fs, d->idx);
72 prt_printf(out, "%s (device %u):", d->label ?: "(no label)", d->idx);
74 prt_str(out, d->dev ?: "(device not found)");
77 prt_str(out, bch2_member_states[u->state]);
82 printbuf_indent_add(out, 2);
88 prt_str(out, "buckets");
91 prt_str(out, "fragmented");
96 for (unsigned i = 0; i < u->nr_data_types; i++)
97 dev_usage_type_to_text(out, u, i);
99 prt_str(out, "capacity:");
102 prt_units_u64(out, (u->nr_buckets * u->bucket_size) << 9);
104 prt_printf(out, "%llu", u->nr_buckets);
107 printbuf_indent_sub(out, 2);
113 static int dev_by_label_cmp(const void *_l, const void *_r)
115 const struct dev_name *l = _l, *r = _r;
117 return (l->label && r->label
118 ? strcmp(l->label, r->label) : 0) ?:
120 ? strcmp(l->dev, r->dev) : 0) ?:
121 cmp_int(l->idx, r->idx);
124 static struct dev_name *dev_idx_to_name(dev_names *dev_names, unsigned idx)
126 darray_for_each(*dev_names, dev)
132 static void replicas_usage_to_text(struct printbuf *out,
133 const struct bch_replicas_usage *r,
134 dev_names *dev_names)
139 char devs[4096], *d = devs;
142 unsigned durability = 0;
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);
148 durability += dev->durability;
154 ? sprintf(d, "%s", dev->dev)
155 : sprintf(d, "%u", dev_idx);
160 bch2_prt_data_type(out, r->r.data_type);
164 prt_printf(out, "%u/%u ", r->r.nr_required, r->r.nr_devs);
167 prt_printf(out, "%u ", durability);
170 prt_printf(out, "%s ", devs);
173 prt_units_u64(out, r->sectors << 9);
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))
184 static void fs_usage_to_text(struct printbuf *out, const char *path)
188 struct bchfs_handle fs = bcache_fs_open(path);
190 dev_names dev_names = bchu_fs_get_devices(fs);
192 struct bch_ioctl_fs_usage *u = bchu_fs_usage(fs);
194 prt_str(out, "Filesystem: ");
195 pr_uuid(out, fs.uuid.b);
198 printbuf_tabstops_reset(out);
199 printbuf_tabstop_push(out, 20);
200 printbuf_tabstop_push(out, 16);
202 prt_str(out, "Size:");
204 prt_units_u64(out, u->capacity << 9);
208 prt_str(out, "Used:");
210 prt_units_u64(out, u->used << 9);
214 prt_str(out, "Online reserved:");
216 prt_units_u64(out, u->online_reserved << 9);
222 printbuf_tabstops_reset(out);
224 printbuf_tabstop_push(out, 16);
225 prt_str(out, "Data type");
228 printbuf_tabstop_push(out, 16);
229 prt_str(out, "Required/total");
232 printbuf_tabstop_push(out, 14);
233 prt_str(out, "Durability");
236 printbuf_tabstop_push(out, 14);
237 prt_str(out, "Devices");
240 printbuf_tabstop_push(out, 14);
242 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
243 if (!u->persistent_reserved[i])
246 prt_str(out, "reserved:");
248 prt_printf(out, "%u/%u ", 1, i);
251 prt_units_u64(out, u->persistent_reserved[i] << 9);
256 struct bch_replicas_usage *r;
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);
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);
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);
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);
278 sort(dev_names.data, dev_names.nr,
279 sizeof(dev_names.data[0]), dev_by_label_cmp, NULL);
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);
287 darray_for_each(dev_names, dev)
288 dev_usage_to_text(out, fs, dev);
290 darray_for_each(dev_names, dev) {
294 darray_exit(&dev_names);
299 static void fs_usage_usage(void)
301 puts("bcachefs fs usage - display detailed filesystem usage\n"
302 "Usage: bcachefs fs usage [OPTION]... <mountpoint>\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>");
310 int cmd_fs_usage(int argc, char *argv[])
312 static const struct option longopts[] = {
313 { "help", no_argument, NULL, 'H' },
314 { "human-readable", no_argument, NULL, 'h' },
317 bool human_readable = false;
318 struct printbuf buf = PRINTBUF;
322 while ((opt = getopt_long(argc, argv, "h",
323 longopts, NULL)) != -1)
326 human_readable = true;
338 printbuf_reset(&buf);
339 buf.human_readable_units = human_readable;
340 fs_usage_to_text(&buf, ".");
341 printf("%s", buf.buf);
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);