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