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