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