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