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