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 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 = open_file_or_dir(path, &dirstream);
50 fprintf(stderr, "ERROR: can't access '%s'\n", path);
54 ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
56 close_file_or_dir(fd, dirstream);
58 fprintf(stderr, "ERROR: quota command failed: %s\n",
65 static const char * const cmd_quota_enable_usage[] = {
66 "btrfs quota enable <path>",
67 "Enable subvolume quota support for a filesystem.",
68 "Any data already present on the filesystem will not count towards",
69 "the space usage numbers. It is recommended to enable quota for a",
70 "filesystem before writing any data to it.",
74 static int cmd_quota_enable(int argc, char **argv)
76 int ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
78 usage(cmd_quota_enable_usage);
82 static const char * const cmd_quota_disable_usage[] = {
83 "btrfs quota disable <path>",
84 "Disable subvolume quota support for a filesystem.",
88 static int cmd_quota_disable(int argc, char **argv)
90 int ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
92 usage(cmd_quota_disable_usage);
96 static const char * const cmd_quota_rescan_usage[] = {
97 "btrfs quota rescan [-s] <path>",
98 "Trash all qgroup numbers and scan the metadata again with the current config.",
100 "-s show status of a running rescan operation",
104 static int cmd_quota_rescan(int argc, char **argv)
110 struct btrfs_ioctl_quota_rescan_args args;
111 int ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
112 DIR *dirstream = NULL;
116 int c = getopt(argc, argv, "s");
121 ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
124 usage(cmd_quota_rescan_usage);
128 if (check_argc_exact(argc - optind, 1))
129 usage(cmd_quota_rescan_usage);
131 memset(&args, 0, sizeof(args));
134 fd = open_file_or_dir(path, &dirstream);
136 fprintf(stderr, "ERROR: can't access '%s'\n", path);
140 ret = ioctl(fd, ioctlnum, &args);
142 close_file_or_dir(fd, dirstream);
144 if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
146 fprintf(stderr, "ERROR: quota rescan failed: "
147 "%s\n", strerror(e));
150 printf("quota rescan started\n");
154 printf("no rescan operation in progress\n");
156 printf("rescan operation running (current key %lld)\n",
164 const struct cmd_group quota_cmd_group = {
165 quota_cmd_group_usage, NULL, {
166 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
167 { "disable", cmd_quota_disable, cmd_quota_disable_usage, 0, 0 },
168 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
173 int cmd_quota(int argc, char **argv)
175 return handle_command_group("a_cmd_group, argc, argv);