1 /* $NetBSD: dm_dev.c,v 1.11 2018/01/05 14:22:26 christos 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.11 2018/01/05 14:22:26 christos 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 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 137 dlen = strlen(dmv->name); 138 139 if (slen != dlen) 140 continue; 141 142 if (strncmp(dm_dev_name, dmv->name, slen) == 0) 143 return dmv; 144 } 145 146 return NULL; 147 } 148 149 /* 150 * Lookup device with its device uuid. Used mostly by LVM2tools. 151 */ 152 static dm_dev_t * 153 dm_dev_lookup_uuid(const char *dm_dev_uuid) 154 { 155 dm_dev_t *dmv; 156 size_t len; 157 158 len = 0; 159 len = strlen(dm_dev_uuid); 160 161 if (len == 0) 162 return NULL; 163 164 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { 165 166 if (strlen(dmv->uuid) != len) 167 continue; 168 169 if (strncmp(dm_dev_uuid, dmv->uuid, strlen(dmv->uuid)) == 0) 170 return dmv; 171 } 172 173 return NULL; 174 } 175 176 /* 177 * Insert new device to the global list of devices. 178 */ 179 int 180 dm_dev_insert(dm_dev_t * dev) 181 { 182 dm_dev_t *dmv; 183 int r; 184 185 dmv = NULL; 186 r = 0; 187 188 KASSERT(dev != NULL); 189 mutex_enter(&dm_dev_mutex); 190 if (((dmv = dm_dev_lookup_uuid(dev->uuid)) == NULL) && 191 ((dmv = dm_dev_lookup_name(dev->name)) == NULL) && 192 ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) { 193 194 TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist); 195 196 } else 197 r = EEXIST; 198 199 mutex_exit(&dm_dev_mutex); 200 return r; 201 } 202 203 #ifdef notyet 204 /* 205 * Lookup device with its minor number. 206 */ 207 int 208 dm_dev_test_minor(int dm_dev_minor) 209 { 210 dm_dev_t *dmv; 211 212 mutex_enter(&dm_dev_mutex); 213 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { 214 if (dm_dev_minor == dmv->minor) { 215 mutex_exit(&dm_dev_mutex); 216 return 1; 217 } 218 } 219 mutex_exit(&dm_dev_mutex); 220 221 return 0; 222 } 223 #endif 224 225 /* 226 * dm_dev_lookup_devt look for selected device_t. We keep this routine 227 * outside of dm_dev_lookup because it is a temporally solution. 228 * 229 * TODO: This is a hack autoconf should be more flexible. 230 */ 231 dm_dev_t * 232 dm_dev_detach(device_t devt) 233 { 234 dm_dev_t *dmv; 235 236 mutex_enter(&dm_dev_mutex); 237 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { 238 if (devt == dmv->devt) { 239 disable_dev(dmv); 240 return dmv; 241 } 242 } 243 mutex_exit(&dm_dev_mutex); 244 245 return NULL; 246 } 247 248 /* 249 * Remove device selected with dm_dev from global list of devices. 250 */ 251 dm_dev_t * 252 dm_dev_rem(const char *dm_dev_name, const char *dm_dev_uuid, 253 int dm_dev_minor) 254 { 255 dm_dev_t *dmv; 256 dmv = NULL; 257 258 mutex_enter(&dm_dev_mutex); 259 260 if (dm_dev_minor > 0) 261 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) { 262 disable_dev(dmv); 263 return dmv; 264 } 265 if (dm_dev_name != NULL) 266 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) { 267 disable_dev(dmv); 268 return dmv; 269 } 270 if (dm_dev_uuid != NULL) 271 if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL) { 272 disable_dev(dmv); 273 return dmv; 274 } 275 mutex_exit(&dm_dev_mutex); 276 277 return NULL; 278 } 279 280 /* 281 * Destroy all devices created in device-mapper. Remove all tables 282 * free all allocated memmory. 283 */ 284 int 285 dm_dev_destroy(void) 286 { 287 dm_dev_t *dmv; 288 mutex_enter(&dm_dev_mutex); 289 290 while (TAILQ_FIRST(&dm_dev_list) != NULL) { 291 292 dmv = TAILQ_FIRST(&dm_dev_list); 293 294 TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list), 295 next_devlist); 296 297 mutex_enter(&dmv->dev_mtx); 298 299 while (dmv->ref_cnt != 0) 300 cv_wait(&dmv->dev_cv, &dmv->dev_mtx); 301 302 /* Destroy active table first. */ 303 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE); 304 305 /* Destroy inactive table if exits, too. */ 306 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE); 307 308 dm_table_head_destroy(&dmv->table_head); 309 310 mutex_exit(&dmv->dev_mtx); 311 mutex_destroy(&dmv->dev_mtx); 312 cv_destroy(&dmv->dev_cv); 313 314 (void) kmem_free(dmv, sizeof(dm_dev_t)); 315 } 316 mutex_exit(&dm_dev_mutex); 317 318 mutex_destroy(&dm_dev_mutex); 319 return 0; 320 } 321 322 /* 323 * Allocate new device entry. 324 */ 325 dm_dev_t * 326 dm_dev_alloc(void) 327 { 328 dm_dev_t *dmv; 329 330 dmv = kmem_zalloc(sizeof(dm_dev_t), KM_SLEEP); 331 dmv->diskp = kmem_zalloc(sizeof(struct disk), KM_SLEEP); 332 return dmv; 333 } 334 335 /* 336 * Freed device entry. 337 */ 338 int 339 dm_dev_free(dm_dev_t * dmv) 340 { 341 KASSERT(dmv != NULL); 342 343 mutex_destroy(&dmv->dev_mtx); 344 mutex_destroy(&dmv->diskp_mtx); 345 cv_destroy(&dmv->dev_cv); 346 347 if (dmv->diskp != NULL) 348 (void) kmem_free(dmv->diskp, sizeof(struct disk)); 349 350 (void) kmem_free(dmv, sizeof(dm_dev_t)); 351 352 return 0; 353 } 354 355 void 356 dm_dev_busy(dm_dev_t * dmv) 357 { 358 mutex_enter(&dmv->dev_mtx); 359 dmv->ref_cnt++; 360 mutex_exit(&dmv->dev_mtx); 361 } 362 363 void 364 dm_dev_unbusy(dm_dev_t * dmv) 365 { 366 KASSERT(dmv->ref_cnt != 0); 367 368 mutex_enter(&dmv->dev_mtx); 369 if (--dmv->ref_cnt == 0) 370 cv_broadcast(&dmv->dev_cv); 371 mutex_exit(&dmv->dev_mtx); 372 } 373 374 /* 375 * Return prop_array of dm_targer_list dictionaries. 376 */ 377 prop_array_t 378 dm_dev_prop_list(void) 379 { 380 dm_dev_t *dmv; 381 prop_array_t dev_array; 382 prop_dictionary_t dev_dict; 383 384 dev_array = prop_array_create(); 385 386 mutex_enter(&dm_dev_mutex); 387 388 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { 389 dev_dict = prop_dictionary_create(); 390 391 prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name); 392 prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor); 393 394 prop_array_add(dev_array, dev_dict); 395 prop_object_release(dev_dict); 396 } 397 398 mutex_exit(&dm_dev_mutex); 399 return dev_array; 400 } 401 402 /* 403 * Initialize global device mutex. 404 */ 405 int 406 dm_dev_init(void) 407 { 408 TAILQ_INIT(&dm_dev_list); /* initialize global dev list */ 409 mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE); 410 return 0; 411 } 412