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