3df7ae36c379ee0d37d8e2fe723ac338a1b384f0
[dragonfly.git] / sys / dev / disk / dm / dm_table.c
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 <dev/disk/dm/dm.h>
38
39 /*
40  * There are two types of users of this interface:
41  *
42  * a) Readers such as
43  *    dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
44  *    dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl
45  *
46  * b) Writers such as
47  *    dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl
48  *
49  * Writers can work with table_head only when there are no readers. We
50  * simply use shared/exclusive locking to ensure this.
51  */
52
53 static int dm_table_busy(dm_table_head_t *, uint8_t);
54 static void dm_table_unbusy(dm_table_head_t *);
55
56 /*
57  * Function to increment table user reference counter. Return id
58  * of table_id table.
59  * DM_TABLE_ACTIVE will return active table id.
60  * DM_TABLE_INACTIVE will return inactive table id.
61  */
62 static int
63 dm_table_busy(dm_table_head_t * head, uint8_t table_id)
64 {
65         uint8_t id;
66
67         id = 0;
68
69         lockmgr(&head->table_mtx, LK_SHARED);
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         atomic_add_int(&head->io_cnt, 1);
77
78         return id;
79 }
80 /*
81  * Function release table lock and eventually wakeup all waiters.
82  */
83 static void
84 dm_table_unbusy(dm_table_head_t * head)
85 {
86         KKASSERT(head->io_cnt != 0);
87
88         atomic_subtract_int(&head->io_cnt, 1);
89
90         lockmgr(&head->table_mtx, LK_RELEASE);
91 }
92 /*
93  * Return current active table to caller, increment io_cnt reference counter.
94  */
95 dm_table_t *
96 dm_table_get_entry(dm_table_head_t * head, uint8_t table_id)
97 {
98         uint8_t id;
99
100         id = dm_table_busy(head, table_id);
101
102         return &head->tables[id];
103 }
104 /*
105  * Decrement io reference counter and release shared lock.
106  */
107 void
108 dm_table_release(dm_table_head_t * head, uint8_t table_id)
109 {
110         dm_table_unbusy(head);
111 }
112 /*
113  * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
114  */
115 void
116 dm_table_switch_tables(dm_table_head_t * head)
117 {
118         lockmgr(&head->table_mtx, LK_EXCLUSIVE);
119
120         head->cur_active_table = 1 - head->cur_active_table;
121
122         lockmgr(&head->table_mtx, LK_RELEASE);
123 }
124 /*
125  * Destroy all table data. This function can run when there are no
126  * readers on table lists.
127  */
128 int
129 dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
130 {
131         dm_table_t *tbl;
132         dm_table_entry_t *table_en;
133         uint8_t id;
134
135         lockmgr(&head->table_mtx, LK_EXCLUSIVE);
136
137         aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
138
139         if (table_id == DM_TABLE_ACTIVE)
140                 id = head->cur_active_table;
141         else
142                 id = 1 - head->cur_active_table;
143
144         tbl = &head->tables[id];
145
146         while (!SLIST_EMPTY(tbl)) {     /* List Deletion. */
147                 table_en = SLIST_FIRST(tbl);
148                 /*
149                  * Remove target specific config data. After successfull
150                  * call table_en->target_config must be set to NULL.
151                  */
152                 table_en->target->destroy(table_en);
153
154                 SLIST_REMOVE_HEAD(tbl, next);
155
156                 kfree(table_en, M_DM);
157         }
158
159         lockmgr(&head->table_mtx, LK_RELEASE);
160
161         return 0;
162 }
163 /*
164  * Return length of active table in device.
165  */
166 uint64_t
167 dm_table_size(dm_table_head_t * head)
168 {
169         dm_table_t *tbl;
170         dm_table_entry_t *table_en;
171         uint64_t length;
172         uint8_t id;
173
174         length = 0;
175
176         id = dm_table_busy(head, DM_TABLE_ACTIVE);
177
178         /* Select active table */
179         tbl = &head->tables[id];
180
181         /*
182          * Find out what tables I want to select.
183          * if length => rawblkno then we should used that table.
184          */
185         SLIST_FOREACH(table_en, tbl, next) {
186                 length += table_en->length;
187         }
188
189         dm_table_unbusy(head);
190
191         return length;
192 }
193 /*
194  * Return > 0 if table is at least one table entry (returns number of entries)
195  * and return 0 if there is not. Target count returned from this function
196  * doesn't need to be true when userspace user receive it (after return
197  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
198  */
199 int
200 dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
201 {
202         dm_table_entry_t *table_en;
203         dm_table_t *tbl;
204         uint32_t target_count;
205         uint8_t id;
206
207         target_count = 0;
208
209         id = dm_table_busy(head, table_id);
210
211         tbl = &head->tables[id];
212
213         SLIST_FOREACH(table_en, tbl, next)
214             target_count++;
215
216         dm_table_unbusy(head);
217
218         return target_count;
219 }
220
221
222 /*
223  * Initialize table_head structures, I'm trying to keep this structure as
224  * opaque as possible.
225  */
226 void
227 dm_table_head_init(dm_table_head_t * head)
228 {
229         head->cur_active_table = 0;
230         head->io_cnt = 0;
231
232         /* Initialize tables. */
233         SLIST_INIT(&head->tables[0]);
234         SLIST_INIT(&head->tables[1]);
235
236         lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
237 }
238 /*
239  * Destroy all variables in table_head
240  */
241 void
242 dm_table_head_destroy(dm_table_head_t * head)
243 {
244         KKASSERT(lockcount(&head->table_mtx) == 0);
245
246         /* tables doens't exists when I call this routine, therefore it
247          * doesn't make sense to have io_cnt != 0 */
248         KKASSERT(head->io_cnt == 0);
249
250         lockuninit(&head->table_mtx);
251 }