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 = btrfs_open_dir(mntpnt, &dirstream, 1);
91 for (i = optind; i < optind + argc - 1; i++){
92 struct btrfs_ioctl_vol_args ioctl_args;
94 u64 dev_block_count = 0;
98 res = test_dev_for_mkfs(argv[i], force);
104 devfd = open(argv[i], O_RDWR);
106 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
111 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
119 path = canonicalize_path(argv[i]);
122 "ERROR: Could not canonicalize pathname '%s': %s\n",
123 argv[i], strerror(errno));
128 memset(&ioctl_args, 0, sizeof(ioctl_args));
129 strncpy_null(ioctl_args.name, path);
130 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
133 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
141 close_file_or_dir(fdmnt, dirstream);
142 btrfs_close_all_devices();
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 = btrfs_open_dir(mntpnt, &dirstream, 1);
162 for(i=1 ; i < argc - 1; i++ ){
163 struct btrfs_ioctl_vol_args arg;
166 if (is_block_device(argv[i]) != 1) {
168 "ERROR: %s is not a block device\n", argv[i]);
172 memset(&arg, 0, sizeof(arg));
173 strncpy_null(arg.name, argv[i]);
174 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
180 msg = btrfs_err_str(res);
184 "ERROR: error removing the device '%s' - %s\n",
190 close_file_or_dir(fdmnt, dirstream);
194 static const char * const cmd_device_remove_usage[] = {
195 "btrfs device remove <device> [<device>...] <path>",
196 "Remove a device from a filesystem",
200 static int cmd_device_remove(int argc, char **argv)
202 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
205 static const char * const cmd_device_delete_usage[] = {
206 "btrfs device delete <device> [<device>...] <path>",
207 "Remove a device from a filesystem",
211 static int cmd_device_delete(int argc, char **argv)
213 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
216 static const char * const cmd_device_scan_usage[] = {
217 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
218 "Scan devices for a btrfs filesystem",
219 " -d|--all-devices (deprecated)",
223 static int cmd_device_scan(int argc, char **argv)
233 static const struct option long_options[] = {
234 { "all-devices", no_argument, NULL, 'd'},
238 c = getopt_long(argc, argv, "d", long_options, NULL);
246 usage(cmd_device_scan_usage);
250 if (all && check_argc_max(argc, 2))
251 usage(cmd_device_scan_usage);
253 if (all || argc == 1) {
254 printf("Scanning for Btrfs filesystems\n");
255 ret = btrfs_scan_lblkid();
257 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
258 ret = btrfs_register_all_devices();
260 fprintf(stderr, "ERROR: error %d while registering\n", ret);
264 for( i = devstart ; i < argc ; i++ ){
267 if (is_block_device(argv[i]) != 1) {
269 "ERROR: %s is not a block device\n", argv[i]);
273 path = canonicalize_path(argv[i]);
276 "ERROR: Could not canonicalize path '%s': %s\n",
277 argv[i], strerror(errno));
281 printf("Scanning for Btrfs filesystems in '%s'\n", path);
282 if (btrfs_register_one_device(path) != 0) {
291 btrfs_close_all_devices();
295 static const char * const cmd_device_ready_usage[] = {
296 "btrfs device ready <device>",
297 "Check device to see if it has all of its devices in cache for mounting",
301 static int cmd_device_ready(int argc, char **argv)
303 struct btrfs_ioctl_vol_args args;
308 if (check_argc_min(argc, 2))
309 usage(cmd_device_ready_usage);
311 fd = open("/dev/btrfs-control", O_RDWR);
313 perror("failed to open /dev/btrfs-control");
317 path = canonicalize_path(argv[argc - 1]);
320 "ERROR: Could not canonicalize pathname '%s': %s\n",
321 argv[argc - 1], strerror(errno));
326 if (is_block_device(path) != 1) {
328 "ERROR: %s is not a block device\n", path);
333 memset(&args, 0, sizeof(args));
334 strncpy_null(args.name, path);
335 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
337 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
338 " is ready for mounting - %s\n", path,
349 static const char * const cmd_device_stats_usage[] = {
350 "btrfs device stats [-z] <path>|<device>",
351 "Show current device IO stats.",
353 "-z show current stats and reset values to zero",
357 static int cmd_device_stats(int argc, char **argv)
360 struct btrfs_ioctl_fs_info_args fi_args;
361 struct btrfs_ioctl_dev_info_args *di_args = NULL;
368 DIR *dirstream = NULL;
371 while ((c = getopt(argc, argv, "z")) != -1) {
374 flags = BTRFS_DEV_STATS_RESET;
378 usage(cmd_device_stats_usage);
382 argc = argc - optind;
383 if (check_argc_exact(argc, 1))
384 usage(cmd_device_stats_usage);
386 dev_path = argv[optind];
388 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
393 "ERROR: '%s' is not a mounted btrfs device\n",
396 fprintf(stderr, "ERROR: can't access '%s': %s\n",
397 dev_path, strerror(errno));
401 ret = get_fs_info(dev_path, &fi_args, &di_args);
403 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
404 "%s\n", strerror(-ret));
408 if (!fi_args.num_devices) {
409 fprintf(stderr, "ERROR: no devices found\n");
414 for (i = 0; i < fi_args.num_devices; i++) {
415 struct btrfs_ioctl_get_dev_stats args = {0};
416 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
418 strncpy((char *)path, (char *)di_args[i].path,
419 BTRFS_DEVICE_PATH_NAME_MAX);
420 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
422 args.devid = di_args[i].devid;
423 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
426 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
428 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
429 path, strerror(errno));
432 char *canonical_path;
434 canonical_path = canonicalize_path((char *)path);
436 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
437 printf("[%s].write_io_errs %llu\n",
439 (unsigned long long) args.values[
440 BTRFS_DEV_STAT_WRITE_ERRS]);
441 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
442 printf("[%s].read_io_errs %llu\n",
444 (unsigned long long) args.values[
445 BTRFS_DEV_STAT_READ_ERRS]);
446 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
447 printf("[%s].flush_io_errs %llu\n",
449 (unsigned long long) args.values[
450 BTRFS_DEV_STAT_FLUSH_ERRS]);
451 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
452 printf("[%s].corruption_errs %llu\n",
454 (unsigned long long) args.values[
455 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
456 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
457 printf("[%s].generation_errs %llu\n",
459 (unsigned long long) args.values[
460 BTRFS_DEV_STAT_GENERATION_ERRS]);
462 free(canonical_path);
468 close_file_or_dir(fdmnt, dirstream);
469 btrfs_close_all_devices();
474 static const char * const cmd_device_usage_usage[] = {
475 "btrfs device usage [options] <path> [<path>..]",
476 "Show detailed information about internal allocations in devices.",
477 HELPINFO_OUTPUT_UNIT_DF,
481 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
485 struct chunk_info *chunkinfo = NULL;
486 struct device_info *devinfo = NULL;
490 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
495 for (i = 0; i < devcount; i++) {
496 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
497 print_device_sizes(fd, &devinfo[i], unit_mode);
498 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
510 static int cmd_device_usage(int argc, char **argv)
514 int more_than_one = 0;
517 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
519 if (check_argc_min(argc, 2) || argv[1][0] == '-')
520 usage(cmd_device_usage_usage);
522 for (i = 1; i < argc; i++) {
524 DIR *dirstream = NULL;
529 fd = btrfs_open_dir(argv[i], &dirstream, 1);
535 ret = _cmd_device_usage(fd, argv[i], unit_mode);
536 close_file_or_dir(fd, dirstream);
546 static const char device_cmd_group_info[] =
547 "manage and query devices in the filesystem";
549 const struct cmd_group device_cmd_group = {
550 device_cmd_group_usage, device_cmd_group_info, {
551 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
552 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
554 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
555 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
556 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
557 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
558 { "usage", cmd_device_usage,
559 cmd_device_usage_usage, NULL, 0 },
564 int cmd_device(int argc, char **argv)
566 return handle_command_group(&device_cmd_group, argc, argv);