Whamcloud - gitweb
resolve race between obd_disconnect and class_disconnect_exports
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_resource.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Peter Braam <braam@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LDLM
43 #ifdef __KERNEL__
44 # include <lustre_dlm.h>
45 #else
46 # include <liblustre.h>
47 #endif
48
49 #include <obd_class.h>
50 #include "ldlm_internal.h"
51
52 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
53
54 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
55 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
56
57 struct semaphore ldlm_srv_namespace_lock;
58 struct list_head ldlm_srv_namespace_list =
59         CFS_LIST_HEAD_INIT(ldlm_srv_namespace_list);
60
61 struct semaphore ldlm_cli_namespace_lock;
62 struct list_head ldlm_cli_namespace_list =
63         CFS_LIST_HEAD_INIT(ldlm_cli_namespace_list);
64
65 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
66 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
67 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
68
69 #ifdef LPROCFS
70 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
71                              unsigned long count, void *data)
72 {
73         ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
74         ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
75         RETURN(count);
76 }
77
78 int ldlm_proc_setup(void)
79 {
80         int rc;
81         struct lprocfs_vars list[] = {
82                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
83                 { NULL }};
84         ENTRY;
85         LASSERT(ldlm_ns_proc_dir == NULL);
86
87         ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
88                                               proc_lustre_root,
89                                               NULL, NULL);
90         if (IS_ERR(ldlm_type_proc_dir)) {
91                 CERROR("LProcFS failed in ldlm-init\n");
92                 rc = PTR_ERR(ldlm_type_proc_dir);
93                 GOTO(err, rc);
94         }
95
96         ldlm_ns_proc_dir = lprocfs_register("namespaces",
97                                             ldlm_type_proc_dir,
98                                             NULL, NULL);
99         if (IS_ERR(ldlm_ns_proc_dir)) {
100                 CERROR("LProcFS failed in ldlm-init\n");
101                 rc = PTR_ERR(ldlm_ns_proc_dir);
102                 GOTO(err_type, rc);
103         }
104
105         ldlm_svc_proc_dir = lprocfs_register("services",
106                                             ldlm_type_proc_dir,
107                                             NULL, NULL);
108         if (IS_ERR(ldlm_svc_proc_dir)) {
109                 CERROR("LProcFS failed in ldlm-init\n");
110                 rc = PTR_ERR(ldlm_svc_proc_dir);
111                 GOTO(err_ns, rc);
112         }
113
114         rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
115
116         RETURN(0);
117
118 err_ns:
119         lprocfs_remove(&ldlm_ns_proc_dir);
120 err_type:
121         lprocfs_remove(&ldlm_type_proc_dir);
122 err:
123         ldlm_svc_proc_dir = NULL;
124         RETURN(rc);
125 }
126
127 void ldlm_proc_cleanup(void)
128 {
129         if (ldlm_svc_proc_dir)
130                 lprocfs_remove(&ldlm_svc_proc_dir);
131
132         if (ldlm_ns_proc_dir)
133                 lprocfs_remove(&ldlm_ns_proc_dir);
134
135         if (ldlm_type_proc_dir)
136                 lprocfs_remove(&ldlm_type_proc_dir);
137 }
138
139 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
140                                int count, int *eof, void *data)
141 {
142         struct ldlm_namespace *ns = data;
143         __u32 *nr = &ns->ns_max_unused;
144
145         if (ns_connect_lru_resize(ns))
146                 nr = &ns->ns_nr_unused;
147         return lprocfs_rd_uint(page, start, off, count, eof, nr);
148 }
149
150 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
151                                unsigned long count, void *data)
152 {
153         struct ldlm_namespace *ns = data;
154         char dummy[MAX_STRING_SIZE + 1] = { '\0' }, *end;
155         unsigned long tmp;
156         int lru_resize;
157
158         if (count >= sizeof(dummy) || count == 0)
159                 return -EINVAL;
160
161         if (copy_from_user(dummy, buffer, count))
162                 return -EFAULT;
163
164         if (strncmp(dummy, "clear", 5) == 0) {
165                 CDEBUG(D_DLMTRACE,
166                        "dropping all unused locks from namespace %s\n",
167                        ns->ns_name);
168                 if (ns_connect_lru_resize(ns)) {
169                         int canceled, unused  = ns->ns_nr_unused;
170
171                         /* Try to cancel all @ns_nr_unused locks. */
172                         canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
173                                                    LDLM_CANCEL_PASSED);
174                         if (canceled < unused) {
175                                 CERROR("not all requested locks are canceled, "
176                                        "requested: %d, canceled: %d\n", unused,
177                                        canceled);
178                                 return -EINVAL;
179                         }
180                 } else {
181                         tmp = ns->ns_max_unused;
182                         ns->ns_max_unused = 0;
183                         ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
184                         ns->ns_max_unused = tmp;
185                 }
186                 return count;
187         }
188
189         tmp = simple_strtoul(dummy, &end, 0);
190         if (dummy == end) {
191                 CERROR("invalid value written\n");
192                 return -EINVAL;
193         }
194         lru_resize = (tmp == 0);
195
196         if (ns_connect_lru_resize(ns)) {
197                 if (!lru_resize)
198                         ns->ns_max_unused = (unsigned int)tmp;
199
200                 if (tmp > ns->ns_nr_unused)
201                         tmp = ns->ns_nr_unused;
202                 tmp = ns->ns_nr_unused - tmp;
203
204                 CDEBUG(D_DLMTRACE,
205                        "changing namespace %s unused locks from %u to %u\n",
206                        ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
207                 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
208
209                 if (!lru_resize) {
210                         CDEBUG(D_DLMTRACE,
211                                "disable lru_resize for namespace %s\n",
212                                ns->ns_name);
213                         ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
214                 }
215         } else {
216                 CDEBUG(D_DLMTRACE,
217                        "changing namespace %s max_unused from %u to %u\n",
218                        ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
219                 ns->ns_max_unused = (unsigned int)tmp;
220                 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
221
222                 /* Make sure that originally lru resize was supported before
223                  * turning it on here. */
224                 if (lru_resize &&
225                     (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
226                         CDEBUG(D_DLMTRACE,
227                                "enable lru_resize for namespace %s\n",
228                                ns->ns_name);
229                         ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
230                 }
231         }
232         return count;
233 }
234
235 void ldlm_proc_namespace(struct ldlm_namespace *ns)
236 {
237         struct lprocfs_vars lock_vars[2];
238         char lock_name[MAX_STRING_SIZE + 1];
239
240         LASSERT(ns != NULL);
241         LASSERT(ns->ns_name != NULL);
242
243         lock_name[MAX_STRING_SIZE] = '\0';
244
245         memset(lock_vars, 0, sizeof(lock_vars));
246         lock_vars[0].name = lock_name;
247
248         snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
249         lock_vars[0].data = &ns->ns_refcount;
250         lock_vars[0].read_fptr = lprocfs_rd_atomic;
251         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
252
253         snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
254         lock_vars[0].data = &ns->ns_locks;
255         lock_vars[0].read_fptr = lprocfs_rd_atomic;
256         lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
257
258         if (ns_is_client(ns)) {
259                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
260                          ns->ns_name);
261                 lock_vars[0].data = &ns->ns_nr_unused;
262                 lock_vars[0].read_fptr = lprocfs_rd_uint;
263                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
264
265                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
266                          ns->ns_name);
267                 lock_vars[0].data = ns;
268                 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
269                 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
270                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
271
272                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
273                          ns->ns_name);
274                 lock_vars[0].data = &ns->ns_max_age;
275                 lock_vars[0].read_fptr = lprocfs_rd_uint;
276                 lock_vars[0].write_fptr = lprocfs_wr_uint;
277                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
278         } else {
279                 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
280                          ns->ns_name);
281                 lock_vars[0].data = &ns->ns_timeouts;
282                 lock_vars[0].read_fptr = lprocfs_rd_uint;
283                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
284
285                 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
286                          ns->ns_name);
287                 lock_vars[0].data = &ns->ns_max_nolock_size;
288                 lock_vars[0].read_fptr = lprocfs_rd_uint;
289                 lock_vars[0].write_fptr = lprocfs_wr_uint;
290                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
291
292                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
293                          ns->ns_name);
294                 lock_vars[0].data = &ns->ns_contention_time;
295                 lock_vars[0].read_fptr = lprocfs_rd_uint;
296                 lock_vars[0].write_fptr = lprocfs_wr_uint;
297                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
298
299                 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
300                          ns->ns_name);
301                 lock_vars[0].data = &ns->ns_contended_locks;
302                 lock_vars[0].read_fptr = lprocfs_rd_uint;
303                 lock_vars[0].write_fptr = lprocfs_wr_uint;
304                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
305         }
306 }
307 #undef MAX_STRING_SIZE
308 #else
309 #define ldlm_proc_namespace(ns) do {} while (0)
310 #endif /* LPROCFS */
311
312 struct ldlm_namespace *
313 ldlm_namespace_new(struct obd_device *obd, char *name,
314                    ldlm_side_t client, ldlm_appetite_t apt)
315 {
316         struct ldlm_namespace *ns = NULL;
317         struct list_head *bucket;
318         int rc, idx, namelen;
319         ENTRY;
320
321         rc = ldlm_get_ref();
322         if (rc) {
323                 CERROR("ldlm_get_ref failed: %d\n", rc);
324                 RETURN(NULL);
325         }
326
327         OBD_ALLOC_PTR(ns);
328         if (!ns)
329                 GOTO(out_ref, NULL);
330
331         OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
332         if (!ns->ns_hash)
333                 GOTO(out_ns, NULL);
334
335         namelen = strlen(name);
336         OBD_ALLOC(ns->ns_name, namelen + 1);
337         if (!ns->ns_name)
338                 GOTO(out_hash, NULL);
339
340         ns->ns_appetite = apt;
341
342         LASSERT(obd != NULL);
343         ns->ns_obd = obd;
344
345         strcpy(ns->ns_name, name);
346
347         CFS_INIT_LIST_HEAD(&ns->ns_root_list);
348         CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
349         ns->ns_refcount = 0;
350         ns->ns_client = client;
351         spin_lock_init(&ns->ns_hash_lock);
352         atomic_set(&ns->ns_locks, 0);
353         ns->ns_resources = 0;
354         cfs_waitq_init(&ns->ns_waitq);
355         ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
356         ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
357         ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
358
359         for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
360              bucket--)
361                 CFS_INIT_LIST_HEAD(bucket);
362
363         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
364         CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
365         ns->ns_nr_unused = 0;
366         ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
367         ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
368         ns->ns_timeouts = 0;
369         spin_lock_init(&ns->ns_unused_lock);
370         ns->ns_orig_connect_flags = 0;
371         ns->ns_connect_flags = 0;
372
373         ldlm_proc_namespace(ns);
374
375         idx = atomic_read(ldlm_namespace_nr(client));
376
377         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
378         if (rc) {
379                 CERROR("Can't initialize lock pool, rc %d\n", rc);
380                 GOTO(out_proc, rc);
381         }
382
383         at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
384         ldlm_namespace_register(ns, client);
385         RETURN(ns);
386 out_proc:
387         ldlm_namespace_cleanup(ns, 0);
388         OBD_FREE(ns->ns_name, namelen + 1);
389 out_hash:
390         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
391 out_ns:
392         OBD_FREE_PTR(ns);
393 out_ref:
394         ldlm_put_ref();
395         RETURN(NULL);
396 }
397
398 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
399
400 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
401  * This is currently only used for recovery, and we make certain assumptions
402  * as a result--notably, that we shouldn't cancel locks with refs. -phil
403  *
404  * Called with the ns_lock held. */
405 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
406                              int flags)
407 {
408         struct list_head *tmp;
409         int rc = 0, client = ns_is_client(res->lr_namespace);
410         int local_only = (flags & LDLM_FL_LOCAL_ONLY);
411         ENTRY;
412
413         do {
414                 struct ldlm_lock *lock = NULL;
415
416                 /* first, we look for non-cleaned-yet lock
417                  * all cleaned locks are marked by CLEANED flag */
418                 lock_res(res);
419                 list_for_each(tmp, q) {
420                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
421                         if (lock->l_flags & LDLM_FL_CLEANED) {
422                                 lock = NULL;
423                                 continue;
424                         }
425                         LDLM_LOCK_GET(lock);
426                         lock->l_flags |= LDLM_FL_CLEANED;
427                         break;
428                 }
429
430                 if (lock == NULL) {
431                         unlock_res(res);
432                         break;
433                 }
434
435                 /* Set CBPENDING so nothing in the cancellation path
436                  * can match this lock */
437                 lock->l_flags |= LDLM_FL_CBPENDING;
438                 lock->l_flags |= LDLM_FL_FAILED;
439                 lock->l_flags |= flags;
440
441                 /* ... without sending a CANCEL message for local_only. */
442                 if (local_only)
443                         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
444
445                 if (local_only && (lock->l_readers || lock->l_writers)) {
446                         /* This is a little bit gross, but much better than the
447                          * alternative: pretend that we got a blocking AST from
448                          * the server, so that when the lock is decref'd, it
449                          * will go away ... */
450                         unlock_res(res);
451                         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
452                         if (lock->l_completion_ast)
453                                 lock->l_completion_ast(lock, 0, NULL);
454                         LDLM_LOCK_PUT(lock);
455                         continue;
456                 }
457
458                 if (client) {
459                         struct lustre_handle lockh;
460
461                         unlock_res(res);
462                         ldlm_lock2handle(lock, &lockh);
463                         rc = ldlm_cli_cancel(&lockh);
464                         if (rc)
465                                 CERROR("ldlm_cli_cancel: %d\n", rc);
466                 } else {
467                         ldlm_resource_unlink_lock(lock);
468                         unlock_res(res);
469                         LDLM_DEBUG(lock, "Freeing a lock still held by a "
470                                    "client node");
471                         ldlm_lock_destroy(lock);
472                 }
473                 LDLM_LOCK_PUT(lock);
474         } while (1);
475
476         EXIT;
477 }
478
479 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
480 {
481         struct list_head *tmp;
482         int i;
483
484         if (ns == NULL) {
485                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
486                 return ELDLM_OK;
487         }
488
489         for (i = 0; i < RES_HASH_SIZE; i++) {
490                 spin_lock(&ns->ns_hash_lock);
491                 tmp = ns->ns_hash[i].next;
492                 while (tmp != &(ns->ns_hash[i])) {
493                         struct ldlm_resource *res;
494                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
495                         ldlm_resource_getref(res);
496                         spin_unlock(&ns->ns_hash_lock);
497
498                         cleanup_resource(res, &res->lr_granted, flags);
499                         cleanup_resource(res, &res->lr_converting, flags);
500                         cleanup_resource(res, &res->lr_waiting, flags);
501
502                         spin_lock(&ns->ns_hash_lock);
503                         tmp  = tmp->next;
504
505                         /* XXX: former stuff caused issues in case of race
506                          * between ldlm_namespace_cleanup() and lockd() when
507                          * client gets blocking ast when lock gets distracted by
508                          * server. This is 1_4 branch solution, let's see how
509                          * will it behave. */
510                         if (!ldlm_resource_putref_locked(res))
511                                 CDEBUG(D_INFO,
512                                        "Namespace %s resource refcount nonzero "
513                                        "(%d) after lock cleanup; forcing cleanup.\n",
514                                        ns->ns_name, atomic_read(&res->lr_refcount));
515                 }
516                 spin_unlock(&ns->ns_hash_lock);
517         }
518
519         return ELDLM_OK;
520 }
521
522 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
523 {
524         ENTRY;
525         /* At shutdown time, don't call the cancellation callback */
526         ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
527
528         if (ns->ns_refcount > 0) {
529                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
530                 int rc;
531                 CDEBUG(D_DLMTRACE,
532                        "dlm namespace %s free waiting on refcount %d\n",
533                        ns->ns_name, ns->ns_refcount);
534 force_wait:
535                 if (force)
536                         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
537
538                 rc = l_wait_event(ns->ns_waitq,
539                                   ns->ns_refcount == 0, &lwi);
540
541                 /* Forced cleanups should be able to reclaim all references,
542                  * so it's safe to wait forever... we can't leak locks... */
543                 if (force && rc == -ETIMEDOUT) {
544                         LCONSOLE_ERROR("Forced cleanup waiting for %s "
545                                        "namespace with %d resources in use, "
546                                        "(rc=%d)\n", ns->ns_name,
547                                        ns->ns_refcount, rc);
548                         GOTO(force_wait, rc);
549                 }
550
551                 if (ns->ns_refcount) {
552                         LCONSOLE_ERROR("Cleanup waiting for %s namespace "
553                                        "with %d resources in use, (rc=%d)\n",
554                                        ns->ns_name,
555                                        ns->ns_refcount, rc);
556                         RETURN(ELDLM_NAMESPACE_EXISTS);
557                 }
558                 CDEBUG(D_DLMTRACE,
559                        "dlm namespace %s free done waiting\n", ns->ns_name);
560         }
561
562         RETURN(ELDLM_OK);
563 }
564
565 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
566                                struct obd_import *imp,
567                                int force)
568 {
569         int rc;
570         ENTRY;
571         if (!ns) {
572                 EXIT;
573                 return;
574         }
575
576         /* Make sure that nobody can find this ns in its list. */
577         ldlm_namespace_unregister(ns, ns->ns_client);
578
579         /* Can fail with -EINTR when force == 0 in which case try harder */
580         rc = __ldlm_namespace_free(ns, force);
581         if (rc != ELDLM_OK) {
582                 if (imp) {
583                         ptlrpc_disconnect_import(imp, 0);
584                         ptlrpc_invalidate_import(imp);
585                 }
586
587                 /* With all requests dropped and the import inactive
588                  * we are gaurenteed all reference will be dropped. */
589                 rc = __ldlm_namespace_free(ns, 1);
590                 LASSERT(rc == 0);
591         }
592         EXIT;
593 }
594
595 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
596 {
597         ENTRY;
598         if (!ns) {
599                 EXIT;
600                 return;
601         }
602
603         /* Fini pool _before_ parent proc dir is removed. This is important
604          * as ldlm_pool_fini() removes own proc dir which is child to @dir.
605          * Removing it after @dir may cause oops. */
606         ldlm_pool_fini(&ns->ns_pool);
607
608 #ifdef LPROCFS
609         {
610                 struct proc_dir_entry *dir;
611                 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
612                 if (dir == NULL) {
613                         CERROR("dlm namespace %s has no procfs dir?\n",
614                                ns->ns_name);
615                 } else {
616                         lprocfs_remove(&dir);
617                 }
618         }
619 #endif
620         OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
621         OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
622
623         /* @ns should be not on list in this time, otherwise this will cause
624          * issues realted to using freed @ns in pools thread. */
625         LASSERT(list_empty(&ns->ns_list_chain));
626         OBD_FREE_PTR(ns);
627         ldlm_put_ref();
628         EXIT;
629 }
630
631 /* Cleanup the resource, and free namespace.
632  * bug 12864:
633  * Deadlock issue:
634  * proc1: destroy import
635  *        class_disconnect_export(grab cl_sem) ->
636  *              -> ldlm_namespace_free ->
637  *              -> lprocfs_remove(grab _lprocfs_lock).
638  * proc2: read proc info
639  *        lprocfs_fops_read(grab _lprocfs_lock) ->
640  *              -> osc_rd_active, etc(grab cl_sem).
641  *
642  * So that I have to split the ldlm_namespace_free into two parts - the first
643  * part ldlm_namespace_free_prior is used to cleanup the resource which is
644  * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
645  * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
646  * held.
647  */
648 void ldlm_namespace_free(struct ldlm_namespace *ns,
649                          struct obd_import *imp,
650                          int force)
651 {
652         ldlm_namespace_free_prior(ns, imp, force);
653         ldlm_namespace_free_post(ns);
654 }
655
656 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
657 {
658         ns->ns_refcount++;
659 }
660
661 void ldlm_namespace_get(struct ldlm_namespace *ns)
662 {
663         spin_lock(&ns->ns_hash_lock);
664         ldlm_namespace_get_locked(ns);
665         spin_unlock(&ns->ns_hash_lock);
666 }
667
668 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
669 {
670         LASSERT(ns->ns_refcount > 0);
671         ns->ns_refcount--;
672         if (ns->ns_refcount == 0 && wakeup)
673                 wake_up(&ns->ns_waitq);
674 }
675
676 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
677 {
678         spin_lock(&ns->ns_hash_lock);
679         ldlm_namespace_put_locked(ns, wakeup);
680         spin_unlock(&ns->ns_hash_lock);
681 }
682
683 /* Register @ns in the list of namespaces */
684 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
685 {
686         mutex_down(ldlm_namespace_lock(client));
687         LASSERT(list_empty(&ns->ns_list_chain));
688         list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
689         atomic_inc(ldlm_namespace_nr(client));
690         mutex_up(ldlm_namespace_lock(client));
691 }
692
693 /* Unregister @ns from the list of namespaces */
694 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
695 {
696         mutex_down(ldlm_namespace_lock(client));
697         LASSERT(!list_empty(&ns->ns_list_chain));
698         /*
699          * Some asserts and possibly other parts of code still using
700          * list_empty(&ns->ns_list_chain). This is why it is important
701          * to use list_del_init() here.
702          */
703         list_del_init(&ns->ns_list_chain);
704         atomic_dec(ldlm_namespace_nr(client));
705         mutex_up(ldlm_namespace_lock(client));
706 }
707
708 /* Should be called under ldlm_namespace_lock(client) taken */
709 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
710 {
711         LASSERT(!list_empty(&ns->ns_list_chain));
712         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
713         list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
714 }
715
716 /* Should be called under ldlm_namespace_lock(client) taken */
717 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
718 {
719         LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
720         LASSERT(!list_empty(ldlm_namespace_list(client)));
721         return container_of(ldlm_namespace_list(client)->next,
722                 struct ldlm_namespace, ns_list_chain);
723 }
724
725 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
726 {
727         __u32 hash = 0;
728         int i;
729
730         for (i = 0; i < RES_NAME_SIZE; i++)
731                 hash += name.name[i];
732
733         hash += (__u32)((unsigned long)parent >> 4);
734
735         return (hash & RES_HASH_MASK);
736 }
737
738 static struct ldlm_resource *ldlm_resource_new(void)
739 {
740         struct ldlm_resource *res;
741         int idx;
742
743         OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
744         if (res == NULL)
745                 return NULL;
746
747         memset(res, 0, sizeof(*res));
748
749         CFS_INIT_LIST_HEAD(&res->lr_children);
750         CFS_INIT_LIST_HEAD(&res->lr_childof);
751         CFS_INIT_LIST_HEAD(&res->lr_granted);
752         CFS_INIT_LIST_HEAD(&res->lr_converting);
753         CFS_INIT_LIST_HEAD(&res->lr_waiting);
754
755         /* initialize interval trees for each lock mode*/
756         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
757                 res->lr_itree[idx].lit_size = 0;
758                 res->lr_itree[idx].lit_mode = 1 << idx;
759                 res->lr_itree[idx].lit_root = NULL;
760         }
761
762         atomic_set(&res->lr_refcount, 1);
763         spin_lock_init(&res->lr_lock);
764
765         /* one who creates the resource must unlock
766          * the semaphore after lvb initialization */
767         init_MUTEX_LOCKED(&res->lr_lvb_sem);
768
769         return res;
770 }
771
772 /* must be called with hash lock held */
773 static struct ldlm_resource *
774 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
775 {
776         struct list_head *bucket, *tmp;
777         struct ldlm_resource *res;
778
779         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
780         bucket = ns->ns_hash + hash;
781
782         list_for_each(tmp, bucket) {
783                 res = list_entry(tmp, struct ldlm_resource, lr_hash);
784                 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
785                         return res;
786         }
787
788         return NULL;
789 }
790
791 /* Args: locked namespace
792  * Returns: newly-allocated, referenced, unlocked resource */
793 static struct ldlm_resource *
794 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
795                   struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
796 {
797         struct list_head *bucket;
798         struct ldlm_resource *res, *old_res;
799         ENTRY;
800
801         LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
802                  "type: %d\n", type);
803
804         res = ldlm_resource_new();
805         if (!res)
806                 RETURN(NULL);
807
808         res->lr_name = name;
809         res->lr_namespace = ns;
810         res->lr_type = type;
811         res->lr_most_restr = LCK_NL;
812
813         spin_lock(&ns->ns_hash_lock);
814         old_res = ldlm_resource_find(ns, name, hash);
815         if (old_res) {
816                 /* someone won the race and added the resource before */
817                 ldlm_resource_getref(old_res);
818                 spin_unlock(&ns->ns_hash_lock);
819                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
820                 /* synchronize WRT resource creation */
821                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
822                         down(&old_res->lr_lvb_sem);
823                         up(&old_res->lr_lvb_sem);
824                 }
825                 RETURN(old_res);
826         }
827
828         /* we won! let's add the resource */
829         bucket = ns->ns_hash + hash;
830         list_add(&res->lr_hash, bucket);
831         ns->ns_resources++;
832         ldlm_namespace_get_locked(ns);
833
834         if (parent == NULL) {
835                 list_add(&res->lr_childof, &ns->ns_root_list);
836         } else {
837                 res->lr_parent = parent;
838                 list_add(&res->lr_childof, &parent->lr_children);
839         }
840         spin_unlock(&ns->ns_hash_lock);
841
842         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
843                 int rc;
844
845                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
846                 rc = ns->ns_lvbo->lvbo_init(res);
847                 if (rc)
848                         CERROR("lvbo_init failed for resource "
849                                LPU64": rc %d\n", name.name[0], rc);
850                 /* we create resource with locked lr_lvb_sem */
851                 up(&res->lr_lvb_sem);
852         }
853
854         RETURN(res);
855 }
856
857 /* Args: unlocked namespace
858  * Locks: takes and releases ns->ns_lock and res->lr_lock
859  * Returns: referenced, unlocked ldlm_resource or NULL */
860 struct ldlm_resource *
861 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
862                   struct ldlm_res_id name, ldlm_type_t type, int create)
863 {
864         __u32 hash = ldlm_hash_fn(parent, name);
865         struct ldlm_resource *res = NULL;
866         ENTRY;
867
868         LASSERT(ns != NULL);
869         LASSERT(ns->ns_hash != NULL);
870         LASSERT(name.name[0] != 0);
871
872         spin_lock(&ns->ns_hash_lock);
873         res = ldlm_resource_find(ns, name, hash);
874         if (res) {
875                 ldlm_resource_getref(res);
876                 spin_unlock(&ns->ns_hash_lock);
877                 /* synchronize WRT resource creation */
878                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
879                         down(&res->lr_lvb_sem);
880                         up(&res->lr_lvb_sem);
881                 }
882                 RETURN(res);
883         }
884         spin_unlock(&ns->ns_hash_lock);
885
886         if (create == 0)
887                 RETURN(NULL);
888
889         res = ldlm_resource_add(ns, parent, name, hash, type);
890         RETURN(res);
891 }
892
893 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
894 {
895         LASSERT(res != NULL);
896         LASSERT(res != LP_POISON);
897         atomic_inc(&res->lr_refcount);
898         CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
899                atomic_read(&res->lr_refcount));
900         return res;
901 }
902
903 void __ldlm_resource_putref_final(struct ldlm_resource *res)
904 {
905         struct ldlm_namespace *ns = res->lr_namespace;
906
907         LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
908
909         if (!list_empty(&res->lr_granted)) {
910                 ldlm_resource_dump(D_ERROR, res);
911                 LBUG();
912         }
913
914         if (!list_empty(&res->lr_converting)) {
915                 ldlm_resource_dump(D_ERROR, res);
916                 LBUG();
917         }
918
919         if (!list_empty(&res->lr_waiting)) {
920                 ldlm_resource_dump(D_ERROR, res);
921                 LBUG();
922         }
923
924         if (!list_empty(&res->lr_children)) {
925                 ldlm_resource_dump(D_ERROR, res);
926                 LBUG();
927         }
928
929         /* Pass 0 as second argument to not wake up ->ns_waitq yet, will do it
930          * later. */
931         ldlm_namespace_put_locked(ns, 0);
932         list_del_init(&res->lr_hash);
933         list_del_init(&res->lr_childof);
934
935         ns->ns_resources--;
936         if (ns->ns_resources == 0)
937                 wake_up(&ns->ns_waitq);
938 }
939
940 /* Returns 1 if the resource was freed, 0 if it remains. */
941 int ldlm_resource_putref(struct ldlm_resource *res)
942 {
943         struct ldlm_namespace *ns = res->lr_namespace;
944         int rc = 0;
945         ENTRY;
946
947         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
948                atomic_read(&res->lr_refcount) - 1);
949         LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
950                  atomic_read(&res->lr_refcount));
951         LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
952                  atomic_read(&res->lr_refcount));
953
954         if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
955                 __ldlm_resource_putref_final(res);
956                 spin_unlock(&ns->ns_hash_lock);
957                 if (res->lr_lvb_data)
958                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
959                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
960                 rc = 1;
961         }
962
963         RETURN(rc);
964 }
965
966 /* Returns 1 if the resource was freed, 0 if it remains. */
967 int ldlm_resource_putref_locked(struct ldlm_resource *res)
968 {
969         int rc = 0;
970         ENTRY;
971
972         CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
973                atomic_read(&res->lr_refcount) - 1);
974         LASSERT(atomic_read(&res->lr_refcount) > 0);
975         LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
976
977         LASSERT(atomic_read(&res->lr_refcount) >= 0);
978         if (atomic_dec_and_test(&res->lr_refcount)) {
979                 __ldlm_resource_putref_final(res);
980                 if (res->lr_lvb_data)
981                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
982                 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
983                 rc = 1;
984         }
985
986         RETURN(rc);
987 }
988
989 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
990                             struct ldlm_lock *lock)
991 {
992         check_res_locked(res);
993
994         ldlm_resource_dump(D_INFO, res);
995         CDEBUG(D_OTHER, "About to add this lock:\n");
996         ldlm_lock_dump(D_OTHER, lock, 0);
997
998         if (lock->l_destroyed) {
999                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1000                 return;
1001         }
1002
1003         LASSERT(list_empty(&lock->l_res_link));
1004
1005         list_add_tail(&lock->l_res_link, head);
1006 }
1007
1008 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1009                                      struct ldlm_lock *new)
1010 {
1011         struct ldlm_resource *res = original->l_resource;
1012
1013         check_res_locked(res);
1014
1015         ldlm_resource_dump(D_INFO, res);
1016         CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1017         ldlm_lock_dump(D_OTHER, new, 0);
1018
1019         if (new->l_destroyed) {
1020                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1021                 goto out;
1022         }
1023
1024         LASSERT(list_empty(&new->l_res_link));
1025
1026         list_add(&new->l_res_link, &original->l_res_link);
1027  out:;
1028 }
1029
1030 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1031 {
1032         int type = lock->l_resource->lr_type;
1033
1034         check_res_locked(lock->l_resource);
1035         if (type == LDLM_IBITS || type == LDLM_PLAIN)
1036                 ldlm_unlink_lock_skiplist(lock);
1037         else if (type == LDLM_EXTENT)
1038                 ldlm_extent_unlink_lock(lock);
1039         list_del_init(&lock->l_res_link);
1040 }
1041
1042 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1043 {
1044         desc->lr_type = res->lr_type;
1045         desc->lr_name = res->lr_name;
1046 }
1047
1048 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1049 {
1050         struct list_head *tmp;
1051
1052         if (!((libcfs_debug | D_ERROR) & level))
1053                 return;
1054
1055         mutex_down(ldlm_namespace_lock(client));
1056
1057         list_for_each(tmp, ldlm_namespace_list(client)) {
1058                 struct ldlm_namespace *ns;
1059                 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1060                 ldlm_namespace_dump(level, ns);
1061         }
1062
1063         mutex_up(ldlm_namespace_lock(client));
1064 }
1065
1066 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1067 {
1068         struct list_head *tmp;
1069
1070         if (!((libcfs_debug | D_ERROR) & level))
1071                 return;
1072
1073         CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1074                ns->ns_name, ns->ns_refcount,
1075                ns_is_client(ns) ? "client" : "server");
1076
1077         if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1078                 return;
1079
1080         spin_lock(&ns->ns_hash_lock);
1081         tmp = ns->ns_root_list.next;
1082         while (tmp != &ns->ns_root_list) {
1083                 struct ldlm_resource *res;
1084                 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1085
1086                 ldlm_resource_getref(res);
1087                 spin_unlock(&ns->ns_hash_lock);
1088
1089                 lock_res(res);
1090                 ldlm_resource_dump(level, res);
1091                 unlock_res(res);
1092
1093                 spin_lock(&ns->ns_hash_lock);
1094                 tmp = tmp->next;
1095                 ldlm_resource_putref_locked(res);
1096         }
1097         ns->ns_next_dump = cfs_time_shift(10);
1098         spin_unlock(&ns->ns_hash_lock);
1099 }
1100
1101 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1102 {
1103         struct list_head *tmp;
1104         int pos;
1105
1106         CLASSERT(RES_NAME_SIZE == 4);
1107
1108         if (!((libcfs_debug | D_ERROR) & level))
1109                 return;
1110
1111         CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1112                ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1113                res->lr_name.name[2], res->lr_name.name[3],
1114                atomic_read(&res->lr_refcount));
1115
1116         if (!list_empty(&res->lr_granted)) {
1117                 pos = 0;
1118                 CDEBUG(level, "Granted locks:\n");
1119                 list_for_each(tmp, &res->lr_granted) {
1120                         struct ldlm_lock *lock;
1121                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1122                         ldlm_lock_dump(level, lock, ++pos);
1123                 }
1124         }
1125         if (!list_empty(&res->lr_converting)) {
1126                 pos = 0;
1127                 CDEBUG(level, "Converting locks:\n");
1128                 list_for_each(tmp, &res->lr_converting) {
1129                         struct ldlm_lock *lock;
1130                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1131                         ldlm_lock_dump(level, lock, ++pos);
1132                 }
1133         }
1134         if (!list_empty(&res->lr_waiting)) {
1135                 pos = 0;
1136                 CDEBUG(level, "Waiting locks:\n");
1137                 list_for_each(tmp, &res->lr_waiting) {
1138                         struct ldlm_lock *lock;
1139                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1140                         ldlm_lock_dump(level, lock, ++pos);
1141                 }
1142         }
1143 }