@@ -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)
@@ -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;
@@ -1048,14 +1079,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);
}