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