4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #ifndef __LIBCFS_LIBCFS_H__
38 #define __LIBCFS_LIBCFS_H__
41 #define __attribute__(x)
44 #if !defined(__WINNT__) && !defined(__KERNEL__)
45 #include <libcfs/posix/libcfs.h>
46 #elif defined(__linux__)
47 #include <libcfs/linux/libcfs.h>
48 #elif defined(__APPLE__)
49 #include <libcfs/darwin/libcfs.h>
50 #elif defined(__WINNT__)
51 #include <libcfs/winnt/libcfs.h>
53 #error Unsupported operating system.
59 # define offsetof(typ,memb) ((long)(long_ptr_t)((char *)&(((typ *)0)->memb)))
63 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
67 #define swap(x,y) do { typeof(x) z = x; x = y; y = z; } while (0)
70 #if !defined(container_of)
71 /* given a pointer @ptr to the field @member embedded into type (usually
72 * struct) @type, return pointer to the embedding instance of @type. */
73 #define container_of(ptr, type, member) \
74 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
77 static inline int __is_po2(unsigned long long val)
79 return !(val & (val - 1));
82 #define IS_PO2(val) __is_po2((unsigned long long)(val))
84 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
87 * Lustre Error Checksum: calculates checksum
88 * of Hex number by XORing each bit.
90 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
91 ((hexnum) >> 8 & 0xf))
95 * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
96 * Lustre RETURN(NULL) macro.
102 #define NULL ((void *)0)
104 #define LUSTRE_SRV_LNET_PID LUSTRE_LNET_PID
108 #include <libcfs/list.h>
110 #ifndef cfs_for_each_possible_cpu
111 # error cfs_for_each_possible_cpu is not supported by kernel!
115 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
116 int libcfs_ipif_enumerate(char ***names);
117 void libcfs_ipif_free_enumeration(char **names, int n);
118 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
119 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
120 void libcfs_sock_abort_accept(cfs_socket_t *sock);
121 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
122 __u32 local_ip, int local_port,
123 __u32 peer_ip, int peer_port);
124 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
125 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
126 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
127 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
128 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
129 void libcfs_sock_release(cfs_socket_t *sock);
131 /* libcfs watchdogs */
134 /* Add a watchdog which fires after "time" milliseconds of delay. You have to
135 * touch it once to enable it. */
136 struct lc_watchdog *lc_watchdog_add(int time,
137 void (*cb)(pid_t pid, void *),
140 /* Enables a watchdog and resets its timer. */
141 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
142 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout, \
143 AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
144 svc->srv_watchdog_factor)
146 /* Disable a watchdog; touch it to restart it. */
147 void lc_watchdog_disable(struct lc_watchdog *lcw);
149 /* Clean up the watchdog */
150 void lc_watchdog_delete(struct lc_watchdog *lcw);
152 /* Dump a debug log */
153 void lc_watchdog_dumplog(pid_t pid, void *data);
155 #endif /* __KERNEL__ */
157 /* need both kernel and user-land acceptor */
158 #define LNET_ACCEPTOR_MIN_RESERVED_PORT 512
159 #define LNET_ACCEPTOR_MAX_RESERVED_PORT 1023
162 * libcfs pseudo device operations
164 * struct cfs_psdev_t and
165 * cfs_psdev_register() and
166 * cfs_psdev_deregister() are declared in
167 * libcfs/<os>/<os>-prim.h
169 * It's just draft now.
172 struct cfs_psdev_file {
175 unsigned long reserved1;
176 unsigned long reserved2;
179 struct cfs_psdev_ops {
180 int (*p_open)(unsigned long, void *);
181 int (*p_close)(unsigned long, void *);
182 int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
183 int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
184 int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
188 * Universal memory allocator API
190 enum cfs_alloc_flags {
191 /* allocation is not allowed to block */
192 CFS_ALLOC_ATOMIC = 0x1,
193 /* allocation is allowed to block */
194 CFS_ALLOC_WAIT = 0x2,
195 /* allocation should return zeroed memory */
196 CFS_ALLOC_ZERO = 0x4,
197 /* allocation is allowed to call file-system code to free/clean
200 /* allocation is allowed to do io to free/clean memory */
202 /* don't report allocation failure to the console */
203 CFS_ALLOC_NOWARN = 0x20,
204 /* standard allocator flag combination */
205 CFS_ALLOC_STD = CFS_ALLOC_FS | CFS_ALLOC_IO,
206 CFS_ALLOC_USER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
207 CFS_ALLOC_NOFS = CFS_ALLOC_WAIT | CFS_ALLOC_IO,
208 CFS_ALLOC_KERNEL = CFS_ALLOC_WAIT | CFS_ALLOC_IO | CFS_ALLOC_FS,
211 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
212 enum cfs_alloc_page_flags {
213 /* allow to return page beyond KVM. It has to be mapped into KVM by
214 * cfs_kmap() and unmapped with cfs_kunmap(). */
215 CFS_ALLOC_HIGHMEM = 0x40,
216 CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO |
221 * Drop into debugger, if possible. Implementation is provided by platform.
224 void cfs_enter_debugger(void);
227 * Defined by platform
229 void cfs_daemonize(char *str);
230 int cfs_daemonize_ctxt(char *str);
231 cfs_sigset_t cfs_get_blocked_sigs(void);
232 cfs_sigset_t cfs_block_allsigs(void);
233 cfs_sigset_t cfs_block_sigs(unsigned long sigs);
234 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs);
235 void cfs_restore_sigs(cfs_sigset_t);
236 int cfs_signal_pending(void);
237 void cfs_clear_sigpending(void);
241 * these macros should be removed in the future,
242 * we keep them just for keeping libcfs compatible
243 * with other branches.
245 #define libcfs_daemonize(s) cfs_daemonize(s)
246 #define cfs_sigmask_lock(f) do { f= 0; } while (0)
247 #define cfs_sigmask_unlock(f) do { f= 0; } while (0)
249 int convert_server_error(__u64 ecode);
250 int convert_client_oflag(int cflag, int *result);
253 * Stack-tracing filling.
257 * Platform-dependent data-type to hold stack frames.
259 struct cfs_stack_trace;
262 * Fill @trace with current back-trace.
264 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
267 * Return instruction pointer for frame @frame_no. NULL if @frame_no is
270 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
273 #define O_NOACCESS O_NONBLOCK
277 * Universal open flags.
279 #define CFS_O_NOACCESS 0003
280 #define CFS_O_ACCMODE CFS_O_NOACCESS
281 #define CFS_O_CREAT 0100
282 #define CFS_O_EXCL 0200
283 #define CFS_O_NOCTTY 0400
284 #define CFS_O_TRUNC 01000
285 #define CFS_O_APPEND 02000
286 #define CFS_O_NONBLOCK 04000
287 #define CFS_O_NDELAY CFS_O_NONBLOCK
288 #define CFS_O_SYNC 010000
289 #define CFS_O_ASYNC 020000
290 #define CFS_O_DIRECT 040000
291 #define CFS_O_LARGEFILE 0100000
292 #define CFS_O_DIRECTORY 0200000
293 #define CFS_O_NOFOLLOW 0400000
294 #define CFS_O_NOATIME 01000000
296 /* convert local open flags to universal open flags */
297 int cfs_oflags2univ(int flags);
298 /* convert universal open flags to local open flags */
299 int cfs_univ2oflags(int flags);
302 * Random number handling
305 /* returns a random 32-bit integer */
306 unsigned int cfs_rand(void);
307 /* seed the generator */
308 void cfs_srand(unsigned int, unsigned int);
309 void cfs_get_random_bytes(void *buf, int size);
311 #include <libcfs/libcfs_debug.h>
312 #include <libcfs/libcfs_cpu.h>
313 #include <libcfs/libcfs_private.h>
314 #include <libcfs/libcfs_ioctl.h>
315 #include <libcfs/libcfs_prim.h>
316 #include <libcfs/libcfs_time.h>
317 #include <libcfs/libcfs_string.h>
318 #include <libcfs/libcfs_kernelcomm.h>
319 #include <libcfs/libcfs_workitem.h>
320 #include <libcfs/libcfs_hash.h>
321 #include <libcfs/libcfs_heap.h>
322 #include <libcfs/libcfs_fail.h>
323 #include <libcfs/params_tree.h>
324 #include <libcfs/libcfs_crypto.h>
326 /* container_of depends on "likely" which is defined in libcfs_private.h */
327 static inline void *__container_of(void *ptr, unsigned long shift)
329 if (unlikely(IS_ERR(ptr) || ptr == NULL))
332 return (char *)ptr - shift;
335 #define container_of0(ptr, type, member) \
336 ((type *)__container_of((void *)(ptr), offsetof(type, member)))
338 #define SET_BUT_UNUSED(a) do { } while(sizeof(a) - sizeof(a))
342 #endif /* _LIBCFS_H */