Skip to content

Commit

Permalink
Implement vectored async IO fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kmeisthax committed Oct 25, 2021
1 parent e8f8530 commit e401e67
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
55 changes: 52 additions & 3 deletions kernel/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "kernel/calls.h"
#include "kernel/task.h"
#include "kernel/aio.h"
#include "kernel/fs.h"
#include "fs/aio.h"
#include "fs/fd.h"

Expand Down Expand Up @@ -186,7 +187,9 @@ dword_t sys_io_cancel(dword_t ctx_id, addr_t iocb, addr_t result) {
* Do a single PREAD operation, falling back to seek-and-read if necessary.
*
* The return code corresponds to the 'sync error' concept of fallback_submit,
* while async errors should be flagged by writing to *err.
* while async errors should be flagged by writing to `*err`.
*
* `*err` also is used to return the total number of bytes read.
*/
static signed int __aio_fallback_pread(
struct fd *fd,
Expand Down Expand Up @@ -243,7 +246,9 @@ static signed int __aio_fallback_pread(
* Do a single PWRITE operation, falling back to seek-and-write if necessary.
*
* The return code corresponds to the 'sync error' concept of fallback_submit,
* while async errors should be flagged by writing to *err.
* while async errors should be flagged by writing to `*err`.
*
* `*err` also is used to return the total number of bytes written.
*/
static signed int __aio_fallback_pwrite(
struct fd *fd,
Expand Down Expand Up @@ -316,6 +321,8 @@ int aio_fallback_submit(struct fd *fd, struct aioctx *ctx, unsigned int event_id
return sync_err;
}

struct iovec_ *iov_list = NULL;

char *buf = NULL;
switch (evt->op) {
case AIOCTX_PREAD:
Expand All @@ -337,7 +344,49 @@ int aio_fallback_submit(struct fd *fd, struct aioctx *ctx, unsigned int event_id
async_result0 = 0;
sync_err = 0;
break;
//TODO: AIOCTX_POLL, AIOCTX_PREADV, AIOCTX_PWRITEV
case AIOCTX_PREADV:
iov_list = read_iovec((addr_t)evt->buf, evt->nbytes);
ssize_t total_read = 0;

for (unsigned int i = 0; i < evt->nbytes; i += 1) {
signed int cur_read = 0;
sync_err = __aio_fallback_pread(fd, iov_list[i].base, iov_list[i].len, evt->offset + total_read, &cur_read);
if (sync_err < 0 || cur_read < 0) {
async_result0 = cur_read;
break;
}

total_read += cur_read;

if (cur_read < iov_list[i].len) break;
}

if (sync_err < 0 || async_result0 < 0) break;

async_result0 = total_read;
break;
case AIOCTX_PWRITEV:
iov_list = read_iovec((addr_t)evt->buf, evt->nbytes);
ssize_t total_write = 0;

for (unsigned int i = 0; i < evt->nbytes; i += 1) {
signed int cur_write = 0;
sync_err = __aio_fallback_pwrite(fd, iov_list[i].base, iov_list[i].len, evt->offset + total_write, &cur_write);
if (sync_err < 0 || cur_write < 0) {
async_result0 = cur_write;
break;
}

total_write += cur_write;

if (cur_write < iov_list[i].len) break;
}

if (sync_err < 0 || async_result0 < 0) break;

async_result0 = total_write;
break;
//TODO: AIOCTX_POLL
default:
sync_err = _EINVAL;
break;
Expand Down
2 changes: 1 addition & 1 deletion kernel/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ dword_t sys_write(fd_t fd_no, addr_t buf_addr, dword_t size) {
// that yet because it's more work and the efficiency gain from that is dwarfed
// by the inefficiency of the emulator.

static struct iovec_ *read_iovec(addr_t iovec_addr, unsigned iovec_count) {
struct iovec_ *read_iovec(addr_t iovec_addr, unsigned iovec_count) {
dword_t iovec_size = sizeof(struct iovec_) * iovec_count;
struct iovec_ *iovec = malloc(iovec_size);
if (iovec == NULL)
Expand Down
7 changes: 5 additions & 2 deletions kernel/fs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef FS_H
#define FS_H
#ifndef KERNEL_FS_H
#define KERNEL_FS_H

#include "misc.h"
#include "util/list.h"
Expand All @@ -8,6 +8,7 @@
#include "fs/fake-db.h"
#include "fs/fix_path.h"
#include "emu/memory.h"
#include "kernel/calls.h"
#include <dirent.h>
#include <sqlite3.h>

Expand Down Expand Up @@ -175,4 +176,6 @@ extern const struct fs_ops devptsfs;
extern const struct fs_ops tmpfs;
void fs_register(const struct fs_ops *fs);

struct iovec_ *read_iovec(addr_t iovec_addr, unsigned iovec_count);

#endif

0 comments on commit e401e67

Please sign in to comment.