aboutsummaryrefslogtreecommitdiff
path: root/tests-mx32/quotactl-xfs.c
blob: fab451e22820e904189bf7c281dc39d5493611e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Check decoding of quotactl xfs subcommands.
 *
 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
 * Copyright (c) 2016-2017 The strace developers.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "tests.h"

#include <asm/unistd.h>

#if defined(__NR_quotactl) && \
	(defined(HAVE_LINUX_QUOTA_H) || defined(HAVE_SYS_QUOTA_H)) && \
	defined(HAVE_LINUX_DQBLK_XFS_H)

# include <stdio.h>
# include <string.h>
# include <unistd.h>

# include <linux/dqblk_xfs.h>

# include "quotactl.h"

# ifndef Q_GETNEXTQUOTA
#  define Q_XGETNEXTQUOTA	XQM_CMD(0x9)
# endif /* !Q_GETNEXTQUOTA */

# ifndef Q_XGETQSTATV

#  define Q_XGETQSTATV		XQM_CMD(8)
#  define FS_QSTATV_VERSION1	1

struct fs_qfilestatv {
	uint64_t	qfs_ino;	/* inode number */
	uint64_t	qfs_nblks;	/* number of BBs 512-byte-blks */
	uint32_t	qfs_nextents;	/* number of extents */
	uint32_t	qfs_pad;	/* pad for 8-byte alignment */
};

struct fs_quota_statv {
	int8_t		qs_version;		/* version for future changes */
	uint8_t		qs_pad1;		/* pad for 16bit alignment */
	uint16_t	qs_flags;		/* XFS_QUOTA_.* flags */
	uint32_t	qs_incoredqs;		/* number of dquots incore */
	struct fs_qfilestatv	qs_uquota;	/* user quota information */
	struct fs_qfilestatv	qs_gquota;	/* group quota information */
	struct fs_qfilestatv	qs_pquota;	/* project quota information */
	int32_t		qs_btimelimit;		/* limit for blks timer */
	int32_t		qs_itimelimit;		/* limit for inodes timer */
	int32_t		qs_rtbtimelimit;	/* limit for rt blks timer */
	uint16_t	qs_bwarnlimit;		/* limit for num warnings */
	uint16_t	qs_iwarnlimit;		/* limit for num warnings */
	uint64_t	qs_pad2[8];		/* for future proofing */
};

# endif /* !Q_XGETQSTATV */

# include "xlat.h"
# include "xlat/xfs_dqblk_flags.h"
# if VERBOSE
#  include "xlat/xfs_quota_flags.h"
# endif


void
print_xdisk_quota(int rc, void *ptr, void *arg)
{
	struct fs_disk_quota *dq = ptr;
	long out_arg = (long) arg;

	if (((rc != 0) && out_arg) || (out_arg > 1)) {
		printf("%p", dq);
		return;
	}

	PRINT_FIELD_D("{", *dq, d_version);
	printf(", d_flags=");
	printflags(xfs_dqblk_flags, (uint8_t) dq->d_flags, "XFS_???_QUOTA");

	PRINT_FIELD_X(", ", *dq, d_fieldmask);
	PRINT_FIELD_U(", ", *dq, d_id);
	PRINT_FIELD_U(", ", *dq, d_blk_hardlimit);
	PRINT_FIELD_U(", ", *dq, d_blk_softlimit);
	PRINT_FIELD_U(", ", *dq, d_ino_hardlimit);
	PRINT_FIELD_U(", ", *dq, d_ino_softlimit);
	PRINT_FIELD_U(", ", *dq, d_bcount);
	PRINT_FIELD_U(", ", *dq, d_icount);

# if VERBOSE
	PRINT_FIELD_D(", ", *dq, d_itimer);
	PRINT_FIELD_D(", ", *dq, d_btimer);
	PRINT_FIELD_U(", ", *dq, d_iwarns);
	PRINT_FIELD_U(", ", *dq, d_bwarns);
	PRINT_FIELD_U(", ", *dq, d_rtb_hardlimit);
	PRINT_FIELD_U(", ", *dq, d_rtb_softlimit);
	PRINT_FIELD_U(", ", *dq, d_rtbcount);
	PRINT_FIELD_D(", ", *dq, d_rtbtimer);
	PRINT_FIELD_U(", ", *dq, d_rtbwarns);
# else
	printf(", ...");
# endif /* !VERBOSE */
	printf("}");
}

