debuggers.hg

changeset 20915:07f33d124053

blktap2: Sort out tapdisk IPC init.

Move I/O and event callbacks setup out of tapdisk-server, into
tapdisk-ipc.

Signed-off-by: Daniel Stodden <daniel.stodden@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jan 29 08:53:52 2010 +0000 (2010-01-29)
parents 2bc69db28153
children f2ef85551a09
files tools/blktap2/drivers/tapdisk-ipc.c tools/blktap2/drivers/tapdisk-ipc.h tools/blktap2/drivers/tapdisk-server.c tools/blktap2/drivers/tapdisk-server.h
line diff
     1.1 --- a/tools/blktap2/drivers/tapdisk-ipc.c	Fri Jan 29 07:14:32 2010 +0000
     1.2 +++ b/tools/blktap2/drivers/tapdisk-ipc.c	Fri Jan 29 08:53:52 2010 +0000
     1.3 @@ -30,12 +30,86 @@
     1.4  #include <stdlib.h>
     1.5  #include <unistd.h>
     1.6  #include <string.h>
     1.7 +#include <fcntl.h>
     1.8  
     1.9  #include "tapdisk.h"
    1.10  #include "tapdisk-ipc.h"
    1.11  #include "tapdisk-vbd.h"
    1.12  #include "tapdisk-server.h"
    1.13  
    1.14 +static void
    1.15 +tapdisk_ipc_read_event(event_id_t id, char mode, void *private)
    1.16 +{
    1.17 +	td_ipc_t *ipc = private;
    1.18 +	tapdisk_ipc_read(ipc);
    1.19 +}
    1.20 +
    1.21 +static void
    1.22 +__tapdisk_ipc_init(td_ipc_t *ipc)
    1.23 +{
    1.24 +	ipc->rfd = -1;
    1.25 +	ipc->wfd = -1;
    1.26 +	ipc->rfd_event = -1;
    1.27 +}
    1.28 +
    1.29 +int
    1.30 +tapdisk_ipc_open(td_ipc_t *ipc, const char *read, const char *write)
    1.31 +{
    1.32 +	int err;
    1.33 +
    1.34 +	memset(ipc, 0, sizeof(td_ipc_t));
    1.35 +	__tapdisk_ipc_init(ipc);
    1.36 +
    1.37 +	if (read) {
    1.38 +		ipc->rfd = open(read, O_RDWR | O_NONBLOCK);
    1.39 +		if (ipc->rfd < 0) {
    1.40 +			err = -errno;
    1.41 +			EPRINTF("FD open failed %s: %d\n", read, err);
    1.42 +			goto fail;
    1.43 +		}
    1.44 +
    1.45 +		ipc->rfd_event = 
    1.46 +			tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
    1.47 +						      ipc->rfd, 0,
    1.48 +						      tapdisk_ipc_read_event,
    1.49 +						      ipc);
    1.50 +		if (ipc->rfd_event < 0) {
    1.51 +			err = ipc->rfd_event;
    1.52 +			goto fail;
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +	if (write) {
    1.57 +		ipc->wfd = open(write, O_RDWR | O_NONBLOCK);
    1.58 +		if (ipc->wfd < 0) {
    1.59 +			err = -errno;
    1.60 +			EPRINTF("FD open failed %s, %d\n", write, err);
    1.61 +			goto fail;
    1.62 +		}
    1.63 +	}
    1.64 +
    1.65 +	return 0;
    1.66 +
    1.67 +fail:
    1.68 +	tapdisk_ipc_close(ipc);
    1.69 +	return err;
    1.70 +}
    1.71 +
    1.72 +void
    1.73 +tapdisk_ipc_close(td_ipc_t *ipc)
    1.74 +{
    1.75 +	if (ipc->rfd > 0)
    1.76 +		close(ipc->rfd);
    1.77 +
    1.78 +	if (ipc->wfd > 0)
    1.79 +		close(ipc->wfd);
    1.80 +
    1.81 +	if (ipc->rfd_event >= 0)
    1.82 +		tapdisk_server_unregister_event(ipc->rfd_event);
    1.83 +
    1.84 +	__tapdisk_ipc_init(ipc);
    1.85 +}
    1.86 +
    1.87  static int
    1.88  tapdisk_ipc_write_message(int fd, tapdisk_message_t *message, int timeout)
    1.89  {
     2.1 --- a/tools/blktap2/drivers/tapdisk-ipc.h	Fri Jan 29 07:14:32 2010 +0000
     2.2 +++ b/tools/blktap2/drivers/tapdisk-ipc.h	Fri Jan 29 08:53:52 2010 +0000
     2.3 @@ -29,13 +29,17 @@
     2.4  #define _TAPDISK_IPC_H_
     2.5  
     2.6  #include "tapdisk-message.h"
     2.7 +#include "scheduler.h"
     2.8  
     2.9  typedef struct td_ipc_handle {
    2.10  	int                         rfd;
    2.11  	int                         wfd;
    2.12 +	event_id_t		    rfd_event;
    2.13  	td_uuid_t                   uuid;
    2.14  } td_ipc_t;
    2.15  
    2.16 +int tapdisk_ipc_open(td_ipc_t *ipc, const char *read, const char *write);
    2.17 +void tapdisk_ipc_close(td_ipc_t *ipc);
    2.18  int tapdisk_ipc_read(td_ipc_t *ipc);
    2.19  int tapdisk_ipc_write(td_ipc_t *ipc, int type);
    2.20  int tapdisk_ipc_write_error(td_ipc_t *ipc, const char *message);
     3.1 --- a/tools/blktap2/drivers/tapdisk-server.c	Fri Jan 29 07:14:32 2010 +0000
     3.2 +++ b/tools/blktap2/drivers/tapdisk-server.c	Fri Jan 29 08:53:52 2010 +0000
     3.3 @@ -26,7 +26,6 @@
     3.4   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     3.5   */
     3.6  #include <stdio.h>
     3.7 -#include <fcntl.h>
     3.8  #include <errno.h>
     3.9  #include <unistd.h>
    3.10  #include <stdlib.h>
    3.11 @@ -223,12 +222,6 @@ tapdisk_server_send_error(const char *me
    3.12  }
    3.13  
    3.14  static void
    3.15 -tapdisk_server_read_ipc_message(event_id_t id, char mode, void *private)
    3.16 -{
    3.17 -	tapdisk_ipc_read(&server.ipc);
    3.18 -}
    3.19 -
    3.20 -static void
    3.21  tapdisk_server_aio_queue_event(event_id_t id, char mode, void *private)
    3.22  {
    3.23  	tapdisk_complete_tiocbs(&server.aio_queue);
    3.24 @@ -242,6 +235,18 @@ tapdisk_server_free_aio_queue(void)
    3.25  }
    3.26  
    3.27  static int
    3.28 +tapdisk_server_init_ipc(const char *read, const char *write)
    3.29 +{
    3.30 +	return tapdisk_ipc_open(&server.ipc, read, write);
    3.31 +}
    3.32 +
    3.33 +static void
    3.34 +tapdisk_server_close_ipc(void)
    3.35 +{
    3.36 +	tapdisk_ipc_close(&server.ipc);
    3.37 +}
    3.38 +
    3.39 +static int
    3.40  tapdisk_server_initialize_aio_queue(void)
    3.41  {
    3.42  	int err;
    3.43 @@ -270,15 +275,7 @@ static void
    3.44  tapdisk_server_close(void)
    3.45  {
    3.46  	tapdisk_server_free_aio_queue();
    3.47 -
    3.48 -	if (server.control_event)
    3.49 -		scheduler_unregister_event(&server.scheduler, server.control_event);
    3.50 -
    3.51 -	if (server.ipc.rfd != -1)
    3.52 -		close(server.ipc.rfd);
    3.53 -
    3.54 -	if (server.ipc.wfd != -1)
    3.55 -		close(server.ipc.wfd);
    3.56 +	tapdisk_server_close_ipc();
    3.57  }
    3.58  
    3.59  static void
    3.60 @@ -334,63 +331,26 @@ int
    3.61  tapdisk_server_initialize(const char *read, const char *write)
    3.62  {
    3.63  	int err;
    3.64 -	event_id_t event_id;
    3.65  
    3.66 -	event_id = 0;
    3.67  	memset(&server, 0, sizeof(tapdisk_server_t));
    3.68 -	server.ipc.rfd = server.ipc.wfd = -1;
    3.69 -
    3.70  	INIT_LIST_HEAD(&server.vbds);
    3.71  
    3.72 -	if (read) {
    3.73 -		server.ipc.rfd = open(read, O_RDWR | O_NONBLOCK);
    3.74 -		if (server.ipc.rfd < 0) {
    3.75 -			err = -errno;
    3.76 -			EPRINTF("FD open failed %s: %d\n", read, err);
    3.77 -			goto fail;
    3.78 -		}
    3.79 -	}
    3.80 -
    3.81 -	if (write) {
    3.82 -		server.ipc.wfd = open(write, O_RDWR | O_NONBLOCK);
    3.83 -		if (server.ipc.wfd < 0) {
    3.84 -			err = -errno;
    3.85 -			EPRINTF("FD open failed %s, %d\n", write, err);
    3.86 -			goto fail;
    3.87 -		}
    3.88 -	}
    3.89 -
    3.90  	scheduler_initialize(&server.scheduler);
    3.91  
    3.92 -	if (read) {
    3.93 -		event_id = scheduler_register_event(&server.scheduler,
    3.94 -						    SCHEDULER_POLL_READ_FD,
    3.95 -						    server.ipc.rfd, 0,
    3.96 -						    tapdisk_server_read_ipc_message,
    3.97 -						    NULL);
    3.98 -		if (event_id < 0) {
    3.99 -			err = event_id;
   3.100 -			goto fail;
   3.101 -		}
   3.102 -	}
   3.103 +	err = tapdisk_server_init_ipc(read, write);
   3.104 +	if (err)
   3.105 +		goto fail;
   3.106  
   3.107  	err = tapdisk_server_initialize_aio_queue();
   3.108  	if (err)
   3.109  		goto fail;
   3.110  
   3.111 -	server.control_event = event_id;
   3.112  	server.run = 1;
   3.113  
   3.114  	return 0;
   3.115  
   3.116  fail:
   3.117 -	if (server.ipc.rfd > 0)
   3.118 -		close(server.ipc.rfd);
   3.119 -	if (server.ipc.wfd > 0)
   3.120 -		close(server.ipc.wfd);
   3.121 -	if (event_id > 0)
   3.122 -		scheduler_unregister_event(&server.scheduler,
   3.123 -					   server.control_event);
   3.124 +	tapdisk_server_close_ipc();
   3.125  	return err;
   3.126  }
   3.127  
     4.1 --- a/tools/blktap2/drivers/tapdisk-server.h	Fri Jan 29 07:14:32 2010 +0000
     4.2 +++ b/tools/blktap2/drivers/tapdisk-server.h	Fri Jan 29 08:53:52 2010 +0000
     4.3 @@ -57,7 +57,6 @@ typedef struct tapdisk_server {
     4.4  	td_ipc_t                     ipc;
     4.5  	struct list_head             vbds;
     4.6  	scheduler_t                  scheduler;
     4.7 -	event_id_t                   control_event;
     4.8  	struct tqueue                aio_queue;
     4.9  	event_id_t                   aio_queue_event_id;
    4.10  } tapdisk_server_t;