xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/quotactl/quotactl04.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  * This testcase checks that quotactl(2) on ext4 filesystem succeeds to:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - turn on quota with Q_QUOTAON flag for project
13*49cdfc7eSAndroid Build Coastguard Worker  * - set disk quota limits with Q_SETQUOTA flag for project
14*49cdfc7eSAndroid Build Coastguard Worker  * - get disk quota limits with Q_GETQUOTA flag for project
15*49cdfc7eSAndroid Build Coastguard Worker  * - set information about quotafile with Q_SETINFO flag for project
16*49cdfc7eSAndroid Build Coastguard Worker  * - get information about quotafile with Q_GETINFO flag for project
17*49cdfc7eSAndroid Build Coastguard Worker  * - get quota format with Q_GETFMT flag for project
18*49cdfc7eSAndroid Build Coastguard Worker  * - get disk quota limit greater than or equal to ID with Q_GETNEXTQUOTA flag for project
19*49cdfc7eSAndroid Build Coastguard Worker  * - turn off quota with Q_QUOTAOFF flag for project
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  * Minimum e2fsprogs version required is 1.43.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "quotactl_syscall_var.h"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #define FMTID QFMT_VFS_V1
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int32_t fmt_id = FMTID;
35*49cdfc7eSAndroid Build Coastguard Worker static int test_id, mount_flag;
36*49cdfc7eSAndroid Build Coastguard Worker static struct dqblk set_dq = {
37*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_bsoftlimit = 100,
38*49cdfc7eSAndroid Build Coastguard Worker 	.dqb_valid = QIF_BLIMITS
39*49cdfc7eSAndroid Build Coastguard Worker };
40*49cdfc7eSAndroid Build Coastguard Worker static struct dqblk res_dq;
41*49cdfc7eSAndroid Build Coastguard Worker static struct dqinfo set_qf = {
42*49cdfc7eSAndroid Build Coastguard Worker 	.dqi_bgrace = 80,
43*49cdfc7eSAndroid Build Coastguard Worker 	.dqi_valid = IIF_BGRACE
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker static struct dqinfo res_qf;
47*49cdfc7eSAndroid Build Coastguard Worker static int32_t fmt_buf;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct if_nextdqblk res_ndq;
50*49cdfc7eSAndroid Build Coastguard Worker static int getnextquota_nsup;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
53*49cdfc7eSAndroid Build Coastguard Worker 	int cmd;
54*49cdfc7eSAndroid Build Coastguard Worker 	int *id;
55*49cdfc7eSAndroid Build Coastguard Worker 	void *addr;
56*49cdfc7eSAndroid Build Coastguard Worker 	void *set_data;
57*49cdfc7eSAndroid Build Coastguard Worker 	void *res_data;
58*49cdfc7eSAndroid Build Coastguard Worker 	int sz;
59*49cdfc7eSAndroid Build Coastguard Worker 	char *des;
60*49cdfc7eSAndroid Build Coastguard Worker 	char *tname;
61*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
62*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAON, PRJQUOTA), &fmt_id, NULL,
63*49cdfc7eSAndroid Build Coastguard Worker 	NULL, NULL, 0, "turn on quota for project",
64*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_QUOTAON, PRJQUOTA)"},
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_SETQUOTA, PRJQUOTA), &test_id, &set_dq,
67*49cdfc7eSAndroid Build Coastguard Worker 	NULL, NULL, 0, "set disk quota limit for project",
68*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_SETQUOTA, PRJQUOTA)"},
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_GETQUOTA, PRJQUOTA), &test_id, &res_dq,
71*49cdfc7eSAndroid Build Coastguard Worker 	&set_dq.dqb_bsoftlimit, &res_dq.dqb_bsoftlimit,
72*49cdfc7eSAndroid Build Coastguard Worker 	sizeof(res_dq.dqb_bsoftlimit), "get disk quota limit for project",
73*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_GETQUOTA, PRJQUOTA)"},
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_SETINFO, PRJQUOTA), &test_id, &set_qf,
76*49cdfc7eSAndroid Build Coastguard Worker 	NULL, NULL, 0, "set information about quotafile for project",
77*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_SETINFO, PRJQUOTA"},
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_GETINFO, PRJQUOTA), &test_id, &res_qf,
80*49cdfc7eSAndroid Build Coastguard Worker 	&set_qf.dqi_bgrace, &res_qf.dqi_bgrace, sizeof(res_qf.dqi_bgrace),
81*49cdfc7eSAndroid Build Coastguard Worker 	"get information about quotafile for project",
82*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_GETINFO, PRJQUOTA"},
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_GETFMT, PRJQUOTA), &test_id, &fmt_buf,
85*49cdfc7eSAndroid Build Coastguard Worker 	&fmt_id, &fmt_buf, sizeof(fmt_buf),
86*49cdfc7eSAndroid Build Coastguard Worker 	"get quota format for project", "QCMD(Q_GETFMT, PRJQUOTA)"},
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_GETNEXTQUOTA, PRJQUOTA), &test_id, &res_ndq,
89*49cdfc7eSAndroid Build Coastguard Worker 	&test_id, &res_ndq.dqb_id, sizeof(res_ndq.dqb_id),
90*49cdfc7eSAndroid Build Coastguard Worker 	"get next disk quota limit for project",
91*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_GETNEXTQUOTA, PRJQUOTA)"},
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	{QCMD(Q_QUOTAOFF, PRJQUOTA), &test_id, NULL,
94*49cdfc7eSAndroid Build Coastguard Worker 	NULL, NULL, 0, "turn off quota for project",
95*49cdfc7eSAndroid Build Coastguard Worker 	"QCMD(Q_QUOTAOFF, PRJQUOTA)"},
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker };
98*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)99*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker 	const char *const fs_opts[] = {"-I 256", "-O quota,project", NULL};
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	quotactl_info();
104*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
105*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
106*49cdfc7eSAndroid Build Coastguard Worker 	mount_flag = 1;
107*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(MNTPOINT, O_RDONLY);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	TEST(do_quotactl(fd, QCMD(Q_GETNEXTQUOTA, PRJQUOTA), tst_device->dev,
110*49cdfc7eSAndroid Build Coastguard Worker 		test_id, (void *) &res_ndq));
111*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EINVAL || TST_ERR == ENOSYS)
112*49cdfc7eSAndroid Build Coastguard Worker 		getnextquota_nsup = 1;
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)115*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > -1)
118*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
119*49cdfc7eSAndroid Build Coastguard Worker 	if (mount_flag && tst_umount(MNTPOINT))
120*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "umount(%s)", MNTPOINT);
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker 
verify_quota(unsigned int n)123*49cdfc7eSAndroid Build Coastguard Worker static void verify_quota(unsigned int n)
124*49cdfc7eSAndroid Build Coastguard Worker {
125*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	res_dq.dqb_bsoftlimit = 0;
128*49cdfc7eSAndroid Build Coastguard Worker 	res_qf.dqi_igrace = 0;
129*49cdfc7eSAndroid Build Coastguard Worker 	fmt_buf = 0;
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->cmd == QCMD(Q_GETNEXTQUOTA, PRJQUOTA) && getnextquota_nsup) {
134*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "current system doesn't support this cmd");
135*49cdfc7eSAndroid Build Coastguard Worker 		return;
136*49cdfc7eSAndroid Build Coastguard Worker 	}
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(do_quotactl(fd, tc->cmd, tst_device->dev, *tc->id, tc->addr),
139*49cdfc7eSAndroid Build Coastguard Worker 			"do_quotactl to %s", tc->des);
140*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
141*49cdfc7eSAndroid Build Coastguard Worker 		return;
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	if (memcmp(tc->res_data, tc->set_data, tc->sz)) {
144*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "quotactl failed to %s", tc->des);
145*49cdfc7eSAndroid Build Coastguard Worker 		tst_res_hexd(TINFO, tc->res_data, tc->sz, "retval:   ");
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_res_hexd(TINFO, tc->set_data, tc->sz, "expected: ");
147*49cdfc7eSAndroid Build Coastguard Worker 		return;
148*49cdfc7eSAndroid Build Coastguard Worker 	}
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "quotactl succeeded to %s", tc->des);
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
154*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
155*49cdfc7eSAndroid Build Coastguard Worker 	.needs_drivers = (const char *const []) {
156*49cdfc7eSAndroid Build Coastguard Worker 		"quota_v2",
157*49cdfc7eSAndroid Build Coastguard Worker 		NULL
158*49cdfc7eSAndroid Build Coastguard Worker 	},
159*49cdfc7eSAndroid Build Coastguard Worker 	.min_kver = "4.5", /* commit 689c958cbe6b (ext4: add project quota support) */
160*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_quota,
161*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
162*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
163*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
164*49cdfc7eSAndroid Build Coastguard Worker 	.needs_device = 1,
165*49cdfc7eSAndroid Build Coastguard Worker 	.dev_fs_type = "ext4",
166*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
167*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
168*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cmds = (const char *[]) {
169*49cdfc7eSAndroid Build Coastguard Worker 		"mkfs.ext4 >= 1.43.0",
170*49cdfc7eSAndroid Build Coastguard Worker 		NULL
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker };
173