7 #include "linux/sort.h"
9 #include "libbcachefs/bcachefs_ioctl.h"
10 #include "libbcachefs/darray.h"
11 #include "libbcachefs/opts.h"
12 #include "libbcachefs/super-io.h"
15 #include "libbcachefs.h"
17 static void __dev_usage_type_to_text(struct printbuf *out,
20 u64 buckets, u64 sectors, u64 frag)
22 prt_printf(out, "%s:", type);
25 prt_units_u64(out, sectors << 9);
28 prt_printf(out, "%llu", buckets);
32 prt_units_u64(out, frag << 9);
38 static void dev_usage_type_to_text(struct printbuf *out,
39 struct bch_ioctl_dev_usage_v2 *u,
40 enum bch_data_type type)
45 case BCH_DATA_need_discard:
46 case BCH_DATA_need_gc_gens:
47 /* sectors are 0 for these types so calculate sectors for them */
48 sectors = u->d[type].buckets * u->bucket_size;
51 sectors = u->d[type].sectors;
54 __dev_usage_type_to_text(out, bch2_data_types[type],
58 u->d[type].fragmented);
61 static void dev_usage_to_text(struct printbuf *out,
62 struct bchfs_handle fs,
65 struct bch_ioctl_dev_usage_v2 *u = bchu_dev_usage(fs, d->idx);
68 prt_printf(out, "%s (device %u):", d->label ?: "(no label)", d->idx);
70 prt_str(out, d->dev ?: "(device not found)");
73 prt_str(out, bch2_member_states[u->state]);
78 printbuf_indent_add(out, 2);
84 prt_str(out, "buckets");
87 prt_str(out, "fragmented");
92 for (unsigned i = 0; i < u->nr_data_types; i++)
93 dev_usage_type_to_text(out, u, i);
95 prt_str(out, "capacity:");
98 prt_units_u64(out, (u->nr_buckets * u->bucket_size) << 9);
100 prt_printf(out, "%llu", u->nr_buckets);
103 printbuf_indent_sub(out, 2);
109 static int dev_by_label_cmp(const void *_l, const void *_r)
111 const struct dev_name *l = _l, *r = _r;
113 return (l->label && r->label
114 ? strcmp(l->label, r->label) : 0) ?:
116 ? strcmp(l->dev, r->dev) : 0) ?:
117 cmp_int(l->idx, r->idx);
120 static struct dev_name *dev_idx_to_name(dev_names *dev_names, unsigned idx)
122 darray_for_each(*dev_names, dev)
128 static void replicas_usage_to_text(struct printbuf *out,
129 const struct bch_replicas_usage *r,
130 dev_names *dev_names)
135 char devs[4096], *d = devs;
138 unsigned durability = 0;
140 for (unsigned i = 0; i < r->r.nr_devs; i++) {
141 unsigned dev_idx = r->r.devs[i];
142 struct dev_name *dev = dev_idx_to_name(dev_names, dev_idx);
144 durability += dev->durability;
150 ? sprintf(d, "%s", dev->dev)
151 : sprintf(d, "%u", dev_idx);
156 prt_printf(out, "%s: ", bch2_data_types[r->r.data_type]);
159 prt_printf(out, "%u/%u ", r->r.nr_required, r->r.nr_devs);
162 prt_printf(out, "%u ", durability);
165 prt_printf(out, "%s ", devs);
168 prt_units_u64(out, r->sectors << 9);
173 #define for_each_usage_replica(_u, _r) \
174 for (_r = (_u)->replicas; \
175 _r != (void *) (_u)->replicas + (_u)->replica_entries_bytes;\
176 _r = replicas_usage_next(_r), \
177 BUG_ON((void *) _r > (void *) (_u)->replicas + (_u)->replica_entries_bytes))
179 static void fs_usage_to_text(struct printbuf *out, const char *path)
183 struct bchfs_handle fs = bcache_fs_open(path);
185 dev_names dev_names = bchu_fs_get_devices(fs);
187 struct bch_ioctl_fs_usage *u = bchu_fs_usage(fs);
189 prt_str(out, "Filesystem: ");
190 pr_uuid(out, fs.uuid.b);
193 printbuf_tabstops_reset(out);
194 printbuf_tabstop_push(out, 20);
195 printbuf_tabstop_push(out, 16);
197 prt_str(out, "Size:");
199 prt_units_u64(out, u->capacity << 9);
203 prt_str(out, "Used:");
205 prt_units_u64(out, u->used << 9);
209 prt_str(out, "Online reserved:");
211 prt_units_u64(out, u->online_reserved << 9);
217 printbuf_tabstops_reset(out);
219 printbuf_tabstop_push(out, 16);
220 prt_str(out, "Data type");
223 printbuf_tabstop_push(out, 16);
224 prt_str(out, "Required/total");
227 printbuf_tabstop_push(out, 14);
228 prt_str(out, "Durability");
231 printbuf_tabstop_push(out, 14);
232 prt_str(out, "Devices");
235 printbuf_tabstop_push(out, 14);
237 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
238 if (!u->persistent_reserved[i])
241 prt_str(out, "reserved:");
243 prt_printf(out, "%u/%u ", 1, i);
246 prt_units_u64(out, u->persistent_reserved[i] << 9);
251 struct bch_replicas_usage *r;
253 for_each_usage_replica(u, r)
254 if (r->r.data_type < BCH_DATA_user)
255 replicas_usage_to_text(out, r, &dev_names);
257 for_each_usage_replica(u, r)
258 if (r->r.data_type == BCH_DATA_user &&
259 r->r.nr_required <= 1)
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 replicas_usage_to_text(out, r, &dev_names);
273 sort(dev_names.data, dev_names.nr,
274 sizeof(dev_names.data[0]), dev_by_label_cmp, NULL);
276 printbuf_tabstops_reset(out);
277 printbuf_tabstop_push(out, 16);
278 printbuf_tabstop_push(out, 20);
279 printbuf_tabstop_push(out, 16);
280 printbuf_tabstop_push(out, 14);
282 darray_for_each(dev_names, dev)
283 dev_usage_to_text(out, fs, dev);
285 darray_for_each(dev_names, dev) {
289 darray_exit(&dev_names);
294 static void fs_usage_usage(void)
296 puts("bcachefs fs usage - display detailed filesystem usage\n"
297 "Usage: bcachefs fs usage [OPTION]... <mountpoint>\n"
300 " -h, --human-readable Human readable units\n"
301 " -H, --help Display this help and exit\n"
302 "Report bugs to <linux-bcachefs@vger.kernel.org>");
305 int cmd_fs_usage(int argc, char *argv[])
307 static const struct option longopts[] = {
308 { "help", no_argument, NULL, 'H' },
309 { "human-readable", no_argument, NULL, 'h' },
312 bool human_readable = false;
313 struct printbuf buf = PRINTBUF;
317 while ((opt = getopt_long(argc, argv, "h",
318 longopts, NULL)) != -1)
321 human_readable = true;
333 printbuf_reset(&buf);
334 buf.human_readable_units = human_readable;
335 fs_usage_to_text(&buf, ".");
336 printf("%s", buf.buf);
338 while ((fs = arg_pop())) {
339 printbuf_reset(&buf);
340 buf.human_readable_units = human_readable;
341 fs_usage_to_text(&buf, fs);
342 printf("%s", buf.buf);