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 */
47 #define LDLM_FL_REDUCE (1 << 14) /* throw away unused locks */
49 #define LDLM_CB_BLOCKING 1
50 #define LDLM_CB_CANCELING 2
52 #define L2B(c) (1 << c)
54 /* compatibility matrix */
55 #define LCK_COMPAT_EX L2B(LCK_NL)
56 #define LCK_COMPAT_PW (LCK_COMPAT_EX | L2B(LCK_CR))
57 #define LCK_COMPAT_PR (LCK_COMPAT_PW | L2B(LCK_PR))
58 #define LCK_COMPAT_CW (LCK_COMPAT_PW | L2B(LCK_CW))
59 #define LCK_COMPAT_CR (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
60 #define LCK_COMPAT_NL (LCK_COMPAT_CR | L2B(LCK_EX))
62 static ldlm_mode_t lck_compat_array[] = {
63 [LCK_EX] LCK_COMPAT_EX,
64 [LCK_PW] LCK_COMPAT_PW,
65 [LCK_PR] LCK_COMPAT_PR,
66 [LCK_CW] LCK_COMPAT_CW,
67 [LCK_CR] LCK_COMPAT_CR,
68 [LCK_NL] LCK_COMPAT_NL
71 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
73 if (exist < LCK_EX || exist > LCK_NL)
75 if (new < LCK_EX || new > LCK_NL)
78 return (lck_compat_array[exist] & L2B(new));
87 #define DLM_OST_NAMESPACE 1
88 #define DLM_MDS_NAMESPACE 2
91 - do we just separate this by security domains and use a prefix for
92 multiple namespaces in the same domain?
96 struct ldlm_namespace {
98 __u32 ns_client; /* is this a client-side lock tree? */
99 struct list_head *ns_hash; /* hash table for ns */
100 __u32 ns_refcount; /* count of resources in the hash */
101 struct list_head ns_root_list; /* all root resources in ns */
102 struct lustre_lock ns_lock; /* protects hash, refcount, list */
103 struct list_head ns_list_chain; /* position in global NS list */
105 struct proc_dir_entry *ns_proc_dir;
108 struct list_head ns_unused_list; /* all root resources in ns */
109 unsigned int ns_nr_unused;
110 unsigned int ns_max_unused;
112 spinlock_t ns_counter_lock;
119 * Resource hash table
123 #define RES_HASH_BITS 14
124 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
125 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
129 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
130 struct ldlm_lock_desc *new, void *data,
131 __u32 data_len, int flag);
133 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags);
138 struct ldlm_resource *l_resource;
139 struct ldlm_lock *l_parent;
140 struct list_head l_children;
141 struct list_head l_childof;
142 struct list_head l_lru;
143 struct list_head l_res_link; /*position in one of three res lists*/
144 struct list_head l_export_chain; /* per-export chain of locks */
145 struct list_head l_pending_chain; /* locks with callbacks pending*/
146 unsigned long l_callback_timeout;
148 ldlm_mode_t l_req_mode;
149 ldlm_mode_t l_granted_mode;
151 ldlm_completion_callback l_completion_ast;
152 ldlm_blocking_callback l_blocking_ast;
154 struct obd_export *l_export;
155 struct lustre_handle *l_connh;
157 struct lustre_handle l_remote_handle;
160 struct ldlm_extent l_extent;
161 __u32 l_version[RES_VERSION_SIZE];
166 /* If the lock is granted, a process sleeps on this waitq to learn when
167 * it's no longer in use. If the lock is not granted, a process sleeps
168 * on this waitq to learn when it becomes granted. */
169 wait_queue_head_t l_waitq;
172 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
173 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
174 ldlm_mode_t mode, int flags, void *data);
176 #define LDLM_PLAIN 10
177 #define LDLM_EXTENT 11
179 #define LDLM_MIN_TYPE 10
180 #define LDLM_MAX_TYPE 11
182 extern ldlm_res_compat ldlm_res_compat_table [];
183 extern ldlm_res_policy ldlm_res_policy_table [];
185 struct ldlm_resource {
186 struct ldlm_namespace *lr_namespace;
187 struct list_head lr_hash;
188 struct ldlm_resource *lr_parent; /* 0 for a root resource */
189 struct list_head lr_children; /* list head for child resources */
190 struct list_head lr_childof; /* part of ns_root_list if root res,
191 * part of lr_children if child */
193 struct list_head lr_granted;
194 struct list_head lr_converting;
195 struct list_head lr_waiting;
196 ldlm_mode_t lr_most_restr;
197 __u32 lr_type; /* LDLM_PLAIN or LDLM_EXTENT */
198 struct ldlm_resource *lr_root;
199 __u64 lr_name[RES_NAME_SIZE];
200 __u32 lr_version[RES_VERSION_SIZE];
201 atomic_t lr_refcount;
205 struct ldlm_ast_work {
206 struct ldlm_lock *w_lock;
208 struct ldlm_lock_desc w_desc;
209 struct list_head w_list;
215 /* Per-export ldlm state. */
216 struct ldlm_export_data {
217 struct list_head led_held_locks;
218 struct obd_import led_import;
221 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
223 return (struct ldlm_extent *)(res->lr_name);
226 extern struct obd_ops ldlm_obd_ops;
228 extern char *ldlm_lockname[];
229 extern char *ldlm_typename[];
230 extern char *ldlm_it2str(int it);
232 #define LDLM_DEBUG(lock, format, a...) \
234 if (lock->l_resource == NULL) { \
235 CDEBUG(D_DLMTRACE, "### " format \
236 " (UNKNOWN: lock %p(rc=%d/%d,%d) mode %s/%s on " \
237 "res \?\? (rc=\?\?) type \?\?\? remote "LPX64")\n" \
238 , ## a, lock, lock->l_refc, lock->l_readers, \
240 ldlm_lockname[lock->l_granted_mode], \
241 ldlm_lockname[lock->l_req_mode], \
242 lock->l_remote_handle.addr); \
245 if (lock->l_resource->lr_type == LDLM_EXTENT) { \
246 CDEBUG(D_DLMTRACE, "### " format \
247 " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64 \
248 "/"LPU64" (rc=%d) type %s ["LPU64"->"LPU64"] remote " \
250 lock->l_resource->lr_namespace->ns_name, lock, \
251 lock->l_refc, lock->l_readers, lock->l_writers, \
252 ldlm_lockname[lock->l_granted_mode], \
253 ldlm_lockname[lock->l_req_mode], \
254 lock->l_resource->lr_name[0], \
255 lock->l_resource->lr_name[1], \
256 atomic_read(&lock->l_resource->lr_refcount), \
257 ldlm_typename[lock->l_resource->lr_type], \
258 lock->l_extent.start, lock->l_extent.end, \
259 lock->l_remote_handle.addr); \
263 CDEBUG(D_DLMTRACE, "### " format \
264 " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res "LPU64 \
265 "/"LPU64" (rc=%d) type %s remote "LPX64")\n" , ## a, \
266 lock->l_resource->lr_namespace->ns_name, lock, \
267 lock->l_refc, lock->l_readers, lock->l_writers, \
268 ldlm_lockname[lock->l_granted_mode], \
269 ldlm_lockname[lock->l_req_mode], \
270 lock->l_resource->lr_name[0], \
271 lock->l_resource->lr_name[1], \
272 atomic_read(&lock->l_resource->lr_refcount), \
273 ldlm_typename[lock->l_resource->lr_type], \
274 lock->l_remote_handle.addr); \
278 #define LDLM_DEBUG_NOLOCK(format, a...) \
279 CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
282 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
283 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, int flags,
287 int ldlm_handle_enqueue(struct ptlrpc_request *req);
288 int ldlm_handle_convert(struct ptlrpc_request *req);
289 int ldlm_handle_cancel(struct ptlrpc_request *req);
290 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
293 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
294 ldlm_mode_t mode, int flags, void *data));
295 void ldlm_unregister_intent(void);
296 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
297 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *, int strict);
298 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
299 void ldlm_cancel_callback(struct ldlm_lock *);
300 int ldlm_lock_set_data(struct lustre_handle *, void *data, int datalen);
302 static inline struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *h)
304 return __ldlm_handle2lock(h, 1);
307 #define LDLM_LOCK_PUT(lock) \
309 /*LDLM_DEBUG(lock, "put");*/ \
310 ldlm_lock_put(lock); \
313 #define LDLM_LOCK_GET(lock) \
315 ldlm_lock_get(lock); \
316 /*LDLM_DEBUG(lock, "get");*/ \
320 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
321 void ldlm_lock_put(struct ldlm_lock *lock);
322 void ldlm_lock_destroy(struct ldlm_lock *lock);
323 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
324 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
325 void ldlm_lock_addref_internal(struct ldlm_lock *, __u32 mode);
326 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
327 void ldlm_grant_lock(struct ldlm_lock *lock);
328 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
329 void *cookie, int cookielen, ldlm_mode_t mode,
330 struct lustre_handle *lockh);
332 ldlm_lock_create(struct ldlm_namespace *ns,
333 struct lustre_handle *parent_lock_handle,
334 __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
336 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
337 int cookie_len, int *flags,
338 ldlm_completion_callback completion,
339 ldlm_blocking_callback blocking);
340 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
342 void ldlm_lock_cancel(struct ldlm_lock *lock);
343 void ldlm_cancel_locks_for_export(struct obd_export *export);
344 void ldlm_run_ast_work(struct list_head *rpc_list);
345 void ldlm_reprocess_all(struct ldlm_resource *res);
346 void ldlm_lock_dump(struct ldlm_lock *lock);
349 int ldlm_test(struct obd_device *device, struct lustre_handle *connh);
350 int ldlm_regression_start(struct obd_device *obddev,
351 struct lustre_handle *connh,
352 unsigned int threads, unsigned int max_locks_in,
353 unsigned int num_resources_in,
354 unsigned int num_extents_in);
355 int ldlm_regression_stop(void);
359 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
360 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
361 int ldlm_namespace_free(struct ldlm_namespace *ns);
362 int ldlm_proc_setup(struct obd_device *obd);
363 void ldlm_proc_cleanup(struct obd_device *obd);
365 /* resource.c - internal */
366 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
367 struct ldlm_resource *parent,
368 __u64 *name, __u32 type, int create);
369 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
370 int ldlm_resource_put(struct ldlm_resource *res);
371 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
372 struct ldlm_lock *lock);
373 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
374 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
375 void ldlm_dump_all_namespaces(void);
376 void ldlm_namespace_dump(struct ldlm_namespace *);
377 void ldlm_resource_dump(struct ldlm_resource *);
378 int ldlm_lock_change_resource(struct ldlm_lock *, __u64 new_resid[3]);
381 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
382 int ldlm_cli_enqueue(struct lustre_handle *conn,
383 struct ptlrpc_request *req,
384 struct ldlm_namespace *ns,
385 struct lustre_handle *parent_lock_handle,
388 void *cookie, int cookielen,
391 ldlm_completion_callback completion,
392 ldlm_blocking_callback callback,
395 struct lustre_handle *lockh);
396 int ldlm_match_or_enqueue(struct lustre_handle *connh,
397 struct ptlrpc_request *req,
398 struct ldlm_namespace *ns,
399 struct lustre_handle *parent_lock_handle,
402 void *cookie, int cookielen,
405 ldlm_completion_callback completion,
406 ldlm_blocking_callback callback,
409 struct lustre_handle *lockh);
410 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
411 void *data, __u32 data_len);
412 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
413 int ldlm_cli_cancel(struct lustre_handle *lockh);
414 int ldlm_cli_cancel_unused(struct ldlm_namespace *, __u64 *, int flags);
415 int ldlm_cancel_lru(struct ldlm_namespace *ns);
418 /* This has to be here because recurisve inclusion sucks. */
419 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
420 void *data, __u32 data_len, int flag);
422 #endif /* __KERNEL__ */
424 /* ioctls for trying requests */
425 #define IOC_LDLM_TYPE 'f'
426 #define IOC_LDLM_MIN_NR 40
428 #define IOC_LDLM_TEST _IOWR('f', 40, long)
429 #define IOC_LDLM_DUMP _IOWR('f', 41, long)
430 #define IOC_LDLM_REGRESS_START _IOWR('f', 42, long)
431 #define IOC_LDLM_REGRESS_STOP _IOWR('f', 43, long)
432 #define IOC_LDLM_MAX_NR 43