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