summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/fs/common/fstest_nfs.c
blob: 5ef256f1be4144d97116583938deb634261a83db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*	$NetBSD: fstest_nfs.c,v 1.9 2011/02/28 21:08:46 pooka Exp $	*/

/*
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/statvfs.h>
#include <sys/wait.h>

#include <assert.h>
#include <atf-c.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <pthread.h>
#include <puffs.h>
#include <puffsdump.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <rump/rump.h>
#include <rump/rump_syscalls.h>

#include "h_fsmacros.h"
#include "mount_nfs.h"
#include "../../net/config/netconfig.c"

#define SERVERADDR "10.3.2.1"
#define SERVERROADDR "10.4.2.1"
#define CLIENTADDR "10.3.2.2"
#define CLIENTROADDR "10.4.2.2"
#define NETNETMASK "255.255.255.0"
#define EXPORTPATH "/myexport"

static void
childfail(int status)
{

	atf_tc_fail("child died");
}

/* fork rump nfsd, configure interface */
static int
donewfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv)
{
	const char *srcdir;
	char *nfsdargv[16];
	char nfsdpath[MAXPATHLEN];
	char imagepath[MAXPATHLEN];
	char ethername[MAXPATHLEN], ethername_ro[MAXPATHLEN];
	char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
	char cwd[MAXPATHLEN];
	struct nfstestargs *args;
	pid_t childpid;
	int pipes[2];
	int devnull;

	/*
	 * First, we start the nfs service.
	 */
	srcdir = atf_tc_get_config_var(tc, "srcdir");
	sprintf(nfsdpath, "%s/../nfs/nfsservice/rumpnfsd", srcdir);
	sprintf(ethername, "/%s/%s.etherbus", getcwd(cwd, sizeof(cwd)), image);
	sprintf(ethername_ro, "%s_ro", ethername);
	sprintf(imagepath, "/%s/%s", cwd, image);

	nfsdargv[0] = nfsdpath;
	nfsdargv[1] = ethername;
	nfsdargv[2] = ethername_ro;
	nfsdargv[3] = __UNCONST(SERVERADDR);
	nfsdargv[4] = __UNCONST(SERVERROADDR);
	nfsdargv[5] = __UNCONST(NETNETMASK);
	nfsdargv[6] = __UNCONST(EXPORTPATH);
	nfsdargv[7] = imagepath;
	nfsdargv[8] = NULL;

	signal(SIGCHLD, childfail);
	if (pipe(pipes) == -1)
		return errno;

	switch ((childpid = fork())) {
	case 0:
		if (chdir(dirname(nfsdpath)) == -1)
			err(1, "chdir");
		close(pipes[0]);
		if (dup2(pipes[1], 3) == -1)
			err(1, "dup2");
		if (execvp(nfsdargv[0], nfsdargv) == -1)
			err(1, "execvp");
	case -1:
		return errno;
	default:
		close(pipes[1]);
		break;	
	}

	/*
	 * Ok, nfsd has been run.  The following sleep helps with the
	 * theoretical problem that nfsd can't start fast enough to
	 * process our mount request and we end up doing a timeout
	 * before the mount.  This would take several seconds.  So
	 * try to make sure nfsd is up&running already at this stage.
	 */
	if (read(pipes[0], &devnull, 4) == -1)
		return errno;

	/*
	 * Configure our networking interface.
	 */
	rump_init();
	netcfg_rump_makeshmif(ethername, ifname);
	netcfg_rump_if(ifname, CLIENTADDR, NETNETMASK);
	netcfg_rump_makeshmif(ethername_ro, ifname_ro);
	netcfg_rump_if(ifname_ro, CLIENTROADDR, NETNETMASK);

	/*
	 * That's it.  The rest is done in mount, since we don't have
	 * the mountpath available here.
	 */
	args = malloc(sizeof(*args));
	if (args == NULL)
		return errno;
	memset(args, 0, sizeof(*args));
	args->ta_childpid = childpid;
	strcpy(args->ta_ethername, ethername);

	*argp = args;

	return 0;
}

