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) 2022 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 without visible quota files
11*49cdfc7eSAndroid Build Coastguard Worker * (use quotactl and quotactl_fd syscall):
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT when addr or special is invalid
14*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL when cmd or type is invalid
15*49cdfc7eSAndroid Build Coastguard Worker * - ENOTBLK when special is not a block device
16*49cdfc7eSAndroid Build Coastguard Worker * - ERANGE when cmd is Q_SETQUOTA, but the specified limits are out of the range
17*49cdfc7eSAndroid Build Coastguard Worker * allowed by the quota format
18*49cdfc7eSAndroid Build Coastguard Worker * - EPERM when the caller lacked the required privilege (CAP_SYS_ADMIN) for the
19*49cdfc7eSAndroid Build Coastguard Worker * specified operation
20*49cdfc7eSAndroid Build Coastguard Worker * - ENOSYS when cmd is Q_QUOTAON, but the fd refers to a socket
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * Minimum e2fsprogs version required is 1.43.
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/quota.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_capability.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "quotactl_syscall_var.h"
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #define OPTION_INVALID 999
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static int32_t fmt_id = QFMT_VFS_V1;
35*49cdfc7eSAndroid Build Coastguard Worker static int test_id;
36*49cdfc7eSAndroid Build Coastguard Worker static int getnextquota_nsup, socket_fd = -1;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static struct if_nextdqblk res_ndq;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static struct dqblk set_dqmax = {
41*49cdfc7eSAndroid Build Coastguard Worker .dqb_bsoftlimit = 0x7fffffffffffffffLL, /* 2^63-1 */
42*49cdfc7eSAndroid Build Coastguard Worker .dqb_valid = QIF_BLIMITS
43*49cdfc7eSAndroid Build Coastguard Worker };
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cap dropadmin = {
46*49cdfc7eSAndroid Build Coastguard Worker .action = TST_CAP_DROP,
47*49cdfc7eSAndroid Build Coastguard Worker .id = CAP_SYS_ADMIN,
48*49cdfc7eSAndroid Build Coastguard Worker .name = "CAP_SYS_ADMIN",
49*49cdfc7eSAndroid Build Coastguard Worker };
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cap needadmin = {
52*49cdfc7eSAndroid Build Coastguard Worker .action = TST_CAP_REQ,
53*49cdfc7eSAndroid Build Coastguard Worker .id = CAP_SYS_ADMIN,
54*49cdfc7eSAndroid Build Coastguard Worker .name = "CAP_SYS_ADMIN",
55*49cdfc7eSAndroid Build Coastguard Worker };
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
58*49cdfc7eSAndroid Build Coastguard Worker int cmd;
59*49cdfc7eSAndroid Build Coastguard Worker int *id;
60*49cdfc7eSAndroid Build Coastguard Worker void *addr;
61*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
62*49cdfc7eSAndroid Build Coastguard Worker int on_flag;
63*49cdfc7eSAndroid Build Coastguard Worker char *des;
64*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
65*49cdfc7eSAndroid Build Coastguard Worker {QCMD(Q_SETQUOTA, USRQUOTA), &fmt_id, NULL, EFAULT, 1,
66*49cdfc7eSAndroid Build Coastguard Worker "EFAULT when addr or special is invalid"},
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker {QCMD(OPTION_INVALID, USRQUOTA), &fmt_id, NULL, EINVAL, 0,
69*49cdfc7eSAndroid Build Coastguard Worker "EINVAL when cmd or type is invalid"},
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, ENOTBLK, 0,
72*49cdfc7eSAndroid Build Coastguard Worker "ENOTBLK when special is not a block device"},
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker {QCMD(Q_SETQUOTA, USRQUOTA), &test_id, &set_dqmax, ERANGE, 1,
75*49cdfc7eSAndroid Build Coastguard Worker "ERANGE when cmd is Q_SETQUOTA, but the specified limits are out of the range"},
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, EPERM, 0,
78*49cdfc7eSAndroid Build Coastguard Worker "EPERM when the caller lacks required privilege(CAP_SYS_ADMIN)"},
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, ENOSYS, 0,
81*49cdfc7eSAndroid Build Coastguard Worker "EINVAL when cmd is Q_QUOTAON, but the fd refers to a socket"}
82*49cdfc7eSAndroid Build Coastguard Worker };
83*49cdfc7eSAndroid Build Coastguard Worker
verify_quotactl(unsigned int n)84*49cdfc7eSAndroid Build Coastguard Worker static void verify_quotactl(unsigned int n)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
87*49cdfc7eSAndroid Build Coastguard Worker int quota_on = 0;
88*49cdfc7eSAndroid Build Coastguard Worker int drop_flag = 0;
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing %s", tc->des);
91*49cdfc7eSAndroid Build Coastguard Worker if (tc->cmd == QCMD(Q_GETNEXTQUOTA, USRQUOTA) && getnextquota_nsup) {
92*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "current system doesn't support Q_GETNEXTQUOTA");
93*49cdfc7eSAndroid Build Coastguard Worker return;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (tc->on_flag) {
97*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS_SILENT(do_quotactl(fd, QCMD(Q_QUOTAON, USRQUOTA), tst_device->dev,
98*49cdfc7eSAndroid Build Coastguard Worker fmt_id, NULL), "do_quotactl(QCMD(Q_QUOTAON, USRQUOTA))");
99*49cdfc7eSAndroid Build Coastguard Worker if (!TST_PASS)
100*49cdfc7eSAndroid Build Coastguard Worker return;
101*49cdfc7eSAndroid Build Coastguard Worker quota_on = 1;
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == EPERM) {
105*49cdfc7eSAndroid Build Coastguard Worker tst_cap_action(&dropadmin);
106*49cdfc7eSAndroid Build Coastguard Worker drop_flag = 1;
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker if (tst_variant) {
110*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == ENOTBLK) {
111*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "quotactl_fd() doesn't have this error, skip");
112*49cdfc7eSAndroid Build Coastguard Worker return;
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == ENOSYS) {
115*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(syscall(__NR_quotactl_fd, socket_fd, tc->cmd, *tc->id,
116*49cdfc7eSAndroid Build Coastguard Worker tc->addr), tc->exp_err, "syscall(quotactl_fd)");
117*49cdfc7eSAndroid Build Coastguard Worker return;
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker } else {
120*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == ENOSYS) {
121*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "quotactl() doesn't use fd, skip");
122*49cdfc7eSAndroid Build Coastguard Worker return;
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_err == ENOTBLK)
126*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(do_quotactl(fd, tc->cmd, "/dev/null", *tc->id, tc->addr),
127*49cdfc7eSAndroid Build Coastguard Worker ENOTBLK, "do_quotactl()");
128*49cdfc7eSAndroid Build Coastguard Worker else
129*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(do_quotactl(fd, tc->cmd, tst_device->dev, *tc->id, tc->addr),
130*49cdfc7eSAndroid Build Coastguard Worker tc->exp_err, "do_quotactl()");
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker if (quota_on) {
133*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS_SILENT(do_quotactl(fd, QCMD(Q_QUOTAOFF, USRQUOTA), tst_device->dev,
134*49cdfc7eSAndroid Build Coastguard Worker fmt_id, NULL), "do_quotactl(QCMD(Q_QUOTAOFF, USRQUOTA)");
135*49cdfc7eSAndroid Build Coastguard Worker if (!TST_PASS)
136*49cdfc7eSAndroid Build Coastguard Worker return;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker if (drop_flag)
140*49cdfc7eSAndroid Build Coastguard Worker tst_cap_action(&needadmin);
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker
setup(void)143*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker quotactl_info();
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker socket_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
150*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(MNTPOINT, O_RDONLY);
151*49cdfc7eSAndroid Build Coastguard Worker TEST(do_quotactl(fd, QCMD(Q_GETNEXTQUOTA, USRQUOTA), tst_device->dev,
152*49cdfc7eSAndroid Build Coastguard Worker test_id, (void *) &res_ndq));
153*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EINVAL || TST_ERR == ENOSYS)
154*49cdfc7eSAndroid Build Coastguard Worker getnextquota_nsup = 1;
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tcases); i++) {
157*49cdfc7eSAndroid Build Coastguard Worker if (!tcases[i].addr)
158*49cdfc7eSAndroid Build Coastguard Worker tcases[i].addr = tst_get_bad_addr(NULL);
159*49cdfc7eSAndroid Build Coastguard Worker }
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)162*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
163*49cdfc7eSAndroid Build Coastguard Worker {
164*49cdfc7eSAndroid Build Coastguard Worker if (fd > -1)
165*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
166*49cdfc7eSAndroid Build Coastguard Worker if (socket_fd > -1)
167*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(socket_fd);
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
171*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
172*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
173*49cdfc7eSAndroid Build Coastguard Worker .needs_drivers = (const char *const []) {
174*49cdfc7eSAndroid Build Coastguard Worker "quota_v2",
175*49cdfc7eSAndroid Build Coastguard Worker NULL
176*49cdfc7eSAndroid Build Coastguard Worker },
177*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
178*49cdfc7eSAndroid Build Coastguard Worker .test = verify_quotactl,
179*49cdfc7eSAndroid Build Coastguard Worker .dev_fs_opts = (const char *const[]){"-O quota", NULL},
180*49cdfc7eSAndroid Build Coastguard Worker .dev_fs_type = "ext4",
181*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
182*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
183*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
184*49cdfc7eSAndroid Build Coastguard Worker .test_variants = QUOTACTL_SYSCALL_VARIANTS,
185*49cdfc7eSAndroid Build Coastguard Worker .needs_cmds = (const char *[]) {
186*49cdfc7eSAndroid Build Coastguard Worker "mkfs.ext4 >= 1.43.0",
187*49cdfc7eSAndroid Build Coastguard Worker NULL
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker };
190