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>
29 static const char * const quota_cmd_group_usage
[] = {
30 "btrfs quota <command> [options] <path>",
34 static int quota_ctl(int cmd
, int argc
, char **argv
)
40 struct btrfs_ioctl_quota_ctl_args args
;
41 DIR *dirstream
= NULL
;
43 if (check_argc_exact(argc
, 2))
46 memset(&args
, 0, sizeof(args
));
49 fd
= btrfs_open_dir(path
, &dirstream
, 1);
53 ret
= ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
);
55 close_file_or_dir(fd
, dirstream
);
57 error("quota command failed: %s", strerror(e
));
63 static const char * const cmd_quota_enable_usage
[] = {
64 "btrfs quota enable <path>",
65 "Enable subvolume quota support for a filesystem.",
66 "Any data already present on the filesystem will not count towards",
67 "the space usage numbers. It is recommended to enable quota for a",
68 "filesystem before writing any data to it.",
72 static int cmd_quota_enable(int argc
, char **argv
)
76 clean_args_no_options(argc
, argv
, cmd_quota_enable_usage
);
78 ret
= quota_ctl(BTRFS_QUOTA_CTL_ENABLE
, argc
, argv
);
81 usage(cmd_quota_enable_usage
);
85 static const char * const cmd_quota_disable_usage
[] = {
86 "btrfs quota disable <path>",
87 "Disable subvolume quota support for a filesystem.",
91 static int cmd_quota_disable(int argc
, char **argv
)
95 clean_args_no_options(argc
, argv
, cmd_quota_disable_usage
);
97 ret
= quota_ctl(BTRFS_QUOTA_CTL_DISABLE
, argc
, argv
);
100 usage(cmd_quota_disable_usage
);
104 static const char * const cmd_quota_rescan_usage
[] = {
105 "btrfs quota rescan [-sw] <path>",
106 "Trash all qgroup numbers and scan the metadata again with the current config.",
108 "-s show status of a running rescan operation",
109 "-w wait for rescan operation to finish (can be already in progress)",
113 static int cmd_quota_rescan(int argc
, char **argv
)
119 struct btrfs_ioctl_quota_rescan_args args
;
120 unsigned long ioctlnum
= BTRFS_IOC_QUOTA_RESCAN
;
121 DIR *dirstream
= NULL
;
122 int wait_for_completion
= 0;
125 int c
= getopt(argc
, argv
, "sw");
130 ioctlnum
= BTRFS_IOC_QUOTA_RESCAN_STATUS
;
133 wait_for_completion
= 1;
136 usage(cmd_quota_rescan_usage
);
140 if (ioctlnum
!= BTRFS_IOC_QUOTA_RESCAN
&& wait_for_completion
) {
141 error("switch -w cannot be used with -s");
145 if (check_argc_exact(argc
- optind
, 1))
146 usage(cmd_quota_rescan_usage
);
148 memset(&args
, 0, sizeof(args
));
151 fd
= btrfs_open_dir(path
, &dirstream
, 1);
155 ret
= ioctl(fd
, ioctlnum
, &args
);
158 if (ioctlnum
== BTRFS_IOC_QUOTA_RESCAN_STATUS
) {
159 close_file_or_dir(fd
, dirstream
);
161 error("could not obtain quota rescan status: %s",
166 printf("no rescan operation in progress\n");
168 printf("rescan operation running (current key %lld)\n",
174 printf("quota rescan started\n");
176 } else if (ret
< 0 && (!wait_for_completion
|| e
!= EINPROGRESS
)) {
177 error("quota rescan failed: %s", strerror(e
));
178 close_file_or_dir(fd
, dirstream
);
182 if (wait_for_completion
) {
183 ret
= ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
, &args
);
186 error("quota rescan wait failed: %s",
188 close_file_or_dir(fd
, dirstream
);
193 close_file_or_dir(fd
, dirstream
);
197 static const char quota_cmd_group_info
[] =
198 "manage filesystem quota settings";
200 const struct cmd_group quota_cmd_group
= {
201 quota_cmd_group_usage
, quota_cmd_group_info
, {
202 { "enable", cmd_quota_enable
, cmd_quota_enable_usage
, NULL
, 0 },
203 { "disable", cmd_quota_disable
, cmd_quota_disable_usage
,
205 { "rescan", cmd_quota_rescan
, cmd_quota_rescan_usage
, NULL
, 0 },
210 int cmd_quota(int argc
, char **argv
)
212 return handle_command_group("a_cmd_group
, argc
, argv
);