xref: /original-bsd/sys/nfs/nfs_vfsops.c (revision 00695d63)
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_vfsops.c	8.6 (Berkeley) 04/12/95
11  */
12 
13 #include <sys/param.h>
14 #include <sys/conf.h>
15 #include <sys/ioctl.h>
16 #include <sys/signal.h>
17 #include <sys/proc.h>
18 #include <sys/namei.h>
19 #include <sys/vnode.h>
20 #include <sys/kernel.h>
21 #include <sys/mount.h>
22 #include <sys/buf.h>
23 #include <sys/mbuf.h>
24 #include <sys/socket.h>
25 #include <sys/socketvar.h>
26 #include <sys/systm.h>
27 
28 #include <net/if.h>
29 #include <net/route.h>
30 #include <netinet/in.h>
31 
32 #include <nfs/rpcv2.h>
33 #include <nfs/nfsproto.h>
34 #include <nfs/nfsnode.h>
35 #include <nfs/nfs.h>
36 #include <nfs/nfsmount.h>
37 #include <nfs/xdr_subs.h>
38 #include <nfs/nfsm_subs.h>
39 #include <nfs/nfsdiskless.h>
40 #include <nfs/nqnfs.h>
41 
42 struct nfsstats nfsstats;
43 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
44 		      struct proc *);
45 extern int nfs_ticks;
46 
47 /*
48  * nfs vfs operations.
49  */
50 struct vfsops nfs_vfsops = {
51 	nfs_mount,
52 	nfs_start,
53 	nfs_unmount,
54 	nfs_root,
55 	nfs_quotactl,
56 	nfs_statfs,
57 	nfs_sync,
58 	nfs_vget,
59 	nfs_fhtovp,
60 	nfs_vptofh,
61 	nfs_init,
62 	nfs_sysctl
63 };
64 
65 /*
66  * This structure must be filled in by a primary bootstrap or bootstrap
67  * server for a diskless/dataless machine. It is initialized below just
68  * to ensure that it is allocated to initialized data (.data not .bss).
69  */
70 struct nfs_diskless nfs_diskless = { 0 };
71 int nfs_diskless_valid = 0;
72 
73 void nfs_disconnect __P((struct nfsmount *));
74 void nfsargs_ntoh __P((struct nfs_args *));
75 int nfs_fsinfo __P((struct nfsmount *, struct vnode *, struct ucred *,
76 	struct proc *));
77 static struct mount *nfs_mountdiskless __P((char *, char *, int,
78     struct sockaddr_in *, struct nfs_args *, register struct vnode **));
79 
80 /*
81  * nfs statfs call
82  */
83 int
84 nfs_statfs(mp, sbp, p)
85 	struct mount *mp;
86 	register struct statfs *sbp;
87 	struct proc *p;
88 {
89 	register struct vnode *vp;
90 	register struct nfs_statfs *sfp;
91 	register caddr_t cp;
92 	register u_long *tl;
93 	register long t1, t2;
94 	caddr_t bpos, dpos, cp2;
95 	struct nfsmount *nmp = VFSTONFS(mp);
96 	int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
97 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
98 	struct ucred *cred;
99 	struct nfsnode *np;
100 	u_quad_t tquad;
101 
102 #ifndef nolint
103 	sfp = (struct nfs_statfs *)0;
104 #endif
105 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
106 	if (error)
107 		return (error);
108 	vp = NFSTOV(np);
109 	cred = crget();
110 	cred->cr_ngroups = 1;
111 	if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
112 		(void)nfs_fsinfo(nmp, vp, cred, p);
113 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
114 	nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
115 	nfsm_fhtom(vp, v3);
116 	nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
117 	if (v3)
118 		nfsm_postop_attr(vp, retattr);
119 	if (!error)
120 		nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
121 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
122 	if (v3) {
123 		sbp->f_bsize = NFS_FABLKSIZE;
124 		fxdr_hyper(&sfp->sf_tbytes, &tquad);
125 		sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
126 		fxdr_hyper(&sfp->sf_fbytes, &tquad);
127 		sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
128 		fxdr_hyper(&sfp->sf_abytes, &tquad);
129 		sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
130 		sbp->f_files = (fxdr_unsigned(long, sfp->sf_tfiles.nfsuquad[1])
131 			& 0x7fffffff);
132 		sbp->f_ffree = (fxdr_unsigned(long, sfp->sf_ffiles.nfsuquad[1])
133 			& 0x7fffffff);
134 	} else {
135 		sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
136 		sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
137 		sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
138 		sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
139 		sbp->f_files = 0;
140 		sbp->f_ffree = 0;
141 	}
142 	if (sbp != &mp->mnt_stat) {
143 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
144 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
145 	}
146 	nfsm_reqdone;
147 	vrele(vp);
148 	crfree(cred);
149 	return (error);
150 }
151 
152 /*
153  * nfs version 3 fsinfo rpc call
154  */
155 int
156 nfs_fsinfo(nmp, vp, cred, p)
157 	register struct nfsmount *nmp;
158 	register struct vnode *vp;
159 	struct ucred *cred;
160 	struct proc *p;
161 {
162 	register struct nfsv3_fsinfo *fsp;
163 	register caddr_t cp;
164 	register long t1, t2;
165 	register u_long *tl, pref, max;
166 	caddr_t bpos, dpos, cp2;
167 	int error = 0, retattr;
168 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
169 
170 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
171 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
172 	nfsm_fhtom(vp, 1);
173 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
174 	nfsm_postop_attr(vp, retattr);
175 	if (!error) {
176 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
177 		pref = fxdr_unsigned(u_long, fsp->fs_wtpref);
178 		if (pref < nmp->nm_wsize)
179 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
180 				~(NFS_FABLKSIZE - 1);
181 		max = fxdr_unsigned(u_long, fsp->fs_wtmax);
182 		if (max < nmp->nm_wsize) {
183 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
184 			if (nmp->nm_wsize == 0)
185 				nmp->nm_wsize = max;
186 		}
187 		pref = fxdr_unsigned(u_long, fsp->fs_rtpref);
188 		if (pref < nmp->nm_rsize)
189 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
190 				~(NFS_FABLKSIZE - 1);
191 		max = fxdr_unsigned(u_long, fsp->fs_rtmax);
192 		if (max < nmp->nm_rsize) {
193 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
194 			if (nmp->nm_rsize == 0)
195 				nmp->nm_rsize = max;
196 		}
197 		pref = fxdr_unsigned(u_long, fsp->fs_dtpref);
198 		if (pref < nmp->nm_readdirsize)
199 			nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
200 				~(NFS_DIRBLKSIZ - 1);
201 		if (max < nmp->nm_readdirsize) {
202 			nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
203 			if (nmp->nm_readdirsize == 0)
204 				nmp->nm_readdirsize = max;
205 		}
206 		nmp->nm_flag |= NFSMNT_GOTFSINFO;
207 	}
208 	nfsm_reqdone;
209 	return (error);
210 }
211 
212 /*
213  * Mount a remote root fs via. nfs. This depends on the info in the
214  * nfs_diskless structure that has been filled in properly by some primary
215  * bootstrap.
216  * It goes something like this:
217  * - do enough of "ifconfig" by calling ifioctl() so that the system
218  *   can talk to the server
219  * - If nfs_diskless.mygateway is filled in, use that address as
220  *   a default gateway.
221  * - hand craft the swap nfs vnode hanging off a fake mount point
222  *	if swdevt[0].sw_dev == NODEV
223  * - build the rootfs mount point and call mountnfs() to do the rest.
224  */
225 int
226 nfs_mountroot()
227 {
228 	register struct mount *mp;
229 	register struct nfs_diskless *nd = &nfs_diskless;
230 	struct socket *so;
231 	struct vnode *vp;
232 	struct proc *p = curproc;		/* XXX */
233 	int error, i;
234 	u_long l;
235 	char buf[128];
236 
237 	/*
238 	 * XXX time must be non-zero when we init the interface or else
239 	 * the arp code will wedge...
240 	 */
241 	if (time.tv_sec == 0)
242 		time.tv_sec = 1;
243 
244 	/*
245 	 * XXX splnet, so networks will receive...
246 	 */
247 	splnet();
248 
249 #ifdef notyet
250 	/* Set up swap credentials. */
251 	proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
252 	proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
253 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
254 		NGROUPS)
255 		proc0.p_ucred->cr_ngroups = NGROUPS;
256 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
257 	    proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
258 #endif
259 
260 	/*
261 	 * Do enough of ifconfig(8) so that the critical net interface can
262 	 * talk to the server.
263 	 */
264 	error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0);
265 	if (error)
266 		panic("nfs_mountroot: socreate(%04x): %d",
267 			nd->myif.ifra_addr.sa_family, error);
268 
269 	/*
270 	 * We might not have been told the right interface, so we pass
271 	 * over the first ten interfaces of the same kind, until we get
272 	 * one of them configured.
273 	 */
274 
275 	for (i = strlen(nd->myif.ifra_name) - 1;
276 		nd->myif.ifra_name[i] >= '0' &&
277 		nd->myif.ifra_name[i] <= '9';
278 		nd->myif.ifra_name[i] ++) {
279 		error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p);
280 		if(!error)
281 			break;
282 	}
283 	if (error)
284 		panic("nfs_mountroot: SIOCAIFADDR: %d", error);
285 	soclose(so);
286 
287 	/*
288 	 * If the gateway field is filled in, set it as the default route.
289 	 */
290 	if (nd->mygateway.sin_len != 0) {
291 		struct sockaddr_in mask, sin;
292 
293 		bzero((caddr_t)&mask, sizeof(mask));
294 		sin = mask;
295 		sin.sin_family = AF_INET;
296 		sin.sin_len = sizeof(sin);
297 		error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
298 		    (struct sockaddr *)&nd->mygateway,
299 		    (struct sockaddr *)&mask,
300 		    RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
301 		if (error)
302 			panic("nfs_mountroot: RTM_ADD: %d", error);
303 	}
304 
305 	if (nd->swap_nblks) {
306 		/*
307 		 * Create a fake mount point just for the swap vnode so that the
308 		 * swap file can be on a different server from the rootfs.
309 		 */
310 		nd->swap_args.fh = nd->swap_fh;
311 		/*
312 		 * If using nfsv3_diskless, replace NFSX_V2FH with
313 		 * nd->swap_fhsize.
314 		 */
315 		nd->swap_args.fhsize = NFSX_V2FH;
316 		l = ntohl(nd->swap_saddr.sin_addr.s_addr);
317 		sprintf(buf,"%ld.%ld.%ld.%ld:%s",
318 			(l >> 24) & 0xff, (l >> 16) & 0xff,
319 			(l >>  8) & 0xff, (l >>  0) & 0xff,nd->swap_hostnam);
320 		printf("NFS SWAP: %s\n",buf);
321 		(void) nfs_mountdiskless(buf, "/swap", 0,
322 		    &nd->swap_saddr, &nd->swap_args, &vp);
323 
324 		for (i=0;swdevt[i].sw_dev != NODEV;i++) ;
325 
326 		/*
327 		 * Since the swap file is not the root dir of a file system,
328 		 * hack it to a regular file.
329 		 */
330 		vp->v_type = VREG;
331 		vp->v_flag = 0;
332 		swapdev_vp = vp;
333 		VREF(vp);
334 		swdevt[i].sw_vp = vp;
335 		swdevt[i].sw_nblks = nd->swap_nblks*2;
336 
337 		if (!swdevt[i].sw_nblks) {
338 			swdevt[i].sw_nblks = 2048;
339 			printf("defaulting to %d kbyte.\n",
340 				swdevt[i].sw_nblks/2);
341 		} else
342 			printf("using %d kbyte.\n",swdevt[i].sw_nblks/2);
343 	}
344 
345 	/*
346 	 * Create the rootfs mount point.
347 	 */
348 	nd->root_args.fh = nd->root_fh;
349 	/*
350 	 * If using nfsv3_diskless, replace NFSX_V2FH with nd->root_fhsize.
351 	 */
352 	nd->root_args.fhsize = NFSX_V2FH;
353 	l = ntohl(nd->swap_saddr.sin_addr.s_addr);
354 	sprintf(buf,"%ld.%ld.%ld.%ld:%s",
355 		(l >> 24) & 0xff, (l >> 16) & 0xff,
356 		(l >>  8) & 0xff, (l >>  0) & 0xff,nd->root_hostnam);
357 	printf("NFS ROOT: %s\n",buf);
358 	mp = nfs_mountdiskless(buf, "/", MNT_RDONLY,
359 	    &nd->root_saddr, &nd->root_args, &vp);
360 
361 	if (vfs_lock(mp))
362 		panic("nfs_mountroot: vfs_lock");
363 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
364 	mp->mnt_flag |= MNT_ROOTFS;
365 	mp->mnt_vnodecovered = NULLVP;
366 	vfs_unlock(mp);
367 	rootvp = vp;
368 
369 	/*
370 	 * This is not really an nfs issue, but it is much easier to
371 	 * set hostname here and then let the "/etc/rc.xxx" files
372 	 * mount the right /var based upon its preset value.
373 	 */
374 	bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
375 	hostname[MAXHOSTNAMELEN - 1] = '\0';
376 	for (i = 0; i < MAXHOSTNAMELEN; i++)
377 		if (hostname[i] == '\0')
378 			break;
379 	hostnamelen = i;
380 	inittodr(ntohl(nd->root_time));
381 	return (0);
382 }
383 
384 /*
385  * Internal version of mount system call for diskless setup.
386  */
387 static struct mount *
388 nfs_mountdiskless(path, which, mountflag, sin, args, vpp)
389 	char *path;
390 	char *which;
391 	int mountflag;
392 	struct sockaddr_in *sin;
393 	struct nfs_args *args;
394 	register struct vnode **vpp;
395 {
396 	register struct mount *mp;
397 	register struct mbuf *m;
398 	register int error;
399 	struct vfsconf *vfsp;
400 
401 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
402 		if (!strcmp(vfsp->vfc_name, "nfs"))
403 			break;
404 	if (vfsp == NULL)
405 		panic("nfs_mountroot: NFS not configured");
406 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
407 	bzero((char *)mp, (u_long)sizeof(struct mount));
408 	mp->mnt_vfc = vfsp;
409 	mp->mnt_op = vfsp->vfc_vfsops;
410 	mp->mnt_flag = mountflag;
411 	MGET(m, MT_SONAME, M_DONTWAIT);
412 	if (m == NULL)
413 		panic("nfs_mountroot: %s mount mbuf", which);
414 	bcopy((caddr_t)sin, mtod(m, caddr_t), sin->sin_len);
415 	m->m_len = sin->sin_len;
416 	if (error = mountnfs(args, mp, m, which, path, vpp))
417 		panic("nfs_mountroot: mount %s on %s: %d", path, which, error);
418 	vfsp->vfc_refcount++;
419 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
420 	mp->mnt_flag |= (vfsp->vfc_flags & MNT_VISFLAGMASK) | MNT_ROOTFS;
421 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
422 
423 	return (mp);
424 }
425 
426 
427 /*
428  * VFS Operations.
429  *
430  * mount system call
431  * It seems a bit dumb to copyinstr() the host and path here and then
432  * bcopy() them in mountnfs(), but I wanted to detect errors before
433  * doing the sockargs() call because sockargs() allocates an mbuf and
434  * an error after that means that I have to release the mbuf.
435  */
436 /* ARGSUSED */
437 int
438 nfs_mount(mp, path, data, ndp, p)
439 	struct mount *mp;
440 	char *path;
441 	caddr_t data;
442 	struct nameidata *ndp;
443 	struct proc *p;
444 {
445 	int error;
446 	struct nfs_args args;
447 	struct mbuf *nam;
448 	struct vnode *vp;
449 	char pth[MNAMELEN], hst[MNAMELEN];
450 	u_int len;
451 	u_char nfh[NFSX_V3FHMAX];
452 
453 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
454 	if (error)
455 		return (error);
456 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
457 	if (error)
458 		return (error);
459 	error = copyinstr(path, pth, MNAMELEN-1, &len);
460 	if (error)
461 		return (error);
462 	bzero(&pth[len], MNAMELEN - len);
463 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
464 	if (error)
465 		return (error);
466 	bzero(&hst[len], MNAMELEN - len);
467 	/* sockargs() call must be after above copyin() calls */
468 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
469 	if (error)
470 		return (error);
471 	args.fh = nfh;
472 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
473 	return (error);
474 }
475 
476 /*
477  * Common code for mount and mountroot
478  */
479 int
480 mountnfs(argp, mp, nam, pth, hst, vpp)
481 	register struct nfs_args *argp;
482 	register struct mount *mp;
483 	struct mbuf *nam;
484 	char *pth, *hst;
485 	struct vnode **vpp;
486 {
487 	register struct nfsmount *nmp;
488 	struct nfsnode *np;
489 	int error, maxio;
490 
491 	if (mp->mnt_flag & MNT_UPDATE) {
492 		nmp = VFSTONFS(mp);
493 		/* update paths, file handles, etc, here	XXX */
494 		m_freem(nam);
495 		return (0);
496 	} else {
497 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
498 		    M_NFSMNT, M_WAITOK);
499 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
500 		TAILQ_INIT(&nmp->nm_uidlruhead);
501 		mp->mnt_data = (qaddr_t)nmp;
502 	}
503 	vfs_getnewfsid(mp);
504 	nmp->nm_mountp = mp;
505 	nmp->nm_flag = argp->flags;
506 	if (nmp->nm_flag & NFSMNT_NQNFS)
507 		/*
508 		 * We have to set mnt_maxsymlink to a non-zero value so
509 		 * that COMPAT_43 routines will know that we are setting
510 		 * the d_type field in directories (and can zero it for
511 		 * unsuspecting binaries).
512 		 */
513 		mp->mnt_maxsymlinklen = 1;
514 	nmp->nm_timeo = NFS_TIMEO;
515 	nmp->nm_retry = NFS_RETRANS;
516 	nmp->nm_wsize = NFS_WSIZE;
517 	nmp->nm_rsize = NFS_RSIZE;
518 	nmp->nm_readdirsize = NFS_READDIRSIZE;
519 	nmp->nm_numgrps = NFS_MAXGRPS;
520 	nmp->nm_readahead = NFS_DEFRAHEAD;
521 	nmp->nm_leaseterm = NQ_DEFLEASE;
522 	nmp->nm_deadthresh = NQ_DEADTHRESH;
523 	CIRCLEQ_INIT(&nmp->nm_timerhead);
524 	nmp->nm_inprog = NULLVP;
525 	nmp->nm_fhsize = argp->fhsize;
526 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
527 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
528 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
529 	nmp->nm_nam = nam;
530 
531 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
532 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
533 		if (nmp->nm_timeo < NFS_MINTIMEO)
534 			nmp->nm_timeo = NFS_MINTIMEO;
535 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
536 			nmp->nm_timeo = NFS_MAXTIMEO;
537 	}
538 
539 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
540 		nmp->nm_retry = argp->retrans;
541 		if (nmp->nm_retry > NFS_MAXREXMIT)
542 			nmp->nm_retry = NFS_MAXREXMIT;
543 	}
544 
545 	if (argp->flags & NFSMNT_NFSV3) {
546 		if (argp->sotype == SOCK_DGRAM)
547 			maxio = NFS_MAXDGRAMDATA;
548 		else
549 			maxio = NFS_MAXDATA;
550 	} else
551 		maxio = NFS_V2MAXDATA;
552 
553 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
554 		nmp->nm_wsize = argp->wsize;
555 		/* Round down to multiple of blocksize */
556 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
557 		if (nmp->nm_wsize <= 0)
558 			nmp->nm_wsize = NFS_FABLKSIZE;
559 	}
560 	if (nmp->nm_wsize > maxio)
561 		nmp->nm_wsize = maxio;
562 	if (nmp->nm_wsize > MAXBSIZE)
563 		nmp->nm_wsize = MAXBSIZE;
564 
565 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
566 		nmp->nm_rsize = argp->rsize;
567 		/* Round down to multiple of blocksize */
568 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
569 		if (nmp->nm_rsize <= 0)
570 			nmp->nm_rsize = NFS_FABLKSIZE;
571 	}
572 	if (nmp->nm_rsize > maxio)
573 		nmp->nm_rsize = maxio;
574 	if (nmp->nm_rsize > MAXBSIZE)
575 		nmp->nm_rsize = MAXBSIZE;
576 
577 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
578 		nmp->nm_readdirsize = argp->readdirsize;
579 		/* Round down to multiple of blocksize */
580 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
581 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
582 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
583 	}
584 	if (nmp->nm_readdirsize > maxio)
585 		nmp->nm_readdirsize = maxio;
586 
587 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
588 		argp->maxgrouplist <= NFS_MAXGRPS)
589 		nmp->nm_numgrps = argp->maxgrouplist;
590 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
591 		argp->readahead <= NFS_MAXRAHEAD)
592 		nmp->nm_readahead = argp->readahead;
593 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
594 		argp->leaseterm <= NQ_MAXLEASE)
595 		nmp->nm_leaseterm = argp->leaseterm;
596 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
597 		argp->deadthresh <= NQ_NEVERDEAD)
598 		nmp->nm_deadthresh = argp->deadthresh;
599 	/* Set up the sockets and per-host congestion */
600 	nmp->nm_sotype = argp->sotype;
601 	nmp->nm_soproto = argp->proto;
602 
603 	/*
604 	 * For Connection based sockets (TCP,...) defer the connect until
605 	 * the first request, in case the server is not responding.
606 	 */
607 	if (nmp->nm_sotype == SOCK_DGRAM &&
608 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
609 		goto bad;
610 
611 	/*
612 	 * This is silly, but it has to be set so that vinifod() works.
613 	 * We do not want to do an nfs_statfs() here since we can get
614 	 * stuck on a dead server and we are holding a lock on the mount
615 	 * point.
616 	 */
617 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
618 	/*
619 	 * A reference count is needed on the nfsnode representing the
620 	 * remote root.  If this object is not persistent, then backward
621 	 * traversals of the mount point (i.e. "..") will not work if
622 	 * the nfsnode gets flushed out of the cache. Ufs does not have
623 	 * this problem, because one can identify root inodes by their
624 	 * number == ROOTINO (2).
625 	 */
626 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
627 	if (error)
628 		goto bad;
629 	*vpp = NFSTOV(np);
630 
631 	return (0);
632 bad:
633 	nfs_disconnect(nmp);
634 	free((caddr_t)nmp, M_NFSMNT);
635 	m_freem(nam);
636 	return (error);
637 }
638 
639 /*
640  * unmount system call
641  */
642 int
643 nfs_unmount(mp, mntflags, p)
644 	struct mount *mp;
645 	int mntflags;
646 	struct proc *p;
647 {
648 	register struct nfsmount *nmp;
649 	struct nfsnode *np;
650 	struct vnode *vp;
651 	int error, flags = 0;
652 	extern int doforce;
653 
654 	if (mntflags & MNT_FORCE) {
655 		if (!doforce)
656 			return (EINVAL);
657 		flags |= FORCECLOSE;
658 	}
659 	nmp = VFSTONFS(mp);
660 	/*
661 	 * Goes something like this..
662 	 * - Check for activity on the root vnode (other than ourselves).
663 	 * - Call vflush() to clear out vnodes for this file system,
664 	 *   except for the root vnode.
665 	 * - Decrement reference on the vnode representing remote root.
666 	 * - Close the socket
667 	 * - Free up the data structures
668 	 */
669 	/*
670 	 * We need to decrement the ref. count on the nfsnode representing
671 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
672 	 * has done vput on this vnode, otherwise we would get deadlock!
673 	 */
674 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
675 	if (error)
676 		return(error);
677 	vp = NFSTOV(np);
678 	if (vp->v_usecount > 2) {
679 		vput(vp);
680 		return (EBUSY);
681 	}
682 
683 	/*
684 	 * Must handshake with nqnfs_clientd() if it is active.
685 	 */
686 	nmp->nm_flag |= NFSMNT_DISMINPROG;
687 	while (nmp->nm_inprog != NULLVP)
688 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
689 	error = vflush(mp, vp, flags);
690 	if (error) {
691 		vput(vp);
692 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
693 		return (error);
694 	}
695 
696 	/*
697 	 * We are now committed to the unmount.
698 	 * For NQNFS, let the server daemon free the nfsmount structure.
699 	 */
700 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
701 		nmp->nm_flag |= NFSMNT_DISMNT;
702 
703 	/*
704 	 * There are two reference counts to get rid of here.
705 	 */
706 	vrele(vp);
707 	vrele(vp);
708 	vgone(vp);
709 	nfs_disconnect(nmp);
710 	m_freem(nmp->nm_nam);
711 
712 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
713 		free((caddr_t)nmp, M_NFSMNT);
714 	return (0);
715 }
716 
717 /*
718  * Return root of a filesystem
719  */
720 int
721 nfs_root(mp, vpp)
722 	struct mount *mp;
723 	struct vnode **vpp;
724 {
725 	register struct vnode *vp;
726 	struct nfsmount *nmp;
727 	struct nfsnode *np;
728 	int error;
729 
730 	nmp = VFSTONFS(mp);
731 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
732 	if (error)
733 		return (error);
734 	vp = NFSTOV(np);
735 	vp->v_type = VDIR;
736 	vp->v_flag = VROOT;
737 	*vpp = vp;
738 	return (0);
739 }
740 
741 extern int syncprt;
742 
743 /*
744  * Flush out the buffer cache
745  */
746 /* ARGSUSED */
747 int
748 nfs_sync(mp, waitfor, cred, p)
749 	struct mount *mp;
750 	int waitfor;
751 	struct ucred *cred;
752 	struct proc *p;
753 {
754 	register struct vnode *vp;
755 	int error, allerror = 0;
756 
757 	/*
758 	 * Force stale buffer cache information to be flushed.
759 	 */
760 loop:
761 	for (vp = mp->mnt_vnodelist.lh_first;
762 	     vp != NULL;
763 	     vp = vp->v_mntvnodes.le_next) {
764 		/*
765 		 * If the vnode that we are about to sync is no longer
766 		 * associated with this mount point, start over.
767 		 */
768 		if (vp->v_mount != mp)
769 			goto loop;
770 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
771 			continue;
772 		if (vget(vp, 1))
773 			goto loop;
774 		error = VOP_FSYNC(vp, cred, waitfor, p);
775 		if (error)
776 			allerror = error;
777 		vput(vp);
778 	}
779 	return (allerror);
780 }
781 
782 /*
783  * NFS flat namespace lookup.
784  * Currently unsupported.
785  */
786 /* ARGSUSED */
787 int
788 nfs_vget(mp, ino, vpp)
789 	struct mount *mp;
790 	ino_t ino;
791 	struct vnode **vpp;
792 {
793 
794 	return (EOPNOTSUPP);
795 }
796 
797 /*
798  * At this point, this should never happen
799  */
800 /* ARGSUSED */
801 int
802 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
803 	register struct mount *mp;
804 	struct fid *fhp;
805 	struct mbuf *nam;
806 	struct vnode **vpp;
807 	int *exflagsp;
808 	struct ucred **credanonp;
809 {
810 
811 	return (EINVAL);
812 }
813 
814 /*
815  * Vnode pointer to File handle, should never happen either
816  */
817 /* ARGSUSED */
818 int
819 nfs_vptofh(vp, fhp)
820 	struct vnode *vp;
821 	struct fid *fhp;
822 {
823 
824 	return (EINVAL);
825 }
826 
827 /*
828  * Vfs start routine, a no-op.
829  */
830 /* ARGSUSED */
831 int
832 nfs_start(mp, flags, p)
833 	struct mount *mp;
834 	int flags;
835 	struct proc *p;
836 {
837 
838 	return (0);
839 }
840 
841 /*
842  * Do operations associated with quotas, not supported
843  */
844 /* ARGSUSED */
845 int
846 nfs_quotactl(mp, cmd, uid, arg, p)
847 	struct mount *mp;
848 	int cmd;
849 	uid_t uid;
850 	caddr_t arg;
851 	struct proc *p;
852 {
853 
854 	return (EOPNOTSUPP);
855 }
856 
857 /*
858  * Do that sysctl thang...
859  */
860 static int
861 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
862 	   size_t newlen, struct proc *p)
863 {
864 	int rv;
865 
866 	/*
867 	 * All names at this level are terminal.
868 	 */
869 	if(namelen > 1)
870 		return ENOTDIR;	/* overloaded */
871 
872 	switch(name[0]) {
873 	case NFS_NFSSTATS:
874 		if(!oldp) {
875 			*oldlenp = sizeof nfsstats;
876 			return 0;
877 		}
878 
879 		if(*oldlenp < sizeof nfsstats) {
880 			*oldlenp = sizeof nfsstats;
881 			return ENOMEM;
882 		}
883 
884 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
885 		if(rv) return rv;
886 
887 		if(newp && newlen != sizeof nfsstats)
888 			return EINVAL;
889 
890 		if(newp) {
891 			return copyin(newp, &nfsstats, sizeof nfsstats);
892 		}
893 		return 0;
894 
895 	default:
896 		return EOPNOTSUPP;
897 	}
898 }
899 
900