1ff56536eSAlex Hornung /* $NetBSD: dm_table.c,v 1.5 2010/01/04 00:19:08 haad Exp $ */ 2ff56536eSAlex Hornung 3ff56536eSAlex Hornung /* 4ba2ce341SAlex Hornung * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> 5ff56536eSAlex Hornung * Copyright (c) 2008 The NetBSD Foundation, Inc. 6ff56536eSAlex Hornung * All rights reserved. 7ff56536eSAlex Hornung * 8ff56536eSAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 9ff56536eSAlex Hornung * by Adam Hamsik. 10ff56536eSAlex Hornung * 11ff56536eSAlex Hornung * Redistribution and use in source and binary forms, with or without 12ff56536eSAlex Hornung * modification, are permitted provided that the following conditions 13ff56536eSAlex Hornung * are met: 14ff56536eSAlex Hornung * 1. Redistributions of source code must retain the above copyright 15ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer. 16ff56536eSAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 17ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer in the 18ff56536eSAlex Hornung * documentation and/or other materials provided with the distribution. 19ff56536eSAlex Hornung * 20ff56536eSAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21ff56536eSAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22ff56536eSAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23ff56536eSAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24ff56536eSAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25ff56536eSAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26ff56536eSAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27ff56536eSAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28ff56536eSAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29ff56536eSAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30ff56536eSAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 31ff56536eSAlex Hornung */ 32ff56536eSAlex Hornung 33ff56536eSAlex Hornung #include <sys/types.h> 34ff56536eSAlex Hornung 355b279a20SAlex Hornung #include <sys/malloc.h> 36ff56536eSAlex Hornung 37a84e173eSAlex Hornung #include <dev/disk/dm/dm.h> 38ff56536eSAlex Hornung 39ff56536eSAlex Hornung /* 40ff56536eSAlex Hornung * There are two types of users of this interface: 41ff56536eSAlex Hornung * 42ff56536eSAlex Hornung * a) Readers such as 43ff56536eSAlex Hornung * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl, 44ff56536eSAlex Hornung * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl 45ff56536eSAlex Hornung * 46ff56536eSAlex Hornung * b) Writers such as 47ff56536eSAlex Hornung * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl 48ff56536eSAlex Hornung * 498f77157dSAlex Hornung * Writers can work with table_head only when there are no readers. We 508f77157dSAlex Hornung * simply use shared/exclusive locking to ensure this. 51ff56536eSAlex Hornung */ 52ff56536eSAlex Hornung 53ff56536eSAlex Hornung /* 54ff56536eSAlex Hornung * Function to increment table user reference counter. Return id 55ff56536eSAlex Hornung * of table_id table. 56ff56536eSAlex Hornung * DM_TABLE_ACTIVE will return active table id. 57ff56536eSAlex Hornung * DM_TABLE_INACTIVE will return inactive table id. 58ff56536eSAlex Hornung */ 59ff56536eSAlex Hornung static int 60ff56536eSAlex Hornung dm_table_busy(dm_table_head_t *head, uint8_t table_id) 61ff56536eSAlex Hornung { 62ff56536eSAlex Hornung uint8_t id; 63ff56536eSAlex Hornung 64ff56536eSAlex Hornung id = 0; 65ff56536eSAlex Hornung 668f77157dSAlex Hornung lockmgr(&head->table_mtx, LK_SHARED); 67ff56536eSAlex Hornung 68ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE) 69ff56536eSAlex Hornung id = head->cur_active_table; 70ff56536eSAlex Hornung else 71ff56536eSAlex Hornung id = 1 - head->cur_active_table; 72ff56536eSAlex Hornung 731efa8440SAlex Hornung atomic_add_int(&head->io_cnt, 1); 74ff56536eSAlex Hornung 75ff56536eSAlex Hornung return id; 76ff56536eSAlex Hornung } 77ff56536eSAlex Hornung /* 78ff56536eSAlex Hornung * Function release table lock and eventually wakeup all waiters. 79ff56536eSAlex Hornung */ 80ff56536eSAlex Hornung static void 81ff56536eSAlex Hornung dm_table_unbusy(dm_table_head_t *head) 82ff56536eSAlex Hornung { 835b279a20SAlex Hornung KKASSERT(head->io_cnt != 0); 84ff56536eSAlex Hornung 851efa8440SAlex Hornung atomic_subtract_int(&head->io_cnt, 1); 86ff56536eSAlex Hornung 875b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 88ff56536eSAlex Hornung } 89ff56536eSAlex Hornung /* 90ff56536eSAlex Hornung * Return current active table to caller, increment io_cnt reference counter. 91ff56536eSAlex Hornung */ 92ff56536eSAlex Hornung dm_table_t * 93ff56536eSAlex Hornung dm_table_get_entry(dm_table_head_t *head, uint8_t table_id) 94ff56536eSAlex Hornung { 95ff56536eSAlex Hornung uint8_t id; 96ff56536eSAlex Hornung 97ff56536eSAlex Hornung id = dm_table_busy(head, table_id); 98ff56536eSAlex Hornung 99ff56536eSAlex Hornung return &head->tables[id]; 100ff56536eSAlex Hornung } 101ff56536eSAlex Hornung /* 1028f77157dSAlex Hornung * Decrement io reference counter and release shared lock. 103ff56536eSAlex Hornung */ 104ff56536eSAlex Hornung void 105ff56536eSAlex Hornung dm_table_release(dm_table_head_t *head, uint8_t table_id) 106ff56536eSAlex Hornung { 107ff56536eSAlex Hornung dm_table_unbusy(head); 108ff56536eSAlex Hornung } 109ff56536eSAlex Hornung /* 110ff56536eSAlex Hornung * Switch table from inactive to active mode. Have to wait until io_cnt is 0. 111ff56536eSAlex Hornung */ 112ff56536eSAlex Hornung void 113ff56536eSAlex Hornung dm_table_switch_tables(dm_table_head_t *head) 114ff56536eSAlex Hornung { 1155b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE); 116ff56536eSAlex Hornung 117ff56536eSAlex Hornung head->cur_active_table = 1 - head->cur_active_table; 118ff56536eSAlex Hornung 1195b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 120ff56536eSAlex Hornung } 121ff56536eSAlex Hornung /* 122ff56536eSAlex Hornung * Destroy all table data. This function can run when there are no 123ff56536eSAlex Hornung * readers on table lists. 124ff56536eSAlex Hornung */ 125ff56536eSAlex Hornung int 126ff56536eSAlex Hornung dm_table_destroy(dm_table_head_t *head, uint8_t table_id) 127ff56536eSAlex Hornung { 128ff56536eSAlex Hornung dm_table_t *tbl; 129ff56536eSAlex Hornung dm_table_entry_t *table_en; 130ff56536eSAlex Hornung uint8_t id; 131ff56536eSAlex Hornung 1325b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE); 133ff56536eSAlex Hornung 134a370741aSTomohiro Kusumi aprint_debug("dm_table_destroy called with %d--%d\n", table_id, head->io_cnt); 135ff56536eSAlex Hornung 136ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE) 137ff56536eSAlex Hornung id = head->cur_active_table; 138ff56536eSAlex Hornung else 139ff56536eSAlex Hornung id = 1 - head->cur_active_table; 140ff56536eSAlex Hornung 141ff56536eSAlex Hornung tbl = &head->tables[id]; 142ff56536eSAlex Hornung 1433cd1dc08STomohiro Kusumi while ((table_en = TAILQ_FIRST(tbl)) != NULL) { 1443cd1dc08STomohiro Kusumi TAILQ_REMOVE(tbl, table_en, next); 145ff56536eSAlex Hornung /* 146ff56536eSAlex Hornung * Remove target specific config data. After successfull 147ff56536eSAlex Hornung * call table_en->target_config must be set to NULL. 148ff56536eSAlex Hornung */ 149ff56536eSAlex Hornung table_en->target->destroy(table_en); 150d471f1f9STomohiro Kusumi 151d471f1f9STomohiro Kusumi dm_table_free_deps(table_en); 152d471f1f9STomohiro Kusumi 153b188342fSAdam Hoka /* decrement the refcount for the target */ 154b188342fSAdam Hoka dm_target_unbusy(table_en->target); 155ff56536eSAlex Hornung 1565b279a20SAlex Hornung kfree(table_en, M_DM); 157ff56536eSAlex Hornung } 1583cd1dc08STomohiro Kusumi KKASSERT(TAILQ_EMPTY(tbl)); 159ff56536eSAlex Hornung 1605b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 161ff56536eSAlex Hornung 162ff56536eSAlex Hornung return 0; 163ff56536eSAlex Hornung } 164ff56536eSAlex Hornung /* 165cc760215SAdam Hoka * Return length of active or inactive table in device. 166ff56536eSAlex Hornung */ 167cc760215SAdam Hoka static uint64_t 168cc760215SAdam Hoka _dm_table_size(dm_table_head_t *head, int table) 169ff56536eSAlex Hornung { 170ff56536eSAlex Hornung dm_table_t *tbl; 171ff56536eSAlex Hornung dm_table_entry_t *table_en; 172ff56536eSAlex Hornung uint64_t length; 173ff56536eSAlex Hornung 174ff56536eSAlex Hornung length = 0; 175ff56536eSAlex Hornung 176ff56536eSAlex Hornung /* Select active table */ 177f274dbd1STomohiro Kusumi tbl = dm_table_get_entry(head, table); 178ff56536eSAlex Hornung 179ff56536eSAlex Hornung /* 180ff56536eSAlex Hornung * Find out what tables I want to select. 181ff56536eSAlex Hornung * if length => rawblkno then we should used that table. 182ff56536eSAlex Hornung */ 1833cd1dc08STomohiro Kusumi TAILQ_FOREACH(table_en, tbl, next) { 184ff56536eSAlex Hornung length += table_en->length; 1855b279a20SAlex Hornung } 186ff56536eSAlex Hornung 187ff56536eSAlex Hornung dm_table_unbusy(head); 188ff56536eSAlex Hornung 189ff56536eSAlex Hornung return length; 190ff56536eSAlex Hornung } 191cc760215SAdam Hoka 192cc760215SAdam Hoka uint64_t 193cc760215SAdam Hoka dm_table_size(dm_table_head_t *head) 194cc760215SAdam Hoka { 195cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_ACTIVE); 196cc760215SAdam Hoka } 197cc760215SAdam Hoka 198cc760215SAdam Hoka uint64_t 199cc760215SAdam Hoka dm_inactive_table_size(dm_table_head_t *head) 200cc760215SAdam Hoka { 201cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_INACTIVE); 202cc760215SAdam Hoka } 203cc760215SAdam Hoka 204ff56536eSAlex Hornung /* 205ff56536eSAlex Hornung * Return > 0 if table is at least one table entry (returns number of entries) 206ff56536eSAlex Hornung * and return 0 if there is not. Target count returned from this function 207ff56536eSAlex Hornung * doesn't need to be true when userspace user receive it (after return 208ff56536eSAlex Hornung * there can be dm_dev_resume_ioctl), therfore this isonly informative. 209ff56536eSAlex Hornung */ 210ff56536eSAlex Hornung int 211ff56536eSAlex Hornung dm_table_get_target_count(dm_table_head_t *head, uint8_t table_id) 212ff56536eSAlex Hornung { 213ff56536eSAlex Hornung dm_table_entry_t *table_en; 214ff56536eSAlex Hornung dm_table_t *tbl; 215ff56536eSAlex Hornung uint32_t target_count; 216ff56536eSAlex Hornung 217ff56536eSAlex Hornung target_count = 0; 218ff56536eSAlex Hornung 219f274dbd1STomohiro Kusumi tbl = dm_table_get_entry(head, table_id); 220ff56536eSAlex Hornung 2213cd1dc08STomohiro Kusumi TAILQ_FOREACH(table_en, tbl, next) 222ff56536eSAlex Hornung target_count++; 223ff56536eSAlex Hornung 224ff56536eSAlex Hornung dm_table_unbusy(head); 225ff56536eSAlex Hornung 226ff56536eSAlex Hornung return target_count; 227ff56536eSAlex Hornung } 228ff56536eSAlex Hornung 229ff56536eSAlex Hornung 230ff56536eSAlex Hornung /* 231ff56536eSAlex Hornung * Initialize table_head structures, I'm trying to keep this structure as 232ff56536eSAlex Hornung * opaque as possible. 233ff56536eSAlex Hornung */ 234ff56536eSAlex Hornung void 235ff56536eSAlex Hornung dm_table_head_init(dm_table_head_t *head) 236ff56536eSAlex Hornung { 237ff56536eSAlex Hornung head->cur_active_table = 0; 238ff56536eSAlex Hornung head->io_cnt = 0; 239ff56536eSAlex Hornung 240ff56536eSAlex Hornung /* Initialize tables. */ 2413cd1dc08STomohiro Kusumi TAILQ_INIT(&head->tables[0]); 2423cd1dc08STomohiro Kusumi TAILQ_INIT(&head->tables[1]); 243ff56536eSAlex Hornung 2445b279a20SAlex Hornung lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE); 245ff56536eSAlex Hornung } 246ff56536eSAlex Hornung /* 247ff56536eSAlex Hornung * Destroy all variables in table_head 248ff56536eSAlex Hornung */ 249ff56536eSAlex Hornung void 250ff56536eSAlex Hornung dm_table_head_destroy(dm_table_head_t *head) 251ff56536eSAlex Hornung { 2525b279a20SAlex Hornung KKASSERT(lockcount(&head->table_mtx) == 0); 2538f77157dSAlex Hornung 254ff56536eSAlex Hornung /* tables doens't exists when I call this routine, therefore it 255ff56536eSAlex Hornung * doesn't make sense to have io_cnt != 0 */ 2565b279a20SAlex Hornung KKASSERT(head->io_cnt == 0); 257ff56536eSAlex Hornung 2585b279a20SAlex Hornung lockuninit(&head->table_mtx); 259ff56536eSAlex Hornung } 260d471f1f9STomohiro Kusumi 261e620de1aSTomohiro Kusumi void 262e620de1aSTomohiro Kusumi dm_table_init_target(dm_table_entry_t *table_en, uint32_t type, void *cfg) 263e620de1aSTomohiro Kusumi { 264e620de1aSTomohiro Kusumi table_en->dev->dev_type = type; 265e620de1aSTomohiro Kusumi table_en->target_config = cfg; 266e620de1aSTomohiro Kusumi } 267e620de1aSTomohiro Kusumi 268d471f1f9STomohiro Kusumi int 269d471f1f9STomohiro Kusumi dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev) 270d471f1f9STomohiro Kusumi { 271d471f1f9STomohiro Kusumi dm_table_head_t *head; 272d471f1f9STomohiro Kusumi dm_mapping_t *map; 273d471f1f9STomohiro Kusumi 274d471f1f9STomohiro Kusumi KKASSERT(pdev); 275d471f1f9STomohiro Kusumi 276d471f1f9STomohiro Kusumi head = &table_en->dev->table_head; 277d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_SHARED); 278d471f1f9STomohiro Kusumi 279d471f1f9STomohiro Kusumi TAILQ_FOREACH(map, &table_en->pdev_maps, next) { 280*4f398b7bSTomohiro Kusumi if (map->data.pdev->udev == pdev->udev) { 281d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE); 282d471f1f9STomohiro Kusumi return -1; 283d471f1f9STomohiro Kusumi } 284d471f1f9STomohiro Kusumi } 285d471f1f9STomohiro Kusumi 286d471f1f9STomohiro Kusumi map = kmalloc(sizeof(*map), M_DM, M_WAITOK | M_ZERO); 287d471f1f9STomohiro Kusumi map->data.pdev = pdev; 288d471f1f9STomohiro Kusumi TAILQ_INSERT_TAIL(&table_en->pdev_maps, map, next); 289d471f1f9STomohiro Kusumi 290d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE); 291d471f1f9STomohiro Kusumi 292d471f1f9STomohiro Kusumi return 0; 293d471f1f9STomohiro Kusumi } 294d471f1f9STomohiro Kusumi 295d471f1f9STomohiro Kusumi void 296d471f1f9STomohiro Kusumi dm_table_free_deps(dm_table_entry_t *table_en) 297d471f1f9STomohiro Kusumi { 298d471f1f9STomohiro Kusumi dm_table_head_t *head; 299d471f1f9STomohiro Kusumi dm_mapping_t *map; 300d471f1f9STomohiro Kusumi 301d471f1f9STomohiro Kusumi head = &table_en->dev->table_head; 302d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_SHARED); 303d471f1f9STomohiro Kusumi 304d471f1f9STomohiro Kusumi while ((map = TAILQ_FIRST(&table_en->pdev_maps)) != NULL) { 305d471f1f9STomohiro Kusumi TAILQ_REMOVE(&table_en->pdev_maps, map, next); 306d471f1f9STomohiro Kusumi kfree(map, M_DM); 307d471f1f9STomohiro Kusumi } 308d471f1f9STomohiro Kusumi KKASSERT(TAILQ_EMPTY(&table_en->pdev_maps)); 309d471f1f9STomohiro Kusumi 310d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE); 311d471f1f9STomohiro Kusumi } 312