From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>,
"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>,
Gautham R Shenoy <ego@in.ibm.com>
Subject: [Qemu-devel] [PATCH 10/17] virtio-9p: Implement P9_TCREATE
Date: Wed, 3 Mar 2010 13:01:07 -0600 [thread overview]
Message-ID: <1267642874-15001-12-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1267642874-15001-1-git-send-email-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 | 80 +++++++++++++++
hw/virtio-9p.c | 267 +++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 345 insertions(+), 2 deletions(-)
diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c
index 441d22d..606f5be 100644
--- a/hw/virtio-9p-local.c
+++ b/hw/virtio-9p-local.c
@@ -135,6 +135,78 @@ static ssize_t local_writev(void *opaque, int fd, const struct iovec *iov,
return writev(fd, iov, iovcnt);
}
+static int local_chmod(void *opaque, const char *path, mode_t mode)
+{
+ return chmod(rpath(path), mode);
+}
+
+static int local_mknod(void *opaque, const char *path, mode_t mode, dev_t dev)
+{
+ return mknod(rpath(path), mode, dev);
+}
+
+static int local_mksock(void *opaque, const char *path)
+{
+ struct sockaddr_un addr;
+ int s;
+
+ addr.sun_family = AF_UNIX;
+ snprintf(addr.sun_path, 108, "%s", rpath(path));
+
+ s = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (s == -1)
+ return -1;
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
+ close(s);
+ return -1;
+ }
+
+ close(s);
+ return 0;
+}
+
+static int local_mkdir(void *opaque, const char *path, mode_t mode)
+{
+ return mkdir(rpath(path), mode);
+}
+
+static int local_fstat(void *opaque, int fd, struct stat *stbuf)
+{
+ return fstat(fd, stbuf);
+}
+
+static int local_open2(void *opaque, const char *path, int flags, mode_t mode)
+{
+ return open(rpath(path), flags, mode);
+}
+
+static int local_symlink(void *opaque, const char *oldpath,
+ const char *newpath)
+{
+ return symlink(oldpath, rpath(newpath));
+}
+
+static int local_link(void *opaque, const char *oldpath, const char *newpath)
+{
+ char *tmp = strdup(rpath(oldpath));
+ int err, serrno = 0;
+
+ if (tmp == NULL)
+ return -ENOMEM;
+
+ err = link(tmp, rpath(newpath));
+ if (err == -1)
+ serrno = errno;
+
+ free(tmp);
+
+ if (err == -1)
+ errno = serrno;
+
+ return err;
+}
+
static V9fsPosixFileOperations ops = {
.lstat = local_lstat,
.setuid = local_setuid,
@@ -150,6 +222,14 @@ static V9fsPosixFileOperations ops = {
.readv = local_readv,
.lseek = local_lseek,
.writev = local_writev,
+ .chmod = local_chmod,
+ .mknod = local_mknod,
+ .mksock = local_mksock,
+ .mkdir = local_mkdir,
+ .fstat = local_fstat,
+ .open2 = local_open2,
+ .symlink = local_symlink,
+ .link = local_link,
};
V9fsPosixFileOperations *virtio_9p_init_local(const char *path)
diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c
index b0662ba..841fcde 100644
--- a/hw/virtio-9p.c
+++ b/hw/virtio-9p.c
@@ -174,6 +174,47 @@ static int posix_writev(V9fsState *s, int fd, const struct iovec *iov,
return s->ops->writev(s->ops->opaque, fd, iov, iovcnt);
}
+static int posix_chmod(V9fsState *s, V9fsString *path, mode_t mode)
+{
+ return s->ops->chmod(s->ops->opaque, path->data, mode);
+}
+
+static int posix_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev)
+{
+ return s->ops->mknod(s->ops->opaque, path->data, mode, dev);
+}
+
+static int posix_mksock(V9fsState *s, V9fsString *path)
+{
+ return s->ops->mksock(s->ops->opaque, path->data);
+}
+
+static int posix_mkdir(V9fsState *s, V9fsString *path, mode_t mode)
+{
+ return s->ops->mkdir(s->ops->opaque, path->data, mode);
+}
+
+static int posix_fstat(V9fsState *s, int fd, struct stat *stbuf)
+{
+ return s->ops->fstat(s->ops->opaque, fd, stbuf);
+}
+
+static int posix_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode)
+{
+ return s->ops->open2(s->ops->opaque, path->data, flags, mode);
+}
+
+static int posix_symlink(V9fsState *s, V9fsString *oldpath,
+ V9fsString *newpath)
+{
+ return s->ops->symlink(s->ops->opaque, oldpath->data, newpath->data);
+}
+
+static int posix_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
+{
+ return s->ops->link(s->ops->opaque, oldpath->data, newpath->data);
+}
+
static void v9fs_string_init(V9fsString *str)
{
str->data = NULL;
@@ -1420,10 +1461,232 @@ out:
qemu_free(vs);
}
+typedef struct V9fsCreateState {
+ V9fsPDU *pdu;
+ size_t offset;
+ int32_t fid;
+ V9fsFidState *fidp;
+ V9fsQID qid;
+ int32_t perm;
+ int8_t mode;
+ struct stat stbuf;
+ V9fsString name;
+ V9fsString extension;
+ V9fsString fullname;
+} V9fsCreateState;
+
+static void v9fs_post_create(V9fsState *s, V9fsCreateState *vs, int err)
+{
+ if (err == 0) {
+ 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, 0);
+
+ 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_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)
+{
+ if (!vs->fidp->dir) {
+ err = -errno;
+ }
+ v9fs_post_create(s, vs, err);
+}
+
+static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
+ int err)
+{
+ if (err) {
+ err = -errno;
+ goto out;
+ }
+
+ vs->fidp->dir = posix_opendir(s, &vs->fullname);
+ v9fs_create_post_opendir(s, vs, err);
+ return;
+
+out:
+ v9fs_post_create(s, vs, err);
+}
+
+static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
+{
+ if (err) {
+ err = -errno;
+ goto out;
+ }
+
+ err = posix_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_mksock(V9fsState *s, V9fsCreateState *vs,
+ int err)
+{
+ if (err) {
+ err = -errno;
+ goto out;
+ }
+
+ err = posix_chmod(s, &vs->fullname, vs->perm & 0777);
+ v9fs_create_post_perms(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->fd = -1;
+ err = -errno;
+ }
+
+ v9fs_post_create(s, vs, err);
+ return;
+}
+
+static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
+{
+ if (vs->fidp->fd == -1) {
+ err = -errno;
+ goto out;
+ }
+
+ err = posix_fstat(s, vs->fidp->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 = -EEXIST;
+ goto out;
+ }
+
+ if (vs->perm & P9_STAT_MODE_DIR) {
+ err = posix_mkdir(s, &vs->fullname, vs->perm & 0777);
+ v9fs_create_post_mkdir(s, vs, err);
+ } else if (vs->perm & P9_STAT_MODE_SYMLINK) {
+ err = posix_symlink(s, &vs->extension, &vs->fullname);
+ v9fs_create_post_perms(s, vs, err);
+ } 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 = posix_link(s, &nfidp->path, &vs->fullname);
+ v9fs_create_post_perms(s, vs, err);
+ } else if (vs->perm & P9_STAT_MODE_DEVICE) {
+ char ctype;
+ uint32_t major, minor;
+ mode_t nmode = 0;
+
+ if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
+ err = -errno;
+ v9fs_post_create(s, vs, err);
+ }
+
+ switch (ctype) {
+ case 'c':
+ nmode = S_IFCHR;
+ break;
+ case 'b':
+ nmode = S_IFBLK;
+ break;
+ default:
+ err = -EIO;
+ v9fs_post_create(s, vs, err);
+ }
+
+ nmode |= vs->perm & 0777;
+ err = posix_mknod(s, &vs->fullname, nmode, makedev(major, minor));
+ v9fs_create_post_perms(s, vs, err);
+ } else if (vs->perm & P9_STAT_MODE_NAMED_PIPE) {
+ err = posix_mknod(s, &vs->fullname, S_IFIFO | (vs->mode & 0777), 0);
+ v9fs_post_create(s, vs, err);
+ } else if (vs->perm & P9_STAT_MODE_SOCKET) {
+ err = posix_mksock(s, &vs->fullname);
+ v9fs_create_post_mksock(s, vs, err);
+ } else {
+ vs->fidp->fd = posix_open2(s, &vs->fullname,
+ omode_to_uflags(vs->mode) | O_CREAT,
+ vs->perm & 0777);
+ v9fs_create_post_open2(s, vs, err);
+ }
+
+ return;
+
+out:
+ v9fs_post_create(s, vs, err);
+}
+
static void v9fs_create(V9fsState *s, V9fsPDU *pdu)
{
- if (debug_9p_pdu)
- pprint_pdu(pdu);
+
+ 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", &vs->fid, &vs->name,
+ &vs->perm, &vs->mode, &vs->extension);
+
+ vs->fidp = lookup_fid(s, vs->fid);
+ if (vs->fidp == NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+
+ v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
+ vs->name.data);
+
+ err = posix_lstat(s, &vs->fullname, &vs->stbuf);
+ v9fs_create_post_lstat(s, vs, err);
+ return;
+
+out:
+ complete_pdu(s, vs->pdu, err);
+ v9fs_string_free(&vs->name);
+ v9fs_string_free(&vs->extension);
+ qemu_free(vs);
}
static void v9fs_flush(V9fsState *s, V9fsPDU *pdu)
--
1.6.5.2
next prev parent reply other threads:[~2010-03-03 19:01 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-03 19:00 [Qemu-devel] [PATCH 00/17][RFC] virtio-9p: paravirtual filesystem passthrough Anthony Liguori
2010-03-03 19:00 ` Anthony Liguori
2010-03-03 19:00 ` [Qemu-devel] [PATCH 01/17] vitio-9p: Add a virtio 9p device to qemu Anthony Liguori
2010-03-03 19:00 ` [Qemu-devel] [PATCH 02/17] vrtio-9p: Implement P9_TVERSION for 9P Anthony Liguori
2010-03-04 9:23 ` [Qemu-devel] " Michael S. Tsirkin
2010-03-04 14:30 ` Aneesh Kumar K. V
2010-03-03 19:01 ` [Qemu-devel] [PATCH 03/17] virtio-9p: Implement P9_TATTACH Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 04/17] virtio-9p: Implement P9_TSTAT Anthony Liguori
2010-03-03 20:35 ` malc
2010-03-04 14:15 ` Aneesh Kumar K. V
2010-03-09 2:08 ` jvrao
2010-03-09 12:30 ` Paul Brook
2010-03-09 14:35 ` Aneesh Kumar K. V
2010-03-09 15:59 ` jvrao
2010-03-11 16:37 ` Paul Brook
2010-03-03 19:01 ` [Qemu-devel] [PATCH 05/17] virtio-9p: Implement P9_TWALK Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 06/17] virtio-9p: Implement P9_TOPEN Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 07/17] virtio-9p: Implement P9_TREAD Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 08/17] virtio-9p: Implement P9_TCLUNK Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 09/17] virtio-9p: Implement P9_TWRITE Anthony Liguori
2010-03-03 19:01 ` Anthony Liguori [this message]
2010-03-03 19:01 ` [Qemu-devel] [PATCH 11/17] virtio-9p: Implement P9_TWSTAT Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 12/17] virtio-9p: Implement P9_TREMOVE Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 13/17] virtio-9p: Implement P9_TFLUSH Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 14/17] virtio-9p: Add multiple mount point support Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 15/17] virtio-9p: Use little endian format on virtio Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 16/17] virtio-9p: Add support for hardlink Anthony Liguori
2010-03-03 19:01 ` [Qemu-devel] [PATCH 17/17] Implement sync support in 9p server Anthony Liguori
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=1267642874-15001-12-git-send-email-aliguori@us.ibm.com \
--to=aliguori@us.ibm.com \
--cc=aneesh.kumar@linux.vnet.ibm.com \
--cc=ego@in.ibm.com \
--cc=qemu-devel@nongnu.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).