xref: /dragonfly/sys/dev/disk/dm/dm_table.c (revision d4ef6694)
1 /*        $NetBSD: dm_table.c,v 1.5 2010/01/04 00:19:08 haad Exp $      */
2 
3 /*
4  * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Adam Hamsik.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 
36 #include <sys/malloc.h>
37 
38 #include <dev/disk/dm/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. We
51  * simply use shared/exclusive locking to ensure this.
52  */
53 
54 static int dm_table_busy(dm_table_head_t *, uint8_t);
55 static void dm_table_unbusy(dm_table_head_t *);
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(dm_table_head_t * head, uint8_t table_id)
65 {
66 	uint8_t id;
67 
68 	id = 0;
69 
70 	lockmgr(&head->table_mtx, LK_SHARED);
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 	atomic_add_int(&head->io_cnt, 1);
78 
79 	return id;
80 }
81 /*
82  * Function release table lock and eventually wakeup all waiters.
83  */
84 static void
85 dm_table_unbusy(dm_table_head_t * head)
86 {
87 	KKASSERT(head->io_cnt != 0);
88 
89 	atomic_subtract_int(&head->io_cnt, 1);
90 
91 	lockmgr(&head->table_mtx, LK_RELEASE);
92 }
93 /*
94  * Return current active table to caller, increment io_cnt reference counter.
95  */
96 dm_table_t *
97 dm_table_get_entry(dm_table_head_t * head, uint8_t table_id)
98 {
99 	uint8_t id;
100 
101 	id = dm_table_busy(head, table_id);
102 
103 	return &head->tables[id];
104 }
105 /*
106  * Decrement io reference counter and release shared lock.
107  */
108 void
109 dm_table_release(dm_table_head_t * head, uint8_t table_id)
110 {
111 	dm_table_unbusy(head);
112 }
113 /*
114  * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
115  */
116 void
117 dm_table_switch_tables(dm_table_head_t * head)
118 {
119 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
120 
121 	head->cur_active_table = 1 - head->cur_active_table;
122 
123 	lockmgr(&head->table_mtx, LK_RELEASE);
124 }
125 /*
126  * Destroy all table data. This function can run when there are no
127  * readers on table lists.
128  */
129 int
130 dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
131 {
132 	dm_table_t *tbl;
133 	dm_table_entry_t *table_en;
134 	uint8_t id;
135 
136 	lockmgr(&head->table_mtx, LK_EXCLUSIVE);
137 
138 	aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
139 
140 	if (table_id == DM_TABLE_ACTIVE)
141 		id = head->cur_active_table;
142 	else
143 		id = 1 - head->cur_active_table;
144 
145 	tbl = &head->tables[id];
146 
147 	while (!SLIST_EMPTY(tbl)) {	/* List Deletion. */
148 		table_en = SLIST_FIRST(tbl);
149 		/*
150 		 * Remove target specific config data. After successfull
151 		 * call table_en->target_config must be set to NULL.
152 		 */
153 		table_en->target->destroy(table_en);
154 		/* decrement the refcount for the target */
155 		dm_target_unbusy(table_en->target);
156 
157 		SLIST_REMOVE_HEAD(tbl, next);
158 
159 		kfree(table_en, M_DM);
160 	}
161 
162 	lockmgr(&head->table_mtx, LK_RELEASE);
163 
164 	return 0;
165 }
166 /*
167  * Return length of active or inactive table in device.
168  */
169 static uint64_t
170 _dm_table_size(dm_table_head_t * head, int table)
171 {
172 	dm_table_t *tbl;
173 	dm_table_entry_t *table_en;
174 	uint64_t length;
175 	uint8_t id;
176 
177 	length = 0;
178 
179 	id = dm_table_busy(head, table);
180 
181 	/* Select active table */
182 	tbl = &head->tables[id];
183 
184 	/*
185 	 * Find out what tables I want to select.
186 	 * if length => rawblkno then we should used that table.
187 	 */
188 	SLIST_FOREACH(table_en, tbl, next) {
189 		length += table_en->length;
190 	}
191 
192 	dm_table_unbusy(head);
193 
194 	return length;
195 }
196 
197 uint64_t
198 dm_table_size(dm_table_head_t *head)
199 {
200 	return _dm_table_size(head, DM_TABLE_ACTIVE);
201 }
202 
203 uint64_t
204 dm_inactive_table_size(dm_table_head_t *head)
205 {
206 	return _dm_table_size(head, DM_TABLE_INACTIVE);
207 }
208 
209 /*
210  * Return > 0 if table is at least one table entry (returns number of entries)
211  * and return 0 if there is not. Target count returned from this function
212  * doesn't need to be true when userspace user receive it (after return
213  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
214  */
215 int
216 dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
217 {
218 	dm_table_entry_t *table_en;
219 	dm_table_t *tbl;
220 	uint32_t target_count;
221 	uint8_t id;
222 
223 	target_count = 0;
224 
225 	id = dm_table_busy(head, table_id);
226 
227 	tbl = &head->tables[id];
228 
229 	SLIST_FOREACH(table_en, tbl, next)
230 	    target_count++;
231 
232 	dm_table_unbusy(head);
233 
234 	return target_count;
235 }
236 
237 
238 /*
239  * Initialize table_head structures, I'm trying to keep this structure as
240  * opaque as possible.
241  */
242 void
243 dm_table_head_init(dm_table_head_t * head)
244 {
245 	head->cur_active_table = 0;
246 	head->io_cnt = 0;
247 
248 	/* Initialize tables. */
249 	SLIST_INIT(&head->tables[0]);
250 	SLIST_INIT(&head->tables[1]);
251 
252 	lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
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 
262 	/* tables doens't exists when I call this routine, therefore it
263 	 * doesn't make sense to have io_cnt != 0 */
264 	KKASSERT(head->io_cnt == 0);
265 
266 	lockuninit(&head->table_mtx);
267 }
268