xref: /dragonfly/sys/dev/disk/dm/dm_dev.c (revision b0730bc5)
1 /*        $NetBSD: dm_dev.c,v 1.8 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 #include <machine/thread.h>
36 #include <sys/thread2.h>
37 
38 #include <sys/disk.h>
39 #include <sys/disklabel.h>
40 #include <sys/devicestat.h>
41 #include <sys/device.h>
42 #include <sys/udev.h>
43 #include <sys/devfs.h>
44 #include <sys/ioccom.h>
45 #include <sys/malloc.h>
46 #include <dev/disk/dm/dm.h>
47 
48 #include "netbsd-dm.h"
49 
50 extern struct dev_ops dm_ops;
51 
52 struct devfs_bitmap dm_minor_bitmap;
53 uint64_t dm_dev_counter;
54 
55 static dm_dev_t *dm_dev_lookup_name(const char *);
56 static dm_dev_t *dm_dev_lookup_uuid(const char *);
57 static dm_dev_t *dm_dev_lookup_minor(int);
58 
59 static struct dm_dev_head dm_dev_list =
60 TAILQ_HEAD_INITIALIZER(dm_dev_list);
61 
62 struct lock dm_dev_mutex;
63 
64 /* dm_dev_mutex must be held by caller before using disable_dev. */
65 static void
66 disable_dev(dm_dev_t * dmv)
67 {
68 	KKASSERT(lockstatus(&dm_dev_mutex, curthread) == LK_EXCLUSIVE);
69 
70 	TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist);
71 
72 	lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
73 	while (dmv->ref_cnt != 0)
74 		cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
75 	lockmgr(&dmv->dev_mtx, LK_RELEASE);
76 }
77 /*
78  * Generic function used to lookup dm_dev_t. Calling with dm_dev_name
79  * and dm_dev_uuid NULL is allowed.
80  */
81 dm_dev_t *
82 dm_dev_lookup(const char *dm_dev_name, const char *dm_dev_uuid,
83     int dm_dev_minor)
84 {
85 	dm_dev_t *dmv;
86 
87 	dmv = NULL;
88 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
89 
90 	/* KKASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
91 	 * > 0); */
92 	if (dm_dev_minor > 0)
93 		if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
94 			dm_dev_busy(dmv);
95 			lockmgr(&dm_dev_mutex, LK_RELEASE);
96 			return dmv;
97 		}
98 	if (dm_dev_name != NULL)
99 		if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
100 			dm_dev_busy(dmv);
101 			lockmgr(&dm_dev_mutex, LK_RELEASE);
102 			return dmv;
103 		}
104 	if (dm_dev_uuid != NULL)
105 		if ((dmv = dm_dev_lookup_uuid(dm_dev_uuid)) != NULL) {
106 			dm_dev_busy(dmv);
107 			lockmgr(&dm_dev_mutex, LK_RELEASE);
108 			return dmv;
109 		}
110 	lockmgr(&dm_dev_mutex, LK_RELEASE);
111 	return NULL;
112 }
113 
114 
115 /*
116  * Lookup device with its minor number.
117  */
118 static dm_dev_t *
119 dm_dev_lookup_minor(int dm_dev_minor)
120 {
121 	dm_dev_t *dmv;
122 
123 	TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
124 		if (dm_dev_minor == dmv->minor)
125 			return dmv;
126 	}
127 
128 	return NULL;
129 }
130 /*
131  * Lookup device with it's device name.
132  */
133 static dm_dev_t *
134 dm_dev_lookup_name(const char *dm_dev_name)
135 {
136 	dm_dev_t *dmv;
137 
138 	TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
139 		if (strcmp(dm_dev_name, dmv->name) == 0)
140 			return dmv;
141 	}
142 
143 	return NULL;
144 }
145 /*
146  * Lookup device with it's device uuid. Used mostly by LVM2tools.
147  */
148 static dm_dev_t *
149 dm_dev_lookup_uuid(const char *dm_dev_uuid)
150 {
151 	dm_dev_t *dmv;
152 
153 	TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
154 		if (strcmp(dm_dev_uuid, dmv->uuid) == 0)
155 			return dmv;
156 	}
157 
158 	return NULL;
159 }
160 /*
161  * Insert new device to the global list of devices.
162  */
163 int
164 dm_dev_insert(dm_dev_t * dev)
165 {
166 	dm_dev_t *dmv;
167 	int r;
168 
169 	dmv = NULL;
170 	r = 0;
171 
172 	KKASSERT(dev != NULL);
173 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
174 	if (((dmv = dm_dev_lookup_uuid(dev->uuid)) == NULL) &&
175 	    ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
176 	    ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
177 
178 		TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
179 
180 	} else
181 		r = EEXIST;
182 
183 	lockmgr(&dm_dev_mutex, LK_RELEASE);
184 	return r;
185 }
186 
187 /*
188  * Remove device selected with dm_dev from global list of devices.
189  */
190 dm_dev_t *
191 dm_dev_rem(dm_dev_t *dmv, const char *dm_dev_name, const char *dm_dev_uuid,
192     int dm_dev_minor)
193 {
194 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
195 
196 	if (dmv != NULL) {
197 		disable_dev(dmv);
198 	} else if (dm_dev_minor > 0) {
199 		if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL)
200 			disable_dev(dmv);
201 	} else if (dm_dev_name != NULL) {
202 		if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL)
203 			disable_dev(dmv);
204 	} else if (dm_dev_uuid != NULL) {
205 		if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL)
206 			disable_dev(dmv);
207 	}
208 
209 	lockmgr(&dm_dev_mutex, LK_RELEASE);
210 
211 	return dmv;
212 }
213 
214 int
215 dm_dev_create(dm_dev_t **dmvp, const char *name, const char *uuid, int flags)
216 {
217 	dm_dev_t *dmv;
218 	char name_buf[MAXPATHLEN];
219 	int r, dm_minor;
220 
221 	if ((dmv = dm_dev_alloc()) == NULL)
222 		return ENOMEM;
223 
224 	if (uuid)
225 		strncpy(dmv->uuid, uuid, DM_UUID_LEN);
226 	else
227 		dmv->uuid[0] = '\0';
228 
229 	if (name)
230 		strlcpy(dmv->name, name, DM_NAME_LEN);
231 
232 	dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0);
233 
234 	dm_table_head_init(&dmv->table_head);
235 
236 	lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
237 	cv_init(&dmv->dev_cv, "dm_dev");
238 
239 	if (flags & DM_READONLY_FLAG)
240 		dmv->flags |= DM_READONLY_FLAG;
241 
242 	aprint_debug("Creating device dm/%s\n", name);
243 	ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name);
244 
245 	devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE,
246 	    DEVSTAT_NO_ORDERED_TAGS,
247 	    DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
248 	    DEVSTAT_PRIORITY_DISK);
249 
250 	dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops);
251 	reference_dev(dmv->devt);
252 
253 	/* Make sure the device are immediately available */
254 	sync_devs();
255 
256 	dmv->devt->si_drv1 = dmv;
257 	dmv->devt->si_drv2 = dmv->diskp;
258 
259 	dmv->minor = minor(dmv->devt);
260 	udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
261 
262 	if ((r = dm_dev_insert(dmv)) != 0)
263 		dm_dev_destroy(dmv);
264 
265 	/* Increment device counter After creating device */
266 	++dm_dev_counter; /* XXX: was atomic 64 */
267 	*dmvp = dmv;
268 
269 	return r;
270 }
271 
272 int
273 dm_dev_destroy(dm_dev_t *dmv)
274 {
275 	int minor;
276 
277 	/* Destroy active table first.  */
278 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
279 
280 	/* Destroy inactive table if exits, too. */
281 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
282 
283 	dm_table_head_destroy(&dmv->table_head);
284 
285 	minor = dkunit(dmv->devt);
286 	disk_destroy(dmv->diskp);
287 	devstat_remove_entry(&dmv->stats);
288 
289 	release_dev(dmv->devt);
290 	devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
291 
292 	lockuninit(&dmv->dev_mtx);
293 	cv_destroy(&dmv->dev_cv);
294 
295 	/* Destroy device */
296 	(void)dm_dev_free(dmv);
297 
298 	/* Decrement device counter After removing device */
299 	--dm_dev_counter; /* XXX: was atomic 64 */
300 
301 	return 0;
302 }
303 
304 /*
305  * dm_detach is called to completely destroy & remove a dm disk device.
306  */
307 int
308 dm_dev_remove(dm_dev_t *dmv)
309 {
310 	/* Remove device from list and wait for refcnt to drop to zero */
311 	dm_dev_rem(dmv, NULL, NULL, -1);
312 
313 	/* Destroy and free the device */
314 	dm_dev_destroy(dmv);
315 
316 	return 0;
317 }
318 
319 int
320 dm_dev_remove_all(int gentle)
321 {
322 	dm_dev_t *dmv, *dmv2;
323 	int r;
324 
325 	r = 0;
326 
327 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
328 
329 	/*
330 	 * Process in reverse order so that it can deal with inter-depentent
331 	 * devices.
332 	 */
333 	TAILQ_FOREACH_REVERSE_MUTABLE(dmv, &dm_dev_list, dm_dev_head,
334 	    next_devlist, dmv2) {
335 		if (gentle && dmv->is_open) {
336 			r = EBUSY;
337 			continue;
338 		}
339 
340 		disable_dev(dmv);
341 		dm_dev_destroy(dmv);
342 	}
343 	lockmgr(&dm_dev_mutex, LK_RELEASE);
344 
345 	return r;
346 }
347 
348 /*
349  * Allocate new device entry.
350  */
351 dm_dev_t *
352 dm_dev_alloc(void)
353 {
354 	dm_dev_t *dmv;
355 
356 	dmv = kmalloc(sizeof(dm_dev_t), M_DM, M_WAITOK | M_ZERO);
357 
358 	if (dmv != NULL)
359 		dmv->diskp = kmalloc(sizeof(struct disk), M_DM, M_WAITOK | M_ZERO);
360 
361 	return dmv;
362 }
363 /*
364  * Freed device entry.
365  */
366 int
367 dm_dev_free(dm_dev_t * dmv)
368 {
369 	KKASSERT(dmv != NULL);
370 
371 	if (dmv->diskp != NULL)
372 		(void) kfree(dmv->diskp, M_DM);
373 
374 	(void) kfree(dmv, M_DM);
375 
376 	return 0;
377 }
378 
379 void
380 dm_dev_busy(dm_dev_t * dmv)
381 {
382 	lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
383 	dmv->ref_cnt++;
384 	lockmgr(&dmv->dev_mtx, LK_RELEASE);
385 }
386 
387 void
388 dm_dev_unbusy(dm_dev_t * dmv)
389 {
390 	KKASSERT(dmv->ref_cnt != 0);
391 
392 	lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
393 	if (--dmv->ref_cnt == 0)
394 		cv_broadcast(&dmv->dev_cv);
395 	lockmgr(&dmv->dev_mtx, LK_RELEASE);
396 }
397 /*
398  * Return prop_array of dm_targer_list dictionaries.
399  */
400 prop_array_t
401 dm_dev_prop_list(void)
402 {
403 	dm_dev_t *dmv;
404 	prop_array_t dev_array;
405 	prop_dictionary_t dev_dict;
406 
407 	dev_array = prop_array_create();
408 
409 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
410 
411 	TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
412 		dev_dict = prop_dictionary_create();
413 
414 		prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name);
415 		prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
416 
417 		prop_array_add(dev_array, dev_dict);
418 		prop_object_release(dev_dict);
419 	}
420 
421 	lockmgr(&dm_dev_mutex, LK_RELEASE);
422 	return dev_array;
423 }
424 /*
425  * Initialize global device mutex.
426  */
427 int
428 dm_dev_init(void)
429 {
430 	TAILQ_INIT(&dm_dev_list);	/* initialize global dev list */
431 	lockinit(&dm_dev_mutex, "dmdevlist", 0, LK_CANRECURSE);
432 	devfs_clone_bitmap_init(&dm_minor_bitmap);
433 	return 0;
434 }
435 
436 /*
437  * Destroy all devices created in device-mapper. Remove all tables
438  * free all allocated memmory.
439  */
440 int
441 dm_dev_uninit(void)
442 {
443 	/* Force removal of all devices */
444 	dm_dev_remove_all(0);
445 
446 	lockuninit(&dm_dev_mutex);
447 	return 0;
448 }
449