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