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