1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 #ifndef _LUSTRE_DLM_H__
6 #define _LUSTRE_DLM_H__
10 #include <linux/proc_fs.h>
11 #include <linux/lustre_lib.h>
12 #include <linux/lustre_net.h>
13 #include <linux/lustre_import.h>
18 #define OBD_LDLM_DEVICENAME "ldlm"
23 ELDLM_LOCK_CHANGED = 300,
24 ELDLM_LOCK_ABORTED = 301,
26 ELDLM_NAMESPACE_EXISTS = 400,
27 ELDLM_BAD_NAMESPACE = 401
30 #define LDLM_NAMESPACE_SERVER 0
31 #define LDLM_NAMESPACE_CLIENT 1
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 #define LDLM_FL_CANCEL (1 << 8)
42 #define LDLM_FL_REPLAY (1 << 9)
43 #define LDLM_FL_INTENT_ONLY (1 << 10) /* don't grant lock, just do intent */
44 #define LDLM_FL_LOCAL_ONLY (1 << 11) /* see ldlm_cli_cancel_unused */
45 #define LDLM_FL_NO_CALLBACK (1 << 12) /* see ldlm_cli_cancel_unused */
46 #define LDLM_FL_HAS_INTENT (1 << 13) /* lock request has intent */
48 #define LDLM_CB_BLOCKING 1
49 #define LDLM_CB_CANCELING 2
51 #define L2B(c) (1 << c)
53 /* compatibility matrix */
54 #define LCK_COMPAT_EX L2B(LCK_NL)
55 #define LCK_COMPAT_PW (LCK_COMPAT_EX | L2B(LCK_CR))
56 #define LCK_COMPAT_PR (LCK_COMPAT_PW | L2B(LCK_PR))
57 #define LCK_COMPAT_CW (LCK_COMPAT_PW | L2B(LCK_CW))
58 #define LCK_COMPAT_CR (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
59 #define LCK_COMPAT_NL (LCK_COMPAT_CR | L2B(LCK_EX))
61 static ldlm_mode_t lck_compat_array[] = {
62 [LCK_EX] LCK_COMPAT_EX,
63 [LCK_PW] LCK_COMPAT_PW,
64 [LCK_PR] LCK_COMPAT_PR,
65 [LCK_CW] LCK_COMPAT_CW,
66 [LCK_CR] LCK_COMPAT_CR,
67 [LCK_NL] LCK_COMPAT_NL
70 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
72 if (exist < LCK_EX || exist > LCK_NL)
74 if (new < LCK_EX || new > LCK_NL)
77 return (lck_compat_array[exist] & L2B(new));
86 #define DLM_OST_NAMESPACE 1
87 #define DLM_MDS_NAMESPACE 2
90 - do we just separate this by security domains and use a prefix for
91 multiple namespaces in the same domain?
95 struct ldlm_namespace {
97 __u32 ns_client; /* is this a client-side lock tree? */
98 struct list_head *ns_hash; /* hash table for ns */
99 __u32 ns_refcount; /* count of resources in the hash */
100 struct list_head ns_root_list; /* all root resources in ns */
101 struct lustre_lock ns_lock; /* protects hash, refcount, list */
102 struct list_head ns_list_chain; /* position in global NS list */
103 struct proc_dir_entry *ns_proc_dir;
105 spinlock_t ns_counter_lock;
112 * Resource hash table
116 #define RES_HASH_BITS 14
117 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
118 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
122 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
123 struct ldlm_lock_desc *new, void *data,
124 __u32 data_len, int flag);
126 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags);
131 struct ldlm_resource *l_resource;
132 struct ldlm_lock *l_parent;
133 struct list_head l_children;
134 struct list_head l_childof;
135 struct list_head l_res_link; /*position in one of three res lists*/
136 struct list_head l_export_chain; /* per-export chain of locks */
137 struct list_head l_pending_chain; /* locks with callbacks pending*/
138 unsigned long l_callback_timeout;
140 ldlm_mode_t l_req_mode;
141 ldlm_mode_t l_granted_mode;
143 ldlm_completion_callback l_completion_ast;
144 ldlm_blocking_callback l_blocking_ast;
146 struct obd_export *l_export;
147 struct lustre_handle *l_connh;
149 struct lustre_handle l_remote_handle;
152 struct ldlm_extent l_extent;
153 __u32 l_version[RES_VERSION_SIZE];
158 /* If the lock is granted, a process sleeps on this waitq to learn when
159 * it's no longer in use. If the lock is not granted, a process sleeps
160 * on this waitq to learn when it becomes granted. */
161 wait_queue_head_t l_waitq;
164 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
165 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
166 ldlm_mode_t mode, int flags, void *data);
168 #define LDLM_PLAIN 10
169 #define LDLM_EXTENT 11
171 #define LDLM_MIN_TYPE 10
172 #define LDLM_MAX_TYPE 11
174 extern ldlm_res_compat ldlm_res_compat_table [];
175 extern ldlm_res_policy ldlm_res_policy_table [];
177 struct ldlm_resource {
178 struct ldlm_namespace *lr_namespace;
179 struct list_head lr_hash;
180 struct ldlm_resource *lr_parent; /* 0 for a root resource */
181 struct list_head lr_children; /* list head for child resources */
182 struct list_head lr_childof; /* part of ns_root_list if root res,
183 * part of lr_children if child */
185 struct list_head lr_granted;
186 struct list_head lr_converting;
187 struct list_head lr_waiting;
188 ldlm_mode_t lr_most_restr;
189 __u32 lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
190 struct ldlm_resource *lr_root;
191 __u64 lr_name[RES_NAME_SIZE];
192 __u32 lr_version[RES_VERSION_SIZE];
193 atomic_t lr_refcount;
197 struct ldlm_ast_work {
198 struct ldlm_lock *w_lock;
200 struct ldlm_lock_desc w_desc;
201 struct list_head w_list;
207 /* Per-export ldlm state. */
208 struct ldlm_export_data {
209 struct list_head led_held_locks;
210 struct obd_import led_import;
213 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
215 return (struct ldlm_extent *)(res->lr_name);
218 extern struct obd_ops ldlm_obd_ops;
220 extern char *ldlm_lockname[];
221 extern char *ldlm_typename[];
222 extern char *ldlm_it2str(int it);
224 #define LDLM_DEBUG(lock, format, a...) \
226 if (lock->l_resource == NULL) { \
227 CDEBUG(D_DLMTRACE, "### " format \
228 " (UNKNOWN: lock %p(rc=%d/%d,%d) mode %s/%s on " \
229 "res \?\? (rc=\?\?) type \?\?\? remote "LPX64")\n" \
230 , ## a, lock, lock->l_refc, lock->l_readers, \
232 ldlm_lockname[lock->l_granted_mode], \
233 ldlm_lockname[lock->l_req_mode], \
234 lock->l_remote_handle.addr); \
237 if (lock->l_resource->lr_type == LDLM_EXTENT) { \
238 CDEBUG(D_DLMTRACE, "### " format \
239 " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64 \
240 "/"LPU64" (rc=%d) type %s ["LPU64"->"LPU64"] remote " \
242 lock->l_resource->lr_namespace->ns_name, lock, \
243 lock->l_refc, lock->l_readers, lock->l_writers, \
244 ldlm_lockname[lock->l_granted_mode], \
245 ldlm_lockname[lock->l_req_mode], \
246 lock->l_resource->lr_name[0], \
247 lock->l_resource->lr_name[1], \
248 atomic_read(&lock->l_resource->lr_refcount), \
249 ldlm_typename[lock->l_resource->lr_type], \
250 lock->l_extent.start, lock->l_extent.end, \
251 lock->l_remote_handle.addr); \
255 CDEBUG(D_DLMTRACE, "### " format \
256 " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64 \
257 "/"LPU64" (rc=%d) type %s remote "LPX64")\n" , ## a, \
258 lock->l_resource->lr_namespace->ns_name, lock, \
259 lock->l_refc, lock->l_readers, lock->l_writers, \
260 ldlm_lockname[lock->l_granted_mode], \
261 ldlm_lockname[lock->l_req_mode], \
262 lock->l_resource->lr_name[0], \
263 lock->l_resource->lr_name[1], \
264 atomic_read(&lock->l_resource->lr_refcount), \
265 ldlm_typename[lock->l_resource->lr_type], \
266 lock->l_remote_handle.addr); \
270 #define LDLM_DEBUG_NOLOCK(format, a...) \
271 CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
274 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
275 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, int flags,
279 int ldlm_handle_enqueue(struct ptlrpc_request *req);
280 int ldlm_handle_convert(struct ptlrpc_request *req);
281 int ldlm_handle_cancel(struct ptlrpc_request *req);
282 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
285 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
286 ldlm_mode_t mode, int flags, void *data));
287 void ldlm_unregister_intent(void);
288 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
289 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int strict);
290 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
291 void ldlm_cancel_callback(struct ldlm_lock *);
292 int ldlm_lock_set_data(struct lustre_handle *, void *data, int datalen);
294 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
296 return __ldlm_handle2lock(h, 1);
299 #define LDLM_LOCK_PUT(lock) \
301 /*LDLM_DEBUG(lock, "put");*/ \
302 ldlm_lock_put(lock); \
305 #define LDLM_LOCK_GET(lock) \
307 ldlm_lock_get(lock); \
308 /*LDLM_DEBUG(lock, "get");*/ \
312 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
313 void ldlm_lock_put(struct ldlm_lock *lock);
314 void ldlm_lock_destroy(struct ldlm_lock *lock);
315 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
316 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
317 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
318 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
319 void ldlm_grant_lock(struct ldlm_lock *lock);
320 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
321 void *cookie, int cookielen, ldlm_mode_t mode,
322 struct lustre_handle *lockh);
324 ldlm_lock_create(struct ldlm_namespace *ns,
325 struct lustre_handle *parent_lock_handle,
326 __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
328 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
329 int cookie_len, int *flags,
330 ldlm_completion_callback completion,
331 ldlm_blocking_callback blocking);
332 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
334 void ldlm_lock_cancel(struct ldlm_lock *lock);
335 void ldlm_cancel_locks_for_export(struct obd_export *export);
336 void ldlm_run_ast_work(struct list_head *rpc_list);
337 void ldlm_reprocess_all(struct ldlm_resource *res);
338 void ldlm_lock_dump(struct ldlm_lock *lock);
341 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
342 int ldlm_regression_start(struct obd_device *obddev,
343 struct lustre_handle *connh,
344 unsigned int threads, unsigned int max_locks_in,
345 unsigned int num_resources_in,
346 unsigned int num_extents_in);
347 int ldlm_regression_stop(void);
351 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
352 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
353 int ldlm_namespace_free(struct ldlm_namespace *ns);
354 int ldlm_proc_setup(struct obd_device *obd);
355 void ldlm_proc_cleanup(struct obd_device *obd);
357 /* resource.c - internal */
358 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
359 struct ldlm_resource *parent,
360 __u64 *name, __u32 type, int create);
361 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
362 int ldlm_resource_put(struct ldlm_resource *res);
363 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
364 struct ldlm_lock *lock);
365 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
366 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
367 void ldlm_dump_all_namespaces(void);
368 void ldlm_namespace_dump(struct ldlm_namespace *);
369 void ldlm_resource_dump(struct ldlm_resource *);
370 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
373 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
374 int ldlm_cli_enqueue(struct lustre_handle *conn,
375 struct ptlrpc_request *req,
376 struct ldlm_namespace *ns,
377 struct lustre_handle *parent_lock_handle,
380 void *cookie, int cookielen,
383 ldlm_completion_callback completion,
384 ldlm_blocking_callback callback,
387 struct lustre_handle *lockh);
388 int ldlm_match_or_enqueue(struct lustre_handle *connh,
389 struct ptlrpc_request *req,
390 struct ldlm_namespace *ns,
391 struct lustre_handle *parent_lock_handle,
394 void *cookie, int cookielen,
397 ldlm_completion_callback completion,
398 ldlm_blocking_callback callback,
401 struct lustre_handle *lockh);
402 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
403 void *data, __u32 data_len);
404 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
405 int ldlm_cli_cancel(struct lustre_handle *lockh);
406 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
409 /* This has to be here because recurisve inclusion sucks. */
410 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
411 void *data, __u32 data_len, int flag);
413 #endif /* __KERNEL__ */
415 /* ioctls for trying requests */
416 #define IOC_LDLM_TYPE 'f'
417 #define IOC_LDLM_MIN_NR 40
419 #define IOC_LDLM_TEST _IOWR('f', 40, long)
420 #define IOC_LDLM_DUMP _IOWR('f', 41, long)
421 #define IOC_LDLM_REGRESS_START _IOWR('f', 42, long)
422 #define IOC_LDLM_REGRESS_STOP _IOWR('f', 43, long)
423 #define IOC_LDLM_MAX_NR 43