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",
46 static int cmd_add_dev(int argc, char **argv)
49 int i, fdmnt, ret=0, e;
50 DIR *dirstream = NULL;
55 static struct option long_options[] = {
56 { "nodiscard", optional_argument, NULL, 'K'},
59 int c = getopt_long(argc, argv, "K", long_options,
68 usage(cmd_add_dev_usage);
74 if (check_argc_min(argc, 2))
75 usage(cmd_add_dev_usage);
77 mntpnt = argv[optind + argc - 1];
79 fdmnt = open_file_or_dir(mntpnt, &dirstream);
81 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
85 for (i = optind; i < optind + argc - 1; i++){
86 struct btrfs_ioctl_vol_args ioctl_args;
88 u64 dev_block_count = 0;
92 res = check_mounted(argv[i]);
94 fprintf(stderr, "error checking %s mount status\n",
100 fprintf(stderr, "%s is mounted\n", argv[i]);
105 devfd = open(argv[i], O_RDWR);
107 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
111 res = fstat(devfd, &st);
113 fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
118 if (!S_ISBLK(st.st_mode)) {
119 fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
125 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
128 fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
135 strncpy_null(ioctl_args.name, argv[i]);
136 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
139 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
140 argv[i], strerror(e));
146 close_file_or_dir(fdmnt, dirstream);
150 static const char * const cmd_rm_dev_usage[] = {
151 "btrfs device delete <device> [<device>...] <path>",
152 "Remove a device from a filesystem",
156 static int cmd_rm_dev(int argc, char **argv)
159 int i, fdmnt, ret=0, e;
160 DIR *dirstream = NULL;
162 if (check_argc_min(argc, 3))
163 usage(cmd_rm_dev_usage);
165 mntpnt = argv[argc - 1];
167 fdmnt = open_file_or_dir(mntpnt, &dirstream);
169 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
173 for(i=1 ; i < argc - 1; i++ ){
174 struct btrfs_ioctl_vol_args arg;
177 strncpy_null(arg.name, argv[i]);
178 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
182 "ERROR: error removing the device '%s' - %s\n",
183 argv[i], btrfs_err_str(res));
185 } else if (res < 0) {
187 "ERROR: error removing the device '%s' - %s\n",
188 argv[i], strerror(e));
193 close_file_or_dir(fdmnt, dirstream);
197 static const char * const cmd_scan_dev_usage[] = {
198 "btrfs device scan [<--all-devices>|<device> [<device>...]]",
199 "Scan devices for a btrfs filesystem",
203 static int cmd_scan_dev(int argc, char **argv)
206 int where = BTRFS_SCAN_PROC;
209 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
210 if (check_argc_max(argc, 2))
211 usage(cmd_scan_dev_usage);
213 where = BTRFS_SCAN_DEV;
219 printf("Scanning for Btrfs filesystems\n");
220 ret = scan_for_btrfs(where, 1);
222 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
228 fd = open("/dev/btrfs-control", O_RDWR);
230 perror("failed to open /dev/btrfs-control");
234 for( i = devstart ; i < argc ; i++ ){
235 struct btrfs_ioctl_vol_args args;
238 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
240 strncpy_null(args.name, argv[i]);
242 * FIXME: which are the error code returned by this ioctl ?
243 * it seems that is impossible to understand if there no is
244 * a btrfs filesystem from an I/O error !!!
246 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
251 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
252 argv[i], strerror(e));
261 static const char * const cmd_ready_dev_usage[] = {
262 "btrfs device ready <device>",
263 "Check device to see if it has all of it's devices in cache for mounting",
267 static int cmd_ready_dev(int argc, char **argv)
269 struct btrfs_ioctl_vol_args args;
273 if (check_argc_min(argc, 2))
274 usage(cmd_ready_dev_usage);
276 fd = open("/dev/btrfs-control", O_RDWR);
278 perror("failed to open /dev/btrfs-control");
282 strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
283 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
285 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
286 " is ready for mounting - %s\n", argv[argc - 1],
295 static const char * const cmd_dev_stats_usage[] = {
296 "btrfs device stats [-z] <path>|<device>",
297 "Show current device IO stats. -z to reset stats afterwards.",
301 static int cmd_dev_stats(int argc, char **argv)
304 struct btrfs_ioctl_fs_info_args fi_args;
305 struct btrfs_ioctl_dev_info_args *di_args = NULL;
312 DIR *dirstream = NULL;
315 while ((c = getopt(argc, argv, "z")) != -1) {
318 flags = BTRFS_DEV_STATS_RESET;
322 fprintf(stderr, "ERROR: device stat args invalid.\n"
323 " device stat [-z] <path>|<device>\n"
324 " -z to reset stats after reading.\n");
329 if (optind + 1 != argc) {
330 fprintf(stderr, "ERROR: device stat needs path|device as single"
335 dev_path = argv[optind];
337 fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
340 fprintf(stderr, "ERROR: can't access '%s'\n", dev_path);
344 ret = get_fs_info(dev_path, &fi_args, &di_args);
346 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
347 "%s\n", strerror(-ret));
351 if (!fi_args.num_devices) {
352 fprintf(stderr, "ERROR: no devices found\n");
357 for (i = 0; i < fi_args.num_devices; i++) {
358 struct btrfs_ioctl_get_dev_stats args = {0};
359 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
361 strncpy((char *)path, (char *)di_args[i].path,
362 BTRFS_DEVICE_PATH_NAME_MAX);
363 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
365 args.devid = di_args[i].devid;
366 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
369 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
371 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
372 path, strerror(errno));
375 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
376 printf("[%s].write_io_errs %llu\n",
378 (unsigned long long) args.values[
379 BTRFS_DEV_STAT_WRITE_ERRS]);
380 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
381 printf("[%s].read_io_errs %llu\n",
383 (unsigned long long) args.values[
384 BTRFS_DEV_STAT_READ_ERRS]);
385 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
386 printf("[%s].flush_io_errs %llu\n",
388 (unsigned long long) args.values[
389 BTRFS_DEV_STAT_FLUSH_ERRS]);
390 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
391 printf("[%s].corruption_errs %llu\n",
393 (unsigned long long) args.values[
394 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
395 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
396 printf("[%s].generation_errs %llu\n",
398 (unsigned long long) args.values[
399 BTRFS_DEV_STAT_GENERATION_ERRS]);
405 close_file_or_dir(fdmnt, dirstream);
410 const struct cmd_group device_cmd_group = {
411 device_cmd_group_usage, NULL, {
412 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
413 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
414 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
415 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
416 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
421 int cmd_device(int argc, char **argv)
423 return handle_command_group(&device_cmd_group, argc, argv);