0ad99f48296d036521b214b58704b2a97be9c5f3
[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                 return 1;
353
354         if (filter_flag) {
355                 qgroupid = btrfs_get_path_rootid(fd);
356                 if (filter_flag & 0x1)
357                         btrfs_qgroup_setup_filter(&filter_set,
358                                         BTRFS_QGROUP_FILTER_ALL_PARENT,
359                                         qgroupid);
360                 if (filter_flag & 0x2)
361                         btrfs_qgroup_setup_filter(&filter_set,
362                                         BTRFS_QGROUP_FILTER_PARENT,
363                                         qgroupid);
364         }
365         ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
366         e = errno;
367         close_file_or_dir(fd, dirstream);
368         if (ret < 0)
369                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
370                                 strerror(e));
371
372         return !!ret;
373 }
374
375 static const char * const cmd_qgroup_limit_usage[] = {
376         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
377         "Set the limits a subvolume quota group.",
378         "",
379         "-c   limit amount of data after compression. This is the default,",
380         "     it is currently not possible to turn off this option.",
381         "-e   limit space exclusively assigned to this qgroup",
382         NULL
383 };
384
385 static int cmd_qgroup_limit(int argc, char **argv)
386 {
387         int ret = 0;
388         int fd;
389         int e;
390         char *path = NULL;
391         struct btrfs_ioctl_qgroup_limit_args args;
392         unsigned long long size;
393         int compressed = 0;
394         int exclusive = 0;
395         DIR *dirstream = NULL;
396
397         optind = 1;
398         while (1) {
399                 int c = getopt(argc, argv, "ce");
400                 if (c < 0)
401                         break;
402                 switch (c) {
403                 case 'c':
404                         compressed = 1;
405                         break;
406                 case 'e':
407                         exclusive = 1;
408                         break;
409                 default:
410                         usage(cmd_qgroup_limit_usage);
411                 }
412         }
413
414         if (check_argc_min(argc - optind, 2))
415                 usage(cmd_qgroup_limit_usage);
416
417         if (!parse_limit(argv[optind], &size)) {
418                 fprintf(stderr, "Invalid size argument given\n");
419                 return 1;
420         }
421
422         memset(&args, 0, sizeof(args));
423         if (compressed)
424                 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
425                                   BTRFS_QGROUP_LIMIT_EXCL_CMPR;
426         if (exclusive) {
427                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
428                 args.lim.max_exclusive = size;
429         } else {
430                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
431                 args.lim.max_referenced = size;
432         }
433
434         if (argc - optind == 2) {
435                 args.qgroupid = 0;
436                 path = argv[optind + 1];
437                 ret = test_issubvolume(path);
438                 if (ret < 0) {
439                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
440                         return 1;
441                 }
442                 if (!ret) {
443                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
444                                 path);
445                         return 1;
446                 }
447                 /*
448                  * keep qgroupid at 0, this indicates that the subvolume the
449                  * fd refers to is to be limited
450                  */
451         } else if (argc - optind == 3) {
452                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
453                 path = argv[optind + 2];
454         } else
455                 usage(cmd_qgroup_limit_usage);
456
457         fd = btrfs_open_dir(path, &dirstream, 1);
458         if (fd < 0)
459                 return 1;
460
461         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
462         e = errno;
463         close_file_or_dir(fd, dirstream);
464         if (ret < 0) {
465                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
466                         "%s\n", strerror(e));
467                 return 1;
468         }
469         return 0;
470 }
471
472 static const char qgroup_cmd_group_info[] =
473 "manage quota groups";
474
475 const struct cmd_group qgroup_cmd_group = {
476         qgroup_cmd_group_usage, qgroup_cmd_group_info, {
477                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
478                    NULL, 0 },
479                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
480                    NULL, 0 },
481                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
482                    NULL, 0 },
483                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
484                    NULL, 0 },
485                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
486                    NULL, 0 },
487                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
488                    NULL, 0 },
489                 NULL_CMD_STRUCT
490         }
491 };
492
493 int cmd_qgroup(int argc, char **argv)
494 {
495         return handle_command_group(&qgroup_cmd_group, argc, argv);
496 }