Whamcloud - gitweb
Land b1_6_elc onto b1_6 (20070621_0218)
[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 /* Cleanup, but also free, the namespace */
423 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
424 {
425         ENTRY;
426         if (!ns)
427                 RETURN(ELDLM_OK);
428
429         mutex_down(&ldlm_namespace_lock);
430         list_del(&ns->ns_list_chain);
431         mutex_up(&ldlm_namespace_lock);
432
433         /* At shutdown time, don't call the cancellation callback */
434         ldlm_namespace_cleanup(ns, 0);
435
436 #ifdef LPROCFS
437         {
438                 struct proc_dir_entry *dir;
439                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
440                 if (dir == NULL) {
441                         CERROR("dlm namespace %s has no procfs dir?\n",
442                                ns->ns_name);
443                 } else {
444                         lprocfs_remove(&dir);
445                 }
446         }
447 #endif
448
449         if (ns->ns_refcount > 0) {
450                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
451                 int rc;
452                 CDEBUG(D_DLMTRACE,
453                        "dlm namespace %s free waiting on refcount %d\n",
454                        ns->ns_name, ns->ns_refcount);
455                 rc = l_wait_event(ns->ns_waitq,
456                                   ns->ns_refcount == 0, &lwi);
457                 if (ns->ns_refcount)
458                         LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
459                                            "namespace cleanup aborted with %d "
460                                            "resources in use. (%d)\nI'm going "
461                                            "to try to clean up anyway, but I "
462                                            "might need a reboot of this node.\n",
463                                             ns->ns_name, (int) ns->ns_refcount, 
464                                             rc);
465                 CDEBUG(D_DLMTRACE,
466                        "dlm namespace %s free done waiting\n", ns->ns_name);
467         }
468
469         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
470         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
471         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
472         OBD_FREE(ns, sizeof(*ns));
473
474         ldlm_put_ref(force);
475
476         return ELDLM_OK;
477 }
478
479 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
480 {
481         __u32 hash = 0;
482         int i;
483
484         for (i = 0; i < RES_NAME_SIZE; i++)
485                 hash += name.name[i];
486
487         hash += (__u32)((unsigned long)parent >> 4);
488
489         return (hash & RES_HASH_MASK);
490 }
491
492 static struct ldlm_resource *ldlm_resource_new(void)
493 {
494         struct ldlm_resource *res;
495
496         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
497         if (res == NULL)
498                 return NULL;
499
500         memset(res, 0, sizeof(*res));
501
502         CFS_INIT_LIST_HEAD(&res->lr_children);
503         CFS_INIT_LIST_HEAD(&res->lr_childof);
504         CFS_INIT_LIST_HEAD(&res->lr_granted);
505         CFS_INIT_LIST_HEAD(&res->lr_converting);
506         CFS_INIT_LIST_HEAD(&res->lr_waiting);
507         atomic_set(&res->lr_refcount, 1);
508         spin_lock_init(&res->lr_lock);
509
510         /* one who creates the resource must unlock
511          * the semaphore after lvb initialization */
512         init_MUTEX_LOCKED(&res->lr_lvb_sem);
513
514         return res;
515 }
516
517 /* must be called with hash lock held */
518 static struct ldlm_resource *
519 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
520 {
521         struct list_head *bucket, *tmp;
522         struct ldlm_resource *res;
523
524         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
525         bucket = ns->ns_hash + hash;
526
527         list_for_each(tmp, bucket) {
528                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
529                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
530                         return res;
531         }
532
533         return NULL;
534 }
535
536 /* Args: locked namespace
537  * Returns: newly-allocated, referenced, unlocked resource */
538 static struct ldlm_resource *
539 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
540                   struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
541 {
542         struct list_head *bucket;
543         struct ldlm_resource *res, *old_res;
544         ENTRY;
545
546         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
547                  "type: %d\n", type);
548
549         res = ldlm_resource_new();
550         if (!res)
551                 RETURN(NULL);
552
553         res->lr_name = name;
554         res->lr_namespace = ns;
555         res->lr_type = type;
556         res->lr_most_restr = LCK_NL;
557
558         spin_lock(&ns->ns_hash_lock);
559         old_res = ldlm_resource_find(ns, name, hash);
560         if (old_res) {
561                 /* someone won the race and added the resource before */
562                 ldlm_resource_getref(old_res);
563                 spin_unlock(&ns->ns_hash_lock);
564                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
565                 /* synchronize WRT resource creation */
566                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
567                         down(&old_res->lr_lvb_sem);
568                         up(&old_res->lr_lvb_sem);
569                 }
570                 RETURN(old_res);
571         }
572
573         /* we won! let's add the resource */
574         bucket = ns->ns_hash + hash;
575         list_add(&res->lr_hash, bucket);
576         ns->ns_resources++;
577         ns->ns_refcount++;
578
579         if (parent == NULL) {
580                 list_add(&res->lr_childof, &ns->ns_root_list);
581         } else {
582                 res->lr_parent = parent;
583                 list_add(&res->lr_childof, &parent->lr_children);
584         }
585         spin_unlock(&ns->ns_hash_lock);
586
587         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
588                 int rc;
589
590                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
591                 rc = ns->ns_lvbo->lvbo_init(res);
592                 if (rc)
593                         CERROR("lvbo_init failed for resource "
594                                LPU64": rc %d\n", name.name[0], rc);
595                 /* we create resource with locked lr_lvb_sem */
596                 up(&res->lr_lvb_sem);
597         }
598
599         RETURN(res);
600 }
601
602 /* Args: unlocked namespace
603  * Locks: takes and releases ns->ns_lock and res->lr_lock
604  * Returns: referenced, unlocked ldlm_resource or NULL */
605 struct ldlm_resource *
606 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
607                   struct ldlm_res_id name, ldlm_type_t type, int create)
608 {
609         __u32 hash = ldlm_hash_fn(parent, name);
610         struct ldlm_resource *res = NULL;
611         ENTRY;
612
613         LASSERT(ns != NULL);
614         LASSERT(ns->ns_hash != NULL);
615         LASSERT(name.name[0] != 0);
616
617         spin_lock(&ns->ns_hash_lock);
618         res = ldlm_resource_find(ns, name, hash);
619         if (res) {
620                 ldlm_resource_getref(res);
621                 spin_unlock(&ns->ns_hash_lock);
622                 /* synchronize WRT resource creation */
623                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
624                         down(&res->lr_lvb_sem);
625                         up(&res->lr_lvb_sem);
626                 }
627                 RETURN(res);
628         }
629         spin_unlock(&ns->ns_hash_lock);
630
631         if (create == 0)
632                 RETURN(NULL);
633
634         res = ldlm_resource_add(ns, parent, name, hash, type);
635         RETURN(res);
636 }
637
638 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
639 {
640         LASSERT(res != NULL);
641         LASSERT(res != LP_POISON);
642         atomic_inc(&res->lr_refcount);
643         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
644                atomic_read(&res->lr_refcount));
645         return res;
646 }
647
648 void __ldlm_resource_putref_final(struct ldlm_resource *res)
649 {
650         struct ldlm_namespace *ns = res->lr_namespace;
651
652         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
653
654         if (!list_empty(&res->lr_granted)) {
655                 ldlm_resource_dump(D_ERROR, res);
656                 LBUG();
657         }
658
659         if (!list_empty(&res->lr_converting)) {
660                 ldlm_resource_dump(D_ERROR, res);
661                 LBUG();
662         }
663
664         if (!list_empty(&res->lr_waiting)) {
665                 ldlm_resource_dump(D_ERROR, res);
666                 LBUG();
667         }
668
669         if (!list_empty(&res->lr_children)) {
670                 ldlm_resource_dump(D_ERROR, res);
671                 LBUG();
672         }
673
674         ns->ns_refcount--;
675         list_del_init(&res->lr_hash);
676         list_del_init(&res->lr_childof);
677
678         ns->ns_resources--;
679         if (ns->ns_resources == 0)
680                 wake_up(&ns->ns_waitq);
681 }
682
683 /* Returns 1 if the resource was freed, 0 if it remains. */
684 int ldlm_resource_putref(struct ldlm_resource *res)
685 {
686         struct ldlm_namespace *ns = res->lr_namespace;
687         int rc = 0;
688         ENTRY;
689
690         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
691                atomic_read(&res->lr_refcount) - 1);
692         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
693                  atomic_read(&res->lr_refcount));
694         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
695                  atomic_read(&res->lr_refcount));
696
697         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
698                 __ldlm_resource_putref_final(res);
699                 spin_unlock(&ns->ns_hash_lock);
700                 if (res->lr_lvb_data)
701                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
702                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
703                 rc = 1;
704         }
705
706         RETURN(rc);
707 }
708
709 /* Returns 1 if the resource was freed, 0 if it remains. */
710 int ldlm_resource_putref_locked(struct ldlm_resource *res)
711 {
712         int rc = 0;
713         ENTRY;
714
715         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
716                atomic_read(&res->lr_refcount) - 1);
717         LASSERT(atomic_read(&res->lr_refcount) > 0);
718         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
719
720         LASSERT(atomic_read(&res->lr_refcount) >= 0);
721         if (atomic_dec_and_test(&res->lr_refcount)) {
722                 __ldlm_resource_putref_final(res);
723                 if (res->lr_lvb_data)
724                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
725                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
726                 rc = 1;
727         }
728
729         RETURN(rc);
730 }
731
732 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
733                             struct ldlm_lock *lock)
734 {
735         check_res_locked(res);
736
737         ldlm_resource_dump(D_OTHER, res);
738         CDEBUG(D_OTHER, "About to add this lock:\n");
739         ldlm_lock_dump(D_OTHER, lock, 0);
740
741         if (lock->l_destroyed) {
742                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
743                 return;
744         }
745
746         LASSERT(list_empty(&lock->l_res_link));
747
748         list_add_tail(&lock->l_res_link, head);
749 }
750
751 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
752                                      struct ldlm_lock *new)
753 {
754         struct ldlm_resource *res = original->l_resource;
755
756         check_res_locked(res);
757
758         ldlm_resource_dump(D_OTHER, res);
759         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
760         ldlm_lock_dump(D_OTHER, new, 0);
761
762         if (new->l_destroyed) {
763                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
764                 goto out;
765         }
766
767         LASSERT(list_empty(&new->l_res_link));
768
769         list_add(&new->l_res_link, &original->l_res_link);
770  out:;
771 }
772
773 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
774 {
775         check_res_locked(lock->l_resource);
776         ldlm_unlink_lock_skiplist(lock);
777         list_del_init(&lock->l_res_link);
778 }
779
780 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
781 {
782         desc->lr_type = res->lr_type;
783         desc->lr_name = res->lr_name;
784 }
785
786 void ldlm_dump_all_namespaces(int level)
787 {
788         struct list_head *tmp;
789
790         if (!((libcfs_debug | D_ERROR) & level))
791                 return;
792
793         mutex_down(&ldlm_namespace_lock);
794
795         list_for_each(tmp, &ldlm_namespace_list) {
796                 struct ldlm_namespace *ns;
797                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
798                 ldlm_namespace_dump(level, ns);
799         }
800
801         mutex_up(&ldlm_namespace_lock);
802 }
803
804 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
805 {
806         struct list_head *tmp;
807
808         if (!((libcfs_debug | D_ERROR) & level))
809                 return;
810
811         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n", 
812                ns->ns_name, ns->ns_refcount, ns->ns_client);
813
814         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
815                 return;
816
817         spin_lock(&ns->ns_hash_lock);
818         tmp = ns->ns_root_list.next;
819         while (tmp != &ns->ns_root_list) {
820                 struct ldlm_resource *res;
821                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
822
823                 ldlm_resource_getref(res);
824                 spin_unlock(&ns->ns_hash_lock);
825
826                 lock_res(res);
827                 ldlm_resource_dump(level, res);
828                 unlock_res(res);
829                 
830                 spin_lock(&ns->ns_hash_lock);
831                 tmp = tmp->next;
832                 ldlm_resource_putref_locked(res);
833         }
834         ns->ns_next_dump = cfs_time_shift(10);
835         spin_unlock(&ns->ns_hash_lock);
836 }
837
838 void ldlm_resource_dump(int level, struct ldlm_resource *res)
839 {
840         struct list_head *tmp;
841         int pos;
842
843         CLASSERT(RES_NAME_SIZE == 4);
844
845         if (!((libcfs_debug | D_ERROR) & level))
846                 return;
847
848         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
849                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
850                res->lr_name.name[2], res->lr_name.name[3],
851                atomic_read(&res->lr_refcount));
852
853         if (!list_empty(&res->lr_granted)) {
854                 pos = 0;
855                 CDEBUG(level, "Granted locks:\n");
856                 list_for_each(tmp, &res->lr_granted) {
857                         struct ldlm_lock *lock;
858                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
859                         ldlm_lock_dump(level, lock, ++pos);
860                 }
861         }
862         if (!list_empty(&res->lr_converting)) {
863                 pos = 0;
864                 CDEBUG(level, "Converting locks:\n");
865                 list_for_each(tmp, &res->lr_converting) {
866                         struct ldlm_lock *lock;
867                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
868                         ldlm_lock_dump(level, lock, ++pos);
869                 }
870         }
871         if (!list_empty(&res->lr_waiting)) {
872                 pos = 0;
873                 CDEBUG(level, "Waiting locks:\n");
874                 list_for_each(tmp, &res->lr_waiting) {
875                         struct ldlm_lock *lock;
876                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
877                         ldlm_lock_dump(level, lock, ++pos);
878                 }
879         }
880 }