btrfs-progs: fix memory leak in cmd_qgroup_show()
[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         int rescan = 0;
41         char *path;
42         struct btrfs_ioctl_qgroup_assign_args args;
43         DIR *dirstream = NULL;
44
45         while (1) {
46                 enum { GETOPT_VAL_RESCAN = 256 };
47                 static const struct option long_options[] = {
48                         { "rescan", no_argument, NULL, GETOPT_VAL_RESCAN },
49                         { NULL, 0, NULL, 0 }
50                 };
51                 int c = getopt_long(argc, argv, "", long_options, NULL);
52
53                 if (c < 0)
54                         break;
55                 switch (c) {
56                 case GETOPT_VAL_RESCAN:
57                         rescan = 1;
58                         break;
59                 default:
60                         /* Usage printed by the caller */
61                         return -1;
62                 }
63         }
64
65         if (check_argc_exact(argc - optind, 3))
66                 return -1;
67
68         memset(&args, 0, sizeof(args));
69         args.assign = assign;
70         args.src = parse_qgroupid(argv[optind]);
71         args.dst = parse_qgroupid(argv[optind + 1]);
72
73         path = argv[optind + 2];
74
75         /*
76          * FIXME src should accept subvol path
77          */
78         if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
79                 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
80                 return 1;
81         }
82         fd = btrfs_open_dir(path, &dirstream, 1);
83         if (fd < 0)
84                 return 1;
85
86         ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
87         e = errno;
88         if (ret < 0) {
89                 fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
90                         strerror(e));
91                 close_file_or_dir(fd, dirstream);
92                 return 1;
93         }
94
95         /*
96          * If ret > 0, it means assign caused qgroup data inconsistent state.
97          * Schedule a quota rescan if requested.
98          *
99          * The return value change only happens in newer kernel. But will not
100          * cause problem since old kernel has a bug that will never clear
101          * INCONSISTENT bit.
102          */
103         if (ret > 0) {
104                 if (rescan) {
105                         struct btrfs_ioctl_quota_rescan_args args;
106
107                         printf("Quota data changed, rescan scheduled\n");
108                         memset(&args, 0, sizeof(args));
109                         ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args);
110                         if (ret < 0)
111                                 fprintf(stderr,
112                                         "ERROR: quota rescan failed: %s\n",
113                                         strerror(errno));
114                 } else {
115                         printf("WARNING: quotas may be inconsistent, rescan needed\n");
116                 }
117         }
118         close_file_or_dir(fd, dirstream);
119         return ret;
120 }
121
122 static int qgroup_create(int create, int argc, char **argv)
123 {
124         int ret = 0;
125         int fd;
126         int e;
127         char *path = argv[2];
128         struct btrfs_ioctl_qgroup_create_args args;
129         DIR *dirstream = NULL;
130
131         if (check_argc_exact(argc, 3))
132                 return -1;
133
134         memset(&args, 0, sizeof(args));
135         args.create = create;
136         args.qgroupid = parse_qgroupid(argv[1]);
137
138         fd = btrfs_open_dir(path, &dirstream, 1);
139         if (fd < 0)
140                 return 1;
141
142         ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
143         e = errno;
144         close_file_or_dir(fd, dirstream);
145         if (ret < 0) {
146                 fprintf(stderr, "ERROR: unable to %s quota group: %s\n",
147                         create ? "create":"destroy", strerror(e));
148                 return 1;
149         }
150         return 0;
151 }
152
153 static int parse_limit(const char *p, unsigned long long *s)
154 {
155         char *endptr;
156         unsigned long long size;
157         unsigned long long CLEAR_VALUE = -1;
158
159         if (strcasecmp(p, "none") == 0) {
160                 *s = CLEAR_VALUE;
161                 return 1;
162         }
163
164         if (p[0] == '-')
165                 return 0;
166
167         size = strtoull(p, &endptr, 10);
168         if (p == endptr)
169                 return 0;
170
171         switch (*endptr) {
172         case 'T':
173         case 't':
174                 size *= 1024;
175                 /* fallthrough */
176         case 'G':
177         case 'g':
178                 size *= 1024;
179                 /* fallthrough */
180         case 'M':
181         case 'm':
182                 size *= 1024;
183                 /* fallthrough */
184         case 'K':
185         case 'k':
186                 size *= 1024;
187                 ++endptr;
188                 break;
189         case 0:
190                 break;
191         default:
192                 return 0;
193         }
194
195         if (*endptr)
196                 return 0;
197
198         *s = size;
199
200         return 1;
201 }
202
203 static const char * const cmd_qgroup_assign_usage[] = {
204         "btrfs qgroup assign [options] <src> <dst> <path>",
205         "Assign SRC as the child qgroup of DST",
206         "",
207         "--rescan       schedule qutoa rescan if needed",
208         "--no-rescan    ",
209         NULL
210 };
211
212 static int cmd_qgroup_assign(int argc, char **argv)
213 {
214         int ret = qgroup_assign(1, argc, argv);
215         if (ret < 0)
216                 usage(cmd_qgroup_assign_usage);
217         return ret;
218 }
219
220 static const char * const cmd_qgroup_remove_usage[] = {
221         "btrfs qgroup remove <src> <dst> <path>",
222         "Remove a child qgroup SRC from DST.",
223         NULL
224 };
225
226 static int cmd_qgroup_remove(int argc, char **argv)
227 {
228         int ret = qgroup_assign(0, argc, argv);
229         if (ret < 0)
230                 usage(cmd_qgroup_remove_usage);
231         return ret;
232 }
233
234 static const char * const cmd_qgroup_create_usage[] = {
235         "btrfs qgroup create <qgroupid> <path>",
236         "Create a subvolume quota group.",
237         NULL
238 };
239
240 static int cmd_qgroup_create(int argc, char **argv)
241 {
242         int ret = qgroup_create(1, argc, argv);
243         if (ret < 0)
244                 usage(cmd_qgroup_create_usage);
245         return ret;
246 }
247
248 static const char * const cmd_qgroup_destroy_usage[] = {
249         "btrfs qgroup destroy <qgroupid> <path>",
250         "Destroy a quota group.",
251         NULL
252 };
253
254 static int cmd_qgroup_destroy(int argc, char **argv)
255 {
256         int ret = qgroup_create(0, argc, argv);
257         if (ret < 0)
258                 usage(cmd_qgroup_destroy_usage);
259         return ret;
260 }
261
262 static const char * const cmd_qgroup_show_usage[] = {
263         "btrfs qgroup show -pcreFf "
264         "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
265         "Show subvolume quota groups.",
266         "-p             print parent qgroup id",
267         "-c             print child qgroup id",
268         "-r             print limit of referenced size of qgroup",
269         "-e             print limit of exclusive size of qgroup",
270         "-F             list all qgroups which impact the given path",
271         "               (including ancestral qgroups)",
272         "-f             list all qgroups which impact the given path",
273         "               (excluding ancestral qgroups)",
274         HELPINFO_OUTPUT_UNIT,
275         "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
276         "               list qgroups sorted by specified items",
277         "               you can use '+' or '-' in front of each item.",
278         "               (+:ascending, -:descending, ascending default)",
279         NULL
280 };
281
282 static int cmd_qgroup_show(int argc, char **argv)
283 {
284         char *path;
285         int ret = 0;
286         int fd;
287         int e;
288         DIR *dirstream = NULL;
289         u64 qgroupid;
290         int filter_flag = 0;
291         unsigned unit_mode;
292
293         struct btrfs_qgroup_comparer_set *comparer_set;
294         struct btrfs_qgroup_filter_set *filter_set;
295         filter_set = btrfs_qgroup_alloc_filter_set();
296         comparer_set = btrfs_qgroup_alloc_comparer_set();
297
298         unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
299
300         optind = 1;
301         while (1) {
302                 int c;
303                 static const struct option long_options[] = {
304                         {"sort", required_argument, NULL, 'S'},
305                         { NULL, 0, NULL, 0 }
306                 };
307
308                 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
309                 if (c < 0)
310                         break;
311                 switch (c) {
312                 case 'p':
313                         btrfs_qgroup_setup_print_column(
314                                 BTRFS_QGROUP_PARENT);
315                         break;
316                 case 'c':
317                         btrfs_qgroup_setup_print_column(
318                                 BTRFS_QGROUP_CHILD);
319                         break;
320                 case 'r':
321                         btrfs_qgroup_setup_print_column(
322                                 BTRFS_QGROUP_MAX_RFER);
323                         break;
324                 case 'e':
325                         btrfs_qgroup_setup_print_column(
326                                 BTRFS_QGROUP_MAX_EXCL);
327                         break;
328                 case 'F':
329                         filter_flag |= 0x1;
330                         break;
331                 case 'f':
332                         filter_flag |= 0x2;
333                         break;
334                 case 'S':
335                         ret = btrfs_qgroup_parse_sort_string(optarg,
336                                                              &comparer_set);
337                         if (ret)
338                                 usage(cmd_qgroup_show_usage);
339                         break;
340                 default:
341                         usage(cmd_qgroup_show_usage);
342                 }
343         }
344         btrfs_qgroup_setup_units(unit_mode);
345
346         if (check_argc_exact(argc - optind, 1))
347                 usage(cmd_qgroup_show_usage);
348
349         path = argv[optind];
350         fd = btrfs_open_dir(path, &dirstream, 1);
351         if (fd < 0) {
352                 btrfs_qgroup_free_filter_set(filter_set);
353                 btrfs_qgroup_free_comparer_set(comparer_set);
354                 return 1;
355         }
356
357         if (filter_flag) {
358                 qgroupid = btrfs_get_path_rootid(fd);
359                 if (filter_flag & 0x1)
360                         btrfs_qgroup_setup_filter(&filter_set,
361                                         BTRFS_QGROUP_FILTER_ALL_PARENT,
362                                         qgroupid);
363                 if (filter_flag & 0x2)
364                         btrfs_qgroup_setup_filter(&filter_set,
365                                         BTRFS_QGROUP_FILTER_PARENT,
366                                         qgroupid);
367         }
368         ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
369         e = errno;
370         close_file_or_dir(fd, dirstream);
371         if (ret < 0)
372                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
373                                 strerror(e));
374
375         return !!ret;
376 }
377
378 static const char * const cmd_qgroup_limit_usage[] = {
379         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
380         "Set the limits a subvolume quota group.",
381         "",
382         "-c   limit amount of data after compression. This is the default,",
383         "     it is currently not possible to turn off this option.",
384         "-e   limit space exclusively assigned to this qgroup",
385         NULL
386 };
387
388 static int cmd_qgroup_limit(int argc, char **argv)
389 {
390         int ret = 0;
391         int fd;
392         int e;
393         char *path = NULL;
394         struct btrfs_ioctl_qgroup_limit_args args;
395         unsigned long long size;
396         int compressed = 0;
397         int exclusive = 0;
398         DIR *dirstream = NULL;
399
400         optind = 1;
401         while (1) {
402                 int c = getopt(argc, argv, "ce");
403                 if (c < 0)
404                         break;
405                 switch (c) {
406                 case 'c':
407                         compressed = 1;
408                         break;
409                 case 'e':
410                         exclusive = 1;
411                         break;
412                 default:
413                         usage(cmd_qgroup_limit_usage);
414                 }
415         }
416
417         if (check_argc_min(argc - optind, 2))
418                 usage(cmd_qgroup_limit_usage);
419
420         if (!parse_limit(argv[optind], &size)) {
421                 fprintf(stderr, "Invalid size argument given\n");
422                 return 1;
423         }
424
425         memset(&args, 0, sizeof(args));
426         if (compressed)
427                 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
428                                   BTRFS_QGROUP_LIMIT_EXCL_CMPR;
429         if (exclusive) {
430                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
431                 args.lim.max_exclusive = size;
432         } else {
433                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
434                 args.lim.max_referenced = size;
435         }
436
437         if (argc - optind == 2) {
438                 args.qgroupid = 0;
439                 path = argv[optind + 1];
440                 ret = test_issubvolume(path);
441                 if (ret < 0) {
442                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
443                         return 1;
444                 }
445                 if (!ret) {
446                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
447                                 path);
448                         return 1;
449                 }
450                 /*
451                  * keep qgroupid at 0, this indicates that the subvolume the
452                  * fd refers to is to be limited
453                  */
454         } else if (argc - optind == 3) {
455                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
456                 path = argv[optind + 2];
457         } else
458                 usage(cmd_qgroup_limit_usage);
459
460         fd = btrfs_open_dir(path, &dirstream, 1);
461         if (fd < 0)
462                 return 1;
463
464         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
465         e = errno;
466         close_file_or_dir(fd, dirstream);
467         if (ret < 0) {
468                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
469                         "%s\n", strerror(e));
470                 return 1;
471         }
472         return 0;
473 }
474
475 static const char qgroup_cmd_group_info[] =
476 "manage quota groups";
477
478 const struct cmd_group qgroup_cmd_group = {
479         qgroup_cmd_group_usage, qgroup_cmd_group_info, {
480                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
481                    NULL, 0 },
482                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
483                    NULL, 0 },
484                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
485                    NULL, 0 },
486                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
487                    NULL, 0 },
488                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
489                    NULL, 0 },
490                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
491                    NULL, 0 },
492                 NULL_CMD_STRUCT
493         }
494 };
495
496 int cmd_qgroup(int argc, char **argv)
497 {
498         return handle_command_group(&qgroup_cmd_group, argc, argv);
499 }