sys/dev/disk/dm/dm_table.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | /* $NetBSD: dm_table.c,v 1.5 2010/01/04 00:19:08 haad Exp $ */ /* * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> * Copyright (c) 2008 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Adam Hamsik. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``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 FOUNDATION OR CONTRIBUTORS * 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. */ #include <sys/param.h> #include <sys/malloc.h> #include <cpu/atomic.h> #include <dev/disk/dm/dm.h> /* * There are two types of users of this interface: * * a) Readers such as * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl, * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl * * b) Writers such as * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl * * Writers can work with table_head only when there are no readers. We * simply use shared/exclusive locking to ensure this. */ /* * Function to increment table user reference counter. Return id * of table_id table. * DM_TABLE_ACTIVE will return active table id. * DM_TABLE_INACTIVE will return inactive table id. */ static int dm_table_busy(dm_table_head_t *head, uint8_t table_id) { uint8_t id; id = 0; lockmgr(&head->table_mtx, LK_SHARED); if (table_id == DM_TABLE_ACTIVE) id = head->cur_active_table; else id = 1 - head->cur_active_table; atomic_add_int(&head->io_cnt, 1); return id; } /* * Function release table lock and eventually wakeup all waiters. */ static void dm_table_unbusy(dm_table_head_t *head) { KKASSERT(head->io_cnt != 0); atomic_subtract_int(&head->io_cnt, 1); lockmgr(&head->table_mtx, LK_RELEASE); } /* * Return current active table to caller, increment io_cnt reference counter. */ dm_table_t * dm_table_get_entry(dm_table_head_t *head, uint8_t table_id) { uint8_t id; id = dm_table_busy(head, table_id); return &head->tables[id]; } /* * Decrement io reference counter and release shared lock. */ void dm_table_release(dm_table_head_t *head, uint8_t table_id) { dm_table_unbusy(head); } /* * Switch table from inactive to active mode. Have to wait until io_cnt is 0. */ void dm_table_switch_tables(dm_table_head_t *head) { lockmgr(&head->table_mtx, LK_EXCLUSIVE); head->cur_active_table = 1 - head->cur_active_table; lockmgr(&head->table_mtx, LK_RELEASE); } /* * Destroy all table data. This function can run when there are no * readers on table lists. */ int dm_table_destroy(dm_table_head_t *head, uint8_t table_id) { dm_table_t *tbl; dm_table_entry_t *table_en; uint8_t id; lockmgr(&head->table_mtx, LK_EXCLUSIVE); dmdebug("table_id=%d io_cnt=%d\n", table_id, head->io_cnt); if (table_id == DM_TABLE_ACTIVE) id = head->cur_active_table; else id = 1 - head->cur_active_table; tbl = &head->tables[id]; while ((table_en = TAILQ_FIRST(tbl)) != NULL) { TAILQ_REMOVE(tbl, table_en, next); if (table_en->target->destroy) table_en->target->destroy(table_en); table_en->target_config = NULL; dm_table_free_deps(table_en); /* decrement the refcount for the target */ dm_target_unbusy(table_en->target); kfree(table_en, M_DM); } KKASSERT(TAILQ_EMPTY(tbl)); lockmgr(&head->table_mtx, LK_RELEASE); return 0; } /* * Return length of active or inactive table in device. */ static uint64_t _dm_table_size(dm_table_head_t *head, int table) { dm_table_t *tbl; dm_table_entry_t *table_en; uint64_t length; length = 0; /* Select active table */ tbl = dm_table_get_entry(head, table); /* * Find out what tables I want to select. * if length => rawblkno then we should used that table. */ TAILQ_FOREACH(table_en, tbl, next) { length += table_en->length; } dm_table_unbusy(head); return length; } uint64_t dm_table_size(dm_table_head_t *head) { return _dm_table_size(head, DM_TABLE_ACTIVE); } uint64_t dm_inactive_table_size(dm_table_head_t *head) { return _dm_table_size(head, DM_TABLE_INACTIVE); } /* * Return > 0 if table is at least one table entry (returns number of entries) * and return 0 if there is not. Target count returned from this function * doesn't need to be true when userspace user receive it (after return * there can be dm_dev_resume_ioctl), therefore this is only informative. */ int dm_table_get_target_count(dm_table_head_t *head, uint8_t table_id) { dm_table_entry_t *table_en; dm_table_t *tbl; uint32_t target_count; target_count = 0; tbl = dm_table_get_entry(head, table_id); TAILQ_FOREACH(table_en, tbl, next) target_count++; dm_table_unbusy(head); return target_count; } /* * Initialize dm_table_head_t structures, I'm trying to keep this structure as * opaque as possible. */ void dm_table_head_init(dm_table_head_t *head) { head->cur_active_table = 0; head->io_cnt = 0; /* Initialize tables. */ TAILQ_INIT(&head->tables[0]); TAILQ_INIT(&head->tables[1]); lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE); } /* * Destroy all variables in table_head */ void dm_table_head_destroy(dm_table_head_t *head) { KKASSERT(!lockinuse(&head->table_mtx)); /* tables don't exist when I call this routine, therefore it * doesn't make sense to have io_cnt != 0 */ KKASSERT(head->io_cnt == 0); lockuninit(&head->table_mtx); } void dm_table_init_target(dm_table_entry_t *table_en, void *cfg) { table_en->target_config = cfg; } int dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev) { dm_table_head_t *head; dm_mapping_t *map; KKASSERT(pdev); head = &table_en->dev->table_head; lockmgr(&head->table_mtx, LK_SHARED); TAILQ_FOREACH(map, &table_en->pdev_maps, next) { if (map->data.pdev->udev == pdev->udev) { lockmgr(&head->table_mtx, LK_RELEASE); return -1; } } map = kmalloc(sizeof(*map), M_DM, M_WAITOK | M_ZERO); map->data.pdev = pdev; TAILQ_INSERT_TAIL(&table_en->pdev_maps, map, next); lockmgr(&head->table_mtx, LK_RELEASE); return 0; } void dm_table_free_deps(dm_table_entry_t *table_en) { dm_table_head_t *head; dm_mapping_t *map; head = &table_en->dev->table_head; lockmgr(&head->table_mtx, LK_SHARED); while ((map = TAILQ_FIRST(&table_en->pdev_maps)) != NULL) { TAILQ_REMOVE(&table_en->pdev_maps, map, next); kfree(map, M_DM); } KKASSERT(TAILQ_EMPTY(&table_en->pdev_maps)); lockmgr(&head->table_mtx, LK_RELEASE); } |