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.
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.
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.
static void blktrans_close (struct block_translator *blktrans, int rc)
 Close block device translator.
static int blktrans_deliver (struct block_translator *blktrans, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Deliver data.
static struct xfer_bufferblktrans_buffer (struct block_translator *blktrans)
 Get underlying data transfer buffer.
int block_translate (struct interface *block, userptr_t buffer, size_t size)
 Insert block device translator.

Variables

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

Detailed Description

Block device translator.

Definition in file blocktrans.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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.

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

                                                   {
        struct block_translator *blktrans =
                container_of ( xferbuf, struct block_translator, xferbuf );

        /* Record length, if applicable */
        if ( blktrans->buffer ) {

                /* We have a (non-reallocatable) data buffer */
                return -ENOTSUP;

        } else {

                /* Record length (for block device capacity) */
                xferbuf->len = len;
                return 0;
        }
}
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.

References assert, block_translator::buffer, container_of, and copy_to_user().

                                                                    {
        struct block_translator *blktrans =
                container_of ( xferbuf, struct block_translator, xferbuf );

        /* Write data to buffer, if applicable */
        if ( blktrans->buffer ) {

                /* Write data to buffer */
                copy_to_user ( blktrans->buffer, offset, data, len );

        } else {

                /* Sanity check */
                assert ( len == 0 );
        }
}
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.

References assert, block_translator::buffer, container_of, and copy_from_user().

                                                             {
        struct block_translator *blktrans =
                container_of ( xferbuf, struct block_translator, xferbuf );

        /* Read data from buffer, if applicable */
        if ( blktrans->buffer ) {

                /* Read data from buffer */
                copy_from_user ( data, blktrans->buffer, offset, len );

        } else {

                /* Sanity check */
                assert ( len == 0 );
        }
}
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.

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, block_translator::xfer, and block_translator::xferbuf.

Referenced by blktrans_deliver().

                                                                         {
        struct block_device_capacity capacity;

        /* Report block device capacity, if applicable */
        if ( ( rc == 0 ) && ( blktrans->blksize ) ) {

                /* Construct block device capacity */
                capacity.blocks =
                        ( blktrans->xferbuf.len / blktrans->blksize );
                capacity.blksize = blktrans->blksize;
                capacity.max_count = -1U;

                /* Report block device capacity */
                block_capacity ( &blktrans->block, &capacity );
        }

        /* Shut down interfaces */
        intf_shutdown ( &blktrans->xfer, rc );
        intf_shutdown ( &blktrans->block, rc );
}
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.

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

                                                           {
        int rc;

        /* Deliver to buffer */
        if ( ( rc = xferbuf_deliver ( &blktrans->xferbuf, iob_disown ( iobuf ),
                                      meta ) ) != 0 ) {
                DBGC ( blktrans, "BLKTRANS %p could not deliver: %s\n",
                       blktrans, strerror ( rc ) );
                goto err;
        }

        return 0;

 err:
        blktrans_close ( blktrans, rc );
        return rc;
}
static struct xfer_buffer* blktrans_buffer ( struct block_translator blktrans) [static, read]

Get underlying data transfer buffer.

Parameters:
blktransBlock device translator
Return values:
xferbufData transfer buffer

Definition at line 188 of file blocktrans.c.

References block_translator::xferbuf.

                                                      {

        return &blktrans->xferbuf;
}
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.

References assert, block_translator::blksize, blktrans_xferbuf_operations, block_translator::block, block_translator::buffer, buffer, DBGC2, interface::dest, 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().

                                                                               {
        struct block_translator *blktrans;
        int rc;

        /* Allocate and initialise structure */
        blktrans = zalloc ( sizeof ( *blktrans ) );
        if ( ! blktrans ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        ref_init ( &blktrans->refcnt, NULL );
        intf_init ( &blktrans->block, &blktrans_block_desc, &blktrans->refcnt );
        intf_init ( &blktrans->xfer, &blktrans_xfer_desc, &blktrans->refcnt );
        blktrans->xferbuf.op = &blktrans_xferbuf_operations;
        blktrans->buffer = buffer;
        if ( buffer ) {
                blktrans->xferbuf.len = size;
        } else {
                blktrans->blksize = size;
        }

        /* Attach to interfaces, mortalise self, and return */
        assert ( block->dest != &null_intf );
        intf_plug_plug ( &blktrans->xfer, block->dest );
        intf_plug_plug ( &blktrans->block, block );
        ref_put ( &blktrans->refcnt );

        DBGC2 ( blktrans, "BLKTRANS %p created", blktrans );
        if ( buffer ) {
                DBGC2 ( blktrans, " for %#lx+%#zx",
                        user_to_phys ( buffer, 0 ), size );
        }
        DBGC2 ( blktrans, "\n" );
        return 0;

        ref_put ( &blktrans->refcnt );
 err_alloc:
        return rc;
}

Variable Documentation

Initial value:

Block device translator data transfer buffer operations.

Definition at line 120 of file blocktrans.c.

Referenced by block_translate().

Initial value:

Block device translator block device interface operations.

Definition at line 194 of file blocktrans.c.

Initial value:

Block device translator block device interface descriptor.

Definition at line 199 of file blocktrans.c.

Initial value:

Block device translator data transfer interface operations.

Definition at line 204 of file blocktrans.c.

Initial value:

Block device translator data transfer interface descriptor.

Definition at line 211 of file blocktrans.c.