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