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