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;
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 error("unable to open device '%s'", argv[i]);
110 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
118 path = canonicalize_path(argv[i]);
120 error("could not canonicalize pathname '%s': %s",
121 argv[i], strerror(errno));
126 memset(&ioctl_args, 0, sizeof(ioctl_args));
127 strncpy_null(ioctl_args.name, path);
128 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
130 error("error adding device '%s': %s",
131 path, strerror(errno));
138 close_file_or_dir(fdmnt, dirstream);
142 static int _cmd_device_remove(int argc, char **argv,
143 const char * const *usagestr)
146 int i, fdmnt, ret = 0;
147 DIR *dirstream = NULL;
149 clean_args_no_options(argc, argv, usagestr);
151 if (check_argc_min(argc - optind, 2))
154 mntpnt = argv[argc - 1];
156 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
160 for(i = optind; i < argc - 1; i++) {
161 struct btrfs_ioctl_vol_args arg;
162 struct btrfs_ioctl_vol_args_v2 argv2 = {0};
166 if (string_is_numerical(argv[i])) {
167 argv2.devid = arg_strtou64(argv[i]);
168 argv2.flags = BTRFS_DEVICE_SPEC_BY_ID;
170 } else if (is_block_device(argv[i]) == 1 ||
171 strcmp(argv[i], "missing") == 0) {
172 strncpy_null(argv2.name, argv[i]);
174 error("not a block device: %s", argv[i]);
180 * Positive values are from BTRFS_ERROR_DEV_*,
181 * otherwise it's a generic error, one of errnos
183 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV_V2, &argv2);
186 * If BTRFS_IOC_RM_DEV_V2 is not supported we get ENOTTY and if
187 * argv2.flags includes a flag which kernel doesn't understand then
188 * we shall get EOPNOTSUPP
190 if (res < 0 && (errno == ENOTTY || errno == EOPNOTSUPP)) {
192 error("device delete by id failed: %s",
197 memset(&arg, 0, sizeof(arg));
198 strncpy_null(arg.name, argv[i]);
199 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
206 msg = btrfs_err_str(res);
208 msg = strerror(errno);
210 error("error removing devid %llu: %s",
211 (unsigned long long)argv2.devid, msg);
213 error("error removing device '%s': %s",
220 close_file_or_dir(fdmnt, dirstream);
224 static const char * const cmd_device_remove_usage[] = {
225 "btrfs device remove <device>|<devid> [<device>|<devid>...] <path>",
226 "Remove a device from a filesystem",
230 static int cmd_device_remove(int argc, char **argv)
232 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
235 static const char * const cmd_device_delete_usage[] = {
236 "btrfs device delete <device>|<devid> [<device>|<devid>...] <path>",
237 "Remove a device from a filesystem",
241 static int cmd_device_delete(int argc, char **argv)
243 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
246 static const char * const cmd_device_scan_usage[] = {
247 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
248 "Scan devices for a btrfs filesystem",
249 " -d|--all-devices (deprecated)",
253 static int cmd_device_scan(int argc, char **argv)
262 static const struct option long_options[] = {
263 { "all-devices", no_argument, NULL, 'd'},
267 c = getopt_long(argc, argv, "d", long_options, NULL);
275 usage(cmd_device_scan_usage);
280 if (all && check_argc_max(argc - optind, 1))
281 usage(cmd_device_scan_usage);
283 if (all || argc - optind == 0) {
284 printf("Scanning for Btrfs filesystems\n");
285 ret = btrfs_scan_lblkid();
286 error_on(ret, "error %d while scanning", ret);
287 ret = btrfs_register_all_devices();
288 error_on(ret, "there are %d errors while registering devices", ret);
292 for( i = devstart ; i < argc ; i++ ){
295 if (is_block_device(argv[i]) != 1) {
296 error("not a block device: %s", argv[i]);
300 path = canonicalize_path(argv[i]);
302 error("could not canonicalize path '%s': %s",
303 argv[i], strerror(errno));
307 printf("Scanning for Btrfs filesystems in '%s'\n", path);
308 if (btrfs_register_one_device(path) != 0) {
320 static const char * const cmd_device_ready_usage[] = {
321 "btrfs device ready <device>",
322 "Check device to see if it has all of its devices in cache for mounting",
326 static int cmd_device_ready(int argc, char **argv)
328 struct btrfs_ioctl_vol_args args;
333 clean_args_no_options(argc, argv, cmd_device_ready_usage);
335 if (check_argc_exact(argc - optind, 1))
336 usage(cmd_device_ready_usage);
338 fd = open("/dev/btrfs-control", O_RDWR);
340 perror("failed to open /dev/btrfs-control");
344 path = canonicalize_path(argv[optind]);
346 error("could not canonicalize pathname '%s': %s",
347 argv[optind], strerror(errno));
352 if (is_block_device(path) != 1) {
353 error("not a block device: %s", path);
358 memset(&args, 0, sizeof(args));
359 strncpy_null(args.name, path);
360 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
362 error("unable to determine if device '%s' is ready for mount: %s",
363 path, strerror(errno));
373 static const char * const cmd_device_stats_usage[] = {
374 "btrfs device stats [-z] <path>|<device>",
375 "Show current device IO stats.",
377 "-z show current stats and reset values to zero",
381 static int cmd_device_stats(int argc, char **argv)
384 struct btrfs_ioctl_fs_info_args fi_args;
385 struct btrfs_ioctl_dev_info_args *di_args = NULL;
392 DIR *dirstream = NULL;
394 while ((c = getopt(argc, argv, "z")) != -1) {
397 flags = BTRFS_DEV_STATS_RESET;
401 usage(cmd_device_stats_usage);
405 if (check_argc_exact(argc - optind, 1))
406 usage(cmd_device_stats_usage);
408 dev_path = argv[optind];
410 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
414 ret = get_fs_info(dev_path, &fi_args, &di_args);
416 error("getting dev info for devstats failed: %s",
421 if (!fi_args.num_devices) {
422 error("no devices found");
427 for (i = 0; i < fi_args.num_devices; i++) {
428 struct btrfs_ioctl_get_dev_stats args = {0};
429 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
431 strncpy((char *)path, (char *)di_args[i].path,
432 BTRFS_DEVICE_PATH_NAME_MAX);
433 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
435 args.devid = di_args[i].devid;
436 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
439 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
440 error("DEV_STATS ioctl failed on %s: %s",
441 path, strerror(errno));
444 char *canonical_path;
446 canonical_path = canonicalize_path((char *)path);
448 /* No path when device is missing. */
449 if (!canonical_path) {
450 canonical_path = malloc(32);
451 if (!canonical_path) {
452 error("not enough memory for path buffer");
455 snprintf(canonical_path, 32,
456 "devid:%llu", args.devid);
459 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
460 printf("[%s].write_io_errs %llu\n",
462 (unsigned long long) args.values[
463 BTRFS_DEV_STAT_WRITE_ERRS]);
464 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
465 printf("[%s].read_io_errs %llu\n",
467 (unsigned long long) args.values[
468 BTRFS_DEV_STAT_READ_ERRS]);
469 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
470 printf("[%s].flush_io_errs %llu\n",
472 (unsigned long long) args.values[
473 BTRFS_DEV_STAT_FLUSH_ERRS]);
474 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
475 printf("[%s].corruption_errs %llu\n",
477 (unsigned long long) args.values[
478 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
479 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
480 printf("[%s].generation_errs %llu\n",
482 (unsigned long long) args.values[
483 BTRFS_DEV_STAT_GENERATION_ERRS]);
485 free(canonical_path);
491 close_file_or_dir(fdmnt, dirstream);
496 static const char * const cmd_device_usage_usage[] = {
497 "btrfs device usage [options] <path> [<path>..]",
498 "Show detailed information about internal allocations in devices.",
499 HELPINFO_UNITS_SHORT_LONG,
503 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
507 struct chunk_info *chunkinfo = NULL;
508 struct device_info *devinfo = NULL;
512 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
517 for (i = 0; i < devcount; i++) {
518 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
519 print_device_sizes(fd, &devinfo[i], unit_mode);
520 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
532 static int cmd_device_usage(int argc, char **argv)
538 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
540 clean_args_no_options(argc, argv, cmd_device_usage_usage);
542 if (check_argc_min(argc - optind, 1))
543 usage(cmd_device_usage_usage);
545 for (i = optind; i < argc; i++) {
547 DIR *dirstream = NULL;
552 fd = btrfs_open_dir(argv[i], &dirstream, 1);
558 ret = _cmd_device_usage(fd, argv[i], unit_mode);
559 close_file_or_dir(fd, dirstream);
568 static const char device_cmd_group_info[] =
569 "manage and query devices in the filesystem";
571 const struct cmd_group device_cmd_group = {
572 device_cmd_group_usage, device_cmd_group_info, {
573 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
574 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
576 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
577 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
578 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
579 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
580 { "usage", cmd_device_usage,
581 cmd_device_usage_usage, NULL, 0 },
586 int cmd_device(int argc, char **argv)
588 return handle_command_group(&device_cmd_group, argc, argv);