xref: /dflybsd-src/sys/dev/disk/dm/dm_table.c (revision f41d807a0c7c535d8f66f0593fb6e95fa20f82d4)
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 
155 		SLIST_REMOVE_HEAD(tbl, next);
156 
157 		kfree(table_en, M_DM);
158 	}
159 
160 	lockmgr(&head->table_mtx, LK_RELEASE);
161 
162 	return 0;
163 }
164 /*
165  * Return length of active table in device.
166  */
167 uint64_t
168 dm_table_size(dm_table_head_t * head)
169 {
170 	dm_table_t *tbl;
171 	dm_table_entry_t *table_en;
172 	uint64_t length;
173 	uint8_t id;
174 
175 	length = 0;
176 
177 	id = dm_table_busy(head, DM_TABLE_ACTIVE);
178 
179 	/* Select active table */
180 	tbl = &head->tables[id];
181 
182 	/*
183 	 * Find out what tables I want to select.
184 	 * if length => rawblkno then we should used that table.
185 	 */
186 	SLIST_FOREACH(table_en, tbl, next) {
187 		length += table_en->length;
188 	}
189 
190 	dm_table_unbusy(head);
191 
192 	return length;
193 }
194 /*
195  * Return > 0 if table is at least one table entry (returns number of entries)
196  * and return 0 if there is not. Target count returned from this function
197  * doesn't need to be true when userspace user receive it (after return
198  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
199  */
200 int
201 dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
202 {
203 	dm_table_entry_t *table_en;
204 	dm_table_t *tbl;
205 	uint32_t target_count;
206 	uint8_t id;
207 
208 	target_count = 0;
209 
210 	id = dm_table_busy(head, table_id);
211 
212 	tbl = &head->tables[id];
213 
214 	SLIST_FOREACH(table_en, tbl, next)
215 	    target_count++;
216 
217 	dm_table_unbusy(head);
218 
219 	return target_count;
220 }
221 
222 
223 /*
224  * Initialize table_head structures, I'm trying to keep this structure as
225  * opaque as possible.
226  */
227 void
228 dm_table_head_init(dm_table_head_t * head)
229 {
230 	head->cur_active_table = 0;
231 	head->io_cnt = 0;
232 
233 	/* Initialize tables. */
234 	SLIST_INIT(&head->tables[0]);
235 	SLIST_INIT(&head->tables[1]);
236 
237 	lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
238 }
239 /*
240  * Destroy all variables in table_head
241  */
242 void
243 dm_table_head_destroy(dm_table_head_t * head)
244 {
245 	KKASSERT(lockcount(&head->table_mtx) == 0);
246 
247 	/* tables doens't exists when I call this routine, therefore it
248 	 * doesn't make sense to have io_cnt != 0 */
249 	KKASSERT(head->io_cnt == 0);
250 
251 	lockuninit(&head->table_mtx);
252 }
253