void
print_xquota_stat(int rc, void *ptr, void *arg)
{
	struct fs_quota_stat *qs = ptr;
	long out_arg = (long) arg;

	if (((rc != 0) && out_arg) || (out_arg > 1)) {
		printf("%p", qs);
		return;
	}

	PRINT_FIELD_D("{", *qs, qs_version);

# if VERBOSE
	printf(", qs_flags=");
	printflags(xfs_quota_flags, qs->qs_flags, "XFS_QUOTA_???");
	PRINT_FIELD_U(", qs_uquota={", qs->qs_uquota, qfs_ino);
	PRINT_FIELD_U(", ", qs->qs_uquota, qfs_nblks);
	PRINT_FIELD_U(", ", qs->qs_uquota, qfs_nextents);
	PRINT_FIELD_U("}, qs_gquota={", qs->qs_gquota, qfs_ino);
	PRINT_FIELD_U(", ", qs->qs_gquota, qfs_nblks);
	PRINT_FIELD_U(", ", qs->qs_gquota, qfs_nextents);
	PRINT_FIELD_U("}, ", *qs, qs_incoredqs);
	PRINT_FIELD_D(", ", *qs, qs_btimelimit);
	PRINT_FIELD_D(", ", *qs, qs_itimelimit);
	PRINT_FIELD_D(", ", *qs, qs_rtbtimelimit);
	PRINT_FIELD_U(", ", *qs, qs_bwarnlimit);
	PRINT_FIELD_U(", ", *qs, qs_iwarnlimit);
# else
	printf(", ...");
# endif /* !VERBOSE */
	printf("}");
}

void
print_xquota_statv(int rc, void *ptr, void *arg)
{
	struct fs_quota_statv *qs = ptr;
	long out_arg = (long) arg;

	if (((rc != 0) && out_arg) || (out_arg > 1)) {
		printf("%p", qs);
		return;
	}

	PRINT_FIELD_D("{", *qs, qs_version);

# if VERBOSE
	printf(", qs_flags=");
	printflags(xfs_quota_flags, qs->qs_flags, "XFS_QUOTA_???");
	PRINT_FIELD_U(", ", *qs, qs_incoredqs);
	PRINT_FIELD_U(", qs_uquota={", qs->qs_uquota, qfs_ino);
	PRINT_FIELD_U(", ", qs->qs_uquota, qfs_nblks);
	PRINT_FIELD_U(", ", qs->qs_uquota, qfs_nextents);
	PRINT_FIELD_U("}, qs_gquota={", qs->qs_gquota, qfs_ino);
	PRINT_FIELD_U(", ", qs->qs_gquota, qfs_nblks);
	PRINT_FIELD_U(", ", qs->qs_gquota, qfs_nextents);
	PRINT_FIELD_U("}, qs_pquota={", qs->qs_pquota, qfs_ino);
	PRINT_FIELD_U(", ", qs->qs_pquota, qfs_nblks);
	PRINT_FIELD_U(", ", qs->qs_pquota, qfs_nextents);
	PRINT_FIELD_D("}, ", *qs, qs_btimelimit);
	PRINT_FIELD_D(", ", *qs, qs_itimelimit);
	PRINT_FIELD_D(", ", *qs, qs_rtbtimelimit);
	PRINT_FIELD_U(", ", *qs, qs_bwarnlimit);
	PRINT_FIELD_U(", ", *qs, qs_iwarnlimit);
# else
	printf(", ...");
# endif /* !VERBOSE */
	printf("}");
}

