btrfs-progs: mkfs: fix regression preventing --rootdir to create file
[btrfs-progs-unstable/devel.git] / cmds-quota.c
blob15bd4b9355ebc93c27143bbc07d4daa1a3b9a7bc
1 /*
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>
20 #include <unistd.h>
22 #include "ctree.h"
23 #include "ioctl.h"
25 #include "commands.h"
26 #include "utils.h"
27 #include "help.h"
29 static const char * const quota_cmd_group_usage[] = {
30 "btrfs quota <command> [options] <path>",
31 NULL
34 static int quota_ctl(int cmd, int argc, char **argv)
36 int ret = 0;
37 int fd;
38 int e;
39 char *path = argv[1];
40 struct btrfs_ioctl_quota_ctl_args args;
41 DIR *dirstream = NULL;
43 if (check_argc_exact(argc, 2))
44 return -1;
46 memset(&args, 0, sizeof(args));
47 args.cmd = cmd;
49 fd = btrfs_open_dir(path, &dirstream, 1);
50 if (fd < 0)
51 return 1;
53 ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
54 e = errno;
55 close_file_or_dir(fd, dirstream);
56 if (ret < 0) {
57 error("quota command failed: %s", strerror(e));
58 return 1;
60 return 0;
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.",
69 NULL
72 static int cmd_quota_enable(int argc, char **argv)
74 int ret;
76 clean_args_no_options(argc, argv, cmd_quota_enable_usage);
78 ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
80 if (ret < 0)
81 usage(cmd_quota_enable_usage);
82 return ret;
85 static const char * const cmd_quota_disable_usage[] = {
86 "btrfs quota disable <path>",
87 "Disable subvolume quota support for a filesystem.",
88 NULL
91 static int cmd_quota_disable(int argc, char **argv)
93 int ret;
95 clean_args_no_options(argc, argv, cmd_quota_disable_usage);
97 ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
99 if (ret < 0)
100 usage(cmd_quota_disable_usage);
101 return ret;
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)",
110 NULL
113 static int cmd_quota_rescan(int argc, char **argv)
115 int ret = 0;
116 int fd;
117 int e;
118 char *path = NULL;
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;
124 while (1) {
125 int c = getopt(argc, argv, "sw");
126 if (c < 0)
127 break;
128 switch (c) {
129 case 's':
130 ioctlnum = BTRFS_IOC_QUOTA_RESCAN_STATUS;
131 break;
132 case 'w':
133 wait_for_completion = 1;
134 break;
135 default:
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");
142 return 1;
145 if (check_argc_exact(argc - optind, 1))
146 usage(cmd_quota_rescan_usage);
148 memset(&args, 0, sizeof(args));
150 path = argv[optind];
151 fd = btrfs_open_dir(path, &dirstream, 1);
152 if (fd < 0)
153 return 1;
155 ret = ioctl(fd, ioctlnum, &args);
156 e = errno;
158 if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN_STATUS) {
159 close_file_or_dir(fd, dirstream);
160 if (ret < 0) {
161 error("could not obtain quota rescan status: %s",
162 strerror(e));
163 return 1;
165 if (!args.flags)
166 printf("no rescan operation in progress\n");
167 else
168 printf("rescan operation running (current key %lld)\n",
169 args.progress);
170 return 0;
173 if (ret == 0) {
174 printf("quota rescan started\n");
175 fflush(stdout);
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);
179 return 1;
182 if (wait_for_completion) {
183 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
184 e = errno;
185 if (ret < 0) {
186 error("quota rescan wait failed: %s",
187 strerror(e));
188 close_file_or_dir(fd, dirstream);
189 return 1;
193 close_file_or_dir(fd, dirstream);
194 return 0;
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,
204 NULL, 0 },
205 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
206 NULL_CMD_STRUCT
210 int cmd_quota(int argc, char **argv)
212 return handle_command_group(&quota_cmd_group, argc, argv);