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