xref: /original-bsd/sys/nfs/nfs_vfsops.c (revision b3c06cab)
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.8 (Berkeley) 05/09/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 	CIRCLEQ_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 	if (args.version != NFS_ARGSVERSION)
457 		return (EPROGMISMATCH);
458 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
459 	if (error)
460 		return (error);
461 	error = copyinstr(path, pth, MNAMELEN-1, &len);
462 	if (error)
463 		return (error);
464 	bzero(&pth[len], MNAMELEN - len);
465 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
466 	if (error)
467 		return (error);
468 	bzero(&hst[len], MNAMELEN - len);
469 	/* sockargs() call must be after above copyin() calls */
470 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
471 	if (error)
472 		return (error);
473 	args.fh = nfh;
474 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
475 	return (error);
476 }
477 
478 /*
479  * Common code for mount and mountroot
480  */
481 int
482 mountnfs(argp, mp, nam, pth, hst, vpp)
483 	register struct nfs_args *argp;
484 	register struct mount *mp;
485 	struct mbuf *nam;
486 	char *pth, *hst;
487 	struct vnode **vpp;
488 {
489 	register struct nfsmount *nmp;
490 	struct nfsnode *np;
491 	int error, maxio;
492 
493 	if (mp->mnt_flag & MNT_UPDATE) {
494 		nmp = VFSTONFS(mp);
495 		/* update paths, file handles, etc, here	XXX */
496 		m_freem(nam);
497 		return (0);
498 	} else {
499 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
500 		    M_NFSMNT, M_WAITOK);
501 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
502 		TAILQ_INIT(&nmp->nm_uidlruhead);
503 		mp->mnt_data = (qaddr_t)nmp;
504 	}
505 	vfs_getnewfsid(mp);
506 	nmp->nm_mountp = mp;
507 	nmp->nm_flag = argp->flags;
508 	if (nmp->nm_flag & NFSMNT_NQNFS)
509 		/*
510 		 * We have to set mnt_maxsymlink to a non-zero value so
511 		 * that COMPAT_43 routines will know that we are setting
512 		 * the d_type field in directories (and can zero it for
513 		 * unsuspecting binaries).
514 		 */
515 		mp->mnt_maxsymlinklen = 1;
516 	nmp->nm_timeo = NFS_TIMEO;
517 	nmp->nm_retry = NFS_RETRANS;
518 	nmp->nm_wsize = NFS_WSIZE;
519 	nmp->nm_rsize = NFS_RSIZE;
520 	nmp->nm_readdirsize = NFS_READDIRSIZE;
521 	nmp->nm_numgrps = NFS_MAXGRPS;
522 	nmp->nm_readahead = NFS_DEFRAHEAD;
523 	nmp->nm_leaseterm = NQ_DEFLEASE;
524 	nmp->nm_deadthresh = NQ_DEADTHRESH;
525 	CIRCLEQ_INIT(&nmp->nm_timerhead);
526 	nmp->nm_inprog = NULLVP;
527 	nmp->nm_fhsize = argp->fhsize;
528 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
529 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
530 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
531 	nmp->nm_nam = nam;
532 
533 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
534 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
535 		if (nmp->nm_timeo < NFS_MINTIMEO)
536 			nmp->nm_timeo = NFS_MINTIMEO;
537 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
538 			nmp->nm_timeo = NFS_MAXTIMEO;
539 	}
540 
541 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
542 		nmp->nm_retry = argp->retrans;
543 		if (nmp->nm_retry > NFS_MAXREXMIT)
544 			nmp->nm_retry = NFS_MAXREXMIT;
545 	}
546 
547 	if (argp->flags & NFSMNT_NFSV3) {
548 		if (argp->sotype == SOCK_DGRAM)
549 			maxio = NFS_MAXDGRAMDATA;
550 		else
551 			maxio = NFS_MAXDATA;
552 	} else
553 		maxio = NFS_V2MAXDATA;
554 
555 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
556 		nmp->nm_wsize = argp->wsize;
557 		/* Round down to multiple of blocksize */
558 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
559 		if (nmp->nm_wsize <= 0)
560 			nmp->nm_wsize = NFS_FABLKSIZE;
561 	}
562 	if (nmp->nm_wsize > maxio)
563 		nmp->nm_wsize = maxio;
564 	if (nmp->nm_wsize > MAXBSIZE)
565 		nmp->nm_wsize = MAXBSIZE;
566 
567 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
568 		nmp->nm_rsize = argp->rsize;
569 		/* Round down to multiple of blocksize */
570 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
571 		if (nmp->nm_rsize <= 0)
572 			nmp->nm_rsize = NFS_FABLKSIZE;
573 	}
574 	if (nmp->nm_rsize > maxio)
575 		nmp->nm_rsize = maxio;
576 	if (nmp->nm_rsize > MAXBSIZE)
577 		nmp->nm_rsize = MAXBSIZE;
578 
579 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
580 		nmp->nm_readdirsize = argp->readdirsize;
581 		/* Round down to multiple of blocksize */
582 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
583 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
584 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
585 	}
586 	if (nmp->nm_readdirsize > maxio)
587 		nmp->nm_readdirsize = maxio;
588 
589 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
590 		argp->maxgrouplist <= NFS_MAXGRPS)
591 		nmp->nm_numgrps = argp->maxgrouplist;
592 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
593 		argp->readahead <= NFS_MAXRAHEAD)
594 		nmp->nm_readahead = argp->readahead;
595 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
596 		argp->leaseterm <= NQ_MAXLEASE)
597 		nmp->nm_leaseterm = argp->leaseterm;
598 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
599 		argp->deadthresh <= NQ_NEVERDEAD)
600 		nmp->nm_deadthresh = argp->deadthresh;
601 	/* Set up the sockets and per-host congestion */
602 	nmp->nm_sotype = argp->sotype;
603 	nmp->nm_soproto = argp->proto;
604 
605 	/*
606 	 * For Connection based sockets (TCP,...) defer the connect until
607 	 * the first request, in case the server is not responding.
608 	 */
609 	if (nmp->nm_sotype == SOCK_DGRAM &&
610 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
611 		goto bad;
612 
613 	/*
614 	 * This is silly, but it has to be set so that vinifod() works.
615 	 * We do not want to do an nfs_statfs() here since we can get
616 	 * stuck on a dead server and we are holding a lock on the mount
617 	 * point.
618 	 */
619 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
620 	/*
621 	 * A reference count is needed on the nfsnode representing the
622 	 * remote root.  If this object is not persistent, then backward
623 	 * traversals of the mount point (i.e. "..") will not work if
624 	 * the nfsnode gets flushed out of the cache. Ufs does not have
625 	 * this problem, because one can identify root inodes by their
626 	 * number == ROOTINO (2).
627 	 */
628 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
629 	if (error)
630 		goto bad;
631 	*vpp = NFSTOV(np);
632 
633 	return (0);
634 bad:
635 	nfs_disconnect(nmp);
636 	free((caddr_t)nmp, M_NFSMNT);
637 	m_freem(nam);
638 	return (error);
639 }
640 
641 /*
642  * unmount system call
643  */
644 int
645 nfs_unmount(mp, mntflags, p)
646 	struct mount *mp;
647 	int mntflags;
648 	struct proc *p;
649 {
650 	register struct nfsmount *nmp;
651 	struct nfsnode *np;
652 	struct vnode *vp;
653 	int error, flags = 0;
654 	extern int doforce;
655 
656 	if (mntflags & MNT_FORCE) {
657 		if (!doforce)
658 			return (EINVAL);
659 		flags |= FORCECLOSE;
660 	}
661 	nmp = VFSTONFS(mp);
662 	/*
663 	 * Goes something like this..
664 	 * - Check for activity on the root vnode (other than ourselves).
665 	 * - Call vflush() to clear out vnodes for this file system,
666 	 *   except for the root vnode.
667 	 * - Decrement reference on the vnode representing remote root.
668 	 * - Close the socket
669 	 * - Free up the data structures
670 	 */
671 	/*
672 	 * We need to decrement the ref. count on the nfsnode representing
673 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
674 	 * has done vput on this vnode, otherwise we would get deadlock!
675 	 */
676 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
677 	if (error)
678 		return(error);
679 	vp = NFSTOV(np);
680 	if (vp->v_usecount > 2) {
681 		vput(vp);
682 		return (EBUSY);
683 	}
684 
685 	/*
686 	 * Must handshake with nqnfs_clientd() if it is active.
687 	 */
688 	nmp->nm_flag |= NFSMNT_DISMINPROG;
689 	while (nmp->nm_inprog != NULLVP)
690 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
691 	error = vflush(mp, vp, flags);
692 	if (error) {
693 		vput(vp);
694 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
695 		return (error);
696 	}
697 
698 	/*
699 	 * We are now committed to the unmount.
700 	 * For NQNFS, let the server daemon free the nfsmount structure.
701 	 */
702 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
703 		nmp->nm_flag |= NFSMNT_DISMNT;
704 
705 	/*
706 	 * There are two reference counts to get rid of here.
707 	 */
708 	vrele(vp);
709 	vrele(vp);
710 	vgone(vp);
711 	nfs_disconnect(nmp);
712 	m_freem(nmp->nm_nam);
713 
714 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
715 		free((caddr_t)nmp, M_NFSMNT);
716 	return (0);
717 }
718 
719 /*
720  * Return root of a filesystem
721  */
722 int
723 nfs_root(mp, vpp)
724 	struct mount *mp;
725 	struct vnode **vpp;
726 {
727 	register struct vnode *vp;
728 	struct nfsmount *nmp;
729 	struct nfsnode *np;
730 	int error;
731 
732 	nmp = VFSTONFS(mp);
733 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
734 	if (error)
735 		return (error);
736 	vp = NFSTOV(np);
737 	vp->v_type = VDIR;
738 	vp->v_flag = VROOT;
739 	*vpp = vp;
740 	return (0);
741 }
742 
743 extern int syncprt;
744 
745 /*
746  * Flush out the buffer cache
747  */
748 /* ARGSUSED */
749 int
750 nfs_sync(mp, waitfor, cred, p)
751 	struct mount *mp;
752 	int waitfor;
753 	struct ucred *cred;
754 	struct proc *p;
755 {
756 	register struct vnode *vp;
757 	int error, allerror = 0;
758 
759 	/*
760 	 * Force stale buffer cache information to be flushed.
761 	 */
762 loop:
763 	for (vp = mp->mnt_vnodelist.lh_first;
764 	     vp != NULL;
765 	     vp = vp->v_mntvnodes.le_next) {
766 		/*
767 		 * If the vnode that we are about to sync is no longer
768 		 * associated with this mount point, start over.
769 		 */
770 		if (vp->v_mount != mp)
771 			goto loop;
772 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
773 			continue;
774 		if (vget(vp, 1))
775 			goto loop;
776 		error = VOP_FSYNC(vp, cred, waitfor, p);
777 		if (error)
778 			allerror = error;
779 		vput(vp);
780 	}
781 	return (allerror);
782 }
783 
784 /*
785  * NFS flat namespace lookup.
786  * Currently unsupported.
787  */
788 /* ARGSUSED */
789 int
790 nfs_vget(mp, ino, vpp)
791 	struct mount *mp;
792 	ino_t ino;
793 	struct vnode **vpp;
794 {
795 
796 	return (EOPNOTSUPP);
797 }
798 
799 /*
800  * At this point, this should never happen
801  */
802 /* ARGSUSED */
803 int
804 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
805 	register struct mount *mp;
806 	struct fid *fhp;
807 	struct mbuf *nam;
808 	struct vnode **vpp;
809 	int *exflagsp;
810 	struct ucred **credanonp;
811 {
812 
813 	return (EINVAL);
814 }
815 
816 /*
817  * Vnode pointer to File handle, should never happen either
818  */
819 /* ARGSUSED */
820 int
821 nfs_vptofh(vp, fhp)
822 	struct vnode *vp;
823 	struct fid *fhp;
824 {
825 
826 	return (EINVAL);
827 }
828 
829 /*
830  * Vfs start routine, a no-op.
831  */
832 /* ARGSUSED */
833 int
834 nfs_start(mp, flags, p)
835 	struct mount *mp;
836 	int flags;
837 	struct proc *p;
838 {
839 
840 	return (0);
841 }
842 
843 /*
844  * Do operations associated with quotas, not supported
845  */
846 /* ARGSUSED */
847 int
848 nfs_quotactl(mp, cmd, uid, arg, p)
849 	struct mount *mp;
850 	int cmd;
851 	uid_t uid;
852 	caddr_t arg;
853 	struct proc *p;
854 {
855 
856 	return (EOPNOTSUPP);
857 }
858 
859 /*
860  * Do that sysctl thang...
861  */
862 static int
863 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
864 	   size_t newlen, struct proc *p)
865 {
866 	int rv;
867 
868 	/*
869 	 * All names at this level are terminal.
870 	 */
871 	if(namelen > 1)
872 		return ENOTDIR;	/* overloaded */
873 
874 	switch(name[0]) {
875 	case NFS_NFSSTATS:
876 		if(!oldp) {
877 			*oldlenp = sizeof nfsstats;
878 			return 0;
879 		}
880 
881 		if(*oldlenp < sizeof nfsstats) {
882 			*oldlenp = sizeof nfsstats;
883 			return ENOMEM;
884 		}
885 
886 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
887 		if(rv) return rv;
888 
889 		if(newp && newlen != sizeof nfsstats)
890 			return EINVAL;
891 
892 		if(newp) {
893 			return copyin(newp, &nfsstats, sizeof nfsstats);
894 		}
895 		return 0;
896 
897 	default:
898 		return EOPNOTSUPP;
899 	}
900 }
901 
902