10 #include <sys/ioctl.h>
12 #include <sys/types.h>
15 #include "libbcachefs/bcachefs_ioctl.h"
16 #include "libbcachefs/super-io.h"
18 #include "libbcachefs.h"
19 #include "libbcachefs/opts.h"
20 #include "tools-util.h"
22 static void device_add_usage(void)
24 puts("bcachefs device add - add a device to an existing filesystem\n"
25 "Usage: bcachefs device add [OPTION]... filesystem device\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"
35 "Report bugs to <linux-bcache@vger.kernel.org>");
38 int cmd_device_add(int argc, char *argv[])
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' },
49 struct format_opts format_opts = format_opts_default();
50 struct dev_opts dev_opts = dev_opts_default();
54 while ((opt = getopt_long(argc, argv, "t:fh",
55 longopts, NULL)) != -1)
58 if (bch2_strtoull_h(optarg, &dev_opts.size))
59 die("invalid filesystem size");
64 dev_opts.bucket_size =
65 hatoi_validate(optarg, "bucket size");
68 dev_opts.discard = true;
71 dev_opts.group = strdup(optarg);
82 char *fs_path = arg_pop();
84 die("Please supply a filesystem");
86 char *dev_path = arg_pop();
88 die("Please supply a device");
91 die("too many arguments");
93 struct bchfs_handle fs = bcache_fs_open(fs_path);
95 dev_opts.path = dev_path;
96 dev_opts.fd = open_for_format(dev_opts.path, force);
98 struct bch_opt_strs fs_opt_strs;
99 memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
101 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
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);
108 struct bch_sb *sb = bch2_format(fs_opt_strs,
116 bchu_disk_add(fs, dev_opts.path);
120 static void device_remove_usage(void)
122 puts("bcachefs device_remove - remove a device from a filesystem\n"
123 "Usage: bcachefs device remove device\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>");
135 int cmd_device_remove(int argc, char *argv[])
137 static const struct option longopts[] = {
138 { "force", 0, NULL, 'f' },
139 { "force-metadata", 0, NULL, 'F' },
140 { "help", 0, NULL, 'h' },
143 int opt, flags = BCH_FORCE_IF_DEGRADED;
145 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
148 flags |= BCH_FORCE_IF_DATA_LOST;
151 flags |= BCH_FORCE_IF_METADATA_LOST;
154 device_remove_usage();
158 char *dev = arg_pop();
160 die("Please supply a device to remove");
163 die("too many arguments");
166 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
167 bchu_disk_remove(fs, dev_idx, flags);
171 static void device_online_usage(void)
173 puts("bcachefs device online - readd a device to a running filesystem\n"
174 "Usage: bcachefs device online [OPTION]... device\n"
177 " -h, --help Display this help and exit\n"
179 "Report bugs to <linux-bcache@vger.kernel.org>");
182 int cmd_device_online(int argc, char *argv[])
186 while ((opt = getopt(argc, argv, "h")) != -1)
189 device_online_usage();
194 char *dev = arg_pop();
196 die("Please supply a device");
199 die("too many arguments");
202 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
203 bchu_disk_online(fs, dev);
207 static void device_offline_usage(void)
209 puts("bcachefs device offline - take a device offline, without removing it\n"
210 "Usage: bcachefs device offline [OPTION]... device\n"
213 " -f, --force Force, if data redundancy will be degraded\n"
214 " -h, --help Display this help and exit\n"
216 "Report bugs to <linux-bcache@vger.kernel.org>");
219 int cmd_device_offline(int argc, char *argv[])
221 static const struct option longopts[] = {
222 { "force", 0, NULL, 'f' },
227 while ((opt = getopt_long(argc, argv, "fh",
228 longopts, NULL)) != -1)
231 flags |= BCH_FORCE_IF_DEGRADED;
234 device_offline_usage();
239 char *dev = arg_pop();
241 die("Please supply a device");
244 die("too many arguments");
247 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
248 bchu_disk_offline(fs, dev_idx, flags);
252 static void device_evacuate_usage(void)
254 puts("bcachefs device evacuate - move data off of a given device\n"
255 "Usage: bcachefs device evacuate [OPTION]... device\n"
258 " -h, --help Display this help and exit\n"
260 "Report bugs to <linux-bcache@vger.kernel.org>");
263 int cmd_device_evacuate(int argc, char *argv[])
267 while ((opt = getopt(argc, argv, "h")) != -1)
270 device_evacuate_usage();
275 char *dev_path = arg_pop();
277 die("Please supply a device");
280 die("too many arguments");
283 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
285 return bchu_data(fs, (struct bch_ioctl_data) {
286 .op = BCH_DATA_OP_MIGRATE,
289 .migrate.dev = dev_idx,
293 static void device_set_state_usage(void)
295 puts("bcachefs device set-state\n"
296 "Usage: bcachefs device set-state device new-state\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>");
305 int cmd_device_set_state(int argc, char *argv[])
307 static const struct option longopts[] = {
308 { "force", 0, NULL, 'f' },
309 { "help", 0, NULL, 'h' },
314 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
317 flags |= BCH_FORCE_IF_DEGRADED;
320 device_set_state_usage();
324 char *dev_path = arg_pop();
326 die("Please supply a device");
328 char *new_state_str = arg_pop();
330 die("Please supply a device state");
332 unsigned new_state = read_string_list_or_die(new_state_str,
333 bch2_dev_state, "device state");
336 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
338 bchu_disk_set_state(fs, dev_idx, new_state, flags);
342 static void device_resize_usage(void)
344 puts("bcachefs device resize \n"
345 "Usage: bcachefs device resize device [ size ]\n"
348 " -h, --help display this help and exit\n"
349 "Report bugs to <linux-bcache@vger.kernel.org>");
353 int cmd_device_resize(int argc, char *argv[])
355 static const struct option longopts[] = {
356 { "help", 0, NULL, 'h' },
362 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
365 device_resize_usage();
369 char *dev = arg_pop();
371 die("Please supply a device to resize");
373 int dev_fd = xopen(dev, O_RDONLY);
375 char *size_arg = arg_pop();
377 size = get_size(dev, dev_fd);
378 else if (bch2_strtoull_h(size_arg, &size))
384 die("Too many arguments");
386 struct stat dev_stat = xfstat(dev_fd);
388 struct mntent *mount = dev_to_mount(dev);
390 if (!S_ISBLK(dev_stat.st_mode))
391 die("%s is mounted but isn't a block device?!", dev);
393 printf("Doing online resize of %s\n", dev);
395 struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
397 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
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");
403 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
405 die("error reading superblock: no member info");
407 /* could also just read this out of sysfs... meh */
408 struct bch_member *m = mi->members + idx;
410 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
412 printf("resizing %s to %llu buckets\n", dev, nbuckets);
413 bchu_disk_resize(fs, idx, nbuckets);
415 printf("Doing offline resize of %s\n", dev);
417 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
419 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
421 struct bch_dev *ca, *resize = NULL;
424 for_each_online_member(ca, c, i) {
426 die("confused: more than one online device?");
428 percpu_ref_get(&resize->io_ref);
431 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
433 printf("resizing %s to %llu buckets\n", dev, nbuckets);
434 int ret = bch2_dev_resize(c, resize, nbuckets);
436 fprintf(stderr, "resize error: %s\n", strerror(-ret));
438 percpu_ref_put(&resize->io_ref);