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