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 l_lock_init(&ns->ns_lock);
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 ldlm_resource_del_lock(lock);
89 rc = ldlm_resource_put(res);
96 int ldlm_namespace_free(struct ldlm_namespace *ns)
98 struct list_head *tmp, *pos;
104 l_lock(&ns->ns_lock);
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 rc = cleanup_resource(res, &res->lr_granted);
113 rc = cleanup_resource(res, &res->lr_converting);
115 rc = cleanup_resource(res, &res->lr_waiting);
118 CERROR("Resource refcount nonzero (%d) after "
119 "lock cleanup; forcing cleanup.\n",
120 atomic_read(&res->lr_refcount));
121 ldlm_resource_dump(res);
122 atomic_set(&res->lr_refcount, 1);
123 rc = ldlm_resource_put(res);
128 vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
129 ptlrpc_cleanup_client(&ns->ns_rpc_client);
130 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
131 OBD_FREE(ns, sizeof(*ns));
136 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
141 for (i = 0; i < RES_NAME_SIZE; i++)
144 hash += (__u32)((unsigned long)parent >> 4);
146 return (hash & RES_HASH_MASK);
149 static struct ldlm_resource *ldlm_resource_new(void)
151 struct ldlm_resource *res;
153 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
158 memset(res, 0, sizeof(*res));
160 INIT_LIST_HEAD(&res->lr_children);
161 INIT_LIST_HEAD(&res->lr_childof);
162 INIT_LIST_HEAD(&res->lr_granted);
163 INIT_LIST_HEAD(&res->lr_converting);
164 INIT_LIST_HEAD(&res->lr_waiting);
166 atomic_set(&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 l_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 atomic_inc(&res->lr_refcount);
245 if (res == NULL && create)
246 res = ldlm_resource_add(ns, parent, name, type);
247 l_unlock(&ns->ns_lock);
252 struct ldlm_resource *ldlm_resource_addref(struct ldlm_resource *res)
254 atomic_inc(&res->lr_refcount);
258 /* Returns 1 if the resource was freed, 0 if it remains. */
259 int ldlm_resource_put(struct ldlm_resource *res)
263 if (atomic_dec_and_test(&res->lr_refcount)) {
264 struct ldlm_namespace *ns = res->lr_namespace;
267 l_lock(&ns->ns_lock);
269 if (atomic_read(&res->lr_refcount) != 0) {
270 /* We lost the race. */
271 l_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 l_unlock(&ns->ns_lock);
298 if (atomic_read(&res->lr_refcount) < 0)
305 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
306 struct ldlm_lock *lock)
308 l_lock(&res->lr_namespace->ns_lock);
310 ldlm_resource_dump(res);
311 ldlm_lock_dump(lock);
313 if (!list_empty(&lock->l_res_link))
316 list_add(&lock->l_res_link, head);
317 l_unlock(&res->lr_namespace->ns_lock);
320 void ldlm_resource_del_lock(struct ldlm_lock *lock)
322 l_lock(&lock->l_resource->lr_namespace->ns_lock);
323 list_del_init(&lock->l_res_link);
324 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
327 int ldlm_get_resource_handle(struct ldlm_resource *res, struct lustre_handle *h)
333 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
335 desc->lr_type = res->lr_type;
336 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
337 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
340 void ldlm_resource_dump(struct ldlm_resource *res)
342 struct list_head *tmp;
345 if (RES_NAME_SIZE != 3)
348 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
349 (unsigned long long)res->lr_name[0],
350 (unsigned long long)res->lr_name[1],
351 (unsigned long long)res->lr_name[2]);
353 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
354 atomic_read(&res->lr_refcount));
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);