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