All of lore.kernel.org
 help / color / mirror / Atom feed
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 11/17] virtio-9p: Implement P9_TWSTAT
Date: Wed,  3 Mar 2010 13:01:08 -0600	[thread overview]
Message-ID: <1267642874-15001-13-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1267642874-15001-1-git-send-email-aliguori@us.ibm.com>

This gets file and directory creation to work

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 |   42 +++++++++
 hw/virtio-9p.c       |  236 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 274 insertions(+), 4 deletions(-)

diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c
index 606f5be..0a9f111 100644
--- a/hw/virtio-9p-local.c
+++ b/hw/virtio-9p-local.c
@@ -207,6 +207,44 @@ static int local_link(void *opaque, const char *oldpath, const char *newpath)
     return err;
 }
 
+static int local_truncate(void *opaque, const char *path, off_t size)
+{
+    return truncate(rpath(path), size);
+}
+
+static int local_rename(void *opaque, const char *oldpath,
+			const char *newpath)
+{
+    char *tmp;
+    int err;
+
+    tmp = strdup(rpath(oldpath));
+    if (tmp == NULL)
+	return -1;
+
+    err = rename(tmp, rpath(newpath));
+    if (err == -1) {
+	int serrno = errno;
+	free(tmp);
+	errno = serrno;
+    } else
+	free(tmp);
+
+    return err;
+
+}
+
+static int local_chown(void *opaque, const char *path, uid_t uid, gid_t gid)
+{
+    return chown(rpath(path), uid, gid);
+}
+
+static int local_utime(void *opaque, const char *path,
+		       const struct utimbuf *buf)
+{
+    return utime(rpath(path), buf);
+}
+
 static V9fsPosixFileOperations ops = {
     .lstat = local_lstat,
     .setuid = local_setuid,
@@ -230,6 +268,10 @@ static V9fsPosixFileOperations ops = {
     .open2 = local_open2,
     .symlink = local_symlink,
     .link = local_link,
+    .truncate = local_truncate,
+    .rename = local_rename,
+    .chown = local_chown,
+    .utime = local_utime,
 };
 
 V9fsPosixFileOperations *virtio_9p_init_local(const char *path)
diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c
index 841fcde..836d65b 100644
--- a/hw/virtio-9p.c
+++ b/hw/virtio-9p.c
@@ -215,6 +215,28 @@ static int posix_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
     return s->ops->link(s->ops->opaque, oldpath->data, newpath->data);
 }
 
+static int posix_truncate(V9fsState *s, V9fsString *path, off_t size)
+{
+    return s->ops->truncate(s->ops->opaque, path->data, size);
+}
+
+static int posix_rename(V9fsState *s, V9fsString *oldpath,
+			V9fsString *newpath)
+{
+    return s->ops->rename(s->ops->opaque, oldpath->data, newpath->data);
+}
+
+static int posix_chown(V9fsState *s, V9fsString *path, uid_t uid, gid_t gid)
+{
+    return s->ops->chown(s->ops->opaque, path->data, uid, gid);
+}
+
+static int posix_utime(V9fsState *s, V9fsString *path,
+		       const struct utimbuf *buf)
+{
+    return s->ops->utime(s->ops->opaque, path->data, buf);
+}
+
 static void v9fs_string_init(V9fsString *str)
 {
     str->data = NULL;
@@ -398,7 +420,8 @@ static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
 }
 
 /* FIXME i can do this with less variables */
-static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src, size_t size)
+static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
+							size_t size)
 {
     struct iovec *sg = pdu->elem.in_sg;
     size_t off = 0;
@@ -1615,7 +1638,8 @@ static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
         uint32_t major, minor;
         mode_t nmode = 0;
 
-        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
+        if (sscanf(vs->extension.data, "%c %u %u", &ctype, &major,
+							&minor) != 3) {
             err = -errno;
             v9fs_post_create(s, vs, err);
         }
@@ -1701,10 +1725,214 @@ static void v9fs_remove(V9fsState *s, V9fsPDU *pdu)
 	pprint_pdu(pdu);
 }
 
