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_add_dev_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_add_dev(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_add_dev_usage);
81 if (check_argc_min(argc, 2))
82 usage(cmd_add_dev_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 const char * const cmd_rm_dev_usage[] = {
147 "btrfs device delete <device> [<device>...] <path>",
148 "Remove a device from a filesystem",
152 static int cmd_rm_dev(int argc, char **argv)
155 int i, fdmnt, ret=0, e;
156 DIR *dirstream = NULL;
158 if (check_argc_min(argc, 3))
159 usage(cmd_rm_dev_usage);
161 mntpnt = argv[argc - 1];
163 fdmnt = open_file_or_dir(mntpnt, &dirstream);
165 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
169 for(i=1 ; i < argc - 1; i++ ){
170 struct btrfs_ioctl_vol_args arg;
173 if (!is_block_device(argv[i])) {
175 "ERROR: %s is not a block device\n", argv[i]);
179 memset(&arg, 0, sizeof(arg));
180 strncpy_null(arg.name, argv[i]);
181 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
187 msg = btrfs_err_str(res);
191 "ERROR: error removing the device '%s' - %s\n",
197 close_file_or_dir(fdmnt, dirstream);
201 static const char * const cmd_scan_dev_usage[] = {
202 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
203 "Scan devices for a btrfs filesystem",
204 " -d|--all-devices (deprecated)",
208 static int cmd_scan_dev(int argc, char **argv)
218 static const struct option long_options[] = {
219 { "all-devices", no_argument, NULL, 'd'},
223 c = getopt_long(argc, argv, "d", long_options, NULL);
231 usage(cmd_scan_dev_usage);
235 if (all && check_argc_max(argc, 2))
236 usage(cmd_scan_dev_usage);
238 if (all || argc == 1) {
239 printf("Scanning for Btrfs filesystems\n");
240 ret = btrfs_scan_lblkid();
242 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
243 ret = btrfs_register_all_devices();
245 fprintf(stderr, "ERROR: error %d while registering\n", ret);
249 for( i = devstart ; i < argc ; i++ ){
252 if (!is_block_device(argv[i])) {
254 "ERROR: %s is not a block device\n", argv[i]);
258 path = canonicalize_path(argv[i]);
261 "ERROR: Could not canonicalize path '%s': %s\n",
262 argv[i], strerror(errno));
266 printf("Scanning for Btrfs filesystems in '%s'\n", path);
267 if (btrfs_register_one_device(path) != 0) {
279 static const char * const cmd_ready_dev_usage[] = {
280 "btrfs device ready <device>",
281 "Check device to see if it has all of its devices in cache for mounting",
285 static int cmd_ready_dev(int argc, char **argv)
287 struct btrfs_ioctl_vol_args args;
292 if (check_argc_min(argc, 2))
293 usage(cmd_ready_dev_usage);
295 fd = open("/dev/btrfs-control", O_RDWR);
297 perror("failed to open /dev/btrfs-control");
301 path = canonicalize_path(argv[argc - 1]);
304 "ERROR: Could not canonicalize pathname '%s': %s\n",
305 argv[argc - 1], strerror(errno));
310 if (!is_block_device(path)) {
312 "ERROR: %s is not a block device\n", path);
317 memset(&args, 0, sizeof(args));
318 strncpy_null(args.name, path);
319 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
321 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
322 " is ready for mounting - %s\n", path,
333 static const char * const cmd_dev_stats_usage[] = {
334 "btrfs device stats [-z] <path>|<device>",
335 "Show current device IO stats. -z to reset stats afterwards.",
339 static int cmd_dev_stats(int argc, char **argv)
342 struct btrfs_ioctl_fs_info_args fi_args;
343 struct btrfs_ioctl_dev_info_args *di_args = NULL;
350 DIR *dirstream = NULL;
353 while ((c = getopt(argc, argv, "z")) != -1) {
356 flags = BTRFS_DEV_STATS_RESET;
360 usage(cmd_dev_stats_usage);
364 argc = argc - optind;
365 if (check_argc_exact(argc, 1))
366 usage(cmd_dev_stats_usage);
368 dev_path = argv[optind];
370 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
375 "ERROR: '%s' is not a mounted btrfs device\n",
378 fprintf(stderr, "ERROR: can't access '%s': %s\n",
379 dev_path, strerror(errno));
383 ret = get_fs_info(dev_path, &fi_args, &di_args);
385 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
386 "%s\n", strerror(-ret));
390 if (!fi_args.num_devices) {
391 fprintf(stderr, "ERROR: no devices found\n");
396 for (i = 0; i < fi_args.num_devices; i++) {
397 struct btrfs_ioctl_get_dev_stats args = {0};
398 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
400 strncpy((char *)path, (char *)di_args[i].path,
401 BTRFS_DEVICE_PATH_NAME_MAX);
402 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
404 args.devid = di_args[i].devid;
405 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
408 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
410 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
411 path, strerror(errno));
414 char *canonical_path;
416 canonical_path = canonicalize_path((char *)path);
418 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
419 printf("[%s].write_io_errs %llu\n",
421 (unsigned long long) args.values[
422 BTRFS_DEV_STAT_WRITE_ERRS]);
423 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
424 printf("[%s].read_io_errs %llu\n",
426 (unsigned long long) args.values[
427 BTRFS_DEV_STAT_READ_ERRS]);
428 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
429 printf("[%s].flush_io_errs %llu\n",
431 (unsigned long long) args.values[
432 BTRFS_DEV_STAT_FLUSH_ERRS]);
433 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
434 printf("[%s].corruption_errs %llu\n",
436 (unsigned long long) args.values[
437 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
438 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
439 printf("[%s].generation_errs %llu\n",
441 (unsigned long long) args.values[
442 BTRFS_DEV_STAT_GENERATION_ERRS]);
444 free(canonical_path);
450 close_file_or_dir(fdmnt, dirstream);
455 const char * const cmd_device_usage_usage[] = {
456 "btrfs device usage [options] <path> [<path>..]",
457 "Show detailed information about internal allocations in devices.",
458 "-b|--raw raw numbers in bytes",
459 "-h|--human-readable",
460 " human friendly numbers, base 1024 (default)",
461 "-H human friendly numbers, base 1000",
462 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
463 "--si use 1000 as a base (kB, MB, GB, TB)",
464 "-k|--kbytes show sizes in KiB, or kB with --si",
465 "-m|--mbytes show sizes in MiB, or MB with --si",
466 "-g|--gbytes show sizes in GiB, or GB with --si",
467 "-t|--tbytes show sizes in TiB, or TB with --si",
471 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
475 struct chunk_info *chunkinfo = NULL;
476 struct device_info *devinfo = NULL;
480 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
485 for (i = 0; i < devcount; i++) {
486 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
487 print_device_sizes(fd, &devinfo[i], unit_mode);
488 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
500 int cmd_device_usage(int argc, char **argv)
502 unsigned unit_mode = UNITS_DEFAULT;
504 int i, more_than_one = 0;
509 static const struct option long_options[] = {
510 { "raw", no_argument, NULL, 'b'},
511 { "kbytes", no_argument, NULL, 'k'},
512 { "mbytes", no_argument, NULL, 'm'},
513 { "gbytes", no_argument, NULL, 'g'},
514 { "tbytes", no_argument, NULL, 't'},
515 { "si", no_argument, NULL, GETOPT_VAL_SI},
516 { "iec", no_argument, NULL, GETOPT_VAL_IEC},
517 { "human-readable", no_argument, NULL,
518 GETOPT_VAL_HUMAN_READABLE},
522 c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
527 unit_mode = UNITS_RAW;
530 units_set_base(&unit_mode, UNITS_KBYTES);
533 units_set_base(&unit_mode, UNITS_MBYTES);
536 units_set_base(&unit_mode, UNITS_GBYTES);
539 units_set_base(&unit_mode, UNITS_TBYTES);
541 case GETOPT_VAL_HUMAN_READABLE:
543 unit_mode = UNITS_HUMAN_BINARY;
546 unit_mode = UNITS_HUMAN_DECIMAL;
549 units_set_mode(&unit_mode, UNITS_DECIMAL);
552 units_set_mode(&unit_mode, UNITS_BINARY);
555 usage(cmd_device_usage_usage);
559 if (check_argc_min(argc - optind, 1))
560 usage(cmd_device_usage_usage);
562 for (i = optind; i < argc ; i++) {
564 DIR *dirstream = NULL;
568 fd = open_file_or_dir(argv[i], &dirstream);
570 fprintf(stderr, "ERROR: can't access '%s'\n",
576 ret = _cmd_device_usage(fd, argv[i], unit_mode);
577 close_file_or_dir(fd, dirstream);
587 static const char device_cmd_group_info[] =
588 "manage and query devices in the filesystem";
590 const struct cmd_group device_cmd_group = {
591 device_cmd_group_usage, device_cmd_group_info, {
592 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
593 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
594 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
595 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
596 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
597 { "usage", cmd_device_usage,
598 cmd_device_usage_usage, NULL, 0 },
603 int cmd_device(int argc, char **argv)
605 return handle_command_group(&device_cmd_group, argc, argv);