Whamcloud - gitweb
76117dab8d499e3e46d7647cf31d1898f5cda604
[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 #include <libcfs/types.h>
41
42 #ifdef __KERNEL__
43 # include <libcfs/linux/libcfs.h>
44 #else /* __KERNEL__ */
45 # include <libcfs/posix/libcfs.h>
46 #endif /* !__KERNEL__ */
47
48 #include "curproc.h"
49
50 #ifndef offsetof
51 # define offsetof(typ,memb) ((long)(long_ptr_t)((char *)&(((typ *)0)->memb)))
52 #endif
53
54 #ifndef ARRAY_SIZE
55 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
56 #endif
57
58 #if !defined(swap)
59 #define swap(x,y) do { typeof(x) z = x; x = y; y = z; } while (0)
60 #endif
61
62 #if !defined(container_of)
63 /* given a pointer @ptr to the field @member embedded into type (usually
64  * struct) @type, return pointer to the embedding instance of @type. */
65 #define container_of(ptr, type, member) \
66         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
67 #endif
68
69 static inline int __is_po2(unsigned long long val)
70 {
71         return !(val & (val - 1));
72 }
73
74 #define IS_PO2(val) __is_po2((unsigned long long)(val))
75
76 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
77
78 /* Sparse annotations */
79 #ifdef __KERNEL__
80 # if !defined(__must_hold)
81 #  ifdef __CHECKER__
82 #   define __must_hold(x) __attribute__((context(x, 1, 1)))
83 #  else /* __CHECKER__ */
84 #   define __must_hold(x)
85 #  endif /* !__CHECKER__ */
86 # endif /* !__must_hold */
87 #else /* __KERNEL__ */
88 # define __acquires(x)
89 # define __releases(x)
90 # define __must_hold(x)
91 #endif /* !__KERNEL__ */
92
93 /*
94  * Lustre Error Checksum: calculates checksum
95  * of Hex number by XORing each bit.
96  */
97 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
98                            ((hexnum) >> 8 & 0xf))
99
100 /*
101  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
102  * Lustre RETURN(NULL) macro.
103  */
104 #if defined(NULL)
105 #undef NULL
106 #endif
107
108 #define NULL ((void *)0)
109
110 #define LUSTRE_SRV_LNET_PID      LUSTRE_LNET_PID
111
112 #ifdef __KERNEL__
113
114 #include <libcfs/list.h>
115
116 #ifndef cfs_for_each_possible_cpu
117 #  error cfs_for_each_possible_cpu is not supported by kernel!
118 #endif
119
120 /* libcfs tcpip */
121 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
122 int libcfs_ipif_enumerate(char ***names);
123 void libcfs_ipif_free_enumeration(char **names, int n);
124 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
125 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
126 void libcfs_sock_abort_accept(cfs_socket_t *sock);
127 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
128                         __u32 local_ip, int local_port,
129                         __u32 peer_ip, int peer_port);
130 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
131 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
132 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
133 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
134 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
135 void libcfs_sock_release(cfs_socket_t *sock);
136
137 /* libcfs watchdogs */
138 struct lc_watchdog;
139
140 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
141  * touch it once to enable it. */
142 struct lc_watchdog *lc_watchdog_add(int time,
143                                     void (*cb)(pid_t pid, void *),
144                                     void *data);
145
146 /* Enables a watchdog and resets its timer. */
147 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
148 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout,                   \
149                           AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
150                           svc->srv_watchdog_factor)
151
152 /* Disable a watchdog; touch it to restart it. */
153 void lc_watchdog_disable(struct lc_watchdog *lcw);
154
155 /* Clean up the watchdog */
156 void lc_watchdog_delete(struct lc_watchdog *lcw);
157
158 /* Dump a debug log */
159 void lc_watchdog_dumplog(pid_t pid, void *data);
160
161 #else /* !__KERNEL__ */
162 #include <unistd.h>
163 #ifndef PAGE_SIZE
164 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
165 #endif
166 #endif /* !__KERNEL__ */
167
168 /* need both kernel and user-land acceptor */
169 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
170 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
171
172 /*
173  * libcfs pseudo device operations
174  *
175  * struct struct miscdevice and
176  * misc_register() and
177  * misc_deregister() are declared in
178  * libcfs/<os>/<os>-prim.h
179  *
180  * It's just draft now.
181  */
182
183 struct cfs_psdev_file {
184         unsigned long   off;
185         void            *private_data;
186         unsigned long   reserved1;
187         unsigned long   reserved2;
188 };
189
190 struct cfs_psdev_ops {
191         int (*p_open)(unsigned long, void *);
192         int (*p_close)(unsigned long, void *);
193         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
194         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
195         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
196 };
197
198 /*
199  * Drop into debugger, if possible. Implementation is provided by platform.
200  */
201
202 void cfs_enter_debugger(void);
203
204 /*
205  * Defined by platform
206  */
207 int unshare_fs_struct(void);
208 sigset_t cfs_get_blocked_sigs(void);
209 sigset_t cfs_block_allsigs(void);
210 sigset_t cfs_block_sigs(unsigned long sigs);
211 sigset_t cfs_block_sigsinv(unsigned long sigs);
212 void cfs_restore_sigs(sigset_t);
213 int cfs_signal_pending(void);
214 void cfs_clear_sigpending(void);
215
216 int convert_server_error(__u64 ecode);
217 int convert_client_oflag(int cflag, int *result);
218
219 /*
220  * Stack-tracing filling.
221  */
222
223 /*
224  * Platform-dependent data-type to hold stack frames.
225  */
226 struct cfs_stack_trace;
227
228 /*
229  * Fill @trace with current back-trace.
230  */
231 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
232
233 /*
234  * Return instruction pointer for frame @frame_no. NULL if @frame_no is
235  * invalid.
236  */
237 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
238
239 /*
240  * Random number handling
241  */
242
243 /* returns a random 32-bit integer */
244 unsigned int cfs_rand(void);
245 /* seed the generator */
246 void cfs_srand(unsigned int, unsigned int);
247 void cfs_get_random_bytes(void *buf, int size);
248
249 #include <libcfs/err.h>
250 #include <libcfs/libcfs_debug.h>
251 #include <libcfs/libcfs_private.h>
252 #include <libcfs/bitmap.h>
253 #include <libcfs/libcfs_cpu.h>
254 #include <libcfs/libcfs_ioctl.h>
255 #include <libcfs/libcfs_prim.h>
256 #include <libcfs/libcfs_time.h>
257 #include <libcfs/libcfs_string.h>
258 #include <libcfs/libcfs_kernelcomm.h>
259 #include <libcfs/libcfs_workitem.h>
260 #include <libcfs/libcfs_hash.h>
261 #include <libcfs/libcfs_heap.h>
262 #include <libcfs/libcfs_fail.h>
263 #include <libcfs/params_tree.h>
264
265 /* container_of depends on "likely" which is defined in libcfs_private.h */
266 static inline void *__container_of(const void *ptr, unsigned long shift)
267 {
268         if (unlikely(IS_ERR(ptr) || ptr == NULL))
269                 return ERR_CAST(ptr);
270         else
271                 return (char *)ptr - shift;
272 }
273
274 #define container_of0(ptr, type, member)                                \
275         ((type *)__container_of((ptr), offsetof(type, member)))
276
277 #define _LIBCFS_H
278
279 #endif /* _LIBCFS_H */