btrfs-progs: mkfs: print the new UUID
[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 _cmd_qgroup_assign(int assign, int argc, char **argv,
36                 const char * const *usage_str)
37 {
38         int ret = 0;
39         int fd;
40         int rescan = 0;
41         char *path;
42         struct btrfs_ioctl_qgroup_assign_args args;
43         DIR *dirstream = NULL;
44
45         if (assign) {
46                 while (1) {
47                         enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
48                         static const struct option long_options[] = {
49                                 { "rescan", no_argument, NULL,
50                                         GETOPT_VAL_RESCAN },
51                                 { "no-rescan", no_argument, NULL,
52                                         GETOPT_VAL_NO_RESCAN },
53                                 { NULL, 0, NULL, 0 }
54                         };
55                         int c = getopt_long(argc, argv, "", long_options, NULL);
56
57                         if (c < 0)
58                                 break;
59                         switch (c) {
60                         case GETOPT_VAL_RESCAN:
61                                 rescan = 1;
62                                 break;
63                         case GETOPT_VAL_NO_RESCAN:
64                                 rescan = 0;
65                                 break;
66                         default:
67                                 /* Usage printed by the caller */
68                                 return -1;
69                         }
70                 }
71         } else {
72                 clean_args_no_options(argc, argv, usage_str);
73         }
74
75         if (check_argc_exact(argc - optind, 3))
76                 usage(usage_str);
77
78         memset(&args, 0, sizeof(args));
79         args.assign = assign;
80         args.src = parse_qgroupid(argv[optind]);
81         args.dst = parse_qgroupid(argv[optind + 1]);
82
83         path = argv[optind + 2];
84
85         /*
86          * FIXME src should accept subvol path
87          */
88         if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
89                 error("bad relation requested: %s", path);
90                 return 1;
91         }
92         fd = btrfs_open_dir(path, &dirstream, 1);
93         if (fd < 0)
94                 return 1;
95
96         ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
97         if (ret < 0) {
98                 error("unable to assign quota group: %s", strerror(errno));
99                 close_file_or_dir(fd, dirstream);
100                 return 1;
101         }
102
103         /*
104          * If ret > 0, it means assign caused qgroup data inconsistent state.
105          * Schedule a quota rescan if requested.
106          *
107          * The return value change only happens in newer kernel. But will not
108          * cause problem since old kernel has a bug that will never clear
109          * INCONSISTENT bit.
110          */
111         if (ret > 0) {
112                 if (rescan) {
113                         struct btrfs_ioctl_quota_rescan_args qargs;
114
115                         printf("Quota data changed, rescan scheduled\n");
116                         memset(&qargs, 0, sizeof(qargs));
117                         ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
118                         if (ret < 0)
119                                 error("quota rescan failed: %s",
120                                         strerror(errno));
121                 } else {
122                         warning("quotas may be inconsistent, rescan needed");
123                 }
124         }
125         close_file_or_dir(fd, dirstream);
126         return ret;
127 }
128
129 static int _cmd_qgroup_create(int create, int argc, char **argv)
130 {
131         int ret = 0;
132         int fd;
133         int e;
134         char *path;
135         struct btrfs_ioctl_qgroup_create_args args;
136         DIR *dirstream = NULL;
137
138         if (check_argc_exact(argc - optind, 2))
139                 return -1;
140
141         memset(&args, 0, sizeof(args));
142         args.create = create;
143         args.qgroupid = parse_qgroupid(argv[optind]);
144         path = argv[optind + 1];
145
146         fd = btrfs_open_dir(path, &dirstream, 1);
147         if (fd < 0)
148                 return 1;
149
150         ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
151         e = errno;
152         close_file_or_dir(fd, dirstream);
153         if (ret < 0) {
154                 error("unable to %s quota group: %s",
155                         create ? "create":"destroy", strerror(e));
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 -pcreFf "
276         "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
277         "Show subvolume quota groups.",
278         "-p             print parent qgroup id",
279         "-c             print child qgroup id",
280         "-r             print limit of referenced size of qgroup",
281         "-e             print limit of exclusive size of qgroup",
282         "-F             list all qgroups which impact the given path",
283         "               (including ancestral qgroups)",
284         "-f             list all qgroups which impact the given path",
285         "               (excluding ancestral qgroups)",
286         HELPINFO_UNITS_LONG,
287         "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
288         "               list qgroups sorted by specified items",
289         "               you can use '+' or '-' in front of each item.",
290         "               (+:ascending, -:descending, ascending default)",
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         int e;
300         DIR *dirstream = NULL;
301         u64 qgroupid;
302         int filter_flag = 0;
303         unsigned unit_mode;
304
305         struct btrfs_qgroup_comparer_set *comparer_set;
306         struct btrfs_qgroup_filter_set *filter_set;
307         filter_set = btrfs_qgroup_alloc_filter_set();
308         comparer_set = btrfs_qgroup_alloc_comparer_set();
309
310         unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
311
312         while (1) {
313                 int c;
314                 static const struct option long_options[] = {
315                         {"sort", required_argument, NULL, 'S'},
316                         { NULL, 0, NULL, 0 }
317                 };
318
319                 c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
320                 if (c < 0)
321                         break;
322                 switch (c) {
323                 case 'p':
324                         btrfs_qgroup_setup_print_column(
325                                 BTRFS_QGROUP_PARENT);
326                         break;
327                 case 'c':
328                         btrfs_qgroup_setup_print_column(
329                                 BTRFS_QGROUP_CHILD);
330                         break;
331                 case 'r':
332                         btrfs_qgroup_setup_print_column(
333                                 BTRFS_QGROUP_MAX_RFER);
334                         break;
335                 case 'e':
336                         btrfs_qgroup_setup_print_column(
337                                 BTRFS_QGROUP_MAX_EXCL);
338                         break;
339                 case 'F':
340                         filter_flag |= 0x1;
341                         break;
342                 case 'f':
343                         filter_flag |= 0x2;
344                         break;
345                 case 'S':
346                         ret = btrfs_qgroup_parse_sort_string(optarg,
347                                                              &comparer_set);
348                         if (ret)
349                                 usage(cmd_qgroup_show_usage);
350                         break;
351                 default:
352                         usage(cmd_qgroup_show_usage);
353                 }
354         }
355         btrfs_qgroup_setup_units(unit_mode);
356
357         if (check_argc_exact(argc - optind, 1))
358                 usage(cmd_qgroup_show_usage);
359
360         path = argv[optind];
361         fd = btrfs_open_dir(path, &dirstream, 1);
362         if (fd < 0) {
363                 free(filter_set);
364                 free(comparer_set);
365                 return 1;
366         }
367
368         if (filter_flag) {
369                 ret = lookup_path_rootid(fd, &qgroupid);
370                 if (ret < 0) {
371                         error("cannot resolve rootid for %s: %s",
372                                         path, strerror(-ret));
373                         close_file_or_dir(fd, dirstream);
374                         goto out;
375                 }
376                 if (filter_flag & 0x1)
377                         btrfs_qgroup_setup_filter(&filter_set,
378                                         BTRFS_QGROUP_FILTER_ALL_PARENT,
379                                         qgroupid);
380                 if (filter_flag & 0x2)
381                         btrfs_qgroup_setup_filter(&filter_set,
382                                         BTRFS_QGROUP_FILTER_PARENT,
383                                         qgroupid);
384         }
385         ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
386         e = errno;
387         close_file_or_dir(fd, dirstream);
388         if (ret < 0)
389                 error("can't list qgroups: %s", strerror(e));
390
391 out:
392         return !!ret;
393 }
394
395 static const char * const cmd_qgroup_limit_usage[] = {
396         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
397         "Set the limits a subvolume quota group.",
398         "",
399         "-c   limit amount of data after compression. This is the default,",
400         "     it is currently not possible to turn off this option.",
401         "-e   limit space exclusively assigned to this qgroup",
402         NULL
403 };
404
405 static int cmd_qgroup_limit(int argc, char **argv)
406 {
407         int ret = 0;
408         int fd;
409         int e;
410         char *path = NULL;
411         struct btrfs_ioctl_qgroup_limit_args args;
412         unsigned long long size;
413         int compressed = 0;
414         int exclusive = 0;
415         DIR *dirstream = NULL;
416
417         while (1) {
418                 int c = getopt(argc, argv, "ce");
419                 if (c < 0)
420                         break;
421                 switch (c) {
422                 case 'c':
423                         compressed = 1;
424                         break;
425                 case 'e':
426                         exclusive = 1;
427                         break;
428                 default:
429                         usage(cmd_qgroup_limit_usage);
430                 }
431         }
432
433         if (check_argc_min(argc - optind, 2))
434                 usage(cmd_qgroup_limit_usage);
435
436         if (!parse_limit(argv[optind], &size)) {
437                 error("invalid size argument: %s", argv[optind]);
438                 return 1;
439         }
440
441         memset(&args, 0, sizeof(args));
442         if (compressed)
443                 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
444                                   BTRFS_QGROUP_LIMIT_EXCL_CMPR;
445         if (exclusive) {
446                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
447                 args.lim.max_exclusive = size;
448         } else {
449                 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
450                 args.lim.max_referenced = size;
451         }
452
453         if (argc - optind == 2) {
454                 args.qgroupid = 0;
455                 path = argv[optind + 1];
456                 ret = test_issubvolume(path);
457                 if (ret < 0) {
458                         error("cannot access '%s': %s", path, strerror(-ret));
459                         return 1;
460                 }
461                 if (!ret) {
462                         error("'%s' is not a subvolume", path);
463                         return 1;
464                 }
465                 /*
466                  * keep qgroupid at 0, this indicates that the subvolume the
467                  * fd refers to is to be limited
468                  */
469         } else if (argc - optind == 3) {
470                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
471                 path = argv[optind + 2];
472         } else
473                 usage(cmd_qgroup_limit_usage);
474
475         fd = btrfs_open_dir(path, &dirstream, 1);
476         if (fd < 0)
477                 return 1;
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                 error("unable to limit requested quota group: %s", strerror(e));
484                 return 1;
485         }
486         return 0;
487 }
488
489 static const char qgroup_cmd_group_info[] =
490 "manage quota groups";
491
492 const struct cmd_group qgroup_cmd_group = {
493         qgroup_cmd_group_usage, qgroup_cmd_group_info, {
494                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
495                    NULL, 0 },
496                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
497                    NULL, 0 },
498                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
499                    NULL, 0 },
500                 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
501                    NULL, 0 },
502                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
503                    NULL, 0 },
504                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
505                    NULL, 0 },
506                 NULL_CMD_STRUCT
507         }
508 };
509
510 int cmd_qgroup(int argc, char **argv)
511 {
512         return handle_command_group(&qgroup_cmd_group, argc, argv);
513 }