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