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