1*11be35a1SLionel Sambuc /* $NetBSD: t_vfsops.c,v 1.11 2011/04/04 19:16:58 hannken Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <sys/stat.h>
30*11be35a1SLionel Sambuc #include <sys/statvfs.h>
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc #include <atf-c.h>
33*11be35a1SLionel Sambuc #include <dirent.h>
34*11be35a1SLionel Sambuc #include <fcntl.h>
35*11be35a1SLionel Sambuc #include <stdlib.h>
36*11be35a1SLionel Sambuc #include <unistd.h>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
39*11be35a1SLionel Sambuc #include <rump/rump.h>
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc #include "../common/h_fsmacros.h"
42*11be35a1SLionel Sambuc #include "../../h_macros.h"
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc static void
tmount(const atf_tc_t * tc,const char * path)45*11be35a1SLionel Sambuc tmount(const atf_tc_t *tc, const char *path)
46*11be35a1SLionel Sambuc {
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc return;
49*11be35a1SLionel Sambuc }
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc static void
tstatvfs(const atf_tc_t * tc,const char * path)52*11be35a1SLionel Sambuc tstatvfs(const atf_tc_t *tc, const char *path)
53*11be35a1SLionel Sambuc {
54*11be35a1SLionel Sambuc const char *fstype = atf_tc_get_md_var(tc, "X-fs.mntname");
55*11be35a1SLionel Sambuc struct statvfs svb;
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc if (rump_sys_statvfs1(path, &svb, ST_WAIT) == -1)
58*11be35a1SLionel Sambuc atf_tc_fail_errno("statvfs");
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc ATF_REQUIRE(svb.f_namemax > 0 && svb.f_namemax <= MAXNAMLEN);
61*11be35a1SLionel Sambuc if (!(FSTYPE_PUFFS(tc) || FSTYPE_P2K_FFS(tc)))
62*11be35a1SLionel Sambuc ATF_REQUIRE_STREQ(svb.f_fstypename, fstype);
63*11be35a1SLionel Sambuc ATF_REQUIRE_STREQ(svb.f_mntonname, path);
64*11be35a1SLionel Sambuc }
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc static void
tsync(const atf_tc_t * tc,const char * path)67*11be35a1SLionel Sambuc tsync(const atf_tc_t *tc, const char *path)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc rump_sys_sync();
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc #define MAGICSTR "just a string, I like A"
74*11be35a1SLionel Sambuc static void
tfilehandle(const atf_tc_t * tc,const char * path)75*11be35a1SLionel Sambuc tfilehandle(const atf_tc_t *tc, const char *path)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc char fpath[MAXPATHLEN];
78*11be35a1SLionel Sambuc char buf[sizeof(MAGICSTR)];
79*11be35a1SLionel Sambuc size_t fhsize;
80*11be35a1SLionel Sambuc void *fhp;
81*11be35a1SLionel Sambuc int fd;
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc sprintf(fpath, "%s/file", path);
84*11be35a1SLionel Sambuc fd = rump_sys_open(fpath, O_RDWR | O_CREAT, 0777);
85*11be35a1SLionel Sambuc if (fd == -1)
86*11be35a1SLionel Sambuc atf_tc_fail_errno("open");
87*11be35a1SLionel Sambuc
88*11be35a1SLionel Sambuc if (rump_sys_write(fd, MAGICSTR, sizeof(MAGICSTR)) != sizeof(MAGICSTR))
89*11be35a1SLionel Sambuc atf_tc_fail("write to file");
90*11be35a1SLionel Sambuc rump_sys_close(fd);
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc /*
93*11be35a1SLionel Sambuc * Get file handle size.
94*11be35a1SLionel Sambuc * This also weeds out unsupported file systems.
95*11be35a1SLionel Sambuc */
96*11be35a1SLionel Sambuc fhsize = 0;
97*11be35a1SLionel Sambuc if (rump_sys_getfh(fpath, NULL, &fhsize) == -1) {
98*11be35a1SLionel Sambuc if (errno == EOPNOTSUPP) {
99*11be35a1SLionel Sambuc atf_tc_skip("file handles not supported");
100*11be35a1SLionel Sambuc } else if (errno != E2BIG) {
101*11be35a1SLionel Sambuc atf_tc_fail_errno("getfh size");
102*11be35a1SLionel Sambuc }
103*11be35a1SLionel Sambuc }
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc fhp = malloc(fhsize);
106*11be35a1SLionel Sambuc if (rump_sys_getfh(fpath, fhp, &fhsize) == -1)
107*11be35a1SLionel Sambuc atf_tc_fail_errno("getfh");
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc /* open file based on file handle */
110*11be35a1SLionel Sambuc fd = rump_sys_fhopen(fhp, fhsize, O_RDONLY);
111*11be35a1SLionel Sambuc if (fd == -1) {
112*11be35a1SLionel Sambuc atf_tc_fail_errno("fhopen");
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc /* check that we got the same file */
116*11be35a1SLionel Sambuc if (rump_sys_read(fd, buf, sizeof(buf)) != sizeof(MAGICSTR))
117*11be35a1SLionel Sambuc atf_tc_fail("read fhopened file");
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc ATF_REQUIRE_STREQ(buf, MAGICSTR);
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc rump_sys_close(fd);
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc #define FNAME "a_file"
125*11be35a1SLionel Sambuc static void
tfhremove(const atf_tc_t * tc,const char * path)126*11be35a1SLionel Sambuc tfhremove(const atf_tc_t *tc, const char *path)
127*11be35a1SLionel Sambuc {
128*11be35a1SLionel Sambuc size_t fhsize;
129*11be35a1SLionel Sambuc void *fhp;
130*11be35a1SLionel Sambuc int fd;
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc RL(rump_sys_chdir(path));
133*11be35a1SLionel Sambuc RL(fd = rump_sys_open(FNAME, O_RDWR | O_CREAT, 0777));
134*11be35a1SLionel Sambuc RL(rump_sys_close(fd));
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc fhsize = 0;
137*11be35a1SLionel Sambuc if (rump_sys_getfh(FNAME, NULL, &fhsize) == -1) {
138*11be35a1SLionel Sambuc if (errno == EOPNOTSUPP) {
139*11be35a1SLionel Sambuc atf_tc_skip("file handles not supported");
140*11be35a1SLionel Sambuc } else if (errno != E2BIG) {
141*11be35a1SLionel Sambuc atf_tc_fail_errno("getfh size");
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc }
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc fhp = malloc(fhsize);
146*11be35a1SLionel Sambuc RL(rump_sys_getfh(FNAME, fhp, &fhsize));
147*11be35a1SLionel Sambuc RL(rump_sys_unlink(FNAME));
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc if (FSTYPE_LFS(tc))
150*11be35a1SLionel Sambuc atf_tc_expect_fail("fhopen() for removed file succeeds "
151*11be35a1SLionel Sambuc "(PR kern/43745)");
152*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ESTALE, rump_sys_fhopen(fhp, fhsize, O_RDONLY) == -1);
153*11be35a1SLionel Sambuc atf_tc_expect_pass();
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc RL(rump_sys_chdir("/"));
156*11be35a1SLionel Sambuc }
157*11be35a1SLionel Sambuc #undef FNAME
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc /*
160*11be35a1SLionel Sambuc * This test only checks the file system doesn't crash. We *might*
161*11be35a1SLionel Sambuc * try a valid file handle.
162*11be35a1SLionel Sambuc */
163*11be35a1SLionel Sambuc static void
tfhinval(const atf_tc_t * tc,const char * path)164*11be35a1SLionel Sambuc tfhinval(const atf_tc_t *tc, const char *path)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc size_t fhsize;
167*11be35a1SLionel Sambuc void *fhp;
168*11be35a1SLionel Sambuc unsigned long seed;
169*11be35a1SLionel Sambuc int fd;
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc srandom(seed = time(NULL));
172*11be35a1SLionel Sambuc printf("RNG seed %lu\n", seed);
173*11be35a1SLionel Sambuc
174*11be35a1SLionel Sambuc RL(rump_sys_chdir(path));
175*11be35a1SLionel Sambuc fhsize = 0;
176*11be35a1SLionel Sambuc if (rump_sys_getfh(".", NULL, &fhsize) == -1) {
177*11be35a1SLionel Sambuc if (errno == EOPNOTSUPP) {
178*11be35a1SLionel Sambuc atf_tc_skip("file handles not supported");
179*11be35a1SLionel Sambuc } else if (errno != E2BIG) {
180*11be35a1SLionel Sambuc atf_tc_fail_errno("getfh size");
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc fhp = malloc(fhsize);
185*11be35a1SLionel Sambuc tests_makegarbage(fhp, fhsize);
186*11be35a1SLionel Sambuc fd = rump_sys_fhopen(fhp, fhsize, O_RDWR);
187*11be35a1SLionel Sambuc if (fd != -1)
188*11be35a1SLionel Sambuc rump_sys_close(fd);
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc RL(rump_sys_chdir("/"));
191*11be35a1SLionel Sambuc }
192*11be35a1SLionel Sambuc
193*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tmount, "mount/unmount");
194*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tstatvfs, "statvfs");
195*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tsync, "sync");
196*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tfilehandle, "file handles");
197*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tfhremove, "fhtovp for removed file");
198*11be35a1SLionel Sambuc ATF_TC_FSAPPLY(tfhinval, "fhopen invalid filehandle");
199*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)200*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
201*11be35a1SLionel Sambuc {
202*11be35a1SLionel Sambuc
203*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tmount);
204*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tstatvfs);
205*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tsync);
206*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tfilehandle);
207*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tfhremove);
208*11be35a1SLionel Sambuc ATF_TP_FSAPPLY(tfhinval);
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambuc return atf_no_error();
211*11be35a1SLionel Sambuc }
212