⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nfs_subs.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1989, 1993 *	The Regents of the University of California.  All rights reserved. * * This code is derived from software contributed to Berkeley by * Rick Macklem at The University of Guelph. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *	@(#)nfs_subs.c	8.3 (Berkeley) 1/4/94 *//* * These functions support the macros and help fiddle mbuf chains for * the nfs op functions. They do things like create the rpc header and * copy data between mbuf chains and uio lists. */#include <sys/param.h>#include <sys/proc.h>#include <sys/systm.h>#include <sys/kernel.h>#include <sys/mount.h>#include <sys/vnode.h>#include <sys/namei.h>#include <sys/mbuf.h>#include <sys/socket.h>#include <sys/stat.h>#include <nfs/rpcv2.h>#include <nfs/nfsv2.h>#include <nfs/nfsnode.h>#include <nfs/nfs.h>#include <nfs/xdr_subs.h>#include <nfs/nfsm_subs.h>#include <nfs/nfsmount.h>#include <nfs/nqnfs.h>#include <nfs/nfsrtt.h>#include <miscfs/specfs/specdev.h>#include <netinet/in.h>#ifdef ISO#include <netiso/iso.h>#endif#define TRUE	1#define	FALSE	0/* * Data items converted to xdr at startup, since they are constant * This is kinda hokey, but may save a little time doing byte swaps */u_long nfs_procids[NFS_NPROCS];u_long nfs_xdrneg1;u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred,	rpc_auth_kerb;u_long nfs_vers, nfs_prog, nfs_true, nfs_false;/* And other global data */static u_long nfs_xid = 0;enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON };extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];extern struct nfsreq nfsreqh;extern int nqnfs_piggy[NFS_NPROCS];extern struct nfsrtt nfsrtt;extern time_t nqnfsstarttime;extern u_long nqnfs_prog, nqnfs_vers;extern int nqsrv_clockskew;extern int nqsrv_writeslack;extern int nqsrv_maxlease;/* * Create the header for an rpc request packet * The hsiz is the size of the rest of the nfs request header. * (just used to decide if a cluster is a good idea) */struct mbuf *nfsm_reqh(vp, procid, hsiz, bposp)	struct vnode *vp;	u_long procid;	int hsiz;	caddr_t *bposp;{	register struct mbuf *mb;	register u_long *tl;	register caddr_t bpos;	struct mbuf *mb2;	struct nfsmount *nmp;	int nqflag;	MGET(mb, M_WAIT, MT_DATA);	if (hsiz >= MINCLSIZE)		MCLGET(mb, M_WAIT);	mb->m_len = 0;	bpos = mtod(mb, caddr_t);		/*	 * For NQNFS, add lease request.	 */	if (vp) {		nmp = VFSTONFS(vp->v_mount);		if (nmp->nm_flag & NFSMNT_NQNFS) {			nqflag = NQNFS_NEEDLEASE(vp, procid);			if (nqflag) {				nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);				*tl++ = txdr_unsigned(nqflag);				*tl = txdr_unsigned(nmp->nm_leaseterm);			} else {				nfsm_build(tl, u_long *, NFSX_UNSIGNED);				*tl = 0;			}		}	}	/* Finally, return values */	*bposp = bpos;	return (mb);}/* * Build the RPC header and fill in the authorization info. * The authorization string argument is only used when the credentials * come from outside of the kernel. * Returns the head of the mbuf list. */struct mbuf *nfsm_rpchead(cr, nqnfs, procid, auth_type, auth_len, auth_str, mrest,	mrest_len, mbp, xidp)	register struct ucred *cr;	int nqnfs;	int procid;	int auth_type;	int auth_len;	char *auth_str;	struct mbuf *mrest;	int mrest_len;	struct mbuf **mbp;	u_long *xidp;{	register struct mbuf *mb;	register u_long *tl;	register caddr_t bpos;	register int i;	struct mbuf *mreq, *mb2;	int siz, grpsiz, authsiz;	authsiz = nfsm_rndup(auth_len);	if (auth_type == RPCAUTH_NQNFS)		authsiz += 2 * NFSX_UNSIGNED;	MGETHDR(mb, M_WAIT, MT_DATA);	if ((authsiz + 10*NFSX_UNSIGNED) >= MINCLSIZE) {		MCLGET(mb, M_WAIT);	} else if ((authsiz + 10*NFSX_UNSIGNED) < MHLEN) {		MH_ALIGN(mb, authsiz + 10*NFSX_UNSIGNED);	} else {		MH_ALIGN(mb, 8*NFSX_UNSIGNED);	}	mb->m_len = 0;	mreq = mb;	bpos = mtod(mb, caddr_t);	/*	 * First the RPC header.	 */	nfsm_build(tl, u_long *, 8*NFSX_UNSIGNED);	if (++nfs_xid == 0)		nfs_xid++;	*tl++ = *xidp = txdr_unsigned(nfs_xid);	*tl++ = rpc_call;	*tl++ = rpc_vers;	if (nqnfs) {		*tl++ = txdr_unsigned(NQNFS_PROG);		*tl++ = txdr_unsigned(NQNFS_VER1);	} else {		*tl++ = txdr_unsigned(NFS_PROG);		*tl++ = txdr_unsigned(NFS_VER2);	}	*tl++ = txdr_unsigned(procid);	/*	 * And then the authorization cred.	 */	*tl++ = txdr_unsigned(auth_type);	*tl = txdr_unsigned(authsiz);	switch (auth_type) {	case RPCAUTH_UNIX:		nfsm_build(tl, u_long *, auth_len);		*tl++ = 0;		/* stamp ?? */		*tl++ = 0;		/* NULL hostname */		*tl++ = txdr_unsigned(cr->cr_uid);		*tl++ = txdr_unsigned(cr->cr_groups[0]);		grpsiz = (auth_len >> 2) - 5;		*tl++ = txdr_unsigned(grpsiz);		for (i = 1; i <= grpsiz; i++)			*tl++ = txdr_unsigned(cr->cr_groups[i]);		break;	case RPCAUTH_NQNFS:		nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);		*tl++ = txdr_unsigned(cr->cr_uid);		*tl = txdr_unsigned(auth_len);		siz = auth_len;		while (siz > 0) {			if (M_TRAILINGSPACE(mb) == 0) {				MGET(mb2, M_WAIT, MT_DATA);				if (siz >= MINCLSIZE)					MCLGET(mb2, M_WAIT);				mb->m_next = mb2;				mb = mb2;				mb->m_len = 0;				bpos = mtod(mb, caddr_t);			}			i = min(siz, M_TRAILINGSPACE(mb));			bcopy(auth_str, bpos, i);			mb->m_len += i;			auth_str += i;			bpos += i;			siz -= i;		}		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {			for (i = 0; i < siz; i++)				*bpos++ = '\0';			mb->m_len += siz;		}		break;	};	nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);	*tl++ = txdr_unsigned(RPCAUTH_NULL);	*tl = 0;	mb->m_next = mrest;	mreq->m_pkthdr.len = authsiz + 10*NFSX_UNSIGNED + mrest_len;	mreq->m_pkthdr.rcvif = (struct ifnet *)0;	*mbp = mb;	return (mreq);}/* * copies mbuf chain to the uio scatter/gather list */nfsm_mbuftouio(mrep, uiop, siz, dpos)	struct mbuf **mrep;	register struct uio *uiop;	int siz;	caddr_t *dpos;{	register char *mbufcp, *uiocp;	register int xfer, left, len;	register struct mbuf *mp;	long uiosiz, rem;	int error = 0;	mp = *mrep;	mbufcp = *dpos;	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;	rem = nfsm_rndup(siz)-siz;	while (siz > 0) {		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)			return (EFBIG);		left = uiop->uio_iov->iov_len;		uiocp = uiop->uio_iov->iov_base;		if (left > siz)			left = siz;		uiosiz = left;		while (left > 0) {			while (len == 0) {				mp = mp->m_next;				if (mp == NULL)					return (EBADRPC);				mbufcp = mtod(mp, caddr_t);				len = mp->m_len;			}			xfer = (left > len) ? len : left;#ifdef notdef			/* Not Yet.. */			if (uiop->uio_iov->iov_op != NULL)				(*(uiop->uio_iov->iov_op))				(mbufcp, uiocp, xfer);			else#endif			if (uiop->uio_segflg == UIO_SYSSPACE)				bcopy(mbufcp, uiocp, xfer);			else				copyout(mbufcp, uiocp, xfer);			left -= xfer;			len -= xfer;			mbufcp += xfer;			uiocp += xfer;			uiop->uio_offset += xfer;			uiop->uio_resid -= xfer;		}		if (uiop->uio_iov->iov_len <= siz) {			uiop->uio_iovcnt--;			uiop->uio_iov++;		} else {			uiop->uio_iov->iov_base += uiosiz;			uiop->uio_iov->iov_len -= uiosiz;		}		siz -= uiosiz;	}	*dpos = mbufcp;	*mrep = mp;	if (rem > 0) {		if (len < rem)			error = nfs_adv(mrep, dpos, rem, len);		else			*dpos += rem;	}	return (error);}/* * copies a uio scatter/gather list to an mbuf chain... */nfsm_uiotombuf(uiop, mq, siz, bpos)	register struct uio *uiop;	struct mbuf **mq;	int siz;	caddr_t *bpos;{	register char *uiocp;	register struct mbuf *mp, *mp2;	register int xfer, left, mlen;	int uiosiz, clflg, rem;	char *cp;	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */		clflg = 1;	else		clflg = 0;	rem = nfsm_rndup(siz)-siz;	mp = mp2 = *mq;	while (siz > 0) {		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)			return (EINVAL);		left = uiop->uio_iov->iov_len;		uiocp = uiop->uio_iov->iov_base;		if (left > siz)			left = siz;		uiosiz = left;		while (left > 0) {			mlen = M_TRAILINGSPACE(mp);			if (mlen == 0) {				MGET(mp, M_WAIT, MT_DATA);				if (clflg)					MCLGET(mp, M_WAIT);				mp->m_len = 0;				mp2->m_next = mp;				mp2 = mp;				mlen = M_TRAILINGSPACE(mp);			}			xfer = (left > mlen) ? mlen : left;#ifdef notdef			/* Not Yet.. */			if (uiop->uio_iov->iov_op != NULL)				(*(uiop->uio_iov->iov_op))				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);			else#endif			if (uiop->uio_segflg == UIO_SYSSPACE)				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);			else				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);			mp->m_len += xfer;			left -= xfer;			uiocp += xfer;			uiop->uio_offset += xfer;			uiop->uio_resid -= xfer;		}		if (uiop->uio_iov->iov_len <= siz) {			uiop->uio_iovcnt--;			uiop->uio_iov++;		} else {			uiop->uio_iov->iov_base += uiosiz;			uiop->uio_iov->iov_len -= uiosiz;		}		siz -= uiosiz;	}	if (rem > 0) {		if (rem > M_TRAILINGSPACE(mp)) {			MGET(mp, M_WAIT, MT_DATA);			mp->m_len = 0;			mp2->m_next = mp;		}		cp = mtod(mp, caddr_t)+mp->m_len;		for (left = 0; left < rem; left++)			*cp++ = '\0';		mp->m_len += rem;		*bpos = cp;	} else		*bpos = mtod(mp, caddr_t)+mp->m_len;	*mq = mp;	return (0);}/* * Help break down an mbuf chain by setting the first siz bytes contiguous * pointed to by returned val. * This is used by the macros nfsm_dissect and nfsm_dissecton for tough * cases. (The macros use the vars. dpos and dpos2) */nfsm_disct(mdp, dposp, siz, left, cp2)	struct mbuf **mdp;	caddr_t *dposp;	int siz;	int left;	caddr_t *cp2;{	register struct mbuf *mp, *mp2;	register int siz2, xfer;	register caddr_t p;	mp = *mdp;	while (left == 0) {		*mdp = mp = mp->m_next;		if (mp == NULL)			return (EBADRPC);		left = mp->m_len;		*dposp = mtod(mp, caddr_t);	}	if (left >= siz) {		*cp2 = *dposp;		*dposp += siz;	} else if (mp->m_next == NULL) {		return (EBADRPC);	} else if (siz > MHLEN) {		panic("nfs S too big");	} else {		MGET(mp2, M_WAIT, MT_DATA);		mp2->m_next = mp->m_next;		mp->m_next = mp2;		mp->m_len -= left;		mp = mp2;		*cp2 = p = mtod(mp, caddr_t);		bcopy(*dposp, p, left);		/* Copy what was left */		siz2 = siz-left;		p += left;		mp2 = mp->m_next;		/* Loop around copying up the siz2 bytes */		while (siz2 > 0) {			if (mp2 == NULL)				return (EBADRPC);			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;			if (xfer > 0) {				bcopy(mtod(mp2, caddr_t), p, xfer);				NFSMADV(mp2, xfer);				mp2->m_len -= xfer;				p += xfer;				siz2 -= xfer;			}			if (siz2 > 0)				mp2 = mp2->m_next;		}		mp->m_len = siz;		*mdp = mp2;		*dposp = mtod(mp2, caddr_t);	}	return (0);}/* * Advance the position in the mbuf chain. */nfs_adv(mdp, dposp, offs, left)	struct mbuf **mdp;	caddr_t *dposp;	int offs;	int left;{	register struct mbuf *m;	register int s;	m = *mdp;	s = left;	while (s < offs) {		offs -= s;		m = m->m_next;		if (m == NULL)			return (EBADRPC);		s = m->m_len;	}	*mdp = m;	*dposp = mtod(m, caddr_t)+offs;	return (0);}/* * Copy a string into mbufs for the hard cases... */nfsm_strtmbuf(mb, bpos, cp, siz)	struct mbuf **mb;	char **bpos;	char *cp;	long siz;{	register struct mbuf *m1, *m2;	long left, xfer, len, tlen;	u_long *tl;	int putsize;	putsize = 1;	m2 = *mb;	left = M_TRAILINGSPACE(m2);	if (left > 0) {		tl = ((u_long *)(*bpos));		*tl++ = txdr_unsigned(siz);		putsize = 0;		left -= NFSX_UNSIGNED;		m2->m_len += NFSX_UNSIGNED;		if (left > 0) {			bcopy(cp, (caddr_t) tl, left);			siz -= left;			cp += left;			m2->m_len += left;			left = 0;		}	}	/* Loop around adding mbufs */	while (siz > 0) {		MGET(m1, M_WAIT, MT_DATA);		if (siz > MLEN)			MCLGET(m1, M_WAIT);		m1->m_len = NFSMSIZ(m1);		m2->m_next = m1;		m2 = m1;		tl = mtod(m1, u_long *);		tlen = 0;		if (putsize) {			*tl++ = txdr_unsigned(siz);			m1->m_len -= NFSX_UNSIGNED;			tlen = NFSX_UNSIGNED;			putsize = 0;		}		if (siz < m1->m_len) {			len = nfsm_rndup(siz);			xfer = siz;			if (xfer < len)				*(tl+(xfer>>2)) = 0;		} else {			xfer = len = m1->m_len;		}		bcopy(cp, (caddr_t) tl, xfer);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -