Home | History | Annotate | Line # | Download | only in nfs
nfs_serv.c revision 1.95.2.8
      1 /*	$NetBSD: nfs_serv.c,v 1.95.2.8 2008/01/21 09:47:33 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      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_serv.c	8.8 (Berkeley) 7/31/95
     35  */
     36 
     37 /*
     38  * nfs version 2 and 3 server calls to vnode ops
     39  * - these routines generally have 3 phases
     40  *   1 - break down and validate rpc request in mbuf list
     41  *   2 - do the vnode ops for the request
     42  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
     43  *   3 - build the rpc reply in an mbuf list
     44  *   nb:
     45  *	- do not mix the phases, since the nfsm_?? macros can return failures
     46  *	  on a bad rpc or similar and do not do any vrele() or vput()'s
     47  *
     48  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
     49  *	error number iff error != 0 whereas
     50  *	returning an error from the server function implies a fatal error
     51  *	such as a badly constructed rpc request that should be dropped without
     52  *	a reply.
     53  *	For Version 3, nfsm_reply() does not return for the error case, since
     54  *	most version 3 rpcs return more than the status for error cases.
     55  */
     56 
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.95.2.8 2008/01/21 09:47:33 yamt Exp $");
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/proc.h>
     63 #include <sys/file.h>
     64 #include <sys/namei.h>
     65 #include <sys/vnode.h>
     66 #include <sys/mount.h>
     67 #include <sys/socket.h>
     68 #include <sys/socketvar.h>
     69 #include <sys/mbuf.h>
     70 #include <sys/dirent.h>
     71 #include <sys/stat.h>
     72 #include <sys/kernel.h>
     73 #include <sys/hash.h>
     74 #include <sys/kauth.h>
     75 
     76 #include <uvm/uvm.h>
     77 
     78 #include <nfs/nfsproto.h>
     79 #include <nfs/rpcv2.h>
     80 #include <nfs/nfs.h>
     81 #include <nfs/xdr_subs.h>
     82 #include <nfs/nfsm_subs.h>
     83 #include <nfs/nfs_var.h>
     84 
     85 /* Global vars */
     86 extern u_int32_t nfs_xdrneg1;
     87 extern u_int32_t nfs_false, nfs_true;
     88 extern enum vtype nv3tov_type[8];
     89 extern struct nfsstats nfsstats;
     90 extern const nfstype nfsv2_type[9];
     91 extern const nfstype nfsv3_type[9];
     92 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
     93 int nfsd_use_loan = 1;	/* use page-loan for READ OP */
     94 
     95 #define	nqsrv_getl(vp, rw)	/* nothing */
     96 
     97 /*
     98  * nfs v3 access service
     99  */
    100 int
    101 nfsrv3_access(nfsd, slp, lwp, mrq)
    102 	struct nfsrv_descript *nfsd;
    103 	struct nfssvc_sock *slp;
    104 	struct lwp *lwp;
    105 	struct mbuf **mrq;
    106 {
    107 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    108 	struct mbuf *nam = nfsd->nd_nam;
    109 	char *dpos = nfsd->nd_dpos;
    110 	kauth_cred_t cred = nfsd->nd_cr;
    111 	struct vnode *vp;
    112 	nfsrvfh_t nsfh;
    113 	u_int32_t *tl;
    114 	int32_t t1;
    115 	char *bpos;
    116 	int error = 0, rdonly, cache = 0, getret;
    117 	char *cp2;
    118 	struct mbuf *mb, *mreq;
    119 	struct vattr va;
    120 	u_long inmode, testmode, outmode;
    121 	u_quad_t frev;
    122 
    123 	nfsm_srvmtofh(&nsfh);
    124 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    125 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
    126 	    (nfsd->nd_flag & ND_KERBAUTH), false);
    127 	if (error) {
    128 		nfsm_reply(NFSX_UNSIGNED);
    129 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    130 		return (0);
    131 	}
    132 	inmode = fxdr_unsigned(u_int32_t, *tl);
    133 	outmode = 0;
    134 	if ((inmode & NFSV3ACCESS_READ) &&
    135 	    nfsrv_access(vp, VREAD, cred, rdonly, lwp, 0) == 0)
    136 		outmode |= NFSV3ACCESS_READ;
    137 	if (vp->v_type != VDIR) {
    138 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
    139 		if (testmode &&
    140 		    nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
    141 			outmode |= testmode;
    142 		if ((inmode & NFSV3ACCESS_EXECUTE) &&
    143 		    nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
    144 			outmode |= NFSV3ACCESS_EXECUTE;
    145 	} else {
    146 		testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
    147 		    NFSV3ACCESS_DELETE);
    148 		if (testmode &&
    149 		    nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
    150 			outmode |= testmode;
    151 		if ((inmode & NFSV3ACCESS_LOOKUP) &&
    152 		    nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
    153 			outmode |= NFSV3ACCESS_LOOKUP;
    154 	}
    155 	getret = VOP_GETATTR(vp, &va, cred);
    156 	vput(vp);
    157 	nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
    158 	nfsm_srvpostop_attr(getret, &va);
    159 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    160 	*tl = txdr_unsigned(outmode);
    161 	nfsm_srvdone;
    162 }
    163 
    164 /*
    165  * nfs getattr service
    166  */
    167 int
    168 nfsrv_getattr(nfsd, slp, lwp, mrq)
    169 	struct nfsrv_descript *nfsd;
    170 	struct nfssvc_sock *slp;
    171 	struct lwp *lwp;
    172 	struct mbuf **mrq;
    173 {
    174 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    175 	struct mbuf *nam = nfsd->nd_nam;
    176 	char *dpos = nfsd->nd_dpos;
    177 	kauth_cred_t cred = nfsd->nd_cr;
    178 	struct nfs_fattr *fp;
    179 	struct vattr va;
    180 	struct vnode *vp;
    181 	nfsrvfh_t nsfh;
    182 	u_int32_t *tl;
    183 	int32_t t1;
    184 	char *bpos;
    185 	int error = 0, rdonly, cache = 0;
    186 	char *cp2;
    187 	struct mbuf *mb, *mreq;
    188 	u_quad_t frev;
    189 
    190 	nfsm_srvmtofh(&nsfh);
    191 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
    192 	    (nfsd->nd_flag & ND_KERBAUTH), false);
    193 	if (error) {
    194 		nfsm_reply(0);
    195 		return (0);
    196 	}
    197 	nqsrv_getl(vp, ND_READ);
    198 	error = VOP_GETATTR(vp, &va, cred);
    199 	vput(vp);
    200 	nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
    201 	if (error)
    202 		return (0);
    203 	nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
    204 	nfsm_srvfillattr(&va, fp);
    205 	nfsm_srvdone;
    206 }
    207 
    208 /*
    209  * nfs setattr service
    210  */
    211 int
    212 nfsrv_setattr(nfsd, slp, lwp, mrq)
    213 	struct nfsrv_descript *nfsd;
    214 	struct nfssvc_sock *slp;
    215 	struct lwp *lwp;
    216 	struct mbuf **mrq;
    217 {
    218 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    219 	struct mbuf *nam = nfsd->nd_nam;
    220 	char *dpos = nfsd->nd_dpos;
    221 	kauth_cred_t cred = nfsd->nd_cr;
    222 	struct vattr va, preat;
    223 	struct nfsv2_sattr *sp;
    224 	struct nfs_fattr *fp;
    225 	struct vnode *vp;
    226 	nfsrvfh_t nsfh;
    227 	u_int32_t *tl;
    228 	int32_t t1;
    229 	char *bpos;
    230 	int error = 0, rdonly, cache = 0, preat_ret = 1, postat_ret = 1;
    231 	int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
    232 	char *cp2;
    233 	struct mbuf *mb, *mreq;
    234 	u_quad_t frev;
    235 	struct timespec guard;
    236 
    237 	memset(&guard, 0, sizeof guard);	/* XXX gcc */
    238 
    239 	nfsm_srvmtofh(&nsfh);
    240 	VATTR_NULL(&va);
    241 	if (v3) {
    242 		nfsm_srvsattr(&va);
    243 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    244 		gcheck = fxdr_unsigned(int, *tl);
    245 		if (gcheck) {
    246 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    247 			fxdr_nfsv3time(tl, &guard);
    248 		}
    249 	} else {
    250 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
    251 		/*
    252 		 * Nah nah nah nah na nah
    253 		 * There is a bug in the Sun client that puts 0xffff in the mode
    254 		 * field of sattr when it should put in 0xffffffff. The u_short
    255 		 * doesn't sign extend.
    256 		 * --> check the low order 2 bytes for 0xffff
    257 		 */
    258 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
    259 			va.va_mode = nfstov_mode(sp->sa_mode);
    260 		if (sp->sa_uid != nfs_xdrneg1)
    261 			va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
    262 		if (sp->sa_gid != nfs_xdrneg1)
    263 			va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
    264 		if (sp->sa_size != nfs_xdrneg1)
    265 			va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
    266 		if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
    267 #ifdef notyet
    268 			fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
    269 #else
    270 			va.va_atime.tv_sec =
    271 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
    272 			va.va_atime.tv_nsec = 0;
    273 #endif
    274 		}
    275 		if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
    276 			fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
    277 
    278 	}
    279 
    280 	/*
    281 	 * Now that we have all the fields, lets do it.
    282 	 */
    283 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
    284 	    (nfsd->nd_flag & ND_KERBAUTH), false);
    285 	if (error) {
    286 		nfsm_reply(2 * NFSX_UNSIGNED);
    287 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    288 		return (0);
    289 	}
    290 	nqsrv_getl(vp, ND_WRITE);
    291 	if (v3) {
    292 		error = preat_ret = VOP_GETATTR(vp, &preat, cred);
    293 		if (!error && gcheck &&
    294 			(preat.va_ctime.tv_sec != guard.tv_sec ||
    295 			 preat.va_ctime.tv_nsec != guard.tv_nsec))
    296 			error = NFSERR_NOT_SYNC;
    297 		if (error) {
    298 			vput(vp);
    299 			nfsm_reply(NFSX_WCCDATA(v3));
    300 			nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    301 			return (0);
    302 		}
    303 	}
    304 
    305 	/*
    306 	 * If the size is being changed write acces is required, otherwise
    307 	 * just check for a read only file system.
    308 	 */
    309 	if (va.va_size == ((u_quad_t)((quad_t) -1))) {
    310 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    311 			error = EROFS;
    312 			goto out;
    313 		}
    314 	} else {
    315 		if (vp->v_type == VDIR) {
    316 			error = EISDIR;
    317 			goto out;
    318 		} else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
    319 			lwp, 0)) != 0)
    320 			goto out;
    321 	}
    322 	error = VOP_SETATTR(vp, &va, cred);
    323 	postat_ret = VOP_GETATTR(vp, &va, cred);
    324 	if (!error)
    325 		error = postat_ret;
    326 out:
    327 	vput(vp);
    328 	nfsm_reply(NFSX_WCCORFATTR(v3));
    329 	if (v3) {
    330 		nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
    331 		return (0);
    332 	} else {
    333 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    334 		nfsm_srvfillattr(&va, fp);
    335 	}
    336 	nfsm_srvdone;
    337 }
    338 
    339 /*
    340  * nfs lookup rpc
    341  */
    342 int
    343 nfsrv_lookup(nfsd, slp, lwp, mrq)
    344 	struct nfsrv_descript *nfsd;
    345 	struct nfssvc_sock *slp;
    346 	struct lwp *lwp;
    347 	struct mbuf **mrq;
    348 {
    349 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    350 	struct mbuf *nam = nfsd->nd_nam;
    351 	char *dpos = nfsd->nd_dpos;
    352 	kauth_cred_t cred = nfsd->nd_cr;
    353 	struct nfs_fattr *fp;
    354 	struct nameidata nd, ind, *ndp = &nd;
    355 	struct vnode *vp, *dirp;
    356 	nfsrvfh_t nsfh;
    357 	char *cp;
    358 	u_int32_t *tl;
    359 	int32_t t1;
    360 	char *bpos;
    361 	int error = 0, cache = 0, dirattr_ret = 1;
    362 	uint32_t len;
    363 	int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
    364 	char *cp2;
    365 	struct mbuf *mb, *mreq;
    366 	struct vattr va, dirattr;
    367 	u_quad_t frev;
    368 
    369 	nfsm_srvmtofh(&nsfh);
    370 	nfsm_srvnamesiz(len);
    371 
    372 	pubflag = nfs_ispublicfh(&nsfh);
    373 
    374 	nd.ni_cnd.cn_cred = cred;
    375 	nd.ni_cnd.cn_nameiop = LOOKUP;
    376 	nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
    377 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
    378 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
    379 
    380 	if (!error && pubflag) {
    381 		if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
    382 			/*
    383 			 * Setup call to lookup() to see if we can find
    384 			 * the index file. Arguably, this doesn't belong
    385 			 * in a kernel.. Ugh.
    386 			 */
    387 			ind = nd;
    388 			VOP_UNLOCK(nd.ni_vp, 0);
    389 			ind.ni_pathlen = strlen(nfs_pub.np_index);
    390 			ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
    391 			    nfs_pub.np_index;
    392 			ind.ni_startdir = nd.ni_vp;
    393 			VREF(ind.ni_startdir);
    394 			error = lookup(&ind);
    395 			if (!error) {
    396 				/*
    397 				 * Found an index file. Get rid of
    398 				 * the old references.
    399 				 */
    400 				if (dirp)
    401 					vrele(dirp);
    402 				dirp = nd.ni_vp;
    403 				vrele(nd.ni_startdir);
    404 				ndp = &ind;
    405 			} else
    406 				error = 0;
    407 		}
    408 		/*
    409 		 * If the public filehandle was used, check that this lookup
    410 		 * didn't result in a filehandle outside the publicly exported
    411 		 * filesystem.
    412 		 */
    413 
    414 		if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) {
    415 			vput(nd.ni_vp);
    416 			error = EPERM;
    417 		}
    418 	}
    419 
    420 	if (dirp) {
    421 		if (v3)
    422 			dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred);
    423 		vrele(dirp);
    424 	}
    425 
    426 	if (error) {
    427 		nfsm_reply(NFSX_POSTOPATTR(v3));
    428 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    429 		return (0);
    430 	}
    431 
    432 	nqsrv_getl(ndp->ni_startdir, ND_READ);
    433 	PNBUF_PUT(nd.ni_cnd.cn_pnbuf);
    434 	vp = ndp->ni_vp;
    435 	error = nfsrv_composefh(vp, &nsfh, v3);
    436 	if (!error)
    437 		error = VOP_GETATTR(vp, &va, cred);
    438 	vput(vp);
    439 	vrele(ndp->ni_startdir);
    440 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPORFATTR(v3) +
    441 	    NFSX_POSTOPATTR(v3));
    442 	if (error) {
    443 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    444 		return (0);
    445 	}
    446 	nfsm_srvfhtom(&nsfh, v3);
    447 	if (v3) {
    448 		nfsm_srvpostop_attr(0, &va);
    449 		nfsm_srvpostop_attr(dirattr_ret, &dirattr);
    450 	} else {
    451 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    452 		nfsm_srvfillattr(&va, fp);
    453 	}
    454 	nfsm_srvdone;
    455 }
    456 
    457 /*
    458  * nfs readlink service
    459  */
    460 int
    461 nfsrv_readlink(nfsd, slp, lwp, mrq)
    462 	struct nfsrv_descript *nfsd;
    463 	struct nfssvc_sock *slp;
    464 	struct lwp *lwp;
    465 	struct mbuf **mrq;
    466 {
    467 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    468 	struct mbuf *nam = nfsd->nd_nam;
    469 	char *dpos = nfsd->nd_dpos;
    470 	kauth_cred_t cred = nfsd->nd_cr;
    471 	struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
    472 	struct iovec *ivp = iv;
    473 	struct mbuf *mp;
    474 	u_int32_t *tl;
    475 	int32_t t1;
    476 	char *bpos;
    477 	int error = 0, rdonly, cache = 0, i, padlen, getret;
    478 	uint32_t len;
    479 	int v3 = (nfsd->nd_flag & ND_NFSV3);
    480 	char *cp2;
    481 	struct mbuf *mb, *mp2 = NULL, *mp3 = NULL, *mreq;
    482 	struct vnode *vp;
    483 	struct vattr attr;
    484 	nfsrvfh_t nsfh;
    485 	struct uio io, *uiop = &io;
    486 	u_quad_t frev;
    487 
    488 	nfsm_srvmtofh(&nsfh);
    489 	len = 0;
    490 	i = 0;
    491 	while (len < NFS_MAXPATHLEN) {
    492 		mp = m_get(M_WAIT, MT_DATA);
    493 		MCLAIM(mp, &nfs_mowner);
    494 		m_clget(mp, M_WAIT);
    495 		mp->m_len = NFSMSIZ(mp);
    496 		if (len == 0)
    497 			mp3 = mp2 = mp;
    498 		else {
    499 			mp2->m_next = mp;
    500 			mp2 = mp;
    501 		}
    502 		if ((len+mp->m_len) > NFS_MAXPATHLEN) {
    503 			mp->m_len = NFS_MAXPATHLEN-len;
    504 			len = NFS_MAXPATHLEN;
    505 		} else
    506 			len += mp->m_len;
    507 		ivp->iov_base = mtod(mp, void *);
    508 		ivp->iov_len = mp->m_len;
    509 		i++;
    510 		ivp++;
    511 	}
    512 	uiop->uio_iov = iv;
    513 	uiop->uio_iovcnt = i;
    514 	uiop->uio_offset = 0;
    515 	uiop->uio_resid = len;
    516 	uiop->uio_rw = UIO_READ;
    517 	UIO_SETUP_SYSSPACE(uiop);
    518 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
    519 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
    520 	if (error) {
    521 		m_freem(mp3);
    522 		nfsm_reply(2 * NFSX_UNSIGNED);
    523 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    524 		return (0);
    525 	}
    526 	if (vp->v_type != VLNK) {
    527 		if (v3)
    528 			error = EINVAL;
    529 		else
    530 			error = ENXIO;
    531 		goto out;
    532 	}
    533 	nqsrv_getl(vp, ND_READ);
    534 	error = VOP_READLINK(vp, uiop, cred);
    535 out:
    536 	getret = VOP_GETATTR(vp, &attr, cred);
    537 	vput(vp);
    538 	if (error)
    539 		m_freem(mp3);
    540 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
    541 	if (v3) {
    542 		nfsm_srvpostop_attr(getret, &attr);
    543 		if (error)
    544 			return (0);
    545 	}
    546 	len -= uiop->uio_resid;
    547 	padlen = nfsm_padlen(len);
    548 	if (uiop->uio_resid || padlen)
    549 		nfs_zeropad(mp3, uiop->uio_resid, padlen);
    550 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    551 	*tl = txdr_unsigned(len);
    552 	mb->m_next = mp3;
    553 	nfsm_srvdone;
    554 }
    555 
    556 /*
    557  * nfs read service
    558  */
    559 int
    560 nfsrv_read(nfsd, slp, lwp, mrq)
    561 	struct nfsrv_descript *nfsd;
    562 	struct nfssvc_sock *slp;
    563 	struct lwp *lwp;
    564 	struct mbuf **mrq;
    565 {
    566 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    567 	struct mbuf *nam = nfsd->nd_nam;
    568 	char *dpos = nfsd->nd_dpos;
    569 	kauth_cred_t cred = nfsd->nd_cr;
    570 	struct mbuf *m;
    571 	struct nfs_fattr *fp;
    572 	u_int32_t *tl;
    573 	int32_t t1;
    574 	int i;
    575 	char *bpos;
    576 	int error = 0, rdonly, cache = 0, getret;
    577 	int v3 = (nfsd->nd_flag & ND_NFSV3);
    578 	uint32_t reqlen, len, cnt, left;
    579 	int padlen;
    580 	char *cp2;
    581 	struct mbuf *mb, *mreq;
    582 	struct vnode *vp;
    583 	nfsrvfh_t nsfh;
    584 	struct uio io, *uiop = &io;
    585 	struct vattr va;
    586 	off_t off;
    587 	u_quad_t frev;
    588 
    589 	nfsm_srvmtofh(&nsfh);
    590 	if (v3) {
    591 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    592 		off = fxdr_hyper(tl);
    593 	} else {
    594 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    595 		off = (off_t)fxdr_unsigned(u_int32_t, *tl);
    596 	}
    597 	nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
    598 	reqlen = fxdr_unsigned(uint32_t, *tl);
    599 	reqlen = MIN(reqlen, NFS_SRVMAXDATA(nfsd));
    600 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
    601 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
    602 	if (error) {
    603 		nfsm_reply(2 * NFSX_UNSIGNED);
    604 		nfsm_srvpostop_attr(1, (struct vattr *)0);
    605 		return (0);
    606 	}
    607 	if (vp->v_type != VREG) {
    608 		if (v3)
    609 			error = EINVAL;
    610 		else
    611 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    612 	}
    613 	if (!error) {
    614 	    nqsrv_getl(vp, ND_READ);
    615 	    if ((error = nfsrv_access(vp, VREAD, cred, rdonly, lwp, 1)) != 0)
    616 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 1);
    617 	}
    618 	getret = VOP_GETATTR(vp, &va, cred);
    619 	if (!error)
    620 		error = getret;
    621 	if (error) {
    622 		vput(vp);
    623 		nfsm_reply(NFSX_POSTOPATTR(v3));
    624 		nfsm_srvpostop_attr(getret, &va);
    625 		return (0);
    626 	}
    627 	if (off >= va.va_size)
    628 		cnt = 0;
    629 	else if ((off + reqlen) > va.va_size)
    630 		cnt = va.va_size - off;
    631 	else
    632 		cnt = reqlen;
    633 	nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
    634 	if (v3) {
    635 		nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
    636 		*tl++ = nfs_true;
    637 		fp = (struct nfs_fattr *)tl;
    638 		tl += (NFSX_V3FATTR / sizeof (u_int32_t));
    639 	} else {
    640 		nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
    641 		fp = (struct nfs_fattr *)tl;
    642 		tl += (NFSX_V2FATTR / sizeof (u_int32_t));
    643 	}
    644 	len = left = cnt;
    645 	if (cnt > 0) {
    646 		if (nfsd_use_loan) {
    647 			struct vm_page **pgpp;
    648 			voff_t pgoff = trunc_page(off);
    649 			int npages;
    650 			vaddr_t lva;
    651 
    652 			npages = (round_page(off + cnt) - pgoff) >> PAGE_SHIFT;
    653 			KASSERT(npages <= M_EXT_MAXPAGES); /* XXX */
    654 
    655 			/* allocate kva for mbuf data */
    656 			lva = sokvaalloc(npages << PAGE_SHIFT, slp->ns_so);
    657 			if (lva == 0) {
    658 				/* fall back to VOP_READ */
    659 				goto loan_fail;
    660 			}
    661 
    662 			/* allocate mbuf */
    663 			m = m_get(M_WAIT, MT_DATA);
    664 			MCLAIM(m, &nfs_mowner);
    665 			pgpp = m->m_ext.ext_pgs;
    666 
    667 			/* loan pages */
    668 			error = uvm_loanuobjpages(&vp->v_uobj, pgoff, npages,
    669 			    pgpp);
    670 			if (error) {
    671 				sokvafree(lva, npages << PAGE_SHIFT);
    672 				m_free(m);
    673 				if (error == EBUSY)
    674 					goto loan_fail;
    675 				goto read_error;
    676 			}
    677 
    678 			/* associate kva to mbuf */
    679 			MEXTADD(m, (void *)(lva + ((vaddr_t)off & PAGE_MASK)),
    680 			    cnt, M_MBUF, soloanfree, slp->ns_so);
    681 			m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
    682 			m->m_len = cnt;
    683 
    684 #if defined(__HAVE_LAZY_MBUF)
    685 			m->m_flags |= M_EXT_LAZY;
    686 			m->m_ext.ext_flags |= M_EXT_LAZY;
    687 #else /* defined(__HAVE_LAZY_MBUF) */
    688 			/* map pages */
    689 			for (i = 0; i < npages; i++) {
    690 				pmap_kenter_pa(lva, VM_PAGE_TO_PHYS(pgpp[i]),
    691 				    VM_PROT_READ);
    692 				lva += PAGE_SIZE;
    693 			}
    694 
    695 			pmap_update(pmap_kernel());
    696 #endif /* defined(__HAVE_LAZY_MBUF) */
    697 
    698 			mb->m_next = m;
    699 			mb = m;
    700 			error = 0;
    701 			uiop->uio_resid = 0;
    702 		} else {
    703 			struct iovec *iv;
    704 			struct iovec *iv2;
    705 			struct mbuf *m2;
    706 			int siz;
    707 loan_fail:
    708 			/*
    709 			 * Generate the mbuf list with the uio_iov ref. to it.
    710 			 */
    711 			i = 0;
    712 			m = m2 = mb;
    713 			while (left > 0) {
    714 				siz = min(M_TRAILINGSPACE(m), left);
    715 				if (siz > 0) {
    716 					left -= siz;
    717 					i++;
    718 				}
    719 				if (left > 0) {
    720 					m = m_get(M_WAIT, MT_DATA);
    721 					MCLAIM(m, &nfs_mowner);
    722 					m_clget(m, M_WAIT);
    723 					m->m_len = 0;
    724 					m2->m_next = m;
    725 					m2 = m;
    726 				}
    727 			}
    728 			iv = malloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
    729 			uiop->uio_iov = iv2 = iv;
    730 			m = mb;
    731 			left = cnt;
    732 			i = 0;
    733 			while (left > 0) {
    734 				if (m == NULL)
    735 					panic("nfsrv_read iov");
    736 				siz = min(M_TRAILINGSPACE(m), left);
    737 				if (siz > 0) {
    738 					iv->iov_base = mtod(m, char *) +
    739 					    m->m_len;
    740 					iv->iov_len = siz;
    741 					m->m_len += siz;
    742 					left -= siz;
    743 					iv++;
    744 					i++;
    745 				}
    746 				m = m->m_next;
    747 			}
    748 			uiop->uio_iovcnt = i;
    749 			uiop->uio_offset = off;
    750 			uiop->uio_resid = cnt;
    751 			uiop->uio_rw = UIO_READ;
    752 			UIO_SETUP_SYSSPACE(uiop);
    753 			error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
    754 			free((void *)iv2, M_TEMP);
    755 		}
    756 read_error:
    757 		if (error || (getret = VOP_GETATTR(vp, &va, cred)) != 0){
    758 			if (!error)
    759 				error = getret;
    760 			m_freem(mreq);
    761 			vput(vp);
    762 			nfsm_reply(NFSX_POSTOPATTR(v3));
    763 			nfsm_srvpostop_attr(getret, &va);
    764 			return (0);
    765 		}
    766 	} else {
    767 		uiop->uio_resid = 0;
    768 	}
    769 	vput(vp);
    770 	nfsm_srvfillattr(&va, fp);
    771 	len -= uiop->uio_resid;
    772 	padlen = nfsm_padlen(len);
    773 	if (uiop->uio_resid || padlen)
    774 		nfs_zeropad(mb, uiop->uio_resid, padlen);
    775 	if (v3) {
    776 		/* count */
    777 		*tl++ = txdr_unsigned(len);
    778 		/* eof */
    779 		if (off + len >= va.va_size)
    780 			*tl++ = nfs_true;
    781 		else
    782 			*tl++ = nfs_false;
    783 	}
    784 	*tl = txdr_unsigned(len);
    785 	nfsm_srvdone;
    786 }
    787 
    788 /*
    789  * nfs write service
    790  */
    791 int
    792 nfsrv_write(nfsd, slp, lwp, mrq)
    793 	struct nfsrv_descript *nfsd;
    794 	struct nfssvc_sock *slp;
    795 	struct lwp *lwp;
    796 	struct mbuf **mrq;
    797 {
    798 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
    799 	struct mbuf *nam = nfsd->nd_nam;
    800 	char *dpos = nfsd->nd_dpos;
    801 	kauth_cred_t cred = nfsd->nd_cr;
    802 	struct iovec *ivp;
    803 	int i, cnt;
    804 	struct mbuf *mp;
    805 	struct nfs_fattr *fp;
    806 	struct iovec *iv;
    807 	struct vattr va, forat;
    808 	u_int32_t *tl;
    809 	int32_t t1;
    810 	char *bpos;
    811 	int error = 0, rdonly, cache = 0, len, forat_ret = 1;
    812 	int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
    813 	int stable = NFSV3WRITE_FILESYNC;
    814 	int v3 = (nfsd->nd_flag & ND_NFSV3);
    815 	char *cp2;
    816 	struct mbuf *mb, *mreq;
    817 	struct vnode *vp;
    818 	nfsrvfh_t nsfh;
    819 	struct uio io, *uiop = &io;
    820 	off_t off;
    821 	u_quad_t frev;
    822 
    823 	if (mrep == NULL) {
    824 		*mrq = NULL;
    825 		return (0);
    826 	}
    827 	nfsm_srvmtofh(&nsfh);
    828 	if (v3) {
    829 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
    830 		off = fxdr_hyper(tl);
    831 		tl += 3;
    832 		stable = fxdr_unsigned(int, *tl++);
    833 	} else {
    834 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    835 		off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
    836 		tl += 2;
    837 	}
    838 	retlen = len = fxdr_unsigned(int32_t, *tl);
    839 	cnt = i = 0;
    840 
    841 	/*
    842 	 * For NFS Version 2, it is not obvious what a write of zero length
    843 	 * should do, but I might as well be consistent with Version 3,
    844 	 * which is to return ok so long as there are no permission problems.
    845 	 */
    846 	if (len > 0) {
    847 		zeroing = 1;
    848 		mp = mrep;
    849 		while (mp) {
    850 			if (mp == md) {
    851 				zeroing = 0;
    852 				adjust = dpos - mtod(mp, char *);
    853 				mp->m_len -= adjust;
    854 				if (mp->m_len > 0 && adjust > 0)
    855 					NFSMADV(mp, adjust);
    856 			}
    857 			if (zeroing)
    858 				mp->m_len = 0;
    859 			else if (mp->m_len > 0) {
    860 				i += mp->m_len;
    861 				if (i > len) {
    862 					mp->m_len -= (i - len);
    863 					zeroing	= 1;
    864 				}
    865 				if (mp->m_len > 0)
    866 					cnt++;
    867 			}
    868 			mp = mp->m_next;
    869 		}
    870 	}
    871 	if (len > NFS_MAXDATA || len < 0 || i < len) {
    872 		error = EIO;
    873 		nfsm_reply(2 * NFSX_UNSIGNED);
    874 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    875 		return (0);
    876 	}
    877 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
    878 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
    879 	if (error) {
    880 		nfsm_reply(2 * NFSX_UNSIGNED);
    881 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    882 		return (0);
    883 	}
    884 	if (v3)
    885 		forat_ret = VOP_GETATTR(vp, &forat, cred);
    886 	if (vp->v_type != VREG) {
    887 		if (v3)
    888 			error = EINVAL;
    889 		else
    890 			error = (vp->v_type == VDIR) ? EISDIR : EACCES;
    891 	}
    892 	if (!error) {
    893 		nqsrv_getl(vp, ND_WRITE);
    894 		error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
    895 	}
    896 	if (error) {
    897 		vput(vp);
    898 		nfsm_reply(NFSX_WCCDATA(v3));
    899 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    900 		return (0);
    901 	}
    902 
    903 	if (len > 0) {
    904 		ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, M_WAITOK);
    905 		uiop->uio_iov = iv = ivp;
    906 		uiop->uio_iovcnt = cnt;
    907 		mp = mrep;
    908 		while (mp) {
    909 			if (mp->m_len > 0) {
    910 				ivp->iov_base = mtod(mp, void *);
    911 				ivp->iov_len = mp->m_len;
    912 				ivp++;
    913 			}
    914 			mp = mp->m_next;
    915 		}
    916 
    917 		/*
    918 		 * XXX
    919 		 * The IO_METASYNC flag indicates that all metadata (and not
    920 		 * just enough to ensure data integrity) must be written to
    921 		 * stable storage synchronously.
    922 		 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
    923 		 */
    924 		if (stable == NFSV3WRITE_UNSTABLE)
    925 			ioflags = IO_NODELOCKED;
    926 		else if (stable == NFSV3WRITE_DATASYNC)
    927 			ioflags = (IO_SYNC | IO_NODELOCKED);
    928 		else
    929 			ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
    930 		uiop->uio_resid = len;
    931 		uiop->uio_rw = UIO_WRITE;
    932 		uiop->uio_offset = off;
    933 		UIO_SETUP_SYSSPACE(uiop);
    934 		error = VOP_WRITE(vp, uiop, ioflags, cred);
    935 		nfsstats.srvvop_writes++;
    936 		free(iv, M_TEMP);
    937 	}
    938 	aftat_ret = VOP_GETATTR(vp, &va, cred);
    939 	vput(vp);
    940 	if (!error)
    941 		error = aftat_ret;
    942 	nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
    943 		2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
    944 	if (v3) {
    945 		nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
    946 		if (error)
    947 			return (0);
    948 		nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
    949 		*tl++ = txdr_unsigned(retlen);
    950 		if (stable == NFSV3WRITE_UNSTABLE)
    951 			*tl++ = txdr_unsigned(stable);
    952 		else
    953 			*tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
    954 		/*
    955 		 * Actually, there is no need to txdr these fields,
    956 		 * but it may make the values more human readable,
    957 		 * for debugging purposes.
    958 		 */
    959 		*tl++ = txdr_unsigned(boottime.tv_sec);
    960 		*tl = txdr_unsigned(boottime.tv_usec);
    961 	} else {
    962 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
    963 		nfsm_srvfillattr(&va, fp);
    964 	}
    965 	nfsm_srvdone;
    966 }
    967 
    968 /*
    969  * XXX elad: the original NFSW_SAMECRED() macro also made sure the
    970  *	     two nd_flag fields of the descriptors contained
    971  *	     ND_KERBAUTH.
    972  */
    973 static int
    974 nfsrv_samecred(kauth_cred_t cred1, kauth_cred_t cred2)
    975 {
    976 	int i, do_ngroups;
    977 
    978 	if (kauth_cred_geteuid(cred1) != kauth_cred_geteuid(cred2))
    979 		return (0);
    980 	if (kauth_cred_ngroups(cred1) != kauth_cred_ngroups(cred2))
    981 		return (0);
    982 	do_ngroups = kauth_cred_ngroups(cred1);
    983 	for (i = 0; i < do_ngroups; i++)
    984 		if (kauth_cred_group(cred1, i) !=
    985 		    kauth_cred_group(cred2, i))
    986 			return (0);
    987 
    988 	return (1);
    989 }
    990 
    991 static struct nfsrvw_delayhash *
    992 nfsrv_nwdelayhash(struct nfssvc_sock *slp, const nfsrvfh_t *nsfh)
    993 {
    994 	uint32_t hash;
    995 
    996 	hash = hash32_buf(NFSRVFH_DATA(nsfh), NFSRVFH_SIZE(nsfh),
    997 	    HASH32_BUF_INIT);
    998 	return &slp->ns_wdelayhashtbl[hash % NFS_WDELAYHASHSIZ];
    999 }
   1000 
   1001 /*
   1002  * NFS write service with write gathering support. Called when
   1003  * nfsrvw_procrastinate > 0.
   1004  * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
   1005  * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
   1006  * Jan. 1994.
   1007  */
   1008 int
   1009 nfsrv_writegather(ndp, slp, lwp, mrq)
   1010 	struct nfsrv_descript **ndp;
   1011 	struct nfssvc_sock *slp;
   1012 	struct lwp *lwp;
   1013 	struct mbuf **mrq;
   1014 {
   1015 	struct timeval now;
   1016 	struct iovec *ivp;
   1017 	struct mbuf *mp;
   1018 	struct nfsrv_descript *wp, *nfsd, *owp, *swp;
   1019 	struct nfs_fattr *fp;
   1020 	int i = 0;
   1021 	struct iovec *iov;
   1022 	struct nfsrvw_delayhash *wpp;
   1023 	kauth_cred_t cred;
   1024 	struct vattr va, forat;
   1025 	u_int32_t *tl;
   1026 	int32_t t1;
   1027 	char *bpos, *dpos;
   1028 	int error = 0, rdonly, cache = 0, len = 0, forat_ret = 1;
   1029 	int ioflags, aftat_ret = 1, adjust, v3, zeroing;
   1030 	char *cp2;
   1031 	struct mbuf *mb, *mreq, *mrep, *md;
   1032 	struct vnode *vp;
   1033 	struct uio io, *uiop = &io;
   1034 	u_quad_t frev, cur_usec;
   1035 
   1036 	*mrq = NULL;
   1037 	if (*ndp) {
   1038 	    nfsd = *ndp;
   1039 	    *ndp = NULL;
   1040 	    mrep = nfsd->nd_mrep;
   1041 	    md = nfsd->nd_md;
   1042 	    dpos = nfsd->nd_dpos;
   1043 	    cred = nfsd->nd_cr;
   1044 	    v3 = (nfsd->nd_flag & ND_NFSV3);
   1045 	    LIST_INIT(&nfsd->nd_coalesce);
   1046 	    nfsd->nd_mreq = NULL;
   1047 	    nfsd->nd_stable = NFSV3WRITE_FILESYNC;
   1048 	    getmicrotime(&now);
   1049 	    cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
   1050 	    nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
   1051 
   1052 	    /*
   1053 	     * Now, get the write header..
   1054 	     */
   1055 	    nfsm_srvmtofh(&nfsd->nd_fh);
   1056 	    if (v3) {
   1057 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1058 		nfsd->nd_off = fxdr_hyper(tl);
   1059 		tl += 3;
   1060 		nfsd->nd_stable = fxdr_unsigned(int, *tl++);
   1061 	    } else {
   1062 		nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1063 		nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
   1064 		tl += 2;
   1065 	    }
   1066 	    len = fxdr_unsigned(int32_t, *tl);
   1067 	    nfsd->nd_len = len;
   1068 	    nfsd->nd_eoff = nfsd->nd_off + len;
   1069 
   1070 	    /*
   1071 	     * Trim the header out of the mbuf list and trim off any trailing
   1072 	     * junk so that the mbuf list has only the write data.
   1073 	     */
   1074 	    zeroing = 1;
   1075 	    i = 0;
   1076 	    mp = mrep;
   1077 	    while (mp) {
   1078 		if (mp == md) {
   1079 		    zeroing = 0;
   1080 		    adjust = dpos - mtod(mp, char *);
   1081 		    mp->m_len -= adjust;
   1082 		    if (mp->m_len > 0 && adjust > 0)
   1083 			NFSMADV(mp, adjust);
   1084 		}
   1085 		if (zeroing)
   1086 		    mp->m_len = 0;
   1087 		else {
   1088 		    i += mp->m_len;
   1089 		    if (i > len) {
   1090 			mp->m_len -= (i - len);
   1091 			zeroing = 1;
   1092 		    }
   1093 		}
   1094 		mp = mp->m_next;
   1095 	    }
   1096 	    if (len > NFS_MAXDATA || len < 0  || i < len) {
   1097 nfsmout:
   1098 		m_freem(mrep);
   1099 		error = EIO;
   1100 		nfsm_writereply(2 * NFSX_UNSIGNED, v3);
   1101 		if (v3)
   1102 		    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1103 		nfsd->nd_mreq = mreq;
   1104 		nfsd->nd_mrep = NULL;
   1105 		nfsd->nd_time = 0;
   1106 	    }
   1107 
   1108 	    /*
   1109 	     * Add this entry to the hash and time queues.
   1110 	     */
   1111 	    owp = NULL;
   1112 	    mutex_enter(&nfsd_lock);
   1113 	    wp = LIST_FIRST(&slp->ns_tq);
   1114 	    while (wp && wp->nd_time < nfsd->nd_time) {
   1115 		owp = wp;
   1116 		wp = LIST_NEXT(wp, nd_tq);
   1117 	    }
   1118 	    if (owp) {
   1119 		LIST_INSERT_AFTER(owp, nfsd, nd_tq);
   1120 	    } else {
   1121 		LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
   1122 	    }
   1123 	    if (nfsd->nd_mrep) {
   1124 		wpp = nfsrv_nwdelayhash(slp, &nfsd->nd_fh);
   1125 		owp = NULL;
   1126 		wp = LIST_FIRST(wpp);
   1127 		while (wp && nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
   1128 		    owp = wp;
   1129 		    wp = LIST_NEXT(wp, nd_hash);
   1130 		}
   1131 		while (wp && wp->nd_off < nfsd->nd_off &&
   1132 		    !nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
   1133 		    owp = wp;
   1134 		    wp = LIST_NEXT(wp, nd_hash);
   1135 		}
   1136 		if (owp) {
   1137 		    LIST_INSERT_AFTER(owp, nfsd, nd_hash);
   1138 
   1139 		    /*
   1140 		     * Search the hash list for overlapping entries and
   1141 		     * coalesce.
   1142 		     */
   1143 		    for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
   1144 			wp = LIST_NEXT(nfsd, nd_hash);
   1145 			if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr))
   1146 			    nfsrvw_coalesce(owp, nfsd);
   1147 		    }
   1148 		} else {
   1149 		    LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
   1150 		}
   1151 	    }
   1152 	    mutex_exit(&nfsd_lock);
   1153 	}
   1154 
   1155 	/*
   1156 	 * Now, do VOP_WRITE()s for any one(s) that need to be done now
   1157 	 * and generate the associated reply mbuf list(s).
   1158 	 */
   1159 loop1:
   1160 	getmicrotime(&now);
   1161 	cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
   1162 	mutex_enter(&nfsd_lock);
   1163 	for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
   1164 		owp = LIST_NEXT(nfsd, nd_tq);
   1165 		if (nfsd->nd_time > cur_usec)
   1166 		    break;
   1167 		if (nfsd->nd_mreq)
   1168 		    continue;
   1169 		LIST_REMOVE(nfsd, nd_tq);
   1170 		LIST_REMOVE(nfsd, nd_hash);
   1171 		mutex_exit(&nfsd_lock);
   1172 
   1173 		mrep = nfsd->nd_mrep;
   1174 		nfsd->nd_mrep = NULL;
   1175 		cred = nfsd->nd_cr;
   1176 		v3 = (nfsd->nd_flag & ND_NFSV3);
   1177 		forat_ret = aftat_ret = 1;
   1178 		error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
   1179 		    nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
   1180 		    false);
   1181 		if (!error) {
   1182 		    if (v3)
   1183 			forat_ret = VOP_GETATTR(vp, &forat, cred);
   1184 		    if (vp->v_type != VREG) {
   1185 			if (v3)
   1186 			    error = EINVAL;
   1187 			else
   1188 			    error = (vp->v_type == VDIR) ? EISDIR : EACCES;
   1189 		    }
   1190 		} else
   1191 		    vp = NULL;
   1192 		if (!error) {
   1193 		    nqsrv_getl(vp, ND_WRITE);
   1194 		    error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
   1195 		}
   1196 
   1197 		if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
   1198 		    ioflags = IO_NODELOCKED;
   1199 		else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
   1200 		    ioflags = (IO_SYNC | IO_NODELOCKED);
   1201 		else
   1202 		    ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
   1203 		uiop->uio_rw = UIO_WRITE;
   1204 		uiop->uio_offset = nfsd->nd_off;
   1205 		uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
   1206 		UIO_SETUP_SYSSPACE(uiop);
   1207 		if (uiop->uio_resid > 0) {
   1208 		    mp = mrep;
   1209 		    i = 0;
   1210 		    while (mp) {
   1211 			if (mp->m_len > 0)
   1212 			    i++;
   1213 			mp = mp->m_next;
   1214 		    }
   1215 		    uiop->uio_iovcnt = i;
   1216 		    iov = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
   1217 		    uiop->uio_iov = ivp = iov;
   1218 		    mp = mrep;
   1219 		    while (mp) {
   1220 			if (mp->m_len > 0) {
   1221 			    ivp->iov_base = mtod(mp, void *);
   1222 			    ivp->iov_len = mp->m_len;
   1223 			    ivp++;
   1224 			}
   1225 			mp = mp->m_next;
   1226 		    }
   1227 		    if (!error) {
   1228 			error = VOP_WRITE(vp, uiop, ioflags, cred);
   1229 			nfsstats.srvvop_writes++;
   1230 		    }
   1231 		    free((void *)iov, M_TEMP);
   1232 		}
   1233 		m_freem(mrep);
   1234 		if (vp) {
   1235 		    aftat_ret = VOP_GETATTR(vp, &va, cred);
   1236 		    vput(vp);
   1237 		}
   1238 
   1239 		/*
   1240 		 * Loop around generating replies for all write rpcs that have
   1241 		 * now been completed.
   1242 		 */
   1243 		swp = nfsd;
   1244 		do {
   1245 		    if (error) {
   1246 			nfsm_writereply(NFSX_WCCDATA(v3), v3);
   1247 			if (v3) {
   1248 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1249 			}
   1250 		    } else {
   1251 			nfsm_writereply(NFSX_PREOPATTR(v3) +
   1252 			    NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
   1253 			    NFSX_WRITEVERF(v3), v3);
   1254 			if (v3) {
   1255 			    nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
   1256 			    nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1257 			    *tl++ = txdr_unsigned(nfsd->nd_len);
   1258 			    *tl++ = txdr_unsigned(swp->nd_stable);
   1259 			    /*
   1260 			     * Actually, there is no need to txdr these fields,
   1261 			     * but it may make the values more human readable,
   1262 			     * for debugging purposes.
   1263 			     */
   1264 			    *tl++ = txdr_unsigned(boottime.tv_sec);
   1265 			    *tl = txdr_unsigned(boottime.tv_usec);
   1266 			} else {
   1267 			    nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   1268 			    nfsm_srvfillattr(&va, fp);
   1269 			}
   1270 		    }
   1271 		    nfsd->nd_mreq = mreq;
   1272 		    if (nfsd->nd_mrep)
   1273 			panic("nfsrv_write: nd_mrep not free");
   1274 
   1275 		    /*
   1276 		     * Done. Put it at the head of the timer queue so that
   1277 		     * the final phase can return the reply.
   1278 		     */
   1279 		    mutex_enter(&nfsd_lock);
   1280 		    if (nfsd != swp) {
   1281 			nfsd->nd_time = 0;
   1282 			LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
   1283 		    }
   1284 		    nfsd = LIST_FIRST(&swp->nd_coalesce);
   1285 		    if (nfsd) {
   1286 			LIST_REMOVE(nfsd, nd_tq);
   1287 		    }
   1288 		    mutex_exit(&nfsd_lock);
   1289 		} while (nfsd);
   1290 		swp->nd_time = 0;
   1291 
   1292 		mutex_enter(&nfsd_lock);
   1293 		LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
   1294 		mutex_exit(&nfsd_lock);
   1295 		goto loop1;
   1296 	}
   1297 	mutex_exit(&nfsd_lock);
   1298 	nfs_timer_start();
   1299 
   1300 	/*
   1301 	 * Search for a reply to return.
   1302 	 */
   1303 	mutex_enter(&nfsd_lock);
   1304 	LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq) {
   1305 		if (nfsd->nd_mreq) {
   1306 		    LIST_REMOVE(nfsd, nd_tq);
   1307 		    *mrq = nfsd->nd_mreq;
   1308 		    *ndp = nfsd;
   1309 		    break;
   1310 		}
   1311 	}
   1312 	mutex_exit(&nfsd_lock);
   1313 	return (0);
   1314 }
   1315 
   1316 /*
   1317  * Coalesce the write request nfsd into owp. To do this we must:
   1318  * - remove nfsd from the queues
   1319  * - merge nfsd->nd_mrep into owp->nd_mrep
   1320  * - update the nd_eoff and nd_stable for owp
   1321  * - put nfsd on owp's nd_coalesce list
   1322  * NB: Must be called at splsoftclock().
   1323  */
   1324 void
   1325 nfsrvw_coalesce(owp, nfsd)
   1326         struct nfsrv_descript *owp;
   1327         struct nfsrv_descript *nfsd;
   1328 {
   1329         int overlap;
   1330         struct mbuf *mp;
   1331 	struct nfsrv_descript *m;
   1332 
   1333 	KASSERT(mutex_owned(&nfsd_lock));
   1334 
   1335         LIST_REMOVE(nfsd, nd_hash);
   1336         LIST_REMOVE(nfsd, nd_tq);
   1337         if (owp->nd_eoff < nfsd->nd_eoff) {
   1338             overlap = owp->nd_eoff - nfsd->nd_off;
   1339             if (overlap < 0)
   1340                 panic("nfsrv_coalesce: bad off");
   1341             if (overlap > 0)
   1342                 m_adj(nfsd->nd_mrep, overlap);
   1343             mp = owp->nd_mrep;
   1344             while (mp->m_next)
   1345                 mp = mp->m_next;
   1346             mp->m_next = nfsd->nd_mrep;
   1347             owp->nd_eoff = nfsd->nd_eoff;
   1348         } else
   1349             m_freem(nfsd->nd_mrep);
   1350         nfsd->nd_mrep = NULL;
   1351         if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
   1352             owp->nd_stable = NFSV3WRITE_FILESYNC;
   1353         else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
   1354             owp->nd_stable == NFSV3WRITE_UNSTABLE)
   1355             owp->nd_stable = NFSV3WRITE_DATASYNC;
   1356         LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
   1357  	/*
   1358  	 * nfsd might hold coalesce elements! Move them to owp.
   1359  	 * Otherwise, requests may be lost and clients will be stuck.
   1360  	 */
   1361 	while ((m = LIST_FIRST(&nfsd->nd_coalesce)) != NULL) {
   1362 		LIST_REMOVE(m, nd_tq);
   1363 		LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
   1364 	}
   1365 }
   1366 
   1367 /*
   1368  * nfs create service
   1369  * now does a truncate to 0 length via. setattr if it already exists
   1370  */
   1371 int
   1372 nfsrv_create(nfsd, slp, lwp, mrq)
   1373 	struct nfsrv_descript *nfsd;
   1374 	struct nfssvc_sock *slp;
   1375 	struct lwp *lwp;
   1376 	struct mbuf **mrq;
   1377 {
   1378 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1379 	struct mbuf *nam = nfsd->nd_nam;
   1380 	char *dpos = nfsd->nd_dpos;
   1381 	kauth_cred_t cred = nfsd->nd_cr;
   1382 	struct nfs_fattr *fp;
   1383 	struct vattr va, dirfor, diraft;
   1384 	struct nfsv2_sattr *sp;
   1385 	u_int32_t *tl;
   1386 	struct nameidata nd;
   1387 	char *cp;
   1388 	int32_t t1;
   1389 	char *bpos;
   1390 	int error = 0, cache = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
   1391 	int rdev = 0;
   1392 	int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
   1393 	char *cp2;
   1394 	struct mbuf *mb, *mreq;
   1395 	struct vnode *vp = NULL, *dirp = NULL;
   1396 	nfsrvfh_t nsfh;
   1397 	u_quad_t frev, tempsize;
   1398 	u_char cverf[NFSX_V3CREATEVERF];
   1399 
   1400 	nd.ni_cnd.cn_nameiop = 0;
   1401 	nfsm_srvmtofh(&nsfh);
   1402 	nfsm_srvnamesiz(len);
   1403 	nd.ni_cnd.cn_cred = cred;
   1404 	nd.ni_cnd.cn_nameiop = CREATE;
   1405 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1406 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   1407 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   1408 	if (dirp && v3) {
   1409 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   1410 	}
   1411 	if (error) {
   1412 		nfsm_reply(NFSX_WCCDATA(v3));
   1413 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1414 		if (dirp)
   1415 			vrele(dirp);
   1416 		return (0);
   1417 	}
   1418 	VATTR_NULL(&va);
   1419 	if (v3) {
   1420 		va.va_mode = 0;
   1421 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1422 		how = fxdr_unsigned(int, *tl);
   1423 		switch (how) {
   1424 		case NFSV3CREATE_GUARDED:
   1425 			if (nd.ni_vp) {
   1426 				error = EEXIST;
   1427 				break;
   1428 			}
   1429 		case NFSV3CREATE_UNCHECKED:
   1430 			nfsm_srvsattr(&va);
   1431 			break;
   1432 		case NFSV3CREATE_EXCLUSIVE:
   1433 			nfsm_dissect(cp, void *, NFSX_V3CREATEVERF);
   1434 			memcpy(cverf, cp, NFSX_V3CREATEVERF);
   1435 			exclusive_flag = 1;
   1436 			break;
   1437 		};
   1438 		va.va_type = VREG;
   1439 	} else {
   1440 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1441 		va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
   1442 		if (va.va_type == VNON)
   1443 			va.va_type = VREG;
   1444 		va.va_mode = nfstov_mode(sp->sa_mode);
   1445 		switch (va.va_type) {
   1446 		case VREG:
   1447 			tsize = fxdr_unsigned(int32_t, sp->sa_size);
   1448 			if (tsize != -1)
   1449 				va.va_size = (u_quad_t)tsize;
   1450 			break;
   1451 		case VCHR:
   1452 		case VBLK:
   1453 		case VFIFO:
   1454 			rdev = fxdr_unsigned(int32_t, sp->sa_size);
   1455 			break;
   1456 		default:
   1457 			break;
   1458 		};
   1459 	}
   1460 
   1461 	/*
   1462 	 * Iff doesn't exist, create it
   1463 	 * otherwise just truncate to 0 length
   1464 	 *   should I set the mode too ??
   1465 	 */
   1466 	if (nd.ni_vp == NULL) {
   1467 		if (va.va_type == VREG || va.va_type == VSOCK) {
   1468 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1469 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1470 			if (!error) {
   1471 				if (exclusive_flag) {
   1472 					exclusive_flag = 0;
   1473 					VATTR_NULL(&va);
   1474 					/*
   1475 					 * XXX
   1476 					 * assuming NFSX_V3CREATEVERF
   1477 					 * == sizeof(nfstime3)
   1478 					 */
   1479 					fxdr_nfsv3time(cverf, &va.va_atime);
   1480 					error = VOP_SETATTR(nd.ni_vp, &va,
   1481 						cred);
   1482 				}
   1483 			}
   1484 		} else if (va.va_type == VCHR || va.va_type == VBLK ||
   1485 			va.va_type == VFIFO) {
   1486 			if (va.va_type == VCHR && rdev == 0xffffffff)
   1487 				va.va_type = VFIFO;
   1488 			if (va.va_type != VFIFO &&
   1489 			    (error = kauth_authorize_generic(cred,
   1490 			    KAUTH_GENERIC_ISSUSER, NULL))) {
   1491 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1492 				vput(nd.ni_dvp);
   1493 				nfsm_reply(0);
   1494 				return (error);
   1495 			} else
   1496 				va.va_rdev = (dev_t)rdev;
   1497 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1498 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
   1499 			    &va);
   1500 			if (error) {
   1501 				nfsm_reply(0);
   1502 			}
   1503 			if (nd.ni_cnd.cn_flags & ISSYMLINK) {
   1504 				vput(nd.ni_vp);
   1505 				vrele(nd.ni_dvp);
   1506 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1507 				error = EINVAL;
   1508 				nfsm_reply(0);
   1509 			}
   1510 		} else {
   1511 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1512 			vput(nd.ni_dvp);
   1513 			error = ENXIO;
   1514 		}
   1515 		vp = nd.ni_vp;
   1516 	} else {
   1517 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1518 		vp = nd.ni_vp;
   1519 		if (nd.ni_dvp == vp)
   1520 			vrele(nd.ni_dvp);
   1521 		else
   1522 			vput(nd.ni_dvp);
   1523 		if (!error && va.va_size != -1) {
   1524 			error = nfsrv_access(vp, VWRITE, cred,
   1525 			    (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
   1526 			if (!error) {
   1527 				nqsrv_getl(vp, ND_WRITE);
   1528 				tempsize = va.va_size;
   1529 				VATTR_NULL(&va);
   1530 				va.va_size = tempsize;
   1531 				error = VOP_SETATTR(vp, &va, cred);
   1532 			}
   1533 		}
   1534 		if (error)
   1535 			vput(vp);
   1536 	}
   1537 	if (!error) {
   1538 		error = nfsrv_composefh(vp, &nsfh, v3);
   1539 		if (!error)
   1540 			error = VOP_GETATTR(vp, &va, cred);
   1541 		vput(vp);
   1542 	}
   1543 	if (v3) {
   1544 		if (exclusive_flag && !error) {
   1545 			/*
   1546 			 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
   1547 			 */
   1548 			char oldverf[NFSX_V3CREATEVERF];
   1549 
   1550 			txdr_nfsv3time(&va.va_atime, oldverf);
   1551 			if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
   1552 				error = EEXIST;
   1553 		}
   1554 		if (dirp) {
   1555 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   1556 		}
   1557 	}
   1558 	if (dirp) {
   1559 		vrele(dirp);
   1560 	}
   1561 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
   1562 	if (v3) {
   1563 		if (!error) {
   1564 			nfsm_srvpostop_fh(&nsfh);
   1565 			nfsm_srvpostop_attr(0, &va);
   1566 		}
   1567 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1568 	} else {
   1569 		nfsm_srvfhtom(&nsfh, v3);
   1570 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   1571 		nfsm_srvfillattr(&va, fp);
   1572 	}
   1573 	return (0);
   1574 nfsmout:
   1575 	if (dirp)
   1576 		vrele(dirp);
   1577 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1578 	if (nd.ni_dvp == nd.ni_vp)
   1579 		vrele(nd.ni_dvp);
   1580 	else
   1581 		vput(nd.ni_dvp);
   1582 	if (nd.ni_vp)
   1583 		vput(nd.ni_vp);
   1584 	return (error);
   1585 }
   1586 
   1587 /*
   1588  * nfs v3 mknod service
   1589  */
   1590 int
   1591 nfsrv_mknod(nfsd, slp, lwp, mrq)
   1592 	struct nfsrv_descript *nfsd;
   1593 	struct nfssvc_sock *slp;
   1594 	struct lwp *lwp;
   1595 	struct mbuf **mrq;
   1596 {
   1597 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1598 	struct mbuf *nam = nfsd->nd_nam;
   1599 	char *dpos = nfsd->nd_dpos;
   1600 	kauth_cred_t cred = nfsd->nd_cr;
   1601 	struct vattr va, dirfor, diraft;
   1602 	u_int32_t *tl;
   1603 	struct nameidata nd;
   1604 	int32_t t1;
   1605 	char *bpos;
   1606 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
   1607 	u_int32_t major, minor;
   1608 	enum vtype vtyp;
   1609 	char *cp2;
   1610 	struct mbuf *mb, *mreq;
   1611 	struct vnode *vp, *dirp = (struct vnode *)0;
   1612 	nfsrvfh_t nsfh;
   1613 	u_quad_t frev;
   1614 
   1615 	nd.ni_cnd.cn_nameiop = 0;
   1616 	nfsm_srvmtofh(&nsfh);
   1617 	nfsm_srvnamesiz(len);
   1618 	nd.ni_cnd.cn_cred = cred;
   1619 	nd.ni_cnd.cn_nameiop = CREATE;
   1620 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1621 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   1622 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   1623 	if (dirp)
   1624 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   1625 	if (error) {
   1626 		nfsm_reply(NFSX_WCCDATA(1));
   1627 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1628 		if (dirp)
   1629 			vrele(dirp);
   1630 		return (0);
   1631 	}
   1632 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1633 	vtyp = nfsv3tov_type(*tl);
   1634 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
   1635 		error = NFSERR_BADTYPE;
   1636 		goto abort;
   1637 	}
   1638 	VATTR_NULL(&va);
   1639 	va.va_mode = 0;
   1640 	nfsm_srvsattr(&va);
   1641 	if (vtyp == VCHR || vtyp == VBLK) {
   1642 		dev_t rdev;
   1643 
   1644 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1645 		major = fxdr_unsigned(u_int32_t, *tl++);
   1646 		minor = fxdr_unsigned(u_int32_t, *tl);
   1647 		rdev = makedev(major, minor);
   1648 		if (major(rdev) != major || minor(rdev) != minor) {
   1649 			error = EINVAL;
   1650 			goto abort;
   1651 		}
   1652 		va.va_rdev = rdev;
   1653 	}
   1654 
   1655 	/*
   1656 	 * Iff doesn't exist, create it.
   1657 	 */
   1658 	if (nd.ni_vp) {
   1659 		error = EEXIST;
   1660 abort:
   1661 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1662 		if (nd.ni_dvp == nd.ni_vp)
   1663 			vrele(nd.ni_dvp);
   1664 		else
   1665 			vput(nd.ni_dvp);
   1666 		if (nd.ni_vp)
   1667 			vput(nd.ni_vp);
   1668 		goto out;
   1669 	}
   1670 	va.va_type = vtyp;
   1671 	if (vtyp == VSOCK) {
   1672 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1673 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1674 	} else {
   1675 		if (va.va_type != VFIFO &&
   1676 		    (error = kauth_authorize_generic(cred,
   1677 		    KAUTH_GENERIC_ISSUSER, NULL))) {
   1678 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1679 			vput(nd.ni_dvp);
   1680 			goto out;
   1681 		}
   1682 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1683 		error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   1684 		if (error)
   1685 			goto out;
   1686 		if (nd.ni_cnd.cn_flags & ISSYMLINK) {
   1687 			vput(nd.ni_vp);
   1688 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1689 			error = EINVAL;
   1690 		}
   1691 	}
   1692 out:
   1693 	vp = nd.ni_vp;
   1694 	if (!error) {
   1695 		error = nfsrv_composefh(vp, &nsfh, true);
   1696 		if (!error)
   1697 			error = VOP_GETATTR(vp, &va, cred);
   1698 		vput(vp);
   1699 	}
   1700 	if (dirp) {
   1701 		diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   1702 		vrele(dirp);
   1703 	}
   1704 	nfsm_reply(NFSX_SRVFH(&nsfh, true) + NFSX_POSTOPATTR(1) +
   1705 	    NFSX_WCCDATA(1));
   1706 	if (!error) {
   1707 		nfsm_srvpostop_fh(&nsfh);
   1708 		nfsm_srvpostop_attr(0, &va);
   1709 	}
   1710 	nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1711 	return (0);
   1712 nfsmout:
   1713 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1714 	if (nd.ni_dvp == nd.ni_vp)
   1715 		vrele(nd.ni_dvp);
   1716 	else
   1717 		vput(nd.ni_dvp);
   1718 	if (nd.ni_vp)
   1719 		vput(nd.ni_vp);
   1720 	if (dirp)
   1721 		vrele(dirp);
   1722 	return (error);
   1723 }
   1724 
   1725 /*
   1726  * nfs remove service
   1727  */
   1728 int
   1729 nfsrv_remove(nfsd, slp, lwp, mrq)
   1730 	struct nfsrv_descript *nfsd;
   1731 	struct nfssvc_sock *slp;
   1732 	struct lwp *lwp;
   1733 	struct mbuf **mrq;
   1734 {
   1735 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1736 	struct mbuf *nam = nfsd->nd_nam;
   1737 	char *dpos = nfsd->nd_dpos;
   1738 	kauth_cred_t cred = nfsd->nd_cr;
   1739 	struct nameidata nd;
   1740 	u_int32_t *tl;
   1741 	int32_t t1;
   1742 	char *bpos;
   1743 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
   1744 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   1745 	char *cp2;
   1746 	struct mbuf *mb, *mreq;
   1747 	struct vnode *vp, *dirp;
   1748 	struct vattr dirfor, diraft;
   1749 	nfsrvfh_t nsfh;
   1750 	u_quad_t frev;
   1751 
   1752 #ifndef nolint
   1753 	vp = (struct vnode *)0;
   1754 #endif
   1755 	nfsm_srvmtofh(&nsfh);
   1756 	nfsm_srvnamesiz(len);
   1757 	nd.ni_cnd.cn_cred = cred;
   1758 	nd.ni_cnd.cn_nameiop = DELETE;
   1759 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   1760 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   1761 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   1762 	if (dirp && v3) {
   1763 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   1764 	}
   1765 	if (!error) {
   1766 		vp = nd.ni_vp;
   1767 		if (vp->v_type == VDIR &&
   1768 		    (error = kauth_authorize_generic(cred,
   1769 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
   1770 			goto out;
   1771 		/*
   1772 		 * The root of a mounted filesystem cannot be deleted.
   1773 		 */
   1774 		if (vp->v_vflag & VV_ROOT) {
   1775 			error = EBUSY;
   1776 			goto out;
   1777 		}
   1778 out:
   1779 		if (!error) {
   1780 			nqsrv_getl(nd.ni_dvp, ND_WRITE);
   1781 			nqsrv_getl(vp, ND_WRITE);
   1782 			error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1783 		} else {
   1784 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1785 			if (nd.ni_dvp == vp)
   1786 				vrele(nd.ni_dvp);
   1787 			else
   1788 				vput(nd.ni_dvp);
   1789 			vput(vp);
   1790 		}
   1791 	}
   1792 	if (dirp) {
   1793 		if (v3) {
   1794 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   1795 		}
   1796 		vrele(dirp);
   1797 	}
   1798 	nfsm_reply(NFSX_WCCDATA(v3));
   1799 	if (v3) {
   1800 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   1801 		return (0);
   1802 	}
   1803 	nfsm_srvdone;
   1804 }
   1805 
   1806 /*
   1807  * nfs rename service
   1808  */
   1809 int
   1810 nfsrv_rename(nfsd, slp, lwp, mrq)
   1811 	struct nfsrv_descript *nfsd;
   1812 	struct nfssvc_sock *slp;
   1813 	struct lwp *lwp;
   1814 	struct mbuf **mrq;
   1815 {
   1816 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   1817 	struct mbuf *nam = nfsd->nd_nam;
   1818 	char *dpos = nfsd->nd_dpos;
   1819 	kauth_cred_t cred = nfsd->nd_cr;
   1820 	u_int32_t *tl;
   1821 	int32_t t1;
   1822 	char *bpos;
   1823 	int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1;
   1824 	uint32_t len, len2;
   1825 	int tdirfor_ret = 1, tdiraft_ret = 1;
   1826 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   1827 	char *cp2;
   1828 	struct mbuf *mb, *mreq;
   1829 	struct nameidata fromnd, tond;
   1830 	struct vnode *fvp, *tvp, *tdvp;
   1831 	struct vnode *fdirp = NULL, *tdirp = NULL;
   1832 	struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
   1833 	nfsrvfh_t fnsfh, tnsfh;
   1834 	u_quad_t frev;
   1835 	uid_t saved_uid;
   1836 
   1837 #ifndef nolint
   1838 	fvp = (struct vnode *)0;
   1839 #endif
   1840 	fromnd.ni_cnd.cn_nameiop = 0;
   1841 	tond.ni_cnd.cn_nameiop = 0;
   1842 	nfsm_srvmtofh(&fnsfh);
   1843 	nfsm_srvnamesiz(len);
   1844 	/*
   1845 	 * Remember our original uid so that we can reset cr_uid before
   1846 	 * the second nfs_namei() call, in case it is remapped.
   1847 	 */
   1848 	saved_uid = kauth_cred_geteuid(cred);
   1849 	fromnd.ni_cnd.cn_cred = cred;
   1850 	fromnd.ni_cnd.cn_nameiop = DELETE;
   1851 	fromnd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
   1852 	error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md,
   1853 		&dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   1854 	if (fdirp && v3) {
   1855 		fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred);
   1856 	}
   1857 	if (error) {
   1858 		nfsm_reply(2 * NFSX_WCCDATA(v3));
   1859 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
   1860 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
   1861 		if (fdirp)
   1862 			vrele(fdirp);
   1863 		return (0);
   1864 	}
   1865 	if (fromnd.ni_dvp != fromnd.ni_vp) {
   1866 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   1867 	}
   1868 	fvp = fromnd.ni_vp;
   1869 	nfsm_srvmtofh(&tnsfh);
   1870 	if (v3) {
   1871 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
   1872 		len2 = fxdr_unsigned(uint32_t, *tl);
   1873 		/* len2 will be checked by nfs_namei */
   1874 	}
   1875 	else {
   1876 		/* NFSv2 */
   1877 		nfsm_strsiz(len2, NFS_MAXNAMLEN);
   1878 	}
   1879 	kauth_cred_seteuid(cred, saved_uid);
   1880 	tond.ni_cnd.cn_cred = cred;
   1881 	tond.ni_cnd.cn_nameiop = RENAME;
   1882 	tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
   1883 	error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md,
   1884 		&dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   1885 	if (tdirp && v3) {
   1886 		tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred);
   1887 	}
   1888 	if (error) {
   1889 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1890 		vrele(fromnd.ni_dvp);
   1891 		vrele(fvp);
   1892 		goto out1;
   1893 	}
   1894 	tdvp = tond.ni_dvp;
   1895 	tvp = tond.ni_vp;
   1896 	if (tvp != NULL) {
   1897 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1898 			if (v3)
   1899 				error = EEXIST;
   1900 			else
   1901 				error = EISDIR;
   1902 			goto out;
   1903 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1904 			if (v3)
   1905 				error = EEXIST;
   1906 			else
   1907 				error = ENOTDIR;
   1908 			goto out;
   1909 		}
   1910 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
   1911 			if (v3)
   1912 				error = EXDEV;
   1913 			else
   1914 				error = ENOTEMPTY;
   1915 			goto out;
   1916 		}
   1917 	}
   1918 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
   1919 		if (v3)
   1920 			error = EXDEV;
   1921 		else
   1922 			error = ENOTEMPTY;
   1923 		goto out;
   1924 	}
   1925 	if (fvp->v_mount != tdvp->v_mount) {
   1926 		if (v3)
   1927 			error = EXDEV;
   1928 		else
   1929 			error = ENOTEMPTY;
   1930 		goto out;
   1931 	}
   1932 	if (fvp == tdvp) {
   1933 		if (v3)
   1934 			error = EINVAL;
   1935 		else
   1936 			error = ENOTEMPTY;
   1937 	}
   1938 	/*
   1939 	 * If source is the same as the destination (that is the
   1940 	 * same vnode with the same name in the same directory),
   1941 	 * then there is nothing to do.
   1942 	 */
   1943 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
   1944 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   1945 	    !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
   1946 	      fromnd.ni_cnd.cn_namelen))
   1947 		error = -1;
   1948 out:
   1949 	if (!error) {
   1950 		nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
   1951 		nqsrv_getl(tdvp, ND_WRITE);
   1952 		if (tvp) {
   1953 			nqsrv_getl(tvp, ND_WRITE);
   1954 		}
   1955 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1956 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1957 	} else {
   1958 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1959 		if (tdvp == tvp)
   1960 			vrele(tdvp);
   1961 		else
   1962 			vput(tdvp);
   1963 		if (tvp)
   1964 			vput(tvp);
   1965 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1966 		vrele(fromnd.ni_dvp);
   1967 		vrele(fvp);
   1968 		if (error == -1)
   1969 			error = 0;
   1970 	}
   1971 	vrele(tond.ni_startdir);
   1972 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   1973 out1:
   1974 	if (fdirp) {
   1975 		if (v3) {
   1976 			fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
   1977 		}
   1978 		vrele(fdirp);
   1979 	}
   1980 	if (tdirp) {
   1981 		if (v3) {
   1982 			tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
   1983 		}
   1984 		vrele(tdirp);
   1985 	}
   1986 	vrele(fromnd.ni_startdir);
   1987 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   1988 	nfsm_reply(2 * NFSX_WCCDATA(v3));
   1989 	if (v3) {
   1990 		nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
   1991 		nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
   1992 	}
   1993 	return (0);
   1994 
   1995 nfsmout:
   1996 	if (fdirp)
   1997 		vrele(fdirp);
   1998 #ifdef notdef
   1999 	if (tdirp)
   2000 		vrele(tdirp);
   2001 #endif
   2002 	if (tond.ni_cnd.cn_nameiop) {
   2003 		vrele(tond.ni_startdir);
   2004 		PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   2005 	}
   2006 	if (fromnd.ni_cnd.cn_nameiop) {
   2007 		vrele(fromnd.ni_startdir);
   2008 		PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   2009 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2010 		vrele(fromnd.ni_dvp);
   2011 		vrele(fvp);
   2012 	}
   2013 	return (error);
   2014 }
   2015 
   2016 /*
   2017  * nfs link service
   2018  */
   2019 int
   2020 nfsrv_link(nfsd, slp, lwp, mrq)
   2021 	struct nfsrv_descript *nfsd;
   2022 	struct nfssvc_sock *slp;
   2023 	struct lwp *lwp;
   2024 	struct mbuf **mrq;
   2025 {
   2026 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2027 	struct mbuf *nam = nfsd->nd_nam;
   2028 	char *dpos = nfsd->nd_dpos;
   2029 	kauth_cred_t cred = nfsd->nd_cr;
   2030 	struct nameidata nd;
   2031 	u_int32_t *tl;
   2032 	int32_t t1;
   2033 	char *bpos;
   2034 	int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
   2035 	int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
   2036 	char *cp2;
   2037 	struct mbuf *mb, *mreq;
   2038 	struct vnode *vp, *xp, *dirp = (struct vnode *)0;
   2039 	struct vattr dirfor, diraft, at;
   2040 	nfsrvfh_t nsfh, dnsfh;
   2041 	u_quad_t frev;
   2042 
   2043 	nfsm_srvmtofh(&nsfh);
   2044 	nfsm_srvmtofh(&dnsfh);
   2045 	nfsm_srvnamesiz(len);
   2046 	error = nfsrv_fhtovp(&nsfh, false, &vp, cred, slp, nam,
   2047 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   2048 	if (error) {
   2049 		nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2050 		nfsm_srvpostop_attr(getret, &at);
   2051 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2052 		return (0);
   2053 	}
   2054 	if (vp->v_type == VDIR && (error = kauth_authorize_generic(cred,
   2055 	    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
   2056 		goto out1;
   2057 	nd.ni_cnd.cn_cred = cred;
   2058 	nd.ni_cnd.cn_nameiop = CREATE;
   2059 	nd.ni_cnd.cn_flags = LOCKPARENT;
   2060 	error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos,
   2061 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   2062 	if (dirp && v3) {
   2063 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   2064 	}
   2065 	if (error)
   2066 		goto out1;
   2067 	xp = nd.ni_vp;
   2068 	if (xp != NULL) {
   2069 		error = EEXIST;
   2070 		goto out;
   2071 	}
   2072 	xp = nd.ni_dvp;
   2073 	if (vp->v_mount != xp->v_mount)
   2074 		error = EXDEV;
   2075 out:
   2076 	if (!error) {
   2077 		nqsrv_getl(vp, ND_WRITE);
   2078 		nqsrv_getl(xp, ND_WRITE);
   2079 		error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   2080 	} else {
   2081 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2082 		if (nd.ni_dvp == nd.ni_vp)
   2083 			vrele(nd.ni_dvp);
   2084 		else
   2085 			vput(nd.ni_dvp);
   2086 		if (nd.ni_vp)
   2087 			vrele(nd.ni_vp);
   2088 	}
   2089 out1:
   2090 	if (v3)
   2091 		getret = VOP_GETATTR(vp, &at, cred);
   2092 	if (dirp) {
   2093 		if (v3) {
   2094 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   2095 		}
   2096 		vrele(dirp);
   2097 	}
   2098 	vrele(vp);
   2099 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
   2100 	if (v3) {
   2101 		nfsm_srvpostop_attr(getret, &at);
   2102 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2103 		return (0);
   2104 	}
   2105 	nfsm_srvdone;
   2106 }
   2107 
   2108 /*
   2109  * nfs symbolic link service
   2110  */
   2111 int
   2112 nfsrv_symlink(nfsd, slp, lwp, mrq)
   2113 	struct nfsrv_descript *nfsd;
   2114 	struct nfssvc_sock *slp;
   2115 	struct lwp *lwp;
   2116 	struct mbuf **mrq;
   2117 {
   2118 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2119 	struct mbuf *nam = nfsd->nd_nam;
   2120 	char *dpos = nfsd->nd_dpos;
   2121 	kauth_cred_t cred = nfsd->nd_cr;
   2122 	struct vattr va, dirfor, diraft;
   2123 	struct nameidata nd;
   2124 	u_int32_t *tl;
   2125 	int32_t t1;
   2126 	struct nfsv2_sattr *sp;
   2127 	char *bpos, *pathcp = NULL, *cp2;
   2128 	struct uio io;
   2129 	struct iovec iv;
   2130 	int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1;
   2131 	uint32_t len, len2;
   2132 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2133 	struct mbuf *mb, *mreq;
   2134 	struct vnode *dirp = (struct vnode *)0;
   2135 	nfsrvfh_t nsfh;
   2136 	u_quad_t frev;
   2137 
   2138 	nd.ni_cnd.cn_nameiop = 0;
   2139 	nfsm_srvmtofh(&nsfh);
   2140 	nfsm_srvnamesiz(len);
   2141 	nd.ni_cnd.cn_cred = cred;
   2142 	nd.ni_cnd.cn_nameiop = CREATE;
   2143 	nd.ni_cnd.cn_flags = LOCKPARENT;
   2144 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   2145 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   2146 	if (dirp && v3) {
   2147 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   2148 	}
   2149 	if (error)
   2150 		goto out;
   2151 	VATTR_NULL(&va);
   2152 	va.va_type = VLNK;
   2153 	if (v3) {
   2154 		va.va_mode = 0;
   2155 		nfsm_srvsattr(&va);
   2156 		nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
   2157 		len2 = fxdr_unsigned(uint32_t, *tl);
   2158 		if (len2 > PATH_MAX) {
   2159 			/* XXX should check _PC_NO_TRUNC */
   2160 			error = ENAMETOOLONG;
   2161 			goto abortop;
   2162 		}
   2163 	}
   2164 	else {
   2165 		/* NFSv2 */
   2166 		nfsm_strsiz(len2, NFS_MAXPATHLEN);
   2167 	}
   2168 	pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
   2169 	iv.iov_base = pathcp;
   2170 	iv.iov_len = len2;
   2171 	io.uio_resid = len2;
   2172 	io.uio_offset = 0;
   2173 	io.uio_iov = &iv;
   2174 	io.uio_iovcnt = 1;
   2175 	io.uio_rw = UIO_READ;
   2176 	UIO_SETUP_SYSSPACE(&io);
   2177 	nfsm_mtouio(&io, len2);
   2178 	if (!v3) {
   2179 		nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2180 		va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
   2181 	}
   2182 	*(pathcp + len2) = '\0';
   2183 	if (nd.ni_vp) {
   2184 		error = EEXIST;
   2185 abortop:
   2186 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2187 		if (nd.ni_dvp == nd.ni_vp)
   2188 			vrele(nd.ni_dvp);
   2189 		else
   2190 			vput(nd.ni_dvp);
   2191 		if (nd.ni_vp)
   2192 			vrele(nd.ni_vp);
   2193 		goto out;
   2194 	}
   2195 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2196 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
   2197 	if (!error) {
   2198 	    if (v3) {
   2199 		error = nfsrv_composefh(nd.ni_vp, &nsfh, v3);
   2200 		if (!error)
   2201 		    error = VOP_GETATTR(nd.ni_vp, &va, cred);
   2202 		vput(nd.ni_vp);
   2203 	    } else {
   2204 		vput(nd.ni_vp);
   2205 	    }
   2206 	}
   2207 out:
   2208 	if (pathcp)
   2209 		free(pathcp, M_TEMP);
   2210 	if (dirp) {
   2211 		if (v3) {
   2212 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   2213 		}
   2214 		vrele(dirp);
   2215 	}
   2216 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
   2217 	    NFSX_WCCDATA(v3));
   2218 	if (v3) {
   2219 		if (!error) {
   2220 			nfsm_srvpostop_fh(&nsfh);
   2221 			nfsm_srvpostop_attr(0, &va);
   2222 		}
   2223 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2224 	}
   2225 	return (0);
   2226 nfsmout:
   2227 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2228 	if (nd.ni_dvp == nd.ni_vp)
   2229 		vrele(nd.ni_dvp);
   2230 	else
   2231 		vput(nd.ni_dvp);
   2232 	if (nd.ni_vp)
   2233 		vrele(nd.ni_vp);
   2234 	if (dirp)
   2235 		vrele(dirp);
   2236 	if (pathcp)
   2237 		free(pathcp, M_TEMP);
   2238 	return (error);
   2239 }
   2240 
   2241 /*
   2242  * nfs mkdir service
   2243  */
   2244 int
   2245 nfsrv_mkdir(nfsd, slp, lwp, mrq)
   2246 	struct nfsrv_descript *nfsd;
   2247 	struct nfssvc_sock *slp;
   2248 	struct lwp *lwp;
   2249 	struct mbuf **mrq;
   2250 {
   2251 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2252 	struct mbuf *nam = nfsd->nd_nam;
   2253 	char *dpos = nfsd->nd_dpos;
   2254 	kauth_cred_t cred = nfsd->nd_cr;
   2255 	struct vattr va, dirfor, diraft;
   2256 	struct nfs_fattr *fp;
   2257 	struct nameidata nd;
   2258 	char *cp;
   2259 	u_int32_t *tl;
   2260 	int32_t t1;
   2261 	char *bpos;
   2262 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
   2263 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2264 	char *cp2;
   2265 	struct mbuf *mb, *mreq;
   2266 	struct vnode *vp, *dirp = (struct vnode *)0;
   2267 	nfsrvfh_t nsfh;
   2268 	u_quad_t frev;
   2269 
   2270 	nfsm_srvmtofh(&nsfh);
   2271 	nfsm_srvnamesiz(len);
   2272 	nd.ni_cnd.cn_cred = cred;
   2273 	nd.ni_cnd.cn_nameiop = CREATE;
   2274 	nd.ni_cnd.cn_flags = LOCKPARENT;
   2275 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   2276 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   2277 	if (dirp && v3) {
   2278 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   2279 	}
   2280 	if (error) {
   2281 		nfsm_reply(NFSX_WCCDATA(v3));
   2282 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2283 		if (dirp)
   2284 			vrele(dirp);
   2285 		return (0);
   2286 	}
   2287 	VATTR_NULL(&va);
   2288 	if (v3) {
   2289 		va.va_mode = 0;
   2290 		nfsm_srvsattr(&va);
   2291 	} else {
   2292 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2293 		va.va_mode = nfstov_mode(*tl++);
   2294 	}
   2295 	va.va_type = VDIR;
   2296 	vp = nd.ni_vp;
   2297 	if (vp != NULL) {
   2298 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2299 		if (nd.ni_dvp == vp)
   2300 			vrele(nd.ni_dvp);
   2301 		else
   2302 			vput(nd.ni_dvp);
   2303 		vrele(vp);
   2304 		error = EEXIST;
   2305 		goto out;
   2306 	}
   2307 	nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2308 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
   2309 	if (!error) {
   2310 		vp = nd.ni_vp;
   2311 		error = nfsrv_composefh(vp, &nsfh, v3);
   2312 		if (!error)
   2313 			error = VOP_GETATTR(vp, &va, cred);
   2314 		vput(vp);
   2315 	}
   2316 out:
   2317 	if (dirp) {
   2318 		if (v3) {
   2319 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   2320 		}
   2321 		vrele(dirp);
   2322 	}
   2323 	nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
   2324 	    NFSX_WCCDATA(v3));
   2325 	if (v3) {
   2326 		if (!error) {
   2327 			nfsm_srvpostop_fh(&nsfh);
   2328 			nfsm_srvpostop_attr(0, &va);
   2329 		}
   2330 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2331 	} else {
   2332 		nfsm_srvfhtom(&nsfh, v3);
   2333 		nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
   2334 		nfsm_srvfillattr(&va, fp);
   2335 	}
   2336 	return (0);
   2337 nfsmout:
   2338 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2339 	if (nd.ni_dvp == nd.ni_vp)
   2340 		vrele(nd.ni_dvp);
   2341 	else
   2342 		vput(nd.ni_dvp);
   2343 	if (nd.ni_vp)
   2344 		vrele(nd.ni_vp);
   2345 	if (dirp)
   2346 		vrele(dirp);
   2347 	return (error);
   2348 }
   2349 
   2350 /*
   2351  * nfs rmdir service
   2352  */
   2353 int
   2354 nfsrv_rmdir(nfsd, slp, lwp, mrq)
   2355 	struct nfsrv_descript *nfsd;
   2356 	struct nfssvc_sock *slp;
   2357 	struct lwp *lwp;
   2358 	struct mbuf **mrq;
   2359 {
   2360 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2361 	struct mbuf *nam = nfsd->nd_nam;
   2362 	char *dpos = nfsd->nd_dpos;
   2363 	kauth_cred_t cred = nfsd->nd_cr;
   2364 	u_int32_t *tl;
   2365 	int32_t t1;
   2366 	char *bpos;
   2367 	int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
   2368 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2369 	char *cp2;
   2370 	struct mbuf *mb, *mreq;
   2371 	struct vnode *vp, *dirp = (struct vnode *)0;
   2372 	struct vattr dirfor, diraft;
   2373 	nfsrvfh_t nsfh;
   2374 	struct nameidata nd;
   2375 	u_quad_t frev;
   2376 
   2377 	nfsm_srvmtofh(&nsfh);
   2378 	nfsm_srvnamesiz(len);
   2379 	nd.ni_cnd.cn_cred = cred;
   2380 	nd.ni_cnd.cn_nameiop = DELETE;
   2381 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
   2382 	error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
   2383 		&dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
   2384 	if (dirp && v3) {
   2385 		dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred);
   2386 	}
   2387 	if (error) {
   2388 		nfsm_reply(NFSX_WCCDATA(v3));
   2389 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2390 		if (dirp)
   2391 			vrele(dirp);
   2392 		return (0);
   2393 	}
   2394 	vp = nd.ni_vp;
   2395 	if (vp->v_type != VDIR) {
   2396 		error = ENOTDIR;
   2397 		goto out;
   2398 	}
   2399 	/*
   2400 	 * No rmdir "." please.
   2401 	 */
   2402 	if (nd.ni_dvp == vp) {
   2403 		error = EINVAL;
   2404 		goto out;
   2405 	}
   2406 	/*
   2407 	 * The root of a mounted filesystem cannot be deleted.
   2408 	 */
   2409 	if (vp->v_vflag & VV_ROOT)
   2410 		error = EBUSY;
   2411 out:
   2412 	if (!error) {
   2413 		nqsrv_getl(nd.ni_dvp, ND_WRITE);
   2414 		nqsrv_getl(vp, ND_WRITE);
   2415 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2416 	} else {
   2417 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2418 		if (nd.ni_dvp == nd.ni_vp)
   2419 			vrele(nd.ni_dvp);
   2420 		else
   2421 			vput(nd.ni_dvp);
   2422 		vput(vp);
   2423 	}
   2424 	if (dirp) {
   2425 		if (v3) {
   2426 			diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
   2427 		}
   2428 		vrele(dirp);
   2429 	}
   2430 	nfsm_reply(NFSX_WCCDATA(v3));
   2431 	if (v3) {
   2432 		nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
   2433 		return (0);
   2434 	}
   2435 	nfsm_srvdone;
   2436 }
   2437 
   2438 /*
   2439  * nfs readdir service
   2440  * - mallocs what it thinks is enough to read
   2441  *	count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR
   2442  * - calls VOP_READDIR()
   2443  * - loops around building the reply
   2444  *	if the output generated exceeds count break out of loop
   2445  *	The nfsm_clget macro is used here so that the reply will be packed
   2446  *	tightly in mbuf clusters.
   2447  * - it only knows that it has encountered eof when the VOP_READDIR()
   2448  *	reads nothing
   2449  * - as such one readdir rpc will return eof false although you are there
   2450  *	and then the next will return eof
   2451  * - it trims out records with d_fileno == 0
   2452  *	this doesn't matter for Unix clients, but they might confuse clients
   2453  *	for other os'.
   2454  * - it trims out records with d_type == DT_WHT
   2455  *	these cannot be seen through NFS (unless we extend the protocol)
   2456  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
   2457  *	than requested, but this may not apply to all filesystems. For
   2458  *	example, client NFS does not { although it is never remote mounted
   2459  *	anyhow }
   2460  *     The alternate call nfsrv_readdirplus() does lookups as well.
   2461  * PS: The NFS protocol spec. does not clarify what the "count" byte
   2462  *	argument is a count of.. just name strings and file id's or the
   2463  *	entire reply rpc or ...
   2464  *	I tried just file name and id sizes and it confused the Sun client,
   2465  *	so I am using the full rpc size now. The "paranoia.." comment refers
   2466  *	to including the status longwords that are not a part of the dir.
   2467  *	"entry" structures, but are in the rpc.
   2468  */
   2469 
   2470 #define	NFS_SRVDIRBLKSIZ	1024
   2471 
   2472 struct flrep {
   2473 	nfsuint64 fl_off;
   2474 	u_int32_t fl_postopok;
   2475 	u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
   2476 	u_int32_t fl_fhok;
   2477 	u_int32_t fl_fhsize;
   2478 };
   2479 
   2480 int
   2481 nfsrv_readdir(nfsd, slp, lwp, mrq)
   2482 	struct nfsrv_descript *nfsd;
   2483 	struct nfssvc_sock *slp;
   2484 	struct lwp *lwp;
   2485 	struct mbuf **mrq;
   2486 {
   2487 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2488 	struct mbuf *nam = nfsd->nd_nam;
   2489 	char *dpos = nfsd->nd_dpos;
   2490 	kauth_cred_t cred = nfsd->nd_cr;
   2491 	char *bp, *be;
   2492 	struct mbuf *mp;
   2493 	struct dirent *dp;
   2494 	char *cp;
   2495 	u_int32_t *tl;
   2496 	int32_t t1;
   2497 	char *bpos;
   2498 	struct mbuf *mb, *mreq, *mp2;
   2499 	char *cpos, *cend, *cp2, *rbuf;
   2500 	struct vnode *vp;
   2501 	struct vattr at;
   2502 	nfsrvfh_t nsfh;
   2503 	struct uio io;
   2504 	struct iovec iv;
   2505 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
   2506 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies;
   2507 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   2508 	u_quad_t frev, off, toff, verf;
   2509 	off_t *cookies = NULL, *cookiep;
   2510 	nfsuint64 jar;
   2511 
   2512 	nfsm_srvmtofh(&nsfh);
   2513 	if (v3) {
   2514 		nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2515 		toff = fxdr_hyper(tl);
   2516 		tl += 2;
   2517 		verf = fxdr_hyper(tl);
   2518 		tl += 2;
   2519 	} else {
   2520 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2521 		toff = fxdr_unsigned(u_quad_t, *tl++);
   2522 	}
   2523 	off = toff;
   2524 	cnt = fxdr_unsigned(int, *tl);
   2525 	siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
   2526 	xfer = NFS_SRVMAXDATA(nfsd);
   2527 	if (siz > xfer)
   2528 		siz = xfer;
   2529 	fullsiz = siz;
   2530 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   2531 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   2532 	if (!error && vp->v_type != VDIR) {
   2533 		error = ENOTDIR;
   2534 		vput(vp);
   2535 	}
   2536 	if (error) {
   2537 		nfsm_reply(NFSX_UNSIGNED);
   2538 		nfsm_srvpostop_attr(getret, &at);
   2539 		return (0);
   2540 	}
   2541 	nqsrv_getl(vp, ND_READ);
   2542 	if (v3) {
   2543 		error = getret = VOP_GETATTR(vp, &at, cred);
   2544 #ifdef NFS3_STRICTVERF
   2545 		/*
   2546 		 * XXX This check is too strict for Solaris 2.5 clients.
   2547 		 */
   2548 		if (!error && toff && verf != at.va_filerev)
   2549 			error = NFSERR_BAD_COOKIE;
   2550 #endif
   2551 	}
   2552 	if (!error)
   2553 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
   2554 	if (error) {
   2555 		vput(vp);
   2556 		nfsm_reply(NFSX_POSTOPATTR(v3));
   2557 		nfsm_srvpostop_attr(getret, &at);
   2558 		return (0);
   2559 	}
   2560 	VOP_UNLOCK(vp, 0);
   2561 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
   2562 again:
   2563 	iv.iov_base = rbuf;
   2564 	iv.iov_len = fullsiz;
   2565 	io.uio_iov = &iv;
   2566 	io.uio_iovcnt = 1;
   2567 	io.uio_offset = (off_t)off;
   2568 	io.uio_resid = fullsiz;
   2569 	io.uio_rw = UIO_READ;
   2570 	UIO_SETUP_SYSSPACE(&io);
   2571 	eofflag = 0;
   2572 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2573 
   2574 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
   2575 
   2576 	off = (off_t)io.uio_offset;
   2577 	if (!cookies && !error)
   2578 		error = NFSERR_PERM;
   2579 	if (v3) {
   2580 		getret = VOP_GETATTR(vp, &at, cred);
   2581 		if (!error)
   2582 			error = getret;
   2583 	}
   2584 
   2585 	VOP_UNLOCK(vp, 0);
   2586 	if (error) {
   2587 		vrele(vp);
   2588 		free((void *)rbuf, M_TEMP);
   2589 		if (cookies)
   2590 			free((void *)cookies, M_TEMP);
   2591 		nfsm_reply(NFSX_POSTOPATTR(v3));
   2592 		nfsm_srvpostop_attr(getret, &at);
   2593 		return (0);
   2594 	}
   2595 	if (io.uio_resid) {
   2596 		siz -= io.uio_resid;
   2597 
   2598 		/*
   2599 		 * If nothing read, return eof
   2600 		 * rpc reply
   2601 		 */
   2602 		if (siz == 0) {
   2603 			vrele(vp);
   2604 			nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
   2605 				2 * NFSX_UNSIGNED);
   2606 			if (v3) {
   2607 				nfsm_srvpostop_attr(getret, &at);
   2608 				nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   2609 				txdr_hyper(at.va_filerev, tl);
   2610 				tl += 2;
   2611 			} else
   2612 				nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2613 			*tl++ = nfs_false;
   2614 			*tl = nfs_true;
   2615 			free((void *)rbuf, M_TEMP);
   2616 			free((void *)cookies, M_TEMP);
   2617 			return (0);
   2618 		}
   2619 	}
   2620 
   2621 	/*
   2622 	 * Check for degenerate cases of nothing useful read.
   2623 	 * If so go try again
   2624 	 */
   2625 	cpos = rbuf;
   2626 	cend = rbuf + siz;
   2627 	dp = (struct dirent *)cpos;
   2628 	cookiep = cookies;
   2629 
   2630 	while (cpos < cend && ncookies > 0 &&
   2631 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
   2632 		cpos += dp->d_reclen;
   2633 		dp = (struct dirent *)cpos;
   2634 		cookiep++;
   2635 		ncookies--;
   2636 	}
   2637 	if (cpos >= cend || ncookies == 0) {
   2638 		toff = off;
   2639 		siz = fullsiz;
   2640 		free(cookies, M_TEMP);
   2641 		cookies = NULL;
   2642 		goto again;
   2643 	}
   2644 
   2645 	len = 3 * NFSX_UNSIGNED;	/* paranoia, probably can be 0 */
   2646 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
   2647 	if (v3) {
   2648 		nfsm_srvpostop_attr(getret, &at);
   2649 		nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2650 		txdr_hyper(at.va_filerev, tl);
   2651 	}
   2652 	mp = mp2 = mb;
   2653 	bp = bpos;
   2654 	be = bp + M_TRAILINGSPACE(mp);
   2655 
   2656 	/* Loop through the records and build reply */
   2657 	while (cpos < cend && ncookies > 0) {
   2658 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
   2659 			nlen = dp->d_namlen;
   2660 			rem = nfsm_rndup(nlen)-nlen;
   2661 			len += (4 * NFSX_UNSIGNED + nlen + rem);
   2662 			if (v3)
   2663 				len += 2 * NFSX_UNSIGNED;
   2664 			if (len > cnt) {
   2665 				eofflag = 0;
   2666 				break;
   2667 			}
   2668 			/*
   2669 			 * Build the directory record xdr from
   2670 			 * the dirent entry.
   2671 			 */
   2672 			nfsm_clget;
   2673 			*tl = nfs_true;
   2674 			bp += NFSX_UNSIGNED;
   2675 			if (v3) {
   2676 				nfsm_clget;
   2677 				*tl = txdr_unsigned(dp->d_fileno >> 32);
   2678 				bp += NFSX_UNSIGNED;
   2679 			}
   2680 			nfsm_clget;
   2681 			*tl = txdr_unsigned(dp->d_fileno);
   2682 			bp += NFSX_UNSIGNED;
   2683 			nfsm_clget;
   2684 			*tl = txdr_unsigned(nlen);
   2685 			bp += NFSX_UNSIGNED;
   2686 
   2687 			/* And loop around copying the name */
   2688 			xfer = nlen;
   2689 			cp = dp->d_name;
   2690 			while (xfer > 0) {
   2691 				nfsm_clget;
   2692 				if ((bp+xfer) > be)
   2693 					tsiz = be-bp;
   2694 				else
   2695 					tsiz = xfer;
   2696 				memcpy(bp, cp, tsiz);
   2697 				bp += tsiz;
   2698 				xfer -= tsiz;
   2699 				if (xfer > 0)
   2700 					cp += tsiz;
   2701 			}
   2702 			/* And null pad to an int32_t boundary */
   2703 			for (i = 0; i < rem; i++)
   2704 				*bp++ = '\0';
   2705 			nfsm_clget;
   2706 
   2707 			/* Finish off the record */
   2708 			txdr_hyper(*cookiep, &jar);
   2709 			if (v3) {
   2710 				*tl = jar.nfsuquad[0];
   2711 				bp += NFSX_UNSIGNED;
   2712 				nfsm_clget;
   2713 			}
   2714 			*tl = jar.nfsuquad[1];
   2715 			bp += NFSX_UNSIGNED;
   2716 		}
   2717 		cpos += dp->d_reclen;
   2718 		dp = (struct dirent *)cpos;
   2719 		cookiep++;
   2720 		ncookies--;
   2721 	}
   2722 	vrele(vp);
   2723 	nfsm_clget;
   2724 	*tl = nfs_false;
   2725 	bp += NFSX_UNSIGNED;
   2726 	nfsm_clget;
   2727 	if (eofflag)
   2728 		*tl = nfs_true;
   2729 	else
   2730 		*tl = nfs_false;
   2731 	bp += NFSX_UNSIGNED;
   2732 	if (mp != mb) {
   2733 		if (bp < be)
   2734 			mp->m_len = bp - mtod(mp, char *);
   2735 	} else
   2736 		mp->m_len += bp - bpos;
   2737 	free((void *)rbuf, M_TEMP);
   2738 	free((void *)cookies, M_TEMP);
   2739 	nfsm_srvdone;
   2740 }
   2741 
   2742 int
   2743 nfsrv_readdirplus(nfsd, slp, lwp, mrq)
   2744 	struct nfsrv_descript *nfsd;
   2745 	struct nfssvc_sock *slp;
   2746 	struct lwp *lwp;
   2747 	struct mbuf **mrq;
   2748 {
   2749 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   2750 	struct mbuf *nam = nfsd->nd_nam;
   2751 	char *dpos = nfsd->nd_dpos;
   2752 	kauth_cred_t cred = nfsd->nd_cr;
   2753 	char *bp, *be;
   2754 	struct mbuf *mp;
   2755 	struct dirent *dp;
   2756 	char *cp;
   2757 	u_int32_t *tl;
   2758 	int32_t t1;
   2759 	char *bpos;
   2760 	struct mbuf *mb, *mreq, *mp2;
   2761 	char *cpos, *cend, *cp2, *rbuf;
   2762 	struct vnode *vp, *nvp;
   2763 	struct flrep fl;
   2764 	nfsrvfh_t nsfh;
   2765 	struct uio io;
   2766 	struct iovec iv;
   2767 	struct vattr va, at, *vap = &va;
   2768 	struct nfs_fattr *fp;
   2769 	int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
   2770 	int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies;
   2771 	u_quad_t frev, off, toff, verf;
   2772 	off_t *cookies = NULL, *cookiep;
   2773 
   2774 	nfsm_srvmtofh(&nsfh);
   2775 	nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2776 	toff = fxdr_hyper(tl);
   2777 	tl += 2;
   2778 	verf = fxdr_hyper(tl);
   2779 	tl += 2;
   2780 	siz = fxdr_unsigned(int, *tl++);
   2781 	cnt = fxdr_unsigned(int, *tl);
   2782 	off = toff;
   2783 	siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
   2784 	xfer = NFS_SRVMAXDATA(nfsd);
   2785 	if (siz > xfer)
   2786 		siz = xfer;
   2787 	fullsiz = siz;
   2788 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   2789 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   2790 	if (!error && vp->v_type != VDIR) {
   2791 		error = ENOTDIR;
   2792 		vput(vp);
   2793 	}
   2794 	if (error) {
   2795 		nfsm_reply(NFSX_UNSIGNED);
   2796 		nfsm_srvpostop_attr(getret, &at);
   2797 		return (0);
   2798 	}
   2799 	error = getret = VOP_GETATTR(vp, &at, cred);
   2800 #ifdef NFS3_STRICTVERF
   2801 	/*
   2802 	 * XXX This check is too strict for Solaris 2.5 clients.
   2803 	 */
   2804 	if (!error && toff && verf != at.va_filerev)
   2805 		error = NFSERR_BAD_COOKIE;
   2806 #endif
   2807 	if (!error) {
   2808 		nqsrv_getl(vp, ND_READ);
   2809 		error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
   2810 	}
   2811 	if (error) {
   2812 		vput(vp);
   2813 		nfsm_reply(NFSX_V3POSTOPATTR);
   2814 		nfsm_srvpostop_attr(getret, &at);
   2815 		return (0);
   2816 	}
   2817 	VOP_UNLOCK(vp, 0);
   2818 
   2819 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
   2820 again:
   2821 	iv.iov_base = rbuf;
   2822 	iv.iov_len = fullsiz;
   2823 	io.uio_iov = &iv;
   2824 	io.uio_iovcnt = 1;
   2825 	io.uio_offset = (off_t)off;
   2826 	io.uio_resid = fullsiz;
   2827 	io.uio_rw = UIO_READ;
   2828 	UIO_SETUP_SYSSPACE(&io);
   2829 	eofflag = 0;
   2830 
   2831 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2832 
   2833 	error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
   2834 
   2835 	off = (u_quad_t)io.uio_offset;
   2836 	getret = VOP_GETATTR(vp, &at, cred);
   2837 
   2838 	VOP_UNLOCK(vp, 0);
   2839 
   2840 	/*
   2841 	 * If the VGET operation doesn't work for this filesystem,
   2842 	 * we can't support readdirplus. Returning NOTSUPP should
   2843 	 * make clients fall back to plain readdir.
   2844 	 * There's no need to check for VPTOFH as well, we wouldn't
   2845 	 * even be here otherwise.
   2846 	 */
   2847 	if (!getret) {
   2848 		if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
   2849 			getret = (getret == EOPNOTSUPP) ?
   2850 				NFSERR_NOTSUPP : NFSERR_IO;
   2851 		else
   2852 			vput(nvp);
   2853 	}
   2854 
   2855 	if (!cookies && !error)
   2856 		error = NFSERR_PERM;
   2857 	if (!error)
   2858 		error = getret;
   2859 	if (error) {
   2860 		vrele(vp);
   2861 		if (cookies)
   2862 			free((void *)cookies, M_TEMP);
   2863 		free((void *)rbuf, M_TEMP);
   2864 		nfsm_reply(NFSX_V3POSTOPATTR);
   2865 		nfsm_srvpostop_attr(getret, &at);
   2866 		return (0);
   2867 	}
   2868 	if (io.uio_resid) {
   2869 		siz -= io.uio_resid;
   2870 
   2871 		/*
   2872 		 * If nothing read, return eof
   2873 		 * rpc reply
   2874 		 */
   2875 		if (siz == 0) {
   2876 			vrele(vp);
   2877 			nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
   2878 				2 * NFSX_UNSIGNED);
   2879 			nfsm_srvpostop_attr(getret, &at);
   2880 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   2881 			txdr_hyper(at.va_filerev, tl);
   2882 			tl += 2;
   2883 			*tl++ = nfs_false;
   2884 			*tl = nfs_true;
   2885 			free((void *)cookies, M_TEMP);
   2886 			free((void *)rbuf, M_TEMP);
   2887 			return (0);
   2888 		}
   2889 	}
   2890 
   2891 	/*
   2892 	 * Check for degenerate cases of nothing useful read.
   2893 	 * If so go try again
   2894 	 */
   2895 	cpos = rbuf;
   2896 	cend = rbuf + siz;
   2897 	dp = (struct dirent *)cpos;
   2898 	cookiep = cookies;
   2899 
   2900 	while (cpos < cend && ncookies > 0 &&
   2901 		(dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
   2902 		cpos += dp->d_reclen;
   2903 		dp = (struct dirent *)cpos;
   2904 		cookiep++;
   2905 		ncookies--;
   2906 	}
   2907 	if (cpos >= cend || ncookies == 0) {
   2908 		toff = off;
   2909 		siz = fullsiz;
   2910 		free(cookies, M_TEMP);
   2911 		cookies = NULL;
   2912 		goto again;
   2913 	}
   2914 
   2915 	dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
   2916 	nfsm_reply(cnt);
   2917 	nfsm_srvpostop_attr(getret, &at);
   2918 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2919 	txdr_hyper(at.va_filerev, tl);
   2920 	mp = mp2 = mb;
   2921 	bp = bpos;
   2922 	be = bp + M_TRAILINGSPACE(mp);
   2923 
   2924 	/* Loop through the records and build reply */
   2925 	while (cpos < cend && ncookies > 0) {
   2926 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
   2927 			nfsrvfh_t nnsfh;
   2928 
   2929 			nlen = dp->d_namlen;
   2930 			rem = nfsm_rndup(nlen)-nlen;
   2931 
   2932 			/*
   2933 			 * For readdir_and_lookup get the vnode using
   2934 			 * the file number.
   2935 			 */
   2936 			if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
   2937 				goto invalid;
   2938 			if (nfsrv_composefh(nvp, &nnsfh, true)) {
   2939 				vput(nvp);
   2940 				goto invalid;
   2941 			}
   2942 			if (VOP_GETATTR(nvp, vap, cred)) {
   2943 				vput(nvp);
   2944 				goto invalid;
   2945 			}
   2946 			vput(nvp);
   2947 
   2948 			/*
   2949 			 * If either the dircount or maxcount will be
   2950 			 * exceeded, get out now. Both of these lengths
   2951 			 * are calculated conservatively, including all
   2952 			 * XDR overheads.
   2953 			 */
   2954 			len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
   2955 				NFSX_V3POSTOPATTR);
   2956 			dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
   2957 			if (len > cnt || dirlen > fullsiz) {
   2958 				eofflag = 0;
   2959 				break;
   2960 			}
   2961 
   2962 			/*
   2963 			 * Build the directory record xdr from
   2964 			 * the dirent entry.
   2965 			 */
   2966 			fp = (struct nfs_fattr *)&fl.fl_fattr;
   2967 			nfsm_srvfillattr(vap, fp);
   2968 			fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
   2969 			fl.fl_fhok = nfs_true;
   2970 			fl.fl_postopok = nfs_true;
   2971 			txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
   2972 
   2973 			nfsm_clget;
   2974 			*tl = nfs_true;
   2975 			bp += NFSX_UNSIGNED;
   2976 			nfsm_clget;
   2977 			*tl = txdr_unsigned(dp->d_fileno >> 32);
   2978 			bp += NFSX_UNSIGNED;
   2979 			nfsm_clget;
   2980 			*tl = txdr_unsigned(dp->d_fileno);
   2981 			bp += NFSX_UNSIGNED;
   2982 			nfsm_clget;
   2983 			*tl = txdr_unsigned(nlen);
   2984 			bp += NFSX_UNSIGNED;
   2985 
   2986 			/* And loop around copying the name */
   2987 			xfer = nlen;
   2988 			cp = dp->d_name;
   2989 			while (xfer > 0) {
   2990 				nfsm_clget;
   2991 				if ((bp + xfer) > be)
   2992 					tsiz = be - bp;
   2993 				else
   2994 					tsiz = xfer;
   2995 				memcpy(bp, cp, tsiz);
   2996 				bp += tsiz;
   2997 				xfer -= tsiz;
   2998 				if (xfer > 0)
   2999 					cp += tsiz;
   3000 			}
   3001 			/* And null pad to an int32_t boundary */
   3002 			for (i = 0; i < rem; i++)
   3003 				*bp++ = '\0';
   3004 
   3005 			/*
   3006 			 * Now copy the flrep structure out.
   3007 			 */
   3008 			xfer = sizeof(struct flrep);
   3009 			cp = (void *)&fl;
   3010 			while (xfer > 0) {
   3011 				nfsm_clget;
   3012 				if ((bp + xfer) > be)
   3013 					tsiz = be - bp;
   3014 				else
   3015 					tsiz = xfer;
   3016 				memcpy(bp, cp, tsiz);
   3017 				bp += tsiz;
   3018 				xfer -= tsiz;
   3019 				if (xfer > 0)
   3020 					cp += tsiz;
   3021 			}
   3022 
   3023 			/*
   3024 			 * ... and filehandle.
   3025 			 */
   3026 			xfer = NFSRVFH_SIZE(&nnsfh);
   3027 			cp = NFSRVFH_DATA(&nnsfh);
   3028 			while (xfer > 0) {
   3029 				nfsm_clget;
   3030 				if ((bp + xfer) > be)
   3031 					tsiz = be - bp;
   3032 				else
   3033 					tsiz = xfer;
   3034 				memcpy(bp, cp, tsiz);
   3035 				bp += tsiz;
   3036 				xfer -= tsiz;
   3037 				if (xfer > 0)
   3038 					cp += tsiz;
   3039 			}
   3040 		}
   3041 invalid:
   3042 		cpos += dp->d_reclen;
   3043 		dp = (struct dirent *)cpos;
   3044 		cookiep++;
   3045 		ncookies--;
   3046 	}
   3047 	vrele(vp);
   3048 	nfsm_clget;
   3049 	*tl = nfs_false;
   3050 	bp += NFSX_UNSIGNED;
   3051 	nfsm_clget;
   3052 	if (eofflag)
   3053 		*tl = nfs_true;
   3054 	else
   3055 		*tl = nfs_false;
   3056 	bp += NFSX_UNSIGNED;
   3057 	if (mp != mb) {
   3058 		if (bp < be)
   3059 			mp->m_len = bp - mtod(mp, char *);
   3060 	} else
   3061 		mp->m_len += bp - bpos;
   3062 	free((void *)cookies, M_TEMP);
   3063 	free((void *)rbuf, M_TEMP);
   3064 	nfsm_srvdone;
   3065 }
   3066 
   3067 /*
   3068  * nfs commit service
   3069  */
   3070 int
   3071 nfsrv_commit(nfsd, slp, lwp, mrq)
   3072 	struct nfsrv_descript *nfsd;
   3073 	struct nfssvc_sock *slp;
   3074 	struct lwp *lwp;
   3075 	struct mbuf **mrq;
   3076 {
   3077 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3078 	struct mbuf *nam = nfsd->nd_nam;
   3079 	char *dpos = nfsd->nd_dpos;
   3080 	kauth_cred_t cred = nfsd->nd_cr;
   3081 	struct vattr bfor, aft;
   3082 	struct vnode *vp;
   3083 	nfsrvfh_t nsfh;
   3084 	u_int32_t *tl;
   3085 	int32_t t1;
   3086 	char *bpos;
   3087 	int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0;
   3088 	uint32_t cnt;
   3089 	char *cp2;
   3090 	struct mbuf *mb, *mreq;
   3091 	u_quad_t frev, off, end;
   3092 
   3093 	nfsm_srvmtofh(&nsfh);
   3094 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   3095 
   3096 	off = fxdr_hyper(tl);
   3097 	tl += 2;
   3098 	cnt = fxdr_unsigned(uint32_t, *tl);
   3099 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   3100 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   3101 	if (error) {
   3102 		nfsm_reply(2 * NFSX_UNSIGNED);
   3103 		nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
   3104 		return (0);
   3105 	}
   3106 	for_ret = VOP_GETATTR(vp, &bfor, cred);
   3107 	end = (cnt > 0) ? off + cnt : vp->v_size;
   3108 	if (end < off || end > vp->v_size)
   3109 		end = vp->v_size;
   3110 	if (off < vp->v_size)
   3111 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end);
   3112 	/* else error == 0, from nfsrv_fhtovp() */
   3113 	aft_ret = VOP_GETATTR(vp, &aft, cred);
   3114 	vput(vp);
   3115 	nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
   3116 	nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
   3117 	if (!error) {
   3118 		nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
   3119 		*tl++ = txdr_unsigned(boottime.tv_sec);
   3120 		*tl = txdr_unsigned(boottime.tv_usec);
   3121 	} else {
   3122 		return (0);
   3123 	}
   3124 	nfsm_srvdone;
   3125 }
   3126 
   3127 /*
   3128  * nfs statfs service
   3129  */
   3130 int
   3131 nfsrv_statfs(nfsd, slp, lwp, mrq)
   3132 	struct nfsrv_descript *nfsd;
   3133 	struct nfssvc_sock *slp;
   3134 	struct lwp *lwp;
   3135 	struct mbuf **mrq;
   3136 {
   3137 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3138 	struct mbuf *nam = nfsd->nd_nam;
   3139 	char *dpos = nfsd->nd_dpos;
   3140 	kauth_cred_t cred = nfsd->nd_cr;
   3141 	struct statvfs *sf = NULL;
   3142 	struct nfs_statfs *sfp;
   3143 	u_int32_t *tl;
   3144 	int32_t t1;
   3145 	char *bpos;
   3146 	int error = 0, rdonly, cache = 0, getret = 1;
   3147 	int v3 = (nfsd->nd_flag & ND_NFSV3);
   3148 	char *cp2;
   3149 	struct mbuf *mb, *mreq;
   3150 	struct vnode *vp;
   3151 	struct vattr at;
   3152 	nfsrvfh_t nsfh;
   3153 	u_quad_t frev, tval;
   3154 
   3155 	nfsm_srvmtofh(&nsfh);
   3156 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   3157 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   3158 	if (error) {
   3159 		nfsm_reply(NFSX_UNSIGNED);
   3160 		nfsm_srvpostop_attr(getret, &at);
   3161 		return (0);
   3162 	}
   3163 	sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK);
   3164 	error = VFS_STATVFS(vp->v_mount, sf);
   3165 	getret = VOP_GETATTR(vp, &at, cred);
   3166 	vput(vp);
   3167 	nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
   3168 	if (v3)
   3169 		nfsm_srvpostop_attr(getret, &at);
   3170 	if (error) {
   3171 		free(sf, M_TEMP);
   3172 		return (0);
   3173 	}
   3174 	nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
   3175 	if (v3) {
   3176 		tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
   3177 		txdr_hyper(tval, &sfp->sf_tbytes);
   3178 		tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
   3179 		txdr_hyper(tval, &sfp->sf_fbytes);
   3180 		tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
   3181 		txdr_hyper(tval, &sfp->sf_abytes);
   3182 		tval = (u_quad_t)sf->f_files;
   3183 		txdr_hyper(tval, &sfp->sf_tfiles);
   3184 		tval = (u_quad_t)sf->f_ffree;
   3185 		txdr_hyper(tval, &sfp->sf_ffiles);
   3186 		txdr_hyper(tval, &sfp->sf_afiles);
   3187 		sfp->sf_invarsec = 0;
   3188 	} else {
   3189 		sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
   3190 		sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
   3191 		sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
   3192 		sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
   3193 		sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
   3194 	}
   3195 nfsmout:
   3196 	if (sf)
   3197 	    free(sf, M_TEMP);
   3198 	return error;
   3199 }
   3200 
   3201 /*
   3202  * nfs fsinfo service
   3203  */
   3204 int
   3205 nfsrv_fsinfo(nfsd, slp, lwp, mrq)
   3206 	struct nfsrv_descript *nfsd;
   3207 	struct nfssvc_sock *slp;
   3208 	struct lwp *lwp;
   3209 	struct mbuf **mrq;
   3210 {
   3211 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3212 	struct mbuf *nam = nfsd->nd_nam;
   3213 	char *dpos = nfsd->nd_dpos;
   3214 	kauth_cred_t cred = nfsd->nd_cr;
   3215 	u_int32_t *tl;
   3216 	struct nfsv3_fsinfo *sip;
   3217 	int32_t t1;
   3218 	char *bpos;
   3219 	int error = 0, rdonly, cache = 0, getret = 1;
   3220 	uint32_t maxdata;
   3221 	char *cp2;
   3222 	struct mbuf *mb, *mreq;
   3223 	struct vnode *vp;
   3224 	struct vattr at;
   3225 	nfsrvfh_t nsfh;
   3226 	u_quad_t frev, maxfsize;
   3227 	struct statvfs *sb;
   3228 
   3229 	nfsm_srvmtofh(&nsfh);
   3230 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   3231 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   3232 	if (error) {
   3233 		nfsm_reply(NFSX_UNSIGNED);
   3234 		nfsm_srvpostop_attr(getret, &at);
   3235 		return (0);
   3236 	}
   3237 
   3238 	/* XXX Try to make a guess on the max file size. */
   3239 	sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
   3240 	VFS_STATVFS(vp->v_mount, sb);
   3241 	maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1;
   3242 	free(sb, M_TEMP);
   3243 
   3244 	getret = VOP_GETATTR(vp, &at, cred);
   3245 	vput(vp);
   3246 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
   3247 	nfsm_srvpostop_attr(getret, &at);
   3248 	nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
   3249 
   3250 	/*
   3251 	 * XXX
   3252 	 * There should be file system VFS OP(s) to get this information.
   3253 	 * For now, assume ufs.
   3254 	 */
   3255 	if (slp->ns_so->so_type == SOCK_DGRAM)
   3256 		maxdata = NFS_MAXDGRAMDATA;
   3257 	else
   3258 		maxdata = NFS_MAXDATA;
   3259 	sip->fs_rtmax = txdr_unsigned(maxdata);
   3260 	sip->fs_rtpref = txdr_unsigned(maxdata);
   3261 	sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
   3262 	sip->fs_wtmax = txdr_unsigned(maxdata);
   3263 	sip->fs_wtpref = txdr_unsigned(maxdata);
   3264 	sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
   3265 	sip->fs_dtpref = txdr_unsigned(maxdata);
   3266 	txdr_hyper(maxfsize, &sip->fs_maxfilesize);
   3267 	sip->fs_timedelta.nfsv3_sec = 0;
   3268 	sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
   3269 	sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
   3270 		NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
   3271 		NFSV3FSINFO_CANSETTIME);
   3272 	nfsm_srvdone;
   3273 }
   3274 
   3275 /*
   3276  * nfs pathconf service
   3277  */
   3278 int
   3279 nfsrv_pathconf(nfsd, slp, lwp, mrq)
   3280 	struct nfsrv_descript *nfsd;
   3281 	struct nfssvc_sock *slp;
   3282 	struct lwp *lwp;
   3283 	struct mbuf **mrq;
   3284 {
   3285 	struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
   3286 	struct mbuf *nam = nfsd->nd_nam;
   3287 	char *dpos = nfsd->nd_dpos;
   3288 	kauth_cred_t cred = nfsd->nd_cr;
   3289 	u_int32_t *tl;
   3290 	struct nfsv3_pathconf *pc;
   3291 	int32_t t1;
   3292 	char *bpos;
   3293 	int error = 0, rdonly, cache = 0, getret = 1;
   3294 	register_t linkmax, namemax, chownres, notrunc;
   3295 	char *cp2;
   3296 	struct mbuf *mb, *mreq;
   3297 	struct vnode *vp;
   3298 	struct vattr at;
   3299 	nfsrvfh_t nsfh;
   3300 	u_quad_t frev;
   3301 
   3302 	nfsm_srvmtofh(&nsfh);
   3303 	error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
   3304 		 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
   3305 	if (error) {
   3306 		nfsm_reply(NFSX_UNSIGNED);
   3307 		nfsm_srvpostop_attr(getret, &at);
   3308 		return (0);
   3309 	}
   3310 	error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
   3311 	if (!error)
   3312 		error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
   3313 	if (!error)
   3314 		error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
   3315 	if (!error)
   3316 		error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
   3317 	getret = VOP_GETATTR(vp, &at, cred);
   3318 	vput(vp);
   3319 	nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
   3320 	nfsm_srvpostop_attr(getret, &at);
   3321 	if (error)
   3322 		return (0);
   3323 	nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
   3324 
   3325 	pc->pc_linkmax = txdr_unsigned(linkmax);
   3326 	pc->pc_namemax = txdr_unsigned(namemax);
   3327 	pc->pc_notrunc = txdr_unsigned(notrunc);
   3328 	pc->pc_chownrestricted = txdr_unsigned(chownres);
   3329 
   3330 	/*
   3331 	 * These should probably be supported by VOP_PATHCONF(), but
   3332 	 * until msdosfs is exportable (why would you want to?), the
   3333 	 * Unix defaults should be ok.
   3334 	 */
   3335 	pc->pc_caseinsensitive = nfs_false;
   3336 	pc->pc_casepreserving = nfs_true;
   3337 	nfsm_srvdone;
   3338 }
   3339 
   3340 /*
   3341  * Null operation, used by clients to ping server
   3342  */
   3343 /* ARGSUSED */
   3344 int
   3345 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
   3346     struct lwp *lwp, struct mbuf **mrq)
   3347 {
   3348 	struct mbuf *mrep = nfsd->nd_mrep;
   3349 	char *bpos;
   3350 	int error = NFSERR_RETVOID, cache = 0;
   3351 	struct mbuf *mb, *mreq;
   3352 	u_quad_t frev;
   3353 
   3354 	nfsm_reply(0);
   3355 	return (0);
   3356 }
   3357 
   3358 /*
   3359  * No operation, used for obsolete procedures
   3360  */
   3361 /* ARGSUSED */
   3362 int
   3363 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
   3364     struct lwp *lwp, struct mbuf **mrq)
   3365 {
   3366 	struct mbuf *mrep = nfsd->nd_mrep;
   3367 	char *bpos;
   3368 	int error, cache = 0;
   3369 	struct mbuf *mb, *mreq;
   3370 	u_quad_t frev;
   3371 
   3372 	if (nfsd->nd_repstat)
   3373 		error = nfsd->nd_repstat;
   3374 	else
   3375 		error = EPROCUNAVAIL;
   3376 	nfsm_reply(0);
   3377 	return (0);
   3378 }
   3379 
   3380 /*
   3381  * Perform access checking for vnodes obtained from file handles that would
   3382  * refer to files already opened by a Unix client. You cannot just use
   3383  * vn_writechk() and VOP_ACCESS() for two reasons.
   3384  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
   3385  * 2 - The owner is to be given access irrespective of mode bits for some
   3386  *     operations, so that processes that chmod after opening a file don't
   3387  *     break. I don't like this because it opens a security hole, but since
   3388  *     the nfs server opens a security hole the size of a barn door anyhow,
   3389  *     what the heck.
   3390  *
   3391  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
   3392  * will return EPERM instead of EACCESS. EPERM is always an error.
   3393  */
   3394 int
   3395 nfsrv_access(vp, flags, cred, rdonly, lwp, override)
   3396 	struct vnode *vp;
   3397 	int flags;
   3398 	kauth_cred_t cred;
   3399 	int rdonly;
   3400 	struct lwp *lwp;
   3401 	int override;
   3402 {
   3403 	struct vattr vattr;
   3404 	int error;
   3405 	if (flags & VWRITE) {
   3406 		/* Just vn_writechk() changed to check rdonly */
   3407 		/*
   3408 		 * Disallow write attempts on read-only file systems;
   3409 		 * unless the file is a socket or a block or character
   3410 		 * device resident on the file system.
   3411 		 */
   3412 		if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3413 			switch (vp->v_type) {
   3414 			case VREG:
   3415 			case VDIR:
   3416 			case VLNK:
   3417 				return (EROFS);
   3418 			default:
   3419 				break;
   3420 			}
   3421 		}
   3422 
   3423 		/*
   3424 		 * If the vnode is in use as a process's text,
   3425 		 * we can't allow writing.
   3426 		 */
   3427 		if (vp->v_iflag & VI_TEXT)
   3428 			return (ETXTBSY);
   3429 	}
   3430 	error = VOP_GETATTR(vp, &vattr, cred);
   3431 	if (error)
   3432 		return (error);
   3433 	error = VOP_ACCESS(vp, flags, cred);
   3434 	/*
   3435 	 * Allow certain operations for the owner (reads and writes
   3436 	 * on files that are already open).
   3437 	 */
   3438 	if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid)
   3439 		error = 0;
   3440 	return error;
   3441 }
   3442