Whamcloud - gitweb
LU-7114 ldiskfs: corrupted bitmaps handling patches
[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, 2015, 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 #endif /* __KERNEL__ */
67
68 #include "curproc.h"
69
70 #define LIBCFS_VERSION  "0.5.0"
71
72 #ifndef ARRAY_SIZE
73 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
74 #endif
75
76 #if !defined(swap)
77 #define swap(x,y) do { typeof(x) z = x; x = y; y = z; } while (0)
78 #endif
79
80 #if !defined(container_of)
81 /* given a pointer @ptr to the field @member embedded into type (usually
82  * struct) @type, return pointer to the embedding instance of @type. */
83 #define container_of(ptr, type, member) \
84         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
85 #endif
86
87 static inline int __is_po2(unsigned long long val)
88 {
89         return !(val & (val - 1));
90 }
91
92 #define IS_PO2(val)       __is_po2((unsigned long long)(val))
93 #define PO2_ROUNDUP_TYPED(x, po2, type) (-(-(type)(x) & -(type)(po2)))
94 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
95
96 /* Sparse annotations */
97 #ifdef __KERNEL__
98 # if !defined(__must_hold)
99 #  ifdef __CHECKER__
100 #   define __must_hold(x) __attribute__((context(x, 1, 1)))
101 #  else /* __CHECKER__ */
102 #   define __must_hold(x)
103 #  endif /* !__CHECKER__ */
104 # endif /* !__must_hold */
105 #else /* __KERNEL__ */
106 # define __acquires(x)
107 # define __releases(x)
108 # define __must_hold(x)
109 #endif /* !__KERNEL__ */
110
111 /*
112  * Lustre Error Checksum: calculates checksum
113  * of Hex number by XORing each bit.
114  */
115 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
116                            ((hexnum) >> 8 & 0xf))
117
118 /*
119  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
120  * Lustre RETURN(NULL) macro.
121  */
122 #if defined(NULL)
123 #undef NULL
124 #endif
125
126 #define NULL ((void *)0)
127
128 #ifdef __KERNEL__
129
130 /* libcfs watchdogs */
131 struct lc_watchdog;
132
133 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
134  * touch it once to enable it. */
135 struct lc_watchdog *lc_watchdog_add(int time,
136                                     void (*cb)(pid_t pid, void *),
137                                     void *data);
138
139 /* Enables a watchdog and resets its timer. */
140 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
141 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout,                   \
142                           AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
143                           svc->srv_watchdog_factor)
144
145 /* Disable a watchdog; touch it to restart it. */
146 void lc_watchdog_disable(struct lc_watchdog *lcw);
147
148 /* Clean up the watchdog */
149 void lc_watchdog_delete(struct lc_watchdog *lcw);
150
151 #endif /* __KERNEL__ */
152
153 /* need both kernel and user-land acceptor */
154 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
155 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
156
157 /*
158  * libcfs pseudo device operations
159  *
160  * struct struct miscdevice and
161  * misc_register() and
162  * misc_deregister() are declared in
163  * libcfs/<os>/<os>-prim.h
164  *
165  * It's just draft now.
166  */
167
168 struct cfs_psdev_file {
169         unsigned long   off;
170         void            *private_data;
171         unsigned long   reserved1;
172         unsigned long   reserved2;
173 };
174
175 struct cfs_psdev_ops {
176         int (*p_open)(unsigned long, void *);
177         int (*p_close)(unsigned long, void *);
178         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
179         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
180         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void __user *);
181 };
182
183 /*
184  * Drop into debugger, if possible. Implementation is provided by platform.
185  */
186
187 void cfs_enter_debugger(void);
188
189 /*
190  * Defined by platform
191  */
192 int unshare_fs_struct(void);
193 sigset_t cfs_block_allsigs(void);
194 sigset_t cfs_block_sigs(unsigned long sigs);
195 sigset_t cfs_block_sigsinv(unsigned long sigs);
196 void cfs_restore_sigs(sigset_t);
197 void cfs_clear_sigpending(void);
198
199 /*
200  * Random number handling
201  */
202
203 /* returns a random 32-bit integer */
204 unsigned int cfs_rand(void);
205 /* seed the generator */
206 void cfs_srand(unsigned int, unsigned int);
207 void cfs_get_random_bytes(void *buf, int size);
208
209 #include <libcfs/byteorder.h>
210 #include <libcfs/libcfs_debug.h>
211 #include <libcfs/libcfs_private.h>
212 #ifdef __KERNEL__
213 # include <libcfs/bitmap.h>
214 # include <libcfs/libcfs_cpu.h>
215 # include <libcfs/libcfs_ioctl.h>
216 # include <libcfs/libcfs_prim.h>
217 #endif /* __KERNEL__ */
218 #include <libcfs/libcfs_time.h>
219 #ifdef __KERNEL__
220 # include <libcfs/libcfs_string.h>
221 # include <libcfs/libcfs_workitem.h>
222 # include <libcfs/libcfs_hash.h>
223 # include <libcfs/libcfs_heap.h>
224 # include <libcfs/libcfs_fail.h>
225
226 /* container_of depends on "likely" which is defined in libcfs_private.h */
227 static inline void *__container_of(const void *ptr, unsigned long shift)
228 {
229         if (unlikely(IS_ERR(ptr) || ptr == NULL))
230                 return ERR_CAST(ptr);
231         else
232                 return (char *)ptr - shift;
233 }
234
235 #define container_of0(ptr, type, member)                                \
236         ((type *)__container_of((ptr), offsetof(type, member)))
237
238 #endif /* __KERNEL__ */
239
240 #endif /* _LIBCFS_LIBCFS_H_ */