Home | History | Annotate | Line # | Download | only in dm
dm_dev.c revision 1.1.2.5
      1 /*        $NetBSD: dm_dev.c,v 1.1.2.5 2008/08/19 23:31:52 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 
    127 	if (dm_dev_uuid == NULL)
    128 		return NULL;
    129 
    130 	mutex_enter(&dm_dev_mutex);
    131 
    132 	TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist)
    133 	    if (strncmp(dm_dev_uuid, dm_dev->uuid, strlen(dm_dev->uuid)) == 0){
    134 		    mutex_exit(&dm_dev_mutex);
    135 		    return dm_dev;
    136 	    }
    137 	mutex_exit(&dm_dev_mutex);
    138 
    139 	return NULL;
    140 }
    141 
    142 /*
    143  * Insert new device to the global list of devices.
    144  */
    145 int
    146 dm_dev_insert(struct dm_dev *dev)
    147 {
    148 	struct dm_dev *dmt;
    149 	int r;
    150 
    151 	dmt = NULL;
    152 	r = 0;
    153 
    154 	/* XXX dev->name can be NULL if uuid is used */
    155 	dmt = dm_dev_lookup_name(dev->name);
    156 
    157 	if (dmt == NULL) {
    158 		mutex_enter(&dm_dev_mutex);
    159 		TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
    160 		mutex_exit(&dm_dev_mutex);
    161 	} else
    162 		r = EEXIST;
    163 
    164 	return r;
    165 }
    166 
    167 
    168 /*
    169  * Remove device selected with name.
    170  */
    171 int
    172 dm_dev_rem(const char *dm_dev_name)
    173 {
    174 	struct dm_dev *dm_dev;
    175 	int r;
    176 
    177 	r = ENOENT;
    178 
    179 	if(dm_dev_name == NULL)
    180 		goto out;
    181 
    182 	if((dm_dev = dm_dev_lookup_name(dm_dev_name)) == NULL)
    183 	    dm_dev = dm_dev_lookup_uuid(dm_dev_name);
    184 
    185 	if (dm_dev == NULL)
    186 		goto out;
    187 
    188 	mutex_enter(&dm_dev_mutex);
    189 
    190 	TAILQ_REMOVE(&dm_dev_list, dm_dev, next_devlist);
    191 
    192 	mutex_exit(&dm_dev_mutex);
    193 
    194 	dm_dev_free(dm_dev);
    195 
    196 	r = 0;
    197  out:
    198 	return r;
    199 }
    200 
    201 /*
    202  * Destroy all devices created in device-mapper. Remove all tables
    203  * free all allocated memmory.
    204  */
    205 
    206 int
    207 dm_dev_destroy(void)
    208 {
    209 	struct dm_dev *dm_dev;
    210 
    211 	mutex_enter(&dm_dev_mutex);
    212 
    213 	while (TAILQ_FIRST(&dm_dev_list) != NULL){
    214 
    215 		dm_dev = TAILQ_FIRST(&dm_dev_list);
    216 
    217 		TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list),
    218 		next_devlist);
    219 
    220 		/* Destroy active table first.  */
    221 		dm_table_destroy(&dm_dev->tables[dm_dev->cur_active_table]);
    222 
    223 		/* Destroy unactive table if exits, too. */
    224 		if (!SLIST_EMPTY(&dm_dev->tables[1 - dm_dev->cur_active_table]))
    225 			dm_table_destroy(&dm_dev->tables[1 - dm_dev->cur_active_table]);
    226 
    227 		(void)kmem_free(dm_dev, sizeof(struct dm_dev));
    228 	}
    229 
    230 	mutex_exit(&dm_dev_mutex);
    231 
    232 	return 0;
    233 }
    234 
    235 /*
    236  * Allocate new device entry.
    237  */
    238 struct dm_dev*
    239 dm_dev_alloc()
    240 {
    241 	struct dm_dev *dmv;
    242 
    243 	if ((dmv = kmem_alloc(sizeof(struct dm_dev), KM_NOSLEEP)) == NULL)
    244 		return NULL;
    245 
    246 	return dmv;
    247 }
    248 
    249 /*
    250  * Freed device entry.
    251  */
    252 int
    253 dm_dev_free(struct dm_dev *dmv)
    254 {
    255 	if (dmv != NULL){
    256 		if (dmv->dm_dklabel != NULL)
    257 			(void)kmem_free(dmv->dm_dklabel,sizeof(struct disklabel));
    258 		(void)kmem_free(dmv,sizeof(struct dm_dev));
    259 	}
    260 
    261 	return 0;
    262 }
    263 
    264 
    265 /*
    266  * Return prop_array of dm_targer_list dictionaries.
    267  */
    268 prop_array_t
    269 dm_dev_prop_list(void)
    270 {
    271 	struct dm_dev *dmd;
    272 
    273 	int j;
    274 
    275 	prop_array_t dev_array;
    276 	prop_dictionary_t dev_dict;
    277 
    278 	j =0;
    279 
    280 	dev_array = prop_array_create();
    281 
    282 	mutex_enter(&dm_dev_mutex);
    283 
    284 	TAILQ_FOREACH(dmd, &dm_dev_list,next_devlist) {
    285 		dev_dict  = prop_dictionary_create();
    286 
    287 		prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmd->name);
    288 
    289 		prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmd->minor);
    290 
    291 		prop_array_set(dev_array, j, dev_dict);
    292 
    293 		prop_object_release(dev_dict);
    294 
    295 		j++;
    296 	}
    297 
    298 	mutex_exit(&dm_dev_mutex);
    299 
    300 	return dev_array;
    301 }
    302 
    303 /*
    304  *Initialize global device mutex.
    305  */
    306 int
    307 dm_dev_init()
    308 {
    309 	mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE);
    310 
    311 	return 0;
    312 }
    313