Whamcloud - gitweb
b=1971
[fs/lustre-release.git] / lustre / include / linux / lustre_dlm.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * (visit-tags-table FILE)
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  */
5
6 #ifndef _LUSTRE_DLM_H__
7 #define _LUSTRE_DLM_H__
8
9 #ifdef __KERNEL__
10 # include <linux/proc_fs.h>
11 #endif 
12
13 #include <linux/lustre_lib.h>
14 #include <linux/lustre_net.h>
15 #include <linux/lustre_import.h>
16 #include <linux/lustre_handles.h>
17 #include <linux/lustre_export.h> /* for obd_export, for LDLM_DEBUG */
18
19 struct obd_ops;
20 struct obd_device;
21
22 #define OBD_LDLM_DEVICENAME  "ldlm"
23
24 #define LDLM_DEFAULT_LRU_SIZE 100
25
26 typedef enum {
27         ELDLM_OK = 0,
28
29         ELDLM_LOCK_CHANGED = 300,
30         ELDLM_LOCK_ABORTED = 301,
31         ELDLM_LOCK_REPLACED = 302,
32
33         ELDLM_NAMESPACE_EXISTS = 400,
34         ELDLM_BAD_NAMESPACE    = 401
35 } ldlm_error_t;
36
37 #define LDLM_NAMESPACE_SERVER 0
38 #define LDLM_NAMESPACE_CLIENT 1
39
40 #define LDLM_FL_LOCK_CHANGED   0x000001 /* extent, mode, or resource changed */
41
42 /* If the server returns one of these flags, then the lock was put on that list.
43  * If the client sends one of these flags (during recovery ONLY!), it wants the
44  * lock added to the specified list, no questions asked. -p */
45 #define LDLM_FL_BLOCK_GRANTED  0x000002
46 #define LDLM_FL_BLOCK_CONV     0x000004
47 #define LDLM_FL_BLOCK_WAIT     0x000008
48
49 #define LDLM_FL_CBPENDING      0x000010 /* this lock is being destroyed */
50 #define LDLM_FL_AST_SENT       0x000020 /* blocking or cancel packet was sent */
51 #define LDLM_FL_WAIT_NOREPROC  0x000040 /* not a real flag, not saved in lock */
52 #define LDLM_FL_CANCEL         0x000080 /* cancellation callback already run */
53
54 /* Lock is being replayed.  This could probably be implied by the fact that one
55  * of BLOCK_{GRANTED,CONV,WAIT} is set, but that is pretty dangerous. */
56 #define LDLM_FL_REPLAY         0x000100
57
58 #define LDLM_FL_INTENT_ONLY    0x000200 /* don't grant lock, just do intent */
59 #define LDLM_FL_LOCAL_ONLY     0x000400 /* see ldlm_cli_cancel_unused */
60
61 /* don't run the cancel callback under ldlm_cli_cancel_unused */
62 #define LDLM_FL_NO_CALLBACK    0x000800
63
64 #define LDLM_FL_HAS_INTENT     0x001000 /* lock request has intent */
65 #define LDLM_FL_CANCELING      0x002000 /* lock cancel has already been sent */
66 #define LDLM_FL_LOCAL          0x004000 /* local lock (ie, no srv/cli split) */
67 #define LDLM_FL_WARN           0x008000 /* see ldlm_cli_cancel_unused */
68 #define LDLM_FL_DISCARD_DATA   0x010000 /* discard (no writeback) on cancel */
69 #define LDLM_FL_MATCH_DATA     0x020000 /* see ldlm_lock_match */
70
71 /* These are flags that are mapped into the flags and ASTs of blocking locks */
72 #define LDLM_AST_DISCARD_DATA  0x80000000 /* Add FL_DISCARD to blocking ASTs */
73
74 /* Flags sent in AST lock_flags to be mapped into the receiving lock. */
75 #define LDLM_AST_FLAGS         (LDLM_FL_DISCARD_DATA)
76
77 /* The blocking callback is overloaded to perform two functions.  These flags
78  * indicate which operation should be performed. */
79 #define LDLM_CB_BLOCKING    1
80 #define LDLM_CB_CANCELING   2
81
82 #define L2B(c) (1 << c)
83
84 /* compatibility matrix */
85 #define LCK_COMPAT_EX  L2B(LCK_NL)
86 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
87 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
88 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
89 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
90 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
91
92 static ldlm_mode_t lck_compat_array[] = {
93         [LCK_EX] LCK_COMPAT_EX,
94         [LCK_PW] LCK_COMPAT_PW,
95         [LCK_PR] LCK_COMPAT_PR,
96         [LCK_CW] LCK_COMPAT_CW,
97         [LCK_CR] LCK_COMPAT_CR,
98         [LCK_NL] LCK_COMPAT_NL
99 };
100
101 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
102 {
103        if (exist < LCK_EX || exist > LCK_NL)
104               LBUG();
105        if (new < LCK_EX || new > LCK_NL)
106               LBUG();
107
108        return (lck_compat_array[exist] & L2B(new));
109 }
110
111 /*
112  *
113  * cluster name spaces
114  *
115  */
116
117 #define DLM_OST_NAMESPACE 1
118 #define DLM_MDS_NAMESPACE 2
119
120 /* XXX
121    - do we just separate this by security domains and use a prefix for
122      multiple namespaces in the same domain?
123    -
124 */
125
126 struct ldlm_namespace {
127         char                  *ns_name;
128         __u32                  ns_client; /* is this a client-side lock tree? */
129         struct list_head      *ns_hash; /* hash table for ns */
130         __u32                  ns_refcount; /* count of resources in the hash */
131         struct list_head       ns_root_list; /* all root resources in ns */
132         struct lustre_lock     ns_lock; /* protects hash, refcount, list */
133         struct list_head       ns_list_chain; /* position in global NS list */
134         /* 
135         struct proc_dir_entry *ns_proc_dir;
136         */
137
138         struct list_head       ns_unused_list; /* all root resources in ns */
139         unsigned int           ns_nr_unused;
140         unsigned int           ns_max_unused;
141
142         spinlock_t             ns_counter_lock;
143         __u64                  ns_locks;
144         __u64                  ns_resources;
145 };
146
147 /*
148  *
149  * Resource hash table
150  *
151  */
152
153 #define RES_HASH_BITS 10
154 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
155 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
156
157 struct ldlm_lock;
158
159 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
160                                       struct ldlm_lock_desc *new, void *data,
161                                       int flag);
162 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags,
163                                         void *data);
164
165 struct ldlm_lock {
166         struct portals_handle l_handle; // must be first in the structure
167         atomic_t              l_refc;
168         struct ldlm_resource *l_resource;
169         struct ldlm_lock     *l_parent;
170         struct list_head      l_children;
171         struct list_head      l_childof;
172         struct list_head      l_lru;
173         struct list_head      l_res_link; // position in one of three res lists
174         struct list_head      l_export_chain; // per-export chain of locks
175         struct list_head      l_pending_chain; // locks with callbacks pending
176         unsigned long         l_callback_timeout;
177
178         ldlm_mode_t           l_req_mode;
179         ldlm_mode_t           l_granted_mode;
180
181         ldlm_completion_callback l_completion_ast;
182         ldlm_blocking_callback   l_blocking_ast;
183
184         struct obd_export    *l_export;
185         struct lustre_handle *l_connh;
186         __u32                 l_flags;
187         struct lustre_handle  l_remote_handle;
188         void                 *l_data;
189         struct ldlm_extent    l_extent;
190         __u32                 l_version[RES_VERSION_SIZE];
191
192         __u32                 l_readers;
193         __u32                 l_writers;
194         __u8                  l_destroyed;
195
196         /* If the lock is granted, a process sleeps on this waitq to learn when
197          * it's no longer in use.  If the lock is not granted, a process sleeps
198          * on this waitq to learn when it becomes granted. */
199         wait_queue_head_t     l_waitq;
200         struct timeval        l_enqueued_time;
201 };
202
203 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
204 typedef int (*ldlm_res_policy)(struct ldlm_namespace *, struct ldlm_lock **,
205                                void *req_cookie, ldlm_mode_t mode, int flags,
206                                void *data);
207
208 #define LDLM_PLAIN       10
209 #define LDLM_EXTENT      11
210
211 #define LDLM_MIN_TYPE 10
212 #define LDLM_MAX_TYPE 11
213
214 struct ldlm_resource {
215         struct ldlm_namespace *lr_namespace;
216         struct list_head       lr_hash;
217         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
218         struct list_head       lr_children; /* list head for child resources */
219         struct list_head       lr_childof;  /* part of ns_root_list if root res,
220                                              * part of lr_children if child */
221
222         struct list_head       lr_granted;
223         struct list_head       lr_converting;
224         struct list_head       lr_waiting;
225         ldlm_mode_t            lr_most_restr;
226         __u32                  lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
227         struct ldlm_resource  *lr_root;
228         struct ldlm_res_id     lr_name;
229         __u32                  lr_version[RES_VERSION_SIZE];
230         atomic_t               lr_refcount;
231
232         /* lr_tmp holds a list head temporarily, during the building of a work
233          * queue.  see ldlm_add_ast_work_item and ldlm_run_ast_work */
234         void                  *lr_tmp;
235 };
236
237 struct ldlm_ast_work {
238         struct ldlm_lock *w_lock;
239         int               w_blocking;
240         struct ldlm_lock_desc w_desc;
241         struct list_head   w_list;
242         int w_flags;
243         void *w_data;
244         int w_datalen;
245 };
246
247 extern struct obd_ops ldlm_obd_ops;
248
249 extern char *ldlm_lockname[];
250 extern char *ldlm_typename[];
251 extern char *ldlm_it2str(int it);
252
253 #define __LDLM_DEBUG(level, lock, format, a...)                               \
254 do {                                                                          \
255         if (lock->l_resource == NULL) {                                       \
256                 CDEBUG(level, "### " format                                   \
257                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "\
258                        "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "   \
259                        LPX64"\n" , ## a, lock, lock->l_handle.h_cookie,       \
260                        atomic_read(&lock->l_refc),                            \
261                        lock->l_readers, lock->l_writers,                      \
262                        ldlm_lockname[lock->l_granted_mode],                   \
263                        ldlm_lockname[lock->l_req_mode],                       \
264                        lock->l_flags, lock->l_remote_handle.cookie);          \
265                 break;                                                        \
266         }                                                                     \
267         if (lock->l_resource->lr_type == LDLM_EXTENT) {                       \
268                 CDEBUG(level, "### " format                                   \
269                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "  \
270                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64\
271                        "] flags: %x remote: "LPX64"\n" , ## a,                \
272                        lock->l_resource->lr_namespace->ns_name, lock,         \
273                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),   \
274                        lock->l_readers, lock->l_writers,                      \
275                        ldlm_lockname[lock->l_granted_mode],                   \
276                        ldlm_lockname[lock->l_req_mode],                       \
277                        lock->l_resource->lr_name.name[0],                     \
278                        lock->l_resource->lr_name.name[1],                     \
279                        atomic_read(&lock->l_resource->lr_refcount),           \
280                        ldlm_typename[lock->l_resource->lr_type],              \
281                        lock->l_extent.start, lock->l_extent.end,              \
282                        lock->l_flags, lock->l_remote_handle.cookie);          \
283                 break;                                                        \
284         }                                                                     \
285         {                                                                     \
286                 CDEBUG(level, "### " format                                   \
287                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "  \
288                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "     \
289                        "remote: "LPX64"\n" , ## a,                            \
290                        lock->l_resource->lr_namespace->ns_name,               \
291                        lock, lock->l_handle.h_cookie,                         \
292                        atomic_read (&lock->l_refc),                           \
293                        lock->l_readers, lock->l_writers,                      \
294                        ldlm_lockname[lock->l_granted_mode],                   \
295                        ldlm_lockname[lock->l_req_mode],                       \
296                        lock->l_resource->lr_name.name[0],                     \
297                        lock->l_resource->lr_name.name[1],                     \
298                        atomic_read(&lock->l_resource->lr_refcount),           \
299                        ldlm_typename[lock->l_resource->lr_type],              \
300                        lock->l_flags, lock->l_remote_handle.cookie);          \
301         }                                                                     \
302 } while (0)
303
304 #define LDLM_DEBUG(lock, format, a...) __LDLM_DEBUG(D_DLMTRACE, lock, \
305                                                     format, ## a)
306 #define LDLM_ERROR(lock, format, a...) __LDLM_DEBUG(D_ERROR, lock, format, ## a)
307
308 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
309         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
310
311 /*
312  * Iterators.
313  */
314
315 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
316 #define LDLM_ITER_STOP     2 /* stop iterating */
317
318 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
319 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
320
321 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
322                           void *closure);
323 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
324                            void *closure);
325 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
326                                ldlm_res_iterator_t iter, void *closure);
327
328 int ldlm_replay_locks(struct obd_import *imp);
329 void ldlm_change_cbdata(struct ldlm_namespace *, struct ldlm_res_id *,
330                         ldlm_iterator_t iter, void *data);
331
332 /* ldlm_extent.c */
333 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
334 int ldlm_extent_policy(struct ldlm_namespace *, struct ldlm_lock **, void *,
335                        ldlm_mode_t, int flags, void *);
336
337 /* ldlm_lockd.c */
338 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
339                              void *data, int flag);
340 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data);
341 int ldlm_handle_enqueue(struct ptlrpc_request *req, ldlm_completion_callback,
342                         ldlm_blocking_callback);
343 int ldlm_handle_convert(struct ptlrpc_request *req);
344 int ldlm_handle_cancel(struct ptlrpc_request *req);
345 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
346
347 /* ldlm_lock.c */
348 void ldlm_register_intent(ldlm_res_policy arg);
349 void ldlm_unregister_intent(void);
350 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
351 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int flags);
352 void ldlm_cancel_callback(struct ldlm_lock *);
353 int ldlm_lock_set_data(struct lustre_handle *, void *data);
354 void ldlm_lock_remove_from_lru(struct ldlm_lock *);
355 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *,
356                                       struct lustre_handle *);
357
358 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
359 {
360         return __ldlm_handle2lock(h, 0);
361 }
362
363 #define LDLM_LOCK_PUT(lock)                     \
364 do {                                            \
365         /*LDLM_DEBUG((lock), "put");*/          \
366         ldlm_lock_put(lock);                    \
367 } while (0)
368
369 #define LDLM_LOCK_GET(lock)                     \
370 ({                                              \
371         ldlm_lock_get(lock);                    \
372         /*LDLM_DEBUG((lock), "get");*/          \
373         lock;                                   \
374 })
375
376 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
377 void ldlm_lock_put(struct ldlm_lock *lock);
378 void ldlm_lock_destroy(struct ldlm_lock *lock);
379 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
380 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
381 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
382 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
383 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode);
384 int ldlm_lock_match(struct ldlm_namespace *ns, int flags, struct ldlm_res_id *,
385                     __u32 type, void *cookie, int cookielen, ldlm_mode_t mode,
386                     void *data, struct lustre_handle *);
387 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
388                                         int *flags);
389 void ldlm_lock_cancel(struct ldlm_lock *lock);
390 void ldlm_cancel_locks_for_export(struct obd_export *export);
391 int ldlm_run_ast_work(struct list_head *rpc_list);
392 void ldlm_reprocess_all(struct ldlm_resource *res);
393 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns);
394 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos);
395 void ldlm_lock_dump_handle(int level, struct lustre_handle *);
396
397 /* ldlm_test.c */
398 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
399 int ldlm_regression_start(struct obd_device *obddev,
400                           struct lustre_handle *connh,
401                           unsigned int threads, unsigned int max_locks_in,
402                           unsigned int num_resources_in,
403                           unsigned int num_extents_in);
404 int ldlm_regression_stop(void);
405
406
407 /* resource.c */
408 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
409 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
410 int ldlm_namespace_free(struct ldlm_namespace *ns);
411 int ldlm_proc_setup(struct obd_device *obd);
412 void ldlm_proc_cleanup(struct obd_device *obd);
413
414 /* resource.c - internal */
415 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
416                                         struct ldlm_resource *parent,
417                                         struct ldlm_res_id, __u32 type,
418                                         int create);
419 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
420 int ldlm_resource_putref(struct ldlm_resource *res);
421 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
422                             struct ldlm_lock *lock);
423 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
424 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
425 void ldlm_dump_all_namespaces(void);
426 void ldlm_namespace_dump(struct ldlm_namespace *);
427 void ldlm_resource_dump(struct ldlm_resource *);
428 int ldlm_lock_change_resource(struct ldlm_namespace *, struct ldlm_lock *,
429                               struct ldlm_res_id);
430
431 /* ldlm_request.c */
432 int ldlm_expired_completion_wait(void *data);
433 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data);
434 int ldlm_cli_enqueue(struct lustre_handle *conn,
435                      struct ptlrpc_request *req,
436                      struct ldlm_namespace *ns,
437                      struct lustre_handle *parent_lock_handle,
438                      struct ldlm_res_id,
439                      __u32 type,
440                      void *cookie, int cookielen,
441                      ldlm_mode_t mode,
442                      int *flags,
443                      ldlm_completion_callback completion,
444                      ldlm_blocking_callback callback,
445                      void *data,
446                      struct lustre_handle *lockh);
447 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
448                     void *data, __u32 data_len);
449 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
450 int ldlm_cli_cancel(struct lustre_handle *lockh);
451 int ldlm_cli_cancel_unused(struct ldlm_namespace *, struct ldlm_res_id *,
452                            int flags, void *opaque);
453
454 /* mds/handler.c */
455 /* This has to be here because recurisve inclusion sucks. */
456 int intent_disposition(struct ldlm_reply *rep, int flag);
457 void intent_set_disposition(struct ldlm_reply *rep, int flag);
458 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
459                      void *data, int flag);
460
461
462 /* ioctls for trying requests */
463 #define IOC_LDLM_TYPE                   'f'
464 #define IOC_LDLM_MIN_NR                 40
465
466 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
467 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
468 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
469 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
470 #define IOC_LDLM_MAX_NR                 43
471
472 #endif