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"
34 static const char * const device_cmd_group_usage[] = {
35 "btrfs device <command> [<args>]",
39 static const char * const cmd_add_dev_usage[] = {
40 "btrfs device add [options] <device> [<device>...] <path>",
41 "Add a device to a filesystem",
42 "-K|--nodiscard do not perform whole device TRIM",
43 "-f|--force force overwrite existing filesystem on the disk",
47 static int cmd_add_dev(int argc, char **argv)
50 int i, fdmnt, ret=0, e;
51 DIR *dirstream = NULL;
58 static struct option long_options[] = {
59 { "nodiscard", optional_argument, NULL, 'K'},
60 { "force", no_argument, NULL, 'f'},
63 int c = getopt_long(argc, argv, "Kf", long_options,
75 usage(cmd_add_dev_usage);
81 if (check_argc_min(argc, 2))
82 usage(cmd_add_dev_usage);
84 mntpnt = argv[optind + argc - 1];
86 fdmnt = open_file_or_dir(mntpnt, &dirstream);
88 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
92 for (i = optind; i < optind + argc - 1; i++){
93 struct btrfs_ioctl_vol_args ioctl_args;
95 u64 dev_block_count = 0;
99 res = test_dev_for_mkfs(argv[i], force, estr);
101 fprintf(stderr, "%s", estr);
106 devfd = open(argv[i], O_RDWR);
108 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
113 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
121 path = canonicalize_path(argv[i]);
124 "ERROR: Could not canonicalize pathname '%s': %s\n",
125 argv[i], strerror(errno));
130 strncpy_null(ioctl_args.name, path);
131 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
134 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
142 close_file_or_dir(fdmnt, dirstream);
146 static const char * const cmd_rm_dev_usage[] = {
147 "btrfs device delete <device> [<device>...] <path>",
148 "Remove a device from a filesystem",
152 static int cmd_rm_dev(int argc, char **argv)
155 int i, fdmnt, ret=0, e;
156 DIR *dirstream = NULL;
158 if (check_argc_min(argc, 3))
159 usage(cmd_rm_dev_usage);
161 mntpnt = argv[argc - 1];
163 fdmnt = open_file_or_dir(mntpnt, &dirstream);
165 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
169 for(i=1 ; i < argc - 1; i++ ){
170 struct btrfs_ioctl_vol_args arg;
173 if (!is_block_device(argv[i])) {
175 "ERROR: %s is not a block device\n", argv[i]);
179 strncpy_null(arg.name, argv[i]);
180 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
184 "ERROR: error removing the device '%s' - %s\n",
185 argv[i], btrfs_err_str(res));
187 } else if (res < 0) {
189 "ERROR: error removing the device '%s' - %s\n",
190 argv[i], strerror(e));
195 close_file_or_dir(fdmnt, dirstream);
199 static const char * const cmd_scan_dev_usage[] = {
200 "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
201 "Scan devices for a btrfs filesystem",
205 static int cmd_scan_dev(int argc, char **argv)
208 int where = BTRFS_SCAN_LBLKID;
216 static struct option long_options[] = {
217 { "all-devices", no_argument, NULL, 'd'},
220 int c = getopt_long(argc, argv, "d", long_options,
226 where = BTRFS_SCAN_DEV;
230 usage(cmd_scan_dev_usage);
234 if (all && check_argc_max(argc, 2))
235 usage(cmd_scan_dev_usage);
237 if (all || argc == 1) {
238 printf("Scanning for Btrfs filesystems\n");
239 ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
241 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
245 fd = open("/dev/btrfs-control", O_RDWR);
247 perror("failed to open /dev/btrfs-control");
252 for( i = devstart ; i < argc ; i++ ){
253 struct btrfs_ioctl_vol_args args;
256 if (!is_block_device(argv[i])) {
258 "ERROR: %s is not a block device\n", argv[i]);
262 path = canonicalize_path(argv[i]);
265 "ERROR: Could not canonicalize path '%s': %s\n",
266 argv[i], strerror(errno));
270 printf("Scanning for Btrfs filesystems in '%s'\n", path);
272 strncpy_null(args.name, path);
274 * FIXME: which are the error code returned by this ioctl ?
275 * it seems that is impossible to understand if there no is
276 * a btrfs filesystem from an I/O error !!!
278 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
282 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
296 static const char * const cmd_ready_dev_usage[] = {
297 "btrfs device ready <device>",
298 "Check device to see if it has all of it's devices in cache for mounting",
302 static int cmd_ready_dev(int argc, char **argv)
304 struct btrfs_ioctl_vol_args args;
309 if (check_argc_min(argc, 2))
310 usage(cmd_ready_dev_usage);
312 fd = open("/dev/btrfs-control", O_RDWR);
314 perror("failed to open /dev/btrfs-control");
318 path = canonicalize_path(argv[argc - 1]);
321 "ERROR: Could not canonicalize pathname '%s': %s\n",
322 argv[argc - 1], strerror(errno));
327 if (!is_block_device(path)) {
329 "ERROR: %s is not a block device\n", path);
334 strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
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_dev_stats_usage[] = {
350 "btrfs device stats [-z] <path>|<device>",
351 "Show current device IO stats. -z to reset stats afterwards.",
355 static int cmd_dev_stats(int argc, char **argv)
358 struct btrfs_ioctl_fs_info_args fi_args;
359 struct btrfs_ioctl_dev_info_args *di_args = NULL;
366 DIR *dirstream = NULL;
369 while ((c = getopt(argc, argv, "z")) != -1) {
372 flags = BTRFS_DEV_STATS_RESET;
376 usage(cmd_dev_stats_usage);
380 argc = argc - optind;
381 if (check_argc_exact(argc, 1))
382 usage(cmd_dev_stats_usage);
384 dev_path = argv[optind];
386 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
391 "ERROR: '%s' is not a mounted btrfs device\n",
394 fprintf(stderr, "ERROR: can't access '%s': %s\n",
395 dev_path, strerror(errno));
399 ret = get_fs_info(dev_path, &fi_args, &di_args);
401 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
402 "%s\n", strerror(-ret));
406 if (!fi_args.num_devices) {
407 fprintf(stderr, "ERROR: no devices found\n");
412 for (i = 0; i < fi_args.num_devices; i++) {
413 struct btrfs_ioctl_get_dev_stats args = {0};
414 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
416 strncpy((char *)path, (char *)di_args[i].path,
417 BTRFS_DEVICE_PATH_NAME_MAX);
418 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
420 args.devid = di_args[i].devid;
421 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
424 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
426 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
427 path, strerror(errno));
430 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
431 printf("[%s].write_io_errs %llu\n",
433 (unsigned long long) args.values[
434 BTRFS_DEV_STAT_WRITE_ERRS]);
435 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
436 printf("[%s].read_io_errs %llu\n",
438 (unsigned long long) args.values[
439 BTRFS_DEV_STAT_READ_ERRS]);
440 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
441 printf("[%s].flush_io_errs %llu\n",
443 (unsigned long long) args.values[
444 BTRFS_DEV_STAT_FLUSH_ERRS]);
445 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
446 printf("[%s].corruption_errs %llu\n",
448 (unsigned long long) args.values[
449 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
450 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
451 printf("[%s].generation_errs %llu\n",
453 (unsigned long long) args.values[
454 BTRFS_DEV_STAT_GENERATION_ERRS]);
460 close_file_or_dir(fdmnt, dirstream);
465 const struct cmd_group device_cmd_group = {
466 device_cmd_group_usage, NULL, {
467 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
468 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
469 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
470 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
471 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
476 int cmd_device(int argc, char **argv)
478 return handle_command_group(&device_cmd_group, argc, argv);