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