Whamcloud - gitweb
- Back out the ldlm_everything_lock patch, which causes a deadlock on cleanup
[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 #include <linux/obd_class.h>
16
17 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
18
19 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
20 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
21 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
22
23 int ldlm_proc_setup(struct obd_device *obd)
24 {
25         ENTRY;
26
27         if (obd->obd_proc_entry == NULL)
28                 RETURN(-EINVAL);
29
30         ldlm_ns_proc_dir = proc_mkdir("namespaces", obd->obd_proc_entry);
31         if (ldlm_ns_proc_dir == NULL) {
32                 CERROR("Couldn't create /proc/lustre/ldlm/namespaces\n");
33                 RETURN(-EPERM);
34         }
35         RETURN(0);
36 }
37
38 void ldlm_proc_cleanup(struct obd_device *obd)
39 {
40         proc_lustre_remove_obd_entry("namespaces", obd);
41 }
42
43 /* FIXME: This can go away when we start to really use lprocfs */
44 static int lprocfs_ll_rd(char *page, char **start, off_t off,
45                          int count, int *eof, void *data)
46 {
47         int len;
48         __u64 *temp = (__u64 *)data;
49
50         len = snprintf(page, count, "%Lu\n", *temp);
51
52         return len;
53 }
54
55 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
56 {
57         struct ldlm_namespace *ns = NULL;
58         struct list_head *bucket;
59         struct proc_dir_entry *proc_entry;
60
61         OBD_ALLOC(ns, sizeof(*ns));
62         if (!ns) {
63                 LBUG();
64                 GOTO(out, NULL);
65         }
66
67         ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
68         if (!ns->ns_hash) {
69                 LBUG();
70                 GOTO(out, ns);
71         }
72         obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
73
74         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
75         if (!ns->ns_name) {
76                 LBUG();
77                 GOTO(out, ns);
78         }
79         strcpy(ns->ns_name, name);
80
81         INIT_LIST_HEAD(&ns->ns_root_list);
82         l_lock_init(&ns->ns_lock);
83         ns->ns_refcount = 0;
84         ns->ns_client = client;
85         spin_lock_init(&ns->ns_counter_lock);
86         ns->ns_locks = 0;
87         ns->ns_resources = 0;
88
89         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
90              bucket--)
91                 INIT_LIST_HEAD(bucket);
92
93         spin_lock(&ldlm_namespace_lock);
94         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
95         spin_unlock(&ldlm_namespace_lock);
96
97         ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
98         if (ns->ns_proc_dir == NULL)
99                 CERROR("Unable to create proc directory for namespace.\n");
100         proc_entry = create_proc_entry("resource_count", 0444, ns->ns_proc_dir);
101         proc_entry->read_proc = lprocfs_ll_rd;
102         proc_entry->data = &ns->ns_resources;
103         proc_entry = create_proc_entry("lock_count", 0444, ns->ns_proc_dir);
104         proc_entry->read_proc = lprocfs_ll_rd;
105         proc_entry->data = &ns->ns_locks;
106
107         RETURN(ns);
108
109  out:
110         if (ns && ns->ns_hash) {
111                 vfree(ns->ns_hash);
112                 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
113         }
114         if (ns && ns->ns_name)
115                 OBD_FREE(ns->ns_name, strlen(name) + 1);
116         if (ns)
117                 OBD_FREE(ns, sizeof(*ns));
118         return NULL;
119 }
120
121 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
122
123 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
124 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
125                              int local_only)
126 {
127         struct list_head *tmp, *pos;
128         int rc = 0, client = res->lr_namespace->ns_client;
129         ENTRY;
130
131         list_for_each_safe(tmp, pos, q) {
132                 struct ldlm_lock *lock;
133                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
134                 LDLM_LOCK_GET(lock);
135
136                 if (client) {
137                         struct lustre_handle lockh;
138                         ldlm_lock2handle(lock, &lockh);
139                         if (!local_only) {
140                                 rc = ldlm_cli_cancel(&lockh);
141                                 if (rc)
142                                         CERROR("ldlm_cli_cancel: %d\n", rc);
143                         }
144                         /* Force local cleanup on errors, too. */
145                         if (local_only || rc != ELDLM_OK)
146                                 ldlm_lock_cancel(lock);
147                 } else {
148                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
149                                    "client node.\n");
150
151                         ldlm_resource_unlink_lock(lock);
152                         ldlm_lock_destroy(lock);
153                 }
154                 LDLM_LOCK_PUT(lock);
155         }
156 }
157
158 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
159 {
160         int i;
161
162         l_lock(&ns->ns_lock);
163         for (i = 0; i < RES_HASH_SIZE; i++) {
164                 struct list_head *tmp, *pos;
165                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
166                         struct ldlm_resource *res;
167                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
168                         ldlm_resource_getref(res);
169
170                         cleanup_resource(res, &res->lr_granted, local_only);
171                         cleanup_resource(res, &res->lr_converting, local_only);
172                         cleanup_resource(res, &res->lr_waiting, local_only);
173
174                         /* XXX this is a bit counter-intuitive and should
175                          * probably be cleaner: don't force cleanup if we're
176                          * local_only (which is only used by recovery).  We
177                          * probably still have outstanding lock refs which
178                          * reference these resources. -phil */
179                         if (!ldlm_resource_put(res) && !local_only) {
180                                 CERROR("Resource refcount nonzero (%d) after "
181                                        "lock cleanup; forcing cleanup.\n",
182                                        atomic_read(&res->lr_refcount));
183                                 ldlm_resource_dump(res);
184                                 atomic_set(&res->lr_refcount, 1);
185                                 ldlm_resource_put(res);
186                         }
187                 }
188         }
189         l_unlock(&ns->ns_lock);
190
191         return ELDLM_OK;
192 }
193
194 /* Cleanup, but also free, the namespace */
195 int ldlm_namespace_free(struct ldlm_namespace *ns)
196 {
197         if (!ns)
198                 RETURN(ELDLM_OK);
199
200         spin_lock(&ldlm_namespace_lock);
201         list_del(&ns->ns_list_chain);
202         remove_proc_entry("resource_count", ns->ns_proc_dir);
203         remove_proc_entry("lock_count", ns->ns_proc_dir);
204         remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
205         spin_unlock(&ldlm_namespace_lock);
206
207         ldlm_namespace_cleanup(ns, 0);
208
209         vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
210         obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
211         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
212         OBD_FREE(ns, sizeof(*ns));
213
214         return ELDLM_OK;
215 }
216
217 int ldlm_client_free(struct obd_export *exp)
218 {
219         struct ldlm_export_data *led = &exp->exp_ldlm_data;
220         ptlrpc_cleanup_client(&led->led_import);
221         RETURN(0);
222 }
223
224 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
225 {
226         __u32 hash = 0;
227         int i;
228
229         for (i = 0; i < RES_NAME_SIZE; i++)
230                 hash += name[i];
231
232         hash += (__u32)((unsigned long)parent >> 4);
233
234         return (hash & RES_HASH_MASK);
235 }
236
237 static struct ldlm_resource *ldlm_resource_new(void)
238 {
239         struct ldlm_resource *res;
240
241         res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
242         if (res == NULL) {
243                 LBUG();
244                 return NULL;
245         }
246         memset(res, 0, sizeof(*res));
247
248         INIT_LIST_HEAD(&res->lr_children);
249         INIT_LIST_HEAD(&res->lr_childof);
250         INIT_LIST_HEAD(&res->lr_granted);
251         INIT_LIST_HEAD(&res->lr_converting);
252         INIT_LIST_HEAD(&res->lr_waiting);
253
254         atomic_set(&res->lr_refcount, 1);
255
256         return res;
257 }
258
259 /* Args: locked namespace
260  * Returns: newly-allocated, referenced, unlocked resource */
261 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
262                                                struct ldlm_resource *parent,
263                                                __u64 *name, __u32 type)
264 {
265         struct list_head *bucket;
266         struct ldlm_resource *res;
267         ENTRY;
268
269         if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
270                 LBUG();
271                 RETURN(NULL);
272         }
273
274         res = ldlm_resource_new();
275         if (!res) {
276                 LBUG();
277                 RETURN(NULL);
278         }
279
280         spin_lock(&ns->ns_counter_lock);
281         ns->ns_resources++;
282         spin_unlock(&ns->ns_counter_lock);
283
284         memcpy(res->lr_name, name, sizeof(res->lr_name));
285         res->lr_namespace = ns;
286         ns->ns_refcount++;
287
288         res->lr_type = type;
289         res->lr_most_restr = LCK_NL;
290
291         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
292         list_add(&res->lr_hash, bucket);
293
294         if (parent == NULL)
295                 list_add(&res->lr_childof, &ns->ns_root_list);
296         else {
297                 res->lr_parent = parent;
298                 list_add(&res->lr_childof, &parent->lr_children);
299         }
300
301         RETURN(res);
302 }
303
304 /* Args: unlocked namespace
305  * Locks: takes and releases ns->ns_lock and res->lr_lock
306  * Returns: referenced, unlocked ldlm_resource or NULL */
307 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
308                                         struct ldlm_resource *parent,
309                                         __u64 *name, __u32 type, int create)
310 {
311         struct list_head *bucket;
312         struct list_head *tmp = bucket;
313         struct ldlm_resource *res = NULL;
314         ENTRY;
315
316         if (ns == NULL || ns->ns_hash == NULL) {
317                 LBUG();
318                 RETURN(NULL);
319         }
320
321         l_lock(&ns->ns_lock);
322         bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
323
324         list_for_each(tmp, bucket) {
325                 struct ldlm_resource *chk;
326                 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
327
328                 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
329                         res = chk;
330                         atomic_inc(&res->lr_refcount);
331                         EXIT;
332                         break;
333                 }
334         }
335
336         if (res == NULL && create)
337                 res = ldlm_resource_add(ns, parent, name, type);
338         l_unlock(&ns->ns_lock);
339
340         RETURN(res);
341 }
342
343 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
344 {
345         atomic_inc(&res->lr_refcount);
346         return res;
347 }
348
349 /* Returns 1 if the resource was freed, 0 if it remains. */
350 int ldlm_resource_put(struct ldlm_resource *res)
351 {
352         int rc = 0;
353
354         if (atomic_dec_and_test(&res->lr_refcount)) {
355                 struct ldlm_namespace *ns = res->lr_namespace;
356                 ENTRY;
357
358                 l_lock(&ns->ns_lock);
359
360                 if (atomic_read(&res->lr_refcount) != 0) {
361                         /* We lost the race. */
362                         l_unlock(&ns->ns_lock);
363                         goto out;
364                 }
365
366                 if (!list_empty(&res->lr_granted))
367                         LBUG();
368
369                 if (!list_empty(&res->lr_converting))
370                         LBUG();
371
372                 if (!list_empty(&res->lr_waiting))
373                         LBUG();
374
375                 if (!list_empty(&res->lr_children))
376                         LBUG();
377
378                 ns->ns_refcount--;
379                 list_del(&res->lr_hash);
380                 list_del(&res->lr_childof);
381
382                 kmem_cache_free(ldlm_resource_slab, res);
383                 l_unlock(&ns->ns_lock);
384
385                 spin_lock(&ns->ns_counter_lock);
386                 ns->ns_resources--;
387                 spin_unlock(&ns->ns_counter_lock);
388
389                 rc = 1;
390         } else {
391                 ENTRY;
392         out:
393                 if (atomic_read(&res->lr_refcount) < 0)
394                         LBUG();
395         }
396
397         RETURN(rc);
398 }
399
400 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
401                             struct ldlm_lock *lock)
402 {
403         l_lock(&res->lr_namespace->ns_lock);
404
405         ldlm_resource_dump(res);
406         ldlm_lock_dump(lock);
407
408         if (!list_empty(&lock->l_res_link))
409                 LBUG();
410
411         list_add(&lock->l_res_link, head);
412         l_unlock(&res->lr_namespace->ns_lock);
413 }
414
415 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
416 {
417         l_lock(&lock->l_resource->lr_namespace->ns_lock);
418         list_del_init(&lock->l_res_link);
419         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
420 }
421
422 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
423 {
424         desc->lr_type = res->lr_type;
425         memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
426         memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
427 }
428
429 void ldlm_dump_all_namespaces(void)
430 {
431         struct list_head *tmp;
432
433         spin_lock(&ldlm_namespace_lock);
434
435         list_for_each(tmp, &ldlm_namespace_list) {
436                 struct ldlm_namespace *ns;
437                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
438                 ldlm_namespace_dump(ns);
439         }
440
441         spin_unlock(&ldlm_namespace_lock);
442 }
443
444 void ldlm_namespace_dump(struct ldlm_namespace *ns)
445 {
446         struct list_head *tmp;
447
448         l_lock(&ns->ns_lock);
449         CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
450                ns->ns_refcount, ns->ns_client);
451
452         list_for_each(tmp, &ns->ns_root_list) {
453                 struct ldlm_resource *res;
454                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
455
456                 /* Once we have resources with children, this should really dump
457                  * them recursively. */
458                 ldlm_resource_dump(res);
459         }
460         l_unlock(&ns->ns_lock);
461 }
462
463 void ldlm_resource_dump(struct ldlm_resource *res)
464 {
465         struct list_head *tmp;
466         char name[256];
467
468         if (RES_NAME_SIZE != 3)
469                 LBUG();
470
471         snprintf(name, sizeof(name), "%Lx %Lx %Lx",
472                  (unsigned long long)res->lr_name[0],
473                  (unsigned long long)res->lr_name[1],
474                  (unsigned long long)res->lr_name[2]);
475
476         CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
477                atomic_read(&res->lr_refcount));
478         CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
479                res->lr_namespace->ns_name);
480         CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
481
482         CDEBUG(D_OTHER, "Granted locks:\n");
483         list_for_each(tmp, &res->lr_granted) {
484                 struct ldlm_lock *lock;
485                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
486                 ldlm_lock_dump(lock);
487         }
488
489         CDEBUG(D_OTHER, "Converting locks:\n");
490         list_for_each(tmp, &res->lr_converting) {
491                 struct ldlm_lock *lock;
492                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
493                 ldlm_lock_dump(lock);
494         }
495
496         CDEBUG(D_OTHER, "Waiting locks:\n");
497         list_for_each(tmp, &res->lr_waiting) {
498                 struct ldlm_lock *lock;
499                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
500                 ldlm_lock_dump(lock);
501         }
502 }