Whamcloud - gitweb
Fixes to the lock LRU
[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         struct proc_dir_entry *ns_proc_dir;
105
106         struct list_head       ns_unused_list; /* all root resources in ns */
107         unsigned int           ns_nr_unused;
108         unsigned int           ns_max_unused;
109
110         spinlock_t             ns_counter_lock;
111         __u64                  ns_locks;
112         __u64                  ns_resources;
113 };
114
115 /*
116  *
117  * Resource hash table
118  *
119  */
120
121 #define RES_HASH_BITS 14
122 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
123 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
124
125 struct ldlm_lock;
126
127 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
128                                       struct ldlm_lock_desc *new, void *data,
129                                       __u32 data_len, int flag);
130
131 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags);
132
133 struct ldlm_lock {
134         __u64                 l_random;
135         int                   l_refc;
136         struct ldlm_resource *l_resource;
137         struct ldlm_lock     *l_parent;
138         struct list_head      l_children;
139         struct list_head      l_childof;
140         struct list_head      l_lru;
141         struct list_head      l_res_link; /*position in one of three res lists*/
142         struct list_head      l_export_chain; /* per-export chain of locks */
143         struct list_head      l_pending_chain; /* locks with callbacks pending*/
144         unsigned long         l_callback_timeout;
145
146         ldlm_mode_t           l_req_mode;
147         ldlm_mode_t           l_granted_mode;
148
149         ldlm_completion_callback    l_completion_ast;
150         ldlm_blocking_callback    l_blocking_ast;
151
152         struct obd_export    *l_export;
153         struct lustre_handle *l_connh;
154         __u32                 l_flags;
155         struct lustre_handle   l_remote_handle;
156         void                 *l_data;
157         __u32                 l_data_len;
158         struct ldlm_extent    l_extent;
159         __u32                 l_version[RES_VERSION_SIZE];
160
161         __u32                 l_readers;
162         __u32                 l_writers;
163
164         /* If the lock is granted, a process sleeps on this waitq to learn when
165          * it's no longer in use.  If the lock is not granted, a process sleeps
166          * on this waitq to learn when it becomes granted. */
167         wait_queue_head_t     l_waitq;
168 };
169
170 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
171 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
172                                ldlm_mode_t mode, int flags, void *data);
173
174 #define LDLM_PLAIN       10
175 #define LDLM_EXTENT      11
176
177 #define LDLM_MIN_TYPE 10
178 #define LDLM_MAX_TYPE 11
179
180 extern ldlm_res_compat ldlm_res_compat_table [];
181 extern ldlm_res_policy ldlm_res_policy_table [];
182
183 struct ldlm_resource {
184         struct ldlm_namespace *lr_namespace;
185         struct list_head       lr_hash;
186         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
187         struct list_head       lr_children; /* list head for child resources */
188         struct list_head       lr_childof;  /* part of ns_root_list if root res,
189                                              * part of lr_children if child */
190
191         struct list_head       lr_granted;
192         struct list_head       lr_converting;
193         struct list_head       lr_waiting;
194         ldlm_mode_t            lr_most_restr;
195         __u32                  lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
196         struct ldlm_resource  *lr_root;
197         __u64                  lr_name[RES_NAME_SIZE];
198         __u32                  lr_version[RES_VERSION_SIZE];
199         atomic_t               lr_refcount;
200         void                  *lr_tmp;
201 };
202
203 struct ldlm_ast_work {
204         struct ldlm_lock *w_lock;
205         int               w_blocking;
206         struct ldlm_lock_desc w_desc;
207         struct list_head   w_list;
208         int w_flags;
209         void *w_data;
210         int w_datalen;
211 };
212
213 /* Per-export ldlm state. */
214 struct ldlm_export_data {
215         struct list_head        led_held_locks;
216         struct obd_import       led_import;
217 };
218
219 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
220 {
221         return (struct ldlm_extent *)(res->lr_name);
222 }
223
224 extern struct obd_ops ldlm_obd_ops;
225
226 extern char *ldlm_lockname[];
227 extern char *ldlm_typename[];
228 extern char *ldlm_it2str(int it);
229
230 #define LDLM_DEBUG(lock, format, a...)                                        \
231 do {                                                                          \
232         if (lock->l_resource == NULL) {                                       \
233                 CDEBUG(D_DLMTRACE, "### " format                              \
234                        " (UNKNOWN: lock %p(rc=%d/%d,%d) mode %s/%s on "       \
235                        "res \?\? (rc=\?\?) type \?\?\? remote "LPX64")\n"     \
236                        , ## a, lock, lock->l_refc, lock->l_readers,           \
237                        lock->l_writers,                                       \
238                        ldlm_lockname[lock->l_granted_mode],                   \
239                        ldlm_lockname[lock->l_req_mode],                       \
240                        lock->l_remote_handle.addr);                           \
241                 break;                                                        \
242         }                                                                     \
243         if (lock->l_resource->lr_type == LDLM_EXTENT) {                       \
244                 CDEBUG(D_DLMTRACE, "### " format                              \
245                        " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64   \
246                        "/"LPU64" (rc=%d) type %s ["LPU64"->"LPU64"] remote "  \
247                        LPX64")\n" , ## a,                                     \
248                        lock->l_resource->lr_namespace->ns_name, lock,         \
249                        lock->l_refc, lock->l_readers, lock->l_writers,        \
250                        ldlm_lockname[lock->l_granted_mode],                   \
251                        ldlm_lockname[lock->l_req_mode],                       \
252                        lock->l_resource->lr_name[0],                          \
253                        lock->l_resource->lr_name[1],                          \
254                        atomic_read(&lock->l_resource->lr_refcount),           \
255                        ldlm_typename[lock->l_resource->lr_type],              \
256                        lock->l_extent.start, lock->l_extent.end,              \
257                        lock->l_remote_handle.addr);                           \
258                 break;                                                        \
259         }                                                                     \
260         {                                                                     \
261                 CDEBUG(D_DLMTRACE, "### " format                              \
262                        " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64   \
263                        "/"LPU64" (rc=%d) type %s remote "LPX64")\n" , ## a,   \
264                        lock->l_resource->lr_namespace->ns_name, lock,         \
265                        lock->l_refc, lock->l_readers, lock->l_writers,        \
266                        ldlm_lockname[lock->l_granted_mode],                   \
267                        ldlm_lockname[lock->l_req_mode],                       \
268                        lock->l_resource->lr_name[0],                          \
269                        lock->l_resource->lr_name[1],                          \
270                        atomic_read(&lock->l_resource->lr_refcount),           \
271                        ldlm_typename[lock->l_resource->lr_type],              \
272                        lock->l_remote_handle.addr);                           \
273         }                                                                     \
274 } while (0)
275
276 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
277         CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
278
279 /* ldlm_extent.c */
280 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
281 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, int flags,
282                        void *);
283
284 /* ldlm_lockd.c */
285 int ldlm_handle_enqueue(struct ptlrpc_request *req);
286 int ldlm_handle_convert(struct ptlrpc_request *req);
287 int ldlm_handle_cancel(struct ptlrpc_request *req);
288 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
289
290 /* ldlm_lock.c */
291 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
292                                      ldlm_mode_t mode, int flags, void *data));
293 void ldlm_unregister_intent(void);
294 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
295 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int strict);
296 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
297 void ldlm_cancel_callback(struct ldlm_lock *);
298 int ldlm_lock_set_data(struct lustre_handle *, void *data, int datalen);
299
300 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
301 {
302         return __ldlm_handle2lock(h, 1);
303 }
304
305 #define LDLM_LOCK_PUT(lock)                     \
306 do {                                            \
307         /*LDLM_DEBUG(lock, "put");*/            \
308         ldlm_lock_put(lock);                    \
309 } while (0)
310
311 #define LDLM_LOCK_GET(lock)                     \
312 ({                                              \
313         ldlm_lock_get(lock);                    \
314         /*LDLM_DEBUG(lock, "get");*/            \
315         lock;                                   \
316 })
317
318 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
319 void ldlm_lock_put(struct ldlm_lock *lock);
320 void ldlm_lock_destroy(struct ldlm_lock *lock);
321 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
322 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
323 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
324 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
325 void ldlm_grant_lock(struct ldlm_lock *lock);
326 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
327                     void *cookie, int cookielen, ldlm_mode_t mode,
328                     struct lustre_handle *lockh);
329 struct ldlm_lock *
330 ldlm_lock_create(struct ldlm_namespace *ns,
331                  struct lustre_handle *parent_lock_handle,
332                  __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
333                  __u32 data_len);
334 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
335                                int cookie_len, int *flags,
336                                ldlm_completion_callback completion,
337                                ldlm_blocking_callback blocking);
338 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
339                                         int *flags);
340 void ldlm_lock_cancel(struct ldlm_lock *lock);
341 void ldlm_cancel_locks_for_export(struct obd_export *export);
342 void ldlm_run_ast_work(struct list_head *rpc_list);
343 void ldlm_reprocess_all(struct ldlm_resource *res);
344 void ldlm_lock_dump(struct ldlm_lock *lock);
345
346 /* ldlm_test.c */
347 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
348 int ldlm_regression_start(struct obd_device *obddev,
349                           struct lustre_handle *connh,
350                           unsigned int threads, unsigned int max_locks_in,
351                           unsigned int num_resources_in,
352                           unsigned int num_extents_in);
353 int ldlm_regression_stop(void);
354
355
356 /* resource.c */
357 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
358 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
359 int ldlm_namespace_free(struct ldlm_namespace *ns);
360 int ldlm_proc_setup(struct obd_device *obd);
361 void ldlm_proc_cleanup(struct obd_device *obd);
362
363 /* resource.c - internal */
364 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
365                                         struct ldlm_resource *parent,
366                                         __u64 *name, __u32 type, int create);
367 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
368 int ldlm_resource_put(struct ldlm_resource *res);
369 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
370                             struct ldlm_lock *lock);
371 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
372 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
373 void ldlm_dump_all_namespaces(void);
374 void ldlm_namespace_dump(struct ldlm_namespace *);
375 void ldlm_resource_dump(struct ldlm_resource *);
376 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
377
378 /* ldlm_request.c */
379 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
380 int ldlm_cli_enqueue(struct lustre_handle *conn,
381                      struct ptlrpc_request *req,
382                      struct ldlm_namespace *ns,
383                      struct lustre_handle *parent_lock_handle,
384                      __u64 *res_id,
385                      __u32 type,
386                      void *cookie, int cookielen,
387                      ldlm_mode_t mode,
388                      int *flags,
389                      ldlm_completion_callback completion,
390                      ldlm_blocking_callback callback,
391                      void *data,
392                      __u32 data_len,
393                      struct lustre_handle *lockh);
394 int ldlm_match_or_enqueue(struct lustre_handle *connh,
395                           struct ptlrpc_request *req,
396                           struct ldlm_namespace *ns,
397                           struct lustre_handle *parent_lock_handle,
398                           __u64 *res_id,
399                           __u32 type,
400                           void *cookie, int cookielen,
401                           ldlm_mode_t mode,
402                           int *flags,
403                           ldlm_completion_callback completion,
404                           ldlm_blocking_callback callback,
405                           void *data,
406                           __u32 data_len,
407                           struct lustre_handle *lockh);
408 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
409                     void *data, __u32 data_len);
410 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
411 int ldlm_cli_cancel(struct lustre_handle *lockh);
412 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
413 int ldlm_cancel_lru(struct ldlm_namespace *ns);
414
415 /* mds/handler.c */
416 /* This has to be here because recurisve inclusion sucks. */
417 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
418                      void *data, __u32 data_len, int flag);
419
420 #endif /* __KERNEL__ */
421
422 /* ioctls for trying requests */
423 #define IOC_LDLM_TYPE                   'f'
424 #define IOC_LDLM_MIN_NR                 40
425
426 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
427 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
428 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
429 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
430 #define IOC_LDLM_MAX_NR                 43
431
432 #endif