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>
34 #include <linux/kp30.h>
36 /* global variables */
37 extern atomic_t obd_memory;
38 extern int obd_memmax;
39 extern unsigned int obd_fail_loc;
40 extern unsigned int obd_timeout;
41 extern unsigned long obd_max_dirty_pages;
42 extern char obd_lustre_upcall[128];
43 extern unsigned int obd_sync_filter;
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
82 #define OBD_FAIL_OST 0x200
83 #define OBD_FAIL_OST_CONNECT_NET 0x201
84 #define OBD_FAIL_OST_DISCONNECT_NET 0x202
85 #define OBD_FAIL_OST_GET_INFO_NET 0x203
86 #define OBD_FAIL_OST_CREATE_NET 0x204
87 #define OBD_FAIL_OST_DESTROY_NET 0x205
88 #define OBD_FAIL_OST_GETATTR_NET 0x206
89 #define OBD_FAIL_OST_SETATTR_NET 0x207
90 #define OBD_FAIL_OST_OPEN_NET 0x208
91 #define OBD_FAIL_OST_CLOSE_NET 0x209
92 #define OBD_FAIL_OST_BRW_NET 0x20a
93 #define OBD_FAIL_OST_PUNCH_NET 0x20b
94 #define OBD_FAIL_OST_STATFS_NET 0x20c
95 #define OBD_FAIL_OST_HANDLE_UNPACK 0x20d
96 #define OBD_FAIL_OST_BRW_WRITE_BULK 0x20e
97 #define OBD_FAIL_OST_BRW_READ_BULK 0x20f
98 #define OBD_FAIL_OST_SYNCFS_NET 0x210
99 #define OBD_FAIL_OST_ALL_REPLY_NET 0x211
100 #define OBD_FAIL_OST_ALL_REQUESTS_NET 0x212
101 #define OBD_FAIL_OST_LDLM_REPLY_NET 0x213
102 #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
104 #define OBD_FAIL_LDLM 0x300
105 #define OBD_FAIL_LDLM_NAMESPACE_NEW 0x301
106 #define OBD_FAIL_LDLM_ENQUEUE 0x302
107 #define OBD_FAIL_LDLM_CONVERT 0x303
108 #define OBD_FAIL_LDLM_CANCEL 0x304
109 #define OBD_FAIL_LDLM_BL_CALLBACK 0x305
110 #define OBD_FAIL_LDLM_CP_CALLBACK 0x306
112 #define OBD_FAIL_OSC 0x400
113 #define OBD_FAIL_OSC_BRW_READ_BULK 0x401
114 #define OBD_FAIL_OSC_BRW_WRITE_BULK 0x402
115 #define OBD_FAIL_OSC_LOCK_BL_AST 0x403
116 #define OBD_FAIL_OSC_LOCK_CP_AST 0x404
118 #define OBD_FAIL_PTLRPC 0x500
119 #define OBD_FAIL_PTLRPC_ACK 0x501
121 #define OBD_FAIL_OBD_PING_NET 0x600
122 #define OBD_FAIL_OBD_LOG_CANCEL_NET 0x601
124 /* preparation for a more advanced failure testbed (not functional yet) */
125 #define OBD_FAIL_MASK_SYS 0x0000FF00
126 #define OBD_FAIL_MASK_LOC (0x000000FF | OBD_FAIL_MASK_SYS)
127 #define OBD_FAIL_ONCE 0x80000000
128 #define OBD_FAILED 0x40000000
129 #define OBD_FAIL_MDS_ALL_NET 0x01000000
130 #define OBD_FAIL_OST_ALL_NET 0x02000000
132 #define OBD_FAIL_CHECK(id) (((obd_fail_loc & OBD_FAIL_MASK_LOC) == \
133 ((id) & OBD_FAIL_MASK_LOC)) && \
134 ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
135 (OBD_FAILED | OBD_FAIL_ONCE)))
137 #define OBD_FAIL_CHECK_ONCE(id) \
139 if (OBD_FAIL_CHECK(id)) { \
140 CERROR("obd_fail_loc=%x\n", id); \
141 obd_fail_loc |= OBD_FAILED; \
142 if ((id) & OBD_FAIL_ONCE) \
143 obd_fail_loc |= OBD_FAIL_ONCE; \
149 #define OBD_FAIL_RETURN(id, ret) \
151 if (OBD_FAIL_CHECK_ONCE(id)) { \
156 #define OBD_FAIL_TIMEOUT(id, secs) \
158 if (OBD_FAIL_CHECK_ONCE(id)) { \
159 CERROR("obd_fail_timeout id %x sleeping for %d secs\n", \
161 set_current_state(TASK_UNINTERRUPTIBLE); \
162 schedule_timeout((secs) * HZ); \
163 set_current_state(TASK_RUNNING); \
164 CERROR("obd_fail_timeout id %x awake\n", (id)); \
168 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
171 #include <linux/types.h>
172 #include <linux/blkdev.h>
175 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
176 #define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
177 #define ll_bdevname(DEV, STORAGE) __bdevname(DEV, STORAGE)
178 #define ll_lock_kernel lock_kernel()
180 #define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
181 #define ll_bdevname(DEV, STORAGE) ((void)__unused_##STORAGE, bdevname((DEV)))
182 #define ll_lock_kernel
185 void dev_set_rdonly(kdev_t dev, int no_write);
186 void dev_clear_rdonly(int);
188 static inline void OBD_FAIL_WRITE(int id, kdev_t dev)
190 if (OBD_FAIL_CHECK(id)) {
191 BDEVNAME_DECLARE_STORAGE(tmp);
192 #ifdef CONFIG_DEV_RDONLY
193 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
194 id, ll_bdevname(kdev_t_to_nr(dev), tmp));
195 dev_set_rdonly(dev, 2);
197 CERROR("obd_fail_loc=%x, can't fail write operation on %s\n",
198 id, ll_bdevname(kdev_t_to_nr(dev), tmp));
200 /* We set FAIL_ONCE because we never "un-fail" a device */
201 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
204 #else /* !__KERNEL__ */
205 #define LTIME_S(time) (time)
206 #endif /* __KERNEL__ */
208 #define OBD_ALLOC_GFP(ptr, size, gfp_mask) \
210 (ptr) = kmalloc(size, gfp_mask); \
211 if ((ptr) == NULL) { \
212 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n", \
213 (int)(size), __FILE__, __LINE__); \
215 memset(ptr, 0, size); \
216 atomic_add(size, &obd_memory); \
217 if (atomic_read(&obd_memory) > obd_memmax) \
218 obd_memmax = atomic_read(&obd_memory); \
219 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
220 (int)(size), ptr, atomic_read(&obd_memory)); \
225 # define OBD_GFP_MASK GFP_KERNEL
228 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
231 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
233 # define OBD_VMALLOC(ptr, size) \
235 (ptr) = vmalloc(size); \
236 if ((ptr) == NULL) { \
237 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n", \
238 (int)(size), __FILE__, __LINE__); \
240 memset(ptr, 0, size); \
241 atomic_add(size, &obd_memory); \
242 if (atomic_read(&obd_memory) > obd_memmax) \
243 obd_memmax = atomic_read(&obd_memory); \
244 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
245 (int)(size), ptr, atomic_read(&obd_memory)); \
250 #ifdef CONFIG_DEBUG_SLAB
251 #define POISON(ptr, c, s) do {} while (0)
253 #define POISON(ptr, c, s) memset(ptr, c, s)
256 #define OBD_FREE(ptr, size) \
259 atomic_sub(size, &obd_memory); \
260 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
261 (int)(size), ptr, atomic_read(&obd_memory)); \
262 POISON(ptr, 0x5a, size); \
264 (ptr) = (void *)0xdeadbeef; \
268 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
270 # define OBD_VFREE(ptr, size) \
273 atomic_sub(size, &obd_memory); \
274 CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n", \
275 (int)(size), ptr, atomic_read(&obd_memory)); \
276 POISON(ptr, 0x5a, size); \
278 (ptr) = (void *)0xdeadbeef; \
282 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
283 * HAVE A CTOR THAT DOES ANYTHING. its work will be cleared here. we'd
284 * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
285 #define OBD_SLAB_ALLOC(ptr, slab, type, size) \
287 LASSERT(!in_interrupt()); \
288 (ptr) = kmem_cache_alloc(slab, type); \
289 if ((ptr) == NULL) { \
290 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
291 (int)(size), __FILE__, __LINE__); \
293 memset(ptr, 0, size); \
294 atomic_add(size, &obd_memory); \
295 if (atomic_read(&obd_memory) > obd_memmax) \
296 obd_memmax = atomic_read(&obd_memory); \
297 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",\
298 (int)(size), ptr, atomic_read(&obd_memory)); \
302 #define OBD_SLAB_FREE(ptr, slab, size) \
305 CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n", \
306 (int)(size), ptr, atomic_read(&obd_memory)); \
307 atomic_sub(size, &obd_memory); \
308 POISON(ptr, 0x5a, size); \
309 kmem_cache_free(slab, ptr); \
310 (ptr) = (void *)0xdeadbeef; \