xref: /netbsd-src/sys/nfs/nfs_vfsops.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: nfs_vfsops.c,v 1.52 1996/10/20 13:13:26 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 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/ioctl.h>
44 #include <sys/signal.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/buf.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.h>
55 
56 #include <net/if.h>
57 #include <net/route.h>
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/nqnfs.h>
69 #include <nfs/nfs_var.h>
70 
71 struct nfsstats nfsstats;
72 extern int nfs_ticks;
73 
74 #ifdef notyet
75 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
76 		      struct proc *);
77 #endif
78 
79 /*
80  * nfs vfs operations.
81  */
82 struct vfsops nfs_vfsops = {
83 	MOUNT_NFS,
84 	nfs_mount,
85 	nfs_start,
86 	nfs_unmount,
87 	nfs_root,
88 	nfs_quotactl,
89 	nfs_statfs,
90 	nfs_sync,
91 	nfs_vget,
92 	nfs_fhtovp,
93 	nfs_vptofh,
94 	nfs_init,
95 #ifdef notyet
96 	nfs_sysctl
97 #endif
98 };
99 
100 extern u_int32_t nfs_procids[NFS_NPROCS];
101 extern u_int32_t nfs_prog, nfs_vers;
102 
103 static struct mount *
104 nfs_mount_diskless __P((struct nfs_dlmount *, char *, int, struct vnode **));
105 
106 #define TRUE	1
107 #define	FALSE	0
108 
109 /*
110  * nfs statfs call
111  */
112 int
113 nfs_statfs(mp, sbp, p)
114 	struct mount *mp;
115 	register struct statfs *sbp;
116 	struct proc *p;
117 {
118 	register struct vnode *vp;
119 	register struct nfs_statfs *sfp;
120 	register caddr_t cp;
121 	register u_int32_t *tl;
122 	register int32_t t1, t2;
123 	caddr_t bpos, dpos, cp2;
124 	struct nfsmount *nmp = VFSTONFS(mp);
125 	int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
126 	struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
127 	struct ucred *cred;
128 	struct nfsnode *np;
129 	u_quad_t tquad;
130 
131 #ifndef nolint
132 	sfp = (struct nfs_statfs *)0;
133 #endif
134 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
135 	if (error)
136 		return (error);
137 	vp = NFSTOV(np);
138 	cred = crget();
139 	cred->cr_ngroups = 0;
140 	if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
141 		(void)nfs_fsinfo(nmp, vp, cred, p);
142 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
143 	nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
144 	nfsm_fhtom(vp, v3);
145 	nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
146 	if (v3)
147 		nfsm_postop_attr(vp, retattr);
148 	if (error) {
149 		if (mrep != NULL)
150 			m_free(mrep);
151 		goto nfsmout;
152 	}
153 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
154 #ifdef COMPAT_09
155 	sbp->f_type = 2;
156 #else
157 	sbp->f_type = 0;
158 #endif
159 	sbp->f_flags = nmp->nm_flag;
160 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
161 	if (v3) {
162 		sbp->f_bsize = NFS_FABLKSIZE;
163 		fxdr_hyper(&sfp->sf_tbytes, &tquad);
164 		sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
165 		fxdr_hyper(&sfp->sf_fbytes, &tquad);
166 		sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
167 		fxdr_hyper(&sfp->sf_abytes, &tquad);
168 		sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
169 		sbp->f_files = (fxdr_unsigned(int32_t,
170 		    sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
171 		sbp->f_ffree = (fxdr_unsigned(int32_t,
172 		    sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
173 	} else {
174 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
175 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
176 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
177 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
178 		sbp->f_files = 0;
179 		sbp->f_ffree = 0;
180 	}
181 	if (sbp != &mp->mnt_stat) {
182 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
183 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
184 	}
185 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
186 	nfsm_reqdone;
187 	vrele(vp);
188 	crfree(cred);
189 	return (error);
190 }
191 
192 /*
193  * nfs version 3 fsinfo rpc call
194  */
195 int
196 nfs_fsinfo(nmp, vp, cred, p)
197 	register struct nfsmount *nmp;
198 	register struct vnode *vp;
199 	struct ucred *cred;
200 	struct proc *p;
201 {
202 	register struct nfsv3_fsinfo *fsp;
203 	register caddr_t cp;
204 	register int32_t t1, t2;
205 	register u_int32_t *tl, pref, max;
206 	caddr_t bpos, dpos, cp2;
207 	int error = 0, retattr;
208 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
209 
210 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
211 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
212 	nfsm_fhtom(vp, 1);
213 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
214 	nfsm_postop_attr(vp, retattr);
215 	if (!error) {
216 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
217 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
218 		if (pref < nmp->nm_wsize)
219 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
220 				~(NFS_FABLKSIZE - 1);
221 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
222 		if (max < nmp->nm_wsize) {
223 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
224 			if (nmp->nm_wsize == 0)
225 				nmp->nm_wsize = max;
226 		}
227 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
228 		if (pref < nmp->nm_rsize)
229 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
230 				~(NFS_FABLKSIZE - 1);
231 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
232 		if (max < nmp->nm_rsize) {
233 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
234 			if (nmp->nm_rsize == 0)
235 				nmp->nm_rsize = max;
236 		}
237 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
238 		if (pref < nmp->nm_readdirsize)
239 			nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
240 				~(NFS_DIRBLKSIZ - 1);
241 		if (max < nmp->nm_readdirsize) {
242 			nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
243 			if (nmp->nm_readdirsize == 0)
244 				nmp->nm_readdirsize = max;
245 		}
246 		nmp->nm_flag |= NFSMNT_GOTFSINFO;
247 	}
248 	nfsm_reqdone;
249 	return (error);
250 }
251 
252 /*
253  * Mount a remote root fs via. NFS.  It goes like this:
254  * - Call nfs_boot_init() to fill in the nfs_diskless struct
255  *   (using RARP, bootparam RPC, mountd RPC)
256  * - hand craft the swap nfs vnode hanging off a fake mount point
257  *	if swdevt[0].sw_dev == NODEV
258  * - build the rootfs mount point and call mountnfs() to do the rest.
259  */
260 int
261 nfs_mountroot()
262 {
263 	struct nfs_diskless nd;
264 	struct vattr attr;
265 	struct mount *mp;
266 	struct vnode *vp;
267 	struct proc *procp;
268 	long n;
269 	int error;
270 
271 	procp = curproc; /* XXX */
272 
273 	/*
274 	 * XXX time must be non-zero when we init the interface or else
275 	 * the arp code will wedge.  [Fixed now in if_ether.c]
276 	 * However, the NFS attribute cache gives false "hits" when
277 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
278 	 */
279 	if (time.tv_sec < NFS_MAXATTRTIMO)
280 		time.tv_sec = NFS_MAXATTRTIMO;
281 
282 	/*
283 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
284 	 * Side effect:  Finds and configures a network interface.
285 	 */
286 	bzero((caddr_t) &nd, sizeof(nd));
287 	nfs_boot_init(&nd, procp);
288 
289 	/*
290 	 * Create the root mount point.
291 	 */
292 	nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root);
293 	mp = nfs_mount_diskless(&nd.nd_root, "/", 0, &vp);
294 	printf("root on %s\n", nd.nd_root.ndm_host);
295 
296 	/*
297 	 * Link it into the mount list.
298 	 */
299 #ifdef Lite2_integrated
300 	simple_lock(&mountlist_slock);
301 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
302 	simple_unlock(&mountlist_slock);
303 	rootvp = vp;
304 	vfs_unbusy(mp, procp);
305 #else
306 	if (vfs_lock(mp))
307 		panic("nfs_mountroot: vfs_lock");
308 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
309 	mp->mnt_vnodecovered = NULLVP;
310 	vfs_unlock(mp);
311 	rootvp = vp;
312 #endif
313 
314 	/* Get root attributes (for the time). */
315 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
316 	if (error) panic("nfs_mountroot: getattr for root");
317 	n = attr.va_mtime.tv_sec;
318 #ifdef	DEBUG
319 	printf("root time: 0x%lx\n", n);
320 #endif
321 	inittodr(n);
322 
323 	/*
324 	 * XXX splnet, so networks will receive...
325 	 */
326 	splnet();
327 
328 #ifdef notyet
329 	/* Set up swap credentials. */
330 	proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
331 	proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
332 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
333 		NGROUPS)
334 		proc0.p_ucred->cr_ngroups = NGROUPS;
335 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
336 	    proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
337 #endif
338 
339 	/*
340 	 * "Mount" the swap device.
341 	 *
342 	 * On a "dataless" configuration (swap on disk) we will have:
343 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
344 	 */
345 	if (bdevvp(swapdev, &swapdev_vp))
346 		panic("nfs_mountroot: can't setup swap vp");
347 	if (swdevt[0].sw_dev != NODEV) {
348 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
349 		return (0);
350 	}
351 
352 	/*
353 	 * If swapping to an nfs node:  (swdevt[0].sw_dev == NODEV)
354 	 * Create a fake mount point just for the swap vnode so that the
355 	 * swap file can be on a different server from the rootfs.
356 	 */
357 	nfs_boot_getfh(&nd.nd_boot, "swap", &nd.nd_swap);
358 	mp = nfs_mount_diskless(&nd.nd_swap, "/swap", 0, &vp);
359 #ifdef Lite2_integrated
360 	vfs_unbusy(mp, procp);
361 #endif
362 	printf("swap on %s\n", nd.nd_swap.ndm_host);
363 
364 	/*
365 	 * Since the swap file is not the root dir of a file system,
366 	 * hack it to a regular file.
367 	 */
368 	vp->v_type = VREG;
369 	vp->v_flag = 0;
370 	swdevt[0].sw_vp = vp;
371 
372 	/*
373 	 * Find out how large the swap file is.
374 	 */
375 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
376 	if (error)
377 		panic("nfs_mountroot: getattr for swap");
378 	n = (long) (attr.va_size >> DEV_BSHIFT);
379 #ifdef	DEBUG
380 	printf("swap size: 0x%lx (blocks)\n", n);
381 #endif
382 	swdevt[0].sw_nblks = n;
383 
384 	return (0);
385 }
386 
387 /*
388  * Internal version of mount system call for diskless setup.
389  */
390 static struct mount *
391 nfs_mount_diskless(ndmntp, mntname, mntflag, vpp)
392 	struct nfs_dlmount *ndmntp;
393 	char *mntname;
394 	int mntflag;
395 	struct vnode **vpp;
396 {
397 	struct mount *mp;
398 	struct mbuf *m;
399 	int error;
400 
401 #ifdef Lite2_integrated
402 	vfs_rootmountalloc("nfs", mntname, &mp);
403 #else
404 	/* Create the mount point. */
405 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
406 	    M_MOUNT, M_WAITOK);
407 	bzero((char *)mp, (u_long)sizeof(struct mount));
408 #endif
409 	mp->mnt_op = &nfs_vfsops;
410 	mp->mnt_flag = mntflag;
411 
412 	/* Get mbuf for server sockaddr. */
413 	m = m_get(M_WAIT, MT_SONAME);
414 	if (m == NULL)
415 		panic("nfs_mountroot: mget soname for %s", mntname);
416 	bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t),
417 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
418 
419 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
420 			 ndmntp->ndm_args.hostname, vpp);
421 	if (error)
422 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
423 
424 	return (mp);
425 }
426 
427 void
428 nfs_decode_args(nmp, argp)
429 	struct nfsmount *nmp;
430 	struct nfs_args *argp;
431 {
432 	int s;
433 	int adjsock;
434 	int maxio;
435 
436 	s = splsoftnet();
437 
438 	/* Re-bind if rsrvd port requested and wasn't on one */
439 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
440 		  && (argp->flags & NFSMNT_RESVPORT);
441 
442 	/* Update flags atomically.  Don't change the lock bits. */
443 	nmp->nm_flag =
444 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
445 	splx(s);
446 
447 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
448 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
449 		if (nmp->nm_timeo < NFS_MINTIMEO)
450 			nmp->nm_timeo = NFS_MINTIMEO;
451 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
452 			nmp->nm_timeo = NFS_MAXTIMEO;
453 	}
454 
455 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
456 		nmp->nm_retry = argp->retrans;
457 		if (nmp->nm_retry > NFS_MAXREXMIT)
458 			nmp->nm_retry = NFS_MAXREXMIT;
459 	}
460 
461 	if (argp->flags & NFSMNT_NFSV3) {
462 		if (argp->sotype == SOCK_DGRAM)
463 			maxio = NFS_MAXDGRAMDATA;
464 		else
465 			maxio = NFS_MAXDATA;
466 	} else
467 		maxio = NFS_V2MAXDATA;
468 
469 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
470 		int osize = nmp->nm_wsize;
471 		nmp->nm_wsize = argp->wsize;
472 		/* Round down to multiple of blocksize */
473 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
474 		if (nmp->nm_wsize <= 0)
475 			nmp->nm_wsize = NFS_FABLKSIZE;
476 		adjsock |= (nmp->nm_wsize != osize);
477 	}
478 	if (nmp->nm_wsize > maxio)
479 		nmp->nm_wsize = maxio;
480 	if (nmp->nm_wsize > MAXBSIZE)
481 		nmp->nm_wsize = MAXBSIZE;
482 
483 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
484 		int osize = nmp->nm_rsize;
485 		nmp->nm_rsize = argp->rsize;
486 		/* Round down to multiple of blocksize */
487 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
488 		if (nmp->nm_rsize <= 0)
489 			nmp->nm_rsize = NFS_FABLKSIZE;
490 		adjsock |= (nmp->nm_rsize != osize);
491 	}
492 	if (nmp->nm_rsize > maxio)
493 		nmp->nm_rsize = maxio;
494 	if (nmp->nm_rsize > MAXBSIZE)
495 		nmp->nm_rsize = MAXBSIZE;
496 
497 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
498 		nmp->nm_readdirsize = argp->readdirsize;
499 		/* Round down to multiple of blocksize */
500 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
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_disconnect(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, "nfscon", 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  * bcopy() 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 	char *path;
552 	caddr_t data;
553 	struct nameidata *ndp;
554 	struct proc *p;
555 {
556 	int error;
557 	struct nfs_args args;
558 	struct mbuf *nam;
559 	struct vnode *vp;
560 	char pth[MNAMELEN], hst[MNAMELEN];
561 	size_t len;
562 	u_char nfh[NFSX_V3FHMAX];
563 
564 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
565 	if (error)
566 		return (error);
567 	if (args.version != NFS_ARGSVERSION)
568 		return (EPROGMISMATCH);
569 	if (mp->mnt_flag & MNT_UPDATE) {
570 		register 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.
577 		 */
578 		args.flags = (args.flags & ~(NFSMNT_NFSV3|NFSMNT_NQNFS)) |
579 		    (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_NQNFS));
580 		nfs_decode_args(nmp, &args);
581 		return (0);
582 	}
583 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
584 	if (error)
585 		return (error);
586 	error = copyinstr(path, pth, MNAMELEN-1, &len);
587 	if (error)
588 		return (error);
589 	bzero(&pth[len], MNAMELEN - len);
590 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
591 	if (error)
592 		return (error);
593 	bzero(&hst[len], MNAMELEN - len);
594 	/* sockargs() call must be after above copyin() calls */
595 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
596 	if (error)
597 		return (error);
598 	args.fh = nfh;
599 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
600 	return (error);
601 }
602 
603 /*
604  * Common code for mount and mountroot
605  */
606 int
607 mountnfs(argp, mp, nam, pth, hst, vpp)
608 	register struct nfs_args *argp;
609 	register struct mount *mp;
610 	struct mbuf *nam;
611 	char *pth, *hst;
612 	struct vnode **vpp;
613 {
614 	register struct nfsmount *nmp;
615 	struct nfsnode *np;
616 	int error;
617 	struct vattr attrs;
618 
619 	if (mp->mnt_flag & MNT_UPDATE) {
620 		nmp = VFSTONFS(mp);
621 		/* update paths, file handles, etc, here	XXX */
622 		m_freem(nam);
623 		return (0);
624 	} else {
625 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
626 		    M_NFSMNT, M_WAITOK);
627 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
628 		mp->mnt_data = (qaddr_t)nmp;
629 		TAILQ_INIT(&nmp->nm_uidlruhead);
630 	}
631 #ifdef Lite2_integrated
632 	vfs_getnewfsid(mp, makefstype(MOUNT_NFS));
633 #else
634 	getnewfsid(mp, makefstype(MOUNT_NFS));
635 #endif
636 	nmp->nm_mountp = mp;
637 	if (argp->flags & NFSMNT_NQNFS)
638 		/*
639 		 * We have to set mnt_maxsymlink to a non-zero value so
640 		 * that COMPAT_43 routines will know that we are setting
641 		 * the d_type field in directories (and can zero it for
642 		 * unsuspecting binaries).
643 		 */
644 		mp->mnt_maxsymlinklen = 1;
645 	nmp->nm_timeo = NFS_TIMEO;
646 	nmp->nm_retry = NFS_RETRANS;
647 	nmp->nm_wsize = NFS_WSIZE;
648 	nmp->nm_rsize = NFS_RSIZE;
649 	nmp->nm_readdirsize = NFS_READDIRSIZE;
650 	nmp->nm_numgrps = NFS_MAXGRPS;
651 	nmp->nm_readahead = NFS_DEFRAHEAD;
652 	nmp->nm_leaseterm = NQ_DEFLEASE;
653 	nmp->nm_deadthresh = NQ_DEADTHRESH;
654 	CIRCLEQ_INIT(&nmp->nm_timerhead);
655 	nmp->nm_inprog = NULLVP;
656 	nmp->nm_fhsize = argp->fhsize;
657 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
658 #ifdef COMPAT_09
659 	mp->mnt_stat.f_type = 2;
660 #else
661 	mp->mnt_stat.f_type = 0;
662 #endif
663 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
664 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
665 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
666 	nmp->nm_nam = nam;
667 
668 	/* Set up the sockets and per-host congestion */
669 	nmp->nm_sotype = argp->sotype;
670 	nmp->nm_soproto = argp->proto;
671 
672 	nfs_decode_args(nmp, argp);
673 
674 	/*
675 	 * For Connection based sockets (TCP,...) defer the connect until
676 	 * the first request, in case the server is not responding.
677 	 */
678 	if (nmp->nm_sotype == SOCK_DGRAM &&
679 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
680 		goto bad;
681 
682 	/*
683 	 * This is silly, but it has to be set so that vinifod() works.
684 	 * We do not want to do an nfs_statfs() here since we can get
685 	 * stuck on a dead server and we are holding a lock on the mount
686 	 * point.
687 	 */
688 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
689 	/*
690 	 * A reference count is needed on the nfsnode representing the
691 	 * remote root.  If this object is not persistent, then backward
692 	 * traversals of the mount point (i.e. "..") will not work if
693 	 * the nfsnode gets flushed out of the cache. Ufs does not have
694 	 * this problem, because one can identify root inodes by their
695 	 * number == ROOTINO (2).
696 	 */
697 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
698 	if (error)
699 		goto bad;
700 	*vpp = NFSTOV(np);
701 	VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc); /* XXX */
702 
703 	return (0);
704 bad:
705 	nfs_disconnect(nmp);
706 	free((caddr_t)nmp, M_NFSMNT);
707 	m_freem(nam);
708 	return (error);
709 }
710 
711 /*
712  * unmount system call
713  */
714 int
715 nfs_unmount(mp, mntflags, p)
716 	struct mount *mp;
717 	int mntflags;
718 	struct proc *p;
719 {
720 	register struct nfsmount *nmp;
721 	struct nfsnode *np;
722 	struct vnode *vp;
723 	int error, flags = 0;
724 
725 	if (mntflags & MNT_FORCE)
726 		flags |= FORCECLOSE;
727 	nmp = VFSTONFS(mp);
728 	/*
729 	 * Goes something like this..
730 	 * - Check for activity on the root vnode (other than ourselves).
731 	 * - Call vflush() to clear out vnodes for this file system,
732 	 *   except for the root vnode.
733 	 * - Decrement reference on the vnode representing remote root.
734 	 * - Close the socket
735 	 * - Free up the data structures
736 	 */
737 	/*
738 	 * We need to decrement the ref. count on the nfsnode representing
739 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
740 	 * has done vput on this vnode, otherwise we would get deadlock!
741 	 */
742 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
743 	if (error)
744 		return(error);
745 	vp = NFSTOV(np);
746 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
747 		vput(vp);
748 		return (EBUSY);
749 	}
750 
751 	/*
752 	 * Must handshake with nqnfs_clientd() if it is active.
753 	 */
754 	nmp->nm_flag |= NFSMNT_DISMINPROG;
755 	while (nmp->nm_inprog != NULLVP)
756 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
757 	error = vflush(mp, vp, flags);
758 	if (error) {
759 		vput(vp);
760 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
761 		return (error);
762 	}
763 
764 	/*
765 	 * We are now committed to the unmount.
766 	 * For NQNFS, let the server daemon free the nfsmount structure.
767 	 */
768 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
769 		nmp->nm_flag |= NFSMNT_DISMNT;
770 
771 	/*
772 	 * There are two reference counts to get rid of here.
773 	 */
774 	vrele(vp);
775 	vrele(vp);
776 	vgone(vp);
777 	nfs_disconnect(nmp);
778 	m_freem(nmp->nm_nam);
779 
780 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
781 		free((caddr_t)nmp, M_NFSMNT);
782 	return (0);
783 }
784 
785 /*
786  * Return root of a filesystem
787  */
788 int
789 nfs_root(mp, vpp)
790 	struct mount *mp;
791 	struct vnode **vpp;
792 {
793 	register struct vnode *vp;
794 	struct nfsmount *nmp;
795 	struct nfsnode *np;
796 	int error;
797 
798 	nmp = VFSTONFS(mp);
799 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
800 	if (error)
801 		return (error);
802 	vp = NFSTOV(np);
803 	if (vp->v_type == VNON)
804 		vp->v_type = VDIR;
805 	vp->v_flag = VROOT;
806 	*vpp = vp;
807 	return (0);
808 }
809 
810 extern int syncprt;
811 
812 /*
813  * Flush out the buffer cache
814  */
815 /* ARGSUSED */
816 int
817 nfs_sync(mp, waitfor, cred, p)
818 	struct mount *mp;
819 	int waitfor;
820 	struct ucred *cred;
821 	struct proc *p;
822 {
823 	register struct vnode *vp;
824 	int error, allerror = 0;
825 
826 	/*
827 	 * Force stale buffer cache information to be flushed.
828 	 */
829 loop:
830 	for (vp = mp->mnt_vnodelist.lh_first;
831 	     vp != NULL;
832 	     vp = vp->v_mntvnodes.le_next) {
833 		/*
834 		 * If the vnode that we are about to sync is no longer
835 		 * associated with this mount point, start over.
836 		 */
837 		if (vp->v_mount != mp)
838 			goto loop;
839 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
840 			continue;
841 #ifdef Lite2_integrated
842 		if (vget(vp, LK_EXCLUSIVE, p))
843 #else
844 		if (vget(vp, 1))
845 #endif
846 			goto loop;
847 		error = VOP_FSYNC(vp, cred, waitfor, p);
848 		if (error)
849 			allerror = error;
850 		vput(vp);
851 	}
852 	return (allerror);
853 }
854 
855 /*
856  * NFS flat namespace lookup.
857  * Currently unsupported.
858  */
859 /* ARGSUSED */
860 int
861 nfs_vget(mp, ino, vpp)
862 	struct mount *mp;
863 	ino_t ino;
864 	struct vnode **vpp;
865 {
866 
867 	return (EOPNOTSUPP);
868 }
869 
870 #ifdef notyet
871 /*
872  * Do that sysctl thang...
873  */
874 static int
875 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
876 	   size_t newlen, struct proc *p)
877 {
878 	int rv;
879 
880 	/*
881 	 * All names at this level are terminal.
882 	 */
883 	if(namelen > 1)
884 		return ENOTDIR;	/* overloaded */
885 
886 	switch(name[0]) {
887 	case NFS_NFSSTATS:
888 		if(!oldp) {
889 			*oldlenp = sizeof nfsstats;
890 			return 0;
891 		}
892 
893 		if(*oldlenp < sizeof nfsstats) {
894 			*oldlenp = sizeof nfsstats;
895 			return ENOMEM;
896 		}
897 
898 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
899 		if(rv) return rv;
900 
901 		if(newp && newlen != sizeof nfsstats)
902 			return EINVAL;
903 
904 		if(newp) {
905 			return copyin(newp, &nfsstats, sizeof nfsstats);
906 		}
907 		return 0;
908 
909 	default:
910 		return EOPNOTSUPP;
911 	}
912 }
913 #endif
914 
915 
916 /*
917  * At this point, this should never happen
918  */
919 /* ARGSUSED */
920 int
921 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
922 	register struct mount *mp;
923 	struct fid *fhp;
924 	struct mbuf *nam;
925 	struct vnode **vpp;
926 	int *exflagsp;
927 	struct ucred **credanonp;
928 {
929 
930 	return (EINVAL);
931 }
932 
933 /*
934  * Vnode pointer to File handle, should never happen either
935  */
936 /* ARGSUSED */
937 int
938 nfs_vptofh(vp, fhp)
939 	struct vnode *vp;
940 	struct fid *fhp;
941 {
942 
943 	return (EINVAL);
944 }
945 
946 /*
947  * Vfs start routine, a no-op.
948  */
949 /* ARGSUSED */
950 int
951 nfs_start(mp, flags, p)
952 	struct mount *mp;
953 	int flags;
954 	struct proc *p;
955 {
956 
957 	return (0);
958 }
959 
960 /*
961  * Do operations associated with quotas, not supported
962  */
963 /* ARGSUSED */
964 int
965 nfs_quotactl(mp, cmd, uid, arg, p)
966 	struct mount *mp;
967 	int cmd;
968 	uid_t uid;
969 	caddr_t arg;
970 	struct proc *p;
971 {
972 
973 	return (EOPNOTSUPP);
974 }
975