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