]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
c311324a75dc17c2749bfadaf4c4e51a86cc45b9
[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_ioctl.h"
16 #include "libbcachefs/super-io.h"
17 #include "cmds.h"
18 #include "libbcachefs.h"
19 #include "libbcachefs/opts.h"
20 #include "tools-util.h"
21
22 static void device_add_usage(void)
23 {
24         puts("bcachefs device add - add a device to an existing filesystem\n"
25              "Usage: bcachefs device add [OPTION]... filesystem device\n"
26              "\n"
27              "Options:\n"
28              "  -S, --fs_size=size          Size of filesystem on device\n"
29              "  -B, --bucket=size           Bucket size\n"
30              "  -D, --discard               Enable discards\n"
31              "  -g, --group=group           Disk group\n"
32              "  -f, --force                 Use device even if it appears to already be formatted\n"
33              "  -h, --help                  Display this help and exit\n"
34              "\n"
35              "Report bugs to <linux-bcache@vger.kernel.org>");
36 }
37
38 int cmd_device_add(int argc, char *argv[])
39 {
40         static const struct option longopts[] = {
41                 { "fs_size",            required_argument,      NULL, 'S' },
42                 { "bucket",             required_argument,      NULL, 'B' },
43                 { "discard",            no_argument,            NULL, 'D' },
44                 { "group",              required_argument,      NULL, 'g' },
45                 { "force",              no_argument,            NULL, 'f' },
46                 { "help",               no_argument,            NULL, 'h' },
47                 { NULL }
48         };
49         struct format_opts format_opts  = format_opts_default();
50         struct dev_opts dev_opts        = dev_opts_default();
51         bool force = false;
52         int opt;
53
54         while ((opt = getopt_long(argc, argv, "t:fh",
55                                   longopts, NULL)) != -1)
56                 switch (opt) {
57                 case 'S':
58                         if (bch2_strtoull_h(optarg, &dev_opts.size))
59                                 die("invalid filesystem size");
60
61                         dev_opts.size >>= 9;
62                         break;
63                 case 'B':
64                         dev_opts.bucket_size =
65                                 hatoi_validate(optarg, "bucket size");
66                         break;
67                 case 'D':
68                         dev_opts.discard = true;
69                         break;
70                 case 'g':
71                         dev_opts.group = strdup(optarg);
72                         break;
73                 case 'f':
74                         force = true;
75                         break;
76                 case 'h':
77                         device_add_usage();
78                         exit(EXIT_SUCCESS);
79                 }
80         args_shift(optind);
81
82         char *fs_path = arg_pop();
83         if (!fs_path)
84                 die("Please supply a filesystem");
85
86         char *dev_path = arg_pop();
87         if (!dev_path)
88                 die("Please supply a device");
89
90         if (argc)
91                 die("too many arguments");
92
93         struct bchfs_handle fs = bcache_fs_open(fs_path);
94
95         dev_opts.path = dev_path;
96         dev_opts.fd = open_for_format(dev_opts.path, force);
97
98         struct bch_opt_strs fs_opt_strs;
99         memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
100
101         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
102
103         opt_set(fs_opts, block_size,
104                 read_file_u64(fs.sysfs_fd, "block_size") >> 9);
105         opt_set(fs_opts, btree_node_size,
106                 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9);
107
108         struct bch_sb *sb = bch2_format(fs_opt_strs,
109                                         fs_opts,
110                                         format_opts,
111                                         &dev_opts, 1);
112         free(sb);
113         fsync(dev_opts.fd);
114         close(dev_opts.fd);
115
116         bchu_disk_add(fs, dev_opts.path);
117         return 0;
118 }
119
120 static void device_remove_usage(void)
121 {
122         puts("bcachefs device_remove - remove a device from a filesystem\n"
123              "Usage:\n"
124              "  bcachefs device remove device\n"
125              "  bcachefs device remove --by-id path devid\n"
126              "\n"
127              "Options:\n"
128              "  -i, --by-id                 Remove device by device id\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;
150         unsigned dev_idx;
151
152         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
153                 switch (opt) {
154                 case 'i':
155                         by_id = true;
156                         break;
157                 case 'f':
158                         flags |= BCH_FORCE_IF_DATA_LOST;
159                         break;
160                 case 'F':
161                         flags |= BCH_FORCE_IF_METADATA_LOST;
162                         break;
163                 case 'h':
164                         device_remove_usage();
165                 }
166         args_shift(optind);
167
168         if (by_id) {
169                 char *path = arg_pop();
170                 if (!path)
171                         die("Please supply filesystem to remove device from");
172
173                 dev_idx = (intptr_t) arg_pop();
174                 if (!dev_idx)
175                         die("Please supply device id");
176
177                 fs = bcache_fs_open(path);
178         } else {
179                 char *dev = arg_pop();
180                 if (!dev)
181                         die("Please supply a device to remove");
182
183                 fs = bchu_fs_open_by_dev(dev, &dev_idx);
184         }
185
186         if (argc)
187                 die("too many arguments");
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         unsigned 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         unsigned 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         unsigned 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          = POS_MIN,
317                 .end            = POS_MAX,
318                 .migrate.dev    = dev_idx,
319         });
320 }
321
322 static void device_set_state_usage(void)
323 {
324         puts("bcachefs device set-state\n"
325              "Usage: bcachefs device set-state device new-state\n"
326              "\n"
327              "Options:\n"
328              "  -f, --force                 Force, if data redundancy will be degraded\n"
329              "  -o, --offline               Set state of an offline device\n"
330              "  -h, --help                  display this help and exit\n"
331              "Report bugs to <linux-bcache@vger.kernel.org>");
332         exit(EXIT_SUCCESS);
333 }
334
335 int cmd_device_set_state(int argc, char *argv[])
336 {
337         static const struct option longopts[] = {
338                 { "force",                      0, NULL, 'f' },
339                 { "offline",                    0, NULL, 'o' },
340                 { "help",                       0, NULL, 'h' },
341                 { NULL }
342         };
343         int opt, flags = 0;
344         bool offline = false;
345
346         while ((opt = getopt_long(argc, argv, "foh", longopts, NULL)) != -1)
347                 switch (opt) {
348                 case 'f':
349                         flags |= BCH_FORCE_IF_DEGRADED;
350                         break;
351                 case 'o':
352                         offline = true;
353                         break;
354                 case 'h':
355                         device_set_state_usage();
356                 }
357         args_shift(optind);
358
359         char *dev_path = arg_pop();
360         if (!dev_path)
361                 die("Please supply a device");
362
363         char *new_state_str = arg_pop();
364         if (!new_state_str)
365                 die("Please supply a device state");
366
367         unsigned new_state = read_string_list_or_die(new_state_str,
368                                         bch2_dev_state, "device state");
369
370         if (!offline) {
371                 unsigned dev_idx;
372                 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
373
374                 bchu_disk_set_state(fs, dev_idx, new_state, flags);
375
376                 bcache_fs_close(fs);
377         } else {
378                 struct bch_opts opts = bch2_opts_empty();
379                 struct bch_sb_handle sb = { NULL };
380
381                 int ret = bch2_read_super(dev_path, &opts, &sb);
382                 if (ret)
383                         die("error opening %s: %s", dev_path, strerror(-ret));
384
385                 struct bch_member *m = bch2_sb_get_members(sb.sb)->members + sb.sb->dev_idx;
386
387                 SET_BCH_MEMBER_STATE(m, new_state);
388
389                 le64_add_cpu(&sb.sb->seq, 1);
390
391                 bch2_super_write(sb.bdev->bd_fd, sb.sb);
392                 bch2_free_super(&sb);
393         }
394
395         return 0;
396 }
397
398 static void device_resize_usage(void)
399 {
400         puts("bcachefs device resize \n"
401              "Usage: bcachefs device resize device [ size ]\n"
402              "\n"
403              "Options:\n"
404              "  -h, --help                  display this help and exit\n"
405              "Report bugs to <linux-bcache@vger.kernel.org>");
406         exit(EXIT_SUCCESS);
407 }
408
409 int cmd_device_resize(int argc, char *argv[])
410 {
411         static const struct option longopts[] = {
412                 { "help",                       0, NULL, 'h' },
413                 { NULL }
414         };
415         u64 size;
416         int opt;
417
418         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
419                 switch (opt) {
420                 case 'h':
421                         device_resize_usage();
422                 }
423         args_shift(optind);
424
425         char *dev = arg_pop();
426         if (!dev)
427                 die("Please supply a device to resize");
428
429         int dev_fd = xopen(dev, O_RDONLY);
430
431         char *size_arg = arg_pop();
432         if (!size_arg)
433                 size = get_size(dev, dev_fd);
434         else if (bch2_strtoull_h(size_arg, &size))
435                 die("invalid size");
436
437         size >>= 9;
438
439         if (argc)
440                 die("Too many arguments");
441
442         struct stat dev_stat = xfstat(dev_fd);
443
444         struct mntent *mount = dev_to_mount(dev);
445         if (mount) {
446                 if (!S_ISBLK(dev_stat.st_mode))
447                         die("%s is mounted but isn't a block device?!", dev);
448
449                 printf("Doing online resize of %s\n", dev);
450
451                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
452
453                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
454
455                 struct bch_sb *sb = bchu_read_super(fs, -1);
456                 if (idx >= sb->nr_devices)
457                         die("error reading superblock: dev idx >= sb->nr_devices");
458
459                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
460                 if (!mi)
461                         die("error reading superblock: no member info");
462
463                 /* could also just read this out of sysfs... meh */
464                 struct bch_member *m = mi->members + idx;
465
466                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
467
468                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
469                 bchu_disk_resize(fs, idx, nbuckets);
470         } else {
471                 printf("Doing offline resize of %s\n", dev);
472
473                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
474                 if (IS_ERR(c))
475                         die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
476
477                 struct bch_dev *ca, *resize = NULL;
478                 unsigned i;
479
480                 for_each_online_member(ca, c, i) {
481                         if (resize)
482                                 die("confused: more than one online device?");
483                         resize = ca;
484                         percpu_ref_get(&resize->io_ref);
485                 }
486
487                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
488
489                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
490                 int ret = bch2_dev_resize(c, resize, nbuckets);
491                 if (ret)
492                         fprintf(stderr, "resize error: %s\n", strerror(-ret));
493
494                 percpu_ref_put(&resize->io_ref);
495                 bch2_fs_stop(c);
496         }
497         return 0;
498 }