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