Home | History | Annotate | Line # | Download | only in nfs
nfsm_subs.h revision 1.8
      1 /*	$NetBSD: nfsm_subs.h,v 1.8 1996/02/09 21:48:43 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfsm_subs.h	8.1 (Berkeley) 6/16/93
     39  */
     40 
     41 /*
     42  * These macros do strange and peculiar things to mbuf chains for
     43  * the assistance of the nfs code. To attempt to use them for any
     44  * other purpose will be dangerous. (they make weird assumptions)
     45  */
     46 
     47 /*
     48  * First define what the actual subs. return
     49  */
     50 #define	M_HASCL(m)	((m)->m_flags & M_EXT)
     51 #define	NFSMINOFF(m) \
     52 		if (M_HASCL(m)) \
     53 			(m)->m_data = (m)->m_ext.ext_buf; \
     54 		else if ((m)->m_flags & M_PKTHDR) \
     55 			(m)->m_data = (m)->m_pktdat; \
     56 		else \
     57 			(m)->m_data = (m)->m_dat
     58 #define	NFSMADV(m, s)	(m)->m_data += (s)
     59 #define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
     60 				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
     61 
     62 /*
     63  * Now for the macros that do the simple stuff and call the functions
     64  * for the hard stuff.
     65  * These macros use several vars. declared in nfsm_reqhead and these
     66  * vars. must not be used elsewhere unless you are careful not to corrupt
     67  * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
     68  * that may be used so long as the value is not expected to retained
     69  * after a macro.
     70  * I know, this is kind of dorkey, but it makes the actual op functions
     71  * fairly clean and deals with the mess caused by the xdr discriminating
     72  * unions.
     73  */
     74 
     75 #define	nfsm_build(a,c,s) \
     76 		{ if ((s) > M_TRAILINGSPACE(mb)) { \
     77 			MGET(mb2, M_WAIT, MT_DATA); \
     78 			if ((s) > MLEN) \
     79 				panic("build > MLEN"); \
     80 			mb->m_next = mb2; \
     81 			mb = mb2; \
     82 			mb->m_len = 0; \
     83 			bpos = mtod(mb, caddr_t); \
     84 		} \
     85 		(a) = (c)(bpos); \
     86 		mb->m_len += (s); \
     87 		bpos += (s); }
     88 
     89 #define	nfsm_dissect(a,c,s) \
     90 		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
     91 		if (t1 >= (s)) { \
     92 			(a) = (c)(dpos); \
     93 			dpos += (s); \
     94 		} else if ((error = nfsm_disct(&md, &dpos, \
     95 					       (s), t1, &cp2)) != 0) { \
     96 			m_freem(mrep); \
     97 			goto nfsmout; \
     98 		} else { \
     99 			(a) = (c)cp2; \
    100 		} }
    101 
    102 #define nfsm_fhtom(v) \
    103 		nfsm_build(cp,caddr_t,NFSX_FH); \
    104 		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
    105 
    106 #define nfsm_srvfhtom(f) \
    107 		nfsm_build(cp,caddr_t,NFSX_FH); \
    108 		bcopy((caddr_t)(f), cp, NFSX_FH)
    109 
    110 #define nfsm_mtofh(d,v) \
    111 		{ struct nfsnode *np; nfsv2fh_t *fhp; \
    112 		nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \
    113 		if ((error = nfs_nget((d)->v_mount, fhp, &np)) != 0) { \
    114 			m_freem(mrep); \
    115 			goto nfsmout; \
    116 		} \
    117 		(v) = NFSTOV(np); \
    118 		nfsm_loadattr(v, (struct vattr *)0); \
    119 		}
    120 
    121 #define	nfsm_loadattr(v,a) \
    122 		{ struct vnode *tvp = (v); \
    123 		if ((error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) != 0) { \
    124 			m_freem(mrep); \
    125 			goto nfsmout; \
    126 		} \
    127 		(v) = tvp; }
    128 
    129 #define	nfsm_strsiz(s,m) \
    130 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    131 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
    132 			m_freem(mrep); \
    133 			error = EBADRPC; \
    134 			goto nfsmout; \
    135 		} }
    136 
    137 #define	nfsm_srvstrsiz(s,m) \
    138 		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
    139 		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
    140 			error = EBADRPC; \
    141 			nfsm_reply(0); \
    142 		} }
    143 
    144 #define nfsm_mtouio(p,s) \
    145 		if ((s) > 0 && \
    146 		    (error = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
    147 			m_freem(mrep); \
    148 			goto nfsmout; \
    149 		}
    150 
    151 #define nfsm_uiotom(p,s) \
    152 		if ((error = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
    153 			m_freem(mreq); \
    154 			goto nfsmout; \
    155 		}
    156 
    157 #define	nfsm_reqhead(v,a,s) \
    158 		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
    159 
    160 #define nfsm_reqdone	m_freem(mrep); \
    161 		nfsmout:
    162 
    163 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
    164 
    165 #define	nfsm_request(v, t, p, c)	\
    166 		if ((error = nfs_request((v), mreq, (t), (p), \
    167 					 (c), &mrep, &md, &dpos)) != 0) \
    168 			goto nfsmout
    169 
    170 #define	nfsm_strtom(a,s,m) \
    171 		if ((s) > (m)) { \
    172 			m_freem(mreq); \
    173 			error = ENAMETOOLONG; \
    174 			goto nfsmout; \
    175 		} \
    176 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
    177 		if (t2 <= M_TRAILINGSPACE(mb)) { \
    178 			nfsm_build(tl,u_int32_t *,t2); \
    179 			*tl++ = txdr_unsigned(s); \
    180 			*(tl+((t2>>2)-2)) = 0; \
    181 			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
    182 		} else if ((error = nfsm_strtmbuf(&mb, &bpos, \
    183 						  (a), (s))) != 0) { \
    184 			m_freem(mreq); \
    185 			goto nfsmout; \
    186 		}
    187 
    188 #define	nfsm_srvdone \
    189 		nfsmout: \
    190 		return(error)
    191 
    192 #define	nfsm_reply(s) \
    193 		{ \
    194 		nfsd->nd_repstat = error; \
    195 		if (error) \
    196 		   (void) nfs_rephead(0, nfsd, error, cache, &frev, \
    197 			mrq, &mb, &bpos); \
    198 		else \
    199 		   (void) nfs_rephead((s), nfsd, error, cache, &frev, \
    200 			mrq, &mb, &bpos); \
    201 		m_freem(mrep); \
    202 		mreq = *mrq; \
    203 		if (error) \
    204 			return(0); \
    205 		}
    206 
    207 #define	nfsm_adv(s) \
    208 		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
    209 		if (t1 >= (s)) { \
    210 			dpos += (s); \
    211 		} else if ((error = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
    212 			m_freem(mrep); \
    213 			goto nfsmout; \
    214 		}
    215 
    216 #define nfsm_srvmtofh(f) \
    217 		nfsm_dissect(tl, u_int32_t *, NFSX_FH); \
    218 		bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
    219 
    220 #define	nfsm_clget \
    221 		if (bp >= be) { \
    222 			if (mp == mb) \
    223 				mp->m_len += bp-bpos; \
    224 			MGET(mp, M_WAIT, MT_DATA); \
    225 			MCLGET(mp, M_WAIT); \
    226 			mp->m_len = NFSMSIZ(mp); \
    227 			mp2->m_next = mp; \
    228 			mp2 = mp; \
    229 			bp = mtod(mp, caddr_t); \
    230 			be = bp+mp->m_len; \
    231 		} \
    232 		tl = (u_int32_t *)bp
    233 
    234 #define	nfsm_srvfillattr \
    235 	fp->fa_type = vtonfs_type(va.va_type); \
    236 	fp->fa_mode = vtonfs_mode(va.va_type, va.va_mode); \
    237 	fp->fa_nlink = txdr_unsigned(va.va_nlink); \
    238 	fp->fa_uid = txdr_unsigned(va.va_uid); \
    239 	fp->fa_gid = txdr_unsigned(va.va_gid); \
    240 	if (nfsd->nd_nqlflag == NQL_NOVAL) { \
    241 		fp->fa_nfsblocksize = txdr_unsigned(va.va_blocksize); \
    242 		if (va.va_type == VFIFO) \
    243 			fp->fa_nfsrdev = 0xffffffff; \
    244 		else \
    245 			fp->fa_nfsrdev = txdr_unsigned(va.va_rdev); \
    246 		fp->fa_nfsfsid = txdr_unsigned(va.va_fsid); \
    247 		fp->fa_nfsfileid = txdr_unsigned(va.va_fileid); \
    248 		fp->fa_nfssize = txdr_unsigned(va.va_size); \
    249 		fp->fa_nfsblocks = txdr_unsigned(va.va_bytes / NFS_FABLKSIZE); \
    250 		txdr_nfstime(&va.va_atime, &fp->fa_nfsatime); \
    251 		txdr_nfstime(&va.va_mtime, &fp->fa_nfsmtime); \
    252 		txdr_nfstime(&va.va_ctime, &fp->fa_nfsctime); \
    253 	} else { \
    254 		fp->fa_nqblocksize = txdr_unsigned(va.va_blocksize); \
    255 		if (va.va_type == VFIFO) \
    256 			fp->fa_nqrdev = 0xffffffff; \
    257 		else \
    258 			fp->fa_nqrdev = txdr_unsigned(va.va_rdev); \
    259 		fp->fa_nqfsid = txdr_unsigned(va.va_fsid); \
    260 		fp->fa_nqfileid = txdr_unsigned(va.va_fileid); \
    261 		txdr_hyper(&va.va_size, &fp->fa_nqsize); \
    262 		txdr_hyper(&va.va_bytes, &fp->fa_nqbytes); \
    263 		txdr_nqtime(&va.va_atime, &fp->fa_nqatime); \
    264 		txdr_nqtime(&va.va_mtime, &fp->fa_nqmtime); \
    265 		txdr_nqtime(&va.va_ctime, &fp->fa_nqctime); \
    266 		fp->fa_nqflags = txdr_unsigned(va.va_flags); \
    267 		fp->fa_nqgen = txdr_unsigned(va.va_gen); \
    268 		txdr_hyper(&va.va_filerev, &fp->fa_nqfilerev); \
    269 	}
    270 
    271