1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Authors: Kent Overstreet <kent.overstreet@gmail.com>
*
* GPLv2
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include "cmds.h"
#include "libbcachefs.h"
#include "libbcachefs/errcode.h"
#include "libbcachefs/opts.h"
#include "libbcachefs/super-io.h"
static void set_option_usage(void)
{
puts("bcachefs set-fs-option \n"
"Usage: bcachefs set-fs-option [OPTION].. device\n"
"\n"
"Options:\n");
bch2_opts_usage(OPT_MOUNT|OPT_RUNTIME);
puts(" -d, --dev-idx index for device specific options\n"
" -h, --help display this help and exit\n"
"Report bugs to <linux-bcachefs@vger.kernel.org>");
exit(EXIT_SUCCESS);
}
static int name_to_dev_idx(struct bch_fs *c, const char *dev)
{
int ret = -1;
rcu_read_lock();
for_each_member_device_rcu(c, ca, NULL)
if (!strcmp(ca->name, dev)) {
ret = ca->dev_idx;
break;
}
rcu_read_unlock();
return ret;
}
int cmd_set_option(int argc, char *argv[])
{
struct bch_opt_strs new_opt_strs = bch2_cmdline_opts_get(&argc, argv, OPT_MOUNT|OPT_DEVICE);
struct bch_opts new_opts = bch2_parse_opts(new_opt_strs);
DARRAY(unsigned) dev_idxs = {};
int opt, ret = 0;
static const struct option longopts[] = {
{ "dev-idx", required_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ NULL }
};
while ((opt = getopt_long(argc, argv, "d:h", longopts, NULL)) != -1)
switch (opt) {
case 'd': {
unsigned dev_idx;
if (kstrtoint(optarg, 10, &dev_idx))
die("error parsing %s", optarg);
darray_push(&dev_idxs, dev_idx);
break;
}
case 'h':
set_option_usage();
break;
}
args_shift(optind);
if (!argc) {
fprintf(stderr, "Please supply device(s)\n");
exit(EXIT_FAILURE);
}
bool online = false;
unsigned i;
for (i = 0; i < argc; i++)
if (dev_mounted(argv[i])) {
online = true;
break;
}
if (!online) {
struct bch_opts open_opts = bch2_opts_empty();
opt_set(open_opts, nostart, true);
struct bch_fs *c = bch2_fs_open(argv, argc, open_opts);
if (IS_ERR(c)) {
fprintf(stderr, "error opening %s: %s\n", argv[0], bch2_err_str(PTR_ERR(c)));
exit(EXIT_FAILURE);
}
for (i = 0; i < bch2_opts_nr; i++) {
const struct bch_option *opt = bch2_opt_table + i;
u64 v = bch2_opt_get_by_id(&new_opts, i);
if (!bch2_opt_defined_by_id(&new_opts, i))
continue;
if (!(opt->flags & (OPT_FS|OPT_DEVICE)))
fprintf(stderr, "Can't set option %s\n", opt->attr.name);
if (opt->flags & OPT_FS) {
ret = bch2_opt_check_may_set(c, NULL, i, v);
if (ret < 0) {
fprintf(stderr, "error setting %s: %i\n", opt->attr.name, ret);
continue;
}
bch2_opt_set_sb(c, NULL, opt, v);
}
if (opt->flags & OPT_DEVICE) {
if (dev_idxs.nr) {
darray_for_each(dev_idxs, dev) {
struct bch_dev *ca = bch2_dev_tryget_noerror(c, *dev);
if (!ca) {
fprintf(stderr, "Couldn't look up device %u\n", *dev);
continue;
}
ret = bch2_opt_check_may_set(c, ca, i, v);
if (ret < 0) {
fprintf(stderr, "error setting %s: %i\n", opt->attr.name, ret);
continue;
}
bch2_opt_set_sb(c, ca, opt, v);
bch2_dev_put(ca);
}
} else {
for (unsigned dev = 0; dev < argc; dev++) {
int dev_idx = name_to_dev_idx(c, argv[dev]);
if (dev_idx < 0) {
fprintf(stderr, "Couldn't look up device %s\n", argv[i]);
continue;
}
bch2_opt_set_sb(c, c->devs[dev_idx], opt, v);
}
}
}
}
bch2_fs_stop(c);
return ret;
} else {
unsigned dev_idx;
struct bchfs_handle fs = bchu_fs_open_by_dev(argv[i], &dev_idx);
for (i = 0; i < argc; i++) {
struct bchfs_handle fs2 = bchu_fs_open_by_dev(argv[i], &dev_idx);
if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs2.uuid)))
die("Filesystem mounted, but not all devices are members");
bcache_fs_close(fs2);
}
for (i = 0; i < bch2_opts_nr; i++) {
if (!new_opt_strs.by_id[i])
continue;
const struct bch_option *opt = bch2_opt_table + i;
if (!(opt->flags & (OPT_FS|OPT_DEVICE)))
fprintf(stderr, "Can't set option %s\n", opt->attr.name);
if (opt->flags & OPT_FS) {
char *path = mprintf("options/%s", opt->attr.name);
write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]);
free(path);
}
if (opt->flags & OPT_DEVICE) {
for (unsigned dev = 0; dev < argc; dev++) {
struct bchfs_handle fs2 = bchu_fs_open_by_dev(argv[i], &dev_idx);
bcache_fs_close(fs2);
char *path = mprintf("dev-%u/%s", dev_idx, opt->attr.name);
write_file_str(fs.sysfs_fd, path, new_opt_strs.by_id[i]);
free(path);
}
}
}
}
return 0;
}
|