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