dm_table.c revision 1.2.8.4 1 1.2.8.4 yamt /* $NetBSD: dm_table.c,v 1.2.8.4 2010/03/11 15:03:26 yamt Exp $ */
2 1.2.8.2 yamt
3 1.2.8.2 yamt /*
4 1.2.8.2 yamt * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.2.8.2 yamt * All rights reserved.
6 1.2.8.2 yamt *
7 1.2.8.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.8.2 yamt * by Adam Hamsik.
9 1.2.8.2 yamt *
10 1.2.8.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.8.2 yamt * modification, are permitted provided that the following conditions
12 1.2.8.2 yamt * are met:
13 1.2.8.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.8.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.8.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.8.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.8.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.8.2 yamt *
19 1.2.8.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.8.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.8.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.8.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.8.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.8.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.8.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.8.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.8.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.8.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.8.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.2.8.2 yamt */
31 1.2.8.2 yamt
32 1.2.8.2 yamt #include <sys/types.h>
33 1.2.8.2 yamt #include <sys/param.h>
34 1.2.8.2 yamt
35 1.2.8.2 yamt #include <sys/kmem.h>
36 1.2.8.2 yamt
37 1.2.8.2 yamt #include "dm.h"
38 1.2.8.2 yamt
39 1.2.8.2 yamt /*
40 1.2.8.2 yamt * There are two types of users of this interface:
41 1.2.8.4 yamt *
42 1.2.8.2 yamt * a) Readers such as
43 1.2.8.2 yamt * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
44 1.2.8.2 yamt * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl
45 1.2.8.2 yamt *
46 1.2.8.2 yamt * b) Writers such as
47 1.2.8.2 yamt * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl
48 1.2.8.2 yamt *
49 1.2.8.2 yamt * Writers can work with table_head only when there are no readers. I
50 1.2.8.2 yamt * use reference counting on io_cnt.
51 1.2.8.2 yamt *
52 1.2.8.2 yamt */
53 1.2.8.2 yamt
54 1.2.8.4 yamt static int dm_table_busy(dm_table_head_t *, uint8_t);
55 1.2.8.2 yamt static void dm_table_unbusy(dm_table_head_t *);
56 1.2.8.2 yamt
57 1.2.8.2 yamt /*
58 1.2.8.2 yamt * Function to increment table user reference counter. Return id
59 1.2.8.2 yamt * of table_id table.
60 1.2.8.2 yamt * DM_TABLE_ACTIVE will return active table id.
61 1.2.8.2 yamt * DM_TABLE_INACTIVE will return inactive table id.
62 1.2.8.2 yamt */
63 1.2.8.2 yamt static int
64 1.2.8.4 yamt dm_table_busy(dm_table_head_t * head, uint8_t table_id)
65 1.2.8.2 yamt {
66 1.2.8.2 yamt uint8_t id;
67 1.2.8.2 yamt
68 1.2.8.2 yamt id = 0;
69 1.2.8.2 yamt
70 1.2.8.2 yamt mutex_enter(&head->table_mtx);
71 1.2.8.2 yamt
72 1.2.8.2 yamt if (table_id == DM_TABLE_ACTIVE)
73 1.2.8.2 yamt id = head->cur_active_table;
74 1.2.8.2 yamt else
75 1.2.8.2 yamt id = 1 - head->cur_active_table;
76 1.2.8.2 yamt
77 1.2.8.2 yamt head->io_cnt++;
78 1.2.8.2 yamt
79 1.2.8.2 yamt mutex_exit(&head->table_mtx);
80 1.2.8.2 yamt return id;
81 1.2.8.2 yamt }
82 1.2.8.2 yamt /*
83 1.2.8.2 yamt * Function release table lock and eventually wakeup all waiters.
84 1.2.8.2 yamt */
85 1.2.8.2 yamt static void
86 1.2.8.4 yamt dm_table_unbusy(dm_table_head_t * head)
87 1.2.8.2 yamt {
88 1.2.8.2 yamt KASSERT(head->io_cnt != 0);
89 1.2.8.2 yamt
90 1.2.8.2 yamt mutex_enter(&head->table_mtx);
91 1.2.8.4 yamt
92 1.2.8.2 yamt if (--head->io_cnt == 0)
93 1.2.8.2 yamt cv_broadcast(&head->table_cv);
94 1.2.8.4 yamt
95 1.2.8.2 yamt mutex_exit(&head->table_mtx);
96 1.2.8.2 yamt }
97 1.2.8.2 yamt /*
98 1.2.8.2 yamt * Return current active table to caller, increment io_cnt reference counter.
99 1.2.8.2 yamt */
100 1.2.8.2 yamt dm_table_t *
101 1.2.8.4 yamt dm_table_get_entry(dm_table_head_t * head, uint8_t table_id)
102 1.2.8.2 yamt {
103 1.2.8.2 yamt uint8_t id;
104 1.2.8.2 yamt
105 1.2.8.2 yamt id = dm_table_busy(head, table_id);
106 1.2.8.4 yamt
107 1.2.8.2 yamt return &head->tables[id];
108 1.2.8.2 yamt }
109 1.2.8.2 yamt /*
110 1.2.8.2 yamt * Decrement io reference counter and wake up all callers, with table_head cv.
111 1.2.8.2 yamt */
112 1.2.8.2 yamt void
113 1.2.8.4 yamt dm_table_release(dm_table_head_t * head, uint8_t table_id)
114 1.2.8.2 yamt {
115 1.2.8.2 yamt dm_table_unbusy(head);
116 1.2.8.2 yamt }
117 1.2.8.2 yamt /*
118 1.2.8.2 yamt * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
119 1.2.8.2 yamt */
120 1.2.8.2 yamt void
121 1.2.8.4 yamt dm_table_switch_tables(dm_table_head_t * head)
122 1.2.8.2 yamt {
123 1.2.8.2 yamt mutex_enter(&head->table_mtx);
124 1.2.8.2 yamt
125 1.2.8.2 yamt while (head->io_cnt != 0)
126 1.2.8.2 yamt cv_wait(&head->table_cv, &head->table_mtx);
127 1.2.8.2 yamt
128 1.2.8.2 yamt head->cur_active_table = 1 - head->cur_active_table;
129 1.2.8.2 yamt
130 1.2.8.2 yamt mutex_exit(&head->table_mtx);
131 1.2.8.2 yamt }
132 1.2.8.2 yamt /*
133 1.2.8.2 yamt * Destroy all table data. This function can run when there are no
134 1.2.8.2 yamt * readers on table lists.
135 1.2.8.2 yamt *
136 1.2.8.2 yamt * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs
137 1.2.8.2 yamt */
138 1.2.8.2 yamt int
139 1.2.8.4 yamt dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
140 1.2.8.2 yamt {
141 1.2.8.4 yamt dm_table_t *tbl;
142 1.2.8.2 yamt dm_table_entry_t *table_en;
143 1.2.8.2 yamt uint8_t id;
144 1.2.8.2 yamt
145 1.2.8.2 yamt mutex_enter(&head->table_mtx);
146 1.2.8.2 yamt
147 1.2.8.4 yamt aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
148 1.2.8.4 yamt
149 1.2.8.2 yamt while (head->io_cnt != 0)
150 1.2.8.2 yamt cv_wait(&head->table_cv, &head->table_mtx);
151 1.2.8.2 yamt
152 1.2.8.2 yamt if (table_id == DM_TABLE_ACTIVE)
153 1.2.8.2 yamt id = head->cur_active_table;
154 1.2.8.2 yamt else
155 1.2.8.2 yamt id = 1 - head->cur_active_table;
156 1.2.8.4 yamt
157 1.2.8.2 yamt tbl = &head->tables[id];
158 1.2.8.4 yamt
159 1.2.8.4 yamt while (!SLIST_EMPTY(tbl)) { /* List Deletion. */
160 1.2.8.2 yamt table_en = SLIST_FIRST(tbl);
161 1.2.8.2 yamt /*
162 1.2.8.2 yamt * Remove target specific config data. After successfull
163 1.2.8.2 yamt * call table_en->target_config must be set to NULL.
164 1.2.8.2 yamt */
165 1.2.8.4 yamt table_en->target->destroy(table_en);
166 1.2.8.4 yamt
167 1.2.8.2 yamt SLIST_REMOVE_HEAD(tbl, next);
168 1.2.8.2 yamt
169 1.2.8.2 yamt kmem_free(table_en, sizeof(*table_en));
170 1.2.8.2 yamt }
171 1.2.8.2 yamt
172 1.2.8.2 yamt mutex_exit(&head->table_mtx);
173 1.2.8.4 yamt
174 1.2.8.2 yamt return 0;
175 1.2.8.2 yamt }
176 1.2.8.2 yamt /*
177 1.2.8.2 yamt * Return length of active table in device.
178 1.2.8.2 yamt */
179 1.2.8.2 yamt uint64_t
180 1.2.8.4 yamt dm_table_size(dm_table_head_t * head)
181 1.2.8.2 yamt {
182 1.2.8.4 yamt dm_table_t *tbl;
183 1.2.8.2 yamt dm_table_entry_t *table_en;
184 1.2.8.2 yamt uint64_t length;
185 1.2.8.2 yamt uint8_t id;
186 1.2.8.4 yamt
187 1.2.8.2 yamt length = 0;
188 1.2.8.2 yamt
189 1.2.8.2 yamt id = dm_table_busy(head, DM_TABLE_ACTIVE);
190 1.2.8.4 yamt
191 1.2.8.2 yamt /* Select active table */
192 1.2.8.2 yamt tbl = &head->tables[id];
193 1.2.8.4 yamt
194 1.2.8.2 yamt /*
195 1.2.8.2 yamt * Find out what tables I want to select.
196 1.2.8.2 yamt * if length => rawblkno then we should used that table.
197 1.2.8.2 yamt */
198 1.2.8.2 yamt SLIST_FOREACH(table_en, tbl, next)
199 1.2.8.2 yamt length += table_en->length;
200 1.2.8.2 yamt
201 1.2.8.2 yamt dm_table_unbusy(head);
202 1.2.8.4 yamt
203 1.2.8.3 yamt return length;
204 1.2.8.2 yamt }
205 1.2.8.2 yamt /*
206 1.2.8.2 yamt * Return > 0 if table is at least one table entry (returns number of entries)
207 1.2.8.2 yamt * and return 0 if there is not. Target count returned from this function
208 1.2.8.2 yamt * doesn't need to be true when userspace user receive it (after return
209 1.2.8.2 yamt * there can be dm_dev_resume_ioctl), therfore this isonly informative.
210 1.2.8.2 yamt */
211 1.2.8.2 yamt int
212 1.2.8.4 yamt dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
213 1.2.8.4 yamt {
214 1.2.8.2 yamt dm_table_entry_t *table_en;
215 1.2.8.2 yamt dm_table_t *tbl;
216 1.2.8.2 yamt uint32_t target_count;
217 1.2.8.2 yamt uint8_t id;
218 1.2.8.2 yamt
219 1.2.8.2 yamt target_count = 0;
220 1.2.8.2 yamt
221 1.2.8.2 yamt id = dm_table_busy(head, table_id);
222 1.2.8.2 yamt
223 1.2.8.2 yamt tbl = &head->tables[id];
224 1.2.8.2 yamt
225 1.2.8.2 yamt SLIST_FOREACH(table_en, tbl, next)
226 1.2.8.2 yamt target_count++;
227 1.2.8.2 yamt
228 1.2.8.2 yamt dm_table_unbusy(head);
229 1.2.8.4 yamt
230 1.2.8.2 yamt return target_count;
231 1.2.8.2 yamt }
232 1.2.8.2 yamt
233 1.2.8.2 yamt
234 1.2.8.2 yamt /*
235 1.2.8.2 yamt * Initialize table_head structures, I'm trying to keep this structure as
236 1.2.8.2 yamt * opaque as possible.
237 1.2.8.2 yamt */
238 1.2.8.2 yamt void
239 1.2.8.4 yamt dm_table_head_init(dm_table_head_t * head)
240 1.2.8.2 yamt {
241 1.2.8.2 yamt head->cur_active_table = 0;
242 1.2.8.2 yamt head->io_cnt = 0;
243 1.2.8.4 yamt
244 1.2.8.2 yamt /* Initialize tables. */
245 1.2.8.2 yamt SLIST_INIT(&head->tables[0]);
246 1.2.8.2 yamt SLIST_INIT(&head->tables[1]);
247 1.2.8.2 yamt
248 1.2.8.2 yamt mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE);
249 1.2.8.2 yamt cv_init(&head->table_cv, "dm_io");
250 1.2.8.2 yamt }
251 1.2.8.2 yamt /*
252 1.2.8.2 yamt * Destroy all variables in table_head
253 1.2.8.2 yamt */
254 1.2.8.2 yamt void
255 1.2.8.4 yamt dm_table_head_destroy(dm_table_head_t * head)
256 1.2.8.2 yamt {
257 1.2.8.2 yamt KASSERT(!mutex_owned(&head->table_mtx));
258 1.2.8.2 yamt KASSERT(!cv_has_waiters(&head->table_cv));
259 1.2.8.4 yamt /* tables doens't exists when I call this routine, therefore it
260 1.2.8.4 yamt * doesn't make sense to have io_cnt != 0 */
261 1.2.8.4 yamt KASSERT(head->io_cnt == 0);
262 1.2.8.4 yamt
263 1.2.8.2 yamt cv_destroy(&head->table_cv);
264 1.2.8.2 yamt mutex_destroy(&head->table_mtx);
265 1.2.8.2 yamt }
266