xref: /netbsd-src/sys/nfs/nfs_vfsops.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: nfs_vfsops.c,v 1.134 2003/12/04 19:38:25 atatat Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.134 2003/12/04 19:38:25 atatat Exp $");
39 
40 #if defined(_KERNEL_OPT)
41 #include "opt_compat_netbsd.h"
42 #include "opt_nfs.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/signal.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/device.h>
51 #include <sys/vnode.h>
52 #include <sys/kernel.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfsproto.h>
67 #include <nfs/nfsnode.h>
68 #include <nfs/nfs.h>
69 #include <nfs/nfsmount.h>
70 #include <nfs/xdr_subs.h>
71 #include <nfs/nfsm_subs.h>
72 #include <nfs/nfsdiskless.h>
73 #include <nfs/nqnfs.h>
74 #include <nfs/nfs_var.h>
75 
76 extern struct nfsstats nfsstats;
77 extern int nfs_ticks;
78 
79 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
80 
81 /*
82  * nfs vfs operations.
83  */
84 
85 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
86 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
87 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
88 
89 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
90 	&nfsv2_vnodeop_opv_desc,
91 	&spec_nfsv2nodeop_opv_desc,
92 	&fifo_nfsv2nodeop_opv_desc,
93 	NULL,
94 };
95 
96 struct vfsops nfs_vfsops = {
97 	MOUNT_NFS,
98 	nfs_mount,
99 	nfs_start,
100 	nfs_unmount,
101 	nfs_root,
102 	nfs_quotactl,
103 	nfs_statfs,
104 	nfs_sync,
105 	nfs_vget,
106 	nfs_fhtovp,
107 	nfs_vptofh,
108 	nfs_vfs_init,
109 	nfs_vfs_reinit,
110 	nfs_vfs_done,
111 	NULL,
112 	nfs_mountroot,
113 	nfs_checkexp,
114 	nfs_vnodeopv_descs,
115 };
116 
117 extern u_int32_t nfs_procids[NFS_NPROCS];
118 extern u_int32_t nfs_prog, nfs_vers;
119 
120 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
121     struct mount **, struct vnode **, struct proc *));
122 
123 /*
124  * nfs statfs call
125  */
126 int
127 nfs_statfs(mp, sbp, p)
128 	struct mount *mp;
129 	struct statfs *sbp;
130 	struct proc *p;
131 {
132 	struct vnode *vp;
133 	struct nfs_statfs *sfp;
134 	caddr_t cp;
135 	u_int32_t *tl;
136 	int32_t t1, t2;
137 	caddr_t bpos, dpos, cp2;
138 	struct nfsmount *nmp = VFSTONFS(mp);
139 	int error = 0, retattr;
140 #ifdef NFS_V2_ONLY
141 	const int v3 = 0;
142 #else
143 	int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
144 #endif
145 	struct mbuf *mreq, *mrep = NULL, *md, *mb;
146 	struct ucred *cred;
147 	u_quad_t tquad;
148 	struct nfsnode *np;
149 
150 #ifndef nolint
151 	sfp = (struct nfs_statfs *)0;
152 #endif
153 	vp = nmp->nm_vnode;
154 	np = VTONFS(vp);
155 	cred = crget();
156 	cred->cr_ngroups = 0;
157 #ifndef NFS_V2_ONLY
158 	if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
159 		(void)nfs_fsinfo(nmp, vp, cred, p);
160 #endif
161 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
162 	nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
163 	nfsm_fhtom(np, v3);
164 	nfsm_request(np, NFSPROC_FSSTAT, p, cred);
165 	if (v3)
166 		nfsm_postop_attr(vp, retattr, 0);
167 	if (error) {
168 		if (mrep != NULL) {
169 			if (mrep->m_next != NULL)
170 				printf("nfs_vfsops: nfs_statfs would loose buffers\n");
171 			m_freem(mrep);
172 		}
173 		goto nfsmout;
174 	}
175 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
176 #ifdef COMPAT_09
177 	sbp->f_type = 2;
178 #else
179 	sbp->f_type = 0;
180 #endif
181 	sbp->f_flags = nmp->nm_flag;
182 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
183 	if (v3) {
184 		sbp->f_bsize = NFS_FABLKSIZE;
185 		tquad = fxdr_hyper(&sfp->sf_tbytes);
186 		sbp->f_blocks = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
187 		tquad = fxdr_hyper(&sfp->sf_fbytes);
188 		sbp->f_bfree = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
189 		tquad = fxdr_hyper(&sfp->sf_abytes);
190 		sbp->f_bavail = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
191 		tquad = fxdr_hyper(&sfp->sf_tfiles);
192 		sbp->f_files = (long)tquad;
193 		tquad = fxdr_hyper(&sfp->sf_ffiles);
194 		sbp->f_ffree = (long)tquad;
195 	} else {
196 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
197 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
198 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
199 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
200 		sbp->f_files = 0;
201 		sbp->f_ffree = 0;
202 	}
203 	copy_statfs_info(sbp, mp);
204 	nfsm_reqdone;
205 	crfree(cred);
206 	return (error);
207 }
208 
209 #ifndef NFS_V2_ONLY
210 /*
211  * nfs version 3 fsinfo rpc call
212  */
213 int
214 nfs_fsinfo(nmp, vp, cred, p)
215 	struct nfsmount *nmp;
216 	struct vnode *vp;
217 	struct ucred *cred;
218 	struct proc *p;
219 {
220 	struct nfsv3_fsinfo *fsp;
221 	caddr_t cp;
222 	int32_t t1, t2;
223 	u_int32_t *tl, pref, max;
224 	caddr_t bpos, dpos, cp2;
225 	int error = 0, retattr;
226 	struct mbuf *mreq, *mrep, *md, *mb;
227 	u_int64_t maxfsize;
228 	struct nfsnode *np = VTONFS(vp);
229 
230 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
231 	nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
232 	nfsm_fhtom(np, 1);
233 	nfsm_request(np, NFSPROC_FSINFO, p, cred);
234 	nfsm_postop_attr(vp, retattr, 0);
235 	if (!error) {
236 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
237 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
238 		if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
239 		    pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
240 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
241 				~(NFS_FABLKSIZE - 1);
242 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
243 		if (max < nmp->nm_wsize && max > 0) {
244 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
245 			if (nmp->nm_wsize == 0)
246 				nmp->nm_wsize = max;
247 		}
248 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
249 		if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
250 		    pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
251 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
252 				~(NFS_FABLKSIZE - 1);
253 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
254 		if (max < nmp->nm_rsize && max > 0) {
255 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
256 			if (nmp->nm_rsize == 0)
257 				nmp->nm_rsize = max;
258 		}
259 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
260 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
261 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
262 				~(NFS_DIRFRAGSIZ - 1);
263 		if (max < nmp->nm_readdirsize && max > 0) {
264 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
265 			if (nmp->nm_readdirsize == 0)
266 				nmp->nm_readdirsize = max;
267 		}
268 		/* XXX */
269 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
270 		maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
271 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
272 			nmp->nm_maxfilesize = maxfsize;
273 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
274 	}
275 	nfsm_reqdone;
276 	return (error);
277 }
278 #endif
279 
280 /*
281  * Mount a remote root fs via. NFS.  It goes like this:
282  * - Call nfs_boot_init() to fill in the nfs_diskless struct
283  * - build the rootfs mount point and call mountnfs() to do the rest.
284  */
285 int
286 nfs_mountroot()
287 {
288 	struct nfs_diskless *nd;
289 	struct vattr attr;
290 	struct mount *mp;
291 	struct vnode *vp;
292 	struct proc *procp;
293 	long n;
294 	int error;
295 
296 	procp = curproc; /* XXX */
297 
298 	if (root_device->dv_class != DV_IFNET)
299 		return (ENODEV);
300 
301 	/*
302 	 * XXX time must be non-zero when we init the interface or else
303 	 * the arp code will wedge.  [Fixed now in if_ether.c]
304 	 * However, the NFS attribute cache gives false "hits" when
305 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
306 	 */
307 	if (time.tv_sec < NFS_MAXATTRTIMO)
308 		time.tv_sec = NFS_MAXATTRTIMO;
309 
310 	/*
311 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
312 	 * Side effect:  Finds and configures a network interface.
313 	 */
314 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
315 	memset((caddr_t)nd, 0, sizeof(*nd));
316 	error = nfs_boot_init(nd, procp);
317 	if (error) {
318 		free(nd, M_NFSMNT);
319 		return (error);
320 	}
321 
322 	/*
323 	 * Create the root mount point.
324 	 */
325 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
326 	if (error)
327 		goto out;
328 	printf("root on %s\n", nd->nd_root.ndm_host);
329 
330 	/*
331 	 * Link it into the mount list.
332 	 */
333 	simple_lock(&mountlist_slock);
334 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
335 	simple_unlock(&mountlist_slock);
336 	rootvp = vp;
337 	mp->mnt_vnodecovered = NULLVP;
338 	vfs_unbusy(mp);
339 
340 	/* Get root attributes (for the time). */
341 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
342 	if (error)
343 		panic("nfs_mountroot: getattr for root");
344 	n = attr.va_atime.tv_sec;
345 #ifdef	DEBUG
346 	printf("root time: 0x%lx\n", n);
347 #endif
348 	inittodr(n);
349 
350 out:
351 	if (error)
352 		nfs_boot_cleanup(nd, procp);
353 	free(nd, M_NFSMNT);
354 	return (error);
355 }
356 
357 /*
358  * Internal version of mount system call for diskless setup.
359  * Separate function because we used to call it twice.
360  * (once for root and once for swap)
361  */
362 static int
363 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
364 	struct nfs_dlmount *ndmntp;
365 	const char *mntname;	/* mount point name */
366 	struct mount **mpp;
367 	struct vnode **vpp;
368 	struct proc *p;
369 {
370 	struct mount *mp;
371 	struct mbuf *m;
372 	int error;
373 
374 	vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
375 
376 	mp->mnt_op = &nfs_vfsops;
377 
378 	/*
379 	 * Historical practice expects NFS root file systems to
380 	 * be initially mounted r/w.
381 	 */
382 	mp->mnt_flag &= ~MNT_RDONLY;
383 
384 	/* Get mbuf for server sockaddr. */
385 	m = m_get(M_WAIT, MT_SONAME);
386 	if (m == NULL)
387 		panic("nfs_mountroot: mget soname for %s", mntname);
388 	MCLAIM(m, &nfs_mowner);
389 	memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
390 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
391 
392 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
393 			 ndmntp->ndm_args.hostname, vpp, p);
394 	if (error) {
395 		mp->mnt_op->vfs_refcount--;
396 		vfs_unbusy(mp);
397 		printf("nfs_mountroot: mount %s failed: %d\n",
398 		       mntname, error);
399 		free(mp, M_MOUNT);
400 	} else
401 		*mpp = mp;
402 
403 	return (error);
404 }
405 
406 void
407 nfs_decode_args(nmp, argp)
408 	struct nfsmount *nmp;
409 	struct nfs_args *argp;
410 {
411 	int s;
412 	int adjsock;
413 	int maxio;
414 
415 	s = splsoftnet();
416 
417 	/*
418 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
419 	 * no sense in that context.
420 	 */
421 	if (argp->sotype == SOCK_STREAM)
422 		argp->flags &= ~NFSMNT_NOCONN;
423 
424 	/*
425 	 * Cookie translation is not needed for v2, silently ignore it.
426 	 */
427 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
428 	    NFSMNT_XLATECOOKIE)
429 		argp->flags &= ~NFSMNT_XLATECOOKIE;
430 
431 	/* Re-bind if rsrvd port requested and wasn't on one */
432 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
433 		  && (argp->flags & NFSMNT_RESVPORT);
434 	/* Also re-bind if we're switching to/from a connected UDP socket */
435 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
436 		    (argp->flags & NFSMNT_NOCONN));
437 
438 	/* Update flags. */
439 	nmp->nm_flag = argp->flags;
440 	splx(s);
441 
442 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
443 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
444 		if (nmp->nm_timeo < NFS_MINTIMEO)
445 			nmp->nm_timeo = NFS_MINTIMEO;
446 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
447 			nmp->nm_timeo = NFS_MAXTIMEO;
448 	}
449 
450 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
451 		nmp->nm_retry = argp->retrans;
452 		if (nmp->nm_retry > NFS_MAXREXMIT)
453 			nmp->nm_retry = NFS_MAXREXMIT;
454 	}
455 
456 #ifndef NFS_V2_ONLY
457 	if (argp->flags & NFSMNT_NFSV3) {
458 		if (argp->sotype == SOCK_DGRAM)
459 			maxio = NFS_MAXDGRAMDATA;
460 		else
461 			maxio = NFS_MAXDATA;
462 	} else
463 #endif
464 		maxio = NFS_V2MAXDATA;
465 
466 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
467 		int osize = nmp->nm_wsize;
468 		nmp->nm_wsize = argp->wsize;
469 		/* Round down to multiple of blocksize */
470 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
471 		if (nmp->nm_wsize <= 0)
472 			nmp->nm_wsize = NFS_FABLKSIZE;
473 		adjsock |= (nmp->nm_wsize != osize);
474 	}
475 	if (nmp->nm_wsize > maxio)
476 		nmp->nm_wsize = maxio;
477 	if (nmp->nm_wsize > MAXBSIZE)
478 		nmp->nm_wsize = MAXBSIZE;
479 
480 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
481 		int osize = nmp->nm_rsize;
482 		nmp->nm_rsize = argp->rsize;
483 		/* Round down to multiple of blocksize */
484 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
485 		if (nmp->nm_rsize <= 0)
486 			nmp->nm_rsize = NFS_FABLKSIZE;
487 		adjsock |= (nmp->nm_rsize != osize);
488 	}
489 	if (nmp->nm_rsize > maxio)
490 		nmp->nm_rsize = maxio;
491 	if (nmp->nm_rsize > MAXBSIZE)
492 		nmp->nm_rsize = MAXBSIZE;
493 
494 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
495 		nmp->nm_readdirsize = argp->readdirsize;
496 		/* Round down to multiple of minimum blocksize */
497 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
498 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
499 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
500 		/* Bigger than buffer size makes no sense */
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_LEASETERM) && argp->leaseterm >= 2 &&
516 		argp->leaseterm <= NQ_MAXLEASE)
517 		nmp->nm_leaseterm = argp->leaseterm;
518 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
519 		argp->deadthresh <= NQ_NEVERDEAD)
520 		nmp->nm_deadthresh = argp->deadthresh;
521 
522 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
523 		    (nmp->nm_soproto != argp->proto));
524 	nmp->nm_sotype = argp->sotype;
525 	nmp->nm_soproto = argp->proto;
526 
527 	if (nmp->nm_so && adjsock) {
528 		nfs_safedisconnect(nmp);
529 		if (nmp->nm_sotype == SOCK_DGRAM)
530 			while (nfs_connect(nmp, (struct nfsreq *)0)) {
531 				printf("nfs_args: retrying connect\n");
532 				(void) tsleep((caddr_t)&lbolt,
533 					      PSOCK, "nfscn3", 0);
534 			}
535 	}
536 }
537 
538 /*
539  * VFS Operations.
540  *
541  * mount system call
542  * It seems a bit dumb to copyinstr() the host and path here and then
543  * memcpy() them in mountnfs(), but I wanted to detect errors before
544  * doing the sockargs() call because sockargs() allocates an mbuf and
545  * an error after that means that I have to release the mbuf.
546  */
547 /* ARGSUSED */
548 int
549 nfs_mount(mp, path, data, ndp, p)
550 	struct mount *mp;
551 	const char *path;
552 	void *data;
553 	struct nameidata *ndp;
554 	struct proc *p;
555 {
556 	int error;
557 	struct nfs_args args;
558 	struct mbuf *nam;
559 	struct nfsmount *nmp = VFSTONFS(mp);
560 	struct sockaddr *sa;
561 	struct vnode *vp;
562 	char *pth, *hst;
563 	size_t len;
564 	u_char *nfh;
565 
566 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
567 	if (error)
568 		return (error);
569 
570 	if (mp->mnt_flag & MNT_GETARGS) {
571 
572 		if (nmp == NULL)
573 			return (EIO);
574 		if (args.addr != NULL) {
575 			sa = mtod(nmp->nm_nam, struct sockaddr *);
576 			error = copyout(sa, args.addr, sa->sa_len);
577 			if (error)
578 				return (error);
579 			args.addrlen = sa->sa_len;
580 		} else
581 			args.addrlen = 0;
582 
583 		args.version = NFS_ARGSVERSION;
584 		args.sotype = nmp->nm_sotype;
585 		args.proto = nmp->nm_soproto;
586 		args.fh = NULL;
587 		args.fhsize = 0;
588 		args.flags = nmp->nm_flag;
589 		args.wsize = nmp->nm_wsize;
590 		args.rsize = nmp->nm_rsize;
591 		args.readdirsize = nmp->nm_readdirsize;
592 		args.timeo = nmp->nm_timeo;
593 		args.retrans = nmp->nm_retry;
594 		args.maxgrouplist = nmp->nm_numgrps;
595 		args.readahead = nmp->nm_readahead;
596 		args.leaseterm = nmp->nm_leaseterm;
597 		args.deadthresh = nmp->nm_deadthresh;
598 		args.hostname = NULL;
599 		return (copyout(&args, data, sizeof(args)));
600 	}
601 
602 	if (args.version != NFS_ARGSVERSION)
603 		return (EPROGMISMATCH);
604 #ifdef NFS_V2_ONLY
605 	if (args.flags & NFSMNT_NQNFS)
606 		return (EPROGUNAVAIL);
607 	if (args.flags & NFSMNT_NFSV3)
608 		return (EPROGMISMATCH);
609 #endif
610 	if (mp->mnt_flag & MNT_UPDATE) {
611 		if (nmp == NULL)
612 			return (EIO);
613 		/*
614 		 * When doing an update, we can't change from or to
615 		 * v3 and/or nqnfs, or change cookie translation
616 		 */
617 		args.flags = (args.flags &
618 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
619 		    (nmp->nm_flag &
620 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
621 		nfs_decode_args(nmp, &args);
622 		return (0);
623 	}
624 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
625 		return (EINVAL);
626 	MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
627 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
628 	if (error)
629 		return (error);
630 	MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
631 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
632 	if (error)
633 		goto free_nfh;
634 	memset(&pth[len], 0, MNAMELEN - len);
635 	MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
636 	error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
637 	if (error)
638 		goto free_pth;
639 	memset(&hst[len], 0, MNAMELEN - len);
640 	/* sockargs() call must be after above copyin() calls */
641 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
642 	if (error)
643 		goto free_hst;
644 	MCLAIM(nam, &nfs_mowner);
645 	args.fh = nfh;
646 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
647 
648 free_hst:
649 	FREE(hst, M_TEMP);
650 free_pth:
651 	FREE(pth, M_TEMP);
652 free_nfh:
653 	FREE(nfh, M_TEMP);
654 
655 	return (error);
656 }
657 
658 /*
659  * Common code for mount and mountroot
660  */
661 int
662 mountnfs(argp, mp, nam, pth, hst, vpp, p)
663 	struct nfs_args *argp;
664 	struct mount *mp;
665 	struct mbuf *nam;
666 	const char *pth, *hst;
667 	struct vnode **vpp;
668 	struct proc *p;
669 {
670 	struct nfsmount *nmp;
671 	struct nfsnode *np;
672 	int error;
673 	struct vattr *attrs;
674 	struct ucred *cr;
675 
676 	/*
677 	 * If the number of nfs iothreads to use has never
678 	 * been set, create a reasonable number of them.
679 	 */
680 
681 	if (nfs_niothreads < 0) {
682 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
683 		nfs_getset_niothreads(TRUE);
684 	}
685 
686 	if (mp->mnt_flag & MNT_UPDATE) {
687 		nmp = VFSTONFS(mp);
688 		/* update paths, file handles, etc, here	XXX */
689 		m_freem(nam);
690 		return (0);
691 	} else {
692 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
693 		    M_NFSMNT, M_WAITOK);
694 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
695 		mp->mnt_data = nmp;
696 		TAILQ_INIT(&nmp->nm_uidlruhead);
697 		TAILQ_INIT(&nmp->nm_bufq);
698 		lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
699 		simple_lock_init(&nmp->nm_slock);
700 	}
701 	vfs_getnewfsid(mp);
702 	nmp->nm_mountp = mp;
703 
704 #ifndef NFS_V2_ONLY
705 	if (argp->flags & NFSMNT_NQNFS)
706 		/*
707 		 * We have to set mnt_maxsymlink to a non-zero value so
708 		 * that COMPAT_43 routines will know that we are setting
709 		 * the d_type field in directories (and can zero it for
710 		 * unsuspecting binaries).
711 		 */
712 		mp->mnt_maxsymlinklen = 1;
713 #endif
714 
715 #ifndef NFS_V2_ONLY
716 	if ((argp->flags & NFSMNT_NFSV3) == 0)
717 #endif
718 		/*
719 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
720 		 * will fill this in.
721 		 */
722 		nmp->nm_maxfilesize = 0xffffffffLL;
723 
724 	nmp->nm_timeo = NFS_TIMEO;
725 	nmp->nm_retry = NFS_RETRANS;
726 	nmp->nm_wsize = NFS_WSIZE;
727 	nmp->nm_rsize = NFS_RSIZE;
728 	nmp->nm_readdirsize = NFS_READDIRSIZE;
729 	nmp->nm_numgrps = NFS_MAXGRPS;
730 	nmp->nm_readahead = NFS_DEFRAHEAD;
731 	nmp->nm_leaseterm = NQ_DEFLEASE;
732 	nmp->nm_deadthresh = NQ_DEADTHRESH;
733 	CIRCLEQ_INIT(&nmp->nm_timerhead);
734 	nmp->nm_inprog = NULLVP;
735 #ifdef COMPAT_09
736 	mp->mnt_stat.f_type = 2;
737 #else
738 	mp->mnt_stat.f_type = 0;
739 #endif
740 	error = set_statfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p);
741 	if (error)
742 		goto bad;
743 	nmp->nm_nam = nam;
744 
745 	/* Set up the sockets and per-host congestion */
746 	nmp->nm_sotype = argp->sotype;
747 	nmp->nm_soproto = argp->proto;
748 
749 	nfs_decode_args(nmp, argp);
750 
751 	mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
752 	mp->mnt_dev_bshift = DEV_BSHIFT;
753 
754 	/*
755 	 * For Connection based sockets (TCP,...) defer the connect until
756 	 * the first request, in case the server is not responding.
757 	 */
758 	if (nmp->nm_sotype == SOCK_DGRAM &&
759 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
760 		goto bad;
761 
762 	/*
763 	 * This is silly, but it has to be set so that vinifod() works.
764 	 * We do not want to do an nfs_statfs() here since we can get
765 	 * stuck on a dead server and we are holding a lock on the mount
766 	 * point.
767 	 */
768 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
769 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
770 	if (error)
771 		goto bad;
772 	*vpp = NFSTOV(np);
773 	MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
774 	VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
775 	if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
776 		cr = crget();
777 		cr->cr_uid = attrs->va_uid;
778 		cr->cr_gid = attrs->va_gid;
779 		cr->cr_ngroups = 0;
780 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
781 		crfree(cr);
782 	}
783 	FREE(attrs, M_TEMP);
784 
785 	/*
786 	 * A reference count is needed on the nfsnode representing the
787 	 * remote root.  If this object is not persistent, then backward
788 	 * traversals of the mount point (i.e. "..") will not work if
789 	 * the nfsnode gets flushed out of the cache. Ufs does not have
790 	 * this problem, because one can identify root inodes by their
791 	 * number == ROOTINO (2). So, just unlock, but no rele.
792 	 */
793 
794 	nmp->nm_vnode = *vpp;
795 	VOP_UNLOCK(*vpp, 0);
796 
797 	return (0);
798 bad:
799 	nfs_disconnect(nmp);
800 	free((caddr_t)nmp, M_NFSMNT);
801 	m_freem(nam);
802 	return (error);
803 }
804 
805 /*
806  * unmount system call
807  */
808 int
809 nfs_unmount(mp, mntflags, p)
810 	struct mount *mp;
811 	int mntflags;
812 	struct proc *p;
813 {
814 	struct nfsmount *nmp;
815 	struct vnode *vp;
816 	int error, flags = 0;
817 
818 	if (mntflags & MNT_FORCE)
819 		flags |= FORCECLOSE;
820 	nmp = VFSTONFS(mp);
821 	/*
822 	 * Goes something like this..
823 	 * - Check for activity on the root vnode (other than ourselves).
824 	 * - Call vflush() to clear out vnodes for this file system,
825 	 *   except for the root vnode.
826 	 * - Decrement reference on the vnode representing remote root.
827 	 * - Close the socket
828 	 * - Free up the data structures
829 	 */
830 	/*
831 	 * We need to decrement the ref. count on the nfsnode representing
832 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
833 	 * has done vput on this vnode, otherwise we would get deadlock!
834 	 */
835 	vp = nmp->nm_vnode;
836 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
837 	if (error != 0)
838 		return error;
839 
840 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
841 		vput(vp);
842 		return (EBUSY);
843 	}
844 
845 	/*
846 	 * Must handshake with nqnfs_clientd() if it is active.
847 	 */
848 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
849 	while (nmp->nm_inprog != NULLVP)
850 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
851 	error = vflush(mp, vp, flags);
852 	if (error) {
853 		vput(vp);
854 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
855 		return (error);
856 	}
857 
858 	/*
859 	 * We are now committed to the unmount; mark the mount structure
860 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
861 	 * will go away cleanly.
862 	 */
863 	nmp->nm_iflag |= NFSMNT_DISMNT;
864 
865 	/*
866 	 * There are two reference counts to get rid of here
867 	 * (see comment in mountnfs()).
868 	 */
869 	vrele(vp);
870 	vput(vp);
871 	vgone(vp);
872 	nfs_disconnect(nmp);
873 	m_freem(nmp->nm_nam);
874 
875 	/*
876 	 * For NQNFS, let the server daemon free the nfsmount structure.
877 	 */
878 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
879 		free((caddr_t)nmp, M_NFSMNT);
880 	return (0);
881 }
882 
883 /*
884  * Return root of a filesystem
885  */
886 int
887 nfs_root(mp, vpp)
888 	struct mount *mp;
889 	struct vnode **vpp;
890 {
891 	struct vnode *vp;
892 	struct nfsmount *nmp;
893 	int error;
894 
895 	nmp = VFSTONFS(mp);
896 	vp = nmp->nm_vnode;
897 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
898 	if (error != 0)
899 		return error;
900 	if (vp->v_type == VNON)
901 		vp->v_type = VDIR;
902 	vp->v_flag = VROOT;
903 	*vpp = vp;
904 	return (0);
905 }
906 
907 extern int syncprt;
908 
909 /*
910  * Flush out the buffer cache
911  */
912 /* ARGSUSED */
913 int
914 nfs_sync(mp, waitfor, cred, p)
915 	struct mount *mp;
916 	int waitfor;
917 	struct ucred *cred;
918 	struct proc *p;
919 {
920 	struct vnode *vp;
921 	int error, allerror = 0;
922 
923 	/*
924 	 * Force stale buffer cache information to be flushed.
925 	 */
926 loop:
927 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
928 		/*
929 		 * If the vnode that we are about to sync is no longer
930 		 * associated with this mount point, start over.
931 		 */
932 		if (vp->v_mount != mp)
933 			goto loop;
934 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
935 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
936 		     vp->v_uobj.uo_npages == 0))
937 			continue;
938 		if (vget(vp, LK_EXCLUSIVE))
939 			goto loop;
940 		error = VOP_FSYNC(vp, cred,
941 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
942 		if (error)
943 			allerror = error;
944 		vput(vp);
945 	}
946 	return (allerror);
947 }
948 
949 /*
950  * NFS flat namespace lookup.
951  * Currently unsupported.
952  */
953 /* ARGSUSED */
954 int
955 nfs_vget(mp, ino, vpp)
956 	struct mount *mp;
957 	ino_t ino;
958 	struct vnode **vpp;
959 {
960 
961 	return (EOPNOTSUPP);
962 }
963 
964 /*
965  * Do that sysctl thang...
966  */
967 static int
968 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
969 {
970 	int error;
971 
972 	nfs_getset_niothreads(0);
973         error = sysctl_lookup(SYSCTLFN_CALL(rnode));
974 	if (error || newp == NULL)
975 		return (error);
976 	nfs_getset_niothreads(1);
977 
978 	return (0);
979 }
980 
981 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
982 {
983 
984 	sysctl_createv(SYSCTL_PERMANENT,
985 		       CTLTYPE_NODE, "vfs", NULL,
986 		       NULL, 0, NULL, 0,
987 		       CTL_VFS, CTL_EOL);
988 	sysctl_createv(SYSCTL_PERMANENT,
989 		       CTLTYPE_NODE, "nfs", NULL,
990 		       NULL, 0, NULL, 0,
991 		       CTL_VFS, 2, CTL_EOL);
992 	/*
993 	 * XXX the "2" above could be dynamic, thereby eliminating one
994 	 * more instance of the "number to vfs" mapping problem, but
995 	 * "2" is the order as taken from sys/mount.h
996 	 */
997 
998 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
999 		       CTLTYPE_STRUCT, "nfsstats", NULL,
1000 		       NULL, 0, &nfsstats, sizeof(nfsstats),
1001 		       CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1002 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
1003 		       CTLTYPE_INT, "iothreads", NULL,
1004 		       sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1005 		       CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1006 }
1007 
1008 /*
1009  * At this point, this should never happen
1010  */
1011 /* ARGSUSED */
1012 int
1013 nfs_fhtovp(mp, fhp, vpp)
1014 	struct mount *mp;
1015 	struct fid *fhp;
1016 	struct vnode **vpp;
1017 {
1018 
1019 	return (EINVAL);
1020 }
1021 
1022 /* ARGSUSED */
1023 int
1024 nfs_checkexp(mp, nam, exflagsp, credanonp)
1025 	struct mount *mp;
1026 	struct mbuf *nam;
1027 	int *exflagsp;
1028 	struct ucred **credanonp;
1029 {
1030 
1031 	return (EINVAL);
1032 }
1033 
1034 /*
1035  * Vnode pointer to File handle, should never happen either
1036  */
1037 /* ARGSUSED */
1038 int
1039 nfs_vptofh(vp, fhp)
1040 	struct vnode *vp;
1041 	struct fid *fhp;
1042 {
1043 
1044 	return (EINVAL);
1045 }
1046 
1047 /*
1048  * Vfs start routine, a no-op.
1049  */
1050 /* ARGSUSED */
1051 int
1052 nfs_start(mp, flags, p)
1053 	struct mount *mp;
1054 	int flags;
1055 	struct proc *p;
1056 {
1057 
1058 	return (0);
1059 }
1060 
1061 /*
1062  * Do operations associated with quotas, not supported
1063  */
1064 /* ARGSUSED */
1065 int
1066 nfs_quotactl(mp, cmd, uid, arg, p)
1067 	struct mount *mp;
1068 	int cmd;
1069 	uid_t uid;
1070 	caddr_t arg;
1071 	struct proc *p;
1072 {
1073 
1074 	return (EOPNOTSUPP);
1075 }
1076