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 /* This code belongs under show_fs */
29 unsigned has_metadata;
38 /* XXX: dirty != used, it doesn't count metadata */
42 static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
44 return (struct bcache_dev) {
48 .has_metadata = read_file_u64(dir, "has_metadata"),
49 .has_data = read_file_u64(dir, "has_data"),
50 .state = read_file_str(dir, "state"),
51 .tier = read_file_u64(dir, "tier"),
53 .bucket_size = read_file_u64(dir, "bucket_size_bytes"),
54 .first_bucket = read_file_u64(dir, "first_bucket"),
55 .nbuckets = read_file_u64(dir, "nbuckets"),
56 .bytes_dirty = read_file_u64(dir, "dirty_bytes"),
60 static void show_dev(struct bcache_dev *dev)
62 u64 capacity = (dev->nbuckets - dev->first_bucket) *
65 * XXX: show fragmentation information, cached/dirty information
68 printf("Device %u (/dev/%s):\n"
69 " Has metadata:\t%u\n"
70 " Has dirty data:\t%u\n"
84 capacity - dev->bytes_dirty,
85 (dev->bytes_dirty * 100) / capacity);
88 int cmd_device_show(int argc, char *argv[])
90 int human_readable = 0;
92 // { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
94 { 'h', "human-readable", N_("print sizes in powers of 1024 (e.g., 1023M)"),
95 NULL, NULL, &human_readable, NULL},
98 char **args = bch_nih_init(argc, argv, opts);
101 die("Please supply a single device");
103 struct bchfs_handle fs = bcache_fs_open(argv[1]);
104 struct dirent *entry;
106 struct bcache_dev devices[256];
107 unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
109 unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */
110 memset(tiers, 0, sizeof(tiers));
112 while ((entry = readdir(fs.sysfs))) {
116 sscanf(entry->d_name, "cache%u%n", &nr, &pos);
117 if (pos != strlen(entry->d_name))
121 if (readlinkat(dirfd(fs.sysfs), entry->d_name,
122 link, sizeof(link)) < 0)
123 die("readlink error: %m\n");
125 char *dev_name = basename(dirname(link));
127 int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
129 devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
130 tiers[devices[nr_devices].tier]++;
136 for (i = 0; i < BCH_TIER_MAX; i++)
140 /* Print out devices sorted by tier: */
143 for (i = 0; i < BCH_TIER_MAX; i++) {
147 if (nr_active_tiers > 1) {
151 printf("Tier %u:\n\n", i);
154 for (j = 0; j < nr_devices; j++) {
155 if (devices[j].tier != i)
161 show_dev(&devices[j]);
169 static void disk_ioctl(const char *fs, const char *dev, int cmd, int flags)
171 struct bch_ioctl_disk i = { .flags = flags, };
173 if (!kstrtoull(dev, 10, &i.dev))
174 i.flags |= BCH_BY_INDEX;
178 xioctl(bcache_fs_open(fs).ioctl_fd, cmd, &i);
181 static void device_add_usage(void)
183 puts("bcachefs device add - add a device to an existing filesystem\n"
184 "Usage: bcachefs device add [OPTION]... filesystem device\n"
187 " --fs_size=size Size of filesystem on device\n"
188 " --bucket=size Bucket size\n"
189 " --discard Enable discards\n"
190 " -t, --tier=# Higher tier (e.g. 1) indicates slower devices\n"
191 " -f, --force Use device even if it appears to already be formatted\n"
192 " -h, --help Display this help and exit\n"
194 "Report bugs to <linux-bcache@vger.kernel.org>");
197 int cmd_device_add(int argc, char *argv[])
199 static const struct option longopts[] = {
200 { "fs_size", required_argument, NULL, 'S' },
201 { "bucket", required_argument, NULL, 'B' },
202 { "discard", no_argument, NULL, 'D' },
203 { "tier", required_argument, NULL, 't' },
204 { "force", no_argument, NULL, 'f' },
207 struct format_opts format_opts = format_opts_default();
208 struct dev_opts dev_opts = dev_opts_default();
212 while ((opt = getopt_long(argc, argv, "t:fh",
213 longopts, NULL)) != -1)
216 if (bch2_strtoull_h(optarg, &dev_opts.size))
217 die("invalid filesystem size");
222 dev_opts.bucket_size =
223 hatoi_validate(optarg, "bucket size");
226 dev_opts.discard = true;
229 if (kstrtouint(optarg, 10, &dev_opts.tier) ||
230 dev_opts.tier >= BCH_TIER_MAX)
241 if (argc - optind != 2)
242 die("Please supply a filesystem and a device to add");
244 struct bchfs_handle fs = bcache_fs_open(argv[optind]);
246 dev_opts.path = argv[optind + 1];
247 dev_opts.fd = open_for_format(dev_opts.path, force);
249 format_opts.block_size =
250 read_file_u64(fs.sysfs_fd, "block_size") >> 9;
251 format_opts.btree_node_size =
252 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
254 struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
259 bchu_disk_add(fs, dev_opts.path);
263 static void device_remove_usage(void)
265 puts("bcachefs device_remove - remove a device from a filesystem\n"
266 "Usage: bcachefs device remove filesystem device\n"
269 " -f, --force Force removal, even if some data\n"
270 " couldn't be migrated\n"
271 " --force-metadata Force removal, even if some metadata\n"
272 " couldn't be migrated\n"
273 " -h, --help display this help and exit\n"
274 "Report bugs to <linux-bcache@vger.kernel.org>");
278 int cmd_device_remove(int argc, char *argv[])
280 static const struct option longopts[] = {
281 { "force", 0, NULL, 'f' },
282 { "force-metadata", 0, NULL, 'F' },
283 { "help", 0, NULL, 'h' },
286 int opt, flags = BCH_FORCE_IF_DEGRADED;
288 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
291 flags |= BCH_FORCE_IF_DATA_LOST;
294 flags |= BCH_FORCE_IF_METADATA_LOST;
297 device_remove_usage();
300 if (argc - optind != 2)
301 die("Please supply a filesystem and at least one device to remove");
303 disk_ioctl(argv[optind], argv[optind + 1],
304 BCH_IOCTL_DISK_REMOVE, flags);
308 static void device_online_usage(void)
310 puts("bcachefs device online - readd a device to a running filesystem\n"
311 "Usage: bcachefs device online [OPTION]... filesystem device\n"
314 " -h, --help Display this help and exit\n"
316 "Report bugs to <linux-bcache@vger.kernel.org>");
319 int cmd_device_online(int argc, char *argv[])
323 while ((opt = getopt(argc, argv, "h")) != -1)
326 device_online_usage();
330 if (argc - optind != 2)
331 die("Please supply a filesystem and a device");
333 disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_ONLINE, 0);
337 static void device_offline_usage(void)
339 puts("bcachefs device offline - take a device offline, without removing it\n"
340 "Usage: bcachefs device offline [OPTION]... filesystem device\n"
343 " -f, --force Force, if data redundancy will be degraded\n"
344 " -h, --help Display this help and exit\n"
346 "Report bugs to <linux-bcache@vger.kernel.org>");
349 int cmd_device_offline(int argc, char *argv[])
351 static const struct option longopts[] = {
352 { "force", 0, NULL, 'f' },
357 while ((opt = getopt_long(argc, argv, "fh",
358 longopts, NULL)) != -1)
361 flags |= BCH_FORCE_IF_DEGRADED;
364 device_offline_usage();
368 if (argc - optind != 2)
369 die("Please supply a filesystem and a device");
371 disk_ioctl(argv[optind], argv[optind + 1],
372 BCH_IOCTL_DISK_OFFLINE, flags);
376 static void device_evacuate_usage(void)
378 puts("bcachefs device evacuate - move data off of a given device\n"
379 "Usage: bcachefs device evacuate [OPTION]... filesystem device\n"
382 " -h, --help Display this help and exit\n"
384 "Report bugs to <linux-bcache@vger.kernel.org>");
387 int cmd_device_evacuate(int argc, char *argv[])
391 while ((opt = getopt(argc, argv, "h")) != -1)
394 device_evacuate_usage();
398 if (argc - optind != 2)
399 die("Please supply a filesystem and a device");
401 disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_EVACUATE, 0);
405 static void device_set_state_usage(void)
407 puts("bcachefs device set-state\n"
408 "Usage: bcachefs device set-state filesystem device new-state\n"
411 " -f, --force Force, if data redundancy will be degraded\n"
412 " -h, --help display this help and exit\n"
413 "Report bugs to <linux-bcache@vger.kernel.org>");
417 int cmd_device_set_state(int argc, char *argv[])
419 static const struct option longopts[] = {
420 { "force", 0, NULL, 'f' },
421 { "help", 0, NULL, 'h' },
426 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
429 flags |= BCH_FORCE_IF_DEGRADED;
432 device_set_state_usage();
435 if (argc - optind != 3)
436 die("Please supply a filesystem, device and state");
438 struct bchfs_handle fs = bcache_fs_open(argv[optind]);
440 bchu_disk_set_state(fs, argv[optind + 1],
441 read_string_list_or_die(argv[optind + 2],
442 bch2_dev_state, "device state"),
447 static void device_resize_usage(void)
449 puts("bcachefs device resize \n"
450 "Usage: bcachefs device resize device [ size ]\n"
453 " -h, --help display this help and exit\n"
454 "Report bugs to <linux-bcache@vger.kernel.org>");
458 int cmd_device_resize(int argc, char *argv[])
460 static const struct option longopts[] = {
461 { "help", 0, NULL, 'h' },
467 while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
470 device_resize_usage();
473 if (argc < optind + 1)
474 die("Please supply a device to resize");
476 char *dev = argv[optind];
477 int dev_fd = xopen(dev, O_RDONLY);
479 if (argc == optind + 1)
480 size = get_size(dev, dev_fd);
481 else if (bch2_strtoull_h(argv[optind + 1], &size))
486 if (argc > optind + 2)
487 die("Too many arguments");
489 struct stat dev_stat = xfstat(dev_fd);
491 char *mount = dev_to_mount(dev);
493 if (!S_ISBLK(dev_stat.st_mode))
494 die("%s is mounted but isn't a block device?!", dev);
496 printf("Doing online resize of %s\n", dev);
498 struct bchfs_handle fs = bcache_fs_open(mount);
500 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
502 struct bch_sb *sb = bchu_read_super(fs, -1);
503 if (idx >= sb->nr_devices)
504 die("error reading superblock: dev idx >= sb->nr_devices");
506 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
508 die("error reading superblock: no member info");
510 /* could also just read this out of sysfs... meh */
511 struct bch_member *m = mi->members + idx;
513 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
515 printf("resizing %s to %llu buckets\n", dev, nbuckets);
516 bchu_disk_resize(fs, idx, nbuckets);
518 printf("Doing offline resize of %s\n", dev);
520 struct bch_fs *c = NULL;
521 struct bch_opts opts = bch2_opts_empty();
522 const char *err = bch2_fs_open(&dev, 1, opts, &c);
524 die("error opening %s: %s", argv[optind], err);
526 struct bch_dev *ca, *resize = NULL;
529 for_each_online_member(ca, c, i) {
531 die("confused: more than one online device?");
533 percpu_ref_get(&resize->io_ref);
536 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
538 printf("resizing %s to %llu buckets\n", dev, nbuckets);
539 int ret = bch2_dev_resize(c, resize, nbuckets);
541 fprintf(stderr, "resize error: %s\n", strerror(-ret));
543 percpu_ref_put(&resize->io_ref);