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);
141 btrfs_close_all_devices();
145 static int _cmd_device_remove(int argc, char **argv,
146 const char * const *usagestr)
149 int i, fdmnt, ret=0, e;
150 DIR *dirstream = NULL;
152 if (check_argc_min(argc, 3))
155 mntpnt = argv[argc - 1];
157 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
161 for(i=1 ; i < argc - 1; i++ ){
162 struct btrfs_ioctl_vol_args arg;
165 if (is_block_device(argv[i]) != 1) {
167 "ERROR: %s is not a block device\n", argv[i]);
171 memset(&arg, 0, sizeof(arg));
172 strncpy_null(arg.name, argv[i]);
173 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
179 msg = btrfs_err_str(res);
183 "ERROR: error removing the device '%s' - %s\n",
189 close_file_or_dir(fdmnt, dirstream);
193 static const char * const cmd_device_remove_usage[] = {
194 "btrfs device remove <device> [<device>...] <path>",
195 "Remove a device from a filesystem",
199 static int cmd_device_remove(int argc, char **argv)
201 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
204 static const char * const cmd_device_delete_usage[] = {
205 "btrfs device delete <device> [<device>...] <path>",
206 "Remove a device from a filesystem",
210 static int cmd_device_delete(int argc, char **argv)
212 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
215 static const char * const cmd_device_scan_usage[] = {
216 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
217 "Scan devices for a btrfs filesystem",
218 " -d|--all-devices (deprecated)",
222 static int cmd_device_scan(int argc, char **argv)
232 static const struct option long_options[] = {
233 { "all-devices", no_argument, NULL, 'd'},
237 c = getopt_long(argc, argv, "d", long_options, NULL);
245 usage(cmd_device_scan_usage);
249 if (all && check_argc_max(argc, 2))
250 usage(cmd_device_scan_usage);
252 if (all || argc == 1) {
253 printf("Scanning for Btrfs filesystems\n");
254 ret = btrfs_scan_lblkid();
256 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
257 ret = btrfs_register_all_devices();
259 fprintf(stderr, "ERROR: error %d while registering\n", ret);
263 for( i = devstart ; i < argc ; i++ ){
266 if (is_block_device(argv[i]) != 1) {
268 "ERROR: %s is not a block device\n", argv[i]);
272 path = canonicalize_path(argv[i]);
275 "ERROR: Could not canonicalize path '%s': %s\n",
276 argv[i], strerror(errno));
280 printf("Scanning for Btrfs filesystems in '%s'\n", path);
281 if (btrfs_register_one_device(path) != 0) {
290 btrfs_close_all_devices();
294 static const char * const cmd_device_ready_usage[] = {
295 "btrfs device ready <device>",
296 "Check device to see if it has all of its devices in cache for mounting",
300 static int cmd_device_ready(int argc, char **argv)
302 struct btrfs_ioctl_vol_args args;
307 if (check_argc_min(argc, 2))
308 usage(cmd_device_ready_usage);
310 fd = open("/dev/btrfs-control", O_RDWR);
312 perror("failed to open /dev/btrfs-control");
316 path = canonicalize_path(argv[argc - 1]);
319 "ERROR: Could not canonicalize pathname '%s': %s\n",
320 argv[argc - 1], strerror(errno));
325 if (is_block_device(path) != 1) {
327 "ERROR: %s is not a block device\n", path);
332 memset(&args, 0, sizeof(args));
333 strncpy_null(args.name, path);
334 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
336 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
337 " is ready for mounting - %s\n", path,
348 static const char * const cmd_device_stats_usage[] = {
349 "btrfs device stats [-z] <path>|<device>",
350 "Show current device IO stats.",
352 "-z show current stats and reset values to zero",
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, 1);
391 ret = get_fs_info(dev_path, &fi_args, &di_args);
393 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
394 "%s\n", strerror(-ret));
398 if (!fi_args.num_devices) {
399 fprintf(stderr, "ERROR: no devices found\n");
404 for (i = 0; i < fi_args.num_devices; i++) {
405 struct btrfs_ioctl_get_dev_stats args = {0};
406 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
408 strncpy((char *)path, (char *)di_args[i].path,
409 BTRFS_DEVICE_PATH_NAME_MAX);
410 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
412 args.devid = di_args[i].devid;
413 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
416 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
418 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
419 path, strerror(errno));
422 char *canonical_path;
424 canonical_path = canonicalize_path((char *)path);
426 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
427 printf("[%s].write_io_errs %llu\n",
429 (unsigned long long) args.values[
430 BTRFS_DEV_STAT_WRITE_ERRS]);
431 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
432 printf("[%s].read_io_errs %llu\n",
434 (unsigned long long) args.values[
435 BTRFS_DEV_STAT_READ_ERRS]);
436 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
437 printf("[%s].flush_io_errs %llu\n",
439 (unsigned long long) args.values[
440 BTRFS_DEV_STAT_FLUSH_ERRS]);
441 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
442 printf("[%s].corruption_errs %llu\n",
444 (unsigned long long) args.values[
445 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
446 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
447 printf("[%s].generation_errs %llu\n",
449 (unsigned long long) args.values[
450 BTRFS_DEV_STAT_GENERATION_ERRS]);
452 free(canonical_path);
458 close_file_or_dir(fdmnt, dirstream);
459 btrfs_close_all_devices();
464 static const char * const cmd_device_usage_usage[] = {
465 "btrfs device usage [options] <path> [<path>..]",
466 "Show detailed information about internal allocations in devices.",
467 HELPINFO_OUTPUT_UNIT_DF,
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 static int cmd_device_usage(int argc, char **argv)
504 int more_than_one = 0;
507 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
509 if (check_argc_min(argc, 2) || argv[1][0] == '-')
510 usage(cmd_device_usage_usage);
512 for (i = 1; i < argc; i++) {
514 DIR *dirstream = NULL;
519 fd = btrfs_open_dir(argv[i], &dirstream, 1);
525 ret = _cmd_device_usage(fd, argv[i], unit_mode);
526 close_file_or_dir(fd, dirstream);
536 static const char device_cmd_group_info[] =
537 "manage and query devices in the filesystem";
539 const struct cmd_group device_cmd_group = {
540 device_cmd_group_usage, device_cmd_group_info, {
541 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
542 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
544 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
545 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
546 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
547 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
548 { "usage", cmd_device_usage,
549 cmd_device_usage_usage, NULL, 0 },
554 int cmd_device(int argc, char **argv)
556 return handle_command_group(&device_cmd_group, argc, argv);