dm - Add method to get inactive table size
[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) 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 or inactive table in device.
166  */
167 static uint64_t
168 _dm_table_size(dm_table_head_t * head, int table)
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, table);
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 uint64_t
196 dm_table_size(dm_table_head_t *head)
197 {
198         return _dm_table_size(head, DM_TABLE_ACTIVE);
199 }
200
201 uint64_t
202 dm_inactive_table_size(dm_table_head_t *head)
203 {
204         return _dm_table_size(head, DM_TABLE_INACTIVE);
205 }
206
207 /*
208  * Return > 0 if table is at least one table entry (returns number of entries)
209  * and return 0 if there is not. Target count returned from this function
210  * doesn't need to be true when userspace user receive it (after return
211  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
212  */
213 int
214 dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
215 {
216         dm_table_entry_t *table_en;
217         dm_table_t *tbl;
218         uint32_t target_count;
219         uint8_t id;
220
221         target_count = 0;
222
223         id = dm_table_busy(head, table_id);
224
225         tbl = &head->tables[id];
226
227         SLIST_FOREACH(table_en, tbl, next)
228             target_count++;
229
230         dm_table_unbusy(head);
231
232         return target_count;
233 }
234
235
236 /*
237  * Initialize table_head structures, I'm trying to keep this structure as
238  * opaque as possible.
239  */
240 void
241 dm_table_head_init(dm_table_head_t * head)
242 {
243         head->cur_active_table = 0;
244         head->io_cnt = 0;
245
246         /* Initialize tables. */
247         SLIST_INIT(&head->tables[0]);
248         SLIST_INIT(&head->tables[1]);
249
250         lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
251 }
252 /*
253  * Destroy all variables in table_head
254  */
255 void
256 dm_table_head_destroy(dm_table_head_t * head)
257 {
258         KKASSERT(lockcount(&head->table_mtx) == 0);
259
260         /* tables doens't exists when I call this routine, therefore it
261          * doesn't make sense to have io_cnt != 0 */
262         KKASSERT(head->io_cnt == 0);
263
264         lockuninit(&head->table_mtx);
265 }