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