| 1 | /* $NetBSD: dm_table.c,v 1.7 2011/08/27 17:10:05 ahoka Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Adam Hamsik. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/param.h> |
| 34 | |
| 35 | #include <sys/kmem.h> |
| 36 | |
| 37 | #include "dm.h" |
| 38 | |
| 39 | /* |
| 40 | * There are two types of users of this interface: |
| 41 | * |
| 42 | * a) Readers such as |
| 43 | * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl, |
| 44 | * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl |
| 45 | * |
| 46 | * b) Writers such as |
| 47 | * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl |
| 48 | * |
| 49 | * Writers can work with table_head only when there are no readers. I |
| 50 | * use reference counting on io_cnt. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | static int dm_table_busy(dm_table_head_t *, uint8_t); |
| 55 | static void dm_table_unbusy(dm_table_head_t *); |
| 56 | |
| 57 | /* |
| 58 | * Function to increment table user reference counter. Return id |
| 59 | * of table_id table. |
| 60 | * DM_TABLE_ACTIVE will return active table id. |
| 61 | * DM_TABLE_INACTIVE will return inactive table id. |
| 62 | */ |
| 63 | static int |
| 64 | dm_table_busy(dm_table_head_t * head, uint8_t table_id) |
| 65 | { |
| 66 | uint8_t id; |
| 67 | |
| 68 | id = 0; |
| 69 | |
| 70 | mutex_enter(&head->table_mtx); |
| 71 | |
| 72 | if (table_id == DM_TABLE_ACTIVE) |
| 73 | id = head->cur_active_table; |
| 74 | else |
| 75 | id = 1 - head->cur_active_table; |
| 76 | |
| 77 | head->io_cnt++; |
| 78 | |
| 79 | mutex_exit(&head->table_mtx); |
| 80 | return id; |
| 81 | } |
| 82 | /* |
| 83 | * Function release table lock and eventually wakeup all waiters. |
| 84 | */ |
| 85 | static void |
| 86 | dm_table_unbusy(dm_table_head_t * head) |
| 87 | { |
| 88 | KASSERT(head->io_cnt != 0); |
| 89 | |
| 90 | mutex_enter(&head->table_mtx); |
| 91 | |
| 92 | if (--head->io_cnt == 0) |
| 93 | cv_broadcast(&head->table_cv); |
| 94 | |
| 95 | mutex_exit(&head->table_mtx); |
| 96 | } |
| 97 | /* |
| 98 | * Return current active table to caller, increment io_cnt reference counter. |
| 99 | */ |
| 100 | dm_table_t * |
| 101 | dm_table_get_entry(dm_table_head_t * head, uint8_t table_id) |
| 102 | { |
| 103 | uint8_t id; |
| 104 | |
| 105 | id = dm_table_busy(head, table_id); |
| 106 | |
| 107 | return &head->tables[id]; |
| 108 | } |
| 109 | /* |
| 110 | * Decrement io reference counter and wake up all callers, with table_head cv. |
| 111 | */ |
| 112 | void |
| 113 | dm_table_release(dm_table_head_t * head, uint8_t table_id) |
| 114 | { |
| 115 | dm_table_unbusy(head); |
| 116 | } |
| 117 | /* |
| 118 | * Switch table from inactive to active mode. Have to wait until io_cnt is 0. |
| 119 | */ |
| 120 | void |
| 121 | dm_table_switch_tables(dm_table_head_t * head) |
| 122 | { |
| 123 | mutex_enter(&head->table_mtx); |
| 124 | |
| 125 | while (head->io_cnt != 0) |
| 126 | cv_wait(&head->table_cv, &head->table_mtx); |
| 127 | |
| 128 | head->cur_active_table = 1 - head->cur_active_table; |
| 129 | |
| 130 | mutex_exit(&head->table_mtx); |
| 131 | } |
| 132 | /* |
| 133 | * Destroy all table data. This function can run when there are no |
| 134 | * readers on table lists. |
| 135 | * |
| 136 | * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs |
| 137 | */ |
| 138 | int |
| 139 | dm_table_destroy(dm_table_head_t * head, uint8_t table_id) |
| 140 | { |
| 141 | dm_table_t *tbl; |
| 142 | dm_table_entry_t *table_en; |
| 143 | uint8_t id; |
| 144 | |
| 145 | mutex_enter(&head->table_mtx); |
| 146 | |
| 147 | aprint_debug("dm_Table_destroy called with %d--%d\n" , table_id, head->io_cnt); |
| 148 | |
| 149 | while (head->io_cnt != 0) |
| 150 | cv_wait(&head->table_cv, &head->table_mtx); |
| 151 | |
| 152 | if (table_id == DM_TABLE_ACTIVE) |
| 153 | id = head->cur_active_table; |
| 154 | else |
| 155 | id = 1 - head->cur_active_table; |
| 156 | |
| 157 | tbl = &head->tables[id]; |
| 158 | |
| 159 | while (!SLIST_EMPTY(tbl)) { /* List Deletion. */ |
| 160 | table_en = SLIST_FIRST(tbl); |
| 161 | /* |
| 162 | * Remove target specific config data. After successfull |
| 163 | * call table_en->target_config must be set to NULL. |
| 164 | */ |
| 165 | table_en->target->destroy(table_en); |
| 166 | |
| 167 | SLIST_REMOVE_HEAD(tbl, next); |
| 168 | |
| 169 | kmem_free(table_en, sizeof(*table_en)); |
| 170 | } |
| 171 | |
| 172 | mutex_exit(&head->table_mtx); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | /* |
| 177 | * Return length of active table in device. |
| 178 | */ |
| 179 | static inline uint64_t |
| 180 | dm_table_size_impl(dm_table_head_t * head, int table) |
| 181 | { |
| 182 | dm_table_t *tbl; |
| 183 | dm_table_entry_t *table_en; |
| 184 | uint64_t length; |
| 185 | uint8_t id; |
| 186 | |
| 187 | length = 0; |
| 188 | |
| 189 | id = dm_table_busy(head, table); |
| 190 | |
| 191 | /* Select active table */ |
| 192 | tbl = &head->tables[id]; |
| 193 | |
| 194 | /* |
| 195 | * Find out what tables I want to select. |
| 196 | * if length => rawblkno then we should used that table. |
| 197 | */ |
| 198 | SLIST_FOREACH(table_en, tbl, next) |
| 199 | length += table_en->length; |
| 200 | |
| 201 | dm_table_unbusy(head); |
| 202 | |
| 203 | return length; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Return length of active table in device. |
| 208 | */ |
| 209 | uint64_t |
| 210 | dm_table_size(dm_table_head_t * head) |
| 211 | { |
| 212 | return dm_table_size_impl(head, DM_TABLE_ACTIVE); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Return length of active table in device. |
| 217 | */ |
| 218 | uint64_t |
| 219 | dm_inactive_table_size(dm_table_head_t * head) |
| 220 | { |
| 221 | return dm_table_size_impl(head, DM_TABLE_INACTIVE); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Return combined disk geometry |
| 226 | */ |
| 227 | void |
| 228 | dm_table_disksize(dm_table_head_t * head, uint64_t *numsecp, unsigned *secsizep) |
| 229 | { |
| 230 | dm_table_t *tbl; |
| 231 | dm_table_entry_t *table_en; |
| 232 | uint64_t length; |
| 233 | unsigned secsize, tsecsize; |
| 234 | uint8_t id; |
| 235 | |
| 236 | length = 0; |
| 237 | |
| 238 | id = dm_table_busy(head, DM_TABLE_ACTIVE); |
| 239 | |
| 240 | /* Select active table */ |
| 241 | tbl = &head->tables[id]; |
| 242 | |
| 243 | /* |
| 244 | * Find out what tables I want to select. |
| 245 | * if length => rawblkno then we should used that table. |
| 246 | */ |
| 247 | secsize = 0; |
| 248 | SLIST_FOREACH(table_en, tbl, next) { |
| 249 | length += table_en->length; |
| 250 | (void)table_en->target->secsize(table_en, &tsecsize); |
| 251 | if (secsize < tsecsize) |
| 252 | secsize = tsecsize; |
| 253 | } |
| 254 | *numsecp = secsize > 0 ? dbtob(length) / secsize : 0; |
| 255 | *secsizep = secsize; |
| 256 | |
| 257 | dm_table_unbusy(head); |
| 258 | } |
| 259 | /* |
| 260 | * Return > 0 if table is at least one table entry (returns number of entries) |
| 261 | * and return 0 if there is not. Target count returned from this function |
| 262 | * doesn't need to be true when userspace user receive it (after return |
| 263 | * there can be dm_dev_resume_ioctl), therfore this isonly informative. |
| 264 | */ |
| 265 | int |
| 266 | dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id) |
| 267 | { |
| 268 | dm_table_entry_t *table_en; |
| 269 | dm_table_t *tbl; |
| 270 | uint32_t target_count; |
| 271 | uint8_t id; |
| 272 | |
| 273 | target_count = 0; |
| 274 | |
| 275 | id = dm_table_busy(head, table_id); |
| 276 | |
| 277 | tbl = &head->tables[id]; |
| 278 | |
| 279 | SLIST_FOREACH(table_en, tbl, next) |
| 280 | target_count++; |
| 281 | |
| 282 | dm_table_unbusy(head); |
| 283 | |
| 284 | return target_count; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /* |
| 289 | * Initialize table_head structures, I'm trying to keep this structure as |
| 290 | * opaque as possible. |
| 291 | */ |
| 292 | void |
| 293 | dm_table_head_init(dm_table_head_t * head) |
| 294 | { |
| 295 | head->cur_active_table = 0; |
| 296 | head->io_cnt = 0; |
| 297 | |
| 298 | /* Initialize tables. */ |
| 299 | SLIST_INIT(&head->tables[0]); |
| 300 | SLIST_INIT(&head->tables[1]); |
| 301 | |
| 302 | mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE); |
| 303 | cv_init(&head->table_cv, "dm_io" ); |
| 304 | } |
| 305 | /* |
| 306 | * Destroy all variables in table_head |
| 307 | */ |
| 308 | void |
| 309 | dm_table_head_destroy(dm_table_head_t * head) |
| 310 | { |
| 311 | KASSERT(!mutex_owned(&head->table_mtx)); |
| 312 | KASSERT(!cv_has_waiters(&head->table_cv)); |
| 313 | /* tables doens't exists when I call this routine, therefore it |
| 314 | * doesn't make sense to have io_cnt != 0 */ |
| 315 | KASSERT(head->io_cnt == 0); |
| 316 | |
| 317 | cv_destroy(&head->table_cv); |
| 318 | mutex_destroy(&head->table_mtx); |
| 319 | } |
| 320 | |