btrfs-progs: qgroup limit: error out if input value is negative
[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 (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
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 %s quota group: %s\n",
103                         create ? "create":"destroy", 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         unsigned long long CLEAR_VALUE = -1;
114
115         if (strcasecmp(p, "none") == 0) {
116                 *s = CLEAR_VALUE;
117                 return 1;
118         }
119
120         if (p[0] == '-')
121                 return 0;
122
123         size = strtoull(p, &endptr, 10);
124         switch (*endptr) {
125         case 'T':
126         case 't':
127                 size *= 1024;
128                 /* fallthrough */
129         case 'G':
130         case 'g':
131                 size *= 1024;
132                 /* fallthrough */
133         case 'M':
134         case 'm':
135                 size *= 1024;
136                 /* fallthrough */
137         case 'K':
138         case 'k':
139                 size *= 1024;
140                 ++endptr;
141                 break;
142         case 0:
143                 break;
144         default:
145                 return 0;
146         }
147
148         if (*endptr)
149                 return 0;
150
151         *s = size;
152
153         return 1;
154 }
155
156 static const char * const cmd_qgroup_assign_usage[] = {
157         "btrfs qgroup assign <src> <dst> <path>",
158         "Enable subvolume qgroup support for a filesystem.",
159         NULL
160 };
161
162 static int cmd_qgroup_assign(int argc, char **argv)
163 {
164         int ret = qgroup_assign(1, argc, argv);
165         if (ret < 0)
166                 usage(cmd_qgroup_assign_usage);
167         return ret;
168 }
169
170 static const char * const cmd_qgroup_remove_usage[] = {
171         "btrfs qgroup remove <src> <dst> <path>",
172         "Remove a subvol from a quota group.",
173         NULL
174 };
175
176 static int cmd_qgroup_remove(int argc, char **argv)
177 {
178         int ret = qgroup_assign(0, argc, argv);
179         if (ret < 0)
180                 usage(cmd_qgroup_remove_usage);
181         return ret;
182 }
183
184 static const char * const cmd_qgroup_create_usage[] = {
185         "btrfs qgroup create <qgroupid> <path>",
186         "Create a subvolume quota group.",
187         NULL
188 };
189
190 static int cmd_qgroup_create(int argc, char **argv)
191 {
192         int ret = qgroup_create(1, argc, argv);
193         if (ret < 0)
194                 usage(cmd_qgroup_create_usage);
195         return ret;
196 }
197
198 static const char * const cmd_qgroup_destroy_usage[] = {
199         "btrfs qgroup destroy <qgroupid> <path>",
200         "Destroy a subvolume quota group.",
201         NULL
202 };
203
204 static int cmd_qgroup_destroy(int argc, char **argv)
205 {
206         int ret = qgroup_create(0, argc, argv);
207         if (ret < 0)
208                 usage(cmd_qgroup_destroy_usage);
209         return ret;
210 }
211
212 static const char * const cmd_qgroup_show_usage[] = {
213         "btrfs qgroup show -pcreFf "
214         "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
215         "Show subvolume quota groups.",
216         "-p             print parent qgroup id",
217         "-c             print child qgroup id",
218         "-r             print limit of referenced size of qgroup",
219         "-e             print limit of exclusive size of qgroup",
220         "-F             list all qgroups which impact the given path",
221         "               (including ancestral qgroups)",
222         "-f             list all qgroups which impact the given path",
223         "               (excluding ancestral qgroups)",
224         "--raw          raw numbers in bytes",
225         "--human-readable",
226         "               human firendly numbers in given base, 1024 by default",
227         "--iec          use 1024 as a base (KiB, MiB, GiB, TiB)",
228         "--si           use 1000 as a base (kB, MB, GB, TB)",
229         "--kbytes       show sizes in KiB, or kB with --si",
230         "--mbytes       show sizes in MiB, or MB with --si",
231         "--gbytes       show sizes in GiB, or GB with --si",
232         "--tbytes       show sizes in TiB, or TB with --si",
233         "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
234         "               list qgroups sorted by specified items",
235         "               you can use '+' or '-' in front of each item.",
236         "               (+:ascending, -:descending, ascending default)",
237         NULL
238 };
239
240 static int cmd_qgroup_show(int argc, char **argv)
241 {
242         char *path;
243         int ret = 0;
244         int fd;
245         int e;
246         DIR *dirstream = NULL;
247         u64 qgroupid;
248         int filter_flag = 0;
249         unsigned unit_mode = UNITS_DEFAULT;
250
251         struct btrfs_qgroup_comparer_set *comparer_set;
252         struct btrfs_qgroup_filter_set *filter_set;
253         filter_set = btrfs_qgroup_alloc_filter_set();
254         comparer_set = btrfs_qgroup_alloc_comparer_set();
255
256         optind = 1;
257         while (1) {
258                 int c;
259                 static const struct option long_options[] = {
260                         {"sort", required_argument, NULL, 'S'},
261                         {"raw", no_argument, NULL, GETOPT_VAL_RAW},
262                         {"kbytes", no_argument, NULL, GETOPT_VAL_KBYTES},
263                         {"mbytes", no_argument, NULL, GETOPT_VAL_MBYTES},
264                         {"gbytes", no_argument, NULL, GETOPT_VAL_GBYTES},
265                         {"tbytes", no_argument, NULL, GETOPT_VAL_TBYTES},
266                         {"si", no_argument, NULL, GETOPT_VAL_SI},
267                         {"iec", no_argument, NULL, GETOPT_VAL_IEC},
268                         { "human-readable", no_argument, NULL,
269                                 GETOPT_VAL_HUMAN_READABLE},
270                         { NULL, 0, NULL, 0 }
271                 };
272
273                 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
274                 if (c < 0)
275                         break;
276                 switch (c) {
277                 case 'p':
278                         btrfs_qgroup_setup_print_column(
279                                 BTRFS_QGROUP_PARENT);
280                         break;
281                 case 'c':
282                         btrfs_qgroup_setup_print_column(
283                                 BTRFS_QGROUP_CHILD);
284                         break;
285                 case 'r':
286                         btrfs_qgroup_setup_print_column(
287                                 BTRFS_QGROUP_MAX_RFER);
288                         break;
289                 case 'e':
290                         btrfs_qgroup_setup_print_column(
291                                 BTRFS_QGROUP_MAX_EXCL);
292                         break;
293                 case 'F':
294                         filter_flag |= 0x1;
295                         break;
296                 case 'f':
297                         filter_flag |= 0x2;
298                         break;
299                 case 'S':
300                         ret = btrfs_qgroup_parse_sort_string(optarg,
301                                                              &comparer_set);
302                         if (ret)
303                                 usage(cmd_qgroup_show_usage);
304                         break;
305                 case GETOPT_VAL_RAW:
306                         unit_mode = UNITS_RAW;
307                         break;
308                 case GETOPT_VAL_KBYTES:
309                         units_set_base(&unit_mode, UNITS_KBYTES);
310                         break;
311                 case GETOPT_VAL_MBYTES:
312                         units_set_base(&unit_mode, UNITS_MBYTES);
313                         break;
314                 case GETOPT_VAL_GBYTES:
315                         units_set_base(&unit_mode, UNITS_GBYTES);
316                         break;
317                 case GETOPT_VAL_TBYTES:
318                         units_set_base(&unit_mode, UNITS_TBYTES);
319                         break;
320                 case GETOPT_VAL_SI:
321                         units_set_mode(&unit_mode, UNITS_DECIMAL);
322                         break;
323                 case GETOPT_VAL_IEC:
324                         units_set_mode(&unit_mode, UNITS_BINARY);
325                         break;
326                 case GETOPT_VAL_HUMAN_READABLE:
327                         unit_mode = UNITS_HUMAN_BINARY;
328                         break;
329                 default:
330                         usage(cmd_qgroup_show_usage);
331                 }
332         }
333         btrfs_qgroup_setup_units(unit_mode);
334
335         if (check_argc_exact(argc - optind, 1))
336                 usage(cmd_qgroup_show_usage);
337
338         path = argv[optind];
339         fd = open_file_or_dir(path, &dirstream);
340         if (fd < 0) {
341                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
342                 return 1;
343         }
344
345         if (filter_flag) {
346                 qgroupid = btrfs_get_path_rootid(fd);
347                 if (filter_flag & 0x1)
348                         btrfs_qgroup_setup_filter(&filter_set,
349                                         BTRFS_QGROUP_FILTER_ALL_PARENT,
350                                         qgroupid);
351                 if (filter_flag & 0x2)
352                         btrfs_qgroup_setup_filter(&filter_set,
353                                         BTRFS_QGROUP_FILTER_PARENT,
354                                         qgroupid);
355         }
356         ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
357         e = errno;
358         close_file_or_dir(fd, dirstream);
359         if (ret < 0)
360                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
361                                 strerror(e));
362
363         return !!ret;
364 }
365
366 static const char * const cmd_qgroup_limit_usage[] = {
367         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
368         "Limit the size of a subvolume quota group.",
369         "",
370         "-c   limit amount of data after compression. This is the default,",
371         "     it is currently not possible to turn off this option.",
372         "-e   limit space exclusively assigned to this qgroup",
373         NULL
374 };
375
376 static int cmd_qgroup_limit(int argc, char **argv)
377 {
378         int ret = 0;
379         int fd;
380         int e;
381         char *path = NULL;
382         struct btrfs_ioctl_qgroup_limit_args args;
383         unsigned long long size;
384         int compressed = 0;
385         int exclusive = 0;
386         DIR *dirstream = NULL;
387
388         optind = 1;
389         while (1) {
390                 int c = getopt(argc, argv, "ce");
391                 if (c < 0)
392                         break;
393                 switch (c) {
394                 case 'c':
395                         compressed = 1;
396                         break;
397                 case 'e':
398                         exclusive = 1;
399                         break;
400                 default:
401                         usage(cmd_qgroup_limit_usage);
402                 }
403         }
404
405         if (check_argc_min(argc - optind, 2))
406                 usage(cmd_qgroup_limit_usage);
407
408         if (!parse_limit(argv[optind], &size)) {
409                 fprintf(stderr, "Invalid size argument given\n");
410                 return 1;
411         }
412
413         memset(&args, 0, sizeof(args));
414         if (compressed)
415                 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
416                                   BTRFS_QGROUP_LIMIT_EXCL_CMPR;
417         if (exclusive) {
418                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
419                 args.lim.max_exclusive = size;
420         } else {
421                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
422                 args.lim.max_referenced = size;
423         }
424
425         if (argc - optind == 2) {
426                 args.qgroupid = 0;
427                 path = argv[optind + 1];
428                 ret = test_issubvolume(path);
429                 if (ret < 0) {
430                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
431                         return 1;
432                 }
433                 if (!ret) {
434                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
435                                 path);
436                         return 1;
437                 }
438                 /*
439                  * keep qgroupid at 0, this indicates that the subvolume the
440                  * fd refers to is to be limited
441                  */
442         } else if (argc - optind == 3) {
443                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
444                 path = argv[optind + 2];
445         } else
446                 usage(cmd_qgroup_limit_usage);
447
448         fd = open_file_or_dir(path, &dirstream);
449         if (fd < 0) {
450                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
451                 return 1;
452         }
453
454         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
455         e = errno;
456         close_file_or_dir(fd, dirstream);
457         if (ret < 0) {
458                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
459                         "%s\n", strerror(e));
460                 return 1;
461         }
462         return 0;
463 }
464
465 const struct cmd_group qgroup_cmd_group = {
466         qgroup_cmd_group_usage, NULL, {
467                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
468                    NULL, 0 },
469                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
470                    NULL, 0 },
471                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
472                    NULL, 0 },
473                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
474                    NULL, 0 },
475                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
476                    NULL, 0 },
477                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
478                    NULL, 0 },
479                 NULL_CMD_STRUCT
480         }
481 };
482
483 int cmd_qgroup(int argc, char **argv)
484 {
485         return handle_command_group(&qgroup_cmd_group, argc, argv);
486 }