btrfs-progs: cleanup filesystem/device usage code

The main point of this is to load the device and chunk infos at one
place and pass down to the printers. The EPERM is handled separately, in
case kernel does not give us all the information about chunks or
devices, but we want to warn and print at least something.

For non-root users, 'filesystem usage' prints only the overall stats and
warns about RAID5/6.

The sole cleanup changes affect mostly the modified code and the related
functions, should be reasonably small.

Signed-off-by: David Sterba <dsterba@suse.cz>
This commit is contained in:
David Sterba 2014-04-25 19:39:11 +02:00
parent 6312ac8843
commit 0c67867530
3 changed files with 104 additions and 93 deletions

View File

@ -462,31 +462,29 @@ static int _cmd_device_usage(int fd, char *path, int mode)
{ {
int i; int i;
int ret = 0; int ret = 0;
int info_count = 0; struct chunk_info *chunkinfo = NULL;
struct chunk_info *info_ptr = 0; struct device_info *devinfo = NULL;
struct device_info *device_info_ptr = 0; int chunkcount = 0;
int device_info_count = 0; int devcount = 0;
if (load_chunk_info(fd, &info_ptr, &info_count) || ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
load_device_info(fd, &device_info_ptr, &device_info_count)) { &devcount);
if (ret) {
ret = -1; ret = -1;
goto exit; goto exit;
} }
for (i = 0; i < device_info_count; i++) { for (i = 0; i < devcount; i++) {
printf("%s, ID: %llu\n", device_info_ptr[i].path, printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
device_info_ptr[i].devid); print_device_sizes(fd, &devinfo[i], mode);
print_device_sizes(fd, &device_info_ptr[i], mode); print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
print_device_chunks(fd, &device_info_ptr[i], mode);
info_ptr, info_count, mode);
printf("\n"); printf("\n");
} }
exit: exit:
if (device_info_ptr) free(devinfo);
free(device_info_ptr); free(chunkinfo);
if (info_ptr)
free(info_ptr);
return ret; return ret;
} }
@ -528,6 +526,7 @@ int cmd_device_usage(int argc, char **argv)
argv[1]); argv[1]);
return 12; return 12;
} }
r = _cmd_device_usage(fd, argv[i], flags); r = _cmd_device_usage(fd, argv[i], flags);
close_file_or_dir(fd, dirstream); close_file_or_dir(fd, dirstream);

View File

