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, 2014, 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__
40 #include <libcfs/types.h>
41 #include <libcfs/list.h>
44 # include <libcfs/linux/libcfs.h>
45 #else /* !__KERNEL__ */
60 # include <sys/ioctl.h>
61 # include <sys/socket.h>
62 # include <sys/stat.h>
63 # include <sys/time.h>
64 # include <sys/types.h>
65 # include <libcfs/user-time.h>
66 # include <libcfs/user-prim.h>
67 # include <libcfs/user-bitops.h>
68 #endif /* __KERNEL__ */
73 #define ARRAY_SIZE(a) ((sizeof (a)) / (sizeof ((a)[0])))
77 #define swap(x,y) do { typeof(x) z = x; x = y; y = z; } while (0)
80 #if !defined(container_of)
81 /* given a pointer @ptr to the field @member embedded into type (usually
82 * struct) @type, return pointer to the embedding instance of @type. */
83 #define container_of(ptr, type, member) \
84 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
87 static inline int __is_po2(unsigned long long val)
89 return !(val & (val - 1));
92 #define IS_PO2(val) __is_po2((unsigned long long)(val))
94 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
96 /* Sparse annotations */
98 # if !defined(__must_hold)
100 # define __must_hold(x) __attribute__((context(x, 1, 1)))
101 # else /* __CHECKER__ */
102 # define __must_hold(x)
103 # endif /* !__CHECKER__ */
104 # endif /* !__must_hold */
105 #else /* __KERNEL__ */
106 # define __acquires(x)
107 # define __releases(x)
108 # define __must_hold(x)
109 #endif /* !__KERNEL__ */
112 * Lustre Error Checksum: calculates checksum
113 * of Hex number by XORing each bit.
115 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
116 ((hexnum) >> 8 & 0xf))
119 * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
120 * Lustre RETURN(NULL) macro.
126 #define NULL ((void *)0)
130 #ifndef cfs_for_each_possible_cpu
131 # error cfs_for_each_possible_cpu is not supported by kernel!
134 /* libcfs watchdogs */
137 /* Add a watchdog which fires after "time" milliseconds of delay. You have to
138 * touch it once to enable it. */
139 struct lc_watchdog *lc_watchdog_add(int time,
140 void (*cb)(pid_t pid, void *),
143 /* Enables a watchdog and resets its timer. */
144 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout);
145 #define CFS_GET_TIMEOUT(svc) (max_t(int, obd_timeout, \
146 AT_OFF ? 0 : at_get(&svc->srv_at_estimate)) * \
147 svc->srv_watchdog_factor)
149 /* Disable a watchdog; touch it to restart it. */
150 void lc_watchdog_disable(struct lc_watchdog *lcw);
152 /* Clean up the watchdog */
153 void lc_watchdog_delete(struct lc_watchdog *lcw);
155 /* Dump a debug log */
156 void lc_watchdog_dumplog(pid_t pid, void *data);
158 #endif /* __KERNEL__ */
160 /* need both kernel and user-land acceptor */
161 #define LNET_ACCEPTOR_MIN_RESERVED_PORT 512
162 #define LNET_ACCEPTOR_MAX_RESERVED_PORT 1023
165 * libcfs pseudo device operations
167 * struct struct miscdevice and
168 * misc_register() and
169 * misc_deregister() are declared in
170 * libcfs/<os>/<os>-prim.h
172 * It's just draft now.
175 struct cfs_psdev_file {
178 unsigned long reserved1;
179 unsigned long reserved2;
182 struct cfs_psdev_ops {
183 int (*p_open)(unsigned long, void *);
184 int (*p_close)(unsigned long, void *);
185 int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
186 int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
187 int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void __user *);
191 * Drop into debugger, if possible. Implementation is provided by platform.
194 void cfs_enter_debugger(void);
197 * Defined by platform
199 int unshare_fs_struct(void);
200 sigset_t cfs_block_allsigs(void);
201 sigset_t cfs_block_sigs(unsigned long sigs);
202 sigset_t cfs_block_sigsinv(unsigned long sigs);
203 void cfs_restore_sigs(sigset_t);
204 int cfs_signal_pending(void);
205 void cfs_clear_sigpending(void);
207 int convert_server_error(__u64 ecode);
208 int convert_client_oflag(int cflag, int *result);
211 * Stack-tracing filling.
215 * Platform-dependent data-type to hold stack frames.
217 struct cfs_stack_trace;
220 * Fill @trace with current back-trace.
222 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
225 * Return instruction pointer for frame @frame_no. NULL if @frame_no is
228 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
231 * Random number handling
234 /* returns a random 32-bit integer */
235 unsigned int cfs_rand(void);
236 /* seed the generator */
237 void cfs_srand(unsigned int, unsigned int);
238 void cfs_get_random_bytes(void *buf, int size);
240 #include <libcfs/byteorder.h>
241 #include <libcfs/err.h>
242 #include <libcfs/libcfs_debug.h>
243 #include <libcfs/libcfs_private.h>
244 #include <libcfs/bitmap.h>
246 # include <libcfs/libcfs_cpu.h>
247 # include <libcfs/libcfs_ioctl.h>
248 # include <libcfs/libcfs_prim.h>
249 #endif /* __KERNEL__ */
250 #include <libcfs/libcfs_time.h>
252 # include <libcfs/libcfs_string.h>
254 #include <libcfs/libcfs_workitem.h>
256 # include <libcfs/libcfs_hash.h>
257 #endif /* __KERNEL__ */
258 #include <libcfs/libcfs_heap.h>
259 #include <libcfs/libcfs_fail.h>
261 /* container_of depends on "likely" which is defined in libcfs_private.h */
262 static inline void *__container_of(const void *ptr, unsigned long shift)
264 if (unlikely(IS_ERR(ptr) || ptr == NULL))
265 return ERR_CAST(ptr);
267 return (char *)ptr - shift;
270 #define container_of0(ptr, type, member) \
271 ((type *)__container_of((ptr), offsetof(type, member)))
275 #endif /* _LIBCFS_H */