2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
22 #include <sys/ioctl.h>
27 #include "kerncompat.h"
31 #include "cmds-fi-usage.h"
35 static const char * const device_cmd_group_usage[] = {
36 "btrfs device <command> [<args>]",
40 static const char * const cmd_device_add_usage[] = {
41 "btrfs device add [options] <device> [<device>...] <path>",
42 "Add a device to a filesystem",
43 "-K|--nodiscard do not perform whole device TRIM",
44 "-f|--force force overwrite existing filesystem on the disk",
48 static int cmd_device_add(int argc, char **argv)
51 int i, fdmnt, ret=0, e;
52 DIR *dirstream = NULL;
58 static const struct option long_options[] = {
59 { "nodiscard", optional_argument, NULL, 'K'},
60 { "force", no_argument, NULL, 'f'},
64 c = getopt_long(argc, argv, "Kf", long_options, NULL);
75 usage(cmd_device_add_usage);
81 if (check_argc_min(argc, 2))
82 usage(cmd_device_add_usage);
84 mntpnt = argv[optind + argc - 1];
86 fdmnt = open_file_or_dir(mntpnt, &dirstream);
88 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
92 for (i = optind; i < optind + argc - 1; i++){
93 struct btrfs_ioctl_vol_args ioctl_args;
95 u64 dev_block_count = 0;
99 res = test_dev_for_mkfs(argv[i], force);
105 devfd = open(argv[i], O_RDWR);
107 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
112 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
120 path = canonicalize_path(argv[i]);
123 "ERROR: Could not canonicalize pathname '%s': %s\n",
124 argv[i], strerror(errno));
129 memset(&ioctl_args, 0, sizeof(ioctl_args));
130 strncpy_null(ioctl_args.name, path);
131 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
134 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
142 close_file_or_dir(fdmnt, dirstream);
146 static int _cmd_device_remove(int argc, char **argv,
147 const char * const *usagestr)
150 int i, fdmnt, ret=0, e;
151 DIR *dirstream = NULL;
153 if (check_argc_min(argc, 3))
156 mntpnt = argv[argc - 1];
158 fdmnt = open_file_or_dir(mntpnt, &dirstream);
160 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
164 for(i=1 ; i < argc - 1; i++ ){
165 struct btrfs_ioctl_vol_args arg;
168 if (!is_block_device(argv[i])) {
170 "ERROR: %s is not a block device\n", argv[i]);
174 memset(&arg, 0, sizeof(arg));
175 strncpy_null(arg.name, argv[i]);
176 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
182 msg = btrfs_err_str(res);
186 "ERROR: error removing the device '%s' - %s\n",
192 close_file_or_dir(fdmnt, dirstream);
196 static const char * const cmd_device_remove_usage[] = {
197 "btrfs device remove <device> [<device>...] <path>",
198 "Remove a device from a filesystem",
202 static int cmd_device_remove(int argc, char **argv)
204 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
207 static const char * const cmd_device_delete_usage[] = {
208 "btrfs device delete <device> [<device>...] <path>",
209 "Remove a device from a filesystem",
213 static int cmd_device_delete(int argc, char **argv)
215 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
218 static const char * const cmd_device_scan_usage[] = {
219 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
220 "Scan devices for a btrfs filesystem",
221 " -d|--all-devices (deprecated)",
225 static int cmd_device_scan(int argc, char **argv)
235 static const struct option long_options[] = {
236 { "all-devices", no_argument, NULL, 'd'},
240 c = getopt_long(argc, argv, "d", long_options, NULL);
248 usage(cmd_device_scan_usage);
252 if (all && check_argc_max(argc, 2))
253 usage(cmd_device_scan_usage);
255 if (all || argc == 1) {
256 printf("Scanning for Btrfs filesystems\n");
257 ret = btrfs_scan_lblkid();
259 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
260 ret = btrfs_register_all_devices();
262 fprintf(stderr, "ERROR: error %d while registering\n", ret);
266 for( i = devstart ; i < argc ; i++ ){
269 if (!is_block_device(argv[i])) {
271 "ERROR: %s is not a block device\n", argv[i]);
275 path = canonicalize_path(argv[i]);
278 "ERROR: Could not canonicalize path '%s': %s\n",
279 argv[i], strerror(errno));
283 printf("Scanning for Btrfs filesystems in '%s'\n", path);
284 if (btrfs_register_one_device(path) != 0) {
296 static const char * const cmd_device_ready_usage[] = {
297 "btrfs device ready <device>",
298 "Check device to see if it has all of its devices in cache for mounting",
302 static int cmd_device_ready(int argc, char **argv)
304 struct btrfs_ioctl_vol_args args;
309 if (check_argc_min(argc, 2))
310 usage(cmd_device_ready_usage);
312 fd = open("/dev/btrfs-control", O_RDWR);
314 perror("failed to open /dev/btrfs-control");
318 path = canonicalize_path(argv[argc - 1]);
321 "ERROR: Could not canonicalize pathname '%s': %s\n",
322 argv[argc - 1], strerror(errno));
327 if (!is_block_device(path)) {
329 "ERROR: %s is not a block device\n", path);
334 memset(&args, 0, sizeof(args));
335 strncpy_null(args.name, path);
336 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
338 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
339 " is ready for mounting - %s\n", path,
350 static const char * const cmd_device_stats_usage[] = {
351 "btrfs device stats [-z] <path>|<device>",
352 "Show current device IO stats. -z to reset stats afterwards.",
356 static int cmd_device_stats(int argc, char **argv)
359 struct btrfs_ioctl_fs_info_args fi_args;
360 struct btrfs_ioctl_dev_info_args *di_args = NULL;
367 DIR *dirstream = NULL;
370 while ((c = getopt(argc, argv, "z")) != -1) {
373 flags = BTRFS_DEV_STATS_RESET;
377 usage(cmd_device_stats_usage);
381 argc = argc - optind;
382 if (check_argc_exact(argc, 1))
383 usage(cmd_device_stats_usage);
385 dev_path = argv[optind];
387 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
392 "ERROR: '%s' is not a mounted btrfs device\n",
395 fprintf(stderr, "ERROR: can't access '%s': %s\n",
396 dev_path, strerror(errno));
400 ret = get_fs_info(dev_path, &fi_args, &di_args);
402 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
403 "%s\n", strerror(-ret));
407 if (!fi_args.num_devices) {
408 fprintf(stderr, "ERROR: no devices found\n");
413 for (i = 0; i < fi_args.num_devices; i++) {
414 struct btrfs_ioctl_get_dev_stats args = {0};
415 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
417 strncpy((char *)path, (char *)di_args[i].path,
418 BTRFS_DEVICE_PATH_NAME_MAX);
419 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
421 args.devid = di_args[i].devid;
422 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
425 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
427 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
428 path, strerror(errno));
431 char *canonical_path;
433 canonical_path = canonicalize_path((char *)path);
435 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
436 printf("[%s].write_io_errs %llu\n",
438 (unsigned long long) args.values[
439 BTRFS_DEV_STAT_WRITE_ERRS]);
440 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
441 printf("[%s].read_io_errs %llu\n",
443 (unsigned long long) args.values[
444 BTRFS_DEV_STAT_READ_ERRS]);
445 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
446 printf("[%s].flush_io_errs %llu\n",
448 (unsigned long long) args.values[
449 BTRFS_DEV_STAT_FLUSH_ERRS]);
450 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
451 printf("[%s].corruption_errs %llu\n",
453 (unsigned long long) args.values[
454 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
455 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
456 printf("[%s].generation_errs %llu\n",
458 (unsigned long long) args.values[
459 BTRFS_DEV_STAT_GENERATION_ERRS]);
461 free(canonical_path);
467 close_file_or_dir(fdmnt, dirstream);
472 const char * const cmd_device_usage_usage[] = {
473 "btrfs device usage [options] <path> [<path>..]",
474 "Show detailed information about internal allocations in devices.",
475 "-b|--raw raw numbers in bytes",
476 "-h|--human-readable",
477 " human friendly numbers, base 1024 (default)",
478 "-H human friendly numbers, base 1000",
479 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
480 "--si use 1000 as a base (kB, MB, GB, TB)",
481 "-k|--kbytes show sizes in KiB, or kB with --si",
482 "-m|--mbytes show sizes in MiB, or MB with --si",
483 "-g|--gbytes show sizes in GiB, or GB with --si",
484 "-t|--tbytes show sizes in TiB, or TB with --si",
488 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
492 struct chunk_info *chunkinfo = NULL;
493 struct device_info *devinfo = NULL;
497 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
502 for (i = 0; i < devcount; i++) {
503 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
504 print_device_sizes(fd, &devinfo[i], unit_mode);
505 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
517 int cmd_device_usage(int argc, char **argv)
519 unsigned unit_mode = UNITS_DEFAULT;
521 int i, more_than_one = 0;
526 static const struct option long_options[] = {
527 { "raw", no_argument, NULL, 'b'},
528 { "kbytes", no_argument, NULL, 'k'},
529 { "mbytes", no_argument, NULL, 'm'},
530 { "gbytes", no_argument, NULL, 'g'},
531 { "tbytes", no_argument, NULL, 't'},
532 { "si", no_argument, NULL, GETOPT_VAL_SI},
533 { "iec", no_argument, NULL, GETOPT_VAL_IEC},
534 { "human-readable", no_argument, NULL,
535 GETOPT_VAL_HUMAN_READABLE},
539 c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
544 unit_mode = UNITS_RAW;
547 units_set_base(&unit_mode, UNITS_KBYTES);
550 units_set_base(&unit_mode, UNITS_MBYTES);
553 units_set_base(&unit_mode, UNITS_GBYTES);
556 units_set_base(&unit_mode, UNITS_TBYTES);
558 case GETOPT_VAL_HUMAN_READABLE:
560 unit_mode = UNITS_HUMAN_BINARY;
563 unit_mode = UNITS_HUMAN_DECIMAL;
566 units_set_mode(&unit_mode, UNITS_DECIMAL);
569 units_set_mode(&unit_mode, UNITS_BINARY);
572 usage(cmd_device_usage_usage);
576 if (check_argc_min(argc - optind, 1))
577 usage(cmd_device_usage_usage);
579 for (i = optind; i < argc ; i++) {
581 DIR *dirstream = NULL;
585 fd = open_file_or_dir(argv[i], &dirstream);
587 fprintf(stderr, "ERROR: can't access '%s'\n",
593 ret = _cmd_device_usage(fd, argv[i], unit_mode);
594 close_file_or_dir(fd, dirstream);
604 static const char device_cmd_group_info[] =
605 "manage and query devices in the filesystem";
607 const struct cmd_group device_cmd_group = {
608 device_cmd_group_usage, device_cmd_group_info, {
609 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
610 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
612 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
613 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
614 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
615 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
616 { "usage", cmd_device_usage,
617 cmd_device_usage_usage, NULL, 0 },
622 int cmd_device(int argc, char **argv)
624 return handle_command_group(&device_cmd_group, argc, argv);