Whamcloud - gitweb
99d3877059f0caeaf08fdb34c67bb9f788b8b382
[fs/lustre-release.git] / lustre / portals / include / linux / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _LIBCFS_H
5
6
7 #define PORTAL_DEBUG
8
9 #ifdef __linux__
10 # include <asm/types.h>
11 # if defined(__powerpc__) && !defined(__KERNEL__)
12 #  define __KERNEL__
13 #  include <asm/timex.h>
14 #  undef __KERNEL__
15 # else
16 #  include <asm/timex.h>
17 # endif
18 #else
19 # include <sys/types.h>
20 typedef u_int32_t __u32;
21 typedef u_int64_t __u64;
22 #endif
23
24 #ifdef __KERNEL__
25 # include <linux/time.h>
26 #else
27 # include <sys/time.h>
28 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
29 #endif
30
31 #ifndef offsetof
32 # define offsetof(typ,memb)     ((int)((char *)&(((typ *)0)->memb)))
33 #endif
34
35 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
36
37 /*
38  *  Debugging
39  */
40 extern unsigned int portal_subsystem_debug;
41 extern unsigned int portal_stack;
42 extern unsigned int portal_debug;
43 extern unsigned int portal_printk;
44 extern unsigned int portal_cerror;
45 /* Debugging subsystems (32 bits, non-overlapping) */
46 #define S_UNDEFINED   0x00000001
47 #define S_MDC         0x00000002
48 #define S_MDS         0x00000004
49 #define S_OSC         0x00000008
50 #define S_OST         0x00000010
51 #define S_CLASS       0x00000020
52 #define S_LOG         0x00000040
53 #define S_LLITE       0x00000080
54 #define S_RPC         0x00000100
55 #define S_MGMT        0x00000200
56 #define S_PORTALS     0x00000400
57 #define S_SOCKNAL     0x00000800
58 #define S_QSWNAL      0x00001000
59 #define S_PINGER      0x00002000
60 #define S_FILTER      0x00004000
61 #define S_PTLBD       0x00008000
62 #define S_ECHO        0x00010000
63 #define S_LDLM        0x00020000
64 #define S_LOV         0x00040000
65 #define S_GMNAL       0x00080000
66 #define S_PTLROUTER   0x00100000
67 #define S_COBD        0x00200000
68 #define S_IBNAL       0x00400000
69
70 /* If you change these values, please keep portals/utils/debug.c
71  * up to date! */
72
73 /* Debugging masks (32 bits, non-overlapping) */
74 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
75 #define D_INODE       0x00000002
76 #define D_SUPER       0x00000004
77 #define D_EXT2        0x00000008 /* anything from ext2_debug */
78 #define D_MALLOC      0x00000010 /* print malloc, free information */
79 #define D_CACHE       0x00000020 /* cache-related items */
80 #define D_INFO        0x00000040 /* general information */
81 #define D_IOCTL       0x00000080 /* ioctl related information */
82 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
83 #define D_NET         0x00000200 /* network communications */
84 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
85 #define D_BUFFS       0x00000800
86 #define D_OTHER       0x00001000
87 #define D_DENTRY      0x00002000
88 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
89 #define D_PAGE        0x00008000 /* bulk page handling */
90 #define D_DLMTRACE    0x00010000
91 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
92 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
93 #define D_HA          0x00080000 /* recovery and failover */
94 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
95 #define D_VFSTRACE    0x00200000
96 #define D_READA       0x00400000 /* read-ahead */
97
98 #ifdef __KERNEL__
99 # include <linux/sched.h> /* THREAD_SIZE */
100 #else
101 # ifndef THREAD_SIZE /* x86_64 has THREAD_SIZE in userspace */
102 #  define THREAD_SIZE 8192
103 # endif
104 #endif
105
106 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
107
108 #ifdef __KERNEL__
109 # ifdef  __ia64__
110 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
111                         ((unsigned long)__builtin_dwarf_cfa() &            \
112                          (THREAD_SIZE - 1)))
113 # else
114 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
115                         ((unsigned long)__builtin_frame_address(0) &       \
116                          (THREAD_SIZE - 1)))
117 # endif
118
119 #define CHECK_STACK(stack)                                                    \
120         do {                                                                  \
121                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
122                         portals_debug_msg(DEBUG_SUBSYSTEM, D_WARNING,         \
123                                           __FILE__, __FUNCTION__, __LINE__,   \
124                                           (stack),"maximum lustre stack %u\n",\
125                                           portal_stack = (stack));            \
126                       /*panic("LBUG");*/                                      \
127                 }                                                             \
128         } while (0)
129 #else /* __KERNEL__ */
130 #define CHECK_STACK(stack) do { } while(0)
131 #define CDEBUG_STACK (0L)
132 #endif /* __KERNEL__ */
133
134 #if 1
135 #define CDEBUG(mask, format, a...)                                            \
136 do {                                                                          \
137         if (portal_cerror == 0)                                               \
138                 break;                                                        \
139         CHECK_STACK(CDEBUG_STACK);                                            \
140         if (((mask) & (D_ERROR | D_EMERG | D_WARNING)) ||                     \
141             (portal_debug & (mask) &&                                         \
142              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
143                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
144                                   __FILE__, __FUNCTION__, __LINE__,           \
145                                   CDEBUG_STACK, format, ## a);                \
146 } while (0)
147
148 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
149 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
150 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
151
152 #define GOTO(label, rc)                                                 \
153 do {                                                                    \
154         long GOTO__ret = (long)(rc);                                    \
155         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
156                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
157                (signed long)GOTO__ret);                                 \
158         goto label;                                                     \
159 } while (0)
160
161 #define RETURN(rc)                                                      \
162 do {                                                                    \
163         typeof(rc) RETURN__ret = (rc);                                  \
164         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
165                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
166         return RETURN__ret;                                             \
167 } while (0)
168
169 #define ENTRY                                                           \
170 do {                                                                    \
171         CDEBUG(D_TRACE, "Process entered\n");                           \
172 } while (0)
173
174 #define EXIT                                                            \
175 do {                                                                    \
176         CDEBUG(D_TRACE, "Process leaving\n");                           \
177 } while(0)
178 #else
179 #define CDEBUG(mask, format, a...)      do { } while (0)
180 #define CWARN(format, a...)             do { } while (0)
181 #define CERROR(format, a...)            printk("<3>" format, ## a)
182 #define CEMERG(format, a...)            printk("<0>" format, ## a)
183 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
184 #define RETURN(rc)                      return (rc)
185 #define ENTRY                           do { } while (0)
186 #define EXIT                            do { } while (0)
187 #endif
188
189 #define PORTALS_CFG_VERSION 0x00010001;
190
191 struct portals_cfg {
192         __u32 pcfg_version;
193         __u32 pcfg_command;
194
195         __u32 pcfg_nal;
196         __u32 pcfg_flags;
197
198         __u32 pcfg_gw_nal;
199         __u64 pcfg_nid;
200         __u64 pcfg_nid2;
201         __u64 pcfg_nid3;
202         __u32 pcfg_id;
203         __u32 pcfg_misc;
204         __u32 pcfg_fd;
205         __u32 pcfg_count;
206         __u32 pcfg_size;
207         __u32 pcfg_wait;
208
209         __u32 pcfg_plen1; /* buffers in userspace */
210         char *pcfg_pbuf1;
211         __u32 pcfg_plen2; /* buffers in userspace */
212         char *pcfg_pbuf2;
213 };
214
215 #define PCFG_INIT(pcfg, cmd)                            \
216 do {                                                    \
217         memset(&pcfg, 0, sizeof(pcfg));                 \
218         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
219         pcfg.pcfg_command = (cmd);                      \
220                                                         \
221 } while (0)
222
223 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
224 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
225 int libcfs_nal_cmd(struct portals_cfg *pcfg);
226 void libcfs_nal_cmd_unregister(int nal);
227
228 struct portal_ioctl_data {
229         __u32 ioc_len;
230         __u32 ioc_version;
231         __u64 ioc_nid;
232         __u64 ioc_nid2;
233         __u64 ioc_nid3;
234         __u32 ioc_count;
235         __u32 ioc_nal;
236         __u32 ioc_nal_cmd;
237         __u32 ioc_fd;
238         __u32 ioc_id;
239
240         __u32 ioc_flags;
241         __u32 ioc_size;
242
243         __u32 ioc_wait;
244         __u32 ioc_timeout;
245         __u32 ioc_misc;
246
247         __u32 ioc_inllen1;
248         char *ioc_inlbuf1;
249         __u32 ioc_inllen2;
250         char *ioc_inlbuf2;
251
252         __u32 ioc_plen1; /* buffers in userspace */
253         char *ioc_pbuf1;
254         __u32 ioc_plen2; /* buffers in userspace */
255         char *ioc_pbuf2;
256
257         char ioc_bulk[0];
258 };
259
260
261 #ifdef __KERNEL__
262
263 #include <linux/list.h>
264
265 struct libcfs_ioctl_handler {
266         struct list_head item;
267         int (*handle_ioctl)(struct portal_ioctl_data *data,
268                             unsigned int cmd, unsigned long args);
269 };
270
271 #define DECLARE_IOCTL_HANDLER(ident, func)              \
272         struct libcfs_ioctl_handler ident = {           \
273                 .item = LIST_HEAD_INIT(ident.item),     \
274                 .handle_ioctl = func                    \
275         }
276
277 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
278 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
279
280 #endif
281
282 #define _LIBCFS_H
283
284 #endif /* _LIBCFS_H */