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>
31 static const char * const qgroup_cmd_group_usage
[] = {
32 "btrfs qgroup <command> [options] <path>",
36 static int _cmd_qgroup_assign(int assign
, int argc
, char **argv
,
37 const char * const *usage_str
)
43 struct btrfs_ioctl_qgroup_assign_args args
;
44 DIR *dirstream
= NULL
;
48 enum { GETOPT_VAL_RESCAN
= 256, GETOPT_VAL_NO_RESCAN
};
49 static const struct option long_options
[] = {
50 { "rescan", no_argument
, NULL
,
52 { "no-rescan", no_argument
, NULL
,
53 GETOPT_VAL_NO_RESCAN
},
56 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
61 case GETOPT_VAL_RESCAN
:
64 case GETOPT_VAL_NO_RESCAN
:
68 /* Usage printed by the caller */
73 clean_args_no_options(argc
, argv
, usage_str
);
76 if (check_argc_exact(argc
- optind
, 3))
79 memset(&args
, 0, sizeof(args
));
81 args
.src
= parse_qgroupid(argv
[optind
]);
82 args
.dst
= parse_qgroupid(argv
[optind
+ 1]);
84 path
= argv
[optind
+ 2];
87 * FIXME src should accept subvol path
89 if (btrfs_qgroup_level(args
.src
) >= btrfs_qgroup_level(args
.dst
)) {
90 error("bad relation requested: %s", path
);
93 fd
= btrfs_open_dir(path
, &dirstream
, 1);
97 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
99 error("unable to assign quota group: %s", strerror(errno
));
100 close_file_or_dir(fd
, dirstream
);
105 * If ret > 0, it means assign caused qgroup data inconsistent state.
106 * Schedule a quota rescan if requested.
108 * The return value change only happens in newer kernel. But will not
109 * cause problem since old kernel has a bug that will never clear
114 struct btrfs_ioctl_quota_rescan_args qargs
;
116 printf("Quota data changed, rescan scheduled\n");
117 memset(&qargs
, 0, sizeof(qargs
));
118 ret
= ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &qargs
);
120 error("quota rescan failed: %s",
123 warning("quotas may be inconsistent, rescan needed");
126 close_file_or_dir(fd
, dirstream
);
130 static int _cmd_qgroup_create(int create
, int argc
, char **argv
)
136 struct btrfs_ioctl_qgroup_create_args args
;
137 DIR *dirstream
= NULL
;
139 if (check_argc_exact(argc
- optind
, 2))
142 memset(&args
, 0, sizeof(args
));
143 args
.create
= create
;
144 args
.qgroupid
= parse_qgroupid(argv
[optind
]);
145 path
= argv
[optind
+ 1];
147 fd
= btrfs_open_dir(path
, &dirstream
, 1);
151 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
);
153 close_file_or_dir(fd
, dirstream
);
155 error("unable to %s quota group: %s",
156 create
? "create":"destroy", strerror(e
));
162 static int parse_limit(const char *p
, unsigned long long *s
)
165 unsigned long long size
;
166 unsigned long long CLEAR_VALUE
= -1;
168 if (strcasecmp(p
, "none") == 0) {
176 size
= strtoull(p
, &endptr
, 10);
212 static const char * const cmd_qgroup_assign_usage
[] = {
213 "btrfs qgroup assign [options] <src> <dst> <path>",
214 "Assign SRC as the child qgroup of DST",
216 "--rescan schedule qutoa rescan if needed",
217 "--no-rescan don't schedule quota rescan",
221 static int cmd_qgroup_assign(int argc
, char **argv
)
223 return _cmd_qgroup_assign(1, argc
, argv
, cmd_qgroup_assign_usage
);
226 static const char * const cmd_qgroup_remove_usage
[] = {
227 "btrfs qgroup remove <src> <dst> <path>",
228 "Remove a child qgroup SRC from DST.",
232 static int cmd_qgroup_remove(int argc
, char **argv
)
234 return _cmd_qgroup_assign(0, argc
, argv
, cmd_qgroup_remove_usage
);
237 static const char * const cmd_qgroup_create_usage
[] = {
238 "btrfs qgroup create <qgroupid> <path>",
239 "Create a subvolume quota group.",
243 static int cmd_qgroup_create(int argc
, char **argv
)
247 clean_args_no_options(argc
, argv
, cmd_qgroup_create_usage
);
249 ret
= _cmd_qgroup_create(1, argc
, argv
);
252 usage(cmd_qgroup_create_usage
);
256 static const char * const cmd_qgroup_destroy_usage
[] = {
257 "btrfs qgroup destroy <qgroupid> <path>",
258 "Destroy a quota group.",
262 static int cmd_qgroup_destroy(int argc
, char **argv
)
266 clean_args_no_options(argc
, argv
, cmd_qgroup_destroy_usage
);
268 ret
= _cmd_qgroup_create(0, argc
, argv
);
271 usage(cmd_qgroup_destroy_usage
);
275 static const char * const cmd_qgroup_show_usage
[] = {
276 "btrfs qgroup show [options] <path>",
277 "Show subvolume quota groups.",
278 "-p print parent qgroup id",
279 "-c print child qgroup id",
280 "-r print limit of referenced size of qgroup",
281 "-e print limit of exclusive size of qgroup",
282 "-F list all qgroups which impact the given path",
283 " (including ancestral qgroups)",
284 "-f list all qgroups which impact the given path",
285 " (excluding ancestral qgroups)",
287 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
288 " list qgroups sorted by specified items",
289 " you can use '+' or '-' in front of each item.",
290 " (+:ascending, -:descending, ascending default)",
291 "--sync force sync of the filesystem before getting info",
295 static int cmd_qgroup_show(int argc
, char **argv
)
300 DIR *dirstream
= NULL
;
306 struct btrfs_qgroup_comparer_set
*comparer_set
;
307 struct btrfs_qgroup_filter_set
*filter_set
;
308 filter_set
= btrfs_qgroup_alloc_filter_set();
309 comparer_set
= btrfs_qgroup_alloc_comparer_set();
311 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 0);
316 GETOPT_VAL_SORT
= 256,
319 static const struct option long_options
[] = {
320 {"sort", required_argument
, NULL
, GETOPT_VAL_SORT
},
321 {"sync", no_argument
, NULL
, GETOPT_VAL_SYNC
},
325 c
= getopt_long(argc
, argv
, "pcreFf", long_options
, NULL
);
330 btrfs_qgroup_setup_print_column(
331 BTRFS_QGROUP_PARENT
);
334 btrfs_qgroup_setup_print_column(
338 btrfs_qgroup_setup_print_column(
339 BTRFS_QGROUP_MAX_RFER
);
342 btrfs_qgroup_setup_print_column(
343 BTRFS_QGROUP_MAX_EXCL
);
351 case GETOPT_VAL_SORT
:
352 ret
= btrfs_qgroup_parse_sort_string(optarg
,
355 usage(cmd_qgroup_show_usage
);
357 case GETOPT_VAL_SYNC
:
361 usage(cmd_qgroup_show_usage
);
364 btrfs_qgroup_setup_units(unit_mode
);
366 if (check_argc_exact(argc
- optind
, 1))
367 usage(cmd_qgroup_show_usage
);
370 fd
= btrfs_open_dir(path
, &dirstream
, 1);
378 ret
= ioctl(fd
, BTRFS_IOC_SYNC
);
380 warning("sync ioctl failed on '%s': %s", path
,
385 ret
= lookup_path_rootid(fd
, &qgroupid
);
387 error("cannot resolve rootid for %s: %s",
388 path
, strerror(-ret
));
389 close_file_or_dir(fd
, dirstream
);
392 if (filter_flag
& 0x1)
393 btrfs_qgroup_setup_filter(&filter_set
,
394 BTRFS_QGROUP_FILTER_ALL_PARENT
,
396 if (filter_flag
& 0x2)
397 btrfs_qgroup_setup_filter(&filter_set
,
398 BTRFS_QGROUP_FILTER_PARENT
,
401 ret
= btrfs_show_qgroups(fd
, filter_set
, comparer_set
);
403 error("can't list qgroups: quotas not enabled");
405 error("can't list qgroups: %s", strerror(-ret
));
406 close_file_or_dir(fd
, dirstream
);
412 static const char * const cmd_qgroup_limit_usage
[] = {
413 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
414 "Set the limits a subvolume quota group.",
416 "-c limit amount of data after compression. This is the default,",
417 " it is currently not possible to turn off this option.",
418 "-e limit space exclusively assigned to this qgroup",
422 static int cmd_qgroup_limit(int argc
, char **argv
)
428 struct btrfs_ioctl_qgroup_limit_args args
;
429 unsigned long long size
;
432 DIR *dirstream
= NULL
;
435 int c
= getopt(argc
, argv
, "ce");
446 usage(cmd_qgroup_limit_usage
);
450 if (check_argc_min(argc
- optind
, 2))
451 usage(cmd_qgroup_limit_usage
);
453 if (!parse_limit(argv
[optind
], &size
)) {
454 error("invalid size argument: %s", argv
[optind
]);
458 memset(&args
, 0, sizeof(args
));
460 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_RFER_CMPR
|
461 BTRFS_QGROUP_LIMIT_EXCL_CMPR
;
463 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_MAX_EXCL
;
464 args
.lim
.max_exclusive
= size
;
466 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_MAX_RFER
;
467 args
.lim
.max_referenced
= size
;
470 if (argc
- optind
== 2) {
472 path
= argv
[optind
+ 1];
473 ret
= test_issubvolume(path
);
475 error("cannot access '%s': %s", path
, strerror(-ret
));
479 error("'%s' is not a subvolume", path
);
483 * keep qgroupid at 0, this indicates that the subvolume the
484 * fd refers to is to be limited
486 } else if (argc
- optind
== 3) {
487 args
.qgroupid
= parse_qgroupid(argv
[optind
+ 1]);
488 path
= argv
[optind
+ 2];
490 usage(cmd_qgroup_limit_usage
);
492 fd
= btrfs_open_dir(path
, &dirstream
, 1);
496 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
);
498 close_file_or_dir(fd
, dirstream
);
500 error("unable to limit requested quota group: %s", strerror(e
));
506 static const char qgroup_cmd_group_info
[] =
507 "manage quota groups";
509 const struct cmd_group qgroup_cmd_group
= {
510 qgroup_cmd_group_usage
, qgroup_cmd_group_info
, {
511 { "assign", cmd_qgroup_assign
, cmd_qgroup_assign_usage
,
513 { "remove", cmd_qgroup_remove
, cmd_qgroup_remove_usage
,
515 { "create", cmd_qgroup_create
, cmd_qgroup_create_usage
,
517 { "destroy", cmd_qgroup_destroy
, cmd_qgroup_destroy_usage
,
519 { "show", cmd_qgroup_show
, cmd_qgroup_show_usage
,
521 { "limit", cmd_qgroup_limit
, cmd_qgroup_limit_usage
,
527 int cmd_qgroup(int argc
, char **argv
)
529 return handle_command_group(&qgroup_cmd_group
, argc
, argv
);