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 /* FIXME - imported cruft, fix sparse errors and warnings */
35 #define BLKGETSIZE64 0
36 #define BTRFS_IOC_SNAP_CREATE_V2 0
37 #define BTRFS_VOL_NAME_MAX 255
38 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
39 static inline int ioctl(int fd, int define, void *arg) { return 0; }
42 static const char * const device_cmd_group_usage[] = {
43 "btrfs device <command> [<args>]",
47 static const char * const cmd_add_dev_usage[] = {
48 "btrfs device add <device> [<device>...] <path>",
49 "Add a device to a filesystem",
53 static int cmd_add_dev(int argc, char **argv)
56 int i, fdmnt, ret=0, e;
57 DIR *dirstream = NULL;
59 if (check_argc_min(argc, 3))
60 usage(cmd_add_dev_usage);
62 mntpnt = argv[argc - 1];
64 fdmnt = open_file_or_dir(mntpnt, &dirstream);
66 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
70 for (i = 1; i < argc - 1; i++ ){
71 struct btrfs_ioctl_vol_args ioctl_args;
73 u64 dev_block_count = 0;
77 res = check_mounted(argv[i]);
79 fprintf(stderr, "error checking %s mount status\n",
85 fprintf(stderr, "%s is mounted\n", argv[i]);
90 devfd = open(argv[i], O_RDWR);
92 fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
96 res = fstat(devfd, &st);
98 fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
103 if (!S_ISBLK(st.st_mode)) {
104 fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
110 res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
113 fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
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));
131 close_file_or_dir(fdmnt, dirstream);
138 static const char * const cmd_rm_dev_usage[] = {
139 "btrfs device delete <device> [<device>...] <path>",
140 "Remove a device from a filesystem",
144 static int cmd_rm_dev(int argc, char **argv)
147 int i, fdmnt, ret=0, e;
148 DIR *dirstream = NULL;
150 if (check_argc_min(argc, 3))
151 usage(cmd_rm_dev_usage);
153 mntpnt = argv[argc - 1];
155 fdmnt = open_file_or_dir(mntpnt, &dirstream);
157 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
161 for(i=1 ; i < argc - 1; i++ ){
162 struct btrfs_ioctl_vol_args arg;
165 strncpy_null(arg.name, argv[i]);
166 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
170 "ERROR: error removing the device '%s' - %s\n",
171 argv[i], btrfs_err_str(res));
173 } else if (res < 0) {
175 "ERROR: error removing the device '%s' - %s\n",
176 argv[i], strerror(e));
181 close_file_or_dir(fdmnt, dirstream);
188 static const char * const cmd_scan_dev_usage[] = {
189 "btrfs device scan [<--all-devices>|<device> [<device>...]]",
190 "Scan devices for a btrfs filesystem",
194 static int cmd_scan_dev(int argc, char **argv)
197 int where = BTRFS_SCAN_PROC;
200 if( argc > 1 && !strcmp(argv[1],"--all-devices")){
201 if (check_argc_max(argc, 2))
202 usage(cmd_scan_dev_usage);
204 where = BTRFS_SCAN_DEV;
210 printf("Scanning for Btrfs filesystems\n");
211 ret = scan_for_btrfs(where, 1);
213 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
219 fd = open("/dev/btrfs-control", O_RDWR);
221 perror("failed to open /dev/btrfs-control");
225 for( i = devstart ; i < argc ; i++ ){
226 struct btrfs_ioctl_vol_args args;
229 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
231 strncpy_null(args.name, argv[i]);
233 * FIXME: which are the error code returned by this ioctl ?
234 * it seems that is impossible to understand if there no is
235 * a btrfs filesystem from an I/O error !!!
237 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
242 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
243 argv[i], strerror(e));
252 static const char * const cmd_ready_dev_usage[] = {
253 "btrfs device ready <device>",
254 "Check device to see if it has all of it's devices in cache for mounting",
258 static int cmd_ready_dev(int argc, char **argv)
260 struct btrfs_ioctl_vol_args args;
264 if (check_argc_min(argc, 2))
265 usage(cmd_ready_dev_usage);
267 fd = open("/dev/btrfs-control", O_RDWR);
269 perror("failed to open /dev/btrfs-control");
273 strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
274 ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
276 fprintf(stderr, "ERROR: unable to determine if the device '%s'"
277 " is ready for mounting - %s\n", argv[argc - 1],
286 static const char * const cmd_dev_stats_usage[] = {
287 "btrfs device stats [-z] <path>|<device>",
288 "Show current device IO stats. -z to reset stats afterwards.",
292 static int cmd_dev_stats(int argc, char **argv)
295 struct btrfs_ioctl_fs_info_args fi_args;
296 struct btrfs_ioctl_dev_info_args *di_args = NULL;
303 DIR *dirstream = NULL;
306 while ((c = getopt(argc, argv, "z")) != -1) {
309 flags = BTRFS_DEV_STATS_RESET;
313 fprintf(stderr, "ERROR: device stat args invalid.\n"
314 " device stat [-z] <path>|<device>\n"
315 " -z to reset stats after reading.\n");
320 if (optind + 1 != argc) {
321 fprintf(stderr, "ERROR: device stat needs path|device as single"
328 fdmnt = open_path_or_dev_mnt(path, &dirstream);
331 fprintf(stderr, "ERROR: can't access '%s'\n", path);
335 ret = get_fs_info(path, &fi_args, &di_args);
337 fprintf(stderr, "ERROR: getting dev info for devstats failed: "
338 "%s\n", strerror(-ret));
342 if (!fi_args.num_devices) {
343 fprintf(stderr, "ERROR: no devices found\n");
348 for (i = 0; i < fi_args.num_devices; i++) {
349 struct btrfs_ioctl_get_dev_stats args = {0};
350 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
352 strncpy((char *)path, (char *)di_args[i].path,
353 BTRFS_DEVICE_PATH_NAME_MAX);
354 path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
356 args.devid = di_args[i].devid;
357 args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
360 if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
362 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
363 path, strerror(errno));
366 if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
367 printf("[%s].write_io_errs %llu\n",
369 (unsigned long long) args.values[
370 BTRFS_DEV_STAT_WRITE_ERRS]);
371 if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
372 printf("[%s].read_io_errs %llu\n",
374 (unsigned long long) args.values[
375 BTRFS_DEV_STAT_READ_ERRS]);
376 if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
377 printf("[%s].flush_io_errs %llu\n",
379 (unsigned long long) args.values[
380 BTRFS_DEV_STAT_FLUSH_ERRS]);
381 if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
382 printf("[%s].corruption_errs %llu\n",
384 (unsigned long long) args.values[
385 BTRFS_DEV_STAT_CORRUPTION_ERRS]);
386 if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
387 printf("[%s].generation_errs %llu\n",
389 (unsigned long long) args.values[
390 BTRFS_DEV_STAT_GENERATION_ERRS]);
396 close_file_or_dir(fdmnt, dirstream);
401 const struct cmd_group device_cmd_group = {
402 device_cmd_group_usage, NULL, {
403 { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
404 { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
405 { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
406 { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
407 { "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
412 int cmd_device(int argc, char **argv)
414 return handle_command_group(&device_cmd_group, argc, argv);