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
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
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
39#include <assert.h>
40#include <block.h>
41#include <errno.h>
42#include <loc.h>
43#include <macros.h>
44#include <stdlib.h>
45#include <bithenge/blob.h>
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
54static inline block_blob_t *blob_as_block(bithenge_blob_t *base)
55{
56 return (block_blob_t *)base;
57}
58
59static inline bithenge_blob_t *block_as_blob(block_blob_t *blob)
60{
61 return &blob->base;
62}
63
64static errno_t block_size(bithenge_blob_t *base, aoff64_t *size)
65{
66 block_blob_t *self = blob_as_block(base);
67 *size = self->size;
68 return EOK;
69}
70
71static errno_t block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
72 aoff64_t *size)
73{
74 block_blob_t *self = blob_as_block(base);
75 if (offset > self->size)
76 return ELIMIT;
77 *size = min(*size, self->size - offset);
78 return block_read_bytes_direct(self->service_id, offset, *size, buffer);
79}
80
81static void block_destroy(bithenge_blob_t *base)
82{
83 block_blob_t *self = blob_as_block(base);
84 block_fini(self->service_id);
85 free(self);
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
95 * @a bithenge_node_t::bithenge_node_destroy after it is used.
96 * @param[out] out Stores the created blob.
97 * @param service_id The service ID of the block device.
98 * @return EOK on success or an error code from errno.h.
99 */
100errno_t bithenge_new_block_blob(bithenge_node_t **out, service_id_t service_id)
101{
102 assert(out);
103
104 // Initialize libblock
105 errno_t rc;
106 rc = block_init(service_id);
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);
115 if (rc != EOK) {
116 block_fini(service_id);
117 return rc;
118 }
119 rc = block_get_nblocks(service_id, &nblocks);
120 if (rc != EOK) {
121 block_fini(service_id);
122 return rc;
123 }
124 size = bsize * nblocks;
125
126 // Create blob
127 block_blob_t *blob = malloc(sizeof(*blob));
128 if (!blob) {
129 block_fini(service_id);
130 return ENOMEM;
131 }
132 rc = bithenge_init_random_access_blob(block_as_blob(blob),
133 &block_ops);
134 if (rc != EOK) {
135 free(blob);
136 block_fini(service_id);
137 return rc;
138 }
139 blob->service_id = service_id;
140 blob->size = size;
141 *out = bithenge_blob_as_node(block_as_blob(blob));
142
143 return EOK;
144}
145
146/** @}
147 */
Note: See TracBrowser for help on using the repository browser.