]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fs.c
Add a mount.bcachefs tool
[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 "ccan/darray/darray.h"
8
9 #include "linux/sort.h"
10
11 #include "libbcachefs/bcachefs_ioctl.h"
12 #include "libbcachefs/opts.h"
13
14 #include "cmds.h"
15 #include "libbcachefs.h"
16
17 static void print_dev_usage(struct bchfs_handle fs,
18                             struct dev_name *d,
19                             enum units units)
20 {
21         struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, d->idx);
22         u64 available = u.nr_buckets;
23         unsigned i;
24
25         printf("\n");
26         printf_pad(20, "%s (device %u):", d->label ?: "(no label)", d->idx);
27         printf("%24s%12s\n", d->dev ?: "(device not found)", bch2_dev_state[u.state]);
28
29         printf("%-20s%12s%12s%12s\n",
30                "", "data", "buckets", "fragmented");
31
32         for (i = BCH_DATA_SB; i < BCH_DATA_NR; i++) {
33                 u64 frag = max((s64) u.buckets[i] * u.bucket_size -
34                                (s64) u.sectors[i], 0LL);
35
36                 printf_pad(20, "  %s:", bch2_data_types[i]);
37                 printf("%12s%12llu%12s\n",
38                        pr_units(u.sectors[i], units),
39                        u.buckets[i],
40                        pr_units(frag, units));
41
42                 if (i != BCH_DATA_CACHED)
43                         available -= u.buckets[i];
44         }
45
46         printf_pad(20, "  available:");
47         printf("%12s%12llu\n",
48                pr_units(available * u.bucket_size, units),
49                available);
50
51         printf_pad(20, "  capacity:");
52         printf("%12s%12llu\n",
53                pr_units(u.nr_buckets * u.bucket_size, units),
54                u.nr_buckets);
55 }
56
57 static int dev_by_label_cmp(const void *_l, const void *_r)
58 {
59         const struct dev_name *l = _l, *r = _r;
60
61         return  (l->label && r->label
62                  ? strcmp(l->label, r->label) : 0) ?:
63                 (l->dev && r->dev
64                  ? strcmp(l->dev, r->dev) : 0) ?:
65                 cmp_int(l->idx, r->idx);
66 }
67
68 static void print_fs_usage(const char *path, enum units units)
69 {
70         unsigned i;
71         char uuid[40];
72
73         struct bchfs_handle fs = bcache_fs_open(path);
74
75         struct dev_name *dev;
76         dev_names dev_names = bchu_fs_get_devices(fs);
77
78         struct bch_ioctl_fs_usage *u = bchu_fs_usage(fs);
79
80         uuid_unparse(fs.uuid.b, uuid);
81         printf("Filesystem %s:\n", uuid);
82
83         printf("%-20s%12s\n", "Size:", pr_units(u->capacity, units));
84         printf("%-20s%12s\n", "Used:", pr_units(u->used, units));
85
86         printf("%-20s%12s\n", "Online reserved:", pr_units(u->online_reserved, units));
87
88         printf("\n");
89         printf("%-16s%-16s%s\n", "Data type", "Required/total", "Devices");
90
91         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
92                 if (!u->persistent_reserved[i])
93                         continue;
94
95                 printf_pad(16, "%s: ", "reserved");
96                 printf_pad(16, "%u/%u ", 1, i);
97                 printf_pad(32, "[] ");
98                 printf("%s\n", pr_units(u->persistent_reserved[i], units));
99         }
100
101         struct bch_replicas_usage *r;
102
103         for (r = u->replicas;
104              r != (void *) u->replicas + u->replica_entries_bytes;
105              r = replicas_usage_next(r)) {
106                 BUG_ON((void *) r > (void *) u->replicas + u->replica_entries_bytes);
107
108                 if (!r->sectors)
109                         continue;
110
111                 char devs[4096], *d = devs;
112                 *d++ = '[';
113
114                 for (i = 0; i < r->r.nr_devs; i++) {
115                         unsigned dev_idx = r->r.devs[i];
116                         if (i)
117                                 *d++ = ' ';
118
119                         darray_foreach(dev, dev_names)
120                                 if (dev->idx == dev_idx)
121                                         goto found;
122                         d = NULL;
123 found:
124                         d += dev && dev->dev
125                                 ? sprintf(d, "%s", dev->dev)
126                                 : sprintf(d, "%u", dev_idx);
127                 }
128                 *d++ = ']';
129                 *d++ = '\0';
130
131                 printf_pad(16, "%s: ", bch2_data_types[r->r.data_type]);
132                 printf_pad(16, "%u/%u ", r->r.nr_required, r->r.nr_devs);
133                 printf_pad(32, "%s ", devs);
134                 printf(" %s\n", pr_units(r->sectors, units));
135         }
136
137         free(u);
138
139         sort(&darray_item(dev_names, 0), darray_size(dev_names),
140              sizeof(darray_item(dev_names, 0)), dev_by_label_cmp, NULL);
141
142         darray_foreach(dev, dev_names)
143                 print_dev_usage(fs, dev, units);
144
145         darray_foreach(dev, dev_names) {
146                 free(dev->dev);
147                 free(dev->label);
148         }
149         darray_free(dev_names);
150
151         bcache_fs_close(fs);
152 }
153
154 int cmd_fs_usage(int argc, char *argv[])
155 {
156         enum units units = BYTES;
157         char *fs;
158         int opt;
159
160         while ((opt = getopt(argc, argv, "h")) != -1)
161                 switch (opt) {
162                 case 'h':
163                         units = HUMAN_READABLE;
164                         break;
165                 }
166         args_shift(optind);
167
168         if (!argc) {
169                 print_fs_usage(".", units);
170         } else {
171                 while ((fs = arg_pop()))
172                         print_fs_usage(fs, units);
173         }
174
175         return 0;
176 }