source: mainline/uspace/srv/bd/hr/raid1.c@ 12cbf25e

Last change on this file since 12cbf25e was 12cbf25e, checked in by Miroslav Cimerman <mc@…>, 11 months ago

hr: disk size checking

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2024 Miroslav Cimerman
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 hr
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <bd_srv.h>
37#include <block.h>
38#include <errno.h>
39#include <hr.h>
40#include <io/log.h>
41#include <ipc/hr.h>
42#include <ipc/services.h>
43#include <loc.h>
44#include <task.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <str_error.h>
48
49#include "var.h"
50#include "util.h"
51
52extern fibril_mutex_t big_lock;
53extern loc_srv_t *hr_srv;
54
55static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
56static errno_t hr_raid1_bd_close(bd_srv_t *);
57static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
58 size_t);
59static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
60static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
61 const void *, size_t);
62static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
63static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
64
65static bd_ops_t hr_raid1_bd_ops = {
66 .open = hr_raid1_bd_open,
67 .close = hr_raid1_bd_close,
68 .sync_cache = hr_raid1_bd_sync_cache,
69 .read_blocks = hr_raid1_bd_read_blocks,
70 .write_blocks = hr_raid1_bd_write_blocks,
71 .get_block_size = hr_raid1_bd_get_block_size,
72 .get_num_blocks = hr_raid1_bd_get_num_blocks
73};
74
75static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
76{
77 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
78 return EOK;
79}
80
81static errno_t hr_raid1_bd_close(bd_srv_t *bd)
82{
83 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
84 return EOK;
85}
86
87static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t size)
88{
89 fibril_mutex_lock(&big_lock);
90 hr_volume_t *vol = bd->srvs->sarg;
91
92 errno_t rc;
93 size_t i;
94
95 for (i = 0; i < vol->dev_no; i++) {
96 rc = block_sync_cache(vol->devs[i], ba, size);
97 if (rc != EOK)
98 break;
99 }
100
101 fibril_mutex_unlock(&big_lock);
102 return rc;
103}
104
105static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
106 void *buf, size_t size)
107{
108 fibril_mutex_lock(&big_lock);
109 hr_volume_t *vol = bd->srvs->sarg;
110
111 errno_t rc;
112 size_t i;
113
114 for (i = 0; i < vol->dev_no; i++) {
115 rc = block_read_direct(vol->devs[i], ba, cnt, buf);
116 if (rc != EOK)
117 break;
118 }
119
120 fibril_mutex_unlock(&big_lock);
121 return rc;
122}
123
124static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
125 const void *data, size_t size)
126{
127 fibril_mutex_lock(&big_lock);
128 hr_volume_t *vol = bd->srvs->sarg;
129
130 errno_t rc;
131 size_t i;
132
133 for (i = 0; i < vol->dev_no; i++) {
134 rc = block_write_direct(vol->devs[i], ba, cnt, data);
135 if (rc != EOK)
136 break;
137 }
138
139 fibril_mutex_unlock(&big_lock);
140 return rc;
141}
142
143static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
144{
145 hr_volume_t *vol = bd->srvs->sarg;
146
147 *rsize = vol->bsize;
148 return EOK;
149}
150
151static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
152{
153 hr_volume_t *vol = bd->srvs->sarg;
154
155 *rnb = vol->nblocks;
156 return EOK;
157}
158
159errno_t hr_raid1_create(hr_volume_t *new_volume)
160{
161 assert(new_volume->level == hr_l_1);
162
163 if (new_volume->dev_no < 2) {
164 log_msg(LOG_DEFAULT, LVL_ERROR,
165 "RAID 1 array needs at least 2 devices");
166 return EINVAL;
167 }
168
169 errno_t rc;
170 size_t i, bsize, last_bsize;
171 uint64_t nblocks, last_nblocks;
172 uint64_t total_blocks = 0;
173
174 rc = hr_init_devs(new_volume);
175 if (rc != EOK)
176 return rc;
177
178 for (i = 0; i < new_volume->dev_no; i++) {
179 rc = block_get_nblocks(new_volume->devs[i], &nblocks);
180 if (rc != EOK)
181 goto error;
182 if (i != 0 && nblocks != last_nblocks) {
183 log_msg(LOG_DEFAULT, LVL_ERROR,
184 "number of blocks differs");
185 rc = EINVAL;
186 goto error;
187 }
188 total_blocks += nblocks;
189 last_nblocks = nblocks;
190 }
191
192 for (i = 0; i < new_volume->dev_no; i++) {
193 rc = block_get_bsize(new_volume->devs[i], &bsize);
194 if (rc != EOK)
195 goto error;
196 if (i != 0 && bsize != last_bsize) {
197 log_msg(LOG_DEFAULT, LVL_ERROR, "block sizes differ");
198 rc = EINVAL;
199 goto error;
200 }
201 last_bsize = bsize;
202 }
203
204 bd_srvs_init(&new_volume->hr_bds);
205 new_volume->hr_bds.ops = &hr_raid1_bd_ops;
206 new_volume->hr_bds.sarg = new_volume;
207 new_volume->nblocks = total_blocks / new_volume->dev_no;
208 new_volume->bsize = bsize;
209
210 rc = hr_register_volume(new_volume);
211 if (rc != EOK)
212 goto error;
213
214 return EOK;
215error:
216 hr_fini_devs(new_volume);
217 return rc;
218}
219
220/** @}
221 */
Note: See TracBrowser for help on using the repository browser.