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