/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
- * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
- * Author: Peter Braam <braam@clusterfs.com>
- * Author: Phil Schwan <phil@clusterfs.com>
+ * GPL HEADER START
*
- * This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Lustre is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
*
- * Lustre is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
*
- * You should have received a copy of the GNU General Public License
- * along with Lustre; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/ldlm/ldlm_lock.c
+ *
+ * Author: Peter Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
*/
#define DEBUG_SUBSYSTEM S_LDLM
#ifdef __KERNEL__
-# include <linux/slab.h>
-# include <linux/module.h>
-# include <linux/lustre_dlm.h>
+# include <libcfs/libcfs.h>
+# ifndef HAVE_VFS_INTENT_PATCHES
+# include <linux/lustre_intent.h>
+# endif
#else
# include <liblustre.h>
-# include <linux/kp30.h>
#endif
-#include <linux/obd_class.h>
+#include <obd_class.h>
#include "ldlm_internal.h"
-//struct lustre_lock ldlm_everything_lock;
-
/* lock types */
char *ldlm_lockname[] = {
[0] "--",
[LCK_PR] "PR",
[LCK_CW] "CW",
[LCK_CR] "CR",
- [LCK_NL] "NL"
+ [LCK_NL] "NL",
+ [LCK_GROUP] "GROUP",
+ [LCK_COS] "COS"
};
+
char *ldlm_typename[] = {
[LDLM_PLAIN] "PLN",
[LDLM_EXTENT] "EXT",
+ [LDLM_FLOCK] "FLK",
+ [LDLM_IBITS] "IBT",
};
-#ifdef __KERNEL__
char *ldlm_it2str(int it)
{
switch (it) {
return "UNKNOWN";
}
}
-#endif
-
-extern kmem_cache_t *ldlm_lock_slab;
-struct lustre_lock ldlm_handle_lock;
-static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
+extern cfs_mem_cache_t *ldlm_lock_slab;
-static ldlm_res_compat ldlm_res_compat_table[] = {
- [LDLM_PLAIN] ldlm_plain_compat,
- [LDLM_EXTENT] ldlm_extent_compat,
-};
-
-static ldlm_res_policy ldlm_intent_policy_func;
-
-static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
- void *req_cookie, ldlm_mode_t mode, int flags,
- void *data)
-{
- if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
- return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
- flags, data);
- }
-
- return ELDLM_OK;
-}
-
-static ldlm_res_policy ldlm_res_policy_table[] = {
- [LDLM_PLAIN] ldlm_plain_policy,
- [LDLM_EXTENT] ldlm_extent_policy,
+static ldlm_processing_policy ldlm_processing_policy_table[] = {
+ [LDLM_PLAIN] ldlm_process_plain_lock,
+ [LDLM_EXTENT] ldlm_process_extent_lock,
+#ifdef __KERNEL__
+ [LDLM_FLOCK] ldlm_process_flock_lock,
+#endif
+ [LDLM_IBITS] ldlm_process_inodebits_lock,
};
-void ldlm_register_intent(ldlm_res_policy arg)
+ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
{
- ldlm_intent_policy_func = arg;
+ return ldlm_processing_policy_table[res->lr_type];
}
-void ldlm_unregister_intent(void)
+void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
{
- ldlm_intent_policy_func = NULL;
+ ns->ns_policy = arg;
}
/*
return lock;
}
+static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
+{
+ LASSERT(size == sizeof(*lock));
+ OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
+}
+
void ldlm_lock_put(struct ldlm_lock *lock)
{
- struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
ENTRY;
+ LASSERT(lock->l_resource != LP_POISON);
+ LASSERT(atomic_read(&lock->l_refc) > 0);
if (atomic_dec_and_test(&lock->l_refc)) {
- l_lock(&ns->ns_lock);
- LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
+ struct ldlm_resource *res;
+
+ LDLM_DEBUG(lock,
+ "final lock_put on destroyed lock, freeing it.");
+
+ res = lock->l_resource;
LASSERT(lock->l_destroyed);
LASSERT(list_empty(&lock->l_res_link));
+ LASSERT(list_empty(&lock->l_pending_chain));
- spin_lock(&ns->ns_counter_lock);
- ns->ns_locks--;
- spin_unlock(&ns->ns_counter_lock);
-
- ldlm_resource_putref(lock->l_resource);
+ atomic_dec(&res->lr_namespace->ns_locks);
+ lu_ref_del(&res->lr_reference, "lock", lock);
+ ldlm_resource_putref(res);
lock->l_resource = NULL;
- if (lock->l_export)
+ if (lock->l_export) {
class_export_put(lock->l_export);
+ lock->l_export = NULL;
+ }
- if (lock->l_parent)
- LDLM_LOCK_PUT(lock->l_parent);
+ if (lock->l_lvb_data != NULL)
+ OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
- OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
- l_unlock(&ns->ns_lock);
+ ldlm_interval_free(ldlm_interval_detach(lock));
+ lu_ref_fini(&lock->l_reference);
+ OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
+ ldlm_lock_free);
}
EXIT;
}
-void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
+int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
{
- ENTRY;
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ int rc = 0;
if (!list_empty(&lock->l_lru)) {
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
list_del_init(&lock->l_lru);
- lock->l_resource->lr_namespace->ns_nr_unused--;
- LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
+ LASSERT(ns->ns_nr_unused > 0);
+ ns->ns_nr_unused--;
+ rc = 1;
}
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ return rc;
+}
+
+int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
+{
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ int rc;
+ ENTRY;
+ spin_lock(&ns->ns_unused_lock);
+ rc = ldlm_lock_remove_from_lru_nolock(lock);
+ spin_unlock(&ns->ns_unused_lock);
EXIT;
+ return rc;
+}
+
+void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
+{
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ lock->l_last_used = cfs_time_current();
+ LASSERT(list_empty(&lock->l_lru));
+ LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
+ list_add_tail(&lock->l_lru, &ns->ns_unused_list);
+ LASSERT(ns->ns_nr_unused >= 0);
+ ns->ns_nr_unused++;
}
-/* This used to have a 'strict' flact, which recovery would use to mark an
+void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
+{
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ ENTRY;
+ spin_lock(&ns->ns_unused_lock);
+ ldlm_lock_add_to_lru_nolock(lock);
+ spin_unlock(&ns->ns_unused_lock);
+ EXIT;
+}
+
+void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
+{
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ ENTRY;
+ spin_lock(&ns->ns_unused_lock);
+ if (!list_empty(&lock->l_lru)) {
+ ldlm_lock_remove_from_lru_nolock(lock);
+ ldlm_lock_add_to_lru_nolock(lock);
+ }
+ spin_unlock(&ns->ns_unused_lock);
+ EXIT;
+}
+
+/* This used to have a 'strict' flag, which recovery would use to mark an
* in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
* shall explain why it's gone: with the new hash table scheme, once you call
* ldlm_lock_destroy, you can never drop your final references on this lock.
* Because it's not in the hash table anymore. -phil */
-void ldlm_lock_destroy(struct ldlm_lock *lock)
+int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
{
ENTRY;
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
- if (!list_empty(&lock->l_children)) {
- LDLM_ERROR(lock, "still has children (%p)!",
- lock->l_children.next);
- ldlm_lock_dump(D_ERROR, lock, 0);
- LBUG();
- }
if (lock->l_readers || lock->l_writers) {
LDLM_ERROR(lock, "lock still has references");
ldlm_lock_dump(D_ERROR, lock, 0);
}
if (!list_empty(&lock->l_res_link)) {
+ LDLM_ERROR(lock, "lock still on resource");
ldlm_lock_dump(D_ERROR, lock, 0);
LBUG();
}
if (lock->l_destroyed) {
LASSERT(list_empty(&lock->l_lru));
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
EXIT;
- return;
+ return 0;
}
lock->l_destroyed = 1;
- list_del_init(&lock->l_export_chain);
+ if (lock->l_export && lock->l_export->exp_lock_hash &&
+ !hlist_unhashed(&lock->l_exp_hash))
+ lustre_hash_del(lock->l_export->exp_lock_hash,
+ &lock->l_remote_handle, &lock->l_exp_hash);
+
ldlm_lock_remove_from_lru(lock);
class_handle_unhash(&lock->l_handle);
if (lock->l_export && lock->l_completion_ast)
lock->l_completion_ast(lock, 0);
#endif
+ EXIT;
+ return 1;
+}
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
- LDLM_LOCK_PUT(lock);
+void ldlm_lock_destroy(struct ldlm_lock *lock)
+{
+ int first;
+ ENTRY;
+ lock_res_and_lock(lock);
+ first = ldlm_lock_destroy_internal(lock);
+ unlock_res_and_lock(lock);
+
+ /* drop reference from hashtable only for first destroy */
+ if (first) {
+ lu_ref_del(&lock->l_reference, "hash", lock);
+ LDLM_LOCK_RELEASE(lock);
+ }
+ EXIT;
+}
+
+void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
+{
+ int first;
+ ENTRY;
+ first = ldlm_lock_destroy_internal(lock);
+ /* drop reference from hashtable only for first destroy */
+ if (first) {
+ lu_ref_del(&lock->l_reference, "hash", lock);
+ LDLM_LOCK_RELEASE(lock);
+ }
EXIT;
}
* usage: pass in a resource on which you have done ldlm_resource_get
* pass in a parent lock on which you have done a ldlm_lock_get
* after return, ldlm_*_put the resource and parent
- * returns: lock with refcount 1
+ * returns: lock with refcount 2 - one for current caller and one for remote
*/
-static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
- struct ldlm_resource *resource)
+static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
{
struct ldlm_lock *lock;
ENTRY;
if (resource == NULL)
LBUG();
- OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_KERNEL, sizeof(*lock));
+ OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
if (lock == NULL)
RETURN(NULL);
+ spin_lock_init(&lock->l_lock);
lock->l_resource = ldlm_resource_getref(resource);
+ lu_ref_add(&resource->lr_reference, "lock", lock);
atomic_set(&lock->l_refc, 2);
- INIT_LIST_HEAD(&lock->l_children);
- INIT_LIST_HEAD(&lock->l_res_link);
- INIT_LIST_HEAD(&lock->l_lru);
- INIT_LIST_HEAD(&lock->l_export_chain);
- INIT_LIST_HEAD(&lock->l_pending_chain);
- init_waitqueue_head(&lock->l_waitq);
-
- spin_lock(&resource->lr_namespace->ns_counter_lock);
- resource->lr_namespace->ns_locks++;
- spin_unlock(&resource->lr_namespace->ns_counter_lock);
-
- if (parent != NULL) {
- l_lock(&parent->l_resource->lr_namespace->ns_lock);
- lock->l_parent = LDLM_LOCK_GET(parent);
- list_add(&lock->l_childof, &parent->l_children);
- l_unlock(&parent->l_resource->lr_namespace->ns_lock);
- }
-
- INIT_LIST_HEAD(&lock->l_handle.h_link);
+ CFS_INIT_LIST_HEAD(&lock->l_res_link);
+ CFS_INIT_LIST_HEAD(&lock->l_lru);
+ CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
+ CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
+ CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
+ CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
+ cfs_waitq_init(&lock->l_waitq);
+ lock->l_blocking_lock = NULL;
+ CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
+ CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
+ CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
+
+ atomic_inc(&resource->lr_namespace->ns_locks);
+ CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
class_handle_hash(&lock->l_handle, lock_handle_addref);
+ CFS_INIT_LIST_HEAD(&lock->l_extents_list);
+ spin_lock_init(&lock->l_extents_list_lock);
+ CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
+ lu_ref_init(&lock->l_reference);
+ lu_ref_add(&lock->l_reference, "hash", lock);
+ lock->l_callback_timeout = 0;
+
RETURN(lock);
}
int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
- struct ldlm_res_id new_resid)
+ const struct ldlm_res_id *new_resid)
{
struct ldlm_resource *oldres = lock->l_resource;
+ struct ldlm_resource *newres;
+ int type;
ENTRY;
- l_lock(&ns->ns_lock);
- if (memcmp(&new_resid, &lock->l_resource->lr_name,
+ LASSERT(ns_is_client(ns));
+
+ lock_res_and_lock(lock);
+ if (memcmp(new_resid, &lock->l_resource->lr_name,
sizeof(lock->l_resource->lr_name)) == 0) {
/* Nothing to do */
- l_unlock(&ns->ns_lock);
+ unlock_res_and_lock(lock);
RETURN(0);
}
- LASSERT(new_resid.name[0] != 0);
+ LASSERT(new_resid->name[0] != 0);
/* This function assumes that the lock isn't on any lists */
LASSERT(list_empty(&lock->l_res_link));
- lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
- lock->l_resource->lr_type, 1);
- if (lock->l_resource == NULL) {
- LBUG();
+ type = oldres->lr_type;
+ unlock_res_and_lock(lock);
+
+ newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
+ lu_ref_add(&newres->lr_reference, "lock", lock);
+ if (newres == NULL)
RETURN(-ENOMEM);
+ /*
+ * To flip the lock from the old to the new resource, lock, oldres and
+ * newres have to be locked. Resource spin-locks are nested within
+ * lock->l_lock, and are taken in the memory address order to avoid
+ * dead-locks.
+ */
+ spin_lock(&lock->l_lock);
+ oldres = lock->l_resource;
+ if (oldres < newres) {
+ lock_res(oldres);
+ lock_res_nested(newres, LRT_NEW);
+ } else {
+ lock_res(newres);
+ lock_res_nested(oldres, LRT_NEW);
}
+ LASSERT(memcmp(new_resid, &oldres->lr_name,
+ sizeof oldres->lr_name) != 0);
+ lock->l_resource = newres;
+ unlock_res(oldres);
+ unlock_res_and_lock(lock);
/* ...and the flowers are still standing! */
+ lu_ref_del(&oldres->lr_reference, "lock", lock);
ldlm_resource_putref(oldres);
- l_unlock(&ns->ns_lock);
RETURN(0);
}
* HANDLES
*/
-void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
+void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
{
lockh->cookie = lock->l_handle.h_cookie;
}
* Return NULL if flag already set
*/
-struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
+struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
+ int flags)
{
struct ldlm_namespace *ns;
- struct ldlm_lock *lock = NULL, *retval = NULL;
+ struct ldlm_lock *lock, *retval = NULL;
ENTRY;
LASSERT(handle);
ns = lock->l_resource->lr_namespace;
LASSERT(ns != NULL);
- l_lock(&ns->ns_lock);
+ lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
+ lock_res_and_lock(lock);
/* It's unlikely but possible that someone marked the lock as
* destroyed after we did handle2object on it */
if (lock->l_destroyed) {
+ unlock_res_and_lock(lock);
CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
LDLM_LOCK_PUT(lock);
GOTO(out, retval);
}
if (flags && (lock->l_flags & flags)) {
+ unlock_res_and_lock(lock);
LDLM_LOCK_PUT(lock);
GOTO(out, retval);
}
if (flags)
lock->l_flags |= flags;
+ unlock_res_and_lock(lock);
retval = lock;
EXIT;
out:
- l_unlock(&ns->ns_lock);
return retval;
}
-struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
- struct lustre_handle *handle)
+void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
{
- struct ldlm_lock *retval = NULL;
-
- l_lock(&ns->ns_lock);
- retval = __ldlm_handle2lock(handle, 0);
- l_unlock(&ns->ns_lock);
+ struct obd_export *exp = lock->l_export?:lock->l_conn_export;
+ /* INODEBITS_INTEROP: If the other side does not support
+ * inodebits, reply with a plain lock descriptor.
+ */
+ if ((lock->l_resource->lr_type == LDLM_IBITS) &&
+ (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
+ struct ldlm_resource res = *lock->l_resource;
+
+ /* Make sure all the right bits are set in this lock we
+ are going to pass to client */
+ LASSERTF(lock->l_policy_data.l_inodebits.bits ==
+ (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
+ "Inappropriate inode lock bits during "
+ "conversion " LPU64 "\n",
+ lock->l_policy_data.l_inodebits.bits);
+ res.lr_type = LDLM_PLAIN;
+ ldlm_res2desc(&res, &desc->l_resource);
+ /* Convert "new" lock mode to something old client can
+ understand */
+ if ((lock->l_req_mode == LCK_CR) ||
+ (lock->l_req_mode == LCK_CW))
+ desc->l_req_mode = LCK_PR;
+ else
+ desc->l_req_mode = lock->l_req_mode;
+ if ((lock->l_granted_mode == LCK_CR) ||
+ (lock->l_granted_mode == LCK_CW)) {
+ desc->l_granted_mode = LCK_PR;
+ } else {
+ /* We never grant PW/EX locks to clients */
+ LASSERT((lock->l_granted_mode != LCK_PW) &&
+ (lock->l_granted_mode != LCK_EX));
+ desc->l_granted_mode = lock->l_granted_mode;
+ }
- return retval;
+ /* We do not copy policy here, because there is no
+ policy for plain locks */
+ } else {
+ ldlm_res2desc(lock->l_resource, &desc->l_resource);
+ desc->l_req_mode = lock->l_req_mode;
+ desc->l_granted_mode = lock->l_granted_mode;
+ desc->l_policy_data = lock->l_policy_data;
+ }
}
-static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
+void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
+ struct list_head *work_list)
{
- return lockmode_compat(a->l_req_mode, b->l_req_mode);
+ if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
+ LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
+ lock->l_flags |= LDLM_FL_AST_SENT;
+ /* If the enqueuing client said so, tell the AST recipient to
+ * discard dirty data, rather than writing back. */
+ if (new->l_flags & LDLM_AST_DISCARD_DATA)
+ lock->l_flags |= LDLM_FL_DISCARD_DATA;
+ LASSERT(list_empty(&lock->l_bl_ast));
+ list_add(&lock->l_bl_ast, work_list);
+ LDLM_LOCK_GET(lock);
+ LASSERT(lock->l_blocking_lock == NULL);
+ lock->l_blocking_lock = LDLM_LOCK_GET(new);
+ }
}
-void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
+void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
{
- ldlm_res2desc(lock->l_resource, &desc->l_resource);
- desc->l_req_mode = lock->l_req_mode;
- desc->l_granted_mode = lock->l_granted_mode;
- memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
- memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
+ if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
+ lock->l_flags |= LDLM_FL_CP_REQD;
+ LDLM_DEBUG(lock, "lock granted; sending completion AST.");
+ LASSERT(list_empty(&lock->l_cp_ast));
+ list_add(&lock->l_cp_ast, work_list);
+ LDLM_LOCK_GET(lock);
+ }
}
-static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
- struct ldlm_lock *new,
- void *data, int datalen)
+/* must be called with lr_lock held */
+void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
+ struct list_head *work_list)
{
- struct ldlm_ast_work *w;
ENTRY;
-
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
- if (new && (lock->l_flags & LDLM_FL_AST_SENT))
- GOTO(out, 0);
-
- CDEBUG(D_OTHER, "lock %p incompatible; sending blocking AST.\n", lock);
-
- OBD_ALLOC(w, sizeof(*w));
- if (!w) {
- LBUG();
- GOTO(out, 0);
- }
-
- w->w_data = data;
- w->w_datalen = datalen;
- if (new) {
- lock->l_flags |= LDLM_FL_AST_SENT;
- w->w_blocking = 1;
- ldlm_lock2desc(new, &w->w_desc);
- }
-
- w->w_lock = LDLM_LOCK_GET(lock);
- list_add(&w->w_list, lock->l_resource->lr_tmp);
+ check_res_locked(lock->l_resource);
+ if (new)
+ ldlm_add_bl_work_item(lock, new, work_list);
+ else
+ ldlm_add_cp_work_item(lock, work_list);
EXIT;
- out:
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
- return;
}
void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
struct ldlm_lock *lock;
lock = ldlm_handle2lock(lockh);
+ LASSERT(lock != NULL);
ldlm_lock_addref_internal(lock, mode);
LDLM_LOCK_PUT(lock);
}
-/* only called for local locks */
-void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
+void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
{
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
ldlm_lock_remove_from_lru(lock);
- if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
+ if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
lock->l_readers++;
- else
+ lu_ref_add_atomic(&lock->l_reference, "reader", lock);
+ }
+ if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
lock->l_writers++;
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ lu_ref_add_atomic(&lock->l_reference, "writer", lock);
+ }
LDLM_LOCK_GET(lock);
+ lu_ref_add_atomic(&lock->l_reference, "user", lock);
LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
}
-void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
+/**
+ * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
+ * or destroyed.
+ *
+ * \retval 0 success, lock was addref-ed
+ *
+ * \retval -EAGAIN lock is being canceled.
+ */
+int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
{
- struct ldlm_namespace *ns;
- ENTRY;
+ struct ldlm_lock *lock;
+ int result;
+
+ result = -EAGAIN;
+ lock = ldlm_handle2lock(lockh);
+ if (lock != NULL) {
+ lock_res_and_lock(lock);
+ if (lock->l_readers != 0 || lock->l_writers != 0 ||
+ !(lock->l_flags & LDLM_FL_CBPENDING)) {
+ ldlm_lock_addref_internal_nolock(lock, mode);
+ result = 0;
+ }
+ unlock_res_and_lock(lock);
+ LDLM_LOCK_PUT(lock);
+ }
+ return result;
+}
+
+/* only called for local locks */
+void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
+{
+ lock_res_and_lock(lock);
+ ldlm_lock_addref_internal_nolock(lock, mode);
+ unlock_res_and_lock(lock);
+}
+/* only called in ldlm_flock_destroy and for local locks.
+ * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
+ * * ldlm_lock_remove_from_lru() does nothing, it is safe
+ * * for ldlm_flock_destroy usage by dropping some code */
+void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
+{
LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
- ns = lock->l_resource->lr_namespace;
- l_lock(&ns->ns_lock);
- if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
+ if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
LASSERT(lock->l_readers > 0);
+ lu_ref_del(&lock->l_reference, "reader", lock);
lock->l_readers--;
- } else {
+ }
+ if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
LASSERT(lock->l_writers > 0);
+ lu_ref_del(&lock->l_reference, "writer", lock);
lock->l_writers--;
}
+ lu_ref_del(&lock->l_reference, "user", lock);
+ LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
+}
+
+void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
+{
+ struct ldlm_namespace *ns;
+ ENTRY;
+
+ lock_res_and_lock(lock);
+
+ ns = lock->l_resource->lr_namespace;
+
+ ldlm_lock_decref_internal_nolock(lock, mode);
+
if (lock->l_flags & LDLM_FL_LOCAL &&
!lock->l_readers && !lock->l_writers) {
/* If this is a local lock on a server namespace and this was
(lock->l_flags & LDLM_FL_CBPENDING)) {
/* If we received a blocked AST and this was the last reference,
* run the callback. */
- if (!ns->ns_client && lock->l_export)
+ if (ns_is_server(ns) && lock->l_export)
CERROR("FL_CBPENDING set on non-local lock--just a "
"warning\n");
LDLM_DEBUG(lock, "final decref done on cbpending lock");
- l_unlock(&ns->ns_lock);
- /* FIXME: need a real 'desc' here */
- if (lock->l_blocking_ast != NULL)
- lock->l_blocking_ast(lock, NULL, lock->l_data,
- LDLM_CB_BLOCKING);
- else
- LDLM_DEBUG(lock, "No blocking AST?");
- } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
+ LDLM_LOCK_GET(lock); /* dropped by bl thread */
+ ldlm_lock_remove_from_lru(lock);
+ unlock_res_and_lock(lock);
+
+ if (lock->l_flags & LDLM_FL_FAIL_LOC)
+ OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
+
+ if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
+ ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
+ ldlm_handle_bl_callback(ns, NULL, lock);
+ } else if (ns_is_client(ns) &&
+ !lock->l_readers && !lock->l_writers &&
+ !(lock->l_flags & LDLM_FL_BL_AST)) {
/* If this is a client-side namespace and this was the last
* reference, put it on the LRU. */
- LASSERT(list_empty(&lock->l_lru));
- LASSERT(ns->ns_nr_unused >= 0);
- list_add_tail(&lock->l_lru, &ns->ns_unused_list);
- ns->ns_nr_unused++;
- l_unlock(&ns->ns_lock);
- ldlm_cancel_lru(ns);
+ ldlm_lock_add_to_lru(lock);
+ unlock_res_and_lock(lock);
+
+ if (lock->l_flags & LDLM_FL_FAIL_LOC)
+ OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
+
+ /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
+ * are not supported by the server, otherwise, it is done on
+ * enqueue. */
+ if (!exp_connect_cancelset(lock->l_conn_export) &&
+ !ns_connect_lru_resize(ns))
+ ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
} else {
- l_unlock(&ns->ns_lock);
+ unlock_res_and_lock(lock);
}
- LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
-
EXIT;
}
void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
{
struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
- LASSERT(lock != NULL);
+ LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
ldlm_lock_decref_internal(lock, mode);
LDLM_LOCK_PUT(lock);
}
LASSERT(lock != NULL);
LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ lock_res_and_lock(lock);
lock->l_flags |= LDLM_FL_CBPENDING;
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ unlock_res_and_lock(lock);
ldlm_lock_decref_internal(lock, mode);
LDLM_LOCK_PUT(lock);
}
-static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
- struct list_head *queue)
+struct sl_insert_point {
+ struct list_head *res_link;
+ struct list_head *mode_link;
+ struct list_head *policy_link;
+};
+
+/*
+ * search_granted_lock
+ *
+ * Description:
+ * Finds a position to insert the new lock.
+ * Parameters:
+ * queue [input]: the granted list where search acts on;
+ * req [input]: the lock whose position to be located;
+ * prev [output]: positions within 3 lists to insert @req to
+ * Return Value:
+ * filled @prev
+ * NOTE: called by
+ * - ldlm_grant_lock_with_skiplist
+ */
+static void search_granted_lock(struct list_head *queue,
+ struct ldlm_lock *req,
+ struct sl_insert_point *prev)
{
- struct list_head *tmp, *pos;
- int rc = 1;
+ struct list_head *tmp;
+ struct ldlm_lock *lock, *mode_end, *policy_end;
+ ENTRY;
- list_for_each_safe(tmp, pos, queue) {
- struct ldlm_lock *child;
- ldlm_res_compat compat;
+ list_for_each(tmp, queue) {
+ lock = list_entry(tmp, struct ldlm_lock, l_res_link);
- child = list_entry(tmp, struct ldlm_lock, l_res_link);
- if (lock == child)
- continue;
+ mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
+ l_sl_mode);
- compat = ldlm_res_compat_table[child->l_resource->lr_type];
- if (compat && compat(child, lock)) {
- CDEBUG(D_OTHER, "compat function succeded, next.\n");
+ if (lock->l_req_mode != req->l_req_mode) {
+ /* jump to last lock of mode group */
+ tmp = &mode_end->l_res_link;
continue;
}
- if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
- CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
- continue;
+
+ /* suitable mode group is found */
+ if (lock->l_resource->lr_type == LDLM_PLAIN) {
+ /* insert point is last lock of the mode group */
+ prev->res_link = &mode_end->l_res_link;
+ prev->mode_link = &mode_end->l_sl_mode;
+ prev->policy_link = &req->l_sl_policy;
+ EXIT;
+ return;
+ } else if (lock->l_resource->lr_type == LDLM_IBITS) {
+ for (;;) {
+ policy_end = list_entry(lock->l_sl_policy.prev,
+ struct ldlm_lock,
+ l_sl_policy);
+
+ if (lock->l_policy_data.l_inodebits.bits ==
+ req->l_policy_data.l_inodebits.bits) {
+ /* insert point is last lock of
+ * the policy group */
+ prev->res_link =
+ &policy_end->l_res_link;
+ prev->mode_link =
+ &policy_end->l_sl_mode;
+ prev->policy_link =
+ &policy_end->l_sl_policy;
+ EXIT;
+ return;
+ }
+
+ if (policy_end == mode_end)
+ /* done with mode group */
+ break;
+
+ /* go to next policy group within mode group */
+ tmp = policy_end->l_res_link.next;
+ lock = list_entry(tmp, struct ldlm_lock,
+ l_res_link);
+ } /* loop over policy groups within the mode group */
+
+ /* insert point is last lock of the mode group,
+ * new policy group is started */
+ prev->res_link = &mode_end->l_res_link;
+ prev->mode_link = &mode_end->l_sl_mode;
+ prev->policy_link = &req->l_sl_policy;
+ EXIT;
+ return;
+ } else {
+ LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
+ LBUG();
}
+ }
+
+ /* insert point is last lock on the queue,
+ * new mode group and new policy group are started */
+ prev->res_link = queue->prev;
+ prev->mode_link = &req->l_sl_mode;
+ prev->policy_link = &req->l_sl_policy;
+ EXIT;
+ return;
+}
+
+static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
+ struct sl_insert_point *prev)
+{
+ struct ldlm_resource *res = lock->l_resource;
+ ENTRY;
+
+ check_res_locked(res);
- rc = 0;
+ ldlm_resource_dump(D_INFO, res);
+ CDEBUG(D_OTHER, "About to add this lock:\n");
+ ldlm_lock_dump(D_OTHER, lock, 0);
- if (send_cbs && child->l_blocking_ast != NULL)
- ldlm_add_ast_work_item(child, lock, NULL, 0);
+ if (lock->l_destroyed) {
+ CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
+ return;
}
- return rc;
+ LASSERT(list_empty(&lock->l_res_link));
+ LASSERT(list_empty(&lock->l_sl_mode));
+ LASSERT(list_empty(&lock->l_sl_policy));
+
+ list_add(&lock->l_res_link, prev->res_link);
+ list_add(&lock->l_sl_mode, prev->mode_link);
+ list_add(&lock->l_sl_policy, prev->policy_link);
+
+ EXIT;
}
-static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
+static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
{
- int rc;
+ struct sl_insert_point prev;
ENTRY;
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
- rc = ldlm_lock_compat_list(lock, send_cbs,
- &lock->l_resource->lr_granted);
- /* FIXME: should we be sending ASTs to converting? */
- if (rc)
- rc = ldlm_lock_compat_list
- (lock, send_cbs, &lock->l_resource->lr_converting);
+ LASSERT(lock->l_req_mode == lock->l_granted_mode);
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
- RETURN(rc);
+ search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
+ ldlm_granted_list_add_lock(lock, &prev);
+ EXIT;
}
/* NOTE: called by
* - ldlm_lock_enqueue
* - ldlm_reprocess_queue
* - ldlm_lock_convert
+ *
+ * must be called with lr_lock held
*/
-void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen,
- int run_ast)
+void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
{
struct ldlm_resource *res = lock->l_resource;
ENTRY;
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
- ldlm_resource_add_lock(res, &res->lr_granted, lock);
+ check_res_locked(res);
+
lock->l_granted_mode = lock->l_req_mode;
+ if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
+ ldlm_grant_lock_with_skiplist(lock);
+ else if (res->lr_type == LDLM_EXTENT)
+ ldlm_extent_add_lock(res, lock);
+ else
+ ldlm_resource_add_lock(res, &res->lr_granted, lock);
if (lock->l_granted_mode < res->lr_most_restr)
res->lr_most_restr = lock->l_granted_mode;
- if (run_ast && lock->l_completion_ast != NULL)
- ldlm_add_ast_work_item(lock, NULL, data, datalen);
+ if (work_list && lock->l_completion_ast != NULL)
+ ldlm_add_ast_work_item(lock, NULL, work_list);
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
EXIT;
}
/* returns a referenced lock or NULL. See the flag descriptions below, in the
* comment above ldlm_lock_match */
-static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
- struct ldlm_extent *extent,
- struct ldlm_lock *old_lock, void *data,
- int flags)
+static struct ldlm_lock *search_queue(struct list_head *queue,
+ ldlm_mode_t *mode,
+ ldlm_policy_data_t *policy,
+ struct ldlm_lock *old_lock,
+ int flags, int unref)
{
struct ldlm_lock *lock;
struct list_head *tmp;
list_for_each(tmp, queue) {
+ ldlm_mode_t match;
+
lock = list_entry(tmp, struct ldlm_lock, l_res_link);
if (lock == old_lock)
/* llite sometimes wants to match locks that will be
* canceled when their users drop, but we allow it to match
* if it passes in CBPENDING and the lock still has users.
- * this is generally only going to be used by children
+ * this is generally only going to be used by children
* whose parents already hold a lock so forward progress
* can still happen. */
if (lock->l_flags & LDLM_FL_CBPENDING &&
!(flags & LDLM_FL_CBPENDING))
continue;
- if (lock->l_flags & LDLM_FL_CBPENDING &&
+ if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
lock->l_readers == 0 && lock->l_writers == 0)
continue;
- if (lock->l_req_mode != mode)
+ if (!(lock->l_req_mode & *mode))
continue;
+ match = lock->l_req_mode;
if (lock->l_resource->lr_type == LDLM_EXTENT &&
- (lock->l_extent.start > extent->start ||
- lock->l_extent.end < extent->end))
+ (lock->l_policy_data.l_extent.start >
+ policy->l_extent.start ||
+ lock->l_policy_data.l_extent.end < policy->l_extent.end))
continue;
- if (lock->l_destroyed)
+ if (unlikely(match == LCK_GROUP) &&
+ lock->l_resource->lr_type == LDLM_EXTENT &&
+ lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
+ continue;
+
+ /* We match if we have existing lock with same or wider set
+ of bits. */
+ if (lock->l_resource->lr_type == LDLM_IBITS &&
+ ((lock->l_policy_data.l_inodebits.bits &
+ policy->l_inodebits.bits) !=
+ policy->l_inodebits.bits))
+ continue;
+
+ if (!unref &&
+ (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
continue;
if ((flags & LDLM_FL_LOCAL_ONLY) &&
!(lock->l_flags & LDLM_FL_LOCAL))
continue;
- if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data) {
- LDLM_DEBUG(lock, "data mismatch: have %p, want %p",
- lock->l_data, data);
- continue;
+ if (flags & LDLM_FL_TEST_LOCK) {
+ LDLM_LOCK_GET(lock);
+ ldlm_lock_touch_in_lru(lock);
+ } else {
+ ldlm_lock_addref_internal_nolock(lock, match);
}
-
- ldlm_lock_addref_internal(lock, mode);
+ *mode = match;
return lock;
}
return NULL;
}
+void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
+{
+ lock->l_flags |= LDLM_FL_LVB_READY;
+ cfs_waitq_signal(&lock->l_waitq);
+}
+
+void ldlm_lock_allow_match(struct ldlm_lock *lock)
+{
+ lock_res_and_lock(lock);
+ ldlm_lock_allow_match_locked(lock);
+ unlock_res_and_lock(lock);
+}
+
/* Can be called in two ways:
*
* If 'ns' is NULL, then lockh describes an existing lock that we want to look
* If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
* to be canceled can still be matched as long as they still have reader
* or writer refernces
- * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
- * data is the same.
+ * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
+ * just tell us if we would have matched.
*
* Returns 1 if it finds an already-existing lock that is compatible; in this
* case, lockh is filled in with a addref()ed lock
+ *
+ * we also check security context, if that failed we simply return 0 (to keep
+ * caller code unchanged), the context failure will be discovered by caller
+ * sometime later.
*/
-int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
- struct ldlm_res_id *res_id, __u32 type, void *cookie,
- int cookielen, ldlm_mode_t mode, void *data,
- struct lustre_handle *lockh)
+ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
+ const struct ldlm_res_id *res_id, ldlm_type_t type,
+ ldlm_policy_data_t *policy, ldlm_mode_t mode,
+ struct lustre_handle *lockh, int unref)
{
struct ldlm_resource *res;
struct ldlm_lock *lock, *old_lock = NULL;
mode = old_lock->l_req_mode;
}
- res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
+ res = ldlm_resource_get(ns, NULL, res_id, type, 0);
if (res == NULL) {
LASSERT(old_lock == NULL);
RETURN(0);
}
- l_lock(&ns->ns_lock);
+ LDLM_RESOURCE_ADDREF(res);
+ lock_res(res);
- lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
- flags);
+ lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
+ flags, unref);
if (lock != NULL)
GOTO(out, rc = 1);
if (flags & LDLM_FL_BLOCK_GRANTED)
GOTO(out, rc = 0);
- lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
- flags);
+ lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
+ flags, unref);
if (lock != NULL)
GOTO(out, rc = 1);
- lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
- flags);
+ lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
+ flags, unref);
if (lock != NULL)
GOTO(out, rc = 1);
EXIT;
out:
+ unlock_res(res);
+ LDLM_RESOURCE_DELREF(res);
ldlm_resource_putref(res);
- l_unlock(&ns->ns_lock);
if (lock) {
ldlm_lock2handle(lock, lockh);
- if (lock->l_completion_ast)
- lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
- NULL);
+ if ((flags & LDLM_FL_LVB_READY) &&
+ (!(lock->l_flags & LDLM_FL_LVB_READY))) {
+ struct l_wait_info lwi;
+ if (lock->l_completion_ast) {
+ int err = lock->l_completion_ast(lock,
+ LDLM_FL_WAIT_NOREPROC,
+ NULL);
+ if (err) {
+ if (flags & LDLM_FL_TEST_LOCK)
+ LDLM_LOCK_RELEASE(lock);
+ else
+ ldlm_lock_decref_internal(lock,
+ mode);
+ rc = 0;
+ goto out2;
+ }
+ }
+
+ lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
+ NULL, LWI_ON_SIGNAL_NOOP, NULL);
+
+ /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
+ l_wait_event(lock->l_waitq,
+ (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
+ }
}
- if (rc)
- LDLM_DEBUG(lock, "matched");
- else
- LDLM_DEBUG_NOLOCK("not matched");
+ out2:
+ if (rc) {
+ LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
+ (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+ res_id->name[2] : policy->l_extent.start,
+ (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+ res_id->name[3] : policy->l_extent.end);
+
+ /* check user's security context */
+ if (lock->l_conn_export &&
+ sptlrpc_import_check_ctx(
+ class_exp2cliimp(lock->l_conn_export))) {
+ if (!(flags & LDLM_FL_TEST_LOCK))
+ ldlm_lock_decref_internal(lock, mode);
+ rc = 0;
+ }
+ if (flags & LDLM_FL_TEST_LOCK)
+ LDLM_LOCK_RELEASE(lock);
+
+ } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
+ LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
+ LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
+ type, mode, res_id->name[0], res_id->name[1],
+ (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+ res_id->name[2] :policy->l_extent.start,
+ (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+ res_id->name[3] : policy->l_extent.end);
+ }
if (old_lock)
LDLM_LOCK_PUT(old_lock);
- return rc;
+ return rc ? mode : 0;
}
/* Returns a referenced lock */
struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
- struct lustre_handle *parent_lock_handle,
- struct ldlm_res_id res_id, __u32 type,
+ const struct ldlm_res_id *res_id,
+ ldlm_type_t type,
ldlm_mode_t mode,
- ldlm_blocking_callback blocking,
- ldlm_completion_callback completion,
- void *data)
+ const struct ldlm_callback_suite *cbs,
+ void *data, __u32 lvb_len)
{
- struct ldlm_resource *res, *parent_res = NULL;
- struct ldlm_lock *lock, *parent_lock = NULL;
+ struct ldlm_lock *lock;
+ struct ldlm_resource *res;
ENTRY;
- if (parent_lock_handle) {
- parent_lock = ldlm_handle2lock(parent_lock_handle);
- if (parent_lock)
- parent_res = parent_lock->l_resource;
- }
-
- res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
+ res = ldlm_resource_get(ns, NULL, res_id, type, 1);
if (res == NULL)
RETURN(NULL);
- lock = ldlm_lock_new(parent_lock, res);
+ lock = ldlm_lock_new(res);
ldlm_resource_putref(res);
- if (parent_lock != NULL)
- LDLM_LOCK_PUT(parent_lock);
if (lock == NULL)
RETURN(NULL);
lock->l_req_mode = mode;
- lock->l_data = data;
- lock->l_blocking_ast = blocking;
- lock->l_completion_ast = completion;
+ lock->l_ast_data = data;
+ lock->l_pid = cfs_curproc_pid();
+ if (cbs) {
+ lock->l_blocking_ast = cbs->lcs_blocking;
+ lock->l_completion_ast = cbs->lcs_completion;
+ lock->l_glimpse_ast = cbs->lcs_glimpse;
+ lock->l_weigh_ast = cbs->lcs_weigh;
+ }
+
+ lock->l_tree_node = NULL;
+ /* if this is the extent lock, allocate the interval tree node */
+ if (type == LDLM_EXTENT) {
+ if (ldlm_interval_alloc(lock) == NULL)
+ GOTO(out, 0);
+ }
+
+ if (lvb_len) {
+ lock->l_lvb_len = lvb_len;
+ OBD_ALLOC(lock->l_lvb_data, lvb_len);
+ if (lock->l_lvb_data == NULL)
+ GOTO(out, 0);
+ }
RETURN(lock);
+
+out:
+ if (lock->l_lvb_data)
+ OBD_FREE(lock->l_lvb_data, lvb_len);
+ ldlm_interval_free(ldlm_interval_detach(lock));
+ OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
+ return NULL;
}
ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
struct ldlm_lock **lockp,
- void *cookie, int cookie_len, int *flags)
+ void *cookie, int *flags)
{
- struct ldlm_resource *res;
struct ldlm_lock *lock = *lockp;
- int local;
- ldlm_res_policy policy;
+ struct ldlm_resource *res = lock->l_resource;
+ int local = ns_is_client(res->lr_namespace);
+ ldlm_processing_policy policy;
+ ldlm_error_t rc = ELDLM_OK;
+ struct ldlm_interval *node = NULL;
ENTRY;
- res = lock->l_resource;
-
- if (res->lr_type == LDLM_EXTENT)
- memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
-
+ lock->l_last_activity = cfs_time_current_sec();
/* policies are not executed on the client or during replay */
- local = res->lr_namespace->ns_client;
- if (!local && !(*flags & LDLM_FL_REPLAY) &&
- (policy = ldlm_res_policy_table[res->lr_type])) {
- int rc;
- rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
- if (rc == ELDLM_LOCK_CHANGED) {
- res = lock->l_resource;
- *flags |= LDLM_FL_LOCK_CHANGED;
- } else if (rc == ELDLM_LOCK_REPLACED) {
+ if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
+ && !local && ns->ns_policy) {
+ rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
+ NULL);
+ if (rc == ELDLM_LOCK_REPLACED) {
/* The lock that was returned has already been granted,
- * and placed into lockp. Destroy the old one and our
+ * and placed into lockp. If it's not the same as the
+ * one we passed in, then destroy the old one and our
* work here is done. */
- ldlm_lock_destroy(lock);
- LDLM_LOCK_PUT(lock);
+ if (lock != *lockp) {
+ ldlm_lock_destroy(lock);
+ LDLM_LOCK_RELEASE(lock);
+ }
*flags |= LDLM_FL_LOCK_CHANGED;
RETURN(0);
- } else if (rc == ELDLM_LOCK_ABORTED) {
+ } else if (rc != ELDLM_OK ||
+ (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
ldlm_lock_destroy(lock);
RETURN(rc);
}
}
- l_lock(&ns->ns_lock);
+ /* For a replaying lock, it might be already in granted list. So
+ * unlinking the lock will cause the interval node to be freed, we
+ * have to allocate the interval node early otherwise we can't regrant
+ * this lock in the future. - jay */
+ if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
+ OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
+ sizeof(*node));
+
+ lock_res_and_lock(lock);
if (local && lock->l_req_mode == lock->l_granted_mode) {
- /* The server returned a blocked lock, but it was granted before
- * we got a chance to actually enqueue it. We don't need to do
- * anything else. */
+ /* The server returned a blocked lock, but it was granted
+ * before we got a chance to actually enqueue it. We don't
+ * need to do anything else. */
*flags &= ~(LDLM_FL_BLOCK_GRANTED |
LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
GOTO(out, ELDLM_OK);
}
+ ldlm_resource_unlink_lock(lock);
+ if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
+ if (node == NULL) {
+ ldlm_lock_destroy_nolock(lock);
+ GOTO(out, rc = -ENOMEM);
+ }
+
+ CFS_INIT_LIST_HEAD(&node->li_group);
+ ldlm_interval_attach(node, lock);
+ node = NULL;
+ }
+
+ /* Some flags from the enqueue want to make it into the AST, via the
+ * lock's l_flags. */
+ lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
+
/* This distinction between local lock trees is very important; a client
* namespace only has information about locks taken by that client, and
* thus doesn't have enough information to decide for itself if it can
*
* FIXME (bug 268): Detect obvious lies by checking compatibility in
* granted/converting queues. */
- ldlm_resource_unlink_lock(lock);
if (local) {
if (*flags & LDLM_FL_BLOCK_CONV)
ldlm_resource_add_lock(res, &res->lr_converting, lock);
else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
ldlm_resource_add_lock(res, &res->lr_waiting, lock);
else
- ldlm_grant_lock(lock, NULL, 0, 0);
+ ldlm_grant_lock(lock, NULL);
GOTO(out, ELDLM_OK);
} else if (*flags & LDLM_FL_REPLAY) {
if (*flags & LDLM_FL_BLOCK_CONV) {
ldlm_resource_add_lock(res, &res->lr_waiting, lock);
GOTO(out, ELDLM_OK);
} else if (*flags & LDLM_FL_BLOCK_GRANTED) {
- ldlm_grant_lock(lock, NULL, 0, 0);
+ ldlm_grant_lock(lock, NULL);
GOTO(out, ELDLM_OK);
}
/* If no flags, fall through to normal enqueue path. */
}
- /* FIXME: We may want to optimize by checking lr_most_restr */
- if (!list_empty(&res->lr_converting)) {
- ldlm_resource_add_lock(res, &res->lr_waiting, lock);
- *flags |= LDLM_FL_BLOCK_CONV;
- GOTO(out, ELDLM_OK);
- }
- if (!list_empty(&res->lr_waiting)) {
- ldlm_resource_add_lock(res, &res->lr_waiting, lock);
- *flags |= LDLM_FL_BLOCK_WAIT;
- GOTO(out, ELDLM_OK);
- }
- if (!ldlm_lock_compat(lock, 0)) {
- ldlm_resource_add_lock(res, &res->lr_waiting, lock);
- *flags |= LDLM_FL_BLOCK_GRANTED;
- GOTO(out, ELDLM_OK);
- }
- ldlm_grant_lock(lock, NULL, 0, 0);
- EXIT;
- out:
- l_unlock(&ns->ns_lock);
- return ELDLM_OK;
+ policy = ldlm_processing_policy_table[res->lr_type];
+ policy(lock, flags, 1, &rc, NULL);
+ GOTO(out, rc);
+out:
+ unlock_res_and_lock(lock);
+ if (node)
+ OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
+ return rc;
}
/* Must be called with namespace taken: queue is waiting or converting. */
-static int ldlm_reprocess_queue(struct ldlm_resource *res,
- struct list_head *queue)
+int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
+ struct list_head *work_list)
{
struct list_head *tmp, *pos;
+ ldlm_processing_policy policy;
+ int flags;
+ int rc = LDLM_ITER_CONTINUE;
+ ldlm_error_t err;
ENTRY;
+ check_res_locked(res);
+
+ policy = ldlm_processing_policy_table[res->lr_type];
+ LASSERT(policy);
+
list_for_each_safe(tmp, pos, queue) {
struct ldlm_lock *pending;
pending = list_entry(tmp, struct ldlm_lock, l_res_link);
CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
- if (!ldlm_lock_compat(pending, 1))
- RETURN(1);
+ flags = 0;
+ rc = policy(pending, &flags, 0, &err, work_list);
+ if (rc != LDLM_ITER_CONTINUE)
+ break;
+ }
+
+ RETURN(rc);
+}
+
+/* Helper function for ldlm_run_ast_work().
+ *
+ * Send an existing rpc set specified by @arg->set and then
+ * destroy it. Create new one if @do_create flag is set. */
+static void
+ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
+{
+ ENTRY;
+
+ ptlrpc_set_wait(arg->set);
+ if (arg->type == LDLM_BL_CALLBACK)
+ OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
+ ptlrpc_set_destroy(arg->set);
+
+ if (do_create)
+ arg->set = ptlrpc_prep_set();
+
+ EXIT;
+}
- list_del_init(&pending->l_res_link);
- ldlm_grant_lock(pending, NULL, 0, 1);
+static int
+ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
+{
+ struct ldlm_lock_desc d;
+ struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
+ ENTRY;
+
+ /* nobody should touch l_bl_ast */
+ lock_res_and_lock(lock);
+ list_del_init(&lock->l_bl_ast);
+
+ LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
+ LASSERT(lock->l_bl_ast_run == 0);
+ LASSERT(lock->l_blocking_lock);
+ lock->l_bl_ast_run++;
+ unlock_res_and_lock(lock);
+
+ ldlm_lock2desc(lock->l_blocking_lock, &d);
+
+ lock->l_blocking_ast(lock, &d, (void *)arg,
+ LDLM_CB_BLOCKING);
+ LDLM_LOCK_RELEASE(lock->l_blocking_lock);
+ lock->l_blocking_lock = NULL;
+ LDLM_LOCK_RELEASE(lock);
+
+ RETURN(1);
+}
+
+static int
+ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
+{
+ struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
+ ldlm_completion_callback completion_callback;
+ int rc = 0;
+ ENTRY;
+
+ /* It's possible to receive a completion AST before we've set
+ * the l_completion_ast pointer: either because the AST arrived
+ * before the reply, or simply because there's a small race
+ * window between receiving the reply and finishing the local
+ * enqueue. (bug 842)
+ *
+ * This can't happen with the blocking_ast, however, because we
+ * will never call the local blocking_ast until we drop our
+ * reader/writer reference, which we won't do until we get the
+ * reply and finish enqueueing. */
+
+ /* nobody should touch l_cp_ast */
+ lock_res_and_lock(lock);
+ list_del_init(&lock->l_cp_ast);
+ LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
+ /* save l_completion_ast since it can be changed by
+ * mds_intent_policy(), see bug 14225 */
+ completion_callback = lock->l_completion_ast;
+ lock->l_flags &= ~LDLM_FL_CP_REQD;
+ unlock_res_and_lock(lock);
+
+ if (completion_callback != NULL) {
+ completion_callback(lock, 0, (void *)arg);
+ rc = 1;
}
+ LDLM_LOCK_RELEASE(lock);
- RETURN(0);
+ RETURN(rc);
+}
+
+static int
+ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
+{
+ struct ldlm_lock_desc desc;
+ struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
+ ENTRY;
+
+ list_del_init(&lock->l_rk_ast);
+
+ /* the desc just pretend to exclusive */
+ ldlm_lock2desc(lock, &desc);
+ desc.l_req_mode = LCK_EX;
+ desc.l_granted_mode = 0;
+
+ lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
+ LDLM_LOCK_RELEASE(lock);
+
+ RETURN(1);
}
-int ldlm_run_ast_work(struct list_head *rpc_list)
+int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
{
+ struct ldlm_cb_set_arg arg;
struct list_head *tmp, *pos;
- int rc, retval = 0;
+ int (*work_ast_lock)(struct list_head *tmp,struct ldlm_cb_set_arg *arg);
+ int ast_count;
ENTRY;
+ arg.set = ptlrpc_prep_set();
+ atomic_set(&arg.restart, 0);
+ switch (ast_type) {
+ case LDLM_WORK_BL_AST:
+ arg.type = LDLM_BL_CALLBACK;
+ work_ast_lock = ldlm_work_bl_ast_lock;
+ break;
+ case LDLM_WORK_CP_AST:
+ arg.type = LDLM_CP_CALLBACK;
+ work_ast_lock = ldlm_work_cp_ast_lock;
+ break;
+ case LDLM_WORK_REVOKE_AST:
+ arg.type = LDLM_BL_CALLBACK;
+ work_ast_lock = ldlm_work_revoke_ast_lock;
+ break;
+ default:
+ LBUG();
+ }
+
+ ast_count = 0;
list_for_each_safe(tmp, pos, rpc_list) {
- struct ldlm_ast_work *w =
- list_entry(tmp, struct ldlm_ast_work, w_list);
-
- /* It's possible to receive a completion AST before we've set
- * the l_completion_ast pointer: either because the AST arrived
- * before the reply, or simply because there's a small race
- * window between receiving the reply and finishing the local
- * enqueue. (bug 842)
- *
- * This can't happen with the blocking_ast, however, because we
- * will never call the local blocking_ast until we drop our
- * reader/writer reference, which we won't do until we get the
- * reply and finish enqueueing. */
- if (w->w_blocking) {
- LASSERT(w->w_lock->l_blocking_ast != NULL);
- rc = w->w_lock->l_blocking_ast
- (w->w_lock, &w->w_desc, w->w_data,
- LDLM_CB_BLOCKING);
- } else if (w->w_lock->l_completion_ast != NULL) {
- rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
- w->w_data);
- } else {
- rc = 0;
+ ast_count += work_ast_lock(tmp, &arg);
+
+ /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
+ * and create a new set for requests that remained in
+ * @rpc_list */
+ if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
+ ldlm_send_and_maybe_create_set(&arg, 1);
+ ast_count = 0;
}
- if (rc == -ERESTART)
- retval = rc;
- else if (rc)
- CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
- "disconnect client\n");
- LDLM_LOCK_PUT(w->w_lock);
- list_del(&w->w_list);
- OBD_FREE(w, sizeof(*w));
}
- RETURN(retval);
+
+ if (ast_count > 0)
+ ldlm_send_and_maybe_create_set(&arg, 0);
+ else
+ /* In case when number of ASTs is multiply of
+ * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
+ * @arg.set must be destroyed here, otherwise we get
+ * write memory leaking. */
+ ptlrpc_set_destroy(arg.set);
+
+ RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
}
static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
{
- (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
+ struct list_head *tmp;
+ int i, rc;
+
+ if (ns == NULL)
+ return;
+
+ ENTRY;
+ spin_lock(&ns->ns_hash_lock);
+ for (i = 0; i < RES_HASH_SIZE; i++) {
+ tmp = ns->ns_hash[i].next;
+ while (tmp != &(ns->ns_hash[i])) {
+ struct ldlm_resource *res =
+ list_entry(tmp, struct ldlm_resource, lr_hash);
+
+ ldlm_resource_getref(res);
+ spin_unlock(&ns->ns_hash_lock);
+ LDLM_RESOURCE_ADDREF(res);
+
+ rc = reprocess_one_queue(res, NULL);
+
+ LDLM_RESOURCE_DELREF(res);
+ spin_lock(&ns->ns_hash_lock);
+ tmp = tmp->next;
+ ldlm_resource_putref_locked(res);
+
+ if (rc == LDLM_ITER_STOP)
+ GOTO(out, rc);
+ }
+ }
+ out:
+ spin_unlock(&ns->ns_hash_lock);
+ EXIT;
}
void ldlm_reprocess_all(struct ldlm_resource *res)
{
- struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
+ CFS_LIST_HEAD(rpc_list);
int rc;
ENTRY;
/* Local lock trees don't get reprocessed. */
- if (res->lr_namespace->ns_client) {
+ if (ns_is_client(res->lr_namespace)) {
EXIT;
return;
}
restart:
- l_lock(&res->lr_namespace->ns_lock);
- res->lr_tmp = &rpc_list;
-
- ldlm_reprocess_queue(res, &res->lr_converting);
- if (list_empty(&res->lr_converting))
- ldlm_reprocess_queue(res, &res->lr_waiting);
-
- res->lr_tmp = NULL;
- l_unlock(&res->lr_namespace->ns_lock);
-
- rc = ldlm_run_ast_work(&rpc_list);
- if (rc == -ERESTART)
+ lock_res(res);
+ rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
+ if (rc == LDLM_ITER_CONTINUE)
+ ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
+ unlock_res(res);
+
+ rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
+ if (rc == -ERESTART) {
+ LASSERT(list_empty(&rpc_list));
goto restart;
+ }
EXIT;
}
void ldlm_cancel_callback(struct ldlm_lock *lock)
{
- l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ check_res_locked(lock->l_resource);
if (!(lock->l_flags & LDLM_FL_CANCEL)) {
lock->l_flags |= LDLM_FL_CANCEL;
if (lock->l_blocking_ast) {
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
- lock->l_blocking_ast(lock, NULL, lock->l_data,
+ // l_check_no_ns_lock(ns);
+ unlock_res_and_lock(lock);
+ lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
LDLM_CB_CANCELING);
- return;
+ lock_res_and_lock(lock);
} else {
LDLM_DEBUG(lock, "no blocking ast");
}
}
- l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ lock->l_flags |= LDLM_FL_BL_DONE;
+}
+
+void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
+{
+ if (req->l_resource->lr_type != LDLM_PLAIN &&
+ req->l_resource->lr_type != LDLM_IBITS)
+ return;
+
+ list_del_init(&req->l_sl_policy);
+ list_del_init(&req->l_sl_mode);
}
void ldlm_lock_cancel(struct ldlm_lock *lock)
struct ldlm_namespace *ns;
ENTRY;
- /* There's no race between calling this and taking the ns lock below;
- * a lock can only be put on the waiting list once, because it can only
- * issue a blocking AST once. */
- ldlm_del_waiting_lock(lock);
+ lock_res_and_lock(lock);
res = lock->l_resource;
ns = res->lr_namespace;
- l_lock(&ns->ns_lock);
/* Please do not, no matter how tempting, remove this LBUG without
* talking to me first. -phik */
if (lock->l_readers || lock->l_writers) {
- LDLM_DEBUG(lock, "lock still has references");
- ldlm_lock_dump(D_OTHER, lock, 0);
+ LDLM_ERROR(lock, "lock still has references");
LBUG();
}
- ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
+ ldlm_del_waiting_lock(lock);
+
+ /* Releases cancel callback. */
+ ldlm_cancel_callback(lock);
+ /* Yes, second time, just in case it was added again while we were
+ running with no res lock in ldlm_cancel_callback */
+ ldlm_del_waiting_lock(lock);
ldlm_resource_unlink_lock(lock);
- ldlm_lock_destroy(lock);
- l_unlock(&ns->ns_lock);
+ ldlm_lock_destroy_nolock(lock);
+
+ if (lock->l_granted_mode == lock->l_req_mode)
+ ldlm_pool_del(&ns->ns_pool, lock);
+
+ /* Make sure we will not be called again for same lock what is possible
+ * if not to zero out lock->l_granted_mode */
+ lock->l_granted_mode = LCK_MINMODE;
+ unlock_res_and_lock(lock);
+
EXIT;
}
if (lock == NULL)
RETURN(-EINVAL);
- lock->l_data = data;
-
+ lock->l_ast_data = data;
LDLM_LOCK_PUT(lock);
-
RETURN(0);
}
-/* This function is only called from one thread (per export); no locking around
- * the list ops needed */
+void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
+{
+ struct obd_export *exp = data;
+ struct ldlm_lock *lock = obj;
+ struct ldlm_resource *res;
+
+ res = ldlm_resource_getref(lock->l_resource);
+ LDLM_LOCK_GET(lock);
+
+ LDLM_DEBUG(lock, "export %p", exp);
+ ldlm_res_lvbo_update(res, NULL, 0, 1);
+ ldlm_lock_cancel(lock);
+ ldlm_reprocess_all(res);
+ ldlm_resource_putref(res);
+ LDLM_LOCK_RELEASE(lock);
+}
+
void ldlm_cancel_locks_for_export(struct obd_export *exp)
{
- struct list_head *iter, *n;
+ lustre_hash_for_each_empty(exp->exp_lock_hash,
+ ldlm_cancel_locks_for_export_cb, exp);
+}
- list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
- struct ldlm_lock *lock;
- struct ldlm_resource *res;
- lock = list_entry(iter, struct ldlm_lock, l_export_chain);
- res = ldlm_resource_getref(lock->l_resource);
- LDLM_DEBUG(lock, "export %p", exp);
- ldlm_lock_cancel(lock);
- ldlm_reprocess_all(res);
- ldlm_resource_putref(res);
- }
+/**
+ * Downgrade an exclusive lock.
+ *
+ * A fast variant of ldlm_lock_convert for convertion of exclusive
+ * locks. The convertion is always successful.
+ *
+ * \param lock A lock to convert
+ * \param new_mode new lock mode
+ */
+void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
+{
+ struct ldlm_namespace *ns;
+ ENTRY;
+
+ LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
+ LASSERT(new_mode == LCK_COS);
+
+ lock_res_and_lock(lock);
+ ldlm_resource_unlink_lock(lock);
+ /*
+ * Remove the lock from pool as it will be added again in
+ * ldlm_grant_lock() called below.
+ */
+ ns = lock->l_resource->lr_namespace;
+ ldlm_pool_del(&ns->ns_pool, lock);
+
+ lock->l_req_mode = new_mode;
+ ldlm_grant_lock(lock, NULL);
+ unlock_res_and_lock(lock);
+ ldlm_reprocess_all(lock->l_resource);
+
+ EXIT;
}
struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
- int *flags)
+ __u32 *flags)
{
- struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
+ CFS_LIST_HEAD(rpc_list);
struct ldlm_resource *res;
struct ldlm_namespace *ns;
int granted = 0;
+ int old_mode, rc;
+ struct sl_insert_point prev;
+ ldlm_error_t err;
+ struct ldlm_interval *node;
ENTRY;
- LBUG();
+ if (new_mode == lock->l_granted_mode) { // No changes? Just return.
+ *flags |= LDLM_FL_BLOCK_GRANTED;
+ RETURN(lock->l_resource);
+ }
+
+ /* I can't check the type of lock here because the bitlock of lock
+ * is not held here, so do the allocation blindly. -jay */
+ OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
+ if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
+ RETURN(NULL);
+
+ LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
+ "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
+
+ lock_res_and_lock(lock);
res = lock->l_resource;
ns = res->lr_namespace;
- l_lock(&ns->ns_lock);
-
+ old_mode = lock->l_req_mode;
lock->l_req_mode = new_mode;
- ldlm_resource_unlink_lock(lock);
+ if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
+ /* remember the lock position where the lock might be
+ * added back to the granted list later and also
+ * remember the join mode for skiplist fixing. */
+ prev.res_link = lock->l_res_link.prev;
+ prev.mode_link = lock->l_sl_mode.prev;
+ prev.policy_link = lock->l_sl_policy.prev;
+ ldlm_resource_unlink_lock(lock);
+ } else {
+ ldlm_resource_unlink_lock(lock);
+ if (res->lr_type == LDLM_EXTENT) {
+ /* FIXME: ugly code, I have to attach the lock to a
+ * interval node again since perhaps it will be granted
+ * soon */
+ CFS_INIT_LIST_HEAD(&node->li_group);
+ ldlm_interval_attach(node, lock);
+ node = NULL;
+ }
+ }
+
+ /*
+ * Remove old lock from the pool before adding the lock with new
+ * mode below in ->policy()
+ */
+ ldlm_pool_del(&ns->ns_pool, lock);
/* If this is a local resource, put it on the appropriate list. */
- if (res->lr_namespace->ns_client) {
+ if (ns_is_client(res->lr_namespace)) {
if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
ldlm_resource_add_lock(res, &res->lr_converting, lock);
} else {
/* This should never happen, because of the way the
* server handles conversions. */
+ LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
+ *flags);
LBUG();
- res->lr_tmp = &rpc_list;
- ldlm_grant_lock(lock, NULL, 0, 0);
- res->lr_tmp = NULL;
+ ldlm_grant_lock(lock, &rpc_list);
granted = 1;
/* FIXME: completion handling not with ns_lock held ! */
if (lock->l_completion_ast)
lock->l_completion_ast(lock, 0, NULL);
}
} else {
- /* FIXME: We should try the conversion right away and possibly
- * return success without the need for an extra AST */
- ldlm_resource_add_lock(res, &res->lr_converting, lock);
- *flags |= LDLM_FL_BLOCK_CONV;
+ int pflags = 0;
+ ldlm_processing_policy policy;
+ policy = ldlm_processing_policy_table[res->lr_type];
+ rc = policy(lock, &pflags, 0, &err, &rpc_list);
+ if (rc == LDLM_ITER_STOP) {
+ lock->l_req_mode = old_mode;
+ if (res->lr_type == LDLM_EXTENT)
+ ldlm_extent_add_lock(res, lock);
+ else
+ ldlm_granted_list_add_lock(lock, &prev);
+
+ res = NULL;
+ } else {
+ *flags |= LDLM_FL_BLOCK_GRANTED;
+ granted = 1;
+ }
}
-
- l_unlock(&ns->ns_lock);
+ unlock_res_and_lock(lock);
if (granted)
- ldlm_run_ast_work(&rpc_list);
+ ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
+ if (node)
+ OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
RETURN(res);
}
void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
{
- char ver[128];
struct obd_device *obd = NULL;
- if (!((portal_debug | D_ERROR) & level))
+ if (!((libcfs_debug | D_ERROR) & level))
return;
- if (RES_VERSION_SIZE != 4)
- LBUG();
-
if (!lock) {
CDEBUG(level, " NULL LDLM lock\n");
return;
}
- snprintf(ver, sizeof(ver), "%x %x %x %x",
- lock->l_version[0], lock->l_version[1],
- lock->l_version[2], lock->l_version[3]);
-
- CDEBUG(level, " -- Lock dump: %p (%s) (rc: %d) (pos: %d)\n", lock, ver,
- atomic_read(&lock->l_refc), pos);
- if (lock->l_connh != NULL)
- obd = class_conn2obd(lock->l_connh);
+ CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
+ lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+ pos, lock->l_pid);
+ if (lock->l_conn_export != NULL)
+ obd = lock->l_conn_export->exp_obd;
if (lock->l_export && lock->l_export->exp_connection) {
- CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
- lock->l_export->exp_connection->c_peer.peer_nid,
- lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
- lock->l_remote_handle.cookie);
+ CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
+ libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
+ lock->l_remote_handle.cookie);
} else if (obd == NULL) {
CDEBUG(level, " Node: local\n");
} else {
struct obd_import *imp = obd->u.cli.cl_import;
- CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
- imp->imp_connection->c_peer.peer_nid,
- imp->imp_connection->c_peer.peer_ni->pni_name,
+ CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
+ libcfs_nid2str(imp->imp_connection->c_peer.nid),
lock->l_remote_handle.cookie);
}
- CDEBUG(level, " Resource: %p ("LPD64")\n", lock->l_resource,
- lock->l_resource->lr_name.name[0]);
- CDEBUG(level, " Req mode: %d, grant mode: %d, readers: %d, writers: "
- "%d\n", (int)lock->l_req_mode, (int)lock->l_granted_mode,
- lock->l_readers, lock->l_writers);
+ CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
+ lock->l_resource,
+ lock->l_resource->lr_name.name[0],
+ lock->l_resource->lr_name.name[1],
+ lock->l_resource->lr_name.name[2]);
+ CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
+ "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
+ ldlm_lockname[lock->l_granted_mode],
+ atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
+ lock->l_flags);
if (lock->l_resource->lr_type == LDLM_EXTENT)
- CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
- lock->l_extent.start, lock->l_extent.end);
+ CDEBUG(level, " Extent: "LPU64" -> "LPU64
+ " (req "LPU64"-"LPU64")\n",
+ lock->l_policy_data.l_extent.start,
+ lock->l_policy_data.l_extent.end,
+ lock->l_req_extent.start, lock->l_req_extent.end);
+ else if (lock->l_resource->lr_type == LDLM_FLOCK)
+ CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
+ lock->l_policy_data.l_flock.pid,
+ lock->l_policy_data.l_flock.start,
+ lock->l_policy_data.l_flock.end);
+ else if (lock->l_resource->lr_type == LDLM_IBITS)
+ CDEBUG(level, " Bits: "LPX64"\n",
+ lock->l_policy_data.l_inodebits.bits);
}
void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
{
struct ldlm_lock *lock;
+ if (!((libcfs_debug | D_ERROR) & level))
+ return;
+
lock = ldlm_handle2lock(lockh);
if (lock == NULL)
return;
LDLM_LOCK_PUT(lock);
}
+
+void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
+ struct libcfs_debug_msg_data *data, const char *fmt,
+ ...)
+{
+ va_list args;
+ cfs_debug_limit_state_t *cdls = data->msg_cdls;
+
+ va_start(args, fmt);
+
+ if (lock->l_resource == NULL) {
+ libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
+ data->msg_fn, data->msg_line, fmt, args,
+ " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+ "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
+ LPX64" expref: %d pid: %u timeout: %lu\n", lock,
+ lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+ lock->l_readers, lock->l_writers,
+ ldlm_lockname[lock->l_granted_mode],
+ ldlm_lockname[lock->l_req_mode],
+ lock->l_flags, lock->l_remote_handle.cookie,
+ lock->l_export ?
+ atomic_read(&lock->l_export->exp_refcount) : -99,
+ lock->l_pid, lock->l_callback_timeout);
+ va_end(args);
+ return;
+ }
+
+ switch (lock->l_resource->lr_type) {
+ case LDLM_EXTENT:
+ libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
+ data->msg_fn, data->msg_line, fmt, args,
+ " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+ "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
+ "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
+ " expref: %d pid: %u timeout %lu\n",
+ lock->l_resource->lr_namespace->ns_name, lock,
+ lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+ lock->l_readers, lock->l_writers,
+ ldlm_lockname[lock->l_granted_mode],
+ ldlm_lockname[lock->l_req_mode],
+ lock->l_resource->lr_name.name[0],
+ lock->l_resource->lr_name.name[1],
+ atomic_read(&lock->l_resource->lr_refcount),
+ ldlm_typename[lock->l_resource->lr_type],
+ lock->l_policy_data.l_extent.start,
+ lock->l_policy_data.l_extent.end,
+ lock->l_req_extent.start, lock->l_req_extent.end,
+ lock->l_flags, lock->l_remote_handle.cookie,
+ lock->l_export ?
+ atomic_read(&lock->l_export->exp_refcount) : -99,
+ lock->l_pid, lock->l_callback_timeout);
+ break;
+
+ case LDLM_FLOCK:
+ libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
+ data->msg_fn, data->msg_line, fmt, args,
+ " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+ "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
+ "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
+ " expref: %d pid: %u timeout: %lu\n",
+ lock->l_resource->lr_namespace->ns_name, lock,
+ lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+ lock->l_readers, lock->l_writers,
+ ldlm_lockname[lock->l_granted_mode],
+ ldlm_lockname[lock->l_req_mode],
+ lock->l_resource->lr_name.name[0],
+ lock->l_resource->lr_name.name[1],
+ atomic_read(&lock->l_resource->lr_refcount),
+ ldlm_typename[lock->l_resource->lr_type],
+ lock->l_policy_data.l_flock.pid,
+ lock->l_policy_data.l_flock.start,
+ lock->l_policy_data.l_flock.end,
+ lock->l_flags, lock->l_remote_handle.cookie,
+ lock->l_export ?
+ atomic_read(&lock->l_export->exp_refcount) : -99,
+ lock->l_pid, lock->l_callback_timeout);
+ break;
+
+ case LDLM_IBITS:
+ libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
+ data->msg_fn, data->msg_line, fmt, args,
+ " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+ "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
+ "flags: "LPX64" remote: "LPX64" expref: %d "
+ "pid: %u timeout: %lu\n",
+ lock->l_resource->lr_namespace->ns_name,
+ lock, lock->l_handle.h_cookie,
+ atomic_read (&lock->l_refc),
+ lock->l_readers, lock->l_writers,
+ ldlm_lockname[lock->l_granted_mode],
+ ldlm_lockname[lock->l_req_mode],
+ lock->l_resource->lr_name.name[0],
+ lock->l_resource->lr_name.name[1],
+ lock->l_policy_data.l_inodebits.bits,
+ atomic_read(&lock->l_resource->lr_refcount),
+ ldlm_typename[lock->l_resource->lr_type],
+ lock->l_flags, lock->l_remote_handle.cookie,
+ lock->l_export ?
+ atomic_read(&lock->l_export->exp_refcount) : -99,
+ lock->l_pid, lock->l_callback_timeout);
+ break;
+
+ default:
+ libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
+ data->msg_fn, data->msg_line, fmt, args,
+ " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+ "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
+ "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
+ lock->l_resource->lr_namespace->ns_name,
+ lock, lock->l_handle.h_cookie,
+ atomic_read (&lock->l_refc),
+ lock->l_readers, lock->l_writers,
+ ldlm_lockname[lock->l_granted_mode],
+ ldlm_lockname[lock->l_req_mode],
+ lock->l_resource->lr_name.name[0],
+ lock->l_resource->lr_name.name[1],
+ atomic_read(&lock->l_resource->lr_refcount),
+ ldlm_typename[lock->l_resource->lr_type],
+ lock->l_flags, lock->l_remote_handle.cookie,
+ lock->l_export ?
+ atomic_read(&lock->l_export->exp_refcount) : -99,
+ lock->l_pid, lock->l_callback_timeout);
+ break;
+ }
+ va_end(args);
+}
+EXPORT_SYMBOL(_ldlm_lock_debug);