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