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