btrfs-progs: Fix a memleak in btrfs_scan_one_device.
[btrfs-progs-unstable/devel.git] / cmds-qgroup.c
blob957fbc956a36abe3dd88b39b3dae733d546cb157
1 /*
2 * Copyright (C) 2012 STRATO. All rights reserved.
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.
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.
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.
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21 #include <getopt.h>
23 #include "ctree.h"
24 #include "ioctl.h"
26 #include "commands.h"
27 #include "qgroup.h"
28 #include "utils.h"
30 static const char * const qgroup_cmd_group_usage[] = {
31 "btrfs qgroup <command> [options] <path>",
32 NULL
35 static int qgroup_assign(int assign, int argc, char **argv)
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;
44 if (check_argc_exact(argc, 4))
45 return -1;
47 memset(&args, 0, sizeof(args));
48 args.assign = assign;
49 args.src = parse_qgroupid(argv[1]);
50 args.dst = parse_qgroupid(argv[2]);
53 * FIXME src should accept subvol path
55 if ((args.src >> 48) >= (args.dst >> 48)) {
56 fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
57 return 1;
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;
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;
73 return 0;
76 static int qgroup_create(int create, int argc, char **argv)
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;
85 if (check_argc_exact(argc, 3))
86 return -1;
88 memset(&args, 0, sizeof(args));
89 args.create = create;
90 args.qgroupid = parse_qgroupid(argv[1]);
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;
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;
106 return 0;
109 static int parse_limit(const char *p, unsigned long long *s)
111 char *endptr;
112 unsigned long long size;
114 if (strcasecmp(p, "none") == 0) {
115 *s = 0;
116 return 1;
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;
143 if (*endptr)
144 return 0;
146 *s = size;
148 return 1;
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
157 static int cmd_qgroup_assign(int argc, char **argv)
159 int ret = qgroup_assign(1, argc, argv);
160 if (ret < 0)
161 usage(cmd_qgroup_assign_usage);
162 return ret;
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
171 static int cmd_qgroup_remove(int argc, char **argv)
173 int ret = qgroup_assign(0, argc, argv);
174 if (ret < 0)
175 usage(cmd_qgroup_remove_usage);
176 return ret;
179 static const char * const cmd_qgroup_create_usage[] = {
180 "btrfs qgroup create <qgroupid> <path>",
181 "Create a subvolume quota group.",
182 NULL
185 static int cmd_qgroup_create(int argc, char **argv)
187 int ret = qgroup_create(1, argc, argv);
188 if (ret < 0)
189 usage(cmd_qgroup_create_usage);
190 return ret;
193 static const char * const cmd_qgroup_destroy_usage[] = {
194 "btrfs qgroup destroy <qgroupid> <path>",
195 "Destroy a subvolume quota group.",
196 NULL
199 static int cmd_qgroup_destroy(int argc, char **argv)
201 int ret = qgroup_create(0, argc, argv);
202 if (ret < 0)
203 usage(cmd_qgroup_destroy_usage);
204 return ret;
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 max referenced size of qgroup",
214 "-e print max exclusive size of qgroup",
215 "-F list all qgroups which impact the given path"
216 "(include ancestral qgroups)",
217 "-f list all qgroups which impact the given path"
218 "(exclude ancestral qgroups)",
219 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
220 " list qgroups in order of qgroupid,"
221 "rfer,max_rfer or max_excl",
222 " you can use '+' or '-' in front of each item.",
223 " (+:ascending, -:descending, ascending default)",
224 NULL
227 static int cmd_qgroup_show(int argc, char **argv)
229 char *path;
230 int ret = 0;
231 int fd;
232 int e;
233 DIR *dirstream = NULL;
234 int c;
235 u64 qgroupid;
236 int filter_flag = 0;
238 struct btrfs_qgroup_comparer_set *comparer_set;
239 struct btrfs_qgroup_filter_set *filter_set;
240 filter_set = btrfs_qgroup_alloc_filter_set();
241 comparer_set = btrfs_qgroup_alloc_comparer_set();
242 struct option long_options[] = {
243 {"sort", 1, NULL, 'S'},
244 {0, 0, 0, 0}
247 optind = 1;
248 while (1) {
249 c = getopt_long(argc, argv, "pcreFf",
250 long_options, NULL);
251 if (c < 0)
252 break;
253 switch (c) {
254 case 'p':
255 btrfs_qgroup_setup_print_column(
256 BTRFS_QGROUP_PARENT);
257 break;
258 case 'c':
259 btrfs_qgroup_setup_print_column(
260 BTRFS_QGROUP_CHILD);
261 break;
262 case 'r':
263 btrfs_qgroup_setup_print_column(
264 BTRFS_QGROUP_MAX_RFER);
265 break;
266 case 'e':
267 btrfs_qgroup_setup_print_column(
268 BTRFS_QGROUP_MAX_EXCL);
269 break;
270 case 'F':
271 filter_flag |= 0x1;
272 break;
273 case 'f':
274 filter_flag |= 0x2;
275 break;
276 case 'S':
277 ret = btrfs_qgroup_parse_sort_string(optarg,
278 &comparer_set);
279 if (ret)
280 usage(cmd_qgroup_show_usage);
281 break;
282 default:
283 usage(cmd_qgroup_show_usage);
286 if (check_argc_exact(argc - optind, 1))
287 usage(cmd_qgroup_show_usage);
289 path = argv[optind];
290 fd = open_file_or_dir(path, &dirstream);
291 if (fd < 0) {
292 fprintf(stderr, "ERROR: can't access '%s'\n", path);
293 return 1;
296 if (filter_flag) {
297 qgroupid = btrfs_get_path_rootid(fd);
298 if (filter_flag & 0x1)
299 btrfs_qgroup_setup_filter(&filter_set,
300 BTRFS_QGROUP_FILTER_ALL_PARENT,
301 qgroupid);
302 if (filter_flag & 0x2)
303 btrfs_qgroup_setup_filter(&filter_set,
304 BTRFS_QGROUP_FILTER_PARENT,
305 qgroupid);
307 ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
308 e = errno;
309 close_file_or_dir(fd, dirstream);
310 if (ret < 0)
311 fprintf(stderr, "ERROR: can't list qgroups: %s\n",
312 strerror(e));
314 return !!ret;
317 static const char * const cmd_qgroup_limit_usage[] = {
318 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
319 "Limit the size of a subvolume quota group.",
321 "-c limit amount of data after compression. This is the default,",
322 " it is currently not possible to turn off this option.",
323 "-e limit space exclusively assigned to this qgroup",
324 NULL
327 static int cmd_qgroup_limit(int argc, char **argv)
329 int ret = 0;
330 int fd;
331 int e;
332 char *path = NULL;
333 struct btrfs_ioctl_qgroup_limit_args args;
334 unsigned long long size;
335 int compressed = 0;
336 int exclusive = 0;
337 DIR *dirstream = NULL;
339 optind = 1;
340 while (1) {
341 int c = getopt(argc, argv, "ce");
342 if (c < 0)
343 break;
344 switch (c) {
345 case 'c':
346 compressed = 1;
347 break;
348 case 'e':
349 exclusive = 1;
350 break;
351 default:
352 usage(cmd_qgroup_limit_usage);
356 if (check_argc_min(argc - optind, 2))
357 usage(cmd_qgroup_limit_usage);
359 if (!parse_limit(argv[optind], &size)) {
360 fprintf(stderr, "Invalid size argument given\n");
361 return 1;
364 memset(&args, 0, sizeof(args));
365 if (size) {
366 if (compressed)
367 args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
368 BTRFS_QGROUP_LIMIT_EXCL_CMPR;
369 if (exclusive) {
370 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
371 args.lim.max_exclusive = size;
372 } else {
373 args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
374 args.lim.max_referenced = size;
378 if (argc - optind == 2) {
379 args.qgroupid = 0;
380 path = argv[optind + 1];
381 ret = test_issubvolume(path);
382 if (ret < 0) {
383 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
384 return 1;
386 if (!ret) {
387 fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
388 path);
389 return 1;
392 * keep qgroupid at 0, this indicates that the subvolume the
393 * fd refers to is to be limited
395 } else if (argc - optind == 3) {
396 args.qgroupid = parse_qgroupid(argv[optind + 1]);
397 path = argv[optind + 2];
398 } else
399 usage(cmd_qgroup_limit_usage);
401 fd = open_file_or_dir(path, &dirstream);
402 if (fd < 0) {
403 fprintf(stderr, "ERROR: can't access '%s'\n", path);
404 return 1;
407 ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
408 e = errno;
409 close_file_or_dir(fd, dirstream);
410 if (ret < 0) {
411 fprintf(stderr, "ERROR: unable to limit requested quota group: "
412 "%s\n", strerror(e));
413 return 1;
415 return 0;
418 const struct cmd_group qgroup_cmd_group = {
419 qgroup_cmd_group_usage, NULL, {
420 { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
421 NULL, 0 },
422 { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
423 NULL, 0 },
424 { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
425 NULL, 0 },
426 { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
427 NULL, 0 },
428 { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
429 NULL, 0 },
430 { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
431 NULL, 0 },
432 NULL_CMD_STRUCT
436 int cmd_qgroup(int argc, char **argv)
438 return handle_command_group(&qgroup_cmd_group, argc, argv);