iPXE
Functions | Variables
blocktrans.c File Reference

Block device translator. More...

#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/blockdev.h>
#include <ipxe/blocktrans.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int blktrans_xferbuf_realloc (struct xfer_buffer *xferbuf, size_t len)
 Reallocate block device translator data buffer. More...
 
static void blktrans_xferbuf_write (struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
 Write data to block device translator data buffer. More...
 
static void blktrans_xferbuf_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read data from block device translator data buffer. More...
 
static void blktrans_close (struct block_translator *blktrans, int rc)
 Close block device translator. More...
 
static int blktrans_deliver (struct block_translator *blktrans, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Deliver data. More...
 
static struct xfer_bufferblktrans_buffer (struct block_translator *blktrans)
 Get underlying data transfer buffer. More...
 
int block_translate (struct interface *block, userptr_t buffer, size_t size)
 Insert block device translator. More...
 

Variables

static struct xfer_buffer_operations blktrans_xferbuf_operations
 Block device translator data transfer buffer operations. More...
 
static struct interface_operation blktrans_block_operations []
 Block device translator block device interface operations. More...
 
static struct interface_descriptor blktrans_block_desc
 Block device translator block device interface descriptor. More...
 
static struct interface_operation blktrans_xfer_operations []
 Block device translator data transfer interface operations. More...
 
static struct interface_descriptor blktrans_xfer_desc
 Block device translator data transfer interface descriptor. More...
 

Detailed Description

Block device translator.

Definition in file blocktrans.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ blktrans_xferbuf_realloc()

static int blktrans_xferbuf_realloc ( struct xfer_buffer xferbuf,
size_t  len 
)
static

Reallocate block device translator data buffer.

Parameters
xferbufData transfer buffer
lenNew length (or zero to free buffer)
Return values
rcReturn status code

Definition at line 48 of file blocktrans.c.

49  {
50  struct block_translator *blktrans =
52 
53  /* Record length, if applicable */
54  if ( blktrans->buffer ) {
55 
56  /* We have a (non-reallocatable) data buffer */
57  return -ENOTSUP;
58 
59  } else {
60 
61  /* Record length (for block device capacity) */
62  xferbuf->len = len;
63  return 0;
64  }
65 }
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
size_t len
Size of data.
Definition: xferbuf.h:23
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A block device translator.
Definition: blocktrans.h:19
uint32_t len
Length.
Definition: ena.h:14
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28
userptr_t buffer
Data buffer.
Definition: blocktrans.h:30

References block_translator::buffer, container_of, ENOTSUP, len, xfer_buffer::len, and block_translator::xferbuf.

◆ blktrans_xferbuf_write()

static void blktrans_xferbuf_write ( struct xfer_buffer xferbuf,
size_t  offset,
const void *  data,
size_t  len 
)
static

Write data to block device translator data buffer.

Parameters
xferbufData transfer buffer
offsetStarting offset
dataData to copy
lenLength of data

Definition at line 75 of file blocktrans.c.

76  {
77  struct block_translator *blktrans =
79 
80  /* Write data to buffer, if applicable */
81  if ( blktrans->buffer ) {
82 
83  /* Write data to buffer */
84  copy_to_user ( blktrans->buffer, offset, data, len );
85 
86  } else {
87 
88  /* Sanity check */
89  assert ( len == 0 );
90  }
91 }
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A block device translator.
Definition: blocktrans.h:19
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
uint32_t len
Length.
Definition: ena.h:14
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
userptr_t buffer
Data buffer.
Definition: blocktrans.h:30

References assert(), block_translator::buffer, container_of, copy_to_user(), data, len, offset, and block_translator::xferbuf.

◆ blktrans_xferbuf_read()

static void blktrans_xferbuf_read ( struct xfer_buffer xferbuf,
size_t  offset,
void *  data,
size_t  len 
)
static

Read data from block device translator data buffer.

Parameters
xferbufData transfer buffer
offsetStarting offset
dataData to read
lenLength of data

Definition at line 101 of file blocktrans.c.

102  {
103  struct block_translator *blktrans =
105 
106  /* Read data from buffer, if applicable */
107  if ( blktrans->buffer ) {
108 
109  /* Read data from buffer */
110  copy_from_user ( data, blktrans->buffer, offset, len );
111 
112  } else {
113 
114  /* Sanity check */
115  assert ( len == 0 );
116  }
117 }
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A block device translator.
Definition: blocktrans.h:19
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint32_t len
Length.
Definition: ena.h:14
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
userptr_t buffer
Data buffer.
Definition: blocktrans.h:30

References assert(), block_translator::buffer, container_of, copy_from_user(), data, len, offset, and block_translator::xferbuf.

◆ blktrans_close()

static void blktrans_close ( struct block_translator blktrans,
int  rc 
)
static

Close block device translator.

Parameters
blktransBlock device translator
rcReason for close

Definition at line 132 of file blocktrans.c.

132  {
133  struct block_device_capacity capacity;
134 
135  /* Report block device capacity, if applicable */
136  if ( ( rc == 0 ) && ( blktrans->blksize ) ) {
137 
138  /* Construct block device capacity */
139  capacity.blocks =
140  ( blktrans->xferbuf.len / blktrans->blksize );
141  capacity.blksize = blktrans->blksize;
142  capacity.max_count = -1U;
143 
144  /* Report block device capacity */
145  block_capacity ( &blktrans->block, &capacity );
146  }
147 
148  /* Shut down interfaces */
149  intf_shutdown ( &blktrans->xfer, rc );
150  intf_shutdown ( &blktrans->block, rc );
151 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
void block_capacity(struct interface *intf, struct block_device_capacity *capacity)
Report block device capacity.
Definition: blockdev.c:129
size_t len
Size of data.
Definition: xferbuf.h:23
uint64_t blocks
Total number of blocks.
Definition: blockdev.h:20
struct interface xfer
Data transfer interface.
Definition: blocktrans.h:25
Block device capacity.
Definition: blockdev.h:18
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28
size_t blksize
Block size.
Definition: blocktrans.h:32
struct interface block
Block device interface.
Definition: blocktrans.h:23

References block_device_capacity::blksize, block_translator::blksize, block_translator::block, block_capacity(), block_device_capacity::blocks, intf_shutdown(), xfer_buffer::len, block_device_capacity::max_count, rc, block_translator::xfer, and block_translator::xferbuf.

Referenced by blktrans_deliver().

◆ blktrans_deliver()

static int blktrans_deliver ( struct block_translator blktrans,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Deliver data.

Parameters
blktransBlock device translator
iobufI/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 161 of file blocktrans.c.

163  {
164  int rc;
165 
166  /* Deliver to buffer */
167  if ( ( rc = xferbuf_deliver ( &blktrans->xferbuf, iob_disown ( iobuf ),
168  meta ) ) != 0 ) {
169  DBGC ( blktrans, "BLKTRANS %p could not deliver: %s\n",
170  blktrans, strerror ( rc ) );
171  goto err;
172  }
173 
174  return 0;
175 
176  err:
177  blktrans_close ( blktrans, rc );
178  return rc;
179 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int xferbuf_deliver(struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Add received data to data transfer buffer.
Definition: xferbuf.c:152
static void blktrans_close(struct block_translator *blktrans, int rc)
Close block device translator.
Definition: blocktrans.c:132
#define DBGC(...)
Definition: compiler.h:505
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
int meta(WINDOW *, bool)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28

References blktrans_close(), DBGC, iob_disown, meta(), rc, strerror(), block_translator::xferbuf, and xferbuf_deliver().

◆ blktrans_buffer()

static struct xfer_buffer* blktrans_buffer ( struct block_translator blktrans)
static

Get underlying data transfer buffer.

Parameters
blktransBlock device translator
Return values
xferbufData transfer buffer

Definition at line 188 of file blocktrans.c.

188  {
189 
190  return &blktrans->xferbuf;
191 }
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28

References block_translator::xferbuf.

◆ block_translate()

int block_translate ( struct interface block,
userptr_t  buffer,
size_t  size 
)

Insert block device translator.

Parameters
blockBlock device interface
bufferData buffer (or UNULL)
sizeLength of data buffer, or block size
Return values
rcReturn status code

Definition at line 223 of file blocktrans.c.

223  {
224  struct block_translator *blktrans;
225  int rc;
226 
227  /* Allocate and initialise structure */
228  blktrans = zalloc ( sizeof ( *blktrans ) );
229  if ( ! blktrans ) {
230  rc = -ENOMEM;
231  goto err_alloc;
232  }
233  ref_init ( &blktrans->refcnt, NULL );
234  intf_init ( &blktrans->block, &blktrans_block_desc, &blktrans->refcnt );
235  intf_init ( &blktrans->xfer, &blktrans_xfer_desc, &blktrans->refcnt );
237  blktrans->buffer = buffer;
238  if ( buffer ) {
239  blktrans->xferbuf.len = size;
240  } else {
241  blktrans->blksize = size;
242  }
243 
244  /* Attach to interfaces, mortalise self, and return */
245  assert ( block->dest != &null_intf );
246  intf_plug_plug ( &blktrans->xfer, block->dest );
247  intf_plug_plug ( &blktrans->block, block );
248  ref_put ( &blktrans->refcnt );
249 
250  DBGC2 ( blktrans, "BLKTRANS %p created", blktrans );
251  if ( buffer ) {
252  DBGC2 ( blktrans, " for %#lx+%#zx",
253  user_to_phys ( buffer, 0 ), size );
254  }
255  DBGC2 ( blktrans, "\n" );
256  return 0;
257 
258  ref_put ( &blktrans->refcnt );
259  err_alloc:
260  return rc;
261 }
struct refcnt refcnt
Reference count.
Definition: blocktrans.h:21
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct interface null_intf
The null interface.
Definition: interface.c:65
static struct interface_descriptor blktrans_block_desc
Block device translator block device interface descriptor.
Definition: blocktrans.c:199
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
uint16_t block
Definition: tftp.h:12
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition: netvsc.h:16
size_t len
Size of data.
Definition: xferbuf.h:23
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A block device translator.
Definition: blocktrans.h:19
struct interface xfer
Data transfer interface.
Definition: blocktrans.h:25
struct xfer_buffer_operations * op
Data transfer buffer operations.
Definition: xferbuf.h:27
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static struct interface_descriptor blktrans_xfer_desc
Block device translator data transfer interface descriptor.
Definition: blocktrans.c:211
#define DBGC2(...)
Definition: compiler.h:522
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
struct xfer_buffer xferbuf
Data transfer buffer.
Definition: blocktrans.h:28
size_t blksize
Block size.
Definition: blocktrans.h:32
static struct xfer_buffer_operations blktrans_xferbuf_operations
Block device translator data transfer buffer operations.
Definition: blocktrans.c:120
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
userptr_t buffer
Data buffer.
Definition: blocktrans.h:30
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
struct interface block
Block device interface.
Definition: blocktrans.h:23

References assert(), block_translator::blksize, blktrans_block_desc, blktrans_xfer_desc, blktrans_xferbuf_operations, block, block_translator::block, buffer, block_translator::buffer, DBGC2, ENOMEM, intf_init(), intf_plug_plug(), xfer_buffer::len, NULL, null_intf, xfer_buffer::op, rc, ref_init, ref_put, block_translator::refcnt, size, user_to_phys(), block_translator::xfer, block_translator::xferbuf, and zalloc().

Referenced by http_block_read(), and http_block_read_capacity().

Variable Documentation

◆ blktrans_xferbuf_operations

struct xfer_buffer_operations blktrans_xferbuf_operations
static
Initial value:
= {
}
static int blktrans_xferbuf_realloc(struct xfer_buffer *xferbuf, size_t len)
Reallocate block device translator data buffer.
Definition: blocktrans.c:48
static void blktrans_xferbuf_read(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read data from block device translator data buffer.
Definition: blocktrans.c:101
static void blktrans_xferbuf_write(struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
Write data to block device translator data buffer.
Definition: blocktrans.c:75

Block device translator data transfer buffer operations.

Definition at line 120 of file blocktrans.c.

Referenced by block_translate().

◆ blktrans_block_operations

struct interface_operation blktrans_block_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void blktrans_close(struct block_translator *blktrans, int rc)
Close block device translator.
Definition: blocktrans.c:132
A block device translator.
Definition: blocktrans.h:19
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32

Block device translator block device interface operations.

Definition at line 194 of file blocktrans.c.

◆ blktrans_block_desc

struct interface_descriptor blktrans_block_desc
static
Initial value:
=
uint16_t block
Definition: tftp.h:12
A block device translator.
Definition: blocktrans.h:19
struct interface xfer
Data transfer interface.
Definition: blocktrans.h:25
static struct interface_operation blktrans_block_operations[]
Block device translator block device interface operations.
Definition: blocktrans.c:194
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:82

Block device translator block device interface descriptor.

Definition at line 199 of file blocktrans.c.

Referenced by block_translate().

◆ blktrans_xfer_operations

struct interface_operation blktrans_xfer_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void blktrans_close(struct block_translator *blktrans, int rc)
Close block device translator.
Definition: blocktrans.c:132
A data transfer buffer.
Definition: xferbuf.h:19
A block device translator.
Definition: blocktrans.h:19
static struct xfer_buffer * blktrans_buffer(struct block_translator *blktrans)
Get underlying data transfer buffer.
Definition: blocktrans.c:188
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
static int blktrans_deliver(struct block_translator *blktrans, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver data.
Definition: blocktrans.c:161

Block device translator data transfer interface operations.

Definition at line 204 of file blocktrans.c.

◆ blktrans_xfer_desc

struct interface_descriptor blktrans_xfer_desc
static
Initial value:
=
uint16_t block
Definition: tftp.h:12
A block device translator.
Definition: blocktrans.h:19
struct interface xfer
Data transfer interface.
Definition: blocktrans.h:25
static struct interface_operation blktrans_xfer_operations[]
Block device translator data transfer interface operations.
Definition: blocktrans.c:204
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:82

Block device translator data transfer interface descriptor.

Definition at line 211 of file blocktrans.c.

Referenced by block_translate().