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