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