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;
70 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
71 unsigned long count, void *data)
73 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
74 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
78 int ldlm_proc_setup(void)
81 struct lprocfs_vars list[] = {
82 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
85 LASSERT(ldlm_ns_proc_dir == NULL);
87 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
90 if (IS_ERR(ldlm_type_proc_dir)) {
91 CERROR("LProcFS failed in ldlm-init\n");
92 rc = PTR_ERR(ldlm_type_proc_dir);
96 ldlm_ns_proc_dir = lprocfs_register("namespaces",
99 if (IS_ERR(ldlm_ns_proc_dir)) {
100 CERROR("LProcFS failed in ldlm-init\n");
101 rc = PTR_ERR(ldlm_ns_proc_dir);
105 ldlm_svc_proc_dir = lprocfs_register("services",
108 if (IS_ERR(ldlm_svc_proc_dir)) {
109 CERROR("LProcFS failed in ldlm-init\n");
110 rc = PTR_ERR(ldlm_svc_proc_dir);
114 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
119 lprocfs_remove(&ldlm_ns_proc_dir);
121 lprocfs_remove(&ldlm_type_proc_dir);
123 ldlm_svc_proc_dir = NULL;
127 void ldlm_proc_cleanup(void)
129 if (ldlm_svc_proc_dir)
130 lprocfs_remove(&ldlm_svc_proc_dir);
132 if (ldlm_ns_proc_dir)
133 lprocfs_remove(&ldlm_ns_proc_dir);
135 if (ldlm_type_proc_dir)
136 lprocfs_remove(&ldlm_type_proc_dir);
139 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
140 int count, int *eof, void *data)
142 struct ldlm_namespace *ns = data;
143 __u32 *nr = &ns->ns_max_unused;
145 if (ns_connect_lru_resize(ns))
146 nr = &ns->ns_nr_unused;
147 return lprocfs_rd_uint(page, start, off, count, eof, nr);
150 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
151 unsigned long count, void *data)
153 struct ldlm_namespace *ns = data;
154 char dummy[MAX_STRING_SIZE + 1] = { '\0' }, *end;
158 if (count >= sizeof(dummy) || count == 0)
161 if (copy_from_user(dummy, buffer, count))
164 if (strncmp(dummy, "clear", 5) == 0) {
166 "dropping all unused locks from namespace %s\n",
168 if (ns_connect_lru_resize(ns)) {
169 int canceled, unused = ns->ns_nr_unused;
171 /* Try to cancel all @ns_nr_unused locks. */
172 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
174 if (canceled < unused) {
175 CERROR("not all requested locks are canceled, "
176 "requested: %d, canceled: %d\n", unused,
181 tmp = ns->ns_max_unused;
182 ns->ns_max_unused = 0;
183 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
184 ns->ns_max_unused = tmp;
189 tmp = simple_strtoul(dummy, &end, 0);
191 CERROR("invalid value written\n");
194 lru_resize = (tmp == 0);
196 if (ns_connect_lru_resize(ns)) {
198 ns->ns_max_unused = (unsigned int)tmp;
200 if (tmp > ns->ns_nr_unused)
201 tmp = ns->ns_nr_unused;
202 tmp = ns->ns_nr_unused - tmp;
205 "changing namespace %s unused locks from %u to %u\n",
206 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
207 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
211 "disable lru_resize for namespace %s\n",
213 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
217 "changing namespace %s max_unused from %u to %u\n",
218 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
219 ns->ns_max_unused = (unsigned int)tmp;
220 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
222 /* Make sure that originally lru resize was supported before
223 * turning it on here. */
225 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
227 "enable lru_resize for namespace %s\n",
229 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
235 void ldlm_proc_namespace(struct ldlm_namespace *ns)
237 struct lprocfs_vars lock_vars[2];
238 char lock_name[MAX_STRING_SIZE + 1];
241 LASSERT(ns->ns_name != NULL);
243 lock_name[MAX_STRING_SIZE] = '\0';
245 memset(lock_vars, 0, sizeof(lock_vars));
246 lock_vars[0].name = lock_name;
248 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
249 lock_vars[0].data = &ns->ns_refcount;
250 lock_vars[0].read_fptr = lprocfs_rd_atomic;
251 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
253 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
254 lock_vars[0].data = &ns->ns_locks;
255 lock_vars[0].read_fptr = lprocfs_rd_atomic;
256 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
258 if (ns_is_client(ns)) {
259 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
261 lock_vars[0].data = &ns->ns_nr_unused;
262 lock_vars[0].read_fptr = lprocfs_rd_uint;
263 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
265 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
267 lock_vars[0].data = ns;
268 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
269 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
270 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
272 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
274 lock_vars[0].data = &ns->ns_max_age;
275 lock_vars[0].read_fptr = lprocfs_rd_uint;
276 lock_vars[0].write_fptr = lprocfs_wr_uint;
277 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
279 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
281 lock_vars[0].data = &ns->ns_timeouts;
282 lock_vars[0].read_fptr = lprocfs_rd_uint;
283 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
285 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
287 lock_vars[0].data = &ns->ns_max_nolock_size;
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);
292 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
294 lock_vars[0].data = &ns->ns_contention_time;
295 lock_vars[0].read_fptr = lprocfs_rd_uint;
296 lock_vars[0].write_fptr = lprocfs_wr_uint;
297 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
299 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
301 lock_vars[0].data = &ns->ns_contended_locks;
302 lock_vars[0].read_fptr = lprocfs_rd_uint;
303 lock_vars[0].write_fptr = lprocfs_wr_uint;
304 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
307 #undef MAX_STRING_SIZE
309 #define ldlm_proc_namespace(ns) do {} while (0)
312 struct ldlm_namespace *
313 ldlm_namespace_new(struct obd_device *obd, char *name,
314 ldlm_side_t client, ldlm_appetite_t apt)
316 struct ldlm_namespace *ns = NULL;
317 struct list_head *bucket;
318 int rc, idx, namelen;
323 CERROR("ldlm_get_ref failed: %d\n", rc);
331 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
335 namelen = strlen(name);
336 OBD_ALLOC(ns->ns_name, namelen + 1);
338 GOTO(out_hash, NULL);
340 ns->ns_appetite = apt;
342 LASSERT(obd != NULL);
345 strcpy(ns->ns_name, name);
347 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
348 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
350 ns->ns_client = client;
351 spin_lock_init(&ns->ns_hash_lock);
352 atomic_set(&ns->ns_locks, 0);
353 ns->ns_resources = 0;
354 cfs_waitq_init(&ns->ns_waitq);
355 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
356 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
357 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
359 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
361 CFS_INIT_LIST_HEAD(bucket);
363 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
364 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
365 ns->ns_nr_unused = 0;
366 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
367 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
369 spin_lock_init(&ns->ns_unused_lock);
370 ns->ns_orig_connect_flags = 0;
371 ns->ns_connect_flags = 0;
373 ldlm_proc_namespace(ns);
375 idx = atomic_read(ldlm_namespace_nr(client));
377 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
379 CERROR("Can't initialize lock pool, rc %d\n", rc);
383 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
384 ldlm_namespace_register(ns, client);
387 ldlm_namespace_cleanup(ns, 0);
388 OBD_FREE(ns->ns_name, namelen + 1);
390 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
398 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
400 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
401 * This is currently only used for recovery, and we make certain assumptions
402 * as a result--notably, that we shouldn't cancel locks with refs. -phil
404 * Called with the ns_lock held. */
405 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
408 struct list_head *tmp;
409 int rc = 0, client = ns_is_client(res->lr_namespace);
410 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
414 struct ldlm_lock *lock = NULL;
416 /* first, we look for non-cleaned-yet lock
417 * all cleaned locks are marked by CLEANED flag */
419 list_for_each(tmp, q) {
420 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
421 if (lock->l_flags & LDLM_FL_CLEANED) {
426 lock->l_flags |= LDLM_FL_CLEANED;
435 /* Set CBPENDING so nothing in the cancellation path
436 * can match this lock */
437 lock->l_flags |= LDLM_FL_CBPENDING;
438 lock->l_flags |= LDLM_FL_FAILED;
439 lock->l_flags |= flags;
441 /* ... without sending a CANCEL message for local_only. */
443 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
445 if (local_only && (lock->l_readers || lock->l_writers)) {
446 /* This is a little bit gross, but much better than the
447 * alternative: pretend that we got a blocking AST from
448 * the server, so that when the lock is decref'd, it
449 * will go away ... */
451 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
452 if (lock->l_completion_ast)
453 lock->l_completion_ast(lock, 0, NULL);
459 struct lustre_handle lockh;
462 ldlm_lock2handle(lock, &lockh);
463 rc = ldlm_cli_cancel(&lockh);
465 CERROR("ldlm_cli_cancel: %d\n", rc);
467 ldlm_resource_unlink_lock(lock);
469 LDLM_DEBUG(lock, "Freeing a lock still held by a "
471 ldlm_lock_destroy(lock);
479 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
481 struct list_head *tmp;
485 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
489 for (i = 0; i < RES_HASH_SIZE; i++) {
490 spin_lock(&ns->ns_hash_lock);
491 tmp = ns->ns_hash[i].next;
492 while (tmp != &(ns->ns_hash[i])) {
493 struct ldlm_resource *res;
494 res = list_entry(tmp, struct ldlm_resource, lr_hash);
495 ldlm_resource_getref(res);
496 spin_unlock(&ns->ns_hash_lock);
498 cleanup_resource(res, &res->lr_granted, flags);
499 cleanup_resource(res, &res->lr_converting, flags);
500 cleanup_resource(res, &res->lr_waiting, flags);
502 spin_lock(&ns->ns_hash_lock);
505 /* XXX: former stuff caused issues in case of race
506 * between ldlm_namespace_cleanup() and lockd() when
507 * client gets blocking ast when lock gets distracted by
508 * server. This is 1_4 branch solution, let's see how
510 if (!ldlm_resource_putref_locked(res))
512 "Namespace %s resource refcount nonzero "
513 "(%d) after lock cleanup; forcing cleanup.\n",
514 ns->ns_name, atomic_read(&res->lr_refcount));
516 spin_unlock(&ns->ns_hash_lock);
522 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
525 /* At shutdown time, don't call the cancellation callback */
526 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
528 if (ns->ns_refcount > 0) {
529 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
532 "dlm namespace %s free waiting on refcount %d\n",
533 ns->ns_name, ns->ns_refcount);
536 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
538 rc = l_wait_event(ns->ns_waitq,
539 ns->ns_refcount == 0, &lwi);
541 /* Forced cleanups should be able to reclaim all references,
542 * so it's safe to wait forever... we can't leak locks... */
543 if (force && rc == -ETIMEDOUT) {
544 LCONSOLE_ERROR("Forced cleanup waiting for %s "
545 "namespace with %d resources in use, "
546 "(rc=%d)\n", ns->ns_name,
547 ns->ns_refcount, rc);
548 GOTO(force_wait, rc);
551 if (ns->ns_refcount) {
552 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
553 "with %d resources in use, (rc=%d)\n",
555 ns->ns_refcount, rc);
556 RETURN(ELDLM_NAMESPACE_EXISTS);
559 "dlm namespace %s free done waiting\n", ns->ns_name);
565 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
566 struct obd_import *imp,
576 /* Make sure that nobody can find this ns in its list. */
577 ldlm_namespace_unregister(ns, ns->ns_client);
579 /* Can fail with -EINTR when force == 0 in which case try harder */
580 rc = __ldlm_namespace_free(ns, force);
581 if (rc != ELDLM_OK) {
583 ptlrpc_disconnect_import(imp, 0);
584 ptlrpc_invalidate_import(imp);
587 /* With all requests dropped and the import inactive
588 * we are gaurenteed all reference will be dropped. */
589 rc = __ldlm_namespace_free(ns, 1);
595 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
603 /* Fini pool _before_ parent proc dir is removed. This is important
604 * as ldlm_pool_fini() removes own proc dir which is child to @dir.
605 * Removing it after @dir may cause oops. */
606 ldlm_pool_fini(&ns->ns_pool);
610 struct proc_dir_entry *dir;
611 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
613 CERROR("dlm namespace %s has no procfs dir?\n",
616 lprocfs_remove(&dir);
620 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
621 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
623 /* @ns should be not on list in this time, otherwise this will cause
624 * issues realted to using freed @ns in pools thread. */
625 LASSERT(list_empty(&ns->ns_list_chain));
631 /* Cleanup the resource, and free namespace.
634 * proc1: destroy import
635 * class_disconnect_export(grab cl_sem) ->
636 * -> ldlm_namespace_free ->
637 * -> lprocfs_remove(grab _lprocfs_lock).
638 * proc2: read proc info
639 * lprocfs_fops_read(grab _lprocfs_lock) ->
640 * -> osc_rd_active, etc(grab cl_sem).
642 * So that I have to split the ldlm_namespace_free into two parts - the first
643 * part ldlm_namespace_free_prior is used to cleanup the resource which is
644 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
645 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
648 void ldlm_namespace_free(struct ldlm_namespace *ns,
649 struct obd_import *imp,
652 ldlm_namespace_free_prior(ns, imp, force);
653 ldlm_namespace_free_post(ns);
656 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
661 void ldlm_namespace_get(struct ldlm_namespace *ns)
663 spin_lock(&ns->ns_hash_lock);
664 ldlm_namespace_get_locked(ns);
665 spin_unlock(&ns->ns_hash_lock);
668 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
670 LASSERT(ns->ns_refcount > 0);
672 if (ns->ns_refcount == 0 && wakeup)
673 wake_up(&ns->ns_waitq);
676 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
678 spin_lock(&ns->ns_hash_lock);
679 ldlm_namespace_put_locked(ns, wakeup);
680 spin_unlock(&ns->ns_hash_lock);
683 /* Register @ns in the list of namespaces */
684 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
686 mutex_down(ldlm_namespace_lock(client));
687 LASSERT(list_empty(&ns->ns_list_chain));
688 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
689 atomic_inc(ldlm_namespace_nr(client));
690 mutex_up(ldlm_namespace_lock(client));
693 /* Unregister @ns from the list of namespaces */
694 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
696 mutex_down(ldlm_namespace_lock(client));
697 LASSERT(!list_empty(&ns->ns_list_chain));
699 * Some asserts and possibly other parts of code still using
700 * list_empty(&ns->ns_list_chain). This is why it is important
701 * to use list_del_init() here.
703 list_del_init(&ns->ns_list_chain);
704 atomic_dec(ldlm_namespace_nr(client));
705 mutex_up(ldlm_namespace_lock(client));
708 /* Should be called under ldlm_namespace_lock(client) taken */
709 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
711 LASSERT(!list_empty(&ns->ns_list_chain));
712 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
713 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
716 /* Should be called under ldlm_namespace_lock(client) taken */
717 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
719 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
720 LASSERT(!list_empty(ldlm_namespace_list(client)));
721 return container_of(ldlm_namespace_list(client)->next,
722 struct ldlm_namespace, ns_list_chain);
725 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
730 for (i = 0; i < RES_NAME_SIZE; i++)
731 hash += name.name[i];
733 hash += (__u32)((unsigned long)parent >> 4);
735 return (hash & RES_HASH_MASK);
738 static struct ldlm_resource *ldlm_resource_new(void)
740 struct ldlm_resource *res;
743 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
747 memset(res, 0, sizeof(*res));
749 CFS_INIT_LIST_HEAD(&res->lr_children);
750 CFS_INIT_LIST_HEAD(&res->lr_childof);
751 CFS_INIT_LIST_HEAD(&res->lr_granted);
752 CFS_INIT_LIST_HEAD(&res->lr_converting);
753 CFS_INIT_LIST_HEAD(&res->lr_waiting);
755 /* initialize interval trees for each lock mode*/
756 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
757 res->lr_itree[idx].lit_size = 0;
758 res->lr_itree[idx].lit_mode = 1 << idx;
759 res->lr_itree[idx].lit_root = NULL;
762 atomic_set(&res->lr_refcount, 1);
763 spin_lock_init(&res->lr_lock);
765 /* one who creates the resource must unlock
766 * the semaphore after lvb initialization */
767 init_MUTEX_LOCKED(&res->lr_lvb_sem);
772 /* must be called with hash lock held */
773 static struct ldlm_resource *
774 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
776 struct list_head *bucket, *tmp;
777 struct ldlm_resource *res;
779 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
780 bucket = ns->ns_hash + hash;
782 list_for_each(tmp, bucket) {
783 res = list_entry(tmp, struct ldlm_resource, lr_hash);
784 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
791 /* Args: locked namespace
792 * Returns: newly-allocated, referenced, unlocked resource */
793 static struct ldlm_resource *
794 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
795 struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
797 struct list_head *bucket;
798 struct ldlm_resource *res, *old_res;
801 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
804 res = ldlm_resource_new();
809 res->lr_namespace = ns;
811 res->lr_most_restr = LCK_NL;
813 spin_lock(&ns->ns_hash_lock);
814 old_res = ldlm_resource_find(ns, name, hash);
816 /* someone won the race and added the resource before */
817 ldlm_resource_getref(old_res);
818 spin_unlock(&ns->ns_hash_lock);
819 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
820 /* synchronize WRT resource creation */
821 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
822 down(&old_res->lr_lvb_sem);
823 up(&old_res->lr_lvb_sem);
828 /* we won! let's add the resource */
829 bucket = ns->ns_hash + hash;
830 list_add(&res->lr_hash, bucket);
832 ldlm_namespace_get_locked(ns);
834 if (parent == NULL) {
835 list_add(&res->lr_childof, &ns->ns_root_list);
837 res->lr_parent = parent;
838 list_add(&res->lr_childof, &parent->lr_children);
840 spin_unlock(&ns->ns_hash_lock);
842 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
845 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
846 rc = ns->ns_lvbo->lvbo_init(res);
848 CERROR("lvbo_init failed for resource "
849 LPU64": rc %d\n", name.name[0], rc);
850 /* we create resource with locked lr_lvb_sem */
851 up(&res->lr_lvb_sem);
857 /* Args: unlocked namespace
858 * Locks: takes and releases ns->ns_lock and res->lr_lock
859 * Returns: referenced, unlocked ldlm_resource or NULL */
860 struct ldlm_resource *
861 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
862 struct ldlm_res_id name, ldlm_type_t type, int create)
864 __u32 hash = ldlm_hash_fn(parent, name);
865 struct ldlm_resource *res = NULL;
869 LASSERT(ns->ns_hash != NULL);
870 LASSERT(name.name[0] != 0);
872 spin_lock(&ns->ns_hash_lock);
873 res = ldlm_resource_find(ns, name, hash);
875 ldlm_resource_getref(res);
876 spin_unlock(&ns->ns_hash_lock);
877 /* synchronize WRT resource creation */
878 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
879 down(&res->lr_lvb_sem);
880 up(&res->lr_lvb_sem);
884 spin_unlock(&ns->ns_hash_lock);
889 res = ldlm_resource_add(ns, parent, name, hash, type);
893 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
895 LASSERT(res != NULL);
896 LASSERT(res != LP_POISON);
897 atomic_inc(&res->lr_refcount);
898 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
899 atomic_read(&res->lr_refcount));
903 void __ldlm_resource_putref_final(struct ldlm_resource *res)
905 struct ldlm_namespace *ns = res->lr_namespace;
907 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
909 if (!list_empty(&res->lr_granted)) {
910 ldlm_resource_dump(D_ERROR, res);
914 if (!list_empty(&res->lr_converting)) {
915 ldlm_resource_dump(D_ERROR, res);
919 if (!list_empty(&res->lr_waiting)) {
920 ldlm_resource_dump(D_ERROR, res);
924 if (!list_empty(&res->lr_children)) {
925 ldlm_resource_dump(D_ERROR, res);
929 /* Pass 0 as second argument to not wake up ->ns_waitq yet, will do it
931 ldlm_namespace_put_locked(ns, 0);
932 list_del_init(&res->lr_hash);
933 list_del_init(&res->lr_childof);
936 if (ns->ns_resources == 0)
937 wake_up(&ns->ns_waitq);
940 /* Returns 1 if the resource was freed, 0 if it remains. */
941 int ldlm_resource_putref(struct ldlm_resource *res)
943 struct ldlm_namespace *ns = res->lr_namespace;
947 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
948 atomic_read(&res->lr_refcount) - 1);
949 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
950 atomic_read(&res->lr_refcount));
951 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
952 atomic_read(&res->lr_refcount));
954 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
955 __ldlm_resource_putref_final(res);
956 spin_unlock(&ns->ns_hash_lock);
957 if (res->lr_lvb_data)
958 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
959 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
966 /* Returns 1 if the resource was freed, 0 if it remains. */
967 int ldlm_resource_putref_locked(struct ldlm_resource *res)
972 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
973 atomic_read(&res->lr_refcount) - 1);
974 LASSERT(atomic_read(&res->lr_refcount) > 0);
975 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
977 LASSERT(atomic_read(&res->lr_refcount) >= 0);
978 if (atomic_dec_and_test(&res->lr_refcount)) {
979 __ldlm_resource_putref_final(res);
980 if (res->lr_lvb_data)
981 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
982 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
989 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
990 struct ldlm_lock *lock)
992 check_res_locked(res);
994 ldlm_resource_dump(D_INFO, res);
995 CDEBUG(D_OTHER, "About to add this lock:\n");
996 ldlm_lock_dump(D_OTHER, lock, 0);
998 if (lock->l_destroyed) {
999 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1003 LASSERT(list_empty(&lock->l_res_link));
1005 list_add_tail(&lock->l_res_link, head);
1008 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1009 struct ldlm_lock *new)
1011 struct ldlm_resource *res = original->l_resource;
1013 check_res_locked(res);
1015 ldlm_resource_dump(D_INFO, res);
1016 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1017 ldlm_lock_dump(D_OTHER, new, 0);
1019 if (new->l_destroyed) {
1020 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1024 LASSERT(list_empty(&new->l_res_link));
1026 list_add(&new->l_res_link, &original->l_res_link);
1030 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1032 int type = lock->l_resource->lr_type;
1034 check_res_locked(lock->l_resource);
1035 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1036 ldlm_unlink_lock_skiplist(lock);
1037 else if (type == LDLM_EXTENT)
1038 ldlm_extent_unlink_lock(lock);
1039 list_del_init(&lock->l_res_link);
1042 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1044 desc->lr_type = res->lr_type;
1045 desc->lr_name = res->lr_name;
1048 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1050 struct list_head *tmp;
1052 if (!((libcfs_debug | D_ERROR) & level))
1055 mutex_down(ldlm_namespace_lock(client));
1057 list_for_each(tmp, ldlm_namespace_list(client)) {
1058 struct ldlm_namespace *ns;
1059 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1060 ldlm_namespace_dump(level, ns);
1063 mutex_up(ldlm_namespace_lock(client));
1066 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1068 struct list_head *tmp;
1070 if (!((libcfs_debug | D_ERROR) & level))
1073 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1074 ns->ns_name, ns->ns_refcount,
1075 ns_is_client(ns) ? "client" : "server");
1077 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1080 spin_lock(&ns->ns_hash_lock);
1081 tmp = ns->ns_root_list.next;
1082 while (tmp != &ns->ns_root_list) {
1083 struct ldlm_resource *res;
1084 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1086 ldlm_resource_getref(res);
1087 spin_unlock(&ns->ns_hash_lock);
1090 ldlm_resource_dump(level, res);
1093 spin_lock(&ns->ns_hash_lock);
1095 ldlm_resource_putref_locked(res);
1097 ns->ns_next_dump = cfs_time_shift(10);
1098 spin_unlock(&ns->ns_hash_lock);
1101 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1103 struct list_head *tmp;
1106 CLASSERT(RES_NAME_SIZE == 4);
1108 if (!((libcfs_debug | D_ERROR) & level))
1111 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1112 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1113 res->lr_name.name[2], res->lr_name.name[3],
1114 atomic_read(&res->lr_refcount));
1116 if (!list_empty(&res->lr_granted)) {
1118 CDEBUG(level, "Granted locks:\n");
1119 list_for_each(tmp, &res->lr_granted) {
1120 struct ldlm_lock *lock;
1121 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1122 ldlm_lock_dump(level, lock, ++pos);
1125 if (!list_empty(&res->lr_converting)) {
1127 CDEBUG(level, "Converting locks:\n");
1128 list_for_each(tmp, &res->lr_converting) {
1129 struct ldlm_lock *lock;
1130 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1131 ldlm_lock_dump(level, lock, ++pos);
1134 if (!list_empty(&res->lr_waiting)) {
1136 CDEBUG(level, "Waiting locks:\n");
1137 list_for_each(tmp, &res->lr_waiting) {
1138 struct ldlm_lock *lock;
1139 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1140 ldlm_lock_dump(level, lock, ++pos);