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