[openib-general] [PATCH] ucma : Encapsulate duplicate code to common routine
Krishna Kumar
krkumar2 at in.ibm.com
Tue Sep 19 00:02:06 PDT 2006
Encapsulate duplicate code to common routine - avoid checking same
errors in multiple places.
Signed-off-by: Krishna Kumar <krkumar2 at in.ibm.com>
--------
diff -ruNp org/core/ucma.c new/core/ucma.c
--- org/core/ucma.c 2006-09-18 17:38:12.000000000 +0530
+++ new/core/ucma.c 2006-09-18 17:39:34.000000000 +0530
@@ -87,20 +87,30 @@ struct ucma_event {
static DEFINE_MUTEX(ctx_mutex);
static DEFINE_IDR(ctx_idr);
-static struct ucma_context* ucma_get_ctx(struct ucma_file *file, int id)
+/* _ucma_find_context : internal find routine. Assumes ctx_mutex is held */
+static inline struct ucma_context* _ucma_find_context(int id)
{
struct ucma_context *ctx;
- mutex_lock(&ctx_mutex);
+ BUG_ON(!mutex_is_locked(&ctx_mutex));
+
ctx = idr_find(&ctx_idr, id);
if (!ctx)
ctx = ERR_PTR(-ENOENT);
else if (ctx->file != file)
ctx = ERR_PTR(-EINVAL);
- else
+ return ctx;
+}
+
+static struct ucma_context* ucma_get_ctx(struct ucma_file *file, int id)
+{
+ struct ucma_context *ctx;
+
+ mutex_lock(&ctx_mutex);
+ ctx = _ucma_find_context(id);
+ if (!IS_ERR(ctx))
atomic_inc(&ctx->ref);
mutex_unlock(&ctx_mutex);
-
return ctx;
}
@@ -354,12 +364,8 @@ static ssize_t ucma_destroy_id(struct uc
return -EFAULT;
mutex_lock(&ctx_mutex);
- ctx = idr_find(&ctx_idr, cmd.id);
- if (!ctx)
- ctx = ERR_PTR(-ENOENT);
- else if (ctx->file != file)
- ctx = ERR_PTR(-EINVAL);
- else
+ ctx = _ucma_find_context(cmd.id);
+ if (!IS_ERR(ctx))
idr_remove(&ctx_idr, ctx->id);
mutex_unlock(&ctx_mutex);
More information about the general
mailing list