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 static void device_add_usage(void)
26 puts("bcachefs device add - add a device to an existing filesystem\n"
27 "Usage: bcachefs device add [OPTION]... filesystem device\n"
30 " -S, --fs_size=size Size of filesystem on device\n"
31 " -B, --bucket=size Bucket size\n"
32 " -D, --discard Enable discards\n"
33 " -g, --group=group Disk group\n"
34 " -f, --force Use device even if it appears to already be formatted\n"
35 " -h, --help Display this help and exit\n"
37 "Report bugs to <linux-bcache@vger.kernel.org>");
40 int cmd_device_add(int argc, char *argv[])
42 static const struct option longopts[] = {
43 { "fs_size", required_argument, NULL, 'S' },
44 { "bucket", required_argument, NULL, 'B' },
45 { "discard", no_argument, NULL, 'D' },
46 { "group", required_argument, NULL, 'g' },
47 { "force", no_argument, NULL, 'f' },
48 { "help", no_argument, NULL, 'h' },
51 struct format_opts format_opts = format_opts_default();
52 struct dev_opts dev_opts = dev_opts_default();
56 while ((opt = getopt_long(argc, argv, "t:fh",
57 longopts, NULL)) != -1)
60 if (bch2_strtoull_h(optarg, &dev_opts.size))
61 die("invalid filesystem size");
66 dev_opts.bucket_size =
67 hatoi_validate(optarg, "bucket size");
70 dev_opts.discard = true;
73 dev_opts.group = strdup(optarg);
84 char *fs_path = arg_pop();
86 die("Please supply a filesystem");
88 char *dev_path = arg_pop();
90 die("Please supply a device");
93 die("too many arguments");
95 struct bchfs_handle fs = bcache_fs_open(fs_path);
97 dev_opts.path = dev_path;
98 dev_opts.fd = open_for_format(dev_opts.path, force);
100 struct bch_opt_strs fs_opt_strs;
101 memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
103 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
105 opt_set(fs_opts, block_size,
106 read_file_u64(fs.sysfs_fd, "block_size") >> 9);
107 opt_set(fs_opts, btree_node_size,
108 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9);
110 struct bch_sb *sb = bch2_format(fs_opt_strs,
118 bchu_disk_add(fs, dev_opts.path);
122 static void device_remove_usage(void)
124 puts("bcachefs device_remove - remove a device from a filesystem\n"
126 " bcachefs device remove <device>|<devid> <path>\n"
129 " -f, --force Force removal, even if some data\n"
130 " couldn't be migrated\n"
131 " -F, --force-metadata Force removal, even if some metadata\n"
132 " couldn't be migrated\n"
133 " -h, --help display this help and exit\n"
134 "Report bugs to <linux-bcache@vger.kernel.org>");
138 int cmd_device_remove(int argc, char *argv[])
140 static const struct option longopts[] = {
141 { "by-id", 0, NULL, 'i' },
142 { "force", 0, NULL, 'f' },
143 { "force-metadata", 0, NULL, 'F' },
144 { "help", 0, NULL, 'h' },
147 struct bchfs_handle fs;
149 int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
151 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
154 flags |= BCH_FORCE_IF_DATA_LOST;
157 flags |= BCH_FORCE_IF_METADATA_LOST;
160 device_remove_usage();
164 char *dev_str = arg_pop();
166 die("Please supply a device");
169 dev_idx = strtoul(dev_str, &end, 10);
170 if (*dev_str && !*end)
173 char *fs_path = arg_pop();
175 fs = bcache_fs_open(fs_path);
178 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
180 die("%s does not seem to be a member of %s",
184 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
186 die("Filesystem path required when specifying device by id");
189 bchu_disk_remove(fs, dev_idx, flags);
193 static void device_online_usage(void)
195 puts("bcachefs device online - readd a device to a running filesystem\n"
196 "Usage: bcachefs device online [OPTION]... device\n"
199 " -h, --help Display this help and exit\n"
201 "Report bugs to <linux-bcache@vger.kernel.org>");
204 int cmd_device_online(int argc, char *argv[])
208 while ((opt = getopt(argc, argv, "h")) != -1)
211 device_online_usage();
216 char *dev = arg_pop();
218 die("Please supply a device");
221 die("too many arguments");
224 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
225 bchu_disk_online(fs, dev);
229 static void device_offline_usage(void)
231 puts("bcachefs device offline - take a device offline, without removing it\n"
232 "Usage: bcachefs device offline [OPTION]... device\n"
235 " -f, --force Force, if data redundancy will be degraded\n"
236 " -h, --help Display this help and exit\n"
238 "Report bugs to <linux-bcache@vger.kernel.org>");
241 int cmd_device_offline(int argc, char *argv[])
243 static const struct option longopts[] = {
244 { "force", 0, NULL, 'f' },
249 while ((opt = getopt_long(argc, argv, "fh",
250 longopts, NULL)) != -1)
253 flags |= BCH_FORCE_IF_DEGRADED;
256 device_offline_usage();
261 char *dev = arg_pop();
263 die("Please supply a device");
266 die("too many arguments");
269 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
270 bchu_disk_offline(fs, dev_idx, flags);
274 static void device_evacuate_usage(void)
276 puts("bcachefs device evacuate - move data off of a given device\n"
277 "Usage: bcachefs device evacuate [OPTION]... device\n"
280 " -h, --help Display this help and exit\n"
282 "Report bugs to <linux-bcache@vger.kernel.org>");
285 int cmd_device_evacuate(int argc, char *argv[])
289 while ((opt = getopt(argc, argv, "h")) != -1)
292 device_evacuate_usage();
297 char *dev_path = arg_pop();
299 die("Please supply a device");
302 die("too many arguments");
305 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
307 struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx);
309 if (u.state == BCH_MEMBER_STATE_rw) {
310 printf("Setting %s readonly\n", dev_path);
311 bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0);
314 return bchu_data(fs, (struct bch_ioctl_data) {
315 .op = BCH_DATA_OP_MIGRATE,
317 .start_pos = POS_MIN,
318 .end_btree = BTREE_ID_NR,
320 .migrate.dev = dev_idx,
324 static void device_set_state_usage(void)
326 puts("bcachefs device set-state\n"
327 "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
329 "<new-state>: one of rw, ro, failed or spare\n"
330 "<path>: path to mounted filesystem, optional unless specifying device by id\n"
333 " -f, --force Force, if data redundancy will be degraded\n"
334 " -o, --offline Set state of an offline device\n"
335 " -h, --help display this help and exit\n"
336 "Report bugs to <linux-bcache@vger.kernel.org>");
340 int cmd_device_set_state(int argc, char *argv[])
342 static const struct option longopts[] = {
343 { "force", 0, NULL, 'f' },
344 { "offline", 0, NULL, 'o' },
345 { "help", 0, NULL, 'h' },
348 struct bchfs_handle fs;
350 int opt, flags = 0, dev_idx;
351 bool offline = false;
353 while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
356 flags |= BCH_FORCE_IF_DEGRADED;
362 device_set_state_usage();
366 char *new_state_str = arg_pop();
368 die("Please supply a device state");
370 unsigned new_state = read_string_list_or_die(new_state_str,
371 bch2_member_states, "device state");
373 char *dev_str = arg_pop();
375 die("Please supply a device");
378 dev_idx = strtoul(dev_str, &end, 10);
379 if (*dev_str && !*end)
383 struct bch_opts opts = bch2_opts_empty();
384 struct bch_sb_handle sb = { NULL };
387 die("Cannot specify offline device by id");
389 int ret = bch2_read_super(dev_str, &opts, &sb);
391 die("error opening %s: %s", dev_str, strerror(-ret));
393 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
395 SET_BCH_MEMBER_STATE(m, new_state);
397 le64_add_cpu(&sb.sb->seq, 1);
399 bch2_super_write(sb.bdev->bd_fd, sb.sb);
400 bch2_free_super(&sb);
404 char *fs_path = arg_pop();
406 fs = bcache_fs_open(fs_path);
409 dev_idx = bchu_dev_path_to_idx(fs, dev_str);
411 die("%s does not seem to be a member of %s",
415 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
417 die("Filesystem path required when specifying device by id");
420 bchu_disk_set_state(fs, dev_idx, new_state, flags);
425 static void device_resize_usage(void)
427 puts("bcachefs device resize \n"
428 "Usage: bcachefs device resize device [ size ]\n"
431 " -h, --help display this help and exit\n"
432 "Report bugs to <linux-bcache@vger.kernel.org>");
436 int cmd_device_resize(int argc, char *argv[])
438 static const struct option longopts[] = {
439 { "help", 0, NULL, 'h' },
445 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
448 device_resize_usage();
452 char *dev = arg_pop();
454 die("Please supply a device to resize");
456 int dev_fd = xopen(dev, O_RDONLY);
458 char *size_arg = arg_pop();
460 size = get_size(dev, dev_fd);
461 else if (bch2_strtoull_h(size_arg, &size))
467 die("Too many arguments");
469 struct stat dev_stat = xfstat(dev_fd);
471 struct mntent *mount = dev_to_mount(dev);
473 if (!S_ISBLK(dev_stat.st_mode))
474 die("%s is mounted but isn't a block device?!", dev);
476 printf("Doing online resize of %s\n", dev);
478 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
480 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
482 struct bch_sb *sb = bchu_read_super(fs, -1);
483 if (idx >= sb->nr_devices)
484 die("error reading superblock: dev idx >= sb->nr_devices");
486 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
488 die("error reading superblock: no member info");
490 /* could also just read this out of sysfs... meh */
491 struct bch_member *m = mi->members + idx;
493 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
495 printf("resizing %s to %llu buckets\n", dev, nbuckets);
496 bchu_disk_resize(fs, idx, nbuckets);
498 printf("Doing offline resize of %s\n", dev);
500 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
502 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
504 struct bch_dev *ca, *resize = NULL;
507 for_each_online_member(ca, c, i) {
509 die("confused: more than one online device?");
511 percpu_ref_get(&resize->io_ref);
514 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
516 printf("resizing %s to %llu buckets\n", dev, nbuckets);
517 int ret = bch2_dev_resize(c, resize, nbuckets);
519 fprintf(stderr, "resize error: %s\n", strerror(-ret));
521 percpu_ref_put(&resize->io_ref);
527 static void device_resize_journal_usage(void)
529 puts("bcachefs device resize-journal \n"
530 "Usage: bcachefs device resize-journal device [ size ]\n"
533 " -h, --help display this help and exit\n"
534 "Report bugs to <linux-bcache@vger.kernel.org>");
538 int cmd_device_resize_journal(int argc, char *argv[])
540 static const struct option longopts[] = {
541 { "help", 0, NULL, 'h' },
547 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
550 device_resize_journal_usage();
554 char *dev = arg_pop();
556 die("Please supply a device");
558 int dev_fd = xopen(dev, O_RDONLY);
560 char *size_arg = arg_pop();
562 size = get_size(dev, dev_fd);
563 else if (bch2_strtoull_h(size_arg, &size))
569 die("Too many arguments");
571 struct stat dev_stat = xfstat(dev_fd);
573 struct mntent *mount = dev_to_mount(dev);
575 if (!S_ISBLK(dev_stat.st_mode))
576 die("%s is mounted but isn't a block device?!", dev);
578 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
580 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
582 struct bch_sb *sb = bchu_read_super(fs, -1);
583 if (idx >= sb->nr_devices)
584 die("error reading superblock: dev idx >= sb->nr_devices");
586 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
588 die("error reading superblock: no member info");
590 /* could also just read this out of sysfs... meh */
591 struct bch_member *m = mi->members + idx;
593 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
595 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
596 bchu_disk_resize_journal(fs, idx, nbuckets);
598 printf("%s is offline - starting:\n", dev);
600 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
602 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
604 struct bch_dev *ca, *resize = NULL;
607 for_each_online_member(ca, c, i) {
609 die("confused: more than one online device?");
611 percpu_ref_get(&resize->io_ref);
614 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
616 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
617 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
619 fprintf(stderr, "resize error: %s\n", strerror(-ret));
621 percpu_ref_put(&resize->io_ref);