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 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
19 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
20 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
22 int ldlm_proc_setup(struct obd_device *obd)
26 if (obd->obd_proc_entry == NULL)
29 ldlm_ns_proc_dir = proc_mkdir("namespaces", obd->obd_proc_entry);
30 if (ldlm_ns_proc_dir == NULL) {
31 CERROR("Couldn't create /proc/lustre/ldlm/namespaces\n");
37 void ldlm_proc_cleanup(struct obd_device *obd)
39 proc_lustre_remove_obd_entry("namespaces", obd);
42 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
44 struct ldlm_namespace *ns = NULL;
45 struct list_head *bucket;
47 OBD_ALLOC(ns, sizeof(*ns));
53 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
59 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
64 strcpy(ns->ns_name, name);
66 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
69 INIT_LIST_HEAD(&ns->ns_root_list);
70 l_lock_init(&ns->ns_lock);
72 ns->ns_client = client;
74 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
76 INIT_LIST_HEAD(bucket);
78 spin_lock(&ldlm_namespace_lock);
79 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
80 ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
81 if (ns->ns_proc_dir == NULL)
82 CERROR("Unable to create proc directory for namespace.\n");
83 spin_unlock(&ldlm_namespace_lock);
88 if (ns && ns->ns_hash)
90 if (ns && ns->ns_name)
91 OBD_FREE(ns->ns_name, strlen(name) + 1);
93 OBD_FREE(ns, sizeof(*ns));
97 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
99 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q)
101 struct list_head *tmp, *pos;
102 int rc = 0, client = res->lr_namespace->ns_client;
105 list_for_each_safe(tmp, pos, q) {
106 struct ldlm_lock *lock;
107 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
111 struct lustre_handle lockh;
112 ldlm_lock2handle(lock, &lockh);
113 /* can we get away without a connh here? */
114 rc = ldlm_cli_cancel(&lockh);
115 if (rc != ELDLM_OK) {
116 /* It failed remotely, but we'll force it to
117 * cleanup locally. */
118 CERROR("ldlm_cli_cancel: %d\n", rc);
119 ldlm_lock_cancel(lock);
122 CERROR("Freeing lock %p still held by client node.\n",
124 ldlm_lock_dump(lock);
126 ldlm_resource_unlink_lock(lock);
127 ldlm_lock_destroy(lock);
133 int ldlm_namespace_free(struct ldlm_namespace *ns)
135 struct list_head *tmp, *pos;
141 spin_lock(&ldlm_namespace_lock);
142 list_del(&ns->ns_list_chain);
143 remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
144 spin_unlock(&ldlm_namespace_lock);
146 l_lock(&ns->ns_lock);
148 for (i = 0; i < RES_HASH_SIZE; i++) {
149 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
150 struct ldlm_resource *res;
151 res = list_entry(tmp, struct ldlm_resource, lr_hash);
152 ldlm_resource_getref(res);
154 cleanup_resource(res, &res->lr_granted);
155 cleanup_resource(res, &res->lr_converting);
156 cleanup_resource(res, &res->lr_waiting);
158 if (!ldlm_resource_put(res)) {
159 CERROR("Resource refcount nonzero (%d) after "
160 "lock cleanup; forcing cleanup.\n",
161 atomic_read(&res->lr_refcount));
162 ldlm_resource_dump(res);
163 atomic_set(&res->lr_refcount, 1);
164 ldlm_resource_put(res);
169 vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
170 ptlrpc_cleanup_client(&ns->ns_rpc_client);
171 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
172 OBD_FREE(ns, sizeof(*ns));
177 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
182 for (i = 0; i < RES_NAME_SIZE; i++)
185 hash += (__u32)((unsigned long)parent >> 4);
187 return (hash & RES_HASH_MASK);
190 static struct ldlm_resource *ldlm_resource_new(void)
192 struct ldlm_resource *res;
194 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
199 memset(res, 0, sizeof(*res));
201 INIT_LIST_HEAD(&res->lr_children);
202 INIT_LIST_HEAD(&res->lr_childof);
203 INIT_LIST_HEAD(&res->lr_granted);
204 INIT_LIST_HEAD(&res->lr_converting);
205 INIT_LIST_HEAD(&res->lr_waiting);
207 atomic_set(&res->lr_refcount, 1);
212 /* Args: locked namespace
213 * Returns: newly-allocated, referenced, unlocked resource */
214 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
215 struct ldlm_resource *parent,
216 __u64 *name, __u32 type)
218 struct list_head *bucket;
219 struct ldlm_resource *res;
222 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
227 res = ldlm_resource_new();
233 memcpy(res->lr_name, name, sizeof(res->lr_name));
234 res->lr_namespace = ns;
238 res->lr_most_restr = LCK_NL;
240 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
241 list_add(&res->lr_hash, bucket);
244 list_add(&res->lr_childof, &ns->ns_root_list);
246 res->lr_parent = parent;
247 list_add(&res->lr_childof, &parent->lr_children);
253 /* Args: unlocked namespace
254 * Locks: takes and releases ns->ns_lock and res->lr_lock
255 * Returns: referenced, unlocked ldlm_resource or NULL */
256 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
257 struct ldlm_resource *parent,
258 __u64 *name, __u32 type, int create)
260 struct list_head *bucket;
261 struct list_head *tmp = bucket;
262 struct ldlm_resource *res = NULL;
265 if (ns == NULL || ns->ns_hash == NULL) {
270 l_lock(&ns->ns_lock);
271 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
273 list_for_each(tmp, bucket) {
274 struct ldlm_resource *chk;
275 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
277 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
279 atomic_inc(&res->lr_refcount);
285 if (res == NULL && create)
286 res = ldlm_resource_add(ns, parent, name, type);
287 l_unlock(&ns->ns_lock);
292 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
294 atomic_inc(&res->lr_refcount);
298 /* Returns 1 if the resource was freed, 0 if it remains. */
299 int ldlm_resource_put(struct ldlm_resource *res)
303 if (atomic_dec_and_test(&res->lr_refcount)) {
304 struct ldlm_namespace *ns = res->lr_namespace;
307 l_lock(&ns->ns_lock);
309 if (atomic_read(&res->lr_refcount) != 0) {
310 /* We lost the race. */
311 l_unlock(&ns->ns_lock);
315 if (!list_empty(&res->lr_granted))
318 if (!list_empty(&res->lr_converting))
321 if (!list_empty(&res->lr_waiting))
324 if (!list_empty(&res->lr_children))
328 list_del(&res->lr_hash);
329 list_del(&res->lr_childof);
331 kmem_cache_free(ldlm_resource_slab, res);
332 l_unlock(&ns->ns_lock);
337 if (atomic_read(&res->lr_refcount) < 0)
344 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
345 struct ldlm_lock *lock)
347 l_lock(&res->lr_namespace->ns_lock);
349 ldlm_resource_dump(res);
350 ldlm_lock_dump(lock);
352 if (!list_empty(&lock->l_res_link))
355 list_add(&lock->l_res_link, head);
356 l_unlock(&res->lr_namespace->ns_lock);
359 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
361 l_lock(&lock->l_resource->lr_namespace->ns_lock);
362 list_del_init(&lock->l_res_link);
363 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
366 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
368 desc->lr_type = res->lr_type;
369 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
370 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
373 void ldlm_dump_all_namespaces(void)
375 struct list_head *tmp;
377 spin_lock(&ldlm_namespace_lock);
379 list_for_each(tmp, &ldlm_namespace_list) {
380 struct ldlm_namespace *ns;
381 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
382 ldlm_namespace_dump(ns);
385 spin_unlock(&ldlm_namespace_lock);
388 void ldlm_namespace_dump(struct ldlm_namespace *ns)
390 struct list_head *tmp;
392 l_lock(&ns->ns_lock);
393 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
394 ns->ns_refcount, ns->ns_client);
396 list_for_each(tmp, &ns->ns_root_list) {
397 struct ldlm_resource *res;
398 res = list_entry(tmp, struct ldlm_resource, lr_childof);
400 /* Once we have resources with children, this should really dump
401 * them recursively. */
402 ldlm_resource_dump(res);
404 l_unlock(&ns->ns_lock);
407 void ldlm_resource_dump(struct ldlm_resource *res)
409 struct list_head *tmp;
412 if (RES_NAME_SIZE != 3)
415 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
416 (unsigned long long)res->lr_name[0],
417 (unsigned long long)res->lr_name[1],
418 (unsigned long long)res->lr_name[2]);
420 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
421 atomic_read(&res->lr_refcount));
422 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
423 res->lr_namespace->ns_name);
424 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
426 CDEBUG(D_OTHER, "Granted locks:\n");
427 list_for_each(tmp, &res->lr_granted) {
428 struct ldlm_lock *lock;
429 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
430 ldlm_lock_dump(lock);
433 CDEBUG(D_OTHER, "Converting locks:\n");
434 list_for_each(tmp, &res->lr_converting) {
435 struct ldlm_lock *lock;
436 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
437 ldlm_lock_dump(lock);
440 CDEBUG(D_OTHER, "Waiting locks:\n");
441 list_for_each(tmp, &res->lr_waiting) {
442 struct ldlm_lock *lock;
443 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
444 ldlm_lock_dump(lock);