Skip to content

Commit ee4834f

Browse files
committed
btrfs-progs: qgroup clear-stale: check if qgroup enabled first
If qgroups are not enabled the 'clear-stale' command unconditionally calls sync on the whole filesystem, which is unnecessary and can slow down the system. Do a check first if qgroups are enabled. Issue: #942 Signed-off-by: David Sterba <[email protected]>
1 parent dc4111c commit ee4834f

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

Diff for: cmds/qgroup.c

+36-4
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,26 @@ static bool key_in_range(const struct btrfs_key *key,
13171317
return true;
13181318
}
13191319

1320+
static int quota_enabled(int fd) {
1321+
struct btrfs_tree_search_args args = { 0 };
1322+
struct btrfs_ioctl_search_key *sk;
1323+
int ret;
1324+
1325+
sk = btrfs_tree_search_sk(&args);
1326+
sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
1327+
sk->min_type = 0;
1328+
sk->max_type = (u8)-1;
1329+
sk->max_objectid = (u64)-1;
1330+
sk->max_offset = (u64)-1;
1331+
sk->max_transid = (u64)-1;
1332+
sk->nr_items = 1;
1333+
1334+
ret = btrfs_tree_search_ioctl(fd, &args);
1335+
if (ret < 0)
1336+
ret = -errno;
1337+
return ret;
1338+
}
1339+
13201340
static int __qgroups_search(int fd, struct btrfs_tree_search_args *args,
13211341
struct qgroup_lookup *qgroup_lookup)
13221342
{
@@ -2209,6 +2229,18 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char *
22092229
if (fd < 0)
22102230
return 1;
22112231

2232+
/* Do the check first so the sync is not done if quotas are not enabled. */
2233+
ret = quota_enabled(fd);
2234+
if (ret == -ENOENT) {
2235+
warning("qgroups not enabled");
2236+
ret = 0;
2237+
goto out_close;
2238+
} else if (ret < 0) {
2239+
errno = -ret;
2240+
error("cannot check qgroup status: %m");
2241+
goto out_close;
2242+
}
2243+
22122244
/* Sync the fs so that the qgroup numbers are uptodate. */
22132245
err = btrfs_util_sync_fd(fd);
22142246
if (err)
@@ -2217,11 +2249,11 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char *
22172249
ret = qgroups_search_all(fd, &qgroup_lookup);
22182250
if (ret == -ENOTTY) {
22192251
error("can't list qgroups: quotas not enabled");
2220-
goto out;
2252+
goto out_close;
22212253
} else if (ret < 0) {
22222254
errno = -ret;
22232255
error("can't list qgroups: %m");
2224-
goto out;
2256+
goto out_close;
22252257
}
22262258

22272259
node = rb_first(&qgroup_lookup.root);
@@ -2235,9 +2267,9 @@ static int cmd_qgroup_clear_stale(const struct cmd_struct *cmd, int argc, char *
22352267
node = rb_next(node);
22362268
}
22372269

2238-
out:
2239-
close(fd);
22402270
__free_all_qgroups(&qgroup_lookup);
2271+
out_close:
2272+
close(fd);
22412273
return !!ret;
22422274
}
22432275
static DEFINE_SIMPLE_COMMAND(qgroup_clear_stale, "clear-stale");

0 commit comments

Comments
 (0)