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