]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
Update bcachefs sources to cbb2e45634dd bcachefs: fix simulateously upgrading & downg...
[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.h"
16 #include "libbcachefs/bcachefs_ioctl.h"
17 #include "libbcachefs/errcode.h"
18 #include "libbcachefs/journal.h"
19 #include "libbcachefs/sb-members.h"
20 #include "libbcachefs/super-io.h"
21 #include "cmds.h"
22 #include "libbcachefs.h"
23 #include "libbcachefs/opts.h"
24 #include "tools-util.h"
25
26 int device_usage(void)
27 {
28        puts("bcachefs device - manage devices within a running filesystem\n"
29             "Usage: bcachefs device <CMD> [OPTION]\n"
30             "\n"
31             "Commands:\n"
32             "  add                     add a new device to an existing filesystem\n"
33             "  remove                  remove a device from an existing filesystem\n"
34             "  online                  re-add an existing member to a filesystem\n"
35             "  offline                 take a device offline, without removing it\n"
36             "  evacuate                migrate data off a specific device\n"
37             "  set-state               mark a device as failed\n"
38             "  resize                  resize filesystem on a device\n"
39             "  resize-journal          resize journal on a device\n"
40             "\n"
41             "Report bugs to <linux-bcachefs@vger.kernel.org>");
42        return 0;
43 }
44
45 static void device_add_usage(void)
46 {
47         puts("bcachefs device add - add a device to an existing filesystem\n"
48              "Usage: bcachefs device add [OPTION]... filesystem device\n"
49              "\n"
50              "Options:\n"
51              "  -S, --fs_size=size          Size of filesystem on device\n"
52              "  -B, --bucket=size           Bucket size\n"
53              "  -D, --discard               Enable discards\n"
54              "  -l, --label=label           Disk label\n"
55              "  -f, --force                 Use device even if it appears to already be formatted\n"
56              "  -h, --help                  Display this help and exit\n"
57              "\n"
58              "Report bugs to <linux-bcachefs@vger.kernel.org>");
59 }
60
61 int cmd_device_add(int argc, char *argv[])
62 {
63         static const struct option longopts[] = {
64                 { "fs_size",            required_argument,      NULL, 'S' },
65                 { "bucket",             required_argument,      NULL, 'B' },
66                 { "discard",            no_argument,            NULL, 'D' },
67                 { "label",              required_argument,      NULL, 'l' },
68                 { "force",              no_argument,            NULL, 'f' },
69                 { "help",               no_argument,            NULL, 'h' },
70                 { NULL }
71         };
72         struct format_opts format_opts  = format_opts_default();
73         struct dev_opts dev_opts        = dev_opts_default();
74         bool force = false;
75         int opt;
76
77         while ((opt = getopt_long(argc, argv, "t:fh",
78                                   longopts, NULL)) != -1)
79                 switch (opt) {
80                 case 'S':
81                         if (bch2_strtoull_h(optarg, &dev_opts.size))
82                                 die("invalid filesystem size");
83                         break;
84                 case 'B':
85                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
86                                 die("bad bucket_size %s", optarg);
87                         break;
88                 case 'D':
89                         dev_opts.discard = true;
90                         break;
91                 case 'l':
92                         dev_opts.label = strdup(optarg);
93                         break;
94                 case 'f':
95                         force = true;
96                         break;
97                 case 'h':
98                         device_add_usage();
99                         exit(EXIT_SUCCESS);
100                 }
101         args_shift(optind);
102
103         char *fs_path = arg_pop();
104         if (!fs_path)
105                 die("Please supply a filesystem");
106
107         dev_opts.path = arg_pop();
108         if (!dev_opts.path)
109                 die("Please supply a device");
110
111         if (argc)
112                 die("too many arguments");
113
114         struct bchfs_handle fs = bcache_fs_open(fs_path);
115
116         int ret = open_for_format(&dev_opts, force);
117         if (ret)
118                 die("Error opening %s: %s", dev_opts.path, strerror(-ret));
119
120         struct bch_opt_strs fs_opt_strs;
121         memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
122
123         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
124
125         opt_set(fs_opts, block_size,
126                 read_file_u64(fs.sysfs_fd, "options/block_size"));
127         opt_set(fs_opts, btree_node_size,
128                 read_file_u64(fs.sysfs_fd, "options/btree_node_size"));
129
130         struct bch_sb *sb = bch2_format(fs_opt_strs,
131                                         fs_opts,
132                                         format_opts,
133                                         &dev_opts, 1);
134         free(sb);
135         fsync(dev_opts.bdev->bd_buffered_fd);
136         close(dev_opts.bdev->bd_buffered_fd);
137
138         bchu_disk_add(fs, dev_opts.path);
139         return 0;
140 }
141
142 static void device_remove_usage(void)
143 {
144         puts("bcachefs device_remove - remove a device from a filesystem\n"
145              "Usage:\n"
146              "  bcachefs device remove <device>|<devid> <path>\n"
147              "\n"
148              "Options:\n"
149              "  -f, --force                 Force removal, even if some data\n"
150              "                              couldn't be migrated\n"
151              "  -F, --force-metadata        Force removal, even if some metadata\n"
152              "                              couldn't be migrated\n"
153              "  -h, --help                  display this help and exit\n"
154              "Report bugs to <linux-bcachefs@vger.kernel.org>");
155         exit(EXIT_SUCCESS);
156 }
157
158 int cmd_device_remove(int argc, char *argv[])
159 {
160         static const struct option longopts[] = {
161                 { "by-id",              0, NULL, 'i' },
162                 { "force",              0, NULL, 'f' },
163                 { "force-metadata",     0, NULL, 'F' },
164                 { "help",               0, NULL, 'h' },
165                 { NULL }
166         };
167         struct bchfs_handle fs;
168         bool by_id = false;
169         int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
170
171         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
172                 switch (opt) {
173                 case 'f':
174                         flags |= BCH_FORCE_IF_DATA_LOST;
175                         break;
176                 case 'F':
177                         flags |= BCH_FORCE_IF_METADATA_LOST;
178                         break;
179                 case 'h':
180                         device_remove_usage();
181                 }
182         args_shift(optind);
183
184         char *dev_str = arg_pop();
185         if (!dev_str)
186                 die("Please supply a device");
187
188         char *end;
189         dev_idx = strtoul(dev_str, &end, 10);
190         if (*dev_str && !*end)
191                 by_id = true;
192
193         char *fs_path = arg_pop();
194         if (fs_path) {
195                 fs = bcache_fs_open(fs_path);
196
197                 if (!by_id) {
198                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
199                         if (dev_idx < 0)
200                                 die("%s does not seem to be a member of %s",
201                                     dev_str, fs_path);
202                 }
203         } else if (!by_id) {
204                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
205         } else {
206                 die("Filesystem path required when specifying device by id");
207         }
208
209         bchu_disk_remove(fs, dev_idx, flags);
210         return 0;
211 }
212
213 static void device_online_usage(void)
214 {
215         puts("bcachefs device online - readd a device to a running filesystem\n"
216              "Usage: bcachefs device online [OPTION]... device\n"
217              "\n"
218              "Options:\n"
219              "  -h, --help                  Display this help and exit\n"
220              "\n"
221              "Report bugs to <linux-bcachefs@vger.kernel.org>");
222 }
223
224 int cmd_device_online(int argc, char *argv[])
225 {
226         int opt;
227
228         while ((opt = getopt(argc, argv, "h")) != -1)
229                 switch (opt) {
230                 case 'h':
231                         device_online_usage();
232                         exit(EXIT_SUCCESS);
233                 }
234         args_shift(optind);
235
236         char *dev = arg_pop();
237         if (!dev)
238                 die("Please supply a device");
239
240         if (argc)
241                 die("too many arguments");
242
243         int dev_idx;
244         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
245         bchu_disk_online(fs, dev);
246         return 0;
247 }
248
249 static void device_offline_usage(void)
250 {
251         puts("bcachefs device offline - take a device offline, without removing it\n"
252              "Usage: bcachefs device offline [OPTION]... device\n"
253              "\n"
254              "Options:\n"
255              "  -f, --force                 Force, if data redundancy will be degraded\n"
256              "  -h, --help                  Display this help and exit\n"
257              "\n"
258              "Report bugs to <linux-bcachefs@vger.kernel.org>");
259 }
260
261 int cmd_device_offline(int argc, char *argv[])
262 {
263         static const struct option longopts[] = {
264                 { "force",              0, NULL, 'f' },
265                 { NULL }
266         };
267         int opt, flags = 0;
268
269         while ((opt = getopt_long(argc, argv, "fh",
270                                   longopts, NULL)) != -1)
271                 switch (opt) {
272                 case 'f':
273                         flags |= BCH_FORCE_IF_DEGRADED;
274                         break;
275                 case 'h':
276                         device_offline_usage();
277                         exit(EXIT_SUCCESS);
278                 }
279         args_shift(optind);
280
281         char *dev = arg_pop();
282         if (!dev)
283                 die("Please supply a device");
284
285         if (argc)
286                 die("too many arguments");
287
288         int dev_idx;
289         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
290         bchu_disk_offline(fs, dev_idx, flags);
291         return 0;
292 }
293
294 static void device_evacuate_usage(void)
295 {
296         puts("bcachefs device evacuate - move data off of a given device\n"
297              "Usage: bcachefs device evacuate [OPTION]... device\n"
298              "\n"
299              "Options:\n"
300              "  -h, --help                  Display this help and exit\n"
301              "\n"
302              "Report bugs to <linux-bcachefs@vger.kernel.org>");
303 }
304
305 int cmd_device_evacuate(int argc, char *argv[])
306 {
307         int opt;
308
309         while ((opt = getopt(argc, argv, "h")) != -1)
310                 switch (opt) {
311                 case 'h':
312                         device_evacuate_usage();
313                         exit(EXIT_SUCCESS);
314                 }
315         args_shift(optind);
316
317         char *dev_path = arg_pop();
318         if (!dev_path)
319                 die("Please supply a device");
320
321         if (argc)
322                 die("too many arguments");
323
324         int dev_idx;
325         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
326
327         struct bch_ioctl_dev_usage_v2 *u = bchu_dev_usage(fs, dev_idx);
328
329         if (u->state == BCH_MEMBER_STATE_rw) {
330                 printf("Setting %s readonly\n", dev_path);
331                 bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0);
332         }
333
334         free(u);
335
336         return bchu_data(fs, (struct bch_ioctl_data) {
337                 .op             = BCH_DATA_OP_migrate,
338                 .start_btree    = 0,
339                 .start_pos      = POS_MIN,
340                 .end_btree      = BTREE_ID_NR,
341                 .end_pos        = POS_MAX,
342                 .migrate.dev    = dev_idx,
343         });
344 }
345
346 static void device_set_state_usage(void)
347 {
348         puts("bcachefs device set-state\n"
349              "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
350              "\n"
351              "<new-state>: one of rw, ro, failed or spare\n"
352              "<path>: path to mounted filesystem, optional unless specifying device by id\n"
353              "\n"
354              "Options:\n"
355              "  -f, --force                 Force, if data redundancy will be degraded\n"
356              "      --force-if-data-lost    Force, if data will be lost\n"
357              "  -o, --offline               Set state of an offline device\n"
358              "  -h, --help                  display this help and exit\n"
359              "Report bugs to <linux-bcachefs@vger.kernel.org>");
360         exit(EXIT_SUCCESS);
361 }
362
363 int cmd_device_set_state(int argc, char *argv[])
364 {
365         static const struct option longopts[] = {
366                 { "force",                      0, NULL, 'f' },
367                 { "force-if-data-lost",         0, NULL, 'F' },
368                 { "offline",                    0, NULL, 'o' },
369                 { "help",                       0, NULL, 'h' },
370                 { NULL }
371         };
372         struct bchfs_handle fs;
373         bool by_id = false;
374         int opt, flags = 0, dev_idx;
375         bool offline = false;
376
377         while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
378                 switch (opt) {
379                 case 'f':
380                         flags |= BCH_FORCE_IF_DEGRADED;
381                         break;
382                 case 'F':
383                         flags |= BCH_FORCE_IF_DEGRADED;
384                         flags |= BCH_FORCE_IF_LOST;
385                         break;
386                 case 'o':
387                         offline = true;
388                         break;
389                 case 'h':
390                         device_set_state_usage();
391                 }
392         args_shift(optind);
393
394         char *new_state_str = arg_pop();
395         if (!new_state_str)
396                 die("Please supply a device state");
397
398         unsigned new_state = read_string_list_or_die(new_state_str,
399                                         bch2_member_states, "device state");
400
401         char *dev_str = arg_pop();
402         if (!dev_str)
403                 die("Please supply a device");
404
405         char *end;
406         dev_idx = strtoul(dev_str, &end, 10);
407         if (*dev_str && !*end)
408                 by_id = true;
409
410         if (offline) {
411                 struct bch_opts opts = bch2_opts_empty();
412                 struct bch_sb_handle sb = { NULL };
413
414                 if (by_id)
415                         die("Cannot specify offline device by id");
416
417                 int ret = bch2_read_super(dev_str, &opts, &sb);
418                 if (ret)
419                         die("error opening %s: %s", dev_str, bch2_err_str(ret));
420
421                 struct bch_member *m = bch2_members_v2_get_mut(sb.sb, sb.sb->dev_idx);
422
423                 SET_BCH_MEMBER_STATE(m, new_state);
424
425                 le64_add_cpu(&sb.sb->seq, 1);
426
427                 bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
428                 ret = fsync(sb.bdev->bd_buffered_fd);
429                 if (ret)
430                         fprintf(stderr, "error writing superblock: fsync error (%m)");
431                 bch2_free_super(&sb);
432                 return ret;
433         }
434
435         char *fs_path = arg_pop();
436         if (fs_path) {
437                 fs = bcache_fs_open(fs_path);
438
439                 if (!by_id) {
440                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
441                         if (dev_idx < 0)
442                                 die("%s does not seem to be a member of %s",
443                                     dev_str, fs_path);
444                 }
445         } else if (!by_id) {
446                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
447         } else {
448                 die("Filesystem path required when specifying device by id");
449         }
450
451         bchu_disk_set_state(fs, dev_idx, new_state, flags);
452
453         return 0;
454 }
455
456 static void device_resize_usage(void)
457 {
458         puts("bcachefs device resize \n"
459              "Usage: bcachefs device resize device [ size ]\n"
460              "\n"
461              "Options:\n"
462              "  -h, --help                  display this help and exit\n"
463              "Report bugs to <linux-bcachefs@vger.kernel.org>");
464         exit(EXIT_SUCCESS);
465 }
466
467 int cmd_device_resize(int argc, char *argv[])
468 {
469         static const struct option longopts[] = {
470                 { "help",                       0, NULL, 'h' },
471                 { NULL }
472         };
473         u64 size;
474         int opt;
475
476         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
477                 switch (opt) {
478                 case 'h':
479                         device_resize_usage();
480                 }
481         args_shift(optind);
482
483         char *dev = arg_pop();
484         if (!dev)
485                 die("Please supply a device to resize");
486
487         int dev_fd = xopen(dev, O_RDONLY);
488
489         char *size_arg = arg_pop();
490         if (!size_arg)
491                 size = get_size(dev_fd);
492         else if (bch2_strtoull_h(size_arg, &size))
493                 die("invalid size");
494
495         size >>= 9;
496
497         if (argc)
498                 die("Too many arguments");
499
500         struct stat dev_stat = xfstat(dev_fd);
501
502         struct mntent *mount = dev_to_mount(dev);
503         if (mount) {
504                 if (!S_ISBLK(dev_stat.st_mode))
505                         die("%s is mounted but isn't a block device?!", dev);
506
507                 printf("Doing online resize of %s\n", dev);
508
509                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
510
511                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
512
513                 struct bch_sb *sb = bchu_read_super(fs, -1);
514                 if (idx >= sb->nr_devices)
515                         die("error reading superblock: dev idx >= sb->nr_devices");
516
517                 struct bch_member m = bch2_sb_member_get(sb, idx);
518
519                 u64 nbuckets = size / le16_to_cpu(m.bucket_size);
520
521                 if (nbuckets < le64_to_cpu(m.nbuckets))
522                         die("Shrinking not supported yet");
523
524                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
525                 bchu_disk_resize(fs, idx, nbuckets);
526         } else {
527                 printf("Doing offline resize of %s\n", dev);
528
529                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
530                 if (IS_ERR(c))
531                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
532
533                 struct bch_dev *resize = NULL;
534
535                 for_each_online_member(c, ca) {
536                         if (resize)
537                                 die("confused: more than one online device?");
538                         resize = ca;
539                         percpu_ref_get(&resize->io_ref);
540                 }
541
542                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
543
544                 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
545                         die("Shrinking not supported yet");
546
547                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
548                 int ret = bch2_dev_resize(c, resize, nbuckets);
549                 if (ret)
550                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
551
552                 percpu_ref_put(&resize->io_ref);
553                 bch2_fs_stop(c);
554         }
555         return 0;
556 }
557
558 static void device_resize_journal_usage(void)
559 {
560         puts("bcachefs device resize-journal \n"
561              "Usage: bcachefs device resize-journal device size\n"
562              "\n"
563              "Options:\n"
564              "  -h, --help                  display this help and exit\n"
565              "Report bugs to <linux-bcachefs@vger.kernel.org>");
566         exit(EXIT_SUCCESS);
567 }
568
569 int cmd_device_resize_journal(int argc, char *argv[])
570 {
571         static const struct option longopts[] = {
572                 { "help",                       0, NULL, 'h' },
573                 { NULL }
574         };
575         u64 size;
576         int opt;
577
578         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
579                 switch (opt) {
580                 case 'h':
581                         device_resize_journal_usage();
582                 }
583         args_shift(optind);
584
585         char *dev = arg_pop();
586         if (!dev)
587                 die("Please supply a device");
588
589         int dev_fd = xopen(dev, O_RDONLY);
590
591         char *size_arg = arg_pop();
592         if (!size_arg)
593                 die("Please supply a journal size");
594         else if (bch2_strtoull_h(size_arg, &size))
595                 die("invalid size");
596
597         size >>= 9;
598
599         if (argc)
600                 die("Too many arguments");
601
602         struct stat dev_stat = xfstat(dev_fd);
603
604         struct mntent *mount = dev_to_mount(dev);
605         if (mount) {
606                 if (!S_ISBLK(dev_stat.st_mode))
607                         die("%s is mounted but isn't a block device?!", dev);
608
609                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
610
611                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
612
613                 struct bch_sb *sb = bchu_read_super(fs, -1);
614                 if (idx >= sb->nr_devices)
615                         die("error reading superblock: dev idx >= sb->nr_devices");
616
617                 struct bch_member m = bch2_sb_member_get(sb, idx);
618
619                 u64 nbuckets = size / le16_to_cpu(m.bucket_size);
620
621                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
622                 bchu_disk_resize_journal(fs, idx, nbuckets);
623         } else {
624                 printf("%s is offline - starting:\n", dev);
625
626                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
627                 if (IS_ERR(c))
628                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
629
630                 struct bch_dev *resize = NULL;
631
632                 for_each_online_member(c, ca) {
633                         if (resize)
634                                 die("confused: more than one online device?");
635                         resize = ca;
636                         percpu_ref_get(&resize->io_ref);
637                 }
638
639                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
640
641                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
642                 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
643                 if (ret)
644                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
645
646                 percpu_ref_put(&resize->io_ref);
647                 bch2_fs_stop(c);
648         }
649         return 0;
650 }