From e142c4ca01fe36a2936e1287e22e57c491b65109 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Thu, 26 May 2022 16:26:53 -0400 Subject: [PATCH] New subcommand: set-option This is for setting superblock options on existing filesystems, either online or offline. Signed-off-by: Kent Overstreet --- bcachefs.c | 9 +++-- cmd_option.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ cmds.h | 1 + tools-util.c | 11 ++++++ tools-util.h | 1 + 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 cmd_option.c diff --git a/bcachefs.c b/bcachefs.c index 1ab36204..31d96287 100644 --- a/bcachefs.c +++ b/bcachefs.c @@ -33,6 +33,7 @@ static void usage(void) "Superblock commands:\n" " format Format a new filesystem\n" " show-super Dump superblock information to stdout\n" + " set-option Set a filesystem option\n" "\n" "Repair:\n" " fsck Check an existing filesystem for errors\n" @@ -59,9 +60,9 @@ static void usage(void) " device resize-journal Resize journal on a device\n" "\n" "Commands for managing subvolumes and snapshots:\n" - " subvolume create Create a new subvolume\n" - " subvolume delete Delete an existing subvolume\n" - " subvolume snapshot Create a snapshot\n" + " subvolume create Create a new subvolume\n" + " subvolume delete Delete an existing subvolume\n" + " subvolume snapshot Create a snapshot\n" "\n" "Commands for managing filesystem data:\n" " data rereplicate Rereplicate degraded data\n" @@ -199,6 +200,8 @@ int main(int argc, char *argv[]) return cmd_version(argc, argv); if (!strcmp(cmd, "show-super")) return cmd_show_super(argc, argv); + if (!strcmp(cmd, "set-option")) + return cmd_set_option(argc, argv); if (argc < 2) { printf("%s: missing command\n", argv[0]); diff --git a/cmd_option.c b/cmd_option.c new file mode 100644 index 00000000..b5f63269 --- /dev/null +++ b/cmd_option.c @@ -0,0 +1,104 @@ +/* + * Authors: Kent Overstreet + * + * GPLv2 + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "cmds.h" +#include "libbcachefs.h" +#include "libbcachefs/opts.h" +#include "libbcachefs/super-io.h" + +static void set_option_usage(void) +{ + puts("bcachefs set-option \n" + "Usage: bcachefs set-option [OPTION].. device\n" + "\n" + "Options:\n"); + bch2_opts_usage(OPT_MOUNT); + puts(" -h, --help display this help and exit\n" + "Report bugs to "); + exit(EXIT_SUCCESS); +} + +int cmd_set_option(int argc, char *argv[]) +{ + struct bch_opt_strs new_opt_strs = bch2_cmdline_opts_get(&argc, argv, OPT_MOUNT); + struct bch_opts new_opts = bch2_parse_opts(new_opt_strs); + struct bch_opts open_opts = bch2_opts_empty(); + unsigned i; + int opt, ret = 0; + + opt_set(open_opts, nostart, true); + + while ((opt = getopt(argc, argv, "h")) != -1) + switch (opt) { + case 'h': + set_option_usage(); + break; + } + args_shift(optind); + + if (!argc) { + fprintf(stderr, "Please supply device(s)\n"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < argc; i++) + if (dev_mounted(argv[i])) + goto online; + + struct bch_fs *c = bch2_fs_open(argv, argc, open_opts); + if (IS_ERR(c)) { + fprintf(stderr, "error opening %s: %s\n", argv[0], strerror(-PTR_ERR(c))); + exit(EXIT_FAILURE); + } + + for (i = 0; i < bch2_opts_nr; i++) { + u64 v = bch2_opt_get_by_id(&new_opts, i); + + if (!bch2_opt_defined_by_id(&new_opts, i)) + continue; + + ret = bch2_opt_check_may_set(c, i, v); + if (ret < 0) { + fprintf(stderr, "error setting %s: %i\n", + bch2_opt_table[i].attr.name, ret); + break; + } + + bch2_opt_set_sb(c, bch2_opt_table + i, v); + bch2_opt_set_by_id(&c->opts, i, v); + } + + bch2_fs_stop(c); + return ret; +online: + unsigned dev_idx; + struct bchfs_handle fs = bchu_fs_open_by_dev(argv[i], &dev_idx); + + for (i = 0; i < bch2_opts_nr; i++) { + if (!new_opt_strs.by_id[i]) + continue; + + char *path = mprintf("options/%s", bch2_opt_table[i].attr.name); + + write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]); + free(path); + } + return 0; +} diff --git a/cmds.h b/cmds.h index b217e2d6..c18a87fd 100644 --- a/cmds.h +++ b/cmds.h @@ -11,6 +11,7 @@ int cmd_format(int argc, char *argv[]); int cmd_show_super(int argc, char *argv[]); +int cmd_set_option(int argc, char *argv[]); #if 0 int cmd_assemble(int argc, char *argv[]); diff --git a/tools-util.c b/tools-util.c index baf24ae5..f29d2026 100644 --- a/tools-util.c +++ b/tools-util.c @@ -128,6 +128,17 @@ struct stat xstat(const char *path) /* File parsing (i.e. sysfs) */ +void write_file_str(int dirfd, const char *path, const char *str) +{ + int fd = xopenat(dirfd, path, O_WRONLY); + ssize_t wrote, len = strlen(str); + + wrote = write(fd, str, len); + if (wrote != len) + die("read error: %m"); + close(fd); +} + char *read_file_str(int dirfd, const char *path) { int fd = xopenat(dirfd, path, O_RDONLY); diff --git a/tools-util.h b/tools-util.h index 8ed00b15..e9f0ce94 100644 --- a/tools-util.h +++ b/tools-util.h @@ -53,6 +53,7 @@ struct stat xstat(const char *); _ret; \ }) +void write_file_str(int, const char *, const char *); char *read_file_str(int, const char *); u64 read_file_u64(int, const char *);