From patchwork Thu Apr 15 14:11:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 50252 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9C3E9B7C8E for ; Fri, 16 Apr 2010 00:41:27 +1000 (EST) Received: from localhost ([127.0.0.1]:57979 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2QFt-0002pA-NF for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 10:41:21 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2PtU-0003Vj-1P for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:12 -0400 Received: from [140.186.70.92] (port=50263 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtI-0003Bd-E4 for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pn7-0000Lp-OU for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:53 -0400 Received: from e28smtp06.in.ibm.com ([122.248.162.6]:41210) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pn2-0000Iw-Cv for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:34 -0400 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by e28smtp06.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FEBU75000639 for ; Thu, 15 Apr 2010 19:41:30 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBU8W3117178 for ; Thu, 15 Apr 2010 19:41:30 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3FEBU54030896 for ; Thu, 15 Apr 2010 19:41:30 +0530 Received: from skywalker.in.ibm.com ([9.77.204.187]) by d28av01.in.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3FEBDaL030329; Thu, 15 Apr 2010 19:41:29 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:41:07 +0530 Message-Id: <1271340671-19558-18-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1271340671-19558-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: ericvh@gmail.com, aliguori@us.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V5 17/21] virtio-9p: Add P9_TCREATE support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Anthony Liguori Implement P9_TCREATE support. [jvrao@linux.vnet.ibm.com: strdup to qemu_strdup conversion] Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/file-op-9p.h | 8 ++ hw/virtio-9p-local.c | 87 +++++++++++++++++ hw/virtio-9p.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 357 insertions(+), 2 deletions(-) diff --git a/hw/file-op-9p.h b/hw/file-op-9p.h index 12503c1..5049b2e 100644 --- a/hw/file-op-9p.h +++ b/hw/file-op-9p.h @@ -29,11 +29,17 @@ typedef struct FileOperations { int (*lstat)(FsContext *, const char *, struct stat *); ssize_t (*readlink)(FsContext *, const char *, char *, size_t); + int (*chmod)(FsContext *, const char *, mode_t); + int (*mknod)(FsContext *, const char *, mode_t, dev_t); + int (*mksock)(FsContext *, const char *); + int (*symlink)(FsContext *, const char *, const char *); + int (*link)(FsContext *, const char *, const char *); int (*setuid)(FsContext *, uid_t); int (*close)(FsContext *, int); int (*closedir)(FsContext *, DIR *); DIR *(*opendir)(FsContext *, const char *); int (*open)(FsContext *, const char *, int); + int (*open2)(FsContext *, const char *, int, mode_t); void (*rewinddir)(FsContext *, DIR *); off_t (*telldir)(FsContext *, DIR *); struct dirent *(*readdir)(FsContext *, DIR *); @@ -41,6 +47,8 @@ typedef struct FileOperations ssize_t (*readv)(FsContext *, int, const struct iovec *, int); ssize_t (*writev)(FsContext *, int, const struct iovec *, int); off_t (*lseek)(FsContext *, int, off_t, int); + int (*mkdir)(FsContext *, const char *, mode_t); + int (*fstat)(FsContext *, int, struct stat *); void *opaque; } FileOperations; #endif diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 87aeba8..f6781ca 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -12,8 +12,11 @@ */ #include "virtio.h" #include "virtio-9p.h" +#include #include #include +#include +#include static const char *rpath(FsContext *ctx, const char *path) { @@ -133,6 +136,82 @@ static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov, return writev(fd, iov, iovcnt); } +static int local_chmod(FsContext *ctx, const char *path, mode_t mode) +{ + return chmod(rpath(ctx, path), mode); +} + +static int local_mknod(FsContext *ctx, const char *path, mode_t mode, dev_t dev) +{ + return mknod(rpath(ctx, path), mode, dev); +} + +static int local_mksock(FsContext *ctx2, const char *path) +{ + struct sockaddr_un addr; + int s; + + addr.sun_family = AF_UNIX; + snprintf(addr.sun_path, 108, "%s", rpath(ctx2, 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(FsContext *ctx, const char *path, mode_t mode) +{ + return mkdir(rpath(ctx, path), mode); +} + +static int local_fstat(FsContext *ctx, int fd, struct stat *stbuf) +{ + return fstat(fd, stbuf); +} + +static int local_open2(FsContext *ctx, const char *path, int flags, mode_t mode) +{ + return open(rpath(ctx, path), flags, mode); +} + +static int local_symlink(FsContext *ctx, const char *oldpath, + const char *newpath) +{ + return symlink(oldpath, rpath(ctx, newpath)); +} + +static int local_link(FsContext *ctx, const char *oldpath, const char *newpath) +{ + char *tmp = qemu_strdup(rpath(ctx, oldpath)); + int err, serrno = 0; + + if (tmp == NULL) { + return -ENOMEM; + } + + err = link(tmp, rpath(ctx, newpath)); + if (err == -1) { + serrno = errno; + } + + qemu_free(tmp); + + if (err == -1) { + errno = serrno; + } + + return err; +} + FileOperations local_ops = { .lstat = local_lstat, .setuid = local_setuid, @@ -148,4 +227,12 @@ FileOperations local_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, }; diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 03d8696..faf13b2 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -103,6 +103,47 @@ static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov, return s->ops->writev(&s->ctx, fd, iov, iovcnt); } +static int v9fs_do_chmod(V9fsState *s, V9fsString *path, mode_t mode) +{ + return s->ops->chmod(&s->ctx, path->data, mode); +} + +static int v9fs_do_mknod(V9fsState *s, V9fsString *path, mode_t mode, dev_t dev) +{ + return s->ops->mknod(&s->ctx, path->data, mode, dev); +} + +static int v9fs_do_mksock(V9fsState *s, V9fsString *path) +{ + return s->ops->mksock(&s->ctx, path->data); +} + +static int v9fs_do_mkdir(V9fsState *s, V9fsString *path, mode_t mode) +{ + return s->ops->mkdir(&s->ctx, path->data, mode); +} + +static int v9fs_do_fstat(V9fsState *s, int fd, struct stat *stbuf) +{ + return s->ops->fstat(&s->ctx, fd, stbuf); +} + +static int v9fs_do_open2(V9fsState *s, V9fsString *path, int flags, mode_t mode) +{ + return s->ops->open2(&s->ctx, path->data, flags, mode); +} + +static int v9fs_do_symlink(V9fsState *s, V9fsString *oldpath, + V9fsString *newpath) +{ + return s->ops->symlink(&s->ctx, oldpath->data, newpath->data); +} + +static int v9fs_do_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath) +{ + return s->ops->link(&s->ctx, oldpath->data, newpath->data); +} + static void v9fs_string_init(V9fsString *str) { str->data = NULL; @@ -1651,11 +1692,230 @@ out: qemu_free(vs); } +typedef struct V9fsCreateState { + V9fsPDU *pdu; + size_t offset; + 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 = v9fs_do_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 = 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_mksock(V9fsState *s, V9fsCreateState *vs, + int err) +{ + if (err) { + err = -errno; + goto out; + } + + err = v9fs_do_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 = v9fs_do_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 = -errno; + goto out; + } + + if (vs->perm & P9_STAT_MODE_DIR) { + err = v9fs_do_mkdir(s, &vs->fullname, vs->perm & 0777); + v9fs_create_post_mkdir(s, vs, err); + } else if (vs->perm & P9_STAT_MODE_SYMLINK) { + err = v9fs_do_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 = v9fs_do_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 = v9fs_do_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 = v9fs_do_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 = v9fs_do_mksock(s, &vs->fullname); + v9fs_create_post_mksock(s, vs, err); + } else { + vs->fidp->fd = v9fs_do_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); + 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; + 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; + +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)