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