]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
Add packaging workflow
[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/super-io.h"
20 #include "cmds.h"
21 #include "libbcachefs.h"
22 #include "libbcachefs/opts.h"
23 #include "tools-util.h"
24
25 int device_usage(void)
26 {
27        puts("bcachefs device - manage devices within a running filesystem\n"
28             "Usage: bcachefs device <CMD> [OPTION]\n"
29             "\n"
30             "Commands:\n"
31             "  add                     add a new device to an existing filesystem\n"
32             "  remove                  remove a device from an existing filesystem\n"
33             "  online                  re-add an existing member to a filesystem\n"
34             "  offline                 take a device offline, without removing it\n"
35             "  evacuate                migrate data off a specific device\n"
36             "  set-state               mark a device as failed\n"
37             "  resize                  resize filesystem on a device\n"
38             "  resize-journal          resize journal on a device\n"
39             "\n"
40             "Report bugs to <linux-bcachefs@vger.kernel.org>");
41        return 0;
42 }
43
44 static void device_add_usage(void)
45 {
46         puts("bcachefs device add - add a device to an existing filesystem\n"
47              "Usage: bcachefs device add [OPTION]... filesystem device\n"
48              "\n"
49              "Options:\n"
50              "  -S, --fs_size=size          Size of filesystem on device\n"
51              "  -B, --bucket=size           Bucket size\n"
52              "  -D, --discard               Enable discards\n"
53              "  -l, --label=label           Disk label\n"
54              "  -f, --force                 Use device even if it appears to already be formatted\n"
55              "  -h, --help                  Display this help and exit\n"
56              "\n"
57              "Report bugs to <linux-bcachefs@vger.kernel.org>");
58 }
59
60 int cmd_device_add(int argc, char *argv[])
61 {
62         static const struct option longopts[] = {
63                 { "fs_size",            required_argument,      NULL, 'S' },
64                 { "bucket",             required_argument,      NULL, 'B' },
65                 { "discard",            no_argument,            NULL, 'D' },
66                 { "label",              required_argument,      NULL, 'l' },
67                 { "force",              no_argument,            NULL, 'f' },
68                 { "help",               no_argument,            NULL, 'h' },
69                 { NULL }
70         };
71         struct format_opts format_opts  = format_opts_default();
72         struct dev_opts dev_opts        = dev_opts_default();
73         bool force = false;
74         int opt;
75
76         while ((opt = getopt_long(argc, argv, "t:fh",
77                                   longopts, NULL)) != -1)
78                 switch (opt) {
79                 case 'S':
80                         if (bch2_strtoull_h(optarg, &dev_opts.size))
81                                 die("invalid filesystem size");
82                         break;
83                 case 'B':
84                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
85                                 die("bad bucket_size %s", optarg);
86                         break;
87                 case 'D':
88                         dev_opts.discard = true;
89                         break;
90                 case 'l':
91                         dev_opts.label = strdup(optarg);
92                         break;
93                 case 'f':
94                         force = true;
95                         break;
96                 case 'h':
97                         device_add_usage();
98                         exit(EXIT_SUCCESS);
99                 }
100         args_shift(optind);
101
102         char *fs_path = arg_pop();
103         if (!fs_path)
104                 die("Please supply a filesystem");
105
106         dev_opts.path = arg_pop();
107         if (!dev_opts.path)
108                 die("Please supply a device");
109
110         if (argc)
111                 die("too many arguments");
112
113         struct bchfs_handle fs = bcache_fs_open(fs_path);
114
115         dev_opts.fd = open_for_format(dev_opts.path, force);
116
117         struct bch_opt_strs fs_opt_strs;
118         memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
119
120         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
121
122         opt_set(fs_opts, block_size,
123                 read_file_u64(fs.sysfs_fd, "options/block_size"));
124         opt_set(fs_opts, btree_node_size,
125                 read_file_u64(fs.sysfs_fd, "options/btree_node_size"));
126
127         struct bch_sb *sb = bch2_format(fs_opt_strs,
128                                         fs_opts,
129                                         format_opts,
130                                         &dev_opts, 1);
131         free(sb);
132         fsync(dev_opts.fd);
133         close(dev_opts.fd);
134
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 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         return bchu_data(fs, (struct bch_ioctl_data) {
332                 .op             = BCH_DATA_OP_MIGRATE,
333                 .start_btree    = 0,
334                 .start_pos      = POS_MIN,
335                 .end_btree      = BTREE_ID_NR,
336                 .end_pos        = POS_MAX,
337                 .migrate.dev    = dev_idx,
338         });
339 }
340
341 static void device_set_state_usage(void)
342 {
343         puts("bcachefs device set-state\n"
344              "Usage: bcachefs device set-state <new-state> <device>|<devid> <path>\n"
345              "\n"
346              "<new-state>: one of rw, ro, failed or spare\n"
347              "<path>: path to mounted filesystem, optional unless specifying device by id\n"
348              "\n"
349              "Options:\n"
350              "  -f, --force                 Force, if data redundancy will be degraded\n"
351              "      --force-if-data-lost    Force, if data will be lost\n"
352              "  -o, --offline               Set state of an offline device\n"
353              "  -h, --help                  display this help and exit\n"
354              "Report bugs to <linux-bcachefs@vger.kernel.org>");
355         exit(EXIT_SUCCESS);
356 }
357
358 int cmd_device_set_state(int argc, char *argv[])
359 {
360         static const struct option longopts[] = {
361                 { "force",                      0, NULL, 'f' },
362                 { "force-if-data-lost",         0, NULL, 'F' },
363                 { "offline",                    0, NULL, 'o' },
364                 { "help",                       0, NULL, 'h' },
365                 { NULL }
366         };
367         struct bchfs_handle fs;
368         bool by_id = false;
369         int opt, flags = 0, dev_idx;
370         bool offline = false;
371
372         while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
373                 switch (opt) {
374                 case 'f':
375                         flags |= BCH_FORCE_IF_DEGRADED;
376                         break;
377                 case 'F':
378                         flags |= BCH_FORCE_IF_DEGRADED;
379                         flags |= BCH_FORCE_IF_LOST;
380                         break;
381                 case 'o':
382                         offline = true;
383                         break;
384                 case 'h':
385                         device_set_state_usage();
386                 }
387         args_shift(optind);
388
389         char *new_state_str = arg_pop();
390         if (!new_state_str)
391                 die("Please supply a device state");
392
393         unsigned new_state = read_string_list_or_die(new_state_str,
394                                         bch2_member_states, "device state");
395
396         char *dev_str = arg_pop();
397         if (!dev_str)
398                 die("Please supply a device");
399
400         char *end;
401         dev_idx = strtoul(dev_str, &end, 10);
402         if (*dev_str && !*end)
403                 by_id = true;
404
405         if (offline) {
406                 struct bch_opts opts = bch2_opts_empty();
407                 struct bch_sb_handle sb = { NULL };
408
409                 if (by_id)
410                         die("Cannot specify offline device by id");
411
412                 int ret = bch2_read_super(dev_str, &opts, &sb);
413                 if (ret)
414                         die("error opening %s: %s", dev_str, bch2_err_str(ret));
415
416                 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
417
418                 SET_BCH_MEMBER_STATE(m, new_state);
419
420                 le64_add_cpu(&sb.sb->seq, 1);
421
422                 bch2_super_write(sb.bdev->bd_buffered_fd, sb.sb);
423                 ret = fsync(sb.bdev->bd_buffered_fd);
424                 if (ret)
425                         fprintf(stderr, "error writing superblock: fsync error (%m)");
426                 bch2_free_super(&sb);
427                 return ret;
428         }
429
430         char *fs_path = arg_pop();
431         if (fs_path) {
432                 fs = bcache_fs_open(fs_path);
433
434                 if (!by_id) {
435                         dev_idx = bchu_dev_path_to_idx(fs, dev_str);
436                         if (dev_idx < 0)
437                                 die("%s does not seem to be a member of %s",
438                                     dev_str, fs_path);
439                 }
440         } else if (!by_id) {
441                 fs = bchu_fs_open_by_dev(dev_str, &dev_idx);
442         } else {
443                 die("Filesystem path required when specifying device by id");
444         }
445
446         bchu_disk_set_state(fs, dev_idx, new_state, flags);
447
448         return 0;
449 }
450
451 static void device_resize_usage(void)
452 {
453         puts("bcachefs device resize \n"
454              "Usage: bcachefs device resize device [ size ]\n"
455              "\n"
456              "Options:\n"
457              "  -h, --help                  display this help and exit\n"
458              "Report bugs to <linux-bcachefs@vger.kernel.org>");
459         exit(EXIT_SUCCESS);
460 }
461
462 int cmd_device_resize(int argc, char *argv[])
463 {
464         static const struct option longopts[] = {
465                 { "help",                       0, NULL, 'h' },
466                 { NULL }
467         };
468         u64 size;
469         int opt;
470
471         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
472                 switch (opt) {
473                 case 'h':
474                         device_resize_usage();
475                 }
476         args_shift(optind);
477
478         char *dev = arg_pop();
479         if (!dev)
480                 die("Please supply a device to resize");
481
482         int dev_fd = xopen(dev, O_RDONLY);
483
484         char *size_arg = arg_pop();
485         if (!size_arg)
486                 size = get_size(dev, dev_fd);
487         else if (bch2_strtoull_h(size_arg, &size))
488                 die("invalid size");
489
490         size >>= 9;
491
492         if (argc)
493                 die("Too many arguments");
494
495         struct stat dev_stat = xfstat(dev_fd);
496
497         struct mntent *mount = dev_to_mount(dev);
498         if (mount) {
499                 if (!S_ISBLK(dev_stat.st_mode))
500                         die("%s is mounted but isn't a block device?!", dev);
501
502                 printf("Doing online resize of %s\n", dev);
503
504                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
505
506                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
507
508                 struct bch_sb *sb = bchu_read_super(fs, -1);
509                 if (idx >= sb->nr_devices)
510                         die("error reading superblock: dev idx >= sb->nr_devices");
511
512                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
513                 if (!mi)
514                         die("error reading superblock: no member info");
515
516                 /* could also just read this out of sysfs... meh */
517                 struct bch_member *m = mi->members + 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 *ca, *resize = NULL;
534                 unsigned i;
535
536                 for_each_online_member(ca, c, i) {
537                         if (resize)
538                                 die("confused: more than one online device?");
539                         resize = ca;
540                         percpu_ref_get(&resize->io_ref);
541                 }
542
543                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
544
545                 if (nbuckets < le64_to_cpu(resize->mi.nbuckets))
546                         die("Shrinking not supported yet");
547
548                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
549                 int ret = bch2_dev_resize(c, resize, nbuckets);
550                 if (ret)
551                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
552
553                 percpu_ref_put(&resize->io_ref);
554                 bch2_fs_stop(c);
555         }
556         return 0;
557 }
558
559 static void device_resize_journal_usage(void)
560 {
561         puts("bcachefs device resize-journal \n"
562              "Usage: bcachefs device resize-journal device size\n"
563              "\n"
564              "Options:\n"
565              "  -h, --help                  display this help and exit\n"
566              "Report bugs to <linux-bcachefs@vger.kernel.org>");
567         exit(EXIT_SUCCESS);
568 }
569
570 int cmd_device_resize_journal(int argc, char *argv[])
571 {
572         static const struct option longopts[] = {
573                 { "help",                       0, NULL, 'h' },
574                 { NULL }
575         };
576         u64 size;
577         int opt;
578
579         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
580                 switch (opt) {
581                 case 'h':
582                         device_resize_journal_usage();
583                 }
584         args_shift(optind);
585
586         char *dev = arg_pop();
587         if (!dev)
588                 die("Please supply a device");
589
590         int dev_fd = xopen(dev, O_RDONLY);
591
592         char *size_arg = arg_pop();
593         if (!size_arg)
594                 die("Please supply a journal size");
595         else if (bch2_strtoull_h(size_arg, &size))
596                 die("invalid size");
597
598         size >>= 9;
599
600         if (argc)
601                 die("Too many arguments");
602
603         struct stat dev_stat = xfstat(dev_fd);
604
605         struct mntent *mount = dev_to_mount(dev);
606         if (mount) {
607                 if (!S_ISBLK(dev_stat.st_mode))
608                         die("%s is mounted but isn't a block device?!", dev);
609
610                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
611
612                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
613
614                 struct bch_sb *sb = bchu_read_super(fs, -1);
615                 if (idx >= sb->nr_devices)
616                         die("error reading superblock: dev idx >= sb->nr_devices");
617
618                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
619                 if (!mi)
620                         die("error reading superblock: no member info");
621
622                 /* could also just read this out of sysfs... meh */
623                 struct bch_member *m = mi->members + idx;
624
625                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
626
627                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
628                 bchu_disk_resize_journal(fs, idx, nbuckets);
629         } else {
630                 printf("%s is offline - starting:\n", dev);
631
632                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
633                 if (IS_ERR(c))
634                         die("error opening %s: %s", dev, bch2_err_str(PTR_ERR(c)));
635
636                 struct bch_dev *ca, *resize = NULL;
637                 unsigned i;
638
639                 for_each_online_member(ca, c, i) {
640                         if (resize)
641                                 die("confused: more than one online device?");
642                         resize = ca;
643                         percpu_ref_get(&resize->io_ref);
644                 }
645
646                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
647
648                 printf("resizing journal on %s to %llu buckets\n", dev, nbuckets);
649                 int ret = bch2_set_nr_journal_buckets(c, resize, nbuckets);
650                 if (ret)
651                         fprintf(stderr, "resize error: %s\n", bch2_err_str(ret));
652
653                 percpu_ref_put(&resize->io_ref);
654                 bch2_fs_stop(c);
655         }
656         return 0;
657 }