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"
32 #include "cmds-fi-usage.h"
36 static const char * const device_cmd_group_usage[] = {
37 "btrfs device <command> [<args>]",
41 static const char * const cmd_device_add_usage[] = {
42 "btrfs device add [options] <device> [<device>...] <path>",
43 "Add a device to a filesystem",
44 "-K|--nodiscard do not perform whole device TRIM",
45 "-f|--force force overwrite existing filesystem on the disk",
49 static int cmd_device_add(int argc, char **argv)
52 int i, fdmnt, ret=0, e;
53 DIR *dirstream = NULL;
59 static const struct option long_options[] = {
60 { "nodiscard", optional_argument, NULL, 'K'},
61 { "force", no_argument, NULL, 'f'},
65 c = getopt_long(argc, argv, "Kf", long_options, NULL);
76 usage(cmd_device_add_usage);
82 if (check_argc_min(argc, 2))
83 usage(cmd_device_add_usage);
85 mntpnt = argv[optind + argc - 1];
87 fdmnt = open_file_or_dir(mntpnt, &dirstream);
89 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
93 for (i = optind; i < optind + argc - 1; i++){
94 struct btrfs_ioctl_vol_args ioctl_args;
96 u64 dev_block_count = 0;
100 res = test_dev_for_mkfs(argv[i], force);
106 devfd = open(argv[i], O_RDWR);
108 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
113 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
121 path = canonicalize_path(argv[i]);
124 "ERROR: Could not canonicalize pathname '%s': %s\n",
125 argv[i], strerror(errno));
130 memset(&ioctl_args, 0, sizeof(ioctl_args));
131 strncpy_null(ioctl_args.name, path);
132 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
135 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
143 close_file_or_dir(fdmnt, dirstream);
144 btrfs_close_all_devices();
148 static int _cmd_device_remove(int argc, char **argv,
149 const char * const *usagestr)
152 int i, fdmnt, ret=0, e;
153 DIR *dirstream = NULL;
155 if (check_argc_min(argc, 3))
158 mntpnt = argv[argc - 1];
160 fdmnt = open_file_or_dir(mntpnt, &dirstream);
162 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
166 for(i=1 ; i < argc - 1; i++ ){
167 struct btrfs_ioctl_vol_args arg;
170 if (!is_block_device(argv[i])) {
172 "ERROR: %s is not a block device\n", argv[i]);
176 memset(&arg, 0, sizeof(arg));
177 strncpy_null(arg.name, argv[i]);
178 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
184 msg = btrfs_err_str(res);
188 "ERROR: error removing the device '%s' - %s\n",
194 close_file_or_dir(fdmnt, dirstream);
198 static const char * const cmd_device_remove_usage[] = {
199 "btrfs device remove <device> [<device>...] <path>",
200 "Remove a device from a filesystem",
204 static int cmd_device_remove(int argc, char **argv)
206 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
209 static const char * const cmd_device_delete_usage[] = {
210 "btrfs device delete <device> [<device>...] <path>",
211 "Remove a device from a filesystem",
215 static int cmd_device_delete(int argc, char **argv)
217 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
220 static const char * const cmd_device_scan_usage[] = {
221 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
222 "Scan devices for a btrfs filesystem",
223 " -d|--all-devices (deprecated)",
227 static int cmd_device_scan(int argc, char **argv)
237 static const struct option long_options[] = {
238 { "all-devices", no_argument, NULL, 'd'},
242 c = getopt_long(argc, argv, "d", long_options, NULL);
250 usage(cmd_device_scan_usage);
254 if (all && check_argc_max(argc, 2))
255 usage(cmd_device_scan_usage);
257 if (all || argc == 1) {
258 printf("Scanning for Btrfs filesystems\n");
259 ret = btrfs_scan_lblkid();
261 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
262 ret = btrfs_register_all_devices();
264 fprintf(stderr, "ERROR: error %d while registering\n", ret);
268 for( i = devstart ; i < argc ; i++ ){
271 if (!is_block_device(argv[i])) {
273 "ERROR: %s is not a block device\n", argv[i]);
277 path = canonicalize_path(argv[i]);
280 "ERROR: Could not canonicalize path '%s': %s\n",
281 argv[i], strerror(errno));
285 printf("Scanning for Btrfs filesystems in '%s'\n", path);
286 if (btrfs_register_one_device(path) != 0) {
295 btrfs_close_all_devices();
299 static const char * const cmd_device_ready_usage[] = {
300 "btrfs device ready <device>",
301 "Check device to see if it has all of its devices in cache for mounting",
305 static int cmd_device_ready(int argc, char **argv)
307 struct btrfs_ioctl_vol_args args;
312 if (check_argc_min(argc, 2))
313 usage(cmd_device_ready_usage);
315 fd = open("/dev/btrfs-control", O_RDWR);
317 perror("failed to open /dev/btrfs-control");
321 path = canonicalize_path(argv[argc - 1]);
324 "ERROR: Could not canonicalize pathname '%s': %s\n",
325 argv[argc - 1], strerror(errno));
330 if (!is_block_device(path)) {
332 "ERROR: %s is not a block device\n", path);
337 memset(&args, 0, sizeof(args));
338 strncpy_null(args.name, path);
339 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
341 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
342 " is ready for mounting - %s\n", path,
353 static const char * const cmd_device_stats_usage[] = {
354 "btrfs device stats [-z] <path>|<device>",
355 "Show current device IO stats.",
357 "-z show current stats and reset values to zero",
361 static int cmd_device_stats(int argc, char **argv)
364 struct btrfs_ioctl_fs_info_args fi_args;
365 struct btrfs_ioctl_dev_info_args *di_args = NULL;
372 DIR *dirstream = NULL;
375 while ((c = getopt(argc, argv, "z")) != -1) {
378 flags = BTRFS_DEV_STATS_RESET;
382 usage(cmd_device_stats_usage);
386 argc = argc - optind;
387 if (check_argc_exact(argc, 1))
388 usage(cmd_device_stats_usage);
390 dev_path = argv[optind];
392 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
397 "ERROR: '%s' is not a mounted btrfs device\n",
400 fprintf(stderr, "ERROR: can't access '%s': %s\n",
401 dev_path, strerror(errno));
405 ret = get_fs_info(dev_path, &fi_args, &di_args);
407 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
408 "%s\n", strerror(-ret));
412 if (!fi_args.num_devices) {
413 fprintf(stderr, "ERROR: no devices found\n");
418 for (i = 0; i < fi_args.num_devices; i++) {
419 struct btrfs_ioctl_get_dev_stats args = {0};
420 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
422 strncpy((char *)path, (char *)di_args[i].path,
423 BTRFS_DEVICE_PATH_NAME_MAX);
424 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
426 args.devid = di_args[i].devid;
427 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
430 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
432 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
433 path, strerror(errno));
436 char *canonical_path;
438 canonical_path = canonicalize_path((char *)path);
440 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
441 printf("[%s].write_io_errs %llu\n",
443 (unsigned long long) args.values[
444 BTRFS_DEV_STAT_WRITE_ERRS]);
445 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
446 printf("[%s].read_io_errs %llu\n",
448 (unsigned long long) args.values[
449 BTRFS_DEV_STAT_READ_ERRS]);
450 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
451 printf("[%s].flush_io_errs %llu\n",
453 (unsigned long long) args.values[
454 BTRFS_DEV_STAT_FLUSH_ERRS]);
455 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
456 printf("[%s].corruption_errs %llu\n",
458 (unsigned long long) args.values[
459 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
460 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
461 printf("[%s].generation_errs %llu\n",
463 (unsigned long long) args.values[
464 BTRFS_DEV_STAT_GENERATION_ERRS]);
466 free(canonical_path);
472 close_file_or_dir(fdmnt, dirstream);
473 btrfs_close_all_devices();
478 const char * const cmd_device_usage_usage[] = {
479 "btrfs device usage [options] <path> [<path>..]",
480 "Show detailed information about internal allocations in devices.",
481 "-b|--raw raw numbers in bytes",
482 "-h|--human-readable",
483 " human friendly numbers, base 1024 (default)",
484 "-H human friendly numbers, base 1000",
485 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
486 "--si use 1000 as a base (kB, MB, GB, TB)",
487 "-k|--kbytes show sizes in KiB, or kB with --si",
488 "-m|--mbytes show sizes in MiB, or MB with --si",
489 "-g|--gbytes show sizes in GiB, or GB with --si",
490 "-t|--tbytes show sizes in TiB, or TB with --si",
494 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
498 struct chunk_info *chunkinfo = NULL;
499 struct device_info *devinfo = NULL;
503 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
508 for (i = 0; i < devcount; i++) {
509 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
510 print_device_sizes(fd, &devinfo[i], unit_mode);
511 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
523 int cmd_device_usage(int argc, char **argv)
525 unsigned unit_mode = UNITS_DEFAULT;
527 int i, more_than_one = 0;
532 static const struct option long_options[] = {
533 { "raw", no_argument, NULL, 'b'},
534 { "kbytes", no_argument, NULL, 'k'},
535 { "mbytes", no_argument, NULL, 'm'},
536 { "gbytes", no_argument, NULL, 'g'},
537 { "tbytes", no_argument, NULL, 't'},
538 { "si", no_argument, NULL, GETOPT_VAL_SI},
539 { "iec", no_argument, NULL, GETOPT_VAL_IEC},
540 { "human-readable", no_argument, NULL,
541 GETOPT_VAL_HUMAN_READABLE},
545 c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
550 unit_mode = UNITS_RAW;
553 units_set_base(&unit_mode, UNITS_KBYTES);
556 units_set_base(&unit_mode, UNITS_MBYTES);
559 units_set_base(&unit_mode, UNITS_GBYTES);
562 units_set_base(&unit_mode, UNITS_TBYTES);
564 case GETOPT_VAL_HUMAN_READABLE:
566 unit_mode = UNITS_HUMAN_BINARY;
569 unit_mode = UNITS_HUMAN_DECIMAL;
572 units_set_mode(&unit_mode, UNITS_DECIMAL);
575 units_set_mode(&unit_mode, UNITS_BINARY);
578 usage(cmd_device_usage_usage);
582 if (check_argc_min(argc - optind, 1))
583 usage(cmd_device_usage_usage);
585 for (i = optind; i < argc ; i++) {
587 DIR *dirstream = NULL;
591 fd = open_file_or_dir(argv[i], &dirstream);
593 fprintf(stderr, "ERROR: can't access '%s'\n",
599 ret = _cmd_device_usage(fd, argv[i], unit_mode);
600 close_file_or_dir(fd, dirstream);
610 static const char device_cmd_group_info[] =
611 "manage and query devices in the filesystem";
613 const struct cmd_group device_cmd_group = {
614 device_cmd_group_usage, device_cmd_group_info, {
615 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
616 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
618 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
619 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
620 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
621 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
622 { "usage", cmd_device_usage,
623 cmd_device_usage_usage, NULL, 0 },
628 int cmd_device(int argc, char **argv)
630 return handle_command_group(&device_cmd_group, argc, argv);