]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
check if fs is mounted before running fsck
[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: bcachefs device remove device\n"
124              "\n"
125              "Options:\n"
126              "  -f, --force                 Force removal, even if some data\n"
127              "                              couldn't be migrated\n"
128              "  -F, --force-metadata        Force removal, even if some metadata\n"
129              "                              couldn't be migrated\n"
130              "  -h, --help                  display this help and exit\n"
131              "Report bugs to <linux-bcache@vger.kernel.org>");
132         exit(EXIT_SUCCESS);
133 }
134
135 int cmd_device_remove(int argc, char *argv[])
136 {
137         static const struct option longopts[] = {
138                 { "force",              0, NULL, 'f' },
139                 { "force-metadata",     0, NULL, 'F' },
140                 { "help",               0, NULL, 'h' },
141                 { NULL }
142         };
143         int opt, flags = BCH_FORCE_IF_DEGRADED;
144
145         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
146                 switch (opt) {
147                 case 'f':
148                         flags |= BCH_FORCE_IF_DATA_LOST;
149                         break;
150                 case 'F':
151                         flags |= BCH_FORCE_IF_METADATA_LOST;
152                         break;
153                 case 'h':
154                         device_remove_usage();
155                 }
156         args_shift(optind);
157
158         char *dev = arg_pop();
159         if (!dev)
160                 die("Please supply a device to remove");
161
162         if (argc)
163                 die("too many arguments");
164
165         unsigned dev_idx;
166         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
167         bchu_disk_remove(fs, dev_idx, flags);
168         return 0;
169 }
170
171 static void device_online_usage(void)
172 {
173         puts("bcachefs device online - readd a device to a running filesystem\n"
174              "Usage: bcachefs device online [OPTION]... device\n"
175              "\n"
176              "Options:\n"
177              "  -h, --help                  Display this help and exit\n"
178              "\n"
179              "Report bugs to <linux-bcache@vger.kernel.org>");
180 }
181
182 int cmd_device_online(int argc, char *argv[])
183 {
184         int opt;
185
186         while ((opt = getopt(argc, argv, "h")) != -1)
187                 switch (opt) {
188                 case 'h':
189                         device_online_usage();
190                         exit(EXIT_SUCCESS);
191                 }
192         args_shift(optind);
193
194         char *dev = arg_pop();
195         if (!dev)
196                 die("Please supply a device");
197
198         if (argc)
199                 die("too many arguments");
200
201         unsigned dev_idx;
202         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
203         bchu_disk_online(fs, dev);
204         return 0;
205 }
206
207 static void device_offline_usage(void)
208 {
209         puts("bcachefs device offline - take a device offline, without removing it\n"
210              "Usage: bcachefs device offline [OPTION]... device\n"
211              "\n"
212              "Options:\n"
213              "  -f, --force                 Force, if data redundancy will be degraded\n"
214              "  -h, --help                  Display this help and exit\n"
215              "\n"
216              "Report bugs to <linux-bcache@vger.kernel.org>");
217 }
218
219 int cmd_device_offline(int argc, char *argv[])
220 {
221         static const struct option longopts[] = {
222                 { "force",              0, NULL, 'f' },
223                 { NULL }
224         };
225         int opt, flags = 0;
226
227         while ((opt = getopt_long(argc, argv, "fh",
228                                   longopts, NULL)) != -1)
229                 switch (opt) {
230                 case 'f':
231                         flags |= BCH_FORCE_IF_DEGRADED;
232                         break;
233                 case 'h':
234                         device_offline_usage();
235                         exit(EXIT_SUCCESS);
236                 }
237         args_shift(optind);
238
239         char *dev = arg_pop();
240         if (!dev)
241                 die("Please supply a device");
242
243         if (argc)
244                 die("too many arguments");
245
246         unsigned dev_idx;
247         struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
248         bchu_disk_offline(fs, dev_idx, flags);
249         return 0;
250 }
251
252 static void device_evacuate_usage(void)
253 {
254         puts("bcachefs device evacuate - move data off of a given device\n"
255              "Usage: bcachefs device evacuate [OPTION]... device\n"
256              "\n"
257              "Options:\n"
258              "  -h, --help                  Display this help and exit\n"
259              "\n"
260              "Report bugs to <linux-bcache@vger.kernel.org>");
261 }
262
263 int cmd_device_evacuate(int argc, char *argv[])
264 {
265         int opt;
266
267         while ((opt = getopt(argc, argv, "h")) != -1)
268                 switch (opt) {
269                 case 'h':
270                         device_evacuate_usage();
271                         exit(EXIT_SUCCESS);
272                 }
273         args_shift(optind);
274
275         char *dev_path = arg_pop();
276         if (!dev_path)
277                 die("Please supply a device");
278
279         if (argc)
280                 die("too many arguments");
281
282         unsigned dev_idx;
283         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
284
285         return bchu_data(fs, (struct bch_ioctl_data) {
286                 .op             = BCH_DATA_OP_MIGRATE,
287                 .start          = POS_MIN,
288                 .end            = POS_MAX,
289                 .migrate.dev    = dev_idx,
290         });
291 }
292
293 static void device_set_state_usage(void)
294 {
295         puts("bcachefs device set-state\n"
296              "Usage: bcachefs device set-state device new-state\n"
297              "\n"
298              "Options:\n"
299              "  -f, --force                 Force, if data redundancy will be degraded\n"
300              "  -h, --help                  display this help and exit\n"
301              "Report bugs to <linux-bcache@vger.kernel.org>");
302         exit(EXIT_SUCCESS);
303 }
304
305 int cmd_device_set_state(int argc, char *argv[])
306 {
307         static const struct option longopts[] = {
308                 { "force",                      0, NULL, 'f' },
309                 { "help",                       0, NULL, 'h' },
310                 { NULL }
311         };
312         int opt, flags = 0;
313
314         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
315                 switch (opt) {
316                 case 'f':
317                         flags |= BCH_FORCE_IF_DEGRADED;
318                         break;
319                 case 'h':
320                         device_set_state_usage();
321                 }
322         args_shift(optind);
323
324         char *dev_path = arg_pop();
325         if (!dev_path)
326                 die("Please supply a device");
327
328         char *new_state_str = arg_pop();
329         if (!new_state_str)
330                 die("Please supply a device state");
331
332         unsigned new_state = read_string_list_or_die(new_state_str,
333                                         bch2_dev_state, "device state");
334
335         unsigned dev_idx;
336         struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
337
338         bchu_disk_set_state(fs, dev_idx, new_state, flags);
339         return 0;
340 }
341
342 static void device_resize_usage(void)
343 {
344         puts("bcachefs device resize \n"
345              "Usage: bcachefs device resize device [ size ]\n"
346              "\n"
347              "Options:\n"
348              "  -h, --help                  display this help and exit\n"
349              "Report bugs to <linux-bcache@vger.kernel.org>");
350         exit(EXIT_SUCCESS);
351 }
352
353 int cmd_device_resize(int argc, char *argv[])
354 {
355         static const struct option longopts[] = {
356                 { "help",                       0, NULL, 'h' },
357                 { NULL }
358         };
359         u64 size;
360         int opt;
361
362         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
363                 switch (opt) {
364                 case 'h':
365                         device_resize_usage();
366                 }
367         args_shift(optind);
368
369         char *dev = arg_pop();
370         if (!dev)
371                 die("Please supply a device to resize");
372
373         int dev_fd = xopen(dev, O_RDONLY);
374
375         char *size_arg = arg_pop();
376         if (!size_arg)
377                 size = get_size(dev, dev_fd);
378         else if (bch2_strtoull_h(size_arg, &size))
379                 die("invalid size");
380
381         size >>= 9;
382
383         if (argc)
384                 die("Too many arguments");
385
386         struct stat dev_stat = xfstat(dev_fd);
387
388         struct mntent *mount = dev_to_mount(dev);
389         if (mount) {
390                 if (!S_ISBLK(dev_stat.st_mode))
391                         die("%s is mounted but isn't a block device?!", dev);
392
393                 printf("Doing online resize of %s\n", dev);
394
395                 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
396
397                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
398
399                 struct bch_sb *sb = bchu_read_super(fs, -1);
400                 if (idx >= sb->nr_devices)
401                         die("error reading superblock: dev idx >= sb->nr_devices");
402
403                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
404                 if (!mi)
405                         die("error reading superblock: no member info");
406
407                 /* could also just read this out of sysfs... meh */
408                 struct bch_member *m = mi->members + idx;
409
410                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
411
412                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
413                 bchu_disk_resize(fs, idx, nbuckets);
414         } else {
415                 printf("Doing offline resize of %s\n", dev);
416
417                 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
418                 if (IS_ERR(c))
419                         die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
420
421                 struct bch_dev *ca, *resize = NULL;
422                 unsigned i;
423
424                 for_each_online_member(ca, c, i) {
425                         if (resize)
426                                 die("confused: more than one online device?");
427                         resize = ca;
428                         percpu_ref_get(&resize->io_ref);
429                 }
430
431                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
432
433                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
434                 int ret = bch2_dev_resize(c, resize, nbuckets);
435                 if (ret)
436                         fprintf(stderr, "resize error: %s\n", strerror(-ret));
437
438                 percpu_ref_put(&resize->io_ref);
439                 bch2_fs_stop(c);
440         }
441         return 0;
442 }