| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup bithenge
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Access block devices as blobs.
|
|---|
| 35 | * @todo Provide more information about the block device (block size).
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include <block.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <macros.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <bithenge/blob.h>
|
|---|
| 45 | #include "block.h"
|
|---|
| 46 |
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | bithenge_blob_t base;
|
|---|
| 49 | service_id_t service_id;
|
|---|
| 50 | aoff64_t size;
|
|---|
| 51 | } block_blob_t;
|
|---|
| 52 |
|
|---|
| 53 | static inline block_blob_t *blob_as_block(bithenge_blob_t *base)
|
|---|
| 54 | {
|
|---|
| 55 | return (block_blob_t *)base;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | static inline bithenge_blob_t *block_as_blob(block_blob_t *blob)
|
|---|
| 59 | {
|
|---|
| 60 | return &blob->base;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | static int block_size(bithenge_blob_t *base, aoff64_t *size)
|
|---|
| 64 | {
|
|---|
| 65 | block_blob_t *self = blob_as_block(base);
|
|---|
| 66 | *size = self->size;
|
|---|
| 67 | return EOK;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | static int block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
|
|---|
| 71 | aoff64_t *size)
|
|---|
| 72 | {
|
|---|
| 73 | block_blob_t *self = blob_as_block(base);
|
|---|
| 74 | if (offset > self->size)
|
|---|
| 75 | return ELIMIT;
|
|---|
| 76 | *size = min(*size, self->size - offset);
|
|---|
| 77 | return block_read_bytes_direct(self->service_id, offset, *size, buffer);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static void block_destroy(bithenge_blob_t *base)
|
|---|
| 81 | {
|
|---|
| 82 | block_blob_t *self = blob_as_block(base);
|
|---|
| 83 | block_fini(self->service_id);
|
|---|
| 84 | free(self);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | static const bithenge_random_access_blob_ops_t block_ops = {
|
|---|
| 88 | .size = block_size,
|
|---|
| 89 | .read = block_read,
|
|---|
| 90 | .destroy = block_destroy,
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | /** Create a blob for a block device. The blob must be freed with
|
|---|
| 94 | * @a bithenge_node_t::bithenge_node_destroy after it is used.
|
|---|
| 95 | * @param[out] out Stores the created blob.
|
|---|
| 96 | * @param service_id The service ID of the block device.
|
|---|
| 97 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 98 | int bithenge_new_block_blob(bithenge_node_t **out, service_id_t service_id)
|
|---|
| 99 | {
|
|---|
| 100 | assert(out);
|
|---|
| 101 |
|
|---|
| 102 | // Initialize libblock
|
|---|
| 103 | int rc;
|
|---|
| 104 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
|
|---|
| 105 | if (rc != EOK)
|
|---|
| 106 | return rc;
|
|---|
| 107 |
|
|---|
| 108 | // Calculate total device size
|
|---|
| 109 | size_t bsize;
|
|---|
| 110 | aoff64_t nblocks;
|
|---|
| 111 | aoff64_t size;
|
|---|
| 112 | rc = block_get_bsize(service_id, &bsize);
|
|---|
| 113 | if (rc != EOK) {
|
|---|
| 114 | block_fini(service_id);
|
|---|
| 115 | return rc;
|
|---|
| 116 | }
|
|---|
| 117 | rc = block_get_nblocks(service_id, &nblocks);
|
|---|
| 118 | if (rc != EOK) {
|
|---|
| 119 | block_fini(service_id);
|
|---|
| 120 | return rc;
|
|---|
| 121 | }
|
|---|
| 122 | size = bsize * nblocks;
|
|---|
| 123 |
|
|---|
| 124 | // Create blob
|
|---|
| 125 | block_blob_t *blob = malloc(sizeof(*blob));
|
|---|
| 126 | if (!blob) {
|
|---|
| 127 | block_fini(service_id);
|
|---|
| 128 | return ENOMEM;
|
|---|
| 129 | }
|
|---|
| 130 | rc = bithenge_init_random_access_blob(block_as_blob(blob),
|
|---|
| 131 | &block_ops);
|
|---|
| 132 | if (rc != EOK) {
|
|---|
| 133 | free(blob);
|
|---|
| 134 | block_fini(service_id);
|
|---|
| 135 | return rc;
|
|---|
| 136 | }
|
|---|
| 137 | blob->service_id = service_id;
|
|---|
| 138 | blob->size = size;
|
|---|
| 139 | *out = bithenge_blob_as_node(block_as_blob(blob));
|
|---|
| 140 |
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** @}
|
|---|
| 145 | */
|
|---|