1 /* $NetBSD: h_quota2_tests.c,v 1.4 2012/09/30 21:26:57 bouyer Exp $ */
4 * rump server for advanced quota tests
5 * this one includes functions to run against the filesystem before
6 * starting to handle rump requests from clients.
9 #include "../common/h_fsmacros.h"
12 #include <semaphore.h>
13 #include <sys/types.h>
14 #include <sys/mount.h>
19 #include <ufs/ufs/ufsmount.h>
20 #include <dev/fssvar.h>
22 #include <rump/rump.h>
23 #include <rump/rump_syscalls.h>
25 #include "../../h_macros.h"
29 #define TEST_NONROOT_ID 1
32 quota_test0(const char *testopts
)
39 for (i
=0; testopts
&& i
< strlen(testopts
); i
++) {
45 errx(1, "test4: unknown option %c", testopts
[i
]);
49 rump_sys_chown(".", TEST_NONROOT_ID
, TEST_NONROOT_ID
);
50 rump_sys_chmod(".", 0777);
51 if (rump_sys_setegid(TEST_NONROOT_ID
) != 0) {
53 warn("rump_sys_setegid");
56 if (rump_sys_seteuid(TEST_NONROOT_ID
) != 0) {
58 warn("rump_sys_seteuid");
61 fd
= rump_sys_open("test_fillup", O_CREAT
| O_RDWR
, 0644);
64 warn("rump_sys_open");
66 while (rump_sys_write(fd
, buf
, sizeof(buf
)) == sizeof(buf
))
77 quota_test1(const char *testopts
)
82 rump_sys_chown(".", TEST_NONROOT_ID
, TEST_NONROOT_ID
);
83 rump_sys_chmod(".", 0777);
84 if (rump_sys_setegid(TEST_NONROOT_ID
) != 0) {
86 warn("rump_sys_setegid");
89 if (rump_sys_seteuid(TEST_NONROOT_ID
) != 0) {
91 warn("rump_sys_seteuid");
94 fd
= rump_sys_open("test_fillup", O_CREAT
| O_RDWR
, 0644);
97 warn("rump_sys_open");
100 * write up to the soft limit, wait a bit, an try to
105 /* write 2k: with the directory this makes 2.5K */
106 for (i
= 0; i
< 4; i
++) {
107 error
= rump_sys_write(fd
, buf
, sizeof(buf
));
108 if (error
!= sizeof(buf
))
109 err(1, "write failed early");
112 /* now try to write an extra .5k */
113 if (rump_sys_write(fd
, buf
, sizeof(buf
)) != sizeof(buf
))
125 quota_test2(const char *testopts
)
127 static char buf
[512];
131 rump_sys_chown(".", TEST_NONROOT_ID
, TEST_NONROOT_ID
);
132 rump_sys_chmod(".", 0777);
133 if (rump_sys_setegid(TEST_NONROOT_ID
) != 0) {
135 warn("rump_sys_setegid");
138 if (rump_sys_seteuid(TEST_NONROOT_ID
) != 0) {
140 warn("rump_sys_seteuid");
145 sprintf(buf
, "file%d", i
);
146 fd
= rump_sys_open(buf
, O_CREAT
| O_RDWR
, 0644);
149 sprintf(buf
, "test file no %d", i
);
150 rump_sys_write(fd
, buf
, strlen(buf
));
162 quota_test3(const char *testopts
)
164 static char buf
[512];
168 rump_sys_chown(".", TEST_NONROOT_ID
, TEST_NONROOT_ID
);
169 rump_sys_chmod(".", 0777);
170 if (rump_sys_setegid(TEST_NONROOT_ID
) != 0) {
172 warn("rump_sys_setegid");
175 if (rump_sys_seteuid(TEST_NONROOT_ID
) != 0) {
177 warn("rump_sys_seteuid");
182 * create files one past the soft limit: one less as we already own the
185 for (i
= 0; i
< 4; i
++) {
186 sprintf(buf
, "file%d", i
);
187 fd
= rump_sys_open(buf
, O_EXCL
| O_CREAT
| O_RDWR
, 0644);
189 err(1, "file create failed early");
190 sprintf(buf
, "test file no %d", i
);
191 rump_sys_write(fd
, buf
, strlen(buf
));
194 /* now create an extra file after grace time: this should fail */
196 sprintf(buf
, "file%d", i
);
197 fd
= rump_sys_open(buf
, O_EXCL
| O_CREAT
| O_RDWR
, 0644);
210 quota_test4(const char *testopts
)
212 static char buf
[512];
220 * take an internal snapshot of the filesystem, and create a new
221 * file with some data
223 rump_sys_chown(".", 0, 0);
224 rump_sys_chmod(".", 0777);
226 for (i
=0; testopts
&& i
< strlen(testopts
); i
++) {
227 switch(testopts
[i
]) {
235 errx(1, "test4: unknown option %c", testopts
[i
]);
239 /* first create the snapshot */
241 fd
= rump_sys_open(FSTEST_MNTNAME
"/le_snap", O_CREAT
| O_RDWR
, 0777);
243 err(1, "create " FSTEST_MNTNAME
"/le_snap");
245 fssfd
= rump_sys_open("/dev/rfss0", O_RDWR
);
247 err(1, "cannot open fss");
248 memset(&fss
, 0, sizeof(fss
));
249 fss
.fss_mount
= __UNCONST("/mnt");
250 fss
.fss_bstore
= __UNCONST(FSTEST_MNTNAME
"/le_snap");
252 if (rump_sys_ioctl(fssfd
, FSSIOCSET
, &fss
) == -1)
253 err(1, "create snapshot");
255 if (rump_sys_unlink(FSTEST_MNTNAME
"/le_snap") == -1)
256 err(1, "unlink snapshot");
259 /* now create some extra files */
261 for (i
= 0; i
< 4; i
++) {
262 sprintf(buf
, "file%d", i
);
263 fd
= rump_sys_open(buf
, O_EXCL
| O_CREAT
| O_RDWR
, 0644);
265 err(1, "create %s", buf
);
266 sprintf(buf
, "test file no %d", i
);
267 rump_sys_write(fd
, buf
, strlen(buf
));
271 if (rump_sys_ioctl(fssfd
, FSSIOCCLR
, NULL
) == -1)
272 err(1, "unconfigure snapshot");
277 quota_test5(const char *testopts
)
279 static char buf
[512];
286 for (i
=0; testopts
&& i
< strlen(testopts
); i
++) {
287 switch(testopts
[i
]) {
298 errx(1, "test4: unknown option %c", testopts
[i
]);
302 struct ufs_args uargs
;
303 uargs
.fspec
= __UNCONST("/diskdev");
304 /* remount the fs read/write */
305 if (rump_sys_mount(MOUNT_FFS
, FSTEST_MNTNAME
,
306 MNT_UPDATE
| (log
? MNT_LOG
: 0),
307 &uargs
, sizeof(uargs
)) == -1)
308 err(1, "mount ffs rw %s", FSTEST_MNTNAME
);
313 * open and unlink a file
316 fd
= rump_sys_open("unlinked_file",
317 O_EXCL
| O_CREAT
| O_RDWR
, 0644);
319 err(1, "create %s", "unlinked_file");
320 sprintf(buf
, "test unlinked_file");
321 rump_sys_write(fd
, buf
, strlen(buf
));
322 if (rump_sys_unlink("unlinked_file") == -1)
323 err(1, "unlink unlinked_file");
324 if (rump_sys_fsync(fd
) == -1)
325 err(1, "fsync unlinked_file");
326 rump_sys_reboot(RUMP_RB_NOSYNC
, NULL
);
327 errx(1, "reboot failed");
334 int (*func
)(const char *);
338 struct quota_test quota_tests
[] = {
339 { quota_test0
, "write up to hard limit"},
340 { quota_test1
, "write beyond the soft limit after grace time"},
341 { quota_test2
, "create file up to hard limit"},
342 { quota_test3
, "create file beyond the soft limit after grace time"},
343 { quota_test4
, "take a snapshot and add some data"},
344 { quota_test5
, "open and unlink a file"},
351 fprintf(stderr
, "usage: %s [-b] [-l] test# diskimage bindurl\n",
353 fprintf(stderr
, "available tests:\n");
354 for (test
= 0; test
< sizeof(quota_tests
) / sizeof(quota_tests
[0]);
356 fprintf(stderr
, "\t%d: %s\n", test
, quota_tests
[test
].desc
);
361 die(const char *reason
, int error
)
364 warnx("%s: %s", reason
, strerror(error
));
366 rump_daemonize_done(error
);
379 main(int argc
, char **argv
)
384 struct ufs_args uargs
;
385 const char *filename
;
386 const char *serverurl
;
387 const char *topts
= NULL
;
391 while ((ch
= getopt(argc
, argv
, "blo:r")) != -1) {
400 mntopts
|= MNT_RDONLY
;
418 test
= strtoul(argv
[0], &end
, 10);
422 if (test
> sizeof(quota_tests
) / sizeof(quota_tests
[0])) {
427 error
= rump_daemonize_begin();
429 errx(1, "rump daemonize: %s", strerror(error
));
434 die("rump init failed", error
);
436 if (rump_sys_mkdir(FSTEST_MNTNAME
, 0777) == -1)
437 err(1, "mount point create");
438 rump_pub_etfs_register("/diskdev", filename
, RUMP_ETFS_BLK
);
439 uargs
.fspec
= __UNCONST("/diskdev");
440 if (rump_sys_mount(MOUNT_FFS
, FSTEST_MNTNAME
, mntopts
,
441 &uargs
, sizeof(uargs
)) == -1)
442 die("mount ffs", errno
);
444 if (rump_sys_chdir(FSTEST_MNTNAME
) == -1)
445 err(1, "cd %s", FSTEST_MNTNAME
);
446 error
= quota_tests
[test
].func(topts
);
448 fprintf(stderr
, " test %lu: %s returned %d: %s\n",
449 test
, quota_tests
[test
].desc
, error
, strerror(error
));
451 if (rump_sys_chdir("/") == -1)
454 error
= rump_init_server(serverurl
);
456 die("rump server init failed", error
);
458 rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS
);
460 sem_init(&sigsem
, 0, 0);
461 signal(SIGTERM
, sigreboot
);
462 signal(SIGINT
, sigreboot
);
465 rump_sys_reboot(0, NULL
);