iPXE
blocktrans.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * Block device translator
00030  *
00031  */
00032 
00033 #include <stdlib.h>
00034 #include <errno.h>
00035 #include <assert.h>
00036 #include <ipxe/iobuf.h>
00037 #include <ipxe/xfer.h>
00038 #include <ipxe/blockdev.h>
00039 #include <ipxe/blocktrans.h>
00040 
00041 /**
00042  * Reallocate block device translator data buffer
00043  *
00044  * @v xferbuf           Data transfer buffer
00045  * @v len               New length (or zero to free buffer)
00046  * @ret rc              Return status code
00047  */
00048 static int blktrans_xferbuf_realloc ( struct xfer_buffer *xferbuf,
00049                                       size_t len ) {
00050         struct block_translator *blktrans =
00051                 container_of ( xferbuf, struct block_translator, xferbuf );
00052 
00053         /* Record length, if applicable */
00054         if ( blktrans->buffer ) {
00055 
00056                 /* We have a (non-reallocatable) data buffer */
00057                 return -ENOTSUP;
00058 
00059         } else {
00060 
00061                 /* Record length (for block device capacity) */
00062                 xferbuf->len = len;
00063                 return 0;
00064         }
00065 }
00066 
00067 /**
00068  * Write data to block device translator data buffer
00069  *
00070  * @v xferbuf           Data transfer buffer
00071  * @v offset            Starting offset
00072  * @v data              Data to copy
00073  * @v len               Length of data
00074  */
00075 static void blktrans_xferbuf_write ( struct xfer_buffer *xferbuf, size_t offset,
00076                                      const void *data, size_t len ) {
00077         struct block_translator *blktrans =
00078                 container_of ( xferbuf, struct block_translator, xferbuf );
00079 
00080         /* Write data to buffer, if applicable */
00081         if ( blktrans->buffer ) {
00082 
00083                 /* Write data to buffer */
00084                 copy_to_user ( blktrans->buffer, offset, data, len );
00085 
00086         } else {
00087 
00088                 /* Sanity check */
00089                 assert ( len == 0 );
00090         }
00091 }
00092 
00093 /**
00094  * Read data from block device translator data buffer
00095  *
00096  * @v xferbuf           Data transfer buffer
00097  * @v offset            Starting offset
00098  * @v data              Data to read
00099  * @v len               Length of data
00100  */
00101 static void blktrans_xferbuf_read ( struct xfer_buffer *xferbuf, size_t offset,
00102                                     void *data, size_t len ) {
00103         struct block_translator *blktrans =
00104                 container_of ( xferbuf, struct block_translator, xferbuf );
00105 
00106         /* Read data from buffer, if applicable */
00107         if ( blktrans->buffer ) {
00108 
00109                 /* Read data from buffer */
00110                 copy_from_user ( data, blktrans->buffer, offset, len );
00111 
00112         } else {
00113 
00114                 /* Sanity check */
00115                 assert ( len == 0 );
00116         }
00117 }
00118 
00119 /** Block device translator data transfer buffer operations */
00120 static struct xfer_buffer_operations blktrans_xferbuf_operations = {
00121         .realloc = blktrans_xferbuf_realloc,
00122         .write = blktrans_xferbuf_write,
00123         .read = blktrans_xferbuf_read,
00124 };
00125 
00126 /**
00127  * Close block device translator
00128  *
00129  * @v blktrans          Block device translator
00130  * @v rc                Reason for close
00131  */
00132 static void blktrans_close ( struct block_translator *blktrans, int rc ) {
00133         struct block_device_capacity capacity;
00134 
00135         /* Report block device capacity, if applicable */
00136         if ( ( rc == 0 ) && ( blktrans->blksize ) ) {
00137 
00138                 /* Construct block device capacity */
00139                 capacity.blocks =
00140                         ( blktrans->xferbuf.len / blktrans->blksize );
00141                 capacity.blksize = blktrans->blksize;
00142                 capacity.max_count = -1U;
00143 
00144                 /* Report block device capacity */
00145                 block_capacity ( &blktrans->block, &capacity );
00146         }
00147 
00148         /* Shut down interfaces */
00149         intf_shutdown ( &blktrans->xfer, rc );
00150         intf_shutdown ( &blktrans->block, rc );
00151 }
00152 
00153 /**
00154  * Deliver data
00155  *
00156  * @v blktrans          Block device translator
00157  * @v iobuf             I/O buffer
00158  * @v meta              Data transfer metadata
00159  * @ret rc              Return status code
00160  */
00161 static int blktrans_deliver ( struct block_translator *blktrans,
00162                               struct io_buffer *iobuf,
00163                               struct xfer_metadata *meta ) {
00164         int rc;
00165 
00166         /* Deliver to buffer */
00167         if ( ( rc = xferbuf_deliver ( &blktrans->xferbuf, iob_disown ( iobuf ),
00168                                       meta ) ) != 0 ) {
00169                 DBGC ( blktrans, "BLKTRANS %p could not deliver: %s\n",
00170                        blktrans, strerror ( rc ) );
00171                 goto err;
00172         }
00173 
00174         return 0;
00175 
00176  err:
00177         blktrans_close ( blktrans, rc );
00178         return rc;
00179 }
00180 
00181 /**
00182  * Get underlying data transfer buffer
00183  *
00184  * @v blktrans          Block device translator
00185  * @ret xferbuf         Data transfer buffer
00186  */
00187 static struct xfer_buffer *
00188 blktrans_buffer ( struct block_translator *blktrans ) {
00189 
00190         return &blktrans->xferbuf;
00191 }
00192 
00193 /** Block device translator block device interface operations */
00194 static struct interface_operation blktrans_block_operations[] = {
00195         INTF_OP ( intf_close, struct block_translator *, blktrans_close ),
00196 };
00197 
00198 /** Block device translator block device interface descriptor */
00199 static struct interface_descriptor blktrans_block_desc =
00200         INTF_DESC_PASSTHRU ( struct block_translator, block,
00201                              blktrans_block_operations, xfer );
00202 
00203 /** Block device translator data transfer interface operations */
00204 static struct interface_operation blktrans_xfer_operations[] = {
00205         INTF_OP ( xfer_deliver, struct block_translator *, blktrans_deliver ),
00206         INTF_OP ( xfer_buffer, struct block_translator *, blktrans_buffer ),
00207         INTF_OP ( intf_close, struct block_translator *, blktrans_close ),
00208 };
00209 
00210 /** Block device translator data transfer interface descriptor */
00211 static struct interface_descriptor blktrans_xfer_desc =
00212         INTF_DESC_PASSTHRU ( struct block_translator, xfer,
00213                              blktrans_xfer_operations, block );
00214 
00215 /**
00216  * Insert block device translator
00217  *
00218  * @v block             Block device interface
00219  * @v buffer            Data buffer (or UNULL)
00220  * @v size              Length of data buffer, or block size
00221  * @ret rc              Return status code
00222  */
00223 int block_translate ( struct interface *block, userptr_t buffer, size_t size ) {
00224         struct block_translator *blktrans;
00225         int rc;
00226 
00227         /* Allocate and initialise structure */
00228         blktrans = zalloc ( sizeof ( *blktrans ) );
00229         if ( ! blktrans ) {
00230                 rc = -ENOMEM;
00231                 goto err_alloc;
00232         }
00233         ref_init ( &blktrans->refcnt, NULL );
00234         intf_init ( &blktrans->block, &blktrans_block_desc, &blktrans->refcnt );
00235         intf_init ( &blktrans->xfer, &blktrans_xfer_desc, &blktrans->refcnt );
00236         blktrans->xferbuf.op = &blktrans_xferbuf_operations;
00237         blktrans->buffer = buffer;
00238         if ( buffer ) {
00239                 blktrans->xferbuf.len = size;
00240         } else {
00241                 blktrans->blksize = size;
00242         }
00243 
00244         /* Attach to interfaces, mortalise self, and return */
00245         assert ( block->dest != &null_intf );
00246         intf_plug_plug ( &blktrans->xfer, block->dest );
00247         intf_plug_plug ( &blktrans->block, block );
00248         ref_put ( &blktrans->refcnt );
00249 
00250         DBGC2 ( blktrans, "BLKTRANS %p created", blktrans );
00251         if ( buffer ) {
00252                 DBGC2 ( blktrans, " for %#lx+%#zx",
00253                         user_to_phys ( buffer, 0 ), size );
00254         }
00255         DBGC2 ( blktrans, "\n" );
00256         return 0;
00257 
00258         ref_put ( &blktrans->refcnt );
00259  err_alloc:
00260         return rc;
00261 }