From: Jacek Caban Subject: [PATCH 01/10 v2] server: Introduced iosb struct for server-side IO_STATUS_BLOCK representation and use it in irp_call. Message-Id: Date: Wed, 19 Oct 2016 19:05:05 +0200 v2: Don't store async queues in a list. Signed-off-by: Jacek Caban --- server/async.c | 34 +++++++++++++++++++++++++++++++++ server/device.c | 58 +++++++++++++++++++++++++-------------------------------- server/file.h | 14 ++++++++++++++ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/server/async.c b/server/async.c index 64aa27a..57499c8 100644 --- a/server/async.c +++ b/server/async.c @@ -379,3 +379,37 @@ void async_wake_up( struct async_queue *queue, unsigned int status ) if (status == STATUS_ALERTED) break; /* only wake up the first one */ } } + +/* allocate iosb struct */ +struct iosb *alloc_iosb( const void *in_data, data_size_t in_size, data_size_t out_size ) +{ + struct iosb *iosb; + + if (!(iosb = mem_alloc( sizeof(*iosb) ))) return NULL; + + iosb->refcount = 1; + iosb->status = STATUS_PENDING; + iosb->result = 0; + iosb->in_size = in_size; + iosb->in_data = NULL; + iosb->out_size = out_size; + iosb->out_data = NULL; + + if (in_size && !(iosb->in_data = memdup( in_data, in_size ))) + { + release_iosb( iosb ); + iosb = NULL; + } + + return iosb; +} + +/* release iosb reference */ +void release_iosb( struct iosb *iosb ) +{ + if (--iosb->refcount) return; + + free( iosb->in_data ); + free( iosb->out_data ); + free( iosb ); +} diff --git a/server/device.c b/server/device.c index e4f55c7..8cc116b 100644 --- a/server/device.c +++ b/server/device.c @@ -50,13 +50,8 @@ struct irp_call struct thread *thread; /* thread that queued the irp */ client_ptr_t user_arg; /* user arg used to identify the request */ struct async *async; /* pending async op */ - unsigned int status; /* resulting status (or STATUS_PENDING) */ irp_params_t params; /* irp parameters */ - data_size_t result; /* size of result (input or output depending on the type) */ - data_size_t in_size; /* size of input data */ - void *in_data; /* input data */ - data_size_t out_size; /* size of output data */ - void *out_data; /* output data */ + struct iosb *iosb; /* I/O status block */ }; static void irp_call_dump( struct object *obj, int verbose ); @@ -242,13 +237,12 @@ static void irp_call_destroy( struct object *obj ) { struct irp_call *irp = (struct irp_call *)obj; - free( irp->in_data ); - free( irp->out_data ); if (irp->async) { async_terminate( irp->async, STATUS_CANCELLED ); release_object( irp->async ); } + if (irp->iosb) release_iosb( irp->iosb ); if (irp->file) release_object( irp->file ); if (irp->thread) release_object( irp->thread ); } @@ -270,14 +264,8 @@ static struct irp_call *create_irp( struct device_file *file, const irp_params_t irp->thread = NULL; irp->async = NULL; irp->params = *params; - irp->status = STATUS_PENDING; - irp->result = 0; - irp->in_size = in_size; - irp->in_data = NULL; - irp->out_size = out_size; - irp->out_data = NULL; - - if (irp->in_size && !(irp->in_data = memdup( in_data, in_size ))) + + if (!(irp->iosb = alloc_iosb( in_data, in_size, out_size ))) { release_object( irp ); irp = NULL; @@ -290,15 +278,16 @@ static void set_irp_result( struct irp_call *irp, unsigned int status, const void *out_data, data_size_t out_size, data_size_t result ) { struct device_file *file = irp->file; + struct iosb *iosb = irp->iosb; if (!file) return; /* already finished */ /* FIXME: handle the STATUS_PENDING case */ - irp->status = status; - irp->result = result; - irp->out_size = min( irp->out_size, out_size ); - if (irp->out_size && !(irp->out_data = memdup( out_data, irp->out_size ))) - irp->out_size = 0; + iosb->status = status; + iosb->result = result; + iosb->out_size = min( iosb->out_size, out_size ); + if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size ))) + iosb->out_size = 0; irp->file = NULL; if (irp->async) { @@ -760,6 +749,7 @@ DECL_HANDLER(get_next_device_request) struct irp_call *irp; struct device_manager *manager; struct list *ptr; + struct iosb *iosb; reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1; @@ -781,20 +771,21 @@ DECL_HANDLER(get_next_device_request) if ((ptr = list_head( &manager->requests ))) { irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry ); + iosb = irp->iosb; if (irp->thread) { reply->client_pid = get_process_id( irp->thread->process ); reply->client_tid = get_thread_id( irp->thread ); } reply->params = irp->params; - reply->in_size = irp->in_size; - reply->out_size = irp->out_size; - if (irp->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW ); + reply->in_size = iosb->in_size; + reply->out_size = iosb->out_size; + if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW ); else if ((reply->next = alloc_handle( current->process, irp, 0, 0 ))) { - set_reply_data_ptr( irp->in_data, irp->in_size ); - irp->in_data = NULL; - irp->in_size = 0; + set_reply_data_ptr( iosb->in_data, iosb->in_size ); + iosb->in_data = NULL; + iosb->in_size = 0; list_remove( &irp->mgr_entry ); list_init( &irp->mgr_entry ); } @@ -832,17 +823,18 @@ DECL_HANDLER(get_irp_result) if ((irp = find_irp_call( file, current, req->user_arg ))) { - if (irp->out_data) + struct iosb *iosb = irp->iosb; + if (iosb->out_data) { - data_size_t size = min( irp->out_size, get_reply_max_size() ); + data_size_t size = min( iosb->out_size, get_reply_max_size() ); if (size) { - set_reply_data_ptr( irp->out_data, size ); - irp->out_data = NULL; + set_reply_data_ptr( iosb->out_data, size ); + iosb->out_data = NULL; } } - reply->size = irp->result; - set_error( irp->status ); + reply->size = iosb->result; + set_error( iosb->status ); list_remove( &irp->dev_entry ); release_object( irp ); /* no longer on the device queue */ } diff --git a/server/file.h b/server/file.h index b643d94..6b1a6aa 100644 --- a/server/file.h +++ b/server/file.h @@ -55,6 +55,18 @@ struct fd_ops int (*cancel_async)(struct fd *, struct process *process, struct thread *thread, client_ptr_t iosb); }; +/* server-side representation of I/O status block */ +struct iosb +{ + unsigned int refcount; /* reference count */ + unsigned int status; /* resulting status (or STATUS_PENDING) */ + data_size_t result; /* size of result (input or output depending on the type) */ + data_size_t in_size; /* size of input data */ + void *in_data; /* input data */ + data_size_t out_size; /* size of output data */ + void *out_data; /* output data */ +}; + /* file descriptor functions */ extern struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, @@ -180,6 +192,8 @@ extern int async_wake_up_by( struct async_queue *queue, struct process *process, extern void async_wake_up( struct async_queue *queue, unsigned int status ); extern struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key ); extern void fd_copy_completion( struct fd *src, struct fd *dst ); +extern struct iosb *alloc_iosb( const void *in_data, data_size_t in_size, data_size_t out_size ); +extern void release_iosb( struct iosb *iosb ); /* access rights that require Unix read permission */ #define FILE_UNIX_READ_ACCESS (FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA)