From bf3a2c8c5020e480495bd1bab0c8c8ebb8989f8b Mon Sep 17 00:00:00 2001 From: Damien Zammit Date: Sat, 16 Nov 2019 16:00:59 +1100 Subject: WIP fix rump (2) --- libmachdevrump/block.c | 56 +++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/libmachdevrump/block.c b/libmachdevrump/block.c index 9648d981..f8b6339c 100644 --- a/libmachdevrump/block.c +++ b/libmachdevrump/block.c @@ -23,7 +23,6 @@ #include #include #include -#include #include "mach_U.h" @@ -63,11 +62,9 @@ struct block_data int rump_fd; /* block device fd handle */ uint64_t media_size; /* total block device size */ uint32_t block_size; /* size in bytes of 1 sector */ - bool opened; /* device is currently open */ + int opened_count; /* device is currently open counter */ bool opening; /* entered open */ bool closing; /* entered close */ - sem_t busy_open; /* semaphore for device currently opening */ - sem_t busy_close; /* semaphore for device currently closing */ }; /* Return a send right associated with network device ND. */ @@ -83,7 +80,7 @@ static struct device_emulation_ops rump_block_emulation_ops; static struct block_data block = { .opening = false, .closing = false, - .opened = false + .opened_count = 0 }; #define DISK_NAME_LEN 32 @@ -120,24 +117,21 @@ static int dev_mode_to_rump_mode(const dev_mode_t mode) static io_return_t device_close (void *d) { - io_return_t err; struct block_data *bd = d; - /* We are now busy closing */ - sem_init(&bd->busy_close, 0, 0); + if (bd->closing || bd->opening) + return D_ALREADY_OPEN; + bd->closing = true; - /* If starting to open, wait until not opening */ - if (bd->opening) { - sem_wait(&bd->busy_open); + if (bd->opened_count == 1) { + mach_print("actually closing\n"); + rump_sys_close (bd->rump_fd); } - err = rump_errno2host (rump_sys_close (bd->rump_fd)); - - bd->opened = false; - sem_post(&bd->busy_close); + bd->opened_count--; bd->closing = false; - return err; + return D_SUCCESS; } static void @@ -161,21 +155,18 @@ device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type, struct block_data *bd = █ char *dev_name; - /* We are now busy opening */ - sem_init(&bd->busy_open, 0, 0); + if (bd->closing || bd->opening) + return D_ALREADY_OPEN; + bd->opening = true; mach_print("device_open\n"); dev_name = translate_name (name); - /* If starting to close, wait until not closing */ - if (bd->closing) { - sem_wait(&bd->busy_close); - } - - if (bd->opened) + if (bd->opened_count > 0) { mach_print ("re-open\n"); + err = D_SUCCESS; goto succeed; } @@ -222,6 +213,7 @@ device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type, bd->mode = mode; err = D_SUCCESS; ports_port_deref (bd); + *devicePoly = MACH_MSG_TYPE_MOVE_SEND; goto succeed; fail: @@ -229,26 +221,16 @@ fail: if (bd) ports_destroy_right (bd); *devp = (device_t) MACH_PORT_NULL; - sem_post(&bd->busy_open); bd->opening = false; return D_NO_SUCH_DEVICE; succeed: -/* - if (bd->want) - { - bd->want = false; - thread_wakeup ((event_t) bd); - } -*/ free (dev_name); *devp = (device_t)&bd->device; - *devicePoly = MACH_MSG_TYPE_MOVE_SEND; mach_print ("device_open ALL GOOD!\n"); - bd->opened = true; - sem_post(&bd->busy_open); + bd->opened_count++; bd->opening = false; - return D_SUCCESS; + return err; } static io_return_t @@ -343,7 +325,7 @@ device_get_status (void *d, dev_flavor_t flavor, dev_status_t status, struct block_data *bd = d; mach_print("device_get_status\n"); - if (!bd->opened) + if (bd->opened_count < 1) return D_INVALID_OPERATION; switch (flavor) -- cgit v1.2.3