/* * Copyright (c) 2025 Miroslav Cimerman * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @addtogroup hr * @{ */ /** * @file */ #include #include #include #include #include #include #include #include #include #include #include #include "superblock.h" #include "util.h" #include "var.h" #include "metadata/foreign/geom/g_mirror.h" #include "metadata/native.h" extern hr_superblock_ops_t metadata_native_ops; extern hr_superblock_ops_t metadata_gmirror_ops; static hr_superblock_ops_t *hr_superblock_ops_all[] = { [HR_METADATA_NATIVE] = &metadata_native_ops, [HR_METADATA_GEOM_MIRROR] = &metadata_gmirror_ops }; hr_superblock_ops_t *get_type_ops(metadata_type_t type) { assert(type >= HR_METADATA_NATIVE && type < HR_METADATA_LAST_DUMMY); return hr_superblock_ops_all[type]; } errno_t find_metadata(service_id_t svc_id, void **rmetadata, metadata_type_t *rtype) { HR_DEBUG("%s()", __func__); errno_t rc; hr_superblock_ops_t *meta_ops; void *meta_block; void *metadata_struct; if (rmetadata == NULL) return EINVAL; if (rtype == NULL) return EINVAL; volatile metadata_type_t type = HR_METADATA_NATIVE; for (; type < HR_METADATA_LAST_DUMMY; type++) { meta_ops = hr_superblock_ops_all[type]; metadata_struct = meta_ops->alloc_struct(); if (metadata_struct == NULL) return ENOMEM; rc = meta_ops->get_block(svc_id, &meta_block); if (rc == ENOMEM) { free(metadata_struct); return ENOMEM; } else if (rc != EOK) { free(metadata_struct); continue; } rc = meta_ops->decode(meta_block, metadata_struct); free(meta_block); if (rc != EOK) { free(metadata_struct); continue; } if (!meta_ops->has_valid_magic(metadata_struct)) { free(metadata_struct); continue; } *rmetadata = metadata_struct; *rtype = type; return EOK; } return ENOFS; } /** @} */