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