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))
98 typedef atomic_t cfs_atomic_t;
100 #define cfs_atomic_read(atom) atomic_read(atom)
101 #define cfs_atomic_inc(atom) atomic_inc(atom)
102 #define cfs_atomic_inc_and_test(atom) atomic_inc_and_test(atom)
103 #define cfs_atomic_inc_return(atom) atomic_inc_return(atom)
104 #define cfs_atomic_inc_not_zero(atom) atomic_inc_not_zero(atom)
105 #define cfs_atomic_add_unless(atom, a, u) atomic_add_unless(atom, a, u)
106 #define cfs_atomic_dec(atom) atomic_dec(atom)
107 #define cfs_atomic_dec_and_test(atom) atomic_dec_and_test(atom)
108 #define cfs_atomic_dec_and_lock(atom, lock) atomic_dec_and_lock(atom, lock)
109 #define cfs_atomic_dec_return(atom) atomic_dec_return(atom)
110 #define cfs_atomic_set(atom, value) atomic_set(atom, value)
111 #define cfs_atomic_add(value, atom) atomic_add(value, atom)
112 #define cfs_atomic_add_return(value, atom) atomic_add_return(value, atom)
113 #define cfs_atomic_sub(value, atom) atomic_sub(value, atom)
114 #define cfs_atomic_sub_and_test(value, atom) atomic_sub_and_test(value, atom)
115 #define cfs_atomic_sub_return(value, atom) atomic_sub_return(value, atom)
116 #define cfs_atomic_cmpxchg(atom, old, nv) atomic_cmpxchg(atom, old, nv)
117 #define CFS_ATOMIC_INIT(i) ATOMIC_INIT(i)
120 * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
121 * Lustre RETURN(NULL) macro.
127 #define NULL ((void *)0)
129 #define LUSTRE_SRV_LNET_PID LUSTRE_LNET_PID
133 #include <libcfs/list.h>
135 #ifndef cfs_for_each_possible_cpu
136 # error cfs_for_each_possible_cpu is not supported by kernel!
140 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
141 int libcfs_ipif_enumerate(char ***names);
142 void libcfs_ipif_free_enumeration(char **names, int n);
143 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
144 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
145 void libcfs_sock_abort_accept(cfs_socket_t *sock);
146 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
147 __u32 local_ip, int local_port,
148 __u32 peer_ip, int peer_port);
149 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
150 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
151 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
152 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
153 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
154 void libcfs_sock_release(cfs_socket_t *sock);
156 /* libcfs watchdogs */
159 /* Add a watchdog which fires after "time" milliseconds of delay. You have to
160 * touch it once to enable it. */
161 struct lc_watchdog *lc_watchdog_add(int time,
162 void (*cb)(pid_t pid, void *),
165 /* Enables a watchdog and resets its timer. */
166 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
167 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout, \
168 AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
169 svc->srv_watchdog_factor)
171 /* Disable a watchdog; touch it to restart it. */
172 void lc_watchdog_disable(struct lc_watchdog *lcw);
174 /* Clean up the watchdog */
175 void lc_watchdog_delete(struct lc_watchdog *lcw);
177 /* Dump a debug log */
178 void lc_watchdog_dumplog(pid_t pid, void *data);
180 #else /* !__KERNEL__ */
183 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
185 #endif /* !__KERNEL__ */
187 /* need both kernel and user-land acceptor */
188 #define LNET_ACCEPTOR_MIN_RESERVED_PORT 512
189 #define LNET_ACCEPTOR_MAX_RESERVED_PORT 1023
192 * libcfs pseudo device operations
194 * struct struct miscdevice and
195 * misc_register() and
196 * misc_deregister() are declared in
197 * libcfs/<os>/<os>-prim.h
199 * It's just draft now.
202 struct cfs_psdev_file {
205 unsigned long reserved1;
206 unsigned long reserved2;
209 struct cfs_psdev_ops {
210 int (*p_open)(unsigned long, void *);
211 int (*p_close)(unsigned long, void *);
212 int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
213 int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
214 int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
218 * Drop into debugger, if possible. Implementation is provided by platform.
221 void cfs_enter_debugger(void);
224 * Defined by platform
226 int unshare_fs_struct(void);
227 sigset_t cfs_get_blocked_sigs(void);
228 sigset_t cfs_block_allsigs(void);
229 sigset_t cfs_block_sigs(unsigned long sigs);
230 sigset_t cfs_block_sigsinv(unsigned long sigs);
231 void cfs_restore_sigs(sigset_t);
232 int cfs_signal_pending(void);
233 void cfs_clear_sigpending(void);
235 int convert_server_error(__u64 ecode);
236 int convert_client_oflag(int cflag, int *result);
239 * Stack-tracing filling.
243 * Platform-dependent data-type to hold stack frames.
245 struct cfs_stack_trace;
248 * Fill @trace with current back-trace.
250 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
253 * Return instruction pointer for frame @frame_no. NULL if @frame_no is
256 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
259 #define O_NOACCESS O_NONBLOCK
263 * Universal open flags.
265 #define CFS_O_NOACCESS 0003
266 #define CFS_O_ACCMODE CFS_O_NOACCESS
267 #define CFS_O_CREAT 0100
268 #define CFS_O_EXCL 0200
269 #define CFS_O_NOCTTY 0400
270 #define CFS_O_TRUNC 01000
271 #define CFS_O_APPEND 02000
272 #define CFS_O_NONBLOCK 04000
273 #define CFS_O_NDELAY CFS_O_NONBLOCK
274 #define CFS_O_SYNC 010000
275 #define CFS_O_ASYNC 020000
276 #define CFS_O_DIRECT 040000
277 #define CFS_O_LARGEFILE 0100000
278 #define CFS_O_DIRECTORY 0200000
279 #define CFS_O_NOFOLLOW 0400000
280 #define CFS_O_NOATIME 01000000
282 /* convert local open flags to universal open flags */
283 int cfs_oflags2univ(int flags);
284 /* convert universal open flags to local open flags */
285 int cfs_univ2oflags(int flags);
288 * Random number handling
291 /* returns a random 32-bit integer */
292 unsigned int cfs_rand(void);
293 /* seed the generator */
294 void cfs_srand(unsigned int, unsigned int);
295 void cfs_get_random_bytes(void *buf, int size);
297 #include <libcfs/libcfs_debug.h>
298 #include <libcfs/libcfs_cpu.h>
299 #include <libcfs/libcfs_private.h>
300 #include <libcfs/libcfs_ioctl.h>
301 #include <libcfs/libcfs_prim.h>
302 #include <libcfs/libcfs_time.h>
303 #include <libcfs/libcfs_string.h>
304 #include <libcfs/libcfs_kernelcomm.h>
305 #include <libcfs/libcfs_workitem.h>
306 #include <libcfs/libcfs_hash.h>
307 #include <libcfs/libcfs_heap.h>
308 #include <libcfs/libcfs_fail.h>
309 #include <libcfs/params_tree.h>
310 #include <libcfs/libcfs_crypto.h>
312 /* container_of depends on "likely" which is defined in libcfs_private.h */
313 static inline void *__container_of(void *ptr, unsigned long shift)
315 if (unlikely(IS_ERR(ptr) || ptr == NULL))
318 return (char *)ptr - shift;
321 #define container_of0(ptr, type, member) \
322 ((type *)__container_of((void *)(ptr), offsetof(type, member)))
326 #endif /* _LIBCFS_H */