btrfs-progs: drop unused argument from btrfs_del_ptr
[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 #include "help.h"
28
29 static const char * const quota_cmd_group_usage[] = {
30         "btrfs quota <command> [options] <path>",
31         NULL
32 };
33
34 static int quota_ctl(int cmd, int argc, char **argv)
35 {
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;
42
43         if (check_argc_exact(argc, 2))
44                 return -1;
45
46         memset(&args, 0, sizeof(args));
47         args.cmd = cmd;
48
49         fd = btrfs_open_dir(path, &dirstream, 1);
50         if (fd < 0)
51                 return 1;
52
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;
59         }
60         return 0;
61 }
62
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
70 };
71
72 static int cmd_quota_enable(int argc, char **argv)
73 {
74         int ret;
75
76         clean_args_no_options(argc, argv, cmd_quota_enable_usage);
77
78         ret = quota_ctl(BTRFS_QUOTA_CTL_ENABLE, argc, argv);
79
80         if (ret < 0)
81                 usage(cmd_quota_enable_usage);
82         return ret;
83 }
84
85 static const char * const cmd_quota_disable_usage[] = {
86         "btrfs quota disable <path>",
87         "Disable subvolume quota support for a filesystem.",
88         NULL
89 };
90
91 static int cmd_quota_disable(int argc, char **argv)
92 {
93         int ret;
94
95         clean_args_no_options(argc, argv, cmd_quota_disable_usage);
96
97         ret = quota_ctl(BTRFS_QUOTA_CTL_DISABLE, argc, argv);
98
99         if (ret < 0)
100                 usage(cmd_quota_disable_usage);
101         return ret;
102 }
103
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.",
107         "",
108         "-s   show status of a running rescan operation",
109         "-w   wait for rescan operation to finish (can be already in progress)",
110         NULL
111 };
112
113 static int cmd_quota_rescan(int argc, char **argv)
114 {
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;
123
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 (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;
164                 }
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;
171         }
172
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;
180         }
181
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;
190                 }
191         }
192
193         close_file_or_dir(fd, dirstream);
194         return 0;
195 }
196
197 static const char quota_cmd_group_info[] =
198 "manage filesystem quota settings";
199
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
207         }
208 };
209
210 int cmd_quota(int argc, char **argv)
211 {
212         return handle_command_group(&quota_cmd_group, argc, argv);
213 }