2010-12-23 17:58:13 +03:00
|
|
|
/* $NetBSD: dm_target.c,v 1.15 2010/12/23 14:58:13 mlelstv Exp $ */
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <sys/kmem.h>
|
2009-02-20 02:07:32 +03:00
|
|
|
#include <sys/module.h>
|
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
#include "netbsd-dm.h"
|
|
|
|
#include "dm.h"
|
|
|
|
|
2010-01-04 03:12:22 +03:00
|
|
|
static dm_target_t *dm_target_lookup_name(const char *);
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
TAILQ_HEAD(dm_target_head, dm_target);
|
|
|
|
|
|
|
|
static struct dm_target_head dm_target_list =
|
|
|
|
TAILQ_HEAD_INITIALIZER(dm_target_list);
|
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
kmutex_t dm_target_mutex;
|
|
|
|
|
|
|
|
/*
|
2009-08-16 15:02:24 +04:00
|
|
|
* Called indirectly from dm_table_load_ioctl to mark target as used.
|
2008-12-19 19:30:41 +03:00
|
|
|
*/
|
|
|
|
void
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_busy(dm_target_t * target)
|
2008-12-19 19:30:41 +03:00
|
|
|
{
|
2009-02-20 02:07:32 +03:00
|
|
|
atomic_inc_32(&target->ref_cnt);
|
2008-12-19 19:30:41 +03:00
|
|
|
}
|
2009-02-20 02:07:32 +03:00
|
|
|
/*
|
|
|
|
* Release reference counter on target.
|
|
|
|
*/
|
2008-12-19 19:30:41 +03:00
|
|
|
void
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_unbusy(dm_target_t * target)
|
2008-12-19 19:30:41 +03:00
|
|
|
{
|
2009-02-20 02:07:32 +03:00
|
|
|
KASSERT(target->ref_cnt > 0);
|
|
|
|
atomic_dec_32(&target->ref_cnt);
|
2008-12-19 19:30:41 +03:00
|
|
|
}
|
2009-02-20 02:07:32 +03:00
|
|
|
/*
|
|
|
|
* Try to autoload target module if it was not found in current
|
|
|
|
* target list.
|
|
|
|
*/
|
|
|
|
dm_target_t *
|
|
|
|
dm_target_autoload(const char *dm_target_name)
|
|
|
|
{
|
2009-02-20 02:20:27 +03:00
|
|
|
char name[30];
|
2009-02-20 02:07:32 +03:00
|
|
|
u_int gen;
|
|
|
|
dm_target_t *dmt;
|
|
|
|
|
2009-02-20 02:20:27 +03:00
|
|
|
snprintf(name, sizeof(name), "dm_target_%s", dm_target_name);
|
2010-01-04 03:12:22 +03:00
|
|
|
name[29] = '\0';
|
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
do {
|
|
|
|
gen = module_gen;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
/* Try to autoload target module */
|
2009-02-20 02:20:27 +03:00
|
|
|
(void) module_autoload(name, MODULE_CLASS_MISC);
|
2010-01-04 03:12:22 +03:00
|
|
|
} while (gen != module_gen);
|
2009-02-20 02:07:32 +03:00
|
|
|
|
|
|
|
mutex_enter(&dm_target_mutex);
|
|
|
|
dmt = dm_target_lookup_name(dm_target_name);
|
2009-02-20 14:14:11 +03:00
|
|
|
if (dmt != NULL)
|
|
|
|
dm_target_busy(dmt);
|
2009-02-20 02:07:32 +03:00
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
return dmt;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Lookup for target in global target list.
|
|
|
|
*/
|
2008-12-19 19:30:41 +03:00
|
|
|
dm_target_t *
|
|
|
|
dm_target_lookup(const char *dm_target_name)
|
|
|
|
{
|
|
|
|
dm_target_t *dmt;
|
|
|
|
|
|
|
|
dmt = NULL;
|
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
if (dm_target_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_enter(&dm_target_mutex);
|
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
dmt = dm_target_lookup_name(dm_target_name);
|
2008-12-19 19:30:41 +03:00
|
|
|
if (dmt != NULL)
|
|
|
|
dm_target_busy(dmt);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
|
|
|
return dmt;
|
2008-12-19 19:30:41 +03:00
|
|
|
}
|
2008-12-19 18:24:03 +03:00
|
|
|
/*
|
|
|
|
* Search for name in TAIL and return apropriate pointer.
|
|
|
|
*/
|
2010-01-04 03:12:22 +03:00
|
|
|
static dm_target_t *
|
2008-12-19 18:24:03 +03:00
|
|
|
dm_target_lookup_name(const char *dm_target_name)
|
|
|
|
{
|
|
|
|
dm_target_t *dm_target;
|
2010-01-04 03:12:22 +03:00
|
|
|
int dlen;
|
|
|
|
int slen;
|
2008-12-19 18:24:03 +03:00
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
slen = strlen(dm_target_name) + 1;
|
2008-12-19 18:24:03 +03:00
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
|
|
|
|
dlen = strlen(dm_target->name) + 1;
|
2008-12-19 18:24:03 +03:00
|
|
|
if (dlen != slen)
|
|
|
|
continue;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2009-02-20 02:07:32 +03:00
|
|
|
if (strncmp(dm_target_name, dm_target->name, slen) == 0)
|
2008-12-19 19:30:41 +03:00
|
|
|
return dm_target;
|
2008-12-19 18:24:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Insert new target struct into the TAIL.
|
|
|
|
* dm_target
|
|
|
|
* contains name, version, function pointer to specifif target functions.
|
|
|
|
*/
|
|
|
|
int
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_insert(dm_target_t * dm_target)
|
2008-12-19 18:24:03 +03:00
|
|
|
{
|
|
|
|
dm_target_t *dmt;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_enter(&dm_target_mutex);
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
dmt = dm_target_lookup_name(dm_target->name);
|
2008-12-19 19:30:41 +03:00
|
|
|
if (dmt != NULL) {
|
|
|
|
mutex_exit(&dm_target_mutex);
|
2008-12-19 18:24:03 +03:00
|
|
|
return EEXIST;
|
2008-12-19 19:30:41 +03:00
|
|
|
}
|
2008-12-19 18:24:03 +03:00
|
|
|
TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
|
2008-12-19 19:30:41 +03:00
|
|
|
|
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove target from TAIL, target is selected with it's name.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dm_target_rem(char *dm_target_name)
|
|
|
|
{
|
2008-12-19 19:30:41 +03:00
|
|
|
dm_target_t *dmt;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
KASSERT(dm_target_name != NULL);
|
2008-12-19 19:30:41 +03:00
|
|
|
|
|
|
|
mutex_enter(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
dmt = dm_target_lookup_name(dm_target_name);
|
|
|
|
if (dmt == NULL) {
|
|
|
|
mutex_exit(&dm_target_mutex);
|
2008-12-19 18:24:03 +03:00
|
|
|
return ENOENT;
|
2008-12-19 19:30:41 +03:00
|
|
|
}
|
|
|
|
if (dmt->ref_cnt > 0) {
|
|
|
|
mutex_exit(&dm_target_mutex);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
2008-12-19 18:24:03 +03:00
|
|
|
TAILQ_REMOVE(&dm_target_list,
|
2008-12-19 19:30:41 +03:00
|
|
|
dmt, dm_target_next);
|
|
|
|
|
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
|
|
|
(void) kmem_free(dmt, sizeof(dm_target_t));
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Destroy all targets and remove them from queue.
|
|
|
|
* This routine is called from dm_detach, before module
|
|
|
|
* is unloaded.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dm_target_destroy(void)
|
|
|
|
{
|
|
|
|
dm_target_t *dm_target;
|
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_enter(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
while (TAILQ_FIRST(&dm_target_list) != NULL) {
|
2008-12-19 18:24:03 +03:00
|
|
|
|
|
|
|
dm_target = TAILQ_FIRST(&dm_target_list);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
TAILQ_REMOVE(&dm_target_list, TAILQ_FIRST(&dm_target_list),
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_next);
|
|
|
|
|
|
|
|
(void) kmem_free(dm_target, sizeof(dm_target_t));
|
2008-12-19 18:24:03 +03:00
|
|
|
}
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-21 03:59:39 +03:00
|
|
|
mutex_destroy(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate new target entry.
|
|
|
|
*/
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_t *
|
2008-12-19 18:24:03 +03:00
|
|
|
dm_target_alloc(const char *name)
|
|
|
|
{
|
2009-09-10 02:38:49 +04:00
|
|
|
return kmem_zalloc(sizeof(dm_target_t), KM_SLEEP);
|
2008-12-19 18:24:03 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Return prop_array of dm_target dictionaries.
|
|
|
|
*/
|
|
|
|
prop_array_t
|
|
|
|
dm_target_prop_list(void)
|
|
|
|
{
|
2010-01-04 03:12:22 +03:00
|
|
|
prop_array_t target_array, ver;
|
2008-12-19 18:24:03 +03:00
|
|
|
prop_dictionary_t target_dict;
|
|
|
|
dm_target_t *dm_target;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
target_array = prop_array_create();
|
2008-12-19 19:30:41 +03:00
|
|
|
|
|
|
|
mutex_enter(&dm_target_mutex);
|
2008-12-19 18:24:03 +03:00
|
|
|
|
2010-01-04 03:12:22 +03:00
|
|
|
TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
|
|
|
|
|
|
|
|
target_dict = prop_dictionary_create();
|
2008-12-19 18:24:03 +03:00
|
|
|
ver = prop_array_create();
|
|
|
|
prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
|
|
|
|
dm_target->name);
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
prop_array_add_uint32(ver, dm_target->version[i]);
|
|
|
|
|
|
|
|
prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
|
|
|
|
prop_array_add(target_array, target_dict);
|
|
|
|
|
|
|
|
prop_object_release(ver);
|
|
|
|
prop_object_release(target_dict);
|
|
|
|
}
|
|
|
|
|
2008-12-19 19:30:41 +03:00
|
|
|
mutex_exit(&dm_target_mutex);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
return target_array;
|
|
|
|
}
|
|
|
|
/* Initialize dm_target subsystem. */
|
|
|
|
int
|
|
|
|
dm_target_init(void)
|
|
|
|
{
|
2010-01-04 03:12:22 +03:00
|
|
|
dm_target_t *dmt, *dmt3;
|
2008-12-19 18:24:03 +03:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = 0;
|
2008-12-19 19:30:41 +03:00
|
|
|
|
|
|
|
mutex_init(&dm_target_mutex, MUTEX_DEFAULT, IPL_NONE);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
dmt = dm_target_alloc("linear");
|
|
|
|
dmt3 = dm_target_alloc("striped");
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
dmt->version[0] = 1;
|
|
|
|
dmt->version[1] = 0;
|
|
|
|
dmt->version[2] = 2;
|
|
|
|
strlcpy(dmt->name, "linear", DM_MAX_TYPE_NAME);
|
|
|
|
dmt->init = &dm_target_linear_init;
|
|
|
|
dmt->status = &dm_target_linear_status;
|
|
|
|
dmt->strategy = &dm_target_linear_strategy;
|
2010-05-18 19:10:38 +04:00
|
|
|
dmt->sync = &dm_target_linear_sync;
|
2008-12-19 18:24:03 +03:00
|
|
|
dmt->deps = &dm_target_linear_deps;
|
|
|
|
dmt->destroy = &dm_target_linear_destroy;
|
|
|
|
dmt->upcall = &dm_target_linear_upcall;
|
2010-12-23 17:58:13 +03:00
|
|
|
dmt->secsize = &dm_target_linear_secsize;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
r = dm_target_insert(dmt);
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
dmt3->version[0] = 1;
|
|
|
|
dmt3->version[1] = 0;
|
|
|
|
dmt3->version[2] = 3;
|
|
|
|
strlcpy(dmt3->name, "striped", DM_MAX_TYPE_NAME);
|
2009-01-02 03:42:31 +03:00
|
|
|
dmt3->init = &dm_target_stripe_init;
|
|
|
|
dmt3->status = &dm_target_stripe_status;
|
|
|
|
dmt3->strategy = &dm_target_stripe_strategy;
|
2010-05-18 19:10:38 +04:00
|
|
|
dmt3->sync = &dm_target_stripe_sync;
|
2009-01-02 03:42:31 +03:00
|
|
|
dmt3->deps = &dm_target_stripe_deps;
|
|
|
|
dmt3->destroy = &dm_target_stripe_destroy;
|
|
|
|
dmt3->upcall = &dm_target_stripe_upcall;
|
2010-12-23 17:58:13 +03:00
|
|
|
dmt3->secsize = &dm_target_stripe_secsize;
|
2010-01-04 03:12:22 +03:00
|
|
|
|
2008-12-19 18:24:03 +03:00
|
|
|
r = dm_target_insert(dmt3);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|