1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_resource.c
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Peter Braam <braam@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
44 # include <lustre_dlm.h>
46 # include <liblustre.h>
49 #include <obd_class.h>
50 #include "ldlm_internal.h"
52 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
54 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
55 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
57 struct semaphore ldlm_srv_namespace_lock;
58 struct list_head ldlm_srv_namespace_list =
59 CFS_LIST_HEAD_INIT(ldlm_srv_namespace_list);
61 struct semaphore ldlm_cli_namespace_lock;
62 struct list_head ldlm_cli_namespace_list =
63 CFS_LIST_HEAD_INIT(ldlm_cli_namespace_list);
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;
69 extern unsigned int ldlm_cancel_unused_locks_before_replay;
72 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
73 unsigned long count, void *data)
75 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
76 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
80 int ldlm_proc_setup(void)
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 },
90 LASSERT(ldlm_ns_proc_dir == NULL);
92 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
95 if (IS_ERR(ldlm_type_proc_dir)) {
96 CERROR("LProcFS failed in ldlm-init\n");
97 rc = PTR_ERR(ldlm_type_proc_dir);
101 ldlm_ns_proc_dir = lprocfs_register("namespaces",
104 if (IS_ERR(ldlm_ns_proc_dir)) {
105 CERROR("LProcFS failed in ldlm-init\n");
106 rc = PTR_ERR(ldlm_ns_proc_dir);
110 ldlm_svc_proc_dir = lprocfs_register("services",
113 if (IS_ERR(ldlm_svc_proc_dir)) {
114 CERROR("LProcFS failed in ldlm-init\n");
115 rc = PTR_ERR(ldlm_svc_proc_dir);
119 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
124 lprocfs_remove(&ldlm_ns_proc_dir);
126 lprocfs_remove(&ldlm_type_proc_dir);
128 ldlm_svc_proc_dir = NULL;
132 void ldlm_proc_cleanup(void)
134 if (ldlm_svc_proc_dir)
135 lprocfs_remove(&ldlm_svc_proc_dir);
137 if (ldlm_ns_proc_dir)
138 lprocfs_remove(&ldlm_ns_proc_dir);
140 if (ldlm_type_proc_dir)
141 lprocfs_remove(&ldlm_type_proc_dir);
144 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
145 int count, int *eof, void *data)
147 struct ldlm_namespace *ns = data;
148 __u32 *nr = &ns->ns_max_unused;
150 if (ns_connect_lru_resize(ns))
151 nr = &ns->ns_nr_unused;
152 return lprocfs_rd_uint(page, start, off, count, eof, nr);
155 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
156 unsigned long count, void *data)
158 struct ldlm_namespace *ns = data;
159 char dummy[MAX_STRING_SIZE + 1] = { '\0' }, *end;
163 if (count >= sizeof(dummy) || count == 0)
166 if (copy_from_user(dummy, buffer, count))
169 if (strncmp(dummy, "clear", 5) == 0) {
171 "dropping all unused locks from namespace %s\n",
173 if (ns_connect_lru_resize(ns)) {
174 int canceled, unused = ns->ns_nr_unused;
176 /* Try to cancel all @ns_nr_unused locks. */
177 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
179 if (canceled < unused) {
180 CERROR("not all requested locks are canceled, "
181 "requested: %d, canceled: %d\n", unused,
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;
194 tmp = simple_strtoul(dummy, &end, 0);
196 CERROR("invalid value written\n");
199 lru_resize = (tmp == 0);
201 if (ns_connect_lru_resize(ns)) {
203 ns->ns_max_unused = (unsigned int)tmp;
205 if (tmp > ns->ns_nr_unused)
206 tmp = ns->ns_nr_unused;
207 tmp = ns->ns_nr_unused - tmp;
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);
216 "disable lru_resize for namespace %s\n",
218 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
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);
227 /* Make sure that originally lru resize was supported before
228 * turning it on here. */
230 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
232 "enable lru_resize for namespace %s\n",
234 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
240 void ldlm_proc_namespace(struct ldlm_namespace *ns)
242 struct lprocfs_vars lock_vars[2];
243 char lock_name[MAX_STRING_SIZE + 1];
246 LASSERT(ns->ns_name != NULL);
248 lock_name[MAX_STRING_SIZE] = '\0';
250 memset(lock_vars, 0, sizeof(lock_vars));
251 lock_vars[0].name = lock_name;
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);
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);
263 if (ns_is_client(ns)) {
264 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
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);
270 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
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);
277 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
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);
284 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
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);
290 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
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);
297 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
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);
304 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
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);
312 #undef MAX_STRING_SIZE
314 #define ldlm_proc_namespace(ns) do {} while (0)
317 struct ldlm_namespace *
318 ldlm_namespace_new(struct obd_device *obd, char *name,
319 ldlm_side_t client, ldlm_appetite_t apt)
321 struct ldlm_namespace *ns = NULL;
322 struct list_head *bucket;
323 int rc, idx, namelen;
328 CERROR("ldlm_get_ref failed: %d\n", rc);
336 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
340 namelen = strlen(name);
341 OBD_ALLOC(ns->ns_name, namelen + 1);
343 GOTO(out_hash, NULL);
345 ns->ns_appetite = apt;
347 LASSERT(obd != NULL);
350 strcpy(ns->ns_name, name);
352 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
353 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
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;
364 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
366 CFS_INIT_LIST_HEAD(bucket);
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;
374 spin_lock_init(&ns->ns_unused_lock);
375 ns->ns_orig_connect_flags = 0;
376 ns->ns_connect_flags = 0;
378 ldlm_proc_namespace(ns);
380 idx = atomic_read(ldlm_namespace_nr(client));
382 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
384 CERROR("Can't initialize lock pool, rc %d\n", rc);
388 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
389 ldlm_namespace_register(ns, client);
392 ldlm_namespace_cleanup(ns, 0);
393 OBD_FREE(ns->ns_name, namelen + 1);
395 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
403 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
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
409 * Called with the ns_lock held. */
410 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
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);
419 struct ldlm_lock *lock = NULL;
421 /* first, we look for non-cleaned-yet lock
422 * all cleaned locks are marked by CLEANED flag */
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) {
431 lock->l_flags |= LDLM_FL_CLEANED;
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;
446 /* ... without sending a CANCEL message for local_only. */
448 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
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 ... */
456 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
457 if (lock->l_completion_ast)
458 lock->l_completion_ast(lock, 0, NULL);
464 struct lustre_handle lockh;
467 ldlm_lock2handle(lock, &lockh);
468 rc = ldlm_cli_cancel(&lockh);
470 CERROR("ldlm_cli_cancel: %d\n", rc);
472 ldlm_resource_unlink_lock(lock);
474 LDLM_DEBUG(lock, "Freeing a lock still held by a "
476 ldlm_lock_destroy(lock);
484 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
486 struct list_head *tmp;
490 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
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);
503 cleanup_resource(res, &res->lr_granted, flags);
504 cleanup_resource(res, &res->lr_converting, flags);
505 cleanup_resource(res, &res->lr_waiting, flags);
507 spin_lock(&ns->ns_hash_lock);
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
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));
526 spin_unlock(&ns->ns_hash_lock);
532 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
535 /* At shutdown time, don't call the cancellation callback */
536 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
538 if (ns->ns_refcount > 0) {
539 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
542 "dlm namespace %s free waiting on refcount %d\n",
543 ns->ns_name, ns->ns_refcount);
546 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
548 rc = l_wait_event(ns->ns_waitq,
549 ns->ns_refcount == 0, &lwi);
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);
561 if (ns->ns_refcount) {
562 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
563 "with %d resources in use, (rc=%d)\n",
565 ns->ns_refcount, rc);
566 RETURN(ELDLM_NAMESPACE_EXISTS);
569 "dlm namespace %s free done waiting\n", ns->ns_name);
575 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
576 struct obd_import *imp,
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) {
591 ptlrpc_disconnect_import(imp, 0);
592 ptlrpc_invalidate_import(imp);
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);
603 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
611 /* Make sure that nobody can find this ns in its list. */
612 ldlm_namespace_unregister(ns, ns->ns_client);
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);
621 struct proc_dir_entry *dir;
622 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
624 CERROR("dlm namespace %s has no procfs dir?\n",
627 lprocfs_remove(&dir);
631 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
632 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
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));
642 /* Cleanup the resource, and free namespace.
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).
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
659 void ldlm_namespace_free(struct ldlm_namespace *ns,
660 struct obd_import *imp,
663 ldlm_namespace_free_prior(ns, imp, force);
664 ldlm_namespace_free_post(ns);
667 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
672 void ldlm_namespace_get(struct ldlm_namespace *ns)
674 spin_lock(&ns->ns_hash_lock);
675 ldlm_namespace_get_locked(ns);
676 spin_unlock(&ns->ns_hash_lock);
679 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
681 LASSERT(ns->ns_refcount > 0);
683 if (ns->ns_refcount == 0 && wakeup)
684 wake_up(&ns->ns_waitq);
687 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
689 spin_lock(&ns->ns_hash_lock);
690 ldlm_namespace_put_locked(ns, wakeup);
691 spin_unlock(&ns->ns_hash_lock);
694 /* Register @ns in the list of namespaces */
695 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
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));
704 /* Unregister @ns from the list of namespaces */
705 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
707 mutex_down(ldlm_namespace_lock(client));
708 LASSERT(!list_empty(&ns->ns_list_chain));
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.
714 list_del_init(&ns->ns_list_chain);
715 atomic_dec(ldlm_namespace_nr(client));
716 mutex_up(ldlm_namespace_lock(client));
719 /* Should be called under ldlm_namespace_lock(client) taken */
720 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
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));
727 /* Should be called under ldlm_namespace_lock(client) taken */
728 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
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);
736 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
741 for (i = 0; i < RES_NAME_SIZE; i++)
742 hash += name.name[i];
744 hash += (__u32)((unsigned long)parent >> 4);
746 return (hash & RES_HASH_MASK);
749 static struct ldlm_resource *ldlm_resource_new(void)
751 struct ldlm_resource *res;
754 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
758 memset(res, 0, sizeof(*res));
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);
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;
773 atomic_set(&res->lr_refcount, 1);
774 spin_lock_init(&res->lr_lock);
776 /* one who creates the resource must unlock
777 * the semaphore after lvb initialization */
778 init_MUTEX_LOCKED(&res->lr_lvb_sem);
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)
787 struct list_head *bucket, *tmp;
788 struct ldlm_resource *res;
790 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
791 bucket = ns->ns_hash + hash;
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)
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)
808 struct list_head *bucket;
809 struct ldlm_resource *res, *old_res;
812 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
815 res = ldlm_resource_new();
820 res->lr_namespace = ns;
822 res->lr_most_restr = LCK_NL;
824 spin_lock(&ns->ns_hash_lock);
825 old_res = ldlm_resource_find(ns, name, hash);
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);
839 /* we won! let's add the resource */
840 bucket = ns->ns_hash + hash;
841 list_add(&res->lr_hash, bucket);
843 ldlm_namespace_get_locked(ns);
845 if (parent == NULL) {
846 list_add(&res->lr_childof, &ns->ns_root_list);
848 res->lr_parent = parent;
849 list_add(&res->lr_childof, &parent->lr_children);
851 spin_unlock(&ns->ns_hash_lock);
853 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
856 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
857 rc = ns->ns_lvbo->lvbo_init(res);
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);
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)
875 __u32 hash = ldlm_hash_fn(parent, name);
876 struct ldlm_resource *res = NULL;
880 LASSERT(ns->ns_hash != NULL);
881 LASSERT(name.name[0] != 0);
883 spin_lock(&ns->ns_hash_lock);
884 res = ldlm_resource_find(ns, name, hash);
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);
895 spin_unlock(&ns->ns_hash_lock);
900 res = ldlm_resource_add(ns, parent, name, hash, type);
904 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
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));
914 void __ldlm_resource_putref_final(struct ldlm_resource *res)
916 struct ldlm_namespace *ns = res->lr_namespace;
918 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
920 if (!list_empty(&res->lr_granted)) {
921 ldlm_resource_dump(D_ERROR, res);
925 if (!list_empty(&res->lr_converting)) {
926 ldlm_resource_dump(D_ERROR, res);
930 if (!list_empty(&res->lr_waiting)) {
931 ldlm_resource_dump(D_ERROR, res);
935 if (!list_empty(&res->lr_children)) {
936 ldlm_resource_dump(D_ERROR, res);
940 /* Pass 0 as second argument to not wake up ->ns_waitq yet, will do it
942 ldlm_namespace_put_locked(ns, 0);
943 list_del_init(&res->lr_hash);
944 list_del_init(&res->lr_childof);
947 if (ns->ns_resources == 0)
948 wake_up(&ns->ns_waitq);
951 /* Returns 1 if the resource was freed, 0 if it remains. */
952 int ldlm_resource_putref(struct ldlm_resource *res)
954 struct ldlm_namespace *ns = res->lr_namespace;
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));
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);
977 /* Returns 1 if the resource was freed, 0 if it remains. */
978 int ldlm_resource_putref_locked(struct ldlm_resource *res)
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);
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);
1000 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1001 struct ldlm_lock *lock)
1003 check_res_locked(res);
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);
1009 if (lock->l_destroyed) {
1010 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1014 LASSERT(list_empty(&lock->l_res_link));
1016 list_add_tail(&lock->l_res_link, head);
1019 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1020 struct ldlm_lock *new)
1022 struct ldlm_resource *res = original->l_resource;
1024 check_res_locked(res);
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);
1030 if (new->l_destroyed) {
1031 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1035 LASSERT(list_empty(&new->l_res_link));
1037 list_add(&new->l_res_link, &original->l_res_link);
1041 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1043 int type = lock->l_resource->lr_type;
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);
1053 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1055 desc->lr_type = res->lr_type;
1056 desc->lr_name = res->lr_name;
1059 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1061 struct list_head *tmp;
1063 if (!((libcfs_debug | D_ERROR) & level))
1066 mutex_down(ldlm_namespace_lock(client));
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);
1074 mutex_up(ldlm_namespace_lock(client));
1077 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1079 struct list_head *tmp;
1081 if (!((libcfs_debug | D_ERROR) & level))
1084 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1085 ns->ns_name, ns->ns_refcount,
1086 ns_is_client(ns) ? "client" : "server");
1088 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
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);
1097 ldlm_resource_getref(res);
1098 spin_unlock(&ns->ns_hash_lock);
1101 ldlm_resource_dump(level, res);
1104 spin_lock(&ns->ns_hash_lock);
1106 ldlm_resource_putref_locked(res);
1108 ns->ns_next_dump = cfs_time_shift(10);
1109 spin_unlock(&ns->ns_hash_lock);
1112 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1114 struct list_head *tmp;
1117 CLASSERT(RES_NAME_SIZE == 4);
1119 if (!((libcfs_debug | D_ERROR) & level))
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));
1127 if (!list_empty(&res->lr_granted)) {
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);
1136 if (!list_empty(&res->lr_converting)) {
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);
1145 if (!list_empty(&res->lr_waiting)) {
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);