From patchwork Thu Apr 29 12:14:56 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: 51287 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 03045B7D2F for ; Thu, 29 Apr 2010 23:07:53 +1000 (EST) Received: from localhost ([127.0.0.1]:59262 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7TF7-0001ZF-W0 for incoming@patchwork.ozlabs.org; Thu, 29 Apr 2010 08:53:26 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O7Sf5-0005xG-C9 for qemu-devel@nongnu.org; Thu, 29 Apr 2010 08:16:11 -0400 Received: from [140.186.70.92] (port=45959 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O7Sf0-0005sK-78 for qemu-devel@nongnu.org; Thu, 29 Apr 2010 08:16:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O7Ser-0006vH-1l for qemu-devel@nongnu.org; Thu, 29 Apr 2010 08:16:06 -0400 Received: from e23smtp08.au.ibm.com ([202.81.31.141]:50270) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O7Seq-0006ut-8G for qemu-devel@nongnu.org; Thu, 29 Apr 2010 08:15:57 -0400 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [202.81.31.245]) by e23smtp08.au.ibm.com (8.14.3/8.13.1) with ESMTP id o3TCG77X022530 for ; Thu, 29 Apr 2010 22:16:07 +1000 Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3TCFtgG1720458 for ; Thu, 29 Apr 2010 22:15:55 +1000 Received: from d23av04.au.ibm.com (loopback [127.0.0.1]) by d23av04.au.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o3TCFsb1020867 for ; Thu, 29 Apr 2010 22:15:54 +1000 Received: from skywalker.in.ibm.com ([9.124.222.113]) by d23av04.au.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o3TCF89B018931; Thu, 29 Apr 2010 22:15:52 +1000 From: "Aneesh Kumar K.V" To: qemu-devel@nongnu.org Date: Thu, 29 Apr 2010 17:44:56 +0530 Message-Id: <1272543303-9830-15-git-send-email-aneesh.kumar@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4.360.g11766c In-Reply-To: <1272543303-9830-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1272543303-9830-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, jvrao@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH -V6 14/21] virtio-9p: Add P9_TREAD 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_TREAD support. Signed-off-by: Anthony Liguori Signed-off-by: Aneesh Kumar K.V --- hw/file-op-9p.h | 6 ++ hw/virtio-9p-local.c | 37 ++++++++ hw/virtio-9p.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 2 deletions(-) diff --git a/hw/file-op-9p.h b/hw/file-op-9p.h index 715f2d0..c1f5e45 100644 --- a/hw/file-op-9p.h +++ b/hw/file-op-9p.h @@ -34,6 +34,12 @@ typedef struct FileOperations int (*closedir)(FsContext *, DIR *); DIR *(*opendir)(FsContext *, const char *); int (*open)(FsContext *, const char *, int); + void (*rewinddir)(FsContext *, DIR *); + off_t (*telldir)(FsContext *, DIR *); + struct dirent *(*readdir)(FsContext *, DIR *); + void (*seekdir)(FsContext *, DIR *, off_t); + ssize_t (*readv)(FsContext *, int, const struct iovec *, int); + off_t (*lseek)(FsContext *, int, off_t, int); void *opaque; } FileOperations; #endif diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 3788d4e..81d1971 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -96,6 +96,37 @@ static DIR *local_opendir(FsContext *ctx, const char *path) return opendir(rpath(ctx, path)); } +static void local_rewinddir(FsContext *ctx, DIR *dir) +{ + return rewinddir(dir); +} + +static off_t local_telldir(FsContext *ctx, DIR *dir) +{ + return telldir(dir); +} + +static struct dirent *local_readdir(FsContext *ctx, DIR *dir) +{ + return readdir(dir); +} + +static void local_seekdir(FsContext *ctx, DIR *dir, off_t off) +{ + return seekdir(dir, off); +} + +static ssize_t local_readv(FsContext *ctx, int fd, const struct iovec *iov, + int iovcnt) +{ + return readv(fd, iov, iovcnt); +} + +static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence) +{ + return lseek(fd, offset, whence); +} + FileOperations local_ops = { .lstat = local_lstat, .setuid = local_setuid, @@ -104,4 +135,10 @@ FileOperations local_ops = { .closedir = local_closedir, .open = local_open, .opendir = local_opendir, + .rewinddir = local_rewinddir, + .telldir = local_telldir, + .readdir = local_readdir, + .seekdir = local_seekdir, + .readv = local_readv, + .lseek = local_lseek, }; diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index e98da1b..96e75d6 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -66,6 +66,37 @@ static DIR *v9fs_do_opendir(V9fsState *s, V9fsString *path) return s->ops->opendir(&s->ctx, path->data); } +static void v9fs_do_rewinddir(V9fsState *s, DIR *dir) +{ + return s->ops->rewinddir(&s->ctx, dir); +} + +static off_t v9fs_do_telldir(V9fsState *s, DIR *dir) +{ + return s->ops->telldir(&s->ctx, dir); +} + +static struct dirent *v9fs_do_readdir(V9fsState *s, DIR *dir) +{ + return s->ops->readdir(&s->ctx, dir); +} + +static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off) +{ + return s->ops->seekdir(&s->ctx, dir, off); +} + +static int v9fs_do_readv(V9fsState *s, int fd, const struct iovec *iov, + int iovcnt) +{ + return s->ops->readv(&s->ctx, fd, iov, iovcnt); +} + +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 void v9fs_string_init(V9fsString *str) { str->data = NULL; @@ -1285,11 +1316,210 @@ static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu) } } +typedef struct V9fsReadState { + V9fsPDU *pdu; + size_t offset; + int32_t count; + int32_t total; + int64_t off; + V9fsFidState *fidp; + struct iovec iov[128]; /* FIXME: bad, bad, bad */ + struct iovec *sg; + off_t dir_pos; + struct dirent *dent; + struct stat stbuf; + V9fsString name; + V9fsStat v9stat; + int32_t len; + int32_t cnt; + int32_t max_count; +} V9fsReadState; + +static void v9fs_read_post_readdir(V9fsState *, V9fsReadState *, ssize_t); + +static void v9fs_read_post_seekdir(V9fsState *s, V9fsReadState *vs, ssize_t err) +{ + if (err) { + goto out; + } + v9fs_stat_free(&vs->v9stat); + v9fs_string_free(&vs->name); + vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); + vs->offset += vs->count; + err = vs->offset; +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); + return; +} + +static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs, + ssize_t err) +{ + if (err) { + err = -errno; + goto out; + } + err = stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat); + if (err) { + goto out; + } + + vs->len = pdu_marshal(vs->pdu, vs->offset + 4 + vs->count, "S", + &vs->v9stat); + if ((vs->len != (vs->v9stat.size + 2)) || + ((vs->count + vs->len) > vs->max_count)) { + v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos); + v9fs_read_post_seekdir(s, vs, err); + return; + } + vs->count += vs->len; + v9fs_stat_free(&vs->v9stat); + v9fs_string_free(&vs->name); + vs->dir_pos = vs->dent->d_off; + vs->dent = v9fs_do_readdir(s, vs->fidp->dir); + v9fs_read_post_readdir(s, vs, err); + return; +out: + v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos); + v9fs_read_post_seekdir(s, vs, err); + return; + +} + +static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err) +{ + if (vs->dent) { + memset(&vs->v9stat, 0, sizeof(vs->v9stat)); + v9fs_string_init(&vs->name); + v9fs_string_sprintf(&vs->name, "%s/%s", vs->fidp->path.data, + vs->dent->d_name); + err = v9fs_do_lstat(s, &vs->name, &vs->stbuf); + v9fs_read_post_dir_lstat(s, vs, err); + return; + } + + vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); + vs->offset += vs->count; + err = vs->offset; + complete_pdu(s, vs->pdu, err); + qemu_free(vs); + return; +} + +static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err) +{ + vs->dent = v9fs_do_readdir(s, vs->fidp->dir); + v9fs_read_post_readdir(s, vs, err); + return; +} + +static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs, + ssize_t err) +{ + vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir); + v9fs_read_post_telldir(s, vs, err); + return; +} + +static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *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_readv(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + if (vs->len == -1) { + err = -errno; + } + v9fs_read_post_readv(s, vs, err); + return; + } + vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->total); + vs->offset += vs->count; + err = vs->offset; + +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + +static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *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_readv(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + if (vs->len == -1) { + err = -errno; + } + v9fs_read_post_readv(s, vs, err); + return; + } +out: + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + static void v9fs_read(V9fsState *s, V9fsPDU *pdu) { - if (debug_9p_pdu) { - pprint_pdu(pdu); + int32_t fid; + V9fsReadState *vs; + ssize_t err = 0; + + vs = qemu_malloc(sizeof(*vs)); + vs->pdu = pdu; + vs->offset = 7; + vs->total = 0; + vs->len = 0; + vs->count = 0; + + pdu_unmarshal(vs->pdu, vs->offset, "dqd", &fid, &vs->off, &vs->count); + + vs->fidp = lookup_fid(s, fid); + if (vs->fidp == NULL) { + err = -EINVAL; + goto out; + } + + if (vs->fidp->dir) { + vs->max_count = vs->count; + vs->count = 0; + if (vs->off == 0) { + v9fs_do_rewinddir(s, vs->fidp->dir); + } + v9fs_read_post_rewinddir(s, vs, err); + return; + } else if (vs->fidp->fd != -1) { + vs->sg = vs->iov; + pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt); + err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET); + v9fs_read_post_lseek(s, vs, err); + return; + } else { + err = -EINVAL; } +out: + complete_pdu(s, pdu, err); + qemu_free(vs); } static void v9fs_write(V9fsState *s, V9fsPDU *pdu)