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;
60 static const struct option long_options[] = {
61 { "nodiscard", optional_argument, NULL, 'K'},
62 { "force", no_argument, NULL, 'f'},
66 c = getopt_long(argc, argv, "Kf", long_options, NULL);
77 usage(cmd_device_add_usage);
81 if (check_argc_min(argc - optind, 2))
82 usage(cmd_device_add_usage);
85 mntpnt = argv[last_dev];
87 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
91 for (i = optind; i < last_dev; i++){
92 struct btrfs_ioctl_vol_args ioctl_args;
94 u64 dev_block_count = 0;
97 res = test_dev_for_mkfs(argv[i], force);
103 devfd = open(argv[i], O_RDWR);
105 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
110 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
118 path = canonicalize_path(argv[i]);
121 "ERROR: Could not canonicalize pathname '%s': %s\n",
122 argv[i], strerror(errno));
127 memset(&ioctl_args, 0, sizeof(ioctl_args));
128 strncpy_null(ioctl_args.name, path);
129 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
132 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
140 close_file_or_dir(fdmnt, dirstream);
144 static int _cmd_device_remove(int argc, char **argv,
145 const char * const *usagestr)
148 int i, fdmnt, ret=0, e;
149 DIR *dirstream = NULL;
151 if (check_argc_min(argc, 3))
154 mntpnt = argv[argc - 1];
156 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
160 for(i=1 ; i < argc - 1; i++ ){
161 struct btrfs_ioctl_vol_args arg;
164 if (is_block_device(argv[i]) != 1) {
166 "ERROR: %s is not a block device\n", argv[i]);
170 memset(&arg, 0, sizeof(arg));
171 strncpy_null(arg.name, argv[i]);
172 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
178 msg = btrfs_err_str(res);
182 "ERROR: error removing the device '%s' - %s\n",
188 close_file_or_dir(fdmnt, dirstream);
192 static const char * const cmd_device_remove_usage[] = {
193 "btrfs device remove <device> [<device>...] <path>",
194 "Remove a device from a filesystem",
198 static int cmd_device_remove(int argc, char **argv)
200 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
203 static const char * const cmd_device_delete_usage[] = {
204 "btrfs device delete <device> [<device>...] <path>",
205 "Remove a device from a filesystem",
209 static int cmd_device_delete(int argc, char **argv)
211 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
214 static const char * const cmd_device_scan_usage[] = {
215 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
216 "Scan devices for a btrfs filesystem",
217 " -d|--all-devices (deprecated)",
221 static int cmd_device_scan(int argc, char **argv)
231 static const struct option long_options[] = {
232 { "all-devices", no_argument, NULL, 'd'},
236 c = getopt_long(argc, argv, "d", long_options, NULL);
244 usage(cmd_device_scan_usage);
248 if (all && check_argc_max(argc, 2))
249 usage(cmd_device_scan_usage);
251 if (all || argc == 1) {
252 printf("Scanning for Btrfs filesystems\n");
253 ret = btrfs_scan_lblkid();
255 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
256 ret = btrfs_register_all_devices();
258 fprintf(stderr, "ERROR: error %d while registering\n", ret);
262 for( i = devstart ; i < argc ; i++ ){
265 if (is_block_device(argv[i]) != 1) {
267 "ERROR: %s is not a block device\n", argv[i]);
271 path = canonicalize_path(argv[i]);
274 "ERROR: Could not canonicalize path '%s': %s\n",
275 argv[i], strerror(errno));
279 printf("Scanning for Btrfs filesystems in '%s'\n", path);
280 if (btrfs_register_one_device(path) != 0) {
292 static const char * const cmd_device_ready_usage[] = {
293 "btrfs device ready <device>",
294 "Check device to see if it has all of its devices in cache for mounting",
298 static int cmd_device_ready(int argc, char **argv)
300 struct btrfs_ioctl_vol_args args;
305 if (check_argc_min(argc, 2))
306 usage(cmd_device_ready_usage);
308 fd = open("/dev/btrfs-control", O_RDWR);
310 perror("failed to open /dev/btrfs-control");
314 path = canonicalize_path(argv[argc - 1]);
317 "ERROR: Could not canonicalize pathname '%s': %s\n",
318 argv[argc - 1], strerror(errno));
323 if (is_block_device(path) != 1) {
325 "ERROR: %s is not a block device\n", path);
330 memset(&args, 0, sizeof(args));
331 strncpy_null(args.name, path);
332 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
334 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
335 " is ready for mounting - %s\n", path,
346 static const char * const cmd_device_stats_usage[] = {
347 "btrfs device stats [-z] <path>|<device>",
348 "Show current device IO stats.",
350 "-z show current stats and reset values to zero",
354 static int cmd_device_stats(int argc, char **argv)
357 struct btrfs_ioctl_fs_info_args fi_args;
358 struct btrfs_ioctl_dev_info_args *di_args = NULL;
365 DIR *dirstream = NULL;
368 while ((c = getopt(argc, argv, "z")) != -1) {
371 flags = BTRFS_DEV_STATS_RESET;
375 usage(cmd_device_stats_usage);
379 argc = argc - optind;
380 if (check_argc_exact(argc, 1))
381 usage(cmd_device_stats_usage);
383 dev_path = argv[optind];
385 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
389 ret = get_fs_info(dev_path, &fi_args, &di_args);
391 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
392 "%s\n", strerror(-ret));
396 if (!fi_args.num_devices) {
397 fprintf(stderr, "ERROR: no devices found\n");
402 for (i = 0; i < fi_args.num_devices; i++) {
403 struct btrfs_ioctl_get_dev_stats args = {0};
404 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
406 strncpy((char *)path, (char *)di_args[i].path,
407 BTRFS_DEVICE_PATH_NAME_MAX);
408 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
410 args.devid = di_args[i].devid;
411 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
414 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
416 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
417 path, strerror(errno));
420 char *canonical_path;
422 canonical_path = canonicalize_path((char *)path);
424 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
425 printf("[%s].write_io_errs %llu\n",
427 (unsigned long long) args.values[
428 BTRFS_DEV_STAT_WRITE_ERRS]);
429 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
430 printf("[%s].read_io_errs %llu\n",
432 (unsigned long long) args.values[
433 BTRFS_DEV_STAT_READ_ERRS]);
434 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
435 printf("[%s].flush_io_errs %llu\n",
437 (unsigned long long) args.values[
438 BTRFS_DEV_STAT_FLUSH_ERRS]);
439 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
440 printf("[%s].corruption_errs %llu\n",
442 (unsigned long long) args.values[
443 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
444 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
445 printf("[%s].generation_errs %llu\n",
447 (unsigned long long) args.values[
448 BTRFS_DEV_STAT_GENERATION_ERRS]);
450 free(canonical_path);
456 close_file_or_dir(fdmnt, dirstream);
461 static const char * const cmd_device_usage_usage[] = {
462 "btrfs device usage [options] <path> [<path>..]",
463 "Show detailed information about internal allocations in devices.",
464 HELPINFO_OUTPUT_UNIT_DF,
468 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
472 struct chunk_info *chunkinfo = NULL;
473 struct device_info *devinfo = NULL;
477 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
482 for (i = 0; i < devcount; i++) {
483 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
484 print_device_sizes(fd, &devinfo[i], unit_mode);
485 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
497 static int cmd_device_usage(int argc, char **argv)
501 int more_than_one = 0;
504 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
506 if (check_argc_min(argc, 2) || argv[1][0] == '-')
507 usage(cmd_device_usage_usage);
509 for (i = 1; i < argc; i++) {
511 DIR *dirstream = NULL;
516 fd = btrfs_open_dir(argv[i], &dirstream, 1);
522 ret = _cmd_device_usage(fd, argv[i], unit_mode);
523 close_file_or_dir(fd, dirstream);
533 static const char device_cmd_group_info[] =
534 "manage and query devices in the filesystem";
536 const struct cmd_group device_cmd_group = {
537 device_cmd_group_usage, device_cmd_group_info, {
538 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
539 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
541 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
542 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
543 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
544 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
545 { "usage", cmd_device_usage,
546 cmd_device_usage_usage, NULL, 0 },
551 int cmd_device(int argc, char **argv)
553 return handle_command_group(&device_cmd_group, argc, argv);