1 /* $NetBSD: dm_dev.c,v 1.19 2021/08/21 22:23:33 andvar 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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: dm_dev.c,v 1.19 2021/08/21 22:23:33 andvar Exp $");
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/disk.h>
37 #include <sys/disklabel.h>
38 #include <sys/ioctl.h>
39 #include <sys/ioccom.h>
40 #include <sys/kmem.h>
41
42 #include "netbsd-dm.h"
43 #include "dm.h"
44
45 static dm_dev_t *dm_dev_lookup_name(const char *);
46 static dm_dev_t *dm_dev_lookup_uuid(const char *);
47 static dm_dev_t *dm_dev_lookup_minor(int);
48
49 static struct dm_dev_head dm_dev_list = TAILQ_HEAD_INITIALIZER(dm_dev_list);
50
51 static kmutex_t dm_dev_mutex;
52
53 /* dm_dev_mutex must be holdby caller before using disable_dev. */
54 static void
disable_dev(dm_dev_t * dmv)55 disable_dev(dm_dev_t *dmv)
56 {
57
58 TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist);
59 mutex_enter(&dmv->dev_mtx);
60 mutex_exit(&dm_dev_mutex);
61 while (dmv->ref_cnt != 0)
62 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
63 mutex_exit(&dmv->dev_mtx);
64 }
65
66 /*
67 * Generic function used to lookup dm_dev_t. Calling with dm_dev_name
68 * and dm_dev_uuid NULL is allowed.
69 */
70 dm_dev_t *
dm_dev_lookup(const char * dm_dev_name,const char * dm_dev_uuid,int dm_dev_minor)71 dm_dev_lookup(const char *dm_dev_name, const char *dm_dev_uuid,
72 int dm_dev_minor)
73 {
74 dm_dev_t *dmv;
75
76 mutex_enter(&dm_dev_mutex);
77 /* KASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
78 * > 0); */
79 if (dm_dev_minor > 0)
80 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
81 dm_dev_busy(dmv);
82 mutex_exit(&dm_dev_mutex);
83 return dmv;
84 }
85 if (dm_dev_name != NULL)
86 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
87 dm_dev_busy(dmv);
88 mutex_exit(&dm_dev_mutex);
89 return dmv;
90 }
91 if (dm_dev_uuid != NULL)
92 if ((dmv = dm_dev_lookup_uuid(dm_dev_uuid)) != NULL) {
93 dm_dev_busy(dmv);
94 mutex_exit(&dm_dev_mutex);
95 return dmv;
96 }
97 mutex_exit(&dm_dev_mutex);
98
99 return NULL;
100 }
101
102 /*
103 * Lookup device with its minor number.
104 */
105 static dm_dev_t *
dm_dev_lookup_minor(int dm_dev_minor)106 dm_dev_lookup_minor(int dm_dev_minor)
107 {
108 dm_dev_t *dmv;
109
110 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist)
111 if (dm_dev_minor == dmv->minor)
112 return dmv;
113
114 return NULL;
115 }
116
117 /*
118 * Lookup device with its device name.
119 */
120 static dm_dev_t *
dm_dev_lookup_name(const char * dm_dev_name)121 dm_dev_lookup_name(const char *dm_dev_name)
122 {
123 dm_dev_t *dmv;
124 size_t dlen;
125 size_t slen;
126
127 slen = strlen(dm_dev_name);
128
129 if (slen == 0)
130 return NULL;
131
132 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
133 dlen = strlen(dmv->name);
134
135 if (slen != dlen)
136 continue;
137
138 if (strncmp(dm_dev_name, dmv->name, slen) == 0)
139 return dmv;
140 }
141
142 return NULL;
143 }
144
145 /*
146 * Lookup device with its device uuid. Used mostly by LVM2tools.
147 */
148 static dm_dev_t *
dm_dev_lookup_uuid(const char * dm_dev_uuid)149 dm_dev_lookup_uuid(const char *dm_dev_uuid)
150 {
151 dm_dev_t *dmv;
152 size_t len;
153
154 len = 0;
155 len = strlen(dm_dev_uuid);
156
157 if (len == 0)
158 return NULL;
159
160 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
161 if (strlen(dmv->uuid) != len)
162 continue;
163
164 if (strncmp(dm_dev_uuid, dmv->uuid, strlen(dmv->uuid)) == 0)
165 return dmv;
166 }
167
168 return NULL;
169 }
170
171 /*
172 * Insert new device to the global list of devices.
173 */
174 int
dm_dev_insert(dm_dev_t * dev)175 dm_dev_insert(dm_dev_t *dev)
176 {
177 dm_dev_t *dmv;
178 int r;
179
180 dmv = NULL;
181 r = 0;
182
183 KASSERT(dev != NULL);
184 mutex_enter(&dm_dev_mutex);
185 if (((dmv = dm_dev_lookup_uuid(dev->uuid)) == NULL) &&
186 ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
187 ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
188 TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
189 } else
190 r = EEXIST;
191 mutex_exit(&dm_dev_mutex);
192
193 return r;
194 }
195
196 #ifdef notyet
197 /*
198 * Lookup device with its minor number.
199 */
200 int
dm_dev_test_minor(int dm_dev_minor)201 dm_dev_test_minor(int dm_dev_minor)
202 {
203 dm_dev_t *dmv;
204
205 mutex_enter(&dm_dev_mutex);
206 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
207 if (dm_dev_minor == dmv->minor) {
208 mutex_exit(&dm_dev_mutex);
209 return 1;
210 }
211 }
212 mutex_exit(&dm_dev_mutex);
213
214 return 0;
215 }
216 #endif
217
218 /*
219 * dm_dev_lookup_devt look for selected device_t. We keep this routine
220 * outside of dm_dev_lookup because it is a temporally solution.
221 *
222 * TODO: This is a hack autoconf should be more flexible.
223 */
224 dm_dev_t *
dm_dev_detach(device_t devt)225 dm_dev_detach(device_t devt)
226 {
227 dm_dev_t *dmv;
228
229 mutex_enter(&dm_dev_mutex);
230 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
231 if (devt == dmv->devt) {
232 disable_dev(dmv);
233 return dmv;
234 }
235 }
236 mutex_exit(&dm_dev_mutex);
237
238 return NULL;
239 }
240
241 /*
242 * Remove device selected with dm_dev from global list of devices.
243 */
244 dm_dev_t *
dm_dev_rem(const char * dm_dev_name,const char * dm_dev_uuid,int dm_dev_minor)245 dm_dev_rem(const char *dm_dev_name, const char *dm_dev_uuid,
246 int dm_dev_minor)
247 {
248 dm_dev_t *dmv;
249
250 mutex_enter(&dm_dev_mutex);
251 if (dm_dev_minor > 0)
252 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
253 disable_dev(dmv);
254 return dmv;
255 }
256 if (dm_dev_name != NULL)
257 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
258 disable_dev(dmv);
259 return dmv;
260 }
261 if (dm_dev_uuid != NULL)
262 if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL) {
263 disable_dev(dmv);
264 return dmv;
265 }
266 mutex_exit(&dm_dev_mutex);
267
268 return NULL;
269 }
270
271 /*
272 * Destroy all devices created in device-mapper. Remove all tables
273 * free all allocated memory.
274 */
275 int
dm_dev_destroy(void)276 dm_dev_destroy(void)
277 {
278 dm_dev_t *dmv;
279
280 mutex_enter(&dm_dev_mutex);
281 while (TAILQ_FIRST(&dm_dev_list) != NULL) {
282 dmv = TAILQ_FIRST(&dm_dev_list);
283
284 TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list),
285 next_devlist);
286
287 mutex_enter(&dmv->dev_mtx);
288
289 while (dmv->ref_cnt != 0)
290 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
291
292 /* Destroy active table first. */
293 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
294
295 /* Destroy inactive table if exits, too. */
296 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
297
298 dm_table_head_destroy(&dmv->table_head);
299
300 mutex_exit(&dmv->dev_mtx);
301 mutex_destroy(&dmv->dev_mtx);
302 cv_destroy(&dmv->dev_cv);
303
304 kmem_free(dmv, sizeof(dm_dev_t));
305 }
306 mutex_exit(&dm_dev_mutex);
307
308 mutex_destroy(&dm_dev_mutex);
309 return 0;
310 }
311
312 /*
313 * Allocate new device entry.
314 */
315 dm_dev_t *
dm_dev_alloc(void)316 dm_dev_alloc(void)
317 {
318 dm_dev_t *dmv;
319
320 dmv = kmem_zalloc(sizeof(dm_dev_t), KM_SLEEP);
321 dmv->diskp = kmem_zalloc(sizeof(struct disk), KM_SLEEP);
322 return dmv;
323 }
324
325 /*
326 * Freed device entry.
327 */
328 int
dm_dev_free(dm_dev_t * dmv)329 dm_dev_free(dm_dev_t *dmv)
330 {
331
332 KASSERT(dmv != NULL);
333
334 mutex_destroy(&dmv->dev_mtx);
335 mutex_destroy(&dmv->diskp_mtx);
336 cv_destroy(&dmv->dev_cv);
337
338 if (dmv->diskp != NULL)
339 kmem_free(dmv->diskp, sizeof(struct disk));
340
341 kmem_free(dmv, sizeof(dm_dev_t));
342
343 return 0;
344 }
345
346 void
dm_dev_busy(dm_dev_t * dmv)347 dm_dev_busy(dm_dev_t *dmv)
348 {
349
350 mutex_enter(&dmv->dev_mtx);
351 dmv->ref_cnt++;
352 mutex_exit(&dmv->dev_mtx);
353 }
354
355 void
dm_dev_unbusy(dm_dev_t * dmv)356 dm_dev_unbusy(dm_dev_t *dmv)
357 {
358
359 KASSERT(dmv->ref_cnt != 0);
360
361 mutex_enter(&dmv->dev_mtx);
362 if (--dmv->ref_cnt == 0)
363 cv_broadcast(&dmv->dev_cv);
364 mutex_exit(&dmv->dev_mtx);
365 }
366
367 /*
368 * Return prop_array of dm_targer_list dictionaries.
369 */
370 prop_array_t
dm_dev_prop_list(void)371 dm_dev_prop_list(void)
372 {
373 dm_dev_t *dmv;
374 prop_array_t dev_array;
375 prop_dictionary_t dev_dict;
376
377 dev_array = prop_array_create();
378
379 mutex_enter(&dm_dev_mutex);
380
381 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
382 dev_dict = prop_dictionary_create();
383
384 prop_dictionary_set_string(dev_dict, DM_DEV_NAME, dmv->name);
385 prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
386
387 prop_array_add(dev_array, dev_dict);
388 prop_object_release(dev_dict);
389 }
390
391 mutex_exit(&dm_dev_mutex);
392 return dev_array;
393 }
394
395 /*
396 * Initialize global device mutex.
397 */
398 int
dm_dev_init(void)399 dm_dev_init(void)
400 {
401
402 TAILQ_INIT(&dm_dev_list); /* initialize global dev list */
403 mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE);
404 return 0;
405 }
406