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