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

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

hr: metadata format agnostic superblock ops

Put metadata specific code behind a new hr_superblock_ops_t
interface, that allows to easily add support for new metadata
formats.

  • Property mode set to 100644
File size: 3.0 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 "metadata/native.h"
49#include "superblock.h"
50#include "util.h"
51#include "var.h"
52
53extern hr_superblock_ops_t metadata_native_ops;
54
55static hr_superblock_ops_t *hr_superblock_ops_all[] = {
56 [HR_METADATA_NATIVE] = &metadata_native_ops
57};
58
59hr_superblock_ops_t *get_type_ops(metadata_type_t type)
60{
61 assert(type >= HR_METADATA_NATIVE && type < HR_METADATA_LAST_DUMMY);
62
63 return hr_superblock_ops_all[type];
64}
65
66errno_t find_metadata(service_id_t svc_id, void **rmetadata,
67 metadata_type_t *rtype)
68{
69 HR_DEBUG("%s()", __func__);
70
71 errno_t rc;
72 hr_superblock_ops_t *meta_ops;
73 void *meta_block;
74 void *metadata_struct;
75
76 if (rmetadata == NULL)
77 return EINVAL;
78 if (rtype == NULL)
79 return EINVAL;
80
81 volatile metadata_type_t type = HR_METADATA_NATIVE;
82 for (; type < HR_METADATA_LAST_DUMMY; type++) {
83 meta_ops = hr_superblock_ops_all[type];
84
85 metadata_struct = meta_ops->alloc_struct();
86 if (metadata_struct == NULL)
87 return ENOMEM;
88
89 rc = meta_ops->get_block(svc_id, &meta_block);
90 if (rc == ENOMEM)
91 return ENOMEM;
92 if (rc != EOK)
93 continue;
94
95 meta_ops->decode(meta_block, metadata_struct);
96
97 free(meta_block);
98
99 if (!meta_ops->has_valid_magic(metadata_struct)) {
100 free(metadata_struct);
101 continue;
102 }
103
104 *rmetadata = metadata_struct;
105 *rtype = type;
106 return EOK;
107 }
108
109 return ENOFS;
110}
111
112/** @}
113 */
Note: See TracBrowser for help on using the repository browser.