Signed-off-by: Venkateswararao Jujjuri "<jv...@linux.vnet.ibm.com>
---
 hw/9pfs/virtio-9p.c |  229 ++++++++++++++++-----------------------------------
 1 files changed, 72 insertions(+), 157 deletions(-)

diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c
index d1733ca..14eefac 100644
--- a/hw/9pfs/virtio-9p.c
+++ b/hw/9pfs/virtio-9p.c
@@ -134,11 +134,6 @@ static int v9fs_do_mknod(V9fsState *s, char *name,
     return s->ops->mknod(&s->ctx, name, &cred);
 }
 
-static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf)
-{
-    return s->ops->fstat(&s->ctx, fd, stbuf);
-}
-
 static int v9fs_do_symlink(V9fsState *s, V9fsFidState *fidp,
         const char *oldpath, const char *newpath, gid_t gid)
 {
@@ -2005,137 +2000,66 @@ out:
     qemu_free(vs);
 }
 
-static void v9fs_create_post_getiounit(V9fsState *s, V9fsCreateState *vs)
-{
-    int err;
-    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
-    stat_to_qid(&vs->stbuf, &vs->qid);
-
-    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
-    err = vs->offset;
-
-    complete_pdu(s, vs->pdu, err);
-    v9fs_string_free(&vs->name);
-    v9fs_string_free(&vs->extension);
-    v9fs_string_free(&vs->fullname);
-    qemu_free(vs);
-}
-
-static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
-{
-    if (err == 0) {
-        vs->iounit = get_iounit(s, &vs->fidp->path);
-        v9fs_create_post_getiounit(s, vs);
-        return;
-    }
-
-    complete_pdu(s, vs->pdu, err);
-    v9fs_string_free(&vs->name);
-    v9fs_string_free(&vs->extension);
-    v9fs_string_free(&vs->fullname);
-    qemu_free(vs);
-}
-
-static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
-{
-    if (err) {
-        err = -errno;
-    }
-    v9fs_post_create(s, vs, err);
-}
-
-static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
-                                                                    int err)
+static void v9fs_create(void *opaque)
 {
-    if (!vs->fidp->fs.dir) {
-        err = -errno;
-    }
-    vs->fidp->fid_type = P9_FID_DIR;
-    v9fs_post_create(s, vs, err);
-}
+    int err = 0;
+    int32_t fid;
+    V9fsCreateState *vs;
+    V9fsPDU *pdu = opaque;
+    V9fsState *s = pdu->s;
 
-static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
-                                                                    int err)
-{
-    if (err) {
-        err = -errno;
-        goto out;
-    }
+    vs = qemu_malloc(sizeof(*vs));
+    vs->pdu = pdu;
+    vs->offset = 7;
 
-    vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
-    v9fs_create_post_opendir(s, vs, err);
-    return;
+    v9fs_string_init(&vs->fullname);
 
-out:
-    v9fs_post_create(s, vs, err);
-}
+    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
+                                &vs->perm, &vs->mode, &vs->extension);
 
-static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
-{
-    if (err < 0) {
+    vs->fidp = lookup_fid(s, fid);
+    if (vs->fidp == NULL) {
+        err = -EINVAL;
         goto out;
     }
 
+    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
+                                                        vs->name.data);
     err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
-    v9fs_create_post_dir_lstat(s, vs, err);
-    return;
-
-out:
-    v9fs_post_create(s, vs, err);
-}
-
-static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
-{
-    if (err) {
-        vs->fidp->fid_type = P9_FID_NONE;
-        close(vs->fidp->fs.fd);
-        err = -errno;
-    }
-    v9fs_post_create(s, vs, err);
-    return;
-}
-
-static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
-{
-    if (err < 0) {
-        goto out;
-    }
-    vs->fidp->fid_type = P9_FID_FILE;
-    err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
-    v9fs_create_post_fstat(s, vs, err);
-
-    return;
-
-out:
-    v9fs_post_create(s, vs, err);
-
-}
-
-static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
-{
-
     if (err == 0 || errno != ENOENT) {
         err = -errno;
         goto out;
     }
-
     if (vs->perm & P9_STAT_MODE_DIR) {
         err = v9fs_co_mkdir(s, vs->fullname.data, vs->perm & 0777,
                 vs->fidp->uid, -1);
-        v9fs_create_post_mkdir(s, vs, err);
+        if (!err) {
+            vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
+        }
+        if (err < 0 || !vs->fidp->fs.dir) {
+            err = -errno;
+            vs->fidp->fid_type = P9_FID_NONE;
+        }
+        vs->fidp->fid_type = P9_FID_DIR;
     } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
         err = v9fs_do_symlink(s, vs->fidp, vs->extension.data,
                 vs->fullname.data, -1);
-        v9fs_create_post_perms(s, vs, err);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
     } else if (vs->perm & P9_STAT_MODE_LINK) {
         int32_t nfid = atoi(vs->extension.data);
         V9fsFidState *nfidp = lookup_fid(s, nfid);
         if (nfidp == NULL) {
-            err = -errno;
-            v9fs_post_create(s, vs, err);
+            err = -EINVAL;
+            goto out;
         }
         err = v9fs_do_link(s, &nfidp->path, &vs->fullname);
-        v9fs_create_post_perms(s, vs, err);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
     } else if (vs->perm & P9_STAT_MODE_DEVICE) {
         char ctype;
         uint32_t major, minor;
@@ -2144,7 +2068,7 @@ static void v9fs_create_post_lstat(V9fsState *s, 
V9fsCreateState *vs, int err)
         if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
                                         &minor) != 3) {
             err = -errno;
-            v9fs_post_create(s, vs, err);
+            goto out;
         }
 
         switch (ctype) {
@@ -2156,69 +2080,60 @@ static void v9fs_create_post_lstat(V9fsState *s, 
V9fsCreateState *vs, int err)
             break;
         default:
             err = -EIO;
-            v9fs_post_create(s, vs, err);
+            goto out;
         }
 
         nmode |= vs->perm & 0777;
         err = v9fs_do_mknod(s, vs->fullname.data, nmode,
                 makedev(major, minor), vs->fidp->uid, -1);
