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

Last change on this file since 7fba146 was 7fba146, checked in by Miroslav Cimerman <mc@…>, 2 months ago

hr: RAID 1: inc meta counter on first write

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * Copyright (c) 2025 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 <inttypes.h>
40#include <io/log.h>
41#include <loc.h>
42#include <mem.h>
43#include <uuid.h>
44#include <stdlib.h>
45#include <stdio.h>
46#include <str.h>
47
48#include "superblock.h"
49#include "util.h"
50#include "var.h"
51
52#include "metadata/native.h"
53
54#include "metadata/foreign/geom/g_mirror.h"
55#include "metadata/foreign/geom/g_stripe.h"
56#include "metadata/foreign/softraid/softraidvar.h"
57
58extern hr_superblock_ops_t metadata_native_ops;
59extern hr_superblock_ops_t metadata_gmirror_ops;
60extern hr_superblock_ops_t metadata_gstripe_ops;
61extern hr_superblock_ops_t metadata_softraid_ops;
62
63static hr_superblock_ops_t *hr_superblock_ops_all[] = {
64 [HR_METADATA_NATIVE] = &metadata_native_ops,
65 [HR_METADATA_GEOM_MIRROR] = &metadata_gmirror_ops,
66 [HR_METADATA_GEOM_STRIPE] = &metadata_gstripe_ops,
67 [HR_METADATA_SOFTRAID] = &metadata_softraid_ops
68};
69
70hr_superblock_ops_t *get_type_ops(hr_metadata_type_t type)
71{
72 assert(type >= HR_METADATA_NATIVE && type < HR_METADATA_LAST_DUMMY);
73
74 return hr_superblock_ops_all[type];
75}
76
77errno_t find_metadata(service_id_t svc_id, void **rmetadata,
78 hr_metadata_type_t *rtype)
79{
80 HR_DEBUG("%s()", __func__);
81
82 errno_t rc;
83 hr_superblock_ops_t *meta_ops;
84 void *meta_block;
85 void *metadata_struct;
86
87 if (rmetadata == NULL)
88 return EINVAL;
89 if (rtype == NULL)
90 return EINVAL;
91
92 volatile hr_metadata_type_t type = HR_METADATA_NATIVE;
93 for (; type < HR_METADATA_LAST_DUMMY; type++) {
94 meta_ops = hr_superblock_ops_all[type];
95
96 metadata_struct = meta_ops->alloc_struct();
97 if (metadata_struct == NULL)
98 return ENOMEM;
99
100 rc = meta_ops->get_block(svc_id, &meta_block);
101 if (rc == ENOMEM) {
102 free(metadata_struct);
103 return ENOMEM;
104 } else if (rc != EOK) {
105 free(metadata_struct);
106 continue;
107 }
108
109 rc = meta_ops->decode(meta_block, metadata_struct);
110
111 free(meta_block);
112
113 if (rc != EOK) {
114 free(metadata_struct);
115 continue;
116 }
117
118 if (!meta_ops->has_valid_magic(metadata_struct)) {
119 free(metadata_struct);
120 continue;
121 }
122
123 *rmetadata = metadata_struct;
124 *rtype = type;
125 return EOK;
126 }
127
128 return ENOFS;
129}
130
131/** @}
132 */
Note: See TracBrowser for help on using the repository browser.