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