891d46cc31cec5769a36d3b66f7850c336f57282
[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 12;
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 12;
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 30;
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 12;
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 30;
105         }
106         return 0;
107 }
108
109 static void print_qgroup_info(u64 objectid, struct btrfs_qgroup_info_item *info)
110 {
111         printf("%llu/%llu %lld %lld\n", objectid >> 48,
112                 objectid & ((1ll << 48) - 1),
113                 btrfs_stack_qgroup_info_referenced(info),
114                 btrfs_stack_qgroup_info_exclusive(info));
115 }
116
117 static int list_qgroups(int fd)
118 {
119         int ret;
120         struct btrfs_ioctl_search_args args;
121         struct btrfs_ioctl_search_key *sk = &args.key;
122         struct btrfs_ioctl_search_header *sh;
123         unsigned long off = 0;
124         unsigned int i;
125         struct btrfs_qgroup_info_item *info;
126
127         memset(&args, 0, sizeof(args));
128
129         /* search in the quota tree */
130         sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
131
132         /*
133          * set the min and max to backref keys.  The search will
134          * only send back this type of key now.
135          */
136         sk->max_type = BTRFS_QGROUP_INFO_KEY;
137         sk->min_type = BTRFS_QGROUP_INFO_KEY;
138         sk->max_objectid = 0;
139         sk->max_offset = (u64)-1;
140         sk->max_transid = (u64)-1;
141
142         /* just a big number, doesn't matter much */
143         sk->nr_items = 4096;
144
145         while (1) {
146                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
147                 if (ret < 0)
148                         return ret;
149
150                 /* the ioctl returns the number of item it found in nr_items */
151                 if (sk->nr_items == 0)
152                         break;
153
154                 off = 0;
155
156                 /*
157                  * for each item, pull the key out of the header and then
158                  * read the root_ref item it contains
159                  */
160                 for (i = 0; i < sk->nr_items; i++) {
161                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
162                                                                   off);
163                         off += sizeof(*sh);
164
165                         if (sh->objectid != 0)
166                                 goto done;
167
168                         if (sh->type != BTRFS_QGROUP_INFO_KEY)
169                                 goto done;
170
171                         info = (struct btrfs_qgroup_info_item *)
172                                         (args.buf + off);
173                         print_qgroup_info(sh->offset, info);
174
175                         off += sh->len;
176
177                         /*
178                          * record the mins in sk so we can make sure the
179                          * next search doesn't repeat this root
180                          */
181                         sk->min_offset = sh->offset;
182                 }
183                 sk->nr_items = 4096;
184                 /*
185                  * this iteration is done, step forward one qgroup for the next
186                  * ioctl
187                  */
188                 if (sk->min_offset < (u64)-1)
189                         sk->min_offset++;
190                 else
191                         break;
192         }
193
194 done:
195         return ret;
196 }
197
198 static int parse_limit(const char *p, unsigned long long *s)
199 {
200         char *endptr;
201         unsigned long long size;
202
203         if (strcasecmp(p, "none") == 0) {
204                 *s = 0;
205                 return 1;
206         }
207         size = strtoull(p, &endptr, 10);
208         switch (*endptr) {
209         case 'T':
210         case 't':
211                 size *= 1024;
212         case 'G':
213         case 'g':
214                 size *= 1024;
215         case 'M':
216         case 'm':
217                 size *= 1024;
218         case 'K':
219         case 'k':
220                 size *= 1024;
221                 ++endptr;
222                 break;
223         case 0:
224                 break;
225         default:
226                 return 0;
227         }
228
229         if (*endptr)
230                 return 0;
231
232         *s = size;
233
234         return 1;
235 }
236
237 static const char * const cmd_qgroup_assign_usage[] = {
238         "btrfs qgroup assign <src> <dst> <path>",
239         "Enable subvolume qgroup support for a filesystem.",
240         NULL
241 };
242
243 static int cmd_qgroup_assign(int argc, char **argv)
244 {
245         int ret = qgroup_assign(1, argc, argv);
246         if (ret < 0)
247                 usage(cmd_qgroup_assign_usage);
248         return ret;
249 }
250
251 static const char * const cmd_qgroup_remove_usage[] = {
252         "btrfs qgroup remove <src> <dst> <path>",
253         "Remove a subvol from a quota group.",
254         NULL
255 };
256
257 static int cmd_qgroup_remove(int argc, char **argv)
258 {
259         int ret = qgroup_assign(0, argc, argv);
260         if (ret < 0)
261                 usage(cmd_qgroup_remove_usage);
262         return ret;
263 }
264
265 static const char * const cmd_qgroup_create_usage[] = {
266         "btrfs qgroup create <qgroupid> <path>",
267         "Create a subvolume quota group.",
268         NULL
269 };
270
271 static int cmd_qgroup_create(int argc, char **argv)
272 {
273         int ret = qgroup_create(1, argc, argv);
274         if (ret < 0)
275                 usage(cmd_qgroup_create_usage);
276         return ret;
277 }
278
279 static const char * const cmd_qgroup_destroy_usage[] = {
280         "btrfs qgroup destroy <qgroupid> <path>",
281         "Destroy a subvolume quota group.",
282         NULL
283 };
284
285 static int cmd_qgroup_destroy(int argc, char **argv)
286 {
287         int ret = qgroup_create(0, argc, argv);
288         if (ret < 0)
289                 usage(cmd_qgroup_destroy_usage);
290         return ret;
291 }
292
293 static const char * const cmd_qgroup_show_usage[] = {
294         "btrfs qgroup show <path>",
295         "Show all subvolume quota groups.",
296         NULL
297 };
298
299 static int cmd_qgroup_show(int argc, char **argv)
300 {
301         int ret = 0;
302         int fd;
303         int e;
304         char *path = argv[1];
305         DIR *dirstream = NULL;
306
307         if (check_argc_exact(argc, 2))
308                 usage(cmd_qgroup_show_usage);
309
310         fd = open_file_or_dir(path, &dirstream);
311         if (fd < 0) {
312                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
313                 return 12;
314         }
315
316         ret = list_qgroups(fd);
317         e = errno;
318         close_file_or_dir(fd, dirstream);
319         if (ret < 0) {
320                 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
321                                 strerror(e));
322                 return 30;
323         }
324
325         return ret;
326 }
327
328 static const char * const cmd_qgroup_limit_usage[] = {
329         "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
330         "Limit the size of a subvolume quota group.",
331         "",
332         "-c   limit amount of data after compression. This is the default,",
333         "     it is currently not possible to turn off this option.",
334         "-e   limit space exclusively assigned to this qgroup",
335         NULL
336 };
337
338 static int cmd_qgroup_limit(int argc, char **argv)
339 {
340         int ret = 0;
341         int fd;
342         int e;
343         char *path = NULL;
344         struct btrfs_ioctl_qgroup_limit_args args;
345         unsigned long long size;
346         int compressed = 0;
347         int exclusive = 0;
348         DIR *dirstream = NULL;
349
350         optind = 1;
351         while (1) {
352                 int c = getopt(argc, argv, "ce");
353                 if (c < 0)
354                         break;
355                 switch (c) {
356                 case 'c':
357                         compressed = 1;
358                         break;
359                 case 'e':
360                         exclusive = 1;
361                         break;
362                 default:
363                         usage(cmd_qgroup_limit_usage);
364                 }
365         }
366
367         if (check_argc_min(argc - optind, 2))
368                 usage(cmd_qgroup_limit_usage);
369
370         if (!parse_limit(argv[optind], &size)) {
371                 fprintf(stderr, "Invalid size argument given\n");
372                 return 1;
373         }
374
375         memset(&args, 0, sizeof(args));
376         if (size) {
377                 if (compressed)
378                         args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
379                                           BTRFS_QGROUP_LIMIT_EXCL_CMPR;
380                 if (exclusive) {
381                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
382                         args.lim.max_exclusive = size;
383                 } else {
384                         args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
385                         args.lim.max_referenced = size;
386                 }
387         }
388
389         if (argc - optind == 2) {
390                 args.qgroupid = 0;
391                 path = argv[optind + 1];
392                 ret = test_issubvolume(path);
393                 if (ret < 0) {
394                         fprintf(stderr, "ERROR: error accessing '%s'\n", path);
395                         return 12;
396                 }
397                 if (!ret) {
398                         fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
399                                 path);
400                         return 13;
401                 }
402                 /*
403                  * keep qgroupid at 0, this indicates that the subvolume the
404                  * fd refers to is to be limited
405                  */
406         } else if (argc - optind == 3) {
407                 args.qgroupid = parse_qgroupid(argv[optind + 1]);
408                 path = argv[optind + 2];
409         } else
410                 usage(cmd_qgroup_limit_usage);
411
412         fd = open_file_or_dir(path, &dirstream);
413         if (fd < 0) {
414                 fprintf(stderr, "ERROR: can't access '%s'\n", path);
415                 return 12;
416         }
417
418         ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
419         e = errno;
420         close_file_or_dir(fd, dirstream);
421         if (ret < 0) {
422                 fprintf(stderr, "ERROR: unable to limit requested quota group: "
423                         "%s\n", strerror(e));
424                 return 30;
425         }
426         return 0;
427 }
428
429 const struct cmd_group qgroup_cmd_group = {
430         qgroup_cmd_group_usage, NULL, {
431                 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage, 0, 0 },
432                 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage, 0, 0 },
433                 { "create", cmd_qgroup_create, cmd_qgroup_create_usage, 0, 0 },
434                 { "destroy", cmd_qgroup_destroy,
435                   cmd_qgroup_destroy_usage, 0, 0 },
436                 { "show", cmd_qgroup_show, cmd_qgroup_show_usage, 0, 0 },
437                 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage, 0, 0 },
438                 { 0, 0, 0, 0, 0 }
439         }
440 };
441
442 int cmd_qgroup(int argc, char **argv)
443 {
444         return handle_command_group(&qgroup_cmd_group, argc, argv);
445 }