-        v9fs_create_post_perms(s, vs, err);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
     } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
         err = v9fs_do_mknod(s, vs->fullname.data, S_IFIFO | (vs->perm & 0777),
                 0, vs->fidp->uid, -1);
-        v9fs_post_create(s, vs, err);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
     } else if (vs->perm & P9_STAT_MODE_SOCKET) {
         err = v9fs_do_mknod(s, vs->fullname.data, S_IFSOCK | (vs->perm & 0777),
                 0, vs->fidp->uid, -1);
-        v9fs_post_create(s, vs, err);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
     } else {
         err = v9fs_co_open2(s, vs->fidp, vs->fullname.data, -1,
-                omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
-
-        v9fs_create_post_open2(s, vs, err);
+                            omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
+        if (err < 0) {
+            err = -errno;
+            goto out;
+        }
+        vs->fidp->fid_type = P9_FID_FILE;
     }
-
-    return;
-
-out:
-    v9fs_post_create(s, vs, err);
-}
-
-static void v9fs_create(void *opaque)
-{
-    V9fsPDU *pdu = opaque;
-    V9fsState *s = pdu->s;
-    int32_t fid;
-    V9fsCreateState *vs;
-    int err = 0;
-
-    vs = qemu_malloc(sizeof(*vs));
-    vs->pdu = pdu;
-    vs->offset = 7;
-
-    v9fs_string_init(&vs->fullname);
-
-    pdu_unmarshal(vs->pdu, vs->offset, "dsdbs", &fid, &vs->name,
-                                &vs->perm, &vs->mode, &vs->extension);
-
-    vs->fidp = lookup_fid(s, fid);
-    if (vs->fidp == NULL) {
-        err = -EINVAL;
+    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
+    if (err < 0) {
+        err = -errno;
+        vs->fidp->fid_type = P9_FID_NONE;
+        if (vs->fidp->fs.fd) {
+            close(vs->fidp->fs.fd);
+        }
         goto out;
     }
-
-    v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
-                                                        vs->name.data);
-
-    err = v9fs_do_lstat(s, &vs->fullname, &vs->stbuf);
-    v9fs_create_post_lstat(s, vs, err);
-    return;
+    vs->iounit = get_iounit(s, &vs->fidp->path);
+    v9fs_string_copy(&vs->fidp->path, &vs->fullname);
+    stat_to_qid(&vs->stbuf, &vs->qid);
+    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, vs->iounit);
+    err = vs->offset;
 
 out:
-    complete_pdu(s, vs->pdu, err);
-    v9fs_string_free(&vs->name);
-    v9fs_string_free(&vs->extension);
-    qemu_free(vs);
+   complete_pdu(s, vs->pdu, err);
+   v9fs_string_free(&vs->name);
+   v9fs_string_free(&vs->extension);
+   v9fs_string_free(&vs->fullname);
+   qemu_free(vs);
 }
 
 static void v9fs_post_symlink(V9fsState *s, V9fsSymlinkState *vs, int err)
-- 
1.7.1


Reply via email to