]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_option.c
cmd_migrate: Fix fsck invocation
[bcachefs-tools-debian] / cmd_option.c
1 /*
2  * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3  *
4  * GPLv2
5  */
6 #include <ctype.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <getopt.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19 #include <uuid/uuid.h>
20
21 #include "cmds.h"
22 #include "libbcachefs.h"
23 #include "libbcachefs/errcode.h"
24 #include "libbcachefs/opts.h"
25 #include "libbcachefs/super-io.h"
26
27 static void set_option_usage(void)
28 {
29         puts("bcachefs set-option \n"
30              "Usage: bcachefs set-option [OPTION].. device\n"
31              "\n"
32              "Options:\n");
33         bch2_opts_usage(OPT_MOUNT);
34         puts("  -h, --help                  display this help and exit\n"
35              "Report bugs to <linux-bcachefs@vger.kernel.org>");
36         exit(EXIT_SUCCESS);
37 }
38
39 int cmd_set_option(int argc, char *argv[])
40 {
41         struct bch_opt_strs new_opt_strs = bch2_cmdline_opts_get(&argc, argv, OPT_MOUNT);
42         struct bch_opts new_opts = bch2_parse_opts(new_opt_strs);
43         struct bch_opts open_opts = bch2_opts_empty();
44         unsigned i;
45         int opt, ret = 0;
46
47         opt_set(open_opts, nostart, true);
48
49         while ((opt = getopt(argc, argv, "h")) != -1)
50                 switch (opt) {
51                 case 'h':
52                         set_option_usage();
53                         break;
54                 }
55         args_shift(optind);
56
57         if (!argc) {
58                 fprintf(stderr, "Please supply device(s)\n");
59                 exit(EXIT_FAILURE);
60         }
61
62         for (i = 0; i < argc; i++)
63                 if (dev_mounted(argv[i]))
64                         goto online;
65
66         struct bch_fs *c = bch2_fs_open(argv, argc, open_opts);
67         if (IS_ERR(c)) {
68                 fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
69                 exit(EXIT_FAILURE);
70         }
71
72         for (i = 0; i < bch2_opts_nr; i++) {
73                 u64 v = bch2_opt_get_by_id(&new_opts, i);
74
75                 if (!bch2_opt_defined_by_id(&new_opts, i))
76                         continue;
77
78                 ret = bch2_opt_check_may_set(c, i, v);
79                 if (ret < 0) {
80                         fprintf(stderr, "error setting %s: %i\n",
81                                 bch2_opt_table[i].attr.name, ret);
82                         break;
83                 }
84
85                 bch2_opt_set_sb(c, bch2_opt_table + i, v);
86                 bch2_opt_set_by_id(&c->opts, i, v);
87         }
88
89         bch2_fs_stop(c);
90         return ret;
91 online:
92         {
93                 unsigned dev_idx;
94                 struct bchfs_handle fs = bchu_fs_open_by_dev(argv[i], &dev_idx);
95
96                 for (i = 0; i < bch2_opts_nr; i++) {
97                         if (!new_opt_strs.by_id[i])
98                                 continue;
99
100                         char *path = mprintf("options/%s", bch2_opt_table[i].attr.name);
101
102                         write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]);
103                         free(path);
104                 }
105         }
106         return 0;
107 }