Whamcloud - gitweb
add debug mask for quota
[fs/lustre-release.git] / lnet / include / libcfs / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef __LIBCFS_LIBCFS_H__
5 #define __LIBCFS_LIBCFS_H__
6
7 #if !__GNUC__
8 #define __attribute__(x)
9 #endif
10
11 #if defined(__linux__)
12 #include <libcfs/linux/libcfs.h>
13 #elif defined(__APPLE__)
14 #include <libcfs/darwin/libcfs.h>
15 #else
16 #error Unsupported operating system.
17 #endif
18
19 #include "curproc.h"
20
21 #define PORTAL_DEBUG
22
23 #ifndef offsetof
24 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
25 #endif
26
27 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
28
29 /*
30  *  Debugging
31  */
32 extern unsigned int portal_subsystem_debug;
33 extern unsigned int portal_stack;
34 extern unsigned int portal_debug;
35 extern unsigned int portal_printk;
36
37 /*
38  * struct ptldebug_header is defined in libcfs/<os>/libcfs.h
39  */
40
41 #define PH_FLAG_FIRST_RECORD 1
42
43 /* Debugging subsystems (32 bits, non-overlapping) */
44 #define S_UNDEFINED   0x00000001
45 #define S_MDC         0x00000002
46 #define S_MDS         0x00000004
47 #define S_OSC         0x00000008
48 #define S_OST         0x00000010
49 #define S_CLASS       0x00000020
50 #define S_LOG         0x00000040
51 #define S_LLITE       0x00000080
52 #define S_RPC         0x00000100
53 #define S_MGMT        0x00000200
54 #define S_PORTALS     0x00000400
55 #define S_NAL         0x00000800 /* ALL NALs */
56 #define S_PINGER      0x00001000
57 #define S_FILTER      0x00002000
58 #define S_PTLBD       0x00004000
59 #define S_ECHO        0x00008000
60 #define S_LDLM        0x00010000
61 #define S_LOV         0x00020000
62 #define S_PTLROUTER   0x00040000
63 #define S_COBD        0x00080000
64 #define S_SM          0x00100000
65 #define S_ASOBD       0x00200000
66 #define S_CONFOBD     0x00400000
67 #define S_LMV         0x00800000
68 #define S_CMOBD       0x01000000
69 /* If you change these values, please keep these files up to date...
70  *    portals/utils/debug.c
71  *    utils/lconf
72  */
73
74 /* Debugging masks (32 bits, non-overlapping) */
75 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
76 #define D_INODE       0x00000002
77 #define D_SUPER       0x00000004
78 #define D_EXT2        0x00000008 /* anything from ext2_debug */
79 #define D_MALLOC      0x00000010 /* print malloc, free information */
80 #define D_CACHE       0x00000020 /* cache-related items */
81 #define D_INFO        0x00000040 /* general information */
82 #define D_IOCTL       0x00000080 /* ioctl related information */
83 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
84 #define D_NET         0x00000200 /* network communications */
85 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
86 #define D_BUFFS       0x00000800
87 #define D_OTHER       0x00001000
88 #define D_DENTRY      0x00002000
89 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
90 #define D_PAGE        0x00008000 /* bulk page handling */
91 #define D_DLMTRACE    0x00010000
92 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
93 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
94 #define D_HA          0x00080000 /* recovery and failover */
95 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
96 #define D_VFSTRACE    0x00200000
97 #define D_READA       0x00400000 /* read-ahead */
98 #define D_MMAP        0x00800000
99 #define D_CONFIG      0x01000000
100 #define D_CONSOLE     0x02000000
101 #define D_QUOTA       0x04000000
102 /* If you change these values, please keep these files up to date...
103  *    portals/utils/debug.c
104  *    utils/lconf
105  */
106
107 #ifndef DEBUG_SUBSYSTEM
108 # define DEBUG_SUBSYSTEM S_UNDEFINED
109 #endif
110
111 #if 1
112 #define CDEBUG(mask, format, a...)                                            \
113 do {                                                                          \
114         CHECK_STACK(CDEBUG_STACK);                                            \
115         if (((mask) & (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)) ||         \
116             (portal_debug & (mask) &&                                         \
117              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
118                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
119                                   __FILE__, __FUNCTION__, __LINE__,           \
120                                   CDEBUG_STACK, format, ## a);                \
121 } while (0)
122
123 #define CDEBUG_MAX_LIMIT 600
124 #define CDEBUG_LIMIT(cdebug_mask, cdebug_format, a...)                        \
125 do {                                                                          \
126         static cfs_time_t cdebug_next = 0;                                    \
127         static int cdebug_count = 0;                                          \
128         static cfs_duration_t cdebug_delay = CFS_MIN_DELAY;                   \
129                                                                               \
130         CHECK_STACK(CDEBUG_STACK);                                            \
131         if (cfs_time_after(cfs_time_current(), cdebug_next)) {                \
132                 portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask, __FILE__,     \
133                                   __FUNCTION__, __LINE__, CDEBUG_STACK,       \
134                                   cdebug_format, ## a);                       \
135                 if (cdebug_count) {                                           \
136                         portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask,       \
137                                           __FILE__, __FUNCTION__, __LINE__,   \
138                                           0, "skipped %d similar messages\n", \
139                                           cdebug_count);                      \
140                         cdebug_count = 0;                                     \
141                 }                                                             \
142                 if (cfs_time_after(cfs_time_current(),                        \
143                                    cdebug_next +                              \
144                                    cfs_time_seconds(CDEBUG_MAX_LIMIT+10)))    \
145                         cdebug_delay = cdebug_delay > (8 * CFS_MIN_DELAY)?    \
146                                        cdebug_delay/8 : CFS_MIN_DELAY;        \
147                 else                                                          \
148                         cdebug_delay = cdebug_delay*2 >= cfs_time_seconds(CDEBUG_MAX_LIMIT)?\
149                                        cfs_time_seconds(CDEBUG_MAX_LIMIT) :   \
150                                        cdebug_delay*2;                        \
151                 cdebug_next = cfs_time_current() + cdebug_delay;              \
152         } else {                                                              \
153                 portals_debug_msg(DEBUG_SUBSYSTEM,                            \
154                                   portal_debug & ~(D_EMERG|D_ERROR|D_WARNING),\
155                                   __FILE__, __FUNCTION__, __LINE__,           \
156                                   CDEBUG_STACK, cdebug_format, ## a);         \
157                 cdebug_count++;                                               \
158         }                                                                     \
159 } while (0)
160
161 #define CWARN(format, a...) CDEBUG_LIMIT(D_WARNING, format, ## a)
162 #define CERROR(format, a...) CDEBUG_LIMIT(D_ERROR, format, ## a)
163 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
164
165 #define LCONSOLE(mask, format, a...) CDEBUG(D_CONSOLE | (mask), format, ## a)
166 #define LCONSOLE_INFO(format, a...)  CDEBUG_LIMIT(D_CONSOLE, format, ## a)
167 #define LCONSOLE_WARN(format, a...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## a)
168 #define LCONSOLE_ERROR(format, a...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, format, ## a)
169 #define LCONSOLE_EMERG(format, a...) CDEBUG(D_CONSOLE | D_EMERG, format, ## a)
170
171 #define GOTO(label, rc)                                                 \
172 do {                                                                    \
173         long GOTO__ret = (long)(rc);                                    \
174         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
175                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
176                (signed long)GOTO__ret);                                 \
177         goto label;                                                     \
178 } while (0)
179
180 #define CDEBUG_ENTRY_EXIT (0)
181
182 #ifdef CDEBUG_ENTRY_EXIT
183
184 /*
185  * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
186  * there will be a warning in osx.
187  */
188 #define RETURN(rc)                                                      \
189 do {                                                                    \
190         typeof(rc) RETURN__ret = (rc);                                  \
191         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
192                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
193         EXIT_NESTING;                                                   \
194         return RETURN__ret;                                             \
195 } while (0)
196
197 #define ENTRY                                                           \
198 ENTRY_NESTING;                                                          \
199 do {                                                                    \
200         CDEBUG(D_TRACE, "Process entered\n");                           \
201 } while (0)
202
203 #define EXIT                                                            \
204 do {                                                                    \
205         CDEBUG(D_TRACE, "Process leaving\n");                           \
206         EXIT_NESTING;                                                   \
207 } while(0)
208 #else
209
210 #define RETURN(rc) return (rc)
211 #define ENTRY
212 #define EXIT
213
214 #endif
215
216 #else
217 #define CDEBUG(mask, format, a...)      do { } while (0)
218 #define CWARN(format, a...)             printk(KERN_WARNING format, ## a)
219 #define CERROR(format, a...)            printk(KERN_ERR format, ## a)
220 #define CEMERG(format, a...)            printk(KERN_EMERG format, ## a)
221 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
222 #define RETURN(rc)                      return (rc)
223 #define ENTRY                           do { } while (0)
224 #define EXIT                            do { } while (0)
225 #endif
226
227 #define LUSTRE_SRV_PTL_PID      LUSTRE_PTL_PID
228
229 #define PORTALS_CFG_VERSION 0x00010001;
230
231 struct portals_cfg {
232         __u32 pcfg_version;
233         __u32 pcfg_command;
234
235         __u32 pcfg_nal;
236         __u32 pcfg_flags;
237
238         __u32 pcfg_gw_nal;
239         __u64 pcfg_nid;
240         __u64 pcfg_nid2;
241         __u64 pcfg_nid3;
242         __u32 pcfg_id;
243         __u32 pcfg_misc;
244         __u32 pcfg_fd;
245         __u32 pcfg_count;
246         __u32 pcfg_size;
247         __u32 pcfg_wait;
248
249         __u32 pcfg_plen1; /* buffers in userspace */
250         char *pcfg_pbuf1;
251         __u32 pcfg_plen2; /* buffers in userspace */
252         char *pcfg_pbuf2;
253 };
254
255 #define PCFG_INIT(pcfg, cmd)                            \
256 do {                                                    \
257         memset(&pcfg, 0, sizeof(pcfg));                 \
258         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
259         pcfg.pcfg_command = (cmd);                      \
260                                                         \
261 } while (0)
262
263 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
264 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
265 int libcfs_nal_cmd(struct portals_cfg *pcfg);
266 void libcfs_nal_cmd_unregister(int nal);
267
268 struct portal_ioctl_data {
269         __u32 ioc_len;
270         __u32 ioc_version;
271         __u64 ioc_nid;
272         __u64 ioc_nid2;
273         __u64 ioc_nid3;
274         __u32 ioc_count;
275         __u32 ioc_nal;
276         __u32 ioc_nal_cmd;
277         __u32 ioc_fd;
278         __u32 ioc_id;
279
280         __u32 ioc_flags;
281         __u32 ioc_size;
282
283         __u32 ioc_wait;
284         __u32 ioc_timeout;
285         __u32 ioc_misc;
286
287         __u32 ioc_inllen1;
288         char *ioc_inlbuf1;
289         __u32 ioc_inllen2;
290         char *ioc_inlbuf2;
291
292         __u32 ioc_plen1; /* buffers in userspace */
293         char *ioc_pbuf1;
294         __u32 ioc_plen2; /* buffers in userspace */
295         char *ioc_pbuf2;
296
297         char ioc_bulk[0];
298 };
299
300
301 #ifdef __KERNEL__
302
303 #include <libcfs/list.h>
304
305 struct libcfs_ioctl_handler {
306         struct list_head item;
307         int (*handle_ioctl)(struct portal_ioctl_data *data,
308                             unsigned int cmd, unsigned long args);
309 };
310
311 #define DECLARE_IOCTL_HANDLER(ident, func)              \
312         struct libcfs_ioctl_handler ident = {           \
313                 .item = CFS_LIST_HEAD_INIT(ident.item),     \
314                 .handle_ioctl = func                    \
315         }
316
317 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
318 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
319
320 /* libcfs watchdogs */
321 struct lc_watchdog;
322
323 /* Just use the default handler (dumplog)  */
324 #define LC_WATCHDOG_DEFAULT_CB NULL
325
326 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
327  * touch it once to enable it. */
328 struct lc_watchdog *lc_watchdog_add(int time,
329                                     void (*cb)(struct lc_watchdog *,
330                                                struct task_struct *,
331                                                void *),
332                                     void *data);
333
334 /* Enables a watchdog and resets its timer. */
335 void lc_watchdog_touch(struct lc_watchdog *lcw);
336
337 /* Disable a watchdog; touch it to restart it. */
338 void lc_watchdog_disable(struct lc_watchdog *lcw);
339
340 /* Clean up the watchdog */
341 void lc_watchdog_delete(struct lc_watchdog *lcw);
342
343 /* Dump a debug log */
344 void lc_watchdog_dumplog(struct lc_watchdog *lcw,
345                          struct task_struct *tsk,
346                          void *data);
347
348 /* __KERNEL__ */
349 #endif
350
351 /*
352  * libcfs pseudo device operations
353  *
354  * struct cfs_psdev_t and
355  * cfs_psdev_register() and
356  * cfs_psdev_deregister() are declared in
357  * libcfs/<os>/cfs_prim.h
358  *
359  * It's just draft now.
360  */
361
362 struct cfs_psdev_file {
363         unsigned long   off;
364         void            *private_data;
365         unsigned long   reserved1;
366         unsigned long   reserved2;
367 };
368
369 struct cfs_psdev_ops {
370         int (*p_open)(unsigned long, void *);
371         int (*p_close)(unsigned long, void *);
372         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
373         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
374         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
375 };
376
377 /*
378  * generic time manipulation functions.
379  */
380
381 static inline int cfs_time_after(cfs_time_t t1, cfs_time_t t2)
382 {
383         return cfs_time_before(t2, t1);
384 }
385
386 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
387 {
388         return cfs_time_beforeq(t2, t1);
389 }
390
391 /*
392  * return seconds since UNIX epoch
393  */
394 static inline time_t cfs_unix_seconds(void)
395 {
396         cfs_fs_time_t t;
397
398         cfs_fs_time_current(&t);
399         return cfs_fs_time_sec(&t);
400 }
401
402 #define CFS_RATELIMIT(seconds)                                  \
403 ({                                                              \
404         /*                                                      \
405          * XXX nikita: non-portable initializer                 \
406          */                                                     \
407         static time_t __next_message = 0;                       \
408         int result;                                             \
409                                                                 \
410         if (cfs_time_after(cfs_time_current(), __next_message)) \
411                 result = 1;                                     \
412         else {                                                  \
413                 __next_message = cfs_time_shift(seconds);       \
414                 result = 0;                                     \
415         }                                                       \
416         result;                                                 \
417 })
418
419 extern void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
420                               const int line, unsigned long stack,
421                               char *format, ...)
422             __attribute__ ((format (printf, 7, 8)));
423
424 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
425 {
426         if (cfs_time_after(cfs_time_current(),
427                            cfs_time_add(now, cfs_time_seconds(15))))
428                 CERROR("slow %s %lu sec\n", msg,
429                        cfs_duration_sec(cfs_time_sub(cfs_time_current(), now)));
430 }
431
432 /*
433  * helper function similar to do_gettimeofday() of Linux kernel
434  */
435 static inline void cfs_fs_timeval(struct timeval *tv)
436 {
437         cfs_fs_time_t time;
438
439         cfs_fs_time_current(&time);
440         cfs_fs_time_usec(&time, tv);
441 }
442
443 /*
444  * return valid time-out based on user supplied one. Currently we only check
445  * that time-out is not shorted than allowed.
446  */
447 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
448 {
449         if (timeout < cfs_time_minimal_timeout())
450                 timeout = cfs_time_minimal_timeout();
451         return timeout;
452 }
453
454 /*
455  * Portable memory allocator API (draft)
456  */
457 enum cfs_alloc_flags {
458         /* allocation is not allowed to block */
459         CFS_ALLOC_ATOMIC = (1 << 0),
460         /* allocation is allowed to block */
461         CFS_ALLOC_WAIT = (1 << 1),
462         /* allocation should return zeroed memory */
463         CFS_ALLOC_ZERO   = (1 << 2),
464         /* allocation is allowed to call file-system code to free/clean
465          * memory */
466         CFS_ALLOC_FS     = (1 << 3),
467         /* allocation is allowed to do io to free/clean memory */
468         CFS_ALLOC_IO     = (1 << 4),
469         /* standard allocator flag combination */
470         CFS_ALLOC_STD    = CFS_ALLOC_FS | CFS_ALLOC_IO,
471         CFS_ALLOC_USER   = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
472 };
473
474 #define CFS_SLAB_ATOMIC         CFS_ALLOC_ATOMIC
475 #define CFS_SLAB_WAIT           CFS_ALLOC_WAIT
476 #define CFS_SLAB_ZERO           CFS_ALLOC_ZERO
477 #define CFS_SLAB_FS             CFS_ALLOC_FS
478 #define CFS_SLAB_IO             CFS_ALLOC_IO
479 #define CFS_SLAB_STD            CFS_ALLOC_STD
480 #define CFS_SLAB_USER           CFS_ALLOC_USER
481
482 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
483 enum cfs_page_alloc_flags {
484         /* allow to return page beyond KVM. It has to be mapped into KVM by
485          * cfs_page_map(); */
486         CFS_ALLOC_HIGH   = (1 << 5),
487         CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
488 };
489
490
491 #define _LIBCFS_H
492
493 #endif /* _LIBCFS_H */