2012-08-07 10:37:54 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kerncompat.h"
|
2012-08-07 10:37:54 +00:00
|
|
|
#include <sys/ioctl.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2023-08-28 20:12:13 +00:00
|
|
|
#include <unistd.h>
|
2023-04-19 21:17:12 +00:00
|
|
|
#include "kernel-shared/uapi/btrfs.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "common/messages.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
2012-08-07 10:37:54 +00:00
|
|
|
|
|
|
|
static const char * const quota_cmd_group_usage[] = {
|
|
|
|
"btrfs quota <command> [options] <path>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2023-09-27 17:46:48 +00:00
|
|
|
static int quota_ctl(int cmd, char *path)
|
2012-08-07 10:37:54 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int fd;
|
|
|
|
struct btrfs_ioctl_quota_ctl_args args;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2012-08-07 10:37:54 +00:00
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.cmd = cmd;
|
|
|
|
|
2015-10-12 13:23:01 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-04 15:22:26 +00:00
|
|
|
return 1;
|
2012-08-07 10:37:54 +00:00
|
|
|
|
|
|
|
ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
|
2013-07-15 11:36:50 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2012-08-07 10:37:54 +00:00
|
|
|
if (ret < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("quota command failed: %m");
|
2013-09-04 15:22:26 +00:00
|
|
|
return 1;
|
2012-08-07 10:37:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const cmd_quota_enable_usage[] = {
|
|
|
|
"btrfs quota enable <path>",
|
|
|
|
"Enable subvolume quota support for a filesystem.",
|
2013-03-27 13:54:12 +00:00
|
|
|
"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.",
|
2023-09-27 17:46:48 +00:00
|
|
|
"",
|
|
|
|
"-s|--simple simple qgroups account ownership by extent lifetime rather than backref walks",
|
2012-08-07 10:37:54 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_quota_enable(const struct cmd_struct *cmd, int argc, char **argv)
|
2012-08-07 10:37:54 +00:00
|
|
|
{
|
2016-03-01 15:02:08 +00:00
|
|
|
int ret;
|
2023-09-27 17:46:48 +00:00
|
|
|
int ctl_cmd = BTRFS_QUOTA_CTL_ENABLE;
|
|
|
|
|
|
|
|
optind = 0;
|
|
|
|
while (1) {
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"simple", no_argument, NULL, 's'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "s", long_options, NULL);
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
ctl_cmd = BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage_unknown_option(cmd, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (check_argc_exact(argc - optind, 1))
|
|
|
|
return -1;
|
2016-03-01 15:02:08 +00:00
|
|
|
|
2023-09-27 17:46:48 +00:00
|
|
|
ret = quota_ctl(ctl_cmd, argv[optind]);
|
2012-08-07 10:37:54 +00:00
|
|
|
if (ret < 0)
|
2023-02-13 05:26:32 +00:00
|
|
|
usage(cmd, 1);
|
2012-08-07 10:37:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(quota_enable, "enable");
|
2012-08-07 10:37:54 +00:00
|
|
|
|
|
|
|
static const char * const cmd_quota_disable_usage[] = {
|
|
|
|
"btrfs quota disable <path>",
|
|
|
|
"Disable subvolume quota support for a filesystem.",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_quota_disable(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-08-07 10:37:54 +00:00
|
|
|
{
|
2016-03-01 15:02:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-03-07 02:42:01 +00:00
|
|
|
clean_args_no_options(cmd, argc, argv);
|
2016-03-01 15:02:08 +00:00
|
|
|
|
2023-09-27 17:46:48 +00:00
|
|
|
if (check_argc_exact(argc, 2))
|
|
|
|
return -1;
|
2016-03-01 15:02:08 +00:00
|
|
|
|
2023-09-27 17:46:48 +00:00
|
|
|
ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argv[1]);
|
2012-08-07 10:37:54 +00:00
|
|
|
if (ret < 0)
|
2023-02-13 05:26:32 +00:00
|
|
|
usage(cmd, 1);
|
2012-08-07 10:37:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(quota_disable, "disable");
|
2012-08-07 10:37:54 +00:00
|
|
|
|
|
|
|
static const char * const cmd_quota_rescan_usage[] = {
|
2013-05-06 19:15:18 +00:00
|
|
|
"btrfs quota rescan [-sw] <path>",
|
2013-04-05 11:44:06 +00:00
|
|
|
"Trash all qgroup numbers and scan the metadata again with the current config.",
|
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("-s|--status", "show status of a running rescan operation"),
|
|
|
|
OPTLINE("-w|--wait", "start rescan and wait for it to finish (can be already in progress)"),
|
|
|
|
OPTLINE("-W|--wait-norescan", "wait for rescan to finish without starting it"),
|
2020-06-11 17:41:16 +00:00
|
|
|
HELPINFO_INSERT_GLOBALS,
|
|
|
|
HELPINFO_INSERT_QUIET,
|
2012-08-07 10:37:54 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_quota_rescan(const struct cmd_struct *cmd, int argc, char **argv)
|
2012-08-07 10:37:54 +00:00
|
|
|
{
|
2013-04-05 11:44:06 +00:00
|
|
|
int ret = 0;
|
|
|
|
int fd;
|
|
|
|
int e;
|
|
|
|
char *path = NULL;
|
|
|
|
struct btrfs_ioctl_quota_rescan_args args;
|
2015-04-20 05:33:16 +00:00
|
|
|
unsigned long ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
|
2013-07-15 11:36:50 +00:00
|
|
|
DIR *dirstream = NULL;
|
2022-10-05 18:02:13 +00:00
|
|
|
bool wait_for_completion = false;
|
2013-04-05 11:44:06 +00:00
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2013-04-05 11:44:06 +00:00
|
|
|
while (1) {
|
2022-10-25 09:54:58 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"status", no_argument, NULL, 's'},
|
|
|
|
{"wait", no_argument, NULL, 'w'},
|
2022-10-25 18:45:27 +00:00
|
|
|
{"wait-norescan", no_argument, NULL, 'W'},
|
2022-10-25 09:54:58 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
int c;
|
|
|
|
|
2022-10-25 18:45:27 +00:00
|
|
|
c = getopt_long(argc, argv, "swW", long_options, NULL);
|
2013-04-05 11:44:06 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
2022-10-25 09:54:58 +00:00
|
|
|
|
2013-04-05 11:44:06 +00:00
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
|
|
|
|
break;
|
2013-05-06 19:15:18 +00:00
|
|
|
case 'w':
|
2022-10-25 18:45:27 +00:00
|
|
|
ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
|
2022-10-05 18:02:13 +00:00
|
|
|
wait_for_completion = true;
|
2013-05-06 19:15:18 +00:00
|
|
|
break;
|
2022-10-25 18:45:27 +00:00
|
|
|
case 'W':
|
|
|
|
ioctlnum = 0;
|
|
|
|
wait_for_completion = 1;
|
|
|
|
break;
|
2013-04-05 11:44:06 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2013-04-05 11:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:27 +00:00
|
|
|
if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS && wait_for_completion) {
|
2016-01-11 10:01:47 +00:00
|
|
|
error("switch -w cannot be used with -s");
|
2013-09-04 15:22:26 +00:00
|
|
|
return 1;
|
2013-05-06 19:15:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 11:44:06 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2013-04-05 11:44:06 +00:00
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
|
|
|
|
path = argv[optind];
|
2015-10-12 13:23:01 +00:00
|
|
|
fd = btrfs_open_dir(path, &dirstream, 1);
|
|
|
|
if (fd < 0)
|
2013-09-04 15:22:26 +00:00
|
|
|
return 1;
|
2013-04-05 11:44:06 +00:00
|
|
|
|
2022-10-25 18:45:27 +00:00
|
|
|
if (ioctlnum) {
|
|
|
|
ret = ioctl(fd, ioctlnum, &args);
|
|
|
|
e = errno;
|
|
|
|
}
|
2013-05-06 19:15:18 +00:00
|
|
|
|
2017-01-18 01:56:39 +00:00
|
|
|
if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS) {
|
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-04-05 11:44:06 +00:00
|
|
|
if (ret < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("could not obtain quota rescan status: %m");
|
2013-09-04 15:22:26 +00:00
|
|
|
return 1;
|
2013-04-05 11:44:06 +00:00
|
|
|
}
|
2017-01-18 01:56:39 +00:00
|
|
|
if (!args.flags)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "no rescan operation in progress\n");
|
2017-01-18 01:56:39 +00:00
|
|
|
else
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "rescan operation running (current key %lld)\n",
|
2013-04-05 11:44:06 +00:00
|
|
|
args.progress);
|
2017-01-18 01:56:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:27 +00:00
|
|
|
if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN && ret == 0) {
|
2022-10-04 14:28:33 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "quota rescan started\n");
|
2017-01-18 01:56:39 +00:00
|
|
|
fflush(stdout);
|
|
|
|
} else if (ret < 0 && (!wait_for_completion || e != EINPROGRESS)) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("quota rescan failed: %m");
|
2017-01-18 01:56:39 +00:00
|
|
|
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) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("quota rescan wait failed: %m");
|
2017-01-18 01:56:39 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
|
|
|
return 1;
|
2013-04-05 11:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 01:56:39 +00:00
|
|
|
close_file_or_dir(fd, dirstream);
|
2013-04-05 11:44:06 +00:00
|
|
|
return 0;
|
2012-08-07 10:37:54 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
static DEFINE_SIMPLE_COMMAND(quota_rescan, "rescan");
|
2012-08-07 10:37:54 +00:00
|
|
|
|
2015-06-09 12:22:05 +00:00
|
|
|
static const char quota_cmd_group_info[] =
|
|
|
|
"manage filesystem quota settings";
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const struct cmd_group quota_cmd_group = {
|
2015-06-09 12:22:05 +00:00
|
|
|
quota_cmd_group_usage, quota_cmd_group_info, {
|
2018-03-07 02:07:03 +00:00
|
|
|
&cmd_struct_quota_enable,
|
|
|
|
&cmd_struct_quota_disable,
|
|
|
|
&cmd_struct_quota_rescan,
|
|
|
|
NULL
|
2012-08-07 10:37:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_GROUP_COMMAND_TOKEN(quota);
|