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>
30 static const char * const qgroup_cmd_group_usage
[] = {
31 "btrfs qgroup <command> [options] <path>",
35 static int qgroup_assign(int assign
, int argc
, char **argv
)
42 struct btrfs_ioctl_qgroup_assign_args args
;
43 DIR *dirstream
= NULL
;
46 enum { GETOPT_VAL_RESCAN
= 256 };
47 static const struct option long_options
[] = {
48 { "rescan", no_argument
, NULL
, GETOPT_VAL_RESCAN
},
51 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
56 case GETOPT_VAL_RESCAN
:
60 /* Usage printed by the caller */
65 if (check_argc_exact(argc
- optind
, 3))
68 memset(&args
, 0, sizeof(args
));
70 args
.src
= parse_qgroupid(argv
[optind
]);
71 args
.dst
= parse_qgroupid(argv
[optind
+ 1]);
73 path
= argv
[optind
+ 2];
76 * FIXME src should accept subvol path
78 if (btrfs_qgroup_level(args
.src
) >= btrfs_qgroup_level(args
.dst
)) {
79 fprintf(stderr
, "ERROR: bad relation requested '%s'\n", path
);
82 fd
= open_file_or_dir(path
, &dirstream
);
84 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
88 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
91 fprintf(stderr
, "ERROR: unable to assign quota group: %s\n",
93 close_file_or_dir(fd
, dirstream
);
98 * If ret > 0, it means assign caused qgroup data inconsistent state.
99 * Schedule a quota rescan if requested.
101 * The return value change only happens in newer kernel. But will not
102 * cause problem since old kernel has a bug that will never clear
107 struct btrfs_ioctl_quota_rescan_args args
;
109 printf("Quota data changed, rescan scheduled\n");
110 memset(&args
, 0, sizeof(args
));
111 ret
= ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
);
114 "ERROR: quota rescan failed: %s\n",
117 printf("WARNING: quotas may be inconsistent, rescan needed\n");
120 close_file_or_dir(fd
, dirstream
);
124 static int qgroup_create(int create
, int argc
, char **argv
)
129 char *path
= argv
[2];
130 struct btrfs_ioctl_qgroup_create_args args
;
131 DIR *dirstream
= NULL
;
133 if (check_argc_exact(argc
, 3))
136 memset(&args
, 0, sizeof(args
));
137 args
.create
= create
;
138 args
.qgroupid
= parse_qgroupid(argv
[1]);
140 fd
= open_file_or_dir(path
, &dirstream
);
142 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
146 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
);
148 close_file_or_dir(fd
, dirstream
);
150 fprintf(stderr
, "ERROR: unable to %s quota group: %s\n",
151 create
? "create":"destroy", strerror(e
));
157 static int parse_limit(const char *p
, unsigned long long *s
)
160 unsigned long long size
;
161 unsigned long long CLEAR_VALUE
= -1;
163 if (strcasecmp(p
, "none") == 0) {
171 size
= strtoull(p
, &endptr
, 10);
207 static const char * const cmd_qgroup_assign_usage
[] = {
208 "btrfs qgroup assign [options] <src> <dst> <path>",
209 "Assign SRC as the child qgroup of DST",
211 "--rescan schedule qutoa rescan if needed",
216 static int cmd_qgroup_assign(int argc
, char **argv
)
218 int ret
= qgroup_assign(1, argc
, argv
);
220 usage(cmd_qgroup_assign_usage
);
224 static const char * const cmd_qgroup_remove_usage
[] = {
225 "btrfs qgroup remove <src> <dst> <path>",
226 "Remove a child qgroup SRC from DST.",
230 static int cmd_qgroup_remove(int argc
, char **argv
)
232 int ret
= qgroup_assign(0, argc
, argv
);
234 usage(cmd_qgroup_remove_usage
);
238 static const char * const cmd_qgroup_create_usage
[] = {
239 "btrfs qgroup create <qgroupid> <path>",
240 "Create a subvolume quota group.",
244 static int cmd_qgroup_create(int argc
, char **argv
)
246 int ret
= qgroup_create(1, argc
, argv
);
248 usage(cmd_qgroup_create_usage
);
252 static const char * const cmd_qgroup_destroy_usage
[] = {
253 "btrfs qgroup destroy <qgroupid> <path>",
254 "Destroy a quota group.",
258 static int cmd_qgroup_destroy(int argc
, char **argv
)
260 int ret
= qgroup_create(0, argc
, argv
);
262 usage(cmd_qgroup_destroy_usage
);
266 static const char * const cmd_qgroup_show_usage
[] = {
267 "btrfs qgroup show -pcreFf "
268 "[--sort=qgroupid,rfer,excl,max_rfer,max_excl] <path>",
269 "Show subvolume quota groups.",
270 "-p print parent qgroup id",
271 "-c print child qgroup id",
272 "-r print limit of referenced size of qgroup",
273 "-e print limit of exclusive size of qgroup",
274 "-F list all qgroups which impact the given path",
275 " (including ancestral qgroups)",
276 "-f list all qgroups which impact the given path",
277 " (excluding ancestral qgroups)",
278 HELPINFO_OUTPUT_UNIT
,
279 "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
280 " list qgroups sorted by specified items",
281 " you can use '+' or '-' in front of each item.",
282 " (+:ascending, -:descending, ascending default)",
286 static int cmd_qgroup_show(int argc
, char **argv
)
292 DIR *dirstream
= NULL
;
297 struct btrfs_qgroup_comparer_set
*comparer_set
;
298 struct btrfs_qgroup_filter_set
*filter_set
;
299 filter_set
= btrfs_qgroup_alloc_filter_set();
300 comparer_set
= btrfs_qgroup_alloc_comparer_set();
302 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 0);
307 static const struct option long_options
[] = {
308 {"sort", required_argument
, NULL
, 'S'},
312 c
= getopt_long(argc
, argv
, "pcreFf", long_options
, NULL
);
317 btrfs_qgroup_setup_print_column(
318 BTRFS_QGROUP_PARENT
);
321 btrfs_qgroup_setup_print_column(
325 btrfs_qgroup_setup_print_column(
326 BTRFS_QGROUP_MAX_RFER
);
329 btrfs_qgroup_setup_print_column(
330 BTRFS_QGROUP_MAX_EXCL
);
339 ret
= btrfs_qgroup_parse_sort_string(optarg
,
342 usage(cmd_qgroup_show_usage
);
345 usage(cmd_qgroup_show_usage
);
348 btrfs_qgroup_setup_units(unit_mode
);
350 if (check_argc_exact(argc
- optind
, 1))
351 usage(cmd_qgroup_show_usage
);
354 fd
= open_file_or_dir(path
, &dirstream
);
356 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
361 qgroupid
= btrfs_get_path_rootid(fd
);
362 if (filter_flag
& 0x1)
363 btrfs_qgroup_setup_filter(&filter_set
,
364 BTRFS_QGROUP_FILTER_ALL_PARENT
,
366 if (filter_flag
& 0x2)
367 btrfs_qgroup_setup_filter(&filter_set
,
368 BTRFS_QGROUP_FILTER_PARENT
,
371 ret
= btrfs_show_qgroups(fd
, filter_set
, comparer_set
);
373 close_file_or_dir(fd
, dirstream
);
375 fprintf(stderr
, "ERROR: can't list qgroups: %s\n",
381 static const char * const cmd_qgroup_limit_usage
[] = {
382 "btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
383 "Set the limits a subvolume quota group.",
385 "-c limit amount of data after compression. This is the default,",
386 " it is currently not possible to turn off this option.",
387 "-e limit space exclusively assigned to this qgroup",
391 static int cmd_qgroup_limit(int argc
, char **argv
)
397 struct btrfs_ioctl_qgroup_limit_args args
;
398 unsigned long long size
;
401 DIR *dirstream
= NULL
;
405 int c
= getopt(argc
, argv
, "ce");
416 usage(cmd_qgroup_limit_usage
);
420 if (check_argc_min(argc
- optind
, 2))
421 usage(cmd_qgroup_limit_usage
);
423 if (!parse_limit(argv
[optind
], &size
)) {
424 fprintf(stderr
, "Invalid size argument given\n");
428 memset(&args
, 0, sizeof(args
));
430 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_RFER_CMPR
|
431 BTRFS_QGROUP_LIMIT_EXCL_CMPR
;
433 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_MAX_EXCL
;
434 args
.lim
.max_exclusive
= size
;
436 args
.lim
.flags
|= BTRFS_QGROUP_LIMIT_MAX_RFER
;
437 args
.lim
.max_referenced
= size
;
440 if (argc
- optind
== 2) {
442 path
= argv
[optind
+ 1];
443 ret
= test_issubvolume(path
);
445 fprintf(stderr
, "ERROR: error accessing '%s'\n", path
);
449 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n",
454 * keep qgroupid at 0, this indicates that the subvolume the
455 * fd refers to is to be limited
457 } else if (argc
- optind
== 3) {
458 args
.qgroupid
= parse_qgroupid(argv
[optind
+ 1]);
459 path
= argv
[optind
+ 2];
461 usage(cmd_qgroup_limit_usage
);
463 fd
= open_file_or_dir(path
, &dirstream
);
465 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
469 ret
= ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
);
471 close_file_or_dir(fd
, dirstream
);
473 fprintf(stderr
, "ERROR: unable to limit requested quota group: "
474 "%s\n", strerror(e
));
480 static const char qgroup_cmd_group_info
[] =
481 "manage quota groups";
483 const struct cmd_group qgroup_cmd_group
= {
484 qgroup_cmd_group_usage
, qgroup_cmd_group_info
, {
485 { "assign", cmd_qgroup_assign
, cmd_qgroup_assign_usage
,
487 { "remove", cmd_qgroup_remove
, cmd_qgroup_remove_usage
,
489 { "create", cmd_qgroup_create
, cmd_qgroup_create_usage
,
491 { "destroy", cmd_qgroup_destroy
, cmd_qgroup_destroy_usage
,
493 { "show", cmd_qgroup_show
, cmd_qgroup_show_usage
,
495 { "limit", cmd_qgroup_limit
, cmd_qgroup_limit_usage
,
501 int cmd_qgroup(int argc
, char **argv
)
503 return handle_command_group(&qgroup_cmd_group
, argc
, argv
);