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