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