1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * by Cluster File Systems, Inc.
12 #define DEBUG_SUBSYSTEM S_LDLM
14 #include <linux/lustre_dlm.h>
16 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
18 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
20 struct ldlm_namespace *ns = NULL;
21 struct list_head *bucket;
23 OBD_ALLOC(ns, sizeof(*ns));
29 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
35 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
40 strcpy(ns->ns_name, name);
42 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
45 INIT_LIST_HEAD(&ns->ns_root_list);
46 ns->ns_lock = SPIN_LOCK_UNLOCKED;
48 ns->ns_client = client;
50 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
52 INIT_LIST_HEAD(bucket);
56 if (ns && ns->ns_hash)
58 if (ns && ns->ns_name)
59 OBD_FREE(ns->ns_name, strlen(name) + 1);
61 OBD_FREE(ns, sizeof(*ns));
65 static int cleanup_resource(struct ldlm_resource *res, struct list_head *q)
67 struct list_head *tmp, *pos;
68 int rc = 0, client = res->lr_namespace->ns_client;
71 list_for_each_safe(tmp, pos, q) {
72 struct ldlm_lock *lock;
73 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
76 rc = ldlm_cli_cancel(lock->l_client, lock);
78 CERROR("ldlm_cli_cancel: %d\n", rc);
81 if (rc == ELDLM_RESOURCE_FREED)
84 CERROR("Freeing a lock still held by a client node.\n");
86 spin_lock(&lock->l_lock);
87 ldlm_resource_del_lock(lock);
90 rc = ldlm_resource_put(res);
97 int ldlm_namespace_free(struct ldlm_namespace *ns)
99 struct list_head *tmp, *pos;
104 /* We should probably take the ns_lock, but then ldlm_resource_put
105 * couldn't take it. Hmm. */
106 for (i = 0; i < RES_HASH_SIZE; i++) {
107 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
108 struct ldlm_resource *res;
109 res = list_entry(tmp, struct ldlm_resource, lr_hash);
111 spin_lock(&res->lr_lock);
112 rc = cleanup_resource(res, &res->lr_granted);
114 rc = cleanup_resource(res, &res->lr_converting);
116 rc = cleanup_resource(res, &res->lr_waiting);
119 CERROR("Resource refcount nonzero after lock "
120 "cleanup; forcing cleanup.\n");
121 res->lr_refcount = 1;
122 rc = ldlm_resource_put(res);
127 vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
128 ptlrpc_cleanup_client(&ns->ns_rpc_client);
129 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
130 OBD_FREE(ns, sizeof(*ns));
135 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
140 for (i = 0; i < RES_NAME_SIZE; i++)
143 hash += (__u32)((unsigned long)parent >> 4);
145 return (hash & RES_HASH_MASK);
148 static struct ldlm_resource *ldlm_resource_new(void)
150 struct ldlm_resource *res;
152 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
157 memset(res, 0, sizeof(*res));
159 INIT_LIST_HEAD(&res->lr_children);
160 INIT_LIST_HEAD(&res->lr_childof);
161 INIT_LIST_HEAD(&res->lr_granted);
162 INIT_LIST_HEAD(&res->lr_converting);
163 INIT_LIST_HEAD(&res->lr_waiting);
165 res->lr_lock = SPIN_LOCK_UNLOCKED;
166 res->lr_refcount = 1;
171 /* Args: locked namespace
172 * Returns: newly-allocated, referenced, unlocked resource */
173 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
174 struct ldlm_resource *parent,
175 __u64 *name, __u32 type)
177 struct list_head *bucket;
178 struct ldlm_resource *res;
181 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
186 res = ldlm_resource_new();
192 memcpy(res->lr_name, name, sizeof(res->lr_name));
193 res->lr_namespace = ns;
197 res->lr_most_restr = LCK_NL;
199 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
200 list_add(&res->lr_hash, bucket);
202 if (parent == NULL) {
203 res->lr_parent = res;
204 list_add(&res->lr_rootlink, &ns->ns_root_list);
206 res->lr_parent = parent;
207 list_add(&res->lr_childof, &parent->lr_children);
213 /* Args: unlocked namespace
214 * Locks: takes and releases ns->ns_lock and res->lr_lock
215 * Returns: referenced, unlocked ldlm_resource or NULL */
216 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
217 struct ldlm_resource *parent,
218 __u64 *name, __u32 type, int create)
220 struct list_head *bucket;
221 struct list_head *tmp = bucket;
222 struct ldlm_resource *res = NULL;
225 if (ns == NULL || ns->ns_hash == NULL) {
230 spin_lock(&ns->ns_lock);
231 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
233 list_for_each(tmp, bucket) {
234 struct ldlm_resource *chk;
235 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
237 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
239 spin_lock(&res->lr_lock);
241 spin_unlock(&res->lr_lock);
247 if (res == NULL && create)
248 res = ldlm_resource_add(ns, parent, name, type);
249 spin_unlock(&ns->ns_lock);
254 /* Args: locked resource
255 * Locks: takes and releases res->lr_lock
256 * takes and releases ns->ns_lock iff res->lr_refcount falls to 0
258 int ldlm_resource_put(struct ldlm_resource *res)
262 if (res->lr_refcount == 1) {
263 struct ldlm_namespace *ns = res->lr_namespace;
266 spin_unlock(&res->lr_lock);
267 spin_lock(&ns->ns_lock);
268 spin_lock(&res->lr_lock);
270 if (res->lr_refcount != 1) {
271 spin_unlock(&ns->ns_lock);
275 if (!list_empty(&res->lr_granted))
278 if (!list_empty(&res->lr_converting))
281 if (!list_empty(&res->lr_waiting))
284 if (!list_empty(&res->lr_children))
288 list_del(&res->lr_hash);
289 list_del(&res->lr_rootlink);
290 list_del(&res->lr_childof);
292 kmem_cache_free(ldlm_resource_slab, res);
293 spin_unlock(&ns->ns_lock);
299 if (res->lr_refcount < 0)
306 /* Must be called with resource->lr_lock taken */
307 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
308 struct ldlm_lock *lock)
310 ldlm_resource_dump(res);
311 ldlm_lock_dump(lock);
312 if (!list_empty(&lock->l_res_link))
315 list_add(&lock->l_res_link, head);
319 /* Must be called with resource->lr_lock taken */
320 void ldlm_resource_del_lock(struct ldlm_lock *lock)
322 if (!list_empty(&lock->l_res_link)) {
323 list_del_init(&lock->l_res_link);
324 lock->l_resource->lr_refcount--;
328 int ldlm_get_resource_handle(struct ldlm_resource *res, struct lustre_handle *h)
334 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
336 desc->lr_type = res->lr_type;
337 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
338 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
341 void ldlm_resource_dump(struct ldlm_resource *res)
343 struct list_head *tmp;
346 if (RES_NAME_SIZE != 3)
349 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
350 (unsigned long long)res->lr_name[0],
351 (unsigned long long)res->lr_name[1],
352 (unsigned long long)res->lr_name[2]);
354 CDEBUG(D_OTHER, "--- Resource: %p (%s)\n", res, name);
355 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
356 res->lr_namespace->ns_name);
357 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
359 CDEBUG(D_OTHER, "Granted locks:\n");
360 list_for_each(tmp, &res->lr_granted) {
361 struct ldlm_lock *lock;
362 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
363 ldlm_lock_dump(lock);
366 CDEBUG(D_OTHER, "Converting locks:\n");
367 list_for_each(tmp, &res->lr_converting) {
368 struct ldlm_lock *lock;
369 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
370 ldlm_lock_dump(lock);
373 CDEBUG(D_OTHER, "Waiting locks:\n");
374 list_for_each(tmp, &res->lr_waiting) {
375 struct ldlm_lock *lock;
376 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
377 ldlm_lock_dump(lock);