1 /*
2 * Copyright (c) Crackerjack Project., 2007
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 *
6 * This program is free software;  you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.
18 *
19 * Test Name: quotactl01
20 *
21 * Description:
22 * This testcase checks the basic flag of quotactl(2) for non-XFS filesystems:
23 * 1) quotactl(2) succeeds to turn on quota with Q_QUOTAON flag for user.
24 * 2) quotactl(2) succeeds to set disk quota limits with Q_SETQUOTA flag
25 *    for user.
26 * 3) quotactl(2) succeeds to get disk quota limits with Q_GETQUOTA flag
27 *    for user.
28 * 4) quotactl(2) succeeds to set information about quotafile with Q_SETINFO
29 *    flag for user.
30 * 5) quotactl(2) succeeds to get information about quotafile with Q_GETINFO
31 *    flag for user.
32 * 6) quotactl(2) succeeds to get quota format with Q_GETFMT flag for user.
33 * 7) quotactl(2) succeeds to update quota usages with Q_SYNC flag for user.
34 * 8) quotactl(2) succeeds to turn off quota with Q_QUOTAOFF flag for user.
35 * 9) quotactl(2) succeeds to turn on quota with Q_QUOTAON flag for group.
36 * 10) quotactl(2) succeeds to set disk quota limits with Q_SETQUOTA flag
37 *     for group.
38 * 11) quotactl(2) succeeds to get disk quota limits with Q_GETQUOTA flag
39 *     for group.
40 * 12) quotactl(2) succeeds to set information about quotafile with Q_SETINFO
41 *     flag for group.
42 * 13) quotactl(2) succeeds to get information about quotafile with Q_GETINFO
43 *     flag for group.
44 * 14) quotactl(2) succeeds to get quota format with Q_GETFMT flag for group.
45 * 15) quotactl(2) succeeds to update quota usages with Q_SYNC flag for group.
46 * 16) quotactl(2) succeeds to turn off quota with Q_QUOTAOFF flag for group.
47 *
48 */
49 
50 #include <errno.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include "config.h"
55 
56 #include "tst_test.h"
57 
58 #if defined(HAVE_QUOTAV2) || defined(HAVE_QUOTAV1)
59 # include <sys/quota.h>
60 
61 # if defined(HAVE_QUOTAV2)
62 #  define _LINUX_QUOTA_VERSION  2
63 #  ifndef QFMT_VFS_V0
64 #   define QFMT_VFS_V0     2
65 #  endif
66 #  define USRPATH MNTPOINT "/aquota.user"
67 #  define GRPPATH MNTPOINT "/aquota.group"
68 #  define FMTID	QFMT_VFS_V0
69 # else
70 #  define _LINUX_QUOTA_VERSION  1
71 #  ifndef QFMT_VFS_OLD
72 #   define QFMT_VFS_OLD    1
73 #  endif
74 #  define USRPATH MNTPOINT "/quota.user"
75 #  define GRPPATH MNTPOINT "/quota.group"
76 #  define FMTID	QFMT_VFS_OLD
77 # endif
78 
79 # define MNTPOINT	"mntpoint"
80 
81 static int32_t fmt_id = FMTID;
82 static int test_id;
83 static struct dqblk set_dq = {
84 	.dqb_bsoftlimit = 100,
85 	.dqb_valid = QIF_BLIMITS
86 };
87 static struct dqblk res_dq;
88 # if defined(HAVE_QUOTAV2)
89 static struct dqinfo set_qf = {
90 	.dqi_bgrace = 80,
91 	.dqi_valid = IIF_BGRACE
92 };
93 static struct dqinfo res_qf;
94 static int32_t fmt_buf;
95 # endif
96 
97 static struct tcase {
98 	int cmd;
99 	int *id;
100 	void *addr;
101 	void *set_data;
102 	void *res_data;
103 	int sz;
104 	char *des;
105 } tcases[] = {
106 	{QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, USRPATH,
107 	NULL, NULL, 0, "turn on quota for user"},
108 
109 	{QCMD(Q_SETQUOTA, USRQUOTA), &test_id, &set_dq,
110 	NULL, NULL, 0, "set disk quota limit for user"},
111 
112 	{QCMD(Q_GETQUOTA, USRQUOTA), &test_id, &res_dq,
113 	&set_dq.dqb_bsoftlimit, &res_dq.dqb_bsoftlimit,
114 	sizeof(res_dq.dqb_bsoftlimit), "get disk quota limit for user"},
115 # if defined(HAVE_QUOTAV2)
116 	{QCMD(Q_SETINFO, USRQUOTA), &test_id, &set_qf,
117 	NULL, NULL, 0, "set information about quotafile for user"},
118 
119 	{QCMD(Q_GETINFO, USRQUOTA), &test_id, &res_qf,
120 	&set_qf.dqi_bgrace, &res_qf.dqi_bgrace, sizeof(res_qf.dqi_bgrace),
121 	"get information about quotafile for user"},
122 
123 	{QCMD(Q_GETFMT, USRQUOTA), &test_id, &fmt_buf,
124 	&fmt_id, &fmt_buf, sizeof(fmt_buf),
125 	"get quota format for user"},
126 # endif
127 	{QCMD(Q_SYNC, USRQUOTA), &test_id, &res_dq,
128 	NULL, NULL, 0, "update quota usages for user"},
129 
130 	{QCMD(Q_QUOTAOFF, USRQUOTA), &test_id, USRPATH,
131 	NULL, NULL, 0, "turn off quota for user"},
132 
133 	{QCMD(Q_QUOTAON, GRPQUOTA), &fmt_id, GRPPATH,
134 	NULL, NULL, 0, "turn on quota for group"},
135 
136 	{QCMD(Q_SETQUOTA, GRPQUOTA), &test_id, &set_dq,
137 	NULL, NULL, 0, "set disk quota limit for group"},
138 
139 	{QCMD(Q_GETQUOTA, GRPQUOTA), &test_id, &res_dq, &set_dq.dqb_bsoftlimit,
140 	&res_dq.dqb_bsoftlimit, sizeof(res_dq.dqb_bsoftlimit),
141 	"set disk quota limit for group"},
142 # if defined(HAVE_QUOTAV2)
143 	{QCMD(Q_SETINFO, GRPQUOTA), &test_id, &set_qf,
144 	NULL, NULL, 0, "set information about quotafile for group"},
145 
146 	{QCMD(Q_GETINFO, GRPQUOTA), &test_id, &res_qf, &set_qf.dqi_bgrace,
147 	&res_qf.dqi_bgrace, sizeof(res_qf.dqi_bgrace),
148 	"get information about quotafile for group"},
149 
150 	{QCMD(Q_GETFMT, GRPQUOTA), &test_id, &fmt_buf,
151 	&fmt_id, &fmt_buf, sizeof(fmt_buf), "get quota format for group"},
152 # endif
153 	{QCMD(Q_SYNC, GRPQUOTA), &test_id, &res_dq,
154 	NULL, NULL, 0, "update quota usages for group"},
155 
156 	{QCMD(Q_QUOTAOFF, GRPQUOTA), &test_id, GRPPATH,
157 	NULL, NULL, 0, "turn off quota for group"}
158 };
159 
setup(void)160 static void setup(void)
161 {
162 	const char *const cmd[] = {"quotacheck", "-ug", MNTPOINT, NULL};
163 	int ret;
164 
165 
166 	ret = tst_run_cmd(cmd, NULL, NULL, 1);
167 	switch (ret) {
168 	case 255:
169 		tst_brk(TCONF, "quotacheck binary not installed");
170 	default:
171 		tst_brk(TBROK, "quotacheck exited with %i", ret);
172 	case 0:
173 	break;
174 	}
175 
176 	test_id = geteuid();
177 
178 	if (access(USRPATH, F_OK) == -1)
179 		tst_brk(TFAIL | TERRNO, "user quotafile didn't exist");
180 
181 	if (access(GRPPATH, F_OK) == -1)
182 		tst_brk(TFAIL | TERRNO, "group quotafile didn't exist");
183 }
184 
verify_quota(unsigned int n)185 static void verify_quota(unsigned int n)
186 {
187 	struct tcase *tc = &tcases[n];
188 
189 	res_dq.dqb_bsoftlimit = 0;
190 	res_qf.dqi_igrace = 0;
191 	fmt_buf = 0;
192 
193 	TEST(quotactl(tc->cmd, tst_device->dev, *tc->id, tc->addr));
194 	if (TST_RET == -1) {
195 		tst_res(TFAIL | TTERRNO, "quotactl failed to %s", tc->des);
196 		return;
197 	}
198 
199 	if (memcmp(tc->res_data, tc->set_data, tc->sz)) {
200 		tst_res(TFAIL, "quotactl failed to %s", tc->des);
201 		tst_res_hexd(TINFO, tc->res_data, tc->sz, "retval:   ");
202 		tst_res_hexd(TINFO, tc->set_data, tc->sz, "expected: ");
203 		return;
204 	}
205 
206 	tst_res(TPASS, "quotactl succeeded to %s", tc->des);
207 }
208 
209 static struct tst_test test = {
210 	.needs_tmpdir = 1,
211 	.needs_root = 1,
212 	.test = verify_quota,
213 	.tcnt = ARRAY_SIZE(tcases),
214 	.mount_device = 1,
215 	.dev_fs_type = "ext4",
216 	.mntpoint = MNTPOINT,
217 	.mnt_data = "usrquota,grpquota",
218 	.setup = setup,
219 };
220 
221 #else
222 	TST_TEST_TCONF("This system didn't support quota");
223 #endif
224