]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fs.c
Add bcachefs fs usage
[bcachefs-tools-debian] / cmd_fs.c
1
2 #include <stdio.h>
3 #include <sys/ioctl.h>
4
5 #include <uuid/uuid.h>
6
7 #include "libbcachefs/bcachefs_ioctl.h"
8 #include "libbcachefs/opts.h"
9
10 #include "cmds.h"
11
12 static inline int printf_pad(unsigned pad, const char * fmt, ...)
13 {
14        va_list args;
15        int ret;
16
17        va_start(args, fmt);
18        ret = vprintf(fmt, args);
19        va_end(args);
20
21        while (ret++ < pad)
22                putchar(' ');
23
24        return ret;
25 }
26
27 static void print_fs_usage(const char *path, enum units units)
28 {
29         unsigned i, j, nr_devices = 4;
30         struct bcache_handle fs = bcache_fs_open(path);
31         struct bch_ioctl_usage *u = NULL;
32         char uuid[40];
33
34         while (1) {
35                 u = xrealloc(u, sizeof(*u) + sizeof(u->devs[0]) * nr_devices);
36                 u->nr_devices = nr_devices;
37
38                 if (!ioctl(fs.ioctl_fd, BCH_IOCTL_USAGE, u))
39                         break;
40                 if (errno != ENOSPC)
41                         die("BCH_IOCTL_USAGE error: %m");
42                 nr_devices *= 2;
43         }
44
45         uuid_unparse(fs.uuid.b, uuid);
46         printf("Filesystem %s:\n", uuid);
47
48         printf("%-20s%12s\n", "Size:", pr_units(u->fs.capacity, units));
49         printf("%-20s%12s\n", "Used:", pr_units(u->fs.used, units));
50
51         printf("%-20s%12s%12s%12s%12s\n",
52                "By replicas:", "1x", "2x", "3x", "4x");
53
54         for (j = BCH_DATA_BTREE; j < BCH_DATA_NR; j++) {
55                 printf_pad(20, "  %s:", bch2_data_types[j]);
56
57                 for (i = 0; i < BCH_REPLICAS_MAX; i++)
58                         printf("%12s", pr_units(u->fs.sectors[j][i], units));
59                 printf("\n");
60         }
61
62         printf_pad(20, "  %s:", "reserved");
63         for (i = 0; i < BCH_REPLICAS_MAX; i++)
64                 printf("%12s", pr_units(u->fs.persistent_reserved[i], units));
65         printf("\n");
66
67         printf("%-20s%12s\n", "  online reserved:", pr_units(u->fs.online_reserved, units));
68
69         for (i = 0; i < u->nr_devices; i++) {
70                 struct bch_ioctl_dev_usage *d = u->devs + i;
71                 char *name = NULL;
72
73                 if (!d->alive)
74                         continue;
75
76                 printf("\n");
77                 printf_pad(20, "Device %u usage:", i);
78                 name = !d->dev ? strdup("(offline)")
79                         : dev_to_path(d->dev)
80                         ?: strdup("(device not found)");
81
82                 printf("%24s%12s\n", name, bch2_dev_state[d->state]);
83                 free(name);
84
85                 printf("%-20s%12s%12s%12s\n",
86                        "", "data", "buckets", "fragmented");
87
88                 for (j = BCH_DATA_SB; j < BCH_DATA_NR; j++) {
89                         u64 frag = max((s64) d->buckets[j] * d->bucket_size -
90                                        (s64) d->sectors[j], 0LL);
91
92                         printf_pad(20, "  %s:", bch2_data_types[j]);
93                         printf("%12s%12llu%12s\n",
94                                pr_units(d->sectors[j], units),
95                                d->buckets[j],
96                                pr_units(frag, units));
97                 }
98         }
99
100         free(u);
101         bcache_fs_close(fs);
102 }
103
104 int cmd_fs_usage(int argc, char *argv[])
105 {
106         enum units units = BYTES;
107         unsigned i;
108         int opt;
109
110         while ((opt = getopt(argc, argv, "h")) != -1)
111                 switch (opt) {
112                 case 'h':
113                         units = HUMAN_READABLE;
114                         break;
115                 }
116
117         if (argc - optind < 1) {
118                 print_fs_usage(".", units);
119         } else {
120                 for (i = optind; i < argc; i++)
121                         print_fs_usage(argv[i], units);
122         }
123
124         return 0;
125 }