Whamcloud - gitweb
Merge of b_md to HEAD:
[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
16 struct obd_ops;
17 struct obd_device;
18
19 #define OBD_LDLM_DEVICENAME  "ldlm"
20
21 typedef enum {
22         ELDLM_OK = 0,
23
24         ELDLM_LOCK_CHANGED = 300,
25         ELDLM_LOCK_ABORTED = 301,
26
27         ELDLM_NAMESPACE_EXISTS = 400,
28         ELDLM_BAD_NAMESPACE    = 401
29 } ldlm_error_t;
30
31 #define LDLM_NAMESPACE_SERVER 0
32 #define LDLM_NAMESPACE_CLIENT 1
33
34 #define LDLM_FL_LOCK_CHANGED   (1 << 0) /* extent, mode, or resource changed */
35
36 /* If the server returns one of these flags, then the lock was put on that list.
37  * If the client sends one of these flags (during recovery ONLY!), it wants the
38  * lock added to the specified list, no questions asked. -p */
39 #define LDLM_FL_BLOCK_GRANTED  (1 << 1)
40 #define LDLM_FL_BLOCK_CONV     (1 << 2)
41 #define LDLM_FL_BLOCK_WAIT     (1 << 3)
42
43 #define LDLM_FL_CBPENDING      (1 << 4) // this lock is being destroyed
44 #define LDLM_FL_AST_SENT       (1 << 5) // blocking or cancel packet was sent
45 #define LDLM_FL_DESTROYED      (1 << 6) // this lock is destroyed
46 #define LDLM_FL_WAIT_NOREPROC  (1 << 7)// not a real lock flag,not saved in lock
47 #define LDLM_FL_CANCEL         (1 << 8) // 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 << 9)
52
53 #define LDLM_FL_INTENT_ONLY    (1 << 10) /* don't grant lock, just do intent */
54 #define LDLM_FL_LOCAL_ONLY     (1 << 11) /* see ldlm_cli_cancel_unused */
55 #define LDLM_FL_NO_CALLBACK    (1 << 12) /* see ldlm_cli_cancel_unused */
56 #define LDLM_FL_HAS_INTENT     (1 << 13) /* lock request has intent */
57 #define LDLM_FL_CANCELING      (1 << 14) /* 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         __u64                 l_random;
149         int                   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
178         /* If the lock is granted, a process sleeps on this waitq to learn when
179          * it's no longer in use.  If the lock is not granted, a process sleeps
180          * on this waitq to learn when it becomes granted. */
181         wait_queue_head_t     l_waitq;
182 };
183
184 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
185 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
186                                ldlm_mode_t mode, int flags, void *data);
187
188 #define LDLM_PLAIN       10
189 #define LDLM_EXTENT      11
190
191 #define LDLM_MIN_TYPE 10
192 #define LDLM_MAX_TYPE 11
193
194 extern ldlm_res_compat ldlm_res_compat_table [];
195 extern ldlm_res_policy ldlm_res_policy_table [];
196
197 struct ldlm_resource {
198         struct ldlm_namespace *lr_namespace;
199         struct list_head       lr_hash;
200         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
201         struct list_head       lr_children; /* list head for child resources */
202         struct list_head       lr_childof;  /* part of ns_root_list if root res,
203                                              * part of lr_children if child */
204
205         struct list_head       lr_granted;
206         struct list_head       lr_converting;
207         struct list_head       lr_waiting;
208         ldlm_mode_t            lr_most_restr;
209         __u32                  lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
210         struct ldlm_resource  *lr_root;
211         __u64                  lr_name[RES_NAME_SIZE];
212         __u32                  lr_version[RES_VERSION_SIZE];
213         atomic_t               lr_refcount;
214         void                  *lr_tmp;
215 };
216
217 struct ldlm_ast_work {
218         struct ldlm_lock *w_lock;
219         int               w_blocking;
220         struct ldlm_lock_desc w_desc;
221         struct list_head   w_list;
222         int w_flags;
223         void *w_data;
224         int w_datalen;
225 };
226
227 /* Per-export ldlm state. */
228 struct ldlm_export_data {
229         struct list_head        led_held_locks;
230         struct obd_import       led_import;
231 };
232
233 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
234 {
235         return (struct ldlm_extent *)(res->lr_name);
236 }
237
238 extern struct obd_ops ldlm_obd_ops;
239
240 extern char *ldlm_lockname[];
241 extern char *ldlm_typename[];
242 extern char *ldlm_it2str(int it);
243
244 #define LDLM_DEBUG(lock, format, a...)                                        \
245 do {                                                                          \
246         if (lock->l_resource == NULL) {                                       \
247                 CDEBUG(D_DLMTRACE, "### " format                              \
248                        " ns: \?\? lock: %p lrc: %d/%d,%d mode: %s/%s "        \
249                        "res: \?\? rrc=\?\? type: \?\?\? remote: "LPX64")\n"   \
250                        , ## a, lock, lock->l_refc, lock->l_readers,           \
251                        lock->l_writers,                                       \
252                        ldlm_lockname[lock->l_granted_mode],                   \
253                        ldlm_lockname[lock->l_req_mode],                       \
254                        lock->l_remote_handle.addr);                           \
255                 break;                                                        \
256         }                                                                     \
257         if (lock->l_resource->lr_type == LDLM_EXTENT) {                       \
258                 CDEBUG(D_DLMTRACE, "### " format                              \
259                        " ns: %s lock: %p lrc: %d/%d,%d mode: %s/%s res: "LPU64   \
260                        "/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64"] remote: "  \
261                        LPX64"\n" , ## a,                                     \
262                        lock->l_resource->lr_namespace->ns_name, lock,         \
263                        lock->l_refc, lock->l_readers, lock->l_writers,        \
264                        ldlm_lockname[lock->l_granted_mode],                   \
265                        ldlm_lockname[lock->l_req_mode],                       \
266                        lock->l_resource->lr_name[0],                          \
267                        lock->l_resource->lr_name[1],                          \
268                        atomic_read(&lock->l_resource->lr_refcount),           \
269                        ldlm_typename[lock->l_resource->lr_type],              \
270                        lock->l_extent.start, lock->l_extent.end,              \
271                        lock->l_remote_handle.addr);                           \
272                 break;                                                        \
273         }                                                                     \
274         {                                                                     \
275                 CDEBUG(D_DLMTRACE, "### " format                              \
276                        " ns: %s lock: %p lrc: %d/%d,%d mode: %s/%s res: "LPU64   \
277                        "/"LPU64" rrc: %d type: %s remote: "LPX64"\n" , ## a,   \
278                        lock->l_resource->lr_namespace->ns_name, lock,         \
279                        lock->l_refc, 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[0],                          \
283                        lock->l_resource->lr_name[1],                          \
284                        atomic_read(&lock->l_resource->lr_refcount),           \
285                        ldlm_typename[lock->l_resource->lr_type],              \
286                        lock->l_remote_handle.addr);                           \
287         }                                                                     \
288 } while (0)
289
290 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
291         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
292
293 /*
294  * Iterators.
295  */
296
297 #define LDLM_ITER_CONTINUE 0 /* keep iterating */
298 #define LDLM_ITER_STOP     1 /* stop iterating */
299
300 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
301
302 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
303                           void *closure);
304 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
305                            void *closure);
306
307 int ldlm_replay_locks(struct obd_import *imp);
308
309 /* ldlm_extent.c */
310 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
311 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, int flags,
312                        void *);
313
314 /* ldlm_lockd.c */
315 int ldlm_handle_enqueue(struct ptlrpc_request *req);
316 int ldlm_handle_convert(struct ptlrpc_request *req);
317 int ldlm_handle_cancel(struct ptlrpc_request *req);
318 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
319
320 /* ldlm_lock.c */
321 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
322                                      ldlm_mode_t mode, int flags, void *data));
323 void ldlm_unregister_intent(void);
324 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
325 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int strict,
326                                      int flags);
327 void ldlm_cancel_callback(struct ldlm_lock *);
328 int ldlm_lock_set_data(struct lustre_handle *, void *data, int datalen);
329
330 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
331 {
332         return __ldlm_handle2lock(h, 1, 0);
333 }
334
335 #define LDLM_LOCK_PUT(lock)                     \
336 do {                                            \
337         /*LDLM_DEBUG(lock, "put");*/            \
338         ldlm_lock_put(lock);                    \
339 } while (0)
340
341 #define LDLM_LOCK_GET(lock)                     \
342 ({                                              \
343         ldlm_lock_get(lock);                    \
344         /*LDLM_DEBUG(lock, "get");*/            \
345         lock;                                   \
346 })
347
348 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
349 void ldlm_lock_put(struct ldlm_lock *lock);
350 void ldlm_lock_destroy(struct ldlm_lock *lock);
351 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
352 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
353 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
354 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
355 void ldlm_grant_lock(struct ldlm_lock *lock);
356 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
357                     void *cookie, int cookielen, ldlm_mode_t mode,
358                     struct lustre_handle *lockh);
359 struct ldlm_lock *
360 ldlm_lock_create(struct ldlm_namespace *ns,
361                  struct lustre_handle *parent_lock_handle,
362                  __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
363                  __u32 data_len);
364 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
365                                int cookie_len, int *flags,
366                                ldlm_completion_callback completion,
367                                ldlm_blocking_callback blocking);
368 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
369                                         int *flags);
370 void ldlm_lock_cancel(struct ldlm_lock *lock);
371 void ldlm_cancel_locks_for_export(struct obd_export *export);
372 void ldlm_run_ast_work(struct list_head *rpc_list);
373 void ldlm_reprocess_all(struct ldlm_resource *res);
374 void ldlm_lock_dump(struct ldlm_lock *lock);
375
376 /* ldlm_test.c */
377 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
378 int ldlm_regression_start(struct obd_device *obddev,
379                           struct lustre_handle *connh,
380                           unsigned int threads, unsigned int max_locks_in,
381                           unsigned int num_resources_in,
382                           unsigned int num_extents_in);
383 int ldlm_regression_stop(void);
384
385
386 /* resource.c */
387 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
388 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
389 int ldlm_namespace_free(struct ldlm_namespace *ns);
390 int ldlm_proc_setup(struct obd_device *obd);
391 void ldlm_proc_cleanup(struct obd_device *obd);
392
393 /* resource.c - internal */
394 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
395                                         struct ldlm_resource *parent,
396                                         __u64 *name, __u32 type, int create);
397 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
398 int ldlm_resource_put(struct ldlm_resource *res);
399 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
400                             struct ldlm_lock *lock);
401 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
402 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
403 void ldlm_dump_all_namespaces(void);
404 void ldlm_namespace_dump(struct ldlm_namespace *);
405 void ldlm_resource_dump(struct ldlm_resource *);
406 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
407
408 /* ldlm_request.c */
409 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
410 int ldlm_cli_enqueue(struct lustre_handle *conn,
411                      struct ptlrpc_request *req,
412                      struct ldlm_namespace *ns,
413                      struct lustre_handle *parent_lock_handle,
414                      __u64 *res_id,
415                      __u32 type,
416                      void *cookie, int cookielen,
417                      ldlm_mode_t mode,
418                      int *flags,
419                      ldlm_completion_callback completion,
420                      ldlm_blocking_callback callback,
421                      void *data,
422                      __u32 data_len,
423                      struct lustre_handle *lockh);
424 int ldlm_match_or_enqueue(struct lustre_handle *connh,
425                           struct ptlrpc_request *req,
426                           struct ldlm_namespace *ns,
427                           struct lustre_handle *parent_lock_handle,
428                           __u64 *res_id,
429                           __u32 type,
430                           void *cookie, int cookielen,
431                           ldlm_mode_t mode,
432                           int *flags,
433                           ldlm_completion_callback completion,
434                           ldlm_blocking_callback callback,
435                           void *data,
436                           __u32 data_len,
437                           struct lustre_handle *lockh);
438 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
439                     void *data, __u32 data_len);
440 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
441 int ldlm_cli_cancel(struct lustre_handle *lockh);
442 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
443 int ldlm_cancel_lru(struct ldlm_namespace *ns);
444
445 /* mds/handler.c */
446 /* This has to be here because recurisve inclusion sucks. */
447 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
448                      void *data, __u32 data_len, int flag);
449
450 #endif /* __KERNEL__ */
451
452 /* ioctls for trying requests */
453 #define IOC_LDLM_TYPE                   'f'
454 #define IOC_LDLM_MIN_NR                 40
455
456 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
457 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
458 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
459 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
460 #define IOC_LDLM_MAX_NR                 43
461
462 #endif