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