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 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
67 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q)
69 struct list_head *tmp, *pos;
70 int rc = 0, client = res->lr_namespace->ns_client;
73 list_for_each_safe(tmp, pos, q) {
74 struct ldlm_lock *lock;
75 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
79 struct lustre_handle lockh;
80 ldlm_lock2handle(lock, &lockh);
81 /* can we get away without a connh here? */
82 rc = ldlm_cli_cancel(&lockh);
84 /* It failed remotely, but we'll force it to
86 CERROR("ldlm_cli_cancel: %d\n", rc);
87 ldlm_lock_cancel(lock);
90 CERROR("Freeing a lock still held by a client node.\n");
92 ldlm_resource_unlink_lock(lock);
93 ldlm_lock_destroy(lock);
99 int ldlm_namespace_free(struct ldlm_namespace *ns)
101 struct list_head *tmp, *pos;
107 l_lock(&ns->ns_lock);
109 for (i = 0; i < RES_HASH_SIZE; i++) {
110 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
111 struct ldlm_resource *res;
112 res = list_entry(tmp, struct ldlm_resource, lr_hash);
113 ldlm_resource_getref(res);
115 cleanup_resource(res, &res->lr_granted);
116 cleanup_resource(res, &res->lr_converting);
117 cleanup_resource(res, &res->lr_waiting);
119 if (!ldlm_resource_put(res)) {
120 CERROR("Resource refcount nonzero (%d) after "
121 "lock cleanup; forcing cleanup.\n",
122 atomic_read(&res->lr_refcount));
123 ldlm_resource_dump(res);
124 atomic_set(&res->lr_refcount, 1);
125 ldlm_resource_put(res);
130 vfree(ns->ns_hash /* , sizeof(struct list_head) * RES_HASH_SIZE */);
131 ptlrpc_cleanup_client(&ns->ns_rpc_client);
132 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
133 OBD_FREE(ns, sizeof(*ns));
138 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
143 for (i = 0; i < RES_NAME_SIZE; i++)
146 hash += (__u32)((unsigned long)parent >> 4);
148 return (hash & RES_HASH_MASK);
151 static struct ldlm_resource *ldlm_resource_new(void)
153 struct ldlm_resource *res;
155 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
160 memset(res, 0, sizeof(*res));
162 INIT_LIST_HEAD(&res->lr_children);
163 INIT_LIST_HEAD(&res->lr_childof);
164 INIT_LIST_HEAD(&res->lr_granted);
165 INIT_LIST_HEAD(&res->lr_converting);
166 INIT_LIST_HEAD(&res->lr_waiting);
168 atomic_set(&res->lr_refcount, 1);
173 /* Args: locked namespace
174 * Returns: newly-allocated, referenced, unlocked resource */
175 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
176 struct ldlm_resource *parent,
177 __u64 *name, __u32 type)
179 struct list_head *bucket;
180 struct ldlm_resource *res;
183 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
188 res = ldlm_resource_new();
194 memcpy(res->lr_name, name, sizeof(res->lr_name));
195 res->lr_namespace = ns;
199 res->lr_most_restr = LCK_NL;
201 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
202 list_add(&res->lr_hash, bucket);
204 if (parent == NULL) {
205 res->lr_parent = res;
206 list_add(&res->lr_rootlink, &ns->ns_root_list);
208 res->lr_parent = parent;
209 list_add(&res->lr_childof, &parent->lr_children);
215 /* Args: unlocked namespace
216 * Locks: takes and releases ns->ns_lock and res->lr_lock
217 * Returns: referenced, unlocked ldlm_resource or NULL */
218 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
219 struct ldlm_resource *parent,
220 __u64 *name, __u32 type, int create)
222 struct list_head *bucket;
223 struct list_head *tmp = bucket;
224 struct ldlm_resource *res = NULL;
227 if (ns == NULL || ns->ns_hash == NULL) {
232 l_lock(&ns->ns_lock);
233 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
235 list_for_each(tmp, bucket) {
236 struct ldlm_resource *chk;
237 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
239 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
241 atomic_inc(&res->lr_refcount);
247 if (res == NULL && create)
248 res = ldlm_resource_add(ns, parent, name, type);
249 l_unlock(&ns->ns_lock);
254 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
256 atomic_inc(&res->lr_refcount);
260 /* Returns 1 if the resource was freed, 0 if it remains. */
261 int ldlm_resource_put(struct ldlm_resource *res)
265 if (atomic_dec_and_test(&res->lr_refcount)) {
266 struct ldlm_namespace *ns = res->lr_namespace;
269 l_lock(&ns->ns_lock);
271 if (atomic_read(&res->lr_refcount) != 0) {
272 /* We lost the race. */
273 l_unlock(&ns->ns_lock);
277 if (!list_empty(&res->lr_granted))
280 if (!list_empty(&res->lr_converting))
283 if (!list_empty(&res->lr_waiting))
286 if (!list_empty(&res->lr_children))
290 list_del(&res->lr_hash);
291 list_del(&res->lr_rootlink);
292 list_del(&res->lr_childof);
294 kmem_cache_free(ldlm_resource_slab, res);
295 l_unlock(&ns->ns_lock);
300 if (atomic_read(&res->lr_refcount) < 0)
307 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
308 struct ldlm_lock *lock)
310 l_lock(&res->lr_namespace->ns_lock);
312 ldlm_resource_dump(res);
313 ldlm_lock_dump(lock);
315 if (!list_empty(&lock->l_res_link))
318 list_add(&lock->l_res_link, head);
319 l_unlock(&res->lr_namespace->ns_lock);
322 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
324 l_lock(&lock->l_resource->lr_namespace->ns_lock);
325 list_del_init(&lock->l_res_link);
326 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
329 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
331 desc->lr_type = res->lr_type;
332 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
333 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
336 void ldlm_resource_dump(struct ldlm_resource *res)
338 struct list_head *tmp;
341 if (RES_NAME_SIZE != 3)
344 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
345 (unsigned long long)res->lr_name[0],
346 (unsigned long long)res->lr_name[1],
347 (unsigned long long)res->lr_name[2]);
349 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
350 atomic_read(&res->lr_refcount));
351 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
352 res->lr_namespace->ns_name);
353 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
355 CDEBUG(D_OTHER, "Granted locks:\n");
356 list_for_each(tmp, &res->lr_granted) {
357 struct ldlm_lock *lock;
358 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
359 ldlm_lock_dump(lock);
362 CDEBUG(D_OTHER, "Converting locks:\n");
363 list_for_each(tmp, &res->lr_converting) {
364 struct ldlm_lock *lock;
365 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
366 ldlm_lock_dump(lock);
369 CDEBUG(D_OTHER, "Waiting locks:\n");
370 list_for_each(tmp, &res->lr_waiting) {
371 struct ldlm_lock *lock;
372 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
373 ldlm_lock_dump(lock);