1 /*	$OpenBSD: nfs_vfsops.c,v 1.57 2005/05/22 17:37:49 pedro Exp $	*/
2 /*	$NetBSD: nfs_vfsops.c,v 1.46.4.1 1996/05/25 22:40:35 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/ioctl.h>
41 #include <sys/signal.h>
42 #include <sys/proc.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/buf.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
60 #include <nfs/nfsnode.h>
61 #include <nfs/nfs.h>
62 #include <nfs/nfsmount.h>
63 #include <nfs/xdr_subs.h>
64 #include <nfs/nfsm_subs.h>
65 #include <nfs/nfsdiskless.h>
66 #include <nfs/nfs_var.h>
67 
68 #define	NQ_DEADTHRESH	NQ_NEVERDEAD	/* Default nm_deadthresh */
69 #define	NQ_NEVERDEAD	9	/* Greater than max. nm_timeouts */
70 
71 extern struct nfsstats nfsstats;
72 extern int nfs_ticks;
73 
74 int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
75 int nfs_checkexp(struct mount *mp, struct mbuf *nam,
76 	 int *extflagsp, struct ucred **credanonp);
77 
78 /*
79  * nfs vfs operations.
80  */
81 const struct vfsops nfs_vfsops = {
82 	nfs_mount,
83 	nfs_start,
84 	nfs_unmount,
85 	nfs_root,
86 	nfs_quotactl,
87 	nfs_statfs,
88 	nfs_sync,
89 	nfs_vget,
90 	nfs_fhtovp,
91 	nfs_vptofh,
92 	nfs_vfs_init,
93 	nfs_sysctl,
94 	nfs_checkexp
95 };
96 
97 extern u_int32_t nfs_procids[NFS_NPROCS];
98 extern u_int32_t nfs_prog, nfs_vers;
99 
100 struct mount *nfs_mount_diskless(struct nfs_dlmount *, char *, int);
101 
102 #define TRUE	1
103 #define	FALSE	0
104 
105 /*
106  * nfs statfs call
107  */
108 int
nfs_statfs(mp,sbp,p)109 nfs_statfs(mp, sbp, p)
110 	struct mount *mp;
111 	struct statfs *sbp;
112 	struct proc *p;
113 {
114 	struct vnode *vp;
115 	struct nfs_statfs *sfp = NULL;
116 	caddr_t cp;
117 	u_int32_t *tl;
118 	int32_t t1, t2;
119 	caddr_t bpos, dpos, cp2;
120 	struct nfsmount *nmp = VFSTONFS(mp);
121 	int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
122 	struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
123 	struct ucred *cred;
124 	struct nfsnode *np;
125 	u_quad_t tquad;
126 
127 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
128 	if (error)
129 		return (error);
130 	vp = NFSTOV(np);
131 	cred = crget();
132 	cred->cr_ngroups = 0;
133 	if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
134 		(void)nfs_fsinfo(nmp, vp, cred, p);
135 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
136 	nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
137 	nfsm_fhtom(vp, v3);
138 	nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
139 	if (v3)
140 		nfsm_postop_attr(vp, retattr);
141 	if (error) {
142 		if (mrep != NULL)
143 			m_free(mrep);
144 		goto nfsmout;
145 	}
146 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
147 	sbp->f_flags = nmp->nm_flag;
148 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
149 	if (v3) {
150 		sbp->f_bsize = NFS_FABLKSIZE;
151 		tquad = fxdr_hyper(&sfp->sf_tbytes);
152 		sbp->f_blocks = (u_int32_t)(tquad / (u_quad_t)NFS_FABLKSIZE);
153 		tquad = fxdr_hyper(&sfp->sf_fbytes);
154 		sbp->f_bfree = (u_int32_t)(tquad / (u_quad_t)NFS_FABLKSIZE);
155 		tquad = fxdr_hyper(&sfp->sf_abytes);
156 		sbp->f_bavail = (int32_t)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
157 		sbp->f_files = (fxdr_unsigned(int32_t,
158 		    sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
159 		sbp->f_ffree = (fxdr_unsigned(int32_t,
160 		    sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
161 	} else {
162 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
163 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
164 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
165 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
166 		sbp->f_files = 0;
167 		sbp->f_ffree = 0;
168 	}
169 	if (sbp != &mp->mnt_stat) {
170 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
171 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
172 		bcopy(&mp->mnt_stat.mount_info.nfs_args,
173 		    &sbp->mount_info.nfs_args, sizeof(struct nfs_args));
174 	}
175 	strncpy(&sbp->f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN);
176 	nfsm_reqdone;
177 	vrele(vp);
178 	crfree(cred);
179 	return (error);
180 }
181 
182 /*
183  * nfs version 3 fsinfo rpc call
184  */
185 int
nfs_fsinfo(nmp,vp,cred,p)186 nfs_fsinfo(nmp, vp, cred, p)
187 	struct nfsmount *nmp;
188 	struct vnode *vp;
189 	struct ucred *cred;
190 	struct proc *p;
191 {
192 	struct nfsv3_fsinfo *fsp;
193 	caddr_t cp;
194 	int32_t t1, t2;
195 	u_int32_t *tl, pref, max;
196 	caddr_t bpos, dpos, cp2;
197 	int error = 0, retattr;
198 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
199 
200 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
201 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
202 	nfsm_fhtom(vp, 1);
203 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
204 	nfsm_postop_attr(vp, retattr);
205 	if (!error) {
206 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
207 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
208 		if (pref < nmp->nm_wsize)
209 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
210 				~(NFS_FABLKSIZE - 1);
211 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
212 		if (max < nmp->nm_wsize) {
213 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
214 			if (nmp->nm_wsize == 0)
215 				nmp->nm_wsize = max;
216 		}
217 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
218 		if (pref < nmp->nm_rsize)
219 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
220 				~(NFS_FABLKSIZE - 1);
221 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
222 		if (max < nmp->nm_rsize) {
223 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
224 			if (nmp->nm_rsize == 0)
225 				nmp->nm_rsize = max;
226 		}
227 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
228 		if (pref < nmp->nm_readdirsize)
229 			nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
230 				~(NFS_DIRBLKSIZ - 1);
231 		if (max < nmp->nm_readdirsize) {
232 			nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
233 			if (nmp->nm_readdirsize == 0)
234 				nmp->nm_readdirsize = max;
235 		}
236 		nmp->nm_flag |= NFSMNT_GOTFSINFO;
237 	}
238 	nfsm_reqdone;
239 	return (error);
240 }
241 
242 /*
243  * Mount a remote root fs via. NFS.  It goes like this:
244  * - Call nfs_boot_init() to fill in the nfs_diskless struct
245  *   (using RARP, bootparam RPC, mountd RPC)
246  * - hand craft the swap nfs vnode hanging off a fake mount point
247  *	if swdevt[0].sw_dev == NODEV
248  * - build the rootfs mount point and call mountnfs() to do the rest.
249  */
250 int
nfs_mountroot()251 nfs_mountroot()
252 {
253 	struct nfs_diskless nd;
254 	struct vattr attr;
255 	struct mount *mp;
256 	struct vnode *vp;
257 	struct proc *procp;
258 	long n;
259 	int error;
260 
261 	procp = curproc; /* XXX */
262 
263 	/*
264 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
265 	 * Side effect:  Finds and configures a network interface.
266 	 */
267 	bzero((caddr_t) &nd, sizeof(nd));
268 	nfs_boot_init(&nd, procp);
269 
270 	/*
271 	 * Create the root mount point.
272 	 */
273 	if (nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root, -1))
274 		panic("nfs_mountroot: root");
275 	mp = nfs_mount_diskless(&nd.nd_root, "/", 0);
276 	nfs_root(mp, &rootvp);
277 	printf("root on %s\n", nd.nd_root.ndm_host);
278 
279 	/*
280 	 * Link it into the mount list.
281 	 */
282 	simple_lock(&mountlist_slock);
283 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
284 	simple_unlock(&mountlist_slock);
285 	vfs_unbusy(mp, procp);
286 
287 	/* Get root attributes (for the time). */
288 	error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp);
289 	if (error) panic("nfs_mountroot: getattr for root");
290 	n = attr.va_atime.tv_sec;
291 #ifdef	DEBUG
292 	printf("root time: 0x%lx\n", n);
293 #endif
294 	inittodr(n);
295 
296 #ifdef notyet
297 	/* Set up swap credentials. */
298 	proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
299 	proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
300 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
301 		NGROUPS)
302 		proc0.p_ucred->cr_ngroups = NGROUPS;
303 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
304 	    proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
305 #endif
306 
307 	/*
308 	 * "Mount" the swap device.
309 	 *
310 	 * On a "dataless" configuration (swap on disk) we will have:
311 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
312 	 */
313 	if (bdevvp(swapdev, &swapdev_vp))
314 		panic("nfs_mountroot: can't setup swap vp");
315 	if (swdevt[0].sw_dev != NODEV) {
316 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
317 		return (0);
318 	}
319 
320 	/*
321 	 * If swapping to an nfs node:  (swdevt[0].sw_dev == NODEV)
322 	 * Create a fake mount point just for the swap vnode so that the
323 	 * swap file can be on a different server from the rootfs.
324 	 *
325 	 * Wait 5 retries, finally no swap is cool. -mickey
326 	 */
327 	error = nfs_boot_getfh(&nd.nd_boot, "swap", &nd.nd_swap, 5);
328 	if (!error) {
329 		mp = nfs_mount_diskless(&nd.nd_swap, "/swap", 0);
330 		nfs_root(mp, &vp);
331 		vfs_unbusy(mp, procp);
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 
340 		/*
341 		 * Next line is a hack to make swapmount() work on NFS swap files.
342 		 * XXX-smurph
343 		 */
344 		swdevt[0].sw_dev = NETDEV;
345 		/* end hack */
346 		swdevt[0].sw_vp = vp;
347 
348 		/*
349 		 * Find out how large the swap file is.
350 		 */
351 		error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
352 		if (error)
353 			printf("nfs_mountroot: getattr for swap\n");
354 		n = (long) (attr.va_size >> DEV_BSHIFT);
355 
356 		printf("swap on %s\n", nd.nd_swap.ndm_host);
357 #ifdef	DEBUG
358 		printf("swap size: 0x%lx (blocks)\n", n);
359 #endif
360 		swdevt[0].sw_nblks = n;
361 		return (0);
362 	}
363 
364 	printf("WARNING: no swap\n");
365 	swdevt[0].sw_dev = NODEV;
366 	swdevt[0].sw_vp = NULL;
367 	swdevt[0].sw_nblks = 0;
368 
369 	return (0);
370 }
371 
372 /*
373  * Internal version of mount system call for diskless setup.
374  */
375 struct mount *
nfs_mount_diskless(ndmntp,mntname,mntflag)376 nfs_mount_diskless(ndmntp, mntname, mntflag)
377 	struct nfs_dlmount *ndmntp;
378 	char *mntname;
379 	int mntflag;
380 {
381 	struct nfs_args args;
382 	struct mount *mp;
383 	struct mbuf *m;
384 	int error;
385 
386 	if (vfs_rootmountalloc("nfs", mntname, &mp))
387 		panic("nfs_mount_diskless: vfs_rootmountalloc failed");
388 	mp->mnt_flag |= mntflag;
389 
390 	/* Initialize mount args. */
391 	bzero((caddr_t) &args, sizeof(args));
392 	args.addr     = (struct sockaddr *)&ndmntp->ndm_saddr;
393 	args.addrlen  = args.addr->sa_len;
394 	args.sotype   = SOCK_DGRAM;
395 	args.fh       = ndmntp->ndm_fh;
396 	args.fhsize   = NFSX_V2FH;
397 	args.hostname = ndmntp->ndm_host;
398 
399 #ifdef	NFS_BOOT_OPTIONS
400 	args.flags    |= NFS_BOOT_OPTIONS;
401 #endif
402 #ifdef	NFS_BOOT_RWSIZE
403 	/*
404 	 * Reduce rsize,wsize for interfaces that consistently
405 	 * drop fragments of long UDP messages.  (i.e. wd8003).
406 	 * You can always change these later via remount.
407 	 */
408 	args.flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
409 	args.wsize    = NFS_BOOT_RWSIZE;
410 	args.rsize    = NFS_BOOT_RWSIZE;
411 #endif
412 
413 	/* Get mbuf for server sockaddr. */
414 	m = m_get(M_WAIT, MT_SONAME);
415 	bcopy((caddr_t)args.addr, mtod(m, caddr_t),
416 	    (m->m_len = args.addr->sa_len));
417 
418 	error = mountnfs(&args, mp, m, mntname, args.hostname);
419 	if (error)
420 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
421 
422 	return (mp);
423 }
424 
425 void
nfs_decode_args(nmp,argp,nargp)426 nfs_decode_args(nmp, argp, nargp)
427 	struct nfsmount *nmp;
428 	struct nfs_args *argp;
429 	struct nfs_args *nargp;
430 {
431 	int s;
432 	int adjsock = 0;
433 	int maxio;
434 
435 	s = splsoftnet();
436 
437 #if 0
438 	/* Re-bind if rsrvd port requested and wasn't on one */
439 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
440 		  && (argp->flags & NFSMNT_RESVPORT);
441 #endif
442 	/* Also re-bind if we're switching to/from a connected UDP socket */
443 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
444 	    (argp->flags & NFSMNT_NOCONN));
445 
446 	/* Update flags atomically.  Don't change the lock bits. */
447 	nmp->nm_flag =
448 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
449 	splx(s);
450 
451 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
452 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
453 		if (nmp->nm_timeo < NFS_MINTIMEO)
454 			nmp->nm_timeo = NFS_MINTIMEO;
455 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
456 			nmp->nm_timeo = NFS_MAXTIMEO;
457 	}
458 
459 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
460 		nmp->nm_retry = argp->retrans;
461 		if (nmp->nm_retry > NFS_MAXREXMIT)
462 			nmp->nm_retry = NFS_MAXREXMIT;
463 	}
464 
465 	if (argp->flags & NFSMNT_NFSV3) {
466 		if (argp->sotype == SOCK_DGRAM)
467 			maxio = NFS_MAXDGRAMDATA;
468 		else
469 			maxio = NFS_MAXDATA;
470 	} else
471 		maxio = NFS_V2MAXDATA;
472 
473 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
474 		int osize = nmp->nm_wsize;
475 		nmp->nm_wsize = argp->wsize;
476 		/* Round down to multiple of blocksize */
477 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
478 		if (nmp->nm_wsize <= 0)
479 			nmp->nm_wsize = NFS_FABLKSIZE;
480 		adjsock |= (nmp->nm_wsize != osize);
481 	}
482 	if (nmp->nm_wsize > maxio)
483 		nmp->nm_wsize = maxio;
484 	if (nmp->nm_wsize > MAXBSIZE)
485 		nmp->nm_wsize = MAXBSIZE;
486 
487 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
488 		int osize = nmp->nm_rsize;
489 		nmp->nm_rsize = argp->rsize;
490 		/* Round down to multiple of blocksize */
491 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
492 		if (nmp->nm_rsize <= 0)
493 			nmp->nm_rsize = NFS_FABLKSIZE;
494 		adjsock |= (nmp->nm_rsize != osize);
495 	}
496 	if (nmp->nm_rsize > maxio)
497 		nmp->nm_rsize = maxio;
498 	if (nmp->nm_rsize > MAXBSIZE)
499 		nmp->nm_rsize = MAXBSIZE;
500 
501 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
502 		nmp->nm_readdirsize = argp->readdirsize;
503 		/* Round down to multiple of blocksize */
504 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
505 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
506 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
507 	} else if (argp->flags & NFSMNT_RSIZE)
508 		nmp->nm_readdirsize = nmp->nm_rsize;
509 
510 	if (nmp->nm_readdirsize > maxio)
511 		nmp->nm_readdirsize = maxio;
512 
513 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
514 		argp->maxgrouplist <= NFS_MAXGRPS)
515 		nmp->nm_numgrps = argp->maxgrouplist;
516 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
517 		argp->readahead <= NFS_MAXRAHEAD)
518 		nmp->nm_readahead = argp->readahead;
519 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
520 		argp->deadthresh <= NQ_NEVERDEAD)
521 		nmp->nm_deadthresh = argp->deadthresh;
522 	if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) {
523 		if (argp->acregmin > 0xffff)
524 			nmp->nm_acregmin = 0xffff;
525 		else
526 			nmp->nm_acregmin = argp->acregmin;
527 	}
528 	if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) {
529 		if (argp->acregmax > 0xffff)
530 			nmp->nm_acregmax = 0xffff;
531 		else
532 			nmp->nm_acregmax = argp->acregmax;
533 	}
534 	if (nmp->nm_acregmin > nmp->nm_acregmax)
535 	  nmp->nm_acregmin = nmp->nm_acregmax;
536 
537 	if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) {
538 		if (argp->acdirmin > 0xffff)
539 			nmp->nm_acdirmin = 0xffff;
540 		else
541 			nmp->nm_acdirmin = argp->acdirmin;
542 	}
543 	if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) {
544 		if (argp->acdirmax > 0xffff)
545 			nmp->nm_acdirmax = 0xffff;
546 		else
547 			nmp->nm_acdirmax = argp->acdirmax;
548 	}
549 	if (nmp->nm_acdirmin > nmp->nm_acdirmax)
550 	  nmp->nm_acdirmin = nmp->nm_acdirmax;
551 
552 	if (nmp->nm_so && adjsock) {
553 		nfs_disconnect(nmp);
554 		if (nmp->nm_sotype == SOCK_DGRAM)
555 			while (nfs_connect(nmp, (struct nfsreq *)0)) {
556 				printf("nfs_args: retrying connect\n");
557 				(void) tsleep((caddr_t)&lbolt,
558 					      PSOCK, "nfscon", 0);
559 			}
560 	}
561 
562 	/* Update nargp based on nmp */
563 	nargp->wsize = nmp->nm_wsize;
564 	nargp->rsize = nmp->nm_rsize;
565 	nargp->readdirsize = nmp->nm_readdirsize;
566 	nargp->timeo = nmp->nm_timeo;
567 	nargp->retrans = nmp->nm_retry;
568 	nargp->maxgrouplist = nmp->nm_numgrps;
569 	nargp->readahead = nmp->nm_readahead;
570 	nargp->deadthresh = nmp->nm_deadthresh;
571 	nargp->acregmin = nmp->nm_acregmin;
572 	nargp->acregmax = nmp->nm_acregmax;
573 	nargp->acdirmin = nmp->nm_acdirmin;
574 	nargp->acdirmax = nmp->nm_acdirmax;
575 }
576 
577 /*
578  * VFS Operations.
579  *
580  * mount system call
581  * It seems a bit dumb to copyinstr() the host and path here and then
582  * bcopy() them in mountnfs(), but I wanted to detect errors before
583  * doing the sockargs() call because sockargs() allocates an mbuf and
584  * an error after that means that I have to release the mbuf.
585  */
586 /* ARGSUSED */
587 int
nfs_mount(mp,path,data,ndp,p)588 nfs_mount(mp, path, data, ndp, p)
589 	struct mount *mp;
590 	const char *path;
591 	void *data;
592 	struct nameidata *ndp;
593 	struct proc *p;
594 {
595 	int error;
596 	struct nfs_args args;
597 	struct mbuf *nam;
598 	char pth[MNAMELEN], hst[MNAMELEN];
599 	size_t len;
600 	u_char nfh[NFSX_V3FHMAX];
601 
602 	error = copyin (data, &args, sizeof (args.version));
603 	if (error)
604 		return (error);
605 	if (args.version == 3) {
606 		error = copyin (data, (caddr_t)&args,
607 				sizeof (struct nfs_args3));
608 		args.flags &= ~(NFSMNT_INTERNAL|NFSMNT_NOAC);
609 	}
610 	else if (args.version == NFS_ARGSVERSION) {
611 		error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
612 		args.flags &= ~NFSMNT_NOAC; /* XXX - compatibility */
613 	}
614 	else
615 		return (EPROGMISMATCH);
616 	if (error)
617 		return (error);
618 
619 	if (nfs_niothreads < 0) {
620 		nfs_niothreads = 4;
621 		nfs_getset_niothreads(TRUE);
622 	}
623 
624 	if (mp->mnt_flag & MNT_UPDATE) {
625 		struct nfsmount *nmp = VFSTONFS(mp);
626 
627 		if (nmp == NULL)
628 			return (EIO);
629 		/*
630 		 * When doing an update, we can't change from or to
631 		 * v3.
632 		 */
633 		args.flags = (args.flags & ~(NFSMNT_NFSV3)) |
634 		    (nmp->nm_flag & (NFSMNT_NFSV3));
635 		nfs_decode_args(nmp, &args, &mp->mnt_stat.mount_info.nfs_args);
636 		return (0);
637 	}
638 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
639 		return (EINVAL);
640 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
641 	if (error)
642 		return (error);
643 	error = copyinstr(path, pth, MNAMELEN-1, &len);
644 	if (error)
645 		return (error);
646 	bzero(&pth[len], MNAMELEN - len);
647 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
648 	if (error)
649 		return (error);
650 	bzero(&hst[len], MNAMELEN - len);
651 	/* sockargs() call must be after above copyin() calls */
652 	error = sockargs(&nam, args.addr, args.addrlen, MT_SONAME);
653 	if (error)
654 		return (error);
655 	args.fh = nfh;
656 	error = mountnfs(&args, mp, nam, pth, hst);
657 	return (error);
658 }
659 
660 /*
661  * Common code for mount and mountroot
662  */
663 int
mountnfs(argp,mp,nam,pth,hst)664 mountnfs(argp, mp, nam, pth, hst)
665 	struct nfs_args *argp;
666 	struct mount *mp;
667 	struct mbuf *nam;
668 	char *pth, *hst;
669 {
670 	struct nfsmount *nmp;
671 	int error;
672 
673 	if (mp->mnt_flag & MNT_UPDATE) {
674 		nmp = VFSTONFS(mp);
675 		/* update paths, file handles, etc, here	XXX */
676 		m_freem(nam);
677 		return (0);
678 	} else {
679 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
680 		    M_NFSMNT, M_WAITOK);
681 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
682 		mp->mnt_data = (qaddr_t)nmp;
683 		TAILQ_INIT(&nmp->nm_uidlruhead);
684 	}
685 
686 	vfs_getnewfsid(mp);
687 	nmp->nm_mountp = mp;
688 	nmp->nm_timeo = NFS_TIMEO;
689 	nmp->nm_retry = NFS_RETRANS;
690 	nmp->nm_wsize = NFS_WSIZE;
691 	nmp->nm_rsize = NFS_RSIZE;
692 	nmp->nm_readdirsize = NFS_READDIRSIZE;
693 	nmp->nm_numgrps = NFS_MAXGRPS;
694 	nmp->nm_readahead = NFS_DEFRAHEAD;
695 	nmp->nm_deadthresh = NQ_DEADTHRESH;
696 	CIRCLEQ_INIT(&nmp->nm_timerhead);
697 	nmp->nm_inprog = NULLVP;
698 	nmp->nm_fhsize = argp->fhsize;
699 	nmp->nm_acregmin = NFS_MINATTRTIMO;
700 	nmp->nm_acregmax = NFS_MAXATTRTIMO;
701 	nmp->nm_acdirmin = NFS_MINATTRTIMO;
702 	nmp->nm_acdirmax = NFS_MAXATTRTIMO;
703 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
704 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN);
705 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
706 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
707 	bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp));
708 	nmp->nm_nam = nam;
709 	nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args);
710 
711 	/* Set up the sockets and per-host congestion */
712 	nmp->nm_sotype = argp->sotype;
713 	nmp->nm_soproto = argp->proto;
714 
715 	/*
716 	 * For Connection based sockets (TCP,...) defer the connect until
717 	 * the first request, in case the server is not responding.
718 	 */
719 	if (nmp->nm_sotype == SOCK_DGRAM &&
720 	    (error = nfs_connect(nmp, (struct nfsreq *)0)))
721 		goto bad;
722 
723 	/*
724 	 * This is silly, but it has to be set so that vinifod() works.
725 	 * We do not want to do an nfs_statfs() here since we can get
726 	 * stuck on a dead server and we are holding a lock on the mount
727 	 * point.
728 	 */
729 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
730 
731 	return (0);
732 bad:
733 	nfs_disconnect(nmp);
734 	free((caddr_t)nmp, M_NFSMNT);
735 	m_freem(nam);
736 	return (error);
737 }
738 
739 /*
740  * unmount system call
741  */
742 int
nfs_unmount(mp,mntflags,p)743 nfs_unmount(mp, mntflags, p)
744 	struct mount *mp;
745 	int mntflags;
746 	struct proc *p;
747 {
748 	struct nfsmount *nmp;
749 	int error, flags = 0;
750 
751 	if (mntflags & MNT_FORCE)
752 		flags |= FORCECLOSE;
753 	nmp = VFSTONFS(mp);
754 	/*
755 	 * Goes something like this..
756 	 * - Call vflush() to clear out vnodes for this file system,
757 	 *   except for the root vnode.
758 	 * - Close the socket
759 	 * - Free up the data structures
760 	 */
761 
762 	/*
763 	 * Must handshake with nqnfs_clientd() if it is active.
764 	 */
765 	nmp->nm_flag |= NFSMNT_DISMINPROG;
766 	while (nmp->nm_inprog != NULLVP)
767 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
768 	error = vflush(mp, NULL, flags);
769 	if (error) {
770 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
771 		return (error);
772 	}
773 
774 	/*
775 	 * We are now committed to the unmount.
776 	 * For NQNFS, let the server daemon free the nfsmount structure.
777 	 */
778 	if (nmp->nm_flag & NFSMNT_KERB)
779 		nmp->nm_flag |= NFSMNT_DISMNT;
780 
781 	nfs_disconnect(nmp);
782 	m_freem(nmp->nm_nam);
783 
784 	if ((nmp->nm_flag & NFSMNT_KERB) == 0)
785 		free((caddr_t)nmp, M_NFSMNT);
786 	return (0);
787 }
788 
789 /*
790  * Return root of a filesystem
791  */
792 int
nfs_root(mp,vpp)793 nfs_root(mp, vpp)
794 	struct mount *mp;
795 	struct vnode **vpp;
796 {
797 	struct nfsmount *nmp;
798 	struct nfsnode *np;
799 	int error;
800 
801 	nmp = VFSTONFS(mp);
802 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
803 	if (error)
804 		return (error);
805 	*vpp = NFSTOV(np);
806 	return (0);
807 }
808 
809 extern int syncprt;
810 
811 /*
812  * Flush out the buffer cache
813  */
814 /* ARGSUSED */
815 int
nfs_sync(mp,waitfor,cred,p)816 nfs_sync(mp, waitfor, cred, p)
817 	struct mount *mp;
818 	int waitfor;
819 	struct ucred *cred;
820 	struct proc *p;
821 {
822 	struct vnode *vp;
823 	int error, allerror = 0;
824 
825 	/*
826 	 * Don't traverse the vnode list if we want to skip all of them.
827 	 */
828 	if (waitfor == MNT_LAZY)
829 		return (allerror);
830 
831 	/*
832 	 * Force stale buffer cache information to be flushed.
833 	 */
834 loop:
835 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL;
836 	     vp = LIST_NEXT(vp, v_mntvnodes)) {
837 		/*
838 		 * If the vnode that we are about to sync is no longer
839 		 * associated with this mount point, start over.
840 		 */
841 		if (vp->v_mount != mp)
842 			goto loop;
843 		if (VOP_ISLOCKED(vp) || LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
844 			continue;
845 		if (vget(vp, LK_EXCLUSIVE, p))
846 			goto loop;
847 		error = VOP_FSYNC(vp, cred, waitfor, p);
848 		if (error)
849 			allerror = error;
850 		vput(vp);
851 	}
852 
853 	return (allerror);
854 }
855 
856 /*
857  * NFS flat namespace lookup.
858  * Currently unsupported.
859  */
860 /* ARGSUSED */
861 int
nfs_vget(mp,ino,vpp)862 nfs_vget(mp, ino, vpp)
863 	struct mount *mp;
864 	ino_t ino;
865 	struct vnode **vpp;
866 {
867 
868 	return (EOPNOTSUPP);
869 }
870 
871 /*
872  * Do that sysctl thang...
873  */
874 int
nfs_sysctl(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen,struct proc * p)875 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
876 	   size_t newlen, struct proc *p)
877 {
878 	int rv;
879 
880 	/*
881 	 * All names at this level are terminal.
882 	 */
883 	if(namelen > 1)
884 		return ENOTDIR;	/* overloaded */
885 
886 	switch(name[0]) {
887 	case NFS_NFSSTATS:
888 		if(!oldp) {
889 			*oldlenp = sizeof nfsstats;
890 			return 0;
891 		}
892 
893 		if(*oldlenp < sizeof nfsstats) {
894 			*oldlenp = sizeof nfsstats;
895 			return ENOMEM;
896 		}
897 
898 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
899 		if(rv) return rv;
900 
901 		if(newp && newlen != sizeof nfsstats)
902 			return EINVAL;
903 
904 		if(newp) {
905 			return copyin(newp, &nfsstats, sizeof nfsstats);
906 		}
907 		return 0;
908 
909 	case NFS_NIOTHREADS:
910 		nfs_getset_niothreads(0);
911 
912 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads);
913 		if (newp)
914 			nfs_getset_niothreads(1);
915 
916 		return rv;
917 
918 	default:
919 		return EOPNOTSUPP;
920 	}
921 }
922 
923 
924 /*
925  * At this point, this should never happen
926  */
927 /* ARGSUSED */
928 int
nfs_fhtovp(mp,fhp,vpp)929 nfs_fhtovp(mp, fhp, vpp)
930 	struct mount *mp;
931 	struct fid *fhp;
932 	struct vnode **vpp;
933 {
934 
935 	return (EINVAL);
936 }
937 
938 /*
939  * Vnode pointer to File handle, should never happen either
940  */
941 /* ARGSUSED */
942 int
nfs_vptofh(vp,fhp)943 nfs_vptofh(vp, fhp)
944 	struct vnode *vp;
945 	struct fid *fhp;
946 {
947 
948 	return (EINVAL);
949 }
950 
951 /*
952  * Vfs start routine, a no-op.
953  */
954 /* ARGSUSED */
955 int
nfs_start(mp,flags,p)956 nfs_start(mp, flags, p)
957 	struct mount *mp;
958 	int flags;
959 	struct proc *p;
960 {
961 
962 	return (0);
963 }
964 
965 /*
966  * Do operations associated with quotas, not supported
967  */
968 /* ARGSUSED */
969 int
nfs_quotactl(mp,cmd,uid,arg,p)970 nfs_quotactl(mp, cmd, uid, arg, p)
971 	struct mount *mp;
972 	int cmd;
973 	uid_t uid;
974 	caddr_t arg;
975 	struct proc *p;
976 {
977 
978 	return (EOPNOTSUPP);
979 }
980 
981 /*
982  * check export permission, not supported
983  */
984 /* ARGUSED */
985 int
nfs_checkexp(mp,nam,exflagsp,credanonp)986 nfs_checkexp(mp, nam, exflagsp, credanonp)
987 	struct mount *mp;
988 	struct mbuf *nam;
989 	int *exflagsp;
990 	struct ucred **credanonp;
991 {
992 	return (EOPNOTSUPP);
993 }
994 
995