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