sys/dev/disk/dm/dm_dev.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | /* $NetBSD: dm_dev.c,v 1.8 2010/01/04 00:19:08 haad Exp $ */ /* * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> * Copyright (c) 2008 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Adam Hamsik. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <sys/disk.h> #include <sys/disklabel.h> #include <sys/devicestat.h> #include <sys/device.h> #include <sys/udev.h> #include <sys/devfs.h> #include <sys/malloc.h> #include <machine/thread.h> #include <dev/disk/dm/dm.h> #include <dev/disk/dm/netbsd-dm.h> extern struct dev_ops dm_ops; static struct devfs_bitmap dm_minor_bitmap; uint64_t dm_dev_counter; static dm_dev_t *dm_dev_lookup_name(const char *); static dm_dev_t *dm_dev_lookup_uuid(const char *); static dm_dev_t *dm_dev_lookup_minor(int); static int dm_dev_destroy(dm_dev_t *); static dm_dev_t *dm_dev_alloc(const char *, const char *); static int dm_dev_free(dm_dev_t *); static TAILQ_HEAD(dm_dev_head, dm_dev) dm_dev_list; static struct lock dm_dev_mutex; static char dummy_uuid[DM_UUID_LEN]; /* * dm_dev_mutex must be held by caller before using disable_dev. */ static void disable_dev(dm_dev_t *dmv) { KKASSERT(lockstatus(&dm_dev_mutex, curthread) == LK_EXCLUSIVE); TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist); dm_dev_counter--; lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE); while (dmv->ref_cnt != 0) cv_wait(&dmv->dev_cv, &dmv->dev_mtx); lockmgr(&dmv->dev_mtx, LK_RELEASE); } static dm_dev_t * _dm_dev_lookup(const char *name, const char *uuid, int minor) { dm_dev_t *dmv; if (minor > 0) { if ((dmv = dm_dev_lookup_minor(minor))) return dmv; } if (name != NULL) { if ((dmv = dm_dev_lookup_name(name))) return dmv; } if (uuid != NULL) { if ((dmv = dm_dev_lookup_uuid(uuid))) return dmv; } return NULL; } /* * Generic function used to lookup dm_dev_t. Calling with name NULL * and uuid NULL is allowed. */ dm_dev_t * dm_dev_lookup(const char *name, const char *uuid, int minor) { dm_dev_t *dmv; lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); dmv = _dm_dev_lookup(name, uuid, minor); if (dmv) dm_dev_busy(dmv); lockmgr(&dm_dev_mutex, LK_RELEASE); return dmv; } /* * Lookup device with its minor number. */ static dm_dev_t * dm_dev_lookup_minor(int dm_dev_minor) { dm_dev_t *dmv; TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { if (dm_dev_minor == dmv->minor) return dmv; } return NULL; } /* * Lookup device with it's device name. */ static dm_dev_t * dm_dev_lookup_name(const char *dm_dev_name) { dm_dev_t *dmv; TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { if (strcmp(dm_dev_name, dmv->name) == 0) return dmv; } return NULL; } /* * Lookup device with it's device uuid. Used mostly by LVM2tools. */ static dm_dev_t * dm_dev_lookup_uuid(const char *dm_dev_uuid) { dm_dev_t *dmv; TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { if (strcmp(dm_dev_uuid, dmv->uuid) == 0) return dmv; } return NULL; } /* * Insert new device to the global list of devices. */ int dm_dev_insert(dm_dev_t *dev) { dm_dev_t *dmv; int r; dmv = NULL; r = 0; KKASSERT(dev != NULL); lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); /* * Ignore uuid lookup if dev->uuid is zero-filled. */ if (memcmp(dev->uuid, dummy_uuid, DM_UUID_LEN)) dmv = dm_dev_lookup_uuid(dev->uuid); if ((dmv == NULL) && (_dm_dev_lookup(dev->name, NULL, dev->minor) == NULL)) { TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist); dm_dev_counter++; } else { KKASSERT(dmv != NULL); r = EEXIST; } lockmgr(&dm_dev_mutex, LK_RELEASE); return r; } #if 0 /* * Remove device selected with dm_dev from global list of devices. */ dm_dev_t * dm_dev_lookup_evict(const char *name, const char *uuid, int minor) { dm_dev_t *dmv; lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); dmv = _dm_dev_lookup(name, uuid, minor); if (dmv) disable_dev(dmv); lockmgr(&dm_dev_mutex, LK_RELEASE); return dmv; } #endif int dm_dev_create(dm_dev_t **dmvp, const char *name, const char *uuid, int flags) { dm_dev_t *dmv; char name_buf[MAXPATHLEN]; int r, dm_minor; if ((dmv = dm_dev_alloc(name, uuid)) == NULL) return ENOMEM; dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0); dm_table_head_init(&dmv->table_head); lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE); cv_init(&dmv->dev_cv, "dm_dev"); if (flags & DM_READONLY_FLAG) dmv->flags |= DM_READONLY_FLAG; dmdebug("Creating device dm/%s\n", name); ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name); devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE, DEVSTAT_NO_ORDERED_TAGS, DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, DEVSTAT_PRIORITY_DISK); dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops); reference_dev(dmv->devt); /* Make sure the device are immediately available */ sync_devs(); dmv->devt->si_drv1 = dmv; dmv->devt->si_drv2 = dmv->diskp; dmv->minor = minor(dmv->devt); udev_dict_set_cstr(dmv->devt, "subsystem", "disk"); if ((r = dm_dev_insert(dmv)) != 0) dm_dev_destroy(dmv); *dmvp = dmv; return r; } static int dm_dev_destroy(dm_dev_t *dmv) { int minor; /* Destroy active table first. */ dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE); /* Destroy inactive table if exits, too. */ dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE); dm_table_head_destroy(&dmv->table_head); minor = dkunit(dmv->devt); disk_destroy(dmv->diskp); devstat_remove_entry(&dmv->stats); release_dev(dmv->devt); devfs_clone_bitmap_put(&dm_minor_bitmap, minor); lockuninit(&dmv->dev_mtx); cv_destroy(&dmv->dev_cv); /* Destroy device */ dm_dev_free(dmv); return 0; } /* * dm_dev_remove is called to completely destroy & remove a dm disk device. */ int dm_dev_remove(dm_dev_t *dmv) { /* Remove device from list and wait for refcnt to drop to zero */ lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); disable_dev(dmv); lockmgr(&dm_dev_mutex, LK_RELEASE); /* Destroy and free the device */ dm_dev_destroy(dmv); return 0; } int dm_dev_remove_all(int gentle) { dm_dev_t *dmv, *dmv2; int r; r = 0; lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); /* * Process in reverse order so that it can deal with inter-depentent * devices. */ TAILQ_FOREACH_REVERSE_MUTABLE(dmv, &dm_dev_list, dm_dev_head, next_devlist, dmv2) { if (gentle && dmv->is_open) { r = EBUSY; continue; } disable_dev(dmv); dm_dev_destroy(dmv); } lockmgr(&dm_dev_mutex, LK_RELEASE); return r; } /* * Allocate new device entry. */ static dm_dev_t * dm_dev_alloc(const char *name, const char*uuid) { dm_dev_t *dmv; dmv = kmalloc(sizeof(*dmv), M_DM, M_WAITOK | M_ZERO); if (dmv == NULL) return NULL; dmv->diskp = kmalloc(sizeof(*dmv->diskp), M_DM, M_WAITOK | M_ZERO); if (dmv->diskp == NULL) { kfree(dmv, M_DM); return NULL; } if (name) strlcpy(dmv->name, name, sizeof(dmv->name)); if (uuid) strncpy(dmv->uuid, uuid, sizeof(dmv->uuid)); return dmv; } /* * Freed device entry. */ static int dm_dev_free(dm_dev_t *dmv) { KKASSERT(dmv != NULL); if (dmv->diskp != NULL) kfree(dmv->diskp, M_DM); kfree(dmv, M_DM); return 0; } void dm_dev_busy(dm_dev_t *dmv) { lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE); dmv->ref_cnt++; lockmgr(&dmv->dev_mtx, LK_RELEASE); } void dm_dev_unbusy(dm_dev_t *dmv) { KKASSERT(dmv->ref_cnt != 0); lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE); if (--dmv->ref_cnt == 0) cv_broadcast(&dmv->dev_cv); lockmgr(&dmv->dev_mtx, LK_RELEASE); } /* * Return prop_array of dm_targer_list dictionaries. */ prop_array_t dm_dev_prop_list(void) { dm_dev_t *dmv; prop_array_t dev_array; prop_dictionary_t dev_dict; dev_array = prop_array_create(); lockmgr(&dm_dev_mutex, LK_EXCLUSIVE); TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) { dev_dict = prop_dictionary_create(); prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name); prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor); prop_array_add(dev_array, dev_dict); prop_object_release(dev_dict); } lockmgr(&dm_dev_mutex, LK_RELEASE); return dev_array; } /* * Initialize global device mutex. */ int dm_dev_init(void) { TAILQ_INIT(&dm_dev_list); /* initialize global dev list */ lockinit(&dm_dev_mutex, "dmdevlist", 0, LK_CANRECURSE); devfs_clone_bitmap_init(&dm_minor_bitmap); memset(dummy_uuid, 0, sizeof(dummy_uuid)); return 0; } /* * Destroy all devices created in device-mapper. Remove all tables * free all allocated memmory. */ int dm_dev_uninit(void) { /* Force removal of all devices */ dm_dev_remove_all(0); lockuninit(&dm_dev_mutex); return 0; } |