Whamcloud - gitweb
Fix one build error, one build warning, and one extremely scary
[fs/lustre-release.git] / lustre / include / linux / lustre_dlm.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #ifndef _LUSTRE_DLM_H__
6 #define _LUSTRE_DLM_H__
7
8 #ifdef __KERNEL__
9
10 #include <linux/obd_class.h>
11 #include <linux/lustre_net.h>
12
13 #define OBD_LDLM_DEVICENAME  "ldlm"
14
15 typedef enum {
16         ELDLM_OK = 0,
17
18         ELDLM_LOCK_CHANGED = 300,
19         ELDLM_LOCK_ABORTED = 301,
20         ELDLM_RESOURCE_FREED = 302,
21
22         ELDLM_NAMESPACE_EXISTS = 400,
23         ELDLM_BAD_NAMESPACE    = 401
24 } ldlm_error_t;
25
26 #define LDLM_NAMESPACE_SERVER 0
27 #define LDLM_NAMESPACE_CLIENT 1
28
29 #define LDLM_FL_LOCK_CHANGED   (1 << 0)
30 #define LDLM_FL_BLOCK_GRANTED  (1 << 1)
31 #define LDLM_FL_BLOCK_CONV     (1 << 2)
32 #define LDLM_FL_BLOCK_WAIT     (1 << 3)
33 #define LDLM_FL_DYING          (1 << 4)
34 #define LDLM_FL_AST_SENT       (1 << 5)
35
36 #define L2B(c) (1 << c)
37
38 /* compatibility matrix */
39 #define LCK_COMPAT_EX  L2B(LCK_NL)
40 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
41 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
42 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
43 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
44 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
45
46 static ldlm_mode_t lck_compat_array[] = {
47         [LCK_EX] LCK_COMPAT_EX,
48         [LCK_PW] LCK_COMPAT_PW,
49         [LCK_PR] LCK_COMPAT_PR,
50         [LCK_CW] LCK_COMPAT_CW,
51         [LCK_CR] LCK_COMPAT_CR,
52         [LCK_NL] LCK_COMPAT_NL
53 };
54
55 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
56 {
57        if (exist < LCK_EX || exist > LCK_NL)
58               LBUG();
59        if (new < LCK_EX || new > LCK_NL)
60               LBUG();
61
62        return (lck_compat_array[exist] & L2B(new));
63 }
64
65 /* 
66  * 
67  * cluster name spaces 
68  *
69  */
70
71 #define DLM_OST_NAMESPACE 1
72 #define DLM_MDS_NAMESPACE 2
73
74 /* XXX 
75    - do we just separate this by security domains and use a prefix for 
76      multiple namespaces in the same domain? 
77    - 
78 */
79
80 struct ldlm_namespace {
81         char                 *ns_name;
82         struct ptlrpc_client  ns_rpc_client; /* used for revocation callbacks */
83         __u32                 ns_client; /* is this a client-side lock tree? */
84         struct list_head     *ns_hash; /* hash table for ns */
85         __u32                 ns_refcount; /* count of resources in the hash */
86         struct list_head      ns_root_list; /* all root resources in ns */
87         spinlock_t            ns_lock; /* protects hash, refcount, list */
88 };
89
90 /* 
91  * 
92  * Resource hash table 
93  *
94  */
95
96 #define RES_HASH_BITS 14
97 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
98 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
99
100 struct ldlm_lock;
101
102 typedef int (*ldlm_lock_callback)(struct ldlm_lock *lock, struct ldlm_lock *new,
103                                   void *data, __u32 data_len,
104                                   struct ptlrpc_request **req);
105
106 struct ldlm_lock {
107         struct ldlm_resource *l_resource;
108         struct ldlm_lock     *l_parent;
109         struct list_head      l_children;
110         struct list_head      l_childof;
111         struct list_head      l_res_link; /*position in one of three res lists*/
112
113         ldlm_mode_t           l_req_mode;
114         ldlm_mode_t           l_granted_mode;
115
116         ldlm_lock_callback    l_completion_ast;
117         ldlm_lock_callback    l_blocking_ast;
118
119         struct ptlrpc_connection *l_connection;
120         struct ptlrpc_client *l_client;
121         __u32                 l_flags;
122         struct lustre_handle    l_remote_handle;
123         void                 *l_data;
124         __u32                 l_data_len;
125         void                 *l_cookie;
126         int                   l_cookie_len;
127         struct ldlm_extent    l_extent;
128         __u32                 l_version[RES_VERSION_SIZE];
129
130         __u32                 l_readers;
131         __u32                 l_writers;
132
133         /* If the lock is granted, a process sleeps on this waitq to learn when
134          * it's no longer in use.  If the lock is not granted, a process sleeps
135          * on this waitq to learn when it becomes granted. */
136         wait_queue_head_t     l_waitq;
137         spinlock_t            l_lock;
138 };
139
140 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
141 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
142                                ldlm_mode_t mode, void *data);
143
144 #define LDLM_PLAIN       10
145 #define LDLM_EXTENT      11
146 #define LDLM_MDSINTENT   12
147
148 #define LDLM_MIN_TYPE 10
149 #define LDLM_MAX_TYPE 12
150
151 extern ldlm_res_compat ldlm_res_compat_table []; 
152 extern ldlm_res_policy ldlm_res_policy_table []; 
153
154 struct ldlm_resource {
155         struct ldlm_namespace *lr_namespace;
156         struct list_head       lr_hash;
157         struct list_head       lr_rootlink; /* link all root resources in NS */
158         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
159         struct list_head       lr_children; /* list head for child resources */
160         struct list_head       lr_childof;  /* part of child list of parent */
161
162         struct list_head       lr_granted;
163         struct list_head       lr_converting;
164         struct list_head       lr_waiting;
165         ldlm_mode_t            lr_most_restr;
166         __u32                  lr_type; /* PLAIN, EXTENT, or MDSINTENT */
167         struct ldlm_resource  *lr_root;
168         __u64                  lr_name[RES_NAME_SIZE];
169         __u32                  lr_version[RES_VERSION_SIZE];
170         __u32                  lr_refcount;
171         spinlock_t             lr_lock; /* protects lists, refcount */
172         void                  *lr_tmp;
173 };
174
175 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
176 {
177         return (struct ldlm_extent *)(res->lr_name);
178 }
179
180 extern struct obd_ops ldlm_obd_ops;
181
182 #define LDLM_DEBUG(lock, format, a...)                          \
183 do {                                                            \
184         CDEBUG(D_DLMTRACE, "### " format                        \
185                " (%s: lock %p mode %d/%d on res %Lu (rc %d) "   \
186                " type %d remote %Lx)\n" , ## a,                  \
187                lock->l_resource->lr_namespace->ns_name, lock,   \
188                lock->l_granted_mode, lock->l_req_mode,          \
189                lock->l_resource->lr_name[0],                    \
190                lock->l_resource->lr_refcount,                   \
191                lock->l_resource->lr_type,                       \
192                lock->l_remote_handle.addr);                     \
193 } while (0)
194
195 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
196         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
197
198 /* ldlm_extent.c */
199 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
200 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, void *);
201
202 /* ldlm_lock.c */
203 void ldlm_lock_free(struct ldlm_lock *lock);
204 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
205 void ldlm_lock_addref(struct ldlm_lock *lock, __u32 mode);
206 void ldlm_lock_decref(struct ldlm_lock *lock, __u32 mode);
207 void ldlm_grant_lock(struct ldlm_resource *res, struct ldlm_lock *lock);
208 int ldlm_local_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
209                           void *cookie, int cookielen, ldlm_mode_t mode,
210                           struct lustre_handle *lockh);
211 ldlm_error_t ldlm_local_lock_create(struct ldlm_namespace *ns,
212                                     struct lustre_handle *parent_lock_handle,
213                                     __u64 *res_id, __u32 type,
214                                      ldlm_mode_t mode,
215                                     void *data,
216                                     __u32 data_len,
217                                     struct lustre_handle *lockh);
218 ldlm_error_t ldlm_local_lock_enqueue(struct lustre_handle *lockh,
219                                      void *cookie, int cookie_len,
220                                      int *flags,
221                                      ldlm_lock_callback completion,
222                                      ldlm_lock_callback blocking);
223 struct ldlm_resource *ldlm_local_lock_convert(struct lustre_handle *lockh,
224                                               int new_mode, int *flags);
225 struct ldlm_resource *ldlm_local_lock_cancel(struct ldlm_lock *lock);
226 void ldlm_reprocess_all(struct ldlm_resource *res);
227 void ldlm_lock_dump(struct ldlm_lock *lock);
228
229 /* ldlm_test.c */
230 int ldlm_test(struct obd_device *device, struct ptlrpc_connection *conn);
231
232 /* resource.c */
233 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
234 int ldlm_namespace_free(struct ldlm_namespace *ns);
235
236 /* resource.c - internal */
237 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
238                                         struct ldlm_resource *parent,
239                                         __u64 *name, __u32 type, int create);
240 int ldlm_resource_put(struct ldlm_resource *res);
241 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
242                             struct ldlm_lock *lock);
243 int ldlm_resource_del_lock(struct ldlm_lock *lock);
244 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
245 void ldlm_resource_dump(struct ldlm_resource *res);
246
247 /* ldlm_request.c */
248 int ldlm_cli_enqueue(struct ptlrpc_client *cl, 
249                      struct ptlrpc_connection *peer,
250                      struct ptlrpc_request *req,
251                      struct ldlm_namespace *ns,
252                      struct lustre_handle *parent_lock_handle,
253                      __u64 *res_id,
254                      __u32 type,
255                      void *cookie, int cookielen,
256                      ldlm_mode_t mode,
257                      int *flags,
258                      ldlm_lock_callback callback,
259                      void *data,
260                      __u32 data_len,
261                      struct lustre_handle *lockh);
262 int ldlm_cli_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
263                       void *data, __u32 data_len, struct ptlrpc_request **reqp);
264 int ldlm_cli_convert(struct ptlrpc_client *, struct lustre_handle *,
265                      int new_mode, int *flags);
266 int ldlm_cli_cancel(struct ptlrpc_client *, struct ldlm_lock *);
267
268 #endif /* __KERNEL__ */
269
270 /* ioctls for trying requests */
271 #define IOC_LDLM_TYPE                   'f'
272 #define IOC_LDLM_MIN_NR                 40
273
274 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
275 #define IOC_LDLM_MAX_NR                 41
276
277 #endif