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