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