#include "commands.h"
#include "qgroup.h"
#include "utils.h"
+#include "help.h"
static const char * const qgroup_cmd_group_usage[] = {
"btrfs qgroup <command> [options] <path>",
NULL
};
-static int qgroup_assign(int assign, int argc, char **argv)
+static int _cmd_qgroup_assign(int assign, int argc, char **argv,
+ const char * const *usage_str)
{
int ret = 0;
int fd;
- int e;
- char *path = argv[3];
+ int rescan = 0;
+ char *path;
struct btrfs_ioctl_qgroup_assign_args args;
+ DIR *dirstream = NULL;
+
+ if (assign) {
+ while (1) {
+ enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
+ static const struct option long_options[] = {
+ { "rescan", no_argument, NULL,
+ GETOPT_VAL_RESCAN },
+ { "no-rescan", no_argument, NULL,
+ GETOPT_VAL_NO_RESCAN },
+ { NULL, 0, NULL, 0 }
+ };
+ int c = getopt_long(argc, argv, "", long_options, NULL);
+
+ if (c < 0)
+ break;
+ switch (c) {
+ case GETOPT_VAL_RESCAN:
+ rescan = 1;
+ break;
+ case GETOPT_VAL_NO_RESCAN:
+ rescan = 0;
+ break;
+ default:
+ /* Usage printed by the caller */
+ return -1;
+ }
+ }
+ } else {
+ clean_args_no_options(argc, argv, usage_str);
+ }
- if (check_argc_exact(argc, 4))
- return -1;
+ if (check_argc_exact(argc - optind, 3))
+ usage(usage_str);
memset(&args, 0, sizeof(args));
args.assign = assign;
- args.src = parse_qgroupid(argv[1]);
- args.dst = parse_qgroupid(argv[2]);
+ args.src = parse_qgroupid(argv[optind]);
+ args.dst = parse_qgroupid(argv[optind + 1]);
+
+ path = argv[optind + 2];
/*
* FIXME src should accept subvol path
*/
- if ((args.src >> 48) >= (args.dst >> 48)) {
- fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
- return 12;
- }
- fd = open_file_or_dir(path);
- if (fd < 0) {
- fprintf(stderr, "ERROR: can't access '%s'\n", path);
- return 12;
+ if (btrfs_qgroup_level(args.src) >= btrfs_qgroup_level(args.dst)) {
+ error("bad relation requested: %s", path);
+ return 1;
}
+ fd = btrfs_open_dir(path, &dirstream, 1);
+ if (fd < 0)
+ return 1;
ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
- e = errno;
- close(fd);
if (ret < 0) {
- fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
- strerror(e));
- return 30;
+ error("unable to assign quota group: %s", strerror(errno));
+ close_file_or_dir(fd, dirstream);
+ return 1;
}
- return 0;
+
+ /*
+ * If ret > 0, it means assign caused qgroup data inconsistent state.
+ * Schedule a quota rescan if requested.
+ *
+ * The return value change only happens in newer kernel. But will not
+ * cause problem since old kernel has a bug that will never clear
+ * INCONSISTENT bit.
+ */
+ if (ret > 0) {
+ if (rescan) {
+ struct btrfs_ioctl_quota_rescan_args qargs;
+
+ printf("Quota data changed, rescan scheduled\n");
+ memset(&qargs, 0, sizeof(qargs));
+ ret = ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &qargs);
+ if (ret < 0)
+ error("quota rescan failed: %s",
+ strerror(errno));
+ } else {
+ warning("quotas may be inconsistent, rescan needed");
+ }
+ }
+ close_file_or_dir(fd, dirstream);
+ return ret;
}
-static int qgroup_create(int create, int argc, char **argv)
+static int _cmd_qgroup_create(int create, int argc, char **argv)
{
int ret = 0;
int fd;
int e;
- char *path = argv[2];
+ char *path;
struct btrfs_ioctl_qgroup_create_args args;
+ DIR *dirstream = NULL;
- if (check_argc_exact(argc, 3))
+ if (check_argc_exact(argc - optind, 2))
return -1;
memset(&args, 0, sizeof(args));
args.create = create;
- args.qgroupid = parse_qgroupid(argv[1]);
+ args.qgroupid = parse_qgroupid(argv[optind]);
+ path = argv[optind + 1];
- fd = open_file_or_dir(path);
- if (fd < 0) {
- fprintf(stderr, "ERROR: can't access '%s'\n", path);
- return 12;
- }
+ fd = btrfs_open_dir(path, &dirstream, 1);
+ if (fd < 0)
+ return 1;
ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
e = errno;
- close(fd);
+ close_file_or_dir(fd, dirstream);
if (ret < 0) {
- fprintf(stderr, "ERROR: unable to create quota group: %s\n",
- strerror(e));
- return 30;
+ error("unable to %s quota group: %s",
+ create ? "create":"destroy", strerror(e));
+ return 1;
}
return 0;
}
-void print_qgroup_info(u64 objectid, struct btrfs_qgroup_info_item *info)
-{
- printf("%llu/%llu %lld %lld\n", objectid >> 48,
- objectid & ((1ll << 48) - 1),
- btrfs_stack_qgroup_info_referenced(info),
- btrfs_stack_qgroup_info_exclusive(info));
-}
-
-int list_qgroups(int fd)
-{
- int ret;
- struct btrfs_ioctl_search_args args;
- struct btrfs_ioctl_search_key *sk = &args.key;
- struct btrfs_ioctl_search_header *sh;
- unsigned long off = 0;
- unsigned int i;
- struct btrfs_qgroup_info_item *info;
-
- memset(&args, 0, sizeof(args));
-
- /* search in the quota tree */
- sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
-
- /*
- * set the min and max to backref keys. The search will
- * only send back this type of key now.
- */
- sk->max_type = BTRFS_QGROUP_INFO_KEY;
- sk->min_type = BTRFS_QGROUP_INFO_KEY;
- sk->max_objectid = 0;
- sk->max_offset = (u64)-1;
- sk->max_transid = (u64)-1;
-
- /* just a big number, doesn't matter much */
- sk->nr_items = 4096;
-
- while (1) {
- ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
- if (ret < 0)
- return ret;
-
- /* the ioctl returns the number of item it found in nr_items */
- if (sk->nr_items == 0)
- break;
-
- off = 0;
-
- /*
- * for each item, pull the key out of the header and then
- * read the root_ref item it contains
- */
- for (i = 0; i < sk->nr_items; i++) {
- sh = (struct btrfs_ioctl_search_header *)(args.buf +
- off);
- off += sizeof(*sh);
-
- if (sh->objectid != 0)
- goto done;
-
- if (sh->type != BTRFS_QGROUP_INFO_KEY)
- goto done;
-
- info = (struct btrfs_qgroup_info_item *)
- (args.buf + off);
- print_qgroup_info(sh->offset, info);
-
- off += sh->len;
-
- /*
- * record the mins in sk so we can make sure the
- * next search doesn't repeat this root
- */
- sk->min_offset = sh->offset;
- }
- sk->nr_items = 4096;
- /*
- * this iteration is done, step forward one qgroup for the next
- * ioctl
- */
- if (sk->min_offset < (u64)-1)
- sk->min_offset++;
- else
- break;
- }
-
-done:
- return ret;
-}
-
static int parse_limit(const char *p, unsigned long long *s)
{
char *endptr;
unsigned long long size;
+ unsigned long long CLEAR_VALUE = -1;
if (strcasecmp(p, "none") == 0) {
- *s = 0;
+ *s = CLEAR_VALUE;
return 1;
}
+
+ if (p[0] == '-')
+ return 0;
+
size = strtoull(p, &endptr, 10);
+ if (p == endptr)
+ return 0;
+
switch (*endptr) {
case 'T':
case 't':
size *= 1024;
+ /* fallthrough */
case 'G':
case 'g':
size *= 1024;
+ /* fallthrough */
case 'M':
case 'm':
size *= 1024;
+ /* fallthrough */
case 'K':
case 'k':
size *= 1024;
}
static const char * const cmd_qgroup_assign_usage[] = {
- "btrfs qgroup assign <src> <dst> <path>",
- "Enable subvolume qgroup support for a filesystem.",
+ "btrfs qgroup assign [options] <src> <dst> <path>",
+ "Assign SRC as the child qgroup of DST",
+ "",
+ "--rescan schedule qutoa rescan if needed",
+ "--no-rescan don't schedule quota rescan",
NULL
};
static int cmd_qgroup_assign(int argc, char **argv)
{
- int ret = qgroup_assign(1, argc, argv);
- if (ret < 0)
- usage(cmd_qgroup_assign_usage);
- return ret;
+ return _cmd_qgroup_assign(1, argc, argv, cmd_qgroup_assign_usage);
}
static const char * const cmd_qgroup_remove_usage[] = {
"btrfs qgroup remove <src> <dst> <path>",
- "Remove a subvol from a quota group.",
+ "Remove a child qgroup SRC from DST.",
NULL
};
static int cmd_qgroup_remove(int argc, char **argv)
{
- int ret = qgroup_assign(0, argc, argv);
- if (ret < 0)
- usage(cmd_qgroup_remove_usage);
- return ret;
+ return _cmd_qgroup_assign(0, argc, argv, cmd_qgroup_remove_usage);
}
static const char * const cmd_qgroup_create_usage[] = {
static int cmd_qgroup_create(int argc, char **argv)
{
- int ret = qgroup_create(1, argc, argv);
+ int ret;
+
+ clean_args_no_options(argc, argv, cmd_qgroup_create_usage);
+
+ ret = _cmd_qgroup_create(1, argc, argv);
+
if (ret < 0)
usage(cmd_qgroup_create_usage);
return ret;
static const char * const cmd_qgroup_destroy_usage[] = {
"btrfs qgroup destroy <qgroupid> <path>",
- "Destroy a subvolume quota group.",
+ "Destroy a quota group.",
NULL
};
static int cmd_qgroup_destroy(int argc, char **argv)
{
- int ret = qgroup_create(0, argc, argv);
+ int ret;
+
+ clean_args_no_options(argc, argv, cmd_qgroup_destroy_usage);
+
+ ret = _cmd_qgroup_create(0, argc, argv);
+
if (ret < 0)
usage(cmd_qgroup_destroy_usage);
return ret;
}
static const char * const cmd_qgroup_show_usage[] = {
- "btrfs qgroup show <path>",
- "Show all subvolume quota groups.",
+ "btrfs qgroup show [options] <path>",
+ "Show subvolume quota groups.",
+ "-p print parent qgroup id",
+ "-c print child qgroup id",
+ "-r print limit of referenced size of qgroup",
+ "-e print limit of exclusive size of qgroup",
+ "-F list all qgroups which impact the given path",
+ " (including ancestral qgroups)",
+ "-f list all qgroups which impact the given path",
+ " (excluding ancestral qgroups)",
+ HELPINFO_UNITS_LONG,
+ "--sort=qgroupid,rfer,excl,max_rfer,max_excl",
+ " list qgroups sorted by specified items",
+ " you can use '+' or '-' in front of each item.",
+ " (+:ascending, -:descending, ascending default)",
+ "--sync force sync of the filesystem before getting info",
NULL
};
static int cmd_qgroup_show(int argc, char **argv)
{
+ char *path;
int ret = 0;
int fd;
- int e;
- char *path = argv[1];
+ DIR *dirstream = NULL;
+ u64 qgroupid;
+ int filter_flag = 0;
+ unsigned unit_mode;
+ int sync = 0;
+
+ struct btrfs_qgroup_comparer_set *comparer_set;
+ struct btrfs_qgroup_filter_set *filter_set;
+ filter_set = btrfs_qgroup_alloc_filter_set();
+ comparer_set = btrfs_qgroup_alloc_comparer_set();
+
+ unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
+
+ while (1) {
+ int c;
+ enum {
+ GETOPT_VAL_SORT = 256,
+ GETOPT_VAL_SYNC
+ };
+ static const struct option long_options[] = {
+ {"sort", required_argument, NULL, GETOPT_VAL_SORT},
+ {"sync", no_argument, NULL, GETOPT_VAL_SYNC},
+ { NULL, 0, NULL, 0 }
+ };
+
+ c = getopt_long(argc, argv, "pcreFf", long_options, NULL);
+ if (c < 0)
+ break;
+ switch (c) {
+ case 'p':
+ btrfs_qgroup_setup_print_column(
+ BTRFS_QGROUP_PARENT);
+ break;
+ case 'c':
+ btrfs_qgroup_setup_print_column(
+ BTRFS_QGROUP_CHILD);
+ break;
+ case 'r':
+ btrfs_qgroup_setup_print_column(
+ BTRFS_QGROUP_MAX_RFER);
+ break;
+ case 'e':
+ btrfs_qgroup_setup_print_column(
+ BTRFS_QGROUP_MAX_EXCL);
+ break;
+ case 'F':
+ filter_flag |= 0x1;
+ break;
+ case 'f':
+ filter_flag |= 0x2;
+ break;
+ case GETOPT_VAL_SORT:
+ ret = btrfs_qgroup_parse_sort_string(optarg,
+ &comparer_set);
+ if (ret)
+ usage(cmd_qgroup_show_usage);
+ break;
+ case GETOPT_VAL_SYNC:
+ sync = 1;
+ break;
+ default:
+ usage(cmd_qgroup_show_usage);
+ }
+ }
+ btrfs_qgroup_setup_units(unit_mode);
- if (check_argc_exact(argc, 2))
+ if (check_argc_exact(argc - optind, 1))
usage(cmd_qgroup_show_usage);
- fd = open_file_or_dir(path);
+ path = argv[optind];
+ fd = btrfs_open_dir(path, &dirstream, 1);
if (fd < 0) {
- fprintf(stderr, "ERROR: can't access '%s'\n", path);
- return 12;
+ free(filter_set);
+ free(comparer_set);
+ return 1;
}
- ret = list_qgroups(fd);
- e = errno;
- close(fd);
- if (ret < 0) {
- fprintf(stderr, "ERROR: can't list qgroups: %s\n",
- strerror(e));
- return 30;
+ if (sync) {
+ ret = ioctl(fd, BTRFS_IOC_SYNC);
+ if (ret < 0)
+ warning("sync ioctl failed on '%s': %s", path,
+ strerror(errno));
}
- return ret;
+ if (filter_flag) {
+ ret = lookup_path_rootid(fd, &qgroupid);
+ if (ret < 0) {
+ error("cannot resolve rootid for %s: %s",
+ path, strerror(-ret));
+ close_file_or_dir(fd, dirstream);
+ goto out;
+ }
+ if (filter_flag & 0x1)
+ btrfs_qgroup_setup_filter(&filter_set,
+ BTRFS_QGROUP_FILTER_ALL_PARENT,
+ qgroupid);
+ if (filter_flag & 0x2)
+ btrfs_qgroup_setup_filter(&filter_set,
+ BTRFS_QGROUP_FILTER_PARENT,
+ qgroupid);
+ }
+ ret = btrfs_show_qgroups(fd, filter_set, comparer_set);
+ close_file_or_dir(fd, dirstream);
+
+out:
+ return !!ret;
}
static const char * const cmd_qgroup_limit_usage[] = {
"btrfs qgroup limit [options] <size>|none [<qgroupid>] <path>",
- "Limit the size of a subvolume quota group.",
+ "Set the limits a subvolume quota group.",
"",
"-c limit amount of data after compression. This is the default,",
" it is currently not possible to turn off this option.",
unsigned long long size;
int compressed = 0;
int exclusive = 0;
+ DIR *dirstream = NULL;
- optind = 1;
while (1) {
int c = getopt(argc, argv, "ce");
if (c < 0)
usage(cmd_qgroup_limit_usage);
if (!parse_limit(argv[optind], &size)) {
- fprintf(stderr, "Invalid size argument given\n");
+ error("invalid size argument: %s", argv[optind]);
return 1;
}
memset(&args, 0, sizeof(args));
- if (size) {
- if (compressed)
- args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
- BTRFS_QGROUP_LIMIT_EXCL_CMPR;
- if (exclusive) {
- args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
- args.lim.max_exclusive = size;
- } else {
- args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
- args.lim.max_referenced = size;
- }
+ if (compressed)
+ args.lim.flags |= BTRFS_QGROUP_LIMIT_RFER_CMPR |
+ BTRFS_QGROUP_LIMIT_EXCL_CMPR;
+ if (exclusive) {
+ args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_EXCL;
+ args.lim.max_exclusive = size;
+ } else {
+ args.lim.flags |= BTRFS_QGROUP_LIMIT_MAX_RFER;
+ args.lim.max_referenced = size;
}
if (argc - optind == 2) {
path = argv[optind + 1];
ret = test_issubvolume(path);
if (ret < 0) {
- fprintf(stderr, "ERROR: error accessing '%s'\n", path);
- return 12;
+ error("cannot access '%s': %s", path, strerror(-ret));
+ return 1;
}
if (!ret) {
- fprintf(stderr, "ERROR: '%s' is not a subvolume\n",
- path);
- return 13;
+ error("'%s' is not a subvolume", path);
+ return 1;
}
/*
* keep qgroupid at 0, this indicates that the subvolume the
} else
usage(cmd_qgroup_limit_usage);
- fd = open_file_or_dir(path);
- if (fd < 0) {
- fprintf(stderr, "ERROR: can't access '%s'\n", path);
- return 12;
- }
+ fd = btrfs_open_dir(path, &dirstream, 1);
+ if (fd < 0)
+ return 1;
ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
e = errno;
- close(fd);
+ close_file_or_dir(fd, dirstream);
if (ret < 0) {
- fprintf(stderr, "ERROR: unable to limit requested quota group: "
- "%s\n", strerror(e));
- return 30;
+ error("unable to limit requested quota group: %s", strerror(e));
+ return 1;
}
return 0;
}
+static const char qgroup_cmd_group_info[] =
+"manage quota groups";
+
const struct cmd_group qgroup_cmd_group = {
- qgroup_cmd_group_usage, NULL, {
- { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage, 0, 0 },
- { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage, 0, 0 },
- { "create", cmd_qgroup_create, cmd_qgroup_create_usage, 0, 0 },
- { "destroy", cmd_qgroup_destroy,
- cmd_qgroup_destroy_usage, 0, 0 },
- { "show", cmd_qgroup_show, cmd_qgroup_show_usage, 0, 0 },
- { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage, 0, 0 },
- { 0, 0, 0, 0, 0 }
+ qgroup_cmd_group_usage, qgroup_cmd_group_info, {
+ { "assign", cmd_qgroup_assign, cmd_qgroup_assign_usage,
+ NULL, 0 },
+ { "remove", cmd_qgroup_remove, cmd_qgroup_remove_usage,
+ NULL, 0 },
+ { "create", cmd_qgroup_create, cmd_qgroup_create_usage,
+ NULL, 0 },
+ { "destroy", cmd_qgroup_destroy, cmd_qgroup_destroy_usage,
+ NULL, 0 },
+ { "show", cmd_qgroup_show, cmd_qgroup_show_usage,
+ NULL, 0 },
+ { "limit", cmd_qgroup_limit, cmd_qgroup_limit_usage,
+ NULL, 0 },
+ NULL_CMD_STRUCT
}
};