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