Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.131.2.8
      1 /*	$NetBSD: nfs_vfsops.c,v 1.131.2.8 2005/03/04 16:54:21 skrll 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.131.2.8 2005/03/04 16:54:21 skrll 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 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    116 	vfs_stdextattrctl,
    117 	nfs_vnodeopv_descs,
    118 };
    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, max;
    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 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
    256 		if (max < nmp->nm_wsize && max > 0) {
    257 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
    258 			if (nmp->nm_wsize == 0)
    259 				nmp->nm_wsize = max;
    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 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
    267 		if (max < nmp->nm_rsize && max > 0) {
    268 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
    269 			if (nmp->nm_rsize == 0)
    270 				nmp->nm_rsize = max;
    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 (max < nmp->nm_readdirsize && max > 0) {
    277 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
    278 			if (nmp->nm_readdirsize == 0)
    279 				nmp->nm_readdirsize = max;
    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 	struct nfs_diskless *nd;
    304 	struct vattr attr;
    305 	struct mount *mp;
    306 	struct vnode *vp;
    307 	struct lwp *l;
    308 	long n;
    309 	int error;
    310 
    311 	l = curlwp; /* XXX */
    312 
    313 	if (root_device->dv_class != DV_IFNET)
    314 		return (ENODEV);
    315 
    316 	/*
    317 	 * XXX time must be non-zero when we init the interface or else
    318 	 * the arp code will wedge.  [Fixed now in if_ether.c]
    319 	 * However, the NFS attribute cache gives false "hits" when
    320 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
    321 	 */
    322 	if (time.tv_sec < NFS_MAXATTRTIMO)
    323 		time.tv_sec = NFS_MAXATTRTIMO;
    324 
    325 	/*
    326 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
    327 	 * Side effect:  Finds and configures a network interface.
    328 	 */
    329 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
    330 	memset((caddr_t)nd, 0, sizeof(*nd));
    331 	error = nfs_boot_init(nd, l);
    332 	if (error) {
    333 		free(nd, M_NFSMNT);
    334 		return (error);
    335 	}
    336 
    337 	/*
    338 	 * Create the root mount point.
    339 	 */
    340 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
    341 	if (error)
    342 		goto out;
    343 	printf("root on %s\n", nd->nd_root.ndm_host);
    344 
    345 	/*
    346 	 * Link it into the mount list.
    347 	 */
    348 	simple_lock(&mountlist_slock);
    349 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    350 	simple_unlock(&mountlist_slock);
    351 	rootvp = vp;
    352 	mp->mnt_vnodecovered = NULLVP;
    353 	vfs_unbusy(mp);
    354 
    355 	/* Get root attributes (for the time). */
    356 	error = VOP_GETATTR(vp, &attr, l->l_proc->p_ucred, l);
    357 	if (error)
    358 		panic("nfs_mountroot: getattr for root");
    359 	n = attr.va_atime.tv_sec;
    360 #ifdef	DEBUG
    361 	printf("root time: 0x%lx\n", n);
    362 #endif
    363 	setrootfstime(n);
    364 
    365 out:
    366 	if (error)
    367 		nfs_boot_cleanup(nd, l);
    368 	free(nd, M_NFSMNT);
    369 	return (error);
    370 }
    371 
    372 /*
    373  * Internal version of mount system call for diskless setup.
    374  * Separate function because we used to call it twice.
    375  * (once for root and once for swap)
    376  */
    377 static int
    378 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, l)
    379 	struct nfs_dlmount *ndmntp;
    380 	const char *mntname;	/* mount point name */
    381 	struct mount **mpp;
    382 	struct vnode **vpp;
    383 	struct lwp *l;
    384 {
    385 	struct mount *mp;
    386 	struct mbuf *m;
    387 	int error;
    388 
    389 	vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
    390 
    391 	mp->mnt_op = &nfs_vfsops;
    392 
    393 	/*
    394 	 * Historical practice expects NFS root file systems to
    395 	 * be initially mounted r/w.
    396 	 */
    397 	mp->mnt_flag &= ~MNT_RDONLY;
    398 
    399 	/* Get mbuf for server sockaddr. */
    400 	m = m_get(M_WAIT, MT_SONAME);
    401 	if (m == NULL)
    402 		panic("nfs_mountroot: mget soname for %s", mntname);
    403 	MCLAIM(m, &nfs_mowner);
    404 	memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
    405 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
    406 
    407 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
    408 			 ndmntp->ndm_args.hostname, vpp, l);
    409 	if (error) {
    410 		mp->mnt_op->vfs_refcount--;
    411 		vfs_unbusy(mp);
    412 		printf("nfs_mountroot: mount %s failed: %d\n",
    413 		       mntname, error);
    414 		free(mp, M_MOUNT);
    415 	} else
    416 		*mpp = mp;
    417 
    418 	return (error);
    419 }
    420 
    421 void
    422 nfs_decode_args(nmp, argp, l)
    423 	struct nfsmount *nmp;
    424 	struct nfs_args *argp;
    425 	struct lwp *l;
    426 {
    427 	int s;
    428 	int adjsock;
    429 	int maxio;
    430 
    431 	s = splsoftnet();
    432 
    433 	/*
    434 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
    435 	 * no sense in that context.
    436 	 */
    437 	if (argp->sotype == SOCK_STREAM)
    438 		argp->flags &= ~NFSMNT_NOCONN;
    439 
    440 	/*
    441 	 * Cookie translation is not needed for v2, silently ignore it.
    442 	 */
    443 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
    444 	    NFSMNT_XLATECOOKIE)
    445 		argp->flags &= ~NFSMNT_XLATECOOKIE;
    446 
    447 	/* Re-bind if rsrvd port requested and wasn't on one */
    448 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
    449 		  && (argp->flags & NFSMNT_RESVPORT);
    450 	/* Also re-bind if we're switching to/from a connected UDP socket */
    451 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
    452 		    (argp->flags & NFSMNT_NOCONN));
    453 
    454 	/* Update flags. */
    455 	nmp->nm_flag = argp->flags;
    456 	splx(s);
    457 
    458 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
    459 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
    460 		if (nmp->nm_timeo < NFS_MINTIMEO)
    461 			nmp->nm_timeo = NFS_MINTIMEO;
    462 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
    463 			nmp->nm_timeo = NFS_MAXTIMEO;
    464 	}
    465 
    466 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
    467 		nmp->nm_retry = argp->retrans;
    468 		if (nmp->nm_retry > NFS_MAXREXMIT)
    469 			nmp->nm_retry = NFS_MAXREXMIT;
    470 	}
    471 
    472 #ifndef NFS_V2_ONLY
    473 	if (argp->flags & NFSMNT_NFSV3) {
    474 		if (argp->sotype == SOCK_DGRAM)
    475 			maxio = NFS_MAXDGRAMDATA;
    476 		else
    477 			maxio = NFS_MAXDATA;
    478 	} else
    479 #endif
    480 		maxio = NFS_V2MAXDATA;
    481 
    482 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
    483 		int osize = nmp->nm_wsize;
    484 		nmp->nm_wsize = argp->wsize;
    485 		/* Round down to multiple of blocksize */
    486 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
    487 		if (nmp->nm_wsize <= 0)
    488 			nmp->nm_wsize = NFS_FABLKSIZE;
    489 		adjsock |= (nmp->nm_wsize != osize);
    490 	}
    491 	if (nmp->nm_wsize > maxio)
    492 		nmp->nm_wsize = maxio;
    493 	if (nmp->nm_wsize > MAXBSIZE)
    494 		nmp->nm_wsize = MAXBSIZE;
    495 
    496 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
    497 		int osize = nmp->nm_rsize;
    498 		nmp->nm_rsize = argp->rsize;
    499 		/* Round down to multiple of blocksize */
    500 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
    501 		if (nmp->nm_rsize <= 0)
    502 			nmp->nm_rsize = NFS_FABLKSIZE;
    503 		adjsock |= (nmp->nm_rsize != osize);
    504 	}
    505 	if (nmp->nm_rsize > maxio)
    506 		nmp->nm_rsize = maxio;
    507 	if (nmp->nm_rsize > MAXBSIZE)
    508 		nmp->nm_rsize = MAXBSIZE;
    509 
    510 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
    511 		nmp->nm_readdirsize = argp->readdirsize;
    512 		/* Round down to multiple of minimum blocksize */
    513 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
    514 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
    515 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
    516 		/* Bigger than buffer size makes no sense */
    517 		if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
    518 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
    519 	} else if (argp->flags & NFSMNT_RSIZE)
    520 		nmp->nm_readdirsize = nmp->nm_rsize;
    521 
    522 	if (nmp->nm_readdirsize > maxio)
    523 		nmp->nm_readdirsize = maxio;
    524 
    525 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
    526 		argp->maxgrouplist <= NFS_MAXGRPS)
    527 		nmp->nm_numgrps = argp->maxgrouplist;
    528 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
    529 		argp->readahead <= NFS_MAXRAHEAD)
    530 		nmp->nm_readahead = argp->readahead;
    531 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
    532 		argp->leaseterm <= NQ_MAXLEASE)
    533 		nmp->nm_leaseterm = argp->leaseterm;
    534 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
    535 		argp->deadthresh <= NQ_NEVERDEAD)
    536 		nmp->nm_deadthresh = argp->deadthresh;
    537 
    538 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
    539 		    (nmp->nm_soproto != argp->proto));
    540 	nmp->nm_sotype = argp->sotype;
    541 	nmp->nm_soproto = argp->proto;
    542 
    543 	if (nmp->nm_so && adjsock) {
    544 		nfs_safedisconnect(nmp);
    545 		if (nmp->nm_sotype == SOCK_DGRAM)
    546 			while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
    547 				printf("nfs_args: retrying connect\n");
    548 				(void) tsleep((caddr_t)&lbolt,
    549 					      PSOCK, "nfscn3", 0);
    550 			}
    551 	}
    552 }
    553 
    554 /*
    555  * VFS Operations.
    556  *
    557  * mount system call
    558  * It seems a bit dumb to copyinstr() the host and path here and then
    559  * memcpy() them in mountnfs(), but I wanted to detect errors before
    560  * doing the sockargs() call because sockargs() allocates an mbuf and
    561  * an error after that means that I have to release the mbuf.
    562  */
    563 /* ARGSUSED */
    564 int
    565 nfs_mount(mp, path, data, ndp, l)
    566 	struct mount *mp;
    567 	const char *path;
    568 	void *data;
    569 	struct nameidata *ndp;
    570 	struct lwp *l;
    571 {
    572 	int error;
    573 	struct nfs_args args;
    574 	struct mbuf *nam;
    575 	struct nfsmount *nmp = VFSTONFS(mp);
    576 	struct sockaddr *sa;
    577 	struct vnode *vp;
    578 	char *pth, *hst;
    579 	struct proc *p;
    580 	size_t len;
    581 	u_char *nfh;
    582 
    583 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
    584 	if (error)
    585 		return (error);
    586 
    587 	p = l->l_proc;
    588 	if (mp->mnt_flag & MNT_GETARGS) {
    589 
    590 		if (nmp == NULL)
    591 			return (EIO);
    592 		if (args.addr != NULL) {
    593 			sa = mtod(nmp->nm_nam, struct sockaddr *);
    594 			error = copyout(sa, args.addr, sa->sa_len);
    595 			if (error)
    596 				return (error);
    597 			args.addrlen = sa->sa_len;
    598 		} else
    599 			args.addrlen = 0;
    600 
    601 		args.version = NFS_ARGSVERSION;
    602 		args.sotype = nmp->nm_sotype;
    603 		args.proto = nmp->nm_soproto;
    604 		args.fh = NULL;
    605 		args.fhsize = 0;
    606 		args.flags = nmp->nm_flag;
    607 		args.wsize = nmp->nm_wsize;
    608 		args.rsize = nmp->nm_rsize;
    609 		args.readdirsize = nmp->nm_readdirsize;
    610 		args.timeo = nmp->nm_timeo;
    611 		args.retrans = nmp->nm_retry;
    612 		args.maxgrouplist = nmp->nm_numgrps;
    613 		args.readahead = nmp->nm_readahead;
    614 		args.leaseterm = nmp->nm_leaseterm;
    615 		args.deadthresh = nmp->nm_deadthresh;
    616 		args.hostname = NULL;
    617 		return (copyout(&args, data, sizeof(args)));
    618 	}
    619 
    620 	if (args.version != NFS_ARGSVERSION)
    621 		return (EPROGMISMATCH);
    622 #ifdef NFS_V2_ONLY
    623 	if (args.flags & NFSMNT_NQNFS)
    624 		return (EPROGUNAVAIL);
    625 	if (args.flags & NFSMNT_NFSV3)
    626 		return (EPROGMISMATCH);
    627 #endif
    628 	if (mp->mnt_flag & MNT_UPDATE) {
    629 		if (nmp == NULL)
    630 			return (EIO);
    631 		/*
    632 		 * When doing an update, we can't change from or to
    633 		 * v3 and/or nqnfs, or change cookie translation
    634 		 */
    635 		args.flags = (args.flags &
    636 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
    637 		    (nmp->nm_flag &
    638 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
    639 		nfs_decode_args(nmp, &args, l);
    640 		return (0);
    641 	}
    642 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
    643 		return (EINVAL);
    644 	MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
    645 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
    646 	if (error)
    647 		return (error);
    648 	MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
    649 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
    650 	if (error)
    651 		goto free_nfh;
    652 	memset(&pth[len], 0, MNAMELEN - len);
    653 	MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
    654 	error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
    655 	if (error)
    656 		goto free_pth;
    657 	memset(&hst[len], 0, MNAMELEN - len);
    658 	/* sockargs() call must be after above copyin() calls */
    659 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
    660 	if (error)
    661 		goto free_hst;
    662 	MCLAIM(nam, &nfs_mowner);
    663 	args.fh = nfh;
    664 	error = mountnfs(&args, mp, nam, pth, hst, &vp, l);
    665 
    666 free_hst:
    667 	FREE(hst, M_TEMP);
    668 free_pth:
    669 	FREE(pth, M_TEMP);
    670 free_nfh:
    671 	FREE(nfh, M_TEMP);
    672 
    673 	return (error);
    674 }
    675 
    676 /*
    677  * Common code for mount and mountroot
    678  */
    679 int
    680 mountnfs(argp, mp, nam, pth, hst, vpp, l)
    681 	struct nfs_args *argp;
    682 	struct mount *mp;
    683 	struct mbuf *nam;
    684 	const char *pth, *hst;
    685 	struct vnode **vpp;
    686 	struct lwp *l;
    687 {
    688 	struct nfsmount *nmp;
    689 	struct nfsnode *np;
    690 	int error;
    691 	struct vattr *attrs;
    692 	struct ucred *cr;
    693 
    694 	/*
    695 	 * If the number of nfs iothreads to use has never
    696 	 * been set, create a reasonable number of them.
    697 	 */
    698 
    699 	if (nfs_niothreads < 0) {
    700 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
    701 		nfs_getset_niothreads(TRUE);
    702 	}
    703 
    704 	if (mp->mnt_flag & MNT_UPDATE) {
    705 		nmp = VFSTONFS(mp);
    706 		/* update paths, file handles, etc, here	XXX */
    707 		m_freem(nam);
    708 		return (0);
    709 	} else {
    710 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
    711 		    M_NFSMNT, M_WAITOK);
    712 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
    713 		mp->mnt_data = nmp;
    714 		TAILQ_INIT(&nmp->nm_uidlruhead);
    715 		TAILQ_INIT(&nmp->nm_bufq);
    716 		lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
    717 		simple_lock_init(&nmp->nm_slock);
    718 	}
    719 	vfs_getnewfsid(mp);
    720 	nmp->nm_mountp = mp;
    721 
    722 #ifndef NFS_V2_ONLY
    723 	if (argp->flags & NFSMNT_NQNFS)
    724 		mp->mnt_iflag |= IMNT_DTYPE;
    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, l);
    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, l);
    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, l)))
    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, l);
    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, l->l_proc->p_ucred, l);
    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, l, 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, l)
    817 	struct mount *mp;
    818 	int mntflags;
    819 	struct lwp *l;
    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, l)
    922 	struct mount *mp;
    923 	int waitfor;
    924 	struct ucred *cred;
    925 	struct lwp *l;
    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, l);
    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, l)
   1067 	struct mount *mp;
   1068 	int flags;
   1069 	struct lwp *l;
   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, l)
   1081 	struct mount *mp;
   1082 	int cmd;
   1083 	uid_t uid;
   1084 	void *arg;
   1085 	struct lwp *l;
   1086 {
   1087 
   1088 	return (EOPNOTSUPP);
   1089 }
   1090