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/obd_class.h>
11 #include <linux/lustre_net.h>
13 #define OBD_LDLM_DEVICENAME "ldlm"
18 ELDLM_LOCK_CHANGED = 300,
19 ELDLM_LOCK_ABORTED = 301,
20 ELDLM_RESOURCE_FREED = 302,
22 ELDLM_NAMESPACE_EXISTS = 400,
23 ELDLM_BAD_NAMESPACE = 401
26 #define LDLM_NAMESPACE_SERVER 0
27 #define LDLM_NAMESPACE_CLIENT 1
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 #define LDLM_FL_WAIT_NOREPROC (1 << 7)
38 #define L2B(c) (1 << c)
40 /* compatibility matrix */
41 #define LCK_COMPAT_EX L2B(LCK_NL)
42 #define LCK_COMPAT_PW (LCK_COMPAT_EX | L2B(LCK_CR))
43 #define LCK_COMPAT_PR (LCK_COMPAT_PW | L2B(LCK_PR))
44 #define LCK_COMPAT_CW (LCK_COMPAT_PW | L2B(LCK_CW))
45 #define LCK_COMPAT_CR (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
46 #define LCK_COMPAT_NL (LCK_COMPAT_CR | L2B(LCK_EX))
48 static ldlm_mode_t lck_compat_array[] = {
49 [LCK_EX] LCK_COMPAT_EX,
50 [LCK_PW] LCK_COMPAT_PW,
51 [LCK_PR] LCK_COMPAT_PR,
52 [LCK_CW] LCK_COMPAT_CW,
53 [LCK_CR] LCK_COMPAT_CR,
54 [LCK_NL] LCK_COMPAT_NL
57 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
59 if (exist < LCK_EX || exist > LCK_NL)
61 if (new < LCK_EX || new > LCK_NL)
64 return (lck_compat_array[exist] & L2B(new));
73 #define DLM_OST_NAMESPACE 1
74 #define DLM_MDS_NAMESPACE 2
77 - do we just separate this by security domains and use a prefix for
78 multiple namespaces in the same domain?
82 struct ldlm_namespace {
84 struct ptlrpc_client ns_rpc_client; /* used for revocation callbacks */
85 __u32 ns_client; /* is this a client-side lock tree? */
86 struct list_head *ns_hash; /* hash table for ns */
87 __u32 ns_refcount; /* count of resources in the hash */
88 struct list_head ns_root_list; /* all root resources in ns */
89 struct lustre_lock ns_lock; /* protects hash, refcount, list */
98 #define RES_HASH_BITS 14
99 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
100 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
104 typedef int (*ldlm_lock_callback)(struct lustre_handle *lockh,
105 struct ldlm_lock_desc *new, void *data,
108 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags);
113 struct ldlm_resource *l_resource;
114 struct ldlm_lock *l_parent;
115 struct list_head l_children;
116 struct list_head l_childof;
117 struct list_head l_res_link; /*position in one of three res lists*/
119 ldlm_mode_t l_req_mode;
120 ldlm_mode_t l_granted_mode;
122 ldlm_completion_callback l_completion_ast;
123 ldlm_lock_callback l_blocking_ast;
125 struct ptlrpc_connection *l_connection;
126 struct ptlrpc_client *l_client;
127 struct lustre_handle *l_connh;
129 struct lustre_handle l_remote_handle;
134 struct ldlm_extent l_extent;
135 __u32 l_version[RES_VERSION_SIZE];
140 /* If the lock is granted, a process sleeps on this waitq to learn when
141 * it's no longer in use. If the lock is not granted, a process sleeps
142 * on this waitq to learn when it becomes granted. */
143 wait_queue_head_t l_waitq;
146 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
147 typedef int (*ldlm_res_policy)(struct ldlm_lock *lock, void *req_cookie,
148 ldlm_mode_t mode, void *data);
150 #define LDLM_PLAIN 10
151 #define LDLM_EXTENT 11
152 #define LDLM_MDSINTENT 12
154 #define LDLM_MIN_TYPE 10
155 #define LDLM_MAX_TYPE 12
157 extern ldlm_res_compat ldlm_res_compat_table [];
158 extern ldlm_res_policy ldlm_res_policy_table [];
160 struct ldlm_resource {
161 struct ldlm_namespace *lr_namespace;
162 struct list_head lr_hash;
163 struct list_head lr_rootlink; /* link all root resources in NS */
164 struct ldlm_resource *lr_parent; /* 0 for a root resource */
165 struct list_head lr_children; /* list head for child resources */
166 struct list_head lr_childof; /* part of child list of parent */
168 struct list_head lr_granted;
169 struct list_head lr_converting;
170 struct list_head lr_waiting;
171 ldlm_mode_t lr_most_restr;
172 __u32 lr_type; /* PLAIN, EXTENT, or MDSINTENT */
173 struct ldlm_resource *lr_root;
174 __u64 lr_name[RES_NAME_SIZE];
175 __u32 lr_version[RES_VERSION_SIZE];
176 atomic_t lr_refcount;
180 struct ldlm_ast_work {
181 struct ldlm_lock *w_lock;
183 struct ldlm_lock_desc w_desc;
184 struct list_head w_list;
190 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
192 return (struct ldlm_extent *)(res->lr_name);
195 extern struct obd_ops ldlm_obd_ops;
198 extern char *ldlm_lockname[];
199 extern char *ldlm_typename[];
200 extern char *ldlm_it2str(int it);
202 #define LDLM_DEBUG(lock, format, a...) \
204 if (lock->l_resource == NULL) \
205 CDEBUG(D_DLMTRACE, "### " format \
206 " (UNKNOWN: lock %p(rc=%d/%d,%d) mode %s/%s on " \
207 "res \?\? (rc=\?\?) type \?\?\? remote %Lx)\n" , \
208 ## a, lock, lock->l_refc, lock->l_readers, \
210 ldlm_lockname[lock->l_granted_mode], \
211 ldlm_lockname[lock->l_req_mode], \
212 lock->l_remote_handle.addr); \
214 CDEBUG(D_DLMTRACE, "### " format \
215 " (%s: lock %p(rc=%d/%d,%d) mode %s/%s on res " \
216 "%Lu (rc=%d) type %s remote %Lx)\n" , ## a, \
217 lock->l_resource->lr_namespace->ns_name, lock, \
218 lock->l_refc, lock->l_readers, lock->l_writers, \
219 ldlm_lockname[lock->l_granted_mode], \
220 ldlm_lockname[lock->l_req_mode], \
221 lock->l_resource->lr_name[0], \
222 atomic_read(&lock->l_resource->lr_refcount), \
223 ldlm_typename[lock->l_resource->lr_type], \
224 lock->l_remote_handle.addr); \
227 #define LDLM_DEBUG_NOLOCK(format, a...) \
228 CDEBUG(D_DLMTRACE, "### " format "\n" , ## a)
231 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
232 int ldlm_extent_policy(struct ldlm_lock *, void *, ldlm_mode_t, void *);
235 int ldlm_handle_enqueue(struct ptlrpc_request *req);
236 int ldlm_handle_convert(struct ptlrpc_request *req);
237 int ldlm_handle_cancel(struct ptlrpc_request *req);
240 void ldlm_register_intent(int (*arg)(struct ldlm_lock *lock, void *req_cookie,
241 ldlm_mode_t mode, void *data));
242 void ldlm_unregister_intent(void);
243 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
244 struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle);
245 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh);
247 #define LDLM_LOCK_PUT(lock) \
249 LDLM_DEBUG(lock, "put"); \
250 ldlm_lock_put(lock); \
253 #define LDLM_LOCK_GET(lock) \
255 ldlm_lock_get(lock); \
256 LDLM_DEBUG(lock, "get"); \
260 void ldlm_lock_put(struct ldlm_lock *lock);
261 void ldlm_lock_destroy(struct ldlm_lock *lock);
262 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
263 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
264 void ldlm_lock_addref_internal(struct ldlm_lock* , __u32 mode);
265 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
266 void ldlm_grant_lock(struct ldlm_lock *lock);
267 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
268 void *cookie, int cookielen, ldlm_mode_t mode,
269 struct lustre_handle *lockh);
271 ldlm_lock_create(struct ldlm_namespace *ns,
272 struct lustre_handle *parent_lock_handle,
273 __u64 *res_id, __u32 type, ldlm_mode_t mode, void *data,
275 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock *lock, void *cookie,
276 int cookie_len, int *flags,
277 ldlm_completion_callback completion,
278 ldlm_lock_callback blocking);
279 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
281 void ldlm_lock_cancel(struct ldlm_lock *lock);
282 void ldlm_run_ast_work(struct list_head *rpc_list);
283 void ldlm_reprocess_all(struct ldlm_resource *res);
284 void ldlm_lock_dump(struct ldlm_lock *lock);
287 int ldlm_test(struct obd_device *device, struct ptlrpc_connection *conn);
290 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 local);
291 int ldlm_namespace_free(struct ldlm_namespace *ns);
293 /* resource.c - internal */
294 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
295 struct ldlm_resource *parent,
296 __u64 *name, __u32 type, int create);
297 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
298 int ldlm_resource_put(struct ldlm_resource *res);
299 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
300 struct ldlm_lock *lock);
301 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
302 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
303 void ldlm_resource_dump(struct ldlm_resource *res);
304 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3]);
307 int ldlm_completion_ast(struct ldlm_lock *lock, int flags);
308 int ldlm_cli_enqueue(struct lustre_handle *conn,
309 struct ptlrpc_request *req,
310 struct ldlm_namespace *ns,
311 struct lustre_handle *parent_lock_handle,
314 void *cookie, int cookielen,
317 ldlm_completion_callback completion,
318 ldlm_lock_callback callback,
321 struct lustre_handle *lockh);
322 int ldlm_match_or_enqueue(struct lustre_handle *connh,
323 struct ptlrpc_request *req,
324 struct ldlm_namespace *ns,
325 struct lustre_handle *parent_lock_handle,
328 void *cookie, int cookielen,
331 ldlm_completion_callback completion,
332 ldlm_lock_callback callback,
335 struct lustre_handle *lockh);
336 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
337 void *data, __u32 data_len);
338 int ldlm_cli_convert(struct lustre_handle *, int new_mode, int *flags);
339 int ldlm_cli_cancel(struct lustre_handle *lockh);
341 #endif /* __KERNEL__ */
343 /* ioctls for trying requests */
344 #define IOC_LDLM_TYPE 'f'
345 #define IOC_LDLM_MIN_NR 40
347 #define IOC_LDLM_TEST _IOWR('f', 40, long)
348 #define IOC_LDLM_MAX_NR 41