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