@ -141,7 +141,7 @@ static int cmp_chunk_info(const void *a, const void *b)
((struct chunk_info *)b)->type); ((struct chunk_info *)b)->type);
} }
int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count) static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
{ {
int ret; int ret;
struct btrfs_ioctl_search_args args; struct btrfs_ioctl_search_args args;
@ -172,11 +172,8 @@ int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
while (1) { while (1) {
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
e = errno; e = errno;
if (ret == -EPERM) { if (ret == -EPERM)
fprintf(stderr, return ret;
"ERROR: can't read detailed chunk info from ioctl(TREE_SEARCH), run as root\n");
return 0;
}
if (ret < 0) { if (ret < 0) {
fprintf(stderr, fprintf(stderr,
@ -308,30 +305,23 @@ static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
* which compose the chunk, which could be different from the number of devices * which compose the chunk, which could be different from the number of devices
* if a disk is added later. * if a disk is added later.
*/ */
static int get_raid56_used(int fd, u64 *raid5_used, u64 *raid6_used) static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
u64 *raid5_used, u64 *raid6_used)
{ {
struct chunk_info *info_ptr=0, *p; *raid5_used = 0;
int info_count=0; *raid6_used = 0;
int ret;
*raid5_used = *raid6_used =0; while (chunkcount-- > 0) {
if (chunks->type & BTRFS_BLOCK_GROUP_RAID5)
ret = load_chunk_info(fd, &info_ptr, &info_count); (*raid5_used) += chunks->size / (chunks->num_stripes - 1);
if( ret < 0) if (chunks->type & BTRFS_BLOCK_GROUP_RAID6)
return ret; (*raid6_used) += chunks->size / (chunks->num_stripes - 2);
for ( p = info_ptr; info_count ; info_count--, p++ ) {
if (p->type & BTRFS_BLOCK_GROUP_RAID5)
(*raid5_used) += p->size / (p->num_stripes -1);
if (p->type & BTRFS_BLOCK_GROUP_RAID6)
(*raid6_used) += p->size / (p->num_stripes -2);
} }
free(info_ptr);
return 0;
} }
static int _cmd_disk_free(int fd, char *path, int mode) static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
int chunkcount, struct device_info *devinfo, int devcount,
char *path, int mode)
{ {
struct btrfs_ioctl_space_args *sargs = 0; struct btrfs_ioctl_space_args *sargs = 0;
int i; int i;
@ -360,15 +350,11 @@ static int _cmd_disk_free(int fd, char *path, int mode)
ret = 19; ret = 19;
goto exit; goto exit;
} }
if (get_raid56_used(fd, &raid5_used, &raid6_used) < 0) { get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
fprintf(stderr,
"ERROR: couldn't get space info on '%s'\n",
path );
ret = 20;
goto exit;
}
total_chunks = total_used = total_free = 0; total_chunks = 0;
total_used = 0;
total_free = 0;
for (i = 0; i < sargs->total_spaces; i++) { for (i = 0; i < sargs->total_spaces; i++) {
float ratio = 1; float ratio = 1;
@ -453,7 +439,7 @@ static int cmp_device_info(const void *a, const void *b)
/* /*
* This function loads the device_info structure and put them in an array * This function loads the device_info structure and put them in an array
*/ */
int load_device_info(int fd, struct device_info **device_info_ptr, static int load_device_info(int fd, struct device_info **device_info_ptr,
int *device_info_count) int *device_info_count)
{ {
int ret, i, ndevs; int ret, i, ndevs;
@ -465,10 +451,8 @@ int load_device_info(int fd, struct device_info **device_info_ptr,
*device_info_ptr = 0; *device_info_ptr = 0;
ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args); ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
if (ret == -EPERM) { if (ret == -EPERM)
fprintf(stderr, "ERROR: can't get filesystem info from ioctl(FS_INFO), run as root\n"); return ret;
return -1;
}
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: cannot get filesystem info\n"); fprintf(stderr, "ERROR: cannot get filesystem info\n");
return -1; return -1;
@ -512,6 +496,29 @@ int load_device_info(int fd, struct device_info **device_info_ptr,
return 0; return 0;
} }
int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
int *chunkcount, struct device_info **devinfo, int *devcount)
{
int ret;
ret = load_chunk_info(fd, chunkinfo, chunkcount);
if (ret == -EPERM) {
fprintf(stderr,
"WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
} else if (ret) {
return ret;
}
ret = load_device_info(fd, devinfo, devcount);
if (ret == -EPERM) {
fprintf(stderr,
"WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
ret = 0;
}
return ret;
}
/* /*
* This function computes the size of a chunk in a disk * This function computes the size of a chunk in a disk
*/ */
@ -744,43 +751,32 @@ static void _cmd_filesystem_usage_linear(int mode,
mode); mode);
} }
static int _cmd_filesystem_usage(int fd, char *path, int mode, int tabular) static int print_filesystem_usage_by_chunk(int fd,
struct chunk_info *chunkinfo, int chunkcount,
struct device_info *devinfo, int devcount,
char *path, int mode, int tabular)
{ {
struct btrfs_ioctl_space_args *sargs = 0; struct btrfs_ioctl_space_args *sargs;
int info_count = 0;
struct chunk_info *info_ptr = 0;
struct device_info *device_info_ptr = 0;
int device_info_count = 0;
int ret = 0; int ret = 0;
if (load_chunk_info(fd, &info_ptr, &info_count) || if (!chunkinfo)
load_device_info(fd, &device_info_ptr, &device_info_count)) { return 0;
ret = -1;
goto exit;
}
if ((sargs = load_space_info(fd, path)) == NULL) { sargs = load_space_info(fd, path);
ret = -1; if (!sargs) {
ret = 1;
goto exit; goto exit;
} }
if (tabular) if (tabular)
_cmd_filesystem_usage_tabular(mode, sargs, _cmd_filesystem_usage_tabular(mode, sargs, chunkinfo,
info_ptr, info_count, chunkcount, devinfo, devcount);
device_info_ptr, device_info_count);
else else
_cmd_filesystem_usage_linear(mode, sargs, _cmd_filesystem_usage_linear(mode, sargs, chunkinfo,
info_ptr, info_count, chunkcount, devinfo, devcount);
device_info_ptr, device_info_count);
exit: exit:
free(sargs);
if (sargs)
free(sargs);
if (device_info_ptr)
free(device_info_ptr);
if (info_ptr)
free(info_ptr);
return ret; return ret;
} }
@ -796,16 +792,17 @@ const char * const cmd_filesystem_usage_usage[] = {
int cmd_filesystem_usage(int argc, char **argv) int cmd_filesystem_usage(int argc, char **argv)
{ {
int flags = DF_HUMAN_UNIT; int flags = DF_HUMAN_UNIT;
int i, more_than_one = 0; int i, more_than_one = 0;
int tabular = 0; int tabular = 0;
optind = 1; optind = 1;
while (1) { while (1) {
char c = getopt(argc, argv, "bt"); int c = getopt(argc, argv, "bt");
if (c < 0) if (c < 0)
break; break;
switch (c) { switch (c) {
case 'b': case 'b':
flags &= ~DF_HUMAN_UNIT; flags &= ~DF_HUMAN_UNIT;
@ -821,9 +818,14 @@ int cmd_filesystem_usage(int argc, char **argv)
if (check_argc_min(argc - optind, 1)) if (check_argc_min(argc - optind, 1))
usage(cmd_filesystem_usage_usage); usage(cmd_filesystem_usage_usage);
for (i = optind; i < argc ; i++) { for (i = optind; i < argc; i++) {
int r, fd; int ret;
DIR *dirstream = NULL; int fd;
DIR *dirstream = NULL;
struct chunk_info *chunkinfo = NULL;
struct device_info *devinfo = NULL;
int chunkcount = 0;
int devcount = 0;
fd = open_file_or_dir(argv[i], &dirstream); fd = open_file_or_dir(argv[i], &dirstream);
if (fd < 0) { if (fd < 0) {
@ -834,15 +836,26 @@ int cmd_filesystem_usage(int argc, char **argv)
if (more_than_one) if (more_than_one)
printf("\n"); printf("\n");
r = _cmd_disk_free(fd, argv[i], flags); ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
&devinfo, &devcount);
if (ret)
goto cleanup;
ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
devinfo, devcount, argv[i], flags);
if (ret)
goto cleanup;
printf("\n"); printf("\n");
r = _cmd_filesystem_usage(fd, argv[i], flags, tabular); ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
devinfo, devcount, argv[i], flags, tabular);
cleanup:
close_file_or_dir(fd, dirstream); close_file_or_dir(fd, dirstream);
free(chunkinfo);
free(devinfo);
if (r) if (ret)
return r; return ret;
more_than_one = 1; more_than_one = 1;
} }
return 0; return 0;

View File

@ -46,9 +46,8 @@ struct chunk_info {
u64 num_stripes; u64 num_stripes;
}; };
int load_device_info(int fd, struct device_info **device_info_ptr, int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
int *device_info_count); int *chunkcount, struct device_info **devinfo, int *devcount);
int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count);
char *df_pretty_sizes(u64 size, int mode); char *df_pretty_sizes(u64 size, int mode);
void print_device_chunks(int fd, struct device_info *devinfo, void print_device_chunks(int fd, struct device_info *devinfo,
struct chunk_info *chunks_info_ptr, struct chunk_info *chunks_info_ptr,