1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2017 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * quotactl(2) with XGETNEXTQUOTA looks for the next active quota for an user
11f08c3bdfSopenharmony_ci * equal or higher to a given ID, in this test the ID is specified to a value
12f08c3bdfSopenharmony_ci * close to UINT_MAX(max value of unsigned int). When reaching the upper limit
13f08c3bdfSopenharmony_ci * and finding no active quota, it should return -1 and set errno to ENOENT.
14f08c3bdfSopenharmony_ci * Actually, quotactl(2) overflows and and return 0 as the "next" active id.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci * This kernel bug of xfs has been fixed in:
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci *  commit 657bdfb7f5e68ca5e2ed009ab473c429b0d6af85
19f08c3bdfSopenharmony_ci *  Author: Eric Sandeen <sandeen@redhat.com>
20f08c3bdfSopenharmony_ci *  Date:   Tue Jan 17 11:43:38 2017 -0800
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci *  xfs: don't wrap ID in xfs_dq_get_next_id
23f08c3bdfSopenharmony_ci */
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define _GNU_SOURCE
26f08c3bdfSopenharmony_ci#include <errno.h>
27f08c3bdfSopenharmony_ci#include <unistd.h>
28f08c3bdfSopenharmony_ci#include <stdio.h>
29f08c3bdfSopenharmony_ci#include <sys/quota.h>
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "tst_test.h"
32f08c3bdfSopenharmony_ci#include "quotactl_syscall_var.h"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#ifdef HAVE_XFS_XQM_H
35f08c3bdfSopenharmony_ci# include <xfs/xqm.h>
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic uint32_t test_id = 0xfffffffc;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void verify_quota(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	struct fs_disk_quota res_dquota;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	res_dquota.d_id = 1;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	TEST(do_quotactl(fd, QCMD(Q_XGETNEXTQUOTA, USRQUOTA), tst_device->dev,
46f08c3bdfSopenharmony_ci		test_id, (void *)&res_dquota));
47f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "quotactl() found the next active ID: %u unexpectedly",
49f08c3bdfSopenharmony_ci				res_dquota.d_id);
50f08c3bdfSopenharmony_ci		return;
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	if (TST_ERR == EINVAL)
54f08c3bdfSopenharmony_ci		tst_brk(TCONF | TTERRNO,
55f08c3bdfSopenharmony_ci			"Q_XGETNEXTQUOTA wasn't supported in quotactl()");
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (TST_ERR != ENOENT)
58f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "quotactl() failed unexpectedly with %s expected ENOENT",
59f08c3bdfSopenharmony_ci				tst_strerrno(TST_ERR));
60f08c3bdfSopenharmony_ci	else
61f08c3bdfSopenharmony_ci		tst_res(TPASS, "quotactl() failed with ENOENT as expected");
62f08c3bdfSopenharmony_ci}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cistatic void setup(void)
65f08c3bdfSopenharmony_ci{
66f08c3bdfSopenharmony_ci	quotactl_info();
67f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(MNTPOINT, O_RDONLY);
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void cleanup(void)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	if (fd > -1)
73f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic struct tst_test test = {
77f08c3bdfSopenharmony_ci	.setup = setup,
78f08c3bdfSopenharmony_ci	.cleanup = cleanup,
79f08c3bdfSopenharmony_ci	.needs_root = 1,
80f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
81f08c3bdfSopenharmony_ci		"CONFIG_XFS_QUOTA",
82f08c3bdfSopenharmony_ci		NULL
83f08c3bdfSopenharmony_ci	},
84f08c3bdfSopenharmony_ci	.test_all = verify_quota,
85f08c3bdfSopenharmony_ci	.mount_device = 1,
86f08c3bdfSopenharmony_ci	.dev_fs_type = "xfs",
87f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
88f08c3bdfSopenharmony_ci	.mnt_data = "usrquota",
89f08c3bdfSopenharmony_ci	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
90f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
91f08c3bdfSopenharmony_ci		{"linux-git", "657bdfb7f5e6"},
92f08c3bdfSopenharmony_ci		{}
93f08c3bdfSopenharmony_ci	}
94f08c3bdfSopenharmony_ci};
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci#else
97f08c3bdfSopenharmony_ci	TST_TEST_TCONF("System doesn't have <xfs/xqm.h>");
98f08c3bdfSopenharmony_ci#endif
99