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