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