Whamcloud - gitweb
- remove check for obd_configured from target_handle_connect(), this should be fixed...
[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_type_proc_dir = NULL;
99         ldlm_ns_proc_dir = NULL;
100         ldlm_svc_proc_dir = NULL;
101         RETURN(rc);
102 }
103
104 void ldlm_proc_cleanup(void)
105 {
106         if (ldlm_svc_proc_dir) {
107                 lprocfs_remove(ldlm_svc_proc_dir);
108                 ldlm_svc_proc_dir = NULL;
109         }
110
111         if (ldlm_ns_proc_dir) {
112                 lprocfs_remove(ldlm_ns_proc_dir);
113                 ldlm_ns_proc_dir = NULL;
114         }
115
116         if (ldlm_type_proc_dir) {
117                 lprocfs_remove(ldlm_type_proc_dir);
118                 ldlm_type_proc_dir = NULL;
119         }
120 }
121
122 static int lprocfs_uint_rd(char *page, char **start, off_t off,
123                            int count, int *eof, void *data)
124 {
125         unsigned int *temp = (unsigned int *)data;
126         return snprintf(page, count, "%u\n", *temp);
127 }
128
129 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
130                                  int count, int *eof, void *data)
131 {
132         struct ldlm_namespace *ns = data;
133         return lprocfs_uint_rd(page, start, off, count, eof,
134                                &ns->ns_max_unused);
135 }
136
137 #define MAX_STRING_SIZE 128
138 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
139                                   unsigned long count, void *data)
140 {
141         struct ldlm_namespace *ns = data;
142         char dummy[MAX_STRING_SIZE + 1], *end;
143         unsigned long tmp;
144
145         dummy[MAX_STRING_SIZE] = '\0';
146         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
147                 return -EFAULT;
148
149         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
150                 CDEBUG(D_DLMTRACE,
151                        "dropping all unused locks from namespace %s\n",
152                        ns->ns_name);
153                 tmp = ns->ns_max_unused;
154                 ns->ns_max_unused = 0;
155                 ldlm_cancel_lru(ns, LDLM_SYNC);
156                 ns->ns_max_unused = tmp;
157                 return count;
158         }
159
160         tmp = simple_strtoul(dummy, &end, 0);
161         if (tmp == 0 && *end) {
162                 CERROR("invalid value written\n");
163                 return -EINVAL;
164         }
165
166         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
167                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
168         ns->ns_max_unused = (unsigned int)tmp;
169
170         ldlm_cancel_lru(ns, LDLM_ASYNC);
171
172         return count;
173 }
174
175 void ldlm_proc_namespace(struct ldlm_namespace *ns)
176 {
177         struct lprocfs_vars lock_vars[2];
178         char lock_name[MAX_STRING_SIZE + 1];
179
180         LASSERT(ns != NULL);
181         LASSERT(ns->ns_name != NULL);
182
183         lock_name[MAX_STRING_SIZE] = '\0';
184
185         memset(lock_vars, 0, sizeof(lock_vars));
186         lock_vars[0].name = lock_name;
187
188         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
189         lock_vars[0].data = &ns->ns_refcount;
190         lock_vars[0].read_fptr = lprocfs_rd_atomic;
191         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
192
193         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
194         lock_vars[0].data = &ns->ns_locks;
195         lock_vars[0].read_fptr = lprocfs_rd_u64;
196         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
197
198         if (ns->ns_client) {
199                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
200                          ns->ns_name);
201                 lock_vars[0].data = &ns->ns_nr_unused;
202                 lock_vars[0].read_fptr = lprocfs_uint_rd;
203                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
204
205                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
206                          ns->ns_name);
207                 lock_vars[0].data = ns;
208                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
209                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
210                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
211         }
212 }
213 #undef MAX_STRING_SIZE
214 #else
215 #define ldlm_proc_namespace(ns) do {} while (0)
216 #endif /* LPROCFS */
217
218 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
219 {
220         struct ldlm_namespace *ns = NULL;
221         struct list_head *bucket;
222         int rc;
223         ENTRY;
224
225         rc = ldlm_get_ref();
226         if (rc) {
227                 CERROR("ldlm_get_ref failed: %d\n", rc);
228                 RETURN(NULL);
229         }
230
231         OBD_ALLOC(ns, sizeof(*ns));
232         if (!ns)
233                 GOTO(out_ref, NULL);
234
235         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
236         if (!ns->ns_hash)
237                 GOTO(out_ns, NULL);
238
239         OBD_ALLOC(ns->ns_name, strlen(name) + 1);
240         if (!ns->ns_name)
241                 GOTO(out_hash, NULL);
242
243         strcpy(ns->ns_name, name);
244
245         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
246         ns->ns_refcount = 0;
247         ns->ns_client = client;
248         spin_lock_init(&ns->ns_hash_lock);
249         atomic_set(&ns->ns_locks, 0);
250         ns->ns_resources = 0;
251         cfs_waitq_init(&ns->ns_waitq);
252
253         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
254              bucket--)
255                 CFS_INIT_LIST_HEAD(bucket);
256
257         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
258         ns->ns_nr_unused = 0;
259         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
260         spin_lock_init(&ns->ns_unused_lock);
261
262         mutex_down(&ldlm_namespace_lock);
263         list_add(&ns->ns_list_chain, &ldlm_namespace_list);
264         mutex_up(&ldlm_namespace_lock);
265         ldlm_proc_namespace(ns);
266         RETURN(ns);
267
268 out_hash:
269         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
270         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
271 out_ns:
272         OBD_FREE(ns, sizeof(*ns));
273 out_ref:
274         ldlm_put_ref(0);
275         RETURN(NULL);
276 }
277
278 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
279
280 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
281  * This is currently only used for recovery, and we make certain assumptions
282  * as a result--notably, that we shouldn't cancel locks with refs. -phil
283  *
284  * Called with the ns_lock held. */
285 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
286                              int flags)
287 {
288         struct list_head *tmp;
289         int rc = 0, client = res->lr_namespace->ns_client;
290         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
291         ENTRY;
292
293         
294         do {
295                 struct ldlm_lock *lock = NULL;
296  
297                 /* first, we look for non-cleaned-yet lock
298                  * all cleaned locks are marked by CLEANED flag */
299                 lock_res(res);
300                 list_for_each(tmp, q) {
301                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
302                         if (lock->l_flags & LDLM_FL_CLEANED) {
303                                 lock = NULL;
304                                 continue;
305                         }
306                         LDLM_LOCK_GET(lock);
307                         lock->l_flags |= LDLM_FL_CLEANED;
308                         break;
309                 }
310                 
311                 if (lock == NULL) {
312                         unlock_res(res);
313                         break;
314                 }
315
316                 /* Set CBPENDING so nothing in the cancellation path
317                  * can match this lock */
318                 lock->l_flags |= LDLM_FL_CBPENDING;
319                 lock->l_flags |= LDLM_FL_FAILED;
320                 lock->l_flags |= flags;
321
322                 if (local_only && (lock->l_readers || lock->l_writers)) {
323                         /* This is a little bit gross, but much better than the
324                          * alternative: pretend that we got a blocking AST from
325                          * the server, so that when the lock is decref'd, it
326                          * will go away ... */
327                         /* ... without sending a CANCEL message. */
328                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
329                         unlock_res(res);
330                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
331                         if (lock->l_completion_ast)
332                                 lock->l_completion_ast(lock, 0, NULL);
333                         LDLM_LOCK_PUT(lock);
334                         continue;
335                 }
336
337                 if (client) {
338                         struct lustre_handle lockh;
339
340                         unlock_res(res);
341                         ldlm_lock2handle(lock, &lockh);
342                         if (!local_only) {
343                                 rc = ldlm_cli_cancel(&lockh);
344                                 if (rc)
345                                         CERROR("ldlm_cli_cancel: %d\n", rc);
346                         }
347                         /* Force local cleanup on errors, too. */
348                         if (local_only || rc != ELDLM_OK)
349                                 ldlm_lock_cancel(lock);
350                 } else {
351                         ldlm_resource_unlink_lock(lock);
352                         unlock_res(res);
353                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
354                                    "client node");
355                         ldlm_lock_destroy(lock);
356                 }
357                 LDLM_LOCK_PUT(lock);
358         } while (1);
359
360         EXIT;
361 }
362
363 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
364 {
365         struct list_head *tmp;
366         int i;
367
368         if (ns == NULL) {
369                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
370                 return ELDLM_OK;
371         }
372
373         for (i = 0; i < RES_HASH_SIZE; i++) {
374                 spin_lock(&ns->ns_hash_lock);
375                 tmp = ns->ns_hash[i].next;
376                 while (tmp != &(ns->ns_hash[i])) {
377                         struct ldlm_resource *res;
378                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
379                         ldlm_resource_getref(res);
380                         spin_unlock(&ns->ns_hash_lock);
381
382                         cleanup_resource(res, &res->lr_granted, flags);
383                         cleanup_resource(res, &res->lr_converting, flags);
384                         cleanup_resource(res, &res->lr_waiting, flags);
385
386                         spin_lock(&ns->ns_hash_lock);
387                         tmp  = tmp->next;
388
389                         /* XXX: former stuff caused issues in case of race
390                          * between ldlm_namespace_cleanup() and lockd() when
391                          * client gets blocking ast when lock gets distracted by
392                          * server. This is 1_4 branch solution, let's see how
393                          * will it behave. */
394                         if (!ldlm_resource_putref_locked(res))
395                                 CDEBUG(D_INFO,
396                                        "Namespace %s resource refcount nonzero "
397                                        "(%d) after lock cleanup; forcing cleanup.\n",
398                                        ns->ns_name, atomic_read(&res->lr_refcount));
399                 }
400                 spin_unlock(&ns->ns_hash_lock);
401         }
402
403         return ELDLM_OK;
404 }
405
406 /* Cleanup, but also free, the namespace */
407 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
408 {
409         ENTRY;
410         if (!ns)
411                 RETURN(ELDLM_OK);
412
413         mutex_down(&ldlm_namespace_lock);
414         list_del(&ns->ns_list_chain);
415         mutex_up(&ldlm_namespace_lock);
416
417         /* At shutdown time, don't call the cancellation callback */
418         ldlm_namespace_cleanup(ns, 0);
419
420 #ifdef LPROCFS
421         {
422                 struct proc_dir_entry *dir;
423                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
424                 if (dir == NULL) {
425                         CERROR("dlm namespace %s has no procfs dir?\n",
426                                ns->ns_name);
427                 } else {
428                         lprocfs_remove(dir);
429                 }
430         }
431 #endif
432
433         if (ns->ns_refcount > 0) {
434                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
435                 int rc;
436                 CDEBUG(D_DLMTRACE,
437                        "dlm namespace %s free waiting on refcount %d\n",
438                        ns->ns_name, ns->ns_refcount);
439                 rc = l_wait_event(ns->ns_waitq,
440                                   ns->ns_refcount == 0, &lwi);
441                 if (ns->ns_refcount)
442                         LCONSOLE_ERROR("Lock manager: wait for %s namespace "
443                                        "cleanup aborted with %d resources in "
444                                        "use. (%d)\nI'm going to try to clean "
445                                        "up anyway, but I might need a reboot "
446                                        "of this node.\n", ns->ns_name,
447                                        (int) ns->ns_refcount, rc);
448                 CDEBUG(D_DLMTRACE,
449                        "dlm namespace %s free done waiting\n", ns->ns_name);
450         }
451
452         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
453         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
454         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
455         OBD_FREE(ns, sizeof(*ns));
456
457         ldlm_put_ref(force);
458
459         return ELDLM_OK;
460 }
461
462 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
463 {
464         __u32 hash = 0;
465         int i;
466
467         for (i = 0; i < RES_NAME_SIZE; i++)
468                 hash += name.name[i];
469
470         hash += (__u32)((unsigned long)parent >> 4);
471
472         return (hash & RES_HASH_MASK);
473 }
474
475 static struct ldlm_resource *ldlm_resource_new(void)
476 {
477         struct ldlm_resource *res;
478
479         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
480         if (res == NULL)
481                 return NULL;
482
483         memset(res, 0, sizeof(*res));
484
485         CFS_INIT_LIST_HEAD(&res->lr_children);
486         CFS_INIT_LIST_HEAD(&res->lr_childof);
487         CFS_INIT_LIST_HEAD(&res->lr_granted);
488         CFS_INIT_LIST_HEAD(&res->lr_converting);
489         CFS_INIT_LIST_HEAD(&res->lr_waiting);
490         atomic_set(&res->lr_refcount, 1);
491         spin_lock_init(&res->lr_lock);
492
493         /* one who creates the resource must unlock
494          * the semaphore after lvb initialization */
495         init_MUTEX_LOCKED(&res->lr_lvb_sem);
496
497         return res;
498 }
499
500 /* must be called with hash lock held */
501 static struct ldlm_resource *
502 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
503 {
504         struct list_head *bucket, *tmp;
505         struct ldlm_resource *res;
506
507         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
508         bucket = ns->ns_hash + hash;
509
510         list_for_each(tmp, bucket) {
511                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
512                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
513                         return res;
514         }
515
516         return NULL;
517 }
518
519 /* Args: locked namespace
520  * Returns: newly-allocated, referenced, unlocked resource */
521 static struct ldlm_resource *
522 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
523                   struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
524 {
525         struct list_head *bucket;
526         struct ldlm_resource *res, *old_res;
527         ENTRY;
528
529         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
530                  "type: %d\n", type);
531
532         res = ldlm_resource_new();
533         if (!res)
534                 RETURN(NULL);
535
536         res->lr_name = name;
537         res->lr_namespace = ns;
538         res->lr_type = type;
539         res->lr_most_restr = LCK_NL;
540
541         spin_lock(&ns->ns_hash_lock);
542         old_res = ldlm_resource_find(ns, name, hash);
543         if (old_res) {
544                 /* someone won the race and added the resource before */
545                 ldlm_resource_getref(old_res);
546                 spin_unlock(&ns->ns_hash_lock);
547                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
548                 /* synchronize WRT resource creation */
549                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
550                         down(&old_res->lr_lvb_sem);
551                         up(&old_res->lr_lvb_sem);
552                 }
553                 RETURN(old_res);
554         }
555
556         /* we won! let's add the resource */
557         bucket = ns->ns_hash + hash;
558         list_add(&res->lr_hash, bucket);
559         ns->ns_resources++;
560         ns->ns_refcount++;
561
562         if (parent == NULL) {
563                 list_add(&res->lr_childof, &ns->ns_root_list);
564         } else {
565                 res->lr_parent = parent;
566                 list_add(&res->lr_childof, &parent->lr_children);
567         }
568         spin_unlock(&ns->ns_hash_lock);
569
570         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
571                 int rc;
572
573                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
574                 rc = ns->ns_lvbo->lvbo_init(res);
575                 if (rc)
576                         CERROR("lvbo_init failed for resource "
577                                LPU64": rc %d\n", name.name[0], rc);
578                 /* we create resource with locked lr_lvb_sem */
579                 up(&res->lr_lvb_sem);
580         }
581
582         RETURN(res);
583 }
584
585 /* Args: unlocked namespace
586  * Locks: takes and releases ns->ns_lock and res->lr_lock
587  * Returns: referenced, unlocked ldlm_resource or NULL */
588 struct ldlm_resource *
589 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
590                   struct ldlm_res_id name, ldlm_type_t type, int create)
591 {
592         __u32 hash = ldlm_hash_fn(parent, name);
593         struct ldlm_resource *res = NULL;
594         ENTRY;
595
596         LASSERT(ns != NULL);
597         LASSERT(ns->ns_hash != NULL);
598         LASSERT(name.name[0] != 0);
599
600         spin_lock(&ns->ns_hash_lock);
601         res = ldlm_resource_find(ns, name, hash);
602         if (res) {
603                 ldlm_resource_getref(res);
604                 spin_unlock(&ns->ns_hash_lock);
605                 /* synchronize WRT resource creation */
606                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
607                         down(&res->lr_lvb_sem);
608                         up(&res->lr_lvb_sem);
609                 }
610                 RETURN(res);
611         }
612         spin_unlock(&ns->ns_hash_lock);
613
614         if (create == 0)
615                 RETURN(NULL);
616
617         res = ldlm_resource_add(ns, parent, name, hash, type);
618         RETURN(res);
619 }
620
621 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
622 {
623         LASSERT(res != NULL);
624         LASSERT(res != LP_POISON);
625         atomic_inc(&res->lr_refcount);
626         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
627                atomic_read(&res->lr_refcount));
628         return res;
629 }
630
631 void __ldlm_resource_putref_final(struct ldlm_resource *res)
632 {
633         struct ldlm_namespace *ns = res->lr_namespace;
634
635         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
636
637         if (!list_empty(&res->lr_granted)) {
638                 ldlm_resource_dump(D_ERROR, res);
639                 LBUG();
640         }
641
642         if (!list_empty(&res->lr_converting)) {
643                 ldlm_resource_dump(D_ERROR, res);
644                 LBUG();
645         }
646
647         if (!list_empty(&res->lr_waiting)) {
648                 ldlm_resource_dump(D_ERROR, res);
649                 LBUG();
650         }
651
652         if (!list_empty(&res->lr_children)) {
653                 ldlm_resource_dump(D_ERROR, res);
654                 LBUG();
655         }
656
657         ns->ns_refcount--;
658         list_del_init(&res->lr_hash);
659         list_del_init(&res->lr_childof);
660
661         ns->ns_resources--;
662         if (ns->ns_resources == 0)
663                 wake_up(&ns->ns_waitq);
664 }
665
666 /* Returns 1 if the resource was freed, 0 if it remains. */
667 int ldlm_resource_putref(struct ldlm_resource *res)
668 {
669         struct ldlm_namespace *ns = res->lr_namespace;
670         int rc = 0;
671         ENTRY;
672
673         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
674                atomic_read(&res->lr_refcount) - 1);
675         LASSERT(atomic_read(&res->lr_refcount) > 0);
676         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
677
678         LASSERT(atomic_read(&res->lr_refcount) >= 0);
679         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
680                 __ldlm_resource_putref_final(res);
681                 spin_unlock(&ns->ns_hash_lock);
682                 if (res->lr_lvb_data)
683                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
684                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
685                 rc = 1;
686         }
687
688         RETURN(rc);
689 }
690
691 /* Returns 1 if the resource was freed, 0 if it remains. */
692 int ldlm_resource_putref_locked(struct ldlm_resource *res)
693 {
694         int rc = 0;
695         ENTRY;
696
697         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
698                atomic_read(&res->lr_refcount) - 1);
699         LASSERT(atomic_read(&res->lr_refcount) > 0);
700         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
701
702         LASSERT(atomic_read(&res->lr_refcount) >= 0);
703         if (atomic_dec_and_test(&res->lr_refcount)) {
704                 __ldlm_resource_putref_final(res);
705                 if (res->lr_lvb_data)
706                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
707                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
708                 rc = 1;
709         }
710
711         RETURN(rc);
712 }
713
714 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
715                             struct ldlm_lock *lock)
716 {
717         check_res_locked(res);
718
719         ldlm_resource_dump(D_OTHER, res);
720         CDEBUG(D_OTHER, "About to add this lock:\n");
721         ldlm_lock_dump(D_OTHER, lock, 0);
722
723         if (lock->l_destroyed) {
724                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
725                 return;
726         }
727
728         LASSERT(list_empty(&lock->l_res_link));
729
730         list_add_tail(&lock->l_res_link, head);
731 }
732
733 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
734                                      struct ldlm_lock *new)
735 {
736         struct ldlm_resource *res = original->l_resource;
737
738         check_res_locked(res);
739
740         ldlm_resource_dump(D_OTHER, res);
741         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
742         ldlm_lock_dump(D_OTHER, new, 0);
743
744         if (new->l_destroyed) {
745                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
746                 goto out;
747         }
748
749         LASSERT(list_empty(&new->l_res_link));
750
751         list_add(&new->l_res_link, &original->l_res_link);
752  out:;
753 }
754
755 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
756 {
757         check_res_locked(lock->l_resource);
758         list_del_init(&lock->l_res_link);
759 }
760
761 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
762 {
763         desc->lr_type = res->lr_type;
764         desc->lr_name = res->lr_name;
765 }
766
767 void ldlm_dump_all_namespaces(int level)
768 {
769         struct list_head *tmp;
770
771         mutex_down(&ldlm_namespace_lock);
772
773         list_for_each(tmp, &ldlm_namespace_list) {
774                 struct ldlm_namespace *ns;
775                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
776                 ldlm_namespace_dump(level, ns);
777         }
778
779         mutex_up(&ldlm_namespace_lock);
780 }
781
782 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
783 {
784         struct list_head *tmp;
785
786         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n",
787                   ns->ns_name, ns->ns_refcount, ns->ns_client);
788
789         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
790                 return;
791
792         spin_lock(&ns->ns_hash_lock);
793         tmp = ns->ns_root_list.next;
794         while (tmp != &ns->ns_root_list) {
795                 struct ldlm_resource *res;
796                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
797
798                 ldlm_resource_getref(res);
799                 spin_unlock(&ns->ns_hash_lock);
800
801                 lock_res(res);
802                 ldlm_resource_dump(level, res);
803                 unlock_res(res);
804                 
805                 spin_lock(&ns->ns_hash_lock);
806                 tmp = tmp->next;
807                 ldlm_resource_putref_locked(res);
808         }
809         ns->ns_next_dump = cfs_time_shift(10);
810         spin_unlock(&ns->ns_hash_lock);
811 }
812
813 void ldlm_resource_dump(int level, struct ldlm_resource *res)
814 {
815         struct list_head *tmp;
816         int pos;
817
818         if (RES_NAME_SIZE != 4)
819                 LBUG();
820
821         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
822                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
823                res->lr_name.name[2], res->lr_name.name[3],
824                atomic_read(&res->lr_refcount));
825
826         if (!list_empty(&res->lr_granted)) {
827                 pos = 0;
828                 CDEBUG(level, "Granted locks:\n");
829                 list_for_each(tmp, &res->lr_granted) {
830                         struct ldlm_lock *lock;
831                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
832                         ldlm_lock_dump(level, lock, ++pos);
833                 }
834         }
835         if (!list_empty(&res->lr_converting)) {
836                 pos = 0;
837                 CDEBUG(level, "Converting locks:\n");
838                 list_for_each(tmp, &res->lr_converting) {
839                         struct ldlm_lock *lock;
840                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
841                         ldlm_lock_dump(level, lock, ++pos);
842                 }
843         }
844         if (!list_empty(&res->lr_waiting)) {
845                 pos = 0;
846                 CDEBUG(level, "Waiting locks:\n");
847                 list_for_each(tmp, &res->lr_waiting) {
848                         struct ldlm_lock *lock;
849                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
850                         ldlm_lock_dump(level, lock, ++pos);
851                 }
852         }
853 }