]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_option.c
Update bcachefs sources to fad6d13aa5 fixup! bcachefs: Add persistent counters
[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/opts.h"
24 #include "libbcachefs/super-io.h"
25
26 static void set_option_usage(void)
27 {
28         puts("bcachefs set-option \n"
29              "Usage: bcachefs set-option [OPTION].. device\n"
30              "\n"
31              "Options:\n");
32         bch2_opts_usage(OPT_MOUNT);
33         puts("  -h, --help                  display this help and exit\n"
34              "Report bugs to <linux-bcachefs@vger.kernel.org>");
35         exit(EXIT_SUCCESS);
36 }
37
38 int cmd_set_option(int argc, char *argv[])
39 {
40         struct bch_opt_strs new_opt_strs = bch2_cmdline_opts_get(&argc, argv, OPT_MOUNT);
41         struct bch_opts new_opts = bch2_parse_opts(new_opt_strs);
42         struct bch_opts open_opts = bch2_opts_empty();
43         unsigned i;
44         int opt, ret = 0;
45
46         opt_set(open_opts, nostart, true);
47
48         while ((opt = getopt(argc, argv, "h")) != -1)
49                 switch (opt) {
50                 case 'h':
51                         set_option_usage();
52                         break;
53                 }
54         args_shift(optind);
55
56         if (!argc) {
57                 fprintf(stderr, "Please supply device(s)\n");
58                 exit(EXIT_FAILURE);
59         }
60
61         for (i = 0; i < argc; i++)
62                 if (dev_mounted(argv[i]))
63                         goto online;
64
65         struct bch_fs *c = bch2_fs_open(argv, argc, open_opts);
66         if (IS_ERR(c)) {
67                 fprintf(stderr, "error opening %s: %s\n", argv[0], strerror(-PTR_ERR(c)));
68                 exit(EXIT_FAILURE);
69         }
70
71         for (i = 0; i < bch2_opts_nr; i++) {
72                 u64 v = bch2_opt_get_by_id(&new_opts, i);
73
74                 if (!bch2_opt_defined_by_id(&new_opts, i))
75                         continue;
76
77                 ret = bch2_opt_check_may_set(c, i, v);
78                 if (ret < 0) {
79                         fprintf(stderr, "error setting %s: %i\n",
80                                 bch2_opt_table[i].attr.name, ret);
81                         break;
82                 }
83
84                 bch2_opt_set_sb(c, bch2_opt_table + i, v);
85                 bch2_opt_set_by_id(&c->opts, i, v);
86         }
87
88         bch2_fs_stop(c);
89         return ret;
90 online:
91         {
92                 unsigned dev_idx;
93                 struct bchfs_handle fs = bchu_fs_open_by_dev(argv[i], &dev_idx);
94
95                 for (i = 0; i < bch2_opts_nr; i++) {
96                         if (!new_opt_strs.by_id[i])
97                                 continue;
98
99                         char *path = mprintf("options/%s", bch2_opt_table[i].attr.name);
100
101                         write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]);
102                         free(path);
103                 }
104         }
105         return 0;
106 }