Whamcloud - gitweb
36e0979a402ee4d568ab525482f4c2d8e29a9ba4
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __LIBCFS_LIBCFS_H__
38 #define __LIBCFS_LIBCFS_H__
39
40 #if !__GNUC__
41 #define __attribute__(x)
42 #endif
43
44 #if !defined(__WINNT__) && !defined(__KERNEL__)
45 #include <libcfs/posix/libcfs.h>
46 #elif defined(__linux__)
47 #include <libcfs/linux/libcfs.h>
48 #elif defined(__APPLE__)
49 #include <libcfs/darwin/libcfs.h>
50 #elif defined(__WINNT__)
51 #include <libcfs/winnt/libcfs.h>
52 #else
53 #error Unsupported operating system.
54 #endif
55
56 #include "curproc.h"
57
58 #ifndef offsetof
59 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
60 #endif
61
62 /* cardinality of array */
63 #ifndef ARRAY_SIZE
64 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
65 #endif
66
67 #if !defined(container_of)
68 /* given a pointer @ptr to the field @member embedded into type (usually
69  * struct) @type, return pointer to the embedding instance of @type. */
70 #define container_of(ptr, type, member) \
71         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
72 #endif
73
74 #define container_of0(ptr, type, member)                        \
75 ({                                                              \
76         typeof(ptr) __ptr = (ptr);                              \
77         type       *__res;                                      \
78                                                                 \
79         if (unlikely(IS_ERR(__ptr) || __ptr == NULL))           \
80                 __res = (type *)__ptr;                          \
81         else                                                    \
82                 __res = container_of(__ptr, type, member);      \
83         __res;                                                  \
84 })
85
86 /*
87  * true iff @i is power-of-2
88  */
89 #define IS_PO2(i)                               \
90 ({                                              \
91         typeof(i) __i;                          \
92                                                 \
93         __i = (i);                              \
94         !(__i & (__i - 1));                     \
95 })
96
97 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
98
99
100 /*
101  * Lustre Error Checksum: calculates checksum
102  * of Hex number by XORing each bit.
103  */
104 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
105                            ((hexnum) >> 8 & 0xf))
106
107
108 /*
109  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
110  * Lustre RETURN(NULL) macro.
111  */
112 #if defined(NULL)
113 #undef NULL
114 #endif
115
116 #define NULL ((void *)0)
117
118 #define LUSTRE_SRV_LNET_PID      LUSTRE_LNET_PID
119
120 #ifdef __KERNEL__
121
122 #include <libcfs/list.h>
123
124 /* libcfs tcpip */
125 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
126 int libcfs_ipif_enumerate(char ***names);
127 void libcfs_ipif_free_enumeration(char **names, int n);
128 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
129 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
130 void libcfs_sock_abort_accept(cfs_socket_t *sock);
131 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
132                         __u32 local_ip, int local_port,
133                         __u32 peer_ip, int peer_port);
134 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
135 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
136 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
137 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
138 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
139 void libcfs_sock_release(cfs_socket_t *sock);
140
141 /* libcfs watchdogs */
142 struct lc_watchdog;
143
144 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
145  * touch it once to enable it. */
146 struct lc_watchdog *lc_watchdog_add(int time,
147                                     void (*cb)(pid_t pid, void *),
148                                     void *data);
149
150 /* Enables a watchdog and resets its timer. */
151 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms);
152 void lc_watchdog_touch(struct lc_watchdog *lcw);
153
154 /* Disable a watchdog; touch it to restart it. */
155 void lc_watchdog_disable(struct lc_watchdog *lcw);
156
157 /* Clean up the watchdog */
158 void lc_watchdog_delete(struct lc_watchdog *lcw);
159
160 /* Dump a debug log */
161 void lc_watchdog_dumplog(pid_t pid, void *data);
162
163 #endif /* __KERNEL__ */
164
165 /* need both kernel and user-land acceptor */
166 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
167 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
168
169 /*
170  * libcfs pseudo device operations
171  *
172  * struct cfs_psdev_t and
173  * cfs_psdev_register() and
174  * cfs_psdev_deregister() are declared in
175  * libcfs/<os>/cfs_prim.h
176  *
177  * It's just draft now.
178  */
179
180 struct cfs_psdev_file {
181         unsigned long   off;
182         void            *private_data;
183         unsigned long   reserved1;
184         unsigned long   reserved2;
185 };
186
187 struct cfs_psdev_ops {
188         int (*p_open)(unsigned long, void *);
189         int (*p_close)(unsigned long, void *);
190         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
191         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
192         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
193 };
194
195 /*
196  * Universal memory allocator API
197  */
198 enum cfs_alloc_flags {
199         /* allocation is not allowed to block */
200         CFS_ALLOC_ATOMIC = 0x1,
201         /* allocation is allowed to block */
202         CFS_ALLOC_WAIT   = 0x2,
203         /* allocation should return zeroed memory */
204         CFS_ALLOC_ZERO   = 0x4,
205         /* allocation is allowed to call file-system code to free/clean
206          * memory */
207         CFS_ALLOC_FS     = 0x8,
208         /* allocation is allowed to do io to free/clean memory */
209         CFS_ALLOC_IO     = 0x10,
210         /* don't report allocation failure to the console */
211         CFS_ALLOC_NOWARN = 0x20,
212         /* standard allocator flag combination */
213         CFS_ALLOC_STD    = CFS_ALLOC_FS | CFS_ALLOC_IO,
214         CFS_ALLOC_USER   = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
215 };
216
217 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
218 enum cfs_alloc_page_flags {
219         /* allow to return page beyond KVM. It has to be mapped into KVM by
220          * cfs_page_map(); */
221         CFS_ALLOC_HIGH   = 0x40,
222         CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
223 };
224
225 /*
226  * Drop into debugger, if possible. Implementation is provided by platform.
227  */
228
229 void cfs_enter_debugger(void);
230
231 /*
232  * Defined by platform
233  */
234 void cfs_daemonize(char *str);
235 int cfs_daemonize_ctxt(char *str);
236 cfs_sigset_t cfs_get_blocked_sigs(void);
237 cfs_sigset_t cfs_block_allsigs(void);
238 cfs_sigset_t cfs_block_sigs(cfs_sigset_t bits);
239 void cfs_restore_sigs(cfs_sigset_t);
240 int cfs_signal_pending(void);
241 void cfs_clear_sigpending(void);
242 /*
243  * XXX Liang:
244  * these macros should be removed in the future,
245  * we keep them just for keeping libcfs compatible
246  * with other branches.
247  */
248 #define libcfs_daemonize(s)     cfs_daemonize(s)
249 #define cfs_sigmask_lock(f)     do { f= 0; } while (0)
250 #define cfs_sigmask_unlock(f)   do { f= 0; } while (0)
251
252 int convert_server_error(__u64 ecode);
253 int convert_client_oflag(int cflag, int *result);
254
255 /*
256  * Stack-tracing filling.
257  */
258
259 /*
260  * Platform-dependent data-type to hold stack frames.
261  */
262 struct cfs_stack_trace;
263
264 /*
265  * Fill @trace with current back-trace.
266  */
267 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
268
269 /*
270  * Return instruction pointer for frame @frame_no. NULL if @frame_no is
271  * invalid.
272  */
273 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
274
275 /*
276  * Universal open flags.
277  */
278 #define CFS_O_ACCMODE           0003
279 #define CFS_O_CREAT             0100
280 #define CFS_O_EXCL              0200
281 #define CFS_O_NOCTTY            0400
282 #define CFS_O_TRUNC             01000
283 #define CFS_O_APPEND            02000
284 #define CFS_O_NONBLOCK          04000
285 #define CFS_O_NDELAY            CFS_O_NONBLOCK
286 #define CFS_O_SYNC              010000
287 #define CFS_O_ASYNC             020000
288 #define CFS_O_DIRECT            040000
289 #define CFS_O_LARGEFILE         0100000
290 #define CFS_O_DIRECTORY         0200000
291 #define CFS_O_NOFOLLOW          0400000
292 #define CFS_O_NOATIME           01000000
293
294 /* convert local open flags to universal open flags */
295 int cfs_oflags2univ(int flags);
296 /* convert universal open flags to local open flags */
297 int cfs_univ2oflags(int flags);
298
299 #include <libcfs/libcfs_debug.h>
300 #include <libcfs/libcfs_private.h>
301 #include <libcfs/libcfs_ioctl.h>
302 #include <libcfs/libcfs_prim.h>
303 #include <libcfs/libcfs_time.h>
304
305 #define _LIBCFS_H
306
307 #endif /* _LIBCFS_H */