]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
disk groups
[bcachefs-tools-debian] / cmd_device.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <libgen.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "libbcachefs/bcachefs_ioctl.h"
16 #include "libbcachefs/super-io.h"
17 #include "cmds.h"
18 #include "libbcachefs.h"
19 #include "libbcachefs/opts.h"
20 #include "tools-util.h"
21
22 static void device_add_usage(void)
23 {
24         puts("bcachefs device add - add a device to an existing filesystem\n"
25              "Usage: bcachefs device add [OPTION]... filesystem device\n"
26              "\n"
27              "Options:\n"
28              "      --fs_size=size          Size of filesystem on device\n"
29              "      --bucket=size           Bucket size\n"
30              "      --discard               Enable discards\n"
31              "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
32              "  -f, --force                 Use device even if it appears to already be formatted\n"
33              "  -h, --help                  Display this help and exit\n"
34              "\n"
35              "Report bugs to <linux-bcache@vger.kernel.org>");
36 }
37
38 int cmd_device_add(int argc, char *argv[])
39 {
40         static const struct option longopts[] = {
41                 { "fs_size",            required_argument,      NULL, 'S' },
42                 { "bucket",             required_argument,      NULL, 'B' },
43                 { "discard",            no_argument,            NULL, 'D' },
44                 { "group",              required_argument,      NULL, 'g' },
45                 { "force",              no_argument,            NULL, 'f' },
46                 { "help",               no_argument,            NULL, 'h' },
47                 { NULL }
48         };
49         struct format_opts format_opts  = format_opts_default();
50         struct dev_opts dev_opts        = dev_opts_default();
51         bool force = false;
52         int opt;
53
54         while ((opt = getopt_long(argc, argv, "t:fh",
55                                   longopts, NULL)) != -1)
56                 switch (opt) {
57                 case 'S':
58                         if (bch2_strtoull_h(optarg, &dev_opts.size))
59                                 die("invalid filesystem size");
60
61                         dev_opts.size >>= 9;
62                         break;
63                 case 'B':
64                         dev_opts.bucket_size =
65                                 hatoi_validate(optarg, "bucket size");
66                         break;
67                 case 'D':
68                         dev_opts.discard = true;
69                         break;
70                 case 'g':
71                         dev_opts.group = strdup(optarg);
72                         break;
73                 case 'f':
74                         force = true;
75                         break;
76                 case 'h':
77                         device_add_usage();
78                         exit(EXIT_SUCCESS);
79                 }
80         args_shift(optind);
81
82         char *fs_path = arg_pop();
83         if (!fs_path)
84                 die("Please supply a filesystem");
85
86         char *dev_path = arg_pop();
87         if (!dev_path)
88                 die("Please supply a device");
89
90         if (argc)
91                 die("too many arguments");
92
93         struct bchfs_handle fs = bcache_fs_open(fs_path);
94
95         dev_opts.path = dev_path;
96         dev_opts.fd = open_for_format(dev_opts.path, force);
97
98         format_opts.block_size  =
99                 read_file_u64(fs.sysfs_fd, "block_size") >> 9;
100         format_opts.btree_node_size =
101                 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
102
103         struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
104         free(sb);
105         fsync(dev_opts.fd);
106         close(dev_opts.fd);
107
108         bchu_disk_add(fs, dev_opts.path);
109         return 0;
110 }
111
112 static void device_remove_usage(void)
113 {
114         puts("bcachefs device_remove - remove a device from a filesystem\n"
115              "Usage: bcachefs device remove device\n"
116              "\n"
117              "Options:\n"
118              "  -f, --force                 Force removal, even if some data\n"
119              "                              couldn't be migrated\n"
120              "      --force-metadata        Force removal, even if some metadata\n"
121              "                              couldn't be migrated\n"
122              "  -h, --help                  display this help and exit\n"
123              "Report bugs to <linux-bcache@vger.kernel.org>");
124         exit(EXIT_SUCCESS);
125 }
126
127 int cmd_device_remove(int argc, char *argv[])
128 {
129         static const struct option longopts[] = {
130                 { "force",              0, NULL, 'f' },
131                 { "force-metadata",     0, NULL, 'F' },
132                 { "help",               0, NULL, 'h' },
133                 { NULL }
134         };
135         int opt, flags = BCH_FORCE_IF_DEGRADED;
136
137         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
138                 switch (opt) {
139                 case 'f':
140                         flags |= BCH_FORCE_IF_DATA_LOST;
141                         break;
142                 case 'F':
143                         flags |= BCH_FORCE_IF_METADATA_LOST;
144                         break;
145                 case 'h':
146                         device_remove_usage();
147                 }
148         args_shift(optind);
149
150         char *dev = arg_pop();
151         if (!dev)
152                 die("Please supply a device to remove");
153
154         if (argc)
155                 die("too many arguments");
156
157         unsigned dev_idx;
158         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
159         bchu_disk_remove(fs, dev_idx, flags);
160         return 0;
161 }
162
163 static void device_online_usage(void)
164 {
165         puts("bcachefs device online - readd a device to a running filesystem\n"
166              "Usage: bcachefs device online [OPTION]... device\n"
167              "\n"
168              "Options:\n"
169              "  -h, --help                  Display this help and exit\n"
170              "\n"
171              "Report bugs to <linux-bcache@vger.kernel.org>");
172 }
173
174 int cmd_device_online(int argc, char *argv[])
175 {
176         int opt;
177
178         while ((opt = getopt(argc, argv, "h")) != -1)
179                 switch (opt) {
180                 case 'h':
181                         device_online_usage();
182                         exit(EXIT_SUCCESS);
183                 }
184         args_shift(optind);
185
186         char *dev = arg_pop();
187         if (!dev)
188                 die("Please supply a device");
189
190         if (argc)
191                 die("too many arguments");
192
193         unsigned dev_idx;
194         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
195         bchu_disk_online(fs, dev);
196         return 0;
197 }
198
199 static void device_offline_usage(void)
200 {
201         puts("bcachefs device offline - take a device offline, without removing it\n"
202              "Usage: bcachefs device offline [OPTION]... device\n"
203              "\n"
204              "Options:\n"
205              "  -f, --force                 Force, if data redundancy will be degraded\n"
206              "  -h, --help                  Display this help and exit\n"
207              "\n"
208              "Report bugs to <linux-bcache@vger.kernel.org>");
209 }
210
211 int cmd_device_offline(int argc, char *argv[])
212 {
213         static const struct option longopts[] = {
214                 { "force",              0, NULL, 'f' },
215                 { NULL }
216         };
217         int opt, flags = 0;
218
219         while ((opt = getopt_long(argc, argv, "fh",
220                                   longopts, NULL)) != -1)
221                 switch (opt) {
222                 case 'f':
223                         flags |= BCH_FORCE_IF_DEGRADED;
224                         break;
225                 case 'h':
226                         device_offline_usage();
227                         exit(EXIT_SUCCESS);
228                 }
229         args_shift(optind);
230
231         char *dev = arg_pop();
232         if (!dev)
233                 die("Please supply a device");
234
235         if (argc)
236                 die("too many arguments");
237
238         unsigned dev_idx;
239         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
240         bchu_disk_offline(fs, dev_idx, flags);
241         return 0;
242 }
243
244 static void device_evacuate_usage(void)
245 {
246         puts("bcachefs device evacuate - move data off of a given device\n"
247              "Usage: bcachefs device evacuate [OPTION]... device\n"
248              "\n"
249              "Options:\n"
250              "  -h, --help                  Display this help and exit\n"
251              "\n"
252              "Report bugs to <linux-bcache@vger.kernel.org>");
253 }
254
255 int cmd_device_evacuate(int argc, char *argv[])
256 {
257         int opt;
258
259         while ((opt = getopt(argc, argv, "h")) != -1)
260                 switch (opt) {
261                 case 'h':
262                         device_evacuate_usage();
263                         exit(EXIT_SUCCESS);
264                 }
265         args_shift(optind);
266
267         char *dev_path = arg_pop();
268         if (!dev_path)
269                 die("Please supply a device");
270
271         if (argc)
272                 die("too many arguments");
273
274         unsigned dev_idx;
275         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
276
277         return bchu_data(fs, (struct bch_ioctl_data) {
278                 .op             = BCH_DATA_OP_MIGRATE,
279                 .start          = POS_MIN,
280                 .end            = POS_MAX,
281                 .migrate.dev    = dev_idx,
282         });
283 }
284
285 static void device_set_state_usage(void)
286 {
287         puts("bcachefs device set-state\n"
288              "Usage: bcachefs device set-state device new-state\n"
289              "\n"
290              "Options:\n"
291              "  -f, --force                 Force, if data redundancy will be degraded\n"
292              "  -h, --help                  display this help and exit\n"
293              "Report bugs to <linux-bcache@vger.kernel.org>");
294         exit(EXIT_SUCCESS);
295 }
296
297 int cmd_device_set_state(int argc, char *argv[])
298 {
299         static const struct option longopts[] = {
300                 { "force",                      0, NULL, 'f' },
301                 { "help",                       0, NULL, 'h' },
302                 { NULL }
303         };
304         int opt, flags = 0;
305
306         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
307                 switch (opt) {
308                 case 'f':
309                         flags |= BCH_FORCE_IF_DEGRADED;
310                         break;
311                 case 'h':
312                         device_set_state_usage();
313                 }
314         args_shift(optind);
315
316         char *dev_path = arg_pop();
317         if (!dev_path)
318                 die("Please supply a device");
319
320         char *new_state_str = arg_pop();
321         if (!new_state_str)
322                 die("Please supply a device state");
323
324         unsigned new_state = read_string_list_or_die(new_state_str,
325                                         bch2_dev_state, "device state");
326
327         unsigned dev_idx;
328         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
329
330         bchu_disk_set_state(fs, dev_idx, new_state, flags);
331         return 0;
332 }
333
334 static void device_resize_usage(void)
335 {
336         puts("bcachefs device resize \n"
337              "Usage: bcachefs device resize device [ size ]\n"
338              "\n"
339              "Options:\n"
340              "  -h, --help                  display this help and exit\n"
341              "Report bugs to <linux-bcache@vger.kernel.org>");
342         exit(EXIT_SUCCESS);
343 }
344
345 int cmd_device_resize(int argc, char *argv[])
346 {
347         static const struct option longopts[] = {
348                 { "help",                       0, NULL, 'h' },
349                 { NULL }
350         };
351         u64 size;
352         int opt;
353
354         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
355                 switch (opt) {
356                 case 'h':
357                         device_resize_usage();
358                 }
359         args_shift(optind);
360
361         char *dev = arg_pop();
362         if (!dev)
363                 die("Please supply a device to resize");
364
365         int dev_fd = xopen(dev, O_RDONLY);
366
367         char *size_arg = arg_pop();
368         if (!size_arg)
369                 size = get_size(dev, dev_fd);
370         else if (bch2_strtoull_h(size_arg, &size))
371                 die("invalid size");
372
373         size >>= 9;
374
375         if (argc)
376                 die("Too many arguments");
377
378         struct stat dev_stat = xfstat(dev_fd);
379
380         char *mount = dev_to_mount(dev);
381         if (mount) {
382                 if (!S_ISBLK(dev_stat.st_mode))
383                         die("%s is mounted but isn't a block device?!", dev);
384
385                 printf("Doing online resize of %s\n", dev);
386
387                 struct bchfs_handle fs = bcache_fs_open(mount);
388
389                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
390
391                 struct bch_sb *sb = bchu_read_super(fs, -1);
392                 if (idx >= sb->nr_devices)
393                         die("error reading superblock: dev idx >= sb->nr_devices");
394
395                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
396                 if (!mi)
397                         die("error reading superblock: no member info");
398
399                 /* could also just read this out of sysfs... meh */
400                 struct bch_member *m = mi->members + idx;
401
402                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
403
404                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
405                 bchu_disk_resize(fs, idx, nbuckets);
406         } else {
407                 printf("Doing offline resize of %s\n", dev);
408
409                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
410                 if (IS_ERR(c))
411                         die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
412
413                 struct bch_dev *ca, *resize = NULL;
414                 unsigned i;
415
416                 for_each_online_member(ca, c, i) {
417                         if (resize)
418                                 die("confused: more than one online device?");
419                         resize = ca;
420                         percpu_ref_get(&resize->io_ref);
421                 }
422
423                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
424
425                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
426                 int ret = bch2_dev_resize(c, resize, nbuckets);
427                 if (ret)
428                         fprintf(stderr, "resize error: %s\n", strerror(-ret));
429
430                 percpu_ref_put(&resize->io_ref);
431                 bch2_fs_stop(c);
432         }
433         return 0;
434 }