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