Changeset aa9bad8 in mainline


Ignore:
Timestamp:
2025-06-27T22:21:05Z (4 weeks ago)
Author:
Miroslav Cimerman <mc@…>
Children:
2de7c1f
Parents:
78433bb
git-author:
Miroslav Cimerman <mc@…> (2025-06-27 22:14:37)
git-committer:
Miroslav Cimerman <mc@…> (2025-06-27 22:21:05)
Message:

hr: let each format implement own probe

This will allow metadata to have superblocks in
different locations accross versions.

This also greatly reduces the format handling interface,
because now, functions used by the probing done
publicly in superblock.c are now used just
inside specific metadata format code.

Location:
uspace/srv/bd/hr
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/hr/metadata/foreign/geom/hr_g_mirror.c

    r78433bb raa9bad8  
    5454#include "g_mirror.h"
    5555
     56/* not exposed */
    5657static void *meta_gmirror_alloc_struct(void);
     58/* static void meta_gmirror_encode(void *, void *); */
     59static errno_t meta_gmirror_decode(const void *, void *);
     60static errno_t meta_gmirror_get_block(service_id_t, void **);
     61/* static errno_t meta_gmirror_write_block(service_id_t, const void *); */
     62static bool meta_gmirror_has_valid_magic(const void *);
     63
     64static errno_t meta_gmirror_probe(service_id_t, void **);
    5765static errno_t meta_gmirror_init_vol2meta(hr_volume_t *);
    5866static errno_t meta_gmirror_init_meta2vol(const list_t *, hr_volume_t *);
    59 static void meta_gmirror_encode(void *, void *);
    60 static errno_t meta_gmirror_decode(const void *, void *);
    61 static errno_t meta_gmirror_get_block(service_id_t, void **);
    62 static errno_t meta_gmirror_write_block(service_id_t, const void *);
    63 static bool meta_gmirror_has_valid_magic(const void *);
    6467static bool meta_gmirror_compare_uuids(const void *, const void *);
    6568static void meta_gmirror_inc_counter(hr_volume_t *);
     
    7578
    7679hr_superblock_ops_t metadata_gmirror_ops = {
    77         .alloc_struct = meta_gmirror_alloc_struct,
     80        .probe = meta_gmirror_probe,
    7881        .init_vol2meta = meta_gmirror_init_vol2meta,
    7982        .init_meta2vol = meta_gmirror_init_meta2vol,
    80         .encode = meta_gmirror_encode,
    81         .decode = meta_gmirror_decode,
    82         .get_block = meta_gmirror_get_block,
    83         .write_block = meta_gmirror_write_block,
    84         .has_valid_magic = meta_gmirror_has_valid_magic,
    8583        .compare_uuids = meta_gmirror_compare_uuids,
    8684        .inc_counter = meta_gmirror_inc_counter,
     
    9694};
    9795
    98 static void *meta_gmirror_alloc_struct(void)
    99 {
    100         return calloc(1, sizeof(struct g_mirror_metadata));
     96static errno_t meta_gmirror_probe(service_id_t svc_id, void **rmd)
     97{
     98        errno_t rc;
     99        void *meta_block;
     100
     101        void *metadata_struct = meta_gmirror_alloc_struct();
     102        if (metadata_struct == NULL)
     103                return ENOMEM;
     104
     105        rc = meta_gmirror_get_block(svc_id, &meta_block);
     106        if (rc != EOK)
     107                goto error;
     108
     109        rc = meta_gmirror_decode(meta_block, metadata_struct);
     110
     111        free(meta_block);
     112
     113        if (rc != EOK)
     114                goto error;
     115
     116        if (!meta_gmirror_has_valid_magic(metadata_struct)) {
     117                rc = ENOFS;
     118                goto error;
     119        }
     120
     121        *rmd = metadata_struct;
     122        return EOK;
     123
     124error:
     125        free(metadata_struct);
     126        return ENOFS;
    101127}
    102128
     
    177203}
    178204
     205static bool meta_gmirror_compare_uuids(const void *m1_v, const void *m2_v)
     206{
     207        const struct g_mirror_metadata *m1 = m1_v;
     208        const struct g_mirror_metadata *m2 = m2_v;
     209        if (m1->md_mid == m2->md_mid)
     210                return true;
     211
     212        return false;
     213}
     214
     215static void meta_gmirror_inc_counter(hr_volume_t *vol)
     216{
     217        fibril_mutex_lock(&vol->md_lock);
     218
     219        struct g_mirror_metadata *md = vol->in_mem_md;
     220
     221        /* XXX: probably md_genid and not md_syncid is incremented */
     222        md->md_genid++;
     223
     224        fibril_mutex_unlock(&vol->md_lock);
     225}
     226
     227static errno_t meta_gmirror_save(hr_volume_t *vol, bool with_state_callback)
     228{
     229        HR_DEBUG("%s()", __func__);
     230
     231        (void)vol;
     232        (void)with_state_callback;
     233
     234        /*
     235         * cannot support right now, because would need to store the
     236         * metadata for all disks, because of hardcoded provider names and
     237         * more importantly, disk unique ids
     238         */
     239
     240        return ENOTSUP;
     241}
     242
     243static errno_t meta_gmirror_save_ext(hr_volume_t *vol, size_t ext_idx,
     244    bool with_state_callback)
     245{
     246        HR_DEBUG("%s()", __func__);
     247
     248        return ENOTSUP;
     249}
     250
     251static const char *meta_gmirror_get_devname(const void *md_v)
     252{
     253        const struct g_mirror_metadata *md = md_v;
     254
     255        return md->md_name;
     256}
     257
     258static hr_level_t meta_gmirror_get_level(const void *md_v)
     259{
     260        (void)md_v;
     261
     262        return HR_LVL_1;
     263}
     264
     265static uint64_t meta_gmirror_get_data_offset(void)
     266{
     267        return 0;
     268}
     269
     270static size_t meta_gmirror_get_size(void)
     271{
     272        return 1;
     273}
     274
     275static uint8_t meta_gmirror_get_flags(void)
     276{
     277        uint8_t flags = 0;
     278
     279        return flags;
     280}
     281
     282static hr_metadata_type_t meta_gmirror_get_type(void)
     283{
     284        return  HR_METADATA_GEOM_MIRROR;
     285}
     286
     287static void meta_gmirror_dump(const void *md_v)
     288{
     289        HR_DEBUG("%s()", __func__);
     290
     291        mirror_metadata_dump(md_v);
     292}
     293
     294static void *meta_gmirror_alloc_struct(void)
     295{
     296        return calloc(1, sizeof(struct g_mirror_metadata));
     297}
     298
     299#if 0
    179300static void meta_gmirror_encode(void *md_v, void *block)
    180301{
     
    183304        mirror_metadata_encode(md_v, block);
    184305}
     306#endif
    185307
    186308static errno_t meta_gmirror_decode(const void *block, void *md_v)
     
    237359}
    238360
     361#if 0
    239362static errno_t meta_gmirror_write_block(service_id_t dev, const void *block)
    240363{
     
    263386        return rc;
    264387}
     388#endif
    265389
    266390static bool meta_gmirror_has_valid_magic(const void *md_v)
     
    276400}
    277401
    278 static bool meta_gmirror_compare_uuids(const void *m1_v, const void *m2_v)
    279 {
    280         const struct g_mirror_metadata *m1 = m1_v;
    281         const struct g_mirror_metadata *m2 = m2_v;
    282         if (m1->md_mid == m2->md_mid)
    283                 return true;
    284 
    285         return false;
    286 }
    287 
    288 static void meta_gmirror_inc_counter(hr_volume_t *vol)
    289 {
    290         fibril_mutex_lock(&vol->md_lock);
    291 
    292         struct g_mirror_metadata *md = vol->in_mem_md;
    293 
    294         /* XXX: probably md_genid and not md_syncid is incremented */
    295         md->md_genid++;
    296 
    297         fibril_mutex_unlock(&vol->md_lock);
    298 }
    299 
    300 static errno_t meta_gmirror_save(hr_volume_t *vol, bool with_state_callback)
    301 {
    302         HR_DEBUG("%s()", __func__);
    303 
    304         (void)vol;
    305         (void)with_state_callback;
    306 
    307         /*
    308          * cannot support right now, because would need to store the
    309          * metadata for all disks, because of hardcoded provider names and
    310          * more importantly, disk unique ids
    311          */
    312 
    313         return ENOTSUP;
    314 }
    315 
    316 static errno_t meta_gmirror_save_ext(hr_volume_t *vol, size_t ext_idx,
    317     bool with_state_callback)
    318 {
    319         HR_DEBUG("%s()", __func__);
    320 
    321         return ENOTSUP;
    322 }
    323 
    324 static const char *meta_gmirror_get_devname(const void *md_v)
    325 {
    326         const struct g_mirror_metadata *md = md_v;
    327 
    328         return md->md_name;
    329 }
    330 
    331 static hr_level_t meta_gmirror_get_level(const void *md_v)
    332 {
    333         (void)md_v;
    334 
    335         return HR_LVL_1;
    336 }
    337 
    338 static uint64_t meta_gmirror_get_data_offset(void)
    339 {
    340         return 0;
    341 }
    342 
    343 static size_t meta_gmirror_get_size(void)
    344 {
    345         return 1;
    346 }
    347 
    348 static uint8_t meta_gmirror_get_flags(void)
    349 {
    350         uint8_t flags = 0;
    351 
    352         return flags;
    353 }
    354 
    355 static hr_metadata_type_t meta_gmirror_get_type(void)
    356 {
    357         return  HR_METADATA_GEOM_MIRROR;
    358 }
    359 
    360 static void meta_gmirror_dump(const void *md_v)
    361 {
    362         HR_DEBUG("%s()", __func__);
    363 
    364         mirror_metadata_dump(md_v);
    365 }
    366 
    367402/** @}
    368403 */
  • uspace/srv/bd/hr/metadata/foreign/geom/hr_g_stripe.c

    r78433bb raa9bad8  
    5454#include "g_stripe.h"
    5555
     56/* not exposed */
    5657static void *meta_gstripe_alloc_struct(void);
     58/* static void meta_gstripe_encode(void *, void *); */
     59static errno_t meta_gstripe_decode(const void *, void *);
     60static errno_t meta_gstripe_get_block(service_id_t, void **);
     61/* static errno_t meta_gstripe_write_block(service_id_t, const void *); */
     62static bool meta_gstripe_has_valid_magic(const void *);
     63
     64static errno_t meta_gstripe_probe(service_id_t, void **);
    5765static errno_t meta_gstripe_init_vol2meta(hr_volume_t *);
    5866static errno_t meta_gstripe_init_meta2vol(const list_t *, hr_volume_t *);
    59 static void meta_gstripe_encode(void *, void *);
    60 static errno_t meta_gstripe_decode(const void *, void *);
    61 static errno_t meta_gstripe_get_block(service_id_t, void **);
    62 static errno_t meta_gstripe_write_block(service_id_t, const void *);
    63 static bool meta_gstripe_has_valid_magic(const void *);
    6467static bool meta_gstripe_compare_uuids(const void *, const void *);
    6568static void meta_gstripe_inc_counter(hr_volume_t *);
     
    7578
    7679hr_superblock_ops_t metadata_gstripe_ops = {
    77         .alloc_struct = meta_gstripe_alloc_struct,
     80        .probe = meta_gstripe_probe,
    7881        .init_vol2meta = meta_gstripe_init_vol2meta,
    7982        .init_meta2vol = meta_gstripe_init_meta2vol,
    80         .encode = meta_gstripe_encode,
    81         .decode = meta_gstripe_decode,
    82         .get_block = meta_gstripe_get_block,
    83         .write_block = meta_gstripe_write_block,
    84         .has_valid_magic = meta_gstripe_has_valid_magic,
    8583        .compare_uuids = meta_gstripe_compare_uuids,
    8684        .inc_counter = meta_gstripe_inc_counter,
     
    9694};
    9795
    98 static void *meta_gstripe_alloc_struct(void)
    99 {
    100         return calloc(1, sizeof(struct g_stripe_metadata));
     96static errno_t meta_gstripe_probe(service_id_t svc_id, void **rmd)
     97{
     98        errno_t rc;
     99        void *meta_block;
     100
     101        void *metadata_struct = meta_gstripe_alloc_struct();
     102        if (metadata_struct == NULL)
     103                return ENOMEM;
     104
     105        rc = meta_gstripe_get_block(svc_id, &meta_block);
     106        if (rc != EOK)
     107                goto error;
     108
     109        rc = meta_gstripe_decode(meta_block, metadata_struct);
     110
     111        free(meta_block);
     112
     113        if (rc != EOK)
     114                goto error;
     115
     116        if (!meta_gstripe_has_valid_magic(metadata_struct)) {
     117                rc = ENOFS;
     118                goto error;
     119        }
     120
     121        *rmd = metadata_struct;
     122        return EOK;
     123
     124error:
     125        free(metadata_struct);
     126        return ENOFS;
    101127}
    102128
     
    178204}
    179205
     206static bool meta_gstripe_compare_uuids(const void *md1_v, const void *md2_v)
     207{
     208        const struct g_stripe_metadata *md1 = md1_v;
     209        const struct g_stripe_metadata *md2 = md2_v;
     210        if (md1->md_id == md2->md_id)
     211                return true;
     212
     213        return false;
     214}
     215
     216static void meta_gstripe_inc_counter(hr_volume_t *vol)
     217{
     218        (void)vol;
     219}
     220
     221static errno_t meta_gstripe_save(hr_volume_t *vol, bool with_state_callback)
     222{
     223        HR_DEBUG("%s()", __func__);
     224
     225        return ENOTSUP;
     226}
     227
     228static errno_t meta_gstripe_save_ext(hr_volume_t *vol, size_t ext_idx,
     229    bool with_state_callback)
     230{
     231        HR_DEBUG("%s()", __func__);
     232
     233        return ENOTSUP;
     234}
     235
     236static const char *meta_gstripe_get_devname(const void *md_v)
     237{
     238        const struct g_stripe_metadata *md = md_v;
     239
     240        return md->md_name;
     241}
     242
     243static hr_level_t meta_gstripe_get_level(const void *md_v)
     244{
     245        (void)md_v;
     246
     247        return HR_LVL_0;
     248}
     249
     250static uint64_t meta_gstripe_get_data_offset(void)
     251{
     252        return 0;
     253}
     254
     255static size_t meta_gstripe_get_size(void)
     256{
     257        return 1;
     258}
     259
     260static uint8_t meta_gstripe_get_flags(void)
     261{
     262        uint8_t flags = 0;
     263
     264        return flags;
     265}
     266
     267static hr_metadata_type_t meta_gstripe_get_type(void)
     268{
     269        return HR_METADATA_GEOM_STRIPE;
     270}
     271
     272static void meta_gstripe_dump(const void *md_v)
     273{
     274        HR_DEBUG("%s()", __func__);
     275
     276        const struct g_stripe_metadata *md = md_v;
     277
     278        printf("     magic: %s\n", md->md_magic);
     279        printf("   version: %u\n", (u_int)md->md_version);
     280        printf("      name: %s\n", md->md_name);
     281        printf("        id: %u\n", (u_int)md->md_id);
     282        printf("        no: %u\n", (u_int)md->md_no);
     283        printf("       all: %u\n", (u_int)md->md_all);
     284        printf("stripesize: %u\n", (u_int)md->md_stripesize);
     285        printf(" mediasize: %jd\n", (intmax_t)md->md_provsize);
     286}
     287
     288static void *meta_gstripe_alloc_struct(void)
     289{
     290        return calloc(1, sizeof(struct g_stripe_metadata));
     291}
     292
     293#if 0
    180294static void meta_gstripe_encode(void *md_v, void *block)
    181295{
     
    184298        stripe_metadata_encode(md_v, block);
    185299}
     300#endif
    186301
    187302static errno_t meta_gstripe_decode(const void *block, void *md_v)
     
    239354}
    240355
     356#if 0
    241357static errno_t meta_gstripe_write_block(service_id_t dev, const void *block)
    242358{
     
    265381        return rc;
    266382}
     383#endif
    267384
    268385static bool meta_gstripe_has_valid_magic(const void *md_v)
     
    278395}
    279396
    280 static bool meta_gstripe_compare_uuids(const void *md1_v, const void *md2_v)
    281 {
    282         const struct g_stripe_metadata *md1 = md1_v;
    283         const struct g_stripe_metadata *md2 = md2_v;
    284         if (md1->md_id == md2->md_id)
    285                 return true;
    286 
    287         return false;
    288 }
    289 
    290 static void meta_gstripe_inc_counter(hr_volume_t *vol)
    291 {
    292         (void)vol;
    293 }
    294 
    295 static errno_t meta_gstripe_save(hr_volume_t *vol, bool with_state_callback)
    296 {
    297         HR_DEBUG("%s()", __func__);
    298 
    299         return ENOTSUP;
    300 }
    301 
    302 static errno_t meta_gstripe_save_ext(hr_volume_t *vol, size_t ext_idx,
    303     bool with_state_callback)
    304 {
    305         HR_DEBUG("%s()", __func__);
    306 
    307         return ENOTSUP;
    308 }
    309 
    310 static const char *meta_gstripe_get_devname(const void *md_v)
    311 {
    312         const struct g_stripe_metadata *md = md_v;
    313 
    314         return md->md_name;
    315 }
    316 
    317 static hr_level_t meta_gstripe_get_level(const void *md_v)
    318 {
    319         (void)md_v;
    320 
    321         return HR_LVL_0;
    322 }
    323 
    324 static uint64_t meta_gstripe_get_data_offset(void)
    325 {
    326         return 0;
    327 }
    328 
    329 static size_t meta_gstripe_get_size(void)
    330 {
    331         return 1;
    332 }
    333 
    334 static uint8_t meta_gstripe_get_flags(void)
    335 {
    336         uint8_t flags = 0;
    337 
    338         return flags;
    339 }
    340 
    341 static hr_metadata_type_t meta_gstripe_get_type(void)
    342 {
    343         return  HR_METADATA_GEOM_STRIPE;
    344 }
    345 
    346 static void meta_gstripe_dump(const void *md_v)
    347 {
    348         HR_DEBUG("%s()", __func__);
    349 
    350         const struct g_stripe_metadata *md = md_v;
    351 
    352         printf("     magic: %s\n", md->md_magic);
    353         printf("   version: %u\n", (u_int)md->md_version);
    354         printf("      name: %s\n", md->md_name);
    355         printf("        id: %u\n", (u_int)md->md_id);
    356         printf("        no: %u\n", (u_int)md->md_no);
    357         printf("       all: %u\n", (u_int)md->md_all);
    358         printf("stripesize: %u\n", (u_int)md->md_stripesize);
    359         printf(" mediasize: %jd\n", (intmax_t)md->md_provsize);
    360 }
    361 
    362397/** @}
    363398 */
  • uspace/srv/bd/hr/metadata/foreign/softraid/hr_softraid.c

    r78433bb raa9bad8  
    5454#include "softraidvar.h"
    5555
     56/* not exposed */
    5657static void *meta_softraid_alloc_struct(void);
     58/* static void meta_softraid_encode(void *, void *); */
     59static errno_t meta_softraid_decode(const void *, void *);
     60static errno_t meta_softraid_get_block(service_id_t, void **);
     61/* static errno_t meta_softraid_write_block(service_id_t, const void *); */
     62static bool meta_softraid_has_valid_magic(const void *);
     63
     64static errno_t meta_softraid_probe(service_id_t, void **);
    5765static errno_t meta_softraid_init_vol2meta(hr_volume_t *);
    5866static errno_t meta_softraid_init_meta2vol(const list_t *, hr_volume_t *);
    59 static void meta_softraid_encode(void *, void *);
    60 static errno_t meta_softraid_decode(const void *, void *);
    61 static errno_t meta_softraid_get_block(service_id_t, void **);
    62 static errno_t meta_softraid_write_block(service_id_t, const void *);
    63 static bool meta_softraid_has_valid_magic(const void *);
    6467static bool meta_softraid_compare_uuids(const void *, const void *);
    6568static void meta_softraid_inc_counter(hr_volume_t *);
     
    7578
    7679hr_superblock_ops_t metadata_softraid_ops = {
    77         .alloc_struct = meta_softraid_alloc_struct,
     80        .probe = meta_softraid_probe,
    7881        .init_vol2meta = meta_softraid_init_vol2meta,
    7982        .init_meta2vol = meta_softraid_init_meta2vol,
    80         .encode = meta_softraid_encode,
    81         .decode = meta_softraid_decode,
    82         .get_block = meta_softraid_get_block,
    83         .write_block = meta_softraid_write_block,
    84         .has_valid_magic = meta_softraid_has_valid_magic,
    8583        .compare_uuids = meta_softraid_compare_uuids,
    8684        .inc_counter = meta_softraid_inc_counter,
     
    9694};
    9795
    98 static void *meta_softraid_alloc_struct(void)
    99 {
    100         return calloc(1, SR_META_SIZE * DEV_BSIZE);
     96static errno_t meta_softraid_probe(service_id_t svc_id, void **rmd)
     97{
     98        errno_t rc;
     99        void *meta_block;
     100
     101        void *metadata_struct = meta_softraid_alloc_struct();
     102        if (metadata_struct == NULL)
     103                return ENOMEM;
     104
     105        rc = meta_softraid_get_block(svc_id, &meta_block);
     106        if (rc != EOK)
     107                goto error;
     108
     109        rc = meta_softraid_decode(meta_block, metadata_struct);
     110
     111        free(meta_block);
     112
     113        if (rc != EOK)
     114                goto error;
     115
     116        if (!meta_softraid_has_valid_magic(metadata_struct)) {
     117                rc = ENOFS;
     118                goto error;
     119        }
     120
     121        *rmd = metadata_struct;
     122        return EOK;
     123
     124error:
     125        free(metadata_struct);
     126        return ENOFS;
    101127}
    102128
     
    181207}
    182208
     209static bool meta_softraid_compare_uuids(const void *m1_v, const void *m2_v)
     210{
     211        const struct sr_metadata *m1 = m1_v;
     212        const struct sr_metadata *m2 = m2_v;
     213        if (memcmp(&m1->ssdi.ssd_uuid, &m2->ssdi.ssd_uuid,
     214            SR_UUID_MAX) == 0)
     215                return true;
     216
     217        return false;
     218}
     219
     220static void meta_softraid_inc_counter(hr_volume_t *vol)
     221{
     222        fibril_mutex_lock(&vol->md_lock);
     223
     224        struct sr_metadata *md = vol->in_mem_md;
     225
     226        md->ssd_ondisk++;
     227
     228        fibril_mutex_unlock(&vol->md_lock);
     229}
     230
     231static errno_t meta_softraid_save(hr_volume_t *vol, bool with_state_callback)
     232{
     233        HR_DEBUG("%s()", __func__);
     234
     235        return ENOTSUP;
     236}
     237
     238static errno_t meta_softraid_save_ext(hr_volume_t *vol, size_t ext_idx,
     239    bool with_state_callback)
     240{
     241        HR_DEBUG("%s()", __func__);
     242
     243        return ENOTSUP;
     244}
     245
     246static const char *meta_softraid_get_devname(const void *md_v)
     247{
     248        const struct sr_metadata *md = md_v;
     249
     250        return md->ssd_devname;
     251}
     252
     253static hr_level_t meta_softraid_get_level(const void *md_v)
     254{
     255        const struct sr_metadata *md = md_v;
     256
     257        switch (md->ssdi.ssd_level) {
     258        case 0:
     259                return HR_LVL_0;
     260        case 1:
     261                return HR_LVL_1;
     262        case 5:
     263                return HR_LVL_5;
     264        default:
     265                return HR_LVL_UNKNOWN;
     266        }
     267}
     268
     269static uint64_t meta_softraid_get_data_offset(void)
     270{
     271        return SR_DATA_OFFSET;
     272}
     273
     274static size_t meta_softraid_get_size(void)
     275{
     276        return SR_META_SIZE;
     277}
     278
     279static uint8_t meta_softraid_get_flags(void)
     280{
     281        uint8_t flags = 0;
     282
     283        return flags;
     284}
     285
     286static hr_metadata_type_t meta_softraid_get_type(void)
     287{
     288        return HR_METADATA_SOFTRAID;
     289}
     290
     291static void meta_softraid_dump(const void *md_v)
     292{
     293        HR_DEBUG("%s()", __func__);
     294
     295        const struct sr_metadata *md = md_v;
     296
     297        sr_meta_print(md);
     298}
     299
     300static void *meta_softraid_alloc_struct(void)
     301{
     302        return calloc(1, SR_META_SIZE * DEV_BSIZE);
     303}
     304
     305#if 0
    183306static void meta_softraid_encode(void *md_v, void *block)
    184307{
     
    188311        (void)block;
    189312}
     313#endif
    190314
    191315static errno_t meta_softraid_decode(const void *block, void *md_v)
     
    372496}
    373497
     498#if 0
    374499static errno_t meta_softraid_write_block(service_id_t dev, const void *block)
    375500{
     
    398523        return rc;
    399524}
     525#endif
    400526
    401527static bool meta_softraid_has_valid_magic(const void *md_v)
     
    411537}
    412538
    413 static bool meta_softraid_compare_uuids(const void *m1_v, const void *m2_v)
    414 {
    415         const struct sr_metadata *m1 = m1_v;
    416         const struct sr_metadata *m2 = m2_v;
    417         if (memcmp(&m1->ssdi.ssd_uuid, &m2->ssdi.ssd_uuid,
    418             SR_UUID_MAX) == 0)
    419                 return true;
    420 
    421         return false;
    422 }
    423 
    424 static void meta_softraid_inc_counter(hr_volume_t *vol)
    425 {
    426         fibril_mutex_lock(&vol->md_lock);
    427 
    428         struct sr_metadata *md = vol->in_mem_md;
    429 
    430         md->ssd_ondisk++;
    431 
    432         fibril_mutex_unlock(&vol->md_lock);
    433 }
    434 
    435 static errno_t meta_softraid_save(hr_volume_t *vol, bool with_state_callback)
    436 {
    437         HR_DEBUG("%s()", __func__);
    438 
    439         return ENOTSUP;
    440 }
    441 
    442 static errno_t meta_softraid_save_ext(hr_volume_t *vol, size_t ext_idx,
    443     bool with_state_callback)
    444 {
    445         HR_DEBUG("%s()", __func__);
    446 
    447         return ENOTSUP;
    448 }
    449 
    450 static const char *meta_softraid_get_devname(const void *md_v)
    451 {
    452         const struct sr_metadata *md = md_v;
    453 
    454         return md->ssd_devname;
    455 }
    456 
    457 static hr_level_t meta_softraid_get_level(const void *md_v)
    458 {
    459         const struct sr_metadata *md = md_v;
    460 
    461         switch (md->ssdi.ssd_level) {
    462         case 0:
    463                 return HR_LVL_0;
    464         case 1:
    465                 return HR_LVL_1;
    466         case 5:
    467                 return HR_LVL_5;
    468         default:
    469                 return HR_LVL_UNKNOWN;
    470         }
    471 }
    472 
    473 static uint64_t meta_softraid_get_data_offset(void)
    474 {
    475         return SR_DATA_OFFSET;
    476 }
    477 
    478 static size_t meta_softraid_get_size(void)
    479 {
    480         return SR_META_SIZE;
    481 }
    482 
    483 static uint8_t meta_softraid_get_flags(void)
    484 {
    485         uint8_t flags = 0;
    486 
    487         return flags;
    488 }
    489 
    490 static hr_metadata_type_t meta_softraid_get_type(void)
    491 {
    492         return HR_METADATA_SOFTRAID;
    493 }
    494 
    495 static void meta_softraid_dump(const void *md_v)
    496 {
    497         HR_DEBUG("%s()", __func__);
    498 
    499         const struct sr_metadata *md = md_v;
    500 
    501         sr_meta_print(md);
    502 }
    503 
    504539/** @}
    505540 */
  • uspace/srv/bd/hr/metadata/native.c

    r78433bb raa9bad8  
    5454#include "native.h"
    5555
     56/* not exposed */
    5657static void *meta_native_alloc_struct(void);
    57 static errno_t meta_native_init_vol2meta(hr_volume_t *);
    58 static errno_t meta_native_init_meta2vol(const list_t *, hr_volume_t *);
    5958static void meta_native_encode(void *, void *);
    6059static errno_t meta_native_decode(const void *, void *);
    6160static errno_t meta_native_get_block(service_id_t, void **);
    6261static errno_t meta_native_write_block(service_id_t, const void *);
     62static bool meta_native_has_valid_magic(const void *);
     63
     64static errno_t meta_native_probe(service_id_t, void **);
     65static errno_t meta_native_init_vol2meta(hr_volume_t *);
     66static errno_t meta_native_init_meta2vol(const list_t *, hr_volume_t *);
    6367static errno_t meta_native_erase_block(service_id_t);
    64 static bool meta_native_has_valid_magic(const void *);
    6568static bool meta_native_compare_uuids(const void *, const void *);
    6669static void meta_native_inc_counter(hr_volume_t *);
     
    7679
    7780hr_superblock_ops_t metadata_native_ops = {
    78         .alloc_struct = meta_native_alloc_struct,
     81        .probe = meta_native_probe,
    7982        .init_vol2meta = meta_native_init_vol2meta,
    8083        .init_meta2vol = meta_native_init_meta2vol,
    81         .encode = meta_native_encode,
    82         .decode = meta_native_decode,
    83         .get_block = meta_native_get_block,
    84         .write_block = meta_native_write_block,
    8584        .erase_block = meta_native_erase_block,
    86         .has_valid_magic = meta_native_has_valid_magic,
    8785        .compare_uuids = meta_native_compare_uuids,
    8886        .inc_counter = meta_native_inc_counter,
     
    9896};
    9997
    100 static void *meta_native_alloc_struct(void)
    101 {
    102         return calloc(1, sizeof(hr_metadata_t));
     98static errno_t meta_native_probe(service_id_t svc_id, void **rmd)
     99{
     100        errno_t rc;
     101        void *meta_block;
     102
     103        void *metadata_struct = meta_native_alloc_struct();
     104        if (metadata_struct == NULL)
     105                return ENOMEM;
     106
     107        rc = meta_native_get_block(svc_id, &meta_block);
     108        if (rc != EOK)
     109                goto error;
     110
     111        rc = meta_native_decode(meta_block, metadata_struct);
     112
     113        free(meta_block);
     114
     115        if (rc != EOK)
     116                goto error;
     117
     118        if (!meta_native_has_valid_magic(metadata_struct)) {
     119                rc = ENOFS;
     120                goto error;
     121        }
     122
     123        *rmd = metadata_struct;
     124        return EOK;
     125
     126error:
     127        free(metadata_struct);
     128        return ENOFS;
    103129}
    104130
     
    200226
    201227        return EOK;
     228}
     229
     230static errno_t meta_native_erase_block(service_id_t dev)
     231{
     232        HR_DEBUG("%s()", __func__);
     233
     234        errno_t rc;
     235        size_t bsize;
     236
     237        rc = block_get_bsize(dev, &bsize);
     238        if (rc != EOK)
     239                return rc;
     240
     241        void *zero_block = calloc(1, bsize);
     242        if (zero_block == NULL)
     243                return ENOMEM;
     244
     245        rc = meta_native_write_block(dev, zero_block);
     246        return rc;
     247}
     248
     249static bool meta_native_compare_uuids(const void *m1p, const void *m2p)
     250{
     251        const hr_metadata_t *m1 = m1p;
     252        const hr_metadata_t *m2 = m2p;
     253        if (memcmp(m1->uuid, m2->uuid, HR_NATIVE_UUID_LEN) == 0)
     254                return true;
     255
     256        return false;
     257}
     258
     259static void meta_native_inc_counter(hr_volume_t *vol)
     260{
     261        fibril_mutex_lock(&vol->md_lock);
     262
     263        hr_metadata_t *md = vol->in_mem_md;
     264
     265        md->counter++;
     266
     267        fibril_mutex_unlock(&vol->md_lock);
     268}
     269
     270static errno_t meta_native_save(hr_volume_t *vol, bool with_state_callback)
     271{
     272        HR_DEBUG("%s()", __func__);
     273
     274        fibril_rwlock_read_lock(&vol->extents_lock);
     275
     276        for (size_t i = 0; i < vol->extent_no; i++)
     277                meta_native_save_ext(vol, i, with_state_callback);
     278
     279        fibril_rwlock_read_unlock(&vol->extents_lock);
     280
     281        return EOK;
     282}
     283
     284static errno_t meta_native_save_ext(hr_volume_t *vol, size_t ext_idx,
     285    bool with_state_callback)
     286{
     287        HR_DEBUG("%s()", __func__);
     288
     289        assert(fibril_rwlock_is_locked(&vol->extents_lock));
     290
     291        void *md_block = hr_calloc_waitok(1, vol->bsize);
     292
     293        hr_metadata_t *md = (hr_metadata_t *)vol->in_mem_md;
     294
     295        hr_extent_t *ext = &vol->extents[ext_idx];
     296
     297        fibril_rwlock_read_lock(&vol->states_lock);
     298        hr_ext_state_t s = ext->state;
     299        fibril_rwlock_read_unlock(&vol->states_lock);
     300
     301        if (s != HR_EXT_ONLINE && s != HR_EXT_REBUILD) {
     302                return EINVAL;
     303        }
     304
     305        fibril_mutex_lock(&vol->md_lock);
     306
     307        md->index = ext_idx;
     308        if (s == HR_EXT_REBUILD)
     309                md->rebuild_pos = vol->rebuild_blk;
     310        else
     311                md->rebuild_pos = 0;
     312        meta_native_encode(md, md_block);
     313        errno_t rc = meta_native_write_block(ext->svc_id, md_block);
     314        if (rc != EOK && with_state_callback)
     315                vol->hr_ops.ext_state_cb(vol, ext_idx, rc);
     316
     317        fibril_mutex_unlock(&vol->md_lock);
     318
     319        if (with_state_callback)
     320                vol->hr_ops.vol_state_eval(vol);
     321
     322        free(md_block);
     323        return EOK;
     324}
     325
     326static const char *meta_native_get_devname(const void *md_v)
     327{
     328        const hr_metadata_t *md = md_v;
     329
     330        return md->devname;
     331}
     332
     333static hr_level_t meta_native_get_level(const void *md_v)
     334{
     335        const hr_metadata_t *md = md_v;
     336
     337        return md->level;
     338}
     339
     340static uint64_t meta_native_get_data_offset(void)
     341{
     342        return HR_NATIVE_DATA_OFF;
     343}
     344
     345static size_t meta_native_get_size(void)
     346{
     347        return HR_NATIVE_META_SIZE;
     348}
     349
     350static uint8_t meta_native_get_flags(void)
     351{
     352        uint8_t flags = 0;
     353
     354        flags |= HR_METADATA_HOTSPARE_SUPPORT;
     355
     356        return flags;
     357}
     358
     359static hr_metadata_type_t meta_native_get_type(void)
     360{
     361        return HR_METADATA_NATIVE;
     362}
     363
     364static void meta_native_dump(const void *md_v)
     365{
     366        HR_DEBUG("%s()", __func__);
     367
     368        const hr_metadata_t *metadata = md_v;
     369
     370        printf("\tmagic: %s\n", metadata->magic);
     371        printf("\tUUID: ");
     372        for (size_t i = 0; i < HR_NATIVE_UUID_LEN; ++i) {
     373                printf("%.2X", metadata->uuid[i]);
     374                if (i + 1 < HR_NATIVE_UUID_LEN)
     375                        printf(" ");
     376        }
     377        printf("\n");
     378        printf("\tdata_blkno: %" PRIu64 "\n", metadata->data_blkno);
     379        printf("\ttruncated_blkno: %" PRIu64 "\n", metadata->truncated_blkno);
     380        printf("\tcounter: %" PRIu64 "\n", metadata->counter);
     381        printf("\tversion: %" PRIu32 "\n", metadata->version);
     382        printf("\textent_no: %" PRIu32 "\n", metadata->extent_no);
     383        printf("\tindex: %" PRIu32 "\n", metadata->index);
     384        printf("\tlevel: %" PRIu32 "\n", metadata->level);
     385        printf("\tlayout: %" PRIu32 "\n", metadata->layout);
     386        printf("\tstrip_size: %" PRIu32 "\n", metadata->strip_size);
     387        printf("\tbsize: %" PRIu32 "\n", metadata->bsize);
     388        printf("\tdevname: %s\n", metadata->devname);
     389}
     390
     391static void *meta_native_alloc_struct(void)
     392{
     393        return calloc(1, sizeof(hr_metadata_t));
    202394}
    203395
     
    342534}
    343535
    344 static errno_t meta_native_erase_block(service_id_t dev)
    345 {
    346         HR_DEBUG("%s()", __func__);
    347 
    348         errno_t rc;
    349         size_t bsize;
    350 
    351         rc = block_get_bsize(dev, &bsize);
    352         if (rc != EOK)
    353                 return rc;
    354 
    355         void *zero_block = calloc(1, bsize);
    356         if (zero_block == NULL)
    357                 return ENOMEM;
    358 
    359         rc = meta_native_write_block(dev, zero_block);
    360         return rc;
    361 }
    362 
    363536static bool meta_native_has_valid_magic(const void *md_v)
    364537{
     
    373546}
    374547
    375 static bool meta_native_compare_uuids(const void *m1p, const void *m2p)
    376 {
    377         const hr_metadata_t *m1 = m1p;
    378         const hr_metadata_t *m2 = m2p;
    379         if (memcmp(m1->uuid, m2->uuid, HR_NATIVE_UUID_LEN) == 0)
    380                 return true;
    381 
    382         return false;
    383 }
    384 
    385 static void meta_native_inc_counter(hr_volume_t *vol)
    386 {
    387         fibril_mutex_lock(&vol->md_lock);
    388 
    389         hr_metadata_t *md = vol->in_mem_md;
    390 
    391         md->counter++;
    392 
    393         fibril_mutex_unlock(&vol->md_lock);
    394 }
    395 
    396 static errno_t meta_native_save(hr_volume_t *vol, bool with_state_callback)
    397 {
    398         HR_DEBUG("%s()", __func__);
    399 
    400         fibril_rwlock_read_lock(&vol->extents_lock);
    401 
    402         for (size_t i = 0; i < vol->extent_no; i++)
    403                 meta_native_save_ext(vol, i, with_state_callback);
    404 
    405         fibril_rwlock_read_unlock(&vol->extents_lock);
    406 
    407         return EOK;
    408 }
    409 
    410 static errno_t meta_native_save_ext(hr_volume_t *vol, size_t ext_idx,
    411     bool with_state_callback)
    412 {
    413         HR_DEBUG("%s()", __func__);
    414 
    415         assert(fibril_rwlock_is_locked(&vol->extents_lock));
    416 
    417         void *md_block = hr_calloc_waitok(1, vol->bsize);
    418 
    419         hr_metadata_t *md = (hr_metadata_t *)vol->in_mem_md;
    420 
    421         hr_extent_t *ext = &vol->extents[ext_idx];
    422 
    423         fibril_rwlock_read_lock(&vol->states_lock);
    424         hr_ext_state_t s = ext->state;
    425         fibril_rwlock_read_unlock(&vol->states_lock);
    426 
    427         if (s != HR_EXT_ONLINE && s != HR_EXT_REBUILD) {
    428                 return EINVAL;
    429         }
    430 
    431         fibril_mutex_lock(&vol->md_lock);
    432 
    433         md->index = ext_idx;
    434         if (s == HR_EXT_REBUILD)
    435                 md->rebuild_pos = vol->rebuild_blk;
    436         else
    437                 md->rebuild_pos = 0;
    438         meta_native_encode(md, md_block);
    439         errno_t rc = meta_native_write_block(ext->svc_id, md_block);
    440         if (rc != EOK && with_state_callback)
    441                 vol->hr_ops.ext_state_cb(vol, ext_idx, rc);
    442 
    443         fibril_mutex_unlock(&vol->md_lock);
    444 
    445         if (with_state_callback)
    446                 vol->hr_ops.vol_state_eval(vol);
    447 
    448         free(md_block);
    449         return EOK;
    450 }
    451 
    452 static const char *meta_native_get_devname(const void *md_v)
    453 {
    454         const hr_metadata_t *md = md_v;
    455 
    456         return md->devname;
    457 }
    458 
    459 static hr_level_t meta_native_get_level(const void *md_v)
    460 {
    461         const hr_metadata_t *md = md_v;
    462 
    463         return md->level;
    464 }
    465 
    466 static uint64_t meta_native_get_data_offset(void)
    467 {
    468         return HR_NATIVE_DATA_OFF;
    469 }
    470 
    471 static size_t meta_native_get_size(void)
    472 {
    473         return HR_NATIVE_META_SIZE;
    474 }
    475 
    476 static uint8_t meta_native_get_flags(void)
    477 {
    478         uint8_t flags = 0;
    479 
    480         flags |= HR_METADATA_HOTSPARE_SUPPORT;
    481 
    482         return flags;
    483 }
    484 
    485 static hr_metadata_type_t meta_native_get_type(void)
    486 {
    487         return HR_METADATA_NATIVE;
    488 }
    489 
    490 static void meta_native_dump(const void *md_v)
    491 {
    492         HR_DEBUG("%s()", __func__);
    493 
    494         const hr_metadata_t *metadata = md_v;
    495 
    496         printf("\tmagic: %s\n", metadata->magic);
    497         printf("\tUUID: ");
    498         for (size_t i = 0; i < HR_NATIVE_UUID_LEN; ++i) {
    499                 printf("%.2X", metadata->uuid[i]);
    500                 if (i + 1 < HR_NATIVE_UUID_LEN)
    501                         printf(" ");
    502         }
    503         printf("\n");
    504         printf("\tdata_blkno: %" PRIu64 "\n", metadata->data_blkno);
    505         printf("\ttruncated_blkno: %" PRIu64 "\n", metadata->truncated_blkno);
    506         printf("\tcounter: %" PRIu64 "\n", metadata->counter);
    507         printf("\tversion: %" PRIu32 "\n", metadata->version);
    508         printf("\textent_no: %" PRIu32 "\n", metadata->extent_no);
    509         printf("\tindex: %" PRIu32 "\n", metadata->index);
    510         printf("\tlevel: %" PRIu32 "\n", metadata->level);
    511         printf("\tlayout: %" PRIu32 "\n", metadata->layout);
    512         printf("\tstrip_size: %" PRIu32 "\n", metadata->strip_size);
    513         printf("\tbsize: %" PRIu32 "\n", metadata->bsize);
    514         printf("\tdevname: %s\n", metadata->devname);
    515 }
    516 
    517548/** @}
    518549 */
  • uspace/srv/bd/hr/superblock.c

    r78433bb raa9bad8  
    6868};
    6969
    70 hr_superblock_ops_t *get_type_ops(hr_metadata_type_t type)
     70hr_superblock_ops_t *hr_get_meta_type_ops(hr_metadata_type_t type)
    7171{
    7272        assert(type >= HR_METADATA_NATIVE && type < HR_METADATA_LAST_DUMMY);
     
    7575}
    7676
    77 errno_t find_metadata(service_id_t svc_id, void **rmetadata,
     77errno_t hr_find_metadata(service_id_t svc_id, void **rmetadata,
    7878    hr_metadata_type_t *rtype)
    7979{
     
    8282        errno_t rc;
    8383        hr_superblock_ops_t *meta_ops;
    84         void *meta_block;
    8584        void *metadata_struct;
    8685
     
    9493                meta_ops = hr_superblock_ops_all[type];
    9594
    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 
     95                rc = meta_ops->probe(svc_id, &metadata_struct);
    11396                if (rc != EOK) {
    114                         free(metadata_struct);
    115                         continue;
    116                 }
    117 
    118                 if (!meta_ops->has_valid_magic(metadata_struct)) {
    119                         free(metadata_struct);
     97                        if (rc != ENOFS)
     98                                return rc;
    12099                        continue;
    121100                }
  • uspace/srv/bd/hr/superblock.h

    r78433bb raa9bad8  
    4444
    4545typedef struct hr_superblock_ops {
    46         void *(*alloc_struct)(void);
     46        errno_t (*probe)(service_id_t, void **);
    4747        errno_t (*init_vol2meta)(hr_volume_t *);
    4848        errno_t (*init_meta2vol)(const list_t *, hr_volume_t *);
    49         void (*encode)(void *, void *);
    50         errno_t (*decode)(const void *, void *);
    51         errno_t (*get_block)(service_id_t, void **);
    52         errno_t (*write_block)(service_id_t, const void *);
    5349        errno_t (*erase_block)(service_id_t);
    54         bool (*has_valid_magic)(const void *);
    5550        bool (*compare_uuids)(const void *, const void *);
    5651        void (*inc_counter)(hr_volume_t *);
     
    6661} hr_superblock_ops_t;
    6762
    68 extern hr_superblock_ops_t *get_type_ops(hr_metadata_type_t);
    69 extern errno_t find_metadata(service_id_t, void **, hr_metadata_type_t *);
     63extern hr_superblock_ops_t *hr_get_meta_type_ops(hr_metadata_type_t);
     64extern errno_t hr_find_metadata(service_id_t, void **, hr_metadata_type_t *);
    7065
    7166#endif
  • uspace/srv/bd/hr/util.c

    r78433bb raa9bad8  
    115115        vol->level = level;
    116116
    117         vol->meta_ops = get_type_ops(metadata_type);
     117        vol->meta_ops = hr_get_meta_type_ops(metadata_type);
    118118
    119119        uint8_t meta_flags = vol->meta_ops->get_flags();
     
    792792        errno_t rc = EOK;
    793793
    794         hr_superblock_ops_t *meta_ops = get_type_ops(type_main);
     794        hr_superblock_ops_t *meta_ops = hr_get_meta_type_ops(type_main);
    795795
    796796        list_foreach(*list, link, struct dev_list_member, iter) {
     
    801801                hr_metadata_type_t type;
    802802
    803                 rc = find_metadata(iter->svc_id, &metadata_struct, &type);
     803                rc = hr_find_metadata(iter->svc_id, &metadata_struct, &type);
    804804                if (rc == ENOFS)
    805805                        continue;
     
    837837        errno_t rc = EOK;
    838838
    839         hr_superblock_ops_t *meta_ops = get_type_ops(type);
     839        hr_superblock_ops_t *meta_ops = hr_get_meta_type_ops(type);
    840840
    841841        link_t *memb_l = list_first(list);
     
    933933                hr_metadata_type_t type;
    934934
    935                 rc = find_metadata(iter->svc_id, &metadata_struct_main, &type);
     935                rc = hr_find_metadata(iter->svc_id, &metadata_struct_main, &type);
    936936                if (rc == ENOFS) {
    937937                        block_fini(iter->svc_id);
Note: See TracChangeset for help on using the changeset viewer.