btrfs-progs/cmds-quota.c
Rosen Penev e4df433b8a btrfs-progs: treewide: Replace strerror(errno) with %m.
As btrfs is specific to Linux, %m can be used instead of strerror(errno)
in format strings. This has some size reduction benefits for embedded
systems.

glibc, musl, and uclibc-ng all support %m as a modifier to printf.
A quick glance at the BIONIC libc source indicates that it has
support for %m as well. BSDs and Windows do not but I do believe
them to be beyond the scope of btrfs-progs.

Compiled sizes on Ubuntu 16.04:

Before:
3916512 btrfs
233688  libbtrfs.so.0.1
4899    bcp
2367672 btrfs-convert
2208488 btrfs-corrupt-block
13302   btrfs-debugfs
2152160 btrfs-debug-tree
2136024 btrfs-find-root
2287592 btrfs-image
2144600 btrfs-map-logical
2130760 btrfs-select-super
2152608 btrfstune
2131760 btrfs-zero-log
2277752 mkfs.btrfs
9166    show-blocks

After:
3908744 btrfs
233256  libbtrfs.so.0.1
4899    bcp
2366560 btrfs-convert
2207432 btrfs-corrupt-block
13302   btrfs-debugfs
2151104 btrfs-debug-tree
2134968 btrfs-find-root
2281864 btrfs-image
2143536 btrfs-map-logical
2129704 btrfs-select-super
2151552 btrfstune
2130696 btrfs-zero-log
2276272 mkfs.btrfs
9166    show-blocks

Total savings: 23928 (24 kilo)bytes

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:03 +01:00

210 lines
4.8 KiB
C

/*
* Copyright (C) 2012 STRATO. All rights reserved.
*
* 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 <sys/ioctl.h>
#include <unistd.h>
#include "ctree.h"
#include "ioctl.h"
#include "commands.h"
#include "utils.h"
#include "help.h"
static const char * const quota_cmd_group_usage[] = {
"btrfs quota <command> [options] <path>",
NULL
};
static int quota_ctl(int cmd, int argc, char **argv)
{
int ret = 0;
int fd;
char *path = argv[1];
struct btrfs_ioctl_quota_ctl_args args;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2))
return -1;
memset(&args, 0, sizeof(args));
args.cmd = cmd;
fd = btrfs_open_dir(path, &dirstream, 1);
if (fd < 0)
return 1;
ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
close_file_or_dir(fd, dirstream);
if (ret < 0) {
error("quota command failed: %m");
return 1;
}
return 0;
}
static const char * const cmd_quota_enable_usage[] = {
"btrfs quota enable <path>",
"Enable subvolume quota support for a filesystem.",
"Any data already present on the filesystem will not count towards",
"the space usage numbers. It is recommended to enable quota for a",
"filesystem before writing any data to it.",
NULL
};
static int cmd_quota_enable(int argc, char **argv)
{
int ret;
clean_args_no_options(argc, argv, cmd_quota_enable_usage);
ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
if (ret < 0)
usage(cmd_quota_enable_usage);
return ret;
}
static const char * const cmd_quota_disable_usage[] = {
"btrfs quota disable <path>",
"Disable subvolume quota support for a filesystem.",
NULL
};
static int cmd_quota_disable(int argc, char **argv)
{
int ret;
clean_args_no_options(argc, argv, cmd_quota_disable_usage);
ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
if (ret < 0)
usage(cmd_quota_disable_usage);
return ret;
}
static const char * const cmd_quota_rescan_usage[] = {
"btrfs quota rescan [-sw] <path>",
"Trash all qgroup numbers and scan the metadata again with the current config.",
"",
"-s show status of a running rescan operation",
"-w wait for rescan operation to finish (can be already in progress)",
NULL
};
static int cmd_quota_rescan(int argc, char **argv)
{
int ret = 0;
int fd;
int e;
char *path = NULL;
struct btrfs_ioctl_quota_rescan_args args;
unsigned long ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
DIR *dirstream = NULL;
int wait_for_completion = 0;
while (1) {
int c = getopt(argc, argv, "sw");
if (c < 0)
break;
switch (c) {
case 's':
ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
break;
case 'w':
wait_for_completion = 1;
break;
default:
usage(cmd_quota_rescan_usage);
}
}
if (ioctlnum != BTRFS_IOC_QUOTA_RESCAN && wait_for_completion) {
error("switch -w cannot be used with -s");
return 1;
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_quota_rescan_usage);
memset(&args, 0, sizeof(args));
path = argv[optind];
fd = btrfs_open_dir(path, &dirstream, 1);
if (fd < 0)
return 1;
ret = ioctl(fd, ioctlnum, &args);
e = errno;
if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS) {
close_file_or_dir(fd, dirstream);
if (ret < 0) {
error("could not obtain quota rescan status: %m");
return 1;
}
if (!args.flags)
printf("no rescan operation in progress\n");
else
printf("rescan operation running (current key %lld)\n",
args.progress);
return 0;
}
if (ret == 0) {
printf("quota rescan started\n");
fflush(stdout);
} else if (ret < 0 && (!wait_for_completion || e != EINPROGRESS)) {
error("quota rescan failed: %m");
close_file_or_dir(fd, dirstream);
return 1;
}
if (wait_for_completion) {
ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
e = errno;
if (ret < 0) {
error("quota rescan wait failed: %m");
close_file_or_dir(fd, dirstream);
return 1;
}
}
close_file_or_dir(fd, dirstream);
return 0;
}
static const char quota_cmd_group_info[] =
"manage filesystem quota settings";
const struct cmd_group quota_cmd_group = {
quota_cmd_group_usage, quota_cmd_group_info, {
{ "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
{ "disable", cmd_quota_disable, cmd_quota_disable_usage,
NULL, 0 },
{ "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
NULL_CMD_STRUCT
}
};
int cmd_quota(int argc, char **argv)
{
return handle_command_group(&quota_cmd_group, argc, argv);
}