Whamcloud - gitweb
151c513c88be4ed1a7ed024443dffeb079c59b6a
[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         int lru_resize;
142
143         dummy[MAX_STRING_SIZE] = '\0';
144         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
145                 return -EFAULT;
146
147         if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
148                 CDEBUG(D_DLMTRACE,
149                        "dropping all unused locks from namespace %s\n",
150                        ns->ns_name);
151                 if (ns_connect_lru_resize(ns)) {
152                         int canceled, unused  = ns->ns_nr_unused;
153                         
154                         /* Try to cancel all @ns_nr_unused locks. */
155                         canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC, 
156                                                    LDLM_CANCEL_PASSED);
157                         if (canceled < unused) {
158                                 CERROR("not all requested locks are canceled, "
159                                        "requested: %d, canceled: %d\n", unused, 
160                                        canceled);
161                                 return -EINVAL;
162                         }
163                 } else {
164                         tmp = ns->ns_max_unused;
165                         ns->ns_max_unused = 0;
166                         ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
167                         ns->ns_max_unused = tmp;
168                 }
169                 return count;
170         }
171
172         tmp = simple_strtoul(dummy, &end, 0);
173         if (dummy == end) {
174                 CERROR("invalid value written\n");
175                 return -EINVAL;
176         }
177         lru_resize = (tmp == 0);
178         
179         if (ns_connect_lru_resize(ns)) {
180                 if (!lru_resize)
181                         ns->ns_max_unused = (unsigned int)tmp;
182                         
183                 if (tmp > ns->ns_nr_unused)
184                         tmp = ns->ns_nr_unused;
185                 tmp = ns->ns_nr_unused - tmp;
186                 
187                 CDEBUG(D_DLMTRACE, "changing namespace %s unused locks from %u to %u\n", 
188                        ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
189                 ldlm_cancel_lru(ns, (unsigned int)tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
190                 
191                 if (!lru_resize) {
192                         CDEBUG(D_DLMTRACE, "disable lru_resize for namespace %s\n", 
193                                ns->ns_name);
194                         ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
195                 }
196         } else {
197                 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
198                        ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
199                 ns->ns_max_unused = (unsigned int)tmp;
200                 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
201                 
202                 /* Make sure that originally lru resize was supported before 
203                  * turning it on here. */
204                 if (lru_resize && 
205                     (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
206                         CDEBUG(D_DLMTRACE, "enable lru_resize for namespace %s\n", 
207                                ns->ns_name);
208                         ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
209                 }
210         }
211
212         return count;
213 }
214
215 void ldlm_proc_namespace(struct ldlm_namespace *ns)
216 {
217         struct lprocfs_vars lock_vars[2];
218         char lock_name[MAX_STRING_SIZE + 1];
219
220         LASSERT(ns != NULL);
221         LASSERT(ns->ns_name != NULL);
222
223         lock_name[MAX_STRING_SIZE] = '\0';
224
225         memset(lock_vars, 0, sizeof(lock_vars));
226         lock_vars[0].name = lock_name;
227
228         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
229         lock_vars[0].data = &ns->ns_refcount;
230         lock_vars[0].read_fptr = lprocfs_rd_atomic;
231         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
232
233         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
234         lock_vars[0].data = &ns->ns_locks;
235         lock_vars[0].read_fptr = lprocfs_rd_atomic;
236         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
237
238         if (ns_is_client(ns)) {
239                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
240                          ns->ns_name);
241                 lock_vars[0].data = &ns->ns_nr_unused;
242                 lock_vars[0].read_fptr = lprocfs_rd_uint;
243                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
244
245                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
246                          ns->ns_name);
247                 lock_vars[0].data = ns;
248                 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
249                 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
250                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
251
252                 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
253                          ns->ns_name);
254                 lock_vars[0].data = ns;
255                 lock_vars[0].read_fptr = lprocfs_rd_uint;
256                 lock_vars[0].write_fptr = lprocfs_wr_uint;
257                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
258
259                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
260                          ns->ns_name);
261                 lock_vars[0].data = &ns->ns_max_age;
262                 lock_vars[0].read_fptr = lprocfs_rd_uint;
263                 lock_vars[0].write_fptr = lprocfs_wr_uint;
264                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
265         } else {
266                 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
267                          ns->ns_name);
268                 lock_vars[0].data = &ns->ns_ctime_age_limit;
269                 lock_vars[0].read_fptr = lprocfs_rd_uint;
270                 lock_vars[0].write_fptr = lprocfs_wr_uint;
271                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
272
273                 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
274                          ns->ns_name);
275                 lock_vars[0].data = &ns->ns_max_nolock_size;
276                 lock_vars[0].read_fptr = lprocfs_rd_uint;
277                 lock_vars[0].write_fptr = lprocfs_wr_uint;
278                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
279
280                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
281                          ns->ns_name);
282                 lock_vars[0].data = &ns->ns_contention_time;
283                 lock_vars[0].read_fptr = lprocfs_rd_uint;
284                 lock_vars[0].write_fptr = lprocfs_wr_uint;
285                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
286
287                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
288                          ns->ns_name);
289                 lock_vars[0].data = &ns->ns_contended_locks;
290                 lock_vars[0].read_fptr = lprocfs_rd_uint;
291                 lock_vars[0].write_fptr = lprocfs_wr_uint;
292                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
293         }
294 }
295 #undef MAX_STRING_SIZE
296 #else
297 #define ldlm_proc_namespace(ns) do {} while (0)
298 #endif /* LPROCFS */
299
300 struct ldlm_namespace *ldlm_namespace_new(char *name, ldlm_side_t client, 
301                                           ldlm_appetite_t apt)
302 {
303         struct ldlm_namespace *ns = NULL;
304         struct list_head *bucket;
305         int rc, idx, namelen;
306         ENTRY;
307
308         rc = ldlm_get_ref();
309         if (rc) {
310                 CERROR("ldlm_get_ref failed: %d\n", rc);
311                 RETURN(NULL);
312         }
313
314         OBD_ALLOC_PTR(ns);
315         if (!ns)
316                 GOTO(out_ref, NULL);
317
318         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
319         if (!ns->ns_hash)
320                 GOTO(out_ns, NULL);
321
322         ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
323         ns->ns_appetite = apt;
324         namelen = strlen(name);
325         OBD_ALLOC(ns->ns_name, namelen + 1);
326         if (!ns->ns_name)
327                 GOTO(out_hash, NULL);
328
329         strcpy(ns->ns_name, name);
330
331         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
332         ns->ns_refcount = 0;
333         ns->ns_client = client;
334         spin_lock_init(&ns->ns_hash_lock);
335         atomic_set(&ns->ns_locks, 0);
336         ns->ns_resources = 0;
337         cfs_waitq_init(&ns->ns_waitq);
338         ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
339         ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
340         ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
341
342         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
343              bucket--)
344                 CFS_INIT_LIST_HEAD(bucket);
345
346         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
347         ns->ns_nr_unused = 0;
348         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
349         ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
350         ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
351         spin_lock_init(&ns->ns_unused_lock);
352         ns->ns_orig_connect_flags = 0;
353         ns->ns_connect_flags = 0;
354         ldlm_proc_namespace(ns);
355
356         idx = atomic_read(ldlm_namespace_nr(client));
357         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
358         if (rc) {
359                 CERROR("Can't initialize lock pool, rc %d\n", rc);
360                 GOTO(out_proc, rc);
361         }
362
363         mutex_down(ldlm_namespace_lock(client));
364         list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
365         atomic_inc(ldlm_namespace_nr(client));
366         mutex_up(ldlm_namespace_lock(client));
367
368         RETURN(ns);
369 out_proc:
370         ldlm_namespace_cleanup(ns, 0);
371         OBD_FREE(ns->ns_name, namelen + 1);
372 out_hash:
373         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
374 out_ns:
375         OBD_FREE_PTR(ns);
376 out_ref:
377         ldlm_put_ref(0);
378         RETURN(NULL);
379 }
380
381 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
382
383 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
384  * This is currently only used for recovery, and we make certain assumptions
385  * as a result--notably, that we shouldn't cancel locks with refs. -phil
386  *
387  * Called with the ns_lock held. */
388 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
389                              int flags)
390 {
391         struct list_head *tmp;
392         int rc = 0, client = ns_is_client(res->lr_namespace);
393         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
394         ENTRY;
395
396
397         do {
398                 struct ldlm_lock *lock = NULL;
399
400                 /* first, we look for non-cleaned-yet lock
401                  * all cleaned locks are marked by CLEANED flag */
402                 lock_res(res);
403                 list_for_each(tmp, q) {
404                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
405                         if (lock->l_flags & LDLM_FL_CLEANED) {
406                                 lock = NULL;
407                                 continue;
408                         }
409                         LDLM_LOCK_GET(lock);
410                         lock->l_flags |= LDLM_FL_CLEANED;
411                         break;
412                 }
413
414                 if (lock == NULL) {
415                         unlock_res(res);
416                         break;
417                 }
418
419                 /* Set CBPENDING so nothing in the cancellation path
420                  * can match this lock */
421                 lock->l_flags |= LDLM_FL_CBPENDING;
422                 lock->l_flags |= LDLM_FL_FAILED;
423                 lock->l_flags |= flags;
424
425                 /* ... without sending a CANCEL message for local_only. */
426                 if (local_only)
427                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
428
429                 if (local_only && (lock->l_readers || lock->l_writers)) {
430                         /* This is a little bit gross, but much better than the
431                          * alternative: pretend that we got a blocking AST from
432                          * the server, so that when the lock is decref'd, it
433                          * will go away ... */
434                         unlock_res(res);
435                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
436                         if (lock->l_completion_ast)
437                                 lock->l_completion_ast(lock, 0, NULL);
438                         LDLM_LOCK_PUT(lock);
439                         continue;
440                 }
441
442                 if (client) {
443                         struct lustre_handle lockh;
444
445                         unlock_res(res);
446                         ldlm_lock2handle(lock, &lockh);
447                         rc = ldlm_cli_cancel(&lockh);
448                         if (rc)
449                                 CERROR("ldlm_cli_cancel: %d\n", rc);
450                 } else {
451                         ldlm_resource_unlink_lock(lock);
452                         unlock_res(res);
453                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
454                                    "client node");
455                         ldlm_lock_destroy(lock);
456                 }
457                 LDLM_LOCK_PUT(lock);
458         } while (1);
459
460         EXIT;
461 }
462
463 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
464 {
465         struct list_head *tmp;
466         int i;
467
468         if (ns == NULL) {
469                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
470                 return ELDLM_OK;
471         }
472
473         for (i = 0; i < RES_HASH_SIZE; i++) {
474                 spin_lock(&ns->ns_hash_lock);
475                 tmp = ns->ns_hash[i].next;
476                 while (tmp != &(ns->ns_hash[i])) {
477                         struct ldlm_resource *res;
478                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
479                         ldlm_resource_getref(res);
480                         spin_unlock(&ns->ns_hash_lock);
481
482                         cleanup_resource(res, &res->lr_granted, flags);
483                         cleanup_resource(res, &res->lr_converting, flags);
484                         cleanup_resource(res, &res->lr_waiting, flags);
485
486                         spin_lock(&ns->ns_hash_lock);
487                         tmp  = tmp->next;
488
489                         /* XXX: former stuff caused issues in case of race
490                          * between ldlm_namespace_cleanup() and lockd() when
491                          * client gets blocking ast when lock gets distracted by
492                          * server. This is 1_4 branch solution, let's see how
493                          * will it behave. */
494                         if (!ldlm_resource_putref_locked(res))
495                                 CDEBUG(D_INFO,
496                                        "Namespace %s resource refcount nonzero "
497                                        "(%d) after lock cleanup; forcing cleanup.\n",
498                                        ns->ns_name, atomic_read(&res->lr_refcount));
499                 }
500                 spin_unlock(&ns->ns_hash_lock);
501         }
502
503         return ELDLM_OK;
504 }
505
506 /* Cleanup, but also free, the namespace */
507 int ldlm_namespace_free_prior(struct ldlm_namespace *ns)
508 {
509         ENTRY;
510         if (!ns)
511                 RETURN(ELDLM_OK);
512
513         mutex_down(ldlm_namespace_lock(ns->ns_client));
514         /*
515          * Some asserts and possibly other parts of code still using 
516          * list_empty(&ns->ns_list_chain). This is why it is important
517          * to use list_del_init() here.
518          */
519         list_del_init(&ns->ns_list_chain);
520         atomic_dec(ldlm_namespace_nr(ns->ns_client));
521         ldlm_pool_fini(&ns->ns_pool);
522         mutex_up(ldlm_namespace_lock(ns->ns_client));
523
524         /* At shutdown time, don't call the cancellation callback */
525         ldlm_namespace_cleanup(ns, 0);
526
527         if (ns->ns_refcount > 0) {
528                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
529                 int rc;
530                 CDEBUG(D_DLMTRACE,
531                        "dlm namespace %s free waiting on refcount %d\n",
532                        ns->ns_name, ns->ns_refcount);
533                 rc = l_wait_event(ns->ns_waitq,
534                                   ns->ns_refcount == 0, &lwi);
535                 if (ns->ns_refcount)
536                         LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
537                                            "namespace cleanup aborted with %d "
538                                            "resources in use. (%d)\nI'm going "
539                                            "to try to clean up anyway, but I "
540                                            "might need a reboot of this node.\n",
541                                             ns->ns_name, (int) ns->ns_refcount, 
542                                             rc);
543                 CDEBUG(D_DLMTRACE,
544                        "dlm namespace %s free done waiting\n", ns->ns_name);
545         }
546
547         RETURN(ELDLM_OK);
548 }
549
550 int ldlm_namespace_free_post(struct ldlm_namespace *ns, int force)
551 {
552         ENTRY;
553         if (!ns)
554                 RETURN(ELDLM_OK);
555
556 #ifdef LPROCFS
557         {
558                 struct proc_dir_entry *dir;
559                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
560                 if (dir == NULL) {
561                         CERROR("dlm namespace %s has no procfs dir?\n",
562                                ns->ns_name);
563                 } else {
564                         lprocfs_remove(&dir);
565                 }
566         }
567 #endif
568
569         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
570         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
571         /* 
572          * @ns should be not on list in this time, otherwise this will cause
573          * issues realted to using freed @ns in pools thread. 
574          */
575         LASSERT(list_empty(&ns->ns_list_chain));
576         OBD_FREE_PTR(ns);
577         ldlm_put_ref(force);
578         RETURN(ELDLM_OK);
579 }
580
581
582 /* Cleanup the resource, and free namespace.
583  * bug 12864:
584  * Deadlock issue:
585  * proc1: destroy import
586  *        class_disconnect_export(grab cl_sem) ->
587  *              -> ldlm_namespace_free ->
588  *              -> lprocfs_remove(grab _lprocfs_lock).
589  * proc2: read proc info
590  *        lprocfs_fops_read(grab _lprocfs_lock) ->
591  *              -> osc_rd_active, etc(grab cl_sem).
592  *
593  * So that I have to split the ldlm_namespace_free into two parts - the first
594  * part ldlm_namespace_free_prior is used to cleanup the resource which is
595  * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
596  * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
597  * held.
598  */
599 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
600 {
601         ldlm_namespace_free_prior(ns);
602         ldlm_namespace_free_post(ns, force);
603         return ELDLM_OK;
604 }
605
606
607 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
608 {
609         LASSERT(ns->ns_refcount >= 0);
610         ns->ns_refcount++;
611 }
612
613 void ldlm_namespace_get(struct ldlm_namespace *ns)
614 {
615         spin_lock(&ns->ns_hash_lock);
616         ldlm_namespace_get_nolock(ns);
617         spin_unlock(&ns->ns_hash_lock);
618 }
619
620 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
621 {
622         LASSERT(ns->ns_refcount > 0);
623         ns->ns_refcount--;
624         if (ns->ns_refcount == 0 && wakeup)
625                 wake_up(&ns->ns_waitq);
626 }
627
628 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
629 {
630         spin_lock(&ns->ns_hash_lock);
631         ldlm_namespace_put_nolock(ns, wakeup);
632         spin_unlock(&ns->ns_hash_lock);
633 }
634
635 /* Should be called under ldlm_namespace_lock(client) taken */
636 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
637 {
638         LASSERT(!list_empty(&ns->ns_list_chain));
639         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
640         list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
641 }
642
643 /* Should be called under ldlm_namespace_lock(client) taken */
644 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
645 {
646         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
647         LASSERT(!list_empty(ldlm_namespace_list(client)));
648         return container_of(ldlm_namespace_list(client)->next, 
649                 struct ldlm_namespace, ns_list_chain);
650 }
651 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
652                           const struct ldlm_res_id *name)
653 {
654         __u32 hash = 0;
655         int i;
656
657         for (i = 0; i < RES_NAME_SIZE; i++)
658                 hash += name->name[i];
659
660         hash += (__u32)((unsigned long)parent >> 4);
661
662         return (hash & RES_HASH_MASK);
663 }
664
665 static struct ldlm_resource *ldlm_resource_new(void)
666 {
667         struct ldlm_resource *res;
668         int idx;
669
670         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
671         if (res == NULL)
672                 return NULL;
673
674         memset(res, 0, sizeof(*res));
675
676         CFS_INIT_LIST_HEAD(&res->lr_children);
677         CFS_INIT_LIST_HEAD(&res->lr_childof);
678         CFS_INIT_LIST_HEAD(&res->lr_granted);
679         CFS_INIT_LIST_HEAD(&res->lr_converting);
680         CFS_INIT_LIST_HEAD(&res->lr_waiting);
681
682         /* initialize interval trees for each lock mode*/
683         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
684                 res->lr_itree[idx].lit_size = 0;
685                 res->lr_itree[idx].lit_mode = 1 << idx;
686                 res->lr_itree[idx].lit_root = NULL;
687         }
688
689         atomic_set(&res->lr_refcount, 1);
690         spin_lock_init(&res->lr_lock);
691
692         /* one who creates the resource must unlock
693          * the semaphore after lvb initialization */
694         init_MUTEX_LOCKED(&res->lr_lvb_sem);
695
696         return res;
697 }
698
699 /* must be called with hash lock held */
700 static struct ldlm_resource *
701 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
702                    __u32 hash)
703 {
704         struct list_head *bucket, *tmp;
705         struct ldlm_resource *res;
706
707         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
708         bucket = ns->ns_hash + hash;
709
710         list_for_each(tmp, bucket) {
711                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
712                 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
713                         return res;
714         }
715
716         return NULL;
717 }
718
719 /* Args: locked namespace
720  * Returns: newly-allocated, referenced, unlocked resource */
721 static struct ldlm_resource *
722 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
723                   const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
724 {
725         struct list_head *bucket;
726         struct ldlm_resource *res, *old_res;
727         ENTRY;
728
729         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
730                  "type: %d\n", type);
731
732         res = ldlm_resource_new();
733         if (!res)
734                 RETURN(NULL);
735
736         res->lr_name = *name;
737         res->lr_namespace = ns;
738         res->lr_type = type;
739         res->lr_most_restr = LCK_NL;
740
741         spin_lock(&ns->ns_hash_lock);
742         old_res = ldlm_resource_find(ns, name, hash);
743         if (old_res) {
744                 /* someone won the race and added the resource before */
745                 ldlm_resource_getref(old_res);
746                 spin_unlock(&ns->ns_hash_lock);
747                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
748                 /* synchronize WRT resource creation */
749                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
750                         down(&old_res->lr_lvb_sem);
751                         up(&old_res->lr_lvb_sem);
752                 }
753                 RETURN(old_res);
754         }
755
756         /* we won! let's add the resource */
757         bucket = ns->ns_hash + hash;
758         list_add(&res->lr_hash, bucket);
759         ns->ns_resources++;
760         ldlm_namespace_get_nolock(ns);
761
762         if (parent == NULL) {
763                 list_add(&res->lr_childof, &ns->ns_root_list);
764         } else {
765                 res->lr_parent = parent;
766                 list_add(&res->lr_childof, &parent->lr_children);
767         }
768         spin_unlock(&ns->ns_hash_lock);
769
770         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
771                 int rc;
772
773                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
774                 rc = ns->ns_lvbo->lvbo_init(res);
775                 if (rc)
776                         CERROR("lvbo_init failed for resource "
777                                LPU64": rc %d\n", name->name[0], rc);
778                 /* we create resource with locked lr_lvb_sem */
779                 up(&res->lr_lvb_sem);
780         }
781
782         RETURN(res);
783 }
784
785 /* Args: unlocked namespace
786  * Locks: takes and releases ns->ns_lock and res->lr_lock
787  * Returns: referenced, unlocked ldlm_resource or NULL */
788 struct ldlm_resource *
789 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
790                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
791 {
792         __u32 hash = ldlm_hash_fn(parent, name);
793         struct ldlm_resource *res = NULL;
794         ENTRY;
795
796         LASSERT(ns != NULL);
797         LASSERT(ns->ns_hash != NULL);
798         LASSERT(name->name[0] != 0);
799
800         spin_lock(&ns->ns_hash_lock);
801         res = ldlm_resource_find(ns, name, hash);
802         if (res) {
803                 ldlm_resource_getref(res);
804                 spin_unlock(&ns->ns_hash_lock);
805                 /* synchronize WRT resource creation */
806                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
807                         down(&res->lr_lvb_sem);
808                         up(&res->lr_lvb_sem);
809                 }
810                 RETURN(res);
811         }
812         spin_unlock(&ns->ns_hash_lock);
813
814         if (create == 0)
815                 RETURN(NULL);
816
817         res = ldlm_resource_add(ns, parent, name, hash, type);
818         RETURN(res);
819 }
820
821 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
822 {
823         LASSERT(res != NULL);
824         LASSERT(res != LP_POISON);
825         atomic_inc(&res->lr_refcount);
826         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
827                atomic_read(&res->lr_refcount));
828         return res;
829 }
830
831 void __ldlm_resource_putref_final(struct ldlm_resource *res)
832 {
833         struct ldlm_namespace *ns = res->lr_namespace;
834
835         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
836
837         if (!list_empty(&res->lr_granted)) {
838                 ldlm_resource_dump(D_ERROR, res);
839                 LBUG();
840         }
841
842         if (!list_empty(&res->lr_converting)) {
843                 ldlm_resource_dump(D_ERROR, res);
844                 LBUG();
845         }
846
847         if (!list_empty(&res->lr_waiting)) {
848                 ldlm_resource_dump(D_ERROR, res);
849                 LBUG();
850         }
851
852         if (!list_empty(&res->lr_children)) {
853                 ldlm_resource_dump(D_ERROR, res);
854                 LBUG();
855         }
856
857         /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few 
858          * lines below when all children are freed. */
859         ldlm_namespace_put_nolock(ns, 0);
860         list_del_init(&res->lr_hash);
861         list_del_init(&res->lr_childof);
862
863         ns->ns_resources--;
864         if (ns->ns_resources == 0)
865                 wake_up(&ns->ns_waitq);
866 }
867
868 /* Returns 1 if the resource was freed, 0 if it remains. */
869 int ldlm_resource_putref(struct ldlm_resource *res)
870 {
871         struct ldlm_namespace *ns = res->lr_namespace;
872         int rc = 0;
873         ENTRY;
874
875         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
876                atomic_read(&res->lr_refcount) - 1);
877         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
878                  atomic_read(&res->lr_refcount));
879         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
880                  atomic_read(&res->lr_refcount));
881
882         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
883                 __ldlm_resource_putref_final(res);
884                 spin_unlock(&ns->ns_hash_lock);
885                 if (res->lr_lvb_data)
886                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
887                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
888                 rc = 1;
889         }
890
891         RETURN(rc);
892 }
893
894 /* Returns 1 if the resource was freed, 0 if it remains. */
895 int ldlm_resource_putref_locked(struct ldlm_resource *res)
896 {
897         int rc = 0;
898         ENTRY;
899
900         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
901                atomic_read(&res->lr_refcount) - 1);
902         LASSERT(atomic_read(&res->lr_refcount) > 0);
903         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
904
905         LASSERT(atomic_read(&res->lr_refcount) >= 0);
906         if (atomic_dec_and_test(&res->lr_refcount)) {
907                 __ldlm_resource_putref_final(res);
908                 if (res->lr_lvb_data)
909                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
910                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
911                 rc = 1;
912         }
913
914         RETURN(rc);
915 }
916
917 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
918                             struct ldlm_lock *lock)
919 {
920         check_res_locked(res);
921
922         ldlm_resource_dump(D_OTHER, res);
923         CDEBUG(D_OTHER, "About to add this lock:\n");
924         ldlm_lock_dump(D_OTHER, lock, 0);
925
926         if (lock->l_destroyed) {
927                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
928                 return;
929         }
930
931         LASSERT(list_empty(&lock->l_res_link));
932
933         list_add_tail(&lock->l_res_link, head);
934 }
935
936 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
937                                      struct ldlm_lock *new)
938 {
939         struct ldlm_resource *res = original->l_resource;
940
941         check_res_locked(res);
942
943         ldlm_resource_dump(D_OTHER, res);
944         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
945         ldlm_lock_dump(D_OTHER, new, 0);
946
947         if (new->l_destroyed) {
948                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
949                 goto out;
950         }
951
952         LASSERT(list_empty(&new->l_res_link));
953
954         list_add(&new->l_res_link, &original->l_res_link);
955  out:;
956 }
957
958 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
959 {
960         int type = lock->l_resource->lr_type;
961
962         check_res_locked(lock->l_resource);
963         if (type == LDLM_IBITS || type == LDLM_PLAIN)
964                 ldlm_unlink_lock_skiplist(lock);
965         else if (type == LDLM_EXTENT)
966                 ldlm_extent_unlink_lock(lock);
967         list_del_init(&lock->l_res_link);
968 }
969
970 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
971 {
972         desc->lr_type = res->lr_type;
973         desc->lr_name = res->lr_name;
974 }
975
976 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
977 {
978         struct list_head *tmp;
979
980         if (!((libcfs_debug | D_ERROR) & level))
981                 return;
982
983         mutex_down(ldlm_namespace_lock(client));
984
985         list_for_each(tmp, ldlm_namespace_list(client)) {
986                 struct ldlm_namespace *ns;
987                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
988                 ldlm_namespace_dump(level, ns);
989         }
990
991         mutex_up(ldlm_namespace_lock(client));
992 }
993
994 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
995 {
996         struct list_head *tmp;
997
998         if (!((libcfs_debug | D_ERROR) & level))
999                 return;
1000
1001         CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n", 
1002                ns->ns_name, ns->ns_refcount, 
1003                ns_is_client(ns) ? "client" : "server");
1004
1005         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1006                 return;
1007
1008         spin_lock(&ns->ns_hash_lock);
1009         tmp = ns->ns_root_list.next;
1010         while (tmp != &ns->ns_root_list) {
1011                 struct ldlm_resource *res;
1012                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1013
1014                 ldlm_resource_getref(res);
1015                 spin_unlock(&ns->ns_hash_lock);
1016
1017                 lock_res(res);
1018                 ldlm_resource_dump(level, res);
1019                 unlock_res(res);
1020
1021                 spin_lock(&ns->ns_hash_lock);
1022                 tmp = tmp->next;
1023                 ldlm_resource_putref_locked(res);
1024         }
1025         ns->ns_next_dump = cfs_time_shift(10);
1026         spin_unlock(&ns->ns_hash_lock);
1027 }
1028
1029 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1030 {
1031         struct list_head *tmp;
1032         int pos;
1033
1034         CLASSERT(RES_NAME_SIZE == 4);
1035
1036         if (!((libcfs_debug | D_ERROR) & level))
1037                 return;
1038
1039         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1040                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1041                res->lr_name.name[2], res->lr_name.name[3],
1042                atomic_read(&res->lr_refcount));
1043
1044         if (!list_empty(&res->lr_granted)) {
1045                 pos = 0;
1046                 CDEBUG(level, "Granted locks:\n");
1047                 list_for_each(tmp, &res->lr_granted) {
1048                         struct ldlm_lock *lock;
1049                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1050                         ldlm_lock_dump(level, lock, ++pos);
1051                 }
1052         }
1053         if (!list_empty(&res->lr_converting)) {
1054                 pos = 0;
1055                 CDEBUG(level, "Converting locks:\n");
1056                 list_for_each(tmp, &res->lr_converting) {
1057                         struct ldlm_lock *lock;
1058                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1059                         ldlm_lock_dump(level, lock, ++pos);
1060                 }
1061         }
1062         if (!list_empty(&res->lr_waiting)) {
1063                 pos = 0;
1064                 CDEBUG(level, "Waiting locks:\n");
1065                 list_for_each(tmp, &res->lr_waiting) {
1066                         struct ldlm_lock *lock;
1067                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1068                         ldlm_lock_dump(level, lock, ++pos);
1069                 }
1070         }
1071 }