Annotation of src/sys/dev/dm/dm_table.c, revision 1.1.2.10
1.1.2.10! haad 1: /* $NetBSD: dm_table.c,v 1.1.2.9 2008/09/08 11:36:24 haad Exp $ */
1.1.2.7 haad 2:
1.1.2.1 haad 3: /*
4: * Copyright (c) 1996, 1997, 1998, 1999, 2002 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:
1.1.2.5 haad 39: /*
1.1.2.10! haad 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(struct dm_table_head *, uint8_t );
! 55: static void dm_table_unbusy(struct dm_table_head *);
! 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(struct dm_table_head *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: /*
! 84: * Function release table lock and eventually wakeup all waiters.
1.1.2.5 haad 85: */
1.1.2.10! haad 86: static void
! 87: dm_table_unbusy(struct dm_table_head *head)
! 88: {
! 89: KASSERT(head->io_cnt != 0);
! 90:
! 91: mutex_enter(&head->table_mtx);
! 92:
! 93: if (--head->io_cnt == 0)
! 94: cv_broadcast(&head->table_cv);
! 95:
! 96: mutex_exit(&head->table_mtx);
! 97: }
! 98:
! 99: /*
! 100: * Return current active table to caller, increment io_cnt reference counter.
! 101: */
! 102: struct dm_table *
! 103: dm_table_get_entry(struct dm_table_head *head, uint8_t table_id)
! 104: {
! 105: uint8_t id;
! 106:
! 107: id = dm_table_busy(head, table_id);
! 108:
! 109: return &head->tables[id];
! 110: }
! 111:
! 112: /*
! 113: * Decrement io reference counter and wake up all callers, with table_head cv.
! 114: */
! 115: void
! 116: dm_table_release(struct dm_table_head *head, uint8_t table_id)
! 117: {
! 118: dm_table_unbusy(head);
! 119: }
! 120:
! 121: /*
! 122: * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
! 123: */
! 124: void
! 125: dm_table_switch_tables(struct dm_table_head *head)
! 126: {
! 127: mutex_enter(&head->table_mtx);
! 128:
! 129: while (head->io_cnt != 0)
! 130: cv_wait(&head->table_cv, &head->table_mtx);
! 131:
! 132: head->cur_active_table = 1 - head->cur_active_table;
! 133:
! 134: mutex_exit(&head->table_mtx);
! 135: }
1.1.2.5 haad 136:
1.1.2.10! haad 137: /*
! 138: * Destroy all table data. This function can run when there are no
! 139: * readers on table lists.
! 140: *
! 141: * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs
! 142: */
1.1.2.1 haad 143: int
1.1.2.10! haad 144: dm_table_destroy(struct dm_table_head *head, uint8_t table_id)
1.1.2.1 haad 145: {
1.1.2.10! haad 146: struct dm_table *tbl;
1.1.2.1 haad 147: struct dm_table_entry *table_en;
1.1.2.10! haad 148: uint8_t id;
1.1.2.1 haad 149:
1.1.2.10! haad 150: mutex_enter(&head->table_mtx);
! 151:
! 152: while (head->io_cnt != 0)
! 153: cv_wait(&head->table_cv, &head->table_mtx);
! 154:
! 155: if (table_id == DM_TABLE_ACTIVE)
! 156: id = head->cur_active_table;
! 157: else
! 158: id = 1 - head->cur_active_table;
! 159:
! 160: tbl = &head->tables[id];
! 161:
! 162: while (!SLIST_EMPTY(tbl)) { /* List Deletion. */
! 163: table_en = SLIST_FIRST(tbl);
1.1.2.6 haad 164: /*
165: * Remove target specific config data. After successfull
166: * call table_en->target_config must be set to NULL.
167: */
168: table_en->target->destroy(table_en);
1.1.2.10! haad 169:
! 170: SLIST_REMOVE_HEAD(tbl, next);
1.1.2.6 haad 171:
1.1.2.2 haad 172: kmem_free(table_en, sizeof(*table_en));
1.1.2.1 haad 173: }
174:
1.1.2.10! haad 175: mutex_exit(&head->table_mtx);
! 176:
1.1.2.1 haad 177: return 0;
178: }
1.1.2.10! haad 179:
! 180: /*
! 181: * Return length of active table in device.
! 182: */
! 183: uint64_t
! 184: dm_table_size(struct dm_table_head *head)
! 185: {
! 186: struct dm_table *tbl;
! 187: struct dm_table_entry *table_en;
! 188: uint64_t length;
! 189: uint8_t id;
! 190:
! 191: length = 0;
! 192:
! 193: id = dm_table_busy(head, DM_TABLE_ACTIVE);
! 194:
! 195: /* Select active table */
! 196: tbl = &head->tables[id];
! 197:
! 198: /*
! 199: * Find out what tables I want to select.
! 200: * if length => rawblkno then we should used that table.
! 201: */
! 202: SLIST_FOREACH(table_en, tbl, next)
! 203: length += table_en->length;
! 204:
! 205: dm_table_unbusy(head);
! 206:
! 207: return length / DEV_BSIZE;
! 208: }
! 209:
! 210: /*
! 211: * Return > 0 if table is at least one table entry (returns number of entries)
! 212: * and return 0 if there is not. Target count returned from this function
! 213: * doesn't need to be true when userspace user receive it (after return
! 214: * there can be dm_dev_resume_ioctl), therfore this isonly informative.
! 215: */
! 216: int
! 217: dm_table_get_target_count(struct dm_table_head *head, uint8_t table_id)
! 218: {
! 219: struct dm_table_entry *table_en;
! 220: struct dm_table *tbl;
! 221: uint32_t target_count;
! 222: uint8_t id;
! 223:
! 224: target_count = 0;
! 225:
! 226: id = dm_table_busy(head, table_id);
! 227:
! 228: tbl = &head->tables[id];
! 229:
! 230: SLIST_FOREACH(table_en, tbl, next)
! 231: target_count++;
! 232:
! 233: dm_table_unbusy(head);
! 234:
! 235: return target_count;
! 236: }
! 237:
! 238:
! 239: /*
! 240: * Initialize table_head structures, I'm trying to keep this structure as
! 241: * opaque as possible.
! 242: */
! 243: void
! 244: dm_table_head_init(struct dm_table_head *head)
! 245: {
! 246: head->cur_active_table = 0;
! 247: head->io_cnt = 0;
! 248:
! 249: /* Initialize tables. */
! 250: SLIST_INIT(&head->tables[0]);
! 251: SLIST_INIT(&head->tables[1]);
! 252:
! 253: mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE);
! 254: cv_init(&head->table_cv, "dm_io");
! 255: }
! 256:
! 257: /*
! 258: * Destroy all variables in table_head
! 259: */
! 260: void
! 261: dm_table_head_destroy(struct dm_table_head *head)
! 262: {
! 263: KASSERT(!mutex_owned(&head->table_mtx));
! 264: KASSERT(!cv_has_waiters(&head->table_cv));
! 265: /* tables doens't exists when I call this routine, therefore
! 266: it doesn't make sense to have io_cnt != 0 */
! 267: KASSERT(head->io_cnt == 0);
! 268:
! 269: cv_destroy(&head->table_cv);
! 270: mutex_destroy(&head->table_mtx);
! 271: }
CVSweb <webmaster@jp.NetBSD.org>