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