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