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