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