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