Btrfs-progs: introduce '-e' option to print max exclusive size of 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 -pcre <path>",
206         "Show all subvolume quota groups.",
207         "-p             print parent qgroup id",
208         "-c             print child qgroup id",
209         "-r             print max referenced size of qgroup",
210         "-e             print max exclusive size of qgroup",
211         NULL
212 };
213
214 static int cmd_qgroup_show(int argc, char **argv)
215 {
216         char *path;
217         int ret = 0;
218         int fd;
219         int e;
220         DIR *dirstream = NULL;
221         int c;
222
223         optind = 1;
224         while (1) {
225                 c = getopt(argc, argv, "pcre");
226                 if (c < 0)
227                         break;
228                 switch (c) {
229                 case 'p':
230                         btrfs_qgroup_setup_print_column(
231                                 BTRFS_QGROUP_PARENT);
232                         break;
233                 case 'c':
234                         btrfs_qgroup_setup_print_column(
235                                 BTRFS_QGROUP_CHILD);
236                         break;
237                 case 'r':
238                         btrfs_qgroup_setup_print_column(
239                                 BTRFS_QGROUP_MAX_RFER);
240                         break;
241                 case 'e':
242                         btrfs_qgroup_setup_print_column(
243                                 BTRFS_QGROUP_MAX_EXCL);
244                         break;
245                 default:
246                         usage(cmd_qgroup_show_usage);
247                 }
248         }
249         if (check_argc_exact(argc - optind, 1))
250                 usage(cmd_qgroup_show_usage);
251
252         path = argv[optind];
253         fd = open_file_or_dir(path, &dirstream);
254         if (fd < 0) {
255                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
256                 return 1;
257         }
258
259         ret = btrfs_show_qgroups(fd);
260         e = errno;
261         close_file_or_dir(fd, dirstream);
262         if (ret < 0)
263                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
264                                 strerror(e));
265
266         return !!ret;
267 }
268
269 static const char * const cmd_qgroup_limit_usage[] = {
270         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
271         "Limit the size of a subvolume quota group.",
272         "",
273         "-c   limit amount of data after compression. This is the default,",
274         "     it is currently not possible to turn off this option.",
275         "-e   limit space exclusively assigned to this qgroup",
276         NULL
277 };
278
279 static int cmd_qgroup_limit(int argc, char **argv)
280 {
281         int ret = 0;
282         int fd;
283         int e;
284         char *path = NULL;
285         struct btrfs_ioctl_qgroup_limit_args args;
286         unsigned long long size;
287         int compressed = 0;
288         int exclusive = 0;
289         DIR *dirstream = NULL;
290
291         optind = 1;
292         while (1) {
293                 int c = getopt(argc, argv, "ce");
294                 if (c < 0)
295                         break;
296                 switch (c) {
297                 case 'c':
298                         compressed = 1;
299                         break;
300                 case 'e':
301                         exclusive = 1;
302                         break;
303                 default:
304                         usage(cmd_qgroup_limit_usage);
305                 }
306         }
307
308         if (check_argc_min(argc - optind, 2))
309                 usage(cmd_qgroup_limit_usage);
310
311         if (!parse_limit(argv[optind], &size)) {
312                 fprintf(stderr, "Invalid size argument given\n");
313                 return 1;
314         }
315
316         memset(&args, 0, sizeof(args));
317         if (size) {
318                 if (compressed)
319                         args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
320                                           BTRFS_QGROUP_LIMIT_EXCL_CMPR;
321                 if (exclusive) {
322                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
323                         args.lim.max_exclusive = size;
324                 } else {
325                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
326                         args.lim.max_referenced = size;
327                 }
328         }
329
330         if (argc - optind == 2) {
331                 args.qgroupid = 0;
332                 path = argv[optind + 1];
333                 ret = test_issubvolume(path);
334                 if (ret < 0) {
335                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
336                         return 1;
337                 }
338                 if (!ret) {
339                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
340                                 path);
341                         return 1;
342                 }
343                 /*
344                  * keep qgroupid at 0, this indicates that the subvolume the
345                  * fd refers to is to be limited
346                  */
347         } else if (argc - optind == 3) {
348                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
349                 path = argv[optind + 2];
350         } else
351                 usage(cmd_qgroup_limit_usage);
352
353         fd = open_file_or_dir(path, &dirstream);
354         if (fd < 0) {
355                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
356                 return 1;
357         }
358
359         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
360         e = errno;
361         close_file_or_dir(fd, dirstream);
362         if (ret < 0) {
363                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
364                         "%s\n", strerror(e));
365                 return 1;
366         }
367         return 0;
368 }
369
370 const struct cmd_group qgroup_cmd_group = {
371         qgroup_cmd_group_usage, NULL, {
372                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
373                    NULL, 0 },
374                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
375                    NULL, 0 },
376                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
377                    NULL, 0 },
378                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
379                    NULL, 0 },
380                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
381                    NULL, 0 },
382                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
383                    NULL, 0 },
384                 NULL_CMD_STRUCT
385         }
386 };
387
388 int cmd_qgroup(int argc, char **argv)
389 {
390         return handle_command_group(&qgroup_cmd_group, argc, argv);
391 }