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