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