int
nfs_fstest_newfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv)
{

	return donewfs(tc, argp, image, size, fspriv);
}

int
nfsro_fstest_newfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv)
{

	return donewfs(tc, argp, image, size, fspriv);
}

/* mount the file system */
static int
domount(const atf_tc_t *tc, void *arg, const char *serverpath,
	const char *path, int flags)
{
	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
	const char *nfscliargs[] = {
		"nfsclient",
		serverpath,
		path,
		NULL,
	};
	struct nfs_args args;
	int mntflags;

	if (rump_sys_mkdir(path, 0777) == -1)
		return errno;

	/* XXX: atf does not reset values */
	optind = 1;
	opterr = 1;

	/*
	 * We use nfs parseargs here, since as a side effect it
	 * takes care of the RPC hulabaloo.
	 */
	mount_nfs_parseargs(__arraycount(nfscliargs)-1, __UNCONST(nfscliargs),
	    &args, &mntflags, canon_dev, canon_dir);

	if (rump_sys_mount(MOUNT_NFS, path, flags, &args, sizeof(args)) == -1) {
		return errno;
	}

	return 0;
}

int
nfs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
{

	return domount(tc, arg, SERVERADDR ":" EXPORTPATH, path, flags);
}

/*
 * This is where the magic happens!
 *
 * If we are mounting r/w, do the normal thing.  However, if we are
 * doing a r/o mount, switch use the r/o server export address
 * and do a r/w mount.  This way we end up testing the r/o export policy
 * of the server! (yes, slightly questionable semantics, but at least
 * we notice very quickly if our assumption is broken in the future ;)
 */
int
nfsro_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
{

	if (flags & MNT_RDONLY) {
		flags &= ~MNT_RDONLY;
		return domount(tc, arg, SERVERROADDR":"EXPORTPATH, path, flags);
	} else {
		return domount(tc, arg, SERVERADDR":"EXPORTPATH, path, flags);
	}
}

static int
dodelfs(const atf_tc_t *tc, void *arg)
{

	/*
	 * XXX: no access to "args" since we're called from "cleanup".
	 * Trust atf to kill nfsd process and remove etherfile.
	 */
#if 0
	/*
	 * It's highly expected that the child will die next, so we
	 * don't need that information anymore thank you very many.
	 */
	signal(SIGCHLD, SIG_IGN);

	/*
	 * Just KILL it.  Sending it SIGTERM first causes it to try
	 * to send some unmount RPCs, leading to sticky situations.
	 */
	kill(args->ta_childpid, SIGKILL);
	wait(&status);

	/* remove ethernet bus */
	if (unlink(args->ta_ethername) == -1)
		atf_tc_fail_errno("unlink ethername");
#endif

	return 0;
}

int
nfs_fstest_delfs(const atf_tc_t *tc, void *arg)
{

	return dodelfs(tc, arg);
}

int
nfsro_fstest_delfs(const atf_tc_t *tc, void *arg)
{

	return dodelfs(tc, arg);
}

static int
dounmount(const atf_tc_t *tc, const char *path, int flags)
{
	int status, i, sverrno;

	/*
	 * NFS handles sillyrenames in an workqueue.  Some of them might
	 * be still in the queue even if all user activity has ceased.
	 * We try to unmount for 2 seconds to give them a chance
	 * to flush out.
	 *
	 * PR kern/43799
	 */
	for (i = 0; i < 20; i++) {
		if ((status = rump_sys_unmount(path, flags)) == 0)
			break;
		sverrno = errno;
		if (sverrno != EBUSY)
			break;
		usleep(100000);
	}
	if (status == -1)
		return sverrno;

	if (rump_sys_rmdir(path) == -1)
		return errno;

	return 0;
}

int
nfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
{

	return dounmount(tc, path, flags);
}

int
nfsro_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
{

	return dounmount(tc, path, flags);
}
OpenPOWER on IntegriCloud