Whamcloud - gitweb
b=15699
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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) ((long)(long_ptr_t)((char *)&(((typ *)0)->memb)))
60 #endif
61
62 #ifndef ARRAY_SIZE
63 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
64 #endif
65
66 #if !defined(container_of)
67 /* given a pointer @ptr to the field @member embedded into type (usually
68  * struct) @type, return pointer to the embedding instance of @type. */
69 #define container_of(ptr, type, member) \
70         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
71 #endif
72
73 static inline int __is_po2(unsigned long long val)
74 {
75         return !(val & (val - 1));
76 }
77
78 #define IS_PO2(val) __is_po2((unsigned long long)(val))
79
80 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
81
82 /*
83  * Lustre Error Checksum: calculates checksum
84  * of Hex number by XORing each bit.
85  */
86 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
87                            ((hexnum) >> 8 & 0xf))
88
89
90 /*
91  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
92  * Lustre RETURN(NULL) macro.
93  */
94 #if defined(NULL)
95 #undef NULL
96 #endif
97
98 #define NULL ((void *)0)
99
100 #define LUSTRE_SRV_LNET_PID      LUSTRE_LNET_PID
101
102 #ifdef __KERNEL__
103
104 #include <libcfs/list.h>
105
106 /* libcfs tcpip */
107 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
108 int libcfs_ipif_enumerate(char ***names);
109 void libcfs_ipif_free_enumeration(char **names, int n);
110 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
111 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
112 void libcfs_sock_abort_accept(cfs_socket_t *sock);
113 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
114                         __u32 local_ip, int local_port,
115                         __u32 peer_ip, int peer_port);
116 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
117 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
118 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
119 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
120 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
121 void libcfs_sock_release(cfs_socket_t *sock);
122
123 /* libcfs watchdogs */
124 struct lc_watchdog;
125
126 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
127  * touch it once to enable it. */
128 struct lc_watchdog *lc_watchdog_add(int time,
129                                     void (*cb)(pid_t pid, void *),
130                                     void *data);
131
132 /* Enables a watchdog and resets its timer. */
133 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms);
134 void lc_watchdog_touch(struct lc_watchdog *lcw);
135
136 /* Disable a watchdog; touch it to restart it. */
137 void lc_watchdog_disable(struct lc_watchdog *lcw);
138
139 /* Clean up the watchdog */
140 void lc_watchdog_delete(struct lc_watchdog *lcw);
141
142 /* Dump a debug log */
143 void lc_watchdog_dumplog(pid_t pid, void *data);
144
145 #endif /* __KERNEL__ */
146
147 /* need both kernel and user-land acceptor */
148 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
149 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
150
151 /*
152  * libcfs pseudo device operations
153  *
154  * struct cfs_psdev_t and
155  * cfs_psdev_register() and
156  * cfs_psdev_deregister() are declared in
157  * libcfs/<os>/<os>-prim.h
158  *
159  * It's just draft now.
160  */
161
162 struct cfs_psdev_file {
163         unsigned long   off;
164         void            *private_data;
165         unsigned long   reserved1;
166         unsigned long   reserved2;
167 };
168
169 struct cfs_psdev_ops {
170         int (*p_open)(unsigned long, void *);
171         int (*p_close)(unsigned long, void *);
172         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
173         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
174         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
175 };
176
177 /*
178  * Universal memory allocator API
179  */
180 enum cfs_alloc_flags {
181         /* allocation is not allowed to block */
182         CFS_ALLOC_ATOMIC = 0x1,
183         /* allocation is allowed to block */
184         CFS_ALLOC_WAIT   = 0x2,
185         /* allocation should return zeroed memory */
186         CFS_ALLOC_ZERO   = 0x4,
187         /* allocation is allowed to call file-system code to free/clean
188          * memory */
189         CFS_ALLOC_FS     = 0x8,
190         /* allocation is allowed to do io to free/clean memory */
191         CFS_ALLOC_IO     = 0x10,
192         /* don't report allocation failure to the console */
193         CFS_ALLOC_NOWARN = 0x20,
194         /* standard allocator flag combination */
195         CFS_ALLOC_STD    = CFS_ALLOC_FS | CFS_ALLOC_IO,
196         CFS_ALLOC_USER   = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
197 };
198
199 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
200 enum cfs_alloc_page_flags {
201         /* allow to return page beyond KVM. It has to be mapped into KVM by
202          * cfs_page_map(); */
203         CFS_ALLOC_HIGH   = 0x40,
204         CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
205 };
206
207 /*
208  * Drop into debugger, if possible. Implementation is provided by platform.
209  */
210
211 void cfs_enter_debugger(void);
212
213 /*
214  * Defined by platform
215  */
216 void cfs_daemonize(char *str);
217 int cfs_daemonize_ctxt(char *str);
218 cfs_sigset_t cfs_get_blocked_sigs(void);
219 cfs_sigset_t cfs_block_allsigs(void);
220 cfs_sigset_t cfs_block_sigs(cfs_sigset_t bits);
221 void cfs_restore_sigs(cfs_sigset_t);
222 int cfs_signal_pending(void);
223 void cfs_clear_sigpending(void);
224 /*
225  * XXX Liang:
226  * these macros should be removed in the future,
227  * we keep them just for keeping libcfs compatible
228  * with other branches.
229  */
230 #define libcfs_daemonize(s)     cfs_daemonize(s)
231 #define cfs_sigmask_lock(f)     do { f= 0; } while (0)
232 #define cfs_sigmask_unlock(f)   do { f= 0; } while (0)
233
234 int convert_server_error(__u64 ecode);
235 int convert_client_oflag(int cflag, int *result);
236
237 /*
238  * Stack-tracing filling.
239  */
240
241 /*
242  * Platform-dependent data-type to hold stack frames.
243  */
244 struct cfs_stack_trace;
245
246 /*
247  * Fill @trace with current back-trace.
248  */
249 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
250
251 /*
252  * Return instruction pointer for frame @frame_no. NULL if @frame_no is
253  * invalid.
254  */
255 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
256
257 /*
258  * Universal open flags.
259  */
260 #define CFS_O_ACCMODE           0003
261 #define CFS_O_CREAT             0100
262 #define CFS_O_EXCL              0200
263 #define CFS_O_NOCTTY            0400
264 #define CFS_O_TRUNC             01000
265 #define CFS_O_APPEND            02000
266 #define CFS_O_NONBLOCK          04000
267 #define CFS_O_NDELAY            CFS_O_NONBLOCK
268 #define CFS_O_SYNC              010000
269 #define CFS_O_ASYNC             020000
270 #define CFS_O_DIRECT            040000
271 #define CFS_O_LARGEFILE         0100000
272 #define CFS_O_DIRECTORY         0200000
273 #define CFS_O_NOFOLLOW          0400000
274 #define CFS_O_NOATIME           01000000
275
276 /* convert local open flags to universal open flags */
277 int cfs_oflags2univ(int flags);
278 /* convert universal open flags to local open flags */
279 int cfs_univ2oflags(int flags);
280
281 #include <libcfs/libcfs_debug.h>
282 #include <libcfs/libcfs_private.h>
283 #include <libcfs/libcfs_ioctl.h>
284 #include <libcfs/libcfs_prim.h>
285 #include <libcfs/libcfs_time.h>
286 #include <libcfs/libcfs_string.h>
287
288 /* container_of depends on "likely" which is defined in libcfs_private.h */
289 static inline void *__container_of(void *ptr, unsigned long shift)
290 {
291         if (unlikely(IS_ERR(ptr) || ptr == NULL))
292                 return ptr;
293         else
294                 return (char *)ptr - shift;
295 }
296
297 #define container_of0(ptr, type, member) \
298         ((type *)__container_of((void *)(ptr), offsetof(type, member)))
299
300 #define _LIBCFS_H
301
302 #endif /* _LIBCFS_H */