Whamcloud - gitweb
99c1785ba17c0b29dc8a7cdabf4e78fa0b967c88
[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
70 /* file & record locking */
71 #define LDLM_FL_BLOCK_NOWAIT   0x040000 // server told not to wait if blocked
72 #define LDLM_FL_TEST_LOCK      0x080000 // return blocking lock
73
74 /* These are flags that are mapped into the flags and ASTs of blocking locks */
75 #define LDLM_AST_DISCARD_DATA  0x80000000 /* Add FL_DISCARD to blocking ASTs */
76
77 /* Flags sent in AST lock_flags to be mapped into the receiving lock. */
78 #define LDLM_AST_FLAGS         (LDLM_FL_DISCARD_DATA)
79
80 /* The blocking callback is overloaded to perform two functions.  These flags
81  * indicate which operation should be performed. */
82 #define LDLM_CB_BLOCKING    1
83 #define LDLM_CB_CANCELING   2
84
85 #define L2B(c) (1 << c)
86
87 /* compatibility matrix */
88 #define LCK_COMPAT_EX  L2B(LCK_NL)
89 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
90 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
91 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
92 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
93 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
94
95 static ldlm_mode_t lck_compat_array[] = {
96         [LCK_EX] LCK_COMPAT_EX,
97         [LCK_PW] LCK_COMPAT_PW,
98         [LCK_PR] LCK_COMPAT_PR,
99         [LCK_CW] LCK_COMPAT_CW,
100         [LCK_CR] LCK_COMPAT_CR,
101         [LCK_NL] LCK_COMPAT_NL
102 };
103
104 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
105 {
106        LASSERT(exist >= LCK_EX && exist <= LCK_NL);
107        LASSERT(new >= LCK_EX && new <= LCK_NL);
108
109        return (lck_compat_array[exist] & L2B(new));
110 }
111
112 /*
113  *
114  * cluster name spaces
115  *
116  */
117
118 #define DLM_OST_NAMESPACE 1
119 #define DLM_MDS_NAMESPACE 2
120
121 /* XXX
122    - do we just separate this by security domains and use a prefix for
123      multiple namespaces in the same domain?
124    -
125 */
126
127 struct ldlm_namespace {
128         char                  *ns_name;
129         __u32                  ns_client; /* is this a client-side lock tree? */
130         struct list_head      *ns_hash; /* hash table for ns */
131         __u32                  ns_refcount; /* count of resources in the hash */
132         struct list_head       ns_root_list; /* all root resources in ns */
133         struct lustre_lock     ns_lock; /* protects hash, refcount, list */
134         struct list_head       ns_list_chain; /* position in global NS list */
135         /* 
136         struct proc_dir_entry *ns_proc_dir;
137         */
138
139         struct list_head       ns_unused_list; /* all root resources in ns */
140         int                    ns_nr_unused;
141         unsigned int           ns_max_unused;
142
143         spinlock_t             ns_counter_lock;
144         __u64                  ns_locks;
145         __u64                  ns_resources;
146 };
147
148 /*
149  *
150  * Resource hash table
151  *
152  */
153
154 #define RES_HASH_BITS 10
155 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
156 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
157
158 struct ldlm_lock;
159
160 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
161                                       struct ldlm_lock_desc *new, void *data,
162                                       int flag);
163 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags,
164                                         void *data);
165
166 struct ldlm_lock {
167         struct portals_handle l_handle; // must be first in the structure
168         atomic_t              l_refc;
169         struct ldlm_resource *l_resource;
170         struct ldlm_lock     *l_parent;
171         struct list_head      l_children;
172         struct list_head      l_childof;
173         struct list_head      l_lru;
174         struct list_head      l_res_link; // position in one of three res lists
175         struct list_head      l_export_chain; // per-export chain of locks
176         struct list_head      l_pending_chain; // locks with callbacks pending
177         unsigned long         l_callback_timeout;
178
179         ldlm_mode_t           l_req_mode;
180         ldlm_mode_t           l_granted_mode;
181
182         ldlm_completion_callback l_completion_ast;
183         ldlm_blocking_callback   l_blocking_ast;
184         void                    *l_ast_data;
185
186         struct obd_export    *l_export;
187         /* XXX phil can fix this, I'm sure */
188         struct obd_export    *l_conn_export;
189 //        struct lustre_handle *l_connh;
190         __u32                 l_flags;
191         struct lustre_handle  l_remote_handle;
192         ldlm_policy_data_t    l_policy_data;
193         __u32                 l_version[RES_VERSION_SIZE];
194
195         __u32                 l_readers;
196         __u32                 l_writers;
197         __u8                  l_destroyed;
198
199         /* If the lock is granted, a process sleeps on this waitq to learn when
200          * it's no longer in use.  If the lock is not granted, a process sleeps
201          * on this waitq to learn when it becomes granted. */
202         wait_queue_head_t     l_waitq;
203         struct timeval        l_enqueued_time;
204 };
205
206 typedef int (*ldlm_res_policy)(struct ldlm_namespace *, struct ldlm_lock **,
207                                void *req_cookie, ldlm_mode_t mode, int flags,
208                                void *data);
209
210 #define LDLM_PLAIN       10
211 #define LDLM_EXTENT      11
212 #define LDLM_FLOCK       12
213
214 #define LDLM_MIN_TYPE 10
215 #define LDLM_MAX_TYPE 12
216
217 struct ldlm_resource {
218         struct ldlm_namespace *lr_namespace;
219         struct list_head       lr_hash;
220         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
221         struct list_head       lr_children; /* list head for child resources */
222         struct list_head       lr_childof;  /* part of ns_root_list if root res,
223                                              * part of lr_children if child */
224
225         struct list_head       lr_granted;
226         struct list_head       lr_converting;
227         struct list_head       lr_waiting;
228         ldlm_mode_t            lr_most_restr;
229         __u32                  lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
230         struct ldlm_resource  *lr_root;
231         struct ldlm_res_id     lr_name;
232         __u32                  lr_version[RES_VERSION_SIZE];
233         atomic_t               lr_refcount;
234
235         /* lr_tmp holds a list head temporarily, during the building of a work
236          * queue.  see ldlm_add_ast_work_item and ldlm_run_ast_work */
237         void                  *lr_tmp;
238 };
239
240 struct ldlm_ast_work {
241         struct ldlm_lock *w_lock;
242         int               w_blocking;
243         struct ldlm_lock_desc w_desc;
244         struct list_head   w_list;
245         int w_flags;
246         void *w_data;
247         int w_datalen;
248 };
249
250 extern struct obd_ops ldlm_obd_ops;
251
252 extern char *ldlm_lockname[];
253 extern char *ldlm_typename[];
254 extern char *ldlm_it2str(int it);
255
256 #define __LDLM_DEBUG(level, lock, format, a...)                               \
257 do {                                                                          \
258         if (lock->l_resource == NULL) {                                       \
259                 CDEBUG(level, "### " format                                   \
260                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "\
261                        "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "   \
262                        LPX64" expref: %d\n" , ## a, lock,                     \
263                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),   \
264                        lock->l_readers, lock->l_writers,                      \
265                        ldlm_lockname[lock->l_granted_mode],                   \
266                        ldlm_lockname[lock->l_req_mode],                       \
267                        lock->l_flags, lock->l_remote_handle.cookie,           \
268                        lock->l_export ?                                       \
269                        atomic_read(&lock->l_export->exp_refcount) : -99);     \
270                 break;                                                        \
271         }                                                                     \
272         if (lock->l_resource->lr_type == LDLM_EXTENT) {                       \
273                 CDEBUG(level, "### " format                                   \
274                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "  \
275                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64\
276                        "] flags: %x remote: "LPX64" expref: %d\n" , ## a,     \
277                        lock->l_resource->lr_namespace->ns_name, lock,         \
278                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),   \
279                        lock->l_readers, lock->l_writers,                      \
280                        ldlm_lockname[lock->l_granted_mode],                   \
281                        ldlm_lockname[lock->l_req_mode],                       \
282                        lock->l_resource->lr_name.name[0],                     \
283                        lock->l_resource->lr_name.name[1],                     \
284                        atomic_read(&lock->l_resource->lr_refcount),           \
285                        ldlm_typename[lock->l_resource->lr_type],              \
286                        lock->l_policy_data.l_extent.start,                    \
287                        lock->l_policy_data.l_extent.end,                      \
288                        lock->l_flags, lock->l_remote_handle.cookie,           \
289                        lock->l_export ?                                       \
290                        atomic_read(&lock->l_export->exp_refcount) : -99);     \
291                 break;                                                        \
292         }                                                                     \
293         if (lock->l_resource->lr_type == LDLM_FLOCK) {                        \
294                 CDEBUG(level, "### " format                                   \
295                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "  \
296                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "       \
297                        "["LPU64"->"LPU64"] flags: %x remote: "LPX64           \
298                        " expref: %d\n" , ## a,                                \
299                        lock->l_resource->lr_namespace->ns_name, lock,         \
300                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),   \
301                        lock->l_readers, lock->l_writers,                      \
302                        ldlm_lockname[lock->l_granted_mode],                   \
303                        ldlm_lockname[lock->l_req_mode],                       \
304                        lock->l_resource->lr_name.name[0],                     \
305                        lock->l_resource->lr_name.name[1],                     \
306                        atomic_read(&lock->l_resource->lr_refcount),           \
307                        ldlm_typename[lock->l_resource->lr_type],              \
308                        lock->l_policy_data.l_flock.pid,                       \
309                        lock->l_policy_data.l_flock.start,                     \
310                        lock->l_policy_data.l_flock.end,                       \
311                        lock->l_flags, lock->l_remote_handle.cookie,           \
312                        lock->l_export ?                                       \
313                        atomic_read(&lock->l_export->exp_refcount) : -99);     \
314                 break;                                                        \
315         }                                                                     \
316         {                                                                     \
317                 CDEBUG(level, "### " format                                   \
318                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "  \
319                        "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "     \
320                        "remote: "LPX64" expref: %d\n" , ## a,                 \
321                        lock->l_resource->lr_namespace->ns_name,               \
322                        lock, lock->l_handle.h_cookie,                         \
323                        atomic_read (&lock->l_refc),                           \
324                        lock->l_readers, lock->l_writers,                      \
325                        ldlm_lockname[lock->l_granted_mode],                   \
326                        ldlm_lockname[lock->l_req_mode],                       \
327                        lock->l_resource->lr_name.name[0],                     \
328                        lock->l_resource->lr_name.name[1],                     \
329                        atomic_read(&lock->l_resource->lr_refcount),           \
330                        ldlm_typename[lock->l_resource->lr_type],              \
331                        lock->l_flags, lock->l_remote_handle.cookie,           \
332                        lock->l_export ?                                       \
333                        atomic_read(&lock->l_export->exp_refcount) : -99);     \
334         }                                                                     \
335 } while (0)
336
337 #define LDLM_DEBUG(lock, format, a...) __LDLM_DEBUG(D_DLMTRACE, lock, \
338                                                     format, ## a)
339 #define LDLM_ERROR(lock, format, a...) __LDLM_DEBUG(D_ERROR, lock, format, ## a)
340
341 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
342         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
343
344 /*
345  * Iterators.
346  */
347
348 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
349 #define LDLM_ITER_STOP     2 /* stop iterating */
350
351 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
352 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
353
354 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
355                           void *closure);
356 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
357                            void *closure);
358 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
359                                ldlm_res_iterator_t iter, void *closure);
360
361 int ldlm_replay_locks(struct obd_import *imp);
362 void ldlm_change_cbdata(struct ldlm_namespace *, struct ldlm_res_id *,
363                         ldlm_iterator_t iter, void *data);
364
365 /* ldlm_flock.c */
366 int ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data);
367
368 /* ldlm_lockd.c */
369 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
370                              void *data, int flag);
371 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data);
372 int ldlm_handle_enqueue(struct ptlrpc_request *req, ldlm_completion_callback,
373                         ldlm_blocking_callback);
374 int ldlm_handle_convert(struct ptlrpc_request *req);
375 int ldlm_handle_cancel(struct ptlrpc_request *req);
376 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
377 int ldlm_get_ref(void);
378 void ldlm_put_ref(int force);
379
380 /* ldlm_lock.c */
381 void ldlm_register_intent(ldlm_res_policy arg);
382 void ldlm_unregister_intent(void);
383 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
384 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int flags);
385 void ldlm_cancel_callback(struct ldlm_lock *);
386 int ldlm_lock_set_data(struct lustre_handle *, void *data);
387 void ldlm_lock_remove_from_lru(struct ldlm_lock *);
388 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *,
389                                       struct lustre_handle *);
390
391 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
392 {
393         return __ldlm_handle2lock(h, 0);
394 }
395
396 #define LDLM_LOCK_PUT(lock)                     \
397 do {                                            \
398         /*LDLM_DEBUG((lock), "put");*/          \
399         ldlm_lock_put(lock);                    \
400 } while (0)
401
402 #define LDLM_LOCK_GET(lock)                     \
403 ({                                              \
404         ldlm_lock_get(lock);                    \
405         /*LDLM_DEBUG((lock), "get");*/          \
406         lock;                                   \
407 })
408
409 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
410 void ldlm_lock_put(struct ldlm_lock *lock);
411 void ldlm_lock_destroy(struct ldlm_lock *lock);
412 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
413 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
414 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
415 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode);
416 int ldlm_lock_match(struct ldlm_namespace *ns, int flags, struct ldlm_res_id *,
417                     __u32 type, void *cookie, int cookielen, ldlm_mode_t mode,
418                     struct lustre_handle *);
419 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
420                                         int *flags);
421 void ldlm_lock_cancel(struct ldlm_lock *lock);
422 void ldlm_cancel_locks_for_export(struct obd_export *export);
423 void ldlm_reprocess_all(struct ldlm_resource *res);
424 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns);
425 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos);
426 void ldlm_lock_dump_handle(int level, struct lustre_handle *);
427
428 /* ldlm_test.c */
429 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
430 int ldlm_regression_start(struct obd_device *obddev,
431                           struct lustre_handle *connh,
432                           unsigned int threads, unsigned int max_locks_in,
433                           unsigned int num_resources_in,
434                           unsigned int num_extents_in);
435 int ldlm_regression_stop(void);
436
437
438 /* resource.c */
439 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
440 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags);
441 int ldlm_namespace_free(struct ldlm_namespace *ns, int force);
442 int ldlm_proc_setup(void);
443 void ldlm_proc_cleanup(void);
444
445 /* resource.c - internal */
446 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
447                                         struct ldlm_resource *parent,
448                                         struct ldlm_res_id, __u32 type,
449                                         int create);
450 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
451 int ldlm_resource_putref(struct ldlm_resource *res);
452 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
453                             struct ldlm_lock *lock);
454 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
455 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
456 void ldlm_dump_all_namespaces(void);
457 void ldlm_namespace_dump(struct ldlm_namespace *);
458 void ldlm_resource_dump(struct ldlm_resource *);
459 int ldlm_lock_change_resource(struct ldlm_namespace *, struct ldlm_lock *,
460                               struct ldlm_res_id);
461
462 /* ldlm_request.c */
463 int ldlm_expired_completion_wait(void *data);
464 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data);
465 int ldlm_cli_enqueue(struct obd_export *exp,
466                      struct ptlrpc_request *req,
467                      struct ldlm_namespace *ns,
468                      struct lustre_handle *parent_lock_handle,
469                      struct ldlm_res_id,
470                      __u32 type,
471                      void *cookie, int cookielen,
472                      ldlm_mode_t mode,
473                      int *flags,
474                      ldlm_completion_callback completion,
475                      ldlm_blocking_callback callback,
476                      void *data,
477                      struct lustre_handle *lockh);
478 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
479                     void *data, __u32 data_len);
480 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
481 int ldlm_cli_cancel(struct lustre_handle *lockh);
482 int ldlm_cli_cancel_unused(struct ldlm_namespace *, struct ldlm_res_id *,
483                            int flags, void *opaque);
484
485 /* mds/handler.c */
486 /* This has to be here because recurisve inclusion sucks. */
487 int intent_disposition(struct ldlm_reply *rep, int flag);
488 void intent_set_disposition(struct ldlm_reply *rep, int flag);
489 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
490                      void *data, int flag);
491
492
493 /* ioctls for trying requests */
494 #define IOC_LDLM_TYPE                   'f'
495 #define IOC_LDLM_MIN_NR                 40
496
497 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
498 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
499 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
500 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
501 #define IOC_LDLM_MAX_NR                 43
502
503 #endif