]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
1914629bbc234fafafe35e11e4a6cd50b01dcb5d
[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 "libbcachefs/bcachefs.h"
16 #include "libbcachefs/bcachefs_ioctl.h"
17 #include "libbcachefs/errcode.h"
18 #include "libbcachefs/journal.h"
19 #include "libbcachefs/sb-members.h"
20 #include "libbcachefs/super-io.h"
21 #include "cmds.h"
22 #include "libbcachefs.h"
23 #include "libbcachefs/opts.h"
24 #include "tools-util.h"
25
26 int device_usage(void)
27 {
28        puts("bcachefs device - manage devices within a running filesystem\n"
29             "Usage: bcachefs device <CMD> [OPTION]\n"
30             "\n"
31             "Commands:\n"
32             "  add                     add a new device to an existing filesystem\n"
33             "  remove                  remove a device from an existing filesystem\n"
34             "  online                  re-add an existing member to a filesystem\n"
35             "  offline                 take a device offline, without removing it\n"
36             "  evacuate                migrate data off a specific device\n"
37             "  set-state               mark a device as failed\n"
38             "  resize                  resize filesystem on a device\n"
39             "  resize-journal          resize journal on a device\n"
40             "\n"
41             "Report bugs to <linux-bcachefs@vger.kernel.org>");
42        return 0;
43 }
44
45 static void device_add_usage(void)
46 {
47         puts("bcachefs device add - add a device to an existing filesystem\n"
48              "Usage: bcachefs device add [OPTION]... filesystem device\n"
49              "\n"
50              "Options:\n"
51              "  -S, --fs_size=size          Size of filesystem on device\n"
52              "  -B, --bucket=size           Bucket size\n"
53              "  -D, --discard               Enable discards\n"
54              "  -l, --label=label           Disk label\n"
55              "  -f, --force                 Use device even if it appears to already be formatted\n"
56              "  -h, --help                  Display this help and exit\n"
57              "\n"
58              "Report bugs to <linux-bcachefs@vger.kernel.org>");
59 }
60
61 int cmd_device_add(int argc, char *argv[])
62 {
63         static const struct option longopts[] = {
64                 { "fs_size",            required_argument,      NULL, 'S' },
65                 { "bucket",             required_argument,      NULL, 'B' },
66                 { "discard",            no_argument,            NULL, 'D' },
67                 { "label",              required_argument,      NULL, 'l' },
68                 { "force",              no_argument,            NULL, 'f' },
69                 { "help",               no_argument,            NULL, 'h' },
70                 { NULL }
71         };
72         struct format_opts format_opts  = format_opts_default();
73         struct dev_opts dev_opts        = dev_opts_default();
74         bool force = false;
75         int opt;
76
77         while ((opt = getopt_long(argc, argv, "t:fh",
78                                   longopts, NULL)) != -1)
79                 switch (opt) {
80                 case 'S':
81                         if (bch2_strtoull_h(optarg, &dev_opts.size))
82                                 die("invalid filesystem size");
83                         break;
84                 case 'B':
85                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
86                                 die("bad bucket_size %s", optarg);
87                         break;
88                 case 'D':
89                         dev_opts.discard = true;
90                         break;
91                 case 'l':
92                         dev_opts.label = strdup(optarg);
93                         break;
94                 case 'f':
95                         force = true;
96                         break;
97                 case 'h':
98                         device_add_usage();
99                         exit(EXIT_SUCCESS);
100                 }
101         args_shift(optind);
102
103         char *fs_path = arg_pop();
104         if (!fs_path)
105                 die("Please supply a filesystem");
106
107         dev_opts.path = arg_pop();
108         if (!dev_opts.path)
109                 die("Please supply a device");
110
111         if (argc)
112                 die("too many arguments");
113
114         struct bchfs_handle fs = bcache_fs_open(fs_path);
115
116         dev_opts.fd = open_for_format(dev_opts.path, force);
117
118         struct bch_opt_strs fs_opt_strs;
119         memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
120
121         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
122
123         opt_set(fs_opts, block_size,
124                 read_file_u64(fs.sysfs_fd, "options/block_size"));
125         opt_set(fs_opts, btree_node_size,
126                 read_file_u64(fs.sysfs_fd, "options/btree_node_size"));
127
128         struct bch_sb *sb = bch2_format(fs_opt_strs,
129                                         fs_opts,
130                                         format_opts,
131                                         &dev_opts, 1);
132         free(sb);
133         fsync(dev_opts.fd);
134         close(dev_opts.fd);
135
136         bchu_disk_add(fs, dev_opts.path);
137         return 0;
138 }
139
140 static void device_remove_usage(void)
141 {
142         puts("bcachefs device_remove - remove a device from a filesystem\n"
143              "Usage:\n"
144              "  bcachefs device remove <device>|<devid> <path>\n"
145              "\n"
146              "Options:\n"
147              "  -f, --force                 Force removal, even if some data\n"
148              "                              couldn't be migrated\n"
149              "  -F, --force-metadata        Force removal, even if some metadata\n"
150              "                              couldn't be migrated\n"
151              "  -h, --help                  display this help and exit\n"
152              "Report bugs to <linux-bcachefs@vger.kernel.org>");
153         exit(EXIT_SUCCESS);
154 }
155
156 int cmd_device_remove(int argc, char *argv[])
157 {
158         static const struct option longopts[] = {
159                 { "by-id",              0, NULL, 'i' },
160                 { "force",              0, NULL, 'f' },
161                 { "force-metadata",     0, NULL, 'F' },
162                 { "help",               0, NULL, 'h' },
163                 { NULL }
164         };
165         struct bchfs_handle fs;
166         bool by_id = false;
167         int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
168
169         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
170                 switch (opt) {
171                 case 'f':
172                         flags |= BCH_FORCE_IF_DATA_LOST;
173                         break;
174                 case 'F':
175                         flags |= BCH_FORCE_IF_METADATA_LOST;
176                         break;
177                 case 'h':
178                         device_remove_usage();
179                 }
180         args_shift(optind);
181
182         char *dev_str = arg_pop();
183         if (!dev_str)
184                 die("Please supply a device");
185
186         char *end;
187         dev_idx = strtoul(dev_str, &end, 10);
188         if (*dev_str && !*end)
189                 by_id = true;
190
191         char *fs_path = arg_pop();
192         if (fs_path) {
193                 fs = bcache_fs_open(fs_path);
194
195                 if (!by_id) {
196                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
197                         if (dev_idx < 0)
198                                 die("%s does not seem to be a member of %s",
199                                     dev_str, fs_path);
200                 }
201         } else if (!by_id) {
202                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
203         } else {
204                 die("Filesystem path required when specifying device by id");
205         }
206
207         bchu_disk_remove(fs, dev_idx, flags);
208         return 0;
209 }
210
211 static void device_online_usage(void)
212 {
213         puts("bcachefs device online - readd a device to a running filesystem\n"
214              "Usage: bcachefs device online [OPTION]... device\n"
215              "\n"
216              "Options:\n"
217              "  -h, --help                  Display this help and exit\n"
218              "\n"
219              "Report bugs to <linux-bcachefs@vger.kernel.org>");
220 }
221
222 int cmd_device_online(int argc, char *argv[])
223 {
224         int opt;
225
226         while ((opt = getopt(argc, argv, "h")) != -1)
227                 switch (opt) {
228                 case 'h':
229                         device_online_usage();
230                         exit(EXIT_SUCCESS);
231                 }
232         args_shift(optind);
233
234         char *dev = arg_pop();
235         if (!dev)
236                 die("Please supply a device");
237
238         if (argc)
239                 die("too many arguments");
240
241         int dev_idx;
242         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
243         bchu_disk_online(fs, dev);
244         return 0;
245 }
246
247 static void device_offline_usage(void)
248 {
249         puts("bcachefs device offline - take a device offline, without removing it\n"
250              "Usage: bcachefs device offline [OPTION]... device\n"
251              "\n"
252              "Options:\n"
253              "  -f, --force                 Force, if data redundancy will be degraded\n"
254              "  -h, --help                  Display this help and exit\n"
255              "\n"
256              "Report bugs to <linux-bcachefs@vger.kernel.org>");
257 }
258
259 int cmd_device_offline(int argc, char *argv[])
260 {
261         static const struct option longopts[] = {
262                 { "force",              0, NULL, 'f' },
263                 { NULL }
264         };
265         int opt, flags = 0;
266
267         while ((opt = getopt_long(argc, argv, "fh",
268                                   longopts, NULL)) != -1)
269                 switch (opt) {
270                 case 'f':
271                         flags |= BCH_FORCE_IF_DEGRADED;
272                         break;
273                 case 'h':
274                         device_offline_usage();
275                         exit(EXIT_SUCCESS);
276                 }
277         args_shift(optind);
278
279         char *dev = arg_pop();
280         if (!dev)
281                 die("Please supply a device");
282
283         if (argc)
284                 die("too many arguments");
285
286         int dev_idx;
287         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
288         bchu_disk_offline(fs, dev_idx, flags);
289         return 0;
290 }
291
292 static void device_evacuate_usage(void)
293 {
294         puts("bcachefs device evacuate - move data off of a given device\n"
295              "Usage: bcachefs device evacuate [OPTION]... device\n"
296              "\n"
297              "Options:\n"
298              "  -h, --help                  Display this help and exit\n"
299              "\n"
300              "Report bugs to <linux-bcachefs@vger.kernel.org>");
301 }
302
303 int cmd_device_evacuate(int argc, char *argv[])
304 {
305         int opt;
306
307         while ((opt = getopt(argc, argv, "h")) != -1)
308                 switch (opt) {
309                 case 'h':
310                         device_evacuate_usage();
311                         exit(EXIT_SUCCESS);
312                 }
313         args_shift(optind);
314
315         char *dev_path = arg_pop();
316         if (!dev_path)
317                 die("Please supply a device");
318
319         if (argc)
320                 die("too many arguments");
321
322         int dev_idx;
323         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
324
325         struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx);
326
327         if (u.state == BCH_MEMBER_STATE_rw) {
328                 printf("Setting %s readonly\n", dev_path);
329                 bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0);
330         }
331
332         return bchu_data(fs, (struct bch_ioctl_data) {
333                 .op             = BCH_DATA_OP_MIGRATE,
334                 .start_btree    = 0,
335                 .start_pos      = POS_MIN,
336                 .end_btree      = BTREE_ID_NR,
337                 .end_pos        = POS_MAX,
338                 .migrate.dev    = dev_idx,
339         });
340 }
341
342 static void device_set_state_usage(void)
343 {
344         puts("bcachefs device set-state\n"
345              "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
346              "\n"
347              "<new-state>: one of rw, ro, failed or spare\n"
348              "<path>: path to mounted filesystem, optional unless specifying device by id\n"
349              "\n"
350              "Options:\n"
351              "  -f, --force                 Force, if data redundancy will be degraded\n"
352              "      --force-if-data-lost    Force, if data will be lost\n"
353              "  -o, --offline               Set state of an offline device\n"
354              "  -h, --help                  display this help and exit\n"
355              "Report bugs to <linux-bcachefs@vger.kernel.org>");
356         exit(EXIT_SUCCESS);
357 }
358
359 int cmd_device_set_state(int argc, char *argv[])
360 {
361         static const struct option longopts[] = {
362                 { "force",                      0, NULL, 'f' },
363                 { "force-if-data-lost",         0, NULL, 'F' },
364                 { "offline",                    0, NULL, 'o' },
365                 { "help",                       0, NULL, 'h' },
366                 { NULL }
367         };
368         struct bchfs_handle fs;
369         bool by_id = false;
370         int opt, flags = 0, dev_idx;
371         bool offline = false;
372
373         while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
374                 switch (opt) {
375                 case 'f':
376                         flags |= BCH_FORCE_IF_DEGRADED;
377                         break;
378                 case 'F':
379                         flags |= BCH_FORCE_IF_DEGRADED;
380                         flags |= BCH_FORCE_IF_LOST;
381                         break;
382                 case 'o':
383                         offline = true;
384                         break;
385                 case 'h':
386                         device_set_state_usage();
387                 }
388         args_shift(optind);
389
390         char *new_state_str = arg_pop();
391         if (!new_state_str)
392                 die("Please supply a device state");
393
394         unsigned new_state = read_string_list_or_die(new_state_str,
395                                         bch2_member_states, "device state");
396
397         char *dev_str = arg_pop();
398         if (!dev_str)
399                 die("Please supply a device");
400
401         char *end;
402         dev_idx = strtoul(dev_str, &end, 10);
403         if (*dev_str && !*end)
404                 by_id = true;
405
406         if (offline) {
407                 struct bch_opts opts = bch2_opts_empty();
408                 struct bch_sb_handle sb = { NULL };
409
410                 if (by_id)
411                         die("Cannot specify offline device by id");
412
413                 int ret = bch2_read_super(dev_str, &opts, &sb);
414                 if (ret)
415                         die("error opening %s: %s", dev_str, bch2_err_str(ret));
416
417                 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
418
419                 SET_BCH_MEMBER_STATE(m, new_state);
420
421                 le64_add_cpu(&sb.sb->seq, 1);
422
423                 bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
424                 ret = fsync(sb.bdev->bd_buffered_fd);
425                 if (ret)
426                         fprintf(stderr, "error writing superblock: fsync error (%m)");
427                 bch2_free_super(&sb);
428                 return ret;
429         }
430
431         char *fs_path = arg_pop();
432         if (fs_path) {
433                 fs = bcache_fs_open(fs_path);
434
435                 if (!by_id) {
436                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
437                         if (dev_idx < 0)
438                                 die("%s does not seem to be a member of %s",
439                                     dev_str, fs_path);
440                 }
441         } else if (!by_id) {
442                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
443         } else {
444                 die("Filesystem path required when specifying device by id");
445         }
446
447         bchu_disk_set_state(fs, dev_idx, new_state, flags);
448
449         return 0;
450 }
451
452 static void device_resize_usage(void)
453 {
454         puts("bcachefs device resize \n"
455              "Usage: bcachefs device resize device [ size ]\n"
456              "\n"
457              "Options:\n"
458              "  -h, --help                  display this help and exit\n"
459              "Report bugs to <linux-bcachefs@vger.kernel.org>");
460         exit(EXIT_SUCCESS);
461 }
462
463 int cmd_device_resize(int argc, char *argv[])
464 {
465         static const struct option longopts[] = {
466                 { "help",                       0, NULL, 'h' },
467                 { NULL }
468         };
469         u64 size;
470         int opt;
471
472         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
473                 switch (opt) {
474                 case 'h':
475                         device_resize_usage();
476                 }
477         args_shift(optind);
478
479         char *dev = arg_pop();
480         if (!dev)
481                 die("Please supply a device to resize");
482
483         int dev_fd = xopen(dev, O_RDONLY);
484
485         char *size_arg = arg_pop();
486         if (!size_arg)
487                 size = get_size(dev, dev_fd);
488         else if (bch2_strtoull_h(size_arg, &size))
489                 die("invalid size");
490
491         size >>= 9;
492
493         if (argc)
494                 die("Too many arguments");
495
496         struct stat dev_stat = xfstat(dev_fd);
497
498         struct mntent *mount = dev_to_mount(dev);
499         if (mount) {
500                 if (!S_ISBLK(dev_stat.st_mode))
501                         die("%s is mounted but isn't a block device?!", dev);
502
503                 printf("Doing online resize of %s\n", dev);
504
505                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
506
507                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
508
509                 struct bch_sb *sb = bchu_read_super(fs, -1);
510                 if (idx >= sb->nr_devices)
511                         die("error reading superblock: dev idx >= sb->nr_devices");
512
513                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
514                 if (!mi)
515                         die("error reading superblock: no member info");
516
517                 /* could also just read this out of sysfs... meh */
518                 struct bch_member *m = mi->members + idx;
519
520                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
521
522                 if (nbuckets < le64_to_cpu(m->nbuckets))
523                         die("Shrinking not supported yet");
524
525                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
526                 bchu_disk_resize(fs, idx, nbuckets);
527         } else {
528                 printf("Doing offline resize of %s\n", dev);
529
530                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
531                 if (IS_ERR(c))
532                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
533
534                 struct bch_dev *ca, *resize = NULL;
535                 unsigned i;
536
537                 for_each_online_member(ca, c, i) {
538                         if (resize)
539                                 die("confused: more than one online device?");
540                         resize = ca;
541                         percpu_ref_get(&resize->io_ref);
542                 }
543
544                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
545
546                 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
547                         die("Shrinking not supported yet");
548
549                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
550                 int ret = bch2_dev_resize(c, resize, nbuckets);
551                 if (ret)
552                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
553
554                 percpu_ref_put(&resize->io_ref);
555                 bch2_fs_stop(c);
556         }
557         return 0;
558 }
559
560 static void device_resize_journal_usage(void)
561 {
562         puts("bcachefs device resize-journal \n"
563              "Usage: bcachefs device resize-journal device size\n"
564              "\n"
565              "Options:\n"
566              "  -h, --help                  display this help and exit\n"
567              "Report bugs to <linux-bcachefs@vger.kernel.org>");
568         exit(EXIT_SUCCESS);
569 }
570
571 int cmd_device_resize_journal(int argc, char *argv[])
572 {
573         static const struct option longopts[] = {
574                 { "help",                       0, NULL, 'h' },
575                 { NULL }
576         };
577         u64 size;
578         int opt;
579
580         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
581                 switch (opt) {
582                 case 'h':
583                         device_resize_journal_usage();
584                 }
585         args_shift(optind);
586
587         char *dev = arg_pop();
588         if (!dev)
589                 die("Please supply a device");
590
591         int dev_fd = xopen(dev, O_RDONLY);
592
593         char *size_arg = arg_pop();
594         if (!size_arg)
595                 die("Please supply a journal size");
596         else if (bch2_strtoull_h(size_arg, &size))
597                 die("invalid size");
598
599         size >>= 9;
600
601         if (argc)
602                 die("Too many arguments");
603
604         struct stat dev_stat = xfstat(dev_fd);
605
606         struct mntent *mount = dev_to_mount(dev);
607         if (mount) {
608                 if (!S_ISBLK(dev_stat.st_mode))
609                         die("%s is mounted but isn't a block device?!", dev);
610
611                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
612
613                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
614
615                 struct bch_sb *sb = bchu_read_super(fs, -1);
616                 if (idx >= sb->nr_devices)
617                         die("error reading superblock: dev idx >= sb->nr_devices");
618
619                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
620                 if (!mi)
621                         die("error reading superblock: no member info");
622
623                 /* could also just read this out of sysfs... meh */
624                 struct bch_member *m = mi->members + idx;
625
626                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
627
628                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
629                 bchu_disk_resize_journal(fs, idx, nbuckets);
630         } else {
631                 printf("%s is offline - starting:\n", dev);
632
633                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
634                 if (IS_ERR(c))
635                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
636
637                 struct bch_dev *ca, *resize = NULL;
638                 unsigned i;
639
640                 for_each_online_member(ca, c, i) {
641                         if (resize)
642                                 die("confused: more than one online device?");
643                         resize = ca;
644                         percpu_ref_get(&resize->io_ref);
645                 }
646
647                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
648
649                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
650                 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
651                 if (ret)
652                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
653
654                 percpu_ref_put(&resize->io_ref);
655                 bch2_fs_stop(c);
656         }
657         return 0;
658 }