Whamcloud - gitweb
a0b996c0a61a76df9e896ae5a084f866f2fc319b
[fs/lustre-release.git] / lustre / include / linux / obd_support.h
1 #ifndef _OBD_SUPPORT
2 #define _OBD_SUPPORT
3 #include <linux/malloc.h>
4 #include <linux/vmalloc.h>
5
6 #define MIN(a,b) (((a)<(b)) ? (a): (b))
7 #define MAX(a,b) (((a)>(b)) ? (a): (b))
8
9 #define obd_unlock_page(page)   do {    if (PageLocked(page)) { \
10                         UnlockPage(page);\
11                 } else {\
12                         printk("file %s, line %d: expecting locked page\n",\
13                                __FILE__, __LINE__); \
14                 }                       \
15 } while(0)
16
17 /*
18  * Debug code
19  */
20 /* global variables */
21 extern int obd_debug_level;
22 extern int obd_print_entry;
23 extern int obd_inodes;
24 extern int obd_pages;
25 extern long obd_memory;
26
27 #define EXT2_OBD_DEBUG
28
29 #ifdef EXT2_OBD_DEBUG
30 #define CMD(cmd) (( cmd == READ ) ? "read" : "write")
31
32 /* debugging masks */
33 #define D_PSDEV   0x001 /* debug information from psdev.c */
34 #define D_INODE   0x002
35 #define D_SUPER   0x004
36 #define D_SNAP    0x008
37 #define D_UNUSED4 0x010
38 #define D_WARNING 0x020 /* misc warnings */
39 #define D_EXT2    0x040 /* anything from ext2_debug */
40 #define D_MALLOC  0x080 /* print malloc, free information */
41 #define D_CACHE   0x100 /* cache-related items */
42 #define D_INFO    0x200 /* general information, especially from interface.c */
43 #define D_IOCTL   0x400 /* ioctl related information */
44 #define D_BLOCKS  0x800 /* ext2 block allocation */
45 #define D_RPC    0x1000 /* rpc communications */
46 #define D_PUNCH  0x2000
47  
48 #define CDEBUG(mask, format, a...)                                      \
49         do {                                                            \
50         if (obd_debug_level & mask) {                                   \
51                 printk("(%s:%d):", __FUNCTION__, __LINE__);             \
52                 printk(format, ## a); }                                 \
53         } while (0)
54
55 #define ENTRY if (obd_print_entry)                                      \
56         printk(KERN_INFO "Process %d entered %s\n", current->pid, __FUNCTION__)
57
58 #define EXIT if (obd_print_entry)                                       \
59         printk(KERN_INFO "Process %d leaving %s [%d]\n", current->pid,  \
60                __FUNCTION__, __LINE__)
61
62 /* Inode common information printed out (used by obdfs and ext2obd inodes) */
63 #define ICDEBUG(inode) {                                                \
64         CDEBUG(D_INFO,                                                  \
65                "ino %ld, atm %ld, mtm %ld, ctm %ld, size %Ld, blocks %ld\n",\
66                inode->i_ino, inode->i_atime, inode->i_mtime, inode->i_ctime,\
67                inode->i_size, inode->i_blocks);                         \
68         CDEBUG(D_INFO, "mode %o, uid %d, gid %d, nlnk %d, count %d\n",  \
69                inode->i_mode, inode->i_uid, inode->i_gid, inode->i_nlink,\
70                atomic_read(&inode->i_count));                                         \
71 }
72
73 /* Ext2 inode information */
74 #define EXDEBUG(inode) {                                                \
75         ICDEBUG(inode);                                                 \
76         CDEBUG(D_INFO, "ext2 blocks: %d %d %d %d %d %d %d %d\n",        \
77                inode->u.ext2_i.i_data[0], inode->u.ext2_i.i_data[1],    \
78                inode->u.ext2_i.i_data[2], inode->u.ext2_i.i_data[3],    \
79                inode->u.ext2_i.i_data[4], inode->u.ext2_i.i_data[5],    \
80                inode->u.ext2_i.i_data[6], inode->u.ext2_i.i_data[7]);   \
81 }
82
83 /* OBDFS inode information */
84 #define OIDEBUG(inode) {                                                \
85         ICDEBUG(inode);                                                 \
86         CDEBUG(D_INFO,"oinfo: flags 0x%08x\n", obdfs_i2info(inode)->oi_flags);\
87         /* obdfs_print_plist(inode); */                                 \
88 }
89
90 #define ODEBUG(obdo) {                                                  \
91         CDEBUG(D_INFO, "id %ld, atm %ld, mtm %ld, ctm %ld, "            \
92                "size %ld, blocks %ld\n",                                \
93                (long)(obdo)->o_id, (long)(obdo)->o_atime,               \
94                (long)(obdo)->o_mtime, (long)(obdo)->o_ctime,            \
95                (long)(obdo)->o_size, (long)(obdo)->o_blocks);           \
96         CDEBUG(D_INFO, " mode %o, uid %d, gid %d, flg 0x%0x, "          \
97                "obdflg 0x%0x, nlnk %d, valid 0x%0x\n",                  \
98                (obdo)->o_mode, (obdo)->o_uid, (obdo)->o_gid, (obdo)->o_flags,\
99                (obdo)->o_obdflags, (obdo)->o_nlink, (obdo)->o_valid);   \
100 }
101
102
103 #define PDEBUG(page,msg) {                                              \
104         if (page){                                                      \
105                 char *uptodate = (Page_Uptodate(page)) ? "upto" : "outof";\
106                 char *locked = (PageLocked(page)) ? "" : "un";          \
107                 char *buffer = page->buffers ? "buffer" : "";           \
108                 int count = page_count(page);                           \
109                 long index = page->index;                               \
110                 CDEBUG(D_CACHE, "%s: ** off %ld, %sdate, %slocked, flag %ld,"\
111                        " cnt %d page 0x%p pages %ld virt %lx %s**\n",   \
112                        msg, index, uptodate, locked, page->flags, count,\
113                        page, page->mapping ? page->mapping->nrpages : -1,\
114                        page->virtual, buffer);                          \
115         } else                                                          \
116                 CDEBUG(D_CACHE, "** %s: no page\n", msg);               \
117 }
118
119 #if 0
120 #define iget(sb, ino) obd_iget(sb, ino)
121 #define iput(sb, ino) obd_iput(sb, ino)
122
123 static inline struct inode *obd_iget(struct super_block *sb, unsigned long ino)
124 {
125         struct inode *inode;
126         
127         if ((inode = iget(sb, ino)) == NULL)
128                 CDEBUG(D_INODE, "NULL in iget for %ld\n", ino);
129         else
130                 obd_inodes++;
131         return inode;
132 }
133
134 static inline void obd_iput(struct inode *inode)
135 {
136         if (inode == NULL)
137                 CDEBUG(D_INODE, "NULL in iput\n");
138         else
139                 obd_inodes--;
140 }
141 #endif
142
143 #else /* EXT2_OBD_DEBUG */
144
145 #define CDEBUG(mask, format, a...) {}
146 #define ENTRY {}
147 #define EXIT {}
148 #define ODEBUG(obdo) {}
149 #define EXDEBUG(inode) {}
150 #define OIDEBUG(inode) {}
151 #define PDEBUG(page, cmd) {}
152
153 #endif /* EXT2_OBD_DEBUG */
154
155
156
157 #define OBD_ALLOC(ptr, cast, size)                                      \
158 do {                                                                    \
159         if (size <= 4096) {                                             \
160                 ptr = (cast)kmalloc((unsigned long) size, GFP_KERNEL);  \
161                 CDEBUG(D_MALLOC, "kmalloced: %d at %x.\n",              \
162                        (int) size, (int) ptr);                          \
163         } else {                                                        \
164                 ptr = (cast)vmalloc((unsigned long) size);              \
165                 CDEBUG(D_MALLOC, "vmalloced: %d at %x.\n",              \
166                        (int) size, (int) ptr);                          \
167         }                                                               \
168         if (ptr == 0) {                                                 \
169                 printk("kernel malloc returns 0 at %s:%d\n",            \
170                        __FILE__, __LINE__);                             \
171         } else {                                                        \
172                 memset(ptr, 0, size);                                   \
173                 obd_memory += size;                                     \
174         }                                                               \
175 } while (0)
176
177 #define OBD_FREE(ptr,size)                              \
178 do {                                                    \
179         if (size <= 4096) {                             \
180                 kfree((ptr));                   \
181                 CDEBUG(D_MALLOC, "kfreed: %d at %x.\n", \
182                        (int) size, (int) ptr);          \
183         } else {                                        \
184                 vfree((ptr));                           \
185                 CDEBUG(D_MALLOC, "vfreed: %d at %x.\n", \
186                        (int) size, (int) ptr);          \
187         }                                               \
188         obd_memory -= size;                             \
189 } while (0)
190
191
192
193 #endif