]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/cmd_device.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / c_src / 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         bchu_disk_add(fs, dev_opts.path);
136         return 0;
137 }
138
139 static void device_remove_usage(void)
140 {
141         puts("bcachefs device_remove - remove a device from a filesystem\n"
142              "Usage:\n"
143              "  bcachefs device remove <device>|<devid> <path>\n"
144              "\n"
145              "Options:\n"
146              "  -f, --force                 Force removal, even if some data\n"
147              "                              couldn't be migrated\n"
148              "  -F, --force-metadata        Force removal, even if some metadata\n"
149              "                              couldn't be migrated\n"
150              "  -h, --help                  display this help and exit\n"
151              "Report bugs to <linux-bcachefs@vger.kernel.org>");
152         exit(EXIT_SUCCESS);
153 }
154
155 int cmd_device_remove(int argc, char *argv[])
156 {
157         static const struct option longopts[] = {
158                 { "by-id",              0, NULL, 'i' },
159                 { "force",              0, NULL, 'f' },
160                 { "force-metadata",     0, NULL, 'F' },
161                 { "help",               0, NULL, 'h' },
162                 { NULL }
163         };
164         struct bchfs_handle fs;
165         bool by_id = false;
166         int opt, flags = BCH_FORCE_IF_DEGRADED, dev_idx;
167
168         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
169                 switch (opt) {
170                 case 'f':
171                         flags |= BCH_FORCE_IF_DATA_LOST;
172                         break;
173                 case 'F':
174                         flags |= BCH_FORCE_IF_METADATA_LOST;
175                         break;
176                 case 'h':
177                         device_remove_usage();
178                 }
179         args_shift(optind);
180
181         char *dev_str = arg_pop();
182         if (!dev_str)
183                 die("Please supply a device");
184
185         char *end;
186         dev_idx = strtoul(dev_str, &end, 10);
187         if (*dev_str && !*end)
188                 by_id = true;
189
190         char *fs_path = arg_pop();
191         if (fs_path) {
192                 fs = bcache_fs_open(fs_path);
193
194                 if (!by_id) {
195                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
196                         if (dev_idx < 0)
197                                 die("%s does not seem to be a member of %s",
198                                     dev_str, fs_path);
199                 }
200         } else if (!by_id) {
201                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
202         } else {
203                 die("Filesystem path required when specifying device by id");
204         }
205
206         bchu_disk_remove(fs, dev_idx, flags);
207         return 0;
208 }
209
210 static void device_online_usage(void)
211 {
212         puts("bcachefs device online - readd a device to a running filesystem\n"
213              "Usage: bcachefs device online [OPTION]... device\n"
214              "\n"
215              "Options:\n"
216              "  -h, --help                  Display this help and exit\n"
217              "\n"
218              "Report bugs to <linux-bcachefs@vger.kernel.org>");
219 }
220
221 int cmd_device_online(int argc, char *argv[])
222 {
223         int opt;
224
225         while ((opt = getopt(argc, argv, "h")) != -1)
226                 switch (opt) {
227                 case 'h':
228                         device_online_usage();
229                         exit(EXIT_SUCCESS);
230                 }
231         args_shift(optind);
232
233         char *dev = arg_pop();
234         if (!dev)
235                 die("Please supply a device");
236
237         if (argc)
238                 die("too many arguments");
239
240         int dev_idx;
241         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
242         bchu_disk_online(fs, dev);
243         return 0;
244 }
245
246 static void device_offline_usage(void)
247 {
248         puts("bcachefs device offline - take a device offline, without removing it\n"
249              "Usage: bcachefs device offline [OPTION]... device\n"
250              "\n"
251              "Options:\n"
252              "  -f, --force                 Force, if data redundancy will be degraded\n"
253              "  -h, --help                  Display this help and exit\n"
254              "\n"
255              "Report bugs to <linux-bcachefs@vger.kernel.org>");
256 }
257
258 int cmd_device_offline(int argc, char *argv[])
259 {
260         static const struct option longopts[] = {
261                 { "force",              0, NULL, 'f' },
262                 { NULL }
263         };
264         int opt, flags = 0;
265
266         while ((opt = getopt_long(argc, argv, "fh",
267                                   longopts, NULL)) != -1)
268                 switch (opt) {
269                 case 'f':
270                         flags |= BCH_FORCE_IF_DEGRADED;
271                         break;
272                 case 'h':
273                         device_offline_usage();
274                         exit(EXIT_SUCCESS);
275                 }
276         args_shift(optind);
277
278         char *dev = arg_pop();
279         if (!dev)
280                 die("Please supply a device");
281
282         if (argc)
283                 die("too many arguments");
284
285         int dev_idx;
286         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
287         bchu_disk_offline(fs, dev_idx, flags);
288         return 0;
289 }
290
291 static void device_evacuate_usage(void)
292 {
293         puts("bcachefs device evacuate - move data off of a given device\n"
294              "Usage: bcachefs device evacuate [OPTION]... device\n"
295              "\n"
296              "Options:\n"
297              "  -h, --help                  Display this help and exit\n"
298              "\n"
299              "Report bugs to <linux-bcachefs@vger.kernel.org>");
300 }
301
302 int cmd_device_evacuate(int argc, char *argv[])
303 {
304         int opt;
305
306         while ((opt = getopt(argc, argv, "h")) != -1)
307                 switch (opt) {
308                 case 'h':
309                         device_evacuate_usage();
310                         exit(EXIT_SUCCESS);
311                 }
312         args_shift(optind);
313
314         char *dev_path = arg_pop();
315         if (!dev_path)
316                 die("Please supply a device");
317
318         if (argc)
319                 die("too many arguments");
320
321         int dev_idx;
322         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
323
324         struct bch_ioctl_dev_usage_v2 *u = bchu_dev_usage(fs, dev_idx);
325
326         if (u->state == BCH_MEMBER_STATE_rw) {
327                 printf("Setting %s readonly\n", dev_path);
328                 bchu_disk_set_state(fs, dev_idx, BCH_MEMBER_STATE_ro, 0);
329         }
330
331         free(u);
332
333         return bchu_data(fs, (struct bch_ioctl_data) {
334                 .op             = BCH_DATA_OP_migrate,
335                 .start_btree    = 0,
336                 .start_pos      = POS_MIN,
337                 .end_btree      = BTREE_ID_NR,
338                 .end_pos        = POS_MAX,
339                 .migrate.dev    = dev_idx,
340         });
341 }
342
343 static void device_set_state_usage(void)
344 {
345         puts("bcachefs device set-state\n"
346              "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
347              "\n"
348              "<new-state>: one of rw, ro, failed or spare\n"
349              "<path>: path to mounted filesystem, optional unless specifying device by id\n"
350              "\n"
351              "Options:\n"
352              "  -f, --force                 Force, if data redundancy will be degraded\n"
353              "      --force-if-data-lost    Force, if data will be lost\n"
354              "  -o, --offline               Set state of an offline device\n"
355              "  -h, --help                  display this help and exit\n"
356              "Report bugs to <linux-bcachefs@vger.kernel.org>");
357         exit(EXIT_SUCCESS);
358 }
359
360 int cmd_device_set_state(int argc, char *argv[])
361 {
362         static const struct option longopts[] = {
363                 { "force",                      0, NULL, 'f' },
364                 { "force-if-data-lost",         0, NULL, 'F' },
365                 { "offline",                    0, NULL, 'o' },
366                 { "help",                       0, NULL, 'h' },
367                 { NULL }
368         };
369         struct bchfs_handle fs;
370         bool by_id = false;
371         int opt, flags = 0, dev_idx;
372         bool offline = false;
373
374         while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
375                 switch (opt) {
376                 case 'f':
377                         flags |= BCH_FORCE_IF_DEGRADED;
378                         break;
379                 case 'F':
380                         flags |= BCH_FORCE_IF_DEGRADED;
381                         flags |= BCH_FORCE_IF_LOST;
382                         break;
383                 case 'o':
384                         offline = true;
385                         break;
386                 case 'h':
387                         device_set_state_usage();
388                 }
389         args_shift(optind);
390
391         char *new_state_str = arg_pop();
392         if (!new_state_str)
393                 die("Please supply a device state");
394
395         unsigned new_state = read_string_list_or_die(new_state_str,
396                                         bch2_member_states, "device state");
397
398         char *dev_str = arg_pop();
399         if (!dev_str)
400                 die("Please supply a device");
401
402         char *end;
403         dev_idx = strtoul(dev_str, &end, 10);
404         if (*dev_str && !*end)
405                 by_id = true;
406
407         if (offline) {
408                 struct bch_opts opts = bch2_opts_empty();
409                 struct bch_sb_handle sb = { NULL };
410
411                 if (by_id)
412                         die("Cannot specify offline device by id");
413
414                 int ret = bch2_read_super(dev_str, &opts, &sb);
415                 if (ret)
416                         die("error opening %s: %s", dev_str, bch2_err_str(ret));
417
418                 struct bch_member *m = bch2_members_v2_get_mut(sb.sb, sb.sb->dev_idx);
419
420                 SET_BCH_MEMBER_STATE(m, new_state);
421
422                 le64_add_cpu(&sb.sb->seq, 1);
423
424                 bch2_super_write(sb.bdev->bd_fd, sb.sb);
425                 ret = fsync(sb.bdev->bd_fd);
426                 if (ret)
427                         fprintf(stderr, "error writing superblock: fsync error (%m)");
428                 bch2_free_super(&sb);
429                 return ret;
430         }
431
432         char *fs_path = arg_pop();
433         if (fs_path) {
434                 fs = bcache_fs_open(fs_path);
435
436                 if (!by_id) {
437                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
438                         if (dev_idx < 0)
439                                 die("%s does not seem to be a member of %s",
440                                     dev_str, fs_path);
441                 }
442         } else if (!by_id) {
443                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
444         } else {
445                 die("Filesystem path required when specifying device by id");
446         }
447
448         bchu_disk_set_state(fs, dev_idx, new_state, flags);
449
450         return 0;
451 }
452
453 static void device_resize_usage(void)
454 {
455         puts("bcachefs device resize \n"
456              "Usage: bcachefs device resize device [ size ]\n"
457              "\n"
458              "Options:\n"
459              "  -h, --help                  display this help and exit\n"
460              "Report bugs to <linux-bcachefs@vger.kernel.org>");
461         exit(EXIT_SUCCESS);
462 }
463
464 int cmd_device_resize(int argc, char *argv[])
465 {
466         static const struct option longopts[] = {
467                 { "help",                       0, NULL, 'h' },
468                 { NULL }
469         };
470         u64 size;
471         int opt;
472
473         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
474                 switch (opt) {
475                 case 'h':
476                         device_resize_usage();
477                 }
478         args_shift(optind);
479
480         char *dev = arg_pop();
481         if (!dev)
482                 die("Please supply a device to resize");
483
484         int dev_fd = xopen(dev, O_RDONLY);
485
486         char *size_arg = arg_pop();
487         if (!size_arg)
488                 size = get_size(dev_fd);
489         else if (bch2_strtoull_h(size_arg, &size))
490                 die("invalid size");
491
492         size >>= 9;
493
494         if (argc)
495                 die("Too many arguments");
496
497         struct stat dev_stat = xfstat(dev_fd);
498
499         struct mntent *mount = dev_to_mount(dev);
500         if (mount) {
501                 if (!S_ISBLK(dev_stat.st_mode))
502                         die("%s is mounted but isn't a block device?!", dev);
503
504                 printf("Doing online resize of %s\n", dev);
505
506                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
507
508                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
509
510                 struct bch_sb *sb = bchu_read_super(fs, -1);
511                 if (idx >= sb->nr_devices)
512                         die("error reading superblock: dev idx >= sb->nr_devices");
513
514                 struct bch_member m = bch2_sb_member_get(sb, idx);
515
516                 u64 nbuckets = size / le16_to_cpu(m.bucket_size);
517
518                 if (nbuckets < le64_to_cpu(m.nbuckets))
519                         die("Shrinking not supported yet");
520
521                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
522                 bchu_disk_resize(fs, idx, nbuckets);
523         } else {
524                 printf("Doing offline resize of %s\n", dev);
525
526                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
527                 if (IS_ERR(c))
528                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
529
530                 struct bch_dev *resize = NULL;
531
532                 for_each_online_member(c, ca) {
533                         if (resize)
534                                 die("confused: more than one online device?");
535                         resize = ca;
536                         percpu_ref_get(&resize->io_ref);
537                 }
538
539                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
540
541                 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
542                         die("Shrinking not supported yet");
543
544                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
545                 int ret = bch2_dev_resize(c, resize, nbuckets);
546                 if (ret)
547                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
548
549                 percpu_ref_put(&resize->io_ref);
550                 bch2_fs_stop(c);
551         }
552         return 0;
553 }
554
555 static void device_resize_journal_usage(void)
556 {
557         puts("bcachefs device resize-journal \n"
558              "Usage: bcachefs device resize-journal device size\n"
559              "\n"
560              "Options:\n"
561              "  -h, --help                  display this help and exit\n"
562              "Report bugs to <linux-bcachefs@vger.kernel.org>");
563         exit(EXIT_SUCCESS);
564 }
565
566 int cmd_device_resize_journal(int argc, char *argv[])
567 {
568         static const struct option longopts[] = {
569                 { "help",                       0, NULL, 'h' },
570                 { NULL }
571         };
572         u64 size;
573         int opt;
574
575         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
576                 switch (opt) {
577                 case 'h':
578                         device_resize_journal_usage();
579                 }
580         args_shift(optind);
581
582         char *dev = arg_pop();
583         if (!dev)
584                 die("Please supply a device");
585
586         int dev_fd = xopen(dev, O_RDONLY);
587
588         char *size_arg = arg_pop();
589         if (!size_arg)
590                 die("Please supply a journal size");
591         else if (bch2_strtoull_h(size_arg, &size))
592                 die("invalid size");
593
594         size >>= 9;
595
596         if (argc)
597                 die("Too many arguments");
598
599         struct stat dev_stat = xfstat(dev_fd);
600
601         struct mntent *mount = dev_to_mount(dev);
602         if (mount) {
603                 if (!S_ISBLK(dev_stat.st_mode))
604                         die("%s is mounted but isn't a block device?!", dev);
605
606                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
607
608                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
609
610                 struct bch_sb *sb = bchu_read_super(fs, -1);
611                 if (idx >= sb->nr_devices)
612                         die("error reading superblock: dev idx >= sb->nr_devices");
613
614                 struct bch_member m = bch2_sb_member_get(sb, idx);
615
616                 u64 nbuckets = size / le16_to_cpu(m.bucket_size);
617
618                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
619                 bchu_disk_resize_journal(fs, idx, nbuckets);
620         } else {
621                 printf("%s is offline - starting:\n", dev);
622
623                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
624                 if (IS_ERR(c))
625                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
626
627                 struct bch_dev *resize = NULL;
628
629                 for_each_online_member(c, ca) {
630                         if (resize)
631                                 die("confused: more than one online device?");
632                         resize = ca;
633                         percpu_ref_get(&resize->io_ref);
634                 }
635
636                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
637
638                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
639                 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
640                 if (ret)
641                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
642
643                 percpu_ref_put(&resize->io_ref);
644                 bch2_fs_stop(c);
645         }
646         return 0;
647 }