nfsm_subs.h revision 1.1 1 1.1 dholland /* $NetBSD: nfsm_subs.h,v 1.1 2013/09/30 07:19:40 dholland Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 1989, 1993
4 1.1 dholland * The Regents of the University of California. All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to Berkeley by
7 1.1 dholland * Rick Macklem at The University of Guelph.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors
18 1.1 dholland * may be used to endorse or promote products derived from this software
19 1.1 dholland * without specific prior written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 dholland * SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland * FreeBSD: head/sys/fs/nfs/nfsm_subs.h 249592 2013-04-17 21:00:22Z ken
34 1.1 dholland * $NetBSD: nfsm_subs.h,v 1.1 2013/09/30 07:19:40 dholland Exp $
35 1.1 dholland */
36 1.1 dholland
37 1.1 dholland #ifndef _NFS_NFSM_SUBS_H_
38 1.1 dholland #define _NFS_NFSM_SUBS_H_
39 1.1 dholland
40 1.1 dholland
41 1.1 dholland /*
42 1.1 dholland * These macros do strange and peculiar things to mbuf chains for
43 1.1 dholland * the assistance of the nfs code. To attempt to use them for any
44 1.1 dholland * other purpose will be dangerous. (they make weird assumptions)
45 1.1 dholland */
46 1.1 dholland
47 1.1 dholland #ifndef APPLE
48 1.1 dholland /*
49 1.1 dholland * First define what the actual subs. return
50 1.1 dholland */
51 1.1 dholland #define M_HASCL(m) ((m)->m_flags & M_EXT)
52 1.1 dholland #define NFSMINOFF(m) \
53 1.1 dholland if (M_HASCL(m)) \
54 1.1 dholland (m)->m_data = (m)->m_ext.ext_buf; \
55 1.1 dholland else if ((m)->m_flags & M_PKTHDR) \
56 1.1 dholland (m)->m_data = (m)->m_pktdat; \
57 1.1 dholland else \
58 1.1 dholland (m)->m_data = (m)->m_dat
59 1.1 dholland #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
60 1.1 dholland (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
61 1.1 dholland #define NFSM_DATAP(m, s) (m)->m_data += (s)
62 1.1 dholland
63 1.1 dholland /*
64 1.1 dholland * Now for the macros that do the simple stuff and call the functions
65 1.1 dholland * for the hard stuff.
66 1.1 dholland * They use fields in struct nfsrv_descript to handle the mbuf queues.
67 1.1 dholland * Replace most of the macro with an inline function, to minimize
68 1.1 dholland * the machine code. The inline functions in lower case can be called
69 1.1 dholland * directly, bypassing the macro.
70 1.1 dholland */
71 1.1 dholland static __inline void *
72 1.1 dholland nfsm_build(struct nfsrv_descript *nd, int siz)
73 1.1 dholland {
74 1.1 dholland void *retp;
75 1.1 dholland struct mbuf *mb2;
76 1.1 dholland
77 1.1 dholland if (siz > M_TRAILINGSPACE(nd->nd_mb)) {
78 1.1 dholland NFSMCLGET(mb2, M_NOWAIT);
79 1.1 dholland if (siz > MLEN)
80 1.1 dholland panic("build > MLEN");
81 1.1 dholland mbuf_setlen(mb2, 0);
82 1.1 dholland nd->nd_bpos = NFSMTOD(mb2, caddr_t);
83 1.1 dholland nd->nd_mb->m_next = mb2;
84 1.1 dholland nd->nd_mb = mb2;
85 1.1 dholland }
86 1.1 dholland retp = (void *)(nd->nd_bpos);
87 1.1 dholland nd->nd_mb->m_len += siz;
88 1.1 dholland nd->nd_bpos += siz;
89 1.1 dholland return (retp);
90 1.1 dholland }
91 1.1 dholland
92 1.1 dholland #define NFSM_BUILD(a, c, s) ((a) = (c)nfsm_build(nd, (s)))
93 1.1 dholland
94 1.1 dholland static __inline void *
95 1.1 dholland nfsm_dissect(struct nfsrv_descript *nd, int siz)
96 1.1 dholland {
97 1.1 dholland int tt1;
98 1.1 dholland void *retp;
99 1.1 dholland
100 1.1 dholland tt1 = NFSMTOD(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
101 1.1 dholland if (tt1 >= siz) {
102 1.1 dholland retp = (void *)nd->nd_dpos;
103 1.1 dholland nd->nd_dpos += siz;
104 1.1 dholland } else {
105 1.1 dholland retp = nfsm_dissct(nd, siz, M_WAITOK);
106 1.1 dholland }
107 1.1 dholland return (retp);
108 1.1 dholland }
109 1.1 dholland
110 1.1 dholland static __inline void *
111 1.1 dholland nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
112 1.1 dholland {
113 1.1 dholland int tt1;
114 1.1 dholland void *retp;
115 1.1 dholland
116 1.1 dholland tt1 = NFSMTOD(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
117 1.1 dholland if (tt1 >= siz) {
118 1.1 dholland retp = (void *)nd->nd_dpos;
119 1.1 dholland nd->nd_dpos += siz;
120 1.1 dholland } else {
121 1.1 dholland retp = nfsm_dissct(nd, siz, M_NOWAIT);
122 1.1 dholland }
123 1.1 dholland return (retp);
124 1.1 dholland }
125 1.1 dholland
126 1.1 dholland #define NFSM_DISSECT(a, c, s) \
127 1.1 dholland do { \
128 1.1 dholland (a) = (c)nfsm_dissect(nd, (s)); \
129 1.1 dholland if ((a) == NULL) { \
130 1.1 dholland error = EBADRPC; \
131 1.1 dholland goto nfsmout; \
132 1.1 dholland } \
133 1.1 dholland } while (0)
134 1.1 dholland
135 1.1 dholland #define NFSM_DISSECT_NONBLOCK(a, c, s) \
136 1.1 dholland do { \
137 1.1 dholland (a) = (c)nfsm_dissect_nonblock(nd, (s)); \
138 1.1 dholland if ((a) == NULL) { \
139 1.1 dholland error = EBADRPC; \
140 1.1 dholland goto nfsmout; \
141 1.1 dholland } \
142 1.1 dholland } while (0)
143 1.1 dholland #endif /* !APPLE */
144 1.1 dholland
145 1.1 dholland #define NFSM_STRSIZ(s, m) \
146 1.1 dholland do { \
147 1.1 dholland tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED); \
148 1.1 dholland if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
149 1.1 dholland error = EBADRPC; \
150 1.1 dholland goto nfsmout; \
151 1.1 dholland } \
152 1.1 dholland } while (0)
153 1.1 dholland
154 1.1 dholland #define NFSM_RNDUP(a) (((a)+3)&(~0x3))
155 1.1 dholland
156 1.1 dholland #endif /* _NFS_NFSM_SUBS_H_ */
157