| 1 | /* |
| 2 | * Copyright (c) 1989 The Regents of the University of California. |
| 3 | * 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 7.11 (Berkeley) 4/16/91 |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * These macros do strange and peculiar things to mbuf chains for |
| 41 | * the assistance of the nfs code. To attempt to use them for any |
| 42 | * other purpose will be dangerous. (they make weird assumptions) |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * First define what the actual subs. return |
| 47 | */ |
| 48 | extern struct mbuf *nfsm_reqh(); |
| 49 | |
| 50 | #define M_HASCL(m) ((m)->m_flags & M_EXT) |
| 51 | #define NFSMGETHDR(m) \ |
| 52 | MGETHDR(m, M_WAIT, MT_DATA); \ |
| 53 | (m)->m_pkthdr.len = 0; \ |
| 54 | (m)->m_pkthdr.rcvif = (struct ifnet *)0 |
| 55 | #define NFSMINOFF(m) \ |
| 56 | if (M_HASCL(m)) \ |
| 57 | (m)->m_data = (m)->m_ext.ext_buf; \ |
| 58 | else \ |
| 59 | (m)->m_data = (m)->m_dat |
| 60 | #define NFSMADV(m, s) (m)->m_data += (s) |
| 61 | #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \ |
| 62 | (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN)) |
| 63 | |
| 64 | /* |
| 65 | * Now for the macros that do the simple stuff and call the functions |
| 66 | * for the hard stuff. |
| 67 | * These macros use several vars. declared in nfsm_reqhead and these |
| 68 | * vars. must not be used elsewhere unless you are careful not to corrupt |
| 69 | * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries |
| 70 | * that may be used so long as the value is not expected to retained |
| 71 | * after a macro. |
| 72 | * I know, this is kind of dorkey, but it makes the actual op functions |
| 73 | * fairly clean and deals with the mess caused by the xdr discriminating |
| 74 | * unions. |
| 75 | */ |
| 76 | |
| 77 | #ifndef lint |
| 78 | #define nfsm_build(a,c,s) \ |
| 79 | t1 = NFSMSIZ(mb); \ |
| 80 | if ((s) > (t1-mb->m_len)) { \ |
| 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 | #else /* lint */ |
| 93 | #define nfsm_build(a,c,s) \ |
| 94 | t1 = NFSMSIZ(mb); \ |
| 95 | if ((s) > (t1-mb->m_len)) { \ |
| 96 | MGET(mb2, M_WAIT, MT_DATA); \ |
| 97 | mb->m_next = mb2; \ |
| 98 | mb = mb2; \ |
| 99 | mb->m_len = 0; \ |
| 100 | bpos = mtod(mb, caddr_t); \ |
| 101 | } \ |
| 102 | (a) = (c)(bpos); \ |
| 103 | mb->m_len += (s); \ |
| 104 | bpos += (s) |
| 105 | #endif /* lint */ |
| 106 | |
| 107 | #define nfsm_disect(a,c,s) \ |
| 108 | t1 = mtod(md, caddr_t)+md->m_len-dpos; \ |
| 109 | if (t1 >= (s)) { \ |
| 110 | (a) = (c)(dpos); \ |
| 111 | dpos += (s); \ |
| 112 | } else if (error = nfsm_disct(&md, &dpos, (s), t1, TRUE, &cp2)) { \ |
| 113 | m_freem(mrep); \ |
| 114 | goto nfsmout; \ |
| 115 | } else { \ |
| 116 | (a) = (c)cp2; \ |
| 117 | } |
| 118 | |
| 119 | #define nfsm_disecton(a,c,s) \ |
| 120 | t1 = mtod(md, caddr_t)+md->m_len-dpos; \ |
| 121 | if (t1 >= (s)) { \ |
| 122 | (a) = (c)(dpos); \ |
| 123 | dpos += (s); \ |
| 124 | } else if (error = nfsm_disct(&md, &dpos, (s), t1, FALSE, &cp2)) { \ |
| 125 | m_freem(mrep); \ |
| 126 | goto nfsmout; \ |
| 127 | } else { \ |
| 128 | (a) = (c)cp2; \ |
| 129 | } |
| 130 | |
| 131 | #define nfsm_fhtom(v) \ |
| 132 | nfsm_build(cp,caddr_t,NFSX_FH); \ |
| 133 | bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH) |
| 134 | |
| 135 | #define nfsm_srvfhtom(f) \ |
| 136 | nfsm_build(cp,caddr_t,NFSX_FH); \ |
| 137 | bcopy((caddr_t)(f), cp, NFSX_FH) |
| 138 | |
| 139 | #define nfsm_mtofh(d,v) \ |
| 140 | { struct nfsnode *np; nfsv2fh_t *fhp; \ |
| 141 | nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); \ |
| 142 | if (error = nfs_nget((d)->v_mount, fhp, &np)) { \ |
| 143 | m_freem(mrep); \ |
| 144 | goto nfsmout; \ |
| 145 | } \ |
| 146 | (v) = NFSTOV(np); \ |
| 147 | nfsm_loadattr(v, (struct vattr *)0); \ |
| 148 | } |
| 149 | |
| 150 | #define nfsm_loadattr(v,a) \ |
| 151 | { struct vnode *tvp = (v); \ |
| 152 | if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \ |
| 153 | m_freem(mrep); \ |
| 154 | goto nfsmout; \ |
| 155 | } \ |
| 156 | (v) = tvp; } |
| 157 | |
| 158 | #define nfsm_strsiz(s,m) \ |
| 159 | nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \ |
| 160 | if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \ |
| 161 | m_freem(mrep); \ |
| 162 | error = EBADRPC; \ |
| 163 | goto nfsmout; \ |
| 164 | } |
| 165 | |
| 166 | #define nfsm_srvstrsiz(s,m) \ |
| 167 | nfsm_disect(tl,u_long *,NFSX_UNSIGNED); \ |
| 168 | if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \ |
| 169 | error = EBADRPC; \ |
| 170 | nfsm_reply(0); \ |
| 171 | } |
| 172 | |
| 173 | #define nfsm_mtouio(p,s) \ |
| 174 | if ((s) > 0 && \ |
| 175 | (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \ |
| 176 | m_freem(mrep); \ |
| 177 | goto nfsmout; \ |
| 178 | } |
| 179 | |
| 180 | #define nfsm_uiotom(p,s) \ |
| 181 | if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \ |
| 182 | m_freem(mreq); \ |
| 183 | goto nfsmout; \ |
| 184 | } |
| 185 | |
| 186 | #define nfsm_reqhead(a,c,s) \ |
| 187 | if ((mreq = nfsm_reqh(nfs_prog,nfs_vers,(a),(c),(s),&bpos,&mb,&xid)) == NULL) { \ |
| 188 | error = ENOBUFS; \ |
| 189 | goto nfsmout; \ |
| 190 | } |
| 191 | |
| 192 | #define nfsm_reqdone m_freem(mrep); \ |
| 193 | nfsmout: |
| 194 | |
| 195 | #define nfsm_rndup(a) (((a)+3)&(~0x3)) |
| 196 | |
| 197 | #define nfsm_request(v, t, p, h) \ |
| 198 | if (error = nfs_request((v), mreq, xid, (t), (p), (h), \ |
| 199 | (v)->v_mount, &mrep, &md, &dpos)) \ |
| 200 | goto nfsmout |
| 201 | |
| 202 | #define nfsm_strtom(a,s,m) \ |
| 203 | if ((s) > (m)) { \ |
| 204 | m_freem(mreq); \ |
| 205 | error = ENAMETOOLONG; \ |
| 206 | goto nfsmout; \ |
| 207 | } \ |
| 208 | t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \ |
| 209 | if(t2<=(NFSMSIZ(mb)-mb->m_len)){ \ |
| 210 | nfsm_build(tl,u_long *,t2); \ |
| 211 | *tl++ = txdr_unsigned(s); \ |
| 212 | *(tl+((t2>>2)-2)) = 0; \ |
| 213 | bcopy((caddr_t)(a), (caddr_t)tl, (s)); \ |
| 214 | } else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \ |
| 215 | m_freem(mreq); \ |
| 216 | goto nfsmout; \ |
| 217 | } |
| 218 | |
| 219 | #define nfsm_srvdone \ |
| 220 | nfsmout: \ |
| 221 | return(error) |
| 222 | |
| 223 | #ifndef lint |
| 224 | #define nfsm_reply(s) \ |
| 225 | { \ |
| 226 | *repstat = error; \ |
| 227 | if (error) \ |
| 228 | nfs_rephead(0, xid, error, mrq, &mb, &bpos); \ |
| 229 | else \ |
| 230 | nfs_rephead((s), xid, error, mrq, &mb, &bpos); \ |
| 231 | m_freem(mrep); \ |
| 232 | mreq = *mrq; \ |
| 233 | if (error) \ |
| 234 | return(0); \ |
| 235 | } |
| 236 | #else /* lint */ |
| 237 | #define nfsm_reply(s) \ |
| 238 | { \ |
| 239 | *repstat = error; \ |
| 240 | if (error) \ |
| 241 | nfs_rephead(0, xid, error, mrq, &mb, &bpos); \ |
| 242 | else \ |
| 243 | nfs_rephead((s), xid, error, mrq, &mb, &bpos); \ |
| 244 | m_freem(mrep); \ |
| 245 | mreq = *mrq; \ |
| 246 | mrep = mreq; \ |
| 247 | if (error) \ |
| 248 | return(0); \ |
| 249 | } |
| 250 | #endif /* lint */ |
| 251 | |
| 252 | #define nfsm_adv(s) \ |
| 253 | t1 = mtod(md, caddr_t)+md->m_len-dpos; \ |
| 254 | if (t1 >= (s)) { \ |
| 255 | dpos += (s); \ |
| 256 | } else if (error = nfs_adv(&md, &dpos, (s), t1)) { \ |
| 257 | m_freem(mrep); \ |
| 258 | goto nfsmout; \ |
| 259 | } |
| 260 | |
| 261 | #define nfsm_srvmtofh(f) \ |
| 262 | nfsm_disecton(tl, u_long *, NFSX_FH); \ |
| 263 | bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH) |
| 264 | |
| 265 | #define nfsm_clget \ |
| 266 | if (bp >= be) { \ |
| 267 | MGET(mp, M_WAIT, MT_DATA); \ |
| 268 | MCLGET(mp, M_WAIT); \ |
| 269 | mp->m_len = NFSMSIZ(mp); \ |
| 270 | if (mp3 == NULL) \ |
| 271 | mp3 = mp2 = mp; \ |
| 272 | else { \ |
| 273 | mp2->m_next = mp; \ |
| 274 | mp2 = mp; \ |
| 275 | } \ |
| 276 | bp = mtod(mp, caddr_t); \ |
| 277 | be = bp+mp->m_len; \ |
| 278 | } \ |
| 279 | tl = (u_long *)bp |
| 280 | |
| 281 | #define nfsm_srvfillattr \ |
| 282 | fp->fa_type = vtonfs_type(vap->va_type); \ |
| 283 | fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \ |
| 284 | fp->fa_nlink = txdr_unsigned(vap->va_nlink); \ |
| 285 | fp->fa_uid = txdr_unsigned(vap->va_uid); \ |
| 286 | fp->fa_gid = txdr_unsigned(vap->va_gid); \ |
| 287 | fp->fa_size = txdr_unsigned(vap->va_size); \ |
| 288 | fp->fa_blocksize = txdr_unsigned(vap->va_blocksize); \ |
| 289 | if (vap->va_type == VFIFO) \ |
| 290 | fp->fa_rdev = 0xffffffff; \ |
| 291 | else \ |
| 292 | fp->fa_rdev = txdr_unsigned(vap->va_rdev); \ |
| 293 | fp->fa_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \ |
| 294 | fp->fa_fsid = txdr_unsigned(vap->va_fsid); \ |
| 295 | fp->fa_fileid = txdr_unsigned(vap->va_fileid); \ |
| 296 | fp->fa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec); \ |
| 297 | fp->fa_atime.tv_usec = txdr_unsigned(vap->va_flags); \ |
| 298 | txdr_time(&vap->va_mtime, &fp->fa_mtime); \ |
| 299 | fp->fa_ctime.tv_sec = txdr_unsigned(vap->va_ctime.tv_sec); \ |
| 300 | fp->fa_ctime.tv_usec = txdr_unsigned(vap->va_gen) |
| 301 | |