Whamcloud - gitweb
merge b_md onto HEAD. as best as I can remember:
[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 /* ldlm_extent.c */
284 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
285 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, int flags,
286                        void *);
287
288 /* ldlm_lockd.c */
289 int ldlm_handle_enqueue(struct ptlrpc_request *req);
290 int ldlm_handle_convert(struct ptlrpc_request *req);
291 int ldlm_handle_cancel(struct ptlrpc_request *req);
292 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
293
294 /* ldlm_lock.c */
295 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
296                                      ldlm_mode_t mode, int flags, void *data));
297 void ldlm_unregister_intent(void);
298 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
299 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int strict, int flags);
300 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
301 void ldlm_cancel_callback(struct ldlm_lock *);
302 int ldlm_lock_set_data(struct lustre_handle *, void *data, int datalen);
303
304 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
305 {
306         return __ldlm_handle2lock(h, 1, 0);
307 }
308
309 #define LDLM_LOCK_PUT(lock)                     \
310 do {                                            \
311         /*LDLM_DEBUG(lock, "put");*/            \
312         ldlm_lock_put(lock);                    \
313 } while (0)
314
315 #define LDLM_LOCK_GET(lock)                     \
316 ({                                              \
317         ldlm_lock_get(lock);                    \
318         /*LDLM_DEBUG(lock, "get");*/            \
319         lock;                                   \
320 })
321
322 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
323 void ldlm_lock_put(struct ldlm_lock *lock);
324 void ldlm_lock_destroy(struct ldlm_lock *lock);
325 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
326 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
327 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
328 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
329 void ldlm_grant_lock(struct ldlm_lock *lock);
330 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
331                     void *cookie, int cookielen, ldlm_mode_t mode,
332                     struct lustre_handle *lockh);
333 struct ldlm_lock *
334 ldlm_lock_create(struct ldlm_namespace *ns,
335                  struct lustre_handle *parent_lock_handle,
336                  __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
337                  __u32 data_len);
338 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
339                                int cookie_len, int *flags,
340                                ldlm_completion_callback completion,
341                                ldlm_blocking_callback blocking);
342 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
343                                         int *flags);
344 void ldlm_lock_cancel(struct ldlm_lock *lock);
345 void ldlm_cancel_locks_for_export(struct obd_export *export);
346 void ldlm_run_ast_work(struct list_head *rpc_list);
347 void ldlm_reprocess_all(struct ldlm_resource *res);
348 void ldlm_lock_dump(struct ldlm_lock *lock);
349
350 /* ldlm_test.c */
351 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
352 int ldlm_regression_start(struct obd_device *obddev,
353                           struct lustre_handle *connh,
354                           unsigned int threads, unsigned int max_locks_in,
355                           unsigned int num_resources_in,
356                           unsigned int num_extents_in);
357 int ldlm_regression_stop(void);
358
359
360 /* resource.c */
361 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
362 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
363 int ldlm_namespace_free(struct ldlm_namespace *ns);
364 int ldlm_proc_setup(struct obd_device *obd);
365 void ldlm_proc_cleanup(struct obd_device *obd);
366
367 /* resource.c - internal */
368 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
369                                         struct ldlm_resource *parent,
370                                         __u64 *name, __u32 type, int create);
371 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
372 int ldlm_resource_put(struct ldlm_resource *res);
373 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
374                             struct ldlm_lock *lock);
375 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
376 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
377 void ldlm_dump_all_namespaces(void);
378 void ldlm_namespace_dump(struct ldlm_namespace *);
379 void ldlm_resource_dump(struct ldlm_resource *);
380 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
381
382 /* ldlm_request.c */
383 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
384 int ldlm_cli_enqueue(struct lustre_handle *conn,
385                      struct ptlrpc_request *req,
386                      struct ldlm_namespace *ns,
387                      struct lustre_handle *parent_lock_handle,
388                      __u64 *res_id,
389                      __u32 type,
390                      void *cookie, int cookielen,
391                      ldlm_mode_t mode,
392                      int *flags,
393                      ldlm_completion_callback completion,
394                      ldlm_blocking_callback callback,
395                      void *data,
396                      __u32 data_len,
397                      struct lustre_handle *lockh);
398 int ldlm_match_or_enqueue(struct lustre_handle *connh,
399                           struct ptlrpc_request *req,
400                           struct ldlm_namespace *ns,
401                           struct lustre_handle *parent_lock_handle,
402                           __u64 *res_id,
403                           __u32 type,
404                           void *cookie, int cookielen,
405                           ldlm_mode_t mode,
406                           int *flags,
407                           ldlm_completion_callback completion,
408                           ldlm_blocking_callback callback,
409                           void *data,
410                           __u32 data_len,
411                           struct lustre_handle *lockh);
412 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
413                     void *data, __u32 data_len);
414 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
415 int ldlm_cli_cancel(struct lustre_handle *lockh);
416 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
417 int ldlm_cancel_lru(struct ldlm_namespace *ns);
418
419 /* mds/handler.c */
420 /* This has to be here because recurisve inclusion sucks. */
421 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
422                      void *data, __u32 data_len, int flag);
423
424 #endif /* __KERNEL__ */
425
426 /* ioctls for trying requests */
427 #define IOC_LDLM_TYPE                   'f'
428 #define IOC_LDLM_MIN_NR                 40
429
430 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
431 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
432 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
433 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
434 #define IOC_LDLM_MAX_NR                 43
435
436 #endif