Whamcloud - gitweb
- move the peter branch changes to the head
[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_CBPENDING      (1 << 4)
34 #define LDLM_FL_AST_SENT       (1 << 5)
35 #define LDLM_FL_DESTROYED      (1 << 6)
36 #define LDLM_FL_WAIT_NOREPROC  (1 << 7)
37
38 #define L2B(c) (1 << c)
39
40 /* compatibility matrix */
41 #define LCK_COMPAT_EX  L2B(LCK_NL)
42 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
43 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
44 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
45 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
46 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
47
48 static ldlm_mode_t lck_compat_array[] = {
49         [LCK_EX] LCK_COMPAT_EX,
50         [LCK_PW] LCK_COMPAT_PW,
51         [LCK_PR] LCK_COMPAT_PR,
52         [LCK_CW] LCK_COMPAT_CW,
53         [LCK_CR] LCK_COMPAT_CR,
54         [LCK_NL] LCK_COMPAT_NL
55 };
56
57 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
58 {
59        if (exist < LCK_EX || exist > LCK_NL)
60               LBUG();
61        if (new < LCK_EX || new > LCK_NL)
62               LBUG();
63
64        return (lck_compat_array[exist] & L2B(new));
65 }
66
67 /* 
68  * 
69  * cluster name spaces 
70  *
71  */
72
73 #define DLM_OST_NAMESPACE 1
74 #define DLM_MDS_NAMESPACE 2
75
76 /* XXX 
77    - do we just separate this by security domains and use a prefix for 
78      multiple namespaces in the same domain? 
79    - 
80 */
81
82 struct ldlm_namespace {
83         char                 *ns_name;
84         struct ptlrpc_client  ns_rpc_client; /* used for revocation callbacks */
85         __u32                 ns_client; /* is this a client-side lock tree? */
86         struct list_head     *ns_hash; /* hash table for ns */
87         __u32                 ns_refcount; /* count of resources in the hash */
88         struct list_head      ns_root_list; /* all root resources in ns */
89         struct lustre_lock    ns_lock; /* protects hash, refcount, list */
90 };
91
92 /* 
93  * 
94  * Resource hash table 
95  *
96  */
97
98 #define RES_HASH_BITS 14
99 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
100 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
101
102 struct ldlm_lock;
103
104 typedef int (*ldlm_lock_callback)(struct lustre_handle *lockh,
105                                   struct ldlm_lock_desc *new, void *data,
106                                   __u32 data_len);
107
108 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags); 
109
110 struct ldlm_lock {
111         __u64                  l_random;
112         int                   l_refc;
113         struct ldlm_resource *l_resource;
114         struct ldlm_lock     *l_parent;
115         struct list_head      l_children;
116         struct list_head      l_childof;
117         struct list_head      l_res_link; /*position in one of three res lists*/
118
119         ldlm_mode_t           l_req_mode;
120         ldlm_mode_t           l_granted_mode;
121
122         ldlm_completion_callback    l_completion_ast;
123         ldlm_lock_callback    l_blocking_ast;
124
125         struct ptlrpc_connection *l_connection;
126         struct ptlrpc_client *l_client;
127         struct lustre_handle *l_connh;
128         __u32                 l_flags;
129         struct lustre_handle    l_remote_handle;
130         void                 *l_data;
131         __u32                 l_data_len;
132         void                 *l_cookie;
133         int                   l_cookie_len;
134         struct ldlm_extent    l_extent;
135         __u32                 l_version[RES_VERSION_SIZE];
136
137         __u32                 l_readers;
138         __u32                 l_writers;
139
140         /* If the lock is granted, a process sleeps on this waitq to learn when
141          * it's no longer in use.  If the lock is not granted, a process sleeps
142          * on this waitq to learn when it becomes granted. */
143         wait_queue_head_t     l_waitq;
144 };
145
146 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
147 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
148                                ldlm_mode_t mode, void *data);
149
150 #define LDLM_PLAIN       10
151 #define LDLM_EXTENT      11
152 #define LDLM_MDSINTENT   12
153
154 #define LDLM_MIN_TYPE 10
155 #define LDLM_MAX_TYPE 12
156
157 extern ldlm_res_compat ldlm_res_compat_table []; 
158 extern ldlm_res_policy ldlm_res_policy_table []; 
159
160 struct ldlm_resource {
161         struct ldlm_namespace *lr_namespace;
162         struct list_head       lr_hash;
163         struct list_head       lr_rootlink; /* link all root resources in NS */
164         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
165         struct list_head       lr_children; /* list head for child resources */
166         struct list_head       lr_childof;  /* part of child list of parent */
167
168         struct list_head       lr_granted;
169         struct list_head       lr_converting;
170         struct list_head       lr_waiting;
171         ldlm_mode_t            lr_most_restr;
172         __u32                  lr_type; /* PLAIN, EXTENT, or MDSINTENT */
173         struct ldlm_resource  *lr_root;
174         __u64                  lr_name[RES_NAME_SIZE];
175         __u32                  lr_version[RES_VERSION_SIZE];
176         atomic_t               lr_refcount;
177         void                  *lr_tmp;
178 };
179
180 struct ldlm_ast_work { 
181         struct ldlm_lock *w_lock;
182         int               w_blocking;
183         struct ldlm_lock_desc w_desc;
184         struct list_head   w_list;
185         int w_flags;
186         void *w_data;
187         int w_datalen;
188 };
189         
190 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
191 {
192         return (struct ldlm_extent *)(res->lr_name);
193 }
194
195 extern struct obd_ops ldlm_obd_ops;
196
197
198 extern char *ldlm_lockname[];
199 extern char *ldlm_typename[];
200 extern char *ldlm_it2str(int it);
201
202 #define LDLM_DEBUG(lock, format, a...)                                  \
203 do {                                                                    \
204         if (lock->l_resource == NULL)                                   \
205                 CDEBUG(D_DLMTRACE, "### " format                        \
206                        " (UNKNOWN: lock %p(rc=%d/%d,%d) mode %s/%s on " \
207                        "res \?\? (rc=\?\?) type \?\?\? remote %Lx)\n" , \
208                        ## a, lock, lock->l_refc, lock->l_readers,       \
209                        lock->l_writers,                                 \
210                        ldlm_lockname[lock->l_granted_mode],             \
211                        ldlm_lockname[lock->l_req_mode],                 \
212                        lock->l_remote_handle.addr);                     \
213         else                                                            \
214                 CDEBUG(D_DLMTRACE, "### " format                        \
215                        " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "  \
216                        "%Lu (rc=%d) type %s remote %Lx)\n" , ## a,      \
217                        lock->l_resource->lr_namespace->ns_name, lock,   \
218                        lock->l_refc, lock->l_readers, lock->l_writers,  \
219                        ldlm_lockname[lock->l_granted_mode],             \
220                        ldlm_lockname[lock->l_req_mode],                 \
221                        lock->l_resource->lr_name[0],                    \
222                        atomic_read(&lock->l_resource->lr_refcount),     \
223                        ldlm_typename[lock->l_resource->lr_type],        \
224                        lock->l_remote_handle.addr);                     \
225 } while (0)
226
227 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
228         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
229
230 /* ldlm_extent.c */
231 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
232 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, void *);
233
234 /* ldlm_lockd.c */
235 int ldlm_handle_enqueue(struct ptlrpc_request *req);
236 int ldlm_handle_convert(struct ptlrpc_request *req);
237 int ldlm_handle_cancel(struct ptlrpc_request *req);
238
239 /* ldlm_lock.c */
240 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
241                                      ldlm_mode_t mode, void *data));
242 void ldlm_unregister_intent(void);
243 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
244 struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle);
245 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
246
247 #define LDLM_LOCK_PUT(lock)                     \
248 do {                                            \
249         LDLM_DEBUG(lock, "put");                \
250         ldlm_lock_put(lock);                    \
251 } while (0)
252
253 #define LDLM_LOCK_GET(lock)                     \
254 ({                                              \
255         ldlm_lock_get(lock);                    \
256         LDLM_DEBUG(lock, "get");                \
257         lock;                                   \
258 })
259
260 void ldlm_lock_put(struct ldlm_lock *lock);
261 void ldlm_lock_destroy(struct ldlm_lock *lock);
262 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
263 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
264 void ldlm_lock_addref_internal(struct ldlm_lock* , __u32 mode);
265 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
266 void ldlm_grant_lock(struct ldlm_lock *lock);
267 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
268                     void *cookie, int cookielen, ldlm_mode_t mode,
269                     struct lustre_handle *lockh);
270 struct ldlm_lock *
271 ldlm_lock_create(struct ldlm_namespace *ns,
272                  struct lustre_handle *parent_lock_handle,
273                  __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
274                  __u32 data_len);
275 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
276                                int cookie_len, int *flags,
277                                ldlm_completion_callback completion,
278                                ldlm_lock_callback blocking);
279 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
280                                         int *flags);
281 void ldlm_lock_cancel(struct ldlm_lock *lock);
282 void ldlm_run_ast_work(struct list_head *rpc_list);
283 void ldlm_reprocess_all(struct ldlm_resource *res);
284 void ldlm_lock_dump(struct ldlm_lock *lock);
285
286 /* ldlm_test.c */
287 int ldlm_test(struct obd_device *device, struct ptlrpc_connection *conn);
288
289 /* resource.c */
290 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
291 int ldlm_namespace_free(struct ldlm_namespace *ns);
292
293 /* resource.c - internal */
294 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
295                                         struct ldlm_resource *parent,
296                                         __u64 *name, __u32 type, int create);
297 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
298 int ldlm_resource_put(struct ldlm_resource *res);
299 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
300                             struct ldlm_lock *lock);
301 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
302 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
303 void ldlm_resource_dump(struct ldlm_resource *res);
304 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3]);
305
306 /* ldlm_request.c */
307 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
308 int ldlm_cli_enqueue(struct lustre_handle *conn,
309                      struct ptlrpc_request *req,
310                      struct ldlm_namespace *ns,
311                      struct lustre_handle *parent_lock_handle,
312                      __u64 *res_id,
313                      __u32 type,
314                      void *cookie, int cookielen,
315                      ldlm_mode_t mode,
316                      int *flags,
317                      ldlm_completion_callback completion,
318                      ldlm_lock_callback callback,
319                      void *data,
320                      __u32 data_len,
321                      struct lustre_handle *lockh);
322 int ldlm_match_or_enqueue(struct lustre_handle *connh, 
323                           struct ptlrpc_request *req,
324                           struct ldlm_namespace *ns,
325                           struct lustre_handle *parent_lock_handle,
326                           __u64 *res_id,
327                           __u32 type,
328                           void *cookie, int cookielen,
329                           ldlm_mode_t mode,
330                           int *flags,
331                           ldlm_completion_callback completion,
332                           ldlm_lock_callback callback,
333                           void *data,
334                           __u32 data_len,
335                           struct lustre_handle *lockh);
336 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
337                     void *data, __u32 data_len);
338 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
339 int ldlm_cli_cancel(struct lustre_handle *lockh);
340
341 #endif /* __KERNEL__ */
342
343 /* ioctls for trying requests */
344 #define IOC_LDLM_TYPE                   'f'
345 #define IOC_LDLM_MIN_NR                 40
346
347 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
348 #define IOC_LDLM_MAX_NR                 41
349
350 #endif