Whamcloud - gitweb
- Fixed some screwy ldlm resource refcount issues
[fs/lustre-release.git] / lustre / ldlm / ldlm_resource.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * by Cluster File Systems, Inc.
10  */
11
12 #define DEBUG_SUBSYSTEM S_LDLM
13
14 #include <linux/lustre_dlm.h>
15
16 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
17
18 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
19 {
20         struct ldlm_namespace *ns = NULL;
21         struct list_head *bucket;
22
23         OBD_ALLOC(ns, sizeof(*ns));
24         if (!ns) {
25                 LBUG();
26                 GOTO(out, NULL);
27         }
28
29         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
30         if (!ns->ns_hash) {
31                 LBUG();
32                 GOTO(out, ns);
33         }
34
35         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
36         if (!ns->ns_name) {
37                 LBUG();
38                 GOTO(out, ns);
39         }
40         strcpy(ns->ns_name, name);
41
42         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
43                            &ns->ns_rpc_client);
44
45         INIT_LIST_HEAD(&ns->ns_root_list);
46         ns->ns_lock = SPIN_LOCK_UNLOCKED;
47         ns->ns_refcount = 0;
48         ns->ns_client = client;
49
50         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
51              bucket--)
52                 INIT_LIST_HEAD(bucket);
53         RETURN(ns);
54
55  out: 
56         if (ns && ns->ns_hash)
57                 vfree(ns->ns_hash);
58         if (ns && ns->ns_name)
59                 OBD_FREE(ns->ns_name, strlen(name) + 1);
60         if (ns) 
61                 OBD_FREE(ns, sizeof(*ns));
62         return NULL;
63 }
64
65 static int cleanup_resource(struct ldlm_resource *res, struct list_head *q)
66 {
67         struct list_head *tmp, *pos;
68         int rc = 0, client = res->lr_namespace->ns_client;
69         ENTRY;
70
71         list_for_each_safe(tmp, pos, q) {
72                 struct ldlm_lock *lock;
73                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
74
75                 if (client) {
76                         rc = ldlm_cli_cancel(lock->l_client, lock);
77                         if (rc < 0) {
78                                 CERROR("ldlm_cli_cancel: %d\n", rc);
79                                 LBUG();
80                         }
81                         if (rc == ELDLM_RESOURCE_FREED)
82                                 rc = 1;
83                 } else {
84                         CERROR("Freeing a lock still held by a client node.\n");
85
86                         spin_lock(&lock->l_lock);
87                         ldlm_resource_del_lock(lock);
88                         ldlm_lock_free(lock);
89
90                         rc = ldlm_resource_put(res);
91                 }
92         }
93
94         RETURN(rc);
95 }
96
97 int ldlm_namespace_free(struct ldlm_namespace *ns)
98 {
99         struct list_head *tmp, *pos;
100         int i, rc;
101
102         if (!ns)
103                 RETURN(ELDLM_OK);
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);
110
111                         spin_lock(&res->lr_lock);
112                         rc = cleanup_resource(res, &res->lr_granted);
113                         if (!rc)
114                                 rc = cleanup_resource(res, &res->lr_converting);
115                         if (!rc)
116                                 rc = cleanup_resource(res, &res->lr_waiting);
117
118                         if (rc == 0) {
119                                 CERROR("Resource refcount nonzero after lock "
120                                        "cleanup; forcing cleanup.\n");
121                                 res->lr_refcount = 1;
122                                 rc = ldlm_resource_put(res);
123                         }
124                 }
125         }
126
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));
131
132         return ELDLM_OK;
133 }
134
135 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
136 {
137         __u32 hash = 0;
138         int i;
139
140         for (i = 0; i < RES_NAME_SIZE; i++)
141                 hash += name[i];
142
143         hash += (__u32)((unsigned long)parent >> 4);
144
145         return (hash & RES_HASH_MASK);
146 }
147
148 static struct ldlm_resource *ldlm_resource_new(void)
149 {
150         struct ldlm_resource *res;
151
152         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
153         if (res == NULL) {
154                 LBUG();
155                 return NULL;
156         }
157         memset(res, 0, sizeof(*res));
158
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);
164
165         res->lr_lock = SPIN_LOCK_UNLOCKED;
166         res->lr_refcount = 1;
167
168         return res;
169 }
170
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)
176 {
177         struct list_head *bucket;
178         struct ldlm_resource *res;
179         ENTRY;
180
181         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
182                 LBUG();
183                 RETURN(NULL);
184         }
185
186         res = ldlm_resource_new();
187         if (!res) {
188                 LBUG();
189                 RETURN(NULL);
190         }
191
192         memcpy(res->lr_name, name, sizeof(res->lr_name));
193         res->lr_namespace = ns;
194         ns->ns_refcount++;
195
196         res->lr_type = type; 
197         res->lr_most_restr = LCK_NL;
198
199         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
200         list_add(&res->lr_hash, bucket);
201
202         if (parent == NULL) {
203                 res->lr_parent = res;
204                 list_add(&res->lr_rootlink, &ns->ns_root_list);
205         } else {
206                 res->lr_parent = parent;
207                 list_add(&res->lr_childof, &parent->lr_children);
208         }
209
210         RETURN(res);
211 }
212
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)
219 {
220         struct list_head *bucket;
221         struct list_head *tmp = bucket;
222         struct ldlm_resource *res = NULL;
223         ENTRY;
224
225         if (ns == NULL || ns->ns_hash == NULL) {
226                 LBUG();
227                 RETURN(NULL);
228         }
229
230         spin_lock(&ns->ns_lock);
231         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
232
233         list_for_each(tmp, bucket) {
234                 struct ldlm_resource *chk;
235                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
236
237                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
238                         res = chk;
239                         spin_lock(&res->lr_lock);
240                         res->lr_refcount++;
241                         spin_unlock(&res->lr_lock);
242                         EXIT;
243                         break;
244                 }
245         }
246
247         if (res == NULL && create)
248                 res = ldlm_resource_add(ns, parent, name, type);
249         spin_unlock(&ns->ns_lock);
250
251         RETURN(res);
252 }
253
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
257  */
258 int ldlm_resource_put(struct ldlm_resource *res)
259 {
260         int rc = 0;
261
262         if (res->lr_refcount == 1) {
263                 struct ldlm_namespace *ns = res->lr_namespace;
264                 ENTRY;
265
266                 spin_unlock(&res->lr_lock);
267                 spin_lock(&ns->ns_lock);
268                 spin_lock(&res->lr_lock);
269
270                 if (res->lr_refcount != 1) {
271                         spin_unlock(&ns->ns_lock);
272                         goto out;
273                 }
274
275                 if (!list_empty(&res->lr_granted))
276                         LBUG();
277
278                 if (!list_empty(&res->lr_converting))
279                         LBUG();
280
281                 if (!list_empty(&res->lr_waiting))
282                         LBUG();
283
284                 if (!list_empty(&res->lr_children))
285                         LBUG();
286
287                 ns->ns_refcount--;
288                 list_del(&res->lr_hash);
289                 list_del(&res->lr_rootlink);
290                 list_del(&res->lr_childof);
291
292                 kmem_cache_free(ldlm_resource_slab, res);
293                 spin_unlock(&ns->ns_lock);
294                 rc = 1;
295         } else {
296                 ENTRY;
297         out:
298                 res->lr_refcount--;
299                 if (res->lr_refcount < 0)
300                         LBUG();
301         }
302
303         RETURN(rc); 
304 }
305
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)
309 {
310         ldlm_resource_dump(res); 
311         ldlm_lock_dump(lock); 
312         if (!list_empty(&lock->l_res_link))
313                 LBUG();
314
315         list_add(&lock->l_res_link, head);
316         res->lr_refcount++;
317 }
318
319 /* Must be called with resource->lr_lock taken */
320 int ldlm_resource_del_lock(struct ldlm_lock *lock)
321 {
322         if (!list_empty(&lock->l_res_link)) {
323                 list_del_init(&lock->l_res_link);
324                 return ldlm_resource_put(lock->l_resource);
325         }
326         return 0;
327 }
328
329 int ldlm_get_resource_handle(struct ldlm_resource *res, struct lustre_handle *h)
330 {
331         LBUG();
332         return 0;
333 }
334
335 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
336 {
337         desc->lr_type = res->lr_type;
338         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
339         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
340 }
341
342 void ldlm_resource_dump(struct ldlm_resource *res)
343 {
344         struct list_head *tmp;
345         char name[256];
346
347         if (RES_NAME_SIZE != 3)
348                 LBUG();
349
350         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
351                  (unsigned long long)res->lr_name[0],
352                  (unsigned long long)res->lr_name[1],
353                  (unsigned long long)res->lr_name[2]);
354
355         CDEBUG(D_OTHER, "--- Resource: %p (%s)\n", res, name);
356         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
357                res->lr_namespace->ns_name);
358         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
359
360         CDEBUG(D_OTHER, "Granted locks:\n");
361         list_for_each(tmp, &res->lr_granted) {
362                 struct ldlm_lock *lock;
363                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
364                 ldlm_lock_dump(lock);
365         }
366
367         CDEBUG(D_OTHER, "Converting locks:\n");
368         list_for_each(tmp, &res->lr_converting) {
369                 struct ldlm_lock *lock;
370                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
371                 ldlm_lock_dump(lock);
372         }
373
374         CDEBUG(D_OTHER, "Waiting locks:\n");
375         list_for_each(tmp, &res->lr_waiting) {
376                 struct ldlm_lock *lock;
377                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
378                 ldlm_lock_dump(lock);
379         }
380 }