Whamcloud - gitweb
import libsysio for b_newsysio
[fs/lustre-release.git] / libsysio / include / sysio.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 #include <limits.h>
49 #include <stdarg.h>
50
51 #if !defined(__IS_UNUSED) && defined(__GNUC__)
52 #define __IS_UNUSED     __attribute__ ((unused))
53 #else
54 #define __IS_UNUSED
55 #endif
56
57 #ifndef PATH_SEPARATOR
58 /*
59  * Path separator.
60  */
61 #define PATH_SEPARATOR                  '/'
62 #endif
63
64 #ifndef MAX_SYMLINK
65 /*
66  * Max recursion depth allowed when resoving symbolic links.
67  */
68 #define MAX_SYMLINK                     250
69 #endif
70
71 #ifndef _LARGEFILE64_SOURCE
72 /*
73  * Not glibc I guess. Define this ourselves.
74  */
75 #define _LARGEFILE64_SOURCE             0
76 #endif
77
78 /*
79  * Define internal file-offset type and it's maximum value.
80  */
81 #if _LARGEFILE64_SOURCE
82 #define _SYSIO_OFF_T                    off64_t
83 #ifdef LLONG_MAX
84 #define _SYSIO_OFF_T_MAX                (LLONG_MAX)
85 #else
86 /*
87  * Don't have LLONG_MAX before C99. We'll need to define it ourselves.
88  */
89 #define _SYSIO_OFF_T_MAX                (9223372036854775807LL)
90 #endif
91 #else
92 #define _SYSIO_OFF_T                    off_t
93 #define _SYSIO_OFF_T_MAX                LONG_MAX
94 #endif
95
96 /*
97  * Internally, all directory entries are carried in the 64-bit capable
98  * structure.
99  */
100 #if _LARGEFILE64_SOURCE
101 #define intnl_dirent dirent64
102 #else
103 #define intnl_dirent dirent
104 #endif
105 struct dirent;
106
107 /*
108  * Internally, all file status is carried in the 64-bit capable
109  * structure.
110  */
111 #if _LARGEFILE64_SOURCE
112 #define intnl_stat stat64
113 #else
114 #define intnl_stat stat
115 #endif
116 struct stat;
117
118 #ifdef _HAVE_STATVFS
119 #if _LARGEFILE64_SOURCE
120 #define intnl_statvfs statvfs64
121 #else
122 #define intnl_statvfs statvfs
123 #define INTNL_STATVFS_IS_NATURAL        1
124 #endif
125 struct statvfs;
126 struct intnl_statvfs;
127 #endif
128
129 /*
130  * Internally, all file status is carried in the 64-bit capable
131  * structure.
132  */
133 #if _LARGEFILE64_SOURCE
134 #define intnl_xtvec xtvec64
135 #else
136 #define intnl_xtvec xtvec
137 #endif
138 struct intnl_xtvec;
139
140 struct iovec;
141
142 struct utimbuf;
143
144 struct intnl_stat;
145
146 struct pnode;
147
148 extern struct pnode *_sysio_cwd;
149
150 extern mode_t _sysio_umask;
151
152 extern int _sysio_init(void);
153 extern void _sysio_shutdown(void);
154 extern int _sysio_boot(const char *buf);
155
156 /*
157  * SYSIO name label macros
158  */
159 #define XPREPEND(p,x) p ## x
160 #define PREPEND(p,x) XPREPEND(p,x)
161 #define SYSIO_LABEL_NAMES 0
162 #if SYSIO_LABEL_NAMES
163 #define SYSIO_INTERFACE_NAME(x) PREPEND(sysio__, x)
164 #else
165 #define SYSIO_INTERFACE_NAME(x) x
166 #endif
167
168 /*
169  * The following should be defined by the system includes, and probably are,
170  * but it's not illegal to have multiple externs, so long as they are the
171  * same. It helps when building the library in a standalone fashion.
172  */
173 extern int SYSIO_INTERFACE_NAME(access)(const char *path, int amode);
174 extern int SYSIO_INTERFACE_NAME(chdir)(const char *path);
175 extern int SYSIO_INTERFACE_NAME(chmod)(const char *path, mode_t mode);
176 extern int SYSIO_INTERFACE_NAME(fchmod)(int fd, mode_t mode);
177 extern int SYSIO_INTERFACE_NAME(chown)(const char *path, uid_t owner,
178                                        gid_t group);
179 extern int SYSIO_INTERFACE_NAME(fchown)(int fd, uid_t owner, gid_t group);
180 extern int SYSIO_INTERFACE_NAME(close)(int d);
181 extern int SYSIO_INTERFACE_NAME(dup)(int oldfd);
182 extern int SYSIO_INTERFACE_NAME(dup2)(int oldfd, int newfd);
183 extern int SYSIO_INTERFACE_NAME(fcntl)(int fd, int cmd, ...);
184 extern int SYSIO_INTERFACE_NAME(fstat)(int fd, struct stat *buf);
185 extern int SYSIO_INTERFACE_NAME(fsync)(int fd);
186 extern char *SYSIO_INTERFACE_NAME(getcwd)(char *buf, size_t size);
187 extern off_t SYSIO_INTERFACE_NAME(lseek)(int fd, off_t offset, int whence);
188 #if _LARGEFILE64_SOURCE
189 extern off64_t SYSIO_INTERFACE_NAME(lseek64)(int fd, off64_t offset, 
190                                              int whence);
191 #endif
192 extern int SYSIO_INTERFACE_NAME(lstat)(const char *path, struct stat *buf);
193 #ifdef BSD
194 extern int SYSIO_INTERFACE_NAME(getdirentries)(int fd, char *buf, int nbytes , 
195                                                long *basep);
196 #else
197 extern ssize_t SYSIO_INTERFACE_NAME(getdirentries)(int fd, char *buf, 
198                                                    size_t nbytes, off_t *basep);
199 #if _LARGEFILE64_SOURCE
200 extern ssize_t SYSIO_INTERFACE_NAME(getdirentries64)(int fd,
201                                                      char *buf,
202                                                      size_t nbytes,
203                                                      off64_t *basep);
204 #endif
205 #endif
206 extern int SYSIO_INTERFACE_NAME(mkdir)(const char *path, mode_t mode);
207 extern int SYSIO_INTERFACE_NAME(open)(const char *path, int flag, ...);
208 #if _LARGEFILE64_SOURCE
209 extern int SYSIO_INTERFACE_NAME(open64)(const char *path, int flag, ...);
210 #endif
211 extern int SYSIO_INTERFACE_NAME(creat)(const char *path, mode_t mode);
212 #if _LARGEFILE64_SOURCE
213 extern int SYSIO_INTERFACE_NAME(creat64)(const char *path, mode_t mode);
214 #endif
215 extern int SYSIO_INTERFACE_NAME(stat)(const char *path, struct stat *buf);
216 #if _LARGEFILE64_SOURCE
217 extern int SYSIO_INTERFACE_NAME(stat64)(const char *path, struct stat64 *buf);
218 #endif
219 #ifdef _HAVE_STATVFS
220 extern int SYSIO_INTERFACE_NAME(statvfs)(const char *path, struct statvfs *buf);
221 #if _LARGEFILE64_SOURCE
222 extern int SYSIO_INTERFACE_NAME(statvfs64)(const char *path, 
223                                 struct statvfs64 *buf);
224 #endif
225 extern int SYSIO_INTERFACE_NAME(fstatvfs)(int fd, struct statvfs *buf);
226 #if _LARGEFILE64_SOURCE
227 extern int SYSIO_INTERFACE_NAME(fstatvfs64)(int fd, struct statvfs64 *buf);
228 #endif
229 #endif
230 extern int SYSIO_INTERFACE_NAME(truncate)(const char *path, off_t length);
231 #if _LARGEFILE64_SOURCE
232 extern int SYSIO_INTERFACE_NAME(truncate64)(const char *path, off64_t length);
233 #endif
234 extern int SYSIO_INTERFACE_NAME(ftruncate)(int fd, off_t length);
235 #if _LARGEFILE64_SOURCE
236 extern int SYSIO_INTERFACE_NAME(ftruncate64)(int fd, off64_t length);
237 #endif
238 extern int SYSIO_INTERFACE_NAME(rmdir)(const char *path);
239 extern int SYSIO_INTERFACE_NAME(symlink)(const char *path1, const char *path2);
240 extern int SYSIO_INTERFACE_NAME(readlink)(const char *path,
241                                 char *buf,
242                                 size_t bufsiz);
243 extern int SYSIO_INTERFACE_NAME(link)(const char *oldpath, const char *newpath);
244 extern int SYSIO_INTERFACE_NAME(unlink)(const char *path);
245 extern int SYSIO_INTERFACE_NAME(rename)(const char *oldpath, 
246                                         const char *newpath);
247 extern int SYSIO_INTERFACE_NAME(fdatasync)(int fd);
248 extern int SYSIO_INTERFACE_NAME(ioctl)(int fd, unsigned long request, ...);
249 extern mode_t SYSIO_INTERFACE_NAME(umask)(mode_t mask);
250 extern int SYSIO_INTERFACE_NAME(mknod)(const char *path, 
251                                        mode_t mode, dev_t dev);
252 extern int SYSIO_INTERFACE_NAME(utime)(const char *path, 
253                                        const struct utimbuf *buf);
254 extern int SYSIO_INTERFACE_NAME(mount)(const char *source, const char *target,
255                                        const char *filesystemtype,
256                                        unsigned long mountflags,
257                                        const void *data);
258 extern int SYSIO_INTERFACE_NAME(umount)(const char *target);
259
260 /* for debugging */
261 #if 0
262 #define ASSERT(cond)                                                    \
263         if (!(cond)) {                                                  \
264                 printf("ASSERTION(" #cond ") failed: " __FILE__ ":"     \
265                         __FUNCTION__ ":%d\n", __LINE__);                \
266                 abort();                                                \
267         }
268
269 #define ERROR(fmt, a...)                                                \
270         do {                                                            \
271                 printf("ERROR(" __FILE__ ":%d):" fmt, __LINE__, ##a);   \
272         while(0)
273
274 #else
275 #define ERROR(fmt)      do{}while(0)
276 #define ASSERT          do{}while(0)
277 #endif
278
279 /*
280  * SYSIO interface frame macros
281  *
282  * + DISPLAY_BLOCK; Allocates storage on the stack for use by the set of
283  *      macros.
284  * + ENTER; Performs entry point work
285  * + RETURN; Returns a value and performs exit point work
286  *
287  * NB: For RETURN, the arguments are the return value and value for errno.
288  * If the value for errno is non-zero then that value, *negated*, is set
289  * into errno.
290  */
291 #define SYSIO_INTERFACE_DISPLAY_BLOCK \
292         int _saved_errno;
293 #define SYSIO_INTERFACE_ENTER \
294         do { \
295                 _saved_errno = errno; \
296                 SYSIO_ENTER; \
297         } while (0)
298 #define SYSIO_INTERFACE_RETURN(rtn, err) \
299         do { \
300                 SYSIO_LEAVE; \
301                 errno = (err) ? -(err) : _saved_errno; \
302                 return (rtn); \
303         } while(0) 
304
305 /* syscall enter/leave hook functions  */
306 #if 0
307 extern void _sysio_sysenter();
308 extern void _sysio_sysleave();
309
310 #define SYSIO_ENTER                                                     \
311         do {                                                            \
312                 _sysio_sysenter();                                      \
313         } while(0)
314
315 #define SYSIO_LEAVE                                                     \
316         do {                                                            \
317                 _sysio_sysleave();                                      \
318         } while(0)
319 #else
320 #define SYSIO_ENTER
321 #define SYSIO_LEAVE
322
323 #endif
324
325 /* accounting for IO stats read and write char count */
326 #if defined(REDSTORM)
327 #define _SYSIO_UPDACCT(w, cc) \
328         do { \
329                 if ((cc) < 0) \
330                         break; \
331                 if (!w) \
332                         _add_iostats(0, (size_t )(cc)); \
333                 else \
334                         _add_iostats((size_t )(cc), 0); \
335         } while(0)
336 #else
337 #define _SYSIO_UPDACCT(w, cc)
338 #endif