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