Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

blocktrans.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * Block device translator
  28. *
  29. */
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include <assert.h>
  33. #include <ipxe/iobuf.h>
  34. #include <ipxe/xfer.h>
  35. #include <ipxe/blockdev.h>
  36. #include <ipxe/blocktrans.h>
  37. /**
  38. * Reallocate block device translator data buffer
  39. *
  40. * @v xferbuf Data transfer buffer
  41. * @v len New length (or zero to free buffer)
  42. * @ret rc Return status code
  43. */
  44. static int blktrans_xferbuf_realloc ( struct xfer_buffer *xferbuf,
  45. size_t len ) {
  46. struct block_translator *blktrans =
  47. container_of ( xferbuf, struct block_translator, xferbuf );
  48. /* Record length, if applicable */
  49. if ( blktrans->buffer ) {
  50. /* We have a (non-reallocatable) data buffer */
  51. return -ENOTSUP;
  52. } else {
  53. /* Record length (for block device capacity) */
  54. xferbuf->len = len;
  55. return 0;
  56. }
  57. }
  58. /**
  59. * Write data to block device translator data buffer
  60. *
  61. * @v xferbuf Data transfer buffer
  62. * @v offset Starting offset
  63. * @v data Data to copy
  64. * @v len Length of data
  65. */
  66. static void blktrans_xferbuf_write ( struct xfer_buffer *xferbuf, size_t offset,
  67. const void *data, size_t len ) {
  68. struct block_translator *blktrans =
  69. container_of ( xferbuf, struct block_translator, xferbuf );
  70. /* Write data to buffer, if applicable */
  71. if ( blktrans->buffer ) {
  72. /* Write data to buffer */
  73. copy_to_user ( blktrans->buffer, offset, data, len );
  74. } else {
  75. /* Sanity check */
  76. assert ( len == 0 );
  77. }
  78. }
  79. /**
  80. * Read data from block device translator data buffer
  81. *
  82. * @v xferbuf Data transfer buffer
  83. * @v offset Starting offset
  84. * @v data Data to read
  85. * @v len Length of data
  86. */
  87. static void blktrans_xferbuf_read ( struct xfer_buffer *xferbuf, size_t offset,
  88. void *data, size_t len ) {
  89. struct block_translator *blktrans =
  90. container_of ( xferbuf, struct block_translator, xferbuf );
  91. /* Read data from buffer, if applicable */
  92. if ( blktrans->buffer ) {
  93. /* Read data from buffer */
  94. copy_from_user ( data, blktrans->buffer, offset, len );
  95. } else {
  96. /* Sanity check */
  97. assert ( len == 0 );
  98. }
  99. }
  100. /** Block device translator data transfer buffer operations */
  101. static struct xfer_buffer_operations blktrans_xferbuf_operations = {
  102. .realloc = blktrans_xferbuf_realloc,
  103. .write = blktrans_xferbuf_write,
  104. .read = blktrans_xferbuf_read,
  105. };
  106. /**
  107. * Close block device translator
  108. *
  109. * @v blktrans Block device translator
  110. * @v rc Reason for close
  111. */
  112. static void blktrans_close ( struct block_translator *blktrans, int rc ) {
  113. struct block_device_capacity capacity;
  114. /* Report block device capacity, if applicable */
  115. if ( ( rc == 0 ) && ( blktrans->blksize ) ) {
  116. /* Construct block device capacity */
  117. capacity.blocks =
  118. ( blktrans->xferbuf.len / blktrans->blksize );
  119. capacity.blksize = blktrans->blksize;
  120. capacity.max_count = -1U;
  121. /* Report block device capacity */
  122. block_capacity ( &blktrans->block, &capacity );
  123. }
  124. /* Shut down interfaces */
  125. intf_shutdown ( &blktrans->xfer, rc );
  126. intf_shutdown ( &blktrans->block, rc );
  127. }
  128. /**
  129. * Deliver data
  130. *
  131. * @v blktrans Block device translator
  132. * @v iobuf I/O buffer
  133. * @v meta Data transfer metadata
  134. * @ret rc Return status code
  135. */
  136. static int blktrans_deliver ( struct block_translator *blktrans,
  137. struct io_buffer *iobuf,
  138. struct xfer_metadata *meta ) {
  139. int rc;
  140. /* Deliver to buffer */
  141. if ( ( rc = xferbuf_deliver ( &blktrans->xferbuf, iob_disown ( iobuf ),
  142. meta ) ) != 0 ) {
  143. DBGC ( blktrans, "BLKTRANS %p could not deliver: %s\n",
  144. blktrans, strerror ( rc ) );
  145. goto err;
  146. }
  147. return 0;
  148. err:
  149. blktrans_close ( blktrans, rc );
  150. return rc;
  151. }
  152. /**
  153. * Get underlying data transfer buffer
  154. *
  155. * @v blktrans Block device translator
  156. * @ret xferbuf Data transfer buffer
  157. */
  158. static struct xfer_buffer *
  159. blktrans_buffer ( struct block_translator *blktrans ) {
  160. return &blktrans->xferbuf;
  161. }
  162. /** Block device translator block device interface operations */
  163. static struct interface_operation blktrans_block_operations[] = {
  164. INTF_OP ( intf_close, struct block_translator *, blktrans_close ),
  165. };
  166. /** Block device translator block device interface descriptor */
  167. static struct interface_descriptor blktrans_block_desc =
  168. INTF_DESC_PASSTHRU ( struct block_translator, block,
  169. blktrans_block_operations, xfer );
  170. /** Block device translator data transfer interface operations */
  171. static struct interface_operation blktrans_xfer_operations[] = {
  172. INTF_OP ( xfer_deliver, struct block_translator *, blktrans_deliver ),
  173. INTF_OP ( xfer_buffer, struct block_translator *, blktrans_buffer ),
  174. INTF_OP ( intf_close, struct block_translator *, blktrans_close ),
  175. };
  176. /** Block device translator data transfer interface descriptor */
  177. static struct interface_descriptor blktrans_xfer_desc =
  178. INTF_DESC_PASSTHRU ( struct block_translator, xfer,
  179. blktrans_xfer_operations, block );
  180. /**
  181. * Insert block device translator
  182. *
  183. * @v block Block device interface
  184. * @v buffer Data buffer (or UNULL)
  185. * @v size Length of data buffer, or block size
  186. * @ret rc Return status code
  187. */
  188. int block_translate ( struct interface *block, userptr_t buffer, size_t size ) {
  189. struct block_translator *blktrans;
  190. int rc;
  191. /* Allocate and initialise structure */
  192. blktrans = zalloc ( sizeof ( *blktrans ) );
  193. if ( ! blktrans ) {
  194. rc = -ENOMEM;
  195. goto err_alloc;
  196. }
  197. ref_init ( &blktrans->refcnt, NULL );
  198. intf_init ( &blktrans->block, &blktrans_block_desc, &blktrans->refcnt );
  199. intf_init ( &blktrans->xfer, &blktrans_xfer_desc, &blktrans->refcnt );
  200. blktrans->xferbuf.op = &blktrans_xferbuf_operations;
  201. blktrans->buffer = buffer;
  202. if ( buffer ) {
  203. blktrans->xferbuf.len = size;
  204. } else {
  205. blktrans->blksize = size;
  206. }
  207. /* Attach to interfaces, mortalise self, and return */
  208. assert ( block->dest != &null_intf );
  209. intf_plug_plug ( &blktrans->xfer, block->dest );
  210. intf_plug_plug ( &blktrans->block, block );
  211. ref_put ( &blktrans->refcnt );
  212. DBGC2 ( blktrans, "BLKTRANS %p created", blktrans );
  213. if ( buffer ) {
  214. DBGC2 ( blktrans, " for %#lx+%#zx",
  215. user_to_phys ( buffer, 0 ), size );
  216. }
  217. DBGC2 ( blktrans, "\n" );
  218. return 0;
  219. ref_put ( &blktrans->refcnt );
  220. err_alloc:
  221. return rc;
  222. }