1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5 *
6 * This testcase checks the basic flag of quotactl(2) for project quota on
7 * non-XFS filesystems.
8 *
9 * 1) quotactl(2) succeeds to turn on quota with Q_QUOTAON flag for project.
10 * 2) quotactl(2) succeeds to set disk quota limits with Q_SETQUOTA flag
11 * for project.
12 * 3) quotactl(2) succeeds to get disk quota limits with Q_GETQUOTA flag
13 * for project.
14 * 4) quotactl(2) succeeds to set information about quotafile with Q_SETINFO
15 * flag for project.
16 * 5) quotactl(2) succeeds to get information about quotafile with Q_GETINFO
17 * flag for project.
18 * 6) quotactl(2) succeeds to get quota format with Q_GETFMT flag for project.
19 * 7) quotactl(2) succeeds to get disk quota limit greater than or equal to
20 * ID with Q_GETNEXTQUOTA flag for project.
21 * 8) quotactl(2) succeeds to turn off quota with Q_QUOTAOFF flag for project.
22 */
23
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <sys/stat.h>
29 #include "config.h"
30 #include "lapi/quotactl.h"
31 #include "tst_test.h"
32
33 #ifndef QFMT_VFS_V1
34 # define QFMT_VFS_V1 4
35 #endif
36
37 #define FMTID QFMT_VFS_V1
38 #define MNTPOINT "mntpoint"
39 static int32_t fmt_id = FMTID;
40 static int test_id, mount_flag;
41 static struct dqblk set_dq = {
42 .dqb_bsoftlimit = 100,
43 .dqb_valid = QIF_BLIMITS
44 };
45 static struct dqblk res_dq;
46 static struct dqinfo set_qf = {
47 .dqi_bgrace = 80,
48 .dqi_valid = IIF_BGRACE
49 };
50
51 static struct dqinfo res_qf;
52 static int32_t fmt_buf;
53
54 static struct if_nextdqblk res_ndq;
55
56 static struct tcase {
57 int cmd;
58 int *id;
59 void *addr;
60 void *set_data;
61 void *res_data;
62 int sz;
63 char *des;
64 char *tname;
65 } tcases[] = {
66 {QCMD(Q_QUOTAON, PRJQUOTA), &fmt_id, NULL,
67 NULL, NULL, 0, "turn on quota for project",
68 "QCMD(Q_QUOTAON, PRJQUOTA)"},
69
70 {QCMD(Q_SETQUOTA, PRJQUOTA), &test_id, &set_dq,
71 NULL, NULL, 0, "set disk quota limit for project",
72 "QCMD(Q_SETQUOTA, PRJQUOTA)"},
73
74 {QCMD(Q_GETQUOTA, PRJQUOTA), &test_id, &res_dq,
75 &set_dq.dqb_bsoftlimit, &res_dq.dqb_bsoftlimit,
76 sizeof(res_dq.dqb_bsoftlimit), "get disk quota limit for project",
77 "QCMD(Q_GETQUOTA, PRJQUOTA)"},
78
79 {QCMD(Q_SETINFO, PRJQUOTA), &test_id, &set_qf,
80 NULL, NULL, 0, "set information about quotafile for project",
81 "QCMD(Q_SETINFO, PRJQUOTA"},
82
83 {QCMD(Q_GETINFO, PRJQUOTA), &test_id, &res_qf,
84 &set_qf.dqi_bgrace, &res_qf.dqi_bgrace, sizeof(res_qf.dqi_bgrace),
85 "get information about quotafile for project",
86 "QCMD(Q_GETINFO, PRJQUOTA"},
87
88 {QCMD(Q_GETFMT, PRJQUOTA), &test_id, &fmt_buf,
89 &fmt_id, &fmt_buf, sizeof(fmt_buf),
90 "get quota format for project", "QCMD(Q_GETFMT, PRJQUOTA)"},
91
92 {QCMD(Q_GETNEXTQUOTA, PRJQUOTA), &test_id, &res_ndq,
93 &test_id, &res_ndq.dqb_id, sizeof(res_ndq.dqb_id),
94 "get next disk quota limit for project",
95 "QCMD(Q_GETNEXTQUOTA, PRJQUOTA)"},
96
97 {QCMD(Q_QUOTAOFF, PRJQUOTA), &test_id, NULL,
98 NULL, NULL, 0, "turn off quota for project",
99 "QCMD(Q_QUOTAOFF, PRJQUOTA)"},
100
101 };
102
setup(void)103 static void setup(void)
104 {
105 const char *const fs_opts[] = {"-I 256", "-O quota,project", NULL};
106
107 test_id = geteuid();
108 SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
109 SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, "quota");
110 mount_flag = 1;
111 }
112
cleanup(void)113 static void cleanup(void)
114 {
115 if (mount_flag && tst_umount(MNTPOINT))
116 tst_res(TWARN | TERRNO, "umount(%s)", MNTPOINT);
117 }
118
verify_quota(unsigned int n)119 static void verify_quota(unsigned int n)
120 {
121 struct tcase *tc = &tcases[n];
122
123 res_dq.dqb_bsoftlimit = 0;
124 res_qf.dqi_igrace = 0;
125 fmt_buf = 0;
126
127 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
128
129 TEST(quotactl(tc->cmd, tst_device->dev, *tc->id, tc->addr));
130 if (TST_RET == -1) {
131 tst_res(TFAIL | TTERRNO, "quotactl failed to %s", tc->des);
132 return;
133 }
134
135 if (memcmp(tc->res_data, tc->set_data, tc->sz)) {
136 tst_res(TFAIL, "quotactl failed to %s", tc->des);
137 tst_res_hexd(TINFO, tc->res_data, tc->sz, "retval: ");
138 tst_res_hexd(TINFO, tc->set_data, tc->sz, "expected: ");
139 return;
140 }
141
142 tst_res(TPASS, "quotactl succeeded to %s", tc->des);
143 }
144
145 static const char *kconfigs[] = {
146 "CONFIG_QFMT_V2",
147 NULL
148 };
149
150 static struct tst_test test = {
151 .needs_root = 1,
152 .needs_kconfigs = kconfigs,
153 .min_kver = "4.10", /* commit 689c958cbe6b (ext4: add project quota support) */
154 .test = verify_quota,
155 .tcnt = ARRAY_SIZE(tcases),
156 .setup = setup,
157 .cleanup = cleanup,
158 .needs_device = 1,
159 .dev_fs_type = "ext4",
160 .mntpoint = MNTPOINT,
161 };
162