Patchwork [2/9] Introduce lock fid_list_lock to protect the fid list.

login
register
mail settings
Submitter Arun Bharadwaj
Date Oct. 14, 2010, 12:23 p.m.
Message ID <20101014122348.2238.88570.stgit@localhost6.localdomain6>
Download mbox | patch
Permalink /patch/67812/
State New
Headers show

Comments

Arun Bharadwaj - Oct. 14, 2010, 12:23 p.m.
From: Sripathi Kodi <sripathik@in.ibm.com>

Currently it is a normal mutex, but I will change it into a rw mutex.
This should be held in read mode while looking up and in write mode
while updating the list.

Signed-off-by: Sripathi Kodi <sripathik@in.ibm.com>
---
 hw/virtio-9p.c |   32 +++++++++++++++++++++++++++++---
 hw/virtio-9p.h |    2 ++
 2 files changed, 31 insertions(+), 3 deletions(-)

Patch

diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c
index 174300d..02a4ec4 100644
--- a/hw/virtio-9p.c
+++ b/hw/virtio-9p.c
@@ -18,7 +18,6 @@ 
 #include "fsdev/qemu-fsdev.h"
 #include "virtio-9p-debug.h"
 #include "virtio-9p-xattr.h"
-#include "qemu-threadlets.h"
 
 int debug_9p_pdu;
 
@@ -552,7 +551,8 @@  static size_t v9fs_string_size(V9fsString *str)
     return str->size;
 }
 
-static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
+/* fid_list_lock should be held on entry */
+static V9fsFidState *__lookup_fid(V9fsState *s, int32_t fid)
 {
     V9fsFidState *f;
 
@@ -565,12 +565,26 @@  static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
     return NULL;
 }
 
+static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)
+{
+    V9fsFidState *f;
+
+    qemu_rwmutex_rdlock(&s->fid_list_lock);
+    f = __lookup_fid(s, fid);
+    qemu_rwmutex_unlock(&s->fid_list_lock);
+
+    return f;
+}
+
+
 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
 {
     V9fsFidState *f;
 
-    f = lookup_fid(s, fid);
+    qemu_rwmutex_wrlock(&s->fid_list_lock);
+    f = __lookup_fid(s, fid);
     if (f) {
+        qemu_rwmutex_unlock(&s->fid_list_lock);
         return NULL;
     }
 
@@ -582,6 +596,7 @@  static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
     f->next = s->fid_list;
     s->fid_list = f;
 
+    qemu_rwmutex_unlock(&(s->fid_list_lock));
     return f;
 }
 
@@ -619,11 +634,13 @@  free_value:
     return retval;
 }
 
+/* On entry, fid_list_lock and fid_lock should NOT be held */
 static int free_fid(V9fsState *s, int32_t fid)
 {
     int retval = 0;
     V9fsFidState **fidpp, *fidp;
 
+    qemu_rwmutex_wrlock(&s->fid_list_lock);
     for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
         if ((*fidpp)->fid == fid) {
             break;
@@ -631,12 +648,18 @@  static int free_fid(V9fsState *s, int32_t fid)
     }
 
     if (*fidpp == NULL) {
+        qemu_rwmutex_unlock(&s->fid_list_lock);
         return -ENOENT;
     }
 
     fidp = *fidpp;
     *fidpp = fidp->next;
 
+    /* We have removed the node from the list, so we can release
+     * the fid_list_lock now
+     */
+    qemu_rwmutex_unlock(&s->fid_list_lock);
+
     if (fidp->fid_type == P9_FID_FILE) {
         v9fs_do_close(s, fidp->fs.fd);
     } else if (fidp->fid_type == P9_FID_DIR) {
@@ -2922,6 +2945,7 @@  static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
             * Fixup fid's pointing to the old name to
             * start pointing to the new name
             */
+            qemu_rwmutex_rdlock(&s->fid_list_lock);
             for (fidp = s->fid_list; fidp; fidp = fidp->next) {
                 if (vs->fidp == fidp) {
                     /*
@@ -2937,6 +2961,7 @@  static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
                                   strlen(vs->fidp->path.data));
                 }
             }
+            qemu_rwmutex_unlock(&s->fid_list_lock);
             v9fs_string_copy(&vs->fidp->path, &vs->name);
         }
     }
@@ -3880,6 +3905,7 @@  VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
     qemu_set_fd_handler(fds[0], v9fs_process_post_ops, NULL, NULL);
     QTAILQ_INIT(&v9fs_async_struct.post_op_list);
     qemu_mutex_init(&(v9fs_async_struct.lock));
+    qemu_rwmutex_init(&(s->fid_list_lock));
     /* Create async queue. */
 
     (void)v9fs_do_async_posix;
diff --git a/hw/virtio-9p.h b/hw/virtio-9p.h
index 6c23319..f58d3d8 100644
--- a/hw/virtio-9p.h
+++ b/hw/virtio-9p.h
@@ -7,6 +7,7 @@ 
 #include <utime.h>
 
 #include "file-op-9p.h"
+#include "qemu-threadlets.h"
 
 /* The feature bitmap for virtio 9P */
 /* The mount point is specified in a config variable */
@@ -206,6 +207,7 @@  typedef struct V9fsState
     V9fsPDU pdus[MAX_REQ];
     QLIST_HEAD(, V9fsPDU) free_list;
     V9fsFidState *fid_list;
+    QemuRWMutex fid_list_lock;
     FileOperations *ops;
     FsContext ctx;
     uint16_t tag_len;