Whamcloud - gitweb
b=13220
[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, 2003 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *   Author: Peter Braam <braam@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LDLM
28 #ifdef __KERNEL__
29 # include <lustre_dlm.h>
30 #else
31 # include <liblustre.h>
32 #endif
33
34 #include <obd_class.h>
35 #include "ldlm_internal.h"
36
37 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
38
39 struct semaphore ldlm_namespace_lock;
40 struct list_head ldlm_namespace_list = CFS_LIST_HEAD_INIT(ldlm_namespace_list);
41 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
42 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
43 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
44
45 #ifdef LPROCFS
46 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
47                              unsigned long count, void *data)
48 {
49         ldlm_dump_all_namespaces(D_DLMTRACE);
50         RETURN(count);
51 }
52
53 int ldlm_proc_setup(void)
54 {
55         int rc;
56         struct lprocfs_vars list[] = {
57                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
58                 { NULL }};
59         ENTRY;
60         LASSERT(ldlm_ns_proc_dir == NULL);
61
62         ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
63                                               proc_lustre_root,
64                                               NULL, NULL);
65         if (IS_ERR(ldlm_type_proc_dir)) {
66                 CERROR("LProcFS failed in ldlm-init\n");
67                 rc = PTR_ERR(ldlm_type_proc_dir);
68                 GOTO(err, rc);
69         }
70
71         ldlm_ns_proc_dir = lprocfs_register("namespaces",
72                                             ldlm_type_proc_dir,
73                                             NULL, NULL);
74         if (IS_ERR(ldlm_ns_proc_dir)) {
75                 CERROR("LProcFS failed in ldlm-init\n");
76                 rc = PTR_ERR(ldlm_ns_proc_dir);
77                 GOTO(err_type, rc);
78         }
79
80         ldlm_svc_proc_dir = lprocfs_register("services",
81                                             ldlm_type_proc_dir,
82                                             NULL, NULL);
83         if (IS_ERR(ldlm_svc_proc_dir)) {
84                 CERROR("LProcFS failed in ldlm-init\n");
85                 rc = PTR_ERR(ldlm_svc_proc_dir);
86                 GOTO(err_ns, rc);
87         }
88
89         rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
90
91         RETURN(0);
92
93 err_ns:
94         lprocfs_remove(&ldlm_ns_proc_dir);
95 err_type:
96         lprocfs_remove(&ldlm_type_proc_dir);
97 err:
98         ldlm_svc_proc_dir = NULL;
99         RETURN(rc);
100 }
101
102 void ldlm_proc_cleanup(void)
103 {
104         if (ldlm_svc_proc_dir) 
105                 lprocfs_remove(&ldlm_svc_proc_dir);
106
107         if (ldlm_ns_proc_dir) 
108                 lprocfs_remove(&ldlm_ns_proc_dir);
109
110         if (ldlm_type_proc_dir)
111                 lprocfs_remove(&ldlm_type_proc_dir);
112 }
113
114 static int lprocfs_uint_rd(char *page, char **start, off_t off,
115                            int count, int *eof, void *data)
116 {
117         unsigned int *temp = (unsigned int *)data;
118         return snprintf(page, count, "%u\n", *temp);
119 }
120
121 #define MAX_STRING_SIZE 128
122 static int lprocfs_uint_wr(struct file *file, const char *buffer,
123                            unsigned long count, void *data)
124 {
125         unsigned *p = data;
126         char dummy[MAX_STRING_SIZE + 1], *end;
127         unsigned long tmp;
128
129         dummy[MAX_STRING_SIZE] = '\0';
130         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
131                 return -EFAULT;
132
133         tmp = simple_strtoul(dummy, &end, 0);
134         if (dummy == end)
135                 return -EINVAL;
136
137         *p = (unsigned int)tmp;
138         return count;
139 }
140
141 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
142                                  int count, int *eof, void *data)
143 {
144         struct ldlm_namespace *ns = data;
145         return lprocfs_uint_rd(page, start, off, count, eof,
146                                &ns->ns_max_unused);
147 }
148
149 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
150                                   unsigned long count, void *data)
151 {
152         struct ldlm_namespace *ns = data;
153         char dummy[MAX_STRING_SIZE + 1], *end;
154         unsigned long tmp;
155
156         dummy[MAX_STRING_SIZE] = '\0';
157         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
158                 return -EFAULT;
159
160         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
161                 CDEBUG(D_DLMTRACE,
162                        "dropping all unused locks from namespace %s\n",
163                        ns->ns_name);
164                 tmp = ns->ns_max_unused;
165                 ns->ns_max_unused = 0;
166                 ldlm_cancel_lru(ns, LDLM_SYNC);
167                 ns->ns_max_unused = tmp;
168                 return count;
169         }
170
171         tmp = simple_strtoul(dummy, &end, 0);
172         if (tmp == 0 && *end) {
173                 CERROR("invalid value written\n");
174                 return -EINVAL;
175         }
176
177         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
178                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
179         ns->ns_max_unused = (unsigned int)tmp;
180
181         ldlm_cancel_lru(ns, LDLM_ASYNC);
182
183         return count;
184 }
185
186 void ldlm_proc_namespace(struct ldlm_namespace *ns)
187 {
188         struct lprocfs_vars lock_vars[2];
189         char lock_name[MAX_STRING_SIZE + 1];
190
191         LASSERT(ns != NULL);
192         LASSERT(ns->ns_name != NULL);
193
194         lock_name[MAX_STRING_SIZE] = '\0';
195
196         memset(lock_vars, 0, sizeof(lock_vars));
197         lock_vars[0].name = lock_name;
198
199         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
200         lock_vars[0].data = &ns->ns_refcount;
201         lock_vars[0].read_fptr = lprocfs_rd_atomic;
202         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
203
204         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
205         lock_vars[0].data = &ns->ns_locks;
206         lock_vars[0].read_fptr = lprocfs_rd_atomic;
207         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
208
209         if (ns->ns_client) {
210                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
211                          ns->ns_name);
212                 lock_vars[0].data = &ns->ns_nr_unused;
213                 lock_vars[0].read_fptr = lprocfs_uint_rd;
214                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
215
216                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
217                          ns->ns_name);
218                 lock_vars[0].data = ns;
219                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
220                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
221                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
222                 
223                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
224                          ns->ns_name);
225                 lock_vars[0].data = &ns->ns_max_age;
226                 lock_vars[0].read_fptr = lprocfs_uint_rd;
227                 lock_vars[0].write_fptr = lprocfs_uint_wr;
228                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
229         }
230 }
231 #undef MAX_STRING_SIZE
232 #else
233 #define ldlm_proc_namespace(ns) do {} while (0)
234 #endif /* LPROCFS */
235
236 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
237 {
238         struct ldlm_namespace *ns = NULL;
239         struct list_head *bucket;
240         int rc;
241         ENTRY;
242
243         rc = ldlm_get_ref();
244         if (rc) {
245                 CERROR("ldlm_get_ref failed: %d\n", rc);
246                 RETURN(NULL);
247         }
248
249         OBD_ALLOC(ns, sizeof(*ns));
250         if (!ns)
251                 GOTO(out_ref, NULL);
252
253         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
254         if (!ns->ns_hash)
255                 GOTO(out_ns, NULL);
256
257         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
258         if (!ns->ns_name)
259                 GOTO(out_hash, NULL);
260
261         strcpy(ns->ns_name, name);
262
263         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
264         ns->ns_refcount = 0;
265         ns->ns_client = client;
266         spin_lock_init(&ns->ns_hash_lock);
267         atomic_set(&ns->ns_locks, 0);
268         ns->ns_resources = 0;
269         cfs_waitq_init(&ns->ns_waitq);
270
271         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
272              bucket--)
273                 CFS_INIT_LIST_HEAD(bucket);
274
275         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
276         ns->ns_nr_unused = 0;
277         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
278         ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
279         spin_lock_init(&ns->ns_unused_lock);
280
281         mutex_down(&ldlm_namespace_lock);
282         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
283         mutex_up(&ldlm_namespace_lock);
284         ldlm_proc_namespace(ns);
285         RETURN(ns);
286
287 out_hash:
288         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
289         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
290 out_ns:
291         OBD_FREE(ns, sizeof(*ns));
292 out_ref:
293         ldlm_put_ref(0);
294         RETURN(NULL);
295 }
296
297 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
298
299 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
300  * This is currently only used for recovery, and we make certain assumptions
301  * as a result--notably, that we shouldn't cancel locks with refs. -phil
302  *
303  * Called with the ns_lock held. */
304 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
305                              int flags)
306 {
307         struct list_head *tmp;
308         int rc = 0, client = res->lr_namespace->ns_client;
309         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
310         ENTRY;
311
312         
313         do {
314                 struct ldlm_lock *lock = NULL;
315  
316                 /* first, we look for non-cleaned-yet lock
317                  * all cleaned locks are marked by CLEANED flag */
318                 lock_res(res);
319                 list_for_each(tmp, q) {
320                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
321                         if (lock->l_flags & LDLM_FL_CLEANED) {
322                                 lock = NULL;
323                                 continue;
324                         }
325                         LDLM_LOCK_GET(lock);
326                         lock->l_flags |= LDLM_FL_CLEANED;
327                         break;
328                 }
329                 
330                 if (lock == NULL) {
331                         unlock_res(res);
332                         break;
333                 }
334
335                 /* Set CBPENDING so nothing in the cancellation path
336                  * can match this lock */
337                 lock->l_flags |= LDLM_FL_CBPENDING;
338                 lock->l_flags |= LDLM_FL_FAILED;
339                 lock->l_flags |= flags;
340
341                 /* ... without sending a CANCEL message for local_only. */
342                 if (local_only)
343                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
344
345                 if (local_only && (lock->l_readers || lock->l_writers)) {
346                         /* This is a little bit gross, but much better than the
347                          * alternative: pretend that we got a blocking AST from
348                          * the server, so that when the lock is decref'd, it
349                          * will go away ... */
350                         unlock_res(res);
351                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
352                         if (lock->l_completion_ast)
353                                 lock->l_completion_ast(lock, 0, NULL);
354                         LDLM_LOCK_PUT(lock);
355                         continue;
356                 }
357
358                 if (client) {
359                         struct lustre_handle lockh;
360
361                         unlock_res(res);
362                         ldlm_lock2handle(lock, &lockh);
363                         rc = ldlm_cli_cancel(&lockh);
364                         if (rc)
365                                 CERROR("ldlm_cli_cancel: %d\n", rc);
366                 } else {
367                         ldlm_resource_unlink_lock(lock);
368                         unlock_res(res);
369                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
370                                    "client node");
371                         ldlm_lock_destroy(lock);
372                 }
373                 LDLM_LOCK_PUT(lock);
374         } while (1);
375
376         EXIT;
377 }
378
379 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
380 {
381         struct list_head *tmp;
382         int i;
383
384         if (ns == NULL) {
385                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
386                 return ELDLM_OK;
387         }
388
389         for (i = 0; i < RES_HASH_SIZE; i++) {
390                 spin_lock(&ns->ns_hash_lock);
391                 tmp = ns->ns_hash[i].next;
392                 while (tmp != &(ns->ns_hash[i])) {
393                         struct ldlm_resource *res;
394                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
395                         ldlm_resource_getref(res);
396                         spin_unlock(&ns->ns_hash_lock);
397
398                         cleanup_resource(res, &res->lr_granted, flags);
399                         cleanup_resource(res, &res->lr_converting, flags);
400                         cleanup_resource(res, &res->lr_waiting, flags);
401
402                         spin_lock(&ns->ns_hash_lock);
403                         tmp  = tmp->next;
404
405                         /* XXX: former stuff caused issues in case of race
406                          * between ldlm_namespace_cleanup() and lockd() when
407                          * client gets blocking ast when lock gets distracted by
408                          * server. This is 1_4 branch solution, let's see how
409                          * will it behave. */
410                         if (!ldlm_resource_putref_locked(res))
411                                 CDEBUG(D_INFO,
412                                        "Namespace %s resource refcount nonzero "
413                                        "(%d) after lock cleanup; forcing cleanup.\n",
414                                        ns->ns_name, atomic_read(&res->lr_refcount));
415                 }
416                 spin_unlock(&ns->ns_hash_lock);
417         }
418
419         return ELDLM_OK;
420 }
421
422 int ldlm_namespace_free_prior(struct ldlm_namespace *ns)
423 {
424         ENTRY;
425         if (!ns)
426                 RETURN(ELDLM_OK);
427
428         mutex_down(&ldlm_namespace_lock);
429         list_del(&ns->ns_list_chain);
430         mutex_up(&ldlm_namespace_lock);
431
432         /* At shutdown time, don't call the cancellation callback */
433         ldlm_namespace_cleanup(ns, 0);
434
435         if (ns->ns_refcount > 0) {
436                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
437                 int rc;
438                 CDEBUG(D_DLMTRACE,
439                        "dlm namespace %s free waiting on refcount %d\n",
440                        ns->ns_name, ns->ns_refcount);
441                 rc = l_wait_event(ns->ns_waitq,
442                                   ns->ns_refcount == 0, &lwi);
443                 if (ns->ns_refcount)
444                         LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
445                                            "namespace cleanup aborted with %d "
446                                            "resources in use. (%d)\nI'm going "
447                                            "to try to clean up anyway, but I "
448                                            "might need a reboot of this node.\n",
449                                             ns->ns_name, (int) ns->ns_refcount, 
450                                             rc);
451                 CDEBUG(D_DLMTRACE,
452                        "dlm namespace %s free done waiting\n", ns->ns_name);
453         }
454
455         RETURN(ELDLM_OK);
456 }
457
458 int ldlm_namespace_free_post(struct ldlm_namespace *ns, int force)
459 {
460         ENTRY;
461         if (!ns)
462                 RETURN(ELDLM_OK);
463
464 #ifdef LPROCFS
465         {
466                 struct proc_dir_entry *dir;
467                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
468                 if (dir == NULL) {
469                         CERROR("dlm namespace %s has no procfs dir?\n",
470                                ns->ns_name);
471                 } else {
472                         lprocfs_remove(&dir);
473                 }
474         }
475 #endif
476
477         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
478         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
479         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
480         OBD_FREE(ns, sizeof(*ns));
481
482         ldlm_put_ref(force);
483         RETURN(ELDLM_OK);
484 }
485
486 /* Cleanup the resource, and free namespace.
487  * bug 12864:
488  * Deadlock issue: 
489  * proc1: destroy import 
490  *        class_disconnect_export(grab cl_sem) -> 
491  *              -> ldlm_namespace_free -> 
492  *              -> lprocfs_remove(grab _lprocfs_lock).
493  * proc2: read proc info
494  *        lprocfs_fops_read(grab _lprocfs_lock) ->
495  *              -> osc_rd_active, etc(grab cl_sem).
496  *
497  * So that I have to split the ldlm_namespace_free into two parts - the first
498  * part ldlm_namespace_free_prior is used to cleanup the resource which is
499  * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
500  * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem 
501  * held.
502  */
503 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
504 {
505         ldlm_namespace_free_prior(ns);
506         ldlm_namespace_free_post(ns, force);
507         return ELDLM_OK;
508 }
509
510 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
511 {
512         __u32 hash = 0;
513         int i;
514
515         for (i = 0; i < RES_NAME_SIZE; i++)
516                 hash += name.name[i];
517
518         hash += (__u32)((unsigned long)parent >> 4);
519
520         return (hash & RES_HASH_MASK);
521 }
522
523 static struct ldlm_resource *ldlm_resource_new(void)
524 {
525         struct ldlm_resource *res;
526
527         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
528         if (res == NULL)
529                 return NULL;
530
531         memset(res, 0, sizeof(*res));
532
533         CFS_INIT_LIST_HEAD(&res->lr_children);
534         CFS_INIT_LIST_HEAD(&res->lr_childof);
535         CFS_INIT_LIST_HEAD(&res->lr_granted);
536         CFS_INIT_LIST_HEAD(&res->lr_converting);
537         CFS_INIT_LIST_HEAD(&res->lr_waiting);
538         atomic_set(&res->lr_refcount, 1);
539         spin_lock_init(&res->lr_lock);
540
541         /* one who creates the resource must unlock
542          * the semaphore after lvb initialization */
543         init_MUTEX_LOCKED(&res->lr_lvb_sem);
544
545         return res;
546 }
547
548 /* must be called with hash lock held */
549 static struct ldlm_resource *
550 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
551 {
552         struct list_head *bucket, *tmp;
553         struct ldlm_resource *res;
554
555         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
556         bucket = ns->ns_hash + hash;
557
558         list_for_each(tmp, bucket) {
559                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
560                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
561                         return res;
562         }
563
564         return NULL;
565 }
566
567 /* Args: locked namespace
568  * Returns: newly-allocated, referenced, unlocked resource */
569 static struct ldlm_resource *
570 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
571                   struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
572 {
573         struct list_head *bucket;
574         struct ldlm_resource *res, *old_res;
575         ENTRY;
576
577         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
578                  "type: %d\n", type);
579
580         res = ldlm_resource_new();
581         if (!res)
582                 RETURN(NULL);
583
584         res->lr_name = name;
585         res->lr_namespace = ns;
586         res->lr_type = type;
587         res->lr_most_restr = LCK_NL;
588
589         spin_lock(&ns->ns_hash_lock);
590         old_res = ldlm_resource_find(ns, name, hash);
591         if (old_res) {
592                 /* someone won the race and added the resource before */
593                 ldlm_resource_getref(old_res);
594                 spin_unlock(&ns->ns_hash_lock);
595                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
596                 /* synchronize WRT resource creation */
597                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
598                         down(&old_res->lr_lvb_sem);
599                         up(&old_res->lr_lvb_sem);
600                 }
601                 RETURN(old_res);
602         }
603
604         /* we won! let's add the resource */
605         bucket = ns->ns_hash + hash;
606         list_add(&res->lr_hash, bucket);
607         ns->ns_resources++;
608         ns->ns_refcount++;
609
610         if (parent == NULL) {
611                 list_add(&res->lr_childof, &ns->ns_root_list);
612         } else {
613                 res->lr_parent = parent;
614                 list_add(&res->lr_childof, &parent->lr_children);
615         }
616         spin_unlock(&ns->ns_hash_lock);
617
618         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
619                 int rc;
620
621                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
622                 rc = ns->ns_lvbo->lvbo_init(res);
623                 if (rc)
624                         CERROR("lvbo_init failed for resource "
625                                LPU64": rc %d\n", name.name[0], rc);
626                 /* we create resource with locked lr_lvb_sem */
627                 up(&res->lr_lvb_sem);
628         }
629
630         RETURN(res);
631 }
632
633 /* Args: unlocked namespace
634  * Locks: takes and releases ns->ns_lock and res->lr_lock
635  * Returns: referenced, unlocked ldlm_resource or NULL */
636 struct ldlm_resource *
637 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
638                   struct ldlm_res_id name, ldlm_type_t type, int create)
639 {
640         __u32 hash = ldlm_hash_fn(parent, name);
641         struct ldlm_resource *res = NULL;
642         ENTRY;
643
644         LASSERT(ns != NULL);
645         LASSERT(ns->ns_hash != NULL);
646         LASSERT(name.name[0] != 0);
647
648         spin_lock(&ns->ns_hash_lock);
649         res = ldlm_resource_find(ns, name, hash);
650         if (res) {
651                 ldlm_resource_getref(res);
652                 spin_unlock(&ns->ns_hash_lock);
653                 /* synchronize WRT resource creation */
654                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
655                         down(&res->lr_lvb_sem);
656                         up(&res->lr_lvb_sem);
657                 }
658                 RETURN(res);
659         }
660         spin_unlock(&ns->ns_hash_lock);
661
662         if (create == 0)
663                 RETURN(NULL);
664
665         res = ldlm_resource_add(ns, parent, name, hash, type);
666         RETURN(res);
667 }
668
669 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
670 {
671         LASSERT(res != NULL);
672         LASSERT(res != LP_POISON);
673         atomic_inc(&res->lr_refcount);
674         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
675                atomic_read(&res->lr_refcount));
676         return res;
677 }
678
679 void __ldlm_resource_putref_final(struct ldlm_resource *res)
680 {
681         struct ldlm_namespace *ns = res->lr_namespace;
682
683         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
684
685         if (!list_empty(&res->lr_granted)) {
686                 ldlm_resource_dump(D_ERROR, res);
687                 LBUG();
688         }
689
690         if (!list_empty(&res->lr_converting)) {
691                 ldlm_resource_dump(D_ERROR, res);
692                 LBUG();
693         }
694
695         if (!list_empty(&res->lr_waiting)) {
696                 ldlm_resource_dump(D_ERROR, res);
697                 LBUG();
698         }
699
700         if (!list_empty(&res->lr_children)) {
701                 ldlm_resource_dump(D_ERROR, res);
702                 LBUG();
703         }
704
705         ns->ns_refcount--;
706         list_del_init(&res->lr_hash);
707         list_del_init(&res->lr_childof);
708
709         ns->ns_resources--;
710         if (ns->ns_resources == 0)
711                 wake_up(&ns->ns_waitq);
712 }
713
714 /* Returns 1 if the resource was freed, 0 if it remains. */
715 int ldlm_resource_putref(struct ldlm_resource *res)
716 {
717         struct ldlm_namespace *ns = res->lr_namespace;
718         int rc = 0;
719         ENTRY;
720
721         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
722                atomic_read(&res->lr_refcount) - 1);
723         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
724                  atomic_read(&res->lr_refcount));
725         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
726                  atomic_read(&res->lr_refcount));
727
728         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
729                 __ldlm_resource_putref_final(res);
730                 spin_unlock(&ns->ns_hash_lock);
731                 if (res->lr_lvb_data)
732                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
733                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
734                 rc = 1;
735         }
736
737         RETURN(rc);
738 }
739
740 /* Returns 1 if the resource was freed, 0 if it remains. */
741 int ldlm_resource_putref_locked(struct ldlm_resource *res)
742 {
743         int rc = 0;
744         ENTRY;
745
746         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
747                atomic_read(&res->lr_refcount) - 1);
748         LASSERT(atomic_read(&res->lr_refcount) > 0);
749         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
750
751         LASSERT(atomic_read(&res->lr_refcount) >= 0);
752         if (atomic_dec_and_test(&res->lr_refcount)) {
753                 __ldlm_resource_putref_final(res);
754                 if (res->lr_lvb_data)
755                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
756                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
757                 rc = 1;
758         }
759
760         RETURN(rc);
761 }
762
763 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
764                             struct ldlm_lock *lock)
765 {
766         check_res_locked(res);
767
768         ldlm_resource_dump(D_OTHER, res);
769         CDEBUG(D_OTHER, "About to add this lock:\n");
770         ldlm_lock_dump(D_OTHER, lock, 0);
771
772         if (lock->l_destroyed) {
773                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
774                 return;
775         }
776
777         LASSERT(list_empty(&lock->l_res_link));
778
779         list_add_tail(&lock->l_res_link, head);
780 }
781
782 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
783                                      struct ldlm_lock *new)
784 {
785         struct ldlm_resource *res = original->l_resource;
786
787         check_res_locked(res);
788
789         ldlm_resource_dump(D_OTHER, res);
790         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
791         ldlm_lock_dump(D_OTHER, new, 0);
792
793         if (new->l_destroyed) {
794                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
795                 goto out;
796         }
797
798         LASSERT(list_empty(&new->l_res_link));
799
800         list_add(&new->l_res_link, &original->l_res_link);
801  out:;
802 }
803
804 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
805 {
806         check_res_locked(lock->l_resource);
807         ldlm_unlink_lock_skiplist(lock);
808         list_del_init(&lock->l_res_link);
809 }
810
811 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
812 {
813         desc->lr_type = res->lr_type;
814         desc->lr_name = res->lr_name;
815 }
816
817 void ldlm_dump_all_namespaces(int level)
818 {
819         struct list_head *tmp;
820
821         if (!((libcfs_debug | D_ERROR) & level))
822                 return;
823
824         mutex_down(&ldlm_namespace_lock);
825
826         list_for_each(tmp, &ldlm_namespace_list) {
827                 struct ldlm_namespace *ns;
828                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
829                 ldlm_namespace_dump(level, ns);
830         }
831
832         mutex_up(&ldlm_namespace_lock);
833 }
834
835 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
836 {
837         struct list_head *tmp;
838
839         if (!((libcfs_debug | D_ERROR) & level))
840                 return;
841
842         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n", 
843                ns->ns_name, ns->ns_refcount, ns->ns_client);
844
845         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
846                 return;
847
848         spin_lock(&ns->ns_hash_lock);
849         tmp = ns->ns_root_list.next;
850         while (tmp != &ns->ns_root_list) {
851                 struct ldlm_resource *res;
852                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
853
854                 ldlm_resource_getref(res);
855                 spin_unlock(&ns->ns_hash_lock);
856
857                 lock_res(res);
858                 ldlm_resource_dump(level, res);
859                 unlock_res(res);
860                 
861                 spin_lock(&ns->ns_hash_lock);
862                 tmp = tmp->next;
863                 ldlm_resource_putref_locked(res);
864         }
865         ns->ns_next_dump = cfs_time_shift(10);
866         spin_unlock(&ns->ns_hash_lock);
867 }
868
869 void ldlm_resource_dump(int level, struct ldlm_resource *res)
870 {
871         struct list_head *tmp;
872         int pos;
873
874         CLASSERT(RES_NAME_SIZE == 4);
875
876         if (!((libcfs_debug | D_ERROR) & level))
877                 return;
878
879         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
880                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
881                res->lr_name.name[2], res->lr_name.name[3],
882                atomic_read(&res->lr_refcount));
883
884         if (!list_empty(&res->lr_granted)) {
885                 pos = 0;
886                 CDEBUG(level, "Granted locks:\n");
887                 list_for_each(tmp, &res->lr_granted) {
888                         struct ldlm_lock *lock;
889                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
890                         ldlm_lock_dump(level, lock, ++pos);
891                 }
892         }
893         if (!list_empty(&res->lr_converting)) {
894                 pos = 0;
895                 CDEBUG(level, "Converting locks:\n");
896                 list_for_each(tmp, &res->lr_converting) {
897                         struct ldlm_lock *lock;
898                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
899                         ldlm_lock_dump(level, lock, ++pos);
900                 }
901         }
902         if (!list_empty(&res->lr_waiting)) {
903                 pos = 0;
904                 CDEBUG(level, "Waiting locks:\n");
905                 list_for_each(tmp, &res->lr_waiting) {
906                         struct ldlm_lock *lock;
907                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
908                         ldlm_lock_dump(level, lock, ++pos);
909                 }
910         }
911 }