xref: /netbsd-src/tests/fs/vfs/t_vfsops.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: t_vfsops.c,v 1.7 2010/08/16 10:47:16 pooka 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 <dirent.h>
34 #include <fcntl.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 static void
45 tmount(const atf_tc_t *tc, const char *path)
46 {
47 
48 	return;
49 }
50 
51 static void
52 tstatvfs(const atf_tc_t *tc, const char *path)
53 {
54 	const char *fstype = atf_tc_get_md_var(tc, "X-fs.type");
55 	struct statvfs svb;
56 
57 	if (rump_sys_statvfs1(path, &svb, ST_WAIT) == -1)
58 		atf_tc_fail_errno("statvfs");
59 
60 	ATF_REQUIRE(svb.f_namemax > 0 && svb.f_namemax <= MAXNAMLEN);
61 	if (!FSTYPE_PUFFS(tc))
62 		ATF_REQUIRE_STREQ(svb.f_fstypename, fstype);
63 	ATF_REQUIRE_STREQ(svb.f_mntonname, path);
64 }
65 
66 static void
67 tsync(const atf_tc_t *tc, const char *path)
68 {
69 
70 	rump_sys_sync();
71 }
72 
73 #define MAGICSTR "just a string, I like A"
74 static void
75 tfilehandle(const atf_tc_t *tc, const char *path)
76 {
77 	char fpath[MAXPATHLEN];
78 	char buf[sizeof(MAGICSTR)];
79 	size_t fhsize;
80 	void *fhp;
81 	int fd;
82 
83 	sprintf(fpath, "%s/file", path);
84 	fd = rump_sys_open(fpath, O_RDWR | O_CREAT, 0777);
85 	if (fd == -1)
86 		atf_tc_fail_errno("open");
87 
88 	if (rump_sys_write(fd, MAGICSTR, sizeof(MAGICSTR)) != sizeof(MAGICSTR))
89 		atf_tc_fail("write to file");
90 	rump_sys_close(fd);
91 
92 	/*
93 	 * Get file handle size.
94 	 * This also weeds out unsupported file systems.
95 	 */
96 	fhsize = 0;
97 	if (rump_sys_getfh(fpath, NULL, &fhsize) == -1) {
98 		if (errno == EOPNOTSUPP) {
99 			atf_tc_skip("file handles not supported");
100 		} else if (errno != E2BIG) {
101 			atf_tc_fail_errno("getfh size");
102 		}
103 	}
104 
105 	fhp = malloc(fhsize);
106 	if (rump_sys_getfh(fpath, fhp, &fhsize) == -1)
107 		atf_tc_fail_errno("getfh");
108 
109 	/* open file based on file handle */
110 	fd = rump_sys_fhopen(fhp, fhsize, O_RDONLY);
111 	if (FSTYPE_TMPFS(tc)) {
112 		atf_tc_expect_fail("PR kern/43605");
113 		if (fd != -1 || errno != EINVAL)
114 			atf_tc_expect_pass();
115 	}
116 	if (fd == -1) {
117 		atf_tc_fail_errno("fhopen");
118 	}
119 
120 	/* check that we got the same file */
121 	if (rump_sys_read(fd, buf, sizeof(buf)) != sizeof(MAGICSTR))
122 		atf_tc_fail("read fhopened file");
123 
124 	ATF_REQUIRE_STREQ(buf, MAGICSTR);
125 
126 	rump_sys_close(fd);
127 }
128 
129 #define FNAME "a_file"
130 static void
131 tfhremove(const atf_tc_t *tc, const char *path)
132 {
133 	size_t fhsize;
134 	void *fhp;
135 	int fd;
136 
137 	/* should repeat above, but ... */
138 	if (FSTYPE_TMPFS(tc))
139 		atf_tc_skip("file handles broken (PR kern/43605)");
140 
141 	RL(rump_sys_chdir(path));
142 	RL(fd = rump_sys_open(FNAME, O_RDWR | O_CREAT, 0777));
143 	RL(rump_sys_close(fd));
144 
145 	fhsize = 0;
146 	if (rump_sys_getfh(FNAME, NULL, &fhsize) == -1) {
147 		if (errno == EOPNOTSUPP) {
148 			atf_tc_skip("file handles not supported");
149 		} else if (errno != E2BIG) {
150 			atf_tc_fail_errno("getfh size");
151 		}
152 	}
153 
154 	fhp = malloc(fhsize);
155 	RL(rump_sys_getfh(FNAME, fhp, &fhsize));
156 	RL(rump_sys_unlink(FNAME));
157 
158 	if (FSTYPE_MSDOS(tc) || FSTYPE_LFS(tc))
159 		atf_tc_expect_fail("fhopen() for removed file succeeds "
160 		    "(PR kern/43745)");
161 	ATF_REQUIRE_ERRNO(ESTALE, rump_sys_fhopen(fhp, fhsize, O_RDONLY) == -1);
162 	atf_tc_expect_pass();
163 
164 	RL(rump_sys_chdir("/"));
165 }
166 #undef FNAME
167 
168 /*
169  * This test only checks the file system doesn't crash.  We *might*
170  * try a valid file handle.
171  */
172 static void
173 tfhinval(const atf_tc_t *tc, const char *path)
174 {
175 	size_t fhsize;
176 	void *fhp;
177 	unsigned long seed;
178 	int fd;
179 
180 	if (FSTYPE_TMPFS(tc))
181 		atf_tc_skip("file handles broken (PR kern/43605)");
182 
183 	srandom(seed = time(NULL));
184 	printf("RNG seed %lu\n", seed);
185 
186 	RL(rump_sys_chdir(path));
187 	fhsize = 0;
188 	if (rump_sys_getfh(".", NULL, &fhsize) == -1) {
189 		if (errno == EOPNOTSUPP) {
190 			atf_tc_skip("file handles not supported");
191 		} else if (errno != E2BIG) {
192 			atf_tc_fail_errno("getfh size");
193 		}
194 	}
195 
196 	fhp = malloc(fhsize);
197 	tests_makegarbage(fhp, fhsize);
198 	fd = rump_sys_fhopen(fhp, fhsize, O_RDWR);
199 	if (fd != -1)
200 		rump_sys_close(fd);
201 
202 	RL(rump_sys_chdir("/"));
203 }
204 
205 ATF_TC_FSAPPLY(tmount, "mount/unmount");
206 ATF_TC_FSAPPLY(tstatvfs, "statvfs");
207 ATF_TC_FSAPPLY(tsync, "sync");
208 ATF_TC_FSAPPLY(tfilehandle, "file handles");
209 ATF_TC_FSAPPLY(tfhremove, "fhtovp for removed file");
210 ATF_TC_FSAPPLY(tfhinval, "fhopen invalid filehandle");
211 
212 ATF_TP_ADD_TCS(tp)
213 {
214 
215 	ATF_TP_FSAPPLY(tmount);
216 	ATF_TP_FSAPPLY(tstatvfs);
217 	ATF_TP_FSAPPLY(tsync);
218 	ATF_TP_FSAPPLY(tfilehandle);
219 	ATF_TP_FSAPPLY(tfhremove);
220 	ATF_TP_FSAPPLY(tfhinval);
221 
222 	return atf_no_error();
223 }
224