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>
26 #include "kerncompat.h"
33 static const char * const device_cmd_group_usage[] = {
34 "btrfs device <command> [<args>]",
38 static const char * const cmd_add_dev_usage[] = {
39 "btrfs device add <device> [<device>...] <path>",
40 "Add a device to a filesystem",
44 static int cmd_add_dev(int argc, char **argv)
47 int i, fdmnt, ret=0, e;
48 DIR *dirstream = NULL;
50 if (check_argc_min(argc, 3))
51 usage(cmd_add_dev_usage);
53 mntpnt = argv[argc - 1];
55 fdmnt = open_file_or_dir(mntpnt, &dirstream);
57 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
61 for (i = 1; i < argc - 1; i++ ){
62 struct btrfs_ioctl_vol_args ioctl_args;
64 u64 dev_block_count = 0;
68 res = check_mounted(argv[i]);
70 fprintf(stderr, "error checking %s mount status\n",
76 fprintf(stderr, "%s is mounted\n", argv[i]);
81 devfd = open(argv[i], O_RDWR);
83 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
87 res = fstat(devfd, &st);
89 fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
94 if (!S_ISBLK(st.st_mode)) {
95 fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
101 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
104 fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
111 strncpy_null(ioctl_args.name, argv[i]);
112 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
115 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
116 argv[i], strerror(e));
122 close_file_or_dir(fdmnt, dirstream);
129 static const char * const cmd_rm_dev_usage[] = {
130 "btrfs device delete <device> [<device>...] <path>",
131 "Remove a device from a filesystem",
135 static int cmd_rm_dev(int argc, char **argv)
138 int i, fdmnt, ret=0, e;
139 DIR *dirstream = NULL;
141 if (check_argc_min(argc, 3))
142 usage(cmd_rm_dev_usage);
144 mntpnt = argv[argc - 1];
146 fdmnt = open_file_or_dir(mntpnt, &dirstream);
148 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
152 for(i=1 ; i < argc - 1; i++ ){
153 struct btrfs_ioctl_vol_args arg;
156 strncpy_null(arg.name, argv[i]);
157 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
161 "ERROR: error removing the device '%s' - %s\n",
162 argv[i], btrfs_err_str(res));
164 } else if (res < 0) {
166 "ERROR: error removing the device '%s' - %s\n",
167 argv[i], strerror(e));
172 close_file_or_dir(fdmnt, dirstream);
179 static const char * const cmd_scan_dev_usage[] = {
180 "btrfs device scan [<--all-devices>|<device> [<device>...]]",
181 "Scan devices for a btrfs filesystem",
185 static int cmd_scan_dev(int argc, char **argv)
188 int where = BTRFS_SCAN_PROC;
191 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
192 if (check_argc_max(argc, 2))
193 usage(cmd_scan_dev_usage);
195 where = BTRFS_SCAN_DEV;
201 printf("Scanning for Btrfs filesystems\n");
202 ret = scan_for_btrfs(where, 1);
204 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
210 fd = open("/dev/btrfs-control", O_RDWR);
212 perror("failed to open /dev/btrfs-control");
216 for( i = devstart ; i < argc ; i++ ){
217 struct btrfs_ioctl_vol_args args;
220 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
222 strncpy_null(args.name, argv[i]);
224 * FIXME: which are the error code returned by this ioctl ?
225 * it seems that is impossible to understand if there no is
226 * a btrfs filesystem from an I/O error !!!
228 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
233 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
234 argv[i], strerror(e));
243 static const char * const cmd_ready_dev_usage[] = {
244 "btrfs device ready <device>",
245 "Check device to see if it has all of it's devices in cache for mounting",
249 static int cmd_ready_dev(int argc, char **argv)
251 struct btrfs_ioctl_vol_args args;
255 if (check_argc_min(argc, 2))
256 usage(cmd_ready_dev_usage);
258 fd = open("/dev/btrfs-control", O_RDWR);
260 perror("failed to open /dev/btrfs-control");
264 strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
265 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
267 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
268 " is ready for mounting - %s\n", argv[argc - 1],
277 static const char * const cmd_dev_stats_usage[] = {
278 "btrfs device stats [-z] <path>|<device>",
279 "Show current device IO stats. -z to reset stats afterwards.",
283 static int cmd_dev_stats(int argc, char **argv)
286 struct btrfs_ioctl_fs_info_args fi_args;
287 struct btrfs_ioctl_dev_info_args *di_args = NULL;
294 DIR *dirstream = NULL;
297 while ((c = getopt(argc, argv, "z")) != -1) {
300 flags = BTRFS_DEV_STATS_RESET;
304 fprintf(stderr, "ERROR: device stat args invalid.\n"
305 " device stat [-z] <path>|<device>\n"
306 " -z to reset stats after reading.\n");
311 if (optind + 1 != argc) {
312 fprintf(stderr, "ERROR: device stat needs path|device as single"
317 dev_path = argv[optind];
319 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
322 fprintf(stderr, "ERROR: can't access '%s'\n", dev_path);
326 ret = get_fs_info(dev_path, &fi_args, &di_args);
328 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
329 "%s\n", strerror(-ret));
333 if (!fi_args.num_devices) {
334 fprintf(stderr, "ERROR: no devices found\n");
339 for (i = 0; i < fi_args.num_devices; i++) {
340 struct btrfs_ioctl_get_dev_stats args = {0};
341 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
343 strncpy((char *)path, (char *)di_args[i].path,
344 BTRFS_DEVICE_PATH_NAME_MAX);
345 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
347 args.devid = di_args[i].devid;
348 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
351 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
353 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
354 path, strerror(errno));
357 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
358 printf("[%s].write_io_errs %llu\n",
360 (unsigned long long) args.values[
361 BTRFS_DEV_STAT_WRITE_ERRS]);
362 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
363 printf("[%s].read_io_errs %llu\n",
365 (unsigned long long) args.values[
366 BTRFS_DEV_STAT_READ_ERRS]);
367 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
368 printf("[%s].flush_io_errs %llu\n",
370 (unsigned long long) args.values[
371 BTRFS_DEV_STAT_FLUSH_ERRS]);
372 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
373 printf("[%s].corruption_errs %llu\n",
375 (unsigned long long) args.values[
376 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
377 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
378 printf("[%s].generation_errs %llu\n",
380 (unsigned long long) args.values[
381 BTRFS_DEV_STAT_GENERATION_ERRS]);
387 close_file_or_dir(fdmnt, dirstream);
392 const struct cmd_group device_cmd_group = {
393 device_cmd_group_usage, NULL, {
394 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
395 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
396 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
397 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
398 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
403 int cmd_device(int argc, char **argv)
405 return handle_command_group(&device_cmd_group, argc, argv);