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