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