Patchwork [-v2,06/22] virtio-9p: Implement P9_TOPEN

login
register
mail settings
Submitter Aneesh Kumar K.V
Date March 16, 2010, 9:15 a.m.
Message ID <1268730920-14584-7-git-send-email-aneesh.kumar@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/47822/
State New
Headers show

Comments

Aneesh Kumar K.V - March 16, 2010, 9:15 a.m.
From: Anthony Liguori <aliguori@us.ibm.com>

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Gautham R Shenoy <ego@in.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
---
 hw/virtio-9p-local.c |   12 ++++
 hw/virtio-9p.c       |  145 +++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 151 insertions(+), 6 deletions(-)

Patch

diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c
index 409f5b0..d8cb70d 100644
--- a/hw/virtio-9p-local.c
+++ b/hw/virtio-9p-local.c
@@ -88,12 +88,24 @@  static int local_closedir(void *opaque, DIR *dir)
     return closedir(dir);
 }
 
+static int local_open(void *opaque, const char *path, int flags)
+{
+    return open(rpath(path), flags);
+}
+
+static DIR *local_opendir(void *opaque, const char *path)
+{
+    return opendir(rpath(path));
+}
+
 static V9fsPosixFileOperations ops = {
     .lstat = local_lstat,
     .setuid = local_setuid,
     .readlink = local_readlink,
     .close = local_close,
     .closedir = local_closedir,
+    .open = local_open,
+    .opendir = local_opendir,
 };
 
 V9fsPosixFileOperations *virtio_9p_init_local(const char *path)
diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c
index 784d399..f1df0b9 100644
--- a/hw/virtio-9p.c
+++ b/hw/virtio-9p.c
@@ -127,6 +127,16 @@  static int posix_closedir(V9fsState *s, DIR *dir)
     return s->ops->closedir(s->ops->opaque, dir);
 }
 
+static int posix_open(V9fsState *s, V9fsString *path, int flags)
+{
+    return s->ops->open(s->ops->opaque, path->data, flags);
+}
+
+static DIR *posix_opendir(V9fsState *s, V9fsString *path)
+{
+    return s->ops->opendir(s->ops->opaque, path->data);
+}
+
 static void v9fs_string_init(V9fsString *str)
 {
     str->data = NULL;
@@ -910,15 +920,138 @@  out:
     v9fs_walk_complete(s, vs, err);
 }
 
-static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
+typedef struct V9fsOpenState {
+    V9fsPDU *pdu;
+    size_t offset;
+    int32_t fid;
+    int8_t mode;
+    V9fsFidState *fidp;
+    V9fsQID qid;
+    struct stat stbuf;
+
+} V9fsOpenState;
+
+enum {
+    Oread		= 0x00,
+    Owrite		= 0x01,
+    Ordwr		= 0x02,
+    Oexec		= 0x03,
+    Oexcl		= 0x04,
+    Otrunc		= 0x10,
+    Orexec		= 0x20,
+    Orclose		= 0x40,
+    Oappend		= 0x80,
+};
+
+static int omode_to_uflags(int8_t mode)
 {
-    if (debug_9p_pdu)
-        pprint_pdu(pdu);
+    int ret = 0;
+
+    switch (mode & 3) {
+        case Oread:
+            ret = O_RDONLY;
+            break;
+        case Ordwr:
+            ret = O_RDWR;
+            break;
+        case Owrite:
+            ret = O_WRONLY;
+            break;
+        case Oexec:
+            ret = O_RDONLY;
+            break;
+    }
+
+    if (mode & Otrunc)
+        ret |= O_TRUNC;
+
+    if (mode & Oappend)
+        ret |= O_APPEND;
+
+    if (mode & Oexcl)
+        ret |= O_EXCL;
+
+    return ret;
+}
+
+static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
+{
+    if (vs->fidp->dir == NULL) {
+        err = -errno;
+        goto out;
+    }
+
+    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
+    err = vs->offset;
+out:
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+
 }
 
-    static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
-{    if (debug_9p_pdu)
-    pprint_pdu(pdu);
+static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
+{
+    if (vs->fidp->fd == -1) {
+        err = -errno;
+        goto out;
+    }
+
+    vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
+    err = vs->offset;
+out:
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
+{
+    BUG_ON(err == -1);
+
+    stat_to_qid(&vs->stbuf, &vs->qid);
+
+    if (S_ISDIR(vs->stbuf.st_mode)) {
+        vs->fidp->dir = posix_opendir(s, &vs->fidp->path);
+        v9fs_open_post_opendir(s, vs, err);
+    } else {
+        vs->fidp->fd = posix_open(s, &vs->fidp->path,
+                                    omode_to_uflags(vs->mode));
+        v9fs_open_post_open(s, vs, err);
+    }
+
+}
+
+static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
+{
+
+    V9fsOpenState *vs;
+    ssize_t err = 0;
+
+
+    vs = qemu_malloc(sizeof(*vs));
+    vs->pdu = pdu;
+    vs->offset = 7;
+
+    pdu_unmarshal(vs->pdu, vs->offset, "db", &vs->fid, &vs->mode);
+
+    vs->fidp = lookup_fid(s, vs->fid);
+    if (vs->fidp == NULL) {
+        err = -ENOENT;
+        goto out;
+    }
+
+    err = posix_lstat(s, &vs->fidp->path, &vs->stbuf);
+
+    v9fs_open_post_lstat(s, vs, err);
+    return;
+out:
+    complete_pdu(s, pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu)
+{
+    if (debug_9p_pdu)
+	pprint_pdu(pdu);
 }
 
 static void v9fs_read(V9fsState *s, V9fsPDU *pdu)