[94d84a0] | 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 <async.h>
|
---|
| 37 | #include <bd_srv.h>
|
---|
| 38 | #include <block.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <hr.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <ipc/hr.h>
|
---|
| 43 | #include <ipc/services.h>
|
---|
| 44 | #include <loc.h>
|
---|
| 45 | #include <task.h>
|
---|
| 46 | #include <stdio.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include <str_error.h>
|
---|
| 49 |
|
---|
| 50 | #include "var.h"
|
---|
[da5c257] | 51 | #include "util.h"
|
---|
[94d84a0] | 52 |
|
---|
| 53 | extern fibril_mutex_t big_lock;
|
---|
| 54 | extern loc_srv_t *hr_srv;
|
---|
| 55 |
|
---|
| 56 | static errno_t hr_raid1_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 57 | static errno_t hr_raid1_bd_close(bd_srv_t *);
|
---|
| 58 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *,
|
---|
| 59 | size_t);
|
---|
| 60 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
|
---|
| 61 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *, aoff64_t, size_t,
|
---|
| 62 | const void *, size_t);
|
---|
| 63 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 64 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
| 65 |
|
---|
| 66 | static bd_ops_t hr_raid1_bd_ops = {
|
---|
| 67 | .open = hr_raid1_bd_open,
|
---|
| 68 | .close = hr_raid1_bd_close,
|
---|
| 69 | .sync_cache = hr_raid1_bd_sync_cache,
|
---|
| 70 | .read_blocks = hr_raid1_bd_read_blocks,
|
---|
| 71 | .write_blocks = hr_raid1_bd_write_blocks,
|
---|
| 72 | .get_block_size = hr_raid1_bd_get_block_size,
|
---|
| 73 | .get_num_blocks = hr_raid1_bd_get_num_blocks
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | static errno_t hr_raid1_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
| 77 | {
|
---|
| 78 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_open()");
|
---|
| 79 | return EOK;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | static errno_t hr_raid1_bd_close(bd_srv_t *bd)
|
---|
| 83 | {
|
---|
| 84 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_bd_close()");
|
---|
| 85 | return EOK;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | static errno_t hr_raid1_bd_sync_cache(bd_srv_t *bd, aoff64_t ba, size_t size)
|
---|
| 89 | {
|
---|
| 90 | fibril_mutex_lock(&big_lock);
|
---|
| 91 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 92 |
|
---|
| 93 | errno_t rc;
|
---|
| 94 | size_t i;
|
---|
| 95 |
|
---|
| 96 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 97 | rc = block_sync_cache(vol->devs[i], ba, size);
|
---|
| 98 | if (rc != EOK)
|
---|
| 99 | break;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | fibril_mutex_unlock(&big_lock);
|
---|
| 103 | return rc;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | static errno_t hr_raid1_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 107 | void *buf, size_t size)
|
---|
| 108 | {
|
---|
| 109 | fibril_mutex_lock(&big_lock);
|
---|
| 110 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 111 |
|
---|
| 112 | errno_t rc;
|
---|
| 113 | size_t i;
|
---|
| 114 |
|
---|
| 115 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 116 | rc = block_read_direct(vol->devs[i], ba, cnt, buf);
|
---|
| 117 | if (rc != EOK)
|
---|
| 118 | break;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | fibril_mutex_unlock(&big_lock);
|
---|
| 122 | return rc;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static errno_t hr_raid1_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 126 | const void *data, size_t size)
|
---|
| 127 | {
|
---|
| 128 | fibril_mutex_lock(&big_lock);
|
---|
| 129 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 130 |
|
---|
| 131 | errno_t rc;
|
---|
| 132 | size_t i;
|
---|
| 133 |
|
---|
| 134 | for (i = 0; i < vol->dev_no; i++) {
|
---|
| 135 | rc = block_write_direct(vol->devs[i], ba, cnt, data);
|
---|
| 136 | if (rc != EOK)
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | fibril_mutex_unlock(&big_lock);
|
---|
| 141 | return rc;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static errno_t hr_raid1_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
| 145 | {
|
---|
| 146 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 147 |
|
---|
| 148 | return block_get_bsize(vol->devs[0], rsize);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | static errno_t hr_raid1_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
| 152 | {
|
---|
| 153 | hr_volume_t *vol = bd->srvs->sarg;
|
---|
| 154 |
|
---|
| 155 | return block_get_nblocks(vol->devs[0], rnb);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | errno_t hr_raid1_create(hr_volume_t *new_volume)
|
---|
| 159 | {
|
---|
| 160 | assert(new_volume->level == hr_l_1);
|
---|
| 161 |
|
---|
| 162 | errno_t rc;
|
---|
| 163 |
|
---|
| 164 | rc = hr_init_devs(new_volume);
|
---|
| 165 | if (rc != EOK)
|
---|
[da5c257] | 166 | return rc;
|
---|
[94d84a0] | 167 |
|
---|
| 168 | bd_srvs_init(&new_volume->hr_bds);
|
---|
| 169 | new_volume->hr_bds.ops = &hr_raid1_bd_ops;
|
---|
| 170 | new_volume->hr_bds.sarg = new_volume;
|
---|
| 171 |
|
---|
[da5c257] | 172 | rc = hr_register_volume(new_volume);
|
---|
[94d84a0] | 173 | if (rc != EOK) {
|
---|
[da5c257] | 174 | hr_fini_devs(new_volume);
|
---|
| 175 | return rc;
|
---|
[94d84a0] | 176 | }
|
---|
| 177 |
|
---|
| 178 | return EOK;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /** @}
|
---|
| 182 | */
|
---|