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