Whamcloud - gitweb
830f1878aa2b8cfab45cfd5895ae9e14e5839263
[fs/lustre-release.git] / libsysio / include / sysio-cmn.h
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2004 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 /*
45  * System IO common information.
46  */
47
48 #if !defined(__IS_UNUSED) && defined(__GNUC__)
49 #define __IS_UNUSED     __attribute__ ((unused))
50 #else
51 #define __IS_UNUSED
52 #endif
53
54 #ifndef _LARGEFILE64_SOURCE
55 /*
56  * Not glibc I guess. Define this ourselves.
57  */
58 #define _LARGEFILE64_SOURCE             0
59 #endif
60
61 /*
62  * Define internal file-offset type and it's maximum value.
63  */
64 #if _LARGEFILE64_SOURCE
65 #define _SYSIO_OFF_T                    off64_t
66 #ifdef LLONG_MAX
67 #define _SYSIO_OFF_T_MAX                (LLONG_MAX)
68 #else
69 /*
70  * Don't have LLONG_MAX before C99. We'll need to define it ourselves.
71  */
72 #define _SYSIO_OFF_T_MAX                (9223372036854775807LL)
73 #endif
74 #else
75 #define _SYSIO_OFF_T                    off_t
76 #define _SYSIO_OFF_T_MAX                LONG_MAX
77 #endif
78
79 /*
80  * Internally, all file status is carried in the 64-bit capable
81  * structure.
82  */
83 #if _LARGEFILE64_SOURCE
84 #define intnl_xtvec xtvec64
85 #else
86 #define intnl_xtvec xtvec
87 #endif
88 struct intnl_xtvec;
89
90 struct iovec;
91
92 /*
93  * SYSIO name label macros
94  */
95 #define XPREPEND(p,x) p ## x
96 #define PREPEND(p,x) XPREPEND(p,x)
97 #define SYSIO_LABEL_NAMES 0
98 #if SYSIO_LABEL_NAMES
99 #define SYSIO_INTERFACE_NAME(x) PREPEND(sysio__, x)
100 #else
101 #define SYSIO_INTERFACE_NAME(x) x
102 #endif
103
104 /* for debugging */
105 #if 0
106 #define ASSERT(cond)                                                    \
107         if (!(cond)) {                                                  \
108                 printf("ASSERTION(" #cond ") failed: " __FILE__ ":"     \
109                         __FUNCTION__ ":%d\n", __LINE__);                \
110                 abort();                                                \
111         }
112
113 #define ERROR(fmt, a...)                                                \
114         do {                                                            \
115                 printf("ERROR(" __FILE__ ":%d):" fmt, __LINE__, ##a);   \
116         while(0)
117
118 #else
119 #define ERROR(fmt)      do{}while(0)
120 #define ASSERT          do{}while(0)
121 #endif
122
123 /*
124  * SYSIO interface frame macros
125  *
126  * + DISPLAY_BLOCK; Allocates storage on the stack for use by the set of
127  *      macros.
128  * + ENTER; Performs entry point work
129  * + RETURN; Returns a value and performs exit point work
130  *
131  * NB: For RETURN, the arguments are the return value and value for errno.
132  * If the value for errno is non-zero then that value, *negated*, is set
133  * into errno.
134  */
135 #define SYSIO_INTERFACE_DISPLAY_BLOCK \
136         int _saved_errno;
137 #define SYSIO_INTERFACE_ENTER \
138         do { \
139                 _saved_errno = errno; \
140                 SYSIO_ENTER; \
141         } while (0)
142 #define SYSIO_INTERFACE_RETURN(rtn, err) \
143         do { \
144                 SYSIO_LEAVE; \
145                 errno = (err) ? -(err) : _saved_errno; \
146                 return (rtn); \
147         } while(0) 
148
149 /* Interface enter/leave hook functions  */
150 #if 0
151 extern void _sysio_sysenter();
152 extern void _sysio_sysleave();
153
154 #define SYSIO_ENTER                                                     \
155         do {                                                            \
156                 _sysio_sysenter();                                      \
157         } while(0)
158
159 #define SYSIO_LEAVE                                                     \
160         do {                                                            \
161                 _sysio_sysleave();                                      \
162         } while(0)
163 #else
164 #define SYSIO_ENTER
165 #define SYSIO_LEAVE
166
167 #endif
168
169 /* accounting for IO stats read and write char count */
170 #if defined(REDSTORM)
171 #define _SYSIO_UPDACCT(w, cc) \
172         do { \
173                 if ((cc) < 0) \
174                         break; \
175                 if (!w) \
176                         _add_iostats(0, (size_t )(cc)); \
177                 else \
178                         _add_iostats((size_t )(cc), 0); \
179         } while(0)
180 #else
181 #define _SYSIO_UPDACCT(w, cc)
182 #endif
183
184 extern ssize_t _sysio_validx(const struct intnl_xtvec *xtv, size_t xtvlen,
185                              const struct iovec *iov, size_t iovlen,
186                              _SYSIO_OFF_T limit);
187 extern ssize_t _sysio_enumerate_extents(const struct intnl_xtvec *xtv,
188                                         size_t xtvlen,
189                                         const struct iovec *iov,
190                                         size_t iovlen,
191                                         ssize_t (*f)(const struct iovec *,
192                                                      int,
193                                                      _SYSIO_OFF_T,
194                                                      ssize_t,
195                                                      void *),
196                                         void *arg);
197 extern ssize_t _sysio_enumerate_iovec(const struct iovec *iov,
198                                       size_t count,
199                                       _SYSIO_OFF_T off,
200                                       ssize_t limit,
201                                       ssize_t (*f)(void *,
202                                                    size_t,
203                                                    _SYSIO_OFF_T,
204                                                    void *),
205                                       void *arg);
206 extern ssize_t _sysio_doio(const struct intnl_xtvec *xtv, size_t xtvlen,
207                            const struct iovec *iov, size_t iovlen,
208                            ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, void *),
209                            void *arg);