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 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, namelen;
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         namelen = strlen(name);
269         OBD_ALLOC(ns->ns_name, namelen + 1);
270         if (!ns->ns_name)
271                 GOTO(out_hash, NULL);
272
273         strcpy(ns->ns_name, name);
274
275         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
276         ns->ns_refcount = 0;
277         ns->ns_client = client;
278         spin_lock_init(&ns->ns_hash_lock);
279         atomic_set(&ns->ns_locks, 0);
280         ns->ns_resources = 0;
281         cfs_waitq_init(&ns->ns_waitq);
282
283         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
284              bucket--)
285                 CFS_INIT_LIST_HEAD(bucket);
286
287         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
288         ns->ns_nr_unused = 0;
289         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
290         ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
291         spin_lock_init(&ns->ns_unused_lock);
292         ns->ns_connect_flags = 0;
293         ldlm_proc_namespace(ns);
294
295         idx = atomic_read(ldlm_namespace_nr(client));
296         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
297         if (rc) {
298                 CERROR("Can't initialize lock pool, rc %d\n", rc);
299                 GOTO(out_proc, rc);
300         }
301
302         mutex_down(ldlm_namespace_lock(client));
303         list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
304         atomic_inc(ldlm_namespace_nr(client));
305         mutex_up(ldlm_namespace_lock(client));
306
307         RETURN(ns);
308 out_proc:
309         ldlm_namespace_cleanup(ns, 0);
310         OBD_FREE(ns->ns_name, namelen + 1);
311 out_hash:
312         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
313 out_ns:
314         OBD_FREE_PTR(ns);
315 out_ref:
316         ldlm_put_ref(client, 0);
317         RETURN(NULL);
318 }
319
320 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
321
322 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
323  * This is currently only used for recovery, and we make certain assumptions
324  * as a result--notably, that we shouldn't cancel locks with refs. -phil
325  *
326  * Called with the ns_lock held. */
327 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
328                              int flags)
329 {
330         struct list_head *tmp;
331         int rc = 0, client = res->lr_namespace->ns_client;
332         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
333         ENTRY;
334
335
336         do {
337                 struct ldlm_lock *lock = NULL;
338
339                 /* first, we look for non-cleaned-yet lock
340                  * all cleaned locks are marked by CLEANED flag */
341                 lock_res(res);
342                 list_for_each(tmp, q) {
343                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
344                         if (lock->l_flags & LDLM_FL_CLEANED) {
345                                 lock = NULL;
346                                 continue;
347                         }
348                         LDLM_LOCK_GET(lock);
349                         lock->l_flags |= LDLM_FL_CLEANED;
350                         break;
351                 }
352
353                 if (lock == NULL) {
354                         unlock_res(res);
355                         break;
356                 }
357
358                 /* Set CBPENDING so nothing in the cancellation path
359                  * can match this lock */
360                 lock->l_flags |= LDLM_FL_CBPENDING;
361                 lock->l_flags |= LDLM_FL_FAILED;
362                 lock->l_flags |= flags;
363
364                 /* ... without sending a CANCEL message for local_only. */
365                 if (local_only)
366                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
367
368                 if (local_only && (lock->l_readers || lock->l_writers)) {
369                         /* This is a little bit gross, but much better than the
370                          * alternative: pretend that we got a blocking AST from
371                          * the server, so that when the lock is decref'd, it
372                          * will go away ... */
373                         unlock_res(res);
374                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
375                         if (lock->l_completion_ast)
376                                 lock->l_completion_ast(lock, 0, NULL);
377                         LDLM_LOCK_PUT(lock);
378                         continue;
379                 }
380
381                 if (client) {
382                         struct lustre_handle lockh;
383
384                         unlock_res(res);
385                         ldlm_lock2handle(lock, &lockh);
386                         rc = ldlm_cli_cancel(&lockh);
387                         if (rc)
388                                 CERROR("ldlm_cli_cancel: %d\n", rc);
389                 } else {
390                         ldlm_resource_unlink_lock(lock);
391                         unlock_res(res);
392                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
393                                    "client node");
394                         ldlm_lock_destroy(lock);
395                 }
396                 LDLM_LOCK_PUT(lock);
397         } while (1);
398
399         EXIT;
400 }
401
402 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
403 {
404         struct list_head *tmp;
405         int i;
406
407         if (ns == NULL) {
408                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
409                 return ELDLM_OK;
410         }
411
412         for (i = 0; i < RES_HASH_SIZE; i++) {
413                 spin_lock(&ns->ns_hash_lock);
414                 tmp = ns->ns_hash[i].next;
415                 while (tmp != &(ns->ns_hash[i])) {
416                         struct ldlm_resource *res;
417                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
418                         ldlm_resource_getref(res);
419                         spin_unlock(&ns->ns_hash_lock);
420
421                         cleanup_resource(res, &res->lr_granted, flags);
422                         cleanup_resource(res, &res->lr_converting, flags);
423                         cleanup_resource(res, &res->lr_waiting, flags);
424
425                         spin_lock(&ns->ns_hash_lock);
426                         tmp  = tmp->next;
427
428                         /* XXX: former stuff caused issues in case of race
429                          * between ldlm_namespace_cleanup() and lockd() when
430                          * client gets blocking ast when lock gets distracted by
431                          * server. This is 1_4 branch solution, let's see how
432                          * will it behave. */
433                         if (!ldlm_resource_putref_locked(res))
434                                 CDEBUG(D_INFO,
435                                        "Namespace %s resource refcount nonzero "
436                                        "(%d) after lock cleanup; forcing cleanup.\n",
437                                        ns->ns_name, atomic_read(&res->lr_refcount));
438                 }
439                 spin_unlock(&ns->ns_hash_lock);
440         }
441
442         return ELDLM_OK;
443 }
444
445 /* Cleanup, but also free, the namespace */
446 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
447 {
448         ldlm_side_t client;
449         ENTRY;
450         if (!ns)
451                 RETURN(ELDLM_OK);
452
453         client = ns->ns_client;
454         mutex_down(ldlm_namespace_lock(client));
455         list_del(&ns->ns_list_chain);
456         atomic_dec(ldlm_namespace_nr(ns->ns_client));
457         ldlm_pool_fini(&ns->ns_pool);
458         mutex_up(ldlm_namespace_lock(client));
459
460         /* At shutdown time, don't call the cancellation callback */
461         ldlm_namespace_cleanup(ns, 0);
462
463 #ifdef LPROCFS
464         {
465                 struct proc_dir_entry *dir;
466                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
467                 if (dir == NULL) {
468                         CERROR("dlm namespace %s has no procfs dir?\n",
469                                ns->ns_name);
470                 } else {
471                         lprocfs_remove(&dir);
472                 }
473         }
474 #endif
475
476         if (ns->ns_refcount > 0) {
477                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
478                 int rc;
479                 CDEBUG(D_DLMTRACE,
480                        "dlm namespace %s free waiting on refcount %d\n",
481                        ns->ns_name, ns->ns_refcount);
482                 rc = l_wait_event(ns->ns_waitq,
483                                   ns->ns_refcount == 0, &lwi);
484                 if (ns->ns_refcount)
485                         LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
486                                            "namespace cleanup aborted with %d "
487                                            "resources in use. (%d)\nI'm going "
488                                            "to try to clean up anyway, but I "
489                                            "might need a reboot of this node.\n",
490                                             ns->ns_name, (int) ns->ns_refcount, 
491                                             rc);
492                 CDEBUG(D_DLMTRACE,
493                        "dlm namespace %s free done waiting\n", ns->ns_name);
494         }
495
496         POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
497         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
498         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
499         OBD_FREE_PTR(ns);
500
501         ldlm_put_ref(client, force);
502
503         RETURN(ELDLM_OK);
504 }
505
506 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
507 {
508         LASSERT(ns->ns_refcount >= 0);
509         ns->ns_refcount++;
510 }
511
512 void ldlm_namespace_get(struct ldlm_namespace *ns)
513 {
514         spin_lock(&ns->ns_hash_lock);
515         ldlm_namespace_get_nolock(ns);
516         spin_unlock(&ns->ns_hash_lock);
517 }
518
519 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
520 {
521         LASSERT(ns->ns_refcount > 0);
522         ns->ns_refcount--;
523         if (ns->ns_refcount == 0 && wakeup)
524                 wake_up(&ns->ns_waitq);
525 }
526
527 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
528 {
529         spin_lock(&ns->ns_hash_lock);
530         ldlm_namespace_put_nolock(ns, wakeup);
531         spin_unlock(&ns->ns_hash_lock);
532 }
533
534 /* Should be called under ldlm_namespace_lock(client) taken */
535 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
536 {
537         LASSERT(!list_empty(&ns->ns_list_chain));
538         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
539         list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
540 }
541
542 /* Should be called under ldlm_namespace_lock(client) taken */
543 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
544 {
545         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
546         LASSERT(!list_empty(ldlm_namespace_list(client)));
547         return container_of(ldlm_namespace_list(client)->next, 
548                 struct ldlm_namespace, ns_list_chain);
549 }
550 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
551                           const struct ldlm_res_id *name)
552 {
553         __u32 hash = 0;
554         int i;
555
556         for (i = 0; i < RES_NAME_SIZE; i++)
557                 hash += name->name[i];
558
559         hash += (__u32)((unsigned long)parent >> 4);
560
561         return (hash & RES_HASH_MASK);
562 }
563
564 static struct ldlm_resource *ldlm_resource_new(void)
565 {
566         struct ldlm_resource *res;
567
568         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
569         if (res == NULL)
570                 return NULL;
571
572         memset(res, 0, sizeof(*res));
573
574         CFS_INIT_LIST_HEAD(&res->lr_children);
575         CFS_INIT_LIST_HEAD(&res->lr_childof);
576         CFS_INIT_LIST_HEAD(&res->lr_granted);
577         CFS_INIT_LIST_HEAD(&res->lr_converting);
578         CFS_INIT_LIST_HEAD(&res->lr_waiting);
579         atomic_set(&res->lr_refcount, 1);
580         spin_lock_init(&res->lr_lock);
581
582         /* one who creates the resource must unlock
583          * the semaphore after lvb initialization */
584         init_MUTEX_LOCKED(&res->lr_lvb_sem);
585
586         return res;
587 }
588
589 /* must be called with hash lock held */
590 static struct ldlm_resource *
591 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
592                    __u32 hash)
593 {
594         struct list_head *bucket, *tmp;
595         struct ldlm_resource *res;
596
597         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
598         bucket = ns->ns_hash + hash;
599
600         list_for_each(tmp, bucket) {
601                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
602                 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
603                         return res;
604         }
605
606         return NULL;
607 }
608
609 /* Args: locked namespace
610  * Returns: newly-allocated, referenced, unlocked resource */
611 static struct ldlm_resource *
612 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
613                   const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
614 {
615         struct list_head *bucket;
616         struct ldlm_resource *res, *old_res;
617         ENTRY;
618
619         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
620                  "type: %d\n", type);
621
622         res = ldlm_resource_new();
623         if (!res)
624                 RETURN(NULL);
625
626         res->lr_name = *name;
627         res->lr_namespace = ns;
628         res->lr_type = type;
629         res->lr_most_restr = LCK_NL;
630
631         spin_lock(&ns->ns_hash_lock);
632         old_res = ldlm_resource_find(ns, name, hash);
633         if (old_res) {
634                 /* someone won the race and added the resource before */
635                 ldlm_resource_getref(old_res);
636                 spin_unlock(&ns->ns_hash_lock);
637                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
638                 /* synchronize WRT resource creation */
639                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
640                         down(&old_res->lr_lvb_sem);
641                         up(&old_res->lr_lvb_sem);
642                 }
643                 RETURN(old_res);
644         }
645
646         /* we won! let's add the resource */
647         bucket = ns->ns_hash + hash;
648         list_add(&res->lr_hash, bucket);
649         ns->ns_resources++;
650         ldlm_namespace_get_nolock(ns);
651
652         if (parent == NULL) {
653                 list_add(&res->lr_childof, &ns->ns_root_list);
654         } else {
655                 res->lr_parent = parent;
656                 list_add(&res->lr_childof, &parent->lr_children);
657         }
658         spin_unlock(&ns->ns_hash_lock);
659
660         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
661                 int rc;
662
663                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
664                 rc = ns->ns_lvbo->lvbo_init(res);
665                 if (rc)
666                         CERROR("lvbo_init failed for resource "
667                                LPU64": rc %d\n", name->name[0], rc);
668                 /* we create resource with locked lr_lvb_sem */
669                 up(&res->lr_lvb_sem);
670         }
671
672         RETURN(res);
673 }
674
675 /* Args: unlocked namespace
676  * Locks: takes and releases ns->ns_lock and res->lr_lock
677  * Returns: referenced, unlocked ldlm_resource or NULL */
678 struct ldlm_resource *
679 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
680                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
681 {
682         __u32 hash = ldlm_hash_fn(parent, name);
683         struct ldlm_resource *res = NULL;
684         ENTRY;
685
686         LASSERT(ns != NULL);
687         LASSERT(ns->ns_hash != NULL);
688         LASSERT(name->name[0] != 0);
689
690         spin_lock(&ns->ns_hash_lock);
691         res = ldlm_resource_find(ns, name, hash);
692         if (res) {
693                 ldlm_resource_getref(res);
694                 spin_unlock(&ns->ns_hash_lock);
695                 /* synchronize WRT resource creation */
696                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
697                         down(&res->lr_lvb_sem);
698                         up(&res->lr_lvb_sem);
699                 }
700                 RETURN(res);
701         }
702         spin_unlock(&ns->ns_hash_lock);
703
704         if (create == 0)
705                 RETURN(NULL);
706
707         res = ldlm_resource_add(ns, parent, name, hash, type);
708         RETURN(res);
709 }
710
711 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
712 {
713         LASSERT(res != NULL);
714         LASSERT(res != LP_POISON);
715         atomic_inc(&res->lr_refcount);
716         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
717                atomic_read(&res->lr_refcount));
718         return res;
719 }
720
721 void __ldlm_resource_putref_final(struct ldlm_resource *res)
722 {
723         struct ldlm_namespace *ns = res->lr_namespace;
724
725         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
726
727         if (!list_empty(&res->lr_granted)) {
728                 ldlm_resource_dump(D_ERROR, res);
729                 LBUG();
730         }
731
732         if (!list_empty(&res->lr_converting)) {
733                 ldlm_resource_dump(D_ERROR, res);
734                 LBUG();
735         }
736
737         if (!list_empty(&res->lr_waiting)) {
738                 ldlm_resource_dump(D_ERROR, res);
739                 LBUG();
740         }
741
742         if (!list_empty(&res->lr_children)) {
743                 ldlm_resource_dump(D_ERROR, res);
744                 LBUG();
745         }
746
747         /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few 
748          * lines below when all children are freed. */
749         ldlm_namespace_put_nolock(ns, 0);
750         list_del_init(&res->lr_hash);
751         list_del_init(&res->lr_childof);
752
753         ns->ns_resources--;
754         if (ns->ns_resources == 0)
755                 wake_up(&ns->ns_waitq);
756 }
757
758 /* Returns 1 if the resource was freed, 0 if it remains. */
759 int ldlm_resource_putref(struct ldlm_resource *res)
760 {
761         struct ldlm_namespace *ns = res->lr_namespace;
762         int rc = 0;
763         ENTRY;
764
765         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
766                atomic_read(&res->lr_refcount) - 1);
767         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
768                  atomic_read(&res->lr_refcount));
769         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
770                  atomic_read(&res->lr_refcount));
771
772         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
773                 __ldlm_resource_putref_final(res);
774                 spin_unlock(&ns->ns_hash_lock);
775                 if (res->lr_lvb_data)
776                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
777                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
778                 rc = 1;
779         }
780
781         RETURN(rc);
782 }
783
784 /* Returns 1 if the resource was freed, 0 if it remains. */
785 int ldlm_resource_putref_locked(struct ldlm_resource *res)
786 {
787         int rc = 0;
788         ENTRY;
789
790         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
791                atomic_read(&res->lr_refcount) - 1);
792         LASSERT(atomic_read(&res->lr_refcount) > 0);
793         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
794
795         LASSERT(atomic_read(&res->lr_refcount) >= 0);
796         if (atomic_dec_and_test(&res->lr_refcount)) {
797                 __ldlm_resource_putref_final(res);
798                 if (res->lr_lvb_data)
799                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
800                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
801                 rc = 1;
802         }
803
804         RETURN(rc);
805 }
806
807 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
808                             struct ldlm_lock *lock)
809 {
810         check_res_locked(res);
811
812         ldlm_resource_dump(D_OTHER, res);
813         CDEBUG(D_OTHER, "About to add this lock:\n");
814         ldlm_lock_dump(D_OTHER, lock, 0);
815
816         if (lock->l_destroyed) {
817                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
818                 return;
819         }
820
821         LASSERT(list_empty(&lock->l_res_link));
822
823         list_add_tail(&lock->l_res_link, head);
824 }
825
826 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
827                                      struct ldlm_lock *new)
828 {
829         struct ldlm_resource *res = original->l_resource;
830
831         check_res_locked(res);
832
833         ldlm_resource_dump(D_OTHER, res);
834         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
835         ldlm_lock_dump(D_OTHER, new, 0);
836
837         if (new->l_destroyed) {
838                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
839                 goto out;
840         }
841
842         LASSERT(list_empty(&new->l_res_link));
843
844         list_add(&new->l_res_link, &original->l_res_link);
845  out:;
846 }
847
848 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
849 {
850         check_res_locked(lock->l_resource);
851         ldlm_unlink_lock_skiplist(lock);
852         list_del_init(&lock->l_res_link);
853 }
854
855 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
856 {
857         desc->lr_type = res->lr_type;
858         desc->lr_name = res->lr_name;
859 }
860
861 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
862 {
863         struct list_head *tmp;
864
865         if (!((libcfs_debug | D_ERROR) & level))
866                 return;
867
868         mutex_down(ldlm_namespace_lock(client));
869
870         list_for_each(tmp, ldlm_namespace_list(client)) {
871                 struct ldlm_namespace *ns;
872                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
873                 ldlm_namespace_dump(level, ns);
874         }
875
876         mutex_up(ldlm_namespace_lock(client));
877 }
878
879 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
880 {
881         struct list_head *tmp;
882
883         if (!((libcfs_debug | D_ERROR) & level))
884                 return;
885
886         CDEBUG(level, "--- Namespace: %s (rc: %d, client: %d)\n",
887                   ns->ns_name, ns->ns_refcount, ns->ns_client);
888
889         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
890                 return;
891
892         spin_lock(&ns->ns_hash_lock);
893         tmp = ns->ns_root_list.next;
894         while (tmp != &ns->ns_root_list) {
895                 struct ldlm_resource *res;
896                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
897
898                 ldlm_resource_getref(res);
899                 spin_unlock(&ns->ns_hash_lock);
900
901                 lock_res(res);
902                 ldlm_resource_dump(level, res);
903                 unlock_res(res);
904
905                 spin_lock(&ns->ns_hash_lock);
906                 tmp = tmp->next;
907                 ldlm_resource_putref_locked(res);
908         }
909         ns->ns_next_dump = cfs_time_shift(10);
910         spin_unlock(&ns->ns_hash_lock);
911 }
912
913 void ldlm_resource_dump(int level, struct ldlm_resource *res)
914 {
915         struct list_head *tmp;
916         int pos;
917
918         CLASSERT(RES_NAME_SIZE == 4);
919
920         if (!((libcfs_debug | D_ERROR) & level))
921                 return;
922
923         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
924                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
925                res->lr_name.name[2], res->lr_name.name[3],
926                atomic_read(&res->lr_refcount));
927
928         if (!list_empty(&res->lr_granted)) {
929                 pos = 0;
930                 CDEBUG(level, "Granted locks:\n");
931                 list_for_each(tmp, &res->lr_granted) {
932                         struct ldlm_lock *lock;
933                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
934                         ldlm_lock_dump(level, lock, ++pos);
935                 }
936         }
937         if (!list_empty(&res->lr_converting)) {
938                 pos = 0;
939                 CDEBUG(level, "Converting locks:\n");
940                 list_for_each(tmp, &res->lr_converting) {
941                         struct ldlm_lock *lock;
942                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
943                         ldlm_lock_dump(level, lock, ++pos);
944                 }
945         }
946         if (!list_empty(&res->lr_waiting)) {
947                 pos = 0;
948                 CDEBUG(level, "Waiting locks:\n");
949                 list_for_each(tmp, &res->lr_waiting) {
950                         struct ldlm_lock *lock;
951                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
952                         ldlm_lock_dump(level, lock, ++pos);
953                 }
954         }
955 }