xref: /original-bsd/sys/nfs/nfs_vfsops.c (revision 898c7514)
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.10 (Berkeley) 05/10/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 int nfs_mountdiskless __P((char *, char *, int, struct sockaddr_in *,
78 	struct nfs_args *, struct vnode **, struct mount **));
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 	struct mount *mp;
229 	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 		printf("nfs_mountroot: socreate(%04x): %d",
267 			nd->myif.ifra_addr.sa_family, error);
268 		return (error);
269 	}
270 
271 	/*
272 	 * We might not have been told the right interface, so we pass
273 	 * over the first ten interfaces of the same kind, until we get
274 	 * one of them configured.
275 	 */
276 
277 	for (i = strlen(nd->myif.ifra_name) - 1;
278 		nd->myif.ifra_name[i] >= '0' &&
279 		nd->myif.ifra_name[i] <= '9';
280 		nd->myif.ifra_name[i] ++) {
281 		error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p);
282 		if(!error)
283 			break;
284 	}
285 	if (error) {
286 		printf("nfs_mountroot: SIOCAIFADDR: %d", error);
287 		return (error);
288 	}
289 	soclose(so);
290 
291 	/*
292 	 * If the gateway field is filled in, set it as the default route.
293 	 */
294 	if (nd->mygateway.sin_len != 0) {
295 		struct sockaddr_in mask, sin;
296 
297 		bzero((caddr_t)&mask, sizeof(mask));
298 		sin = mask;
299 		sin.sin_family = AF_INET;
300 		sin.sin_len = sizeof(sin);
301 		error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
302 		    (struct sockaddr *)&nd->mygateway,
303 		    (struct sockaddr *)&mask,
304 		    RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
305 		if (error) {
306 			printf("nfs_mountroot: RTM_ADD: %d", error);
307 			return (error);
308 		}
309 	}
310 
311 	if (nd->swap_nblks) {
312 		/*
313 		 * Create a fake mount point just for the swap vnode so that the
314 		 * swap file can be on a different server from the rootfs.
315 		 */
316 		nd->swap_args.fh = nd->swap_fh;
317 		/*
318 		 * If using nfsv3_diskless, replace NFSX_V2FH with
319 		 * nd->swap_fhsize.
320 		 */
321 		nd->swap_args.fhsize = NFSX_V2FH;
322 		l = ntohl(nd->swap_saddr.sin_addr.s_addr);
323 		sprintf(buf,"%ld.%ld.%ld.%ld:%s",
324 			(l >> 24) & 0xff, (l >> 16) & 0xff,
325 			(l >>  8) & 0xff, (l >>  0) & 0xff,nd->swap_hostnam);
326 		printf("NFS SWAP: %s\n",buf);
327 		if (error = nfs_mountdiskless(buf, "/swap", 0,
328 		    &nd->swap_saddr, &nd->swap_args, &vp, &mp))
329 			return (error);
330 
331 		for (i=0;swdevt[i].sw_dev != NODEV;i++) ;
332 
333 		/*
334 		 * Since the swap file is not the root dir of a file system,
335 		 * hack it to a regular file.
336 		 */
337 		vp->v_type = VREG;
338 		vp->v_flag = 0;
339 		swapdev_vp = vp;
340 		VREF(vp);
341 		swdevt[i].sw_vp = vp;
342 		swdevt[i].sw_nblks = nd->swap_nblks*2;
343 
344 		if (!swdevt[i].sw_nblks) {
345 			swdevt[i].sw_nblks = 2048;
346 			printf("defaulting to %d kbyte.\n",
347 				swdevt[i].sw_nblks/2);
348 		} else
349 			printf("using %d kbyte.\n",swdevt[i].sw_nblks/2);
350 	}
351 
352 	/*
353 	 * Create the rootfs mount point.
354 	 */
355 	nd->root_args.fh = nd->root_fh;
356 	/*
357 	 * If using nfsv3_diskless, replace NFSX_V2FH with nd->root_fhsize.
358 	 */
359 	nd->root_args.fhsize = NFSX_V2FH;
360 	l = ntohl(nd->swap_saddr.sin_addr.s_addr);
361 	sprintf(buf,"%ld.%ld.%ld.%ld:%s",
362 		(l >> 24) & 0xff, (l >> 16) & 0xff,
363 		(l >>  8) & 0xff, (l >>  0) & 0xff,nd->root_hostnam);
364 	printf("NFS ROOT: %s\n",buf);
365 	if (error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
366 	    &nd->root_saddr, &nd->root_args, &vp, &mp))
367 		return (error);
368 
369 	if (error = vfs_lock(mp)) {
370 		printf("nfs_mountroot: vfs_lock");
371 		return (error);
372 	}
373 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
374 	vfs_unlock(mp);
375 	rootvp = vp;
376 
377 	/*
378 	 * This is not really an nfs issue, but it is much easier to
379 	 * set hostname here and then let the "/etc/rc.xxx" files
380 	 * mount the right /var based upon its preset value.
381 	 */
382 	bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
383 	hostname[MAXHOSTNAMELEN - 1] = '\0';
384 	for (i = 0; i < MAXHOSTNAMELEN; i++)
385 		if (hostname[i] == '\0')
386 			break;
387 	hostnamelen = i;
388 	inittodr(ntohl(nd->root_time));
389 	return (0);
390 }
391 
392 /*
393  * Internal version of mount system call for diskless setup.
394  */
395 static int
396 nfs_mountdiskless(path, which, mountflag, sin, args, vpp, mpp)
397 	char *path;
398 	char *which;
399 	int mountflag;
400 	struct sockaddr_in *sin;
401 	struct nfs_args *args;
402 	struct vnode **vpp;
403 	struct mount **mpp;
404 {
405 	struct mount *mp;
406 	struct mbuf *m;
407 	int error;
408 
409 	if (error = vfs_rootmountalloc("nfs", path, &mp)) {
410 		printf("nfs_mountroot: NFS not configured");
411 		return (error);
412 	}
413 	mp->mnt_flag = mountflag;
414 	MGET(m, MT_SONAME, M_DONTWAIT);
415 	if (m == NULL) {
416 		printf("nfs_mountroot: %s mount mbuf", which);
417 		return (error);
418 	}
419 	bcopy((caddr_t)sin, mtod(m, caddr_t), sin->sin_len);
420 	m->m_len = sin->sin_len;
421 	if (error = mountnfs(args, mp, m, which, path, vpp)) {
422 		printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
423 		return (error);
424 	}
425 	(void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
426 	*mpp = mp;
427 	return (0);
428 }
429 
430 /*
431  * VFS Operations.
432  *
433  * mount system call
434  * It seems a bit dumb to copyinstr() the host and path here and then
435  * bcopy() them in mountnfs(), but I wanted to detect errors before
436  * doing the sockargs() call because sockargs() allocates an mbuf and
437  * an error after that means that I have to release the mbuf.
438  */
439 /* ARGSUSED */
440 int
441 nfs_mount(mp, path, data, ndp, p)
442 	struct mount *mp;
443 	char *path;
444 	caddr_t data;
445 	struct nameidata *ndp;
446 	struct proc *p;
447 {
448 	int error;
449 	struct nfs_args args;
450 	struct mbuf *nam;
451 	struct vnode *vp;
452 	char pth[MNAMELEN], hst[MNAMELEN];
453 	u_int len;
454 	u_char nfh[NFSX_V3FHMAX];
455 
456 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
457 	if (error)
458 		return (error);
459 	if (args.version != NFS_ARGSVERSION)
460 		return (EPROGMISMATCH);
461 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
462 	if (error)
463 		return (error);
464 	error = copyinstr(path, pth, MNAMELEN-1, &len);
465 	if (error)
466 		return (error);
467 	bzero(&pth[len], MNAMELEN - len);
468 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
469 	if (error)
470 		return (error);
471 	bzero(&hst[len], MNAMELEN - len);
472 	/* sockargs() call must be after above copyin() calls */
473 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
474 	if (error)
475 		return (error);
476 	args.fh = nfh;
477 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
478 	return (error);
479 }
480 
481 /*
482  * Common code for mount and mountroot
483  */
484 int
485 mountnfs(argp, mp, nam, pth, hst, vpp)
486 	register struct nfs_args *argp;
487 	register struct mount *mp;
488 	struct mbuf *nam;
489 	char *pth, *hst;
490 	struct vnode **vpp;
491 {
492 	register struct nfsmount *nmp;
493 	struct nfsnode *np;
494 	int error, maxio;
495 
496 	if (mp->mnt_flag & MNT_UPDATE) {
497 		nmp = VFSTONFS(mp);
498 		/* update paths, file handles, etc, here	XXX */
499 		m_freem(nam);
500 		return (0);
501 	} else {
502 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
503 		    M_NFSMNT, M_WAITOK);
504 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
505 		TAILQ_INIT(&nmp->nm_uidlruhead);
506 		mp->mnt_data = (qaddr_t)nmp;
507 	}
508 	vfs_getnewfsid(mp);
509 	nmp->nm_mountp = mp;
510 	nmp->nm_flag = argp->flags;
511 	if (nmp->nm_flag & NFSMNT_NQNFS)
512 		/*
513 		 * We have to set mnt_maxsymlink to a non-zero value so
514 		 * that COMPAT_43 routines will know that we are setting
515 		 * the d_type field in directories (and can zero it for
516 		 * unsuspecting binaries).
517 		 */
518 		mp->mnt_maxsymlinklen = 1;
519 	nmp->nm_timeo = NFS_TIMEO;
520 	nmp->nm_retry = NFS_RETRANS;
521 	nmp->nm_wsize = NFS_WSIZE;
522 	nmp->nm_rsize = NFS_RSIZE;
523 	nmp->nm_readdirsize = NFS_READDIRSIZE;
524 	nmp->nm_numgrps = NFS_MAXGRPS;
525 	nmp->nm_readahead = NFS_DEFRAHEAD;
526 	nmp->nm_leaseterm = NQ_DEFLEASE;
527 	nmp->nm_deadthresh = NQ_DEADTHRESH;
528 	CIRCLEQ_INIT(&nmp->nm_timerhead);
529 	nmp->nm_inprog = NULLVP;
530 	nmp->nm_fhsize = argp->fhsize;
531 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
532 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
533 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
534 	nmp->nm_nam = nam;
535 
536 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
537 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
538 		if (nmp->nm_timeo < NFS_MINTIMEO)
539 			nmp->nm_timeo = NFS_MINTIMEO;
540 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
541 			nmp->nm_timeo = NFS_MAXTIMEO;
542 	}
543 
544 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
545 		nmp->nm_retry = argp->retrans;
546 		if (nmp->nm_retry > NFS_MAXREXMIT)
547 			nmp->nm_retry = NFS_MAXREXMIT;
548 	}
549 
550 	if (argp->flags & NFSMNT_NFSV3) {
551 		if (argp->sotype == SOCK_DGRAM)
552 			maxio = NFS_MAXDGRAMDATA;
553 		else
554 			maxio = NFS_MAXDATA;
555 	} else
556 		maxio = NFS_V2MAXDATA;
557 
558 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
559 		nmp->nm_wsize = argp->wsize;
560 		/* Round down to multiple of blocksize */
561 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
562 		if (nmp->nm_wsize <= 0)
563 			nmp->nm_wsize = NFS_FABLKSIZE;
564 	}
565 	if (nmp->nm_wsize > maxio)
566 		nmp->nm_wsize = maxio;
567 	if (nmp->nm_wsize > MAXBSIZE)
568 		nmp->nm_wsize = MAXBSIZE;
569 
570 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
571 		nmp->nm_rsize = argp->rsize;
572 		/* Round down to multiple of blocksize */
573 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
574 		if (nmp->nm_rsize <= 0)
575 			nmp->nm_rsize = NFS_FABLKSIZE;
576 	}
577 	if (nmp->nm_rsize > maxio)
578 		nmp->nm_rsize = maxio;
579 	if (nmp->nm_rsize > MAXBSIZE)
580 		nmp->nm_rsize = MAXBSIZE;
581 
582 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
583 		nmp->nm_readdirsize = argp->readdirsize;
584 		/* Round down to multiple of blocksize */
585 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
586 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
587 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
588 	}
589 	if (nmp->nm_readdirsize > maxio)
590 		nmp->nm_readdirsize = maxio;
591 
592 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
593 		argp->maxgrouplist <= NFS_MAXGRPS)
594 		nmp->nm_numgrps = argp->maxgrouplist;
595 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
596 		argp->readahead <= NFS_MAXRAHEAD)
597 		nmp->nm_readahead = argp->readahead;
598 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
599 		argp->leaseterm <= NQ_MAXLEASE)
600 		nmp->nm_leaseterm = argp->leaseterm;
601 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
602 		argp->deadthresh <= NQ_NEVERDEAD)
603 		nmp->nm_deadthresh = argp->deadthresh;
604 	/* Set up the sockets and per-host congestion */
605 	nmp->nm_sotype = argp->sotype;
606 	nmp->nm_soproto = argp->proto;
607 
608 	/*
609 	 * For Connection based sockets (TCP,...) defer the connect until
610 	 * the first request, in case the server is not responding.
611 	 */
612 	if (nmp->nm_sotype == SOCK_DGRAM &&
613 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
614 		goto bad;
615 
616 	/*
617 	 * This is silly, but it has to be set so that vinifod() works.
618 	 * We do not want to do an nfs_statfs() here since we can get
619 	 * stuck on a dead server and we are holding a lock on the mount
620 	 * point.
621 	 */
622 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
623 	/*
624 	 * A reference count is needed on the nfsnode representing the
625 	 * remote root.  If this object is not persistent, then backward
626 	 * traversals of the mount point (i.e. "..") will not work if
627 	 * the nfsnode gets flushed out of the cache. Ufs does not have
628 	 * this problem, because one can identify root inodes by their
629 	 * number == ROOTINO (2).
630 	 */
631 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
632 	if (error)
633 		goto bad;
634 	*vpp = NFSTOV(np);
635 
636 	return (0);
637 bad:
638 	nfs_disconnect(nmp);
639 	free((caddr_t)nmp, M_NFSMNT);
640 	m_freem(nam);
641 	return (error);
642 }
643 
644 /*
645  * unmount system call
646  */
647 int
648 nfs_unmount(mp, mntflags, p)
649 	struct mount *mp;
650 	int mntflags;
651 	struct proc *p;
652 {
653 	register struct nfsmount *nmp;
654 	struct nfsnode *np;
655 	struct vnode *vp;
656 	int error, flags = 0;
657 
658 	if (mntflags & MNT_FORCE)
659 		flags |= FORCECLOSE;
660 	nmp = VFSTONFS(mp);
661 	/*
662 	 * Goes something like this..
663 	 * - Check for activity on the root vnode (other than ourselves).
664 	 * - Call vflush() to clear out vnodes for this file system,
665 	 *   except for the root vnode.
666 	 * - Decrement reference on the vnode representing remote root.
667 	 * - Close the socket
668 	 * - Free up the data structures
669 	 */
670 	/*
671 	 * We need to decrement the ref. count on the nfsnode representing
672 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
673 	 * has done vput on this vnode, otherwise we would get deadlock!
674 	 */
675 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
676 	if (error)
677 		return(error);
678 	vp = NFSTOV(np);
679 	if (vp->v_usecount > 2) {
680 		vput(vp);
681 		return (EBUSY);
682 	}
683 
684 	/*
685 	 * Must handshake with nqnfs_clientd() if it is active.
686 	 */
687 	nmp->nm_flag |= NFSMNT_DISMINPROG;
688 	while (nmp->nm_inprog != NULLVP)
689 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
690 	error = vflush(mp, vp, flags);
691 	if (error) {
692 		vput(vp);
693 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
694 		return (error);
695 	}
696 
697 	/*
698 	 * We are now committed to the unmount.
699 	 * For NQNFS, let the server daemon free the nfsmount structure.
700 	 */
701 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
702 		nmp->nm_flag |= NFSMNT_DISMNT;
703 
704 	/*
705 	 * There are two reference counts to get rid of here.
706 	 */
707 	vrele(vp);
708 	vrele(vp);
709 	vgone(vp);
710 	nfs_disconnect(nmp);
711 	m_freem(nmp->nm_nam);
712 
713 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
714 		free((caddr_t)nmp, M_NFSMNT);
715 	return (0);
716 }
717 
718 /*
719  * Return root of a filesystem
720  */
721 int
722 nfs_root(mp, vpp)
723 	struct mount *mp;
724 	struct vnode **vpp;
725 {
726 	register struct vnode *vp;
727 	struct nfsmount *nmp;
728 	struct nfsnode *np;
729 	int error;
730 
731 	nmp = VFSTONFS(mp);
732 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
733 	if (error)
734 		return (error);
735 	vp = NFSTOV(np);
736 	vp->v_type = VDIR;
737 	vp->v_flag = VROOT;
738 	*vpp = vp;
739 	return (0);
740 }
741 
742 extern int syncprt;
743 
744 /*
745  * Flush out the buffer cache
746  */
747 /* ARGSUSED */
748 int
749 nfs_sync(mp, waitfor, cred, p)
750 	struct mount *mp;
751 	int waitfor;
752 	struct ucred *cred;
753 	struct proc *p;
754 {
755 	register struct vnode *vp;
756 	int error, allerror = 0;
757 
758 	/*
759 	 * Force stale buffer cache information to be flushed.
760 	 */
761 loop:
762 	for (vp = mp->mnt_vnodelist.lh_first;
763 	     vp != NULL;
764 	     vp = vp->v_mntvnodes.le_next) {
765 		/*
766 		 * If the vnode that we are about to sync is no longer
767 		 * associated with this mount point, start over.
768 		 */
769 		if (vp->v_mount != mp)
770 			goto loop;
771 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
772 			continue;
773 		if (vget(vp, 1))
774 			goto loop;
775 		error = VOP_FSYNC(vp, cred, waitfor, p);
776 		if (error)
777 			allerror = error;
778 		vput(vp);
779 	}
780 	return (allerror);
781 }
782 
783 /*
784  * NFS flat namespace lookup.
785  * Currently unsupported.
786  */
787 /* ARGSUSED */
788 int
789 nfs_vget(mp, ino, vpp)
790 	struct mount *mp;
791 	ino_t ino;
792 	struct vnode **vpp;
793 {
794 
795 	return (EOPNOTSUPP);
796 }
797 
798 /*
799  * At this point, this should never happen
800  */
801 /* ARGSUSED */
802 int
803 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
804 	register struct mount *mp;
805 	struct fid *fhp;
806 	struct mbuf *nam;
807 	struct vnode **vpp;
808 	int *exflagsp;
809 	struct ucred **credanonp;
810 {
811 
812 	return (EINVAL);
813 }
814 
815 /*
816  * Vnode pointer to File handle, should never happen either
817  */
818 /* ARGSUSED */
819 int
820 nfs_vptofh(vp, fhp)
821 	struct vnode *vp;
822 	struct fid *fhp;
823 {
824 
825 	return (EINVAL);
826 }
827 
828 /*
829  * Vfs start routine, a no-op.
830  */
831 /* ARGSUSED */
832 int
833 nfs_start(mp, flags, p)
834 	struct mount *mp;
835 	int flags;
836 	struct proc *p;
837 {
838 
839 	return (0);
840 }
841 
842 /*
843  * Do operations associated with quotas, not supported
844  */
845 /* ARGSUSED */
846 int
847 nfs_quotactl(mp, cmd, uid, arg, p)
848 	struct mount *mp;
849 	int cmd;
850 	uid_t uid;
851 	caddr_t arg;
852 	struct proc *p;
853 {
854 
855 	return (EOPNOTSUPP);
856 }
857 
858 /*
859  * Do that sysctl thang...
860  */
861 static int
862 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
863 	   size_t newlen, struct proc *p)
864 {
865 	int rv;
866 
867 	/*
868 	 * All names at this level are terminal.
869 	 */
870 	if(namelen > 1)
871 		return ENOTDIR;	/* overloaded */
872 
873 	switch(name[0]) {
874 	case NFS_NFSSTATS:
875 		if(!oldp) {
876 			*oldlenp = sizeof nfsstats;
877 			return 0;
878 		}
879 
880 		if(*oldlenp < sizeof nfsstats) {
881 			*oldlenp = sizeof nfsstats;
882 			return ENOMEM;
883 		}
884 
885 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
886 		if(rv) return rv;
887 
888 		if(newp && newlen != sizeof nfsstats)
889 			return EINVAL;
890 
891 		if(newp) {
892 			return copyin(newp, &nfsstats, sizeof nfsstats);
893 		}
894 		return 0;
895 
896 	default:
897 		return EOPNOTSUPP;
898 	}
899 }
900 
901