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