Home | History | Annotate | Line # | Download | only in dm
dm_target_stripe.c revision 1.11
      1 /*$NetBSD: dm_target_stripe.c,v 1.11 2010/10/23 21:18:54 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 
     44 #ifdef DM_TARGET_MODULE
     45 /*
     46  * Every target can be compiled directly to dm driver or as a
     47  * separate module this part of target is used for loading targets
     48  * to dm driver.
     49  * Target can be unloaded from kernel only if there are no users of
     50  * it e.g. there are no devices which uses that target.
     51  */
     52 #include <sys/kernel.h>
     53 #include <sys/module.h>
     54 
     55 MODULE(MODULE_CLASS_MISC, dm_target_stripe, NULL);
     56 
     57 static int
     58 dm_target_stripe_modcmd(modcmd_t cmd, void *arg)
     59 {
     60 	dm_target_t *dmt;
     61 	int r;
     62 	dmt = NULL;
     63 
     64 	switch (cmd) {
     65 	case MODULE_CMD_INIT:
     66 		if ((dmt = dm_target_lookup("stripe")) != NULL) {
     67 			dm_target_unbusy(dmt);
     68 			return EEXIST;
     69 		}
     70 		dmt = dm_target_alloc("stripe");
     71 
     72 		dmt->version[0] = 1;
     73 		dmt->version[1] = 0;
     74 		dmt->version[2] = 0;
     75 		strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
     76 		dmt->init = &dm_target_stripe_init;
     77 		dmt->status = &dm_target_stripe_status;
     78 		dmt->strategy = &dm_target_stripe_strategy;
     79 		dmt->sync = &dm_target_stripe_sync;
     80 		dmt->deps = &dm_target_stripe_deps;
     81 		dmt->destroy = &dm_target_stripe_destroy;
     82 		dmt->upcall = &dm_target_stripe_upcall;
     83 
     84 		r = dm_target_insert(dmt);
     85 
     86 		break;
     87 
     88 	case MODULE_CMD_FINI:
     89 		r = dm_target_rem("stripe");
     90 		break;
     91 
     92 	case MODULE_CMD_STAT:
     93 		return ENOTTY;
     94 
     95 	default:
     96 		return ENOTTY;
     97 	}
     98 
     99 	return r;
    100 }
    101 #endif
    102 
    103 /*
    104  * Init function called from dm_table_load_ioctl.
    105  * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset
    106  * pair in the parameters.
    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, char *params)
    113 {
    114 	dm_target_linear_config_t *tlc;
    115 	dm_target_stripe_config_t *tsc;
    116 	size_t len;
    117 	char **ap, *argv[10];
    118 	int strpc, strpi;
    119 
    120 	if (params == NULL)
    121 		return EINVAL;
    122 
    123 	len = strlen(params) + 1;
    124 
    125 	/*
    126 	 * Parse a string, containing tokens delimited by white space,
    127 	 * into an argument vector
    128 	 */
    129 	for (ap = argv; ap < &argv[9] &&
    130 	    (*ap = strsep(&params, " \t")) != NULL;) {
    131 		if (**ap != '\0')
    132 			ap++;
    133 	}
    134 
    135 	printf("Stripe target init function called!!\n");
    136 
    137 	printf("Stripe target chunk size %s number of stripes %s\n",
    138 	    argv[1], argv[0]);
    139 
    140 	if ((tsc = kmem_alloc(sizeof(*tsc), KM_NOSLEEP)) == NULL)
    141 		return ENOMEM;
    142 
    143 	/* Initialize linked list for striping devices */
    144 	TAILQ_INIT(&tsc->stripe_devs);
    145 
    146 	/* Save length of param string */
    147 	tsc->params_len = len;
    148 	tsc->stripe_chunksize = atoi(argv[1]);
    149 	tsc->stripe_num = (uint8_t) atoi(argv[0]);
    150 
    151 	strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2);
    152 	for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) {
    153 		printf("Stripe target device name %s -- offset %s\n",
    154 		       argv[strpi], argv[strpi+1]);
    155 
    156 		tlc = kmem_alloc(sizeof(*tlc), KM_NOSLEEP);
    157 		if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL)
    158 			return ENOENT;
    159 		tlc->offset = atoi(argv[strpi+1]);
    160 
    161 		/* Insert striping device to linked list. */
    162 		TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries);
    163 	}
    164 
    165 	*target_config = tsc;
    166 
    167 	dmv->dev_type = DM_STRIPE_DEV;
    168 
    169 	return 0;
    170 }
    171 /* Status routine called to get params string. */
    172 char *
    173 dm_target_stripe_status(void *target_config)
    174 {
    175 	dm_target_linear_config_t *tlc;
    176 	dm_target_stripe_config_t *tsc;
    177 	char *params, *tmp;
    178 
    179 	tsc = target_config;
    180 
    181 	if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
    182 		return NULL;
    183 
    184 	if ((tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
    185 		return NULL;
    186 
    187 	snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64,
    188 	    tsc->stripe_num, tsc->stripe_chunksize);
    189 
    190 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    191 		snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64,
    192 		    tlc->pdev->name, tlc->offset);
    193 		strcat(params, tmp);
    194 	}
    195 
    196 	kmem_free(tmp, DM_MAX_PARAMS_SIZE);
    197 
    198 	return params;
    199 }
    200 /* Strategy routine called from dm_strategy. */
    201 int
    202 dm_target_stripe_strategy(dm_table_entry_t * table_en, struct buf * bp)
    203 {
    204 	dm_target_linear_config_t *tlc;
    205 	dm_target_stripe_config_t *tsc;
    206 	struct buf *nestbuf;
    207 	uint64_t blkno, blkoff;
    208 	uint64_t stripe, stripe_blknr;
    209 	uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
    210 	int i, stripe_devnr;
    211 
    212 	tsc = table_en->target_config;
    213 	if (tsc == NULL)
    214 		return 0;
    215 
    216 /*	printf("Stripe target read function called %" PRIu64 "!!\n",
    217 	tlc->offset);*/
    218 
    219 	/* calculate extent of request */
    220 	KASSERT(bp->b_resid % DEV_BSIZE == 0);
    221 
    222 	blkno = bp->b_blkno;
    223 	blkoff = 0;
    224 	num_blks = bp->b_resid / DEV_BSIZE;
    225 	for (;;) {
    226 		/* blockno to strip piece nr */
    227 		stripe = blkno / tsc->stripe_chunksize;
    228 		stripe_off = blkno % tsc->stripe_chunksize;
    229 
    230 		/* where we are inside the strip */
    231 		stripe_devnr = stripe % tsc->stripe_num;
    232 		stripe_blknr = stripe / tsc->stripe_num;
    233 
    234 		/* how much is left before we hit a boundary */
    235 		stripe_rest = tsc->stripe_chunksize - stripe_off;
    236 
    237 		/* issue this piece on stripe `stripe' */
    238 		issue_blks = MIN(stripe_rest, num_blks);
    239 		nestbuf = getiobuf(NULL, true);
    240 
    241 		nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
    242 		nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
    243 
    244 		tlc = TAILQ_FIRST(&tsc->stripe_devs);
    245 		for (i = 0; i < stripe_devnr && tlc == NULL; i++)
    246 			tlc = TAILQ_NEXT(tlc, entries);
    247 
    248 		/* by this point we should have an tlc */
    249 		KASSERT(tlc == NULL);
    250 
    251 		nestbuf->b_blkno += tlc->offset;
    252 
    253 		VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf);
    254 
    255 		blkno += issue_blks;
    256 		blkoff += issue_blks * DEV_BSIZE;
    257 		num_blks -= issue_blks;
    258 
    259 		if (num_blks <= 0)
    260 			break;
    261 	}
    262 
    263 	return 0;
    264 }
    265 /* Sync underlying disk caches. */
    266 int
    267 dm_target_stripe_sync(dm_table_entry_t * table_en)
    268 {
    269 	int cmd, err;
    270 	dm_target_stripe_config_t *tsc;
    271 	dm_target_linear_config_t *tlc;
    272 
    273 	tsc = table_en->target_config;
    274 
    275 	err = 0;
    276 	cmd = 1;
    277 
    278 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    279 		if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC,
    280 			    &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
    281 			return err;
    282 	}
    283 
    284 	return err;
    285 
    286 }
    287 /* Destroy target specific data. */
    288 int
    289 dm_target_stripe_destroy(dm_table_entry_t * table_en)
    290 {
    291 	dm_target_stripe_config_t *tsc;
    292 	dm_target_linear_config_t *tlc;
    293 
    294 	tsc = table_en->target_config;
    295 
    296 	if (tsc == NULL)
    297 		return 0;
    298 
    299 	while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) {
    300 		TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries);
    301 		dm_pdev_decr(tlc->pdev);
    302 		kmem_free(tlc, sizeof(*tlc));
    303 	}
    304 
    305 	/* Unbusy target so we can unload it */
    306 	dm_target_unbusy(table_en->target);
    307 
    308 	kmem_free(tsc, sizeof(*tsc));
    309 
    310 	table_en->target_config = NULL;
    311 
    312 	return 0;
    313 }
    314 /* Doesn't not need to do anything here. */
    315 int
    316 dm_target_stripe_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
    317 {
    318 	dm_target_stripe_config_t *tsc;
    319 	dm_target_linear_config_t *tlc;
    320 	struct vattr va;
    321 
    322 	int error;
    323 
    324 	if (table_en->target_config == NULL)
    325 		return ENOENT;
    326 
    327 	tsc = table_en->target_config;
    328 
    329 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    330 		if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    331 			return error;
    332 
    333 		prop_array_add_uint64(prop_array, (uint64_t) va.va_rdev);
    334 	}
    335 
    336 	return 0;
    337 }
    338 /* Unsupported for this target. */
    339 int
    340 dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
    341 {
    342 	return 0;
    343 }
    344