xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/quotactl/quotactl06.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) 2019-2021 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[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  * Tests basic error handling of the quotactl syscall with visible quota files
11*49cdfc7eSAndroid Build Coastguard Worker  * (cover two formats, vfsv0 and vfsv1):
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - EACCES when cmd is Q_QUOTAON and addr existed but not a regular file
14*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when the file specified by special or addr does not exist
15*49cdfc7eSAndroid Build Coastguard Worker  * - EBUSY when cmd is Q_QUOTAON and another Q_QUOTAON had already been
16*49cdfc7eSAndroid Build Coastguard Worker  *   performed
17*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT when addr or special is invalid
18*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL when cmd or type is invalid
19*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTBLK when special is not a block device
20*49cdfc7eSAndroid Build Coastguard Worker  * - ESRCH when no disk quota is found for the indicated user and quotas have
21*49cdfc7eSAndroid Build Coastguard Worker  *   not been turned on for this fs
22*49cdfc7eSAndroid Build Coastguard Worker  * - ESRCH when cmd is Q_QUOTAON, but the quota format was not found
23*49cdfc7eSAndroid Build Coastguard Worker  * - ESRCH when cmd is Q_GETNEXTQUOTA, but there is no ID greater than or
24*49cdfc7eSAndroid Build Coastguard Worker  *   equal to id that has an active quota
25*49cdfc7eSAndroid Build Coastguard Worker  * - ERANGE when cmd is Q_SETQUOTA, but the specified limits are out of the
26*49cdfc7eSAndroid Build Coastguard Worker  *   range allowed by the quota format
27*49cdfc7eSAndroid Build Coastguard Worker  * - EPERM when the caller lacked the required privilege (CAP_SYS_ADMIN) for
28*49cdfc7eSAndroid Build Coastguard Worker  *   the specified operation
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * For ERANGE error, the vfsv0 and vfsv1 format's maximum quota limit setting
31*49cdfc7eSAndroid Build Coastguard Worker  * have been fixed since the following kernel patch:
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  *  commit 7e08da50cf706151f324349f9235ebd311226997
34*49cdfc7eSAndroid Build Coastguard Worker  *  Author: Jan Kara <[email protected]>
35*49cdfc7eSAndroid Build Coastguard Worker  *  Date:   Wed Mar 4 14:42:02 2015 +0100
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  *  quota: Fix maximum quota limit settings
38*49cdfc7eSAndroid Build Coastguard Worker  */
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/quota.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
43*49cdfc7eSAndroid Build Coastguard Worker #include "quotactl_fmt_var.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "tst_capability.h"
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define OPTION_INVALID 999
47*49cdfc7eSAndroid Build Coastguard Worker #define USRPATH MNTPOINT "/aquota.user"
48*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
49*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR1 MNTPOINT "/testdir1"
50*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR2 MNTPOINT "/testdir2"
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static char usrpath[] = USRPATH;
53*49cdfc7eSAndroid Build Coastguard Worker static char testdir1[] = TESTDIR1;
54*49cdfc7eSAndroid Build Coastguard Worker static char testdir2[] = TESTDIR2;
55*49cdfc7eSAndroid Build Coastguard Worker static int32_t fmt_id;
56*49cdfc7eSAndroid Build Coastguard Worker static int32_t fmt_invalid = 999;
57*49cdfc7eSAndroid Build Coastguard Worker static int test_invalid = 1;
58*49cdfc7eSAndroid Build Coastguard Worker static int test_id;
59*49cdfc7eSAndroid Build Coastguard Worker static int getnextquota_nsup;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker static struct if_nextdqblk res_ndq;
62*49cdfc7eSAndroid Build Coastguard Worker static struct dqblk set_dq = {
63*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_bsoftlimit = 100,
64*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_valid = QIF_BLIMITS
65*49cdfc7eSAndroid Build Coastguard Worker };
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker static struct dqblk set_dqmax = {
68*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_bsoftlimit = 0x7fffffffffffffffLL,  /* 2^63-1 */
69*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_valid = QIF_BLIMITS
70*49cdfc7eSAndroid Build Coastguard Worker };
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cap dropadmin = {
73*49cdfc7eSAndroid Build Coastguard Worker 	.action = TST_CAP_DROP,
74*49cdfc7eSAndroid Build Coastguard Worker 	.id = CAP_SYS_ADMIN,
75*49cdfc7eSAndroid Build Coastguard Worker 	.name = "CAP_SYS_ADMIN",
76*49cdfc7eSAndroid Build Coastguard Worker };
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cap needadmin = {
79*49cdfc7eSAndroid Build Coastguard Worker 	.action = TST_CAP_REQ,
80*49cdfc7eSAndroid Build Coastguard Worker 	.id = CAP_SYS_ADMIN,
81*49cdfc7eSAndroid Build Coastguard Worker 	.name = "CAP_SYS_ADMIN",
82*49cdfc7eSAndroid Build Coastguard Worker };
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
85*49cdfc7eSAndroid Build Coastguard Worker 	int cmd;
86*49cdfc7eSAndroid Build Coastguard Worker 	int *id;
87*49cdfc7eSAndroid Build Coastguard Worker 	void *addr;
88*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
89*49cdfc7eSAndroid Build Coastguard Worker 	int on_flag;
90*49cdfc7eSAndroid Build Coastguard Worker 	char *des;
91*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
92*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, testdir1, EACCES, 0,
93*49cdfc7eSAndroid Build Coastguard Worker 	"EACCES when cmd is Q_QUOTAON and addr existed but not a regular file"},
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, testdir2, ENOENT, 0,
96*49cdfc7eSAndroid Build Coastguard Worker 	"ENOENT when the file specified by special or addr does not exist"},
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, usrpath, EBUSY, 1,
99*49cdfc7eSAndroid Build Coastguard Worker 	"EBUSY when cmd is Q_QUOTAON and another Q_QUOTAON had already been performed"},
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_SETQUOTA, USRQUOTA), &fmt_id, NULL, EFAULT, 1,
102*49cdfc7eSAndroid Build Coastguard Worker 	"EFAULT when addr or special is invalid"},
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(OPTION_INVALID, USRQUOTA), &fmt_id, usrpath, EINVAL, 0,
105*49cdfc7eSAndroid Build Coastguard Worker 	"EINVAL when cmd or type is invalid"},
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, usrpath, ENOTBLK, 0,
108*49cdfc7eSAndroid Build Coastguard Worker 	"ENOTBLK when special is not a block device"},
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_SETQUOTA, USRQUOTA), &test_id, &set_dq, ESRCH, 0,
111*49cdfc7eSAndroid Build Coastguard Worker 	"ESRCH is for Q_SETQUOTA but no quota found for the user or quotas are off"},
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_invalid, usrpath, ESRCH, 0,
114*49cdfc7eSAndroid Build Coastguard Worker 	"ESRCH when cmd is Q_QUOTAON, but the quota format was not found"},
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_GETNEXTQUOTA, USRQUOTA), &test_invalid, usrpath, ESRCH, 0,
117*49cdfc7eSAndroid Build Coastguard Worker 	"ESRCH for Q_GETNEXTQUOTA, but the id was last one"},
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_SETQUOTA, USRQUOTA), &test_id, &set_dqmax, ERANGE, 1,
120*49cdfc7eSAndroid Build Coastguard Worker 	"ERANGE for Q_SETQUOTA, but the specified limits are out of range"},
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, usrpath, EPERM, 0,
123*49cdfc7eSAndroid Build Coastguard Worker 	"EPERM when the caller lacks the required privilege (CAP_SYS_ADMIN)"},
124*49cdfc7eSAndroid Build Coastguard Worker };
125*49cdfc7eSAndroid Build Coastguard Worker 
verify_quotactl(unsigned int n)126*49cdfc7eSAndroid Build Coastguard Worker static void verify_quotactl(unsigned int n)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
129*49cdfc7eSAndroid Build Coastguard Worker 	int quota_on = 0;
130*49cdfc7eSAndroid Build Coastguard Worker 	int drop_flag = 0;
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing %s", tc->des);
133*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->cmd == QCMD(Q_GETNEXTQUOTA, USRQUOTA) && getnextquota_nsup) {
134*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "current system doesn't support Q_GETNEXTQUOTA");
135*49cdfc7eSAndroid Build Coastguard Worker 		return;
136*49cdfc7eSAndroid Build Coastguard Worker 	}
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->on_flag) {
139*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_PASS_SILENT(quotactl(QCMD(Q_QUOTAON, USRQUOTA),
140*49cdfc7eSAndroid Build Coastguard Worker 			tst_device->dev, fmt_id, usrpath),
141*49cdfc7eSAndroid Build Coastguard Worker 			"quotactl with Q_QUOTAON");
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 		if (!TST_PASS)
144*49cdfc7eSAndroid Build Coastguard Worker 			return;
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 		quota_on = 1;
147*49cdfc7eSAndroid Build Coastguard Worker 	}
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_err == EPERM) {
150*49cdfc7eSAndroid Build Coastguard Worker 		tst_cap_action(&dropadmin);
151*49cdfc7eSAndroid Build Coastguard Worker 		drop_flag = 1;
152*49cdfc7eSAndroid Build Coastguard Worker 	}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_err == ENOTBLK) {
155*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(quotactl(tc->cmd, "/dev/null", *tc->id, tc->addr),
156*49cdfc7eSAndroid Build Coastguard Worker 			ENOTBLK, "quotactl()");
157*49cdfc7eSAndroid Build Coastguard Worker 	} else {
158*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(quotactl(tc->cmd, tst_device->dev, *tc->id,
159*49cdfc7eSAndroid Build Coastguard Worker 			tc->addr), tc->exp_err, "quotactl()");
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	if (quota_on) {
163*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_PASS_SILENT(quotactl(QCMD(Q_QUOTAOFF, USRQUOTA),
164*49cdfc7eSAndroid Build Coastguard Worker 			tst_device->dev, fmt_id, usrpath),
165*49cdfc7eSAndroid Build Coastguard Worker 			"quotactl with Q_QUOTAOFF");
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 		if (!TST_PASS)
168*49cdfc7eSAndroid Build Coastguard Worker 			return;
169*49cdfc7eSAndroid Build Coastguard Worker 	}
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker 	if (drop_flag)
172*49cdfc7eSAndroid Build Coastguard Worker 		tst_cap_action(&needadmin);
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)175*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
176*49cdfc7eSAndroid Build Coastguard Worker {
177*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
178*49cdfc7eSAndroid Build Coastguard Worker 	const struct quotactl_fmt_variant *var = &fmt_variants[tst_variant];
179*49cdfc7eSAndroid Build Coastguard Worker 	const char *const cmd[] = {
180*49cdfc7eSAndroid Build Coastguard Worker 		"quotacheck", "-ugF", var->fmt_name, MNTPOINT, NULL
181*49cdfc7eSAndroid Build Coastguard Worker 	};
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "quotactl() with %s format", var->fmt_name);
184*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CMD(cmd, NULL, NULL);
185*49cdfc7eSAndroid Build Coastguard Worker 	fmt_id = var->fmt_id;
186*49cdfc7eSAndroid Build Coastguard Worker 	/* vfsv0 block limit 2^42, vfsv1 block limit 2^63 - 1 */
187*49cdfc7eSAndroid Build Coastguard Worker 	set_dqmax.dqb_bsoftlimit = tst_variant ? 0x20000000000000 : 0x100000000;
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_ACCESS(USRPATH, F_OK);
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TESTDIR1, 0666);
192*49cdfc7eSAndroid Build Coastguard Worker 
193*49cdfc7eSAndroid Build Coastguard Worker 	TEST(quotactl(QCMD(Q_GETNEXTQUOTA, USRQUOTA), tst_device->dev,
194*49cdfc7eSAndroid Build Coastguard Worker 		test_id, (void *) &res_ndq));
195*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EINVAL || TST_ERR == ENOSYS)
196*49cdfc7eSAndroid Build Coastguard Worker 		getnextquota_nsup = 1;
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
199*49cdfc7eSAndroid Build Coastguard Worker 		if (!tcases[i].addr)
200*49cdfc7eSAndroid Build Coastguard Worker 			tcases[i].addr = tst_get_bad_addr(NULL);
201*49cdfc7eSAndroid Build Coastguard Worker 	}
202*49cdfc7eSAndroid Build Coastguard Worker }
203*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)204*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
205*49cdfc7eSAndroid Build Coastguard Worker {
206*49cdfc7eSAndroid Build Coastguard Worker 	if (!access(USRPATH, F_OK))
207*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNLINK(USRPATH);
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	if (!access(TESTDIR1, F_OK))
210*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_RMDIR(TESTDIR1);
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker 
213*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
214*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
215*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
216*49cdfc7eSAndroid Build Coastguard Worker 	.needs_drivers = (const char *const []) {
217*49cdfc7eSAndroid Build Coastguard Worker 		"quota_v2",
218*49cdfc7eSAndroid Build Coastguard Worker 		NULL
219*49cdfc7eSAndroid Build Coastguard Worker 	},
220*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
221*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_quotactl,
222*49cdfc7eSAndroid Build Coastguard Worker 	.dev_fs_type = "ext4",
223*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
224*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
225*49cdfc7eSAndroid Build Coastguard Worker 	.mnt_data = "usrquota",
226*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cmds = (const char *const []) {
227*49cdfc7eSAndroid Build Coastguard Worker 		"quotacheck",
228*49cdfc7eSAndroid Build Coastguard Worker 		NULL
229*49cdfc7eSAndroid Build Coastguard Worker 	},
230*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
231*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = QUOTACTL_FMT_VARIANTS,
232*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
233*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "7e08da50cf70"},
234*49cdfc7eSAndroid Build Coastguard Worker 		{}
235*49cdfc7eSAndroid Build Coastguard Worker 	}
236*49cdfc7eSAndroid Build Coastguard Worker };
237