+static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
+{
+	mode_t ret;
+
+	ret = mode & 0777;
+	if (mode & P9_STAT_MODE_DIR)
+		ret |= S_IFDIR;
+
+	if (dotu) {
+		if (mode & P9_STAT_MODE_SYMLINK)
+			ret |= S_IFLNK;
+		if (mode & P9_STAT_MODE_SOCKET)
+			ret |= S_IFSOCK;
+		if (mode & P9_STAT_MODE_NAMED_PIPE)
+			ret |= S_IFIFO;
+		if (mode & P9_STAT_MODE_DEVICE) {
+			if (extension && extension->data[0] == 'c')
+				ret |= S_IFCHR;
+			else
+				ret |= S_IFBLK;
+		}
+	}
+
+	if (!(ret&~0777))
+		ret |= S_IFREG;
+
+	if (mode & P9_STAT_MODE_SETUID)
+		ret |= S_ISUID;
+	if (mode & P9_STAT_MODE_SETGID)
+		ret |= S_ISGID;
+	if (mode & P9_STAT_MODE_SETVTX)
+		ret |= S_ISVTX;
+
+	return ret;
+}
+
+typedef struct V9fsWstatState
+{
+    V9fsPDU *pdu;
+    size_t offset;
+    int32_t fid;
+    int16_t unused;
+    V9fsStat v9stat;
+    V9fsFidState *fidp;
+    V9fsString nname;
+} V9fsWstatState;
+
+static void v9fs_wstat_post_truncate(V9fsState *s, V9fsWstatState *vs, int err)
+{
+    if (err < 0) {
+        goto out;
+    }
+
+    err = vs->offset;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_wstat_post_rename(V9fsState *s, V9fsWstatState *vs, int err)
+{
+    if (err < 0) {
+        goto out;
+    }
+
+    if (vs->v9stat.name.size != 0) {
+        v9fs_string_free(&vs->nname);
+    }
+
+    if (vs->v9stat.length != -1) {
+	if (posix_truncate(s, &vs->fidp->path, vs->v9stat.length) < 0) {
+	    err = -errno;
+	}
+    }
+    v9fs_wstat_post_truncate(s, vs, err);
+    return;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_wstat_post_chown(V9fsState *s, V9fsWstatState *vs, int err)
+{
+    if (err < 0) {
+        goto out;
+    }
+
+    if (vs->v9stat.name.size != 0) {
+	char *old_name, *new_name;
+	char *end;
+
+	old_name = vs->fidp->path.data;
+	if ((end = strrchr(old_name, '/')))
+	    end++;
+	else
+	    end = old_name;
+
+	new_name = qemu_malloc(end - old_name + vs->v9stat.name.size + 1);
+	BUG_ON(new_name == NULL);
+
+	memset(new_name, 0, end - old_name + vs->v9stat.name.size + 1);
+	memcpy(new_name, old_name, end - old_name);
+	memcpy(new_name + (end - old_name), vs->v9stat.name.data,
+						vs->v9stat.name.size);
+	vs->nname.data = new_name;
+	vs->nname.size = strlen(new_name);
+
+	if (strcmp(new_name, vs->fidp->path.data) != 0) {
+	    if (posix_rename(s, &vs->fidp->path, &vs->nname)) {
+		err = -errno;
+	    }
+	}
+    }
+    v9fs_wstat_post_rename(s, vs, err);
+    return;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_wstat_post_utime(V9fsState *s, V9fsWstatState *vs, int err)
+{
+    if (err < 0) {
+        goto out;
+    }
+
+    if (vs->v9stat.n_gid != -1) {
+	if (posix_chown(s, &vs->fidp->path, vs->v9stat.n_uid,
+					vs->v9stat.n_gid)) {
+	    err = -errno;
+	}
+    }
+    v9fs_wstat_post_chown(s, vs, err);
+    return;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
+static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err)
+{
+    if (err < 0) {
+        goto out;
+    }
+
+    if (vs->v9stat.mtime != -1) {
+	struct utimbuf tb;
+	tb.actime = 0;
+	tb.modtime = vs->v9stat.mtime;
+	if (posix_utime(s, &vs->fidp->path, &tb)) {
+	    err = -errno;
+	}
+    }
+
+    v9fs_wstat_post_utime(s, vs, err);
+    return;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
+}
+
 static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
 {
-    if (debug_9p_pdu)
-	pprint_pdu(pdu);
+    V9fsWstatState *vs;
+    int err = 0;
+
+    vs = qemu_malloc(sizeof(*vs));
+    vs->pdu = pdu;
+    vs->offset = 7;
+
+    pdu_unmarshal(pdu, vs->offset, "dwS", &vs->fid, &vs->unused, &vs->v9stat);
+
+    vs->fidp = lookup_fid(s, vs->fid);
+    if (vs->fidp == NULL) {
+	err = -EINVAL;
+	goto out;
+    }
+
+    if (vs->v9stat.mode != -1) {
+	if (vs->v9stat.mode & P9_STAT_MODE_DIR && vs->fidp->dir == NULL) {
+	    err = -EIO;
+	    goto out;
+	}
+
+        if (posix_chmod(s, &vs->fidp->path,
+                        v9mode_to_mode(vs->v9stat.mode,
+			&vs->v9stat.extension))) {
+            err = -errno;
+        }
+    }
+
+    v9fs_wstat_post_chmod(s, vs, err);
+    return;
+
+out:
+    v9fs_stat_free(&vs->v9stat);
+    complete_pdu(s, vs->pdu, err);
+    qemu_free(vs);
 }
 
 typedef void (pdu_handler_t)(V9fsState *s, V9fsPDU *pdu);
-- 
1.6.5.2

  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 ` [Qemu-devel] [PATCH 10/17] virtio-9p: Implement P9_TCREATE Anthony Liguori
2010-03-03 19:01 ` Anthony Liguori [this message]
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-13-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.