Skip to content

Commit

Permalink
[LibOS] Clean up select and poll implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Borys Popławski <[email protected]>
  • Loading branch information
boryspoplawski committed Nov 30, 2022
1 parent 0a01e6d commit af40f61
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 245 deletions.
10 changes: 3 additions & 7 deletions libos/include/libos_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

struct libos_handle;

#define FS_POLL_RD 0x01
#define FS_POLL_WR 0x02
#define FS_POLL_ER 0x04

/* Describes mount parameters. Passed to `mount_fs`, and to the `mount` callback. */
struct libos_mount_params {
/* Filesystem type (corresponds to `name` field of `libos_fs` */
Expand Down Expand Up @@ -182,8 +178,8 @@ struct libos_fs_ops {
int (*checkout)(struct libos_handle* hdl);
int (*checkin)(struct libos_handle* hdl);

/* poll a single handle */
int (*poll)(struct libos_handle* hdl, int poll_type);
/* Poll a single handle. Must not block. */
int (*poll)(struct libos_handle* hdl, int in_events, int* out_events);

/* checkpoint/migrate the file system */
ssize_t (*checkpoint)(void** checkpoint, void* mount_data);
Expand Down Expand Up @@ -950,7 +946,7 @@ int generic_readdir(struct libos_dentry* dent, readdir_callback_t callback, void
int generic_inode_stat(struct libos_dentry* dent, struct stat* buf);
int generic_inode_hstat(struct libos_handle* hdl, struct stat* buf);
file_off_t generic_inode_seek(struct libos_handle* hdl, file_off_t offset, int origin);
int generic_inode_poll(struct libos_handle* hdl, int poll_type);
int generic_inode_poll(struct libos_handle* hdl, int events, int* out_events);

int generic_emulated_mmap(struct libos_handle* hdl, void* addr, size_t size, int prot, int flags,
uint64_t offset);
Expand Down
2 changes: 1 addition & 1 deletion libos/include/libos_fs_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ ssize_t mem_file_read(struct libos_mem_file* mem, file_off_t pos_start, void* bu
ssize_t mem_file_write(struct libos_mem_file* mem, file_off_t pos_start, const void* buf,
size_t size);
int mem_file_truncate(struct libos_mem_file* mem, file_off_t size);
int mem_file_poll(struct libos_mem_file* mem, file_off_t pos, int poll_type);
int mem_file_poll(struct libos_mem_file* mem, file_off_t pos, int events, int* out_events);
2 changes: 1 addition & 1 deletion libos/include/libos_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ long libos_syscall_fchmodat(int dfd, const char* filename, mode_t mode);
long libos_syscall_fchownat(int dfd, const char* filename, uid_t user, gid_t group, int flags);
long libos_syscall_faccessat(int dfd, const char* filename, mode_t mode);
long libos_syscall_pselect6(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
const struct __kernel_timespec* tsp, void* sigmask_argpack);
struct __kernel_timespec* tsp, void* sigmask_argpack);
long libos_syscall_ppoll(struct pollfd* fds, unsigned int nfds, struct timespec* tsp,
const __sigset_t* sigmask_ptr, size_t sigsetsize);
long libos_syscall_set_robust_list(struct robust_list_head* head, size_t len);
Expand Down
12 changes: 5 additions & 7 deletions libos/src/fs/libos_fs_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ int mem_file_truncate(struct libos_mem_file* mem, file_off_t size) {
return 0;
}

int mem_file_poll(struct libos_mem_file* mem, file_off_t pos, int poll_type) {
int ret = 0;
if ((poll_type & FS_POLL_RD) && (pos < mem->size))
ret |= FS_POLL_RD;
if (poll_type & FS_POLL_WR)
ret |= FS_POLL_WR;
return ret;
int mem_file_poll(struct libos_mem_file* mem, file_off_t pos, int events, int* out_events) {
*out_events = events & (POLLOUT | POLLWRNORM);
if ((events & (POLLIN | POLLRDNORM)) && (pos < mem->size))
*out_events |= events & (POLLIN | POLLRDNORM);
return 0;
}
16 changes: 8 additions & 8 deletions libos/src/fs/libos_fs_pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,26 +468,26 @@ static int pseudo_close(struct libos_handle* hdl) {
}
}

static int pseudo_poll(struct libos_handle* hdl, int poll_type) {
static int pseudo_poll(struct libos_handle* hdl, int events, int* out_events) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
assert(hdl->type == TYPE_STR);
lock(&hdl->pos_lock);
lock(&hdl->lock);
int ret = mem_file_poll(&hdl->info.str.mem, hdl->pos, poll_type);
int ret = mem_file_poll(&hdl->info.str.mem, hdl->pos, events, out_events);
unlock(&hdl->lock);
unlock(&hdl->pos_lock);
return ret;
}

case PSEUDO_DEV: {
int ret = 0;
if ((poll_type & FS_POLL_RD) && node->dev.dev_ops.read)
ret |= FS_POLL_RD;
if ((poll_type & FS_POLL_WR) && node->dev.dev_ops.write)
ret |= FS_POLL_WR;
return ret;
*out_events = 0;
if ((events & POLLIN) && node->dev.dev_ops.read)
*out_events |= POLLIN;
if ((events & POLLOUT) && node->dev.dev_ops.write)
*out_events |= POLLOUT;
return 0;
}

default:
Expand Down
15 changes: 2 additions & 13 deletions libos/src/fs/libos_fs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,16 @@ file_off_t generic_inode_seek(struct libos_handle* hdl, file_off_t offset, int o
return ret;
}

int generic_inode_poll(struct libos_handle* hdl, int poll_type) {
int generic_inode_poll(struct libos_handle* hdl, int events, int* out_events) {
int ret;

lock(&hdl->pos_lock);
lock(&hdl->inode->lock);

if (hdl->inode->type == S_IFREG) {
ret = 0;
if (poll_type & FS_POLL_WR)
ret |= FS_POLL_WR;
/* TODO: The `hdl->pos < hdl->inode->size` condition is wrong, the `poll` syscall treats
* end-of-file as readable. Check if removing this condition doesn't break anything
* in our `poll` implementation. */
if ((poll_type & FS_POLL_RD) && hdl->pos < hdl->inode->size)
ret |= FS_POLL_RD;
*out_events = events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
} else {
ret = -EAGAIN;
}

unlock(&hdl->inode->lock);
unlock(&hdl->pos_lock);
return ret;
}

Expand Down
Loading

0 comments on commit af40f61

Please sign in to comment.