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