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