source: mainline/uspace/srv/bd/hr/superblock.c@ abc2c4b

Last change on this file since abc2c4b was 68c966e, checked in by Miroslav Cimerman <mc@…>, 10 months ago

hr: use HR_DEVNAME_LEN and HR_UUID_LEN defines

  • Property mode set to 100644
File size: 5.2 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 <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
52static errno_t read_metadata(service_id_t, hr_metadata_t *);
53
54errno_t hr_write_meta_to_vol(hr_volume_t *vol)
55{
56 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_write_meta_to_vol()");
57
58 errno_t rc;
59 size_t i;
60 size_t meta_blkno; /* blocks needed to write metadata */
61 hr_metadata_t *metadata;
62 uuid_t uuid;
63
64 metadata = calloc(1, HR_META_SIZE * vol->bsize);
65 if (metadata == NULL)
66 return ENOMEM;
67
68 meta_blkno = (HR_META_OFF + HR_META_SIZE);
69 if (vol->level != hr_l_1)
70 meta_blkno *= vol->dev_no;
71
72 if (vol->nblocks < meta_blkno) {
73 log_msg(LOG_DEFAULT, LVL_ERROR,
74 "not enough blocks to write metadata");
75 rc = EINVAL;
76 goto error;
77 } else if (vol->nblocks == meta_blkno) {
78 log_msg(LOG_DEFAULT, LVL_ERROR,
79 "there would be zero data blocks after writing metadata, aborting");
80 rc = EINVAL;
81 goto error;
82 }
83
84 metadata->magic = host2uint64_t_le(HR_MAGIC);
85 metadata->extent_no = host2uint32_t_le(vol->dev_no);
86 metadata->level = host2uint32_t_le(vol->level);
87 metadata->nblocks = host2uint64_t_le(vol->nblocks);
88 metadata->data_blkno = host2uint64_t_le(vol->data_blkno);
89 metadata->data_offset = host2uint32_t_le(vol->data_offset);
90 metadata->strip_size = host2uint32_t_le(vol->strip_size);
91 for (i = 0; i < vol->dev_no; i++) {
92 metadata->index = host2uint32_t_le(i);
93
94 rc = uuid_generate(&uuid);
95 if (rc != EOK)
96 goto error;
97 uuid_encode(&uuid, metadata->uuid);
98
99 str_cpy(metadata->devname, HR_DEVNAME_LEN, vol->devname);
100
101 rc = block_write_direct(vol->extents[i].svc_id, HR_META_OFF,
102 HR_META_SIZE, metadata);
103 if (rc != EOK)
104 goto error;
105
106 /* rndgen */
107 fibril_usleep(1000);
108 }
109error:
110 free(metadata);
111 return rc;
112}
113
114errno_t hr_get_vol_from_meta(hr_config_t *cfg, hr_volume_t *new_volume)
115{
116 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_get_vol_from_meta()");
117
118 errno_t rc;
119 hr_metadata_t *metadata;
120
121 metadata = calloc(1, HR_META_SIZE * new_volume->bsize);
122 if (metadata == NULL)
123 return ENOMEM;
124
125 /* for now assume metadata are in sync across extents */
126 rc = read_metadata(cfg->devs[0], metadata);
127 if (rc != EOK)
128 goto end;
129
130 /* TODO: sort new_volume->extents according to metadata extent index */
131
132 if (uint64_t_le2host(metadata->magic) != HR_MAGIC) {
133 printf("invalid magic\n");
134 rc = EINVAL;
135 goto end;
136 }
137
138 new_volume->level = uint32_t_le2host(metadata->level);
139 new_volume->dev_no = uint32_t_le2host(metadata->extent_no);
140 new_volume->nblocks = uint64_t_le2host(metadata->nblocks);
141 new_volume->data_blkno = uint64_t_le2host(metadata->data_blkno);
142 new_volume->data_offset = uint32_t_le2host(metadata->data_offset);
143 new_volume->strip_size = uint32_t_le2host(metadata->strip_size);
144
145 if (new_volume->dev_no != cfg->dev_no) {
146 log_msg(LOG_DEFAULT, LVL_ERROR,
147 "number of divices in array differ: specified %zu, metadata states %zu",
148 cfg->dev_no, new_volume->dev_no);
149 rc = EINVAL;
150 goto end;
151 }
152
153 if (str_cmp(metadata->devname, new_volume->devname) != 0) {
154 log_msg(LOG_DEFAULT, LVL_NOTE,
155 "devname on metadata (%s) and config (%s) differ, using config",
156 metadata->devname, new_volume->devname);
157 }
158end:
159 free(metadata);
160 return rc;
161}
162
163static errno_t read_metadata(service_id_t dev, hr_metadata_t *metadata)
164{
165 errno_t rc;
166 size_t bsize;
167 uint64_t nblocks;
168
169 rc = block_get_bsize(dev, &bsize);
170 if (rc != EOK)
171 return rc;
172
173 rc = block_get_nblocks(dev, &nblocks);
174 if (rc != EOK)
175 return rc;
176
177 if (nblocks < (HR_META_SIZE + HR_META_OFF) * bsize)
178 return EINVAL;
179
180 rc = block_read_direct(dev, HR_META_OFF, HR_META_SIZE, metadata);
181 if (rc != EOK)
182 return rc;
183
184 return EOK;
185}
186
187/** @}
188 */
Note: See TracBrowser for help on using the repository browser.