nfsm_subs.h revision 3305
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Rick Macklem at The University of Guelph. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 3. All advertising materials mentioning features or use of this software 171541Srgrimes * must display the following acknowledgement: 181541Srgrimes * This product includes software developed by the University of 191541Srgrimes * California, Berkeley and its contributors. 201541Srgrimes * 4. Neither the name of the University nor the names of its contributors 211541Srgrimes * may be used to endorse or promote products derived from this software 221541Srgrimes * without specific prior written permission. 231541Srgrimes * 241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341541Srgrimes * SUCH DAMAGE. 351541Srgrimes * 361541Srgrimes * @(#)nfsm_subs.h 8.1 (Berkeley) 6/16/93 373305Sphk * $Id: nfsm_subs.h,v 1.3 1994/08/21 06:50:10 paul Exp $ 381541Srgrimes */ 391541Srgrimes 402175Spaul#ifndef _NFS_NFSM_SUBS_H_ 412175Spaul#define _NFS_NFSM_SUBS_H_ 422175Spaul 431541Srgrimes/* 441541Srgrimes * These macros do strange and peculiar things to mbuf chains for 451541Srgrimes * the assistance of the nfs code. To attempt to use them for any 461541Srgrimes * other purpose will be dangerous. (they make weird assumptions) 471541Srgrimes */ 481541Srgrimes 491541Srgrimes/* 501541Srgrimes * First define what the actual subs. return 511541Srgrimes */ 521541Srgrimesextern struct mbuf *nfsm_reqh(); 531541Srgrimes 541541Srgrimes#define M_HASCL(m) ((m)->m_flags & M_EXT) 551541Srgrimes#define NFSMINOFF(m) \ 561541Srgrimes if (M_HASCL(m)) \ 571541Srgrimes (m)->m_data = (m)->m_ext.ext_buf; \ 581541Srgrimes else if ((m)->m_flags & M_PKTHDR) \ 591541Srgrimes (m)->m_data = (m)->m_pktdat; \ 601541Srgrimes else \ 611541Srgrimes (m)->m_data = (m)->m_dat 621541Srgrimes#define NFSMADV(m, s) (m)->m_data += (s) 631541Srgrimes#define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \ 641541Srgrimes (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN)) 651541Srgrimes 661541Srgrimes/* 671541Srgrimes * Now for the macros that do the simple stuff and call the functions 681541Srgrimes * for the hard stuff. 691541Srgrimes * These macros use several vars. declared in nfsm_reqhead and these 701541Srgrimes * vars. must not be used elsewhere unless you are careful not to corrupt 711541Srgrimes * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries 721541Srgrimes * that may be used so long as the value is not expected to retained 731541Srgrimes * after a macro. 741541Srgrimes * I know, this is kind of dorkey, but it makes the actual op functions 751541Srgrimes * fairly clean and deals with the mess caused by the xdr discriminating 761541Srgrimes * unions. 771541Srgrimes */ 781541Srgrimes 791541Srgrimes#define nfsm_build(a,c,s) \ 801541Srgrimes { if ((s) > M_TRAILINGSPACE(mb)) { \ 811541Srgrimes MGET(mb2, M_WAIT, MT_DATA); \ 821541Srgrimes if ((s) > MLEN) \ 831541Srgrimes panic("build > MLEN"); \ 841541Srgrimes mb->m_next = mb2; \ 851541Srgrimes mb = mb2; \ 861541Srgrimes mb->m_len = 0; \ 871541Srgrimes bpos = mtod(mb, caddr_t); \ 881541Srgrimes } \ 891541Srgrimes (a) = (c)(bpos); \ 901541Srgrimes mb->m_len += (s); \ 911541Srgrimes bpos += (s); } 921541Srgrimes 931541Srgrimes#define nfsm_dissect(a,c,s) \ 941541Srgrimes { t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 951541Srgrimes if (t1 >= (s)) { \ 961541Srgrimes (a) = (c)(dpos); \ 971541Srgrimes dpos += (s); \ 981541Srgrimes } else { \ 993305Sphk error = nfsm_disct(&md, &dpos, (s), t1, &cp2); \ 1003305Sphk if (error) { \ 1013305Sphk m_freem(mrep); \ 1023305Sphk goto nfsmout; \ 1033305Sphk } else { \ 1043305Sphk (a) = (c)cp2; \ 1053305Sphk } \ 1061541Srgrimes } } 1071541Srgrimes 1081541Srgrimes#define nfsm_fhtom(v) \ 1091541Srgrimes nfsm_build(cp,caddr_t,NFSX_FH); \ 1101541Srgrimes bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH) 1111541Srgrimes 1121541Srgrimes#define nfsm_srvfhtom(f) \ 1131541Srgrimes nfsm_build(cp,caddr_t,NFSX_FH); \ 1141541Srgrimes bcopy((caddr_t)(f), cp, NFSX_FH) 1151541Srgrimes 1161541Srgrimes#define nfsm_mtofh(d,v) \ 1171541Srgrimes { struct nfsnode *np; nfsv2fh_t *fhp; \ 1181541Srgrimes nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \ 1193305Sphk error = nfs_nget((d)->v_mount, fhp, &np); \ 1203305Sphk if (error) { \ 1211541Srgrimes m_freem(mrep); \ 1221541Srgrimes goto nfsmout; \ 1231541Srgrimes } \ 1241541Srgrimes (v) = NFSTOV(np); \ 1251541Srgrimes nfsm_loadattr(v, (struct vattr *)0); \ 1261541Srgrimes } 1271541Srgrimes 1281541Srgrimes#define nfsm_loadattr(v,a) \ 1291541Srgrimes { struct vnode *tvp = (v); \ 1303305Sphk error = nfs_loadattrcache(&tvp, &md, &dpos, (a)); \ 1313305Sphk if (error) { \ 1321541Srgrimes m_freem(mrep); \ 1331541Srgrimes goto nfsmout; \ 1341541Srgrimes } \ 1351541Srgrimes (v) = tvp; } 1361541Srgrimes 1371541Srgrimes#define nfsm_strsiz(s,m) \ 1381541Srgrimes { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \ 1391541Srgrimes if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \ 1401541Srgrimes m_freem(mrep); \ 1411541Srgrimes error = EBADRPC; \ 1421541Srgrimes goto nfsmout; \ 1431541Srgrimes } } 1441541Srgrimes 1451541Srgrimes#define nfsm_srvstrsiz(s,m) \ 1461541Srgrimes { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \ 1471541Srgrimes if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \ 1481541Srgrimes error = EBADRPC; \ 1491541Srgrimes nfsm_reply(0); \ 1501541Srgrimes } } 1511541Srgrimes 1521541Srgrimes#define nfsm_mtouio(p,s) \ 1531541Srgrimes if ((s) > 0 && \ 1541541Srgrimes (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \ 1551541Srgrimes m_freem(mrep); \ 1561541Srgrimes goto nfsmout; \ 1571541Srgrimes } 1581541Srgrimes 1591541Srgrimes#define nfsm_uiotom(p,s) \ 1603305Sphk error = nfsm_uiotombuf((p),&mb,(s),&bpos); \ 1613305Sphk if (error) { \ 1621541Srgrimes m_freem(mreq); \ 1631541Srgrimes goto nfsmout; \ 1641541Srgrimes } 1651541Srgrimes 1661541Srgrimes#define nfsm_reqhead(v,a,s) \ 1671541Srgrimes mb = mreq = nfsm_reqh((v),(a),(s),&bpos) 1681541Srgrimes 1691541Srgrimes#define nfsm_reqdone m_freem(mrep); \ 1701541Srgrimes nfsmout: 1711541Srgrimes 1721541Srgrimes#define nfsm_rndup(a) (((a)+3)&(~0x3)) 1731541Srgrimes 1741541Srgrimes#define nfsm_request(v, t, p, c) \ 1753305Sphk error = nfs_request((v), mreq, (t), (p), \ 1763305Sphk (c), &mrep, &md, &dpos); \ 1773305Sphk if (error) \ 1781541Srgrimes goto nfsmout 1791541Srgrimes 1801541Srgrimes#define nfsm_strtom(a,s,m) \ 1811541Srgrimes if ((s) > (m)) { \ 1821541Srgrimes m_freem(mreq); \ 1831541Srgrimes error = ENAMETOOLONG; \ 1841541Srgrimes goto nfsmout; \ 1851541Srgrimes } \ 1861541Srgrimes t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \ 1871541Srgrimes if (t2 <= M_TRAILINGSPACE(mb)) { \ 1881541Srgrimes nfsm_build(tl,u_long *,t2); \ 1891541Srgrimes *tl++ = txdr_unsigned(s); \ 1901541Srgrimes *(tl+((t2>>2)-2)) = 0; \ 1911541Srgrimes bcopy((caddr_t)(a), (caddr_t)tl, (s)); \ 1923305Sphk } else { \ 1933305Sphk error = nfsm_strtmbuf(&mb, &bpos, (a), (s)); \ 1943305Sphk if (error) { \ 1953305Sphk m_freem(mreq); \ 1963305Sphk goto nfsmout; \ 1973305Sphk } \ 1981541Srgrimes } 1991541Srgrimes 2001541Srgrimes#define nfsm_srvdone \ 2011541Srgrimes nfsmout: \ 2021541Srgrimes return(error) 2031541Srgrimes 2041541Srgrimes#define nfsm_reply(s) \ 2051541Srgrimes { \ 2061541Srgrimes nfsd->nd_repstat = error; \ 2071541Srgrimes if (error) \ 2081541Srgrimes (void) nfs_rephead(0, nfsd, error, cache, &frev, \ 2091541Srgrimes mrq, &mb, &bpos); \ 2101541Srgrimes else \ 2111541Srgrimes (void) nfs_rephead((s), nfsd, error, cache, &frev, \ 2121541Srgrimes mrq, &mb, &bpos); \ 2131541Srgrimes m_freem(mrep); \ 2141541Srgrimes mreq = *mrq; \ 2151541Srgrimes if (error) \ 2161541Srgrimes return(0); \ 2171541Srgrimes } 2181541Srgrimes 2191541Srgrimes#define nfsm_adv(s) \ 2201541Srgrimes t1 = mtod(md, caddr_t)+md->m_len-dpos; \ 2211541Srgrimes if (t1 >= (s)) { \ 2221541Srgrimes dpos += (s); \ 2233305Sphk } else { \ 2243305Sphk error = nfs_adv(&md, &dpos, (s), t1); \ 2253305Sphk if (error) { \ 2263305Sphk m_freem(mrep); \ 2273305Sphk goto nfsmout; \ 2283305Sphk } \ 2291541Srgrimes } 2301541Srgrimes 2311541Srgrimes#define nfsm_srvmtofh(f) \ 2321541Srgrimes nfsm_dissect(tl, u_long *, NFSX_FH); \ 2331541Srgrimes bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH) 2341541Srgrimes 2351541Srgrimes#define nfsm_clget \ 2361541Srgrimes if (bp >= be) { \ 2371541Srgrimes if (mp == mb) \ 2381541Srgrimes mp->m_len += bp-bpos; \ 2391541Srgrimes MGET(mp, M_WAIT, MT_DATA); \ 2401541Srgrimes MCLGET(mp, M_WAIT); \ 2411541Srgrimes mp->m_len = NFSMSIZ(mp); \ 2421541Srgrimes mp2->m_next = mp; \ 2431541Srgrimes mp2 = mp; \ 2441541Srgrimes bp = mtod(mp, caddr_t); \ 2451541Srgrimes be = bp+mp->m_len; \ 2461541Srgrimes } \ 2471541Srgrimes tl = (u_long *)bp 2481541Srgrimes 2491541Srgrimes#define nfsm_srvfillattr \ 2501541Srgrimes fp->fa_type = vtonfs_type(vap->va_type); \ 2511541Srgrimes fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \ 2521541Srgrimes fp->fa_nlink = txdr_unsigned(vap->va_nlink); \ 2531541Srgrimes fp->fa_uid = txdr_unsigned(vap->va_uid); \ 2541541Srgrimes fp->fa_gid = txdr_unsigned(vap->va_gid); \ 2551541Srgrimes if (nfsd->nd_nqlflag == NQL_NOVAL) { \ 2561541Srgrimes fp->fa_nfsblocksize = txdr_unsigned(vap->va_blocksize); \ 2571541Srgrimes if (vap->va_type == VFIFO) \ 2581541Srgrimes fp->fa_nfsrdev = 0xffffffff; \ 2591541Srgrimes else \ 2601541Srgrimes fp->fa_nfsrdev = txdr_unsigned(vap->va_rdev); \ 2611541Srgrimes fp->fa_nfsfsid = txdr_unsigned(vap->va_fsid); \ 2621541Srgrimes fp->fa_nfsfileid = txdr_unsigned(vap->va_fileid); \ 2631541Srgrimes fp->fa_nfssize = txdr_unsigned(vap->va_size); \ 2641541Srgrimes fp->fa_nfsblocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \ 2651541Srgrimes txdr_nfstime(&vap->va_atime, &fp->fa_nfsatime); \ 2661541Srgrimes txdr_nfstime(&vap->va_mtime, &fp->fa_nfsmtime); \ 2671541Srgrimes fp->fa_nfsctime.nfs_sec = txdr_unsigned(vap->va_ctime.ts_sec); \ 2681541Srgrimes fp->fa_nfsctime.nfs_usec = txdr_unsigned(vap->va_gen); \ 2691541Srgrimes } else { \ 2701541Srgrimes fp->fa_nqblocksize = txdr_unsigned(vap->va_blocksize); \ 2711541Srgrimes if (vap->va_type == VFIFO) \ 2721541Srgrimes fp->fa_nqrdev = 0xffffffff; \ 2731541Srgrimes else \ 2741541Srgrimes fp->fa_nqrdev = txdr_unsigned(vap->va_rdev); \ 2751541Srgrimes fp->fa_nqfsid = txdr_unsigned(vap->va_fsid); \ 2761541Srgrimes fp->fa_nqfileid = txdr_unsigned(vap->va_fileid); \ 2771541Srgrimes txdr_hyper(&vap->va_size, &fp->fa_nqsize); \ 2781541Srgrimes txdr_hyper(&vap->va_bytes, &fp->fa_nqbytes); \ 2791541Srgrimes txdr_nqtime(&vap->va_atime, &fp->fa_nqatime); \ 2801541Srgrimes txdr_nqtime(&vap->va_mtime, &fp->fa_nqmtime); \ 2811541Srgrimes txdr_nqtime(&vap->va_ctime, &fp->fa_nqctime); \ 2821541Srgrimes fp->fa_nqflags = txdr_unsigned(vap->va_flags); \ 2831541Srgrimes fp->fa_nqgen = txdr_unsigned(vap->va_gen); \ 2841541Srgrimes txdr_hyper(&vap->va_filerev, &fp->fa_nqfilerev); \ 2851541Srgrimes } 2861541Srgrimes 2872175Spaul#endif 288