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