Whamcloud - gitweb
land b_md onto 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_WAIT_NOREPROC  (1 << 6)// not a real lock flag,not saved in lock
46 #define LDLM_FL_CANCEL         (1 << 7) // cancellation callback already run
47
48 /* Lock is being replayed.  This could probably be implied by the fact that one
49  * of BLOCK_{GRANTED,CONV,WAIT} is set, but that is pretty dangerous. */
50 #define LDLM_FL_REPLAY         (1 << 8)
51
52 #define LDLM_FL_INTENT_ONLY    (1 << 9) /* don't grant lock, just do intent */
53 #define LDLM_FL_LOCAL_ONLY     (1 << 10) /* see ldlm_cli_cancel_unused */
54 #define LDLM_FL_NO_CALLBACK    (1 << 11) /* see ldlm_cli_cancel_unused */
55 #define LDLM_FL_HAS_INTENT     (1 << 12) /* lock request has intent */
56 #define LDLM_FL_CANCELING      (1 << 13) /* lock cancel has already been sent */
57
58 /* The blocking callback is overloaded to perform two functions.  These flags
59  * indicate which operation should be performed. */
60 #define LDLM_CB_BLOCKING    1
61 #define LDLM_CB_CANCELING   2
62
63 #define L2B(c) (1 << c)
64
65 /* compatibility matrix */
66 #define LCK_COMPAT_EX  L2B(LCK_NL)
67 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
68 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
69 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
70 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
71 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
72
73 static ldlm_mode_t lck_compat_array[] = {
74         [LCK_EX] LCK_COMPAT_EX,
75         [LCK_PW] LCK_COMPAT_PW,
76         [LCK_PR] LCK_COMPAT_PR,
77         [LCK_CW] LCK_COMPAT_CW,
78         [LCK_CR] LCK_COMPAT_CR,
79         [LCK_NL] LCK_COMPAT_NL
80 };
81
82 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
83 {
84        if (exist < LCK_EX || exist > LCK_NL)
85               LBUG();
86        if (new < LCK_EX || new > LCK_NL)
87               LBUG();
88
89        return (lck_compat_array[exist] & L2B(new));
90 }
91
92 /*
93  *
94  * cluster name spaces
95  *
96  */
97
98 #define DLM_OST_NAMESPACE 1
99 #define DLM_MDS_NAMESPACE 2
100
101 /* XXX
102    - do we just separate this by security domains and use a prefix for
103      multiple namespaces in the same domain?
104    -
105 */
106
107 struct ldlm_namespace {
108         char                  *ns_name;
109         __u32                  ns_client; /* is this a client-side lock tree? */
110         struct list_head      *ns_hash; /* hash table for ns */
111         __u32                  ns_refcount; /* count of resources in the hash */
112         struct list_head       ns_root_list; /* all root resources in ns */
113         struct lustre_lock     ns_lock; /* protects hash, refcount, list */
114         struct list_head       ns_list_chain; /* position in global NS list */
115         /* 
116         struct proc_dir_entry *ns_proc_dir;
117         */
118
119         struct list_head       ns_unused_list; /* all root resources in ns */
120         unsigned int           ns_nr_unused;
121         unsigned int           ns_max_unused;
122
123         spinlock_t             ns_counter_lock;
124         __u64                  ns_locks;
125         __u64                  ns_resources;
126 };
127
128 /*
129  *
130  * Resource hash table
131  *
132  */
133
134 #define RES_HASH_BITS 14
135 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
136 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
137
138 struct ldlm_lock;
139
140 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
141                                       struct ldlm_lock_desc *new, void *data,
142                                       __u32 data_len, int flag);
143
144 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags);
145
146 struct ldlm_lock {
147         __u64                 l_random;
148         atomic_t              l_refc;
149         struct ldlm_resource *l_resource;
150         struct ldlm_lock     *l_parent;
151         struct list_head      l_children;
152         struct list_head      l_childof;
153         struct list_head      l_lru;
154         struct list_head      l_res_link; /*position in one of three res lists*/
155         struct list_head      l_export_chain; /* per-export chain of locks */
156         struct list_head      l_pending_chain; /* locks with callbacks pending*/
157         unsigned long         l_callback_timeout;
158
159         ldlm_mode_t           l_req_mode;
160         ldlm_mode_t           l_granted_mode;
161
162         ldlm_completion_callback l_completion_ast;
163         ldlm_blocking_callback   l_blocking_ast;
164
165         struct obd_export    *l_export;
166         struct lustre_handle *l_connh;
167         __u32                 l_flags;
168         struct lustre_handle  l_remote_handle;
169         void                 *l_data;
170         __u32                 l_data_len;
171         struct ldlm_extent    l_extent;
172         __u32                 l_version[RES_VERSION_SIZE];
173
174         __u32                 l_readers;
175         __u32                 l_writers;
176         __u8                  l_destroyed;
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 void ldlm_lock_remove_from_lru(struct ldlm_lock *);
330
331 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
332 {
333         return __ldlm_handle2lock(h, 1, 0);
334 }
335
336 #define LDLM_LOCK_PUT(lock)                     \
337 do {                                            \
338         /*LDLM_DEBUG(lock, "put");*/            \
339         ldlm_lock_put(lock);                    \
340 } while (0)
341
342 #define LDLM_LOCK_GET(lock)                     \
343 ({                                              \
344         ldlm_lock_get(lock);                    \
345         /*LDLM_DEBUG(lock, "get");*/            \
346         lock;                                   \
347 })
348
349 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
350 void ldlm_lock_put(struct ldlm_lock *lock);
351 void ldlm_lock_destroy(struct ldlm_lock *lock);
352 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
353 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
354 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
355 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
356 void ldlm_grant_lock(struct ldlm_lock *lock);
357 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
358                     void *cookie, int cookielen, ldlm_mode_t mode,
359                     struct lustre_handle *lockh);
360 struct ldlm_lock *
361 ldlm_lock_create(struct ldlm_namespace *ns,
362                  struct lustre_handle *parent_lock_handle,
363                  __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
364                  __u32 data_len);
365 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
366                                int cookie_len, int *flags,
367                                ldlm_completion_callback completion,
368                                ldlm_blocking_callback blocking);
369 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
370                                         int *flags);
371 void ldlm_lock_cancel(struct ldlm_lock *lock);
372 void ldlm_cancel_locks_for_export(struct obd_export *export);
373 void ldlm_run_ast_work(struct list_head *rpc_list);
374 void ldlm_reprocess_all(struct ldlm_resource *res);
375 void ldlm_lock_dump(struct ldlm_lock *lock);
376
377 /* ldlm_test.c */
378 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
379 int ldlm_regression_start(struct obd_device *obddev,
380                           struct lustre_handle *connh,
381                           unsigned int threads, unsigned int max_locks_in,
382                           unsigned int num_resources_in,
383                           unsigned int num_extents_in);
384 int ldlm_regression_stop(void);
385
386
387 /* resource.c */
388 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
389 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
390 int ldlm_namespace_free(struct ldlm_namespace *ns);
391 int ldlm_proc_setup(struct obd_device *obd);
392 void ldlm_proc_cleanup(struct obd_device *obd);
393
394 /* resource.c - internal */
395 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
396                                         struct ldlm_resource *parent,
397                                         __u64 *name, __u32 type, int create);
398 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
399 int ldlm_resource_putref(struct ldlm_resource *res);
400 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
401                             struct ldlm_lock *lock);
402 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
403 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
404 void ldlm_dump_all_namespaces(void);
405 void ldlm_namespace_dump(struct ldlm_namespace *);
406 void ldlm_resource_dump(struct ldlm_resource *);
407 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
408
409 /* ldlm_request.c */
410 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
411 int ldlm_cli_enqueue(struct lustre_handle *conn,
412                      struct ptlrpc_request *req,
413                      struct ldlm_namespace *ns,
414                      struct lustre_handle *parent_lock_handle,
415                      __u64 *res_id,
416                      __u32 type,
417                      void *cookie, int cookielen,
418                      ldlm_mode_t mode,
419                      int *flags,
420                      ldlm_completion_callback completion,
421                      ldlm_blocking_callback callback,
422                      void *data,
423                      __u32 data_len,
424                      struct lustre_handle *lockh);
425 int ldlm_match_or_enqueue(struct lustre_handle *connh,
426                           struct ptlrpc_request *req,
427                           struct ldlm_namespace *ns,
428                           struct lustre_handle *parent_lock_handle,
429                           __u64 *res_id,
430                           __u32 type,
431                           void *cookie, int cookielen,
432                           ldlm_mode_t mode,
433                           int *flags,
434                           ldlm_completion_callback completion,
435                           ldlm_blocking_callback callback,
436                           void *data,
437                           __u32 data_len,
438                           struct lustre_handle *lockh);
439 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
440                     void *data, __u32 data_len);
441 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
442 int ldlm_cli_cancel(struct lustre_handle *lockh);
443 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
444 int ldlm_cancel_lru(struct ldlm_namespace *ns);
445
446 /* mds/handler.c */
447 /* This has to be here because recurisve inclusion sucks. */
448 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
449                      void *data, __u32 data_len, int flag);
450
451 #endif /* __KERNEL__ */
452
453 /* ioctls for trying requests */
454 #define IOC_LDLM_TYPE                   'f'
455 #define IOC_LDLM_MIN_NR                 40
456
457 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
458 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
459 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
460 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
461 #define IOC_LDLM_MAX_NR                 43
462
463 #endif