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 <block.h>
|
---|
37 | #include <byteorder.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <loc.h>
|
---|
41 | #include <mem.h>
|
---|
42 | #include <uuid.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <types/uuid.h>
|
---|
47 |
|
---|
48 | #include "superblock.h"
|
---|
49 | #include "util.h"
|
---|
50 | #include "var.h"
|
---|
51 |
|
---|
52 | static errno_t read_metadata(service_id_t, hr_metadata_t *);
|
---|
53 | static errno_t hr_fill_meta_from_vol(hr_volume_t *, hr_metadata_t *);
|
---|
54 | static errno_t validate_meta(hr_metadata_t *);
|
---|
55 |
|
---|
56 | errno_t hr_write_meta_to_vol(hr_volume_t *vol)
|
---|
57 | {
|
---|
58 | HR_DEBUG("hr_write_meta_to_vol()\n");
|
---|
59 |
|
---|
60 | errno_t rc;
|
---|
61 | size_t i;
|
---|
62 | hr_metadata_t *metadata;
|
---|
63 | uuid_t uuid;
|
---|
64 |
|
---|
65 | metadata = calloc(1, HR_META_SIZE * vol->bsize);
|
---|
66 | if (metadata == NULL)
|
---|
67 | return ENOMEM;
|
---|
68 |
|
---|
69 | rc = hr_fill_meta_from_vol(vol, metadata);
|
---|
70 | if (rc != EOK)
|
---|
71 | goto error;
|
---|
72 |
|
---|
73 | for (i = 0; i < vol->extent_no; i++) {
|
---|
74 | metadata->index = host2uint32_t_le(i);
|
---|
75 |
|
---|
76 | rc = uuid_generate(&uuid);
|
---|
77 | if (rc != EOK)
|
---|
78 | goto error;
|
---|
79 | uuid_encode(&uuid, metadata->uuid);
|
---|
80 |
|
---|
81 | rc = block_write_direct(vol->extents[i].svc_id, HR_META_OFF,
|
---|
82 | HR_META_SIZE, metadata);
|
---|
83 | if (rc != EOK)
|
---|
84 | goto error;
|
---|
85 |
|
---|
86 | /* rndgen */
|
---|
87 | fibril_usleep(1000);
|
---|
88 | }
|
---|
89 | error:
|
---|
90 | free(metadata);
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 | errno_t hr_write_meta_to_ext(hr_volume_t *vol, size_t ext)
|
---|
95 | {
|
---|
96 | HR_DEBUG("hr_write_meta_to_vol()\n");
|
---|
97 |
|
---|
98 | errno_t rc;
|
---|
99 | hr_metadata_t *metadata;
|
---|
100 | uuid_t uuid;
|
---|
101 |
|
---|
102 | metadata = calloc(1, HR_META_SIZE * vol->bsize);
|
---|
103 | if (metadata == NULL)
|
---|
104 | return ENOMEM;
|
---|
105 |
|
---|
106 | rc = hr_fill_meta_from_vol(vol, metadata);
|
---|
107 | if (rc != EOK)
|
---|
108 | goto error;
|
---|
109 |
|
---|
110 | metadata->index = host2uint32_t_le(ext);
|
---|
111 |
|
---|
112 | rc = uuid_generate(&uuid);
|
---|
113 | if (rc != EOK)
|
---|
114 | goto error;
|
---|
115 | uuid_encode(&uuid, metadata->uuid);
|
---|
116 |
|
---|
117 | rc = block_write_direct(vol->extents[ext].svc_id, HR_META_OFF,
|
---|
118 | HR_META_SIZE, metadata);
|
---|
119 | if (rc != EOK)
|
---|
120 | goto error;
|
---|
121 |
|
---|
122 | error:
|
---|
123 | free(metadata);
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Fill metadata members from
|
---|
129 | * specified volume.
|
---|
130 | *
|
---|
131 | * Does not fill extent index and UUID.
|
---|
132 | */
|
---|
133 | static errno_t hr_fill_meta_from_vol(hr_volume_t *vol, hr_metadata_t *metadata)
|
---|
134 | {
|
---|
135 | HR_DEBUG("hr_fill_meta_from_vol()\n");
|
---|
136 |
|
---|
137 | size_t meta_blkno = HR_META_OFF + HR_META_SIZE;
|
---|
138 |
|
---|
139 | if (vol->level != HR_LVL_1)
|
---|
140 | meta_blkno *= vol->extent_no;
|
---|
141 |
|
---|
142 | if (vol->nblocks < meta_blkno) {
|
---|
143 | HR_ERROR("hr_fill_meta_from_vol(): volume \"%s\" does not "
|
---|
144 | " have enough space to store metada, aborting\n",
|
---|
145 | vol->devname);
|
---|
146 | return EINVAL;
|
---|
147 | } else if (vol->nblocks == meta_blkno) {
|
---|
148 | HR_ERROR("hr_fill_meta_from_vol(): volume \"%s\" would have "
|
---|
149 | "zero data blocks after writing metadata, aborting\n",
|
---|
150 | vol->devname);
|
---|
151 | return EINVAL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | metadata->magic = host2uint64_t_le(HR_MAGIC);
|
---|
155 | metadata->version = host2uint32_t_le(~(0U)); /* unused */
|
---|
156 | metadata->extent_no = host2uint32_t_le(vol->extent_no);
|
---|
157 | /* index filled separately for each extent */
|
---|
158 | metadata->level = host2uint32_t_le(vol->level);
|
---|
159 | metadata->layout = host2uint32_t_le(vol->layout);
|
---|
160 | metadata->strip_size = host2uint32_t_le(vol->strip_size);
|
---|
161 | metadata->nblocks = host2uint64_t_le(vol->nblocks);
|
---|
162 | metadata->data_blkno = host2uint64_t_le(vol->data_blkno);
|
---|
163 | metadata->data_offset = host2uint64_t_le(vol->data_offset);
|
---|
164 | metadata->counter = host2uint64_t_le(~(0UL)); /* unused */
|
---|
165 | /* UUID generated separately for each extent */
|
---|
166 | str_cpy(metadata->devname, HR_DEVNAME_LEN, vol->devname);
|
---|
167 |
|
---|
168 | return EOK;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static errno_t validate_meta(hr_metadata_t *md)
|
---|
172 | {
|
---|
173 | if (uint64_t_le2host(md->magic) != HR_MAGIC) {
|
---|
174 | HR_ERROR("invalid magic\n");
|
---|
175 | return EINVAL;
|
---|
176 | }
|
---|
177 | return EOK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | errno_t hr_fill_vol_from_meta(hr_volume_t *vol)
|
---|
181 | {
|
---|
182 | HR_DEBUG("hr_get_vol_from_meta()\n");
|
---|
183 |
|
---|
184 | errno_t rc;
|
---|
185 | hr_metadata_t *metadata;
|
---|
186 |
|
---|
187 | metadata = calloc(1, HR_META_SIZE * vol->bsize);
|
---|
188 | if (metadata == NULL)
|
---|
189 | return ENOMEM;
|
---|
190 |
|
---|
191 | service_id_t assembly_svc_id_order[HR_MAX_EXTENTS] = { 0 };
|
---|
192 | for (size_t i = 0; i < vol->extent_no; i++)
|
---|
193 | assembly_svc_id_order[i] = vol->extents[i].svc_id;
|
---|
194 |
|
---|
195 | size_t md_order_indices[HR_MAX_EXTENTS] = { 0 };
|
---|
196 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
197 | if (assembly_svc_id_order[i] == 0) {
|
---|
198 | /* set invalid index */
|
---|
199 | md_order_indices[i] = vol->extent_no;
|
---|
200 | continue;
|
---|
201 | }
|
---|
202 | rc = read_metadata(assembly_svc_id_order[i], metadata);
|
---|
203 | if (rc != EOK)
|
---|
204 | goto end;
|
---|
205 | rc = validate_meta(metadata);
|
---|
206 | if (rc != EOK)
|
---|
207 | goto end;
|
---|
208 | md_order_indices[i] = uint32_t_le2host(metadata->index);
|
---|
209 | }
|
---|
210 |
|
---|
211 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
212 | vol->extents[i].svc_id = 0;
|
---|
213 | vol->extents[i].status = HR_EXT_MISSING;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* sort */
|
---|
217 | for (size_t i = 0; i < vol->extent_no; i++) {
|
---|
218 | for (size_t j = 0; j < vol->extent_no; j++) {
|
---|
219 | if (i == md_order_indices[j]) {
|
---|
220 | vol->extents[i].svc_id =
|
---|
221 | assembly_svc_id_order[j];
|
---|
222 | vol->extents[i].status = HR_EXT_ONLINE;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * still assume metadata are in sync across extents
|
---|
229 | */
|
---|
230 |
|
---|
231 | if (vol->extent_no != uint32_t_le2host(metadata->extent_no)) {
|
---|
232 | HR_ERROR("number of divices in array differ: specified %zu, "
|
---|
233 | "metadata states %u",
|
---|
234 | vol->extent_no, uint32_t_le2host(metadata->extent_no));
|
---|
235 | rc = EINVAL;
|
---|
236 | goto end;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* TODO: handle version */
|
---|
240 | vol->level = uint32_t_le2host(metadata->level);
|
---|
241 | vol->layout = (uint8_t)uint32_t_le2host(metadata->layout);
|
---|
242 | vol->strip_size = uint32_t_le2host(metadata->strip_size);
|
---|
243 | vol->nblocks = uint64_t_le2host(metadata->nblocks);
|
---|
244 | vol->data_blkno = uint64_t_le2host(metadata->data_blkno);
|
---|
245 | vol->data_offset = uint64_t_le2host(metadata->data_offset);
|
---|
246 | vol->counter = uint64_t_le2host(0x00); /* unused */
|
---|
247 |
|
---|
248 | if (str_cmp(metadata->devname, vol->devname) != 0) {
|
---|
249 | HR_WARN("devname on metadata (%s) and config (%s) differ, "
|
---|
250 | "using config", metadata->devname, vol->devname);
|
---|
251 | }
|
---|
252 | end:
|
---|
253 | free(metadata);
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 |
|
---|
257 | static errno_t read_metadata(service_id_t dev, hr_metadata_t *metadata)
|
---|
258 | {
|
---|
259 | errno_t rc;
|
---|
260 | uint64_t nblocks;
|
---|
261 |
|
---|
262 | rc = block_get_nblocks(dev, &nblocks);
|
---|
263 | if (rc != EOK)
|
---|
264 | return rc;
|
---|
265 |
|
---|
266 | if (nblocks < HR_META_SIZE + HR_META_OFF)
|
---|
267 | return EINVAL;
|
---|
268 |
|
---|
269 | rc = block_read_direct(dev, HR_META_OFF, HR_META_SIZE, metadata);
|
---|
270 | if (rc != EOK)
|
---|
271 | return rc;
|
---|
272 |
|
---|
273 | return EOK;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /** @}
|
---|
277 | */
|
---|