xref: /openbsd-src/sys/nfs/nfs_vfsops.c (revision aa997e528a848ca5596493c2a801bdd6fb26ae61)
1 /*	$OpenBSD: nfs_vfsops.c,v 1.116 2018/02/10 05:24:23 deraadt Exp $	*/
2 /*	$NetBSD: nfs_vfsops.c,v 1.46.4.1 1996/05/25 22:40:35 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/ioctl.h>
41 #include <sys/signal.h>
42 #include <sys/proc.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/lock.h>
46 #include <sys/kernel.h>
47 #include <sys/mount.h>
48 #include <sys/swap.h>
49 #include <sys/buf.h>
50 #include <sys/mbuf.h>
51 #include <sys/dirent.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.h>
55 #include <sys/sysctl.h>
56 #include <sys/queue.h>
57 
58 #include <netinet/in.h>
59 
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfs/nfsnode.h>
63 #include <nfs/nfs.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/xdr_subs.h>
66 #include <nfs/nfsm_subs.h>
67 #include <nfs/nfsdiskless.h>
68 #include <nfs/nfs_var.h>
69 
70 extern struct nfsstats nfsstats;
71 extern int nfs_ticks;
72 extern u_int32_t nfs_procids[NFS_NPROCS];
73 
74 int		nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
75 int		nfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
76 struct mount	*nfs_mount_diskless(struct nfs_dlmount *, char *, int);
77 int	mountnfs(struct nfs_args *, struct mount *, struct mbuf *,
78 	    const char *, char *);
79 int	nfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *);
80 int	nfs_root(struct mount *, struct vnode **);
81 int	nfs_start(struct mount *, int, struct proc *);
82 int	nfs_statfs(struct mount *, struct statfs *, struct proc *);
83 int	nfs_sync(struct mount *, int, int, struct ucred *, struct proc *);
84 int	nfs_unmount(struct mount *, int, struct proc *);
85 int	nfs_vget(struct mount *, ino_t, struct vnode **);
86 int	nfs_vptofh(struct vnode *, struct fid *);
87 int	nfs_mountroot(void);
88 void	nfs_decode_args(struct nfsmount *, struct nfs_args *,
89 	    struct nfs_args *);
90 int	nfs_fhtovp(struct mount *, struct fid *, struct vnode **);
91 
92 /*
93  * nfs vfs operations.
94  */
95 const struct vfsops nfs_vfsops = {
96 	nfs_mount,
97 	nfs_start,
98 	nfs_unmount,
99 	nfs_root,
100 	nfs_quotactl,
101 	nfs_statfs,
102 	nfs_sync,
103 	nfs_vget,
104 	nfs_fhtovp,
105 	nfs_vptofh,
106 	nfs_vfs_init,
107 	nfs_sysctl,
108 	nfs_checkexp
109 };
110 
111 /*
112  * nfs statfs call
113  */
114 int
115 nfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
116 {
117 	struct vnode *vp;
118 	struct nfs_statfs *sfp = NULL;
119 	struct nfsm_info	info;
120 	u_int32_t *tl;
121 	int32_t t1;
122 	caddr_t cp2;
123 	struct nfsmount *nmp = VFSTONFS(mp);
124 	int error = 0, retattr;
125 	struct ucred *cred;
126 	struct nfsnode *np;
127 	u_quad_t tquad;
128 
129 	info.nmi_v3 = (nmp->nm_flag & NFSMNT_NFSV3);
130 
131 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
132 	if (error)
133 		return (error);
134 	vp = NFSTOV(np);
135 	cred = crget();
136 	cred->cr_ngroups = 0;
137 	if (info.nmi_v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
138 		(void)nfs_fsinfo(nmp, vp, cred, p);
139 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
140 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(info.nmi_v3));
141 	nfsm_fhtom(&info, vp, info.nmi_v3);
142 
143 	info.nmi_procp = p;
144 	info.nmi_cred = cred;
145 	error = nfs_request(vp, NFSPROC_FSSTAT, &info);
146 	if (info.nmi_v3)
147 		nfsm_postop_attr(vp, retattr);
148 	if (error) {
149 		m_freem(info.nmi_mrep);
150 		goto nfsmout;
151 	}
152 
153 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(info.nmi_v3));
154 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
155 	if (info.nmi_v3) {
156 		sbp->f_bsize = NFS_FABLKSIZE;
157 		tquad = fxdr_hyper(&sfp->sf_tbytes);
158 		sbp->f_blocks = tquad / (u_quad_t)NFS_FABLKSIZE;
159 		tquad = fxdr_hyper(&sfp->sf_fbytes);
160 		sbp->f_bfree = tquad / (u_quad_t)NFS_FABLKSIZE;
161 		tquad = fxdr_hyper(&sfp->sf_abytes);
162 		sbp->f_bavail = (quad_t)tquad / (quad_t)NFS_FABLKSIZE;
163 
164 		tquad = fxdr_hyper(&sfp->sf_tfiles);
165 		sbp->f_files = tquad;
166 		tquad = fxdr_hyper(&sfp->sf_ffiles);
167 		sbp->f_ffree = tquad;
168 		sbp->f_favail = tquad;
169 	} else {
170 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
171 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
172 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
173 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
174 		sbp->f_files = 0;
175 		sbp->f_ffree = 0;
176 		sbp->f_favail = 0;
177 	}
178 	copy_statfs_info(sbp, mp);
179 	m_freem(info.nmi_mrep);
180 nfsmout:
181 	vrele(vp);
182 	crfree(cred);
183 	return (error);
184 }
185 
186 /*
187  * nfs version 3 fsinfo rpc call
188  */
189 int
190 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
191     struct proc *p)
192 {
193 	struct nfsv3_fsinfo *fsp;
194 	struct nfsm_info	info;
195 	int32_t t1;
196 	u_int32_t *tl, pref, max;
197 	caddr_t cp2;
198 	int error = 0, retattr;
199 
200 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
201 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(1));
202 	nfsm_fhtom(&info, vp, 1);
203 
204 	info.nmi_procp = p;
205 	info.nmi_cred = cred;
206 	error = nfs_request(vp, NFSPROC_FSINFO, &info);
207 
208 	nfsm_postop_attr(vp, retattr);
209 	if (error) {
210 		m_freem(info.nmi_mrep);
211 		goto nfsmout;
212 	}
213 
214 	nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
215 	pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
216 	if (pref < nmp->nm_wsize)
217 		nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
218 			~(NFS_FABLKSIZE - 1);
219 	max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
220 	if (max < nmp->nm_wsize) {
221 		nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
222 		if (nmp->nm_wsize == 0)
223 			nmp->nm_wsize = max;
224 	}
225 	pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
226 	if (pref < nmp->nm_rsize)
227 		nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
228 			~(NFS_FABLKSIZE - 1);
229 	max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
230 	if (max < nmp->nm_rsize) {
231 		nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
232 		if (nmp->nm_rsize == 0)
233 			nmp->nm_rsize = max;
234 	}
235 	pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
236 	if (pref < nmp->nm_readdirsize)
237 		nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
238 			~(NFS_DIRBLKSIZ - 1);
239 	if (max < nmp->nm_readdirsize) {
240 		nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
241 		if (nmp->nm_readdirsize == 0)
242 			nmp->nm_readdirsize = max;
243 	}
244 	nmp->nm_flag |= NFSMNT_GOTFSINFO;
245 
246 	m_freem(info.nmi_mrep);
247 nfsmout:
248 	return (error);
249 }
250 
251 struct nfs_diskless nfs_diskless;
252 
253 /*
254  * Mount a remote root fs via. NFS.  It goes like this:
255  * - Call nfs_boot_init() to fill in the nfs_diskless struct
256  *   (using RARP, bootparam RPC, mountd RPC)
257  * - hand craft the swap nfs vnode hanging off a fake mount point
258  *	if swdevt[0].sw_dev == NODEV
259  * - build the rootfs mount point and call mountnfs() to do the rest.
260  */
261 int
262 nfs_mountroot(void)
263 {
264 	struct vattr attr;
265 	struct mount *mp;
266 	struct vnode *vp;
267 	struct proc *procp;
268 	long n;
269 	int error;
270 
271 	procp = curproc; /* XXX */
272 
273 	/*
274 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
275 	 * Side effect:	 Finds and configures a network interface.
276 	 */
277 	nfs_boot_init(&nfs_diskless, procp);
278 
279 	/*
280 	 * Create the root mount point.
281 	 */
282 	if (nfs_boot_getfh(&nfs_diskless.nd_boot, "root", &nfs_diskless.nd_root, -1))
283 		panic("nfs_mountroot: root");
284 	mp = nfs_mount_diskless(&nfs_diskless.nd_root, "/", 0);
285 	nfs_root(mp, &rootvp);
286 	printf("root on %s\n", nfs_diskless.nd_root.ndm_host);
287 
288 	/*
289 	 * Link it into the mount list.
290 	 */
291 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
292 	vfs_unbusy(mp);
293 
294 	/* Get root attributes (for the time). */
295 	error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp);
296 	if (error) panic("nfs_mountroot: getattr for root");
297 	n = attr.va_atime.tv_sec;
298 #ifdef	DEBUG
299 	printf("root time: 0x%lx\n", n);
300 #endif
301 	inittodr(n);
302 
303 #ifdef notyet
304 	/* Set up swap credentials. */
305 	proc0.p_ucred->cr_uid = ntohl(nfs_diskless.swap_ucred.cr_uid);
306 	proc0.p_ucred->cr_gid = ntohl(nfs_diskless.swap_ucred.cr_gid);
307 	if ((proc0.p_ucred->cr_ngroups = ntohs(nfs_diskless.swap_ucred.cr_ngroups)) >
308 		NGROUPS_MAX)
309 		proc0.p_ucred->cr_ngroups = NGROUPS_MAX;
310 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
311 	    proc0.p_ucred->cr_groups[i] = ntohl(nfs_diskless.swap_ucred.cr_groups[i]);
312 #endif
313 
314 	/*
315 	 * "Mount" the swap device.
316 	 *
317 	 * On a "dataless" configuration (swap on disk) we will have:
318 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
319 	 */
320 	if (swdevt[0].sw_dev != NODEV) {
321 		if (bdevvp(swapdev, &swapdev_vp))
322 			panic("nfs_mountroot: can't setup swap vp");
323 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
324 		return (0);
325 	}
326 
327 	/*
328 	 * If swapping to an nfs node:	(swdevt[0].sw_dev == NODEV)
329 	 * Create a fake mount point just for the swap vnode so that the
330 	 * swap file can be on a different server from the rootfs.
331 	 *
332 	 * Wait 5 retries, finally no swap is cool. -mickey
333 	 */
334 	error = nfs_boot_getfh(&nfs_diskless.nd_boot, "swap", &nfs_diskless.nd_swap, 5);
335 	if (!error) {
336 		mp = nfs_mount_diskless(&nfs_diskless.nd_swap, "/swap", 0);
337 		nfs_root(mp, &vp);
338 		vfs_unbusy(mp);
339 
340 		/*
341 		 * Since the swap file is not the root dir of a file system,
342 		 * hack it to a regular file.
343 		 */
344 		vp->v_type = VREG;
345 		vp->v_flag = 0;
346 
347 		/*
348 		 * Next line is a hack to make swapmount() work on NFS
349 		 * swap files.
350 		 */
351 		swdevt[0].sw_dev = NETDEV;
352 		/* end hack */
353 		nfs_diskless.sw_vp = vp;
354 
355 		/*
356 		 * Find out how large the swap file is.
357 		 */
358 		error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
359 		if (error)
360 			printf("nfs_mountroot: getattr for swap\n");
361 		n = (long) (attr.va_size >> DEV_BSHIFT);
362 
363 		printf("swap on %s\n", nfs_diskless.nd_swap.ndm_host);
364 #ifdef	DEBUG
365 		printf("swap size: 0x%lx (blocks)\n", n);
366 #endif
367 		return (0);
368 	}
369 
370 	printf("WARNING: no swap\n");
371 	swdevt[0].sw_dev = NODEV;
372 	return (0);
373 }
374 
375 /*
376  * Internal version of mount system call for diskless setup.
377  */
378 struct mount *
379 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag)
380 {
381 	struct mount *mp;
382 	struct mbuf *m;
383 	int error;
384 
385 	if (vfs_rootmountalloc("nfs", mntname, &mp))
386 		panic("nfs_mount_diskless: vfs_rootmountalloc failed");
387 	mp->mnt_flag |= mntflag;
388 
389 	/* Get mbuf for server sockaddr. */
390 	m = m_get(M_WAIT, MT_SONAME);
391 	bcopy(ndmntp->ndm_args.addr, mtod(m, caddr_t),
392 	    (m->m_len = ndmntp->ndm_args.addr->sa_len));
393 
394 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
395 	    ndmntp->ndm_args.hostname);
396 	if (error)
397 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
398 
399 	return (mp);
400 }
401 
402 void
403 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp,
404     struct nfs_args *nargp)
405 {
406 	int adjsock = 0;
407 	int maxio;
408 
409 #if 0
410 	/* Re-bind if rsrvd port requested and wasn't on one */
411 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
412 		  && (argp->flags & NFSMNT_RESVPORT);
413 #endif
414 	/* Also re-bind if we're switching to/from a connected UDP socket */
415 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
416 	    (argp->flags & NFSMNT_NOCONN));
417 
418 	nmp->nm_flag =
419 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
420 
421 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
422 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
423 		if (nmp->nm_timeo < NFS_MINTIMEO)
424 			nmp->nm_timeo = NFS_MINTIMEO;
425 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
426 			nmp->nm_timeo = NFS_MAXTIMEO;
427 	}
428 
429 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1)
430 		nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT);
431 	if (!(nmp->nm_flag & NFSMNT_SOFT))
432 		nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */
433 
434 	if (argp->flags & NFSMNT_NFSV3) {
435 		if (argp->sotype == SOCK_DGRAM)
436 			maxio = NFS_MAXDGRAMDATA;
437 		else
438 			maxio = NFS_MAXDATA;
439 	} else
440 		maxio = NFS_V2MAXDATA;
441 
442 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
443 		int osize = nmp->nm_wsize;
444 		nmp->nm_wsize = argp->wsize;
445 		/* Round down to multiple of blocksize */
446 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
447 		if (nmp->nm_wsize <= 0)
448 			nmp->nm_wsize = NFS_FABLKSIZE;
449 		adjsock |= (nmp->nm_wsize != osize);
450 	}
451 	if (nmp->nm_wsize > maxio)
452 		nmp->nm_wsize = maxio;
453 	if (nmp->nm_wsize > MAXBSIZE)
454 		nmp->nm_wsize = MAXBSIZE;
455 
456 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
457 		int osize = nmp->nm_rsize;
458 		nmp->nm_rsize = argp->rsize;
459 		/* Round down to multiple of blocksize */
460 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
461 		if (nmp->nm_rsize <= 0)
462 			nmp->nm_rsize = NFS_FABLKSIZE;
463 		adjsock |= (nmp->nm_rsize != osize);
464 	}
465 	if (nmp->nm_rsize > maxio)
466 		nmp->nm_rsize = maxio;
467 	if (nmp->nm_rsize > MAXBSIZE)
468 		nmp->nm_rsize = MAXBSIZE;
469 
470 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
471 		nmp->nm_readdirsize = argp->readdirsize;
472 		/* Round down to multiple of blocksize */
473 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
474 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
475 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
476 	} else if (argp->flags & NFSMNT_RSIZE)
477 		nmp->nm_readdirsize = nmp->nm_rsize;
478 
479 	if (nmp->nm_readdirsize > maxio)
480 		nmp->nm_readdirsize = maxio;
481 
482 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
483 		argp->maxgrouplist <= NFS_MAXGRPS)
484 		nmp->nm_numgrps = argp->maxgrouplist;
485 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
486 		argp->readahead <= NFS_MAXRAHEAD)
487 		nmp->nm_readahead = argp->readahead;
488 	if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) {
489 		if (argp->acregmin > 0xffff)
490 			nmp->nm_acregmin = 0xffff;
491 		else
492 			nmp->nm_acregmin = argp->acregmin;
493 	}
494 	if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) {
495 		if (argp->acregmax > 0xffff)
496 			nmp->nm_acregmax = 0xffff;
497 		else
498 			nmp->nm_acregmax = argp->acregmax;
499 	}
500 	if (nmp->nm_acregmin > nmp->nm_acregmax)
501 	  nmp->nm_acregmin = nmp->nm_acregmax;
502 
503 	if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) {
504 		if (argp->acdirmin > 0xffff)
505 			nmp->nm_acdirmin = 0xffff;
506 		else
507 			nmp->nm_acdirmin = argp->acdirmin;
508 	}
509 	if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) {
510 		if (argp->acdirmax > 0xffff)
511 			nmp->nm_acdirmax = 0xffff;
512 		else
513 			nmp->nm_acdirmax = argp->acdirmax;
514 	}
515 	if (nmp->nm_acdirmin > nmp->nm_acdirmax)
516 	  nmp->nm_acdirmin = nmp->nm_acdirmax;
517 
518 	if (nmp->nm_so && adjsock) {
519 		nfs_disconnect(nmp);
520 		if (nmp->nm_sotype == SOCK_DGRAM)
521 			while (nfs_connect(nmp, NULL)) {
522 				printf("nfs_args: retrying connect\n");
523 				(void) tsleep(&lbolt,
524 					      PSOCK, "nfscon", 0);
525 			}
526 	}
527 
528 	/* Update nargp based on nmp */
529 	nargp->wsize = nmp->nm_wsize;
530 	nargp->rsize = nmp->nm_rsize;
531 	nargp->readdirsize = nmp->nm_readdirsize;
532 	nargp->timeo = nmp->nm_timeo;
533 	nargp->retrans = nmp->nm_retry;
534 	nargp->maxgrouplist = nmp->nm_numgrps;
535 	nargp->readahead = nmp->nm_readahead;
536 	nargp->acregmin = nmp->nm_acregmin;
537 	nargp->acregmax = nmp->nm_acregmax;
538 	nargp->acdirmin = nmp->nm_acdirmin;
539 	nargp->acdirmax = nmp->nm_acdirmax;
540 }
541 
542 /*
543  * VFS Operations.
544  *
545  * mount system call
546  * It seems a bit dumb to copyinstr() the host here and then
547  * bcopy() it in mountnfs(), but I wanted to detect errors before
548  * doing the sockargs() call because sockargs() allocates an mbuf and
549  * an error after that means that I have to release the mbuf.
550  */
551 /* ARGSUSED */
552 int
553 nfs_mount(struct mount *mp, const char *path, void *data,
554     struct nameidata *ndp, struct proc *p)
555 {
556 	int error;
557 	struct nfs_args *args = data;
558 	struct mbuf *nam;
559 	char hst[MNAMELEN];
560 	size_t len;
561 	u_char nfh[NFSX_V3FHMAX];
562 
563 	if (args &&
564 	    (args->flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS)
565 		return (EINVAL);
566 
567 	if (nfs_niothreads < 0) {
568 		nfs_niothreads = 4;
569 		nfs_getset_niothreads(1);
570 	}
571 
572 	if (mp->mnt_flag & MNT_UPDATE) {
573 		struct nfsmount *nmp = VFSTONFS(mp);
574 
575 		if (nmp == NULL)
576 			return (EIO);
577 		/*
578 		 * When doing an update, we can't change from or to
579 		 * v3.
580 		 */
581 		if (args) {
582 			args->flags = (args->flags & ~(NFSMNT_NFSV3)) |
583 			    (nmp->nm_flag & (NFSMNT_NFSV3));
584 			nfs_decode_args(nmp, args, &mp->mnt_stat.mount_info.nfs_args);
585 		}
586 		return (0);
587 	}
588 	if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
589 		return (EINVAL);
590 	error = copyin(args->fh, nfh, args->fhsize);
591 	if (error)
592 		return (error);
593 	error = copyinstr(args->hostname, hst, MNAMELEN-1, &len);
594 	if (error)
595 		return (error);
596 	memset(&hst[len], 0, MNAMELEN - len);
597 	/* sockargs() call must be after above copyin() calls */
598 	error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
599 	if (error)
600 		return (error);
601 	args->fh = nfh;
602 	error = mountnfs(args, mp, nam, path, hst);
603 	return (error);
604 }
605 
606 /*
607  * Common code for mount and mountroot
608  */
609 int
610 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam,
611     const char *pth, char *hst)
612 {
613 	struct nfsmount *nmp;
614 	int error;
615 
616 	if (mp->mnt_flag & MNT_UPDATE) {
617 		nmp = VFSTONFS(mp);
618 		/* update paths, file handles, etc, here	XXX */
619 		m_freem(nam);
620 		return (0);
621 	} else {
622 		nmp = malloc(sizeof(*nmp), M_NFSMNT,
623 		    M_WAITOK|M_ZERO);
624 		mp->mnt_data = nmp;
625 	}
626 
627 	vfs_getnewfsid(mp);
628 	nmp->nm_mountp = mp;
629 	nmp->nm_timeo = NFS_TIMEO;
630 	nmp->nm_retry = NFS_RETRANS;
631 	nmp->nm_wsize = NFS_WSIZE;
632 	nmp->nm_rsize = NFS_RSIZE;
633 	nmp->nm_readdirsize = NFS_READDIRSIZE;
634 	nmp->nm_numgrps = NFS_MAXGRPS;
635 	nmp->nm_readahead = NFS_DEFRAHEAD;
636 	nmp->nm_fhsize = argp->fhsize;
637 	nmp->nm_acregmin = NFS_MINATTRTIMO;
638 	nmp->nm_acregmax = NFS_MAXATTRTIMO;
639 	nmp->nm_acdirmin = NFS_MINATTRTIMO;
640 	nmp->nm_acdirmax = NFS_MAXATTRTIMO;
641 	bcopy(argp->fh, nmp->nm_fh, argp->fhsize);
642 	mp->mnt_stat.f_namemax = MAXNAMLEN;
643 	memset(mp->mnt_stat.f_mntonname, 0, MNAMELEN);
644 	strlcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
645 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
646 	strlcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
647 	memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
648 	strlcpy(mp->mnt_stat.f_mntfromspec, hst, MNAMELEN);
649 	bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp));
650 	nmp->nm_nam = nam;
651 	nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args);
652 
653 	nfs_ninit(nmp);
654 	TAILQ_INIT(&nmp->nm_reqsq);
655 	timeout_set_proc(&nmp->nm_rtimeout, nfs_timer, nmp);
656 
657 	/* Set up the sockets and per-host congestion */
658 	nmp->nm_sotype = argp->sotype;
659 	nmp->nm_soproto = argp->proto;
660 
661 	/*
662 	 * For Connection based sockets (TCP,...) defer the connect until
663 	 * the first request, in case the server is not responding.
664 	 */
665 	if (nmp->nm_sotype == SOCK_DGRAM &&
666 	    (error = nfs_connect(nmp, NULL)))
667 		goto bad;
668 
669 	/*
670 	 * This is silly, but it has to be set so that vinifod() works.
671 	 * We do not want to do an nfs_statfs() here since we can get
672 	 * stuck on a dead server and we are holding a lock on the mount
673 	 * point.
674 	 */
675 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
676 
677 	return (0);
678 bad:
679 	nfs_disconnect(nmp);
680 	free(nmp, M_NFSMNT, sizeof(*nmp));
681 	m_freem(nam);
682 	return (error);
683 }
684 
685 /* unmount system call */
686 int
687 nfs_unmount(struct mount *mp, int mntflags, struct proc *p)
688 {
689 	struct nfsmount *nmp;
690 	int error, flags;
691 
692 	nmp = VFSTONFS(mp);
693 	flags = 0;
694 
695 	if (mntflags & MNT_FORCE)
696 		flags |= FORCECLOSE;
697 
698 	error = vflush(mp, NULL, flags);
699 	if (error)
700 		return (error);
701 
702 	nfs_disconnect(nmp);
703 	m_freem(nmp->nm_nam);
704 	timeout_del(&nmp->nm_rtimeout);
705 	free(nmp, M_NFSMNT, sizeof(*nmp));
706 	mp->mnt_data = NULL;
707 	return (0);
708 }
709 
710 /*
711  * Return root of a filesystem
712  */
713 int
714 nfs_root(struct mount *mp, struct vnode **vpp)
715 {
716 	struct nfsmount *nmp;
717 	struct nfsnode *np;
718 	int error;
719 
720 	nmp = VFSTONFS(mp);
721 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
722 	if (error)
723 		return (error);
724 	*vpp = NFSTOV(np);
725 	return (0);
726 }
727 
728 /*
729  * Flush out the buffer cache
730  */
731 int
732 nfs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, struct proc *p)
733 {
734 	struct vnode *vp;
735 	int error, allerror = 0;
736 
737 	/*
738 	 * Don't traverse the vnode list if we want to skip all of them.
739 	 */
740 	if (waitfor == MNT_LAZY)
741 		return (allerror);
742 
743 	/*
744 	 * Force stale buffer cache information to be flushed.
745 	 */
746 loop:
747 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
748 		/*
749 		 * If the vnode that we are about to sync is no longer
750 		 * associated with this mount point, start over.
751 		 */
752 		if (vp->v_mount != mp)
753 			goto loop;
754 		if (VOP_ISLOCKED(vp) || LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
755 			continue;
756 		if (vget(vp, LK_EXCLUSIVE, p))
757 			goto loop;
758 		error = VOP_FSYNC(vp, cred, waitfor, p);
759 		if (error)
760 			allerror = error;
761 		vput(vp);
762 	}
763 
764 	return (allerror);
765 }
766 
767 /*
768  * NFS flat namespace lookup.
769  * Currently unsupported.
770  */
771 /* ARGSUSED */
772 int
773 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
774 {
775 
776 	return (EOPNOTSUPP);
777 }
778 
779 /*
780  * Do that sysctl thang...
781  */
782 int
783 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
784     size_t newlen, struct proc *p)
785 {
786 	int rv;
787 
788 	/*
789 	 * All names at this level are terminal.
790 	 */
791 	if(namelen > 1)
792 		return ENOTDIR;	/* overloaded */
793 
794 	switch(name[0]) {
795 	case NFS_NFSSTATS:
796 		if(!oldp) {
797 			*oldlenp = sizeof nfsstats;
798 			return 0;
799 		}
800 
801 		if(*oldlenp < sizeof nfsstats) {
802 			*oldlenp = sizeof nfsstats;
803 			return ENOMEM;
804 		}
805 
806 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
807 		if(rv) return rv;
808 
809 		if(newp && newlen != sizeof nfsstats)
810 			return EINVAL;
811 
812 		if(newp) {
813 			return copyin(newp, &nfsstats, sizeof nfsstats);
814 		}
815 		return 0;
816 
817 	case NFS_NIOTHREADS:
818 		nfs_getset_niothreads(0);
819 
820 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads);
821 		if (newp)
822 			nfs_getset_niothreads(1);
823 
824 		return rv;
825 
826 	default:
827 		return EOPNOTSUPP;
828 	}
829 }
830 
831 
832 /*
833  * At this point, this should never happen
834  */
835 /* ARGSUSED */
836 int
837 nfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
838 {
839 	return (EINVAL);
840 }
841 
842 /*
843  * Vnode pointer to File handle, should never happen either
844  */
845 /* ARGSUSED */
846 int
847 nfs_vptofh(struct vnode *vp, struct fid *fhp)
848 {
849 	return (EINVAL);
850 }
851 
852 /*
853  * Vfs start routine, a no-op.
854  */
855 /* ARGSUSED */
856 int
857 nfs_start(struct mount *mp, int flags, struct proc *p)
858 {
859 	return (0);
860 }
861 
862 /*
863  * Do operations associated with quotas, not supported
864  */
865 /* ARGSUSED */
866 int
867 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, struct proc *p)
868 {
869 	return (EOPNOTSUPP);
870 }
871 
872 /*
873  * check export permission, not supported
874  */
875 /* ARGUSED */
876 int
877 nfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
878     struct ucred **credanonp)
879 {
880 	return (EOPNOTSUPP);
881 }
882 
883