10 #include <sys/ioctl.h>
12 #include <sys/types.h>
15 #include "libbcachefs/bcachefs.h"
16 #include "libbcachefs/bcachefs_ioctl.h"
17 #include "libbcachefs/journal.h"
18 #include "libbcachefs/super-io.h"
20 #include "libbcachefs.h"
21 #include "libbcachefs/opts.h"
22 #include "tools-util.h"
24 int device_usage(void)
26 puts("bcachefs device - manage devices within a running filesystem\n"
27 "Usage: bcachefs device <CMD> [OPTION]\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"
39 "Report bugs to <linux-bcachefs@vger.kernel.org>");
43 static void device_add_usage(void)
45 puts("bcachefs device add - add a device to an existing filesystem\n"
46 "Usage: bcachefs device add [OPTION]... filesystem device\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"
56 "Report bugs to <linux-bcache@vger.kernel.org>");
59 int cmd_device_add(int argc, char *argv[])
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' },
70 struct format_opts format_opts = format_opts_default();
71 struct dev_opts dev_opts = dev_opts_default();
75 while ((opt = getopt_long(argc, argv, "t:fh",
76 longopts, NULL)) != -1)
79 if (bch2_strtoull_h(optarg, &dev_opts.size))
80 die("invalid filesystem size");
85 dev_opts.bucket_size =
86 hatoi_validate(optarg, "bucket size");
89 dev_opts.discard = true;
92 dev_opts.label = strdup(optarg);
103 char *fs_path = arg_pop();
105 die("Please supply a filesystem");
107 dev_opts.path = arg_pop();
109 die("Please supply a device");
112 die("too many arguments");
114 struct bchfs_handle fs = bcache_fs_open(fs_path);
116 dev_opts.fd = open_for_format(dev_opts.path, force);
118 struct bch_opt_strs fs_opt_strs;
119 memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
121 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
123 opt_set(fs_opts, block_size,
124 read_file_u64(fs.sysfs_fd, "options/block_size") >> 9);
125 opt_set(fs_opts, btree_node_size,
126 read_file_u64(fs.sysfs_fd, "options/btree_node_size") >> 9);
128 struct bch_sb *sb = bch2_format(fs_opt_strs,
136 bchu_disk_add(fs, dev_opts.path);
140 static void device_remove_usage(void)
142 puts("bcachefs device_remove - remove a device from a filesystem\n"
144 " bcachefs device remove <device>|<devid> <path>\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-bcache@vger.kernel.org>");
156 int cmd_device_remove(int argc, char *argv[])
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' },
165 struct bchfs_handle fs;
167 int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
169 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
172 flags |= BCH_FORCE_IF_DATA_LOST;
175 flags |= BCH_FORCE_IF_METADATA_LOST;
178 device_remove_usage();
182 char *dev_str = arg_pop();
184 die("Please supply a device");
187 dev_idx = strtoul(dev_str, &end, 10);
188 if (*dev_str && !*end)
191 char *fs_path = arg_pop();
193 fs = bcache_fs_open(fs_path);
196 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
198 die("%s does not seem to be a member of %s",
202 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
204 die("Filesystem path required when specifying device by id");
207 bchu_disk_remove(fs, dev_idx, flags);
211 static void device_online_usage(void)
213 puts("bcachefs device online - readd a device to a running filesystem\n"
214 "Usage: bcachefs device online [OPTION]... device\n"
217 " -h, --help Display this help and exit\n"
219 "Report bugs to <linux-bcache@vger.kernel.org>");
222 int cmd_device_online(int argc, char *argv[])
226 while ((opt = getopt(argc, argv, "h")) != -1)
229 device_online_usage();
234 char *dev = arg_pop();
236 die("Please supply a device");
239 die("too many arguments");
242 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
243 bchu_disk_online(fs, dev);
247 static void device_offline_usage(void)
249 puts("bcachefs device offline - take a device offline, without removing it\n"
250 "Usage: bcachefs device offline [OPTION]... device\n"
253 " -f, --force Force, if data redundancy will be degraded\n"
254 " -h, --help Display this help and exit\n"
256 "Report bugs to <linux-bcache@vger.kernel.org>");
259 int cmd_device_offline(int argc, char *argv[])
261 static const struct option longopts[] = {
262 { "force", 0, NULL, 'f' },
267 while ((opt = getopt_long(argc, argv, "fh",
268 longopts, NULL)) != -1)
271 flags |= BCH_FORCE_IF_DEGRADED;
274 device_offline_usage();
279 char *dev = arg_pop();
281 die("Please supply a device");
284 die("too many arguments");
287 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
288 bchu_disk_offline(fs, dev_idx, flags);
292 static void device_evacuate_usage(void)
294 puts("bcachefs device evacuate - move data off of a given device\n"
295 "Usage: bcachefs device evacuate [OPTION]... device\n"
298 " -h, --help Display this help and exit\n"
300 "Report bugs to <linux-bcache@vger.kernel.org>");
303 int cmd_device_evacuate(int argc, char *argv[])
307 while ((opt = getopt(argc, argv, "h")) != -1)
310 device_evacuate_usage();
315 char *dev_path = arg_pop();
317 die("Please supply a device");
320 die("too many arguments");
323 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
325 struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx);
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);
332 return bchu_data(fs, (struct bch_ioctl_data) {
333 .op = BCH_DATA_OP_MIGRATE,
335 .start_pos = POS_MIN,
336 .end_btree = BTREE_ID_NR,
338 .migrate.dev = dev_idx,
342 static void device_set_state_usage(void)
344 puts("bcachefs device set-state\n"
345 "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\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"
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-bcache@vger.kernel.org>");
359 int cmd_device_set_state(int argc, char *argv[])
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' },
368 struct bchfs_handle fs;
370 int opt, flags = 0, dev_idx;
371 bool offline = false;
373 while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
376 flags |= BCH_FORCE_IF_DEGRADED;
379 flags |= BCH_FORCE_IF_DEGRADED;
380 flags |= BCH_FORCE_IF_LOST;
386 device_set_state_usage();
390 char *new_state_str = arg_pop();
392 die("Please supply a device state");
394 unsigned new_state = read_string_list_or_die(new_state_str,
395 bch2_member_states, "device state");
397 char *dev_str = arg_pop();
399 die("Please supply a device");
402 dev_idx = strtoul(dev_str, &end, 10);
403 if (*dev_str && !*end)
407 struct bch_opts opts = bch2_opts_empty();
408 struct bch_sb_handle sb = { NULL };
411 die("Cannot specify offline device by id");
413 int ret = bch2_read_super(dev_str, &opts, &sb);
415 die("error opening %s: %s", dev_str, strerror(-ret));
417 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
419 SET_BCH_MEMBER_STATE(m, new_state);
421 le64_add_cpu(&sb.sb->seq, 1);
423 bch2_super_write(sb.bdev->bd_fd, sb.sb);
424 bch2_free_super(&sb);
428 char *fs_path = arg_pop();
430 fs = bcache_fs_open(fs_path);
433 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
435 die("%s does not seem to be a member of %s",
439 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
441 die("Filesystem path required when specifying device by id");
444 bchu_disk_set_state(fs, dev_idx, new_state, flags);
449 static void device_resize_usage(void)
451 puts("bcachefs device resize \n"
452 "Usage: bcachefs device resize device [ size ]\n"
455 " -h, --help display this help and exit\n"
456 "Report bugs to <linux-bcache@vger.kernel.org>");
460 int cmd_device_resize(int argc, char *argv[])
462 static const struct option longopts[] = {
463 { "help", 0, NULL, 'h' },
469 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
472 device_resize_usage();
476 char *dev = arg_pop();
478 die("Please supply a device to resize");
480 int dev_fd = xopen(dev, O_RDONLY);
482 char *size_arg = arg_pop();
484 size = get_size(dev, dev_fd);
485 else if (bch2_strtoull_h(size_arg, &size))
491 die("Too many arguments");
493 struct stat dev_stat = xfstat(dev_fd);
495 struct mntent *mount = dev_to_mount(dev);
497 if (!S_ISBLK(dev_stat.st_mode))
498 die("%s is mounted but isn't a block device?!", dev);
500 printf("Doing online resize of %s\n", dev);
502 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
504 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
506 struct bch_sb *sb = bchu_read_super(fs, -1);
507 if (idx >= sb->nr_devices)
508 die("error reading superblock: dev idx >= sb->nr_devices");
510 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
512 die("error reading superblock: no member info");
514 /* could also just read this out of sysfs... meh */
515 struct bch_member *m = mi->members + idx;
517 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
519 if (nbuckets < le64_to_cpu(m->nbuckets))
520 die("Shrinking not supported yet");
522 printf("resizing %s to %llu buckets\n", dev, nbuckets);
523 bchu_disk_resize(fs, idx, nbuckets);
525 printf("Doing offline resize of %s\n", dev);
527 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
529 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
531 struct bch_dev *ca, *resize = NULL;
534 for_each_online_member(ca, c, i) {
536 die("confused: more than one online device?");
538 percpu_ref_get(&resize->io_ref);
541 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
543 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
544 die("Shrinking not supported yet");
546 printf("resizing %s to %llu buckets\n", dev, nbuckets);
547 int ret = bch2_dev_resize(c, resize, nbuckets);
549 fprintf(stderr, "resize error: %s\n", strerror(-ret));
551 percpu_ref_put(&resize->io_ref);
557 static void device_resize_journal_usage(void)
559 puts("bcachefs device resize-journal \n"
560 "Usage: bcachefs device resize-journal device size\n"
563 " -h, --help display this help and exit\n"
564 "Report bugs to <linux-bcache@vger.kernel.org>");
568 int cmd_device_resize_journal(int argc, char *argv[])
570 static const struct option longopts[] = {
571 { "help", 0, NULL, 'h' },
577 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
580 device_resize_journal_usage();
584 char *dev = arg_pop();
586 die("Please supply a device");
588 int dev_fd = xopen(dev, O_RDONLY);
590 char *size_arg = arg_pop();
592 die("Please supply a journal size");
593 else if (bch2_strtoull_h(size_arg, &size))
599 die("Too many arguments");
601 struct stat dev_stat = xfstat(dev_fd);
603 struct mntent *mount = dev_to_mount(dev);
605 if (!S_ISBLK(dev_stat.st_mode))
606 die("%s is mounted but isn't a block device?!", dev);
608 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
610 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
612 struct bch_sb *sb = bchu_read_super(fs, -1);
613 if (idx >= sb->nr_devices)
614 die("error reading superblock: dev idx >= sb->nr_devices");
616 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
618 die("error reading superblock: no member info");
620 /* could also just read this out of sysfs... meh */
621 struct bch_member *m = mi->members + idx;
623 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
625 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
626 bchu_disk_resize_journal(fs, idx, nbuckets);
628 printf("%s is offline - starting:\n", dev);
630 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
632 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
634 struct bch_dev *ca, *resize = NULL;
637 for_each_online_member(ca, c, i) {
639 die("confused: more than one online device?");
641 percpu_ref_get(&resize->io_ref);
644 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
646 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
647 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
649 fprintf(stderr, "resize error: %s\n", strerror(-ret));
651 percpu_ref_put(&resize->io_ref);