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