Home | History | Annotate | Line # | Download | only in vfs
t_io.c revision 1.9
      1 /*	$NetBSD: t_io.c,v 1.9 2012/03/20 05:21:45 jruoho Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/stat.h>
     30 #include <sys/statvfs.h>
     31 
     32 #include <atf-c.h>
     33 #include <fcntl.h>
     34 #include <libgen.h>
     35 #include <stdlib.h>
     36 #include <unistd.h>
     37 
     38 #include <rump/rump_syscalls.h>
     39 #include <rump/rump.h>
     40 
     41 #include "../common/h_fsmacros.h"
     42 #include "../../h_macros.h"
     43 
     44 #define TESTSTR "this is a string.  collect enough and you'll have Em"
     45 #define TESTSZ sizeof(TESTSTR)
     46 
     47 static void
     48 holywrite(const atf_tc_t *tc, const char *mp)
     49 {
     50 	char buf[1024];
     51 	char *b2, *b3;
     52 	size_t therange = getpagesize()+1;
     53 	int fd;
     54 
     55 	FSTEST_ENTER();
     56 
     57 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT|O_TRUNC, 0666));
     58 
     59 	memset(buf, 'A', sizeof(buf));
     60 	RL(rump_sys_pwrite(fd, buf, 1, getpagesize()));
     61 
     62 	memset(buf, 'B', sizeof(buf));
     63 	RL(rump_sys_pwrite(fd, buf, 2, getpagesize()-1));
     64 
     65 	REQUIRE_LIBC(b2 = malloc(2 * getpagesize()), NULL);
     66 	REQUIRE_LIBC(b3 = malloc(2 * getpagesize()), NULL);
     67 
     68 	RL(rump_sys_pread(fd, b2, therange, 0));
     69 
     70 	memset(b3, 0, therange);
     71 	memset(b3 + getpagesize() - 1, 'B', 2);
     72 
     73 	ATF_REQUIRE_EQ(memcmp(b2, b3, therange), 0);
     74 
     75 	rump_sys_close(fd);
     76 	FSTEST_EXIT();
     77 }
     78 
     79 static void
     80 extendbody(const atf_tc_t *tc, off_t seekcnt)
     81 {
     82 	char buf[TESTSZ+1];
     83 	struct stat sb;
     84 	int fd;
     85 
     86 	FSTEST_ENTER();
     87 	RL(fd = rump_sys_open("testfile",
     88 	    O_CREAT | O_RDWR | (seekcnt ? O_APPEND : 0)));
     89 	RL(rump_sys_ftruncate(fd, seekcnt));
     90 	RL(rump_sys_fstat(fd, &sb));
     91 	ATF_REQUIRE_EQ(sb.st_size, seekcnt);
     92 	atf_tc_expect_pass();
     93 
     94 	ATF_REQUIRE_EQ(rump_sys_write(fd, TESTSTR, TESTSZ), TESTSZ);
     95 	ATF_REQUIRE_EQ(rump_sys_pread(fd, buf, TESTSZ, seekcnt), TESTSZ);
     96 	ATF_REQUIRE_STREQ(buf, TESTSTR);
     97 
     98 	RL(rump_sys_fstat(fd, &sb));
     99 	ATF_REQUIRE_EQ(sb.st_size, (off_t)TESTSZ + seekcnt);
    100 	RL(rump_sys_close(fd));
    101 	FSTEST_EXIT();
    102 }
    103 
    104 static void
    105 extendfile(const atf_tc_t *tc, const char *mp)
    106 {
    107 
    108 	extendbody(tc, 0);
    109 }
    110 
    111 static void
    112 extendfile_append(const atf_tc_t *tc, const char *mp)
    113 {
    114 
    115 	extendbody(tc, 37);
    116 }
    117 
    118 static void
    119 overwritebody(const atf_tc_t *tc, off_t count, bool dotrunc)
    120 {
    121 	char *buf;
    122 	int fd;
    123 
    124 	REQUIRE_LIBC(buf = malloc(count), NULL);
    125 	FSTEST_ENTER();
    126 	RL(fd = rump_sys_open("testi", O_CREAT | O_RDWR));
    127 	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
    128 	RL(rump_sys_close(fd));
    129 
    130 	RL(fd = rump_sys_open("testi", O_CREAT | O_RDWR));
    131 	if (dotrunc)
    132 		RL(rump_sys_ftruncate(fd, 0));
    133 	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
    134 	RL(rump_sys_close(fd));
    135 	FSTEST_EXIT();
    136 }
    137 
    138 static void
    139 overwrite512(const atf_tc_t *tc, const char *mp)
    140 {
    141 
    142 	overwritebody(tc, 512, false);
    143 }
    144 
    145 static void
    146 overwrite64k(const atf_tc_t *tc, const char *mp)
    147 {
    148 
    149 	overwritebody(tc, 1<<16, false);
    150 }
    151 
    152 static void
    153 overwrite_trunc(const atf_tc_t *tc, const char *mp)
    154 {
    155 
    156 	overwritebody(tc, 1<<16, true);
    157 }
    158 
    159 static void
    160 shrinkfile(const atf_tc_t *tc, const char *mp)
    161 {
    162 	int fd;
    163 
    164 	FSTEST_ENTER();
    165 	RL(fd = rump_sys_open("file", O_RDWR|O_CREAT|O_TRUNC, 0666));
    166 	RL(rump_sys_ftruncate(fd, 2));
    167 	RL(rump_sys_ftruncate(fd, 1));
    168 	rump_sys_close(fd);
    169 	FSTEST_EXIT();
    170 }
    171 
    172 ATF_TC_FSAPPLY(holywrite, "create a sparse file and fill hole");
    173 ATF_TC_FSAPPLY(extendfile, "check that extending a file works");
    174 ATF_TC_FSAPPLY(extendfile_append, "check that extending a file works "
    175 				  "with a append-only fd (PR kern/44307)");
    176 ATF_TC_FSAPPLY(overwrite512, "write a 512 byte file twice");
    177 ATF_TC_FSAPPLY(overwrite64k, "write a 64k byte file twice");
    178 ATF_TC_FSAPPLY(overwrite_trunc, "write 64k + truncate + rewrite");
    179 ATF_TC_FSAPPLY(shrinkfile, "shrink file");
    180 
    181 ATF_TP_ADD_TCS(tp)
    182 {
    183 
    184 	ATF_TP_FSAPPLY(holywrite);
    185 	ATF_TP_FSAPPLY(extendfile);
    186 	ATF_TP_FSAPPLY(extendfile_append);
    187 	ATF_TP_FSAPPLY(overwrite512);
    188 	ATF_TP_FSAPPLY(overwrite64k);
    189 	ATF_TP_FSAPPLY(overwrite_trunc);
    190 	ATF_TP_FSAPPLY(shrinkfile);
    191 
    192 	return atf_no_error();
    193 }
    194