source: mainline/uspace/lib/bithenge/src/helenos/block.c

Last change on this file was 7ae01d5, checked in by Jiri Svoboda <jiri@…>, 15 months ago

Remove unused comm_size parameter of block_init()

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