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