xref: /dflybsd-src/sys/dev/disk/dm/dm_table.c (revision 8bd3d23cacb2ee28cb21ee3d50f68e868fa018cc)
1 /*        $NetBSD: dm_table.c,v 1.5 2010/01/04 00:19:08 haad 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 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 
35 #include <sys/malloc.h>
36 
37 #include "dm.h"
38 
39 MALLOC_DECLARE(M_DM);
40 
41 /*
42  * There are two types of users of this interface:
43  *
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 
56 static int dm_table_busy(dm_table_head_t *, uint8_t);
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
66 dm_table_busy(dm_table_head_t * head, uint8_t table_id)
67 {
68 	uint8_t id;
69 
70 	id = 0;
71 
72 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
73 
74 	if (table_id == DM_TABLE_ACTIVE)
75 		id = head->cur_active_table;
76 	else
77 		id = 1 - head->cur_active_table;
78 
79 	head->io_cnt++;
80 
81 	lockmgr(&head->table_mtx, LK_RELEASE);
82 	return id;
83 }
84 /*
85  * Function release table lock and eventually wakeup all waiters.
86  */
87 static void
88 dm_table_unbusy(dm_table_head_t * head)
89 {
90 	KKASSERT(head->io_cnt != 0);
91 
92 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
93 
94 	if (--head->io_cnt == 0)
95 		cv_broadcast(&head->table_cv);
96 
97 	lockmgr(&head->table_mtx, LK_RELEASE);
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 	dm_table_unbusy(head);
118 }
119 /*
120  * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
121  */
122 void
123 dm_table_switch_tables(dm_table_head_t * head)
124 {
125 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
126 
127 	while (head->io_cnt != 0)
128 		cv_wait(&head->table_cv, &head->table_mtx);
129 
130 	head->cur_active_table = 1 - head->cur_active_table;
131 
132 	lockmgr(&head->table_mtx, LK_RELEASE);
133 }
134 /*
135  * Destroy all table data. This function can run when there are no
136  * readers on table lists.
137  *
138  * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs
139  */
140 int
141 dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
142 {
143 	dm_table_t *tbl;
144 	dm_table_entry_t *table_en;
145 	uint8_t id;
146 
147 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
148 
149 	aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
150 
151 	while (head->io_cnt != 0)
152 		cv_wait(&head->table_cv, &head->table_mtx);
153 
154 	if (table_id == DM_TABLE_ACTIVE)
155 		id = head->cur_active_table;
156 	else
157 		id = 1 - head->cur_active_table;
158 
159 	tbl = &head->tables[id];
160 
161 	while (!SLIST_EMPTY(tbl)) {	/* List Deletion. */
162 		table_en = SLIST_FIRST(tbl);
163 		/*
164 		 * Remove target specific config data. After successfull
165 		 * call table_en->target_config must be set to NULL.
166 		 */
167 		table_en->target->destroy(table_en);
168 
169 		SLIST_REMOVE_HEAD(tbl, next);
170 
171 		kfree(table_en, M_DM);
172 	}
173 
174 	lockmgr(&head->table_mtx, LK_RELEASE);
175 
176 	return 0;
177 }
178 /*
179  * Return length of active table in device.
180  */
181 uint64_t
182 dm_table_size(dm_table_head_t * head)
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, DM_TABLE_ACTIVE);
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 
204 	dm_table_unbusy(head);
205 
206 	return length;
207 }
208 /*
209  * Return > 0 if table is at least one table entry (returns number of entries)
210  * and return 0 if there is not. Target count returned from this function
211  * doesn't need to be true when userspace user receive it (after return
212  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
213  */
214 int
215 dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
216 {
217 	dm_table_entry_t *table_en;
218 	dm_table_t *tbl;
219 	uint32_t target_count;
220 	uint8_t id;
221 
222 	target_count = 0;
223 
224 	id = dm_table_busy(head, table_id);
225 
226 	tbl = &head->tables[id];
227 
228 	SLIST_FOREACH(table_en, tbl, next)
229 	    target_count++;
230 
231 	dm_table_unbusy(head);
232 
233 	return target_count;
234 }
235 
236 
237 /*
238  * Initialize table_head structures, I'm trying to keep this structure as
239  * opaque as possible.
240  */
241 void
242 dm_table_head_init(dm_table_head_t * head)
243 {
244 	head->cur_active_table = 0;
245 	head->io_cnt = 0;
246 
247 	/* Initialize tables. */
248 	SLIST_INIT(&head->tables[0]);
249 	SLIST_INIT(&head->tables[1]);
250 
251 	lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
252 	cv_init(&head->table_cv, "dm_io");
253 }
254 /*
255  * Destroy all variables in table_head
256  */
257 void
258 dm_table_head_destroy(dm_table_head_t * head)
259 {
260 	KKASSERT(lockcount(&head->table_mtx) == 0);
261 #if 0
262 	KKASSERT(!cv_has_waiters(&head->table_cv));
263 #endif
264 	/* tables doens't exists when I call this routine, therefore it
265 	 * doesn't make sense to have io_cnt != 0 */
266 	KKASSERT(head->io_cnt == 0);
267 
268 	cv_destroy(&head->table_cv);
269 	lockuninit(&head->table_mtx);
270 }
271