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