xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: xen-devel@lists.xenproject.org, jbeulich@suse.com
Cc: Bob Liu <lliubbo@gmail.com>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Subject: [PATCH v2.1 01/15] tmem: refactor function do_tmem_op()
Date: Wed,  9 Apr 2014 09:26:05 -0400	[thread overview]
Message-ID: <1397049979-3479-2-git-send-email-konrad.wilk@oracle.com> (raw)
In-Reply-To: <1397049979-3479-1-git-send-email-konrad.wilk@oracle.com>

From: Bob Liu <lliubbo@gmail.com>

Refactor function do_tmem_op() to make it more readable.

Signed-off-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
[v2: Fixed up tab vs spaces, also removed dead code and added gulped code]
---
 xen/common/tmem.c | 168 +++++++++++++++++++++++++-----------------------------
 1 file changed, 79 insertions(+), 89 deletions(-)

diff --git a/xen/common/tmem.c b/xen/common/tmem.c
index 602a38b..823f166 100644
--- a/xen/common/tmem.c
+++ b/xen/common/tmem.c
@@ -2603,7 +2603,6 @@ long do_tmem_op(tmem_cli_op_t uops)
     bool_t succ_get = 0, succ_put = 0;
     bool_t non_succ_get = 0, non_succ_put = 0;
     bool_t flush = 0, flush_obj = 0;
-    bool_t write_lock_set = 0, read_lock_set = 0;
 
     if ( !tmem_initialized )
         return -ENODEV;
@@ -2626,114 +2625,105 @@ long do_tmem_op(tmem_cli_op_t uops)
         return -EFAULT;
     }
 
+    /* Acquire wirte lock for all command at first */
+    write_lock(&tmem_rwlock);
+
     if ( op.cmd == TMEM_CONTROL )
     {
-        write_lock(&tmem_rwlock);
-        write_lock_set = 1;
         rc = do_tmem_control(&op);
-        goto out;
-    } else if ( op.cmd == TMEM_AUTH ) {
-        write_lock(&tmem_rwlock);
-        write_lock_set = 1;
+    }
+    else if ( op.cmd == TMEM_AUTH )
+    {
         rc = tmemc_shared_pool_auth(op.u.creat.arg1,op.u.creat.uuid[0],
                          op.u.creat.uuid[1],op.u.creat.flags);
-        goto out;
-    } else if ( op.cmd == TMEM_RESTORE_NEW ) {
-        write_lock(&tmem_rwlock);
-        write_lock_set = 1;
+    }
+    else if ( op.cmd == TMEM_RESTORE_NEW )
+    {
         rc = do_tmem_new_pool(op.u.creat.arg1, op.pool_id, op.u.creat.flags,
                          op.u.creat.uuid[0], op.u.creat.uuid[1]);
-        goto out;
     }
