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