#include <sys/types.h>
#include <unistd.h>
+#include "libbcachefs/bcachefs.h"
+#include "libbcachefs/bcachefs_ioctl.h"
+#include "libbcachefs/errcode.h"
+#include "libbcachefs/journal.h"
+#include "libbcachefs/sb-members.h"
+#include "libbcachefs/super-io.h"
#include "cmds.h"
-#include "libbcache.h"
-#include "linux/bcache-ioctl.h"
+#include "libbcachefs.h"
+#include "libbcachefs/opts.h"
#include "tools-util.h"
-/* This code belongs under show_fs */
-#if 0
-
-struct bcache_dev {
- unsigned nr;
- const char *name;
-
- unsigned has_metadata;
- unsigned has_data;
- const char *state;
- unsigned tier;
-
- u64 bucket_size;
- u64 first_bucket;
- u64 nbuckets;
-
- /* XXX: dirty != used, it doesn't count metadata */
- u64 bytes_dirty;
-};
-
-static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
+int device_usage(void)
{
- return (struct bcache_dev) {
- .nr = nr,
- .name = dev_name,
-
- .has_metadata = read_file_u64(dir, "has_metadata"),
- .has_data = read_file_u64(dir, "has_data"),
- .state = read_file_str(dir, "state"),
- .tier = read_file_u64(dir, "tier"),
-
- .bucket_size = read_file_u64(dir, "bucket_size_bytes"),
- .first_bucket = read_file_u64(dir, "first_bucket"),
- .nbuckets = read_file_u64(dir, "nbuckets"),
- .bytes_dirty = read_file_u64(dir, "dirty_bytes"),
- };
+ puts("bcachefs device - manage devices within a running filesystem\n"
+ "Usage: bcachefs device <CMD> [OPTION]\n"
+ "\n"
+ "Commands:\n"
+ " add add a new device to an existing filesystem\n"
+ " remove remove a device from an existing filesystem\n"
+ " online re-add an existing member to a filesystem\n"
+ " offline take a device offline, without removing it\n"
+ " evacuate migrate data off a specific device\n"
+ " set-state mark a device as failed\n"
+ " resize resize filesystem on a device\n"
+ " resize-journal resize journal on a device\n"
+ "\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+ return 0;
}
-static void show_dev(struct bcache_dev *dev)
+static void device_add_usage(void)
{
- u64 capacity = (dev->nbuckets - dev->first_bucket) *
- dev->bucket_size;
- /*
- * XXX: show fragmentation information, cached/dirty information
- */
-
- printf("Device %u (/dev/%s):\n"
- " Has metadata:\t%u\n"
- " Has dirty data:\t%u\n"
- " State:\t\t%s\n"
- " Tier:\t\t%u\n"
- " Size:\t\t%llu\n"
- " Used:\t\t%llu\n"
- " Free:\t\t%llu\n"
- " Use%%:\t\t%llu\n",
- dev->nr, dev->name,
- dev->has_metadata,
- dev->has_data,
- dev->state,
- dev->tier,
- capacity,
- dev->bytes_dirty,
- capacity - dev->bytes_dirty,
- (dev->bytes_dirty * 100) / capacity);
+ puts("bcachefs device add - add a device to an existing filesystem\n"
+ "Usage: bcachefs device add [OPTION]... filesystem device\n"
+ "\n"
+ "Options:\n"
+ " -S, --fs_size=size Size of filesystem on device\n"
+ " -B, --bucket=size Bucket size\n"
+ " -D, --discard Enable discards\n"
+ " -l, --label=label Disk label\n"
+ " -f, --force Use device even if it appears to already be formatted\n"
+ " -h, --help Display this help and exit\n"
+ "\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
}
-int cmd_device_show(int argc, char *argv[])
+int cmd_device_add(int argc, char *argv[])
{
- int human_readable = 0;
- NihOption opts[] = {
- // { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
-
- { 'h', "human-readable", N_("print sizes in powers of 1024 (e.g., 1023M)"),
- NULL, NULL, &human_readable, NULL},
- NIH_OPTION_LAST
+ static const struct option longopts[] = {
+ { "fs_size", required_argument, NULL, 'S' },
+ { "bucket", required_argument, NULL, 'B' },
+ { "discard", no_argument, NULL, 'D' },
+ { "label", required_argument, NULL, 'l' },
+ { "force", no_argument, NULL, 'f' },
+ { "help", no_argument, NULL, 'h' },
+ { NULL }
};
- char **args = bch_nih_init(argc, argv, opts);
+ struct format_opts format_opts = format_opts_default();
+ struct dev_opts dev_opts = dev_opts_default();
+ bool force = false;
+ int opt;
- if (argc != 2)
- die("Please supply a single device");
+ while ((opt = getopt_long(argc, argv, "t:fh",
+ longopts, NULL)) != -1)
+ switch (opt) {
+ case 'S':
+ if (bch2_strtoull_h(optarg, &dev_opts.size))
+ die("invalid filesystem size");
+ break;
+ case 'B':
+ if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
+ die("bad bucket_size %s", optarg);
+ break;
+ case 'D':
+ dev_opts.discard = true;
+ break;
+ case 'l':
+ dev_opts.label = strdup(optarg);
+ break;
+ case 'f':
+ force = true;
+ break;
+ case 'h':
+ device_add_usage();
+ exit(EXIT_SUCCESS);
+ }
+ args_shift(optind);
+
+ char *fs_path = arg_pop();
+ if (!fs_path)
+ die("Please supply a filesystem");
- struct bcache_handle fs = bcache_fs_open(argv[1]);
- struct dirent *entry;
+ dev_opts.path = arg_pop();
+ if (!dev_opts.path)
+ die("Please supply a device");
- struct bcache_dev devices[256];
- unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
+ if (argc)
+ die("too many arguments");
- unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */
- memset(tiers, 0, sizeof(tiers));
+ struct bchfs_handle fs = bcache_fs_open(fs_path);
- while ((entry = readdir(fs.sysfs))) {
- unsigned nr;
- int pos = 0;
+ int ret = open_for_format(&dev_opts, force);
+ if (ret)
+ die("Error opening %s: %s", dev_opts.path, strerror(-ret));
- sscanf(entry->d_name, "cache%u%n", &nr, &pos);
- if (pos != strlen(entry->d_name))
- continue;
+ struct bch_opt_strs fs_opt_strs;
+ memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
- char link[PATH_MAX];
- if (readlinkat(dirfd(fs.sysfs), entry->d_name,
- link, sizeof(link)) < 0)
- die("readlink error: %s\n", strerror(errno));
+ struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
- char *dev_name = basename(dirname(link));
+ opt_set(fs_opts, block_size,
+ read_file_u64(fs.sysfs_fd, "options/block_size"));
+ opt_set(fs_opts, btree_node_size,
+ read_file_u64(fs.sysfs_fd, "options/btree_node_size"));
- int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
+ struct bch_sb *sb = bch2_format(fs_opt_strs,
+ fs_opts,
+ format_opts,
+ &dev_opts, 1);
+ free(sb);
+ fsync(dev_opts.bdev->bd_buffered_fd);
+ close(dev_opts.bdev->bd_buffered_fd);
- devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
- tiers[devices[nr_devices].tier]++;
- nr_devices++;
+ bchu_disk_add(fs, dev_opts.path);
+ return 0;
+}
+
+static void device_remove_usage(void)
+{
+ puts("bcachefs device_remove - remove a device from a filesystem\n"
+ "Usage:\n"
+ " bcachefs device remove <device>|<devid> <path>\n"
+ "\n"
+ "Options:\n"
+ " -f, --force Force removal, even if some data\n"
+ " couldn't be migrated\n"
+ " -F, --force-metadata Force removal, even if some metadata\n"
+ " couldn't be migrated\n"
+ " -h, --help display this help and exit\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+ exit(EXIT_SUCCESS);
+}
+
+int cmd_device_remove(int argc, char *argv[])
+{
+ static const struct option longopts[] = {
+ { "by-id", 0, NULL, 'i' },
+ { "force", 0, NULL, 'f' },
+ { "force-metadata", 0, NULL, 'F' },
+ { "help", 0, NULL, 'h' },
+ { NULL }
+ };
+ struct bchfs_handle fs;
+ bool by_id = false;
+ int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
- close(fd);
+ while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
+ switch (opt) {
+ case 'f':
+ flags |= BCH_FORCE_IF_DATA_LOST;
+ break;
+ case 'F':
+ flags |= BCH_FORCE_IF_METADATA_LOST;
+ break;
+ case 'h':
+ device_remove_usage();
+ }
+ args_shift(optind);
+
+ char *dev_str = arg_pop();
+ if (!dev_str)
+ die("Please supply a device");
+
+ char *end;
+ dev_idx = strtoul(dev_str, &end, 10);
+ if (*dev_str && !*end)
+ by_id = true;
+
+ char *fs_path = arg_pop();
+ if (fs_path) {
+ fs = bcache_fs_open(fs_path);
+
+ if (!by_id) {
+ dev_idx = bchu_dev_path_to_idx(fs, dev_str);
+ if (dev_idx < 0)
+ die("%s does not seem to be a member of %s",
+ dev_str, fs_path);
+ }
+ } else if (!by_id) {
+ fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
+ } else {
+ die("Filesystem path required when specifying device by id");
}
- for (i = 0; i < BCH_TIER_MAX; i++)
- if (tiers[i])
- nr_active_tiers++;
+ bchu_disk_remove(fs, dev_idx, flags);
+ return 0;
+}
- /* Print out devices sorted by tier: */
- bool first = true;
+static void device_online_usage(void)
+{
+ puts("bcachefs device online - readd a device to a running filesystem\n"
+ "Usage: bcachefs device online [OPTION]... device\n"
+ "\n"
+ "Options:\n"
+ " -h, --help Display this help and exit\n"
+ "\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+}
- for (i = 0; i < BCH_TIER_MAX; i++) {
- if (!tiers[i])
- continue;
+int cmd_device_online(int argc, char *argv[])
+{
+ int opt;
- if (nr_active_tiers > 1) {
- if (!first)
- printf("\n");
- first = false;
- printf("Tier %u:\n\n", i);
+ while ((opt = getopt(argc, argv, "h")) != -1)
+ switch (opt) {
+ case 'h':
+ device_online_usage();
+ exit(EXIT_SUCCESS);
}
+ args_shift(optind);
- for (j = 0; j < nr_devices; j++) {
- if (devices[j].tier != i)
- continue;
+ char *dev = arg_pop();
+ if (!dev)
+ die("Please supply a device");
- if (!first)
- printf("\n");
- first = false;
- show_dev(&devices[j]);
- }
- }
+ if (argc)
+ die("too many arguments");
+ int dev_idx;
+ struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
+ bchu_disk_online(fs, dev);
return 0;
}
-#endif
-int cmd_device_show(int argc, char *argv[])
+static void device_offline_usage(void)
{
- struct bch_sb *sb;
+ puts("bcachefs device offline - take a device offline, without removing it\n"
+ "Usage: bcachefs device offline [OPTION]... device\n"
+ "\n"
+ "Options:\n"
+ " -f, --force Force, if data redundancy will be degraded\n"
+ " -h, --help Display this help and exit\n"
+ "\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+}
- if (argc != 2)
- die("please supply a single device");
+int cmd_device_offline(int argc, char *argv[])
+{
+ static const struct option longopts[] = {
+ { "force", 0, NULL, 'f' },
+ { NULL }
+ };
+ int opt, flags = 0;
- sb = bcache_super_read(argv[1]);
- bcache_super_print(sb, HUMAN_READABLE);
+ while ((opt = getopt_long(argc, argv, "fh",
+ longopts, NULL)) != -1)
+ switch (opt) {
+ case 'f':
+ flags |= BCH_FORCE_IF_DEGRADED;
+ break;
+ case 'h':
+ device_offline_usage();
+ exit(EXIT_SUCCESS);
+ }
+ args_shift(optind);
+ char *dev = arg_pop();
+ if (!dev)
+ die("Please supply a device");
+
+ if (argc)
+ die("too many arguments");
+
+ int dev_idx;
+ struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
+ bchu_disk_offline(fs, dev_idx, flags);
return 0;
}
-static void device_add_usage(void)
+static void device_evacuate_usage(void)
{
- puts("bcache device_add - add a device to an existing filesystem\n"
- "Usage: bcache device_add [OPTION]... filesystem device\n"
+ puts("bcachefs device evacuate - move data off of a given device\n"
+ "Usage: bcachefs device evacuate [OPTION]... device\n"
"\n"
"Options:\n"
- " --fs_size=size Size of filesystem on device\n"
- " --bucket=size Bucket size\n"
- " --discard Enable discards\n"
- " -t, --tier=# Higher tier (e.g. 1) indicates slower devices\n"
- " -f, --force Use device even if it appears to already be formatted\n"
" -h, --help Display this help and exit\n"
"\n"
- "Report bugs to <linux-bcache@vger.kernel.org>");
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
}
-static const struct option device_add_opts[] = {
- { "fs_size", required_argument, NULL, 'S' },
- { "bucket", required_argument, NULL, 'B' },
- { "discard", no_argument, NULL, 'D' },
- { "tier", required_argument, NULL, 't' },
- { "force", no_argument, NULL, 'f' },
- { NULL }
-};
-
-int cmd_device_add(int argc, char *argv[])
+int cmd_device_evacuate(int argc, char *argv[])
{
- struct format_opts format_opts = format_opts_default();
- struct dev_opts dev_opts = { 0 };
- bool force = false;
int opt;
- while ((opt = getopt_long(argc, argv, "t:fh",
- device_add_opts, NULL)) != -1)
+ while ((opt = getopt(argc, argv, "h")) != -1)
switch (opt) {
- case 'S':
- if (bch_strtoull_h(optarg, &dev_opts.size))
- die("invalid filesystem size");
+ case 'h':
+ device_evacuate_usage();
+ exit(EXIT_SUCCESS);
+ }
+ args_shift(optind);
- dev_opts.size >>= 9;
- break;
- case 'B':
- dev_opts.bucket_size =
- hatoi_validate(optarg, "bucket size");
- break;
- case 'D':
- dev_opts.discard = true;
+ char *dev_path = arg_pop();
+ if (!dev_path)
+ die("Please supply a device");
+
+ if (argc)
+ die("too many arguments");
+
+ int dev_idx;
+ struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
+
+ struct bch_ioctl_dev_usage_v2 *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);
+ }
+
+ free(u);
+
+ return bchu_data(fs, (struct bch_ioctl_data) {
+ .op = BCH_DATA_OP_migrate,
+ .start_btree = 0,
+ .start_pos = POS_MIN,
+ .end_btree = BTREE_ID_NR,
+ .end_pos = POS_MAX,
+ .migrate.dev = dev_idx,
+ });
+}
+
+static void device_set_state_usage(void)
+{
+ puts("bcachefs device set-state\n"
+ "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
+ "\n"
+ "<new-state>: one of rw, ro, failed or spare\n"
+ "<path>: path to mounted filesystem, optional unless specifying device by id\n"
+ "\n"
+ "Options:\n"
+ " -f, --force Force, if data redundancy will be degraded\n"
+ " --force-if-data-lost Force, if data will be lost\n"
+ " -o, --offline Set state of an offline device\n"
+ " -h, --help display this help and exit\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+ exit(EXIT_SUCCESS);
+}
+
+int cmd_device_set_state(int argc, char *argv[])
+{
+ static const struct option longopts[] = {
+ { "force", 0, NULL, 'f' },
+ { "force-if-data-lost", 0, NULL, 'F' },
+ { "offline", 0, NULL, 'o' },
+ { "help", 0, NULL, 'h' },
+ { NULL }
+ };
+ struct bchfs_handle fs;
+ bool by_id = false;
+ int opt, flags = 0, dev_idx;
+ bool offline = false;
+
+ while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
+ switch (opt) {
+ case 'f':
+ flags |= BCH_FORCE_IF_DEGRADED;
break;
- case 't':
- if (kstrtouint(optarg, 10, &dev_opts.tier) ||
- dev_opts.tier >= BCH_TIER_MAX)
- die("invalid tier");
+ case 'F':
+ flags |= BCH_FORCE_IF_DEGRADED;
+ flags |= BCH_FORCE_IF_LOST;
break;
- case 'f':
- force = true;
+ case 'o':
+ offline = true;
break;
case 'h':
- device_add_usage();
- exit(EXIT_SUCCESS);
+ device_set_state_usage();
}
+ args_shift(optind);
- if (argc - optind != 2)
- die("Please supply a filesystem and a device to add");
+ char *new_state_str = arg_pop();
+ if (!new_state_str)
+ die("Please supply a device state");
- struct bcache_handle fs = bcache_fs_open(argv[optind]);
+ unsigned new_state = read_string_list_or_die(new_state_str,
+ bch2_member_states, "device state");
- dev_opts.path = argv[optind + 1];
- dev_opts.fd = open_for_format(dev_opts.path, force);
+ char *dev_str = arg_pop();
+ if (!dev_str)
+ die("Please supply a device");
- format_opts.block_size =
- read_file_u64(fs.sysfs_fd, "block_size_bytes") >> 9;
- format_opts.btree_node_size =
- read_file_u64(fs.sysfs_fd, "btree_node_size_bytes") >> 9;
+ char *end;
+ dev_idx = strtoul(dev_str, &end, 10);
+ if (*dev_str && !*end)
+ by_id = true;
- struct bch_sb *sb = bcache_format(format_opts, &dev_opts, 1);
- free(sb);
- fsync(dev_opts.fd);
- close(dev_opts.fd);
+ if (offline) {
+ struct bch_opts opts = bch2_opts_empty();
+ struct bch_sb_handle sb = { NULL };
- struct bch_ioctl_disk_add ia = {
- .dev = (__u64) dev_opts.path,
- };
+ if (by_id)
+ die("Cannot specify offline device by id");
+
+ int ret = bch2_read_super(dev_str, &opts, &sb);
+ if (ret)
+ die("error opening %s: %s", dev_str, bch2_err_str(ret));
- xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &ia);
+ struct bch_member *m = bch2_members_v2_get_mut(sb.sb, sb.sb->dev_idx);
+
+ SET_BCH_MEMBER_STATE(m, new_state);
+
+ le64_add_cpu(&sb.sb->seq, 1);
+
+ bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
+ ret = fsync(sb.bdev->bd_buffered_fd);
+ if (ret)
+ fprintf(stderr, "error writing superblock: fsync error (%m)");
+ bch2_free_super(&sb);
+ return ret;
+ }
+
+ char *fs_path = arg_pop();
+ if (fs_path) {
+ fs = bcache_fs_open(fs_path);
+
+ if (!by_id) {
+ dev_idx = bchu_dev_path_to_idx(fs, dev_str);
+ if (dev_idx < 0)
+ die("%s does not seem to be a member of %s",
+ dev_str, fs_path);
+ }
+ } else if (!by_id) {
+ fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
+ } else {
+ die("Filesystem path required when specifying device by id");
+ }
+
+ bchu_disk_set_state(fs, dev_idx, new_state, flags);
return 0;
}
-static void device_fail_usage(void)
+static void device_resize_usage(void)
{
- puts("bcache device_fail - mark a device as failed\n"
- "Usage: bcache device_fail filesystem [devices]\n"
+ puts("bcachefs device resize \n"
+ "Usage: bcachefs device resize device [ size ]\n"
"\n"
"Options:\n"
- " -f, --force Force removal, even if some data\n"
- " couldn't be migrated\n"
- " --force-metadata Force removal, even if some metadata\n"
- " couldn't be migrated\n"
" -h, --help display this help and exit\n"
- "Report bugs to <linux-bcache@vger.kernel.org>");
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
exit(EXIT_SUCCESS);
}
-int cmd_device_fail(int argc, char *argv[])
+int cmd_device_resize(int argc, char *argv[])
{
static const struct option longopts[] = {
- { "force-degraded", 0, NULL, 'f' },
- //{ "force-data-lost", 0, NULL, 'F' },
- //{ "force-metadata-lost", 0, NULL, 'F' },
{ "help", 0, NULL, 'h' },
{ NULL }
};
- int opt, force_degraded = 0, force_data = 0, force_metadata = 0;
+ u64 size;
+ int opt;
- while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
+ while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (opt) {
- case 'f':
- force_degraded = 1;
- break;
case 'h':
- device_fail_usage();
+ device_resize_usage();
}
+ args_shift(optind);
- if (argc - optind < 2)
- die("Please supply a filesystem and at least one device to fail");
+ char *dev = arg_pop();
+ if (!dev)
+ die("Please supply a device to resize");
- struct bcache_handle fs = bcache_fs_open(argv[optind]);
+ int dev_fd = xopen(dev, O_RDONLY);
- for (unsigned i = optind + 1; i < argc; i++) {
- struct bch_ioctl_disk_set_state ir = {
- .dev = (__u64) argv[i],
- .new_state = BCH_MEMBER_STATE_FAILED,
- };
+ char *size_arg = arg_pop();
+ if (!size_arg)
+ size = get_size(dev_fd);
+ else if (bch2_strtoull_h(size_arg, &size))
+ die("invalid size");
- if (force_degraded)
- ir.flags |= BCH_FORCE_IF_DEGRADED;
- if (force_data)
- ir.flags |= BCH_FORCE_IF_DATA_LOST;
- if (force_metadata)
- ir.flags |= BCH_FORCE_IF_METADATA_LOST;
+ size >>= 9;
- xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &ir);
- }
+ 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);
+
+ printf("Doing online resize of %s\n", 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_member m = bch2_sb_member_get(sb, idx);
+
+ u64 nbuckets = size / le16_to_cpu(m.bucket_size);
+
+ if (nbuckets < le64_to_cpu(m.nbuckets))
+ die("Shrinking not supported yet");
+
+ printf("resizing %s to %llu buckets\n", dev, nbuckets);
+ bchu_disk_resize(fs, idx, nbuckets);
+ } else {
+ printf("Doing offline resize of %s\n", dev);
+ struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
+ if (IS_ERR(c))
+ die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
+
+ struct bch_dev *resize = NULL;
+
+ for_each_online_member(c, ca) {
+ 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);
+
+ if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
+ die("Shrinking not supported yet");
+
+ printf("resizing %s to %llu buckets\n", dev, nbuckets);
+ int ret = bch2_dev_resize(c, resize, nbuckets);
+ if (ret)
+ fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
+
+ percpu_ref_put(&resize->io_ref);
+ bch2_fs_stop(c);
+ }
return 0;
}
-static void device_remove_usage(void)
+static void device_resize_journal_usage(void)
{
- puts("bcache device_remove - remove one or more devices from a filesystem\n"
- "Usage: bcache device_remove filesystem [devices]\n"
+ puts("bcachefs device resize-journal \n"
+ "Usage: bcachefs device resize-journal device size\n"
"\n"
"Options:\n"
- " -f, --force Force removal, even if some data\n"
- " couldn't be migrated\n"
- " --force-metadata Force removal, even if some metadata\n"
- " couldn't be migrated\n"
" -h, --help display this help and exit\n"
- "Report bugs to <linux-bcache@vger.kernel.org>");
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
exit(EXIT_SUCCESS);
}
-int cmd_device_remove(int argc, char *argv[])
+int cmd_device_resize_journal(int argc, char *argv[])
{
static const struct option longopts[] = {
- { "force", 0, NULL, 'f' },
- { "force-metadata", 0, NULL, 'F' },
- { "help", 0, NULL, 'h' },
+ { "help", 0, NULL, 'h' },
{ NULL }
};
- int opt, force_data = 0, force_metadata = 0;
+ u64 size;
+ int opt;
- while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
+ while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (opt) {
- case 'f':
- force_data = 1;
- break;
- case 'F':
- force_metadata = 1;
- break;
case 'h':
- device_remove_usage();
+ device_resize_journal_usage();
}
+ args_shift(optind);
- if (argc - optind < 2)
- die("Please supply a filesystem and at least one device to remove");
+ char *dev = arg_pop();
+ if (!dev)
+ die("Please supply a device");
- struct bcache_handle fs = bcache_fs_open(argv[optind]);
+ int dev_fd = xopen(dev, O_RDONLY);
- for (unsigned i = optind + 1; i < argc; i++) {
- struct bch_ioctl_disk_remove ir = {
- .dev = (__u64) argv[i],
- };
+ char *size_arg = arg_pop();
+ if (!size_arg)
+ die("Please supply a journal size");
+ else if (bch2_strtoull_h(size_arg, &size))
+ die("invalid size");
- if (force_data)
- ir.flags |= BCH_FORCE_IF_DATA_LOST;
- if (force_metadata)
- ir.flags |= BCH_FORCE_IF_METADATA_LOST;
+ size >>= 9;
- xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &ir);
- }
+ 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_member m = bch2_sb_member_get(sb, 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, bch2_err_str(PTR_ERR(c)));
+
+ struct bch_dev *resize = NULL;
+
+ for_each_online_member(c, ca) {
+ 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", bch2_err_str(ret));
+
+ percpu_ref_put(&resize->io_ref);
+ bch2_fs_stop(c);
+ }
return 0;
}