2 * Copyright (C) 2012 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <sys/ioctl.h>
28 static const char * const quota_cmd_group_usage[] = {
29 "btrfs quota <command> [options] <path>",
33 static int quota_ctl(int cmd, int argc, char **argv)
39 struct btrfs_ioctl_quota_ctl_args args;
40 DIR *dirstream = NULL;
42 if (check_argc_exact(argc, 2))
45 memset(&args, 0, sizeof(args));
48 fd = btrfs_open_dir(path, &dirstream, 1);
52 ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
54 close_file_or_dir(fd, dirstream);
56 error("quota command failed: %s", strerror(e));
62 static const char * const cmd_quota_enable_usage[] = {
63 "btrfs quota enable <path>",
64 "Enable subvolume quota support for a filesystem.",
65 "Any data already present on the filesystem will not count towards",
66 "the space usage numbers. It is recommended to enable quota for a",
67 "filesystem before writing any data to it.",
71 static int cmd_quota_enable(int argc, char **argv)
75 clean_args_no_options(argc, argv, cmd_quota_enable_usage);
77 ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
80 usage(cmd_quota_enable_usage);
84 static const char * const cmd_quota_disable_usage[] = {
85 "btrfs quota disable <path>",
86 "Disable subvolume quota support for a filesystem.",
90 static int cmd_quota_disable(int argc, char **argv)
94 clean_args_no_options(argc, argv, cmd_quota_disable_usage);
96 ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
99 usage(cmd_quota_disable_usage);
103 static const char * const cmd_quota_rescan_usage[] = {
104 "btrfs quota rescan [-sw] <path>",
105 "Trash all qgroup numbers and scan the metadata again with the current config.",
107 "-s show status of a running rescan operation",
108 "-w wait for rescan operation to finish (can be already in progress)",
112 static int cmd_quota_rescan(int argc, char **argv)
118 struct btrfs_ioctl_quota_rescan_args args;
119 unsigned long ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
120 DIR *dirstream = NULL;
121 int wait_for_completion = 0;
124 int c = getopt(argc, argv, "sw");
129 ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
132 wait_for_completion = 1;
135 usage(cmd_quota_rescan_usage);
139 if (ioctlnum != BTRFS_IOC_QUOTA_RESCAN && wait_for_completion) {
140 error("switch -w cannot be used with -s");
144 if (check_argc_exact(argc - optind, 1))
145 usage(cmd_quota_rescan_usage);
147 memset(&args, 0, sizeof(args));
150 fd = btrfs_open_dir(path, &dirstream, 1);
154 ret = ioctl(fd, ioctlnum, &args);
157 if (wait_for_completion && (ret == 0 || e == EINPROGRESS)) {
158 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
161 close_file_or_dir(fd, dirstream);
163 if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
165 error("quota rescan failed: %s", strerror(e));
168 printf("quota rescan started\n");
172 printf("no rescan operation in progress\n");
174 printf("rescan operation running (current key %lld)\n",
182 static const char quota_cmd_group_info[] =
183 "manage filesystem quota settings";
185 const struct cmd_group quota_cmd_group = {
186 quota_cmd_group_usage, quota_cmd_group_info, {
187 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
188 { "disable", cmd_quota_disable, cmd_quota_disable_usage,
190 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
195 int cmd_quota(int argc, char **argv)
197 return handle_command_group("a_cmd_group, argc, argv);