Annotation of src/sys/dev/dm/dm_table.c, revision 1.9
1.9 ! tkusumi 1: /* $NetBSD: dm_table.c,v 1.8 2018/01/05 14:22:26 christos Exp $ */
1.2 haad 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: */
1.8 christos 31: #include <sys/cdefs.h>
1.9 ! tkusumi 32: __KERNEL_RCSID(0, "$NetBSD: dm_table.c,v 1.8 2018/01/05 14:22:26 christos Exp $");
1.2 haad 33:
34: #include <sys/types.h>
35: #include <sys/param.h>
36:
37: #include <sys/kmem.h>
38:
39: #include "dm.h"
40:
41: /*
42: * There are two types of users of this interface:
1.5 haad 43: *
1.2 haad 44: * a) Readers such as
45: * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
46: * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl
47: *
48: * b) Writers such as
49: * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl
50: *
51: * Writers can work with table_head only when there are no readers. I
52: * use reference counting on io_cnt.
53: *
54: */
55:
1.5 haad 56: static int dm_table_busy(dm_table_head_t *, uint8_t);
1.2 haad 57: static void dm_table_unbusy(dm_table_head_t *);
58:
59: /*
60: * Function to increment table user reference counter. Return id
61: * of table_id table.
62: * DM_TABLE_ACTIVE will return active table id.
63: * DM_TABLE_INACTIVE will return inactive table id.
64: */
65: static int
1.5 haad 66: dm_table_busy(dm_table_head_t * head, uint8_t table_id)
1.2 haad 67: {
68: uint8_t id;
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: }
1.8 christos 82:
1.2 haad 83: /*
84: * Function release table lock and eventually wakeup all waiters.
85: */
86: static void
1.5 haad 87: dm_table_unbusy(dm_table_head_t * head)
1.2 haad 88: {
89: KASSERT(head->io_cnt != 0);
90:
91: mutex_enter(&head->table_mtx);
1.5 haad 92:
1.2 haad 93: if (--head->io_cnt == 0)
94: cv_broadcast(&head->table_cv);
1.5 haad 95:
1.2 haad 96: mutex_exit(&head->table_mtx);
97: }
1.8 christos 98:
1.2 haad 99: /*
100: * Return current active table to caller, increment io_cnt reference counter.
101: */
102: dm_table_t *
1.5 haad 103: dm_table_get_entry(dm_table_head_t * head, uint8_t table_id)
1.2 haad 104: {
105: uint8_t id;
106:
107: id = dm_table_busy(head, table_id);
1.5 haad 108:
1.2 haad 109: return &head->tables[id];
110: }
111: /*
112: * Decrement io reference counter and wake up all callers, with table_head cv.
113: */
114: void
1.5 haad 115: dm_table_release(dm_table_head_t * head, uint8_t table_id)
1.2 haad 116: {
117: dm_table_unbusy(head);
118: }
1.8 christos 119:
1.2 haad 120: /*
121: * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
122: */
123: void
1.5 haad 124: dm_table_switch_tables(dm_table_head_t * head)
1.2 haad 125: {
126: mutex_enter(&head->table_mtx);
127:
128: while (head->io_cnt != 0)
129: cv_wait(&head->table_cv, &head->table_mtx);
130:
131: head->cur_active_table = 1 - head->cur_active_table;
132:
133: mutex_exit(&head->table_mtx);
134: }
1.8 christos 135:
1.2 haad 136: /*
137: * Destroy all table data. This function can run when there are no
138: * readers on table lists.
139: *
140: * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs
141: */
142: int
1.5 haad 143: dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
1.2 haad 144: {
1.5 haad 145: dm_table_t *tbl;
1.2 haad 146: dm_table_entry_t *table_en;
147: uint8_t id;
148:
149: mutex_enter(&head->table_mtx);
150:
1.4 joerg 151: aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
152:
1.2 haad 153: while (head->io_cnt != 0)
154: cv_wait(&head->table_cv, &head->table_mtx);
155:
156: if (table_id == DM_TABLE_ACTIVE)
157: id = head->cur_active_table;
158: else
159: id = 1 - head->cur_active_table;
1.5 haad 160:
1.2 haad 161: tbl = &head->tables[id];
1.5 haad 162:
163: while (!SLIST_EMPTY(tbl)) { /* List Deletion. */
1.2 haad 164: table_en = SLIST_FIRST(tbl);
165: /*
166: * Remove target specific config data. After successfull
167: * call table_en->target_config must be set to NULL.
168: */
1.5 haad 169: table_en->target->destroy(table_en);
170:
1.2 haad 171: SLIST_REMOVE_HEAD(tbl, next);
172:
173: kmem_free(table_en, sizeof(*table_en));
174: }
175:
176: mutex_exit(&head->table_mtx);
1.5 haad 177:
1.2 haad 178: return 0;
179: }
1.8 christos 180:
1.2 haad 181: /*
182: * Return length of active table in device.
183: */
1.7 ahoka 184: static inline uint64_t
185: dm_table_size_impl(dm_table_head_t * head, int table)
1.2 haad 186: {
1.5 haad 187: dm_table_t *tbl;
1.2 haad 188: dm_table_entry_t *table_en;
189: uint64_t length;
190: uint8_t id;
1.5 haad 191:
1.2 haad 192: length = 0;
193:
1.7 ahoka 194: id = dm_table_busy(head, table);
1.5 haad 195:
1.2 haad 196: /* Select active table */
197: tbl = &head->tables[id];
1.5 haad 198:
1.2 haad 199: /*
200: * Find out what tables I want to select.
201: * if length => rawblkno then we should used that table.
202: */
203: SLIST_FOREACH(table_en, tbl, next)
1.9 ! tkusumi 204: length += table_en->length;
1.2 haad 205:
206: dm_table_unbusy(head);
1.5 haad 207:
1.3 jakllsch 208: return length;
1.2 haad 209: }
1.7 ahoka 210:
211: /*
212: * Return length of active table in device.
213: */
214: uint64_t
215: dm_table_size(dm_table_head_t * head)
216: {
217: return dm_table_size_impl(head, DM_TABLE_ACTIVE);
218: }
219:
220: /*
221: * Return length of active table in device.
222: */
223: uint64_t
224: dm_inactive_table_size(dm_table_head_t * head)
225: {
226: return dm_table_size_impl(head, DM_TABLE_INACTIVE);
227: }
228:
1.2 haad 229: /*
1.6 mlelstv 230: * Return combined disk geometry
231: */
232: void
233: dm_table_disksize(dm_table_head_t * head, uint64_t *numsecp, unsigned *secsizep)
234: {
235: dm_table_t *tbl;
236: dm_table_entry_t *table_en;
237: uint64_t length;
238: unsigned secsize, tsecsize;
239: uint8_t id;
240:
241: length = 0;
242:
243: id = dm_table_busy(head, DM_TABLE_ACTIVE);
244:
245: /* Select active table */
246: tbl = &head->tables[id];
247:
248: /*
249: * Find out what tables I want to select.
250: * if length => rawblkno then we should used that table.
251: */
252: secsize = 0;
253: SLIST_FOREACH(table_en, tbl, next) {
1.9 ! tkusumi 254: length += table_en->length;
! 255: (void)table_en->target->secsize(table_en, &tsecsize);
! 256: if (secsize < tsecsize)
! 257: secsize = tsecsize;
1.6 mlelstv 258: }
259: *numsecp = secsize > 0 ? dbtob(length) / secsize : 0;
260: *secsizep = secsize;
261:
262: dm_table_unbusy(head);
263: }
1.8 christos 264:
1.6 mlelstv 265: /*
1.2 haad 266: * Return > 0 if table is at least one table entry (returns number of entries)
267: * and return 0 if there is not. Target count returned from this function
268: * doesn't need to be true when userspace user receive it (after return
269: * there can be dm_dev_resume_ioctl), therfore this isonly informative.
270: */
271: int
1.5 haad 272: dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
273: {
1.2 haad 274: dm_table_entry_t *table_en;
275: dm_table_t *tbl;
276: uint32_t target_count;
277: uint8_t id;
278:
279: target_count = 0;
280:
281: id = dm_table_busy(head, table_id);
282:
283: tbl = &head->tables[id];
284:
285: SLIST_FOREACH(table_en, tbl, next)
1.9 ! tkusumi 286: target_count++;
1.2 haad 287:
288: dm_table_unbusy(head);
1.5 haad 289:
1.2 haad 290: return target_count;
291: }
292:
293:
294: /*
295: * Initialize table_head structures, I'm trying to keep this structure as
296: * opaque as possible.
297: */
298: void
1.5 haad 299: dm_table_head_init(dm_table_head_t * head)
1.2 haad 300: {
301: head->cur_active_table = 0;
302: head->io_cnt = 0;
1.5 haad 303:
1.2 haad 304: /* Initialize tables. */
305: SLIST_INIT(&head->tables[0]);
306: SLIST_INIT(&head->tables[1]);
307:
308: mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE);
309: cv_init(&head->table_cv, "dm_io");
310: }
1.8 christos 311:
1.2 haad 312: /*
313: * Destroy all variables in table_head
314: */
315: void
1.5 haad 316: dm_table_head_destroy(dm_table_head_t * head)
1.2 haad 317: {
318: KASSERT(!mutex_owned(&head->table_mtx));
319: KASSERT(!cv_has_waiters(&head->table_cv));
1.5 haad 320: /* tables doens't exists when I call this routine, therefore it
321: * doesn't make sense to have io_cnt != 0 */
322: KASSERT(head->io_cnt == 0);
323:
1.2 haad 324: cv_destroy(&head->table_cv);
325: mutex_destroy(&head->table_mtx);
326: }
CVSweb <webmaster@jp.NetBSD.org>