2012-02-03 19:00:17 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2013-09-17 14:54:01 +00:00
|
|
|
#include <getopt.h>
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
#include "kerncompat.h"
|
|
|
|
#include "ctree.h"
|
|
|
|
#include "ioctl.h"
|
|
|
|
#include "utils.h"
|
2015-04-17 16:29:38 +00:00
|
|
|
#include "cmds-fi-usage.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
#include "commands.h"
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-08 15:45:54 +00:00
|
|
|
static const char * const device_cmd_group_usage[] = {
|
|
|
|
"btrfs device <command> [<args>]",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
|
|
|
static const char * const cmd_add_dev_usage[] = {
|
2013-09-17 14:54:01 +00:00
|
|
|
"btrfs device add [options] <device> [<device>...] <path>",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Add a device to a filesystem",
|
2013-09-17 14:54:01 +00:00
|
|
|
"-K|--nodiscard do not perform whole device TRIM",
|
2013-09-27 17:30:05 +00:00
|
|
|
"-f|--force force overwrite existing filesystem on the disk",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static int cmd_add_dev(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *mntpnt;
|
2012-02-03 19:00:17 +00:00
|
|
|
int i, fdmnt, ret=0, e;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2013-09-17 14:54:01 +00:00
|
|
|
int discard = 1;
|
2013-09-27 17:30:05 +00:00
|
|
|
int force = 0;
|
2013-09-17 14:54:01 +00:00
|
|
|
|
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option long_options[] = {
|
2013-09-17 14:54:01 +00:00
|
|
|
{ "nodiscard", optional_argument, NULL, 'K'},
|
2013-09-27 17:30:05 +00:00
|
|
|
{ "force", no_argument, NULL, 'f'},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
2013-09-17 14:54:01 +00:00
|
|
|
};
|
2015-04-08 15:33:55 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "Kf", long_options, NULL);
|
2013-09-17 14:54:01 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'K':
|
|
|
|
discard = 0;
|
|
|
|
break;
|
2013-09-27 17:30:05 +00:00
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
2013-09-17 14:54:01 +00:00
|
|
|
default:
|
|
|
|
usage(cmd_add_dev_usage);
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-09-17 14:54:01 +00:00
|
|
|
argc = argc - optind;
|
|
|
|
|
|
|
|
if (check_argc_min(argc, 2))
|
2012-02-03 19:00:17 +00:00
|
|
|
usage(cmd_add_dev_usage);
|
|
|
|
|
2013-09-17 14:54:01 +00:00
|
|
|
mntpnt = argv[optind + argc - 1];
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-07-15 11:36:50 +00:00
|
|
|
fdmnt = open_file_or_dir(mntpnt, &dirstream);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (fdmnt < 0) {
|
2014-01-14 21:44:03 +00:00
|
|
|
fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 14:54:01 +00:00
|
|
|
for (i = optind; i < optind + argc - 1; i++){
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_ioctl_vol_args ioctl_args;
|
|
|
|
int devfd, res;
|
|
|
|
u64 dev_block_count = 0;
|
|
|
|
int mixed = 0;
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2015-06-10 22:46:30 +00:00
|
|
|
res = test_dev_for_mkfs(argv[i], force);
|
2013-09-27 17:30:05 +00:00
|
|
|
if (res) {
|
2013-10-21 13:58:08 +00:00
|
|
|
ret++;
|
2012-02-03 19:00:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
devfd = open(argv[i], O_RDWR);
|
2013-01-22 21:48:04 +00:00
|
|
|
if (devfd < 0) {
|
2012-02-03 19:00:17 +00:00
|
|
|
fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-27 12:37:55 +00:00
|
|
|
res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
|
2013-09-17 14:54:01 +00:00
|
|
|
0, &mixed, discard);
|
2013-12-18 04:07:55 +00:00
|
|
|
close(devfd);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (res) {
|
|
|
|
ret++;
|
2013-12-18 04:07:55 +00:00
|
|
|
goto error_out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 20:43:11 +00:00
|
|
|
path = canonicalize_path(argv[i]);
|
|
|
|
if (!path) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Could not canonicalize pathname '%s': %s\n",
|
|
|
|
argv[i], strerror(errno));
|
|
|
|
ret++;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&ioctl_args, 0, sizeof(ioctl_args));
|
2014-06-04 20:43:11 +00:00
|
|
|
strncpy_null(ioctl_args.name, path);
|
2012-02-03 19:00:17 +00:00
|
|
|
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
|
|
|
|
e = errno;
|
2014-06-04 20:43:11 +00:00
|
|
|
if (res < 0) {
|
2012-02-03 19:00:17 +00:00
|
|
|
fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
|
2014-06-04 20:43:11 +00:00
|
|
|
path, strerror(e));
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 04:07:55 +00:00
|
|
|
error_out:
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2013-09-04 15:22:22 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static const char * const cmd_rm_dev_usage[] = {
|
|
|
|
"btrfs device delete <device> [<device>...] <path>",
|
|
|
|
"Remove a device from a filesystem",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static int cmd_rm_dev(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *mntpnt;
|
2012-02-03 19:00:17 +00:00
|
|
|
int i, fdmnt, ret=0, e;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
if (check_argc_min(argc, 3))
|
|
|
|
usage(cmd_rm_dev_usage);
|
|
|
|
|
|
|
|
mntpnt = argv[argc - 1];
|
|
|
|
|
2013-07-15 11:36:50 +00:00
|
|
|
fdmnt = open_file_or_dir(mntpnt, &dirstream);
|
2012-02-03 19:00:17 +00:00
|
|
|
if (fdmnt < 0) {
|
2014-01-14 21:44:03 +00:00
|
|
|
fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
for(i=1 ; i < argc - 1; i++ ){
|
2012-02-03 19:00:17 +00:00
|
|
|
struct btrfs_ioctl_vol_args arg;
|
|
|
|
int res;
|
|
|
|
|
2013-12-12 07:47:00 +00:00
|
|
|
if (!is_block_device(argv[i])) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: %s is not a block device\n", argv[i]);
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&arg, 0, sizeof(arg));
|
2013-01-25 19:27:47 +00:00
|
|
|
strncpy_null(arg.name, argv[i]);
|
2012-02-03 19:00:17 +00:00
|
|
|
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
|
|
|
|
e = errno;
|
2015-04-13 12:37:01 +00:00
|
|
|
if (res) {
|
|
|
|
const char *msg;
|
|
|
|
|
2015-05-11 13:55:52 +00:00
|
|
|
if (res > 0)
|
2015-04-13 12:37:01 +00:00
|
|
|
msg = btrfs_err_str(res);
|
|
|
|
else
|
|
|
|
msg = strerror(e);
|
2013-07-15 05:30:53 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: error removing the device '%s' - %s\n",
|
2015-04-13 12:37:01 +00:00
|
|
|
argv[i], msg);
|
2012-02-03 19:00:17 +00:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2013-09-04 15:22:22 +00:00
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
static const char * const cmd_scan_dev_usage[] = {
|
2014-03-19 06:10:02 +00:00
|
|
|
"btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
|
2012-02-03 19:00:17 +00:00
|
|
|
"Scan devices for a btrfs filesystem",
|
2014-09-13 01:21:21 +00:00
|
|
|
" -d|--all-devices (deprecated)",
|
2012-02-03 19:00:17 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_scan_dev(int argc, char **argv)
|
2012-02-03 19:00:17 +00:00
|
|
|
{
|
2014-10-15 00:50:38 +00:00
|
|
|
int i;
|
2014-03-06 03:36:48 +00:00
|
|
|
int devstart = 1;
|
|
|
|
int all = 0;
|
|
|
|
int ret = 0;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2014-03-06 03:36:48 +00:00
|
|
|
optind = 1;
|
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2015-01-19 12:44:49 +00:00
|
|
|
static const struct option long_options[] = {
|
2014-03-06 03:36:48 +00:00
|
|
|
{ "all-devices", no_argument, NULL, 'd'},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
2014-03-06 03:36:48 +00:00
|
|
|
};
|
2015-04-08 15:33:55 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "d", long_options, NULL);
|
2014-03-06 03:36:48 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-03 19:00:17 +00:00
|
|
|
usage(cmd_scan_dev_usage);
|
2014-03-06 03:36:48 +00:00
|
|
|
}
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:36:48 +00:00
|
|
|
if (all && check_argc_max(argc, 2))
|
|
|
|
usage(cmd_scan_dev_usage);
|
|
|
|
|
|
|
|
if (all || argc == 1) {
|
2012-02-03 19:00:17 +00:00
|
|
|
printf("Scanning for Btrfs filesystems\n");
|
2014-10-31 04:11:20 +00:00
|
|
|
ret = btrfs_scan_lblkid();
|
2014-03-06 03:36:48 +00:00
|
|
|
if (ret)
|
2012-02-03 19:00:17 +00:00
|
|
|
fprintf(stderr, "ERROR: error %d while scanning\n", ret);
|
2014-10-31 04:11:20 +00:00
|
|
|
ret = btrfs_register_all_devices();
|
|
|
|
if (ret)
|
|
|
|
fprintf(stderr, "ERROR: error %d while registering\n", ret);
|
2014-03-06 03:36:48 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = devstart ; i < argc ; i++ ){
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2012-02-03 19:00:17 +00:00
|
|
|
|
2013-12-12 07:47:00 +00:00
|
|
|
if (!is_block_device(argv[i])) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: %s is not a block device\n", argv[i]);
|
2014-03-06 03:36:48 +00:00
|
|
|
ret = 1;
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2013-12-12 07:47:00 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
path = canonicalize_path(argv[i]);
|
|
|
|
if (!path) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Could not canonicalize path '%s': %s\n",
|
|
|
|
argv[i], strerror(errno));
|
|
|
|
ret = 1;
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2014-06-04 20:43:11 +00:00
|
|
|
}
|
|
|
|
printf("Scanning for Btrfs filesystems in '%s'\n", path);
|
2014-10-15 00:50:38 +00:00
|
|
|
if (btrfs_register_one_device(path) != 0) {
|
|
|
|
ret = 1;
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2014-10-15 00:50:38 +00:00
|
|
|
goto out;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
free(path);
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:36:48 +00:00
|
|
|
out:
|
|
|
|
return !!ret;
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 18:06:20 +00:00
|
|
|
static const char * const cmd_ready_dev_usage[] = {
|
|
|
|
"btrfs device ready <device>",
|
2014-09-02 11:57:20 +00:00
|
|
|
"Check device to see if it has all of its devices in cache for mounting",
|
2013-01-19 18:06:20 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_ready_dev(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct btrfs_ioctl_vol_args args;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
2014-06-04 20:43:11 +00:00
|
|
|
char *path;
|
2013-01-19 18:06:20 +00:00
|
|
|
|
|
|
|
if (check_argc_min(argc, 2))
|
|
|
|
usage(cmd_ready_dev_usage);
|
|
|
|
|
|
|
|
fd = open("/dev/btrfs-control", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("failed to open /dev/btrfs-control");
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2013-01-19 18:06:20 +00:00
|
|
|
}
|
2014-06-04 20:43:11 +00:00
|
|
|
|
|
|
|
path = canonicalize_path(argv[argc - 1]);
|
|
|
|
if (!path) {
|
2013-12-12 07:47:00 +00:00
|
|
|
fprintf(stderr,
|
2014-06-04 20:43:11 +00:00
|
|
|
"ERROR: Could not canonicalize pathname '%s': %s\n",
|
|
|
|
argv[argc - 1], strerror(errno));
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
2013-12-12 07:47:00 +00:00
|
|
|
}
|
2013-01-19 18:06:20 +00:00
|
|
|
|
2014-06-04 20:43:11 +00:00
|
|
|
if (!is_block_device(path)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: %s is not a block device\n", path);
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:36:51 +00:00
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
strncpy_null(args.name, path);
|
2013-01-19 18:06:20 +00:00
|
|
|
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "ERROR: unable to determine if the device '%s'"
|
2014-06-04 20:43:11 +00:00
|
|
|
" is ready for mounting - %s\n", path,
|
2013-01-19 18:06:20 +00:00
|
|
|
strerror(errno));
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
2014-06-04 20:43:11 +00:00
|
|
|
out:
|
|
|
|
free(path);
|
2013-01-19 18:06:20 +00:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-15 09:30:39 +00:00
|
|
|
static const char * const cmd_dev_stats_usage[] = {
|
|
|
|
"btrfs device stats [-z] <path>|<device>",
|
|
|
|
"Show current device IO stats. -z to reset stats afterwards.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_dev_stats(int argc, char **argv)
|
|
|
|
{
|
2013-08-14 23:16:34 +00:00
|
|
|
char *dev_path;
|
2012-05-15 09:30:39 +00:00
|
|
|
struct btrfs_ioctl_fs_info_args fi_args;
|
|
|
|
struct btrfs_ioctl_dev_info_args *di_args = NULL;
|
|
|
|
int ret;
|
|
|
|
int fdmnt;
|
|
|
|
int i;
|
2013-07-09 16:38:29 +00:00
|
|
|
int c;
|
2012-05-15 09:30:39 +00:00
|
|
|
int err = 0;
|
|
|
|
__u64 flags = 0;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-05-15 09:30:39 +00:00
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
while ((c = getopt(argc, argv, "z")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'z':
|
|
|
|
flags = BTRFS_DEV_STATS_RESET;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2014-03-06 03:36:47 +00:00
|
|
|
usage(cmd_dev_stats_usage);
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:36:47 +00:00
|
|
|
argc = argc - optind;
|
|
|
|
if (check_argc_exact(argc, 1))
|
|
|
|
usage(cmd_dev_stats_usage);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2013-08-14 23:16:34 +00:00
|
|
|
dev_path = argv[optind];
|
2012-05-15 09:30:39 +00:00
|
|
|
|
2013-08-14 23:16:34 +00:00
|
|
|
fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
|
2013-03-12 04:17:40 +00:00
|
|
|
|
2012-05-15 09:30:39 +00:00
|
|
|
if (fdmnt < 0) {
|
2014-04-28 08:37:29 +00:00
|
|
|
if (errno == EINVAL)
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: '%s' is not a mounted btrfs device\n",
|
|
|
|
dev_path);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "ERROR: can't access '%s': %s\n",
|
|
|
|
dev_path, strerror(errno));
|
2013-09-04 15:22:22 +00:00
|
|
|
return 1;
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 23:16:34 +00:00
|
|
|
ret = get_fs_info(dev_path, &fi_args, &di_args);
|
2012-05-15 09:30:39 +00:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "ERROR: getting dev info for devstats failed: "
|
|
|
|
"%s\n", strerror(-ret));
|
|
|
|
err = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!fi_args.num_devices) {
|
|
|
|
fprintf(stderr, "ERROR: no devices found\n");
|
|
|
|
err = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < fi_args.num_devices; i++) {
|
|
|
|
struct btrfs_ioctl_get_dev_stats args = {0};
|
|
|
|
__u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
|
|
|
|
|
|
|
|
strncpy((char *)path, (char *)di_args[i].path,
|
|
|
|
BTRFS_DEVICE_PATH_NAME_MAX);
|
|
|
|
path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
|
|
|
|
|
|
|
|
args.devid = di_args[i].devid;
|
|
|
|
args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
|
|
|
|
args.flags = flags;
|
|
|
|
|
|
|
|
if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
|
|
|
|
path, strerror(errno));
|
|
|
|
err = 1;
|
|
|
|
} else {
|
2014-11-13 01:36:20 +00:00
|
|
|
char *canonical_path;
|
|
|
|
|
|
|
|
canonical_path = canonicalize_path((char *)path);
|
|
|
|
|
2012-05-15 09:30:39 +00:00
|
|
|
if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
|
|
|
|
printf("[%s].write_io_errs %llu\n",
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path,
|
2012-05-15 09:30:39 +00:00
|
|
|
(unsigned long long) args.values[
|
|
|
|
BTRFS_DEV_STAT_WRITE_ERRS]);
|
|
|
|
if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
|
|
|
|
printf("[%s].read_io_errs %llu\n",
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path,
|
2012-05-15 09:30:39 +00:00
|
|
|
(unsigned long long) args.values[
|
|
|
|
BTRFS_DEV_STAT_READ_ERRS]);
|
|
|
|
if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
|
|
|
|
printf("[%s].flush_io_errs %llu\n",
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path,
|
2012-05-15 09:30:39 +00:00
|
|
|
(unsigned long long) args.values[
|
|
|
|
BTRFS_DEV_STAT_FLUSH_ERRS]);
|
|
|
|
if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
|
|
|
|
printf("[%s].corruption_errs %llu\n",
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path,
|
2012-05-15 09:30:39 +00:00
|
|
|
(unsigned long long) args.values[
|
|
|
|
BTRFS_DEV_STAT_CORRUPTION_ERRS]);
|
|
|
|
if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
|
|
|
|
printf("[%s].generation_errs %llu\n",
|
2014-11-13 01:36:20 +00:00
|
|
|
canonical_path,
|
2012-05-15 09:30:39 +00:00
|
|
|
(unsigned long long) args.values[
|
|
|
|
BTRFS_DEV_STAT_GENERATION_ERRS]);
|
2014-11-13 01:36:20 +00:00
|
|
|
|
|
|
|
free(canonical_path);
|
2012-05-15 09:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(di_args);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fdmnt, dirstream);
|
2012-05-15 09:30:39 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-04-23 17:00:22 +00:00
|
|
|
const char * const cmd_device_usage_usage[] = {
|
2014-12-17 17:16:18 +00:00
|
|
|
"btrfs device usage [options] <path> [<path>..]",
|
|
|
|
"Show detailed information about internal allocations in devices.",
|
|
|
|
"-b|--raw raw numbers in bytes",
|
2015-01-21 17:47:24 +00:00
|
|
|
"-h|--human-readable",
|
|
|
|
" human friendly numbers, base 1024 (default)",
|
2014-12-17 17:16:18 +00:00
|
|
|
"-H human friendly numbers, base 1000",
|
|
|
|
"--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
|
|
|
|
"--si use 1000 as a base (kB, MB, GB, TB)",
|
|
|
|
"-k|--kbytes show sizes in KiB, or kB with --si",
|
|
|
|
"-m|--mbytes show sizes in MiB, or MB with --si",
|
|
|
|
"-g|--gbytes show sizes in GiB, or GB with --si",
|
|
|
|
"-t|--tbytes show sizes in TiB, or TB with --si",
|
2014-04-23 17:00:22 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-12-18 13:52:36 +00:00
|
|
|
static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
|
2014-04-23 17:00:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2014-04-25 17:39:11 +00:00
|
|
|
struct chunk_info *chunkinfo = NULL;
|
|
|
|
struct device_info *devinfo = NULL;
|
|
|
|
int chunkcount = 0;
|
|
|
|
int devcount = 0;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2014-04-25 17:39:11 +00:00
|
|
|
ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
|
|
|
|
&devcount);
|
2014-04-28 16:55:05 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2014-04-25 17:39:11 +00:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
|
2014-12-18 13:52:36 +00:00
|
|
|
print_device_sizes(fd, &devinfo[i], unit_mode);
|
2014-04-25 17:39:11 +00:00
|
|
|
print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
|
2014-12-18 13:52:36 +00:00
|
|
|
unit_mode);
|
2014-04-23 17:00:22 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:55:05 +00:00
|
|
|
out:
|
2014-04-25 17:39:11 +00:00
|
|
|
free(devinfo);
|
|
|
|
free(chunkinfo);
|
2014-04-23 17:00:22 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_device_usage(int argc, char **argv)
|
|
|
|
{
|
2014-12-17 17:16:18 +00:00
|
|
|
unsigned unit_mode = UNITS_DEFAULT;
|
2014-04-28 16:55:05 +00:00
|
|
|
int ret = 0;
|
2014-04-23 17:00:22 +00:00
|
|
|
int i, more_than_one = 0;
|
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
while (1) {
|
2015-04-08 15:33:55 +00:00
|
|
|
int c;
|
2014-12-17 17:16:18 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "raw", no_argument, NULL, 'b'},
|
|
|
|
{ "kbytes", no_argument, NULL, 'k'},
|
|
|
|
{ "mbytes", no_argument, NULL, 'm'},
|
|
|
|
{ "gbytes", no_argument, NULL, 'g'},
|
|
|
|
{ "tbytes", no_argument, NULL, 't'},
|
2015-01-21 17:09:32 +00:00
|
|
|
{ "si", no_argument, NULL, GETOPT_VAL_SI},
|
|
|
|
{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
|
2015-01-21 17:47:24 +00:00
|
|
|
{ "human-readable", no_argument, NULL,
|
|
|
|
GETOPT_VAL_HUMAN_READABLE},
|
2015-01-21 17:53:02 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2014-12-17 17:16:18 +00:00
|
|
|
};
|
2014-04-23 17:00:22 +00:00
|
|
|
|
2015-04-08 15:33:55 +00:00
|
|
|
c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
|
2014-04-23 17:00:22 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
2014-12-17 17:16:18 +00:00
|
|
|
unit_mode = UNITS_RAW;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
units_set_base(&unit_mode, UNITS_KBYTES);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
units_set_base(&unit_mode, UNITS_MBYTES);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
units_set_base(&unit_mode, UNITS_GBYTES);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
units_set_base(&unit_mode, UNITS_TBYTES);
|
|
|
|
break;
|
2015-01-21 17:47:24 +00:00
|
|
|
case GETOPT_VAL_HUMAN_READABLE:
|
2014-12-17 17:16:18 +00:00
|
|
|
case 'h':
|
|
|
|
unit_mode = UNITS_HUMAN_BINARY;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
unit_mode = UNITS_HUMAN_DECIMAL;
|
|
|
|
break;
|
2015-01-21 17:09:32 +00:00
|
|
|
case GETOPT_VAL_SI:
|
2014-12-17 17:16:18 +00:00
|
|
|
units_set_mode(&unit_mode, UNITS_DECIMAL);
|
|
|
|
break;
|
2015-01-21 17:09:32 +00:00
|
|
|
case GETOPT_VAL_IEC:
|
2014-12-17 17:16:18 +00:00
|
|
|
units_set_mode(&unit_mode, UNITS_BINARY);
|
2014-04-23 17:00:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(cmd_device_usage_usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 1))
|
|
|
|
usage(cmd_device_usage_usage);
|
|
|
|
|
|
|
|
for (i = optind; i < argc ; i++) {
|
2014-04-28 16:55:05 +00:00
|
|
|
int fd;
|
2014-04-23 17:00:22 +00:00
|
|
|
DIR *dirstream = NULL;
|
|
|
|
if (more_than_one)
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
fd = open_file_or_dir(argv[i], &dirstream);
|
|
|
|
if (fd < 0) {
|
2014-04-28 16:55:05 +00:00
|
|
|
fprintf(stderr, "ERROR: can't access '%s'\n",
|
2014-04-23 17:00:22 +00:00
|
|
|
argv[1]);
|
2014-04-28 16:55:05 +00:00
|
|
|
ret = 1;
|
|
|
|
goto out;
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
2014-04-25 17:39:11 +00:00
|
|
|
|
2014-12-17 17:16:18 +00:00
|
|
|
ret = _cmd_device_usage(fd, argv[i], unit_mode);
|
2014-04-23 17:00:22 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
|
2014-04-28 16:55:05 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2014-04-23 17:00:22 +00:00
|
|
|
more_than_one = 1;
|
|
|
|
}
|
2014-04-28 16:55:05 +00:00
|
|
|
out:
|
|
|
|
return !!ret;
|
2014-04-23 17:00:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char device_cmd_group_info[] =
|
|
|
|
"manage and query devices in the filesystem";
|
|
|
|
|
2012-02-03 19:00:17 +00:00
|
|
|
const struct cmd_group device_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
device_cmd_group_usage, device_cmd_group_info, {
|
2012-02-03 19:00:17 +00:00
|
|
|
{ "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
|
|
|
|
{ "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
|
|
|
|
{ "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
|
2013-01-19 18:06:20 +00:00
|
|
|
{ "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
|
2012-05-15 09:30:39 +00:00
|
|
|
{ "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
|
2014-04-23 17:00:22 +00:00
|
|
|
{ "usage", cmd_device_usage,
|
|
|
|
cmd_device_usage_usage, NULL, 0 },
|
2013-08-14 23:16:45 +00:00
|
|
|
NULL_CMD_STRUCT
|
2012-02-03 19:00:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_device(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return handle_command_group(&device_cmd_group, argc, argv);
|
|
|
|
}
|