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