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 { "tier", required_argument, NULL, 't' },
45 { "force", no_argument, NULL, 'f' },
48 struct format_opts format_opts = format_opts_default();
49 struct dev_opts dev_opts = dev_opts_default();
53 while ((opt = getopt_long(argc, argv, "t:fh",
54 longopts, NULL)) != -1)
57 if (bch2_strtoull_h(optarg, &dev_opts.size))
58 die("invalid filesystem size");
63 dev_opts.bucket_size =
64 hatoi_validate(optarg, "bucket size");
67 dev_opts.discard = true;
70 if (kstrtouint(optarg, 10, &dev_opts.tier) ||
71 dev_opts.tier >= BCH_TIER_MAX)
83 char *fs_path = arg_pop();
85 die("Please supply a filesystem");
87 char *dev_path = arg_pop();
89 die("Please supply a device");
92 die("too many arguments");
94 struct bchfs_handle fs = bcache_fs_open(fs_path);
96 dev_opts.path = dev_path;
97 dev_opts.fd = open_for_format(dev_opts.path, force);
99 format_opts.block_size =
100 read_file_u64(fs.sysfs_fd, "block_size") >> 9;
101 format_opts.btree_node_size =
102 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
104 struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
109 bchu_disk_add(fs, dev_opts.path);
113 static void device_remove_usage(void)
115 puts("bcachefs device_remove - remove a device from a filesystem\n"
116 "Usage: bcachefs device remove device\n"
119 " -f, --force Force removal, even if some data\n"
120 " couldn't be migrated\n"
121 " --force-metadata Force removal, even if some metadata\n"
122 " couldn't be migrated\n"
123 " -h, --help display this help and exit\n"
124 "Report bugs to <linux-bcache@vger.kernel.org>");
128 int cmd_device_remove(int argc, char *argv[])
130 static const struct option longopts[] = {
131 { "force", 0, NULL, 'f' },
132 { "force-metadata", 0, NULL, 'F' },
133 { "help", 0, NULL, 'h' },
136 int opt, flags = BCH_FORCE_IF_DEGRADED;
138 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
141 flags |= BCH_FORCE_IF_DATA_LOST;
144 flags |= BCH_FORCE_IF_METADATA_LOST;
147 device_remove_usage();
151 char *dev = arg_pop();
153 die("Please supply a device to remove");
156 die("too many arguments");
159 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
160 bchu_disk_remove(fs, dev_idx, flags);
164 static void device_online_usage(void)
166 puts("bcachefs device online - readd a device to a running filesystem\n"
167 "Usage: bcachefs device online [OPTION]... device\n"
170 " -h, --help Display this help and exit\n"
172 "Report bugs to <linux-bcache@vger.kernel.org>");
175 int cmd_device_online(int argc, char *argv[])
179 while ((opt = getopt(argc, argv, "h")) != -1)
182 device_online_usage();
187 char *dev = arg_pop();
189 die("Please supply a device");
192 die("too many arguments");
195 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
196 bchu_disk_online(fs, dev);
200 static void device_offline_usage(void)
202 puts("bcachefs device offline - take a device offline, without removing it\n"
203 "Usage: bcachefs device offline [OPTION]... device\n"
206 " -f, --force Force, if data redundancy will be degraded\n"
207 " -h, --help Display this help and exit\n"
209 "Report bugs to <linux-bcache@vger.kernel.org>");
212 int cmd_device_offline(int argc, char *argv[])
214 static const struct option longopts[] = {
215 { "force", 0, NULL, 'f' },
220 while ((opt = getopt_long(argc, argv, "fh",
221 longopts, NULL)) != -1)
224 flags |= BCH_FORCE_IF_DEGRADED;
227 device_offline_usage();
232 char *dev = arg_pop();
234 die("Please supply a device");
237 die("too many arguments");
240 struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
241 bchu_disk_offline(fs, dev_idx, flags);
245 static void device_evacuate_usage(void)
247 puts("bcachefs device evacuate - move data off of a given device\n"
248 "Usage: bcachefs device evacuate [OPTION]... device\n"
251 " -h, --help Display this help and exit\n"
253 "Report bugs to <linux-bcache@vger.kernel.org>");
256 int cmd_device_evacuate(int argc, char *argv[])
260 while ((opt = getopt(argc, argv, "h")) != -1)
263 device_evacuate_usage();
268 char *dev_path = arg_pop();
270 die("Please supply a device");
273 die("too many arguments");
276 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
278 return bchu_data(fs, (struct bch_ioctl_data) {
279 .op = BCH_DATA_OP_MIGRATE,
282 .migrate.dev = dev_idx,
286 static void device_set_state_usage(void)
288 puts("bcachefs device set-state\n"
289 "Usage: bcachefs device set-state device new-state\n"
292 " -f, --force Force, if data redundancy will be degraded\n"
293 " -h, --help display this help and exit\n"
294 "Report bugs to <linux-bcache@vger.kernel.org>");
298 int cmd_device_set_state(int argc, char *argv[])
300 static const struct option longopts[] = {
301 { "force", 0, NULL, 'f' },
302 { "help", 0, NULL, 'h' },
307 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
310 flags |= BCH_FORCE_IF_DEGRADED;
313 device_set_state_usage();
317 char *dev_path = arg_pop();
319 die("Please supply a device");
321 char *new_state_str = arg_pop();
323 die("Please supply a device state");
325 unsigned new_state = read_string_list_or_die(new_state_str,
326 bch2_dev_state, "device state");
329 struct bchfs_handle fs = bchu_fs_open_by_dev(dev_path, &dev_idx);
331 bchu_disk_set_state(fs, dev_idx, new_state, flags);
335 static void device_resize_usage(void)
337 puts("bcachefs device resize \n"
338 "Usage: bcachefs device resize device [ size ]\n"
341 " -h, --help display this help and exit\n"
342 "Report bugs to <linux-bcache@vger.kernel.org>");
346 int cmd_device_resize(int argc, char *argv[])
348 static const struct option longopts[] = {
349 { "help", 0, NULL, 'h' },
355 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
358 device_resize_usage();
362 char *dev = arg_pop();
364 die("Please supply a device to resize");
366 int dev_fd = xopen(dev, O_RDONLY);
368 char *size_arg = arg_pop();
370 size = get_size(dev, dev_fd);
371 else if (bch2_strtoull_h(size_arg, &size))
377 die("Too many arguments");
379 struct stat dev_stat = xfstat(dev_fd);
381 char *mount = dev_to_mount(dev);
383 if (!S_ISBLK(dev_stat.st_mode))
384 die("%s is mounted but isn't a block device?!", dev);
386 printf("Doing online resize of %s\n", dev);
388 struct bchfs_handle fs = bcache_fs_open(mount);
390 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
392 struct bch_sb *sb = bchu_read_super(fs, -1);
393 if (idx >= sb->nr_devices)
394 die("error reading superblock: dev idx >= sb->nr_devices");
396 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
398 die("error reading superblock: no member info");
400 /* could also just read this out of sysfs... meh */
401 struct bch_member *m = mi->members + idx;
403 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
405 printf("resizing %s to %llu buckets\n", dev, nbuckets);
406 bchu_disk_resize(fs, idx, nbuckets);
408 printf("Doing offline resize of %s\n", dev);
410 struct bch_fs *c = bch2_fs_open(&dev, 1, bch2_opts_empty());
412 die("error opening %s: %s", dev, strerror(-PTR_ERR(c)));
414 struct bch_dev *ca, *resize = NULL;
417 for_each_online_member(ca, c, i) {
419 die("confused: more than one online device?");
421 percpu_ref_get(&resize->io_ref);
424 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
426 printf("resizing %s to %llu buckets\n", dev, nbuckets);
427 int ret = bch2_dev_resize(c, resize, nbuckets);
429 fprintf(stderr, "resize error: %s\n", strerror(-ret));
431 percpu_ref_put(&resize->io_ref);