Whamcloud - gitweb
Merge b_md into HEAD
[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 #include <linux/config.h>
27 #include <linux/autoconf.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/kp30.h>
31
32 /* global variables */
33 extern atomic_t obd_memory;
34 extern int obd_memmax;
35 extern unsigned long obd_fail_loc;
36 extern unsigned long obd_timeout;
37 extern char obd_recovery_upcall[128];
38 extern unsigned long obd_sync_filter;
39
40 #define OBD_FAIL_MDS                     0x100
41 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
42 #define OBD_FAIL_MDS_GETATTR_NET         0x102
43 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
44 #define OBD_FAIL_MDS_READPAGE_NET        0x104
45 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
46 #define OBD_FAIL_MDS_SENDPAGE            0x106
47 #define OBD_FAIL_MDS_REINT_NET           0x107
48 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
49 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
50 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
51 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
52 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
53 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
54 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
55 #define OBD_FAIL_MDS_REINT_LINK          0x10f
56 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
57 #define OBD_FAIL_MDS_REINT_RENAME        0x111
58 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
59 #define OBD_FAIL_MDS_OPEN_NET            0x113
60 #define OBD_FAIL_MDS_OPEN_PACK           0x114
61 #define OBD_FAIL_MDS_CLOSE_NET           0x115
62 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
63 #define OBD_FAIL_MDS_CONNECT_NET         0x117
64 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
65 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
66 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
67 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
68 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
69 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
70 #define OBD_FAIL_MDS_STATFS_NET          0x11e
71 #define OBD_FAIL_MDS_GETATTR_NAME_NET    0x11f
72 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x120
73 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x121
74
75 #define OBD_FAIL_OST                     0x200
76 #define OBD_FAIL_OST_CONNECT_NET         0x201
77 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
78 #define OBD_FAIL_OST_GET_INFO_NET        0x203
79 #define OBD_FAIL_OST_CREATE_NET          0x204
80 #define OBD_FAIL_OST_DESTROY_NET         0x205
81 #define OBD_FAIL_OST_GETATTR_NET         0x206
82 #define OBD_FAIL_OST_SETATTR_NET         0x207
83 #define OBD_FAIL_OST_OPEN_NET            0x208
84 #define OBD_FAIL_OST_CLOSE_NET           0x209
85 #define OBD_FAIL_OST_BRW_NET             0x20a
86 #define OBD_FAIL_OST_PUNCH_NET           0x20b
87 #define OBD_FAIL_OST_STATFS_NET          0x20c
88 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
89 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
90 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
91
92 #define OBD_FAIL_LDLM                    0x300
93 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
94 #define OBD_FAIL_LDLM_ENQUEUE            0x302
95 #define OBD_FAIL_LDLM_CONVERT            0x303
96 #define OBD_FAIL_LDLM_CANCEL             0x304
97 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
98 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
99
100 #define OBD_FAIL_OSC                     0x400
101 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
102 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
103 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
104 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
105
106 /* preparation for a more advanced failure testbed (not functional yet) */
107 #define OBD_FAIL_MASK_SYS    0x0000FF00
108 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
109 #define OBD_FAIL_ONCE        0x80000000
110 #define OBD_FAILED           0x40000000
111 #define OBD_FAIL_MDS_ALL_NET 0x01000000
112 #define OBD_FAIL_OST_ALL_NET 0x02000000
113
114 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
115                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
116                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
117                                 (OBD_FAILED | OBD_FAIL_ONCE)))
118
119 #define OBD_FAIL_RETURN(id, ret)                                             \
120 do {                                                                         \
121         if (OBD_FAIL_CHECK(id)) {                                            \
122                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n", id, ret);  \
123                 obd_fail_loc |= OBD_FAILED;                                  \
124                 if ((id) & OBD_FAIL_ONCE)                                    \
125                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
126                 RETURN(ret);                                                 \
127         }                                                                    \
128 } while(0)
129
130 #include <linux/types.h>
131 #include <linux/blkdev.h>
132
133 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
134
135 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
136 #define ll_bdevname(a) __bdevname((a))
137 #define ll_lock_kernel lock_kernel()
138 #else
139 #define ll_lock_kernel
140 #define ll_bdevname(a) bdevname((a))
141 #endif
142
143 static inline void OBD_FAIL_WRITE(int id, kdev_t dev)
144 {
145         if (OBD_FAIL_CHECK(id)) {
146 #ifdef CONFIG_DEV_RDONLY
147                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
148                        id, ll_bdevname(dev));
149                 dev_set_rdonly(dev, 2);
150 #else
151                 CERROR("obd_fail_loc=%x, can't fail write operation on %s\n",
152                        id, ll_bdevname(dev));
153 #endif
154                 /* We set FAIL_ONCE because we never "un-fail" a device */
155                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
156         }
157 }
158
159 #define OBD_ALLOC(ptr, size)                                            \
160 do {                                                                    \
161         void *lptr;                                                     \
162         int s = (size);                                                 \
163         (ptr) = lptr = kmalloc(s, GFP_KERNEL);                          \
164         if (lptr == NULL) {                                             \
165                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed "       \
166                        "at %s:%d\n", s, __FILE__, __LINE__);            \
167         } else {                                                        \
168                 int obd_curmem;                                         \
169                 memset(lptr, 0, s);                                     \
170                 atomic_add(s, &obd_memory);                             \
171                 obd_curmem = atomic_read(&obd_memory);                  \
172                 if (obd_curmem > obd_memmax)                            \
173                         obd_memmax = obd_curmem;                        \
174                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p "      \
175                        "(tot %d)\n", s, lptr, obd_curmem);              \
176         }                                                               \
177 } while (0)
178
179 #ifdef CONFIG_DEBUG_SLAB
180 #define POISON(lptr, s) do {} while (0)
181 #else
182 #define POISON(lptr, s) memset(lptr, 0x5a, s)
183 #endif
184
185 #define OBD_FREE(ptr, size)                                             \
186 do {                                                                    \
187         void *lptr = (ptr);                                             \
188         int s = (size);                                                 \
189         LASSERT(lptr);                                                  \
190         POISON(lptr, s);                                                \
191         kfree(lptr);                                                    \
192         atomic_sub(s, &obd_memory);                                     \
193         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
194                s, lptr, atomic_read(&obd_memory));                      \
195         (ptr) = (void *)0xdeadbeef;                                     \
196 } while (0)
197
198 #ifdef CONFIG_HIGHMEM
199 extern void obd_kmap_get(int count, int server);
200 extern void obd_kmap_put(int count);
201 #else
202 #define obd_kmap_get(count, server) do {} while (0)
203 #define obd_kmap_put(count) do {} while (0)
204 #endif
205 #endif