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