568482fc2a73a0eafd8eabee96ed6405274e7604
[platform/upstream/btrfs-progs.git] / cmds-quota.c
1 /*
2  * Copyright (C) 2012 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21
22 #include "ctree.h"
23 #include "ioctl.h"
24
25 #include "commands.h"
26 #include "utils.h"
27
28 static const char * const quota_cmd_group_usage[] = {
29         "btrfs quota <command> [options] <path>",
30         NULL
31 };
32
33 static int quota_ctl(int cmd, int argc, char **argv)
34 {
35         int ret = 0;
36         int fd;
37         int e;
38         char *path = argv[1];
39         struct btrfs_ioctl_quota_ctl_args args;
40         DIR *dirstream = NULL;
41
42         if (check_argc_exact(argc, 2))
43                 return -1;
44
45         memset(&args, 0, sizeof(args));
46         args.cmd = cmd;
47
48         fd = btrfs_open_dir(path, &dirstream, 1);
49         if (fd < 0)
50                 return 1;
51
52         ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
53         e = errno;
54         close_file_or_dir(fd, dirstream);
55         if (ret < 0) {
56                 error("quota command failed: %s", strerror(e));
57                 return 1;
58         }
59         return 0;
60 }
61
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.",
68         NULL
69 };
70
71 static int cmd_quota_enable(int argc, char **argv)
72 {
73         int ret;
74
75         clean_args_no_options(argc, argv, cmd_quota_enable_usage);
76
77         ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
78
79         if (ret < 0)
80                 usage(cmd_quota_enable_usage);
81         return ret;
82 }
83
84 static const char * const cmd_quota_disable_usage[] = {
85         "btrfs quota disable <path>",
86         "Disable subvolume quota support for a filesystem.",
87         NULL
88 };
89
90 static int cmd_quota_disable(int argc, char **argv)
91 {
92         int ret;
93
94         clean_args_no_options(argc, argv, cmd_quota_disable_usage);
95
96         ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
97
98         if (ret < 0)
99                 usage(cmd_quota_disable_usage);
100         return ret;
101 }
102
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.",
106         "",
107         "-s   show status of a running rescan operation",
108         "-w   wait for rescan operation to finish (can be already in progress)",
109         NULL
110 };
111
112 static int cmd_quota_rescan(int argc, char **argv)
113 {
114         int ret = 0;
115         int fd;
116         int e;
117         char *path = NULL;
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;
122
123         optind = 1;
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);
137                 }
138         }
139
140         if (ioctlnum != BTRFS_IOC_QUOTA_RESCAN && wait_for_completion) {
141                 error("switch -w cannot be used with -s");
142                 return 1;
143         }
144
145         if (check_argc_exact(argc - optind, 1))
146                 usage(cmd_quota_rescan_usage);
147
148         memset(&args, 0, sizeof(args));
149
150         path = argv[optind];
151         fd = btrfs_open_dir(path, &dirstream, 1);
152         if (fd < 0)
153                 return 1;
154
155         ret = ioctl(fd, ioctlnum, &args);
156         e = errno;
157
158         if (wait_for_completion && (ret == 0 || e == EINPROGRESS)) {
159                 ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT, &args);
160                 e = errno;
161         }
162         close_file_or_dir(fd, dirstream);
163
164         if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
165                 if (ret < 0) {
166                         error("quota rescan failed: %s", strerror(e));
167                         return 1;
168                 }  else {
169                         printf("quota rescan started\n");
170                 }
171         } else {
172                 if (!args.flags) {
173                         printf("no rescan operation in progress\n");
174                 } else {
175                         printf("rescan operation running (current key %lld)\n",
176                                 args.progress);
177                 }
178         }
179
180         return 0;
181 }
182
183 static const char quota_cmd_group_info[] =
184 "manage filesystem quota settings";
185
186 const struct cmd_group quota_cmd_group = {
187         quota_cmd_group_usage, quota_cmd_group_info, {
188                 { "enable", cmd_quota_enable, cmd_quota_enable_usage, NULL, 0 },
189                 { "disable", cmd_quota_disable, cmd_quota_disable_usage,
190                    NULL, 0 },
191                 { "rescan", cmd_quota_rescan, cmd_quota_rescan_usage, NULL, 0 },
192                 NULL_CMD_STRUCT
193         }
194 };
195
196 int cmd_quota(int argc, char **argv)
197 {
198         return handle_command_group(&quota_cmd_group, argc, argv);
199 }