1ff56536eSAlex Hornung /* $NetBSD: dm_target.c,v 1.12 2010/01/04 00:14:41 haad Exp $ */ 2ff56536eSAlex Hornung 3ff56536eSAlex Hornung /* 4ba2ce341SAlex Hornung * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> 5ff56536eSAlex Hornung * Copyright (c) 2008 The NetBSD Foundation, Inc. 6ff56536eSAlex Hornung * All rights reserved. 7ff56536eSAlex Hornung * 8ff56536eSAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 9ff56536eSAlex Hornung * by Adam Hamsik. 10ff56536eSAlex Hornung * 11ff56536eSAlex Hornung * Redistribution and use in source and binary forms, with or without 12ff56536eSAlex Hornung * modification, are permitted provided that the following conditions 13ff56536eSAlex Hornung * are met: 14ff56536eSAlex Hornung * 1. Redistributions of source code Must retain the above copyright 15ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer. 16ff56536eSAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 17ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer in the 18ff56536eSAlex Hornung * documentation and/or other materials provided with the distribution. 19ff56536eSAlex Hornung * 20ff56536eSAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21ff56536eSAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22ff56536eSAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23ff56536eSAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24ff56536eSAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25ff56536eSAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26ff56536eSAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27ff56536eSAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28ff56536eSAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29ff56536eSAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30ff56536eSAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 31ff56536eSAlex Hornung */ 32ff56536eSAlex Hornung 335b279a20SAlex Hornung #include <sys/malloc.h> 34ff56536eSAlex Hornung #include <sys/module.h> 357115a22bSAlex Hornung #include <sys/linker.h> 36*30ef4508STomohiro Kusumi #include <cpu/atomic.h> 37a84e173eSAlex Hornung #include <dev/disk/dm/dm.h> 38*30ef4508STomohiro Kusumi #include <dev/disk/dm/netbsd-dm.h> 39ff56536eSAlex Hornung 40ff56536eSAlex Hornung static dm_target_t *dm_target_lookup_name(const char *); 41ff56536eSAlex Hornung 424e359672STomohiro Kusumi static TAILQ_HEAD(, dm_target) dm_target_list; 43ff56536eSAlex Hornung 44637b454aSTomohiro Kusumi static struct lock dm_target_mutex; 45ff56536eSAlex Hornung 46ff56536eSAlex Hornung void 47ff56536eSAlex Hornung dm_target_busy(dm_target_t *target) 48ff56536eSAlex Hornung { 495b279a20SAlex Hornung atomic_add_int(&target->ref_cnt, 1); 50ff56536eSAlex Hornung } 51b7c11cdaSTomohiro Kusumi 52ff56536eSAlex Hornung /* 53ff56536eSAlex Hornung * Release reference counter on target. 54ff56536eSAlex Hornung */ 55ff56536eSAlex Hornung void 56ff56536eSAlex Hornung dm_target_unbusy(dm_target_t *target) 57ff56536eSAlex Hornung { 585b279a20SAlex Hornung KKASSERT(target->ref_cnt > 0); 595b279a20SAlex Hornung atomic_subtract_int(&target->ref_cnt, 1); 60ff56536eSAlex Hornung } 61ff56536eSAlex Hornung 62ff56536eSAlex Hornung /* 637115a22bSAlex Hornung * Try to autoload the module for the requested target. 647115a22bSAlex Hornung */ 657115a22bSAlex Hornung dm_target_t * 667115a22bSAlex Hornung dm_target_autoload(const char *dm_target_name) 677115a22bSAlex Hornung { 687115a22bSAlex Hornung char mod_name[128]; 697115a22bSAlex Hornung dm_target_t *dmt; 707115a22bSAlex Hornung linker_file_t linker_file; 717115a22bSAlex Hornung int error; 727115a22bSAlex Hornung 737115a22bSAlex Hornung ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name); 747115a22bSAlex Hornung error = linker_reference_module(mod_name, NULL, &linker_file); 757115a22bSAlex Hornung if (error != 0) { 767115a22bSAlex Hornung kprintf("dm: could not autoload module for target %s\n", 777115a22bSAlex Hornung dm_target_name); 787115a22bSAlex Hornung return NULL; 797115a22bSAlex Hornung } 807115a22bSAlex Hornung 817115a22bSAlex Hornung dmt = dm_target_lookup(dm_target_name); 827115a22bSAlex Hornung if (dmt == NULL) { 837115a22bSAlex Hornung linker_release_module(NULL, NULL, linker_file); 847115a22bSAlex Hornung return NULL; 857115a22bSAlex Hornung } 867115a22bSAlex Hornung 877115a22bSAlex Hornung /* XXX: extra-big hack to allow users to kldunload the module */ 887115a22bSAlex Hornung linker_file->userrefs = 1; 897115a22bSAlex Hornung 907115a22bSAlex Hornung return dmt; 917115a22bSAlex Hornung } 927115a22bSAlex Hornung 937115a22bSAlex Hornung /* 94ff56536eSAlex Hornung * Lookup for target in global target list. 95ff56536eSAlex Hornung */ 96ff56536eSAlex Hornung dm_target_t * 97ff56536eSAlex Hornung dm_target_lookup(const char *dm_target_name) 98ff56536eSAlex Hornung { 99ff56536eSAlex Hornung dm_target_t *dmt; 100ff56536eSAlex Hornung 101ff56536eSAlex Hornung dmt = NULL; 102ff56536eSAlex Hornung 103ff56536eSAlex Hornung if (dm_target_name == NULL) 104ff56536eSAlex Hornung return NULL; 105ff56536eSAlex Hornung 1065b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_EXCLUSIVE); 107ff56536eSAlex Hornung 108ff56536eSAlex Hornung dmt = dm_target_lookup_name(dm_target_name); 109ff56536eSAlex Hornung if (dmt != NULL) 110ff56536eSAlex Hornung dm_target_busy(dmt); 111ff56536eSAlex Hornung 1125b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 113ff56536eSAlex Hornung 114ff56536eSAlex Hornung return dmt; 115ff56536eSAlex Hornung } 116b7c11cdaSTomohiro Kusumi 117ff56536eSAlex Hornung /* 118b7c11cdaSTomohiro Kusumi * Search for name in TAILQ and return apropriate pointer. 119ff56536eSAlex Hornung */ 120ff56536eSAlex Hornung static dm_target_t * 121ff56536eSAlex Hornung dm_target_lookup_name(const char *dm_target_name) 122ff56536eSAlex Hornung { 123ff56536eSAlex Hornung dm_target_t *dm_target; 124ff56536eSAlex Hornung 125ff56536eSAlex Hornung TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) { 1260d42d35bSAlex Hornung if (strcmp(dm_target_name, dm_target->name) == 0) 127ff56536eSAlex Hornung return dm_target; 128ff56536eSAlex Hornung } 129ff56536eSAlex Hornung 130ff56536eSAlex Hornung return NULL; 131ff56536eSAlex Hornung } 132b7c11cdaSTomohiro Kusumi 133ff56536eSAlex Hornung /* 134b7c11cdaSTomohiro Kusumi * Insert new target struct into the TAILQ. 135b7c11cdaSTomohiro Kusumi * dm_target contains name, version, function pointer to specific target 136b7c11cdaSTomohiro Kusumi * functions. 137ff56536eSAlex Hornung */ 138ff56536eSAlex Hornung int 139ff56536eSAlex Hornung dm_target_insert(dm_target_t *dm_target) 140ff56536eSAlex Hornung { 141ff56536eSAlex Hornung dm_target_t *dmt; 142ff56536eSAlex Hornung 143b4e97860STomohiro Kusumi if (dm_target->init == NULL) { 144b4e97860STomohiro Kusumi kprintf("dm: %s missing init\n", dm_target->name); 145b4e97860STomohiro Kusumi return EINVAL; 146b4e97860STomohiro Kusumi } 147b4e97860STomohiro Kusumi if (dm_target->destroy == NULL) { 148b4e97860STomohiro Kusumi kprintf("dm: %s missing destroy\n", dm_target->name); 149b4e97860STomohiro Kusumi return EINVAL; 150b4e97860STomohiro Kusumi } 151b4e97860STomohiro Kusumi if (dm_target->strategy == NULL) { 152b4e97860STomohiro Kusumi kprintf("dm: %s missing strategy\n", dm_target->name); 153b4e97860STomohiro Kusumi return EINVAL; 154b4e97860STomohiro Kusumi } 155b4e97860STomohiro Kusumi 1565b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_EXCLUSIVE); 157ff56536eSAlex Hornung 158ff56536eSAlex Hornung dmt = dm_target_lookup_name(dm_target->name); 159ff56536eSAlex Hornung if (dmt != NULL) { 1605b279a20SAlex Hornung kprintf("uhoh, target_insert EEXIST\n"); 1615b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 162ff56536eSAlex Hornung return EEXIST; 163ff56536eSAlex Hornung } 164ff56536eSAlex Hornung TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next); 165ff56536eSAlex Hornung 1665b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 167ff56536eSAlex Hornung 168ff56536eSAlex Hornung return 0; 169ff56536eSAlex Hornung } 170ff56536eSAlex Hornung 171ff56536eSAlex Hornung /* 172b7c11cdaSTomohiro Kusumi * Remove target from TAILQ, target is selected with it's name. 173ff56536eSAlex Hornung */ 174ff56536eSAlex Hornung int 175a6b470c8STomohiro Kusumi dm_target_remove(char *dm_target_name) 176ff56536eSAlex Hornung { 177ff56536eSAlex Hornung dm_target_t *dmt; 178ff56536eSAlex Hornung 1795b279a20SAlex Hornung KKASSERT(dm_target_name != NULL); 180ff56536eSAlex Hornung 1815b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_EXCLUSIVE); 182ff56536eSAlex Hornung 183ff56536eSAlex Hornung dmt = dm_target_lookup_name(dm_target_name); 184ff56536eSAlex Hornung if (dmt == NULL) { 1855b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 186ff56536eSAlex Hornung return ENOENT; 187ff56536eSAlex Hornung } 188ff56536eSAlex Hornung if (dmt->ref_cnt > 0) { 1895b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 190ff56536eSAlex Hornung return EBUSY; 191ff56536eSAlex Hornung } 192b7c11cdaSTomohiro Kusumi TAILQ_REMOVE(&dm_target_list, dmt, dm_target_next); 193ff56536eSAlex Hornung 1945b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 195ff56536eSAlex Hornung 196d880b9dfSTomohiro Kusumi dm_target_free(dmt); 197ff56536eSAlex Hornung 198ff56536eSAlex Hornung return 0; 199ff56536eSAlex Hornung } 200ff56536eSAlex Hornung 201ff56536eSAlex Hornung /* 202ff56536eSAlex Hornung * Allocate new target entry. 203ff56536eSAlex Hornung */ 204ff56536eSAlex Hornung dm_target_t * 205ff56536eSAlex Hornung dm_target_alloc(const char *name) 206ff56536eSAlex Hornung { 207efaf91a7STomohiro Kusumi dm_target_t *dmt; 208efaf91a7STomohiro Kusumi 209efaf91a7STomohiro Kusumi dmt = kmalloc(sizeof(*dmt), M_DM, M_WAITOK | M_ZERO); 210efaf91a7STomohiro Kusumi if (dmt == NULL) 211efaf91a7STomohiro Kusumi return NULL; 212efaf91a7STomohiro Kusumi 213efaf91a7STomohiro Kusumi if (name) 214efaf91a7STomohiro Kusumi strlcpy(dmt->name, name, sizeof(dmt->name)); 215efaf91a7STomohiro Kusumi 216efaf91a7STomohiro Kusumi return dmt; 217ff56536eSAlex Hornung } 218efaf91a7STomohiro Kusumi 219d880b9dfSTomohiro Kusumi int 220d880b9dfSTomohiro Kusumi dm_target_free(dm_target_t *dmt) 221d880b9dfSTomohiro Kusumi { 222d880b9dfSTomohiro Kusumi KKASSERT(dmt != NULL); 223d880b9dfSTomohiro Kusumi 224d880b9dfSTomohiro Kusumi kfree(dmt, M_DM); 225d880b9dfSTomohiro Kusumi 226d880b9dfSTomohiro Kusumi return 0; 227d880b9dfSTomohiro Kusumi } 228d880b9dfSTomohiro Kusumi 229ff56536eSAlex Hornung /* 230ff56536eSAlex Hornung * Return prop_array of dm_target dictionaries. 231ff56536eSAlex Hornung */ 232ff56536eSAlex Hornung prop_array_t 233ff56536eSAlex Hornung dm_target_prop_list(void) 234ff56536eSAlex Hornung { 235ff56536eSAlex Hornung prop_array_t target_array, ver; 236ff56536eSAlex Hornung prop_dictionary_t target_dict; 237ff56536eSAlex Hornung dm_target_t *dm_target; 238ff56536eSAlex Hornung 239ff56536eSAlex Hornung size_t i; 240ff56536eSAlex Hornung 241ff56536eSAlex Hornung target_array = prop_array_create(); 242ff56536eSAlex Hornung 2435b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_EXCLUSIVE); 244ff56536eSAlex Hornung 245ff56536eSAlex Hornung TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) { 246ff56536eSAlex Hornung 247ff56536eSAlex Hornung target_dict = prop_dictionary_create(); 248ff56536eSAlex Hornung ver = prop_array_create(); 249ff56536eSAlex Hornung prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME, 250ff56536eSAlex Hornung dm_target->name); 251ff56536eSAlex Hornung 252ff56536eSAlex Hornung for (i = 0; i < 3; i++) 253ff56536eSAlex Hornung prop_array_add_uint32(ver, dm_target->version[i]); 254ff56536eSAlex Hornung 255ff56536eSAlex Hornung prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver); 256ff56536eSAlex Hornung prop_array_add(target_array, target_dict); 257ff56536eSAlex Hornung 258ff56536eSAlex Hornung prop_object_release(ver); 259ff56536eSAlex Hornung prop_object_release(target_dict); 260ff56536eSAlex Hornung } 261ff56536eSAlex Hornung 2625b279a20SAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 263ff56536eSAlex Hornung 264ff56536eSAlex Hornung return target_array; 265ff56536eSAlex Hornung } 2665c411e8eSAlex Hornung 267b7c11cdaSTomohiro Kusumi /* 268b7c11cdaSTomohiro Kusumi * Initialize dm_target subsystem. 269b7c11cdaSTomohiro Kusumi */ 270ff56536eSAlex Hornung int 271ff56536eSAlex Hornung dm_target_init(void) 272ff56536eSAlex Hornung { 2734e359672STomohiro Kusumi TAILQ_INIT(&dm_target_list); /* initialize global target list */ 2745b279a20SAlex Hornung lockinit(&dm_target_mutex, "dmtrgt", 0, LK_CANRECURSE); 275ff56536eSAlex Hornung 2765c411e8eSAlex Hornung return 0; 277ff56536eSAlex Hornung } 2789fada28aSAlex Hornung 2799fada28aSAlex Hornung /* 2809fada28aSAlex Hornung * Destroy all targets and remove them from queue. 2817350b8b6STomohiro Kusumi * This routine is called from dmdestroy, before module 2829fada28aSAlex Hornung * is unloaded. 2839fada28aSAlex Hornung */ 2849fada28aSAlex Hornung int 2859fada28aSAlex Hornung dm_target_uninit(void) 2869fada28aSAlex Hornung { 2879fada28aSAlex Hornung dm_target_t *dm_target; 2889fada28aSAlex Hornung 2899fada28aSAlex Hornung lockmgr(&dm_target_mutex, LK_EXCLUSIVE); 2909fada28aSAlex Hornung 29133b3a12eSTomohiro Kusumi while ((dm_target = TAILQ_FIRST(&dm_target_list)) != NULL) { 29233b3a12eSTomohiro Kusumi TAILQ_REMOVE(&dm_target_list, dm_target, dm_target_next); 293d880b9dfSTomohiro Kusumi dm_target_free(dm_target); 2949fada28aSAlex Hornung } 29533b3a12eSTomohiro Kusumi KKASSERT(TAILQ_EMPTY(&dm_target_list)); 29633b3a12eSTomohiro Kusumi 2979fada28aSAlex Hornung lockmgr(&dm_target_mutex, LK_RELEASE); 2989fada28aSAlex Hornung 2999fada28aSAlex Hornung lockuninit(&dm_target_mutex); 3009fada28aSAlex Hornung 3019fada28aSAlex Hornung return 0; 3029fada28aSAlex Hornung } 303