Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #ifndef __LIBCFS_LIBCFS_H__
40 #define __LIBCFS_LIBCFS_H__
41
42 #if !__GNUC__
43 #define __attribute__(x)
44 #endif
45
46 #if !defined(__WINNT__) && !defined(__KERNEL__)
47 #include <libcfs/posix/libcfs.h>
48 #elif defined(__linux__)
49 #include <libcfs/linux/libcfs.h>
50 #elif defined(__APPLE__)
51 #include <libcfs/darwin/libcfs.h>
52 #elif defined(__WINNT__)
53 #include <libcfs/winnt/libcfs.h>
54 #else
55 #error Unsupported operating system.
56 #endif
57
58 #include "curproc.h"
59
60 #ifndef offsetof
61 # define offsetof(typ,memb) ((long)(long_ptr_t)((char *)&(((typ *)0)->memb)))
62 #endif
63
64 #ifndef ARRAY_SIZE
65 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
66 #endif
67
68 #if !defined(container_of)
69 /* given a pointer @ptr to the field @member embedded into type (usually
70  * struct) @type, return pointer to the embedding instance of @type. */
71 #define container_of(ptr, type, member) \
72         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
73 #endif
74
75 static inline int __is_po2(unsigned long long val)
76 {
77         return !(val & (val - 1));
78 }
79
80 #define IS_PO2(val) __is_po2((unsigned long long)(val))
81
82 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
83
84 /*
85  * Lustre Error Checksum: calculates checksum
86  * of Hex number by XORing each bit.
87  */
88 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
89                            ((hexnum) >> 8 & 0xf))
90
91
92 /*
93  * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
94  * Lustre RETURN(NULL) macro.
95  */
96 #if defined(NULL)
97 #undef NULL
98 #endif
99
100 #define NULL ((void *)0)
101
102 #define LUSTRE_SRV_LNET_PID      LUSTRE_LNET_PID
103
104 #ifdef __KERNEL__
105
106 #include <libcfs/list.h>
107
108 #ifndef cfs_for_each_possible_cpu
109 #  error cfs_for_each_possible_cpu is not supported by kernel!
110 #endif
111
112 /* libcfs tcpip */
113 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
114 int libcfs_ipif_enumerate(char ***names);
115 void libcfs_ipif_free_enumeration(char **names, int n);
116 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
117 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
118 void libcfs_sock_abort_accept(cfs_socket_t *sock);
119 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
120                         __u32 local_ip, int local_port,
121                         __u32 peer_ip, int peer_port);
122 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
123 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
124 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
125 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
126 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
127 void libcfs_sock_release(cfs_socket_t *sock);
128
129 /* libcfs watchdogs */
130 struct lc_watchdog;
131
132 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
133  * touch it once to enable it. */
134 struct lc_watchdog *lc_watchdog_add(int time,
135                                     void (*cb)(pid_t pid, void *),
136                                     void *data);
137
138 /* Enables a watchdog and resets its timer. */
139 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
140 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout,                   \
141                           AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
142                           svc->srv_watchdog_factor)
143
144 /* Disable a watchdog; touch it to restart it. */
145 void lc_watchdog_disable(struct lc_watchdog *lcw);
146
147 /* Clean up the watchdog */
148 void lc_watchdog_delete(struct lc_watchdog *lcw);
149
150 /* Dump a debug log */
151 void lc_watchdog_dumplog(pid_t pid, void *data);
152
153 #endif /* __KERNEL__ */
154
155 /* need both kernel and user-land acceptor */
156 #define LNET_ACCEPTOR_MIN_RESERVED_PORT    512
157 #define LNET_ACCEPTOR_MAX_RESERVED_PORT    1023
158
159 /*
160  * libcfs pseudo device operations
161  *
162  * struct cfs_psdev_t and
163  * cfs_psdev_register() and
164  * cfs_psdev_deregister() are declared in
165  * libcfs/<os>/<os>-prim.h
166  *
167  * It's just draft now.
168  */
169
170 struct cfs_psdev_file {
171         unsigned long   off;
172         void            *private_data;
173         unsigned long   reserved1;
174         unsigned long   reserved2;
175 };
176
177 struct cfs_psdev_ops {
178         int (*p_open)(unsigned long, void *);
179         int (*p_close)(unsigned long, void *);
180         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
181         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
182         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
183 };
184
185 /*
186  * Universal memory allocator API
187  */
188 enum cfs_alloc_flags {
189         /* allocation is not allowed to block */
190         CFS_ALLOC_ATOMIC = 0x1,
191         /* allocation is allowed to block */
192         CFS_ALLOC_WAIT   = 0x2,
193         /* allocation should return zeroed memory */
194         CFS_ALLOC_ZERO   = 0x4,
195         /* allocation is allowed to call file-system code to free/clean
196          * memory */
197         CFS_ALLOC_FS     = 0x8,
198         /* allocation is allowed to do io to free/clean memory */
199         CFS_ALLOC_IO     = 0x10,
200         /* don't report allocation failure to the console */
201         CFS_ALLOC_NOWARN = 0x20,
202         /* standard allocator flag combination */
203         CFS_ALLOC_STD    = CFS_ALLOC_FS | CFS_ALLOC_IO,
204         CFS_ALLOC_USER   = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
205 };
206
207 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
208 enum cfs_alloc_page_flags {
209         /* allow to return page beyond KVM. It has to be mapped into KVM by
210          * cfs_page_map(); */
211         CFS_ALLOC_HIGH   = 0x40,
212         CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
213 };
214
215 /*
216  * Drop into debugger, if possible. Implementation is provided by platform.
217  */
218
219 void cfs_enter_debugger(void);
220
221 /*
222  * Defined by platform
223  */
224 void cfs_daemonize(char *str);
225 int cfs_daemonize_ctxt(char *str);
226 cfs_sigset_t cfs_get_blocked_sigs(void);
227 cfs_sigset_t cfs_block_allsigs(void);
228 cfs_sigset_t cfs_block_sigs(cfs_sigset_t bits);
229 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs);
230 void cfs_restore_sigs(cfs_sigset_t);
231 int cfs_signal_pending(void);
232 void cfs_clear_sigpending(void);
233
234 /*
235  * XXX Liang:
236  * these macros should be removed in the future,
237  * we keep them just for keeping libcfs compatible
238  * with other branches.
239  */
240 #define libcfs_daemonize(s)     cfs_daemonize(s)
241 #define cfs_sigmask_lock(f)     do { f= 0; } while (0)
242 #define cfs_sigmask_unlock(f)   do { f= 0; } while (0)
243
244 int convert_server_error(__u64 ecode);
245 int convert_client_oflag(int cflag, int *result);
246
247 /*
248  * Stack-tracing filling.
249  */
250
251 /*
252  * Platform-dependent data-type to hold stack frames.
253  */
254 struct cfs_stack_trace;
255
256 /*
257  * Fill @trace with current back-trace.
258  */
259 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
260
261 /*
262  * Return instruction pointer for frame @frame_no. NULL if @frame_no is
263  * invalid.
264  */
265 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
266
267 #ifndef O_NOACCESS
268 #define O_NOACCESS O_NONBLOCK
269 #endif
270
271 /*
272  * Universal open flags.
273  */
274 #define CFS_O_NOACCESS          0003
275 #define CFS_O_ACCMODE           CFS_O_NOACCESS
276 #define CFS_O_CREAT             0100
277 #define CFS_O_EXCL              0200
278 #define CFS_O_NOCTTY            0400
279 #define CFS_O_TRUNC             01000
280 #define CFS_O_APPEND            02000
281 #define CFS_O_NONBLOCK          04000
282 #define CFS_O_NDELAY            CFS_O_NONBLOCK
283 #define CFS_O_SYNC              010000
284 #define CFS_O_ASYNC             020000
285 #define CFS_O_DIRECT            040000
286 #define CFS_O_LARGEFILE         0100000
287 #define CFS_O_DIRECTORY         0200000
288 #define CFS_O_NOFOLLOW          0400000
289 #define CFS_O_NOATIME           01000000
290
291 /* convert local open flags to universal open flags */
292 int cfs_oflags2univ(int flags);
293 /* convert universal open flags to local open flags */
294 int cfs_univ2oflags(int flags);
295
296 /*
297  * Random number handling
298  */
299
300 /* returns a random 32-bit integer */
301 unsigned int cfs_rand(void);
302 /* seed the generator */
303 void cfs_srand(unsigned int, unsigned int);
304 void cfs_get_random_bytes(void *buf, int size);
305
306 #include <libcfs/libcfs_debug.h>
307 #include <libcfs/libcfs_private.h>
308 #include <libcfs/libcfs_ioctl.h>
309 #include <libcfs/libcfs_prim.h>
310 #include <libcfs/libcfs_time.h>
311 #include <libcfs/libcfs_string.h>
312 #include <libcfs/libcfs_kernelcomm.h>
313 #include <libcfs/libcfs_workitem.h>
314 #include <libcfs/libcfs_hash.h>
315 #include <libcfs/libcfs_fail.h>
316 #include <libcfs/params_tree.h>
317
318 /* container_of depends on "likely" which is defined in libcfs_private.h */
319 static inline void *__container_of(void *ptr, unsigned long shift)
320 {
321         if (unlikely(IS_ERR(ptr) || ptr == NULL))
322                 return ptr;
323         else
324                 return (char *)ptr - shift;
325 }
326
327 #define container_of0(ptr, type, member) \
328         ((type *)__container_of((void *)(ptr), offsetof(type, member)))
329
330 #define SET_BUT_UNUSED(a) do { } while(sizeof(a) - sizeof(a))
331
332 #define _LIBCFS_H
333
334 #endif /* _LIBCFS_H */