Home | History | Annotate | Line # | Download | only in dm
dm_target_stripe.c revision 1.5.2.3
      1  1.5.2.3  yamt /*$NetBSD: dm_target_stripe.c,v 1.5.2.3 2009/06/20 07:20:21 yamt Exp $*/
      2  1.5.2.2  yamt 
      3  1.5.2.2  yamt /*
      4  1.5.2.2  yamt  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.5.2.2  yamt  * All rights reserved.
      6  1.5.2.2  yamt  *
      7  1.5.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.2.2  yamt  * by Adam Hamsik.
      9  1.5.2.2  yamt  *
     10  1.5.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.5.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.5.2.2  yamt  * are met:
     13  1.5.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.5.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.5.2.2  yamt  *
     19  1.5.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.5.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.5.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.5.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.5.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.5.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.5.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.5.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.5.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.5.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.5.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.5.2.2  yamt  */
     31  1.5.2.2  yamt 
     32  1.5.2.2  yamt /*
     33  1.5.2.2  yamt  * This file implements initial version of device-mapper stripe target.
     34  1.5.2.2  yamt  */
     35  1.5.2.2  yamt #include <sys/types.h>
     36  1.5.2.2  yamt #include <sys/param.h>
     37  1.5.2.2  yamt 
     38  1.5.2.2  yamt #include <sys/buf.h>
     39  1.5.2.2  yamt #include <sys/kmem.h>
     40  1.5.2.2  yamt #include <sys/vnode.h>
     41  1.5.2.2  yamt 
     42  1.5.2.2  yamt #include "dm.h"
     43  1.5.2.3  yamt #include "netbsd-dm.h"
     44  1.5.2.2  yamt 
     45  1.5.2.2  yamt #ifdef DM_TARGET_MODULE
     46  1.5.2.2  yamt /*
     47  1.5.2.2  yamt  * Every target can be compiled directly to dm driver or as a
     48  1.5.2.2  yamt  * separate module this part of target is used for loading targets
     49  1.5.2.2  yamt  * to dm driver.
     50  1.5.2.2  yamt  * Target can be unloaded from kernel only if there are no users of
     51  1.5.2.2  yamt  * it e.g. there are no devices which uses that target.
     52  1.5.2.2  yamt  */
     53  1.5.2.2  yamt #include <sys/kernel.h>
     54  1.5.2.2  yamt #include <sys/module.h>
     55  1.5.2.2  yamt 
     56  1.5.2.2  yamt MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
     57  1.5.2.2  yamt 
     58  1.5.2.2  yamt static int
     59  1.5.2.2  yamt dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
     60  1.5.2.2  yamt {
     61  1.5.2.2  yamt 	dm_target_t *dmt;
     62  1.5.2.2  yamt 	int r;
     63  1.5.2.2  yamt 	dmt = NULL;
     64  1.5.2.2  yamt 
     65  1.5.2.2  yamt 	switch (cmd) {
     66  1.5.2.2  yamt 	case MODULE_CMD_INIT:
     67  1.5.2.2  yamt 		if ((dmt = dm_target_lookup("stripe")) != NULL) {
     68  1.5.2.2  yamt 			dm_target_unbusy(dmt);
     69  1.5.2.2  yamt 			return EEXIST;
     70  1.5.2.2  yamt 		}
     71  1.5.2.2  yamt 
     72  1.5.2.2  yamt 		dmt = dm_target_alloc("stripe");
     73  1.5.2.2  yamt 
     74  1.5.2.2  yamt 		dmt->version[0] = 1;
     75  1.5.2.2  yamt 		dmt->version[1] = 0;
     76  1.5.2.2  yamt 		dmt->version[2] = 0;
     77  1.5.2.2  yamt 		strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
     78  1.5.2.2  yamt 		dmt->init = &dm_target_stripe_init;
     79  1.5.2.2  yamt 		dmt->status = &dm_target_stripe_status;
     80  1.5.2.2  yamt 		dmt->strategy = &dm_target_stripe_strategy;
     81  1.5.2.2  yamt 		dmt->deps = &dm_target_stripe_deps;
     82  1.5.2.2  yamt 		dmt->destroy = &dm_target_stripe_destroy;
     83  1.5.2.2  yamt 		dmt->upcall = &dm_target_stripe_upcall;
     84  1.5.2.2  yamt 
     85  1.5.2.2  yamt 		r = dm_target_insert(dmt);
     86  1.5.2.2  yamt 
     87  1.5.2.2  yamt 		break;
     88  1.5.2.2  yamt 
     89  1.5.2.2  yamt 	case MODULE_CMD_FINI:
     90  1.5.2.2  yamt 		r = dm_target_rem("stripe");
     91  1.5.2.2  yamt 		break;
     92  1.5.2.2  yamt 
     93  1.5.2.2  yamt 	case MODULE_CMD_STAT:
     94  1.5.2.2  yamt 		return ENOTTY;
     95  1.5.2.2  yamt 
     96  1.5.2.2  yamt 	default:
     97  1.5.2.2  yamt 		return ENOTTY;
     98  1.5.2.2  yamt 	}
     99  1.5.2.2  yamt 
    100  1.5.2.2  yamt 	return r;
    101  1.5.2.2  yamt }
    102  1.5.2.2  yamt 
    103  1.5.2.2  yamt #endif
    104  1.5.2.2  yamt 
    105  1.5.2.2  yamt /*
    106  1.5.2.2  yamt  * Init function called from dm_table_load_ioctl.
    107  1.5.2.2  yamt  * Example line sent to dm from lvm tools when using striped target.
    108  1.5.2.2  yamt  * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
    109  1.5.2.2  yamt  * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
    110  1.5.2.2  yamt  */
    111  1.5.2.2  yamt int
    112  1.5.2.3  yamt dm_target_stripe_init(dm_dev_t *dmv, void **target_config, prop_dictionary_t dict)
    113  1.5.2.2  yamt {
    114  1.5.2.2  yamt 	dm_target_stripe_config_t *tsc;
    115  1.5.2.3  yamt 	prop_array_t dev_array;
    116  1.5.2.3  yamt 	prop_dictionary_t dev_dict1, dev_dict2;
    117  1.5.2.3  yamt 
    118  1.5.2.3  yamt 	uint64_t stripes, chunk_size, offset1, offset2;
    119  1.5.2.3  yamt 	const char *device1, *device2;
    120  1.5.2.3  yamt 
    121  1.5.2.3  yamt 	if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_STRIPES,
    122  1.5.2.3  yamt 		&stripes) == false)
    123  1.5.2.2  yamt 		return EINVAL;
    124  1.5.2.2  yamt 
    125  1.5.2.3  yamt 	if (prop_dictionary_get_uint64(dict, DM_TARGET_STRIPE_CHUNKSIZE,
    126  1.5.2.3  yamt 		&chunk_size) == false)
    127  1.5.2.3  yamt 		return EINVAL;
    128  1.5.2.3  yamt 
    129  1.5.2.3  yamt 	dev_array = prop_dictionary_get(dict, DM_TARGET_STRIPE_DEVARRAY);
    130  1.5.2.2  yamt 
    131  1.5.2.3  yamt 	/* XXX Support for more than 2 devices */
    132  1.5.2.3  yamt 	dev_dict1 = prop_array_get(dev_array, 0);
    133  1.5.2.3  yamt 	dev_dict2 = prop_array_get(dev_array, 1);
    134  1.5.2.2  yamt 
    135  1.5.2.3  yamt 	if(dev_dict2 == NULL || dev_dict1 == NULL)
    136  1.5.2.3  yamt 		return EINVAL;
    137  1.5.2.3  yamt 
    138  1.5.2.3  yamt         prop_dictionary_get_cstring_nocopy(dev_dict1, DM_TARGET_STRIPE_DEVICE, &device1);
    139  1.5.2.3  yamt 	prop_dictionary_get_uint64(dev_dict1, DM_TARGET_STRIPE_OFFSET, &offset1);
    140  1.5.2.3  yamt 	prop_dictionary_get_cstring_nocopy(dev_dict2, DM_TARGET_STRIPE_DEVICE, &device2);
    141  1.5.2.3  yamt 	prop_dictionary_get_uint64(dev_dict2, DM_TARGET_STRIPE_OFFSET, &offset2);
    142  1.5.2.3  yamt 
    143  1.5.2.3  yamt 	aprint_debug("Stripe target init function called!!\n");
    144  1.5.2.3  yamt 
    145  1.5.2.3  yamt 	aprint_debug("Stripe target chunk size %"PRIu64" number of stripes %"PRIu64"\n", chunk_size, stripes);
    146  1.5.2.3  yamt 	aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device1, offset1);
    147  1.5.2.3  yamt 	aprint_debug("Stripe target device name %s -- offset %"PRIu64"\n", device2, offset2);
    148  1.5.2.2  yamt 
    149  1.5.2.3  yamt 	if (stripes > MAX_STRIPES)
    150  1.5.2.2  yamt 		return ENOTSUP;
    151  1.5.2.2  yamt 
    152  1.5.2.2  yamt 	if ((tsc = kmem_alloc(sizeof(dm_target_stripe_config_t), KM_NOSLEEP))
    153  1.5.2.2  yamt 	    == NULL)
    154  1.5.2.2  yamt 		return ENOMEM;
    155  1.5.2.2  yamt 
    156  1.5.2.2  yamt 	/* Insert dmp to global pdev list */
    157  1.5.2.3  yamt 	if ((tsc->stripe_devs[0].pdev = dm_pdev_insert(device1)) == NULL)
    158  1.5.2.2  yamt 		return ENOENT;
    159  1.5.2.2  yamt 
    160  1.5.2.2  yamt 	/* Insert dmp to global pdev list */
    161  1.5.2.3  yamt 	if ((tsc->stripe_devs[1].pdev = dm_pdev_insert(device2)) == NULL)
    162  1.5.2.2  yamt 		return ENOENT;
    163  1.5.2.2  yamt 
    164  1.5.2.3  yamt 	tsc->stripe_devs[0].offset = offset1;
    165  1.5.2.3  yamt 	tsc->stripe_devs[1].offset = offset2;
    166  1.5.2.2  yamt 
    167  1.5.2.2  yamt 	/* Save length of param string */
    168  1.5.2.3  yamt 	tsc->params_len = DM_MAX_PARAMS_SIZE;
    169  1.5.2.3  yamt 	tsc->stripe_chunksize = chunk_size;
    170  1.5.2.3  yamt 	tsc->stripe_num = (uint8_t)stripes;
    171  1.5.2.2  yamt 
    172  1.5.2.2  yamt 	*target_config = tsc;
    173  1.5.2.2  yamt 
    174  1.5.2.2  yamt 	dmv->dev_type = DM_STRIPE_DEV;
    175  1.5.2.2  yamt 
    176  1.5.2.2  yamt 	return 0;
    177  1.5.2.2  yamt }
    178  1.5.2.2  yamt 
    179  1.5.2.2  yamt /* Status routine called to get params string. */
    180  1.5.2.2  yamt char *
    181  1.5.2.2  yamt dm_target_stripe_status(void *target_config)
    182  1.5.2.2  yamt {
    183  1.5.2.2  yamt 	dm_target_stripe_config_t *tsc;
    184  1.5.2.2  yamt 	char *params;
    185  1.5.2.2  yamt 
    186  1.5.2.2  yamt 	tsc = target_config;
    187  1.5.2.2  yamt 
    188  1.5.2.2  yamt 	if ((params = kmem_alloc(tsc->params_len, KM_NOSLEEP)) == NULL)
    189  1.5.2.2  yamt 		return NULL;
    190  1.5.2.2  yamt 
    191  1.5.2.2  yamt 	snprintf(params, tsc->params_len, "%d %"PRIu64" %s %"PRIu64" %s %"PRIu64,
    192  1.5.2.2  yamt 	    tsc->stripe_num, tsc->stripe_chunksize,
    193  1.5.2.2  yamt 	    tsc->stripe_devs[0].pdev->name, tsc->stripe_devs[0].offset,
    194  1.5.2.2  yamt 	    tsc->stripe_devs[1].pdev->name, tsc->stripe_devs[1].offset);
    195  1.5.2.2  yamt 
    196  1.5.2.2  yamt 	return params;
    197  1.5.2.2  yamt }
    198  1.5.2.2  yamt 
    199  1.5.2.2  yamt /* Strategy routine called from dm_strategy. */
    200  1.5.2.2  yamt int
    201  1.5.2.2  yamt dm_target_stripe_strategy(dm_table_entry_t *table_en, struct buf *bp)
    202  1.5.2.2  yamt {
    203  1.5.2.2  yamt 	dm_target_stripe_config_t *tsc;
    204  1.5.2.2  yamt 	struct buf *nestbuf;
    205  1.5.2.2  yamt 	uint64_t blkno, blkoff;
    206  1.5.2.2  yamt 	uint64_t stripe, stripe_blknr;
    207  1.5.2.2  yamt 	uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
    208  1.5.2.2  yamt 	int stripe_devnr;
    209  1.5.2.2  yamt 
    210  1.5.2.2  yamt 	tsc = table_en->target_config;
    211  1.5.2.2  yamt 	if (tsc == NULL)
    212  1.5.2.2  yamt 		return 0;
    213  1.5.2.2  yamt 
    214  1.5.2.3  yamt /*	aprint_debug("Stripe target read function called %" PRIu64 "!!\n",
    215  1.5.2.2  yamt 	tlc->offset);*/
    216  1.5.2.2  yamt 
    217  1.5.2.2  yamt 	/* calculate extent of request */
    218  1.5.2.2  yamt 	KASSERT(bp->b_resid % DEV_BSIZE == 0);
    219  1.5.2.2  yamt 
    220  1.5.2.2  yamt 	blkno  = bp->b_blkno;
    221  1.5.2.2  yamt 	blkoff = 0;
    222  1.5.2.2  yamt 	num_blks = bp->b_resid / DEV_BSIZE;
    223  1.5.2.2  yamt 	for (;;) {
    224  1.5.2.2  yamt 		/* blockno to strip piece nr */
    225  1.5.2.2  yamt 		stripe     = blkno / tsc->stripe_chunksize;
    226  1.5.2.2  yamt 		stripe_off = blkno % tsc->stripe_chunksize;
    227  1.5.2.2  yamt 
    228  1.5.2.2  yamt 		/* where we are inside the strip */
    229  1.5.2.2  yamt 		stripe_devnr = stripe % tsc->stripe_num;
    230  1.5.2.2  yamt 		stripe_blknr = stripe / tsc->stripe_num;
    231  1.5.2.2  yamt 
    232  1.5.2.2  yamt 		/* how much is left before we hit a boundary */
    233  1.5.2.2  yamt 		stripe_rest = tsc->stripe_chunksize - stripe_off;
    234  1.5.2.2  yamt 
    235  1.5.2.2  yamt 		/* issue this piece on stripe `stripe' */
    236  1.5.2.2  yamt 		issue_blks = MIN(stripe_rest, num_blks);
    237  1.5.2.2  yamt 		nestbuf = getiobuf(NULL, true);
    238  1.5.2.2  yamt 
    239  1.5.2.2  yamt 		nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
    240  1.5.2.2  yamt 		nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
    241  1.5.2.2  yamt 		nestbuf->b_blkno += tsc->stripe_devs[stripe_devnr].offset;
    242  1.5.2.2  yamt 
    243  1.5.2.2  yamt 		VOP_STRATEGY(tsc->stripe_devs[stripe_devnr].pdev->pdev_vnode, nestbuf);
    244  1.5.2.2  yamt 
    245  1.5.2.2  yamt 		blkno    += issue_blks;
    246  1.5.2.2  yamt 		blkoff   += issue_blks * DEV_BSIZE;
    247  1.5.2.2  yamt 		num_blks -= issue_blks;
    248  1.5.2.2  yamt 
    249  1.5.2.2  yamt 		if (num_blks <= 0)
    250  1.5.2.2  yamt 			break;
    251  1.5.2.2  yamt 	}
    252  1.5.2.2  yamt 
    253  1.5.2.2  yamt 	return 0;
    254  1.5.2.2  yamt }
    255  1.5.2.2  yamt 
    256  1.5.2.2  yamt /* Doesn't do anything here. */
    257  1.5.2.2  yamt int
    258  1.5.2.2  yamt dm_target_stripe_destroy(dm_table_entry_t *table_en)
    259  1.5.2.2  yamt {
    260  1.5.2.2  yamt 	dm_target_stripe_config_t *tsc;
    261  1.5.2.2  yamt 
    262  1.5.2.2  yamt 	tsc = table_en->target_config;
    263  1.5.2.2  yamt 
    264  1.5.2.2  yamt 	if (tsc == NULL)
    265  1.5.2.2  yamt 		return 0;
    266  1.5.2.2  yamt 
    267  1.5.2.2  yamt 	dm_pdev_decr(tsc->stripe_devs[0].pdev);
    268  1.5.2.2  yamt 	dm_pdev_decr(tsc->stripe_devs[1].pdev);
    269  1.5.2.2  yamt 
    270  1.5.2.2  yamt 	/* Unbusy target so we can unload it */
    271  1.5.2.2  yamt 	dm_target_unbusy(table_en->target);
    272  1.5.2.2  yamt 
    273  1.5.2.2  yamt 	kmem_free(tsc, sizeof(dm_target_stripe_config_t));
    274  1.5.2.2  yamt 
    275  1.5.2.2  yamt 	table_en->target_config = NULL;
    276  1.5.2.2  yamt 
    277  1.5.2.2  yamt 	return 0;
    278  1.5.2.2  yamt }
    279  1.5.2.2  yamt 
    280  1.5.2.2  yamt /* Doesn't not need to do anything here. */
    281  1.5.2.2  yamt int
    282  1.5.2.2  yamt dm_target_stripe_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
    283  1.5.2.2  yamt {
    284  1.5.2.2  yamt 	dm_target_stripe_config_t *tsc;
    285  1.5.2.2  yamt 	struct vattr va;
    286  1.5.2.2  yamt 
    287  1.5.2.2  yamt 	int error;
    288  1.5.2.2  yamt 
    289  1.5.2.2  yamt 	if (table_en->target_config == NULL)
    290  1.5.2.2  yamt 		return ENOENT;
    291  1.5.2.2  yamt 
    292  1.5.2.2  yamt 	tsc = table_en->target_config;
    293  1.5.2.2  yamt 
    294  1.5.2.2  yamt 	if ((error = VOP_GETATTR(tsc->stripe_devs[0].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    295  1.5.2.2  yamt 		return error;
    296  1.5.2.2  yamt 
    297  1.5.2.2  yamt 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    298  1.5.2.2  yamt 
    299  1.5.2.2  yamt 	if ((error = VOP_GETATTR(tsc->stripe_devs[1].pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    300  1.5.2.2  yamt 		return error;
    301  1.5.2.2  yamt 
    302  1.5.2.2  yamt 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    303  1.5.2.2  yamt 
    304  1.5.2.2  yamt 	return 0;
    305  1.5.2.2  yamt }
    306  1.5.2.2  yamt 
    307  1.5.2.2  yamt /* Unsupported for this target. */
    308  1.5.2.2  yamt int
    309  1.5.2.2  yamt dm_target_stripe_upcall(dm_table_entry_t *table_en, struct buf *bp)
    310  1.5.2.2  yamt {
    311  1.5.2.2  yamt 	return 0;
    312  1.5.2.2  yamt }
    313