From patchwork Wed Mar 3 19:01:04 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 46863 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 36B2FB7C09 for ; Thu, 4 Mar 2010 06:21:07 +1100 (EST) Received: from localhost ([127.0.0.1]:36846 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nmu7y-00069J-QT for incoming@patchwork.ozlabs.org; Wed, 03 Mar 2010 14:21:02 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NmtpN-0007R0-WA for qemu-devel@nongnu.org; Wed, 03 Mar 2010 14:01:50 -0500 Received: from [199.232.76.173] (port=60464 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NmtpN-0007QC-Ik for qemu-devel@nongnu.org; Wed, 03 Mar 2010 14:01:49 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NmtpL-0001ak-Im for qemu-devel@nongnu.org; Wed, 03 Mar 2010 14:01:48 -0500 Received: from e4.ny.us.ibm.com ([32.97.182.144]:39299) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NmtpL-0001aZ-4X for qemu-devel@nongnu.org; Wed, 03 Mar 2010 14:01:47 -0500 Received: from d01relay01.pok.ibm.com (d01relay01.pok.ibm.com [9.56.227.233]) by e4.ny.us.ibm.com (8.14.3/8.13.1) with ESMTP id o23IouXF024402 for ; Wed, 3 Mar 2010 13:50:56 -0500 Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d01relay01.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o23J1jxk162482 for ; Wed, 3 Mar 2010 14:01:46 -0500 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o23J3iTh009993 for ; Wed, 3 Mar 2010 12:03:44 -0700 Received: from localhost.localdomain (sig-9-76-214-227.mts.ibm.com [9.76.214.227]) by d03av06.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o23J3daG009640; Wed, 3 Mar 2010 12:03:44 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 3 Mar 2010 13:01:04 -0600 Message-Id: <1267642874-15001-9-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.5.2 In-Reply-To: <1267642874-15001-1-git-send-email-aliguori@us.ibm.com> References: <1267642874-15001-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Venkateswararao Jujjuri , "Aneesh Kumar K.V" Subject: [Qemu-devel] [PATCH 07/17] virtio-9p: Implement P9_TREAD 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 Signed-off-by: Anthony Liguori Signed-off-by: Venkateswararao Jujjuri Signed-off-by: Aneesh Kumar K.V --- hw/virtio-9p-local.c | 37 ++++++++ hw/virtio-9p.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 287 insertions(+), 3 deletions(-) diff --git a/hw/virtio-9p-local.c b/hw/virtio-9p-local.c index 803b0ea..90664a0 100644 --- a/hw/virtio-9p-local.c +++ b/hw/virtio-9p-local.c @@ -98,6 +98,37 @@ static DIR *local_opendir(void *opaque, const char *path) return opendir(rpath(path)); } +static void local_rewinddir(void *opaque, DIR *dir) +{ + return rewinddir(dir); +} + +static off_t local_telldir(void *opaque, DIR *dir) +{ + return telldir(dir); +} + +static struct dirent *local_readdir(void *opaque, DIR *dir) +{ + return readdir(dir); +} + +static void local_seekdir(void *opaque, DIR *dir, off_t off) +{ + return seekdir(dir, off); +} + +static ssize_t local_readv(void *opaque, int fd, const struct iovec *iov, + int iovcnt) +{ + return readv(fd, iov, iovcnt); +} + +static off_t local_lseek(void *opaque, int fd, off_t offset, int whence) +{ + return lseek(fd, offset, whence); +} + static V9fsPosixFileOperations ops = { .lstat = local_lstat, .setuid = local_setuid, @@ -106,6 +137,12 @@ static V9fsPosixFileOperations 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, }; V9fsPosixFileOperations *virtio_9p_init_local(const char *path) diff --git a/hw/virtio-9p.c b/hw/virtio-9p.c index 7709e4c..b30933a 100644 --- a/hw/virtio-9p.c +++ b/hw/virtio-9p.c @@ -137,6 +137,37 @@ static DIR *posix_opendir(V9fsState *s, V9fsString *path) return s->ops->opendir(s->ops->opaque, path->data); } +static void posix_rewinddir(V9fsState *s, DIR *dir) +{ + return s->ops->rewinddir(s->ops->opaque, dir); +} + +static off_t posix_telldir(V9fsState *s, DIR *dir) +{ + return s->ops->telldir(s->ops->opaque, dir); +} + +static struct dirent *posix_readdir(V9fsState *s, DIR *dir) +{ + return s->ops->readdir(s->ops->opaque, dir); +} + +static void posix_seekdir(V9fsState *s, DIR *dir, off_t off) +{ + return s->ops->seekdir(s->ops->opaque, dir, off); +} + +static int posix_readv(V9fsState *s, int fd, const struct iovec *iov, + int iovcnt) +{ + return s->ops->readv(s->ops->opaque, fd, iov, iovcnt); +} + +static off_t posix_lseek(V9fsState *s, int fd, off_t offset, int whence) +{ + return s->ops->lseek(s->ops->opaque, fd, offset, whence); +} + static void v9fs_string_init(V9fsString *str) { str->data = NULL; @@ -1050,14 +1081,230 @@ out: qemu_free(vs); } -static void v9fs_clunk(V9fsState *s, V9fsPDU *pdu) +static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt) { - if (debug_9p_pdu) - pprint_pdu(pdu); + while (len && *iovcnt) { + if (len < sg->iov_len) { + sg->iov_len -= len; + sg->iov_base += len; + len = 0; + } else { + len -= sg->iov_len; + sg++; + *iovcnt -= 1; + } + } + + return sg; +} + +static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt) +{ + int i; + int total = 0; + + for (i = 0; i < *cnt; i++) { + if ((total + sg[i].iov_len) > cap) { + sg[i].iov_len -= ((total + sg[i].iov_len) - cap); + i++; + break; + } + total += sg[i].iov_len; + } + + *cnt = i; + + return sg; +} + +static void print_sg(struct iovec *sg, int cnt) +{ + int i; + + printf("sg[%d]: {", cnt); + for (i = 0; i < cnt; i++) { + if (i) + printf(", "); + printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); + } + printf("}\n"); +} + +typedef struct V9fsReadState { + V9fsPDU *pdu; + size_t offset; + int32_t fid; + 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) +{ + 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; + complete_pdu(s, vs->pdu, err); + qemu_free(vs); + return; +} + +static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs, + ssize_t err) +{ + BUG_ON(err == -1); + stat_to_v9stat(s, &vs->name, &vs->stbuf, &vs->v9stat); + + 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)) { + posix_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 = posix_readdir(s, vs->fidp->dir); + v9fs_read_post_readdir(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 = posix_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 = posix_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 = posix_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) +{ + BUG_ON(vs->len < 0); + 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 = posix_readv(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + 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; + complete_pdu(s, vs->pdu, err); + qemu_free(vs); +} + +static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err) +{ + BUG_ON(err == -1); + 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 = posix_readv(s, vs->fidp->fd, vs->sg, vs->cnt); + } while (vs->len == -1 && errno == EINTR); + v9fs_read_post_readv(s, vs, err); + return; + } } static void v9fs_read(V9fsState *s, V9fsPDU *pdu) { + 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", &vs->fid, &vs->off, &vs->count); + + vs->fidp = lookup_fid(s, vs->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) { + posix_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 = posix_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_clunk(V9fsState *s, V9fsPDU *pdu) +{ if (debug_9p_pdu) pprint_pdu(pdu); }