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 #include <sys/param.h> 35ff56536eSAlex Hornung 365b279a20SAlex Hornung #include <sys/malloc.h> 37ff56536eSAlex Hornung 38a84e173eSAlex Hornung #include <dev/disk/dm/dm.h> 39ff56536eSAlex Hornung 40ff56536eSAlex Hornung /* 41ff56536eSAlex Hornung * There are two types of users of this interface: 42ff56536eSAlex Hornung * 43ff56536eSAlex Hornung * a) Readers such as 44ff56536eSAlex Hornung * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl, 45ff56536eSAlex Hornung * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl 46ff56536eSAlex Hornung * 47ff56536eSAlex Hornung * b) Writers such as 48ff56536eSAlex Hornung * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl 49ff56536eSAlex Hornung * 508f77157dSAlex Hornung * Writers can work with table_head only when there are no readers. We 518f77157dSAlex Hornung * simply use shared/exclusive locking to ensure this. 52ff56536eSAlex Hornung */ 53ff56536eSAlex Hornung 54ff56536eSAlex Hornung static int dm_table_busy(dm_table_head_t *, uint8_t); 55ff56536eSAlex Hornung static void dm_table_unbusy(dm_table_head_t *); 56ff56536eSAlex Hornung 57ff56536eSAlex Hornung /* 58ff56536eSAlex Hornung * Function to increment table user reference counter. Return id 59ff56536eSAlex Hornung * of table_id table. 60ff56536eSAlex Hornung * DM_TABLE_ACTIVE will return active table id. 61ff56536eSAlex Hornung * DM_TABLE_INACTIVE will return inactive table id. 62ff56536eSAlex Hornung */ 63ff56536eSAlex Hornung static int 64ff56536eSAlex Hornung dm_table_busy(dm_table_head_t * head, uint8_t table_id) 65ff56536eSAlex Hornung { 66ff56536eSAlex Hornung uint8_t id; 67ff56536eSAlex Hornung 68ff56536eSAlex Hornung id = 0; 69ff56536eSAlex Hornung 708f77157dSAlex Hornung lockmgr(&head->table_mtx, LK_SHARED); 71ff56536eSAlex Hornung 72ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE) 73ff56536eSAlex Hornung id = head->cur_active_table; 74ff56536eSAlex Hornung else 75ff56536eSAlex Hornung id = 1 - head->cur_active_table; 76ff56536eSAlex Hornung 771efa8440SAlex Hornung atomic_add_int(&head->io_cnt, 1); 78ff56536eSAlex Hornung 79ff56536eSAlex Hornung return id; 80ff56536eSAlex Hornung } 81ff56536eSAlex Hornung /* 82ff56536eSAlex Hornung * Function release table lock and eventually wakeup all waiters. 83ff56536eSAlex Hornung */ 84ff56536eSAlex Hornung static void 85ff56536eSAlex Hornung dm_table_unbusy(dm_table_head_t * head) 86ff56536eSAlex Hornung { 875b279a20SAlex Hornung KKASSERT(head->io_cnt != 0); 88ff56536eSAlex Hornung 891efa8440SAlex Hornung atomic_subtract_int(&head->io_cnt, 1); 90ff56536eSAlex Hornung 915b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 92ff56536eSAlex Hornung } 93ff56536eSAlex Hornung /* 94ff56536eSAlex Hornung * Return current active table to caller, increment io_cnt reference counter. 95ff56536eSAlex Hornung */ 96ff56536eSAlex Hornung dm_table_t * 97ff56536eSAlex Hornung dm_table_get_entry(dm_table_head_t * head, uint8_t table_id) 98ff56536eSAlex Hornung { 99ff56536eSAlex Hornung uint8_t id; 100ff56536eSAlex Hornung 101ff56536eSAlex Hornung id = dm_table_busy(head, table_id); 102ff56536eSAlex Hornung 103ff56536eSAlex Hornung return &head->tables[id]; 104ff56536eSAlex Hornung } 105ff56536eSAlex Hornung /* 1068f77157dSAlex Hornung * Decrement io reference counter and release shared lock. 107ff56536eSAlex Hornung */ 108ff56536eSAlex Hornung void 109ff56536eSAlex Hornung dm_table_release(dm_table_head_t * head, uint8_t table_id) 110ff56536eSAlex Hornung { 111ff56536eSAlex Hornung dm_table_unbusy(head); 112ff56536eSAlex Hornung } 113ff56536eSAlex Hornung /* 114ff56536eSAlex Hornung * Switch table from inactive to active mode. Have to wait until io_cnt is 0. 115ff56536eSAlex Hornung */ 116ff56536eSAlex Hornung void 117ff56536eSAlex Hornung dm_table_switch_tables(dm_table_head_t * head) 118ff56536eSAlex Hornung { 1195b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE); 120ff56536eSAlex Hornung 121ff56536eSAlex Hornung head->cur_active_table = 1 - head->cur_active_table; 122ff56536eSAlex Hornung 1235b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 124ff56536eSAlex Hornung } 125ff56536eSAlex Hornung /* 126ff56536eSAlex Hornung * Destroy all table data. This function can run when there are no 127ff56536eSAlex Hornung * readers on table lists. 128ff56536eSAlex Hornung */ 129ff56536eSAlex Hornung int 130ff56536eSAlex Hornung dm_table_destroy(dm_table_head_t * head, uint8_t table_id) 131ff56536eSAlex Hornung { 132ff56536eSAlex Hornung dm_table_t *tbl; 133ff56536eSAlex Hornung dm_table_entry_t *table_en; 134ff56536eSAlex Hornung uint8_t id; 135ff56536eSAlex Hornung 1365b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE); 137ff56536eSAlex Hornung 138ff56536eSAlex Hornung aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt); 139ff56536eSAlex Hornung 140ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE) 141ff56536eSAlex Hornung id = head->cur_active_table; 142ff56536eSAlex Hornung else 143ff56536eSAlex Hornung id = 1 - head->cur_active_table; 144ff56536eSAlex Hornung 145ff56536eSAlex Hornung tbl = &head->tables[id]; 146ff56536eSAlex Hornung 147ff56536eSAlex Hornung while (!SLIST_EMPTY(tbl)) { /* List Deletion. */ 148ff56536eSAlex Hornung table_en = SLIST_FIRST(tbl); 149ff56536eSAlex Hornung /* 150ff56536eSAlex Hornung * Remove target specific config data. After successfull 151ff56536eSAlex Hornung * call table_en->target_config must be set to NULL. 152ff56536eSAlex Hornung */ 153ff56536eSAlex Hornung table_en->target->destroy(table_en); 154*b188342fSAdam Hoka /* decrement the refcount for the target */ 155*b188342fSAdam Hoka dm_target_unbusy(table_en->target); 156ff56536eSAlex Hornung 157ff56536eSAlex Hornung SLIST_REMOVE_HEAD(tbl, next); 158ff56536eSAlex Hornung 1595b279a20SAlex Hornung kfree(table_en, M_DM); 160ff56536eSAlex Hornung } 161ff56536eSAlex Hornung 1625b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE); 163ff56536eSAlex Hornung 164ff56536eSAlex Hornung return 0; 165ff56536eSAlex Hornung } 166ff56536eSAlex Hornung /* 167cc760215SAdam Hoka * Return length of active or inactive table in device. 168ff56536eSAlex Hornung */ 169cc760215SAdam Hoka static uint64_t 170cc760215SAdam Hoka _dm_table_size(dm_table_head_t * head, int table) 171ff56536eSAlex Hornung { 172ff56536eSAlex Hornung dm_table_t *tbl; 173ff56536eSAlex Hornung dm_table_entry_t *table_en; 174ff56536eSAlex Hornung uint64_t length; 175ff56536eSAlex Hornung uint8_t id; 176ff56536eSAlex Hornung 177ff56536eSAlex Hornung length = 0; 178ff56536eSAlex Hornung 179cc760215SAdam Hoka id = dm_table_busy(head, table); 180ff56536eSAlex Hornung 181ff56536eSAlex Hornung /* Select active table */ 182ff56536eSAlex Hornung tbl = &head->tables[id]; 183ff56536eSAlex Hornung 184ff56536eSAlex Hornung /* 185ff56536eSAlex Hornung * Find out what tables I want to select. 186ff56536eSAlex Hornung * if length => rawblkno then we should used that table. 187ff56536eSAlex Hornung */ 1885b279a20SAlex Hornung SLIST_FOREACH(table_en, tbl, next) { 189ff56536eSAlex Hornung length += table_en->length; 1905b279a20SAlex Hornung } 191ff56536eSAlex Hornung 192ff56536eSAlex Hornung dm_table_unbusy(head); 193ff56536eSAlex Hornung 194ff56536eSAlex Hornung return length; 195ff56536eSAlex Hornung } 196cc760215SAdam Hoka 197cc760215SAdam Hoka uint64_t 198cc760215SAdam Hoka dm_table_size(dm_table_head_t *head) 199cc760215SAdam Hoka { 200cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_ACTIVE); 201cc760215SAdam Hoka } 202cc760215SAdam Hoka 203cc760215SAdam Hoka uint64_t 204cc760215SAdam Hoka dm_inactive_table_size(dm_table_head_t *head) 205cc760215SAdam Hoka { 206cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_INACTIVE); 207cc760215SAdam Hoka } 208cc760215SAdam Hoka 209ff56536eSAlex Hornung /* 210ff56536eSAlex Hornung * Return > 0 if table is at least one table entry (returns number of entries) 211ff56536eSAlex Hornung * and return 0 if there is not. Target count returned from this function 212ff56536eSAlex Hornung * doesn't need to be true when userspace user receive it (after return 213ff56536eSAlex Hornung * there can be dm_dev_resume_ioctl), therfore this isonly informative. 214ff56536eSAlex Hornung */ 215ff56536eSAlex Hornung int 216ff56536eSAlex Hornung dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id) 217ff56536eSAlex Hornung { 218ff56536eSAlex Hornung dm_table_entry_t *table_en; 219ff56536eSAlex Hornung dm_table_t *tbl; 220ff56536eSAlex Hornung uint32_t target_count; 221ff56536eSAlex Hornung uint8_t id; 222ff56536eSAlex Hornung 223ff56536eSAlex Hornung target_count = 0; 224ff56536eSAlex Hornung 225ff56536eSAlex Hornung id = dm_table_busy(head, table_id); 226ff56536eSAlex Hornung 227ff56536eSAlex Hornung tbl = &head->tables[id]; 228ff56536eSAlex Hornung 229ff56536eSAlex Hornung SLIST_FOREACH(table_en, tbl, next) 230ff56536eSAlex Hornung target_count++; 231ff56536eSAlex Hornung 232ff56536eSAlex Hornung dm_table_unbusy(head); 233ff56536eSAlex Hornung 234ff56536eSAlex Hornung return target_count; 235ff56536eSAlex Hornung } 236ff56536eSAlex Hornung 237ff56536eSAlex Hornung 238ff56536eSAlex Hornung /* 239ff56536eSAlex Hornung * Initialize table_head structures, I'm trying to keep this structure as 240ff56536eSAlex Hornung * opaque as possible. 241ff56536eSAlex Hornung */ 242ff56536eSAlex Hornung void 243ff56536eSAlex Hornung dm_table_head_init(dm_table_head_t * head) 244ff56536eSAlex Hornung { 245ff56536eSAlex Hornung head->cur_active_table = 0; 246ff56536eSAlex Hornung head->io_cnt = 0; 247ff56536eSAlex Hornung 248ff56536eSAlex Hornung /* Initialize tables. */ 249ff56536eSAlex Hornung SLIST_INIT(&head->tables[0]); 250ff56536eSAlex Hornung SLIST_INIT(&head->tables[1]); 251ff56536eSAlex Hornung 2525b279a20SAlex Hornung lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE); 253ff56536eSAlex Hornung } 254ff56536eSAlex Hornung /* 255ff56536eSAlex Hornung * Destroy all variables in table_head 256ff56536eSAlex Hornung */ 257ff56536eSAlex Hornung void 258ff56536eSAlex Hornung dm_table_head_destroy(dm_table_head_t * head) 259ff56536eSAlex Hornung { 2605b279a20SAlex Hornung KKASSERT(lockcount(&head->table_mtx) == 0); 2618f77157dSAlex Hornung 262ff56536eSAlex Hornung /* tables doens't exists when I call this routine, therefore it 263ff56536eSAlex Hornung * doesn't make sense to have io_cnt != 0 */ 2645b279a20SAlex Hornung KKASSERT(head->io_cnt == 0); 265ff56536eSAlex Hornung 2665b279a20SAlex Hornung lockuninit(&head->table_mtx); 267ff56536eSAlex Hornung } 268