/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
- * Copyright (C) 2002 Cluster File Systems, Inc.
+ * Copyright (c) 2002 Cluster File Systems, Inc.
+ * Author: Peter Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
*
- * This code is issued under the GNU General Public License.
- * See the file COPYING in this distribution
+ * This file is part of Lustre, http://www.lustre.org.
*
- * by Cluster File Systems, Inc.
- * authors, Peter Braam <braam@clusterfs.com> &
- * Phil Schwan <phil@clusterfs.com>
+ * 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.
+ *
+ * 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.
+ *
+ * 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.
*/
#define DEBUG_SUBSYSTEM S_LDLM
#include <linux/slab.h>
#include <linux/module.h>
+#include <linux/random.h>
#include <linux/lustre_dlm.h>
#include <linux/lustre_mds.h>
+#include <linux/obd_class.h>
+
+/* lock types */
+char *ldlm_lockname[] = {
+ [0] "--",
+ [LCK_EX] "EX",
+ [LCK_PW] "PW",
+ [LCK_PR] "PR",
+ [LCK_CW] "CW",
+ [LCK_CR] "CR",
+ [LCK_NL] "NL"
+};
+char *ldlm_typename[] = {
+ [LDLM_PLAIN] "PLN",
+ [LDLM_EXTENT] "EXT",
+ [LDLM_MDSINTENT] "INT"
+};
+
+char *ldlm_it2str(int it)
+{
+ switch (it) {
+ case IT_OPEN:
+ return "open";
+ case IT_CREAT:
+ return "creat";
+ case (IT_OPEN | IT_CREAT):
+ return "open|creat";
+ case IT_MKDIR:
+ return "mkdir";
+ case IT_LINK:
+ return "link";
+ case IT_SYMLINK:
+ return "symlink";
+ case IT_UNLINK:
+ return "unlink";
+ case IT_RMDIR:
+ return "rmdir";
+ case IT_RENAME:
+ return "rename";
+ case IT_RENAME2:
+ return "rename2";
+ case IT_READDIR:
+ return "readdir";
+ case IT_GETATTR:
+ return "getattr";
+ case IT_SETATTR:
+ return "setattr";
+ case IT_READLINK:
+ return "readlink";
+ case IT_MKNOD:
+ return "mknod";
+ case IT_LOOKUP:
+ return "lookup";
+ default:
+ CERROR("Unknown intent %d\n", it);
+ return "UNKNOWN";
+ }
+}
extern kmem_cache_t *ldlm_lock_slab;
-int (*mds_reint_p)(int offset, struct ptlrpc_request *req) = NULL;
-int (*mds_getattr_name_p)(int offset, struct ptlrpc_request *req) = NULL;
static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
-static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
- ldlm_mode_t mode, void *data);
-ldlm_res_compat ldlm_res_compat_table [] = {
+ldlm_res_compat ldlm_res_compat_table[] = {
[LDLM_PLAIN] ldlm_plain_compat,
[LDLM_EXTENT] ldlm_extent_compat,
[LDLM_MDSINTENT] ldlm_plain_compat
};
-ldlm_res_policy ldlm_res_policy_table [] = {
+ldlm_res_policy ldlm_res_policy_table[] = {
[LDLM_PLAIN] NULL,
[LDLM_EXTENT] ldlm_extent_policy,
- [LDLM_MDSINTENT] ldlm_intent_policy
+ [LDLM_MDSINTENT] NULL
};
-static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
- ldlm_mode_t mode, void *data)
+void ldlm_register_intent(int (*arg) (struct ldlm_lock * lock, void *req_cookie,
+ ldlm_mode_t mode, void *data))
{
- struct ptlrpc_request *req = req_cookie;
- int rc = 0;
- ENTRY;
+ ldlm_res_policy_table[LDLM_MDSINTENT] = arg;
+}
- if (!req_cookie)
- RETURN(0);
+void ldlm_unregister_intent(void)
+{
+ ldlm_res_policy_table[LDLM_MDSINTENT] = NULL;
+}
- if (req->rq_reqmsg->bufcount > 1) {
- /* an intent needs to be considered */
- struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
- struct mds_body *mds_rep;
- struct ldlm_reply *rep;
- struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
- __u32 type = lock->l_resource->lr_type;
- __u64 new_resid[3] = {0, 0, 0}, old_res;
- int bufcount = -1, rc, size[3] = {sizeof(struct ldlm_reply),
- sizeof(struct mds_body),
- sizeof(struct obdo)};
-
- it->opc = NTOH__u64(it->opc);
-
- LDLM_DEBUG(lock, "intent policy, opc: %Ld", it->opc);
-
- /* prepare reply */
- switch(it->opc) {
- case IT_GETATTR:
- /* Note that in the negative case you may be returning
- * a file and its obdo */
- case IT_CREAT:
- case IT_CREAT|IT_OPEN:
- case IT_MKDIR:
- case IT_SYMLINK:
- case IT_MKNOD:
- case IT_LINK:
- case IT_OPEN:
- case IT_RENAME:
- bufcount = 3;
- break;
- case IT_UNLINK:
- bufcount = 2;
- size[1] = sizeof(struct obdo);
- break;
- case IT_RMDIR:
- bufcount = 1;
- break;
- default:
- LBUG();
- }
+/*
+ * REFCOUNTED LOCK OBJECTS
+ */
- rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
- &req->rq_repmsg);
- if (rc) {
- rc = req->rq_status = -ENOMEM;
- RETURN(rc);
- }
- rep = lustre_msg_buf(req->rq_repmsg, 0);
- rep->lock_policy_res1 = 1;
-
- /* execute policy */
- switch (it->opc) {
- case IT_CREAT:
- case IT_CREAT|IT_OPEN:
- case IT_MKDIR:
- case IT_SETATTR:
- case IT_SYMLINK:
- case IT_MKNOD:
- case IT_LINK:
- case IT_UNLINK:
- case IT_RMDIR:
- case IT_RENAME2:
- if (mds_reint_p == NULL)
- mds_reint_p =
- inter_module_get_request
- ("mds_reint", "mds");
- if (IS_ERR(mds_reint_p)) {
- CERROR("MDSINTENT locks require the MDS "
- "module.\n");
- LBUG();
- RETURN(-EINVAL);
- }
- rc = mds_reint_p(2, req);
- if (rc)
- LBUG();
- break;
- case IT_GETATTR:
- case IT_READDIR:
- case IT_RENAME:
- case IT_OPEN:
- if (mds_getattr_name_p == NULL)
- mds_getattr_name_p =
- inter_module_get_request
- ("mds_getattr_name", "mds");
- if (IS_ERR(mds_getattr_name_p)) {
- CERROR("MDSINTENT locks require the MDS "
- "module.\n");
- LBUG();
- RETURN(-EINVAL);
- }
- rc = mds_getattr_name_p(2, req);
- if (rc) {
- req->rq_status = rc;
- RETURN(rc);
- }
- break;
- case IT_READDIR|IT_OPEN:
- LBUG();
- break;
- default:
- CERROR("Unhandled intent\n");
- LBUG();
- }
-
- if (it->opc == IT_UNLINK || it->opc == IT_RMDIR)
- RETURN(ELDLM_LOCK_ABORTED);
+/*
+ * Lock refcounts, during creation:
+ * - one special one for allocation, dec'd only once in destroy
+ * - one for being a lock that's in-use
+ * - one for the addref associated with a new lock
+ */
+struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
+{
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ lock->l_refc++;
+ ldlm_resource_getref(lock->l_resource);
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ return lock;
+}
- mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
- rep->lock_policy_res2 = req->rq_status;
- new_resid[0] = mds_rep->ino;
- old_res = lock->l_resource->lr_name[0];
+void ldlm_lock_put(struct ldlm_lock *lock)
+{
+ struct lustre_lock *nslock = &lock->l_resource->lr_namespace->ns_lock;
+ ENTRY;
- CDEBUG(D_INFO, "remote intent: locking %d instead of"
- "%ld\n", mds_rep->ino, (long)old_res);
- spin_lock(&lock->l_resource->lr_lock);
- if (!ldlm_resource_put(lock->l_resource))
- /* unlock it unless the resource was freed */
- spin_unlock(&lock->l_resource->lr_lock);
+ l_lock(nslock);
+ lock->l_refc--;
+ //LDLM_DEBUG(lock, "after refc--");
+ if (lock->l_refc < 0)
+ LBUG();
- lock->l_resource =
- ldlm_resource_get(ns, NULL, new_resid, type, 1);
- if (lock->l_resource == NULL) {
- LBUG();
- RETURN(-ENOMEM);
- }
- LDLM_DEBUG(lock, "intent policy, old res %ld",
- (long)old_res);
- RETURN(ELDLM_LOCK_CHANGED);
- } else {
- int size = sizeof(struct ldlm_reply);
- rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
- &req->rq_repmsg);
- if (rc) {
- CERROR("out of memory\n");
- LBUG();
- RETURN(-ENOMEM);
- }
+ ldlm_resource_put(lock->l_resource);
+ if (lock->l_parent)
+ LDLM_LOCK_PUT(lock->l_parent);
+
+ if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
+ lock->l_resource = NULL;
+ LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
+ if (lock->l_export && lock->l_export->exp_connection)
+ ptlrpc_put_connection(lock->l_export->exp_connection);
+ CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 1).\n",
+ sizeof(*lock), lock);
+ kmem_cache_free(ldlm_lock_slab, lock);
}
- RETURN(rc);
+ l_unlock(nslock);
+ EXIT;
}
-static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
+void ldlm_lock_destroy(struct ldlm_lock *lock)
{
- return lockmode_compat(a->l_req_mode, b->l_req_mode);
+ ENTRY;
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
+
+ if (!list_empty(&lock->l_children)) {
+ LDLM_DEBUG(lock, "still has children (%p)!",
+ lock->l_children.next);
+ ldlm_lock_dump(lock);
+ LBUG();
+ }
+ if (lock->l_readers || lock->l_writers) {
+ LDLM_DEBUG(lock, "lock still has references");
+ ldlm_lock_dump(lock);
+ LBUG();
+ }
+
+ if (!list_empty(&lock->l_res_link)) {
+ ldlm_lock_dump(lock);
+ LBUG();
+ }
+
+ if (lock->l_flags & LDLM_FL_DESTROYED) {
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ EXIT;
+ return;
+ }
+
+ list_del(&lock->l_export_chain);
+ lock->l_export = NULL;
+ lock->l_flags = LDLM_FL_DESTROYED;
+
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ LDLM_LOCK_PUT(lock);
+ EXIT;
}
-/* Args: referenced, unlocked parent (or NULL)
- * referenced, unlocked resource
- * Locks: parent->l_lock */
+/*
+ usage: pass in a resource on which you have done get
+ pass in a parent lock on which you have done a get
+ do not put the resource or the parent
+ returns: lock with refcount 1
+*/
static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
struct ldlm_resource *resource)
{
struct ldlm_lock *lock;
+ ENTRY;
if (resource == NULL)
LBUG();
lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
if (lock == NULL)
- return NULL;
+ RETURN(NULL);
+ CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
+ "%p (tot %d).\n", sizeof(*lock), lock, 1);
memset(lock, 0, sizeof(*lock));
+ get_random_bytes(&lock->l_random, sizeof(__u64));
+
lock->l_resource = resource;
+ /* this refcount matches the one of the resource passed
+ in which is not being put away */
+ lock->l_refc = 1;
INIT_LIST_HEAD(&lock->l_children);
INIT_LIST_HEAD(&lock->l_res_link);
+ INIT_LIST_HEAD(&lock->l_inode_link);
+ INIT_LIST_HEAD(&lock->l_export_chain);
+ INIT_LIST_HEAD(&lock->l_pending_chain);
init_waitqueue_head(&lock->l_waitq);
- lock->l_lock = SPIN_LOCK_UNLOCKED;
if (parent != NULL) {
- spin_lock(&parent->l_lock);
+ l_lock(&parent->l_resource->lr_namespace->ns_lock);
lock->l_parent = parent;
list_add(&lock->l_childof, &parent->l_children);
- spin_unlock(&parent->l_lock);
+ l_unlock(&parent->l_resource->lr_namespace->ns_lock);
}
-
- return lock;
+ /* this is the extra refcount, to prevent the lock
+ evaporating */
+ LDLM_LOCK_GET(lock);
+ RETURN(lock);
}
-/* Args: unreferenced, locked lock
- *
- * Caller must do its own ldlm_resource_put() on lock->l_resource */
-void ldlm_lock_free(struct ldlm_lock *lock)
+int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
{
- if (!list_empty(&lock->l_children)) {
- CERROR("lock %p still has children (%p)!\n", lock,
- lock->l_children.next);
- ldlm_lock_dump(lock);
+ struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+ struct ldlm_resource *oldres = lock->l_resource;
+ int type, i;
+ ENTRY;
+
+ l_lock(&ns->ns_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);
+ RETURN(0);
+ }
+
+ type = lock->l_resource->lr_type;
+ if (new_resid[0] == 0)
LBUG();
+ lock->l_resource = ldlm_resource_get(ns, NULL, new_resid, type, 1);
+ if (lock->l_resource == NULL) {
+ LBUG();
+ RETURN(-ENOMEM);
}
- if (lock->l_readers || lock->l_writers)
- CDEBUG(D_INFO, "lock still has references (%d readers, %d "
- "writers)\n", lock->l_readers, lock->l_writers);
+ /* move references over */
+ for (i = 0; i < lock->l_refc; i++) {
+ int rc;
+ ldlm_resource_getref(lock->l_resource);
+ rc = ldlm_resource_put(oldres);
+ if (rc == 1 && i != lock->l_refc - 1)
+ LBUG();
+ }
+ /* compensate for the initial get above.. */
+ ldlm_resource_put(lock->l_resource);
+
+ l_unlock(&ns->ns_lock);
+ RETURN(0);
+}
+
+/*
+ * HANDLES
+ */
+
+void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
+{
+ lockh->addr = (__u64) (unsigned long)lock;
+ lockh->cookie = lock->l_random;
+}
+
+struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle)
+{
+ struct ldlm_lock *lock = NULL, *retval = NULL;
+ ENTRY;
+
+ if (!handle || !handle->addr)
+ RETURN(NULL);
+
+ lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
+ if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock))
+ RETURN(NULL);
+
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ if (lock->l_random != handle->cookie)
+ GOTO(out, NULL);
+
+ if (lock->l_flags & LDLM_FL_DESTROYED)
+ GOTO(out, NULL);
+
+ retval = LDLM_LOCK_GET(lock);
+ EXIT;
+ out:
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ return retval;
+}
- if (lock->l_connection)
- ptlrpc_put_connection(lock->l_connection);
- kmem_cache_free(ldlm_lock_slab, lock);
+static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
+{
+ return lockmode_compat(a->l_req_mode, b->l_req_mode);
}
void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
}
-/* Args: unlocked lock */
-void ldlm_lock_addref(struct ldlm_lock *lock, __u32 mode)
-{
- spin_lock(&lock->l_lock);
- if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
- lock->l_readers++;
- else
- lock->l_writers++;
- spin_unlock(&lock->l_lock);
-}
-
-int ldlm_send_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock *new)
+static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
+ struct ldlm_lock *new)
{
- struct ptlrpc_request *req = NULL;
+ struct ldlm_ast_work *w;
ENTRY;
- spin_lock(&lock->l_lock);
- if (lock->l_flags & LDLM_FL_AST_SENT)
- RETURN(0);
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ if (new && (lock->l_flags & LDLM_FL_AST_SENT))
+ GOTO(out, 0);
- lock->l_flags |= LDLM_FL_AST_SENT;
+ OBD_ALLOC(w, sizeof(*w));
+ if (!w) {
+ LBUG();
+ GOTO(out, 0);
+ }
- lock->l_blocking_ast(lock, new, lock->l_data, lock->l_data_len, &req);
- spin_unlock(&lock->l_lock);
- if (req != NULL) {
- struct list_head *list = lock->l_resource->lr_tmp;
- list_add(&req->rq_multi, list);
+ if (new) {
+ lock->l_flags |= LDLM_FL_AST_SENT;
+ w->w_blocking = 1;
+ ldlm_lock2desc(new, &w->w_desc);
}
- RETURN(1);
+
+ w->w_lock = LDLM_LOCK_GET(lock);
+ list_add(&w->w_list, lock->l_resource->lr_tmp);
+ 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);
+ 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)
+{
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
+ if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
+ lock->l_readers++;
+ else
+ lock->l_writers++;
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+ LDLM_LOCK_GET(lock);
+ LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
}
/* Args: unlocked lock */
-void ldlm_lock_decref(struct ldlm_lock *lock, __u32 mode)
+void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
{
+ struct ldlm_lock *lock = ldlm_handle2lock(lockh);
ENTRY;
if (lock == NULL)
LBUG();
- spin_lock(&lock->l_lock);
+ LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
+ l_lock(&lock->l_resource->lr_namespace->ns_lock);
if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
lock->l_readers--;
else
lock->l_writers--;
+
+ /* If we received a blocked AST and this was the last reference,
+ * run the callback. */
if (!lock->l_readers && !lock->l_writers &&
- lock->l_flags & LDLM_FL_DYING) {
- /* Read this lock its rights. */
+ (lock->l_flags & LDLM_FL_CBPENDING)) {
if (!lock->l_resource->lr_namespace->ns_client) {
- CERROR("LDLM_FL_DYING set on non-local lock!\n");
+ CERROR("LDLM_FL_CBPENDING set on non-local lock!\n");
LBUG();
}
- CDEBUG(D_INFO, "final decref done on dying lock, "
- "calling callback.\n");
- spin_unlock(&lock->l_lock);
- /* This function pointer is unfortunately overloaded. This
- * call will not result in an RPC. */
+ LDLM_DEBUG(lock, "final decref done on cbpending lock");
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+
+ /* FIXME: need a real 'desc' here */
lock->l_blocking_ast(lock, NULL, lock->l_data,
- lock->l_data_len, NULL);
+ lock->l_data_len);
} else
- spin_unlock(&lock->l_lock);
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+
+ LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
+ LDLM_LOCK_PUT(lock); /* matches the handle2lock above */
+
EXIT;
}
-/* Args: unlocked lock */
-static int _ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs,
- struct list_head *queue)
+static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
+ struct list_head *queue)
{
struct list_head *tmp, *pos;
- int rc = 0;
+ int rc = 1;
list_for_each_safe(tmp, pos, queue) {
struct ldlm_lock *child;
continue;
compat = ldlm_res_compat_table[child->l_resource->lr_type];
- if (compat(child, lock)) {
+ if (compat && compat(child, lock)) {
CDEBUG(D_OTHER, "compat function succeded, next.\n");
continue;
}
continue;
}
- rc = 1;
+ rc = 0;
- CDEBUG(D_OTHER, "compat function failed and lock modes "
- "incompat\n");
if (send_cbs && child->l_blocking_ast != NULL) {
- CDEBUG(D_OTHER, "incompatible; sending blocking "
- "AST.\n");
- /* It's very difficult to actually send the AST from
- * here, because we'd have to drop the lock before going
- * to sleep to wait for the reply. Instead we build the
- * packet and send it later. */
- ldlm_send_blocking_ast(child, lock);
+ CDEBUG(D_OTHER, "lock %p incompatible; sending "
+ "blocking AST.\n", child);
+ ldlm_add_ast_work_item(child, lock);
}
}
return rc;
}
-/* Args: unlocked lock */
static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
{
int rc;
ENTRY;
- rc = _ldlm_lock_compat(lock, send_cbs, &lock->l_resource->lr_granted);
+ 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? */
- rc |= _ldlm_lock_compat(lock, send_cbs,
- &lock->l_resource->lr_converting);
+ if (rc)
+ rc = ldlm_lock_compat_list
+ (lock, send_cbs, &lock->l_resource->lr_converting);
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
RETURN(rc);
}
-/* Args: locked lock, locked resource */
-void ldlm_grant_lock(struct ldlm_resource *res, struct ldlm_lock *lock)
+/* NOTE: called by
+ - ldlm_handle_enqueuque - resource
+*/
+void ldlm_grant_lock(struct ldlm_lock *lock)
{
- struct ptlrpc_request *req = NULL;
+ 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);
lock->l_granted_mode = lock->l_req_mode;
res->lr_most_restr = lock->l_granted_mode;
if (lock->l_completion_ast) {
- lock->l_completion_ast(lock, NULL, lock->l_data,
- lock->l_data_len, &req);
- if (req != NULL) {
- struct list_head *list = res->lr_tmp;
- if (list == NULL) {
- LBUG();
- return;
- }
- list_add(&req->rq_multi, list);
- }
+ ldlm_add_ast_work_item(lock, NULL);
}
+ l_unlock(&lock->l_resource->lr_namespace->ns_lock);
EXIT;
}
-static int search_queue(struct list_head *queue, ldlm_mode_t mode,
- struct ldlm_extent *extent, struct lustre_handle *lockh)
+/* returns a referenced lock or NULL */
+static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
+ struct ldlm_extent *extent)
{
+ struct ldlm_lock *lock;
struct list_head *tmp;
list_for_each(tmp, queue) {
- struct ldlm_lock *lock;
lock = list_entry(tmp, struct ldlm_lock, l_res_link);
- if (lock->l_flags & LDLM_FL_DYING)
+ if (lock->l_flags & LDLM_FL_CBPENDING)
continue;
/* lock_convert() takes the resource lock, so we're sure that
lock->l_extent.end < extent->end))
continue;
- ldlm_lock_addref(lock, mode);
- ldlm_object2handle(lock, lockh);
- return 1;
+ ldlm_lock_addref_internal(lock, mode);
+ return lock;
}
- return 0;
+ return NULL;
}
/* Must be called with no resource or lock locks held.
*
* Returns 1 if it finds an already-existing lock that is compatible; in this
- * case, lockh is filled in with a addref()ed lock */
-int ldlm_local_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
- void *cookie, int cookielen, ldlm_mode_t mode,
- struct lustre_handle *lockh)
+ * case, lockh is filled in with a addref()ed lock
+*/
+int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
+ void *cookie, int cookielen, ldlm_mode_t mode,
+ struct lustre_handle *lockh)
{
struct ldlm_resource *res;
+ struct ldlm_lock *lock;
int rc = 0;
ENTRY;
if (res == NULL)
RETURN(0);
- spin_lock(&res->lr_lock);
- if (search_queue(&res->lr_granted, mode, cookie, lockh))
+ ns = res->lr_namespace;
+ l_lock(&ns->ns_lock);
+
+ if ((lock = search_queue(&res->lr_granted, mode, cookie)))
GOTO(out, rc = 1);
- if (search_queue(&res->lr_converting, mode, cookie, lockh))
+ if ((lock = search_queue(&res->lr_converting, mode, cookie)))
GOTO(out, rc = 1);
- if (search_queue(&res->lr_waiting, mode, cookie, lockh))
+ if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
GOTO(out, rc = 1);
EXIT;
- out:
- if (!ldlm_resource_put(res))
- spin_unlock(&res->lr_lock);
+ out:
+ ldlm_resource_put(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);
+ }
+ if (rc)
+ LDLM_DEBUG(lock, "matched");
+ else
+ LDLM_DEBUG_NOLOCK("not matched");
return rc;
}
-/* Must be called without the resource lock held. Returns a referenced,
- * unlocked ldlm_lock. */
-ldlm_error_t ldlm_local_lock_create(struct ldlm_namespace *ns,
- struct lustre_handle *parent_lock_handle,
- __u64 *res_id, __u32 type,
- ldlm_mode_t mode,
- void *data,
- __u32 data_len,
- struct lustre_handle *lockh)
+/* Returns a referenced lock */
+struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
+ struct lustre_handle *parent_lock_handle,
+ __u64 * res_id, __u32 type,
+ ldlm_mode_t mode, void *data, __u32 data_len)
{
struct ldlm_resource *res, *parent_res = NULL;
struct ldlm_lock *lock, *parent_lock;
- parent_lock = lustre_handle2object(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);
if (res == NULL)
- RETURN(-ENOMEM);
+ RETURN(NULL);
lock = ldlm_lock_new(parent_lock, res);
if (lock == NULL) {
- spin_lock(&res->lr_lock);
- if (!ldlm_resource_put(res))
- spin_unlock(&res->lr_lock);
- RETURN(-ENOMEM);
+ ldlm_resource_put(res);
+ RETURN(NULL);
}
lock->l_req_mode = mode;
lock->l_data = data;
lock->l_data_len = data_len;
- ldlm_lock_addref(lock, mode);
- ldlm_object2handle(lock, lockh);
- return ELDLM_OK;
+ return lock;
}
/* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
-ldlm_error_t ldlm_local_lock_enqueue(struct lustre_handle *lockh,
- void *cookie, int cookie_len,
- int *flags,
- ldlm_lock_callback completion,
- ldlm_lock_callback blocking)
+ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
+ void *cookie, int cookie_len,
+ int *flags,
+ ldlm_completion_callback completion,
+ ldlm_blocking_callback blocking)
{
struct ldlm_resource *res;
- struct ldlm_lock *lock;
- int incompat = 0, local;
+ int local;
ldlm_res_policy policy;
ENTRY;
- lock = lustre_handle2object(lockh);
res = lock->l_resource;
- local = res->lr_namespace->ns_client;
- spin_lock(&res->lr_lock);
-
lock->l_blocking_ast = blocking;
if (res->lr_type == LDLM_EXTENT)
memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
/* policies are not executed on the client */
+ local = res->lr_namespace->ns_client;
if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
int rc;
-
- /* We do this dancing with refcounts and locks because the
- * policy function could send an RPC */
- res->lr_refcount++;
- spin_unlock(&res->lr_lock);
-
rc = policy(lock, cookie, lock->l_req_mode, NULL);
- spin_lock(&res->lr_lock);
- if (ldlm_resource_put(res))
- res = NULL;
-
if (rc == ELDLM_LOCK_CHANGED) {
- if (res)
- spin_unlock(&res->lr_lock);
res = lock->l_resource;
- spin_lock(&res->lr_lock);
*flags |= LDLM_FL_LOCK_CHANGED;
} else if (rc == ELDLM_LOCK_ABORTED) {
- /* Abort. */
- if (res && !ldlm_resource_put(res))
- spin_unlock(&res->lr_lock);
- ldlm_lock_free(lock);
+ ldlm_lock_destroy(lock);
RETURN(rc);
}
}
/* 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. */
- GOTO(out_noput, ELDLM_OK);
+ GOTO(out, ELDLM_OK);
}
- /* If this is a local resource, put it on the appropriate list. */
- list_del_init(&lock->l_res_link);
+ /* 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
+ * be granted (below). In this case, we do exactly what the server
+ * tells us to do, as dictated by the 'flags' */
+ ldlm_resource_unlink_lock(lock);
if (local) {
if (*flags & LDLM_FL_BLOCK_CONV)
ldlm_resource_add_lock(res, res->lr_converting.prev,
else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
else
- ldlm_grant_lock(res, lock);
+ ldlm_grant_lock(lock);
GOTO(out, ELDLM_OK);
}
*flags |= LDLM_FL_BLOCK_WAIT;
GOTO(out, ELDLM_OK);
}
- incompat = ldlm_lock_compat(lock, 0);
- if (incompat) {
+ if (!ldlm_lock_compat(lock, 0)) {
ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
*flags |= LDLM_FL_BLOCK_GRANTED;
GOTO(out, ELDLM_OK);
}
- ldlm_grant_lock(res, lock);
+ ldlm_grant_lock(lock);
EXIT;
- out:
- /* We're called with a lock that has a referenced resource and is not on
- * any resource list. When we added it to a list, we incurred an extra
- * reference. */
- if (ldlm_resource_put(lock->l_resource))
- res = NULL;
- out_noput:
+ out:
/* Don't set 'completion_ast' until here so that if the lock is granted
* immediately we don't do an unnecessary completion call. */
lock->l_completion_ast = completion;
- if (res)
- spin_unlock(&res->lr_lock);
return ELDLM_OK;
}
-/* Must be called with resource->lr_lock taken. */
+/* Must be called with namespace taken: queue is waiting or converting. */
static int ldlm_reprocess_queue(struct ldlm_resource *res,
- struct list_head *converting)
+ struct list_head *queue)
{
struct list_head *tmp, *pos;
ENTRY;
- list_for_each_safe(tmp, pos, converting) {
+ 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);
- /* the resource lock protects ldlm_lock_compat */
- if (ldlm_lock_compat(pending, 1))
+ if (!ldlm_lock_compat(pending, 1))
RETURN(1);
list_del_init(&pending->l_res_link);
- ldlm_grant_lock(res, pending);
-
- ldlm_lock_addref(pending, pending->l_req_mode);
- ldlm_lock_decref(pending, pending->l_granted_mode);
+ ldlm_grant_lock(pending);
}
RETURN(0);
}
-static void ldlm_send_delayed_asts(struct list_head *rpc_list)
+void ldlm_run_ast_work(struct list_head *rpc_list)
{
struct list_head *tmp, *pos;
+ int rc;
ENTRY;
list_for_each_safe(tmp, pos, rpc_list) {
- int rc;
- struct ptlrpc_request *req =
- list_entry(tmp, struct ptlrpc_request, rq_multi);
-
- CDEBUG(D_INFO, "Sending callback.\n");
+ struct ldlm_ast_work *w =
+ list_entry(tmp, struct ldlm_ast_work, w_list);
- rc = ptlrpc_queue_wait(req);
- rc = ptlrpc_check_status(req, rc);
- ptlrpc_free_req(req);
+ if (w->w_blocking)
+ rc = w->w_lock->l_blocking_ast
+ (w->w_lock, &w->w_desc, w->w_data,
+ w->w_datalen);
+ else
+ rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
if (rc)
- CERROR("Callback send failed: %d\n", rc);
+ CERROR("Failed AST - should clean & disconnect "
+ "client\n");
+ LDLM_LOCK_PUT(w->w_lock);
+ list_del(&w->w_list);
+ OBD_FREE(w, sizeof(*w));
}
EXIT;
}
return;
}
- spin_lock(&res->lr_lock);
+ l_lock(&res->lr_namespace->ns_lock);
res->lr_tmp = &rpc_list;
ldlm_reprocess_queue(res, &res->lr_converting);
ldlm_reprocess_queue(res, &res->lr_waiting);
res->lr_tmp = NULL;
- spin_unlock(&res->lr_lock);
+ l_unlock(&res->lr_namespace->ns_lock);
- ldlm_send_delayed_asts(&rpc_list);
+ ldlm_run_ast_work(&rpc_list);
EXIT;
}
-/* Must be called with lock and lock->l_resource unlocked */
-struct ldlm_resource *ldlm_local_lock_cancel(struct ldlm_lock *lock)
+void ldlm_lock_cancel(struct ldlm_lock *lock)
{
struct ldlm_resource *res;
+ struct ldlm_namespace *ns;
ENTRY;
res = lock->l_resource;
+ ns = res->lr_namespace;
- spin_lock(&res->lr_lock);
- spin_lock(&lock->l_lock);
-
+ l_lock(&ns->ns_lock);
if (lock->l_readers || lock->l_writers)
CDEBUG(D_INFO, "lock still has references (%d readers, %d "
"writers)\n", lock->l_readers, lock->l_writers);
- if (ldlm_resource_del_lock(lock))
- res = NULL; /* res was freed, nothing else to do. */
- else
- spin_unlock(&res->lr_lock);
- ldlm_lock_free(lock);
-
- RETURN(res);
+ ldlm_resource_unlink_lock(lock);
+ ldlm_lock_destroy(lock);
+ l_unlock(&ns->ns_lock);
+ EXIT;
}
-/* Must be called with lock and lock->l_resource unlocked */
-struct ldlm_resource *ldlm_local_lock_convert(struct lustre_handle *lockh,
- int new_mode, int *flags)
+struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
+ int *flags)
{
- struct ldlm_lock *lock;
+ struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
struct ldlm_resource *res;
+ struct ldlm_namespace *ns;
+ int granted = 0;
ENTRY;
- lock = lustre_handle2object(lockh);
res = lock->l_resource;
+ ns = res->lr_namespace;
- spin_lock(&res->lr_lock);
+ l_lock(&ns->ns_lock);
lock->l_req_mode = new_mode;
- list_del_init(&lock->l_res_link);
+ ldlm_resource_unlink_lock(lock);
/* If this is a local resource, put it on the appropriate list. */
if (res->lr_namespace->ns_client) {
- if (*flags & LDLM_FL_BLOCK_CONV)
+ if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
ldlm_resource_add_lock(res, res->lr_converting.prev,
lock);
- else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
- ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
- else
- ldlm_grant_lock(res, lock);
- } else {
- list_add(&lock->l_res_link, res->lr_converting.prev);
- }
+ else {
+ res->lr_tmp = &rpc_list;
+ ldlm_grant_lock(lock);
+ res->lr_tmp = NULL;
+ granted = 1;
+ /* FIXME: completion handling not with ns_lock held ! */
+ if (lock->l_completion_ast)
+ lock->l_completion_ast(lock, 0);
+ }
+ } else
+ list_add_tail(&lock->l_res_link, &res->lr_converting);
- spin_unlock(&res->lr_lock);
+ l_unlock(&ns->ns_lock);
+ if (granted)
+ ldlm_run_ast_work(&rpc_list);
RETURN(res);
}
lock->l_version[2], lock->l_version[3]);
CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
+ if (lock->l_export && lock->l_export->exp_connection)
+ CDEBUG(D_OTHER, " Node: NID %x (rhandle: %Lx)\n",
+ lock->l_export->exp_connection->c_peer.peer_nid,
+ lock->l_remote_handle.addr);
+ else
+ CDEBUG(D_OTHER, " Node: local\n");
CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
CDEBUG(D_OTHER, " Resource: %p (%Ld)\n", lock->l_resource,
lock->l_resource->lr_name[0]);