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;
98 res = test_dev_for_mkfs(argv[i], force, estr);
100 fprintf(stderr, "%s", estr);
105 devfd = open(argv[i], O_RDWR);
107 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
112 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
120 strncpy_null(ioctl_args.name, argv[i]);
121 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
124 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
125 argv[i], strerror(e));
132 close_file_or_dir(fdmnt, dirstream);
136 static const char * const cmd_rm_dev_usage[] = {
137 "btrfs device delete <device> [<device>...] <path>",
138 "Remove a device from a filesystem",
142 static int cmd_rm_dev(int argc, char **argv)
145 int i, fdmnt, ret=0, e;
146 DIR *dirstream = NULL;
148 if (check_argc_min(argc, 3))
149 usage(cmd_rm_dev_usage);
151 mntpnt = argv[argc - 1];
153 fdmnt = open_file_or_dir(mntpnt, &dirstream);
155 fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
159 for(i=1 ; i < argc - 1; i++ ){
160 struct btrfs_ioctl_vol_args arg;
163 if (!is_block_device(argv[i])) {
165 "ERROR: %s is not a block device\n", argv[i]);
169 strncpy_null(arg.name, argv[i]);
170 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
174 "ERROR: error removing the device '%s' - %s\n",
175 argv[i], btrfs_err_str(res));
177 } else if (res < 0) {
179 "ERROR: error removing the device '%s' - %s\n",
180 argv[i], strerror(e));
185 close_file_or_dir(fdmnt, dirstream);
189 static const char * const cmd_scan_dev_usage[] = {
190 "btrfs device scan [<--all-devices>|<device> [<device>...]]",
191 "Scan devices for a btrfs filesystem",
195 static int cmd_scan_dev(int argc, char **argv)
198 int where = BTRFS_SCAN_LBLKID;
201 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
202 if (check_argc_max(argc, 2))
203 usage(cmd_scan_dev_usage);
205 where = BTRFS_SCAN_DEV;
211 printf("Scanning for Btrfs filesystems\n");
212 ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
214 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
220 fd = open("/dev/btrfs-control", O_RDWR);
222 perror("failed to open /dev/btrfs-control");
226 for( i = devstart ; i < argc ; i++ ){
227 struct btrfs_ioctl_vol_args args;
230 if (!is_block_device(argv[i])) {
232 "ERROR: %s is not a block device\n", argv[i]);
236 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
238 strncpy_null(args.name, argv[i]);
240 * FIXME: which are the error code returned by this ioctl ?
241 * it seems that is impossible to understand if there no is
242 * a btrfs filesystem from an I/O error !!!
244 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
249 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
250 argv[i], strerror(e));
259 static const char * const cmd_ready_dev_usage[] = {
260 "btrfs device ready <device>",
261 "Check device to see if it has all of it's devices in cache for mounting",
265 static int cmd_ready_dev(int argc, char **argv)
267 struct btrfs_ioctl_vol_args args;
271 if (check_argc_min(argc, 2))
272 usage(cmd_ready_dev_usage);
274 fd = open("/dev/btrfs-control", O_RDWR);
276 perror("failed to open /dev/btrfs-control");
279 if (!is_block_device(argv[1])) {
281 "ERROR: %s is not a block device\n", argv[1]);
286 strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
287 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
289 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
290 " is ready for mounting - %s\n", argv[argc - 1],
299 static const char * const cmd_dev_stats_usage[] = {
300 "btrfs device stats [-z] <path>|<device>",
301 "Show current device IO stats. -z to reset stats afterwards.",
305 static int cmd_dev_stats(int argc, char **argv)
308 struct btrfs_ioctl_fs_info_args fi_args;
309 struct btrfs_ioctl_dev_info_args *di_args = NULL;
316 DIR *dirstream = NULL;
319 while ((c = getopt(argc, argv, "z")) != -1) {
322 flags = BTRFS_DEV_STATS_RESET;
326 usage(cmd_dev_stats_usage);
330 argc = argc - optind;
331 if (check_argc_exact(argc, 1))
332 usage(cmd_dev_stats_usage);
334 dev_path = argv[optind];
336 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
339 fprintf(stderr, "ERROR: can't access '%s'\n", dev_path);
343 ret = get_fs_info(dev_path, &fi_args, &di_args);
345 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
346 "%s\n", strerror(-ret));
350 if (!fi_args.num_devices) {
351 fprintf(stderr, "ERROR: no devices found\n");
356 for (i = 0; i < fi_args.num_devices; i++) {
357 struct btrfs_ioctl_get_dev_stats args = {0};
358 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
360 strncpy((char *)path, (char *)di_args[i].path,
361 BTRFS_DEVICE_PATH_NAME_MAX);
362 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
364 args.devid = di_args[i].devid;
365 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
368 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
370 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
371 path, strerror(errno));
374 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
375 printf("[%s].write_io_errs %llu\n",
377 (unsigned long long) args.values[
378 BTRFS_DEV_STAT_WRITE_ERRS]);
379 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
380 printf("[%s].read_io_errs %llu\n",
382 (unsigned long long) args.values[
383 BTRFS_DEV_STAT_READ_ERRS]);
384 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
385 printf("[%s].flush_io_errs %llu\n",
387 (unsigned long long) args.values[
388 BTRFS_DEV_STAT_FLUSH_ERRS]);
389 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
390 printf("[%s].corruption_errs %llu\n",
392 (unsigned long long) args.values[
393 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
394 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
395 printf("[%s].generation_errs %llu\n",
397 (unsigned long long) args.values[
398 BTRFS_DEV_STAT_GENERATION_ERRS]);
404 close_file_or_dir(fdmnt, dirstream);
409 const struct cmd_group device_cmd_group = {
410 device_cmd_group_usage, NULL, {
411 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
412 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
413 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
414 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
415 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
420 int cmd_device(int argc, char **argv)
422 return handle_command_group(&device_cmd_group, argc, argv);