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