xref: /openbsd-src/sys/nfs/nfs_vfsops.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: nfs_vfsops.c,v 1.100 2014/07/12 18:43:52 tedu 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/swap.h>
48 #include <sys/buf.h>
49 #include <sys/mbuf.h>
50 #include <sys/dirent.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
55 #include <sys/queue.h>
56 
57 #include <net/if.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 
61 #include <nfs/rpcv2.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfsnode.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsmount.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsdiskless.h>
69 #include <nfs/nfs_var.h>
70 
71 extern struct nfsstats nfsstats;
72 extern int nfs_ticks;
73 extern u_int32_t nfs_procids[NFS_NPROCS];
74 
75 int		nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
76 int		nfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
77 struct mount	*nfs_mount_diskless(struct nfs_dlmount *, char *, int);
78 
79 /*
80  * nfs vfs operations.
81  */
82 const struct vfsops nfs_vfsops = {
83 	nfs_mount,
84 	nfs_start,
85 	nfs_unmount,
86 	nfs_root,
87 	nfs_quotactl,
88 	nfs_statfs,
89 	nfs_sync,
90 	nfs_vget,
91 	nfs_fhtovp,
92 	nfs_vptofh,
93 	nfs_vfs_init,
94 	nfs_sysctl,
95 	nfs_checkexp
96 };
97 
98 /*
99  * nfs statfs call
100  */
101 int
102 nfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
103 {
104 	struct vnode *vp;
105 	struct nfs_statfs *sfp = NULL;
106 	struct nfsm_info	info;
107 	u_int32_t *tl;
108 	int32_t t1;
109 	caddr_t cp2;
110 	struct nfsmount *nmp = VFSTONFS(mp);
111 	int error = 0, retattr;
112 	struct ucred *cred;
113 	struct nfsnode *np;
114 	u_quad_t tquad;
115 
116 	info.nmi_v3 = (nmp->nm_flag & NFSMNT_NFSV3);
117 
118 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
119 	if (error)
120 		return (error);
121 	vp = NFSTOV(np);
122 	cred = crget();
123 	cred->cr_ngroups = 0;
124 	if (info.nmi_v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
125 		(void)nfs_fsinfo(nmp, vp, cred, p);
126 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
127 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(info.nmi_v3));
128 	nfsm_fhtom(&info, vp, info.nmi_v3);
129 
130 	info.nmi_procp = p;
131 	info.nmi_cred = cred;
132 	error = nfs_request(vp, NFSPROC_FSSTAT, &info);
133 	if (info.nmi_v3)
134 		nfsm_postop_attr(vp, retattr);
135 	if (error) {
136 		m_freem(info.nmi_mrep);
137 		goto nfsmout;
138 	}
139 
140 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(info.nmi_v3));
141 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
142 	if (info.nmi_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(info.nmi_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(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
178     struct proc *p)
179 {
180 	struct nfsv3_fsinfo *fsp;
181 	struct nfsm_info	info;
182 	int32_t t1;
183 	u_int32_t *tl, pref, max;
184 	caddr_t cp2;
185 	int error = 0, retattr;
186 
187 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
188 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(1));
189 	nfsm_fhtom(&info, vp, 1);
190 
191 	info.nmi_procp = p;
192 	info.nmi_cred = cred;
193 	error = nfs_request(vp, NFSPROC_FSINFO, &info);
194 
195 	nfsm_postop_attr(vp, retattr);
196 	if (error) {
197 		m_freem(info.nmi_mrep);
198 		goto nfsmout;
199 	}
200 
201 	nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
202 	pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
203 	if (pref < nmp->nm_wsize)
204 		nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
205 			~(NFS_FABLKSIZE - 1);
206 	max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
207 	if (max < nmp->nm_wsize) {
208 		nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
209 		if (nmp->nm_wsize == 0)
210 			nmp->nm_wsize = max;
211 	}
212 	pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
213 	if (pref < nmp->nm_rsize)
214 		nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
215 			~(NFS_FABLKSIZE - 1);
216 	max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
217 	if (max < nmp->nm_rsize) {
218 		nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
219 		if (nmp->nm_rsize == 0)
220 			nmp->nm_rsize = max;
221 	}
222 	pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
223 	if (pref < nmp->nm_readdirsize)
224 		nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
225 			~(NFS_DIRBLKSIZ - 1);
226 	if (max < nmp->nm_readdirsize) {
227 		nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
228 		if (nmp->nm_readdirsize == 0)
229 			nmp->nm_readdirsize = max;
230 	}
231 	nmp->nm_flag |= NFSMNT_GOTFSINFO;
232 
233 	m_freem(info.nmi_mrep);
234 nfsmout:
235 	return (error);
236 }
237 
238 struct nfs_diskless nfs_diskless;
239 
240 /*
241  * Mount a remote root fs via. NFS.  It goes like this:
242  * - Call nfs_boot_init() to fill in the nfs_diskless struct
243  *   (using RARP, bootparam RPC, mountd RPC)
244  * - hand craft the swap nfs vnode hanging off a fake mount point
245  *	if swdevt[0].sw_dev == NODEV
246  * - build the rootfs mount point and call mountnfs() to do the rest.
247  */
248 int
249 nfs_mountroot(void)
250 {
251 	struct vattr attr;
252 	struct mount *mp;
253 	struct vnode *vp;
254 	struct proc *procp;
255 	long n;
256 	int error;
257 
258 	procp = curproc; /* XXX */
259 
260 	/*
261 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
262 	 * Side effect:	 Finds and configures a network interface.
263 	 */
264 	nfs_boot_init(&nfs_diskless, procp);
265 
266 	/*
267 	 * Create the root mount point.
268 	 */
269 	if (nfs_boot_getfh(&nfs_diskless.nd_boot, "root", &nfs_diskless.nd_root, -1))
270 		panic("nfs_mountroot: root");
271 	mp = nfs_mount_diskless(&nfs_diskless.nd_root, "/", 0);
272 	nfs_root(mp, &rootvp);
273 	printf("root on %s\n", nfs_diskless.nd_root.ndm_host);
274 
275 	/*
276 	 * Link it into the mount list.
277 	 */
278 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
279 	vfs_unbusy(mp);
280 
281 	/* Get root attributes (for the time). */
282 	error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp);
283 	if (error) panic("nfs_mountroot: getattr for root");
284 	n = attr.va_atime.tv_sec;
285 #ifdef	DEBUG
286 	printf("root time: 0x%lx\n", n);
287 #endif
288 	inittodr(n);
289 
290 #ifdef notyet
291 	/* Set up swap credentials. */
292 	proc0.p_ucred->cr_uid = ntohl(nfs_diskless.swap_ucred.cr_uid);
293 	proc0.p_ucred->cr_gid = ntohl(nfs_diskless.swap_ucred.cr_gid);
294 	if ((proc0.p_ucred->cr_ngroups = ntohs(nfs_diskless.swap_ucred.cr_ngroups)) >
295 		NGROUPS)
296 		proc0.p_ucred->cr_ngroups = NGROUPS;
297 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
298 	    proc0.p_ucred->cr_groups[i] = ntohl(nfs_diskless.swap_ucred.cr_groups[i]);
299 #endif
300 
301 	/*
302 	 * "Mount" the swap device.
303 	 *
304 	 * On a "dataless" configuration (swap on disk) we will have:
305 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
306 	 */
307 	if (swdevt[0].sw_dev != NODEV) {
308 		if (bdevvp(swapdev, &swapdev_vp))
309 			panic("nfs_mountroot: can't setup swap vp");
310 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
311 		return (0);
312 	}
313 
314 	/*
315 	 * If swapping to an nfs node:	(swdevt[0].sw_dev == NODEV)
316 	 * Create a fake mount point just for the swap vnode so that the
317 	 * swap file can be on a different server from the rootfs.
318 	 *
319 	 * Wait 5 retries, finally no swap is cool. -mickey
320 	 */
321 	error = nfs_boot_getfh(&nfs_diskless.nd_boot, "swap", &nfs_diskless.nd_swap, 5);
322 	if (!error) {
323 		mp = nfs_mount_diskless(&nfs_diskless.nd_swap, "/swap", 0);
324 		nfs_root(mp, &vp);
325 		vfs_unbusy(mp);
326 
327 		/*
328 		 * Since the swap file is not the root dir of a file system,
329 		 * hack it to a regular file.
330 		 */
331 		vp->v_type = VREG;
332 		vp->v_flag = 0;
333 
334 		/*
335 		 * Next line is a hack to make swapmount() work on NFS
336 		 * swap files.
337 		 */
338 		swdevt[0].sw_dev = NETDEV;
339 		/* end hack */
340 		nfs_diskless.sw_vp = vp;
341 
342 		/*
343 		 * Find out how large the swap file is.
344 		 */
345 		error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
346 		if (error)
347 			printf("nfs_mountroot: getattr for swap\n");
348 		n = (long) (attr.va_size >> DEV_BSHIFT);
349 
350 		printf("swap on %s\n", nfs_diskless.nd_swap.ndm_host);
351 #ifdef	DEBUG
352 		printf("swap size: 0x%lx (blocks)\n", n);
353 #endif
354 		return (0);
355 	}
356 
357 	printf("WARNING: no swap\n");
358 	swdevt[0].sw_dev = NODEV;
359 	return (0);
360 }
361 
362 /*
363  * Internal version of mount system call for diskless setup.
364  */
365 struct mount *
366 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag)
367 {
368 	struct mount *mp;
369 	struct mbuf *m;
370 	int error;
371 
372 	if (vfs_rootmountalloc("nfs", mntname, &mp))
373 		panic("nfs_mount_diskless: vfs_rootmountalloc failed");
374 	mp->mnt_flag |= mntflag;
375 
376 	/* Get mbuf for server sockaddr. */
377 	m = m_get(M_WAIT, MT_SONAME);
378 	bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t),
379 	    (m->m_len = ndmntp->ndm_args.addr->sa_len));
380 
381 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
382 	    ndmntp->ndm_args.hostname);
383 	if (error)
384 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
385 
386 	return (mp);
387 }
388 
389 void
390 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp,
391     struct nfs_args *nargp)
392 {
393 	int s;
394 	int adjsock = 0;
395 	int maxio;
396 
397 	s = splsoftnet();
398 
399 #if 0
400 	/* Re-bind if rsrvd port requested and wasn't on one */
401 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
402 		  && (argp->flags & NFSMNT_RESVPORT);
403 #endif
404 	/* Also re-bind if we're switching to/from a connected UDP socket */
405 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
406 	    (argp->flags & NFSMNT_NOCONN));
407 
408 	/* Update flags atomically.  Don't change the lock bits. */
409 	nmp->nm_flag =
410 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
411 	splx(s);
412 
413 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
414 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
415 		if (nmp->nm_timeo < NFS_MINTIMEO)
416 			nmp->nm_timeo = NFS_MINTIMEO;
417 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
418 			nmp->nm_timeo = NFS_MAXTIMEO;
419 	}
420 
421 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1)
422 		nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT);
423 	if (!(nmp->nm_flag & NFSMNT_SOFT))
424 		nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */
425 
426 	if (argp->flags & NFSMNT_NFSV3) {
427 		if (argp->sotype == SOCK_DGRAM)
428 			maxio = NFS_MAXDGRAMDATA;
429 		else
430 			maxio = NFS_MAXDATA;
431 	} else
432 		maxio = NFS_V2MAXDATA;
433 
434 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
435 		int osize = nmp->nm_wsize;
436 		nmp->nm_wsize = argp->wsize;
437 		/* Round down to multiple of blocksize */
438 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
439 		if (nmp->nm_wsize <= 0)
440 			nmp->nm_wsize = NFS_FABLKSIZE;
441 		adjsock |= (nmp->nm_wsize != osize);
442 	}
443 	if (nmp->nm_wsize > maxio)
444 		nmp->nm_wsize = maxio;
445 	if (nmp->nm_wsize > MAXBSIZE)
446 		nmp->nm_wsize = MAXBSIZE;
447 
448 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
449 		int osize = nmp->nm_rsize;
450 		nmp->nm_rsize = argp->rsize;
451 		/* Round down to multiple of blocksize */
452 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
453 		if (nmp->nm_rsize <= 0)
454 			nmp->nm_rsize = NFS_FABLKSIZE;
455 		adjsock |= (nmp->nm_rsize != osize);
456 	}
457 	if (nmp->nm_rsize > maxio)
458 		nmp->nm_rsize = maxio;
459 	if (nmp->nm_rsize > MAXBSIZE)
460 		nmp->nm_rsize = MAXBSIZE;
461 
462 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
463 		nmp->nm_readdirsize = argp->readdirsize;
464 		/* Round down to multiple of blocksize */
465 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
466 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
467 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
468 	} else if (argp->flags & NFSMNT_RSIZE)
469 		nmp->nm_readdirsize = nmp->nm_rsize;
470 
471 	if (nmp->nm_readdirsize > maxio)
472 		nmp->nm_readdirsize = maxio;
473 
474 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
475 		argp->maxgrouplist <= NFS_MAXGRPS)
476 		nmp->nm_numgrps = argp->maxgrouplist;
477 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
478 		argp->readahead <= NFS_MAXRAHEAD)
479 		nmp->nm_readahead = argp->readahead;
480 	if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) {
481 		if (argp->acregmin > 0xffff)
482 			nmp->nm_acregmin = 0xffff;
483 		else
484 			nmp->nm_acregmin = argp->acregmin;
485 	}
486 	if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) {
487 		if (argp->acregmax > 0xffff)
488 			nmp->nm_acregmax = 0xffff;
489 		else
490 			nmp->nm_acregmax = argp->acregmax;
491 	}
492 	if (nmp->nm_acregmin > nmp->nm_acregmax)
493 	  nmp->nm_acregmin = nmp->nm_acregmax;
494 
495 	if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) {
496 		if (argp->acdirmin > 0xffff)
497 			nmp->nm_acdirmin = 0xffff;
498 		else
499 			nmp->nm_acdirmin = argp->acdirmin;
500 	}
501 	if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) {
502 		if (argp->acdirmax > 0xffff)
503 			nmp->nm_acdirmax = 0xffff;
504 		else
505 			nmp->nm_acdirmax = argp->acdirmax;
506 	}
507 	if (nmp->nm_acdirmin > nmp->nm_acdirmax)
508 	  nmp->nm_acdirmin = nmp->nm_acdirmax;
509 
510 	if (nmp->nm_so && adjsock) {
511 		nfs_disconnect(nmp);
512 		if (nmp->nm_sotype == SOCK_DGRAM)
513 			while (nfs_connect(nmp, NULL)) {
514 				printf("nfs_args: retrying connect\n");
515 				(void) tsleep((caddr_t)&lbolt,
516 					      PSOCK, "nfscon", 0);
517 			}
518 	}
519 
520 	/* Update nargp based on nmp */
521 	nargp->wsize = nmp->nm_wsize;
522 	nargp->rsize = nmp->nm_rsize;
523 	nargp->readdirsize = nmp->nm_readdirsize;
524 	nargp->timeo = nmp->nm_timeo;
525 	nargp->retrans = nmp->nm_retry;
526 	nargp->maxgrouplist = nmp->nm_numgrps;
527 	nargp->readahead = nmp->nm_readahead;
528 	nargp->acregmin = nmp->nm_acregmin;
529 	nargp->acregmax = nmp->nm_acregmax;
530 	nargp->acdirmin = nmp->nm_acdirmin;
531 	nargp->acdirmax = nmp->nm_acdirmax;
532 }
533 
534 /*
535  * VFS Operations.
536  *
537  * mount system call
538  * It seems a bit dumb to copyinstr() the host here and then
539  * bcopy() it in mountnfs(), but I wanted to detect errors before
540  * doing the sockargs() call because sockargs() allocates an mbuf and
541  * an error after that means that I have to release the mbuf.
542  */
543 /* ARGSUSED */
544 int
545 nfs_mount(struct mount *mp, const char *path, void *data,
546     struct nameidata *ndp, struct proc *p)
547 {
548 	int error;
549 	struct nfs_args args;
550 	struct mbuf *nam;
551 	char hst[MNAMELEN];
552 	size_t len;
553 	u_char nfh[NFSX_V3FHMAX];
554 
555 	error = copyin(data, &args, sizeof(args.version));
556 	if (error)
557 		return (error);
558 	if (args.version == 3) {
559 		error = copyin (data, (caddr_t)&args,
560 				sizeof (struct nfs_args3));
561 		args.flags &= ~(NFSMNT_INTERNAL|NFSMNT_NOAC);
562 	} else if (args.version == NFS_ARGSVERSION) {
563 		error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
564 		args.flags &= ~NFSMNT_NOAC; /* XXX - compatibility */
565 	} else
566 		return (EPROGMISMATCH);
567 	if (error)
568 		return (error);
569 
570 	if ((args.flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS)
571 		return (EINVAL);
572 
573 	if (nfs_niothreads < 0) {
574 		nfs_niothreads = 4;
575 		nfs_getset_niothreads(1);
576 	}
577 
578 	if (mp->mnt_flag & MNT_UPDATE) {
579 		struct nfsmount *nmp = VFSTONFS(mp);
580 
581 		if (nmp == NULL)
582 			return (EIO);
583 		/*
584 		 * When doing an update, we can't change from or to
585 		 * v3.
586 		 */
587 		args.flags = (args.flags & ~(NFSMNT_NFSV3)) |
588 		    (nmp->nm_flag & (NFSMNT_NFSV3));
589 		nfs_decode_args(nmp, &args, &mp->mnt_stat.mount_info.nfs_args);
590 		return (0);
591 	}
592 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
593 		return (EINVAL);
594 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
595 	if (error)
596 		return (error);
597 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
598 	if (error)
599 		return (error);
600 	bzero(&hst[len], MNAMELEN - len);
601 	/* sockargs() call must be after above copyin() calls */
602 	error = sockargs(&nam, args.addr, args.addrlen, MT_SONAME);
603 	if (error)
604 		return (error);
605 	args.fh = nfh;
606 	error = mountnfs(&args, mp, nam, path, hst);
607 	return (error);
608 }
609 
610 /*
611  * Common code for mount and mountroot
612  */
613 int
614 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam,
615     const char *pth, char *hst)
616 {
617 	struct nfsmount *nmp;
618 	int error;
619 
620 	if (mp->mnt_flag & MNT_UPDATE) {
621 		nmp = VFSTONFS(mp);
622 		/* update paths, file handles, etc, here	XXX */
623 		m_freem(nam);
624 		return (0);
625 	} else {
626 		nmp = malloc(sizeof(struct nfsmount), M_NFSMNT,
627 		    M_WAITOK|M_ZERO);
628 		mp->mnt_data = (qaddr_t)nmp;
629 	}
630 
631 	vfs_getnewfsid(mp);
632 	nmp->nm_mountp = mp;
633 	nmp->nm_timeo = NFS_TIMEO;
634 	nmp->nm_retry = NFS_RETRANS;
635 	nmp->nm_wsize = NFS_WSIZE;
636 	nmp->nm_rsize = NFS_RSIZE;
637 	nmp->nm_readdirsize = NFS_READDIRSIZE;
638 	nmp->nm_numgrps = NFS_MAXGRPS;
639 	nmp->nm_readahead = NFS_DEFRAHEAD;
640 	nmp->nm_fhsize = argp->fhsize;
641 	nmp->nm_acregmin = NFS_MINATTRTIMO;
642 	nmp->nm_acregmax = NFS_MAXATTRTIMO;
643 	nmp->nm_acdirmin = NFS_MINATTRTIMO;
644 	nmp->nm_acdirmax = NFS_MAXATTRTIMO;
645 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
646 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN);
647 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
648 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
649 	bcopy(hst, mp->mnt_stat.f_mntfromspec, MNAMELEN);
650 	bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp));
651 	nmp->nm_nam = nam;
652 	nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args);
653 
654 	RB_INIT(&nmp->nm_ntree);
655 	TAILQ_INIT(&nmp->nm_reqsq);
656 	timeout_set(&nmp->nm_rtimeout, nfs_timer, nmp);
657 
658 	/* Set up the sockets and per-host congestion */
659 	nmp->nm_sotype = argp->sotype;
660 	nmp->nm_soproto = argp->proto;
661 
662 	/*
663 	 * For Connection based sockets (TCP,...) defer the connect until
664 	 * the first request, in case the server is not responding.
665 	 */
666 	if (nmp->nm_sotype == SOCK_DGRAM &&
667 	    (error = nfs_connect(nmp, NULL)))
668 		goto bad;
669 
670 	/*
671 	 * This is silly, but it has to be set so that vinifod() works.
672 	 * We do not want to do an nfs_statfs() here since we can get
673 	 * stuck on a dead server and we are holding a lock on the mount
674 	 * point.
675 	 */
676 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
677 
678 	return (0);
679 bad:
680 	nfs_disconnect(nmp);
681 	free((caddr_t)nmp, M_NFSMNT, 0);
682 	m_freem(nam);
683 	return (error);
684 }
685 
686 /* unmount system call */
687 int
688 nfs_unmount(struct mount *mp, int mntflags, struct proc *p)
689 {
690 	struct nfsmount *nmp;
691 	int error, flags;
692 
693 	nmp = VFSTONFS(mp);
694 	flags = 0;
695 
696 	if (mntflags & MNT_FORCE)
697 		flags |= FORCECLOSE;
698 
699 	error = vflush(mp, NULL, flags);
700 	if (error)
701 		return (error);
702 
703 	nfs_disconnect(nmp);
704 	m_freem(nmp->nm_nam);
705 	timeout_del(&nmp->nm_rtimeout);
706 	free(nmp, M_NFSMNT, 0);
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, 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