xref: /netbsd-src/sys/nfs/nfs_vfsops.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: nfs_vfsops.c,v 1.66 1997/10/19 01:46:40 fvdl 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/device.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 
57 #include <net/if.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 
61 #include <nfs/rpcv2.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfsnode.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsmount.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsdiskless.h>
69 #include <nfs/nqnfs.h>
70 #include <nfs/nfs_var.h>
71 
72 extern struct nfsstats nfsstats;
73 extern int nfs_ticks;
74 
75 #ifdef notyet
76 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
77 		      struct proc *);
78 #endif
79 
80 /*
81  * nfs vfs operations.
82  */
83 struct vfsops nfs_vfsops = {
84 	MOUNT_NFS,
85 	nfs_mount,
86 	nfs_start,
87 	nfs_unmount,
88 	nfs_root,
89 	nfs_quotactl,
90 	nfs_statfs,
91 	nfs_sync,
92 	nfs_vget,
93 	nfs_fhtovp,
94 	nfs_vptofh,
95 	nfs_vfs_init,
96 	nfs_mountroot,
97 #ifdef notyet
98 	nfs_sysctl
99 #endif
100 };
101 
102 extern u_int32_t nfs_procids[NFS_NPROCS];
103 extern u_int32_t nfs_prog, nfs_vers;
104 
105 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
106     struct mount **, struct vnode **, struct proc *));
107 
108 #define TRUE	1
109 #define	FALSE	0
110 
111 /*
112  * nfs statfs call
113  */
114 int
115 nfs_statfs(mp, sbp, p)
116 	struct mount *mp;
117 	register struct statfs *sbp;
118 	struct proc *p;
119 {
120 	register struct vnode *vp;
121 	register struct nfs_statfs *sfp;
122 	register caddr_t cp;
123 	register u_int32_t *tl;
124 	register int32_t t1, t2;
125 	caddr_t bpos, dpos, cp2;
126 	struct nfsmount *nmp = VFSTONFS(mp);
127 	int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
128 	struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
129 	struct ucred *cred;
130 	struct nfsnode *np;
131 	u_quad_t tquad;
132 
133 #ifndef nolint
134 	sfp = (struct nfs_statfs *)0;
135 #endif
136 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
137 	if (error)
138 		return (error);
139 	vp = NFSTOV(np);
140 	cred = crget();
141 	cred->cr_ngroups = 0;
142 	if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
143 		(void)nfs_fsinfo(nmp, vp, cred, p);
144 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
145 	nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
146 	nfsm_fhtom(vp, v3);
147 	nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
148 	if (v3)
149 		nfsm_postop_attr(vp, retattr);
150 	if (error) {
151 		if (mrep != NULL)
152 			m_free(mrep);
153 		goto nfsmout;
154 	}
155 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
156 #ifdef COMPAT_09
157 	sbp->f_type = 2;
158 #else
159 	sbp->f_type = 0;
160 #endif
161 	sbp->f_flags = nmp->nm_flag;
162 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
163 	if (v3) {
164 		sbp->f_bsize = NFS_FABLKSIZE;
165 		fxdr_hyper(&sfp->sf_tbytes, &tquad);
166 		sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
167 		fxdr_hyper(&sfp->sf_fbytes, &tquad);
168 		sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
169 		fxdr_hyper(&sfp->sf_abytes, &tquad);
170 		sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
171 		sbp->f_files = (fxdr_unsigned(int32_t,
172 		    sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
173 		sbp->f_ffree = (fxdr_unsigned(int32_t,
174 		    sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
175 	} else {
176 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
177 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
178 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
179 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
180 		sbp->f_files = 0;
181 		sbp->f_ffree = 0;
182 	}
183 	if (sbp != &mp->mnt_stat) {
184 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
185 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
186 	}
187 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
188 	nfsm_reqdone;
189 	vrele(vp);
190 	crfree(cred);
191 	return (error);
192 }
193 
194 /*
195  * nfs version 3 fsinfo rpc call
196  */
197 int
198 nfs_fsinfo(nmp, vp, cred, p)
199 	register struct nfsmount *nmp;
200 	register struct vnode *vp;
201 	struct ucred *cred;
202 	struct proc *p;
203 {
204 	register struct nfsv3_fsinfo *fsp;
205 	register caddr_t cp;
206 	register int32_t t1, t2;
207 	register u_int32_t *tl, pref, max;
208 	caddr_t bpos, dpos, cp2;
209 	int error = 0, retattr;
210 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
211 	u_int64_t maxfsize;
212 
213 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
214 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
215 	nfsm_fhtom(vp, 1);
216 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
217 	nfsm_postop_attr(vp, retattr);
218 	if (!error) {
219 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
220 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
221 		if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
222 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
223 				~(NFS_FABLKSIZE - 1);
224 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
225 		if (max < nmp->nm_wsize && max > 0) {
226 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
227 			if (nmp->nm_wsize == 0)
228 				nmp->nm_wsize = max;
229 		}
230 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
231 		if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
232 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
233 				~(NFS_FABLKSIZE - 1);
234 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
235 		if (max < nmp->nm_rsize && max > 0) {
236 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
237 			if (nmp->nm_rsize == 0)
238 				nmp->nm_rsize = max;
239 		}
240 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
241 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
242 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
243 				~(NFS_DIRFRAGSIZ - 1);
244 		if (max < nmp->nm_readdirsize && max > 0) {
245 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
246 			if (nmp->nm_readdirsize == 0)
247 				nmp->nm_readdirsize = max;
248 		}
249 		/* XXX */
250 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
251 		fxdr_hyper(&fsp->fs_maxfilesize, &maxfsize);
252 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
253 			nmp->nm_maxfilesize = maxfsize;
254 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
255 	}
256 	nfsm_reqdone;
257 	return (error);
258 }
259 
260 /*
261  * Mount a remote root fs via. NFS.  It goes like this:
262  * - Call nfs_boot_init() to fill in the nfs_diskless struct
263  * - build the rootfs mount point and call mountnfs() to do the rest.
264  */
265 int
266 nfs_mountroot()
267 {
268 	struct nfs_diskless *nd;
269 	struct vattr attr;
270 	struct mount *mp;
271 	struct vnode *vp;
272 	struct proc *procp;
273 	long n;
274 	int error;
275 
276 	procp = curproc; /* XXX */
277 
278 	if (root_device->dv_class != DV_IFNET)
279 		return (ENODEV);
280 
281 	/*
282 	 * XXX time must be non-zero when we init the interface or else
283 	 * the arp code will wedge.  [Fixed now in if_ether.c]
284 	 * However, the NFS attribute cache gives false "hits" when
285 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
286 	 */
287 	if (time.tv_sec < NFS_MAXATTRTIMO)
288 		time.tv_sec = NFS_MAXATTRTIMO;
289 
290 	/*
291 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
292 	 * Side effect:  Finds and configures a network interface.
293 	 */
294 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
295 	bzero((caddr_t)nd, sizeof(*nd));
296 	error = nfs_boot_init(nd, procp);
297 	if (error)
298 		goto out;
299 
300 	/*
301 	 * Create the root mount point.
302 	 */
303 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
304 	if (error)
305 		goto out;
306 	printf("root on %s\n", nd->nd_root.ndm_host);
307 
308 	/*
309 	 * Link it into the mount list.
310 	 */
311 #ifdef Lite2_integrated
312 	simple_lock(&mountlist_slock);
313 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
314 	simple_unlock(&mountlist_slock);
315 	rootvp = vp;
316 	vfs_unbusy(mp, procp);
317 #else
318 	if (vfs_lock(mp))
319 		panic("nfs_mountroot: vfs_lock");
320 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
321 	mp->mnt_vnodecovered = NULLVP;
322 	vfs_unlock(mp);
323 	rootvp = vp;
324 #endif
325 
326 	/* Get root attributes (for the time). */
327 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
328 	if (error)
329 		panic("nfs_mountroot: getattr for root");
330 	n = attr.va_mtime.tv_sec;
331 #ifdef	DEBUG
332 	printf("root time: 0x%lx\n", n);
333 #endif
334 	inittodr(n);
335 
336 #if 0
337 	/*
338 	 * XXX splnet, so networks will receive...
339 	 * XXX Which port needed this hack?
340 	 * XXX Should already be at spl0.
341 	 */
342 	splnet();
343 #endif
344 
345 #if 0	/* swap now comes in from swapctl(2) */
346 #ifdef notyet
347 	/* Set up swap credentials. */
348 	proc0.p_ucred->cr_uid = 0;
349 	proc0.p_ucred->cr_gid = 0;
350 	proc0.p_ucred->cr_ngroups = 0;
351 #endif
352 
353 	/*
354 	 * "Mount" the swap device.
355 	 *
356 	 * On a "dataless" configuration (swap on disk) we will have:
357 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
358 	 */
359 	if (bdevvp(swapdev, &swapdev_vp))
360 		panic("nfs_mountroot: can't setup swap vp");
361 	if (swdevt[0].sw_dev != NODEV) {
362 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
363 		error = 0;
364 		goto out;
365 	}
366 
367 	/*
368 	 * If swapping to an nfs node:  (swdevt[0].sw_dev == NODEV)
369 	 * Create a fake mount point just for the swap vnode so that the
370 	 * swap file can be on a different server from the rootfs.
371 	 */
372 	error = nfs_mount_diskless(&nd->nd_swap, "/swap", &mp, &vp, procp);
373 	if (error) {
374 		printf("nfs_boot: warning: mount(swap), error=%d\n", error);
375 		error = 0;
376 		goto out;
377 	}
378 #ifdef Lite2_integrated
379 	vfs_unbusy(mp, procp);
380 #endif
381 	printf("swap on %s\n", nd->nd_swap.ndm_host);
382 
383 	/*
384 	 * Since the swap file is not the root dir of a file system,
385 	 * hack it to a regular file.
386 	 */
387 	vp->v_type = VREG;
388 	vp->v_flag = 0;
389 	swdevt[0].sw_vp = vp;
390 
391 	/*
392 	 * Find out how large the swap file is.
393 	 */
394 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
395 	if (error)
396 		panic("nfs_mountroot: getattr for swap");
397 	n = (long) (attr.va_size >> DEV_BSHIFT);
398 #ifdef	DEBUG
399 	printf("swap size: 0x%lx (blocks)\n", n);
400 #endif
401 	swdevt[0].sw_nblks = n;
402 #endif
403 
404 out:
405 	free(nd, M_NFSMNT);
406 	return (error);
407 }
408 
409 /*
410  * Internal version of mount system call for diskless setup.
411  * Separate function because we used to call it twice.
412  * (once for root and once for swap)
413  */
414 static int
415 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
416 	struct nfs_dlmount *ndmntp;
417 	const char *mntname;	/* mount point name */
418 	struct mount **mpp;
419 	struct vnode **vpp;
420 	struct proc *p;
421 {
422 	struct mount *mp;
423 	struct mbuf *m;
424 	int error;
425 
426 #ifdef Lite2_integrated
427 	vfs_rootmountalloc("nfs", mntname, &mp);
428 #else
429 	/* Create the mount point. */
430 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
431 	    M_MOUNT, M_WAITOK);
432 	bzero((char *)mp, (u_long)sizeof(struct mount));
433 #endif
434 	mp->mnt_op = &nfs_vfsops;
435 	/* mp->mnt_flag = 0 */
436 
437 	/* Get mbuf for server sockaddr. */
438 	m = m_get(M_WAIT, MT_SONAME);
439 	if (m == NULL)
440 		panic("nfs_mountroot: mget soname for %s", mntname);
441 	bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t),
442 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
443 
444 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
445 			 ndmntp->ndm_args.hostname, vpp, p);
446 	if (error) {
447 		printf("nfs_mountroot: mount %s failed: %d\n",
448 		       mntname, error);
449 		free(mp, M_MOUNT);
450 	} else
451 		*mpp = mp;
452 
453 	return (error);
454 }
455 
456 void
457 nfs_decode_args(nmp, argp)
458 	struct nfsmount *nmp;
459 	struct nfs_args *argp;
460 {
461 	int s;
462 	int adjsock;
463 	int maxio;
464 
465 	s = splsoftnet();
466 
467 	/*
468 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
469 	 * no sense in that context.
470 	 */
471 	if (argp->sotype == SOCK_STREAM)
472 		argp->flags &= ~NFSMNT_NOCONN;
473 
474 	/*
475 	 * Cookie translation is not needed for v2, silently ignore it.
476 	 */
477 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
478 	    NFSMNT_XLATECOOKIE)
479 		argp->flags &= ~NFSMNT_XLATECOOKIE;
480 
481 	/* Re-bind if rsrvd port requested and wasn't on one */
482 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
483 		  && (argp->flags & NFSMNT_RESVPORT);
484 	/* Also re-bind if we're switching to/from a connected UDP socket */
485 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
486 		    (argp->flags & NFSMNT_NOCONN));
487 
488 	/* Update flags atomically.  Don't change the lock bits. */
489 	nmp->nm_flag = argp->flags | nmp->nm_flag;
490 	splx(s);
491 
492 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
493 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
494 		if (nmp->nm_timeo < NFS_MINTIMEO)
495 			nmp->nm_timeo = NFS_MINTIMEO;
496 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
497 			nmp->nm_timeo = NFS_MAXTIMEO;
498 	}
499 
500 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
501 		nmp->nm_retry = argp->retrans;
502 		if (nmp->nm_retry > NFS_MAXREXMIT)
503 			nmp->nm_retry = NFS_MAXREXMIT;
504 	}
505 
506 	if (argp->flags & NFSMNT_NFSV3) {
507 		if (argp->sotype == SOCK_DGRAM)
508 			maxio = NFS_MAXDGRAMDATA;
509 		else
510 			maxio = NFS_MAXDATA;
511 	} else
512 		maxio = NFS_V2MAXDATA;
513 
514 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
515 		int osize = nmp->nm_wsize;
516 		nmp->nm_wsize = argp->wsize;
517 		/* Round down to multiple of blocksize */
518 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
519 		if (nmp->nm_wsize <= 0)
520 			nmp->nm_wsize = NFS_FABLKSIZE;
521 		adjsock |= (nmp->nm_wsize != osize);
522 	}
523 	if (nmp->nm_wsize > maxio)
524 		nmp->nm_wsize = maxio;
525 	if (nmp->nm_wsize > MAXBSIZE)
526 		nmp->nm_wsize = MAXBSIZE;
527 
528 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
529 		int osize = nmp->nm_rsize;
530 		nmp->nm_rsize = argp->rsize;
531 		/* Round down to multiple of blocksize */
532 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
533 		if (nmp->nm_rsize <= 0)
534 			nmp->nm_rsize = NFS_FABLKSIZE;
535 		adjsock |= (nmp->nm_rsize != osize);
536 	}
537 	if (nmp->nm_rsize > maxio)
538 		nmp->nm_rsize = maxio;
539 	if (nmp->nm_rsize > MAXBSIZE)
540 		nmp->nm_rsize = MAXBSIZE;
541 
542 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
543 		nmp->nm_readdirsize = argp->readdirsize;
544 		/* Round down to multiple of minimum blocksize */
545 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
546 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
547 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
548 		/* Bigger than buffer size makes no sense */
549 		if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
550 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
551 	} else if (argp->flags & NFSMNT_RSIZE)
552 		nmp->nm_readdirsize = nmp->nm_rsize;
553 
554 	if (nmp->nm_readdirsize > maxio)
555 		nmp->nm_readdirsize = maxio;
556 
557 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
558 		argp->maxgrouplist <= NFS_MAXGRPS)
559 		nmp->nm_numgrps = argp->maxgrouplist;
560 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
561 		argp->readahead <= NFS_MAXRAHEAD)
562 		nmp->nm_readahead = argp->readahead;
563 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
564 		argp->leaseterm <= NQ_MAXLEASE)
565 		nmp->nm_leaseterm = argp->leaseterm;
566 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
567 		argp->deadthresh <= NQ_NEVERDEAD)
568 		nmp->nm_deadthresh = argp->deadthresh;
569 
570 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
571 		    (nmp->nm_soproto != argp->proto));
572 	nmp->nm_sotype = argp->sotype;
573 	nmp->nm_soproto = argp->proto;
574 
575 	if (nmp->nm_so && adjsock) {
576 		nfs_disconnect(nmp);
577 		if (nmp->nm_sotype == SOCK_DGRAM)
578 			while (nfs_connect(nmp, (struct nfsreq *)0)) {
579 				printf("nfs_args: retrying connect\n");
580 				(void) tsleep((caddr_t)&lbolt,
581 					      PSOCK, "nfscon", 0);
582 			}
583 	}
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 	struct vnode *vp;
608 	char pth[MNAMELEN], hst[MNAMELEN];
609 	size_t len;
610 	u_char nfh[NFSX_V3FHMAX];
611 
612 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
613 	if (error)
614 		return (error);
615 	if (args.version != NFS_ARGSVERSION)
616 		return (EPROGMISMATCH);
617 	if (mp->mnt_flag & MNT_UPDATE) {
618 		register struct nfsmount *nmp = VFSTONFS(mp);
619 
620 		if (nmp == NULL)
621 			return (EIO);
622 		/*
623 		 * When doing an update, we can't change from or to
624 		 * v3 and/or nqnfs, or change cookie translation
625 		 */
626 		args.flags = (args.flags &
627 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
628 		    (nmp->nm_flag &
629 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
630 		nfs_decode_args(nmp, &args);
631 		return (0);
632 	}
633 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
634 	if (error)
635 		return (error);
636 	error = copyinstr(path, pth, MNAMELEN-1, &len);
637 	if (error)
638 		return (error);
639 	bzero(&pth[len], MNAMELEN - len);
640 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
641 	if (error)
642 		return (error);
643 	bzero(&hst[len], MNAMELEN - len);
644 	/* sockargs() call must be after above copyin() calls */
645 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
646 	if (error)
647 		return (error);
648 	args.fh = nfh;
649 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
650 	return (error);
651 }
652 
653 /*
654  * Common code for mount and mountroot
655  */
656 int
657 mountnfs(argp, mp, nam, pth, hst, vpp, p)
658 	register struct nfs_args *argp;
659 	register struct mount *mp;
660 	struct mbuf *nam;
661 	const char *pth, *hst;
662 	struct vnode **vpp;
663 	struct proc *p;
664 {
665 	register struct nfsmount *nmp;
666 	struct nfsnode *np;
667 	int error;
668 	struct vattr attrs;
669 	struct ucred *cr;
670 
671 	if (mp->mnt_flag & MNT_UPDATE) {
672 		nmp = VFSTONFS(mp);
673 		/* update paths, file handles, etc, here	XXX */
674 		m_freem(nam);
675 		return (0);
676 	} else {
677 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
678 		    M_NFSMNT, M_WAITOK);
679 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
680 		mp->mnt_data = (qaddr_t)nmp;
681 		TAILQ_INIT(&nmp->nm_uidlruhead);
682 		TAILQ_INIT(&nmp->nm_bufq);
683 	}
684 #ifdef Lite2_integrated
685 	vfs_getnewfsid(mp, makefstype(MOUNT_NFS));
686 #else
687 	getnewfsid(mp, makefstype(MOUNT_NFS));
688 #endif
689 	nmp->nm_mountp = mp;
690 
691 	if (argp->flags & NFSMNT_NQNFS)
692 		/*
693 		 * We have to set mnt_maxsymlink to a non-zero value so
694 		 * that COMPAT_43 routines will know that we are setting
695 		 * the d_type field in directories (and can zero it for
696 		 * unsuspecting binaries).
697 		 */
698 		mp->mnt_maxsymlinklen = 1;
699 
700 	if ((argp->flags & NFSMNT_NFSV3) == 0)
701 		/*
702 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
703 		 * will fill this in.
704 		 */
705 		nmp->nm_maxfilesize = 0xffffffffLL;
706 
707 	nmp->nm_timeo = NFS_TIMEO;
708 	nmp->nm_retry = NFS_RETRANS;
709 	nmp->nm_wsize = NFS_WSIZE;
710 	nmp->nm_rsize = NFS_RSIZE;
711 	nmp->nm_readdirsize = NFS_READDIRSIZE;
712 	nmp->nm_numgrps = NFS_MAXGRPS;
713 	nmp->nm_readahead = NFS_DEFRAHEAD;
714 	nmp->nm_leaseterm = NQ_DEFLEASE;
715 	nmp->nm_deadthresh = NQ_DEADTHRESH;
716 	CIRCLEQ_INIT(&nmp->nm_timerhead);
717 	nmp->nm_inprog = NULLVP;
718 	nmp->nm_fhsize = argp->fhsize;
719 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
720 #ifdef COMPAT_09
721 	mp->mnt_stat.f_type = 2;
722 #else
723 	mp->mnt_stat.f_type = 0;
724 #endif
725 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
726 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
727 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
728 	nmp->nm_nam = nam;
729 
730 	/* Set up the sockets and per-host congestion */
731 	nmp->nm_sotype = argp->sotype;
732 	nmp->nm_soproto = argp->proto;
733 
734 	nfs_decode_args(nmp, argp);
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 	/*
752 	 * A reference count is needed on the nfsnode representing the
753 	 * remote root.  If this object is not persistent, then backward
754 	 * traversals of the mount point (i.e. "..") will not work if
755 	 * the nfsnode gets flushed out of the cache. Ufs does not have
756 	 * this problem, because one can identify root inodes by their
757 	 * number == ROOTINO (2).
758 	 */
759 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
760 	if (error)
761 		goto bad;
762 	*vpp = NFSTOV(np);
763 	VOP_GETATTR(*vpp, &attrs, p->p_ucred, p);
764 	if (nmp->nm_flag & NFSMNT_NFSV3) {
765 		cr = crget();
766 		cr->cr_uid = attrs.va_uid;
767 		cr->cr_gid = attrs.va_gid;
768 		cr->cr_ngroups = 0;
769 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
770 		crfree(cr);
771 	}
772 
773 	return (0);
774 bad:
775 	nfs_disconnect(nmp);
776 	free((caddr_t)nmp, M_NFSMNT);
777 	m_freem(nam);
778 	return (error);
779 }
780 
781 /*
782  * unmount system call
783  */
784 int
785 nfs_unmount(mp, mntflags, p)
786 	struct mount *mp;
787 	int mntflags;
788 	struct proc *p;
789 {
790 	register struct nfsmount *nmp;
791 	struct nfsnode *np;
792 	struct vnode *vp;
793 	int error, flags = 0;
794 
795 	if (mntflags & MNT_FORCE)
796 		flags |= FORCECLOSE;
797 	nmp = VFSTONFS(mp);
798 	/*
799 	 * Goes something like this..
800 	 * - Check for activity on the root vnode (other than ourselves).
801 	 * - Call vflush() to clear out vnodes for this file system,
802 	 *   except for the root vnode.
803 	 * - Decrement reference on the vnode representing remote root.
804 	 * - Close the socket
805 	 * - Free up the data structures
806 	 */
807 	/*
808 	 * We need to decrement the ref. count on the nfsnode representing
809 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
810 	 * has done vput on this vnode, otherwise we would get deadlock!
811 	 */
812 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
813 	if (error)
814 		return(error);
815 	vp = NFSTOV(np);
816 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
817 		vput(vp);
818 		return (EBUSY);
819 	}
820 
821 	/*
822 	 * Must handshake with nqnfs_clientd() if it is active.
823 	 */
824 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
825 	while (nmp->nm_inprog != NULLVP)
826 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
827 	error = vflush(mp, vp, flags);
828 	if (error) {
829 		vput(vp);
830 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
831 		return (error);
832 	}
833 
834 	/*
835 	 * We are now committed to the unmount.
836 	 * For NQNFS, let the server daemon free the nfsmount structure.
837 	 */
838 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
839 		nmp->nm_iflag |= NFSMNT_DISMNT;
840 
841 	/*
842 	 * There are two reference counts to get rid of here.
843 	 */
844 	vrele(vp);
845 	vrele(vp);
846 	vgone(vp);
847 	nfs_disconnect(nmp);
848 	m_freem(nmp->nm_nam);
849 
850 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
851 		free((caddr_t)nmp, M_NFSMNT);
852 	return (0);
853 }
854 
855 /*
856  * Return root of a filesystem
857  */
858 int
859 nfs_root(mp, vpp)
860 	struct mount *mp;
861 	struct vnode **vpp;
862 {
863 	register struct vnode *vp;
864 	struct nfsmount *nmp;
865 	struct nfsnode *np;
866 	int error;
867 
868 	nmp = VFSTONFS(mp);
869 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
870 	if (error)
871 		return (error);
872 	vp = NFSTOV(np);
873 	if (vp->v_type == VNON)
874 		vp->v_type = VDIR;
875 	vp->v_flag = VROOT;
876 	*vpp = vp;
877 	return (0);
878 }
879 
880 extern int syncprt;
881 
882 /*
883  * Flush out the buffer cache
884  */
885 /* ARGSUSED */
886 int
887 nfs_sync(mp, waitfor, cred, p)
888 	struct mount *mp;
889 	int waitfor;
890 	struct ucred *cred;
891 	struct proc *p;
892 {
893 	register struct vnode *vp;
894 	int error, allerror = 0;
895 
896 	/*
897 	 * Force stale buffer cache information to be flushed.
898 	 */
899 loop:
900 	for (vp = mp->mnt_vnodelist.lh_first;
901 	     vp != NULL;
902 	     vp = vp->v_mntvnodes.le_next) {
903 		/*
904 		 * If the vnode that we are about to sync is no longer
905 		 * associated with this mount point, start over.
906 		 */
907 		if (vp->v_mount != mp)
908 			goto loop;
909 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
910 			continue;
911 #ifdef Lite2_integrated
912 		if (vget(vp, LK_EXCLUSIVE, p))
913 #else
914 		if (vget(vp, 1))
915 #endif
916 			goto loop;
917 		error = VOP_FSYNC(vp, cred, waitfor, p);
918 		if (error)
919 			allerror = error;
920 		vput(vp);
921 	}
922 	return (allerror);
923 }
924 
925 /*
926  * NFS flat namespace lookup.
927  * Currently unsupported.
928  */
929 /* ARGSUSED */
930 int
931 nfs_vget(mp, ino, vpp)
932 	struct mount *mp;
933 	ino_t ino;
934 	struct vnode **vpp;
935 {
936 
937 	return (EOPNOTSUPP);
938 }
939 
940 #ifdef notyet
941 /*
942  * Do that sysctl thang...
943  */
944 static int
945 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
946 	   size_t newlen, struct proc *p)
947 {
948 	int rv;
949 
950 	/*
951 	 * All names at this level are terminal.
952 	 */
953 	if(namelen > 1)
954 		return ENOTDIR;	/* overloaded */
955 
956 	switch(name[0]) {
957 	case NFS_NFSSTATS:
958 		if(!oldp) {
959 			*oldlenp = sizeof nfsstats;
960 			return 0;
961 		}
962 
963 		if(*oldlenp < sizeof nfsstats) {
964 			*oldlenp = sizeof nfsstats;
965 			return ENOMEM;
966 		}
967 
968 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
969 		if(rv) return rv;
970 
971 		if(newp && newlen != sizeof nfsstats)
972 			return EINVAL;
973 
974 		if(newp) {
975 			return copyin(newp, &nfsstats, sizeof nfsstats);
976 		}
977 		return 0;
978 
979 	default:
980 		return EOPNOTSUPP;
981 	}
982 }
983 #endif
984 
985 
986 /*
987  * At this point, this should never happen
988  */
989 /* ARGSUSED */
990 int
991 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
992 	register struct mount *mp;
993 	struct fid *fhp;
994 	struct mbuf *nam;
995 	struct vnode **vpp;
996 	int *exflagsp;
997 	struct ucred **credanonp;
998 {
999 
1000 	return (EINVAL);
1001 }
1002 
1003 /*
1004  * Vnode pointer to File handle, should never happen either
1005  */
1006 /* ARGSUSED */
1007 int
1008 nfs_vptofh(vp, fhp)
1009 	struct vnode *vp;
1010 	struct fid *fhp;
1011 {
1012 
1013 	return (EINVAL);
1014 }
1015 
1016 /*
1017  * Vfs start routine, a no-op.
1018  */
1019 /* ARGSUSED */
1020 int
1021 nfs_start(mp, flags, p)
1022 	struct mount *mp;
1023 	int flags;
1024 	struct proc *p;
1025 {
1026 
1027 	return (0);
1028 }
1029 
1030 /*
1031  * Do operations associated with quotas, not supported
1032  */
1033 /* ARGSUSED */
1034 int
1035 nfs_quotactl(mp, cmd, uid, arg, p)
1036 	struct mount *mp;
1037 	int cmd;
1038 	uid_t uid;
1039 	caddr_t arg;
1040 	struct proc *p;
1041 {
1042 
1043 	return (EOPNOTSUPP);
1044 }
1045