From patchwork Thu May 19 02:56:59 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jvrao X-Patchwork-Id: 96290 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 22D04B6F6E for ; Thu, 19 May 2011 13:00:55 +1000 (EST) Received: from localhost ([::1]:48171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QMtTn-0002Ic-Gl for incoming@patchwork.ozlabs.org; Wed, 18 May 2011 23:00:51 -0400 Received: from eggs.gnu.org ([140.186.70.92]:35730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QMtQc-0005I0-JU for qemu-devel@nongnu.org; Wed, 18 May 2011 22:57:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QMtQb-0004u1-0C for qemu-devel@nongnu.org; Wed, 18 May 2011 22:57:34 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:55707) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QMtQa-0004tq-LC for qemu-devel@nongnu.org; Wed, 18 May 2011 22:57:32 -0400 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by e34.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p4J2ivrn004060 for ; Wed, 18 May 2011 20:44:58 -0600 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p4J2vRNE152796 for ; Wed, 18 May 2011 20:57:27 -0600 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p4IKv0Vx015358 for ; Wed, 18 May 2011 14:57:00 -0600 Received: from oc6675851006.ibm.com (sig-9-65-27-236.mts.ibm.com [9.65.27.236]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p4IKuo4U014807; Wed, 18 May 2011 14:56:59 -0600 From: "Venkateswararao Jujjuri (JV)" To: qemu-devel@nongnu.org Date: Wed, 18 May 2011 19:56:59 -0700 Message-Id: <1305773834-16359-10-git-send-email-jvrao@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1305773834-16359-1-git-send-email-jvrao@linux.vnet.ibm.com> References: <1305773834-16359-1-git-send-email-jvrao@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.110.152 Cc: aliguori@us.ibm.com, "Venkateswararao Jujjuri \"" , stefanha@linux.vnet.ibm.com, "Aneesh Kumar K.V" Subject: [Qemu-devel] [V3 09/24] hw/9pfs: Update v9fs_readdir to use coroutines X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Aneesh Kumar K.V Signed-off-by: Aneesh Kumar K.V Signed-off-by: Venkateswararao Jujjuri " --- hw/9pfs/virtio-9p.c | 173 +++++++++++++++++++++------------------------------ 1 files changed, 72 insertions(+), 101 deletions(-) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 54976c4..f48cc47 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -112,11 +112,6 @@ 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); @@ -1967,7 +1962,7 @@ static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs, 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->fs.dir); + v9fs_co_readdir(s, vs->fidp, &vs->dent); v9fs_read_post_readdir(s, vs, err); return; out: @@ -1999,7 +1994,7 @@ static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err) static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err) { - vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); + v9fs_co_readdir(s, vs->fidp, &vs->dent); v9fs_read_post_readdir(s, vs, err); return; } @@ -2128,126 +2123,102 @@ out: qemu_free(vs); } -typedef struct V9fsReadDirState { - V9fsPDU *pdu; - V9fsFidState *fidp; - V9fsQID qid; - off_t saved_dir_pos; - struct dirent *dent; - int32_t count; - int32_t max_count; - size_t offset; - int64_t initial_offset; - V9fsString name; -} V9fsReadDirState; - -static void v9fs_readdir_post_seekdir(V9fsState *s, V9fsReadDirState *vs) +static size_t v9fs_readdir_data_size(V9fsString *name) { - vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); - vs->offset += vs->count; - complete_pdu(s, vs->pdu, vs->offset); - qemu_free(vs); - return; + /* + * Size of each dirent on the wire: size of qid (13) + size of offset (8) + * size of type (1) + size of name.size (2) + strlen(name.data) + */ + return 24 + v9fs_string_size(name); } -/* Size of each dirent on the wire: size of qid (13) + size of offset (8) - * size of type (1) + size of name.size (2) + strlen(name.data) - */ -#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data)) - -static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs) +static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu, + V9fsFidState *fidp, int32_t max_count) { - int len; size_t size; + V9fsQID qid; + V9fsString name; + int len, err = 0; + int32_t count = 0; + off_t saved_dir_pos; + struct dirent *dent; - if (vs->dent) { - v9fs_string_init(&vs->name); - v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name); - - if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) { + /* save the directory position */ + saved_dir_pos = v9fs_co_telldir(s, fidp); + if (saved_dir_pos < 0) { + return saved_dir_pos; + } + while (1) { + err = v9fs_co_readdir(s, fidp, &dent); + if (err || !dent) { + break; + } + v9fs_string_init(&name); + v9fs_string_sprintf(&name, "%s", dent->d_name); + if ((count + v9fs_readdir_data_size(&name)) > max_count) { /* Ran out of buffer. Set dir back to old position and return */ - v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos); - v9fs_readdir_post_seekdir(s, vs); - return; + v9fs_co_seekdir(s, fidp, saved_dir_pos); + v9fs_string_free(&name); + return count; } - - /* Fill up just the path field of qid because the client uses + /* + * Fill up just the path field of qid because the client uses * only that. To fill the entire qid structure we will have * to stat each dirent found, which is expensive */ - size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path)); - memcpy(&vs->qid.path, &vs->dent->d_ino, size); + size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); + memcpy(&qid.path, &dent->d_ino, size); /* Fill the other fields with dummy values */ - vs->qid.type = 0; - vs->qid.version = 0; - - len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs", - &vs->qid, vs->dent->d_off, - vs->dent->d_type, &vs->name); - vs->count += len; - v9fs_string_free(&vs->name); - vs->saved_dir_pos = vs->dent->d_off; - vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); - v9fs_readdir_post_readdir(s, vs); - return; - } - - vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); - vs->offset += vs->count; - complete_pdu(s, vs->pdu, vs->offset); - qemu_free(vs); - return; -} - -static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs) -{ - vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); - v9fs_readdir_post_readdir(s, vs); - return; -} + qid.type = 0; + qid.version = 0; -static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs) -{ - vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir); - v9fs_readdir_post_telldir(s, vs); - return; + /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ + len = pdu_marshal(pdu, 11 + count, "Qqbs", + &qid, dent->d_off, + dent->d_type, &name); + count += len; + v9fs_string_free(&name); + saved_dir_pos = dent->d_off; + } + if (err < 0) { + return err; + } + return count; } static void v9fs_readdir(void *opaque) { - V9fsPDU *pdu = opaque; - V9fsState *s = pdu->s; int32_t fid; - V9fsReadDirState *vs; - ssize_t err = 0; + V9fsFidState *fidp; + ssize_t retval = 0; size_t offset = 7; + int64_t initial_offset; + int32_t count, max_count; + V9fsPDU *pdu = opaque; + V9fsState *s = pdu->s; - vs = qemu_malloc(sizeof(*vs)); - vs->pdu = pdu; - vs->offset = 7; - vs->count = 0; - - pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset, - &vs->max_count); + pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count); - vs->fidp = lookup_fid(s, fid); - if (vs->fidp == NULL || !(vs->fidp->fs.dir)) { - err = -EINVAL; + fidp = lookup_fid(s, fid); + if (fidp == NULL || !fidp->fs.dir) { + retval = -EINVAL; goto out; } - - if (vs->initial_offset == 0) { - v9fs_do_rewinddir(s, vs->fidp->fs.dir); + if (initial_offset == 0) { + v9fs_co_rewinddir(s, fidp); } else { - v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset); + v9fs_co_seekdir(s, fidp, initial_offset); } - - v9fs_readdir_post_setdir(s, vs); - return; - + count = v9fs_do_readdir(s, pdu, fidp, max_count); + if (count < 0) { + retval = count; + goto out; + } + retval = offset; + retval += pdu_marshal(pdu, offset, "d", count); + retval += count; out: - complete_pdu(s, pdu, err); - qemu_free(vs); + complete_pdu(s, pdu, retval); } static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,