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 " --fs_size=size Size of filesystem on device\n"
29 " --bucket=size Bucket size\n"
30 " --discard Enable discards\n"
31 " -t, --tier=# Higher tier (e.g. 1) indicates slower devices\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 format_opts.block_size =
99 read_file_u64(fs.sysfs_fd, "block_size") >> 9;
100 format_opts.btree_node_size =
101 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
103 struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
108 bchu_disk_add(fs, dev_opts.path);
112 static void device_remove_usage(void)
114 puts("bcachefs device_remove - remove a device from a filesystem\n"
115 "Usage: bcachefs device remove device\n"
118 " -f, --force Force removal, even if some data\n"
119 " couldn't be migrated\n"
120 " --force-metadata Force removal, even if some metadata\n"
121 " couldn't be migrated\n"
122 " -h, --help display this help and exit\n"
123 "Report bugs to <linux-bcache@vger.kernel.org>");
127 int cmd_device_remove(int argc, char *argv[])
129 static const struct option longopts[] = {
130 { "force", 0, NULL, 'f' },
131 { "force-metadata", 0, NULL, 'F' },
132 { "help", 0, NULL, 'h' },
135 int opt, flags = BCH_FORCE_IF_DEGRADED;
137 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
140 flags |= BCH_FORCE_IF_DATA_LOST;
143 flags |= BCH_FORCE_IF_METADATA_LOST;
146 device_remove_usage();
150 char *dev = arg_pop();
152 die("Please supply a device to remove");
155 die("too many arguments");
158 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
159 bchu_disk_remove(fs, dev_idx, flags);
163 static void device_online_usage(void)
165 puts("bcachefs device online - readd a device to a running filesystem\n"
166 "Usage: bcachefs device online [OPTION]... device\n"
169 " -h, --help Display this help and exit\n"
171 "Report bugs to <linux-bcache@vger.kernel.org>");
174 int cmd_device_online(int argc, char *argv[])
178 while ((opt = getopt(argc, argv, "h")) != -1)
181 device_online_usage();
186 char *dev = arg_pop();
188 die("Please supply a device");
191 die("too many arguments");
194 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
195 bchu_disk_online(fs, dev);
199 static void device_offline_usage(void)
201 puts("bcachefs device offline - take a device offline, without removing it\n"
202 "Usage: bcachefs device offline [OPTION]... device\n"
205 " -f, --force Force, if data redundancy will be degraded\n"
206 " -h, --help Display this help and exit\n"
208 "Report bugs to <linux-bcache@vger.kernel.org>");
211 int cmd_device_offline(int argc, char *argv[])
213 static const struct option longopts[] = {
214 { "force", 0, NULL, 'f' },
219 while ((opt = getopt_long(argc, argv, "fh",
220 longopts, NULL)) != -1)
223 flags |= BCH_FORCE_IF_DEGRADED;
226 device_offline_usage();
231 char *dev = arg_pop();
233 die("Please supply a device");
236 die("too many arguments");
239 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
240 bchu_disk_offline(fs, dev_idx, flags);
244 static void device_evacuate_usage(void)
246 puts("bcachefs device evacuate - move data off of a given device\n"
247 "Usage: bcachefs device evacuate [OPTION]... device\n"
250 " -h, --help Display this help and exit\n"
252 "Report bugs to <linux-bcache@vger.kernel.org>");
255 int cmd_device_evacuate(int argc, char *argv[])
259 while ((opt = getopt(argc, argv, "h")) != -1)
262 device_evacuate_usage();
267 char *dev_path = arg_pop();
269 die("Please supply a device");
272 die("too many arguments");
275 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
277 return bchu_data(fs, (struct bch_ioctl_data) {
278 .op = BCH_DATA_OP_MIGRATE,
281 .migrate.dev = dev_idx,
285 static void device_set_state_usage(void)
287 puts("bcachefs device set-state\n"
288 "Usage: bcachefs device set-state device new-state\n"
291 " -f, --force Force, if data redundancy will be degraded\n"
292 " -h, --help display this help and exit\n"
293 "Report bugs to <linux-bcache@vger.kernel.org>");
297 int cmd_device_set_state(int argc, char *argv[])
299 static const struct option longopts[] = {
300 { "force", 0, NULL, 'f' },
301 { "help", 0, NULL, 'h' },
306 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
309 flags |= BCH_FORCE_IF_DEGRADED;
312 device_set_state_usage();
316 char *dev_path = arg_pop();
318 die("Please supply a device");
320 char *new_state_str = arg_pop();
322 die("Please supply a device state");
324 unsigned new_state = read_string_list_or_die(new_state_str,
325 bch2_dev_state, "device state");
328 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
330 bchu_disk_set_state(fs, dev_idx, new_state, flags);
334 static void device_resize_usage(void)
336 puts("bcachefs device resize \n"
337 "Usage: bcachefs device resize device [ size ]\n"
340 " -h, --help display this help and exit\n"
341 "Report bugs to <linux-bcache@vger.kernel.org>");
345 int cmd_device_resize(int argc, char *argv[])
347 static const struct option longopts[] = {
348 { "help", 0, NULL, 'h' },
354 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
357 device_resize_usage();
361 char *dev = arg_pop();
363 die("Please supply a device to resize");
365 int dev_fd = xopen(dev, O_RDONLY);
367 char *size_arg = arg_pop();
369 size = get_size(dev, dev_fd);
370 else if (bch2_strtoull_h(size_arg, &size))
376 die("Too many arguments");
378 struct stat dev_stat = xfstat(dev_fd);
380 char *mount = dev_to_mount(dev);
382 if (!S_ISBLK(dev_stat.st_mode))
383 die("%s is mounted but isn't a block device?!", dev);
385 printf("Doing online resize of %s\n", dev);
387 struct bchfs_handle fs = bcache_fs_open(mount);
389 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
391 struct bch_sb *sb = bchu_read_super(fs, -1);
392 if (idx >= sb->nr_devices)
393 die("error reading superblock: dev idx >= sb->nr_devices");
395 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
397 die("error reading superblock: no member info");
399 /* could also just read this out of sysfs... meh */
400 struct bch_member *m = mi->members + idx;
402 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
404 printf("resizing %s to %llu buckets\n", dev, nbuckets);
405 bchu_disk_resize(fs, idx, nbuckets);
407 printf("Doing offline resize of %s\n", dev);
409 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
411 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
413 struct bch_dev *ca, *resize = NULL;
416 for_each_online_member(ca, c, i) {
418 die("confused: more than one online device?");
420 percpu_ref_get(&resize->io_ref);
423 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
425 printf("resizing %s to %llu buckets\n", dev, nbuckets);
426 int ret = bch2_dev_resize(c, resize, nbuckets);
428 fprintf(stderr, "resize error: %s\n", strerror(-ret));
430 percpu_ref_put(&resize->io_ref);