Home | History | Annotate | Line # | Download | only in dm
dm_dev.c revision 1.1.2.7
      1 /*        $NetBSD: dm_dev.c,v 1.1.2.7 2008/09/05 01:04:23 haad Exp $      */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002 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 
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 
     36 #include <sys/disklabel.h>
     37 #include <sys/errno.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/ioccom.h>
     40 #include <sys/kmem.h>
     41 #include <sys/lkm.h>
     42 #include <sys/queue.h>
     43 
     44 #include "netbsd-dm.h"
     45 #include "dm.h"
     46 
     47 /*TAILQ_HEAD(dm_dev_head, dm_dev);*/
     48 
     49 static struct dm_dev_head dm_dev_list =
     50 TAILQ_HEAD_INITIALIZER(dm_dev_list);
     51 /*static struct db_cmd_tbl_en db_base_cmd_builtins =
     52   { .db_cmd = db_command_table };*/
     53 
     54 kmutex_t dm_dev_mutex;
     55 
     56 /*
     57  * Locking architecture, for now I use mutexes later we can convert them to
     58  * rw_locks. I use IPL_NONE for specifing IPL type for mutex.
     59  * I will enter into mutex everytime I'm working with dm_dev_list.
     60  */
     61 
     62 /*
     63  * Lookup device with its minor number.
     64  */
     65 struct dm_dev*
     66 dm_dev_lookup_minor(int dm_dev_minor)
     67 {
     68 	struct dm_dev *dm_dev;
     69 
     70 	mutex_enter(&dm_dev_mutex);
     71 
     72 	TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist){
     73 		if (dm_dev_minor == dm_dev->minor){
     74 			mutex_exit(&dm_dev_mutex);
     75 			return dm_dev;
     76 		}
     77 	}
     78 
     79 
     80 	mutex_exit(&dm_dev_mutex);
     81 
     82 	return NULL;
     83 }
     84 
     85 /*
     86  * Lookup device with it's device name.
     87  */
     88 struct dm_dev*
     89 dm_dev_lookup_name(const char *dm_dev_name)
     90 {
     91 	struct dm_dev *dm_dev;
     92 	int dlen; int slen;
     93 
     94 	if (dm_dev_name == NULL)
     95 		return NULL;
     96 
     97 	slen = strlen(dm_dev_name);
     98 
     99 	mutex_enter(&dm_dev_mutex);
    100 
    101 	TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist){
    102 
    103 		dlen = strlen(dm_dev->name);
    104 
    105 		if(slen != dlen)
    106 			continue;
    107 
    108 		if (strncmp(dm_dev_name, dm_dev->name, slen) == 0) {
    109 			mutex_exit(&dm_dev_mutex);
    110 			return dm_dev;
    111 		}
    112 	}
    113 
    114 	mutex_exit(&dm_dev_mutex);
    115 
    116 	return NULL;
    117 }
    118 
    119 /*
    120  * Lookup device with it's device uuid. Used mostly by LVM2tools.
    121  */
    122 struct dm_dev*
    123 dm_dev_lookup_uuid(const char *dm_dev_uuid)
    124 {
    125 	struct dm_dev *dm_dev;
    126 	size_t len;
    127 
    128 	len = 0;
    129 
    130 	if (dm_dev_uuid == NULL)
    131 		return NULL;
    132 
    133 	len = strlen(dm_dev_uuid);
    134 
    135 	mutex_enter(&dm_dev_mutex);
    136 
    137 	TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist){
    138 
    139 		if (strlen(dm_dev->uuid) != len)
    140 				continue;
    141 
    142 	    if (strncmp(dm_dev_uuid, dm_dev->uuid, strlen(dm_dev->uuid)) == 0){
    143 		    mutex_exit(&dm_dev_mutex);
    144 		    return dm_dev;
    145 	    }
    146 	}
    147 		mutex_exit(&dm_dev_mutex);
    148 
    149 	return NULL;
    150 }
    151 
    152 /*
    153  * Insert new device to the global list of devices.
    154  */
    155 int
    156 dm_dev_insert(struct dm_dev *dev)
    157 {
    158 	struct dm_dev *dmt;
    159 	int r;
    160 
    161 	dmt = NULL;
    162 	r = 0;
    163 
    164 	if (((dmt = dm_dev_lookup_uuid(dev->uuid)) == NULL) &&
    165 	    ((dmt = dm_dev_lookup_name(dev->name)) == NULL) &&
    166 	    ((dmt = dm_dev_lookup_minor(dev->minor)) == NULL)){
    167 
    168 		mutex_enter(&dm_dev_mutex);
    169 		TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
    170 		mutex_exit(&dm_dev_mutex);
    171 	} else
    172 		r = EEXIST;
    173 
    174 	return r;
    175 }
    176 
    177 
    178 /*
    179  * Remove device selected with dm_dev from global list of devices.
    180  */
    181 int
    182 dm_dev_rem(struct dm_dev *dm_dev)
    183 {
    184 	mutex_enter(&dm_dev_mutex);
    185 
    186 	TAILQ_REMOVE(&dm_dev_list, dm_dev, next_devlist);
    187 
    188 	mutex_exit(&dm_dev_mutex);
    189 
    190 	return 0;
    191 }
    192 
    193 /*
    194  * Destroy all devices created in device-mapper. Remove all tables
    195  * free all allocated memmory.
    196  */
    197 
    198 int
    199 dm_dev_destroy(void)
    200 {
    201 	struct dm_dev *dm_dev;
    202 
    203 	mutex_enter(&dm_dev_mutex);
    204 
    205 	while (TAILQ_FIRST(&dm_dev_list) != NULL){
    206 
    207 		dm_dev = TAILQ_FIRST(&dm_dev_list);
    208 
    209 		TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list),
    210 		next_devlist);
    211 
    212 		/* Destroy active table first.  */
    213 		dm_table_destroy(&dm_dev->tables[dm_dev->cur_active_table]);
    214 
    215 		/* Destroy unactive table if exits, too. */
    216 		if (!SLIST_EMPTY(&dm_dev->tables[1 - dm_dev->cur_active_table]))
    217 			dm_table_destroy(&dm_dev->tables[1 - dm_dev->cur_active_table]);
    218 
    219 		(void)kmem_free(dm_dev, sizeof(struct dm_dev));
    220 	}
    221 
    222 	mutex_exit(&dm_dev_mutex);
    223 
    224 	return 0;
    225 }
    226 
    227 /*
    228  * Allocate new device entry.
    229  */
    230 struct dm_dev*
    231 dm_dev_alloc()
    232 {
    233 	struct dm_dev *dmv;
    234 
    235 	if ((dmv = kmem_alloc(sizeof(struct dm_dev), KM_NOSLEEP)) == NULL)
    236 		return NULL;
    237 
    238 	return dmv;
    239 }
    240 
    241 /*
    242  * Freed device entry.
    243  */
    244 int
    245 dm_dev_free(struct dm_dev *dmv)
    246 {
    247 	if (dmv != NULL)
    248 		(void)kmem_free(dmv, sizeof(struct dm_dev));
    249 
    250 	return 0;
    251 }
    252 
    253 
    254 /*
    255  * Return prop_array of dm_targer_list dictionaries.
    256  */
    257 prop_array_t
    258 dm_dev_prop_list(void)
    259 {
    260 	struct dm_dev *dmd;
    261 
    262 	int j;
    263 
    264 	prop_array_t dev_array;
    265 	prop_dictionary_t dev_dict;
    266 
    267 	j =0;
    268 
    269 	dev_array = prop_array_create();
    270 
    271 	mutex_enter(&dm_dev_mutex);
    272 
    273 	TAILQ_FOREACH(dmd, &dm_dev_list,next_devlist) {
    274 		dev_dict  = prop_dictionary_create();
    275 
    276 		prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmd->name);
    277 
    278 		prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmd->minor);
    279 
    280 		prop_array_set(dev_array, j, dev_dict);
    281 
    282 		prop_object_release(dev_dict);
    283 
    284 		j++;
    285 	}
    286 
    287 	mutex_exit(&dm_dev_mutex);
    288 
    289 	return dev_array;
    290 }
    291 
    292 /*
    293  * Initialize global device mutex.
    294  */
    295 int
    296 dm_dev_init()
    297 {
    298 	TAILQ_INIT(&dm_dev_list); /* initialize global dev list */
    299 
    300 	mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE);
    301 
    302 	return 0;
    303 }
    304