xref: /netbsd-src/sys/nfs/nfs_vfsops.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: nfs_vfsops.c,v 1.143 2004/08/15 07:19:54 mycroft 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.143 2004/08/15 07:19:54 mycroft Exp $");
39 
40 #if defined(_KERNEL_OPT)
41 #include "opt_compat_netbsd.h"
42 #include "opt_nfs.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/signal.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/device.h>
51 #include <sys/vnode.h>
52 #include <sys/kernel.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/mbuf.h>
56 #include <sys/dirent.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 
62 #include <net/if.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 
66 #include <nfs/rpcv2.h>
67 #include <nfs/nfsproto.h>
68 #include <nfs/nfsnode.h>
69 #include <nfs/nfs.h>
70 #include <nfs/nfsmount.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfs/nfsm_subs.h>
73 #include <nfs/nfsdiskless.h>
74 #include <nfs/nqnfs.h>
75 #include <nfs/nfs_var.h>
76 
77 extern struct nfsstats nfsstats;
78 extern int nfs_ticks;
79 
80 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
81 
82 /*
83  * nfs vfs operations.
84  */
85 
86 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
87 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
88 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
89 
90 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
91 	&nfsv2_vnodeop_opv_desc,
92 	&spec_nfsv2nodeop_opv_desc,
93 	&fifo_nfsv2nodeop_opv_desc,
94 	NULL,
95 };
96 
97 struct vfsops nfs_vfsops = {
98 	MOUNT_NFS,
99 	nfs_mount,
100 	nfs_start,
101 	nfs_unmount,
102 	nfs_root,
103 	nfs_quotactl,
104 	nfs_statvfs,
105 	nfs_sync,
106 	nfs_vget,
107 	nfs_fhtovp,
108 	nfs_vptofh,
109 	nfs_vfs_init,
110 	nfs_vfs_reinit,
111 	nfs_vfs_done,
112 	NULL,
113 	nfs_mountroot,
114 	nfs_checkexp,
115 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
116 	nfs_vnodeopv_descs,
117 };
118 
119 extern u_int32_t nfs_procids[NFS_NPROCS];
120 extern u_int32_t nfs_prog, nfs_vers;
121 
122 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
123     struct mount **, struct vnode **, struct proc *));
124 
125 /*
126  * nfs statvfs call
127  */
128 int
129 nfs_statvfs(mp, sbp, p)
130 	struct mount *mp;
131 	struct statvfs *sbp;
132 	struct proc *p;
133 {
134 	struct vnode *vp;
135 	struct nfs_statfs *sfp;
136 	caddr_t cp;
137 	u_int32_t *tl;
138 	int32_t t1, t2;
139 	caddr_t bpos, dpos, cp2;
140 	struct nfsmount *nmp = VFSTONFS(mp);
141 	int error = 0, retattr;
142 #ifdef NFS_V2_ONLY
143 	const int v3 = 0;
144 #else
145 	int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
146 #endif
147 	struct mbuf *mreq, *mrep = NULL, *md, *mb;
148 	struct ucred *cred;
149 	u_quad_t tquad;
150 	struct nfsnode *np;
151 
152 #ifndef nolint
153 	sfp = (struct nfs_statfs *)0;
154 #endif
155 	vp = nmp->nm_vnode;
156 	np = VTONFS(vp);
157 	cred = crget();
158 	cred->cr_ngroups = 0;
159 #ifndef NFS_V2_ONLY
160 	if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
161 		(void)nfs_fsinfo(nmp, vp, cred, p);
162 #endif
163 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
164 	nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
165 	nfsm_fhtom(np, v3);
166 	nfsm_request(np, NFSPROC_FSSTAT, p, cred);
167 	if (v3)
168 		nfsm_postop_attr(vp, retattr, 0);
169 	if (error) {
170 		if (mrep != NULL) {
171 			if (mrep->m_next != NULL)
172 				printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
173 			m_freem(mrep);
174 		}
175 		goto nfsmout;
176 	}
177 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
178 	sbp->f_flag = nmp->nm_flag;
179 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
180 	if (v3) {
181 		sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
182 		tquad = fxdr_hyper(&sfp->sf_tbytes);
183 		sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
184 		tquad = fxdr_hyper(&sfp->sf_fbytes);
185 		sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
186 		tquad = fxdr_hyper(&sfp->sf_abytes);
187 		tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
188 		sbp->f_bresvd = sbp->f_bfree - tquad;
189 		sbp->f_bavail = tquad;
190 #ifdef COMPAT_20
191 		/* Handle older NFS servers returning negative values */
192 		if ((quad_t)sbp->f_bavail < 0)
193 			sbp->f_bavail = 0;
194 #endif
195 		tquad = fxdr_hyper(&sfp->sf_tfiles);
196 		sbp->f_files = tquad;
197 		tquad = fxdr_hyper(&sfp->sf_ffiles);
198 		sbp->f_ffree = tquad;
199 		sbp->f_favail = tquad;
200 		sbp->f_fresvd = 0;
201 		sbp->f_namemax = MAXNAMLEN;
202 	} else {
203 		sbp->f_bsize = NFS_FABLKSIZE;
204 		sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
205 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
206 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
207 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
208 		sbp->f_fresvd = 0;
209 		sbp->f_files = 0;
210 		sbp->f_ffree = 0;
211 		sbp->f_favail = 0;
212 		sbp->f_fresvd = 0;
213 		sbp->f_namemax = MAXNAMLEN;
214 	}
215 	copy_statvfs_info(sbp, mp);
216 	nfsm_reqdone;
217 	crfree(cred);
218 	return (error);
219 }
220 
221 #ifndef NFS_V2_ONLY
222 /*
223  * nfs version 3 fsinfo rpc call
224  */
225 int
226 nfs_fsinfo(nmp, vp, cred, p)
227 	struct nfsmount *nmp;
228 	struct vnode *vp;
229 	struct ucred *cred;
230 	struct proc *p;
231 {
232 	struct nfsv3_fsinfo *fsp;
233 	caddr_t cp;
234 	int32_t t1, t2;
235 	u_int32_t *tl, pref, max;
236 	caddr_t bpos, dpos, cp2;
237 	int error = 0, retattr;
238 	struct mbuf *mreq, *mrep, *md, *mb;
239 	u_int64_t maxfsize;
240 	struct nfsnode *np = VTONFS(vp);
241 
242 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
243 	nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
244 	nfsm_fhtom(np, 1);
245 	nfsm_request(np, NFSPROC_FSINFO, p, cred);
246 	nfsm_postop_attr(vp, retattr, 0);
247 	if (!error) {
248 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
249 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
250 		if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
251 		    pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
252 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
253 				~(NFS_FABLKSIZE - 1);
254 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
255 		if (max < nmp->nm_wsize && max > 0) {
256 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
257 			if (nmp->nm_wsize == 0)
258 				nmp->nm_wsize = max;
259 		}
260 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
261 		if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
262 		    pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
263 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
264 				~(NFS_FABLKSIZE - 1);
265 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
266 		if (max < nmp->nm_rsize && max > 0) {
267 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
268 			if (nmp->nm_rsize == 0)
269 				nmp->nm_rsize = max;
270 		}
271 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
272 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
273 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
274 				~(NFS_DIRFRAGSIZ - 1);
275 		if (max < nmp->nm_readdirsize && max > 0) {
276 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
277 			if (nmp->nm_readdirsize == 0)
278 				nmp->nm_readdirsize = max;
279 		}
280 		/* XXX */
281 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
282 		maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
283 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
284 			nmp->nm_maxfilesize = maxfsize;
285 		nmp->nm_mountp->mnt_fs_bshift =
286 		    ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
287 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
288 	}
289 	nfsm_reqdone;
290 	return (error);
291 }
292 #endif
293 
294 /*
295  * Mount a remote root fs via. NFS.  It goes like this:
296  * - Call nfs_boot_init() to fill in the nfs_diskless struct
297  * - build the rootfs mount point and call mountnfs() to do the rest.
298  */
299 int
300 nfs_mountroot()
301 {
302 	struct nfs_diskless *nd;
303 	struct vattr attr;
304 	struct mount *mp;
305 	struct vnode *vp;
306 	struct proc *procp;
307 	long n;
308 	int error;
309 
310 	procp = curproc; /* XXX */
311 
312 	if (root_device->dv_class != DV_IFNET)
313 		return (ENODEV);
314 
315 	/*
316 	 * XXX time must be non-zero when we init the interface or else
317 	 * the arp code will wedge.  [Fixed now in if_ether.c]
318 	 * However, the NFS attribute cache gives false "hits" when
319 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
320 	 */
321 	if (time.tv_sec < NFS_MAXATTRTIMO)
322 		time.tv_sec = NFS_MAXATTRTIMO;
323 
324 	/*
325 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
326 	 * Side effect:  Finds and configures a network interface.
327 	 */
328 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
329 	memset((caddr_t)nd, 0, sizeof(*nd));
330 	error = nfs_boot_init(nd, procp);
331 	if (error) {
332 		free(nd, M_NFSMNT);
333 		return (error);
334 	}
335 
336 	/*
337 	 * Create the root mount point.
338 	 */
339 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
340 	if (error)
341 		goto out;
342 	printf("root on %s\n", nd->nd_root.ndm_host);
343 
344 	/*
345 	 * Link it into the mount list.
346 	 */
347 	simple_lock(&mountlist_slock);
348 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
349 	simple_unlock(&mountlist_slock);
350 	rootvp = vp;
351 	mp->mnt_vnodecovered = NULLVP;
352 	vfs_unbusy(mp);
353 
354 	/* Get root attributes (for the time). */
355 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
356 	if (error)
357 		panic("nfs_mountroot: getattr for root");
358 	n = attr.va_atime.tv_sec;
359 #ifdef	DEBUG
360 	printf("root time: 0x%lx\n", n);
361 #endif
362 	setrootfstime(n);
363 
364 out:
365 	if (error)
366 		nfs_boot_cleanup(nd, procp);
367 	free(nd, M_NFSMNT);
368 	return (error);
369 }
370 
371 /*
372  * Internal version of mount system call for diskless setup.
373  * Separate function because we used to call it twice.
374  * (once for root and once for swap)
375  */
376 static int
377 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
378 	struct nfs_dlmount *ndmntp;
379 	const char *mntname;	/* mount point name */
380 	struct mount **mpp;
381 	struct vnode **vpp;
382 	struct proc *p;
383 {
384 	struct mount *mp;
385 	struct mbuf *m;
386 	int error;
387 
388 	vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
389 
390 	mp->mnt_op = &nfs_vfsops;
391 
392 	/*
393 	 * Historical practice expects NFS root file systems to
394 	 * be initially mounted r/w.
395 	 */
396 	mp->mnt_flag &= ~MNT_RDONLY;
397 
398 	/* Get mbuf for server sockaddr. */
399 	m = m_get(M_WAIT, MT_SONAME);
400 	if (m == NULL)
401 		panic("nfs_mountroot: mget soname for %s", mntname);
402 	MCLAIM(m, &nfs_mowner);
403 	memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
404 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
405 
406 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
407 			 ndmntp->ndm_args.hostname, vpp, p);
408 	if (error) {
409 		mp->mnt_op->vfs_refcount--;
410 		vfs_unbusy(mp);
411 		printf("nfs_mountroot: mount %s failed: %d\n",
412 		       mntname, error);
413 		free(mp, M_MOUNT);
414 	} else
415 		*mpp = mp;
416 
417 	return (error);
418 }
419 
420 void
421 nfs_decode_args(nmp, argp, p)
422 	struct nfsmount *nmp;
423 	struct nfs_args *argp;
424 	struct proc *p;
425 {
426 	int s;
427 	int adjsock;
428 	int maxio;
429 
430 	s = splsoftnet();
431 
432 	/*
433 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
434 	 * no sense in that context.
435 	 */
436 	if (argp->sotype == SOCK_STREAM)
437 		argp->flags &= ~NFSMNT_NOCONN;
438 
439 	/*
440 	 * Cookie translation is not needed for v2, silently ignore it.
441 	 */
442 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
443 	    NFSMNT_XLATECOOKIE)
444 		argp->flags &= ~NFSMNT_XLATECOOKIE;
445 
446 	/* Re-bind if rsrvd port requested and wasn't on one */
447 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
448 		  && (argp->flags & NFSMNT_RESVPORT);
449 	/* Also re-bind if we're switching to/from a connected UDP socket */
450 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
451 		    (argp->flags & NFSMNT_NOCONN));
452 
453 	/* Update flags. */
454 	nmp->nm_flag = argp->flags;
455 	splx(s);
456 
457 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
458 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
459 		if (nmp->nm_timeo < NFS_MINTIMEO)
460 			nmp->nm_timeo = NFS_MINTIMEO;
461 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
462 			nmp->nm_timeo = NFS_MAXTIMEO;
463 	}
464 
465 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
466 		nmp->nm_retry = argp->retrans;
467 		if (nmp->nm_retry > NFS_MAXREXMIT)
468 			nmp->nm_retry = NFS_MAXREXMIT;
469 	}
470 
471 #ifndef NFS_V2_ONLY
472 	if (argp->flags & NFSMNT_NFSV3) {
473 		if (argp->sotype == SOCK_DGRAM)
474 			maxio = NFS_MAXDGRAMDATA;
475 		else
476 			maxio = NFS_MAXDATA;
477 	} else
478 #endif
479 		maxio = NFS_V2MAXDATA;
480 
481 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
482 		int osize = nmp->nm_wsize;
483 		nmp->nm_wsize = argp->wsize;
484 		/* Round down to multiple of blocksize */
485 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
486 		if (nmp->nm_wsize <= 0)
487 			nmp->nm_wsize = NFS_FABLKSIZE;
488 		adjsock |= (nmp->nm_wsize != osize);
489 	}
490 	if (nmp->nm_wsize > maxio)
491 		nmp->nm_wsize = maxio;
492 	if (nmp->nm_wsize > MAXBSIZE)
493 		nmp->nm_wsize = MAXBSIZE;
494 
495 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
496 		int osize = nmp->nm_rsize;
497 		nmp->nm_rsize = argp->rsize;
498 		/* Round down to multiple of blocksize */
499 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
500 		if (nmp->nm_rsize <= 0)
501 			nmp->nm_rsize = NFS_FABLKSIZE;
502 		adjsock |= (nmp->nm_rsize != osize);
503 	}
504 	if (nmp->nm_rsize > maxio)
505 		nmp->nm_rsize = maxio;
506 	if (nmp->nm_rsize > MAXBSIZE)
507 		nmp->nm_rsize = MAXBSIZE;
508 
509 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
510 		nmp->nm_readdirsize = argp->readdirsize;
511 		/* Round down to multiple of minimum blocksize */
512 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
513 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
514 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
515 		/* Bigger than buffer size makes no sense */
516 		if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
517 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
518 	} else if (argp->flags & NFSMNT_RSIZE)
519 		nmp->nm_readdirsize = nmp->nm_rsize;
520 
521 	if (nmp->nm_readdirsize > maxio)
522 		nmp->nm_readdirsize = maxio;
523 
524 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
525 		argp->maxgrouplist <= NFS_MAXGRPS)
526 		nmp->nm_numgrps = argp->maxgrouplist;
527 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
528 		argp->readahead <= NFS_MAXRAHEAD)
529 		nmp->nm_readahead = argp->readahead;
530 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
531 		argp->leaseterm <= NQ_MAXLEASE)
532 		nmp->nm_leaseterm = argp->leaseterm;
533 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
534 		argp->deadthresh <= NQ_NEVERDEAD)
535 		nmp->nm_deadthresh = argp->deadthresh;
536 
537 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
538 		    (nmp->nm_soproto != argp->proto));
539 	nmp->nm_sotype = argp->sotype;
540 	nmp->nm_soproto = argp->proto;
541 
542 	if (nmp->nm_so && adjsock) {
543 		nfs_safedisconnect(nmp);
544 		if (nmp->nm_sotype == SOCK_DGRAM)
545 			while (nfs_connect(nmp, (struct nfsreq *)0, p)) {
546 				printf("nfs_args: retrying connect\n");
547 				(void) tsleep((caddr_t)&lbolt,
548 					      PSOCK, "nfscn3", 0);
549 			}
550 	}
551 }
552 
553 /*
554  * VFS Operations.
555  *
556  * mount system call
557  * It seems a bit dumb to copyinstr() the host and path here and then
558  * memcpy() them in mountnfs(), but I wanted to detect errors before
559  * doing the sockargs() call because sockargs() allocates an mbuf and
560  * an error after that means that I have to release the mbuf.
561  */
562 /* ARGSUSED */
563 int
564 nfs_mount(mp, path, data, ndp, p)
565 	struct mount *mp;
566 	const char *path;
567 	void *data;
568 	struct nameidata *ndp;
569 	struct proc *p;
570 {
571 	int error;
572 	struct nfs_args args;
573 	struct mbuf *nam;
574 	struct nfsmount *nmp = VFSTONFS(mp);
575 	struct sockaddr *sa;
576 	struct vnode *vp;
577 	char *pth, *hst;
578 	size_t len;
579 	u_char *nfh;
580 
581 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
582 	if (error)
583 		return (error);
584 
585 	if (mp->mnt_flag & MNT_GETARGS) {
586 
587 		if (nmp == NULL)
588 			return (EIO);
589 		if (args.addr != NULL) {
590 			sa = mtod(nmp->nm_nam, struct sockaddr *);
591 			error = copyout(sa, args.addr, sa->sa_len);
592 			if (error)
593 				return (error);
594 			args.addrlen = sa->sa_len;
595 		} else
596 			args.addrlen = 0;
597 
598 		args.version = NFS_ARGSVERSION;
599 		args.sotype = nmp->nm_sotype;
600 		args.proto = nmp->nm_soproto;
601 		args.fh = NULL;
602 		args.fhsize = 0;
603 		args.flags = nmp->nm_flag;
604 		args.wsize = nmp->nm_wsize;
605 		args.rsize = nmp->nm_rsize;
606 		args.readdirsize = nmp->nm_readdirsize;
607 		args.timeo = nmp->nm_timeo;
608 		args.retrans = nmp->nm_retry;
609 		args.maxgrouplist = nmp->nm_numgrps;
610 		args.readahead = nmp->nm_readahead;
611 		args.leaseterm = nmp->nm_leaseterm;
612 		args.deadthresh = nmp->nm_deadthresh;
613 		args.hostname = NULL;
614 		return (copyout(&args, data, sizeof(args)));
615 	}
616 
617 	if (args.version != NFS_ARGSVERSION)
618 		return (EPROGMISMATCH);
619 #ifdef NFS_V2_ONLY
620 	if (args.flags & NFSMNT_NQNFS)
621 		return (EPROGUNAVAIL);
622 	if (args.flags & NFSMNT_NFSV3)
623 		return (EPROGMISMATCH);
624 #endif
625 	if (mp->mnt_flag & MNT_UPDATE) {
626 		if (nmp == NULL)
627 			return (EIO);
628 		/*
629 		 * When doing an update, we can't change from or to
630 		 * v3 and/or nqnfs, or change cookie translation
631 		 */
632 		args.flags = (args.flags &
633 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
634 		    (nmp->nm_flag &
635 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
636 		nfs_decode_args(nmp, &args, p);
637 		return (0);
638 	}
639 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
640 		return (EINVAL);
641 	MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
642 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
643 	if (error)
644 		return (error);
645 	MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
646 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
647 	if (error)
648 		goto free_nfh;
649 	memset(&pth[len], 0, MNAMELEN - len);
650 	MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
651 	error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
652 	if (error)
653 		goto free_pth;
654 	memset(&hst[len], 0, MNAMELEN - len);
655 	/* sockargs() call must be after above copyin() calls */
656 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
657 	if (error)
658 		goto free_hst;
659 	MCLAIM(nam, &nfs_mowner);
660 	args.fh = nfh;
661 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
662 
663 free_hst:
664 	FREE(hst, M_TEMP);
665 free_pth:
666 	FREE(pth, M_TEMP);
667 free_nfh:
668 	FREE(nfh, M_TEMP);
669 
670 	return (error);
671 }
672 
673 /*
674  * Common code for mount and mountroot
675  */
676 int
677 mountnfs(argp, mp, nam, pth, hst, vpp, p)
678 	struct nfs_args *argp;
679 	struct mount *mp;
680 	struct mbuf *nam;
681 	const char *pth, *hst;
682 	struct vnode **vpp;
683 	struct proc *p;
684 {
685 	struct nfsmount *nmp;
686 	struct nfsnode *np;
687 	int error;
688 	struct vattr *attrs;
689 	struct ucred *cr;
690 
691 	/*
692 	 * If the number of nfs iothreads to use has never
693 	 * been set, create a reasonable number of them.
694 	 */
695 
696 	if (nfs_niothreads < 0) {
697 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
698 		nfs_getset_niothreads(TRUE);
699 	}
700 
701 	if (mp->mnt_flag & MNT_UPDATE) {
702 		nmp = VFSTONFS(mp);
703 		/* update paths, file handles, etc, here	XXX */
704 		m_freem(nam);
705 		return (0);
706 	} else {
707 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
708 		    M_NFSMNT, M_WAITOK);
709 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
710 		mp->mnt_data = nmp;
711 		TAILQ_INIT(&nmp->nm_uidlruhead);
712 		TAILQ_INIT(&nmp->nm_bufq);
713 		lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
714 		simple_lock_init(&nmp->nm_slock);
715 	}
716 	vfs_getnewfsid(mp);
717 	nmp->nm_mountp = mp;
718 
719 #ifndef NFS_V2_ONLY
720 	if (argp->flags & NFSMNT_NQNFS)
721 		mp->mnt_iflag |= IMNT_DTYPE;
722 #endif
723 
724 #ifndef NFS_V2_ONLY
725 	if ((argp->flags & NFSMNT_NFSV3) == 0)
726 #endif
727 		/*
728 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
729 		 * will fill this in.
730 		 */
731 		nmp->nm_maxfilesize = 0xffffffffLL;
732 
733 	nmp->nm_timeo = NFS_TIMEO;
734 	nmp->nm_retry = NFS_RETRANS;
735 	nmp->nm_wsize = NFS_WSIZE;
736 	nmp->nm_rsize = NFS_RSIZE;
737 	nmp->nm_readdirsize = NFS_READDIRSIZE;
738 	nmp->nm_numgrps = NFS_MAXGRPS;
739 	nmp->nm_readahead = NFS_DEFRAHEAD;
740 	nmp->nm_leaseterm = NQ_DEFLEASE;
741 	nmp->nm_deadthresh = NQ_DEADTHRESH;
742 	CIRCLEQ_INIT(&nmp->nm_timerhead);
743 	nmp->nm_inprog = NULLVP;
744 	error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p);
745 	if (error)
746 		goto bad;
747 	nmp->nm_nam = nam;
748 
749 	/* Set up the sockets and per-host congestion */
750 	nmp->nm_sotype = argp->sotype;
751 	nmp->nm_soproto = argp->proto;
752 
753 	nfs_decode_args(nmp, argp, p);
754 
755 	mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
756 	mp->mnt_dev_bshift = DEV_BSHIFT;
757 
758 	/*
759 	 * For Connection based sockets (TCP,...) defer the connect until
760 	 * the first request, in case the server is not responding.
761 	 */
762 	if (nmp->nm_sotype == SOCK_DGRAM &&
763 		(error = nfs_connect(nmp, (struct nfsreq *)0, p)))
764 		goto bad;
765 
766 	/*
767 	 * This is silly, but it has to be set so that vinifod() works.
768 	 * We do not want to do an nfs_statvfs() here since we can get
769 	 * stuck on a dead server and we are holding a lock on the mount
770 	 * point.
771 	 */
772 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
773 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
774 	if (error)
775 		goto bad;
776 	*vpp = NFSTOV(np);
777 	MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
778 	VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
779 	if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
780 		cr = crget();
781 		cr->cr_uid = attrs->va_uid;
782 		cr->cr_gid = attrs->va_gid;
783 		cr->cr_ngroups = 0;
784 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
785 		crfree(cr);
786 	}
787 	FREE(attrs, M_TEMP);
788 
789 	/*
790 	 * A reference count is needed on the nfsnode representing the
791 	 * remote root.  If this object is not persistent, then backward
792 	 * traversals of the mount point (i.e. "..") will not work if
793 	 * the nfsnode gets flushed out of the cache. Ufs does not have
794 	 * this problem, because one can identify root inodes by their
795 	 * number == ROOTINO (2). So, just unlock, but no rele.
796 	 */
797 
798 	nmp->nm_vnode = *vpp;
799 	VOP_UNLOCK(*vpp, 0);
800 
801 	return (0);
802 bad:
803 	nfs_disconnect(nmp);
804 	free((caddr_t)nmp, M_NFSMNT);
805 	m_freem(nam);
806 	return (error);
807 }
808 
809 /*
810  * unmount system call
811  */
812 int
813 nfs_unmount(mp, mntflags, p)
814 	struct mount *mp;
815 	int mntflags;
816 	struct proc *p;
817 {
818 	struct nfsmount *nmp;
819 	struct vnode *vp;
820 	int error, flags = 0;
821 
822 	if (mntflags & MNT_FORCE)
823 		flags |= FORCECLOSE;
824 	nmp = VFSTONFS(mp);
825 	/*
826 	 * Goes something like this..
827 	 * - Check for activity on the root vnode (other than ourselves).
828 	 * - Call vflush() to clear out vnodes for this file system,
829 	 *   except for the root vnode.
830 	 * - Decrement reference on the vnode representing remote root.
831 	 * - Close the socket
832 	 * - Free up the data structures
833 	 */
834 	/*
835 	 * We need to decrement the ref. count on the nfsnode representing
836 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
837 	 * has done vput on this vnode, otherwise we would get deadlock!
838 	 */
839 	vp = nmp->nm_vnode;
840 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
841 	if (error != 0)
842 		return error;
843 
844 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
845 		vput(vp);
846 		return (EBUSY);
847 	}
848 
849 	/*
850 	 * Must handshake with nqnfs_clientd() if it is active.
851 	 */
852 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
853 	while (nmp->nm_inprog != NULLVP)
854 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
855 	error = vflush(mp, vp, flags);
856 	if (error) {
857 		vput(vp);
858 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
859 		return (error);
860 	}
861 
862 	/*
863 	 * We are now committed to the unmount; mark the mount structure
864 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
865 	 * will go away cleanly.
866 	 */
867 	nmp->nm_iflag |= NFSMNT_DISMNT;
868 
869 	/*
870 	 * There are two reference counts to get rid of here
871 	 * (see comment in mountnfs()).
872 	 */
873 	vrele(vp);
874 	vput(vp);
875 	vgone(vp);
876 	nfs_disconnect(nmp);
877 	m_freem(nmp->nm_nam);
878 
879 	/*
880 	 * For NQNFS, let the server daemon free the nfsmount structure.
881 	 */
882 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
883 		free((caddr_t)nmp, M_NFSMNT);
884 	return (0);
885 }
886 
887 /*
888  * Return root of a filesystem
889  */
890 int
891 nfs_root(mp, vpp)
892 	struct mount *mp;
893 	struct vnode **vpp;
894 {
895 	struct vnode *vp;
896 	struct nfsmount *nmp;
897 	int error;
898 
899 	nmp = VFSTONFS(mp);
900 	vp = nmp->nm_vnode;
901 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
902 	if (error != 0)
903 		return error;
904 	if (vp->v_type == VNON)
905 		vp->v_type = VDIR;
906 	vp->v_flag = VROOT;
907 	*vpp = vp;
908 	return (0);
909 }
910 
911 extern int syncprt;
912 
913 /*
914  * Flush out the buffer cache
915  */
916 /* ARGSUSED */
917 int
918 nfs_sync(mp, waitfor, cred, p)
919 	struct mount *mp;
920 	int waitfor;
921 	struct ucred *cred;
922 	struct proc *p;
923 {
924 	struct vnode *vp;
925 	int error, allerror = 0;
926 
927 	/*
928 	 * Force stale buffer cache information to be flushed.
929 	 */
930 loop:
931 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
932 		/*
933 		 * If the vnode that we are about to sync is no longer
934 		 * associated with this mount point, start over.
935 		 */
936 		if (vp->v_mount != mp)
937 			goto loop;
938 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
939 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
940 		     vp->v_uobj.uo_npages == 0))
941 			continue;
942 		if (vget(vp, LK_EXCLUSIVE))
943 			goto loop;
944 		error = VOP_FSYNC(vp, cred,
945 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
946 		if (error)
947 			allerror = error;
948 		vput(vp);
949 	}
950 	return (allerror);
951 }
952 
953 /*
954  * NFS flat namespace lookup.
955  * Currently unsupported.
956  */
957 /* ARGSUSED */
958 int
959 nfs_vget(mp, ino, vpp)
960 	struct mount *mp;
961 	ino_t ino;
962 	struct vnode **vpp;
963 {
964 
965 	return (EOPNOTSUPP);
966 }
967 
968 /*
969  * Do that sysctl thang...
970  */
971 static int
972 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
973 {
974 	int error;
975 
976 	nfs_getset_niothreads(0);
977         error = sysctl_lookup(SYSCTLFN_CALL(rnode));
978 	if (error || newp == NULL)
979 		return (error);
980 	nfs_getset_niothreads(1);
981 
982 	return (0);
983 }
984 
985 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
986 {
987 
988 	sysctl_createv(clog, 0, NULL, NULL,
989 		       CTLFLAG_PERMANENT,
990 		       CTLTYPE_NODE, "vfs", NULL,
991 		       NULL, 0, NULL, 0,
992 		       CTL_VFS, CTL_EOL);
993 	sysctl_createv(clog, 0, NULL, NULL,
994 		       CTLFLAG_PERMANENT,
995 		       CTLTYPE_NODE, "nfs",
996 		       SYSCTL_DESCR("NFS vfs options"),
997 		       NULL, 0, NULL, 0,
998 		       CTL_VFS, 2, CTL_EOL);
999 	/*
1000 	 * XXX the "2" above could be dynamic, thereby eliminating one
1001 	 * more instance of the "number to vfs" mapping problem, but
1002 	 * "2" is the order as taken from sys/mount.h
1003 	 */
1004 
1005 	sysctl_createv(clog, 0, NULL, NULL,
1006 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1007 		       CTLTYPE_STRUCT, "nfsstats",
1008 		       SYSCTL_DESCR("NFS operation statistics"),
1009 		       NULL, 0, &nfsstats, sizeof(nfsstats),
1010 		       CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1011 	sysctl_createv(clog, 0, NULL, NULL,
1012 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1013 		       CTLTYPE_INT, "iothreads",
1014 		       SYSCTL_DESCR("Number of NFS client processes desired"),
1015 		       sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1016 		       CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1017 }
1018 
1019 /*
1020  * At this point, this should never happen
1021  */
1022 /* ARGSUSED */
1023 int
1024 nfs_fhtovp(mp, fhp, vpp)
1025 	struct mount *mp;
1026 	struct fid *fhp;
1027 	struct vnode **vpp;
1028 {
1029 
1030 	return (EINVAL);
1031 }
1032 
1033 /* ARGSUSED */
1034 int
1035 nfs_checkexp(mp, nam, exflagsp, credanonp)
1036 	struct mount *mp;
1037 	struct mbuf *nam;
1038 	int *exflagsp;
1039 	struct ucred **credanonp;
1040 {
1041 
1042 	return (EINVAL);
1043 }
1044 
1045 /*
1046  * Vnode pointer to File handle, should never happen either
1047  */
1048 /* ARGSUSED */
1049 int
1050 nfs_vptofh(vp, fhp)
1051 	struct vnode *vp;
1052 	struct fid *fhp;
1053 {
1054 
1055 	return (EINVAL);
1056 }
1057 
1058 /*
1059  * Vfs start routine, a no-op.
1060  */
1061 /* ARGSUSED */
1062 int
1063 nfs_start(mp, flags, p)
1064 	struct mount *mp;
1065 	int flags;
1066 	struct proc *p;
1067 {
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * Do operations associated with quotas, not supported
1074  */
1075 /* ARGSUSED */
1076 int
1077 nfs_quotactl(mp, cmd, uid, arg, p)
1078 	struct mount *mp;
1079 	int cmd;
1080 	uid_t uid;
1081 	void *arg;
1082 	struct proc *p;
1083 {
1084 
1085 	return (EOPNOTSUPP);
1086 }
1087