-
-    /* create per-client tmem structure dynamically on first use by client */
-    if ( client == NULL )
-    {
-        write_lock(&tmem_rwlock);
-        write_lock_set = 1;
-        if ( (client = client_create(current->domain->domain_id)) == NULL )
+    else {
+    /*
+	 * For other commands, create per-client tmem structure dynamically on
+	 * first use by client.
+	 */
+        if ( client == NULL )
         {
-            tmem_client_err("tmem: can't create tmem structure for %s\n",
-                           tmem_client_str);
-            rc = -ENOMEM;
-            goto out;
+            if ( (client = client_create(current->domain->domain_id)) == NULL )
+            {
+                tmem_client_err("tmem: can't create tmem structure for %s\n",
+                               tmem_client_str);
+                rc = -ENOMEM;
+                goto out;
+            }
         }
-    }
 
-    if ( op.cmd == TMEM_NEW_POOL || op.cmd == TMEM_DESTROY_POOL )
-    {
-        if ( !write_lock_set )
+        if ( op.cmd == TMEM_NEW_POOL || op.cmd == TMEM_DESTROY_POOL )
         {
-            write_lock(&tmem_rwlock);
-            write_lock_set = 1;
+            if ( op.cmd == TMEM_NEW_POOL )
+                rc = do_tmem_new_pool(TMEM_CLI_ID_NULL, 0, op.u.creat.flags,
+                                op.u.creat.uuid[0], op.u.creat.uuid[1]);
+	        else
+                rc = do_tmem_destroy_pool(op.pool_id);
         }
-    }
-    else
-    {
-        if ( !write_lock_set )
+        else
         {
+            if ( ((uint32_t)op.pool_id >= MAX_POOLS_PER_DOMAIN) ||
+                 ((pool = client->pools[op.pool_id]) == NULL) )
+            {
+                tmem_client_err("tmem: operation requested on uncreated pool\n");
+                rc = -ENODEV;
+                goto out;
+            }
+            /* Commands only need read lock */
+            write_unlock(&tmem_rwlock);
             read_lock(&tmem_rwlock);
-            read_lock_set = 1;
-        }
-        if ( ((uint32_t)op.pool_id >= MAX_POOLS_PER_DOMAIN) ||
-             ((pool = client->pools[op.pool_id]) == NULL) )
-        {
-            tmem_client_err("tmem: operation requested on uncreated pool\n");
-            rc = -ENODEV;
-            goto out;
-        }
-    }
 
-    oidp = (struct oid *)&op.u.gen.oid[0];
-    switch ( op.cmd )
-    {
-    case TMEM_NEW_POOL:
-        rc = do_tmem_new_pool(TMEM_CLI_ID_NULL, 0, op.u.creat.flags,
-                              op.u.creat.uuid[0], op.u.creat.uuid[1]);
-        break;
-    case TMEM_PUT_PAGE:
-        if (tmem_ensure_avail_pages())
-            rc = do_tmem_put(pool, oidp, op.u.gen.index, op.u.gen.cmfn,
-                        tmem_cli_buf_null);
-        else
-            rc = -ENOMEM;
-        if (rc == 1) succ_put = 1;
-        else non_succ_put = 1;
-        break;
-    case TMEM_GET_PAGE:
-        rc = do_tmem_get(pool, oidp, op.u.gen.index, op.u.gen.cmfn,
-                        tmem_cli_buf_null);
-        if (rc == 1) succ_get = 1;
-        else non_succ_get = 1;
-        break;
-    case TMEM_FLUSH_PAGE:
-        flush = 1;
-        rc = do_tmem_flush_page(pool, oidp, op.u.gen.index);
-        break;
-    case TMEM_FLUSH_OBJECT:
-        rc = do_tmem_flush_object(pool, oidp);
-        flush_obj = 1;
-        break;
-    case TMEM_DESTROY_POOL:
-        flush = 1;
-        rc = do_tmem_destroy_pool(op.pool_id);
-        break;
-    default:
-        tmem_client_warn("tmem: op %d not implemented\n", op.cmd);
-        rc = -ENOSYS;
-        break;
+            oidp = (struct oid *)&op.u.gen.oid[0];
+            switch ( op.cmd )
+            {
+            case TMEM_NEW_POOL:
+            case TMEM_DESTROY_POOL:
+                BUG(); /* Done earlier. */
+                break;
+            case TMEM_PUT_PAGE:
+                if (tmem_ensure_avail_pages())
+                    rc = do_tmem_put(pool, oidp, op.u.gen.index, op.u.gen.cmfn,
+                                tmem_cli_buf_null);
+                else
+                    rc = -ENOMEM;
+                if (rc == 1) succ_put = 1;
+                else non_succ_put = 1;
+                break;
+            case TMEM_GET_PAGE:
+                rc = do_tmem_get(pool, oidp, op.u.gen.index, op.u.gen.cmfn,
+                                tmem_cli_buf_null);
+                if (rc == 1) succ_get = 1;
+                else non_succ_get = 1;
+                break;
+            case TMEM_FLUSH_PAGE:
+                flush = 1;
+                rc = do_tmem_flush_page(pool, oidp, op.u.gen.index);
+                break;
+            case TMEM_FLUSH_OBJECT:
+                rc = do_tmem_flush_object(pool, oidp);
+                flush_obj = 1;
+                break;
+            default:
+                tmem_client_warn("tmem: op %d not implemented\n", op.cmd);
+                rc = -ENOSYS;
+                break;
+            }
+            read_unlock(&tmem_rwlock);
+            if ( rc < 0 )
+                errored_tmem_ops++;
+            return rc;
+        }
     }
-
 out:
+    write_unlock(&tmem_rwlock);
     if ( rc < 0 )
         errored_tmem_ops++;
-    if ( write_lock_set )
-        write_unlock(&tmem_rwlock);
-    else if ( read_lock_set )
-        read_unlock(&tmem_rwlock);
-    else 
-        ASSERT(0);
-
     return rc;
 }
 
-- 
1.8.5.3

  reply	other threads:[~2014-04-09 13:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-09 13:26 [PATCH/GIT PULL] Cleanups and bug-fix in tmem for v4.5 (v2.1) Konrad Rzeszutek Wilk
2014-04-09 13:26 ` Konrad Rzeszutek Wilk [this message]
2014-04-09 13:26 ` [PATCH v2.1 02/15] tmem: remove pageshift from struct tmem_pool Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 03/15] tmem: cleanup: drop unneeded client/pool initialization Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 04/15] tmem: bugfix in obj allocate path Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 05/15] tmem: cleanup: remove unneed parameter from pgp_delist() Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 06/15] tmem: cleanup: remove unneed parameter from pgp_free() Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 07/15] tmem: cleanup the pgp free path Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 08/15] tmem: drop oneline function client_freeze() Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 09/15] tmem: cleanup: drop global_pool_list Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 10/15] tmem: fix the return value of tmemc_set_var() Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 11/15] tmem: remove unneeded parameters from obj destroy path Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 12/15] tmem: cleanup: refactor function tmemc_shared_pool_auth() Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 13/15] tmem: reorg the shared pool allocate path Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 14/15] tmem: remove useless parameter from client and pool flush Konrad Rzeszutek Wilk
2014-04-09 13:26 ` [PATCH v2.1 15/15] xen: tmem: tmem_try_to_evict_pgp: fix a lock issue Konrad Rzeszutek Wilk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1397049979-3479-2-git-send-email-konrad.wilk@oracle.com \
    --to=konrad.wilk@oracle.com \
    --cc=jbeulich@suse.com \
    --cc=lliubbo@gmail.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).