Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / include / linux / obd_support.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef _OBD_SUPPORT
24 #define _OBD_SUPPORT
25
26 #ifdef __KERNEL__
27 #include <linux/config.h>
28 #include <linux/autoconf.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #else
32
33 #endif
34 #include <linux/kp30.h>
35 #include <linux/lustre_compat25.h>
36
37 /* global variables */
38 extern atomic_t obd_memory;
39 extern int obd_memmax;
40 extern unsigned int obd_fail_loc;
41 extern unsigned int obd_timeout;
42 extern char obd_lustre_upcall[128];
43 extern unsigned int obd_sync_filter;
44
45 #define OBD_FAIL_MDS                     0x100
46 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
47 #define OBD_FAIL_MDS_GETATTR_NET         0x102
48 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
49 #define OBD_FAIL_MDS_READPAGE_NET        0x104
50 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
51 #define OBD_FAIL_MDS_SENDPAGE            0x106
52 #define OBD_FAIL_MDS_REINT_NET           0x107
53 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
54 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
55 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
56 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
57 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
58 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
59 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
60 #define OBD_FAIL_MDS_REINT_LINK          0x10f
61 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
62 #define OBD_FAIL_MDS_REINT_RENAME        0x111
63 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
64 #define OBD_FAIL_MDS_OPEN_NET            0x113
65 #define OBD_FAIL_MDS_OPEN_PACK           0x114
66 #define OBD_FAIL_MDS_CLOSE_NET           0x115
67 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
68 #define OBD_FAIL_MDS_CONNECT_NET         0x117
69 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
70 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
71 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
72 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
73 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
74 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
75 #define OBD_FAIL_MDS_STATFS_NET          0x11e
76 #define OBD_FAIL_MDS_GETATTR_NAME_NET    0x11f
77 #define OBD_FAIL_MDS_PIN_NET             0x120
78 #define OBD_FAIL_MDS_UNPIN_NET           0x121
79 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
80 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
81 #define OBD_FAIL_MDS_SYNC_NET            0x124
82 #define OBD_FAIL_MDS_SYNC_PACK           0x125
83 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
84 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
85
86 #define OBD_FAIL_OST                     0x200
87 #define OBD_FAIL_OST_CONNECT_NET         0x201
88 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
89 #define OBD_FAIL_OST_GET_INFO_NET        0x203
90 #define OBD_FAIL_OST_CREATE_NET          0x204
91 #define OBD_FAIL_OST_DESTROY_NET         0x205
92 #define OBD_FAIL_OST_GETATTR_NET         0x206
93 #define OBD_FAIL_OST_SETATTR_NET         0x207
94 #define OBD_FAIL_OST_OPEN_NET            0x208
95 #define OBD_FAIL_OST_CLOSE_NET           0x209
96 #define OBD_FAIL_OST_BRW_NET             0x20a
97 #define OBD_FAIL_OST_PUNCH_NET           0x20b
98 #define OBD_FAIL_OST_STATFS_NET          0x20c
99 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
100 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
101 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
102 #define OBD_FAIL_OST_SYNC_NET            0x210
103 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
104 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
105 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
106 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
107
108 #define OBD_FAIL_LDLM                    0x300
109 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
110 #define OBD_FAIL_LDLM_ENQUEUE            0x302
111 #define OBD_FAIL_LDLM_CONVERT            0x303
112 #define OBD_FAIL_LDLM_CANCEL             0x304
113 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
114 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
115
116 #define OBD_FAIL_OSC                     0x400
117 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
118 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
119 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
120 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
121
122 #define OBD_FAIL_PTLRPC                  0x500
123 #define OBD_FAIL_PTLRPC_ACK              0x501
124
125 #define OBD_FAIL_OBD_PING_NET            0x600
126 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
127 #define OBD_FAIL_OBD_LOGD_NET            0x602
128
129 /* preparation for a more advanced failure testbed (not functional yet) */
130 #define OBD_FAIL_MASK_SYS    0x0000FF00
131 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
132 #define OBD_FAIL_ONCE        0x80000000
133 #define OBD_FAILED           0x40000000
134 #define OBD_FAIL_MDS_ALL_NET 0x01000000
135 #define OBD_FAIL_OST_ALL_NET 0x02000000
136
137 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
138                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
139                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
140                                 (OBD_FAILED | OBD_FAIL_ONCE)))
141
142 #define OBD_FAIL_CHECK_ONCE(id)                                              \
143 ({      int _ret_ = 0;                                                       \
144         if (OBD_FAIL_CHECK(id)) {                                            \
145                 CERROR("obd_fail_loc=%x\n", id);                             \
146                 obd_fail_loc |= OBD_FAILED;                                  \
147                 if ((id) & OBD_FAIL_ONCE)                                    \
148                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
149                 _ret_ = 1;                                                   \
150         }                                                                    \
151         _ret_;                                                               \
152 })
153
154 #define OBD_FAIL_RETURN(id, ret)                                             \
155 do {                                                                         \
156         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
157                 RETURN(ret);                                                 \
158         }                                                                    \
159 } while(0)
160
161 #define OBD_FAIL_TIMEOUT(id, secs)                                           \
162 do {                                                                         \
163         if  (OBD_FAIL_CHECK_ONCE(id)) {                                      \
164                 CERROR("obd_fail_timeout id %x sleeping for %d secs\n",     \
165                        (id), (secs));                                        \
166                 set_current_state(TASK_UNINTERRUPTIBLE);                     \
167                 schedule_timeout((secs) * HZ);                               \
168                 set_current_state(TASK_RUNNING);                             \
169                 CERROR("obd_fail_timeout id %x awake\n", (id));              \
170        }                                                                     \
171 } while(0)
172
173 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
174
175 #ifdef __KERNEL__
176 #include <linux/types.h>
177 #include <linux/blkdev.h>
178
179
180 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
181 #define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
182 #define ll_bdevname(SB, STORAGE) __bdevname(kdev_t_to_nr(SB->s_dev), STORAGE)
183 #define ll_lock_kernel lock_kernel()
184 #define ll_sbdev(SB)    ((SB)->s_bdev)
185 void dev_set_rdonly(struct block_device *, int);
186 #else
187 #define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
188 #define ll_sbdev(SB)    (kdev_t_to_nr((SB)->s_dev))
189 #define ll_bdevname(SB,STORAGE) ((void)__unused_##STORAGE,bdevname(ll_sbdev(SB)))
190 #define ll_lock_kernel
191 void dev_set_rdonly(kdev_t, int);
192 #endif
193
194 void dev_clear_rdonly(int);
195
196 static inline void OBD_FAIL_WRITE(int id, struct super_block *sb)
197 {
198         if (OBD_FAIL_CHECK(id)) {
199                 BDEVNAME_DECLARE_STORAGE(tmp);
200                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
201                        id, ll_bdevname(sb, tmp));
202                 dev_set_rdonly(ll_sbdev(sb), 2);
203                 /* We set FAIL_ONCE because we never "un-fail" a device */
204                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
205         }
206 }
207 #else /* !__KERNEL__ */
208 #define LTIME_S(time) (time)
209 /* for obd_class.h */
210 #ifndef ERR_PTR
211 # define ERR_PTR(a) ((void *)(a))
212 #endif
213 #endif  /* __KERNEL__ */
214
215 #ifndef GFP_MEMALLOC
216 #define GFP_MEMALLOC 0
217 #endif
218
219 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
220 do {                                                                          \
221         (ptr) = kmalloc(size, (gfp_mask));                                    \
222         if ((ptr) == NULL) {                                                  \
223                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
224                        (int)(size), __FILE__, __LINE__);                      \
225         } else {                                                              \
226                 memset(ptr, 0, size);                                         \
227                 atomic_add(size, &obd_memory);                                \
228                 if (atomic_read(&obd_memory) > obd_memmax)                    \
229                         obd_memmax = atomic_read(&obd_memory);                \
230                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
231                        (int)(size), ptr, atomic_read(&obd_memory));           \
232         }                                                                     \
233 } while (0)
234
235 #ifndef OBD_GFP_MASK
236 # define OBD_GFP_MASK (GFP_KERNEL | GFP_MEMALLOC)
237 #endif
238
239 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
240 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, GFP_KERNEL)
241
242 #ifdef __arch_um__
243 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
244 #else
245 # define OBD_VMALLOC(ptr, size)                                               \
246 do {                                                                          \
247         (ptr) = vmalloc(size);                                                \
248         if ((ptr) == NULL) {                                                  \
249                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
250                        (int)(size), __FILE__, __LINE__);                      \
251         } else {                                                              \
252                 memset(ptr, 0, size);                                         \
253                 atomic_add(size, &obd_memory);                                \
254                 if (atomic_read(&obd_memory) > obd_memmax)                    \
255                         obd_memmax = atomic_read(&obd_memory);                \
256                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
257                        (int)(size), ptr, atomic_read(&obd_memory));           \
258         }                                                                     \
259 } while (0)
260 #endif
261
262 #ifdef CONFIG_DEBUG_SLAB
263 #define POISON(ptr, c, s) do {} while (0)
264 #else
265 #define POISON(ptr, c, s) memset(ptr, c, s)
266 #endif
267
268 #if POISON_BULK
269 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE);       \
270                                     kunmap(page); } while (0)
271 #else
272 #define POISON_PAGE(page, val) do { } while (0)
273 #endif
274
275 #define OBD_FREE(ptr, size)                                                   \
276 do {                                                                          \
277         LASSERT(ptr);                                                         \
278         atomic_sub(size, &obd_memory);                                        \
279         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",           \
280                (int)(size), ptr, atomic_read(&obd_memory));                   \
281         POISON(ptr, 0x5a, size);                                              \
282         kfree(ptr);                                                           \
283         (ptr) = (void *)0xdeadbeef;                                           \
284 } while (0)
285
286 #ifdef __arch_um__
287 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
288 #else
289 # define OBD_VFREE(ptr, size)                                                 \
290 do {                                                                          \
291         LASSERT(ptr);                                                         \
292         atomic_sub(size, &obd_memory);                                        \
293         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",           \
294                (int)(size), ptr, atomic_read(&obd_memory));                   \
295         POISON(ptr, 0x5a, size);                                              \
296         vfree(ptr);                                                           \
297         (ptr) = (void *)0xdeadbeef;                                           \
298 } while (0)
299 #endif
300
301 #ifndef SLAB_MEMALLOC
302 #define SLAB_MEMALLOC 0
303 #endif
304
305 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
306  * HAVE A CTOR THAT DOES ANYTHING.  its work will be cleared here.  we'd
307  * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
308 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                 \
309 do {                                                                          \
310         LASSERT(!in_interrupt());                                             \
311         (ptr) = kmem_cache_alloc(slab, (type | SLAB_MEMALLOC));               \
312         if ((ptr) == NULL) {                                                  \
313                 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
314                        (int)(size), __FILE__, __LINE__);                      \
315         } else {                                                              \
316                 memset(ptr, 0, size);                                         \
317                 atomic_add(size, &obd_memory);                                \
318                 if (atomic_read(&obd_memory) > obd_memmax)                    \
319                         obd_memmax = atomic_read(&obd_memory);                \
320                 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",\
321                        (int)(size), ptr, atomic_read(&obd_memory));           \
322         }                                                                     \
323 } while (0)
324
325 #define OBD_SLAB_FREE(ptr, slab, size)                                        \
326 do {                                                                          \
327         LASSERT(ptr);                                                         \
328         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n",       \
329                (int)(size), ptr, atomic_read(&obd_memory));                   \
330         atomic_sub(size, &obd_memory);                                        \
331         POISON(ptr, 0x5a, size);                                              \
332         kmem_cache_free(slab, ptr);                                           \
333         (ptr) = (void *)0xdeadbeef;                                           \
334 } while (0)
335
336 #endif