diff --git a/bcachefs.c b/bcachefs.c index b4958f13..3a84c38a 100644 --- a/bcachefs.c +++ b/bcachefs.c @@ -128,6 +128,8 @@ static int device_cmds(int argc, char *argv[]) return cmd_device_set_state(argc, argv); if (!strcmp(cmd, "resize")) return cmd_device_resize(argc, argv); + if (!strcmp(cmd, "resize-journal")) + return cmd_device_resize_journal(argc, argv); usage(); return 0; diff --git a/cmd_device.c b/cmd_device.c index c311324a..a7ff3615 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" @@ -496,3 +498,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; +} diff --git a/cmds.h b/cmds.h index bcd27adc..a05c98b9 100644 --- a/cmds.h +++ b/cmds.h @@ -28,6 +28,7 @@ int cmd_device_offline(int argc, char *argv[]); int cmd_device_evacuate(int argc, char *argv[]); int cmd_device_set_state(int argc, char *argv[]); int cmd_device_resize(int argc, char *argv[]); +int cmd_device_resize_journal(int argc, char *argv[]); int cmd_data_rereplicate(int argc, char *argv[]); diff --git a/libbcachefs.h b/libbcachefs.h index 30add92c..6ae24e9f 100644 --- a/libbcachefs.h +++ b/libbcachefs.h @@ -214,6 +214,19 @@ static inline void bchu_disk_resize(struct bchfs_handle fs, xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE, &i); } +static inline void bchu_disk_resize_journal(struct bchfs_handle fs, + unsigned idx, + u64 nbuckets) +{ + struct bch_ioctl_disk_resize i = { + .flags = BCH_BY_INDEX, + .dev = idx, + .nbuckets = nbuckets, + }; + + xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_RESIZE_JOURNAL, &i); +} + int bchu_data(struct bchfs_handle, struct bch_ioctl_data); struct dev_name {