Whamcloud - gitweb
Branch HEAD
[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 snprintf(page, count, "%u\n", ns->ns_max_unused);
146 }
147
148 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
149                                   unsigned long count, void *data)
150 {
151         struct ldlm_namespace *ns = data;
152         char dummy[MAX_STRING_SIZE + 1], *end;
153         unsigned long tmp;
154
155         dummy[MAX_STRING_SIZE] = '\0';
156         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
157                 return -EFAULT;
158
159         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
160                 CDEBUG(D_DLMTRACE,
161                        "dropping all unused locks from namespace %s\n",
162                        ns->ns_name);
163                 tmp = ns->ns_max_unused;
164                 ns->ns_max_unused = 0;
165                 ldlm_cancel_lru(ns, LDLM_SYNC);
166                 ns->ns_max_unused = tmp;
167                 return count;
168         }
169
170         tmp = simple_strtoul(dummy, &end, 0);
171         if (tmp == 0 && *end) {
172                 CERROR("invalid value written\n");
173                 return -EINVAL;
174         }
175
176         CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
177                ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
178         ns->ns_max_unused = (unsigned int)tmp;
179
180         ldlm_cancel_lru(ns, LDLM_ASYNC);
181
182         return count;
183 }
184
185 void ldlm_proc_namespace(struct ldlm_namespace *ns)
186 {
187         struct lprocfs_vars lock_vars[2];
188         char lock_name[MAX_STRING_SIZE + 1];
189
190         LASSERT(ns != NULL);
191         LASSERT(ns->ns_name != NULL);
192
193         lock_name[MAX_STRING_SIZE] = '\0';
194
195         memset(lock_vars, 0, sizeof(lock_vars));
196         lock_vars[0].name = lock_name;
197
198         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
199         lock_vars[0].data = &ns->ns_refcount;
200         lock_vars[0].read_fptr = lprocfs_rd_atomic;
201         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
202
203         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
204         lock_vars[0].data = &ns->ns_locks;
205         lock_vars[0].read_fptr = lprocfs_rd_atomic;
206         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
207
208         if (ns->ns_client) {
209                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
210                          ns->ns_name);
211                 lock_vars[0].data = &ns->ns_nr_unused;
212                 lock_vars[0].read_fptr = lprocfs_uint_rd;
213                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
214
215                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
216                          ns->ns_name);
217                 lock_vars[0].data = ns;
218                 lock_vars[0].read_fptr = lprocfs_read_lru_size;
219                 lock_vars[0].write_fptr = lprocfs_write_lru_size;
220                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
221
222                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
223                          ns->ns_name);
224                 lock_vars[0].data = &ns->ns_max_age;
225                 lock_vars[0].read_fptr = lprocfs_uint_rd;
226                 lock_vars[0].write_fptr = lprocfs_uint_wr;
227                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
228
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,
480                           const struct ldlm_res_id *name)
481 {
482         __u32 hash = 0;
483         int i;
484
485         for (i = 0; i < RES_NAME_SIZE; i++)
486                 hash += name->name[i];
487
488         hash += (__u32)((unsigned long)parent >> 4);
489
490         return (hash & RES_HASH_MASK);
491 }
492
493 static struct ldlm_resource *ldlm_resource_new(void)
494 {
495         struct ldlm_resource *res;
496
497         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
498         if (res == NULL)
499                 return NULL;
500
501         memset(res, 0, sizeof(*res));
502
503         CFS_INIT_LIST_HEAD(&res->lr_children);
504         CFS_INIT_LIST_HEAD(&res->lr_childof);
505         CFS_INIT_LIST_HEAD(&res->lr_granted);
506         CFS_INIT_LIST_HEAD(&res->lr_converting);
507         CFS_INIT_LIST_HEAD(&res->lr_waiting);
508         atomic_set(&res->lr_refcount, 1);
509         spin_lock_init(&res->lr_lock);
510
511         /* one who creates the resource must unlock
512          * the semaphore after lvb initialization */
513         init_MUTEX_LOCKED(&res->lr_lvb_sem);
514
515         return res;
516 }
517
518 /* must be called with hash lock held */
519 static struct ldlm_resource *
520 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
521                    __u32 hash)
522 {
523         struct list_head *bucket, *tmp;
524         struct ldlm_resource *res;
525
526         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
527         bucket = ns->ns_hash + hash;
528
529         list_for_each(tmp, bucket) {
530                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
531                 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
532                         return res;
533         }
534
535         return NULL;
536 }
537
538 /* Args: locked namespace
539  * Returns: newly-allocated, referenced, unlocked resource */
540 static struct ldlm_resource *
541 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
542                   const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
543 {
544         struct list_head *bucket;
545         struct ldlm_resource *res, *old_res;
546         ENTRY;
547
548         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
549                  "type: %d\n", type);
550
551         res = ldlm_resource_new();
552         if (!res)
553                 RETURN(NULL);
554
555         res->lr_name = *name;
556         res->lr_namespace = ns;
557         res->lr_type = type;
558         res->lr_most_restr = LCK_NL;
559
560         spin_lock(&ns->ns_hash_lock);
561         old_res = ldlm_resource_find(ns, name, hash);
562         if (old_res) {
563                 /* someone won the race and added the resource before */
564                 ldlm_resource_getref(old_res);
565                 spin_unlock(&ns->ns_hash_lock);
566                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
567                 /* synchronize WRT resource creation */
568                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
569                         down(&old_res->lr_lvb_sem);
570                         up(&old_res->lr_lvb_sem);
571                 }
572                 RETURN(old_res);
573         }
574
575         /* we won! let's add the resource */
576         bucket = ns->ns_hash + hash;
577         list_add(&res->lr_hash, bucket);
578         ns->ns_resources++;
579         ns->ns_refcount++;
580
581         if (parent == NULL) {
582                 list_add(&res->lr_childof, &ns->ns_root_list);
583         } else {
584                 res->lr_parent = parent;
585                 list_add(&res->lr_childof, &parent->lr_children);
586         }
587         spin_unlock(&ns->ns_hash_lock);
588
589         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
590                 int rc;
591
592                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
593                 rc = ns->ns_lvbo->lvbo_init(res);
594                 if (rc)
595                         CERROR("lvbo_init failed for resource "
596                                LPU64": rc %d\n", name->name[0], rc);
597                 /* we create resource with locked lr_lvb_sem */
598                 up(&res->lr_lvb_sem);
599         }
600
601         RETURN(res);
602 }
603
604 /* Args: unlocked namespace
605  * Locks: takes and releases ns->ns_lock and res->lr_lock
606  * Returns: referenced, unlocked ldlm_resource or NULL */
607 struct ldlm_resource *
608 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
609                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
610 {
611         __u32 hash = ldlm_hash_fn(parent, name);
612         struct ldlm_resource *res = NULL;
613         ENTRY;
614
615         LASSERT(ns != NULL);
616         LASSERT(ns->ns_hash != NULL);
617         LASSERT(name->name[0] != 0);
618
619         spin_lock(&ns->ns_hash_lock);
620         res = ldlm_resource_find(ns, name, hash);
621         if (res) {
622                 ldlm_resource_getref(res);
623                 spin_unlock(&ns->ns_hash_lock);
624                 /* synchronize WRT resource creation */
625                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
626                         down(&res->lr_lvb_sem);
627                         up(&res->lr_lvb_sem);
628                 }
629                 RETURN(res);
630         }
631         spin_unlock(&ns->ns_hash_lock);
632
633         if (create == 0)
634                 RETURN(NULL);
635
636         res = ldlm_resource_add(ns, parent, name, hash, type);
637         RETURN(res);
638 }
639
640 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
641 {
642         LASSERT(res != NULL);
643         LASSERT(res != LP_POISON);
644         atomic_inc(&res->lr_refcount);
645         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
646                atomic_read(&res->lr_refcount));
647         return res;
648 }
649
650 void __ldlm_resource_putref_final(struct ldlm_resource *res)
651 {
652         struct ldlm_namespace *ns = res->lr_namespace;
653
654         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
655
656         if (!list_empty(&res->lr_granted)) {
657                 ldlm_resource_dump(D_ERROR, res);
658                 LBUG();
659         }
660
661         if (!list_empty(&res->lr_converting)) {
662                 ldlm_resource_dump(D_ERROR, res);
663                 LBUG();
664         }
665
666         if (!list_empty(&res->lr_waiting)) {
667                 ldlm_resource_dump(D_ERROR, res);
668                 LBUG();
669         }
670
671         if (!list_empty(&res->lr_children)) {
672                 ldlm_resource_dump(D_ERROR, res);
673                 LBUG();
674         }
675
676         ns->ns_refcount--;
677         list_del_init(&res->lr_hash);
678         list_del_init(&res->lr_childof);
679
680         ns->ns_resources--;
681         if (ns->ns_resources == 0)
682                 wake_up(&ns->ns_waitq);
683 }
684
685 /* Returns 1 if the resource was freed, 0 if it remains. */
686 int ldlm_resource_putref(struct ldlm_resource *res)
687 {
688         struct ldlm_namespace *ns = res->lr_namespace;
689         int rc = 0;
690         ENTRY;
691
692         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
693                atomic_read(&res->lr_refcount) - 1);
694         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
695                  atomic_read(&res->lr_refcount));
696         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
697                  atomic_read(&res->lr_refcount));
698
699         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
700                 __ldlm_resource_putref_final(res);
701                 spin_unlock(&ns->ns_hash_lock);
702                 if (res->lr_lvb_data)
703                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
704                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
705                 rc = 1;
706         }
707
708         RETURN(rc);
709 }
710
711 /* Returns 1 if the resource was freed, 0 if it remains. */
712 int ldlm_resource_putref_locked(struct ldlm_resource *res)
713 {
714         int rc = 0;
715         ENTRY;
716
717         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
718                atomic_read(&res->lr_refcount) - 1);
719         LASSERT(atomic_read(&res->lr_refcount) > 0);
720         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
721
722         LASSERT(atomic_read(&res->lr_refcount) >= 0);
723         if (atomic_dec_and_test(&res->lr_refcount)) {
724                 __ldlm_resource_putref_final(res);
725                 if (res->lr_lvb_data)
726                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
727                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
728                 rc = 1;
729         }
730
731         RETURN(rc);
732 }
733
734 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
735                             struct ldlm_lock *lock)
736 {
737         check_res_locked(res);
738
739         ldlm_resource_dump(D_OTHER, res);
740         CDEBUG(D_OTHER, "About to add this lock:\n");
741         ldlm_lock_dump(D_OTHER, lock, 0);
742
743         if (lock->l_destroyed) {
744                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
745                 return;
746         }
747
748         LASSERT(list_empty(&lock->l_res_link));
749
750         list_add_tail(&lock->l_res_link, head);
751 }
752
753 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
754                                      struct ldlm_lock *new)
755 {
756         struct ldlm_resource *res = original->l_resource;
757
758         check_res_locked(res);
759
760         ldlm_resource_dump(D_OTHER, res);
761         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
762         ldlm_lock_dump(D_OTHER, new, 0);
763
764         if (new->l_destroyed) {
765                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
766                 goto out;
767         }
768
769         LASSERT(list_empty(&new->l_res_link));
770
771         list_add(&new->l_res_link, &original->l_res_link);
772  out:;
773 }
774
775 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
776 {
777         check_res_locked(lock->l_resource);
778         ldlm_unlink_lock_skiplist(lock);
779         list_del_init(&lock->l_res_link);
780 }
781
782 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
783 {
784         desc->lr_type = res->lr_type;
785         desc->lr_name = res->lr_name;
786 }
787
788 void ldlm_dump_all_namespaces(int level)
789 {
790         struct list_head *tmp;
791
792         if (!((libcfs_debug | D_ERROR) & level))
793                 return;
794
795         mutex_down(&ldlm_namespace_lock);
796
797         list_for_each(tmp, &ldlm_namespace_list) {
798                 struct ldlm_namespace *ns;
799                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
800                 ldlm_namespace_dump(level, ns);
801         }
802
803         mutex_up(&ldlm_namespace_lock);
804 }
805
806 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
807 {
808         struct list_head *tmp;
809
810         if (!((libcfs_debug | D_ERROR) & level))
811                 return;
812
813         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n",
814                   ns->ns_name, ns->ns_refcount, ns->ns_client);
815
816         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
817                 return;
818
819         spin_lock(&ns->ns_hash_lock);
820         tmp = ns->ns_root_list.next;
821         while (tmp != &ns->ns_root_list) {
822                 struct ldlm_resource *res;
823                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
824
825                 ldlm_resource_getref(res);
826                 spin_unlock(&ns->ns_hash_lock);
827
828                 lock_res(res);
829                 ldlm_resource_dump(level, res);
830                 unlock_res(res);
831
832                 spin_lock(&ns->ns_hash_lock);
833                 tmp = tmp->next;
834                 ldlm_resource_putref_locked(res);
835         }
836         ns->ns_next_dump = cfs_time_shift(10);
837         spin_unlock(&ns->ns_hash_lock);
838 }
839
840 void ldlm_resource_dump(int level, struct ldlm_resource *res)
841 {
842         struct list_head *tmp;
843         int pos;
844
845         CLASSERT(RES_NAME_SIZE == 4);
846
847         if (!((libcfs_debug | D_ERROR) & level))
848                 return;
849
850         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
851                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
852                res->lr_name.name[2], res->lr_name.name[3],
853                atomic_read(&res->lr_refcount));
854
855         if (!list_empty(&res->lr_granted)) {
856                 pos = 0;
857                 CDEBUG(level, "Granted locks:\n");
858                 list_for_each(tmp, &res->lr_granted) {
859                         struct ldlm_lock *lock;
860                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
861                         ldlm_lock_dump(level, lock, ++pos);
862                 }
863         }
864         if (!list_empty(&res->lr_converting)) {
865                 pos = 0;
866                 CDEBUG(level, "Converting locks:\n");
867                 list_for_each(tmp, &res->lr_converting) {
868                         struct ldlm_lock *lock;
869                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
870                         ldlm_lock_dump(level, lock, ++pos);
871                 }
872         }
873         if (!list_empty(&res->lr_waiting)) {
874                 pos = 0;
875                 CDEBUG(level, "Waiting locks:\n");
876                 list_for_each(tmp, &res->lr_waiting) {
877                         struct ldlm_lock *lock;
878                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
879                         ldlm_lock_dump(level, lock, ++pos);
880                 }
881         }
882 }