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