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