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