xref: /openbsd/sys/nfs/nfs_vfsops.c (revision 898184e3)
1 /*	$OpenBSD: nfs_vfsops.c,v 1.95 2013/01/16 04:05:22 deraadt Exp $	*/
2 /*	$NetBSD: nfs_vfsops.c,v 1.46.4.1 1996/05/25 22:40:35 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/ioctl.h>
41 #include <sys/signal.h>
42 #include <sys/proc.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/swap.h>
48 #include <sys/buf.h>
49 #include <sys/mbuf.h>
50 #include <sys/dirent.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
55 #include <sys/queue.h>
56 
57 #include <net/if.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 
61 #include <nfs/rpcv2.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfsnode.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsmount.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsdiskless.h>
69 #include <nfs/nfs_var.h>
70 
71 extern struct nfsstats nfsstats;
72 extern int nfs_ticks;
73 extern u_int32_t nfs_procids[NFS_NPROCS];
74 
75 int		nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
76 int		nfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
77 struct mount	*nfs_mount_diskless(struct nfs_dlmount *, char *, int);
78 
79 /*
80  * nfs vfs operations.
81  */
82 const struct vfsops nfs_vfsops = {
83 	nfs_mount,
84 	nfs_start,
85 	nfs_unmount,
86 	nfs_root,
87 	nfs_quotactl,
88 	nfs_statfs,
89 	nfs_sync,
90 	nfs_vget,
91 	nfs_fhtovp,
92 	nfs_vptofh,
93 	nfs_vfs_init,
94 	nfs_sysctl,
95 	nfs_checkexp
96 };
97 
98 /*
99  * nfs statfs call
100  */
101 int
102 nfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
103 {
104 	struct vnode *vp;
105 	struct nfs_statfs *sfp = NULL;
106 	struct nfsm_info	info;
107 	u_int32_t *tl;
108 	int32_t t1;
109 	caddr_t cp2;
110 	struct nfsmount *nmp = VFSTONFS(mp);
111 	int error = 0, retattr;
112 	struct ucred *cred;
113 	struct nfsnode *np;
114 	u_quad_t tquad;
115 
116 	info.nmi_v3 = (nmp->nm_flag & NFSMNT_NFSV3);
117 
118 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
119 	if (error)
120 		return (error);
121 	vp = NFSTOV(np);
122 	cred = crget();
123 	cred->cr_ngroups = 0;
124 	if (info.nmi_v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
125 		(void)nfs_fsinfo(nmp, vp, cred, p);
126 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
127 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(info.nmi_v3));
128 	nfsm_fhtom(&info, vp, info.nmi_v3);
129 
130 	info.nmi_procp = p;
131 	info.nmi_cred = cred;
132 	error = nfs_request(vp, NFSPROC_FSSTAT, &info);
133 	if (info.nmi_v3)
134 		nfsm_postop_attr(vp, retattr);
135 	if (error) {
136 		m_freem(info.nmi_mrep);
137 		goto nfsmout;
138 	}
139 
140 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(info.nmi_v3));
141 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
142 	if (info.nmi_v3) {
143 		sbp->f_bsize = NFS_FABLKSIZE;
144 		tquad = fxdr_hyper(&sfp->sf_tbytes);
145 		sbp->f_blocks = tquad / (u_quad_t)NFS_FABLKSIZE;
146 		tquad = fxdr_hyper(&sfp->sf_fbytes);
147 		sbp->f_bfree = tquad / (u_quad_t)NFS_FABLKSIZE;
148 		tquad = fxdr_hyper(&sfp->sf_abytes);
149 		sbp->f_bavail = (quad_t)tquad / (quad_t)NFS_FABLKSIZE;
150 
151 		tquad = fxdr_hyper(&sfp->sf_tfiles);
152 		sbp->f_files = tquad;
153 		tquad = fxdr_hyper(&sfp->sf_ffiles);
154 		sbp->f_ffree = tquad;
155 		sbp->f_favail = tquad;
156 		sbp->f_namemax = MAXNAMLEN;
157 	} else {
158 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
159 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
160 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
161 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
162 		sbp->f_files = 0;
163 		sbp->f_ffree = 0;
164 	}
165 	copy_statfs_info(sbp, mp);
166 	m_freem(info.nmi_mrep);
167 nfsmout:
168 	vrele(vp);
169 	crfree(cred);
170 	return (error);
171 }
172 
173 /*
174  * nfs version 3 fsinfo rpc call
175  */
176 int
177 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
178     struct proc *p)
179 {
180 	struct nfsv3_fsinfo *fsp;
181 	struct nfsm_info	info;
182 	int32_t t1;
183 	u_int32_t *tl, pref, max;
184 	caddr_t cp2;
185 	int error = 0, retattr;
186 
187 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
188 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(1));
189 	nfsm_fhtom(&info, vp, 1);
190 
191 	info.nmi_procp = p;
192 	info.nmi_cred = cred;
193 	error = nfs_request(vp, NFSPROC_FSINFO, &info);
194 
195 	nfsm_postop_attr(vp, retattr);
196 	if (error) {
197 		m_freem(info.nmi_mrep);
198 		goto nfsmout;
199 	}
200 
201 	nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
202 	pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
203 	if (pref < nmp->nm_wsize)
204 		nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
205 			~(NFS_FABLKSIZE - 1);
206 	max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
207 	if (max < nmp->nm_wsize) {
208 		nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
209 		if (nmp->nm_wsize == 0)
210 			nmp->nm_wsize = max;
211 	}
212 	pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
213 	if (pref < nmp->nm_rsize)
214 		nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
215 			~(NFS_FABLKSIZE - 1);
216 	max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
217 	if (max < nmp->nm_rsize) {
218 		nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
219 		if (nmp->nm_rsize == 0)
220 			nmp->nm_rsize = max;
221 	}
222 	pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
223 	if (pref < nmp->nm_readdirsize)
224 		nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
225 			~(NFS_DIRBLKSIZ - 1);
226 	if (max < nmp->nm_readdirsize) {
227 		nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
228 		if (nmp->nm_readdirsize == 0)
229 			nmp->nm_readdirsize = max;
230 	}
231 	nmp->nm_flag |= NFSMNT_GOTFSINFO;
232 
233 	m_freem(info.nmi_mrep);
234 nfsmout:
235 	return (error);
236 }
237 
238 /*
239  * Mount a remote root fs via. NFS.  It goes like this:
240  * - Call nfs_boot_init() to fill in the nfs_diskless struct
241  *   (using RARP, bootparam RPC, mountd RPC)
242  * - hand craft the swap nfs vnode hanging off a fake mount point
243  *	if swdevt[0].sw_dev == NODEV
244  * - build the rootfs mount point and call mountnfs() to do the rest.
245  */
246 int
247 nfs_mountroot(void)
248 {
249 	struct nfs_diskless nd;
250 	struct vattr attr;
251 	struct mount *mp;
252 	struct vnode *vp;
253 	struct proc *procp;
254 	long n;
255 	int error;
256 
257 	procp = curproc; /* XXX */
258 
259 	/*
260 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
261 	 * Side effect:	 Finds and configures a network interface.
262 	 */
263 	bzero((caddr_t) &nd, sizeof(nd));
264 	nfs_boot_init(&nd, procp);
265 
266 	/*
267 	 * Create the root mount point.
268 	 */
269 	if (nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root, -1))
270 		panic("nfs_mountroot: root");
271 	mp = nfs_mount_diskless(&nd.nd_root, "/", 0);
272 	nfs_root(mp, &rootvp);
273 	printf("root on %s\n", nd.nd_root.ndm_host);
274 
275 	/*
276 	 * Link it into the mount list.
277 	 */
278 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
279 	vfs_unbusy(mp);
280 
281 	/* Get root attributes (for the time). */
282 	error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp);
283 	if (error) panic("nfs_mountroot: getattr for root");
284 	n = attr.va_atime.tv_sec;
285 #ifdef	DEBUG
286 	printf("root time: 0x%lx\n", n);
287 #endif
288 	inittodr(n);
289 
290 #ifdef notyet
291 	/* Set up swap credentials. */
292 	proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
293 	proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
294 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
295 		NGROUPS)
296 		proc0.p_ucred->cr_ngroups = NGROUPS;
297 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
298 	    proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
299 #endif
300 
301 	/*
302 	 * "Mount" the swap device.
303 	 *
304 	 * On a "dataless" configuration (swap on disk) we will have:
305 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
306 	 */
307 	if (bdevvp(swapdev, &swapdev_vp))
308 		panic("nfs_mountroot: can't setup swap vp");
309 	if (swdevt[0].sw_dev != NODEV) {
310 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
311 		return (0);
312 	}
313 
314 	/*
315 	 * If swapping to an nfs node:	(swdevt[0].sw_dev == NODEV)
316 	 * Create a fake mount point just for the swap vnode so that the
317 	 * swap file can be on a different server from the rootfs.
318 	 *
319 	 * Wait 5 retries, finally no swap is cool. -mickey
320 	 */
321 	error = nfs_boot_getfh(&nd.nd_boot, "swap", &nd.nd_swap, 5);
322 	if (!error) {
323 		mp = nfs_mount_diskless(&nd.nd_swap, "/swap", 0);
324 		nfs_root(mp, &vp);
325 		vfs_unbusy(mp);
326 
327 		/*
328 		 * Since the swap file is not the root dir of a file system,
329 		 * hack it to a regular file.
330 		 */
331 		vp->v_type = VREG;
332 		vp->v_flag = 0;
333 
334 		/*
335 		 * Next line is a hack to make swapmount() work on NFS
336 		 * swap files.
337 		 * XXX-smurph
338 		 */
339 		swdevt[0].sw_dev = NETDEV;
340 		/* end hack */
341 		swdevt[0].sw_vp = vp;
342 
343 		/*
344 		 * Find out how large the swap file is.
345 		 */
346 		error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
347 		if (error)
348 			printf("nfs_mountroot: getattr for swap\n");
349 		n = (long) (attr.va_size >> DEV_BSHIFT);
350 
351 		printf("swap on %s\n", nd.nd_swap.ndm_host);
352 #ifdef	DEBUG
353 		printf("swap size: 0x%lx (blocks)\n", n);
354 #endif
355 		return (0);
356 	}
357 
358 	printf("WARNING: no swap\n");
359 	swdevt[0].sw_dev = NODEV;
360 	swdevt[0].sw_vp = NULL;
361 
362 	return (0);
363 }
364 
365 /*
366  * Internal version of mount system call for diskless setup.
367  */
368 struct mount *
369 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag)
370 {
371 	struct nfs_args args;
372 	struct mount *mp;
373 	struct mbuf *m;
374 	int error;
375 
376 	if (vfs_rootmountalloc("nfs", mntname, &mp))
377 		panic("nfs_mount_diskless: vfs_rootmountalloc failed");
378 	mp->mnt_flag |= mntflag;
379 
380 	/* Initialize mount args. */
381 	bzero((caddr_t) &args, sizeof(args));
382 	args.addr     = (struct sockaddr *)&ndmntp->ndm_saddr;
383 	args.addrlen  = args.addr->sa_len;
384 	args.sotype   = SOCK_DGRAM;
385 	args.fh	      = ndmntp->ndm_fh;
386 	args.fhsize   = NFSX_V2FH;
387 	args.hostname = ndmntp->ndm_host;
388 
389 #ifdef	NFS_BOOT_OPTIONS
390 	args.flags    |= NFS_BOOT_OPTIONS;
391 #endif
392 #ifdef	NFS_BOOT_RWSIZE
393 	/*
394 	 * Reduce rsize,wsize for interfaces that consistently
395 	 * drop fragments of long UDP messages.	 (i.e. wd8003).
396 	 * You can always change these later via remount.
397 	 */
398 	args.flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
399 	args.wsize    = NFS_BOOT_RWSIZE;
400 	args.rsize    = NFS_BOOT_RWSIZE;
401 #endif
402 
403 	/* Get mbuf for server sockaddr. */
404 	m = m_get(M_WAIT, MT_SONAME);
405 	bcopy((caddr_t)args.addr, mtod(m, caddr_t),
406 	    (m->m_len = args.addr->sa_len));
407 
408 	error = mountnfs(&args, mp, m, mntname, args.hostname);
409 	if (error)
410 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
411 
412 	return (mp);
413 }
414 
415 void
416 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp,
417     struct nfs_args *nargp)
418 {
419 	int s;
420 	int adjsock = 0;
421 	int maxio;
422 
423 	s = splsoftnet();
424 
425 #if 0
426 	/* Re-bind if rsrvd port requested and wasn't on one */
427 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
428 		  && (argp->flags & NFSMNT_RESVPORT);
429 #endif
430 	/* Also re-bind if we're switching to/from a connected UDP socket */
431 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
432 	    (argp->flags & NFSMNT_NOCONN));
433 
434 	/* Update flags atomically.  Don't change the lock bits. */
435 	nmp->nm_flag =
436 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
437 	splx(s);
438 
439 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
440 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
441 		if (nmp->nm_timeo < NFS_MINTIMEO)
442 			nmp->nm_timeo = NFS_MINTIMEO;
443 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
444 			nmp->nm_timeo = NFS_MAXTIMEO;
445 	}
446 
447 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1)
448 		nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT);
449 	if (!(nmp->nm_flag & NFSMNT_SOFT))
450 		nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */
451 
452 	if (argp->flags & NFSMNT_NFSV3) {
453 		if (argp->sotype == SOCK_DGRAM)
454 			maxio = NFS_MAXDGRAMDATA;
455 		else
456 			maxio = NFS_MAXDATA;
457 	} else
458 		maxio = NFS_V2MAXDATA;
459 
460 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
461 		int osize = nmp->nm_wsize;
462 		nmp->nm_wsize = argp->wsize;
463 		/* Round down to multiple of blocksize */
464 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
465 		if (nmp->nm_wsize <= 0)
466 			nmp->nm_wsize = NFS_FABLKSIZE;
467 		adjsock |= (nmp->nm_wsize != osize);
468 	}
469 	if (nmp->nm_wsize > maxio)
470 		nmp->nm_wsize = maxio;
471 	if (nmp->nm_wsize > MAXBSIZE)
472 		nmp->nm_wsize = MAXBSIZE;
473 
474 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
475 		int osize = nmp->nm_rsize;
476 		nmp->nm_rsize = argp->rsize;
477 		/* Round down to multiple of blocksize */
478 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
479 		if (nmp->nm_rsize <= 0)
480 			nmp->nm_rsize = NFS_FABLKSIZE;
481 		adjsock |= (nmp->nm_rsize != osize);
482 	}
483 	if (nmp->nm_rsize > maxio)
484 		nmp->nm_rsize = maxio;
485 	if (nmp->nm_rsize > MAXBSIZE)
486 		nmp->nm_rsize = MAXBSIZE;
487 
488 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
489 		nmp->nm_readdirsize = argp->readdirsize;
490 		/* Round down to multiple of blocksize */
491 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
492 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
493 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
494 	} else if (argp->flags & NFSMNT_RSIZE)
495 		nmp->nm_readdirsize = nmp->nm_rsize;
496 
497 	if (nmp->nm_readdirsize > maxio)
498 		nmp->nm_readdirsize = maxio;
499 
500 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
501 		argp->maxgrouplist <= NFS_MAXGRPS)
502 		nmp->nm_numgrps = argp->maxgrouplist;
503 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
504 		argp->readahead <= NFS_MAXRAHEAD)
505 		nmp->nm_readahead = argp->readahead;
506 	if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) {
507 		if (argp->acregmin > 0xffff)
508 			nmp->nm_acregmin = 0xffff;
509 		else
510 			nmp->nm_acregmin = argp->acregmin;
511 	}
512 	if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) {
513 		if (argp->acregmax > 0xffff)
514 			nmp->nm_acregmax = 0xffff;
515 		else
516 			nmp->nm_acregmax = argp->acregmax;
517 	}
518 	if (nmp->nm_acregmin > nmp->nm_acregmax)
519 	  nmp->nm_acregmin = nmp->nm_acregmax;
520 
521 	if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) {
522 		if (argp->acdirmin > 0xffff)
523 			nmp->nm_acdirmin = 0xffff;
524 		else
525 			nmp->nm_acdirmin = argp->acdirmin;
526 	}
527 	if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) {
528 		if (argp->acdirmax > 0xffff)
529 			nmp->nm_acdirmax = 0xffff;
530 		else
531 			nmp->nm_acdirmax = argp->acdirmax;
532 	}
533 	if (nmp->nm_acdirmin > nmp->nm_acdirmax)
534 	  nmp->nm_acdirmin = nmp->nm_acdirmax;
535 
536 	if (nmp->nm_so && adjsock) {
537 		nfs_disconnect(nmp);
538 		if (nmp->nm_sotype == SOCK_DGRAM)
539 			while (nfs_connect(nmp, NULL)) {
540 				printf("nfs_args: retrying connect\n");
541 				(void) tsleep((caddr_t)&lbolt,
542 					      PSOCK, "nfscon", 0);
543 			}
544 	}
545 
546 	/* Update nargp based on nmp */
547 	nargp->wsize = nmp->nm_wsize;
548 	nargp->rsize = nmp->nm_rsize;
549 	nargp->readdirsize = nmp->nm_readdirsize;
550 	nargp->timeo = nmp->nm_timeo;
551 	nargp->retrans = nmp->nm_retry;
552 	nargp->maxgrouplist = nmp->nm_numgrps;
553 	nargp->readahead = nmp->nm_readahead;
554 	nargp->acregmin = nmp->nm_acregmin;
555 	nargp->acregmax = nmp->nm_acregmax;
556 	nargp->acdirmin = nmp->nm_acdirmin;
557 	nargp->acdirmax = nmp->nm_acdirmax;
558 }
559 
560 /*
561  * VFS Operations.
562  *
563  * mount system call
564  * It seems a bit dumb to copyinstr() the host here and then
565  * bcopy() it in mountnfs(), but I wanted to detect errors before
566  * doing the sockargs() call because sockargs() allocates an mbuf and
567  * an error after that means that I have to release the mbuf.
568  */
569 /* ARGSUSED */
570 int
571 nfs_mount(struct mount *mp, const char *path, void *data,
572     struct nameidata *ndp, struct proc *p)
573 {
574 	int error;
575 	struct nfs_args args;
576 	struct mbuf *nam;
577 	char hst[MNAMELEN];
578 	size_t len;
579 	u_char nfh[NFSX_V3FHMAX];
580 
581 	error = copyin(data, &args, sizeof(args.version));
582 	if (error)
583 		return (error);
584 	if (args.version == 3) {
585 		error = copyin (data, (caddr_t)&args,
586 				sizeof (struct nfs_args3));
587 		args.flags &= ~(NFSMNT_INTERNAL|NFSMNT_NOAC);
588 	} else if (args.version == NFS_ARGSVERSION) {
589 		error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
590 		args.flags &= ~NFSMNT_NOAC; /* XXX - compatibility */
591 	} else
592 		return (EPROGMISMATCH);
593 	if (error)
594 		return (error);
595 
596 	if ((args.flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS)
597 		return (EINVAL);
598 
599 	if (nfs_niothreads < 0) {
600 		nfs_niothreads = 4;
601 		nfs_getset_niothreads(1);
602 	}
603 
604 	if (mp->mnt_flag & MNT_UPDATE) {
605 		struct nfsmount *nmp = VFSTONFS(mp);
606 
607 		if (nmp == NULL)
608 			return (EIO);
609 		/*
610 		 * When doing an update, we can't change from or to
611 		 * v3.
612 		 */
613 		args.flags = (args.flags & ~(NFSMNT_NFSV3)) |
614 		    (nmp->nm_flag & (NFSMNT_NFSV3));
615 		nfs_decode_args(nmp, &args, &mp->mnt_stat.mount_info.nfs_args);
616 		return (0);
617 	}
618 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
619 		return (EINVAL);
620 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
621 	if (error)
622 		return (error);
623 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
624 	if (error)
625 		return (error);
626 	bzero(&hst[len], MNAMELEN - len);
627 	/* sockargs() call must be after above copyin() calls */
628 	error = sockargs(&nam, args.addr, args.addrlen, MT_SONAME);
629 	if (error)
630 		return (error);
631 	args.fh = nfh;
632 	error = mountnfs(&args, mp, nam, path, hst);
633 	return (error);
634 }
635 
636 /*
637  * Common code for mount and mountroot
638  */
639 int
640 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam,
641     const char *pth, char *hst)
642 {
643 	struct nfsmount *nmp;
644 	int error;
645 
646 	if (mp->mnt_flag & MNT_UPDATE) {
647 		nmp = VFSTONFS(mp);
648 		/* update paths, file handles, etc, here	XXX */
649 		m_freem(nam);
650 		return (0);
651 	} else {
652 		nmp = malloc(sizeof(struct nfsmount), M_NFSMNT,
653 		    M_WAITOK|M_ZERO);
654 		mp->mnt_data = (qaddr_t)nmp;
655 	}
656 
657 	vfs_getnewfsid(mp);
658 	nmp->nm_mountp = mp;
659 	nmp->nm_timeo = NFS_TIMEO;
660 	nmp->nm_retry = NFS_RETRANS;
661 	nmp->nm_wsize = NFS_WSIZE;
662 	nmp->nm_rsize = NFS_RSIZE;
663 	nmp->nm_readdirsize = NFS_READDIRSIZE;
664 	nmp->nm_numgrps = NFS_MAXGRPS;
665 	nmp->nm_readahead = NFS_DEFRAHEAD;
666 	nmp->nm_fhsize = argp->fhsize;
667 	nmp->nm_acregmin = NFS_MINATTRTIMO;
668 	nmp->nm_acregmax = NFS_MAXATTRTIMO;
669 	nmp->nm_acdirmin = NFS_MINATTRTIMO;
670 	nmp->nm_acdirmax = NFS_MAXATTRTIMO;
671 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
672 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN);
673 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
674 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
675 	bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp));
676 	nmp->nm_nam = nam;
677 	nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args);
678 
679 	RB_INIT(&nmp->nm_ntree);
680 	TAILQ_INIT(&nmp->nm_reqsq);
681 	timeout_set(&nmp->nm_rtimeout, nfs_timer, nmp);
682 
683 	/* Set up the sockets and per-host congestion */
684 	nmp->nm_sotype = argp->sotype;
685 	nmp->nm_soproto = argp->proto;
686 
687 	/*
688 	 * For Connection based sockets (TCP,...) defer the connect until
689 	 * the first request, in case the server is not responding.
690 	 */
691 	if (nmp->nm_sotype == SOCK_DGRAM &&
692 	    (error = nfs_connect(nmp, NULL)))
693 		goto bad;
694 
695 	/*
696 	 * This is silly, but it has to be set so that vinifod() works.
697 	 * We do not want to do an nfs_statfs() here since we can get
698 	 * stuck on a dead server and we are holding a lock on the mount
699 	 * point.
700 	 */
701 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
702 
703 	return (0);
704 bad:
705 	nfs_disconnect(nmp);
706 	free((caddr_t)nmp, M_NFSMNT);
707 	m_freem(nam);
708 	return (error);
709 }
710 
711 /* unmount system call */
712 int
713 nfs_unmount(struct mount *mp, int mntflags, struct proc *p)
714 {
715 	struct nfsmount *nmp;
716 	int error, flags;
717 
718 	nmp = VFSTONFS(mp);
719 	flags = 0;
720 
721 	if (mntflags & MNT_FORCE)
722 		flags |= FORCECLOSE;
723 
724 	error = vflush(mp, NULL, flags);
725 	if (error)
726 		return (error);
727 
728 	nfs_disconnect(nmp);
729 	m_freem(nmp->nm_nam);
730 	timeout_del(&nmp->nm_rtimeout);
731 	free(nmp, M_NFSMNT);
732 	return (0);
733 }
734 
735 /*
736  * Return root of a filesystem
737  */
738 int
739 nfs_root(struct mount *mp, struct vnode **vpp)
740 {
741 	struct nfsmount *nmp;
742 	struct nfsnode *np;
743 	int error;
744 
745 	nmp = VFSTONFS(mp);
746 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
747 	if (error)
748 		return (error);
749 	*vpp = NFSTOV(np);
750 	return (0);
751 }
752 
753 /*
754  * Flush out the buffer cache
755  */
756 int
757 nfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
758 {
759 	struct vnode *vp;
760 	int error, allerror = 0;
761 
762 	/*
763 	 * Don't traverse the vnode list if we want to skip all of them.
764 	 */
765 	if (waitfor == MNT_LAZY)
766 		return (allerror);
767 
768 	/*
769 	 * Force stale buffer cache information to be flushed.
770 	 */
771 loop:
772 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
773 		/*
774 		 * If the vnode that we are about to sync is no longer
775 		 * associated with this mount point, start over.
776 		 */
777 		if (vp->v_mount != mp)
778 			goto loop;
779 		if (VOP_ISLOCKED(vp) || LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
780 			continue;
781 		if (vget(vp, LK_EXCLUSIVE, p))
782 			goto loop;
783 		error = VOP_FSYNC(vp, cred, waitfor, p);
784 		if (error)
785 			allerror = error;
786 		vput(vp);
787 	}
788 
789 	return (allerror);
790 }
791 
792 /*
793  * NFS flat namespace lookup.
794  * Currently unsupported.
795  */
796 /* ARGSUSED */
797 int
798 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
799 {
800 
801 	return (EOPNOTSUPP);
802 }
803 
804 /*
805  * Do that sysctl thang...
806  */
807 int
808 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
809     size_t newlen, struct proc *p)
810 {
811 	int rv;
812 
813 	/*
814 	 * All names at this level are terminal.
815 	 */
816 	if(namelen > 1)
817 		return ENOTDIR;	/* overloaded */
818 
819 	switch(name[0]) {
820 	case NFS_NFSSTATS:
821 		if(!oldp) {
822 			*oldlenp = sizeof nfsstats;
823 			return 0;
824 		}
825 
826 		if(*oldlenp < sizeof nfsstats) {
827 			*oldlenp = sizeof nfsstats;
828 			return ENOMEM;
829 		}
830 
831 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
832 		if(rv) return rv;
833 
834 		if(newp && newlen != sizeof nfsstats)
835 			return EINVAL;
836 
837 		if(newp) {
838 			return copyin(newp, &nfsstats, sizeof nfsstats);
839 		}
840 		return 0;
841 
842 	case NFS_NIOTHREADS:
843 		nfs_getset_niothreads(0);
844 
845 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads);
846 		if (newp)
847 			nfs_getset_niothreads(1);
848 
849 		return rv;
850 
851 	default:
852 		return EOPNOTSUPP;
853 	}
854 }
855 
856 
857 /*
858  * At this point, this should never happen
859  */
860 /* ARGSUSED */
861 int
862 nfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
863 {
864 	return (EINVAL);
865 }
866 
867 /*
868  * Vnode pointer to File handle, should never happen either
869  */
870 /* ARGSUSED */
871 int
872 nfs_vptofh(struct vnode *vp, struct fid *fhp)
873 {
874 	return (EINVAL);
875 }
876 
877 /*
878  * Vfs start routine, a no-op.
879  */
880 /* ARGSUSED */
881 int
882 nfs_start(struct mount *mp, int flags, struct proc *p)
883 {
884 	return (0);
885 }
886 
887 /*
888  * Do operations associated with quotas, not supported
889  */
890 /* ARGSUSED */
891 int
892 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, struct proc *p)
893 {
894 	return (EOPNOTSUPP);
895 }
896 
897 /*
898  * check export permission, not supported
899  */
900 /* ARGUSED */
901 int
902 nfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
903     struct ucred **credanonp)
904 {
905 	return (EOPNOTSUPP);
906 }
907 
908