1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * (visit-tags-table FILE)
3 * vim:expandtab:shiftwidth=8:tabstop=8:
6 #ifndef _LUSTRE_DLM_H__
7 #define _LUSTRE_DLM_H__
10 # include <linux/proc_fs.h>
13 #include <linux/lustre_lib.h>
14 #include <linux/lustre_net.h>
15 #include <linux/lustre_import.h>
16 #include <linux/lustre_handles.h>
17 #include <linux/lustre_export.h> /* for obd_export, for LDLM_DEBUG */
22 #define OBD_LDLM_DEVICENAME "ldlm"
24 #define LDLM_DEFAULT_LRU_SIZE 100
29 ELDLM_LOCK_CHANGED = 300,
30 ELDLM_LOCK_ABORTED = 301,
31 ELDLM_LOCK_REPLACED = 302,
33 ELDLM_NAMESPACE_EXISTS = 400,
34 ELDLM_BAD_NAMESPACE = 401
37 #define LDLM_NAMESPACE_SERVER 0
38 #define LDLM_NAMESPACE_CLIENT 1
40 #define LDLM_FL_LOCK_CHANGED 0x000001 /* extent, mode, or resource changed */
42 /* If the server returns one of these flags, then the lock was put on that list.
43 * If the client sends one of these flags (during recovery ONLY!), it wants the
44 * lock added to the specified list, no questions asked. -p */
45 #define LDLM_FL_BLOCK_GRANTED 0x000002
46 #define LDLM_FL_BLOCK_CONV 0x000004
47 #define LDLM_FL_BLOCK_WAIT 0x000008
49 #define LDLM_FL_CBPENDING 0x000010 /* this lock is being destroyed */
50 #define LDLM_FL_AST_SENT 0x000020 /* blocking or cancel packet was sent */
51 #define LDLM_FL_WAIT_NOREPROC 0x000040 /* not a real flag, not saved in lock */
52 #define LDLM_FL_CANCEL 0x000080 /* cancellation callback already run */
54 /* Lock is being replayed. This could probably be implied by the fact that one
55 * of BLOCK_{GRANTED,CONV,WAIT} is set, but that is pretty dangerous. */
56 #define LDLM_FL_REPLAY 0x000100
58 #define LDLM_FL_INTENT_ONLY 0x000200 /* don't grant lock, just do intent */
59 #define LDLM_FL_LOCAL_ONLY 0x000400 /* see ldlm_cli_cancel_unused */
61 /* don't run the cancel callback under ldlm_cli_cancel_unused */
62 #define LDLM_FL_NO_CALLBACK 0x000800
64 #define LDLM_FL_HAS_INTENT 0x001000 /* lock request has intent */
65 #define LDLM_FL_CANCELING 0x002000 /* lock cancel has already been sent */
66 #define LDLM_FL_LOCAL 0x004000 /* local lock (ie, no srv/cli split) */
67 #define LDLM_FL_WARN 0x008000 /* see ldlm_cli_cancel_unused */
68 #define LDLM_FL_DISCARD_DATA 0x010000 /* discard (no writeback) on cancel */
70 /* file & record locking */
71 #define LDLM_FL_BLOCK_NOWAIT 0x040000 // server told not to wait if blocked
72 #define LDLM_FL_TEST_LOCK 0x080000 // return blocking lock
74 /* These are flags that are mapped into the flags and ASTs of blocking locks */
75 #define LDLM_AST_DISCARD_DATA 0x80000000 /* Add FL_DISCARD to blocking ASTs */
77 /* Flags sent in AST lock_flags to be mapped into the receiving lock. */
78 #define LDLM_AST_FLAGS (LDLM_FL_DISCARD_DATA)
80 /* The blocking callback is overloaded to perform two functions. These flags
81 * indicate which operation should be performed. */
82 #define LDLM_CB_BLOCKING 1
83 #define LDLM_CB_CANCELING 2
85 #define L2B(c) (1 << c)
87 /* compatibility matrix */
88 #define LCK_COMPAT_EX L2B(LCK_NL)
89 #define LCK_COMPAT_PW (LCK_COMPAT_EX | L2B(LCK_CR))
90 #define LCK_COMPAT_PR (LCK_COMPAT_PW | L2B(LCK_PR))
91 #define LCK_COMPAT_CW (LCK_COMPAT_PW | L2B(LCK_CW))
92 #define LCK_COMPAT_CR (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
93 #define LCK_COMPAT_NL (LCK_COMPAT_CR | L2B(LCK_EX))
95 static ldlm_mode_t lck_compat_array[] = {
96 [LCK_EX] LCK_COMPAT_EX,
97 [LCK_PW] LCK_COMPAT_PW,
98 [LCK_PR] LCK_COMPAT_PR,
99 [LCK_CW] LCK_COMPAT_CW,
100 [LCK_CR] LCK_COMPAT_CR,
101 [LCK_NL] LCK_COMPAT_NL
104 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
106 LASSERT(exist >= LCK_EX && exist <= LCK_NL);
107 LASSERT(new >= LCK_EX && new <= LCK_NL);
109 return (lck_compat_array[exist] & L2B(new));
114 * cluster name spaces
118 #define DLM_OST_NAMESPACE 1
119 #define DLM_MDS_NAMESPACE 2
122 - do we just separate this by security domains and use a prefix for
123 multiple namespaces in the same domain?
127 struct ldlm_namespace {
129 __u32 ns_client; /* is this a client-side lock tree? */
130 struct list_head *ns_hash; /* hash table for ns */
131 __u32 ns_refcount; /* count of resources in the hash */
132 struct list_head ns_root_list; /* all root resources in ns */
133 struct lustre_lock ns_lock; /* protects hash, refcount, list */
134 struct list_head ns_list_chain; /* position in global NS list */
136 struct proc_dir_entry *ns_proc_dir;
139 struct list_head ns_unused_list; /* all root resources in ns */
141 unsigned int ns_max_unused;
143 spinlock_t ns_counter_lock;
150 * Resource hash table
154 #define RES_HASH_BITS 10
155 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
156 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
160 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
161 struct ldlm_lock_desc *new, void *data,
163 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags,
167 struct portals_handle l_handle; // must be first in the structure
169 struct ldlm_resource *l_resource;
170 struct ldlm_lock *l_parent;
171 struct list_head l_children;
172 struct list_head l_childof;
173 struct list_head l_lru;
174 struct list_head l_res_link; // position in one of three res lists
175 struct list_head l_export_chain; // per-export chain of locks
176 struct list_head l_pending_chain; // locks with callbacks pending
177 unsigned long l_callback_timeout;
179 ldlm_mode_t l_req_mode;
180 ldlm_mode_t l_granted_mode;
182 ldlm_completion_callback l_completion_ast;
183 ldlm_blocking_callback l_blocking_ast;
186 struct obd_export *l_export;
187 /* XXX phil can fix this, I'm sure */
188 struct obd_export *l_conn_export;
189 // struct lustre_handle *l_connh;
191 struct lustre_handle l_remote_handle;
192 ldlm_policy_data_t l_policy_data;
193 __u32 l_version[RES_VERSION_SIZE];
199 /* If the lock is granted, a process sleeps on this waitq to learn when
200 * it's no longer in use. If the lock is not granted, a process sleeps
201 * on this waitq to learn when it becomes granted. */
202 wait_queue_head_t l_waitq;
203 struct timeval l_enqueued_time;
206 typedef int (*ldlm_res_policy)(struct ldlm_namespace *, struct ldlm_lock **,
207 void *req_cookie, ldlm_mode_t mode, int flags,
210 #define LDLM_PLAIN 10
211 #define LDLM_EXTENT 11
212 #define LDLM_FLOCK 12
214 #define LDLM_MIN_TYPE 10
215 #define LDLM_MAX_TYPE 12
217 struct ldlm_resource {
218 struct ldlm_namespace *lr_namespace;
219 struct list_head lr_hash;
220 struct ldlm_resource *lr_parent; /* 0 for a root resource */
221 struct list_head lr_children; /* list head for child resources */
222 struct list_head lr_childof; /* part of ns_root_list if root res,
223 * part of lr_children if child */
225 struct list_head lr_granted;
226 struct list_head lr_converting;
227 struct list_head lr_waiting;
228 ldlm_mode_t lr_most_restr;
229 __u32 lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
230 struct ldlm_resource *lr_root;
231 struct ldlm_res_id lr_name;
232 __u32 lr_version[RES_VERSION_SIZE];
233 atomic_t lr_refcount;
235 /* lr_tmp holds a list head temporarily, during the building of a work
236 * queue. see ldlm_add_ast_work_item and ldlm_run_ast_work */
240 struct ldlm_ast_work {
241 struct ldlm_lock *w_lock;
243 struct ldlm_lock_desc w_desc;
244 struct list_head w_list;
250 extern struct obd_ops ldlm_obd_ops;
252 extern char *ldlm_lockname[];
253 extern char *ldlm_typename[];
254 extern char *ldlm_it2str(int it);
256 #define __LDLM_DEBUG(level, lock, format, a...) \
258 if (lock->l_resource == NULL) { \
259 CDEBUG(level, "### " format \
260 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "\
261 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: " \
262 LPX64" expref: %d\n" , ## a, lock, \
263 lock->l_handle.h_cookie, atomic_read(&lock->l_refc), \
264 lock->l_readers, lock->l_writers, \
265 ldlm_lockname[lock->l_granted_mode], \
266 ldlm_lockname[lock->l_req_mode], \
267 lock->l_flags, lock->l_remote_handle.cookie, \
269 atomic_read(&lock->l_export->exp_refcount) : -99); \
272 if (lock->l_resource->lr_type == LDLM_EXTENT) { \
273 CDEBUG(level, "### " format \
274 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s " \
275 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64\
276 "] flags: %x remote: "LPX64" expref: %d\n" , ## a, \
277 lock->l_resource->lr_namespace->ns_name, lock, \
278 lock->l_handle.h_cookie, atomic_read(&lock->l_refc), \
279 lock->l_readers, lock->l_writers, \
280 ldlm_lockname[lock->l_granted_mode], \
281 ldlm_lockname[lock->l_req_mode], \
282 lock->l_resource->lr_name.name[0], \
283 lock->l_resource->lr_name.name[1], \
284 atomic_read(&lock->l_resource->lr_refcount), \
285 ldlm_typename[lock->l_resource->lr_type], \
286 lock->l_policy_data.l_extent.start, \
287 lock->l_policy_data.l_extent.end, \
288 lock->l_flags, lock->l_remote_handle.cookie, \
290 atomic_read(&lock->l_export->exp_refcount) : -99); \
293 if (lock->l_resource->lr_type == LDLM_FLOCK) { \
294 CDEBUG(level, "### " format \
295 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s " \
296 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d " \
297 "["LPU64"->"LPU64"] flags: %x remote: "LPX64 \
298 " expref: %d\n" , ## a, \
299 lock->l_resource->lr_namespace->ns_name, lock, \
300 lock->l_handle.h_cookie, atomic_read(&lock->l_refc), \
301 lock->l_readers, lock->l_writers, \
302 ldlm_lockname[lock->l_granted_mode], \
303 ldlm_lockname[lock->l_req_mode], \
304 lock->l_resource->lr_name.name[0], \
305 lock->l_resource->lr_name.name[1], \
306 atomic_read(&lock->l_resource->lr_refcount), \
307 ldlm_typename[lock->l_resource->lr_type], \
308 lock->l_policy_data.l_flock.pid, \
309 lock->l_policy_data.l_flock.start, \
310 lock->l_policy_data.l_flock.end, \
311 lock->l_flags, lock->l_remote_handle.cookie, \
313 atomic_read(&lock->l_export->exp_refcount) : -99); \
317 CDEBUG(level, "### " format \
318 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s " \
319 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x " \
320 "remote: "LPX64" expref: %d\n" , ## a, \
321 lock->l_resource->lr_namespace->ns_name, \
322 lock, lock->l_handle.h_cookie, \
323 atomic_read (&lock->l_refc), \
324 lock->l_readers, lock->l_writers, \
325 ldlm_lockname[lock->l_granted_mode], \
326 ldlm_lockname[lock->l_req_mode], \
327 lock->l_resource->lr_name.name[0], \
328 lock->l_resource->lr_name.name[1], \
329 atomic_read(&lock->l_resource->lr_refcount), \
330 ldlm_typename[lock->l_resource->lr_type], \
331 lock->l_flags, lock->l_remote_handle.cookie, \
333 atomic_read(&lock->l_export->exp_refcount) : -99); \
337 #define LDLM_DEBUG(lock, format, a...) __LDLM_DEBUG(D_DLMTRACE, lock, \
339 #define LDLM_ERROR(lock, format, a...) __LDLM_DEBUG(D_ERROR, lock, format, ## a)
341 #define LDLM_DEBUG_NOLOCK(format, a...) \
342 CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
348 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
349 #define LDLM_ITER_STOP 2 /* stop iterating */
351 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
352 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
354 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
356 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
358 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
359 ldlm_res_iterator_t iter, void *closure);
361 int ldlm_replay_locks(struct obd_import *imp);
362 void ldlm_change_cbdata(struct ldlm_namespace *, struct ldlm_res_id *,
363 ldlm_iterator_t iter, void *data);
366 int ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data);
369 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
370 void *data, int flag);
371 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data);
372 int ldlm_handle_enqueue(struct ptlrpc_request *req, ldlm_completion_callback,
373 ldlm_blocking_callback);
374 int ldlm_handle_convert(struct ptlrpc_request *req);
375 int ldlm_handle_cancel(struct ptlrpc_request *req);
376 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
377 int ldlm_get_ref(void);
378 void ldlm_put_ref(int force);
381 void ldlm_register_intent(ldlm_res_policy arg);
382 void ldlm_unregister_intent(void);
383 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
384 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int flags);
385 void ldlm_cancel_callback(struct ldlm_lock *);
386 int ldlm_lock_set_data(struct lustre_handle *, void *data);
387 void ldlm_lock_remove_from_lru(struct ldlm_lock *);
388 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *,
389 struct lustre_handle *);
391 void *ldlm_put_lock_into_req(struct ptlrpc_request *,
392 struct lustre_handle *, int);
394 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
396 return __ldlm_handle2lock(h, 0);
399 #define LDLM_LOCK_PUT(lock) \
401 /*LDLM_DEBUG((lock), "put");*/ \
402 ldlm_lock_put(lock); \
405 #define LDLM_LOCK_GET(lock) \
407 ldlm_lock_get(lock); \
408 /*LDLM_DEBUG((lock), "get");*/ \
412 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
413 void ldlm_lock_put(struct ldlm_lock *lock);
414 void ldlm_lock_destroy(struct ldlm_lock *lock);
415 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
416 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
417 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
418 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode);
419 int ldlm_lock_match(struct ldlm_namespace *ns, int flags, struct ldlm_res_id *,
420 __u32 type, void *cookie, int cookielen, ldlm_mode_t mode,
421 struct lustre_handle *);
422 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
424 void ldlm_lock_cancel(struct ldlm_lock *lock);
425 void ldlm_cancel_locks_for_export(struct obd_export *export);
426 void ldlm_reprocess_all(struct ldlm_resource *res);
427 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns);
428 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos);
429 void ldlm_lock_dump_handle(int level, struct lustre_handle *);
432 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
433 int ldlm_regression_start(struct obd_device *obddev,
434 struct lustre_handle *connh,
435 unsigned int threads, unsigned int max_locks_in,
436 unsigned int num_resources_in,
437 unsigned int num_extents_in);
438 int ldlm_regression_stop(void);
442 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
443 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags);
444 int ldlm_namespace_free(struct ldlm_namespace *ns, int force);
445 int ldlm_proc_setup(void);
446 void ldlm_proc_cleanup(void);
448 /* resource.c - internal */
449 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
450 struct ldlm_resource *parent,
451 struct ldlm_res_id, __u32 type,
453 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
454 int ldlm_resource_putref(struct ldlm_resource *res);
455 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
456 struct ldlm_lock *lock);
457 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
458 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
459 void ldlm_dump_all_namespaces(void);
460 void ldlm_namespace_dump(struct ldlm_namespace *);
461 void ldlm_resource_dump(struct ldlm_resource *);
462 int ldlm_lock_change_resource(struct ldlm_namespace *, struct ldlm_lock *,
466 int ldlm_expired_completion_wait(void *data);
467 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data);
468 int ldlm_cli_enqueue(struct obd_export *exp,
469 struct ptlrpc_request *req,
470 struct ldlm_namespace *ns,
471 struct lustre_handle *parent_lock_handle,
474 void *cookie, int cookielen,
477 ldlm_completion_callback completion,
478 ldlm_blocking_callback callback,
480 struct lustre_handle *lockh);
481 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
482 void *data, __u32 data_len);
483 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
484 int ldlm_cli_cancel(struct lustre_handle *lockh);
485 int ldlm_cli_cancel_unused(struct ldlm_namespace *, struct ldlm_res_id *,
486 int flags, void *opaque);
489 /* This has to be here because recurisve inclusion sucks. */
490 int intent_disposition(struct ldlm_reply *rep, int flag);
491 void intent_set_disposition(struct ldlm_reply *rep, int flag);
492 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
493 void *data, int flag);
496 /* ioctls for trying requests */
497 #define IOC_LDLM_TYPE 'f'
498 #define IOC_LDLM_MIN_NR 40
500 #define IOC_LDLM_TEST _IOWR('f', 40, long)
501 #define IOC_LDLM_DUMP _IOWR('f', 41, long)
502 #define IOC_LDLM_REGRESS_START _IOWR('f', 42, long)
503 #define IOC_LDLM_REGRESS_STOP _IOWR('f', 43, long)
504 #define IOC_LDLM_MAX_NR 43