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\n"
127 " bcachefs device remove --by-id path devid\n"
130 " -i, --by-id Remove device by device id\n"
131 " -f, --force Force removal, even if some data\n"
132 " couldn't be migrated\n"
133 " -F, --force-metadata Force removal, even if some metadata\n"
134 " couldn't be migrated\n"
135 " -h, --help display this help and exit\n"
136 "Report bugs to <linux-bcache@vger.kernel.org>");
140 int cmd_device_remove(int argc, char *argv[])
142 static const struct option longopts[] = {
143 { "by-id", 0, NULL, 'i' },
144 { "force", 0, NULL, 'f' },
145 { "force-metadata", 0, NULL, 'F' },
146 { "help", 0, NULL, 'h' },
149 struct bchfs_handle fs;
151 int opt, flags = BCH_FORCE_IF_DEGRADED;
154 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
160 flags |= BCH_FORCE_IF_DATA_LOST;
163 flags |= BCH_FORCE_IF_METADATA_LOST;
166 device_remove_usage();
171 char *path = arg_pop();
173 die("Please supply filesystem to remove device from");
175 char *dev_str = arg_pop();
177 die("Please supply device id");
180 dev_idx = strtoul(dev_str, NULL, 10);
182 die("Error parsing device id: %m");
184 fs = bcache_fs_open(path);
186 char *dev = arg_pop();
188 die("Please supply a device to remove");
190 fs = bchu_fs_open_by_dev(dev, &dev_idx);
194 die("too many arguments");
196 bchu_disk_remove(fs, dev_idx, flags);
200 static void device_online_usage(void)
202 puts("bcachefs device online - readd a device to a running filesystem\n"
203 "Usage: bcachefs device online [OPTION]... device\n"
206 " -h, --help Display this help and exit\n"
208 "Report bugs to <linux-bcache@vger.kernel.org>");
211 int cmd_device_online(int argc, char *argv[])
215 while ((opt = getopt(argc, argv, "h")) != -1)
218 device_online_usage();
223 char *dev = arg_pop();
225 die("Please supply a device");
228 die("too many arguments");
231 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
232 bchu_disk_online(fs, dev);
236 static void device_offline_usage(void)
238 puts("bcachefs device offline - take a device offline, without removing it\n"
239 "Usage: bcachefs device offline [OPTION]... device\n"
242 " -f, --force Force, if data redundancy will be degraded\n"
243 " -h, --help Display this help and exit\n"
245 "Report bugs to <linux-bcache@vger.kernel.org>");
248 int cmd_device_offline(int argc, char *argv[])
250 static const struct option longopts[] = {
251 { "force", 0, NULL, 'f' },
256 while ((opt = getopt_long(argc, argv, "fh",
257 longopts, NULL)) != -1)
260 flags |= BCH_FORCE_IF_DEGRADED;
263 device_offline_usage();
268 char *dev = arg_pop();
270 die("Please supply a device");
273 die("too many arguments");
276 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
277 bchu_disk_offline(fs, dev_idx, flags);
281 static void device_evacuate_usage(void)
283 puts("bcachefs device evacuate - move data off of a given device\n"
284 "Usage: bcachefs device evacuate [OPTION]... device\n"
287 " -h, --help Display this help and exit\n"
289 "Report bugs to <linux-bcache@vger.kernel.org>");
292 int cmd_device_evacuate(int argc, char *argv[])
296 while ((opt = getopt(argc, argv, "h")) != -1)
299 device_evacuate_usage();
304 char *dev_path = arg_pop();
306 die("Please supply a device");
309 die("too many arguments");
312 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
314 struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx);
316 if (u.state == BCH_MEMBER_STATE_rw) {
317 printf("Setting %s readonly\n", dev_path);
318 bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0);
321 return bchu_data(fs, (struct bch_ioctl_data) {
322 .op = BCH_DATA_OP_MIGRATE,
325 .migrate.dev = dev_idx,
329 static void device_set_state_usage(void)
331 puts("bcachefs device set-state\n"
332 "Usage: bcachefs device set-state device new-state\n"
335 " -f, --force Force, if data redundancy will be degraded\n"
336 " -o, --offline Set state of an offline device\n"
337 " -h, --help display this help and exit\n"
338 "Report bugs to <linux-bcache@vger.kernel.org>");
342 int cmd_device_set_state(int argc, char *argv[])
344 static const struct option longopts[] = {
345 { "force", 0, NULL, 'f' },
346 { "offline", 0, NULL, 'o' },
347 { "help", 0, NULL, 'h' },
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 *dev_path = arg_pop();
368 die("Please supply a device");
370 char *new_state_str = arg_pop();
372 die("Please supply a device state");
374 unsigned new_state = read_string_list_or_die(new_state_str,
375 bch2_member_states, "device state");
379 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
381 bchu_disk_set_state(fs, dev_idx, new_state, flags);
385 struct bch_opts opts = bch2_opts_empty();
386 struct bch_sb_handle sb = { NULL };
388 int ret = bch2_read_super(dev_path, &opts, &sb);
390 die("error opening %s: %s", dev_path, strerror(-ret));
392 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
394 SET_BCH_MEMBER_STATE(m, new_state);
396 le64_add_cpu(&sb.sb->seq, 1);
398 bch2_super_write(sb.bdev->bd_fd, sb.sb);
399 bch2_free_super(&sb);
405 static void device_resize_usage(void)
407 puts("bcachefs device resize \n"
408 "Usage: bcachefs device resize device [ size ]\n"
411 " -h, --help display this help and exit\n"
412 "Report bugs to <linux-bcache@vger.kernel.org>");
416 int cmd_device_resize(int argc, char *argv[])
418 static const struct option longopts[] = {
419 { "help", 0, NULL, 'h' },
425 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
428 device_resize_usage();
432 char *dev = arg_pop();
434 die("Please supply a device to resize");
436 int dev_fd = xopen(dev, O_RDONLY);
438 char *size_arg = arg_pop();
440 size = get_size(dev, dev_fd);
441 else if (bch2_strtoull_h(size_arg, &size))
447 die("Too many arguments");
449 struct stat dev_stat = xfstat(dev_fd);
451 struct mntent *mount = dev_to_mount(dev);
453 if (!S_ISBLK(dev_stat.st_mode))
454 die("%s is mounted but isn't a block device?!", dev);
456 printf("Doing online resize of %s\n", dev);
458 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
460 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
462 struct bch_sb *sb = bchu_read_super(fs, -1);
463 if (idx >= sb->nr_devices)
464 die("error reading superblock: dev idx >= sb->nr_devices");
466 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
468 die("error reading superblock: no member info");
470 /* could also just read this out of sysfs... meh */
471 struct bch_member *m = mi->members + idx;
473 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
475 printf("resizing %s to %llu buckets\n", dev, nbuckets);
476 bchu_disk_resize(fs, idx, nbuckets);
478 printf("Doing offline resize of %s\n", dev);
480 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
482 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
484 struct bch_dev *ca, *resize = NULL;
487 for_each_online_member(ca, c, i) {
489 die("confused: more than one online device?");
491 percpu_ref_get(&resize->io_ref);
494 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
496 printf("resizing %s to %llu buckets\n", dev, nbuckets);
497 int ret = bch2_dev_resize(c, resize, nbuckets);
499 fprintf(stderr, "resize error: %s\n", strerror(-ret));
501 percpu_ref_put(&resize->io_ref);
507 static void device_resize_journal_usage(void)
509 puts("bcachefs device resize-journal \n"
510 "Usage: bcachefs device resize-journal device [ size ]\n"
513 " -h, --help display this help and exit\n"
514 "Report bugs to <linux-bcache@vger.kernel.org>");
518 int cmd_device_resize_journal(int argc, char *argv[])
520 static const struct option longopts[] = {
521 { "help", 0, NULL, 'h' },
527 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
530 device_resize_journal_usage();
534 char *dev = arg_pop();
536 die("Please supply a device");
538 int dev_fd = xopen(dev, O_RDONLY);
540 char *size_arg = arg_pop();
542 size = get_size(dev, dev_fd);
543 else if (bch2_strtoull_h(size_arg, &size))
549 die("Too many arguments");
551 struct stat dev_stat = xfstat(dev_fd);
553 struct mntent *mount = dev_to_mount(dev);
555 if (!S_ISBLK(dev_stat.st_mode))
556 die("%s is mounted but isn't a block device?!", dev);
558 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
560 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
562 struct bch_sb *sb = bchu_read_super(fs, -1);
563 if (idx >= sb->nr_devices)
564 die("error reading superblock: dev idx >= sb->nr_devices");
566 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
568 die("error reading superblock: no member info");
570 /* could also just read this out of sysfs... meh */
571 struct bch_member *m = mi->members + idx;
573 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
575 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
576 bchu_disk_resize_journal(fs, idx, nbuckets);
578 printf("%s is offline - starting:\n", dev);
580 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
582 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
584 struct bch_dev *ca, *resize = NULL;
587 for_each_online_member(ca, c, i) {
589 die("confused: more than one online device?");
591 percpu_ref_get(&resize->io_ref);
594 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
596 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
597 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
599 fprintf(stderr, "resize error: %s\n", strerror(-ret));
601 percpu_ref_put(&resize->io_ref);