xref: /freebsd/contrib/netbsd-tests/fs/vfs/t_io.c (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*63d1fd59SEnji Cooper /*	$NetBSD: t_io.c,v 1.17 2017/01/13 21:30:40 christos Exp $	*/
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper  * modification, are permitted provided that the following conditions
957718be8SEnji Cooper  * are met:
1057718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper  *
1657718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper  */
2857718be8SEnji Cooper 
2957718be8SEnji Cooper #include <sys/stat.h>
3057718be8SEnji Cooper #include <sys/statvfs.h>
3157718be8SEnji Cooper 
3257718be8SEnji Cooper #include <atf-c.h>
3357718be8SEnji Cooper #include <fcntl.h>
3457718be8SEnji Cooper #include <libgen.h>
3557718be8SEnji Cooper #include <stdlib.h>
3657718be8SEnji Cooper #include <unistd.h>
3757718be8SEnji Cooper 
3857718be8SEnji Cooper #include <rump/rump_syscalls.h>
3957718be8SEnji Cooper #include <rump/rump.h>
4057718be8SEnji Cooper 
4157718be8SEnji Cooper #include "../common/h_fsmacros.h"
42*63d1fd59SEnji Cooper #include "h_macros.h"
4357718be8SEnji Cooper 
4457718be8SEnji Cooper #define TESTSTR "this is a string.  collect enough and you'll have Em"
4557718be8SEnji Cooper #define TESTSZ sizeof(TESTSTR)
4657718be8SEnji Cooper 
4757718be8SEnji Cooper static void
holywrite(const atf_tc_t * tc,const char * mp)4857718be8SEnji Cooper holywrite(const atf_tc_t *tc, const char *mp)
4957718be8SEnji Cooper {
5057718be8SEnji Cooper 	char buf[1024];
5157718be8SEnji Cooper 	char *b2, *b3;
5257718be8SEnji Cooper 	size_t therange = getpagesize()+1;
5357718be8SEnji Cooper 	int fd;
5457718be8SEnji Cooper 
5557718be8SEnji Cooper 	FSTEST_ENTER();
5657718be8SEnji Cooper 
5757718be8SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT|O_TRUNC, 0666));
5857718be8SEnji Cooper 
5957718be8SEnji Cooper 	memset(buf, 'A', sizeof(buf));
6057718be8SEnji Cooper 	RL(rump_sys_pwrite(fd, buf, 1, getpagesize()));
6157718be8SEnji Cooper 
6257718be8SEnji Cooper 	memset(buf, 'B', sizeof(buf));
6357718be8SEnji Cooper 	RL(rump_sys_pwrite(fd, buf, 2, getpagesize()-1));
6457718be8SEnji Cooper 
6557718be8SEnji Cooper 	REQUIRE_LIBC(b2 = malloc(2 * getpagesize()), NULL);
6657718be8SEnji Cooper 	REQUIRE_LIBC(b3 = malloc(2 * getpagesize()), NULL);
6757718be8SEnji Cooper 
6857718be8SEnji Cooper 	RL(rump_sys_pread(fd, b2, therange, 0));
6957718be8SEnji Cooper 
7057718be8SEnji Cooper 	memset(b3, 0, therange);
7157718be8SEnji Cooper 	memset(b3 + getpagesize() - 1, 'B', 2);
7257718be8SEnji Cooper 
7357718be8SEnji Cooper 	ATF_REQUIRE_EQ(memcmp(b2, b3, therange), 0);
7457718be8SEnji Cooper 
7557718be8SEnji Cooper 	rump_sys_close(fd);
7657718be8SEnji Cooper 	FSTEST_EXIT();
7757718be8SEnji Cooper }
7857718be8SEnji Cooper 
7957718be8SEnji Cooper static void
extendbody(const atf_tc_t * tc,off_t seekcnt)8057718be8SEnji Cooper extendbody(const atf_tc_t *tc, off_t seekcnt)
8157718be8SEnji Cooper {
8257718be8SEnji Cooper 	char buf[TESTSZ+1];
8357718be8SEnji Cooper 	struct stat sb;
8457718be8SEnji Cooper 	int fd;
8557718be8SEnji Cooper 
8657718be8SEnji Cooper 	FSTEST_ENTER();
8757718be8SEnji Cooper 	RL(fd = rump_sys_open("testfile",
8857718be8SEnji Cooper 	    O_CREAT | O_RDWR | (seekcnt ? O_APPEND : 0)));
8957718be8SEnji Cooper 	RL(rump_sys_ftruncate(fd, seekcnt));
9057718be8SEnji Cooper 	RL(rump_sys_fstat(fd, &sb));
9157718be8SEnji Cooper 	ATF_REQUIRE_EQ(sb.st_size, seekcnt);
9257718be8SEnji Cooper 
9357718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_write(fd, TESTSTR, TESTSZ), TESTSZ);
9457718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_pread(fd, buf, TESTSZ, seekcnt), TESTSZ);
9557718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, TESTSTR);
9657718be8SEnji Cooper 
9757718be8SEnji Cooper 	RL(rump_sys_fstat(fd, &sb));
9857718be8SEnji Cooper 	ATF_REQUIRE_EQ(sb.st_size, (off_t)TESTSZ + seekcnt);
9957718be8SEnji Cooper 	RL(rump_sys_close(fd));
10057718be8SEnji Cooper 	FSTEST_EXIT();
10157718be8SEnji Cooper }
10257718be8SEnji Cooper 
10357718be8SEnji Cooper static void
extendfile(const atf_tc_t * tc,const char * mp)10457718be8SEnji Cooper extendfile(const atf_tc_t *tc, const char *mp)
10557718be8SEnji Cooper {
10657718be8SEnji Cooper 
10757718be8SEnji Cooper 	extendbody(tc, 0);
10857718be8SEnji Cooper }
10957718be8SEnji Cooper 
11057718be8SEnji Cooper static void
extendfile_append(const atf_tc_t * tc,const char * mp)11157718be8SEnji Cooper extendfile_append(const atf_tc_t *tc, const char *mp)
11257718be8SEnji Cooper {
11357718be8SEnji Cooper 
11457718be8SEnji Cooper 	extendbody(tc, 37);
11557718be8SEnji Cooper }
11657718be8SEnji Cooper 
11757718be8SEnji Cooper static void
overwritebody(const atf_tc_t * tc,off_t count,bool dotrunc)11857718be8SEnji Cooper overwritebody(const atf_tc_t *tc, off_t count, bool dotrunc)
11957718be8SEnji Cooper {
12057718be8SEnji Cooper 	char *buf;
12157718be8SEnji Cooper 	int fd;
12257718be8SEnji Cooper 
12357718be8SEnji Cooper 	REQUIRE_LIBC(buf = malloc(count), NULL);
12457718be8SEnji Cooper 	FSTEST_ENTER();
12557718be8SEnji Cooper 	RL(fd = rump_sys_open("testi", O_CREAT | O_RDWR, 0666));
12657718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
12757718be8SEnji Cooper 	RL(rump_sys_close(fd));
12857718be8SEnji Cooper 
12957718be8SEnji Cooper 	RL(fd = rump_sys_open("testi", O_RDWR));
13057718be8SEnji Cooper 	if (dotrunc)
13157718be8SEnji Cooper 		RL(rump_sys_ftruncate(fd, 0));
13257718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
13357718be8SEnji Cooper 	RL(rump_sys_close(fd));
13457718be8SEnji Cooper 	FSTEST_EXIT();
13557718be8SEnji Cooper }
13657718be8SEnji Cooper 
13757718be8SEnji Cooper static void
overwrite512(const atf_tc_t * tc,const char * mp)13857718be8SEnji Cooper overwrite512(const atf_tc_t *tc, const char *mp)
13957718be8SEnji Cooper {
14057718be8SEnji Cooper 
14157718be8SEnji Cooper 	overwritebody(tc, 512, false);
14257718be8SEnji Cooper }
14357718be8SEnji Cooper 
14457718be8SEnji Cooper static void
overwrite64k(const atf_tc_t * tc,const char * mp)14557718be8SEnji Cooper overwrite64k(const atf_tc_t *tc, const char *mp)
14657718be8SEnji Cooper {
14757718be8SEnji Cooper 
14857718be8SEnji Cooper 	overwritebody(tc, 1<<16, false);
14957718be8SEnji Cooper }
15057718be8SEnji Cooper 
15157718be8SEnji Cooper static void
overwrite_trunc(const atf_tc_t * tc,const char * mp)15257718be8SEnji Cooper overwrite_trunc(const atf_tc_t *tc, const char *mp)
15357718be8SEnji Cooper {
15457718be8SEnji Cooper 
15557718be8SEnji Cooper 	overwritebody(tc, 1<<16, true);
15657718be8SEnji Cooper }
15757718be8SEnji Cooper 
15857718be8SEnji Cooper static void
shrinkfile(const atf_tc_t * tc,const char * mp)15957718be8SEnji Cooper shrinkfile(const atf_tc_t *tc, const char *mp)
16057718be8SEnji Cooper {
16157718be8SEnji Cooper 	int fd;
16257718be8SEnji Cooper 
16357718be8SEnji Cooper 	FSTEST_ENTER();
16457718be8SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT|O_TRUNC, 0666));
16557718be8SEnji Cooper 	RL(rump_sys_ftruncate(fd, 2));
16657718be8SEnji Cooper 	RL(rump_sys_ftruncate(fd, 1));
16757718be8SEnji Cooper 	rump_sys_close(fd);
16857718be8SEnji Cooper 	FSTEST_EXIT();
16957718be8SEnji Cooper }
17057718be8SEnji Cooper 
17157718be8SEnji Cooper #define TBSIZE 9000
17257718be8SEnji Cooper static void
read_after_unlink(const atf_tc_t * tc,const char * mp)17357718be8SEnji Cooper read_after_unlink(const atf_tc_t *tc, const char *mp)
17457718be8SEnji Cooper {
17557718be8SEnji Cooper 	char buf[TBSIZE], buf2[TBSIZE];
17657718be8SEnji Cooper 	int fd;
17757718be8SEnji Cooper 
17857718be8SEnji Cooper 	FSTEST_ENTER();
17957718be8SEnji Cooper 
18057718be8SEnji Cooper 	/* create file and put some content into it */
18157718be8SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT, 0666));
18257718be8SEnji Cooper 	memset(buf, 'D', TBSIZE);
18357718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, TBSIZE), TBSIZE);
18457718be8SEnji Cooper 	rump_sys_close(fd);
18557718be8SEnji Cooper 
18657718be8SEnji Cooper 	/* flush buffers from UBC to file system */
18757718be8SEnji Cooper 	ATF_REQUIRE_ERRNO(EBUSY, rump_sys_unmount(mp, 0) == -1);
18857718be8SEnji Cooper 
18957718be8SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDWR));
19057718be8SEnji Cooper 	RL(rump_sys_unlink("file"));
19157718be8SEnji Cooper 
19257718be8SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_read(fd, buf2, TBSIZE), TBSIZE);
19357718be8SEnji Cooper 	ATF_REQUIRE_EQ(memcmp(buf, buf2, TBSIZE), 0);
19457718be8SEnji Cooper 	rump_sys_close(fd);
19557718be8SEnji Cooper 
19657718be8SEnji Cooper 	FSTEST_EXIT();
19757718be8SEnji Cooper }
19857718be8SEnji Cooper 
19957718be8SEnji Cooper static void
wrrd_after_unlink(const atf_tc_t * tc,const char * mp)20057718be8SEnji Cooper wrrd_after_unlink(const atf_tc_t *tc, const char *mp)
20157718be8SEnji Cooper {
20257718be8SEnji Cooper 	int value = 0x11;
20357718be8SEnji Cooper 	int v2;
20457718be8SEnji Cooper 	int fd;
20557718be8SEnji Cooper 
20657718be8SEnji Cooper 	FSTEST_ENTER();
20757718be8SEnji Cooper 
20857718be8SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT, 0666));
20957718be8SEnji Cooper 	RL(rump_sys_unlink("file"));
21057718be8SEnji Cooper 
21157718be8SEnji Cooper 	RL(rump_sys_pwrite(fd, &value, sizeof(value), 654321));
21257718be8SEnji Cooper 
21357718be8SEnji Cooper 	/*
21457718be8SEnji Cooper 	 * We can't easily invalidate the buffer since we hold a
21557718be8SEnji Cooper 	 * reference, but try to get them to flush anyway.
21657718be8SEnji Cooper 	 */
21757718be8SEnji Cooper 	RL(rump_sys_fsync(fd));
21857718be8SEnji Cooper 	RL(rump_sys_pread(fd, &v2, sizeof(v2), 654321));
21957718be8SEnji Cooper 	rump_sys_close(fd);
22057718be8SEnji Cooper 
22157718be8SEnji Cooper 	ATF_REQUIRE_EQ(value, v2);
22257718be8SEnji Cooper 	FSTEST_EXIT();
22357718be8SEnji Cooper }
22457718be8SEnji Cooper 
225640235e2SEnji Cooper static void
read_fault(const atf_tc_t * tc,const char * mp)226640235e2SEnji Cooper read_fault(const atf_tc_t *tc, const char *mp)
227640235e2SEnji Cooper {
228640235e2SEnji Cooper 	char ch = 123;
229640235e2SEnji Cooper 	int fd;
230640235e2SEnji Cooper 
231640235e2SEnji Cooper 	FSTEST_ENTER();
232640235e2SEnji Cooper 	RL(fd = rump_sys_open("file", O_CREAT | O_RDWR, 0777));
233640235e2SEnji Cooper 	ATF_REQUIRE_EQ(rump_sys_write(fd, &ch, 1), 1);
234640235e2SEnji Cooper 	RL(rump_sys_close(fd));
235640235e2SEnji Cooper 	RL(fd = rump_sys_open("file", O_RDONLY | O_SYNC | O_RSYNC));
236640235e2SEnji Cooper 	ATF_REQUIRE_ERRNO(EFAULT, rump_sys_read(fd, NULL, 1) == -1);
237640235e2SEnji Cooper 	RL(rump_sys_close(fd));
238640235e2SEnji Cooper 	FSTEST_EXIT();
239640235e2SEnji Cooper }
240640235e2SEnji Cooper 
24157718be8SEnji Cooper ATF_TC_FSAPPLY(holywrite, "create a sparse file and fill hole");
24257718be8SEnji Cooper ATF_TC_FSAPPLY(extendfile, "check that extending a file works");
24357718be8SEnji Cooper ATF_TC_FSAPPLY(extendfile_append, "check that extending a file works "
24457718be8SEnji Cooper 				  "with a append-only fd (PR kern/44307)");
24557718be8SEnji Cooper ATF_TC_FSAPPLY(overwrite512, "write a 512 byte file twice");
24657718be8SEnji Cooper ATF_TC_FSAPPLY(overwrite64k, "write a 64k byte file twice");
24757718be8SEnji Cooper ATF_TC_FSAPPLY(overwrite_trunc, "write 64k + truncate + rewrite");
24857718be8SEnji Cooper ATF_TC_FSAPPLY(shrinkfile, "shrink file");
24957718be8SEnji Cooper ATF_TC_FSAPPLY(read_after_unlink, "contents can be read off disk after unlink");
25057718be8SEnji Cooper ATF_TC_FSAPPLY(wrrd_after_unlink, "file can be written and read after unlink");
251640235e2SEnji Cooper ATF_TC_FSAPPLY(read_fault, "read at bad address must return EFAULT");
25257718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)25357718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
25457718be8SEnji Cooper {
25557718be8SEnji Cooper 
25657718be8SEnji Cooper 	ATF_TP_FSAPPLY(holywrite);
25757718be8SEnji Cooper 	ATF_TP_FSAPPLY(extendfile);
25857718be8SEnji Cooper 	ATF_TP_FSAPPLY(extendfile_append);
25957718be8SEnji Cooper 	ATF_TP_FSAPPLY(overwrite512);
26057718be8SEnji Cooper 	ATF_TP_FSAPPLY(overwrite64k);
26157718be8SEnji Cooper 	ATF_TP_FSAPPLY(overwrite_trunc);
26257718be8SEnji Cooper 	ATF_TP_FSAPPLY(shrinkfile);
26357718be8SEnji Cooper 	ATF_TP_FSAPPLY(read_after_unlink);
26457718be8SEnji Cooper 	ATF_TP_FSAPPLY(wrrd_after_unlink);
265640235e2SEnji Cooper 	ATF_TP_FSAPPLY(read_fault);
26657718be8SEnji Cooper 
26757718be8SEnji Cooper 	return atf_no_error();
26857718be8SEnji Cooper }
269