X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=cmd_device.c;h=b356bcb1dbae888d80b9ab76b7d60f88dea9c666;hb=9e5d774875df1605c23f38f1da6c9dbb49826471;hp=87ede0f625578fe2147e8cdd60db9717759c75ef;hpb=4c96e9dda3c3c3aeb139c547e1d9203d8e15bbcb;p=bcachefs-tools-debian diff --git a/cmd_device.c b/cmd_device.c index 87ede0f..b356bcb 100644 --- a/cmd_device.c +++ b/cmd_device.c @@ -12,7 +12,9 @@ #include #include +#include "libbcachefs/bcachefs.h" #include "libbcachefs/bcachefs_ioctl.h" +#include "libbcachefs/journal.h" #include "libbcachefs/super-io.h" #include "cmds.h" #include "libbcachefs.h" @@ -170,10 +172,15 @@ int cmd_device_remove(int argc, char *argv[]) if (!path) die("Please supply filesystem to remove device from"); - dev_idx = (intptr_t) arg_pop(); - if (!dev_idx) + char *dev_str = arg_pop(); + if (!dev_str) die("Please supply device id"); + errno = 0; + dev_idx = strtoul(dev_str, NULL, 10); + if (errno) + die("Error parsing device id: %m"); + fs = bcache_fs_open(path); } else { char *dev = arg_pop(); @@ -304,6 +311,13 @@ int cmd_device_evacuate(int argc, char *argv[]) unsigned dev_idx; struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx); + struct bch_ioctl_dev_usage u = bchu_dev_usage(fs, dev_idx); + + if (u.state == BCH_MEMBER_STATE_rw) { + printf("Setting %s readonly\n", dev_path); + bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0); + } + return bchu_data(fs, (struct bch_ioctl_data) { .op = BCH_DATA_OP_MIGRATE, .start = POS_MIN, @@ -319,6 +333,7 @@ static void device_set_state_usage(void) "\n" "Options:\n" " -f, --force Force, if data redundancy will be degraded\n" + " -o, --offline Set state of an offline device\n" " -h, --help display this help and exit\n" "Report bugs to "); exit(EXIT_SUCCESS); @@ -328,16 +343,21 @@ int cmd_device_set_state(int argc, char *argv[]) { static const struct option longopts[] = { { "force", 0, NULL, 'f' }, + { "offline", 0, NULL, 'o' }, { "help", 0, NULL, 'h' }, { NULL } }; int opt, flags = 0; + bool offline = false; - while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1) + while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1) switch (opt) { case 'f': flags |= BCH_FORCE_IF_DEGRADED; break; + case 'o': + offline = true; + break; case 'h': device_set_state_usage(); } @@ -352,12 +372,33 @@ int cmd_device_set_state(int argc, char *argv[]) die("Please supply a device state"); unsigned new_state = read_string_list_or_die(new_state_str, - bch2_dev_state, "device state"); + bch2_member_states, "device state"); - unsigned dev_idx; - struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx); + if (!offline) { + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx); + + bchu_disk_set_state(fs, dev_idx, new_state, flags); + + bcache_fs_close(fs); + } else { + struct bch_opts opts = bch2_opts_empty(); + struct bch_sb_handle sb = { NULL }; + + int ret = bch2_read_super(dev_path, &opts, &sb); + if (ret) + die("error opening %s: %s", dev_path, strerror(-ret)); + + struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx; + + SET_BCH_MEMBER_STATE(m, new_state); + + le64_add_cpu(&sb.sb->seq, 1); + + bch2_super_write(sb.bdev->bd_fd, sb.sb); + bch2_free_super(&sb); + } - bchu_disk_set_state(fs, dev_idx, new_state, flags); return 0; } @@ -462,3 +503,103 @@ int cmd_device_resize(int argc, char *argv[]) } return 0; } + +static void device_resize_journal_usage(void) +{ + puts("bcachefs device resize-journal \n" + "Usage: bcachefs device resize-journal device [ size ]\n" + "\n" + "Options:\n" + " -h, --help display this help and exit\n" + "Report bugs to "); + exit(EXIT_SUCCESS); +} + +int cmd_device_resize_journal(int argc, char *argv[]) +{ + static const struct option longopts[] = { + { "help", 0, NULL, 'h' }, + { NULL } + }; + u64 size; + int opt; + + while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1) + switch (opt) { + case 'h': + device_resize_journal_usage(); + } + args_shift(optind); + + char *dev = arg_pop(); + if (!dev) + die("Please supply a device"); + + int dev_fd = xopen(dev, O_RDONLY); + + char *size_arg = arg_pop(); + if (!size_arg) + size = get_size(dev, dev_fd); + else if (bch2_strtoull_h(size_arg, &size)) + die("invalid size"); + + size >>= 9; + + if (argc) + die("Too many arguments"); + + struct stat dev_stat = xfstat(dev_fd); + + struct mntent *mount = dev_to_mount(dev); + if (mount) { + if (!S_ISBLK(dev_stat.st_mode)) + die("%s is mounted but isn't a block device?!", dev); + + struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir); + + unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev); + + struct bch_sb *sb = bchu_read_super(fs, -1); + if (idx >= sb->nr_devices) + die("error reading superblock: dev idx >= sb->nr_devices"); + + struct bch_sb_field_members *mi = bch2_sb_get_members(sb); + if (!mi) + die("error reading superblock: no member info"); + + /* could also just read this out of sysfs... meh */ + struct bch_member *m = mi->members + idx; + + u64 nbuckets = size / le16_to_cpu(m->bucket_size); + + printf("resizing journal on %s to %llu buckets\n", dev, nbuckets); + bchu_disk_resize_journal(fs, idx, nbuckets); + } else { + printf("%s is offline - starting:\n", dev); + + struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty()); + if (IS_ERR(c)) + die("error opening %s: %s", dev, strerror(-PTR_ERR(c))); + + struct bch_dev *ca, *resize = NULL; + unsigned i; + + for_each_online_member(ca, c, i) { + if (resize) + die("confused: more than one online device?"); + resize = ca; + percpu_ref_get(&resize->io_ref); + } + + u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size); + + printf("resizing journal on %s to %llu buckets\n", dev, nbuckets); + int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets); + if (ret) + fprintf(stderr, "resize error: %s\n", strerror(-ret)); + + percpu_ref_put(&resize->io_ref); + bch2_fs_stop(c); + } + return 0; +}