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 to '%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
,
115 fprintf(stderr
, "ERROR: Unable to init '%s'\n", argv
[i
]);
122 strncpy_null(ioctl_args
.name
, argv
[i
]);
123 res
= ioctl(fdmnt
, BTRFS_IOC_ADD_DEV
, &ioctl_args
);
126 fprintf(stderr
, "ERROR: error adding the device '%s' - %s\n",
127 argv
[i
], strerror(e
));
133 close_file_or_dir(fdmnt
, dirstream
);
137 static const char * const cmd_rm_dev_usage
[] = {
138 "btrfs device delete <device> [<device>...] <path>",
139 "Remove a device from a filesystem",
143 static int cmd_rm_dev(int argc
, char **argv
)
146 int i
, fdmnt
, ret
=0, e
;
147 DIR *dirstream
= NULL
;
149 if (check_argc_min(argc
, 3))
150 usage(cmd_rm_dev_usage
);
152 mntpnt
= argv
[argc
- 1];
154 fdmnt
= open_file_or_dir(mntpnt
, &dirstream
);
156 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
160 for(i
=1 ; i
< argc
- 1; i
++ ){
161 struct btrfs_ioctl_vol_args arg
;
164 strncpy_null(arg
.name
, argv
[i
]);
165 res
= ioctl(fdmnt
, BTRFS_IOC_RM_DEV
, &arg
);
169 "ERROR: error removing the device '%s' - %s\n",
170 argv
[i
], btrfs_err_str(res
));
172 } else if (res
< 0) {
174 "ERROR: error removing the device '%s' - %s\n",
175 argv
[i
], strerror(e
));
180 close_file_or_dir(fdmnt
, dirstream
);
184 static const char * const cmd_scan_dev_usage
[] = {
185 "btrfs device scan [<--all-devices>|<device> [<device>...]]",
186 "Scan devices for a btrfs filesystem",
190 static int cmd_scan_dev(int argc
, char **argv
)
193 int where
= BTRFS_SCAN_LBLKID
;
196 if( argc
> 1 && !strcmp(argv
[1],"--all-devices")){
197 if (check_argc_max(argc
, 2))
198 usage(cmd_scan_dev_usage
);
200 where
= BTRFS_SCAN_DEV
;
206 printf("Scanning for Btrfs filesystems\n");
207 ret
= scan_for_btrfs(where
, BTRFS_UPDATE_KERNEL
);
209 fprintf(stderr
, "ERROR: error %d while scanning\n", ret
);
215 fd
= open("/dev/btrfs-control", O_RDWR
);
217 perror("failed to open /dev/btrfs-control");
221 for( i
= devstart
; i
< argc
; i
++ ){
222 struct btrfs_ioctl_vol_args args
;
225 printf("Scanning for Btrfs filesystems in '%s'\n", argv
[i
]);
227 strncpy_null(args
.name
, argv
[i
]);
229 * FIXME: which are the error code returned by this ioctl ?
230 * it seems that is impossible to understand if there no is
231 * a btrfs filesystem from an I/O error !!!
233 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
238 fprintf(stderr
, "ERROR: unable to scan the device '%s' - %s\n",
239 argv
[i
], strerror(e
));
248 static const char * const cmd_ready_dev_usage
[] = {
249 "btrfs device ready <device>",
250 "Check device to see if it has all of it's devices in cache for mounting",
254 static int cmd_ready_dev(int argc
, char **argv
)
256 struct btrfs_ioctl_vol_args args
;
260 if (check_argc_min(argc
, 2))
261 usage(cmd_ready_dev_usage
);
263 fd
= open("/dev/btrfs-control", O_RDWR
);
265 perror("failed to open /dev/btrfs-control");
269 strncpy(args
.name
, argv
[argc
- 1], BTRFS_PATH_NAME_MAX
);
270 ret
= ioctl(fd
, BTRFS_IOC_DEVICES_READY
, &args
);
272 fprintf(stderr
, "ERROR: unable to determine if the device '%s'"
273 " is ready for mounting - %s\n", argv
[argc
- 1],
282 static const char * const cmd_dev_stats_usage
[] = {
283 "btrfs device stats [-z] <path>|<device>",
284 "Show current device IO stats. -z to reset stats afterwards.",
288 static int cmd_dev_stats(int argc
, char **argv
)
291 struct btrfs_ioctl_fs_info_args fi_args
;
292 struct btrfs_ioctl_dev_info_args
*di_args
= NULL
;
299 DIR *dirstream
= NULL
;
302 while ((c
= getopt(argc
, argv
, "z")) != -1) {
305 flags
= BTRFS_DEV_STATS_RESET
;
309 fprintf(stderr
, "ERROR: device stat args invalid.\n"
310 " device stat [-z] <path>|<device>\n"
311 " -z to reset stats after reading.\n");
316 if (optind
+ 1 != argc
) {
317 fprintf(stderr
, "ERROR: device stat needs path|device as single"
322 dev_path
= argv
[optind
];
324 fdmnt
= open_path_or_dev_mnt(dev_path
, &dirstream
);
327 fprintf(stderr
, "ERROR: can't access '%s'\n", dev_path
);
331 ret
= get_fs_info(dev_path
, &fi_args
, &di_args
);
333 fprintf(stderr
, "ERROR: getting dev info for devstats failed: "
334 "%s\n", strerror(-ret
));
338 if (!fi_args
.num_devices
) {
339 fprintf(stderr
, "ERROR: no devices found\n");
344 for (i
= 0; i
< fi_args
.num_devices
; i
++) {
345 struct btrfs_ioctl_get_dev_stats args
= {0};
346 __u8 path
[BTRFS_DEVICE_PATH_NAME_MAX
+ 1];
348 strncpy((char *)path
, (char *)di_args
[i
].path
,
349 BTRFS_DEVICE_PATH_NAME_MAX
);
350 path
[BTRFS_DEVICE_PATH_NAME_MAX
] = '\0';
352 args
.devid
= di_args
[i
].devid
;
353 args
.nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
356 if (ioctl(fdmnt
, BTRFS_IOC_GET_DEV_STATS
, &args
) < 0) {
358 "ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
359 path
, strerror(errno
));
362 if (args
.nr_items
>= BTRFS_DEV_STAT_WRITE_ERRS
+ 1)
363 printf("[%s].write_io_errs %llu\n",
365 (unsigned long long) args
.values
[
366 BTRFS_DEV_STAT_WRITE_ERRS
]);
367 if (args
.nr_items
>= BTRFS_DEV_STAT_READ_ERRS
+ 1)
368 printf("[%s].read_io_errs %llu\n",
370 (unsigned long long) args
.values
[
371 BTRFS_DEV_STAT_READ_ERRS
]);
372 if (args
.nr_items
>= BTRFS_DEV_STAT_FLUSH_ERRS
+ 1)
373 printf("[%s].flush_io_errs %llu\n",
375 (unsigned long long) args
.values
[
376 BTRFS_DEV_STAT_FLUSH_ERRS
]);
377 if (args
.nr_items
>= BTRFS_DEV_STAT_CORRUPTION_ERRS
+ 1)
378 printf("[%s].corruption_errs %llu\n",
380 (unsigned long long) args
.values
[
381 BTRFS_DEV_STAT_CORRUPTION_ERRS
]);
382 if (args
.nr_items
>= BTRFS_DEV_STAT_GENERATION_ERRS
+ 1)
383 printf("[%s].generation_errs %llu\n",
385 (unsigned long long) args
.values
[
386 BTRFS_DEV_STAT_GENERATION_ERRS
]);
392 close_file_or_dir(fdmnt
, dirstream
);
397 const struct cmd_group device_cmd_group
= {
398 device_cmd_group_usage
, NULL
, {
399 { "add", cmd_add_dev
, cmd_add_dev_usage
, NULL
, 0 },
400 { "delete", cmd_rm_dev
, cmd_rm_dev_usage
, NULL
, 0 },
401 { "scan", cmd_scan_dev
, cmd_scan_dev_usage
, NULL
, 0 },
402 { "ready", cmd_ready_dev
, cmd_ready_dev_usage
, NULL
, 0 },
403 { "stats", cmd_dev_stats
, cmd_dev_stats_usage
, NULL
, 0 },
408 int cmd_device(int argc
, char **argv
)
410 return handle_command_group(&device_cmd_group
, argc
, argv
);