1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
27 #include <linux/config.h>
28 #include <linux/autoconf.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
32 #include <libcfs/kp30.h>
33 #include <linux/lustre_compat25.h>
35 /* global variables */
36 extern int obd_memmax;
37 extern atomic_t obd_memory;
39 extern unsigned int obd_fail_loc;
40 extern unsigned int obd_timeout;
41 extern unsigned int ldlm_timeout;
42 extern char obd_lustre_upcall[128];
43 extern unsigned int obd_sync_filter;
44 extern unsigned int obd_dump_on_timeout;
45 extern wait_queue_head_t obd_race_waitq;
47 #define OBD_FAIL_MDS 0x100
48 #define OBD_FAIL_MDS_HANDLE_UNPACK 0x101
49 #define OBD_FAIL_MDS_GETATTR_NET 0x102
50 #define OBD_FAIL_MDS_GETATTR_PACK 0x103
51 #define OBD_FAIL_MDS_READPAGE_NET 0x104
52 #define OBD_FAIL_MDS_READPAGE_PACK 0x105
53 #define OBD_FAIL_MDS_SENDPAGE 0x106
54 #define OBD_FAIL_MDS_REINT_NET 0x107
55 #define OBD_FAIL_MDS_REINT_UNPACK 0x108
56 #define OBD_FAIL_MDS_REINT_SETATTR 0x109
57 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
58 #define OBD_FAIL_MDS_REINT_CREATE 0x10b
59 #define OBD_FAIL_MDS_REINT_CREATE_WRITE 0x10c
60 #define OBD_FAIL_MDS_REINT_UNLINK 0x10d
61 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE 0x10e
62 #define OBD_FAIL_MDS_REINT_LINK 0x10f
63 #define OBD_FAIL_MDS_REINT_LINK_WRITE 0x110
64 #define OBD_FAIL_MDS_REINT_RENAME 0x111
65 #define OBD_FAIL_MDS_REINT_RENAME_WRITE 0x112
66 #define OBD_FAIL_MDS_OPEN_NET 0x113
67 #define OBD_FAIL_MDS_OPEN_PACK 0x114
68 #define OBD_FAIL_MDS_CLOSE_NET 0x115
69 #define OBD_FAIL_MDS_CLOSE_PACK 0x116
70 #define OBD_FAIL_MDS_CONNECT_NET 0x117
71 #define OBD_FAIL_MDS_CONNECT_PACK 0x118
72 #define OBD_FAIL_MDS_REINT_NET_REP 0x119
73 #define OBD_FAIL_MDS_DISCONNECT_NET 0x11a
74 #define OBD_FAIL_MDS_GETSTATUS_NET 0x11b
75 #define OBD_FAIL_MDS_GETSTATUS_PACK 0x11c
76 #define OBD_FAIL_MDS_STATFS_PACK 0x11d
77 #define OBD_FAIL_MDS_STATFS_NET 0x11e
78 #define OBD_FAIL_MDS_GETATTR_LOCK_NET 0x11f
79 #define OBD_FAIL_MDS_PIN_NET 0x120
80 #define OBD_FAIL_MDS_UNPIN_NET 0x121
81 #define OBD_FAIL_MDS_ALL_REPLY_NET 0x122
82 #define OBD_FAIL_MDS_ALL_REQUEST_NET 0x123
83 #define OBD_FAIL_MDS_SYNC_NET 0x124
84 #define OBD_FAIL_MDS_SYNC_PACK 0x125
85 #define OBD_FAIL_MDS_DONE_WRITING_NET 0x126
86 #define OBD_FAIL_MDS_DONE_WRITING_PACK 0x127
87 #define OBD_FAIL_MDS_ALLOC_OBDO 0x128
88 #define OBD_FAIL_MDS_PAUSE_OPEN 0x129
89 #define OBD_FAIL_MDS_STATFS_LCW_SLEEP 0x12a
90 #define OBD_FAIL_MDS_OPEN_CREATE 0x12b
92 #define OBD_FAIL_OST 0x200
93 #define OBD_FAIL_OST_CONNECT_NET 0x201
94 #define OBD_FAIL_OST_DISCONNECT_NET 0x202
95 #define OBD_FAIL_OST_GET_INFO_NET 0x203
96 #define OBD_FAIL_OST_CREATE_NET 0x204
97 #define OBD_FAIL_OST_DESTROY_NET 0x205
98 #define OBD_FAIL_OST_GETATTR_NET 0x206
99 #define OBD_FAIL_OST_SETATTR_NET 0x207
100 #define OBD_FAIL_OST_OPEN_NET 0x208
101 #define OBD_FAIL_OST_CLOSE_NET 0x209
102 #define OBD_FAIL_OST_BRW_NET 0x20a
103 #define OBD_FAIL_OST_PUNCH_NET 0x20b
104 #define OBD_FAIL_OST_STATFS_NET 0x20c
105 #define OBD_FAIL_OST_HANDLE_UNPACK 0x20d
106 #define OBD_FAIL_OST_BRW_WRITE_BULK 0x20e
107 #define OBD_FAIL_OST_BRW_READ_BULK 0x20f
108 #define OBD_FAIL_OST_SYNC_NET 0x210
109 #define OBD_FAIL_OST_ALL_REPLY_NET 0x211
110 #define OBD_FAIL_OST_ALL_REQUESTS_NET 0x212
111 #define OBD_FAIL_OST_LDLM_REPLY_NET 0x213
112 #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
113 #define OBD_FAIL_OST_ENOSPC 0x215
114 #define OBD_FAIL_OST_EROFS 0x216
117 #define OBD_FAIL_LDLM 0x300
118 #define OBD_FAIL_LDLM_NAMESPACE_NEW 0x301
119 #define OBD_FAIL_LDLM_ENQUEUE 0x302
120 #define OBD_FAIL_LDLM_CONVERT 0x303
121 #define OBD_FAIL_LDLM_CANCEL 0x304
122 #define OBD_FAIL_LDLM_BL_CALLBACK 0x305
123 #define OBD_FAIL_LDLM_CP_CALLBACK 0x306
124 #define OBD_FAIL_LDLM_GL_CALLBACK 0x307
125 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
126 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
127 #define OBD_FAIL_LDLM_CREATE_RESOURCE 0x30a
128 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED 0x30b
129 #define OBD_FAIL_LDLM_REPLY 0x30c
132 #define OBD_FAIL_OSC 0x400
133 #define OBD_FAIL_OSC_BRW_READ_BULK 0x401
134 #define OBD_FAIL_OSC_BRW_WRITE_BULK 0x402
135 #define OBD_FAIL_OSC_LOCK_BL_AST 0x403
136 #define OBD_FAIL_OSC_LOCK_CP_AST 0x404
137 #define OBD_FAIL_OSC_MATCH 0x405
139 #define OBD_FAIL_PTLRPC 0x500
140 #define OBD_FAIL_PTLRPC_ACK 0x501
141 #define OBD_FAIL_PTLRPC_RQBD 0x502
142 #define OBD_FAIL_PTLRPC_BULK_GET_NET 0x503
143 #define OBD_FAIL_PTLRPC_BULK_PUT_NET 0x504
145 #define OBD_FAIL_OBD_PING_NET 0x600
146 #define OBD_FAIL_OBD_LOG_CANCEL_NET 0x601
147 #define OBD_FAIL_OBD_LOGD_NET 0x602
149 #define OBD_FAIL_TGT_REPLY_NET 0x700
150 #define OBD_FAIL_TGT_CONN_RACE 0x701
152 #define OBD_FAIL_SVCSEC_ACCEPT_BEG 0x750
153 #define OBD_FAIL_SVCSEC_ACCEPT_END 0x751
154 #define OBD_FAIL_SVCSEC_WRAP_BEG 0x752
155 #define OBD_FAIL_SVCSEC_WRAP_END 0x753
156 #define OBD_FAIL_SVCGSS_ERR_NOTIFY 0x760
157 #define OBD_FAIL_SVCGSS_INIT_REQ 0x780
158 #define OBD_FAIL_SVCGSS_INIT_REP 0x781
160 #define OBD_FAIL_MDC_REVALIDATE_PAUSE 0x800
162 /* preparation for a more advanced failure testbed (not functional yet) */
163 #define OBD_FAIL_MASK_SYS 0x0000FF00
164 #define OBD_FAIL_MASK_LOC (0x000000FF | OBD_FAIL_MASK_SYS)
165 #define OBD_FAIL_ONCE 0x80000000
166 #define OBD_FAILED 0x40000000
167 #define OBD_FAIL_MDS_ALL_NET 0x01000000
168 #define OBD_FAIL_OST_ALL_NET 0x02000000
170 #define OBD_FAIL_CHECK(id) (((obd_fail_loc & OBD_FAIL_MASK_LOC) == \
171 ((id) & OBD_FAIL_MASK_LOC)) && \
172 ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
173 (OBD_FAILED | OBD_FAIL_ONCE)))
175 #define OBD_FAIL_CHECK_ONCE(id) \
177 if (OBD_FAIL_CHECK(id)) { \
178 CERROR("obd_fail_loc=%x\n", id); \
179 obd_fail_loc |= OBD_FAILED; \
180 if ((id) & OBD_FAIL_ONCE) \
181 obd_fail_loc |= OBD_FAIL_ONCE; \
187 #define OBD_FAIL_RETURN(id, ret) \
189 if (OBD_FAIL_CHECK_ONCE(id)) { \
194 #define OBD_FAIL_GOTO(id, label, ret) \
196 if (OBD_FAIL_CHECK_ONCE(id)) { \
197 GOTO(label, (ret)); \
201 #define OBD_FAIL_TIMEOUT(id, secs) \
203 if (OBD_FAIL_CHECK_ONCE(id)) { \
204 CERROR("obd_fail_timeout id %x sleeping for %d secs\n", \
206 set_current_state(TASK_UNINTERRUPTIBLE); \
207 schedule_timeout((secs) * HZ); \
208 set_current_state(TASK_RUNNING); \
209 CERROR("obd_fail_timeout id %x awake\n", (id)); \
214 /* The idea here is to synchronise two threads to force a race. The
215 * first thread that calls this with a matching fail_loc is put to
216 * sleep. The next thread that calls with the same fail_loc wakes up
217 * the first and continues. */
218 #define OBD_RACE(id) \
220 if (OBD_FAIL_CHECK_ONCE(id)) { \
221 CERROR("obd_race id %x sleeping\n", (id)); \
222 sleep_on(&obd_race_waitq); \
223 CERROR("obd_fail_race id %x awake\n", (id)); \
224 } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) == \
225 ((id) & OBD_FAIL_MASK_LOC)) { \
226 wake_up(&obd_race_waitq); \
230 /* sigh. an expedient fix until OBD_RACE is fixed up */
231 #define OBD_RACE(foo) LBUG()
234 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
237 # include <linux/types.h>
238 # include <linux/blkdev.h>
241 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
242 # define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
243 # define ll_bdevname(SB, STORAGE) __bdevname(kdev_t_to_nr(SB->s_dev), STORAGE)
244 # define ll_lock_kernel lock_kernel()
245 # define ll_sbdev(SB) ((SB)->s_bdev)
246 void dev_set_rdonly(struct block_device *, int);
248 # define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
249 # define ll_sbdev(SB) (kdev_t_to_nr((SB)->s_dev))
250 # define ll_bdevname(SB,STORAGE) ((void)__unused_##STORAGE,bdevname(ll_sbdev(SB)))
251 # define ll_lock_kernel
252 void dev_set_rdonly(kdev_t, int);
255 void dev_clear_rdonly(int);
257 static inline void OBD_FAIL_WRITE(int id, struct super_block *sb)
259 if (OBD_FAIL_CHECK(id)) {
260 BDEVNAME_DECLARE_STORAGE(tmp);
261 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
262 id, ll_bdevname(sb, tmp));
263 dev_set_rdonly(ll_sbdev(sb), 2);
264 /* We set FAIL_ONCE because we never "un-fail" a device */
265 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
268 #else /* !__KERNEL__ */
269 # define LTIME_S(time) (time)
270 /* for obd_class.h */
272 # define ERR_PTR(a) ((void *)(a))
274 #endif /* __KERNEL__ */
276 extern atomic_t portal_kmemory;
278 #if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
279 #define MEM_LOC_LEN 128
282 struct hlist_node m_hash;
283 char m_loc[MEM_LOC_LEN];
288 void lvfs_memdbg_insert(struct mtrack *mt);
289 void lvfs_memdbg_remove(struct mtrack *mt);
290 struct mtrack *lvfs_memdbg_find(void *ptr);
292 int lvfs_memdbg_check_insert(struct mtrack *mt);
293 struct mtrack *lvfs_memdbg_check_remove(void *ptr);
295 static inline struct mtrack *
296 __new_mtrack(void *ptr, int size,
297 char *file, int line)
301 mt = kmalloc(sizeof(*mt), GFP_KERNEL);
305 snprintf(mt->m_loc, sizeof(mt->m_loc) - 1,
306 "%s:%d", file, line);
314 __free_mtrack(struct mtrack *mt)
320 __get_mtrack(void *ptr, int size,
321 char *file, int line)
325 mt = __new_mtrack(ptr, size, file, line);
327 CWARN("can't allocate new memory track\n");
331 if (!lvfs_memdbg_check_insert(mt))
338 __put_mtrack(void *ptr, int size,
339 char *file, int line)
343 if (!(mt = lvfs_memdbg_check_remove(ptr))) {
344 CWARN("ptr 0x%p is not allocated. Attempt to free "
345 "not allocated memory at %s:%d\n", ptr,
349 if (mt->m_size != size) {
350 CWARN("freeing memory chunk of different size "
351 "than allocated (%d != %d) at %s:%d\n",
352 mt->m_size, size, file, line);
359 #define get_mtrack(ptr, size, file, line) \
360 __get_mtrack((ptr), (size), (file), (line))
362 #define put_mtrack(ptr, size, file, line) \
363 __put_mtrack((ptr), (size), (file), (line))
365 #else /* !CONFIG_DEBUG_MEMORY */
367 #define get_mtrack(ptr, size, file, line) \
370 #define put_mtrack(ptr, size, file, line) \
372 #endif /* !CONFIG_DEBUG_MEMORY */
374 #define OBD_ALLOC_GFP(ptr, size, gfp_mask) \
376 (ptr) = kmalloc(size, (gfp_mask)); \
377 if ((ptr) == NULL) { \
378 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n", \
379 (int)(size), __FILE__, __LINE__); \
380 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
381 atomic_read(&obd_memory), atomic_read(&portal_kmemory)); \
383 memset(ptr, 0, size); \
384 atomic_add(size, &obd_memory); \
385 if (atomic_read(&obd_memory) > obd_memmax) \
386 obd_memmax = atomic_read(&obd_memory); \
387 get_mtrack((ptr), (size), __FILE__, __LINE__); \
388 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
389 (int)(size), (ptr), atomic_read(&obd_memory)); \
394 # define OBD_GFP_MASK GFP_NOFS
397 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
398 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, GFP_KERNEL)
401 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
403 # define OBD_VMALLOC(ptr, size) \
405 (ptr) = vmalloc(size); \
406 if ((ptr) == NULL) { \
407 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n", \
408 (int)(size), __FILE__, __LINE__); \
409 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
410 atomic_read(&obd_memory), atomic_read(&portal_kmemory)); \
412 memset(ptr, 0, size); \
413 atomic_add(size, &obd_memory); \
414 if (atomic_read(&obd_memory) > obd_memmax) \
415 obd_memmax = atomic_read(&obd_memory); \
416 get_mtrack((ptr), (size), __FILE__, __LINE__); \
417 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
418 (int)(size), ptr, atomic_read(&obd_memory)); \
423 #ifdef CONFIG_DEBUG_SLAB
424 #define POISON(ptr, c, s) do {} while (0)
426 #define POISON(ptr, c, s) memset(ptr, c, s)
430 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE); \
431 kunmap(page); } while (0)
433 #define POISON_PAGE(page, val) do { } while (0)
436 #define OBD_FREE(ptr, size) \
439 put_mtrack((ptr), (size), __FILE__, __LINE__); \
440 atomic_sub(size, &obd_memory); \
441 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
442 (int)(size), ptr, atomic_read(&obd_memory)); \
443 POISON(ptr, 0x5a, size); \
445 (ptr) = (void *)0xdeadbeef; \
449 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
451 # define OBD_VFREE(ptr, size) \
454 put_mtrack((ptr), (size), __FILE__, __LINE__); \
455 atomic_sub(size, &obd_memory); \
456 CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n", \
457 (int)(size), ptr, atomic_read(&obd_memory)); \
458 POISON(ptr, 0x5a, size); \
460 (ptr) = (void *)0xdeadbeef; \
465 * we memset() the slab object to 0 when allocation succeeds, so DO NOT HAVE A
466 * CTOR THAT DOES ANYTHING. Its work will be cleared here. We'd love to assert
467 * on that, but slab.c keeps kmem_cache_s all to itself.
469 #define OBD_SLAB_ALLOC(ptr, slab, type, size) \
471 LASSERT(!in_interrupt()); \
472 (ptr) = kmem_cache_alloc(slab, (type)); \
473 if ((ptr) == NULL) { \
474 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
475 (int)(size), __FILE__, __LINE__); \
476 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
477 atomic_read(&obd_memory), atomic_read(&portal_kmemory)); \
479 memset(ptr, 0, size); \
480 atomic_add(size, &obd_memory); \
481 if (atomic_read(&obd_memory) > obd_memmax) \
482 obd_memmax = atomic_read(&obd_memory); \
483 get_mtrack((ptr), (size), __FILE__, __LINE__); \
484 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n", \
485 (int)(size), ptr, atomic_read(&obd_memory)); \
489 #define OBD_SLAB_FREE(ptr, slab, size) \
492 CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n", \
493 (int)(size), ptr, atomic_read(&obd_memory)); \
494 put_mtrack((ptr), (size), __FILE__, __LINE__); \
495 atomic_sub(size, &obd_memory); \
496 POISON(ptr, 0x5a, size); \
497 kmem_cache_free(slab, ptr); \
498 (ptr) = (void *)0xdeadbeef; \