]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
Refactoring for device specific commands
[bcachefs-tools-debian] / cmd_device.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <libgen.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "cmds.h"
16 #include "libbcache.h"
17 #include "linux/bcache-ioctl.h"
18 #include "opts.h"
19 #include "tools-util.h"
20
21 /* This code belongs under show_fs */
22 #if 0
23
24 struct bcache_dev {
25         unsigned        nr;
26         const char      *name;
27
28         unsigned        has_metadata;
29         unsigned        has_data;
30         const char      *state;
31         unsigned        tier;
32
33         u64             bucket_size;
34         u64             first_bucket;
35         u64             nbuckets;
36
37         /* XXX: dirty != used, it doesn't count metadata */
38         u64             bytes_dirty;
39 };
40
41 static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
42 {
43         return (struct bcache_dev) {
44                 .nr             = nr,
45                 .name           = dev_name,
46
47                 .has_metadata   = read_file_u64(dir, "has_metadata"),
48                 .has_data       = read_file_u64(dir, "has_data"),
49                 .state          = read_file_str(dir, "state"),
50                 .tier           = read_file_u64(dir, "tier"),
51
52                 .bucket_size    = read_file_u64(dir, "bucket_size_bytes"),
53                 .first_bucket   = read_file_u64(dir, "first_bucket"),
54                 .nbuckets       = read_file_u64(dir, "nbuckets"),
55                 .bytes_dirty    = read_file_u64(dir, "dirty_bytes"),
56         };
57 }
58
59 static void show_dev(struct bcache_dev *dev)
60 {
61         u64 capacity = (dev->nbuckets - dev->first_bucket) *
62                 dev->bucket_size;
63         /*
64          * XXX: show fragmentation information, cached/dirty information
65          */
66
67         printf("Device %u (/dev/%s):\n"
68                "    Has metadata:\t%u\n"
69                "    Has dirty data:\t%u\n"
70                "    State:\t\t%s\n"
71                "    Tier:\t\t%u\n"
72                "    Size:\t\t%llu\n"
73                "    Used:\t\t%llu\n"
74                "    Free:\t\t%llu\n"
75                "    Use%%:\t\t%llu\n",
76                dev->nr, dev->name,
77                dev->has_metadata,
78                dev->has_data,
79                dev->state,
80                dev->tier,
81                capacity,
82                dev->bytes_dirty,
83                capacity - dev->bytes_dirty,
84                (dev->bytes_dirty * 100) / capacity);
85 }
86
87 int cmd_device_show(int argc, char *argv[])
88 {
89         int human_readable = 0;
90         NihOption opts[] = {
91         //      { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
92
93                 { 'h',  "human-readable",               N_("print sizes in powers of 1024 (e.g., 1023M)"),
94                         NULL, NULL,     &human_readable,        NULL},
95                 NIH_OPTION_LAST
96         };
97         char **args = bch_nih_init(argc, argv, opts);
98
99         if (argc != 2)
100                 die("Please supply a single device");
101
102         struct bcache_handle fs = bcache_fs_open(argv[1]);
103         struct dirent *entry;
104
105         struct bcache_dev devices[256];
106         unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
107
108         unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */
109         memset(tiers, 0, sizeof(tiers));
110
111         while ((entry = readdir(fs.sysfs))) {
112                 unsigned nr;
113                 int pos = 0;
114
115                 sscanf(entry->d_name, "cache%u%n", &nr, &pos);
116                 if (pos != strlen(entry->d_name))
117                         continue;
118
119                 char link[PATH_MAX];
120                 if (readlinkat(dirfd(fs.sysfs), entry->d_name,
121                                link, sizeof(link)) < 0)
122                         die("readlink error: %s\n", strerror(errno));
123
124                 char *dev_name = basename(dirname(link));
125
126                 int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
127
128                 devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
129                 tiers[devices[nr_devices].tier]++;
130                 nr_devices++;
131
132                 close(fd);
133         }
134
135         for (i = 0; i < BCH_TIER_MAX; i++)
136                 if (tiers[i])
137                         nr_active_tiers++;
138
139         /* Print out devices sorted by tier: */
140         bool first = true;
141
142         for (i = 0; i < BCH_TIER_MAX; i++) {
143                 if (!tiers[i])
144                         continue;
145
146                 if (nr_active_tiers > 1) {
147                         if (!first)
148                                 printf("\n");
149                         first = false;
150                         printf("Tier %u:\n\n", i);
151                 }
152
153                 for (j = 0; j < nr_devices; j++) {
154                         if (devices[j].tier != i)
155                                 continue;
156
157                         if (!first)
158                                 printf("\n");
159                         first = false;
160                         show_dev(&devices[j]);
161                 }
162         }
163
164         return 0;
165 }
166 #endif
167
168 static void disk_ioctl(const char *fs, const char *dev, int cmd, int flags)
169 {
170         struct bch_ioctl_disk i = { .flags = flags, .dev = (__u64) dev, };
171
172         xioctl(bcache_fs_open(fs).ioctl_fd, cmd, &i);
173 }
174
175 static void device_add_usage(void)
176 {
177         puts("bcache device add - add a device to an existing filesystem\n"
178              "Usage: bcache device add [OPTION]... filesystem device\n"
179              "\n"
180              "Options:\n"
181              "      --fs_size=size          Size of filesystem on device\n"
182              "      --bucket=size           Bucket size\n"
183              "      --discard               Enable discards\n"
184              "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
185              "  -f, --force                 Use device even if it appears to already be formatted\n"
186              "  -h, --help                  Display this help and exit\n"
187              "\n"
188              "Report bugs to <linux-bcache@vger.kernel.org>");
189 }
190
191 int cmd_device_add(int argc, char *argv[])
192 {
193         static const struct option longopts[] = {
194                 { "fs_size",            required_argument,      NULL, 'S' },
195                 { "bucket",             required_argument,      NULL, 'B' },
196                 { "discard",            no_argument,            NULL, 'D' },
197                 { "tier",               required_argument,      NULL, 't' },
198                 { "force",              no_argument,            NULL, 'f' },
199                 { NULL }
200         };
201         struct format_opts format_opts = format_opts_default();
202         struct dev_opts dev_opts = { 0 };
203         bool force = false;
204         int opt;
205
206         while ((opt = getopt_long(argc, argv, "t:fh",
207                                   longopts, NULL)) != -1)
208                 switch (opt) {
209                 case 'S':
210                         if (bch_strtoull_h(optarg, &dev_opts.size))
211                                 die("invalid filesystem size");
212
213                         dev_opts.size >>= 9;
214                         break;
215                 case 'B':
216                         dev_opts.bucket_size =
217                                 hatoi_validate(optarg, "bucket size");
218                         break;
219                 case 'D':
220                         dev_opts.discard = true;
221                         break;
222                 case 't':
223                         if (kstrtouint(optarg, 10, &dev_opts.tier) ||
224                             dev_opts.tier >= BCH_TIER_MAX)
225                                 die("invalid tier");
226                         break;
227                 case 'f':
228                         force = true;
229                         break;
230                 case 'h':
231                         device_add_usage();
232                         exit(EXIT_SUCCESS);
233                 }
234
235         if (argc - optind != 2)
236                 die("Please supply a filesystem and a device to add");
237
238         struct bcache_handle fs = bcache_fs_open(argv[optind]);
239
240         dev_opts.path = argv[optind + 1];
241         dev_opts.fd = open_for_format(dev_opts.path, force);
242
243         format_opts.block_size  =
244                 read_file_u64(fs.sysfs_fd, "block_size_bytes") >> 9;
245         format_opts.btree_node_size =
246                 read_file_u64(fs.sysfs_fd, "btree_node_size_bytes") >> 9;
247
248         struct bch_sb *sb = bcache_format(format_opts, &dev_opts, 1);
249         free(sb);
250         fsync(dev_opts.fd);
251         close(dev_opts.fd);
252
253         struct bch_ioctl_disk i = { .dev = (__u64) dev_opts.path, };
254
255         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &i);
256         return 0;
257 }
258
259 static void device_remove_usage(void)
260 {
261         puts("bcache device_remove - remove a device from a filesystem\n"
262              "Usage: bcache device remove filesystem device\n"
263              "\n"
264              "Options:\n"
265              "  -f, --force                 Force removal, even if some data\n"
266              "                              couldn't be migrated\n"
267              "      --force-metadata        Force removal, even if some metadata\n"
268              "                              couldn't be migrated\n"
269              "  -h, --help                  display this help and exit\n"
270              "Report bugs to <linux-bcache@vger.kernel.org>");
271         exit(EXIT_SUCCESS);
272 }
273
274 int cmd_device_remove(int argc, char *argv[])
275 {
276         static const struct option longopts[] = {
277                 { "force",              0, NULL, 'f' },
278                 { "force-metadata",     0, NULL, 'F' },
279                 { "help",               0, NULL, 'h' },
280                 { NULL }
281         };
282         int opt, flags = 0;
283
284         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
285                 switch (opt) {
286                 case 'f':
287                         flags |= BCH_FORCE_IF_DATA_LOST;
288                         break;
289                 case 'F':
290                         flags |= BCH_FORCE_IF_METADATA_LOST;
291                         break;
292                 case 'h':
293                         device_remove_usage();
294                 }
295
296         if (argc - optind != 2)
297                 die("Please supply a filesystem and at least one device to remove");
298
299         disk_ioctl(argv[optind], argv[optind + 1],
300                    BCH_IOCTL_DISK_REMOVE, flags);
301         return 0;
302 }
303
304 static void device_online_usage(void)
305 {
306         puts("bcache device online - readd a device to a running filesystem\n"
307              "Usage: bcache device online [OPTION]... filesystem device\n"
308              "\n"
309              "Options:\n"
310              "  -h, --help                  Display this help and exit\n"
311              "\n"
312              "Report bugs to <linux-bcache@vger.kernel.org>");
313 }
314
315 int cmd_device_online(int argc, char *argv[])
316 {
317         int opt;
318
319         while ((opt = getopt(argc, argv, "h")) != -1)
320                 switch (opt) {
321                 case 'h':
322                         device_online_usage();
323                         exit(EXIT_SUCCESS);
324                 }
325
326         if (argc - optind != 2)
327                 die("Please supply a filesystem and a device");
328
329         disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_ONLINE, 0);
330         return 0;
331 }
332
333 static void device_offline_usage(void)
334 {
335         puts("bcache device offline - take a device offline, without removing it\n"
336              "Usage: bcache device offline [OPTION]... filesystem device\n"
337              "\n"
338              "Options:\n"
339              "  -f, --force                 Force, if data redundancy will be degraded\n"
340              "  -h, --help                  Display this help and exit\n"
341              "\n"
342              "Report bugs to <linux-bcache@vger.kernel.org>");
343 }
344
345 int cmd_device_offline(int argc, char *argv[])
346 {
347         static const struct option longopts[] = {
348                 { "force",              0, NULL, 'f' },
349                 { NULL }
350         };
351         int opt, flags = 0;
352
353         while ((opt = getopt_long(argc, argv, "fh",
354                                   longopts, NULL)) != -1)
355                 switch (opt) {
356                 case 'f':
357                         flags |= BCH_FORCE_IF_DEGRADED;
358                         break;
359                 case 'h':
360                         device_offline_usage();
361                         exit(EXIT_SUCCESS);
362                 }
363
364         if (argc - optind != 2)
365                 die("Please supply a filesystem and a device");
366
367         disk_ioctl(argv[optind], argv[optind + 1],
368                    BCH_IOCTL_DISK_OFFLINE, flags);
369         return 0;
370 }
371
372 static void device_evacuate_usage(void)
373 {
374         puts("bcache device evacuate - move data off of a given device\n"
375              "Usage: bcache device evacuate [OPTION]... filesystem device\n"
376              "\n"
377              "Options:\n"
378              "  -h, --help                  Display this help and exit\n"
379              "\n"
380              "Report bugs to <linux-bcache@vger.kernel.org>");
381 }
382
383 int cmd_device_evacuate(int argc, char *argv[])
384 {
385         int opt;
386
387         while ((opt = getopt(argc, argv, "h")) != -1)
388                 switch (opt) {
389                 case 'h':
390                         device_evacuate_usage();
391                         exit(EXIT_SUCCESS);
392                 }
393
394         if (argc - optind != 2)
395                 die("Please supply a filesystem and a device");
396
397         disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_EVACUATE, 0);
398         return 0;
399 }
400
401 static void device_set_state_usage(void)
402 {
403         puts("bcache device set-state\n"
404              "Usage: bcache device set-state filesystem device new-state\n"
405              "\n"
406              "Options:\n"
407              "  -f, --force                 Force, if data redundancy will be degraded\n"
408              "  -h, --help                  display this help and exit\n"
409              "Report bugs to <linux-bcache@vger.kernel.org>");
410         exit(EXIT_SUCCESS);
411 }
412
413 int cmd_device_set_state(int argc, char *argv[])
414 {
415         static const struct option longopts[] = {
416                 { "force",                      0, NULL, 'f' },
417                 { "help",                       0, NULL, 'h' },
418                 { NULL }
419         };
420         int opt, flags = 0;
421
422         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
423                 switch (opt) {
424                 case 'f':
425                         flags |= BCH_FORCE_IF_DEGRADED;
426                         break;
427                 case 'h':
428                         device_set_state_usage();
429                 }
430
431         if (argc - optind != 3)
432                 die("Please supply a filesystem, device and state");
433
434         struct bcache_handle fs = bcache_fs_open(argv[optind]);
435
436         struct bch_ioctl_disk_set_state i = {
437                 .flags          = flags,
438                 .dev            = (__u64) argv[optind + 1],
439                 .new_state      = read_string_list_or_die(argv[optind + 2],
440                                                 bch_dev_state, "device state"),
441         };
442
443         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &i);
444         return 0;
445 }