Home | History | Annotate | Line # | Download | only in dm
dm_target_stripe.c revision 1.7
      1 /*$NetBSD: dm_target_stripe.c,v 1.7 2009/09/09 22:38:49 haad Exp $*/
      2 
      3 /*
      4  * Copyright (c) 2009 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  * This file implements initial version of device-mapper stripe target.
     34  */
     35 #include <sys/types.h>
     36 #include <sys/param.h>
     37 
     38 #include <sys/buf.h>
     39 #include <sys/kmem.h>
     40 #include <sys/vnode.h>
     41 
     42 #include "dm.h"
     43 #include "netbsd-dm.h"
     44 
     45 #ifdef DM_TARGET_MODULE
     46 /*
     47  * Every target can be compiled directly to dm driver or as a
     48  * separate module this part of target is used for loading targets
     49  * to dm driver.
     50  * Target can be unloaded from kernel only if there are no users of
     51  * it e.g. there are no devices which uses that target.
     52  */
     53 #include <sys/kernel.h>
     54 #include <sys/module.h>
     55 
     56 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
     57 
     58 static int
     59 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
     60 {
     61 	dm_target_t *dmt;
     62 	int r;
     63 	dmt = NULL;
     64 
     65 	switch (cmd) {
     66 	case MODULE_CMD_INIT:
     67 		if ((dmt = dm_target_lookup("stripe")) != NULL) {
     68 			dm_target_unbusy(dmt);
     69 			return EEXIST;
     70 		}
     71 
     72 		dmt = dm_target_alloc("stripe");
     73 
     74 		dmt->version[0] = 1;
     75 		dmt->version[1] = 0;
     76 		dmt->version[2] = 0;
     77 		strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
     78 		dmt->init = &dm_target_stripe_init;
     79 		dmt->status = &dm_target_stripe_status;
     80 		dmt->strategy = &dm_target_stripe_strategy;
     81 		dmt->deps = &dm_target_stripe_deps;
     82 		dmt->destroy = &dm_target_stripe_destroy;
     83 		dmt->upcall = &dm_target_stripe_upcall;
     84 
     85 		r = dm_target_insert(dmt);
     86 
     87 		break;
     88 
     89 	case MODULE_CMD_FINI:
     90 		r = dm_target_rem("stripe");
     91 		break;
     92 
     93 	case MODULE_CMD_STAT:
     94 		return ENOTTY;
     95 
     96 	default:
     97 		return ENOTTY;
     98 	}
     99 
    100 	return r;
    101 }
    102 
    103 #endif
    104 
    105 /*
    106  * Init function called from dm_table_load_ioctl.
    107  * Example line sent to dm from lvm tools when using striped target.
    108  * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
    109  * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
    110  */
    111 int
    112 dm_target_stripe_init(dm_dev_t *dmv, void **target_config, prop_dictionary_t dict)
    113 {
    114 	dm_target_stripe_config_t *tsc;
    115 	prop_array_t dev_array;
    116 	prop_dictionary_t dev_dict1, dev_dict2;
    117 
    118 	uint64_t stripes, chunk_size, offset1, offset2;
    119 	const char *device1, *device2;
    120 
    121 	if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_STRIPES,
    122 		&stripes) == false)
    123 		return EINVAL;
    124 
    125 	if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_CHUNKSIZE,
    126 		&chunk_size) == false)
    127 		return EINVAL;
    128 
    129 	dev_array = prop_dictionary_get(dict, DM_TARGET_STRIPE_DEVARRAY);
    130 
    131 	/* XXX Support for more than 2 devices */
    132 	dev_dict1 = prop_array_get(dev_array, 0);
    133 	dev_dict2 = prop_array_get(dev_array, 1);
    134 
    135 	if(dev_dict2 == NULL || dev_dict1 == NULL)
    136 		return EINVAL;
    137 
    138         prop_dictionary_get_cstring_nocopy(dev_dict1, DM_TARGET_STRIPE_DEVICE, &device1);
    139 	prop_dictionary_get_uint64(dev_dict1, DM_TARGET_STRIPE_OFFSET, &offset1);
    140 	prop_dictionary_get_cstring_nocopy(dev_dict2, DM_TARGET_STRIPE_DEVICE, &device2);
    141 	prop_dictionary_get_uint64(dev_dict2, DM_TARGET_STRIPE_OFFSET, &offset2);
    142 
    143 	aprint_debug("Stripe target init function called!!\n");
    144 
    145 	aprint_debug("Stripe target chunk size %"PRIu64" number of stripes %"PRIu64"\n", chunk_size, stripes);
    146 	aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device1, offset1);
    147 	aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device2, offset2);
    148 
    149 	if (stripes > MAX_STRIPES)
    150 		return ENOTSUP;
    151 
    152 	if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP))
    153 	    == NULL)
    154 		return ENOMEM;
    155 
    156 	/* Insert dmp to global pdev list */
    157 	if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(device1)) == NULL)
    158 		return ENOENT;
    159 
    160 	/* Insert dmp to global pdev list */
    161 	if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(device2)) == NULL)
    162 		return ENOENT;
    163 
    164 	tsc->stripe_devs[0].offset = offset1;
    165 	tsc->stripe_devs[1].offset = offset2;
    166 
    167 	tsc->stripe_chunksize = chunk_size;
    168 	tsc->stripe_num = (uint8_t)stripes;
    169 
    170 	*target_config = tsc;
    171 
    172 	dmv->dev_type = DM_STRIPE_DEV;
    173 
    174 	return 0;
    175 }
    176 
    177 /* Status routine called to get params string. */
    178 char *
    179 dm_target_stripe_status(void *target_config)
    180 {
    181 	dm_target_stripe_config_t *tsc;
    182 	char *params;
    183 
    184 	tsc = target_config;
    185 
    186 	if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
    187 		return NULL;
    188 
    189 	snprintf(params, DM_MAX_PARAMS_SIZE, "%d %"PRIu64" %s %"PRIu64" %s %"PRIu64,
    190 	    tsc->stripe_num, tsc->stripe_chunksize,
    191 	    tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset,
    192 	    tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset);
    193 
    194 	return params;
    195 }
    196 
    197 /* Strategy routine called from dm_strategy. */
    198 int
    199 dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
    200 {
    201 	dm_target_stripe_config_t *tsc;
    202 	struct buf *nestbuf;
    203 	uint64_t blkno, blkoff;
    204 	uint64_t stripe, stripe_blknr;
    205 	uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
    206 	int stripe_devnr;
    207 
    208 	tsc = table_en->target_config;
    209 	if (tsc == NULL)
    210 		return 0;
    211 
    212 /*	aprint_debug("Stripe target read function called %" PRIu64 "!!\n",
    213 	tlc->offset);*/
    214 
    215 	/* calculate extent of request */
    216 	KASSERT(bp->b_resid % DEV_BSIZE == 0);
    217 
    218 	blkno  = bp->b_blkno;
    219 	blkoff = 0;
    220 	num_blks = bp->b_resid / DEV_BSIZE;
    221 	for (;;) {
    222 		/* blockno to strip piece nr */
    223 		stripe     = blkno / tsc->stripe_chunksize;
    224 		stripe_off = blkno % tsc->stripe_chunksize;
    225 
    226 		/* where we are inside the strip */
    227 		stripe_devnr = stripe % tsc->stripe_num;
    228 		stripe_blknr = stripe / tsc->stripe_num;
    229 
    230 		/* how much is left before we hit a boundary */
    231 		stripe_rest = tsc->stripe_chunksize - stripe_off;
    232 
    233 		/* issue this piece on stripe `stripe' */
    234 		issue_blks = MIN(stripe_rest, num_blks);
    235 		nestbuf = getiobuf(NULL, true);
    236 
    237 		nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
    238 		nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
    239 		nestbuf->b_blkno += tsc->stripe_devs[stripe_devnr].offset;
    240 
    241 		VOP_STRATEGY(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode, nestbuf);
    242 
    243 		blkno    += issue_blks;
    244 		blkoff   += issue_blks * DEV_BSIZE;
    245 		num_blks -= issue_blks;
    246 
    247 		if (num_blks <= 0)
    248 			break;
    249 	}
    250 
    251 	return 0;
    252 }
    253 
    254 /* Doesn't do anything here. */
    255 int
    256 dm_target_stripe_destroy(dm_table_entry_t *table_en)
    257 {
    258 	dm_target_stripe_config_t *tsc;
    259 
    260 	tsc = table_en->target_config;
    261 
    262 	if (tsc == NULL)
    263 		return 0;
    264 
    265 	dm_pdev_decr(tsc->stripe_devs[0].pdev);
    266 	dm_pdev_decr(tsc->stripe_devs[1].pdev);
    267 
    268 	/* Unbusy target so we can unload it */
    269 	dm_target_unbusy(table_en->target);
    270 
    271 	kmem_free(tsc, sizeof(dm_target_stripe_config_t));
    272 
    273 	table_en->target_config = NULL;
    274 
    275 	return 0;
    276 }
    277 
    278 /* Doesn't not need to do anything here. */
    279 int
    280 dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
    281 {
    282 	dm_target_stripe_config_t *tsc;
    283 	struct vattr va;
    284 
    285 	int error;
    286 
    287 	if (table_en->target_config == NULL)
    288 		return ENOENT;
    289 
    290 	tsc = table_en->target_config;
    291 
    292 	if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    293 		return error;
    294 
    295 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    296 
    297 	if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    298 		return error;
    299 
    300 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    301 
    302 	return 0;
    303 }
    304 
    305 /* Unsupported for this target. */
    306 int
    307 dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
    308 {
    309 	return 0;
    310 }
    311