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 if (check_argc_min(argc, 3))
152 mntpnt = argv[argc - 1];
154 fdmnt = btrfs_open_dir(mntpnt, &dirstream, 1);
158 for(i=1 ; i < argc - 1; i++ ){
159 struct btrfs_ioctl_vol_args arg;
162 if (is_block_device(argv[i]) != 1 && strcmp(argv[i], "missing")) {
163 error("not a block device: %s", argv[i]);
167 memset(&arg, 0, sizeof(arg));
168 strncpy_null(arg.name, argv[i]);
169 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
174 msg = btrfs_err_str(res);
176 msg = strerror(errno);
177 error("error removing device '%s': %s",
183 close_file_or_dir(fdmnt, dirstream);
187 static const char * const cmd_device_remove_usage[] = {
188 "btrfs device remove <device> [<device>...] <path>",
189 "Remove a device from a filesystem",
193 static int cmd_device_remove(int argc, char **argv)
195 return _cmd_device_remove(argc, argv, cmd_device_remove_usage);
198 static const char * const cmd_device_delete_usage[] = {
199 "btrfs device delete <device> [<device>...] <path>",
200 "Remove a device from a filesystem",
204 static int cmd_device_delete(int argc, char **argv)
206 return _cmd_device_remove(argc, argv, cmd_device_delete_usage);
209 static const char * const cmd_device_scan_usage[] = {
210 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
211 "Scan devices for a btrfs filesystem",
212 " -d|--all-devices (deprecated)",
216 static int cmd_device_scan(int argc, char **argv)
226 static const struct option long_options[] = {
227 { "all-devices", no_argument, NULL, 'd'},
231 c = getopt_long(argc, argv, "d", long_options, NULL);
239 usage(cmd_device_scan_usage);
243 if (all && check_argc_max(argc, 2))
244 usage(cmd_device_scan_usage);
246 if (all || argc == 1) {
247 printf("Scanning for Btrfs filesystems\n");
248 ret = btrfs_scan_lblkid();
249 error_on(ret, "error %d while scanning", ret);
250 ret = btrfs_register_all_devices();
251 error_on(ret, "error %d while registering devices", ret);
255 for( i = devstart ; i < argc ; i++ ){
258 if (is_block_device(argv[i]) != 1) {
259 error("not a block device: %s", argv[i]);
263 path = canonicalize_path(argv[i]);
265 error("could not canonicalize path '%s': %s",
266 argv[i], strerror(errno));
270 printf("Scanning for Btrfs filesystems in '%s'\n", path);
271 if (btrfs_register_one_device(path) != 0) {
283 static const char * const cmd_device_ready_usage[] = {
284 "btrfs device ready <device>",
285 "Check device to see if it has all of its devices in cache for mounting",
289 static int cmd_device_ready(int argc, char **argv)
291 struct btrfs_ioctl_vol_args args;
296 if (check_argc_min(argc, 2))
297 usage(cmd_device_ready_usage);
299 fd = open("/dev/btrfs-control", O_RDWR);
301 perror("failed to open /dev/btrfs-control");
305 path = canonicalize_path(argv[argc - 1]);
307 error("could not canonicalize pathname '%s': %s",
308 argv[argc - 1], strerror(errno));
313 if (is_block_device(path) != 1) {
314 error("not a block device: %s", path);
319 memset(&args, 0, sizeof(args));
320 strncpy_null(args.name, path);
321 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
323 error("unable to determine if device '%s' is ready for mount: %s",
324 path, strerror(errno));
334 static const char * const cmd_device_stats_usage[] = {
335 "btrfs device stats [-z] <path>|<device>",
336 "Show current device IO stats.",
338 "-z show current stats and reset values to zero",
342 static int cmd_device_stats(int argc, char **argv)
345 struct btrfs_ioctl_fs_info_args fi_args;
346 struct btrfs_ioctl_dev_info_args *di_args = NULL;
353 DIR *dirstream = NULL;
356 while ((c = getopt(argc, argv, "z")) != -1) {
359 flags = BTRFS_DEV_STATS_RESET;
363 usage(cmd_device_stats_usage);
367 argc = argc - optind;
368 if (check_argc_exact(argc, 1))
369 usage(cmd_device_stats_usage);
371 dev_path = argv[optind];
373 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
377 ret = get_fs_info(dev_path, &fi_args, &di_args);
379 error("getting dev info for devstats failed: %s",
384 if (!fi_args.num_devices) {
385 error("no devices found");
390 for (i = 0; i < fi_args.num_devices; i++) {
391 struct btrfs_ioctl_get_dev_stats args = {0};
392 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
394 strncpy((char *)path, (char *)di_args[i].path,
395 BTRFS_DEVICE_PATH_NAME_MAX);
396 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
398 args.devid = di_args[i].devid;
399 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
402 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
403 error("DEV_STATS ioctl failed on %s: %s",
404 path, strerror(errno));
407 char *canonical_path;
409 canonical_path = canonicalize_path((char *)path);
411 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
412 printf("[%s].write_io_errs %llu\n",
414 (unsigned long long) args.values[
415 BTRFS_DEV_STAT_WRITE_ERRS]);
416 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
417 printf("[%s].read_io_errs %llu\n",
419 (unsigned long long) args.values[
420 BTRFS_DEV_STAT_READ_ERRS]);
421 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
422 printf("[%s].flush_io_errs %llu\n",
424 (unsigned long long) args.values[
425 BTRFS_DEV_STAT_FLUSH_ERRS]);
426 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
427 printf("[%s].corruption_errs %llu\n",
429 (unsigned long long) args.values[
430 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
431 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
432 printf("[%s].generation_errs %llu\n",
434 (unsigned long long) args.values[
435 BTRFS_DEV_STAT_GENERATION_ERRS]);
437 free(canonical_path);
443 close_file_or_dir(fdmnt, dirstream);
448 static const char * const cmd_device_usage_usage[] = {
449 "btrfs device usage [options] <path> [<path>..]",
450 "Show detailed information about internal allocations in devices.",
451 HELPINFO_UNITS_SHORT_LONG,
455 static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
459 struct chunk_info *chunkinfo = NULL;
460 struct device_info *devinfo = NULL;
464 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
469 for (i = 0; i < devcount; i++) {
470 printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
471 print_device_sizes(fd, &devinfo[i], unit_mode);
472 print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
484 static int cmd_device_usage(int argc, char **argv)
490 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
492 if (check_argc_min(argc, 2) || argv[1][0] == '-')
493 usage(cmd_device_usage_usage);
495 for (i = 1; i < argc; i++) {
497 DIR *dirstream = NULL;
502 fd = btrfs_open_dir(argv[i], &dirstream, 1);
508 ret = _cmd_device_usage(fd, argv[i], unit_mode);
509 close_file_or_dir(fd, dirstream);
518 static const char device_cmd_group_info[] =
519 "manage and query devices in the filesystem";
521 const struct cmd_group device_cmd_group = {
522 device_cmd_group_usage, device_cmd_group_info, {
523 { "add", cmd_device_add, cmd_device_add_usage, NULL, 0 },
524 { "delete", cmd_device_delete, cmd_device_delete_usage, NULL,
526 { "remove", cmd_device_remove, cmd_device_remove_usage, NULL, 0 },
527 { "scan", cmd_device_scan, cmd_device_scan_usage, NULL, 0 },
528 { "ready", cmd_device_ready, cmd_device_ready_usage, NULL, 0 },
529 { "stats", cmd_device_stats, cmd_device_stats_usage, NULL, 0 },
530 { "usage", cmd_device_usage,
531 cmd_device_usage_usage, NULL, 0 },
536 int cmd_device(int argc, char **argv)
538 return handle_command_group(&device_cmd_group, argc, argv);