xref: /dragonfly/sys/dev/disk/dm/dm_dev.c (revision cad2e385)
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 struct dm_dev_head dm_dev_list =
58 TAILQ_HEAD_INITIALIZER(dm_dev_list);
59 
60 static struct lock dm_dev_mutex;
61 
62 static char dummy_uuid[DM_UUID_LEN];
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 
175 	/*
176 	 * Ignore uuid lookup if dev->uuid is zero-filled.
177 	 */
178 	if (memcmp(dev->uuid, dummy_uuid, DM_UUID_LEN))
179 		dmv = dm_dev_lookup_uuid(dev->uuid);
180 
181 	if ((dmv == NULL) &&
182 	    ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
183 	    ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
184 		TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
185 	} else {
186 		KKASSERT(dmv != NULL);
187 		r = EEXIST;
188 	}
189 
190 	lockmgr(&dm_dev_mutex, LK_RELEASE);
191 	return r;
192 }
193 
194 dm_dev_t *
195 dm_dev_rem_dev(dm_dev_t *dmv)
196 {
197 	if (dmv == NULL)
198 		return NULL;
199 
200 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
201 	disable_dev(dmv);
202 	lockmgr(&dm_dev_mutex, LK_RELEASE);
203 
204 	KKASSERT(dmv != NULL);
205 	return dmv;
206 }
207 
208 /*
209  * Remove device selected with dm_dev from global list of devices.
210  */
211 dm_dev_t *
212 dm_dev_rem(const char *dm_dev_name, const char *dm_dev_uuid, int dm_dev_minor)
213 {
214 	dm_dev_t *dmv = NULL;
215 
216 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
217 
218 	if (dm_dev_minor > 0) {
219 		if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL)
220 			disable_dev(dmv);
221 	} else if (dm_dev_name != NULL) {
222 		if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL)
223 			disable_dev(dmv);
224 	} else if (dm_dev_uuid != NULL) {
225 		if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL)
226 			disable_dev(dmv);
227 	}
228 
229 	lockmgr(&dm_dev_mutex, LK_RELEASE);
230 
231 	return dmv;
232 }
233 
234 int
235 dm_dev_create(dm_dev_t **dmvp, const char *name, const char *uuid, int flags)
236 {
237 	dm_dev_t *dmv;
238 	char name_buf[MAXPATHLEN];
239 	int r, dm_minor;
240 
241 	if ((dmv = dm_dev_alloc()) == NULL)
242 		return ENOMEM;
243 
244 	if (uuid)
245 		strncpy(dmv->uuid, uuid, DM_UUID_LEN);
246 	else
247 		dmv->uuid[0] = '\0';
248 
249 	if (name)
250 		strlcpy(dmv->name, name, DM_NAME_LEN);
251 
252 	dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0);
253 
254 	dm_table_head_init(&dmv->table_head);
255 
256 	lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
257 	cv_init(&dmv->dev_cv, "dm_dev");
258 
259 	if (flags & DM_READONLY_FLAG)
260 		dmv->flags |= DM_READONLY_FLAG;
261 
262 	aprint_debug("Creating device dm/%s\n", name);
263 	ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name);
264 
265 	devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE,
266 	    DEVSTAT_NO_ORDERED_TAGS,
267 	    DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
268 	    DEVSTAT_PRIORITY_DISK);
269 
270 	dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops);
271 	reference_dev(dmv->devt);
272 
273 	/* Make sure the device are immediately available */
274 	sync_devs();
275 
276 	dmv->devt->si_drv1 = dmv;
277 	dmv->devt->si_drv2 = dmv->diskp;
278 
279 	dmv->minor = minor(dmv->devt);
280 	udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
281 
282 	if ((r = dm_dev_insert(dmv)) != 0)
283 		dm_dev_destroy(dmv);
284 
285 	/* Increment device counter After creating device */
286 	++dm_dev_counter; /* XXX: was atomic 64 */
287 	*dmvp = dmv;
288 
289 	return r;
290 }
291 
292 int
293 dm_dev_destroy(dm_dev_t *dmv)
294 {
295 	int minor;
296 
297 	/* Destroy active table first.  */
298 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
299 
300 	/* Destroy inactive table if exits, too. */
301 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
302 
303 	dm_table_head_destroy(&dmv->table_head);
304 
305 	minor = dkunit(dmv->devt);
306 	disk_destroy(dmv->diskp);
307 	devstat_remove_entry(&dmv->stats);
308 
309 	release_dev(dmv->devt);
310 	devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
311 
312 	lockuninit(&dmv->dev_mtx);
313 	cv_destroy(&dmv->dev_cv);
314 
315 	/* Destroy device */
316 	dm_dev_free(dmv);
317 
318 	/* Decrement device counter After removing device */
319 	--dm_dev_counter; /* XXX: was atomic 64 */
320 
321 	return 0;
322 }
323 
324 /*
325  * dm_dev_remove is called to completely destroy & remove a dm disk device.
326  */
327 int
328 dm_dev_remove(dm_dev_t *dmv)
329 {
330 	/* Remove device from list and wait for refcnt to drop to zero */
331 	dm_dev_rem_dev(dmv);
332 
333 	/* Destroy and free the device */
334 	dm_dev_destroy(dmv);
335 
336 	return 0;
337 }
338 
339 int
340 dm_dev_remove_all(int gentle)
341 {
342 	dm_dev_t *dmv, *dmv2;
343 	int r;
344 
345 	r = 0;
346 
347 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
348 
349 	/*
350 	 * Process in reverse order so that it can deal with inter-depentent
351 	 * devices.
352 	 */
353 	TAILQ_FOREACH_REVERSE_MUTABLE(dmv, &dm_dev_list, dm_dev_head,
354 	    next_devlist, dmv2) {
355 		if (gentle && dmv->is_open) {
356 			r = EBUSY;
357 			continue;
358 		}
359 
360 		disable_dev(dmv);
361 		dm_dev_destroy(dmv);
362 	}
363 	lockmgr(&dm_dev_mutex, LK_RELEASE);
364 
365 	return r;
366 }
367 
368 /*
369  * Allocate new device entry.
370  */
371 dm_dev_t *
372 dm_dev_alloc(void)
373 {
374 	dm_dev_t *dmv;
375 
376 	dmv = kmalloc(sizeof(dm_dev_t), M_DM, M_WAITOK | M_ZERO);
377 
378 	if (dmv != NULL)
379 		dmv->diskp = kmalloc(sizeof(struct disk), M_DM, M_WAITOK | M_ZERO);
380 
381 	return dmv;
382 }
383 /*
384  * Freed device entry.
385  */
386 int
387 dm_dev_free(dm_dev_t *dmv)
388 {
389 	KKASSERT(dmv != NULL);
390 
391 	if (dmv->diskp != NULL)
392 		kfree(dmv->diskp, M_DM);
393 
394 	kfree(dmv, M_DM);
395 
396 	return 0;
397 }
398 
399 void
400 dm_dev_busy(dm_dev_t *dmv)
401 {
402 	lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
403 	dmv->ref_cnt++;
404 	lockmgr(&dmv->dev_mtx, LK_RELEASE);
405 }
406 
407 void
408 dm_dev_unbusy(dm_dev_t *dmv)
409 {
410 	KKASSERT(dmv->ref_cnt != 0);
411 
412 	lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
413 	if (--dmv->ref_cnt == 0)
414 		cv_broadcast(&dmv->dev_cv);
415 	lockmgr(&dmv->dev_mtx, LK_RELEASE);
416 }
417 /*
418  * Return prop_array of dm_targer_list dictionaries.
419  */
420 prop_array_t
421 dm_dev_prop_list(void)
422 {
423 	dm_dev_t *dmv;
424 	prop_array_t dev_array;
425 	prop_dictionary_t dev_dict;
426 
427 	dev_array = prop_array_create();
428 
429 	lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
430 
431 	TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
432 		dev_dict = prop_dictionary_create();
433 
434 		prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name);
435 		prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
436 
437 		prop_array_add(dev_array, dev_dict);
438 		prop_object_release(dev_dict);
439 	}
440 
441 	lockmgr(&dm_dev_mutex, LK_RELEASE);
442 	return dev_array;
443 }
444 /*
445  * Initialize global device mutex.
446  */
447 int
448 dm_dev_init(void)
449 {
450 	TAILQ_INIT(&dm_dev_list);	/* initialize global dev list */
451 	lockinit(&dm_dev_mutex, "dmdevlist", 0, LK_CANRECURSE);
452 	devfs_clone_bitmap_init(&dm_minor_bitmap);
453 
454 	memset(dummy_uuid, 0, sizeof(dummy_uuid));
455 	return 0;
456 }
457 
458 /*
459  * Destroy all devices created in device-mapper. Remove all tables
460  * free all allocated memmory.
461  */
462 int
463 dm_dev_uninit(void)
464 {
465 	/* Force removal of all devices */
466 	dm_dev_remove_all(0);
467 
468 	lockuninit(&dm_dev_mutex);
469 	return 0;
470 }
471