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