Btrfs-progs: restructure show_qgroups
[platform/upstream/btrfs-progs.git] / cmds-qgroup.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 #include <getopt.h>
22
23 #include "ctree.h"
24 #include "ioctl.h"
25
26 #include "commands.h"
27 #include "qgroup.h"
28 #include "utils.h"
29
30 static const char * const qgroup_cmd_group_usage[] = {
31         "btrfs qgroup <command> [options] <path>",
32         NULL
33 };
34
35 static int qgroup_assign(int assign, int argc, char **argv)
36 {
37         int ret = 0;
38         int fd;
39         int e;
40         char *path = argv[3];
41         struct btrfs_ioctl_qgroup_assign_args args;
42         DIR *dirstream = NULL;
43
44         if (check_argc_exact(argc, 4))
45                 return -1;
46
47         memset(&args, 0, sizeof(args));
48         args.assign = assign;
49         args.src = parse_qgroupid(argv[1]);
50         args.dst = parse_qgroupid(argv[2]);
51
52         /*
53          * FIXME src should accept subvol path
54          */
55         if ((args.src >> 48) >= (args.dst >> 48)) {
56                 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
57                 return 1;
58         }
59         fd = open_file_or_dir(path, &dirstream);
60         if (fd < 0) {
61                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
62                 return 1;
63         }
64
65         ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
66         e = errno;
67         close_file_or_dir(fd, dirstream);
68         if (ret < 0) {
69                 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
70                         strerror(e));
71                 return 1;
72         }
73         return 0;
74 }
75
76 static int qgroup_create(int create, int argc, char **argv)
77 {
78         int ret = 0;
79         int fd;
80         int e;
81         char *path = argv[2];
82         struct btrfs_ioctl_qgroup_create_args args;
83         DIR *dirstream = NULL;
84
85         if (check_argc_exact(argc, 3))
86                 return -1;
87
88         memset(&args, 0, sizeof(args));
89         args.create = create;
90         args.qgroupid = parse_qgroupid(argv[1]);
91
92         fd = open_file_or_dir(path, &dirstream);
93         if (fd < 0) {
94                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
95                 return 1;
96         }
97
98         ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
99         e = errno;
100         close_file_or_dir(fd, dirstream);
101         if (ret < 0) {
102                 fprintf(stderr, "ERROR: unable to create quota group: %s\n",
103                         strerror(e));
104                 return 1;
105         }
106         return 0;
107 }
108
109 static int parse_limit(const char *p, unsigned long long *s)
110 {
111         char *endptr;
112         unsigned long long size;
113
114         if (strcasecmp(p, "none") == 0) {
115                 *s = 0;
116                 return 1;
117         }
118         size = strtoull(p, &endptr, 10);
119         switch (*endptr) {
120         case 'T':
121         case 't':
122                 size *= 1024;
123         case 'G':
124         case 'g':
125                 size *= 1024;
126         case 'M':
127         case 'm':
128                 size *= 1024;
129         case 'K':
130         case 'k':
131                 size *= 1024;
132                 ++endptr;
133                 break;
134         case 0:
135                 break;
136         default:
137                 return 0;
138         }
139
140         if (*endptr)
141                 return 0;
142
143         *s = size;
144
145         return 1;
146 }
147
148 static const char * const cmd_qgroup_assign_usage[] = {
149         "btrfs qgroup assign <src> <dst> <path>",
150         "Enable subvolume qgroup support for a filesystem.",
151         NULL
152 };
153
154 static int cmd_qgroup_assign(int argc, char **argv)
155 {
156         int ret = qgroup_assign(1, argc, argv);
157         if (ret < 0)
158                 usage(cmd_qgroup_assign_usage);
159         return ret;
160 }
161
162 static const char * const cmd_qgroup_remove_usage[] = {
163         "btrfs qgroup remove <src> <dst> <path>",
164         "Remove a subvol from a quota group.",
165         NULL
166 };
167
168 static int cmd_qgroup_remove(int argc, char **argv)
169 {
170         int ret = qgroup_assign(0, argc, argv);
171         if (ret < 0)
172                 usage(cmd_qgroup_remove_usage);
173         return ret;
174 }
175
176 static const char * const cmd_qgroup_create_usage[] = {
177         "btrfs qgroup create <qgroupid> <path>",
178         "Create a subvolume quota group.",
179         NULL
180 };
181
182 static int cmd_qgroup_create(int argc, char **argv)
183 {
184         int ret = qgroup_create(1, argc, argv);
185         if (ret < 0)
186                 usage(cmd_qgroup_create_usage);
187         return ret;
188 }
189
190 static const char * const cmd_qgroup_destroy_usage[] = {
191         "btrfs qgroup destroy <qgroupid> <path>",
192         "Destroy a subvolume quota group.",
193         NULL
194 };
195
196 static int cmd_qgroup_destroy(int argc, char **argv)
197 {
198         int ret = qgroup_create(0, argc, argv);
199         if (ret < 0)
200                 usage(cmd_qgroup_destroy_usage);
201         return ret;
202 }
203
204 static const char * const cmd_qgroup_show_usage[] = {
205         "btrfs qgroup show <path>",
206         "Show all subvolume quota groups.",
207         NULL
208 };
209
210 static int cmd_qgroup_show(int argc, char **argv)
211 {
212         int ret = 0;
213         int fd;
214         int e;
215         char *path = argv[1];
216         DIR *dirstream = NULL;
217
218         if (check_argc_exact(argc, 2))
219                 usage(cmd_qgroup_show_usage);
220
221         fd = open_file_or_dir(path, &dirstream);
222         if (fd < 0) {
223                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
224                 return 1;
225         }
226
227         ret = btrfs_show_qgroups(fd);
228         e = errno;
229         close_file_or_dir(fd, dirstream);
230         if (ret < 0)
231                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
232                                 strerror(e));
233
234         return !!ret;
235 }
236
237 static const char * const cmd_qgroup_limit_usage[] = {
238         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
239         "Limit the size of a subvolume quota group.",
240         "",
241         "-c   limit amount of data after compression. This is the default,",
242         "     it is currently not possible to turn off this option.",
243         "-e   limit space exclusively assigned to this qgroup",
244         NULL
245 };
246
247 static int cmd_qgroup_limit(int argc, char **argv)
248 {
249         int ret = 0;
250         int fd;
251         int e;
252         char *path = NULL;
253         struct btrfs_ioctl_qgroup_limit_args args;
254         unsigned long long size;
255         int compressed = 0;
256         int exclusive = 0;
257         DIR *dirstream = NULL;
258
259         optind = 1;
260         while (1) {
261                 int c = getopt(argc, argv, "ce");
262                 if (c < 0)
263                         break;
264                 switch (c) {
265                 case 'c':
266                         compressed = 1;
267                         break;
268                 case 'e':
269                         exclusive = 1;
270                         break;
271                 default:
272                         usage(cmd_qgroup_limit_usage);
273                 }
274         }
275
276         if (check_argc_min(argc - optind, 2))
277                 usage(cmd_qgroup_limit_usage);
278
279         if (!parse_limit(argv[optind], &size)) {
280                 fprintf(stderr, "Invalid size argument given\n");
281                 return 1;
282         }
283
284         memset(&args, 0, sizeof(args));
285         if (size) {
286                 if (compressed)
287                         args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
288                                           BTRFS_QGROUP_LIMIT_EXCL_CMPR;
289                 if (exclusive) {
290                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
291                         args.lim.max_exclusive = size;
292                 } else {
293                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
294                         args.lim.max_referenced = size;
295                 }
296         }
297
298         if (argc - optind == 2) {
299                 args.qgroupid = 0;
300                 path = argv[optind + 1];
301                 ret = test_issubvolume(path);
302                 if (ret < 0) {
303                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
304                         return 1;
305                 }
306                 if (!ret) {
307                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
308                                 path);
309                         return 1;
310                 }
311                 /*
312                  * keep qgroupid at 0, this indicates that the subvolume the
313                  * fd refers to is to be limited
314                  */
315         } else if (argc - optind == 3) {
316                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
317                 path = argv[optind + 2];
318         } else
319                 usage(cmd_qgroup_limit_usage);
320
321         fd = open_file_or_dir(path, &dirstream);
322         if (fd < 0) {
323                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
324                 return 1;
325         }
326
327         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
328         e = errno;
329         close_file_or_dir(fd, dirstream);
330         if (ret < 0) {
331                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
332                         "%s\n", strerror(e));
333                 return 1;
334         }
335         return 0;
336 }
337
338 const struct cmd_group qgroup_cmd_group = {
339         qgroup_cmd_group_usage, NULL, {
340                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
341                    NULL, 0 },
342                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
343                    NULL, 0 },
344                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
345                    NULL, 0 },
346                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
347                    NULL, 0 },
348                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
349                    NULL, 0 },
350                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
351                    NULL, 0 },
352                 NULL_CMD_STRUCT
353         }
354 };
355
356 int cmd_qgroup(int argc, char **argv)
357 {
358         return handle_command_group(&qgroup_cmd_group, argc, argv);
359 }