From patchwork Thu Apr 15 14:11:06 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: 50246 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 838ADB7C8E for ; Fri, 16 Apr 2010 00:32:15 +1000 (EST) Received: from localhost ([127.0.0.1]:40383 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2Q0d-0005Pp-RF for incoming@patchwork.ozlabs.org; Thu, 15 Apr 2010 10:25:35 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2PtM-0003Rh-QC for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:05 -0400 Received: from [140.186.70.92] (port=50430 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2PtG-0003MB-13 for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:18:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2Pn3-0000KN-DW for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:57 -0400 Received: from e28smtp06.in.ibm.com ([122.248.162.6]:41209) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2Pn2-0000Ir-Cq for qemu-devel@nongnu.org; Thu, 15 Apr 2010 10:11:33 -0400 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp06.in.ibm.com (8.14.3/8.13.1) with ESMTP id o3FEBTCt000627 for ; Thu, 15 Apr 2010 19:41:29 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3FEBTAh2797794 for ; Thu, 15 Apr 2010 19:41:29 +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 o3FEBTiL030867 for ; Thu, 15 Apr 2010 19:41:29 +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 o3FEBDaK030329; Thu, 15 Apr 2010 19:41:28 +0530 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 15 Apr 2010 19:41:06 +0530 Message-Id: <1271340671-19558-17-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 16/21] virtio-9p: Add P9_TWRITE 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_TWRITE support. This gets write to file to work Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/file-op-9p.h | 1 + hw/virtio-9p-local.c | 7 +++ hw/virtio-9p.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 118 insertions(+), 2 deletions(-) diff --git a/hw/file-op-9p.h b/hw/file-op-9p.h index c1f5e45..12503c1 100644 --- a/hw/file-op-9p.h +++ b/hw/file-op-9p.h @@ -39,6 +39,7 @@ typedef struct FileOperations struct dirent *(*readdir)(FsContext *, DIR *); void (*seekdir)(FsContext *, DIR *, off_t); 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); void *opaque; } FileOperations; diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 81d1971..87aeba8 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -127,6 +127,12 @@ static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence) return lseek(fd, offset, whence); } +static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov, + int iovcnt) +{ + return writev(fd, iov, iovcnt); +} + FileOperations local_ops = { .lstat = local_lstat, .setuid = local_setuid, @@ -141,4 +147,5 @@ FileOperations local_ops = { .seekdir = local_seekdir, .readv = local_readv, .lseek = local_lseek, + .writev = local_writev, }; diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 480f68b..03d8696 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -97,6 +97,12 @@ static off_t v9fs_do_lseek(V9fsState *s, int fd, off_t offset, int whence) return s->ops->lseek(&s->ctx, fd, offset, whence); } +static int v9fs_do_writev(V9fsState *s, int fd, const struct iovec *iov, + int iovcnt) +{ + return s->ops->writev(&s->ctx, fd, iov, iovcnt); +} + static void v9fs_string_init(V9fsString *str) { str->data = NULL; @@ -1536,11 +1542,113 @@ out: qemu_free(vs); } +typedef struct V9fsWriteState { + V9fsPDU *pdu; + size_t offset; + int32_t len; + int32_t count; + int32_t total; + int64_t off; + V9fsFidState *fidp; + struct iovec iov[128]; /* FIXME: bad, bad, bad */ + struct iovec *sg; + int cnt; +} V9fsWriteState; + +static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs, + ssize_t err) +{ + if (err < 0) { + /* IO error return the error */ + err = -errno; + goto out; + } + vs->total += vs->len; + vs->sg = adjust_sg(vs->sg, vs->len, &vs->cnt); + if (vs->total < vs->count && vs->len > 0) { + do { + if (0) { + print_sg(vs->sg, vs->cnt); + } + vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + if (vs->len == -1) { + err = -errno; + } + v9fs_write_post_writev(s, vs, err); + return; + } + vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total); + + err = vs->offset; +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + +static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err) +{ + if (err == -1) { + err = -errno; + goto out; + } + vs->sg = cap_sg(vs->sg, vs->count, &vs->cnt); + + if (vs->total < vs->count) { + do { + if (0) { + print_sg(vs->sg, vs->cnt); + } + vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + if (vs->len == -1) { + err = -errno; + } + v9fs_write_post_writev(s, vs, err); + return; + } + +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + static void v9fs_write(V9fsState *s, V9fsPDU *pdu) { - if (debug_9p_pdu) { - pprint_pdu(pdu); + int32_t fid; + V9fsWriteState *vs; + ssize_t err; + + vs = qemu_malloc(sizeof(*vs)); + + vs->pdu = pdu; + vs->offset = 7; + vs->sg = vs->iov; + vs->total = 0; + vs->len = 0; + + pdu_unmarshal(vs->pdu, vs->offset, "dqdv", &fid, &vs->off, &vs->count, + vs->sg, &vs->cnt); + + vs->fidp = lookup_fid(s, fid); + if (vs->fidp == NULL) { + err = -EINVAL; + goto out; } + + if (vs->fidp->fd == -1) { + err = -EINVAL; + goto out; + } + + err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET); + + v9fs_write_post_lseek(s, vs, err); + return; + +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); } static void v9fs_create(V9fsState *s, V9fsPDU *pdu)