hw/9pfs: Update v9fs_readdir to use coroutines

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
This commit is contained in:
Aneesh Kumar K.V 2011-05-18 15:57:17 -07:00
parent dcb9dbe3c7
commit 5e4eaa79cf

View File

@ -112,11 +112,6 @@ static off_t v9fs_do_telldir(V9fsState *s, DIR *dir)
return s->ops->telldir(&s->ctx, 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) static void v9fs_do_seekdir(V9fsState *s, DIR *dir, off_t off)
{ {
return s->ops->seekdir(&s->ctx, dir, off); return s->ops->seekdir(&s->ctx, dir, off);
@ -1966,7 +1961,7 @@ static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
v9fs_stat_free(&vs->v9stat); v9fs_stat_free(&vs->v9stat);
v9fs_string_free(&vs->name); v9fs_string_free(&vs->name);
vs->dir_pos = vs->dent->d_off; 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); v9fs_read_post_readdir(s, vs, err);
return; return;
out: out:
@ -1998,7 +1993,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) 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); v9fs_read_post_readdir(s, vs, err);
return; return;
} }
@ -2127,126 +2122,102 @@ out:
qemu_free(vs); qemu_free(vs);
} }
typedef struct V9fsReadDirState { static size_t v9fs_readdir_data_size(V9fsString *name)
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)
{ {
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); /*
vs->offset += vs->count; * Size of each dirent on the wire: size of qid (13) + size of offset (8)
complete_pdu(s, vs->pdu, vs->offset); * size of type (1) + size of name.size (2) + strlen(name.data)
qemu_free(vs); */
return; return 24 + v9fs_string_size(name);
} }
/* Size of each dirent on the wire: size of qid (13) + size of offset (8) static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
* size of type (1) + size of name.size (2) + strlen(name.data) V9fsFidState *fidp, int32_t max_count)
*/
#define V9_READDIR_DATA_SZ (24 + strlen(vs->name.data))
static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
{ {
int len;
size_t size; 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) { /* save the directory position */
v9fs_string_init(&vs->name); saved_dir_pos = v9fs_co_telldir(s, fidp);
v9fs_string_sprintf(&vs->name, "%s", vs->dent->d_name); if (saved_dir_pos < 0) {
return saved_dir_pos;
if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) { }
/* Ran out of buffer. Set dir back to old position and return */ while (1) {
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos); err = v9fs_co_readdir(s, fidp, &dent);
v9fs_readdir_post_seekdir(s, vs); if (err || !dent) {
return; break;
} }
v9fs_string_init(&name);
/* Fill up just the path field of qid because the client uses 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_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
* only that. To fill the entire qid structure we will have * only that. To fill the entire qid structure we will have
* to stat each dirent found, which is expensive * to stat each dirent found, which is expensive
*/ */
size = MIN(sizeof(vs->dent->d_ino), sizeof(vs->qid.path)); size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
memcpy(&vs->qid.path, &vs->dent->d_ino, size); memcpy(&qid.path, &dent->d_ino, size);
/* Fill the other fields with dummy values */ /* Fill the other fields with dummy values */
vs->qid.type = 0; qid.type = 0;
vs->qid.version = 0; qid.version = 0;
len = pdu_marshal(vs->pdu, vs->offset+4+vs->count, "Qqbs", /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
&vs->qid, vs->dent->d_off, len = pdu_marshal(pdu, 11 + count, "Qqbs",
vs->dent->d_type, &vs->name); &qid, dent->d_off,
vs->count += len; dent->d_type, &name);
v9fs_string_free(&vs->name); count += len;
vs->saved_dir_pos = vs->dent->d_off; v9fs_string_free(&name);
vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir); saved_dir_pos = dent->d_off;
v9fs_readdir_post_readdir(s, vs);
return;
} }
if (err < 0) {
vs->offset += pdu_marshal(vs->pdu, vs->offset, "d", vs->count); return err;
vs->offset += vs->count; }
complete_pdu(s, vs->pdu, vs->offset); return count;
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;
}
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;
} }
static void v9fs_readdir(void *opaque) static void v9fs_readdir(void *opaque)
{ {
int32_t fid;
V9fsFidState *fidp;
ssize_t retval = 0;
size_t offset = 7;
int64_t initial_offset;
int32_t count, max_count;
V9fsPDU *pdu = opaque; V9fsPDU *pdu = opaque;
V9fsState *s = pdu->s; V9fsState *s = pdu->s;
int32_t fid;
V9fsReadDirState *vs;
ssize_t err = 0;
size_t offset = 7;
vs = qemu_malloc(sizeof(*vs)); pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count);
vs->pdu = pdu;
vs->offset = 7;
vs->count = 0;
pdu_unmarshal(vs->pdu, offset, "dqd", &fid, &vs->initial_offset, fidp = lookup_fid(s, fid);
&vs->max_count); if (fidp == NULL || !fidp->fs.dir) {
retval = -EINVAL;
vs->fidp = lookup_fid(s, fid);
if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
err = -EINVAL;
goto out; goto out;
} }
if (initial_offset == 0) {
if (vs->initial_offset == 0) { v9fs_co_rewinddir(s, fidp);
v9fs_do_rewinddir(s, vs->fidp->fs.dir);
} else { } else {
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset); v9fs_co_seekdir(s, fidp, initial_offset);
} }
count = v9fs_do_readdir(s, pdu, fidp, max_count);
v9fs_readdir_post_setdir(s, vs); if (count < 0) {
return; retval = count;
goto out;
}
retval = offset;
retval += pdu_marshal(pdu, offset, "d", count);
retval += count;
out: out:
complete_pdu(s, pdu, err); complete_pdu(s, pdu, retval);
qemu_free(vs);
} }
static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs, static void v9fs_write_post_pwritev(V9fsState *s, V9fsWriteState *vs,