int
main(void)
{
	char *bogus_special = (char *) tail_alloc(1) + 1;
	void *bogus_addr = (char *) tail_alloc(1) + 1;

	char bogus_special_str[sizeof(void *) * 2 + sizeof("0x")];
	char bogus_addr_str[sizeof(void *) * 2 + sizeof("0x")];
	char unterminated_str[sizeof(void *) * 2 + sizeof("0x")];

	long rc;
	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_disk_quota, xdq);
	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_quota_stat, xqstat);
	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_quota_statv, xqstatv);
	TAIL_ALLOC_OBJECT_CONST_PTR(uint32_t, flags);
	char *unterminated = tail_memdup(unterminated_data,
		sizeof(unterminated_data));

	snprintf(bogus_special_str, sizeof(bogus_special_str), "%p",
		 bogus_special);
	snprintf(bogus_addr_str, sizeof(bogus_addr_str), "%p",
		 bogus_addr);
	snprintf(unterminated_str, sizeof(unterminated_str), "%p",
		 unterminated);


	/* Q_XQUOTAON */

	*flags = 0xdeadbeef;

	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    ARG_STR(QCMD(Q_XQUOTAON, USRQUOTA)),
		    ARG_STR("/dev/bogus/"), flags,
		    "[XFS_QUOTA_UDQ_ACCT|XFS_QUOTA_UDQ_ENFD"
		    "|XFS_QUOTA_GDQ_ACCT|XFS_QUOTA_GDQ_ENFD"
		    "|XFS_QUOTA_PDQ_ENFD|0xdeadbec0]");

	rc = syscall(__NR_quotactl, QCMD(Q_XQUOTAON, 0xfacefeed), bogus_dev,
		     bogus_id, bogus_addr);
	printf("quotactl(QCMD(Q_XQUOTAON, %#x /* ???QUOTA */)"
	       ", %s, %p) = %s\n",
	       QCMD_TYPE(QCMD(Q_XQUOTAON, 0xfacefeed)),
	       bogus_dev_str, bogus_addr, sprintrc(rc));


	/* Q_XQUOTAOFF */

	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    ARG_STR(QCMD(Q_XQUOTAOFF, USRQUOTA)),
		    bogus_special, bogus_special_str,
		    bogus_addr, bogus_addr_str);
	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    ARG_STR(QCMD(Q_XQUOTAOFF, GRPQUOTA)),
		    ARG_STR("/dev/bogus/"),
		    ARG_STR(NULL));
	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    QCMD(Q_XQUOTAOFF, 3),
		    "QCMD(Q_XQUOTAOFF, 0x3 /* ???QUOTA */)",
		    ARG_STR("/dev/bogus/"), flags,
		    "[XFS_QUOTA_UDQ_ACCT|XFS_QUOTA_UDQ_ENFD"
		    "|XFS_QUOTA_GDQ_ACCT|XFS_QUOTA_GDQ_ENFD"
		    "|XFS_QUOTA_PDQ_ENFD|0xdeadbec0]");


	/* Q_XGETQUOTA */

	/* Trying our best to get successful result */
	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, USRQUOTA)),
		    ARG_STR("/dev/sda1"), getuid(), xdq, print_xdisk_quota,
		    (intptr_t) 1);

	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, GRPQUOTA)),
		    ARG_STR(NULL), -1, xdq, print_xdisk_quota, (intptr_t) 2);


	/* Q_XGETNEXTQUOTA */

	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_XGETNEXTQUOTA, USRQUOTA)),
		    ARG_STR("/dev/sda1"), 0, xdq, print_xdisk_quota,
		    (intptr_t) 1);


	/* Q_XSETQLIM */

	check_quota(CQF_NONE, ARG_STR(QCMD(Q_XSETQLIM, PRJQUOTA)),
		    bogus_special, bogus_special_str, 0, bogus_addr);

	fill_memory_ex(xdq, sizeof(*xdq), 0x8e, 0x80);

	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_XSETQLIM, PRJQUOTA)),
		    bogus_dev, bogus_dev_str, 3141592653U,
		    xdq, print_xdisk_quota, (intptr_t) 0);


	/* Q_XGETQSTAT */

	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
		    ARG_STR(QCMD(Q_XGETQSTAT, USRQUOTA)),
		    ARG_STR("/dev/sda1"), xqstat, print_xquota_stat, (intptr_t) 1);

	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
		    ARG_STR(QCMD(Q_XGETQSTATV, PRJQUOTA)),
		    unterminated, unterminated_str,
		    xqstat + 1, print_xquota_stat, (intptr_t) 2);


	/* Q_XGETQSTATV */

	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
		    ARG_STR(QCMD(Q_XGETQSTAT, USRQUOTA)),
		    ARG_STR("/dev/sda1"), xqstatv, print_xquota_statv, 1);

	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
		    ARG_STR(QCMD(Q_XGETQSTATV, GRPQUOTA)),
		    ARG_STR(NULL), xqstatv, print_xquota_statv, (intptr_t) 2);


	/* Q_XQUOTARM */

	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    ARG_STR(QCMD(Q_XQUOTARM, PRJQUOTA)),
		    bogus_special, bogus_special_str, ARG_STR(NULL));
	check_quota(CQF_ID_SKIP,
		    ARG_STR(QCMD(Q_XQUOTARM, USRQUOTA)),
		    unterminated, unterminated_str, flags + 1);

	*flags = 0xdeadbeef;
	check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
		    ARG_STR(QCMD(Q_XQUOTARM, GRPQUOTA)),
		    ARG_STR(NULL), flags,
		    "[XFS_USER_QUOTA|XFS_PROJ_QUOTA"
		    "|XFS_GROUP_QUOTA|0xdeadbee8]");


	/* Q_XQUOTASYNC */

	check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
		    ARG_STR(QCMD(Q_XQUOTASYNC, USRQUOTA)),
		    bogus_special, bogus_special_str);
	check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
		    QCMD(Q_XQUOTASYNC, 0xfff),
		    "QCMD(Q_XQUOTASYNC, 0xff /* ???QUOTA */)",
		    ARG_STR(NULL));

	puts("+++ exited with 0 +++");

	return 0;
}

#else

SKIP_MAIN_UNDEFINED("__NR_quotactl && "
	"(HAVE_LINUX_QUOTA_H || HAVE_SYS_QUOTA_H) && "
	"HAVE_LINUX_DQBLK_XFS_H");

#endif