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