Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.139
      1 /*	$NetBSD: nfs_vfsops.c,v 1.139 2004/05/25 04:44:44 atatat Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.139 2004/05/25 04:44:44 atatat Exp $");
     39 
     40 #if defined(_KERNEL_OPT)
     41 #include "opt_compat_netbsd.h"
     42 #include "opt_nfs.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/signal.h>
     48 #include <sys/proc.h>
     49 #include <sys/namei.h>
     50 #include <sys/device.h>
     51 #include <sys/vnode.h>
     52 #include <sys/kernel.h>
     53 #include <sys/mount.h>
     54 #include <sys/buf.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/dirent.h>
     57 #include <sys/socket.h>
     58 #include <sys/socketvar.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/systm.h>
     61 
     62 #include <net/if.h>
     63 #include <net/route.h>
     64 #include <netinet/in.h>
     65 
     66 #include <nfs/rpcv2.h>
     67 #include <nfs/nfsproto.h>
     68 #include <nfs/nfsnode.h>
     69 #include <nfs/nfs.h>
     70 #include <nfs/nfsmount.h>
     71 #include <nfs/xdr_subs.h>
     72 #include <nfs/nfsm_subs.h>
     73 #include <nfs/nfsdiskless.h>
     74 #include <nfs/nqnfs.h>
     75 #include <nfs/nfs_var.h>
     76 
     77 extern struct nfsstats nfsstats;
     78 extern int nfs_ticks;
     79 
     80 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
     81 
     82 /*
     83  * nfs vfs operations.
     84  */
     85 
     86 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
     87 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
     88 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
     89 
     90 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
     91 	&nfsv2_vnodeop_opv_desc,
     92 	&spec_nfsv2nodeop_opv_desc,
     93 	&fifo_nfsv2nodeop_opv_desc,
     94 	NULL,
     95 };
     96 
     97 struct vfsops nfs_vfsops = {
     98 	MOUNT_NFS,
     99 	nfs_mount,
    100 	nfs_start,
    101 	nfs_unmount,
    102 	nfs_root,
    103 	nfs_quotactl,
    104 	nfs_statvfs,
    105 	nfs_sync,
    106 	nfs_vget,
    107 	nfs_fhtovp,
    108 	nfs_vptofh,
    109 	nfs_vfs_init,
    110 	nfs_vfs_reinit,
    111 	nfs_vfs_done,
    112 	NULL,
    113 	nfs_mountroot,
    114 	nfs_checkexp,
    115 	nfs_vnodeopv_descs,
    116 };
    117 
    118 extern u_int32_t nfs_procids[NFS_NPROCS];
    119 extern u_int32_t nfs_prog, nfs_vers;
    120 
    121 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
    122     struct mount **, struct vnode **, struct proc *));
    123 
    124 /*
    125  * nfs statvfs call
    126  */
    127 int
    128 nfs_statvfs(mp, sbp, p)
    129 	struct mount *mp;
    130 	struct statvfs *sbp;
    131 	struct proc *p;
    132 {
    133 	struct vnode *vp;
    134 	struct nfs_statfs *sfp;
    135 	caddr_t cp;
    136 	u_int32_t *tl;
    137 	int32_t t1, t2;
    138 	caddr_t bpos, dpos, cp2;
    139 	struct nfsmount *nmp = VFSTONFS(mp);
    140 	int error = 0, retattr;
    141 #ifdef NFS_V2_ONLY
    142 	const int v3 = 0;
    143 #else
    144 	int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
    145 #endif
    146 	struct mbuf *mreq, *mrep = NULL, *md, *mb;
    147 	struct ucred *cred;
    148 	u_quad_t tquad;
    149 	struct nfsnode *np;
    150 
    151 #ifndef nolint
    152 	sfp = (struct nfs_statfs *)0;
    153 #endif
    154 	vp = nmp->nm_vnode;
    155 	np = VTONFS(vp);
    156 	cred = crget();
    157 	cred->cr_ngroups = 0;
    158 #ifndef NFS_V2_ONLY
    159 	if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
    160 		(void)nfs_fsinfo(nmp, vp, cred, p);
    161 #endif
    162 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
    163 	nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
    164 	nfsm_fhtom(np, v3);
    165 	nfsm_request(np, NFSPROC_FSSTAT, p, cred);
    166 	if (v3)
    167 		nfsm_postop_attr(vp, retattr, 0);
    168 	if (error) {
    169 		if (mrep != NULL) {
    170 			if (mrep->m_next != NULL)
    171 				printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
    172 			m_freem(mrep);
    173 		}
    174 		goto nfsmout;
    175 	}
    176 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
    177 	sbp->f_flag = nmp->nm_flag;
    178 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
    179 	if (v3) {
    180 		sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
    181 		tquad = fxdr_hyper(&sfp->sf_tbytes);
    182 		sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    183 		tquad = fxdr_hyper(&sfp->sf_fbytes);
    184 		sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    185 		tquad = fxdr_hyper(&sfp->sf_abytes);
    186 		tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    187 		sbp->f_bresvd = sbp->f_bfree - tquad;
    188 		sbp->f_bavail = tquad;
    189 #ifdef COMPAT_20
    190 		/* Handle older NFS servers returning negative values */
    191 		if ((quad_t)sbp->f_bavail < 0)
    192 			sbp->f_bavail = 0;
    193 #endif
    194 		tquad = fxdr_hyper(&sfp->sf_tfiles);
    195 		sbp->f_files = tquad;
    196 		tquad = fxdr_hyper(&sfp->sf_ffiles);
    197 		sbp->f_ffree = tquad;
    198 		sbp->f_favail = tquad;
    199 		sbp->f_fresvd = 0;
    200 		sbp->f_namemax = MAXNAMLEN;
    201 	} else {
    202 		sbp->f_bsize = NFS_FABLKSIZE;
    203 		sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
    204 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
    205 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
    206 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
    207 		sbp->f_fresvd = 0;
    208 		sbp->f_files = 0;
    209 		sbp->f_ffree = 0;
    210 		sbp->f_favail = 0;
    211 		sbp->f_fresvd = 0;
    212 		sbp->f_namemax = MAXNAMLEN;
    213 	}
    214 	copy_statvfs_info(sbp, mp);
    215 	nfsm_reqdone;
    216 	crfree(cred);
    217 	return (error);
    218 }
    219 
    220 #ifndef NFS_V2_ONLY
    221 /*
    222  * nfs version 3 fsinfo rpc call
    223  */
    224 int
    225 nfs_fsinfo(nmp, vp, cred, p)
    226 	struct nfsmount *nmp;
    227 	struct vnode *vp;
    228 	struct ucred *cred;
    229 	struct proc *p;
    230 {
    231 	struct nfsv3_fsinfo *fsp;
    232 	caddr_t cp;
    233 	int32_t t1, t2;
    234 	u_int32_t *tl, pref, max;
    235 	caddr_t bpos, dpos, cp2;
    236 	int error = 0, retattr;
    237 	struct mbuf *mreq, *mrep, *md, *mb;
    238 	u_int64_t maxfsize;
    239 	struct nfsnode *np = VTONFS(vp);
    240 
    241 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
    242 	nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
    243 	nfsm_fhtom(np, 1);
    244 	nfsm_request(np, NFSPROC_FSINFO, p, cred);
    245 	nfsm_postop_attr(vp, retattr, 0);
    246 	if (!error) {
    247 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
    248 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
    249 		if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
    250 		    pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
    251 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
    252 				~(NFS_FABLKSIZE - 1);
    253 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
    254 		if (max < nmp->nm_wsize && max > 0) {
    255 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
    256 			if (nmp->nm_wsize == 0)
    257 				nmp->nm_wsize = max;
    258 		}
    259 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
    260 		if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
    261 		    pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
    262 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
    263 				~(NFS_FABLKSIZE - 1);
    264 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
    265 		if (max < nmp->nm_rsize && max > 0) {
    266 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
    267 			if (nmp->nm_rsize == 0)
    268 				nmp->nm_rsize = max;
    269 		}
    270 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
    271 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
    272 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
    273 				~(NFS_DIRFRAGSIZ - 1);
    274 		if (max < nmp->nm_readdirsize && max > 0) {
    275 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
    276 			if (nmp->nm_readdirsize == 0)
    277 				nmp->nm_readdirsize = max;
    278 		}
    279 		/* XXX */
    280 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
    281 		maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
    282 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
    283 			nmp->nm_maxfilesize = maxfsize;
    284 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
    285 	}
    286 	nfsm_reqdone;
    287 	return (error);
    288 }
    289 #endif
    290 
    291 /*
    292  * Mount a remote root fs via. NFS.  It goes like this:
    293  * - Call nfs_boot_init() to fill in the nfs_diskless struct
    294  * - build the rootfs mount point and call mountnfs() to do the rest.
    295  */
    296 int
    297 nfs_mountroot()
    298 {
    299 	struct nfs_diskless *nd;
    300 	struct vattr attr;
    301 	struct mount *mp;
    302 	struct vnode *vp;
    303 	struct proc *procp;
    304 	long n;
    305 	int error;
    306 
    307 	procp = curproc; /* XXX */
    308 
    309 	if (root_device->dv_class != DV_IFNET)
    310 		return (ENODEV);
    311 
    312 	/*
    313 	 * XXX time must be non-zero when we init the interface or else
    314 	 * the arp code will wedge.  [Fixed now in if_ether.c]
    315 	 * However, the NFS attribute cache gives false "hits" when
    316 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
    317 	 */
    318 	if (time.tv_sec < NFS_MAXATTRTIMO)
    319 		time.tv_sec = NFS_MAXATTRTIMO;
    320 
    321 	/*
    322 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
    323 	 * Side effect:  Finds and configures a network interface.
    324 	 */
    325 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
    326 	memset((caddr_t)nd, 0, sizeof(*nd));
    327 	error = nfs_boot_init(nd, procp);
    328 	if (error) {
    329 		free(nd, M_NFSMNT);
    330 		return (error);
    331 	}
    332 
    333 	/*
    334 	 * Create the root mount point.
    335 	 */
    336 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
    337 	if (error)
    338 		goto out;
    339 	printf("root on %s\n", nd->nd_root.ndm_host);
    340 
    341 	/*
    342 	 * Link it into the mount list.
    343 	 */
    344 	simple_lock(&mountlist_slock);
    345 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    346 	simple_unlock(&mountlist_slock);
    347 	rootvp = vp;
    348 	mp->mnt_vnodecovered = NULLVP;
    349 	vfs_unbusy(mp);
    350 
    351 	/* Get root attributes (for the time). */
    352 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
    353 	if (error)
    354 		panic("nfs_mountroot: getattr for root");
    355 	n = attr.va_atime.tv_sec;
    356 #ifdef	DEBUG
    357 	printf("root time: 0x%lx\n", n);
    358 #endif
    359 	inittodr(n);
    360 
    361 out:
    362 	if (error)
    363 		nfs_boot_cleanup(nd, procp);
    364 	free(nd, M_NFSMNT);
    365 	return (error);
    366 }
    367 
    368 /*
    369  * Internal version of mount system call for diskless setup.
    370  * Separate function because we used to call it twice.
    371  * (once for root and once for swap)
    372  */
    373 static int
    374 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
    375 	struct nfs_dlmount *ndmntp;
    376 	const char *mntname;	/* mount point name */
    377 	struct mount **mpp;
    378 	struct vnode **vpp;
    379 	struct proc *p;
    380 {
    381 	struct mount *mp;
    382 	struct mbuf *m;
    383 	int error;
    384 
    385 	vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
    386 
    387 	mp->mnt_op = &nfs_vfsops;
    388 
    389 	/*
    390 	 * Historical practice expects NFS root file systems to
    391 	 * be initially mounted r/w.
    392 	 */
    393 	mp->mnt_flag &= ~MNT_RDONLY;
    394 
    395 	/* Get mbuf for server sockaddr. */
    396 	m = m_get(M_WAIT, MT_SONAME);
    397 	if (m == NULL)
    398 		panic("nfs_mountroot: mget soname for %s", mntname);
    399 	MCLAIM(m, &nfs_mowner);
    400 	memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
    401 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
    402 
    403 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
    404 			 ndmntp->ndm_args.hostname, vpp, p);
    405 	if (error) {
    406 		mp->mnt_op->vfs_refcount--;
    407 		vfs_unbusy(mp);
    408 		printf("nfs_mountroot: mount %s failed: %d\n",
    409 		       mntname, error);
    410 		free(mp, M_MOUNT);
    411 	} else
    412 		*mpp = mp;
    413 
    414 	return (error);
    415 }
    416 
    417 void
    418 nfs_decode_args(nmp, argp, p)
    419 	struct nfsmount *nmp;
    420 	struct nfs_args *argp;
    421 	struct proc *p;
    422 {
    423 	int s;
    424 	int adjsock;
    425 	int maxio;
    426 
    427 	s = splsoftnet();
    428 
    429 	/*
    430 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
    431 	 * no sense in that context.
    432 	 */
    433 	if (argp->sotype == SOCK_STREAM)
    434 		argp->flags &= ~NFSMNT_NOCONN;
    435 
    436 	/*
    437 	 * Cookie translation is not needed for v2, silently ignore it.
    438 	 */
    439 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
    440 	    NFSMNT_XLATECOOKIE)
    441 		argp->flags &= ~NFSMNT_XLATECOOKIE;
    442 
    443 	/* Re-bind if rsrvd port requested and wasn't on one */
    444 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
    445 		  && (argp->flags & NFSMNT_RESVPORT);
    446 	/* Also re-bind if we're switching to/from a connected UDP socket */
    447 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
    448 		    (argp->flags & NFSMNT_NOCONN));
    449 
    450 	/* Update flags. */
    451 	nmp->nm_flag = argp->flags;
    452 	splx(s);
    453 
    454 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
    455 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
    456 		if (nmp->nm_timeo < NFS_MINTIMEO)
    457 			nmp->nm_timeo = NFS_MINTIMEO;
    458 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
    459 			nmp->nm_timeo = NFS_MAXTIMEO;
    460 	}
    461 
    462 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
    463 		nmp->nm_retry = argp->retrans;
    464 		if (nmp->nm_retry > NFS_MAXREXMIT)
    465 			nmp->nm_retry = NFS_MAXREXMIT;
    466 	}
    467 
    468 #ifndef NFS_V2_ONLY
    469 	if (argp->flags & NFSMNT_NFSV3) {
    470 		if (argp->sotype == SOCK_DGRAM)
    471 			maxio = NFS_MAXDGRAMDATA;
    472 		else
    473 			maxio = NFS_MAXDATA;
    474 	} else
    475 #endif
    476 		maxio = NFS_V2MAXDATA;
    477 
    478 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
    479 		int osize = nmp->nm_wsize;
    480 		nmp->nm_wsize = argp->wsize;
    481 		/* Round down to multiple of blocksize */
    482 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
    483 		if (nmp->nm_wsize <= 0)
    484 			nmp->nm_wsize = NFS_FABLKSIZE;
    485 		adjsock |= (nmp->nm_wsize != osize);
    486 	}
    487 	if (nmp->nm_wsize > maxio)
    488 		nmp->nm_wsize = maxio;
    489 	if (nmp->nm_wsize > MAXBSIZE)
    490 		nmp->nm_wsize = MAXBSIZE;
    491 
    492 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
    493 		int osize = nmp->nm_rsize;
    494 		nmp->nm_rsize = argp->rsize;
    495 		/* Round down to multiple of blocksize */
    496 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
    497 		if (nmp->nm_rsize <= 0)
    498 			nmp->nm_rsize = NFS_FABLKSIZE;
    499 		adjsock |= (nmp->nm_rsize != osize);
    500 	}
    501 	if (nmp->nm_rsize > maxio)
    502 		nmp->nm_rsize = maxio;
    503 	if (nmp->nm_rsize > MAXBSIZE)
    504 		nmp->nm_rsize = MAXBSIZE;
    505 
    506 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
    507 		nmp->nm_readdirsize = argp->readdirsize;
    508 		/* Round down to multiple of minimum blocksize */
    509 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
    510 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
    511 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
    512 		/* Bigger than buffer size makes no sense */
    513 		if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
    514 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
    515 	} else if (argp->flags & NFSMNT_RSIZE)
    516 		nmp->nm_readdirsize = nmp->nm_rsize;
    517 
    518 	if (nmp->nm_readdirsize > maxio)
    519 		nmp->nm_readdirsize = maxio;
    520 
    521 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
    522 		argp->maxgrouplist <= NFS_MAXGRPS)
    523 		nmp->nm_numgrps = argp->maxgrouplist;
    524 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
    525 		argp->readahead <= NFS_MAXRAHEAD)
    526 		nmp->nm_readahead = argp->readahead;
    527 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
    528 		argp->leaseterm <= NQ_MAXLEASE)
    529 		nmp->nm_leaseterm = argp->leaseterm;
    530 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
    531 		argp->deadthresh <= NQ_NEVERDEAD)
    532 		nmp->nm_deadthresh = argp->deadthresh;
    533 
    534 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
    535 		    (nmp->nm_soproto != argp->proto));
    536 	nmp->nm_sotype = argp->sotype;
    537 	nmp->nm_soproto = argp->proto;
    538 
    539 	if (nmp->nm_so && adjsock) {
    540 		nfs_safedisconnect(nmp);
    541 		if (nmp->nm_sotype == SOCK_DGRAM)
    542 			while (nfs_connect(nmp, (struct nfsreq *)0, p)) {
    543 				printf("nfs_args: retrying connect\n");
    544 				(void) tsleep((caddr_t)&lbolt,
    545 					      PSOCK, "nfscn3", 0);
    546 			}
    547 	}
    548 }
    549 
    550 /*
    551  * VFS Operations.
    552  *
    553  * mount system call
    554  * It seems a bit dumb to copyinstr() the host and path here and then
    555  * memcpy() them in mountnfs(), but I wanted to detect errors before
    556  * doing the sockargs() call because sockargs() allocates an mbuf and
    557  * an error after that means that I have to release the mbuf.
    558  */
    559 /* ARGSUSED */
    560 int
    561 nfs_mount(mp, path, data, ndp, p)
    562 	struct mount *mp;
    563 	const char *path;
    564 	void *data;
    565 	struct nameidata *ndp;
    566 	struct proc *p;
    567 {
    568 	int error;
    569 	struct nfs_args args;
    570 	struct mbuf *nam;
    571 	struct nfsmount *nmp = VFSTONFS(mp);
    572 	struct sockaddr *sa;
    573 	struct vnode *vp;
    574 	char *pth, *hst;
    575 	size_t len;
    576 	u_char *nfh;
    577 
    578 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
    579 	if (error)
    580 		return (error);
    581 
    582 	if (mp->mnt_flag & MNT_GETARGS) {
    583 
    584 		if (nmp == NULL)
    585 			return (EIO);
    586 		if (args.addr != NULL) {
    587 			sa = mtod(nmp->nm_nam, struct sockaddr *);
    588 			error = copyout(sa, args.addr, sa->sa_len);
    589 			if (error)
    590 				return (error);
    591 			args.addrlen = sa->sa_len;
    592 		} else
    593 			args.addrlen = 0;
    594 
    595 		args.version = NFS_ARGSVERSION;
    596 		args.sotype = nmp->nm_sotype;
    597 		args.proto = nmp->nm_soproto;
    598 		args.fh = NULL;
    599 		args.fhsize = 0;
    600 		args.flags = nmp->nm_flag;
    601 		args.wsize = nmp->nm_wsize;
    602 		args.rsize = nmp->nm_rsize;
    603 		args.readdirsize = nmp->nm_readdirsize;
    604 		args.timeo = nmp->nm_timeo;
    605 		args.retrans = nmp->nm_retry;
    606 		args.maxgrouplist = nmp->nm_numgrps;
    607 		args.readahead = nmp->nm_readahead;
    608 		args.leaseterm = nmp->nm_leaseterm;
    609 		args.deadthresh = nmp->nm_deadthresh;
    610 		args.hostname = NULL;
    611 		return (copyout(&args, data, sizeof(args)));
    612 	}
    613 
    614 	if (args.version != NFS_ARGSVERSION)
    615 		return (EPROGMISMATCH);
    616 #ifdef NFS_V2_ONLY
    617 	if (args.flags & NFSMNT_NQNFS)
    618 		return (EPROGUNAVAIL);
    619 	if (args.flags & NFSMNT_NFSV3)
    620 		return (EPROGMISMATCH);
    621 #endif
    622 	if (mp->mnt_flag & MNT_UPDATE) {
    623 		if (nmp == NULL)
    624 			return (EIO);
    625 		/*
    626 		 * When doing an update, we can't change from or to
    627 		 * v3 and/or nqnfs, or change cookie translation
    628 		 */
    629 		args.flags = (args.flags &
    630 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
    631 		    (nmp->nm_flag &
    632 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
    633 		nfs_decode_args(nmp, &args, p);
    634 		return (0);
    635 	}
    636 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
    637 		return (EINVAL);
    638 	MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
    639 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
    640 	if (error)
    641 		return (error);
    642 	MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
    643 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
    644 	if (error)
    645 		goto free_nfh;
    646 	memset(&pth[len], 0, MNAMELEN - len);
    647 	MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
    648 	error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
    649 	if (error)
    650 		goto free_pth;
    651 	memset(&hst[len], 0, MNAMELEN - len);
    652 	/* sockargs() call must be after above copyin() calls */
    653 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
    654 	if (error)
    655 		goto free_hst;
    656 	MCLAIM(nam, &nfs_mowner);
    657 	args.fh = nfh;
    658 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
    659 
    660 free_hst:
    661 	FREE(hst, M_TEMP);
    662 free_pth:
    663 	FREE(pth, M_TEMP);
    664 free_nfh:
    665 	FREE(nfh, M_TEMP);
    666 
    667 	return (error);
    668 }
    669 
    670 /*
    671  * Common code for mount and mountroot
    672  */
    673 int
    674 mountnfs(argp, mp, nam, pth, hst, vpp, p)
    675 	struct nfs_args *argp;
    676 	struct mount *mp;
    677 	struct mbuf *nam;
    678 	const char *pth, *hst;
    679 	struct vnode **vpp;
    680 	struct proc *p;
    681 {
    682 	struct nfsmount *nmp;
    683 	struct nfsnode *np;
    684 	int error;
    685 	struct vattr *attrs;
    686 	struct ucred *cr;
    687 
    688 	/*
    689 	 * If the number of nfs iothreads to use has never
    690 	 * been set, create a reasonable number of them.
    691 	 */
    692 
    693 	if (nfs_niothreads < 0) {
    694 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
    695 		nfs_getset_niothreads(TRUE);
    696 	}
    697 
    698 	if (mp->mnt_flag & MNT_UPDATE) {
    699 		nmp = VFSTONFS(mp);
    700 		/* update paths, file handles, etc, here	XXX */
    701 		m_freem(nam);
    702 		return (0);
    703 	} else {
    704 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
    705 		    M_NFSMNT, M_WAITOK);
    706 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
    707 		mp->mnt_data = nmp;
    708 		TAILQ_INIT(&nmp->nm_uidlruhead);
    709 		TAILQ_INIT(&nmp->nm_bufq);
    710 		lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
    711 		simple_lock_init(&nmp->nm_slock);
    712 	}
    713 	vfs_getnewfsid(mp);
    714 	nmp->nm_mountp = mp;
    715 
    716 #ifndef NFS_V2_ONLY
    717 	if (argp->flags & NFSMNT_NQNFS)
    718 		/*
    719 		 * We have to set mnt_maxsymlink to a non-zero value so
    720 		 * that COMPAT_43 routines will know that we are setting
    721 		 * the d_type field in directories (and can zero it for
    722 		 * unsuspecting binaries).
    723 		 */
    724 		mp->mnt_maxsymlinklen = 1;
    725 #endif
    726 
    727 #ifndef NFS_V2_ONLY
    728 	if ((argp->flags & NFSMNT_NFSV3) == 0)
    729 #endif
    730 		/*
    731 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
    732 		 * will fill this in.
    733 		 */
    734 		nmp->nm_maxfilesize = 0xffffffffLL;
    735 
    736 	nmp->nm_timeo = NFS_TIMEO;
    737 	nmp->nm_retry = NFS_RETRANS;
    738 	nmp->nm_wsize = NFS_WSIZE;
    739 	nmp->nm_rsize = NFS_RSIZE;
    740 	nmp->nm_readdirsize = NFS_READDIRSIZE;
    741 	nmp->nm_numgrps = NFS_MAXGRPS;
    742 	nmp->nm_readahead = NFS_DEFRAHEAD;
    743 	nmp->nm_leaseterm = NQ_DEFLEASE;
    744 	nmp->nm_deadthresh = NQ_DEADTHRESH;
    745 	CIRCLEQ_INIT(&nmp->nm_timerhead);
    746 	nmp->nm_inprog = NULLVP;
    747 	error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p);
    748 	if (error)
    749 		goto bad;
    750 	nmp->nm_nam = nam;
    751 
    752 	/* Set up the sockets and per-host congestion */
    753 	nmp->nm_sotype = argp->sotype;
    754 	nmp->nm_soproto = argp->proto;
    755 
    756 	nfs_decode_args(nmp, argp, p);
    757 
    758 	mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
    759 	mp->mnt_dev_bshift = DEV_BSHIFT;
    760 
    761 	/*
    762 	 * For Connection based sockets (TCP,...) defer the connect until
    763 	 * the first request, in case the server is not responding.
    764 	 */
    765 	if (nmp->nm_sotype == SOCK_DGRAM &&
    766 		(error = nfs_connect(nmp, (struct nfsreq *)0, p)))
    767 		goto bad;
    768 
    769 	/*
    770 	 * This is silly, but it has to be set so that vinifod() works.
    771 	 * We do not want to do an nfs_statvfs() here since we can get
    772 	 * stuck on a dead server and we are holding a lock on the mount
    773 	 * point.
    774 	 */
    775 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
    776 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
    777 	if (error)
    778 		goto bad;
    779 	*vpp = NFSTOV(np);
    780 	MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
    781 	VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
    782 	if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
    783 		cr = crget();
    784 		cr->cr_uid = attrs->va_uid;
    785 		cr->cr_gid = attrs->va_gid;
    786 		cr->cr_ngroups = 0;
    787 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
    788 		crfree(cr);
    789 	}
    790 	FREE(attrs, M_TEMP);
    791 
    792 	/*
    793 	 * A reference count is needed on the nfsnode representing the
    794 	 * remote root.  If this object is not persistent, then backward
    795 	 * traversals of the mount point (i.e. "..") will not work if
    796 	 * the nfsnode gets flushed out of the cache. Ufs does not have
    797 	 * this problem, because one can identify root inodes by their
    798 	 * number == ROOTINO (2). So, just unlock, but no rele.
    799 	 */
    800 
    801 	nmp->nm_vnode = *vpp;
    802 	VOP_UNLOCK(*vpp, 0);
    803 
    804 	return (0);
    805 bad:
    806 	nfs_disconnect(nmp);
    807 	free((caddr_t)nmp, M_NFSMNT);
    808 	m_freem(nam);
    809 	return (error);
    810 }
    811 
    812 /*
    813  * unmount system call
    814  */
    815 int
    816 nfs_unmount(mp, mntflags, p)
    817 	struct mount *mp;
    818 	int mntflags;
    819 	struct proc *p;
    820 {
    821 	struct nfsmount *nmp;
    822 	struct vnode *vp;
    823 	int error, flags = 0;
    824 
    825 	if (mntflags & MNT_FORCE)
    826 		flags |= FORCECLOSE;
    827 	nmp = VFSTONFS(mp);
    828 	/*
    829 	 * Goes something like this..
    830 	 * - Check for activity on the root vnode (other than ourselves).
    831 	 * - Call vflush() to clear out vnodes for this file system,
    832 	 *   except for the root vnode.
    833 	 * - Decrement reference on the vnode representing remote root.
    834 	 * - Close the socket
    835 	 * - Free up the data structures
    836 	 */
    837 	/*
    838 	 * We need to decrement the ref. count on the nfsnode representing
    839 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
    840 	 * has done vput on this vnode, otherwise we would get deadlock!
    841 	 */
    842 	vp = nmp->nm_vnode;
    843 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    844 	if (error != 0)
    845 		return error;
    846 
    847 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
    848 		vput(vp);
    849 		return (EBUSY);
    850 	}
    851 
    852 	/*
    853 	 * Must handshake with nqnfs_clientd() if it is active.
    854 	 */
    855 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
    856 	while (nmp->nm_inprog != NULLVP)
    857 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
    858 	error = vflush(mp, vp, flags);
    859 	if (error) {
    860 		vput(vp);
    861 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
    862 		return (error);
    863 	}
    864 
    865 	/*
    866 	 * We are now committed to the unmount; mark the mount structure
    867 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
    868 	 * will go away cleanly.
    869 	 */
    870 	nmp->nm_iflag |= NFSMNT_DISMNT;
    871 
    872 	/*
    873 	 * There are two reference counts to get rid of here
    874 	 * (see comment in mountnfs()).
    875 	 */
    876 	vrele(vp);
    877 	vput(vp);
    878 	vgone(vp);
    879 	nfs_disconnect(nmp);
    880 	m_freem(nmp->nm_nam);
    881 
    882 	/*
    883 	 * For NQNFS, let the server daemon free the nfsmount structure.
    884 	 */
    885 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
    886 		free((caddr_t)nmp, M_NFSMNT);
    887 	return (0);
    888 }
    889 
    890 /*
    891  * Return root of a filesystem
    892  */
    893 int
    894 nfs_root(mp, vpp)
    895 	struct mount *mp;
    896 	struct vnode **vpp;
    897 {
    898 	struct vnode *vp;
    899 	struct nfsmount *nmp;
    900 	int error;
    901 
    902 	nmp = VFSTONFS(mp);
    903 	vp = nmp->nm_vnode;
    904 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    905 	if (error != 0)
    906 		return error;
    907 	if (vp->v_type == VNON)
    908 		vp->v_type = VDIR;
    909 	vp->v_flag = VROOT;
    910 	*vpp = vp;
    911 	return (0);
    912 }
    913 
    914 extern int syncprt;
    915 
    916 /*
    917  * Flush out the buffer cache
    918  */
    919 /* ARGSUSED */
    920 int
    921 nfs_sync(mp, waitfor, cred, p)
    922 	struct mount *mp;
    923 	int waitfor;
    924 	struct ucred *cred;
    925 	struct proc *p;
    926 {
    927 	struct vnode *vp;
    928 	int error, allerror = 0;
    929 
    930 	/*
    931 	 * Force stale buffer cache information to be flushed.
    932 	 */
    933 loop:
    934 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    935 		/*
    936 		 * If the vnode that we are about to sync is no longer
    937 		 * associated with this mount point, start over.
    938 		 */
    939 		if (vp->v_mount != mp)
    940 			goto loop;
    941 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
    942 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    943 		     vp->v_uobj.uo_npages == 0))
    944 			continue;
    945 		if (vget(vp, LK_EXCLUSIVE))
    946 			goto loop;
    947 		error = VOP_FSYNC(vp, cred,
    948 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
    949 		if (error)
    950 			allerror = error;
    951 		vput(vp);
    952 	}
    953 	return (allerror);
    954 }
    955 
    956 /*
    957  * NFS flat namespace lookup.
    958  * Currently unsupported.
    959  */
    960 /* ARGSUSED */
    961 int
    962 nfs_vget(mp, ino, vpp)
    963 	struct mount *mp;
    964 	ino_t ino;
    965 	struct vnode **vpp;
    966 {
    967 
    968 	return (EOPNOTSUPP);
    969 }
    970 
    971 /*
    972  * Do that sysctl thang...
    973  */
    974 static int
    975 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
    976 {
    977 	int error;
    978 
    979 	nfs_getset_niothreads(0);
    980         error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    981 	if (error || newp == NULL)
    982 		return (error);
    983 	nfs_getset_niothreads(1);
    984 
    985 	return (0);
    986 }
    987 
    988 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
    989 {
    990 
    991 	sysctl_createv(clog, 0, NULL, NULL,
    992 		       CTLFLAG_PERMANENT,
    993 		       CTLTYPE_NODE, "vfs", NULL,
    994 		       NULL, 0, NULL, 0,
    995 		       CTL_VFS, CTL_EOL);
    996 	sysctl_createv(clog, 0, NULL, NULL,
    997 		       CTLFLAG_PERMANENT,
    998 		       CTLTYPE_NODE, "nfs",
    999 		       SYSCTL_DESCR("NFS vfs options"),
   1000 		       NULL, 0, NULL, 0,
   1001 		       CTL_VFS, 2, CTL_EOL);
   1002 	/*
   1003 	 * XXX the "2" above could be dynamic, thereby eliminating one
   1004 	 * more instance of the "number to vfs" mapping problem, but
   1005 	 * "2" is the order as taken from sys/mount.h
   1006 	 */
   1007 
   1008 	sysctl_createv(clog, 0, NULL, NULL,
   1009 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1010 		       CTLTYPE_STRUCT, "nfsstats",
   1011 		       SYSCTL_DESCR("NFS operation statistics"),
   1012 		       NULL, 0, &nfsstats, sizeof(nfsstats),
   1013 		       CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
   1014 	sysctl_createv(clog, 0, NULL, NULL,
   1015 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1016 		       CTLTYPE_INT, "iothreads",
   1017 		       SYSCTL_DESCR("Number of NFS client processes desired"),
   1018 		       sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
   1019 		       CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
   1020 }
   1021 
   1022 /*
   1023  * At this point, this should never happen
   1024  */
   1025 /* ARGSUSED */
   1026 int
   1027 nfs_fhtovp(mp, fhp, vpp)
   1028 	struct mount *mp;
   1029 	struct fid *fhp;
   1030 	struct vnode **vpp;
   1031 {
   1032 
   1033 	return (EINVAL);
   1034 }
   1035 
   1036 /* ARGSUSED */
   1037 int
   1038 nfs_checkexp(mp, nam, exflagsp, credanonp)
   1039 	struct mount *mp;
   1040 	struct mbuf *nam;
   1041 	int *exflagsp;
   1042 	struct ucred **credanonp;
   1043 {
   1044 
   1045 	return (EINVAL);
   1046 }
   1047 
   1048 /*
   1049  * Vnode pointer to File handle, should never happen either
   1050  */
   1051 /* ARGSUSED */
   1052 int
   1053 nfs_vptofh(vp, fhp)
   1054 	struct vnode *vp;
   1055 	struct fid *fhp;
   1056 {
   1057 
   1058 	return (EINVAL);
   1059 }
   1060 
   1061 /*
   1062  * Vfs start routine, a no-op.
   1063  */
   1064 /* ARGSUSED */
   1065 int
   1066 nfs_start(mp, flags, p)
   1067 	struct mount *mp;
   1068 	int flags;
   1069 	struct proc *p;
   1070 {
   1071 
   1072 	return (0);
   1073 }
   1074 
   1075 /*
   1076  * Do operations associated with quotas, not supported
   1077  */
   1078 /* ARGSUSED */
   1079 int
   1080 nfs_quotactl(mp, cmd, uid, arg, p)
   1081 	struct mount *mp;
   1082 	int cmd;
   1083 	uid_t uid;
   1084 	void *arg;
   1085 	struct proc *p;
   1086 {
   1087 
   1088 	return (EOPNOTSUPP);
   1089 }
   1090