Whamcloud - gitweb
76abc5a0a20d1c3d1aaf6d92bb17c32ef06851f6
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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(swap)
67 #define swap(x,y) do { typeof(x) z = x; x = y; y = z; } while (0)
68 #endif
69
70 #if !defined(container_of)
71 /* given a pointer @ptr to the field @member embedded into type (usually
72  * struct) @type, return pointer to the embedding instance of @type. */
73 #define container_of(ptr, type, member) \
74         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
75 #endif
76
77 static inline int __is_po2(unsigned long long val)
78 {
79         return !(val & (val - 1));
80 }
81
82 #define IS_PO2(val) __is_po2((unsigned long long)(val))
83
84 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
85
86 /*
87  * Lustre Error Checksum: calculates checksum
88  * of Hex number by XORing each bit.
89  */
90 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
91                            ((hexnum) >> 8 & 0xf))
92
93
94 /*
95  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
96  * Lustre RETURN(NULL) macro.
97  */
98 #if defined(NULL)
99 #undef NULL
100 #endif
101
102 #define NULL ((void *)0)
103
104 #define LUSTRE_SRV_LNET_PID      LUSTRE_LNET_PID
105
106 #ifdef __KERNEL__
107
108 #include <libcfs/list.h>
109
110 #ifndef cfs_for_each_possible_cpu
111 #  error cfs_for_each_possible_cpu is not supported by kernel!
112 #endif
113
114 /* libcfs tcpip */
115 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
116 int libcfs_ipif_enumerate(char ***names);
117 void libcfs_ipif_free_enumeration(char **names, int n);
118 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
119 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
120 void libcfs_sock_abort_accept(cfs_socket_t *sock);
121 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
122                         __u32 local_ip, int local_port,
123                         __u32 peer_ip, int peer_port);
124 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
125 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
126 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
127 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
128 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
129 void libcfs_sock_release(cfs_socket_t *sock);
130
131 /* libcfs watchdogs */
132 struct lc_watchdog;
133
134 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
135  * touch it once to enable it. */
136 struct lc_watchdog *lc_watchdog_add(int time,
137                                     void (*cb)(pid_t pid, void *),
138                                     void *data);
139
140 /* Enables a watchdog and resets its timer. */
141 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
142 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout,                   \
143                           AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
144                           svc->srv_watchdog_factor)
145
146 /* Disable a watchdog; touch it to restart it. */
147 void lc_watchdog_disable(struct lc_watchdog *lcw);
148
149 /* Clean up the watchdog */
150 void lc_watchdog_delete(struct lc_watchdog *lcw);
151
152 /* Dump a debug log */
153 void lc_watchdog_dumplog(pid_t pid, void *data);
154
155 #endif /* __KERNEL__ */
156
157 /* need both kernel and user-land acceptor */
158 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
159 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
160
161 /*
162  * libcfs pseudo device operations
163  *
164  * struct cfs_psdev_t and
165  * cfs_psdev_register() and
166  * cfs_psdev_deregister() are declared in
167  * libcfs/<os>/<os>-prim.h
168  *
169  * It's just draft now.
170  */
171
172 struct cfs_psdev_file {
173         unsigned long   off;
174         void            *private_data;
175         unsigned long   reserved1;
176         unsigned long   reserved2;
177 };
178
179 struct cfs_psdev_ops {
180         int (*p_open)(unsigned long, void *);
181         int (*p_close)(unsigned long, void *);
182         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
183         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
184         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
185 };
186
187 /*
188  * Drop into debugger, if possible. Implementation is provided by platform.
189  */
190
191 void cfs_enter_debugger(void);
192
193 /*
194  * Defined by platform
195  */
196 int unshare_fs_struct(void);
197 cfs_sigset_t cfs_get_blocked_sigs(void);
198 cfs_sigset_t cfs_block_allsigs(void);
199 cfs_sigset_t cfs_block_sigs(unsigned long sigs);
200 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs);
201 void cfs_restore_sigs(cfs_sigset_t);
202 int cfs_signal_pending(void);
203 void cfs_clear_sigpending(void);
204
205 int convert_server_error(__u64 ecode);
206 int convert_client_oflag(int cflag, int *result);
207
208 /*
209  * Stack-tracing filling.
210  */
211
212 /*
213  * Platform-dependent data-type to hold stack frames.
214  */
215 struct cfs_stack_trace;
216
217 /*
218  * Fill @trace with current back-trace.
219  */
220 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
221
222 /*
223  * Return instruction pointer for frame @frame_no. NULL if @frame_no is
224  * invalid.
225  */
226 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
227
228 #ifndef O_NOACCESS
229 #define O_NOACCESS O_NONBLOCK
230 #endif
231
232 /*
233  * Universal open flags.
234  */
235 #define CFS_O_NOACCESS          0003
236 #define CFS_O_ACCMODE           CFS_O_NOACCESS
237 #define CFS_O_CREAT             0100
238 #define CFS_O_EXCL              0200
239 #define CFS_O_NOCTTY            0400
240 #define CFS_O_TRUNC             01000
241 #define CFS_O_APPEND            02000
242 #define CFS_O_NONBLOCK          04000
243 #define CFS_O_NDELAY            CFS_O_NONBLOCK
244 #define CFS_O_SYNC              010000
245 #define CFS_O_ASYNC             020000
246 #define CFS_O_DIRECT            040000
247 #define CFS_O_LARGEFILE         0100000
248 #define CFS_O_DIRECTORY         0200000
249 #define CFS_O_NOFOLLOW          0400000
250 #define CFS_O_NOATIME           01000000
251
252 /* convert local open flags to universal open flags */
253 int cfs_oflags2univ(int flags);
254 /* convert universal open flags to local open flags */
255 int cfs_univ2oflags(int flags);
256
257 /*
258  * Random number handling
259  */
260
261 /* returns a random 32-bit integer */
262 unsigned int cfs_rand(void);
263 /* seed the generator */
264 void cfs_srand(unsigned int, unsigned int);
265 void cfs_get_random_bytes(void *buf, int size);
266
267 #include <libcfs/libcfs_debug.h>
268 #include <libcfs/libcfs_cpu.h>
269 #include <libcfs/libcfs_private.h>
270 #include <libcfs/libcfs_ioctl.h>
271 #include <libcfs/libcfs_prim.h>
272 #include <libcfs/libcfs_time.h>
273 #include <libcfs/libcfs_string.h>
274 #include <libcfs/libcfs_kernelcomm.h>
275 #include <libcfs/libcfs_workitem.h>
276 #include <libcfs/libcfs_hash.h>
277 #include <libcfs/libcfs_heap.h>
278 #include <libcfs/libcfs_fail.h>
279 #include <libcfs/params_tree.h>
280 #include <libcfs/libcfs_crypto.h>
281
282 /* container_of depends on "likely" which is defined in libcfs_private.h */
283 static inline void *__container_of(void *ptr, unsigned long shift)
284 {
285         if (unlikely(IS_ERR(ptr) || ptr == NULL))
286                 return ptr;
287         else
288                 return (char *)ptr - shift;
289 }
290
291 #define container_of0(ptr, type, member) \
292         ((type *)__container_of((void *)(ptr), offsetof(type, member)))
293
294 #define _LIBCFS_H
295
296 #endif /* _LIBCFS_H */