xref: /netbsd-src/sys/nfs/nfs_vfsops.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: nfs_vfsops.c,v 1.106 2001/07/30 22:51:06 jdolecek 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_OPT)
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 	if (args.flags & NFSMNT_NQNFS)
568 		return (EPROGUNAVAIL);
569 	if (args.flags & NFSMNT_NFSV3)
570 		return (EPROGMISMATCH);
571 #endif
572 	if (mp->mnt_flag & MNT_UPDATE) {
573 		struct nfsmount *nmp = VFSTONFS(mp);
574 
575 		if (nmp == NULL)
576 			return (EIO);
577 		/*
578 		 * When doing an update, we can't change from or to
579 		 * v3 and/or nqnfs, or change cookie translation
580 		 */
581 		args.flags = (args.flags &
582 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
583 		    (nmp->nm_flag &
584 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
585 		nfs_decode_args(nmp, &args);
586 		return (0);
587 	}
588 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
589 		return (EINVAL);
590 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
591 	if (error)
592 		return (error);
593 	error = copyinstr(path, pth, MNAMELEN-1, &len);
594 	if (error)
595 		return (error);
596 	memset(&pth[len], 0, MNAMELEN - len);
597 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
598 	if (error)
599 		return (error);
600 	memset(&hst[len], 0, MNAMELEN - len);
601 	/* sockargs() call must be after above copyin() calls */
602 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
603 	if (error)
604 		return (error);
605 	args.fh = nfh;
606 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
607 	return (error);
608 }
609 
610 /*
611  * Common code for mount and mountroot
612  */
613 int
614 mountnfs(argp, mp, nam, pth, hst, vpp, p)
615 	struct nfs_args *argp;
616 	struct mount *mp;
617 	struct mbuf *nam;
618 	const char *pth, *hst;
619 	struct vnode **vpp;
620 	struct proc *p;
621 {
622 	struct nfsmount *nmp;
623 	struct nfsnode *np;
624 	int error;
625 	struct vattr attrs;
626 	struct ucred *cr;
627 
628 	/*
629 	 * If the number of nfs iothreads to use has never
630 	 * been set, create a reasonable number of them.
631 	 */
632 
633 	if (nfs_niothreads < 0) {
634 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
635 		nfs_getset_niothreads(TRUE);
636 	}
637 
638 	if (mp->mnt_flag & MNT_UPDATE) {
639 		nmp = VFSTONFS(mp);
640 		/* update paths, file handles, etc, here	XXX */
641 		m_freem(nam);
642 		return (0);
643 	} else {
644 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
645 		    M_NFSMNT, M_WAITOK);
646 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
647 		mp->mnt_data = (qaddr_t)nmp;
648 		TAILQ_INIT(&nmp->nm_uidlruhead);
649 		TAILQ_INIT(&nmp->nm_bufq);
650 	}
651 	vfs_getnewfsid(mp);
652 	nmp->nm_mountp = mp;
653 
654 #ifndef NFS_V2_ONLY
655 	if (argp->flags & NFSMNT_NQNFS)
656 		/*
657 		 * We have to set mnt_maxsymlink to a non-zero value so
658 		 * that COMPAT_43 routines will know that we are setting
659 		 * the d_type field in directories (and can zero it for
660 		 * unsuspecting binaries).
661 		 */
662 		mp->mnt_maxsymlinklen = 1;
663 #endif
664 
665 #ifndef NFS_V2_ONLY
666 	if ((argp->flags & NFSMNT_NFSV3) == 0)
667 #endif
668 		/*
669 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
670 		 * will fill this in.
671 		 */
672 		nmp->nm_maxfilesize = 0xffffffffLL;
673 
674 	nmp->nm_timeo = NFS_TIMEO;
675 	nmp->nm_retry = NFS_RETRANS;
676 	nmp->nm_wsize = NFS_WSIZE;
677 	nmp->nm_rsize = NFS_RSIZE;
678 	nmp->nm_readdirsize = NFS_READDIRSIZE;
679 	nmp->nm_numgrps = NFS_MAXGRPS;
680 	nmp->nm_readahead = NFS_DEFRAHEAD;
681 	nmp->nm_leaseterm = NQ_DEFLEASE;
682 	nmp->nm_deadthresh = NQ_DEADTHRESH;
683 	CIRCLEQ_INIT(&nmp->nm_timerhead);
684 	nmp->nm_inprog = NULLVP;
685 #ifdef COMPAT_09
686 	mp->mnt_stat.f_type = 2;
687 #else
688 	mp->mnt_stat.f_type = 0;
689 #endif
690 	mp->mnt_fs_bshift = DEV_BSHIFT;
691 	mp->mnt_dev_bshift = -1;
692 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name,
693 	    MFSNAMELEN);
694 	memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
695 	memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
696 	nmp->nm_nam = nam;
697 
698 	/* Set up the sockets and per-host congestion */
699 	nmp->nm_sotype = argp->sotype;
700 	nmp->nm_soproto = argp->proto;
701 
702 	nfs_decode_args(nmp, argp);
703 
704 	/*
705 	 * For Connection based sockets (TCP,...) defer the connect until
706 	 * the first request, in case the server is not responding.
707 	 */
708 	if (nmp->nm_sotype == SOCK_DGRAM &&
709 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
710 		goto bad;
711 
712 	/*
713 	 * This is silly, but it has to be set so that vinifod() works.
714 	 * We do not want to do an nfs_statfs() here since we can get
715 	 * stuck on a dead server and we are holding a lock on the mount
716 	 * point.
717 	 */
718 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
719 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
720 	if (error)
721 		goto bad;
722 	*vpp = NFSTOV(np);
723 	VOP_GETATTR(*vpp, &attrs, p->p_ucred, p);
724 	if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
725 		cr = crget();
726 		cr->cr_uid = attrs.va_uid;
727 		cr->cr_gid = attrs.va_gid;
728 		cr->cr_ngroups = 0;
729 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
730 		crfree(cr);
731 	}
732 
733 	/*
734 	 * A reference count is needed on the nfsnode representing the
735 	 * remote root.  If this object is not persistent, then backward
736 	 * traversals of the mount point (i.e. "..") will not work if
737 	 * the nfsnode gets flushed out of the cache. Ufs does not have
738 	 * this problem, because one can identify root inodes by their
739 	 * number == ROOTINO (2). So, just unlock, but no rele.
740 	 */
741 
742 	nmp->nm_vnode = *vpp;
743 	VOP_UNLOCK(*vpp, 0);
744 
745 	return (0);
746 bad:
747 	nfs_disconnect(nmp);
748 	free((caddr_t)nmp, M_NFSMNT);
749 	m_freem(nam);
750 	return (error);
751 }
752 
753 /*
754  * unmount system call
755  */
756 int
757 nfs_unmount(mp, mntflags, p)
758 	struct mount *mp;
759 	int mntflags;
760 	struct proc *p;
761 {
762 	struct nfsmount *nmp;
763 	struct vnode *vp;
764 	int error, flags = 0;
765 
766 	if (mntflags & MNT_FORCE)
767 		flags |= FORCECLOSE;
768 	nmp = VFSTONFS(mp);
769 	/*
770 	 * Goes something like this..
771 	 * - Check for activity on the root vnode (other than ourselves).
772 	 * - Call vflush() to clear out vnodes for this file system,
773 	 *   except for the root vnode.
774 	 * - Decrement reference on the vnode representing remote root.
775 	 * - Close the socket
776 	 * - Free up the data structures
777 	 */
778 	/*
779 	 * We need to decrement the ref. count on the nfsnode representing
780 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
781 	 * has done vput on this vnode, otherwise we would get deadlock!
782 	 */
783 	vp = nmp->nm_vnode;
784 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
785 	if (error != 0)
786 		return error;
787 
788 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
789 		vput(vp);
790 		return (EBUSY);
791 	}
792 
793 	/*
794 	 * Must handshake with nqnfs_clientd() if it is active.
795 	 */
796 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
797 	while (nmp->nm_inprog != NULLVP)
798 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
799 	error = vflush(mp, vp, flags);
800 	if (error) {
801 		vput(vp);
802 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
803 		return (error);
804 	}
805 
806 	/*
807 	 * We are now committed to the unmount; mark the mount structure
808 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
809 	 * will go away cleanly.
810 	 */
811 	nmp->nm_iflag |= NFSMNT_DISMNT;
812 
813 	/*
814 	 * There are two reference counts to get rid of here
815 	 * (see comment in mountnfs()).
816 	 */
817 	vrele(vp);
818 	vput(vp);
819 	vgone(vp);
820 	nfs_disconnect(nmp);
821 	m_freem(nmp->nm_nam);
822 
823 	/*
824 	 * For NQNFS, let the server daemon free the nfsmount structure.
825 	 */
826 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
827 		free((caddr_t)nmp, M_NFSMNT);
828 	return (0);
829 }
830 
831 /*
832  * Return root of a filesystem
833  */
834 int
835 nfs_root(mp, vpp)
836 	struct mount *mp;
837 	struct vnode **vpp;
838 {
839 	struct vnode *vp;
840 	struct nfsmount *nmp;
841 	int error;
842 
843 	nmp = VFSTONFS(mp);
844 	vp = nmp->nm_vnode;
845 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
846 	if (error != 0)
847 		return error;
848 	if (vp->v_type == VNON)
849 		vp->v_type = VDIR;
850 	vp->v_flag = VROOT;
851 	*vpp = vp;
852 	return (0);
853 }
854 
855 extern int syncprt;
856 
857 /*
858  * Flush out the buffer cache
859  */
860 /* ARGSUSED */
861 int
862 nfs_sync(mp, waitfor, cred, p)
863 	struct mount *mp;
864 	int waitfor;
865 	struct ucred *cred;
866 	struct proc *p;
867 {
868 	struct vnode *vp;
869 	int error, allerror = 0;
870 
871 	/*
872 	 * Force stale buffer cache information to be flushed.
873 	 */
874 loop:
875 	for (vp = mp->mnt_vnodelist.lh_first;
876 	     vp != NULL;
877 	     vp = vp->v_mntvnodes.le_next) {
878 		/*
879 		 * If the vnode that we are about to sync is no longer
880 		 * associated with this mount point, start over.
881 		 */
882 		if (vp->v_mount != mp)
883 			goto loop;
884 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
885 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
886 		     vp->v_uvm.u_obj.uo_npages == 0))
887 			continue;
888 		if (vget(vp, LK_EXCLUSIVE))
889 			goto loop;
890 		error = VOP_FSYNC(vp, cred,
891 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
892 		if (error)
893 			allerror = error;
894 		vput(vp);
895 	}
896 	return (allerror);
897 }
898 
899 /*
900  * NFS flat namespace lookup.
901  * Currently unsupported.
902  */
903 /* ARGSUSED */
904 int
905 nfs_vget(mp, ino, vpp)
906 	struct mount *mp;
907 	ino_t ino;
908 	struct vnode **vpp;
909 {
910 
911 	return (EOPNOTSUPP);
912 }
913 
914 /*
915  * Do that sysctl thang...
916  */
917 int
918 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
919 	int *name;
920 	u_int namelen;
921 	void *oldp;
922 	size_t *oldlenp;
923 	void *newp;
924 	size_t newlen;
925 	struct proc *p;
926 {
927 	int rv;
928 
929 	/*
930 	 * All names at this level are terminal.
931 	 */
932 	if(namelen > 1)
933 		return ENOTDIR;	/* overloaded */
934 
935 	switch(name[0]) {
936 	case NFS_NFSSTATS:
937 		if(!oldp) {
938 			*oldlenp = sizeof nfsstats;
939 			return 0;
940 		}
941 
942 		if(*oldlenp < sizeof nfsstats) {
943 			*oldlenp = sizeof nfsstats;
944 			return ENOMEM;
945 		}
946 
947 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
948 		if(rv) return rv;
949 
950 		if(newp && newlen != sizeof nfsstats)
951 			return EINVAL;
952 
953 		if(newp) {
954 			return copyin(newp, &nfsstats, sizeof nfsstats);
955 		}
956 		return 0;
957 
958 	case NFS_IOTHREADS:
959 		nfs_getset_niothreads(0);
960 
961 		rv = (sysctl_int(oldp, oldlenp, newp, newlen,
962 		    &nfs_niothreads));
963 
964 		if (newp)
965 			nfs_getset_niothreads(1);
966 
967 		return rv;
968 
969 	default:
970 		return EOPNOTSUPP;
971 	}
972 }
973 
974 
975 /*
976  * At this point, this should never happen
977  */
978 /* ARGSUSED */
979 int
980 nfs_fhtovp(mp, fhp, vpp)
981 	struct mount *mp;
982 	struct fid *fhp;
983 	struct vnode **vpp;
984 {
985 
986 	return (EINVAL);
987 }
988 
989 /* ARGSUSED */
990 int
991 nfs_checkexp(mp, nam, exflagsp, credanonp)
992 	struct mount *mp;
993 	struct mbuf *nam;
994 	int *exflagsp;
995 	struct ucred **credanonp;
996 {
997 
998 	return (EINVAL);
999 }
1000 
1001 /*
1002  * Vnode pointer to File handle, should never happen either
1003  */
1004 /* ARGSUSED */
1005 int
1006 nfs_vptofh(vp, fhp)
1007 	struct vnode *vp;
1008 	struct fid *fhp;
1009 {
1010 
1011 	return (EINVAL);
1012 }
1013 
1014 /*
1015  * Vfs start routine, a no-op.
1016  */
1017 /* ARGSUSED */
1018 int
1019 nfs_start(mp, flags, p)
1020 	struct mount *mp;
1021 	int flags;
1022 	struct proc *p;
1023 {
1024 
1025 	return (0);
1026 }
1027 
1028 /*
1029  * Do operations associated with quotas, not supported
1030  */
1031 /* ARGSUSED */
1032 int
1033 nfs_quotactl(mp, cmd, uid, arg, p)
1034 	struct mount *mp;
1035 	int cmd;
1036 	uid_t uid;
1037 	caddr_t arg;
1038 	struct proc *p;
1039 {
1040 
1041 	return (EOPNOTSUPP);
1042 }
1043