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-bcachefs@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");
83 if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
84 die("bad bucket_size %s", optarg);
87 dev_opts.discard = true;
90 dev_opts.label = strdup(optarg);
101 char *fs_path = arg_pop();
103 die("Please supply a filesystem");
105 dev_opts.path = arg_pop();
107 die("Please supply a device");
110 die("too many arguments");
112 struct bchfs_handle fs = bcache_fs_open(fs_path);
114 dev_opts.fd = open_for_format(dev_opts.path, force);
116 struct bch_opt_strs fs_opt_strs;
117 memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
119 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
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"));
126 struct bch_sb *sb = bch2_format(fs_opt_strs,
134 bchu_disk_add(fs, dev_opts.path);
138 static void device_remove_usage(void)
140 puts("bcachefs device_remove - remove a device from a filesystem\n"
142 " bcachefs device remove <device>|<devid> <path>\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-bcachefs@vger.kernel.org>");
154 int cmd_device_remove(int argc, char *argv[])
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' },
163 struct bchfs_handle fs;
165 int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
167 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
170 flags |= BCH_FORCE_IF_DATA_LOST;
173 flags |= BCH_FORCE_IF_METADATA_LOST;
176 device_remove_usage();
180 char *dev_str = arg_pop();
182 die("Please supply a device");
185 dev_idx = strtoul(dev_str, &end, 10);
186 if (*dev_str && !*end)
189 char *fs_path = arg_pop();
191 fs = bcache_fs_open(fs_path);
194 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
196 die("%s does not seem to be a member of %s",
200 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
202 die("Filesystem path required when specifying device by id");
205 bchu_disk_remove(fs, dev_idx, flags);
209 static void device_online_usage(void)
211 puts("bcachefs device online - readd a device to a running filesystem\n"
212 "Usage: bcachefs device online [OPTION]... device\n"
215 " -h, --help Display this help and exit\n"
217 "Report bugs to <linux-bcachefs@vger.kernel.org>");
220 int cmd_device_online(int argc, char *argv[])
224 while ((opt = getopt(argc, argv, "h")) != -1)
227 device_online_usage();
232 char *dev = arg_pop();
234 die("Please supply a device");
237 die("too many arguments");
240 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
241 bchu_disk_online(fs, dev);
245 static void device_offline_usage(void)
247 puts("bcachefs device offline - take a device offline, without removing it\n"
248 "Usage: bcachefs device offline [OPTION]... device\n"
251 " -f, --force Force, if data redundancy will be degraded\n"
252 " -h, --help Display this help and exit\n"
254 "Report bugs to <linux-bcachefs@vger.kernel.org>");
257 int cmd_device_offline(int argc, char *argv[])
259 static const struct option longopts[] = {
260 { "force", 0, NULL, 'f' },
265 while ((opt = getopt_long(argc, argv, "fh",
266 longopts, NULL)) != -1)
269 flags |= BCH_FORCE_IF_DEGRADED;
272 device_offline_usage();
277 char *dev = arg_pop();
279 die("Please supply a device");
282 die("too many arguments");
285 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
286 bchu_disk_offline(fs, dev_idx, flags);
290 static void device_evacuate_usage(void)
292 puts("bcachefs device evacuate - move data off of a given device\n"
293 "Usage: bcachefs device evacuate [OPTION]... device\n"
296 " -h, --help Display this help and exit\n"
298 "Report bugs to <linux-bcachefs@vger.kernel.org>");
301 int cmd_device_evacuate(int argc, char *argv[])
305 while ((opt = getopt(argc, argv, "h")) != -1)
308 device_evacuate_usage();
313 char *dev_path = arg_pop();
315 die("Please supply a device");
318 die("too many arguments");
321 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
323 struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx);
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);
330 return bchu_data(fs, (struct bch_ioctl_data) {
331 .op = BCH_DATA_OP_MIGRATE,
333 .start_pos = POS_MIN,
334 .end_btree = BTREE_ID_NR,
336 .migrate.dev = dev_idx,
340 static void device_set_state_usage(void)
342 puts("bcachefs device set-state\n"
343 "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\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"
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-bcachefs@vger.kernel.org>");
357 int cmd_device_set_state(int argc, char *argv[])
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' },
366 struct bchfs_handle fs;
368 int opt, flags = 0, dev_idx;
369 bool offline = false;
371 while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
374 flags |= BCH_FORCE_IF_DEGRADED;
377 flags |= BCH_FORCE_IF_DEGRADED;
378 flags |= BCH_FORCE_IF_LOST;
384 device_set_state_usage();
388 char *new_state_str = arg_pop();
390 die("Please supply a device state");
392 unsigned new_state = read_string_list_or_die(new_state_str,
393 bch2_member_states, "device state");
395 char *dev_str = arg_pop();
397 die("Please supply a device");
400 dev_idx = strtoul(dev_str, &end, 10);
401 if (*dev_str && !*end)
405 struct bch_opts opts = bch2_opts_empty();
406 struct bch_sb_handle sb = { NULL };
409 die("Cannot specify offline device by id");
411 int ret = bch2_read_super(dev_str, &opts, &sb);
413 die("error opening %s: %s", dev_str, strerror(-ret));
415 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
417 SET_BCH_MEMBER_STATE(m, new_state);
419 le64_add_cpu(&sb.sb->seq, 1);
421 bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
422 ret = fsync(sb.bdev->bd_buffered_fd);
424 fprintf(stderr, "error writing superblock: fsync error (%m)");
425 bch2_free_super(&sb);
429 char *fs_path = arg_pop();
431 fs = bcache_fs_open(fs_path);
434 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
436 die("%s does not seem to be a member of %s",
440 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
442 die("Filesystem path required when specifying device by id");
445 bchu_disk_set_state(fs, dev_idx, new_state, flags);
450 static void device_resize_usage(void)
452 puts("bcachefs device resize \n"
453 "Usage: bcachefs device resize device [ size ]\n"
456 " -h, --help display this help and exit\n"
457 "Report bugs to <linux-bcachefs@vger.kernel.org>");
461 int cmd_device_resize(int argc, char *argv[])
463 static const struct option longopts[] = {
464 { "help", 0, NULL, 'h' },
470 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
473 device_resize_usage();
477 char *dev = arg_pop();
479 die("Please supply a device to resize");
481 int dev_fd = xopen(dev, O_RDONLY);
483 char *size_arg = arg_pop();
485 size = get_size(dev, dev_fd);
486 else if (bch2_strtoull_h(size_arg, &size))
492 die("Too many arguments");
494 struct stat dev_stat = xfstat(dev_fd);
496 struct mntent *mount = dev_to_mount(dev);
498 if (!S_ISBLK(dev_stat.st_mode))
499 die("%s is mounted but isn't a block device?!", dev);
501 printf("Doing online resize of %s\n", dev);
503 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
505 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
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");
511 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
513 die("error reading superblock: no member info");
515 /* could also just read this out of sysfs... meh */
516 struct bch_member *m = mi->members + idx;
518 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
520 if (nbuckets < le64_to_cpu(m->nbuckets))
521 die("Shrinking not supported yet");
523 printf("resizing %s to %llu buckets\n", dev, nbuckets);
524 bchu_disk_resize(fs, idx, nbuckets);
526 printf("Doing offline resize of %s\n", dev);
528 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
530 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
532 struct bch_dev *ca, *resize = NULL;
535 for_each_online_member(ca, c, i) {
537 die("confused: more than one online device?");
539 percpu_ref_get(&resize->io_ref);
542 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
544 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
545 die("Shrinking not supported yet");
547 printf("resizing %s to %llu buckets\n", dev, nbuckets);
548 int ret = bch2_dev_resize(c, resize, nbuckets);
550 fprintf(stderr, "resize error: %s\n", strerror(-ret));
552 percpu_ref_put(&resize->io_ref);
558 static void device_resize_journal_usage(void)
560 puts("bcachefs device resize-journal \n"
561 "Usage: bcachefs device resize-journal device size\n"
564 " -h, --help display this help and exit\n"
565 "Report bugs to <linux-bcachefs@vger.kernel.org>");
569 int cmd_device_resize_journal(int argc, char *argv[])
571 static const struct option longopts[] = {
572 { "help", 0, NULL, 'h' },
578 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
581 device_resize_journal_usage();
585 char *dev = arg_pop();
587 die("Please supply a device");
589 int dev_fd = xopen(dev, O_RDONLY);
591 char *size_arg = arg_pop();
593 die("Please supply a journal size");
594 else if (bch2_strtoull_h(size_arg, &size))
600 die("Too many arguments");
602 struct stat dev_stat = xfstat(dev_fd);
604 struct mntent *mount = dev_to_mount(dev);
606 if (!S_ISBLK(dev_stat.st_mode))
607 die("%s is mounted but isn't a block device?!", dev);
609 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
611 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
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");
617 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
619 die("error reading superblock: no member info");
621 /* could also just read this out of sysfs... meh */
622 struct bch_member *m = mi->members + idx;
624 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
626 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
627 bchu_disk_resize_journal(fs, idx, nbuckets);
629 printf("%s is offline - starting:\n", dev);
631 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
633 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
635 struct bch_dev *ca, *resize = NULL;
638 for_each_online_member(ca, c, i) {
640 die("confused: more than one online device?");
642 percpu_ref_get(&resize->io_ref);
645 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
647 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
648 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
650 fprintf(stderr, "resize error: %s\n", strerror(-ret));
652 percpu_ref_put(&resize->io_ref);