Home | History | Annotate | Line # | Download | only in dm
dm_target_stripe.c revision 1.11
      1  1.11     haad /*$NetBSD: dm_target_stripe.c,v 1.11 2010/10/23 21:18:54 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.9     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.1     haad 		dmt = dm_target_alloc("stripe");
     71   1.9     haad 
     72   1.1     haad 		dmt->version[0] = 1;
     73   1.1     haad 		dmt->version[1] = 0;
     74   1.1     haad 		dmt->version[2] = 0;
     75   1.1     haad 		strlcpy(dmt->name, "stripe", DM_MAX_TYPE_NAME);
     76   1.1     haad 		dmt->init = &dm_target_stripe_init;
     77   1.1     haad 		dmt->status = &dm_target_stripe_status;
     78   1.1     haad 		dmt->strategy = &dm_target_stripe_strategy;
     79  1.10     haad 		dmt->sync = &dm_target_stripe_sync;
     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.9     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 #endif
    102   1.1     haad 
    103   1.3     haad /*
    104   1.3     haad  * Init function called from dm_table_load_ioctl.
    105  1.11     haad  * DM_STRIPE_DEV_OFFSET should always hold the index of the first device-offset
    106  1.11     haad  * pair in the parameters.
    107   1.3     haad  * Example line sent to dm from lvm tools when using striped target.
    108   1.3     haad  * start length striped #stripes chunk_size device1 offset1 ... deviceN offsetN
    109   1.3     haad  * 0 65536 striped 2 512 /dev/hda 0 /dev/hdb 0
    110   1.3     haad  */
    111   1.1     haad int
    112   1.9     haad dm_target_stripe_init(dm_dev_t * dmv, void **target_config, char *params)
    113   1.1     haad {
    114  1.11     haad 	dm_target_linear_config_t *tlc;
    115   1.3     haad 	dm_target_stripe_config_t *tsc;
    116   1.8     haad 	size_t len;
    117   1.8     haad 	char **ap, *argv[10];
    118  1.11     haad 	int strpc, strpi;
    119   1.8     haad 
    120   1.9     haad 	if (params == NULL)
    121   1.3     haad 		return EINVAL;
    122   1.8     haad 
    123   1.8     haad 	len = strlen(params) + 1;
    124   1.9     haad 
    125   1.8     haad 	/*
    126   1.8     haad 	 * Parse a string, containing tokens delimited by white space,
    127   1.8     haad 	 * into an argument vector
    128   1.8     haad 	 */
    129   1.8     haad 	for (ap = argv; ap < &argv[9] &&
    130   1.9     haad 	    (*ap = strsep(&params, " \t")) != NULL;) {
    131   1.8     haad 		if (**ap != '\0')
    132   1.8     haad 			ap++;
    133   1.8     haad 	}
    134   1.9     haad 
    135   1.8     haad 	printf("Stripe target init function called!!\n");
    136   1.1     haad 
    137  1.11     haad 	printf("Stripe target chunk size %s number of stripes %s\n",
    138  1.11     haad 	    argv[1], argv[0]);
    139   1.3     haad 
    140  1.11     haad 	if ((tsc = kmem_alloc(sizeof(*tsc), KM_NOSLEEP)) == NULL)
    141   1.3     haad 		return ENOMEM;
    142   1.9     haad 
    143  1.11     haad 	/* Initialize linked list for striping devices */
    144  1.11     haad 	TAILQ_INIT(&tsc->stripe_devs);
    145   1.3     haad 
    146   1.8     haad 	/* Save length of param string */
    147   1.8     haad 	tsc->params_len = len;
    148   1.8     haad 	tsc->stripe_chunksize = atoi(argv[1]);
    149   1.9     haad 	tsc->stripe_num = (uint8_t) atoi(argv[0]);
    150   1.9     haad 
    151  1.11     haad 	strpc = DM_STRIPE_DEV_OFFSET + (tsc->stripe_num * 2);
    152  1.11     haad 	for (strpi = DM_STRIPE_DEV_OFFSET; strpi < strpc; strpi += 2) {
    153  1.11     haad 		printf("Stripe target device name %s -- offset %s\n",
    154  1.11     haad 		       argv[strpi], argv[strpi+1]);
    155  1.11     haad 
    156  1.11     haad 		tlc = kmem_alloc(sizeof(*tlc), KM_NOSLEEP);
    157  1.11     haad 		if ((tlc->pdev = dm_pdev_insert(argv[strpi])) == NULL)
    158  1.11     haad 			return ENOENT;
    159  1.11     haad 		tlc->offset = atoi(argv[strpi+1]);
    160  1.11     haad 
    161  1.11     haad 		/* Insert striping device to linked list. */
    162  1.11     haad 		TAILQ_INSERT_TAIL(&tsc->stripe_devs, tlc, entries);
    163  1.11     haad 	}
    164  1.11     haad 
    165   1.3     haad 	*target_config = tsc;
    166   1.1     haad 
    167   1.1     haad 	dmv->dev_type = DM_STRIPE_DEV;
    168   1.9     haad 
    169   1.3     haad 	return 0;
    170   1.1     haad }
    171   1.1     haad /* Status routine called to get params string. */
    172   1.1     haad char *
    173   1.1     haad dm_target_stripe_status(void *target_config)
    174   1.1     haad {
    175  1.11     haad 	dm_target_linear_config_t *tlc;
    176   1.3     haad 	dm_target_stripe_config_t *tsc;
    177  1.11     haad 	char *params, *tmp;
    178   1.3     haad 
    179   1.3     haad 	tsc = target_config;
    180   1.9     haad 
    181   1.7     haad 	if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
    182   1.3     haad 		return NULL;
    183   1.3     haad 
    184  1.11     haad 	if ((tmp = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP)) == NULL)
    185  1.11     haad 		return NULL;
    186  1.11     haad 
    187  1.11     haad 	snprintf(params, DM_MAX_PARAMS_SIZE, "%d %" PRIu64,
    188  1.11     haad 	    tsc->stripe_num, tsc->stripe_chunksize);
    189  1.11     haad 
    190  1.11     haad 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    191  1.11     haad 		snprintf(tmp, DM_MAX_PARAMS_SIZE, " %s %" PRIu64,
    192  1.11     haad 		    tlc->pdev->name, tlc->offset);
    193  1.11     haad 		strcat(params, tmp);
    194  1.11     haad 	}
    195  1.11     haad 
    196  1.11     haad 	kmem_free(tmp, DM_MAX_PARAMS_SIZE);
    197   1.9     haad 
    198   1.3     haad 	return params;
    199   1.9     haad }
    200   1.1     haad /* Strategy routine called from dm_strategy. */
    201   1.1     haad int
    202   1.9     haad dm_target_stripe_strategy(dm_table_entry_t * table_en, struct buf * bp)
    203   1.1     haad {
    204  1.11     haad 	dm_target_linear_config_t *tlc;
    205   1.4  reinoud 	dm_target_stripe_config_t *tsc;
    206   1.4  reinoud 	struct buf *nestbuf;
    207   1.4  reinoud 	uint64_t blkno, blkoff;
    208   1.4  reinoud 	uint64_t stripe, stripe_blknr;
    209   1.4  reinoud 	uint32_t stripe_off, stripe_rest, num_blks, issue_blks;
    210  1.11     haad 	int i, stripe_devnr;
    211   1.4  reinoud 
    212   1.4  reinoud 	tsc = table_en->target_config;
    213   1.4  reinoud 	if (tsc == NULL)
    214   1.4  reinoud 		return 0;
    215   1.4  reinoud 
    216   1.8     haad /*	printf("Stripe target read function called %" PRIu64 "!!\n",
    217   1.4  reinoud 	tlc->offset);*/
    218   1.4  reinoud 
    219   1.4  reinoud 	/* calculate extent of request */
    220   1.4  reinoud 	KASSERT(bp->b_resid % DEV_BSIZE == 0);
    221   1.4  reinoud 
    222   1.9     haad 	blkno = bp->b_blkno;
    223   1.4  reinoud 	blkoff = 0;
    224   1.4  reinoud 	num_blks = bp->b_resid / DEV_BSIZE;
    225   1.4  reinoud 	for (;;) {
    226   1.4  reinoud 		/* blockno to strip piece nr */
    227   1.9     haad 		stripe = blkno / tsc->stripe_chunksize;
    228   1.4  reinoud 		stripe_off = blkno % tsc->stripe_chunksize;
    229   1.4  reinoud 
    230   1.4  reinoud 		/* where we are inside the strip */
    231   1.4  reinoud 		stripe_devnr = stripe % tsc->stripe_num;
    232   1.4  reinoud 		stripe_blknr = stripe / tsc->stripe_num;
    233   1.4  reinoud 
    234   1.4  reinoud 		/* how much is left before we hit a boundary */
    235   1.4  reinoud 		stripe_rest = tsc->stripe_chunksize - stripe_off;
    236   1.4  reinoud 
    237   1.4  reinoud 		/* issue this piece on stripe `stripe' */
    238   1.4  reinoud 		issue_blks = MIN(stripe_rest, num_blks);
    239   1.4  reinoud 		nestbuf = getiobuf(NULL, true);
    240   1.4  reinoud 
    241   1.4  reinoud 		nestiobuf_setup(bp, nestbuf, blkoff, issue_blks * DEV_BSIZE);
    242   1.4  reinoud 		nestbuf->b_blkno = stripe_blknr * tsc->stripe_chunksize + stripe_off;
    243   1.4  reinoud 
    244  1.11     haad 		tlc = TAILQ_FIRST(&tsc->stripe_devs);
    245  1.11     haad 		for (i = 0; i < stripe_devnr && tlc == NULL; i++)
    246  1.11     haad 			tlc = TAILQ_NEXT(tlc, entries);
    247  1.11     haad 
    248  1.11     haad 		/* by this point we should have an tlc */
    249  1.11     haad 		KASSERT(tlc == NULL);
    250  1.11     haad 
    251  1.11     haad 		nestbuf->b_blkno += tlc->offset;
    252  1.11     haad 
    253  1.11     haad 		VOP_STRATEGY(tlc->pdev->pdev_vnode, nestbuf);
    254   1.4  reinoud 
    255   1.9     haad 		blkno += issue_blks;
    256   1.9     haad 		blkoff += issue_blks * DEV_BSIZE;
    257   1.4  reinoud 		num_blks -= issue_blks;
    258   1.4  reinoud 
    259   1.4  reinoud 		if (num_blks <= 0)
    260   1.4  reinoud 			break;
    261   1.4  reinoud 	}
    262   1.1     haad 
    263   1.1     haad 	return 0;
    264   1.1     haad }
    265  1.10     haad /* Sync underlying disk caches. */
    266  1.10     haad int
    267  1.10     haad dm_target_stripe_sync(dm_table_entry_t * table_en)
    268  1.10     haad {
    269  1.11     haad 	int cmd, err;
    270  1.10     haad 	dm_target_stripe_config_t *tsc;
    271  1.11     haad 	dm_target_linear_config_t *tlc;
    272  1.10     haad 
    273  1.10     haad 	tsc = table_en->target_config;
    274  1.10     haad 
    275  1.10     haad 	err = 0;
    276  1.10     haad 	cmd = 1;
    277  1.10     haad 
    278  1.11     haad 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    279  1.11     haad 		if ((err = VOP_IOCTL(tlc->pdev->pdev_vnode, DIOCCACHESYNC,
    280  1.10     haad 			    &cmd, FREAD|FWRITE, kauth_cred_get())) != 0)
    281  1.10     haad 			return err;
    282  1.10     haad 	}
    283  1.10     haad 
    284  1.10     haad 	return err;
    285  1.10     haad 
    286  1.10     haad }
    287  1.10     haad /* Destroy target specific data. */
    288   1.1     haad int
    289   1.9     haad dm_target_stripe_destroy(dm_table_entry_t * table_en)
    290   1.1     haad {
    291   1.3     haad 	dm_target_stripe_config_t *tsc;
    292  1.11     haad 	dm_target_linear_config_t *tlc;
    293   1.9     haad 
    294   1.3     haad 	tsc = table_en->target_config;
    295   1.3     haad 
    296   1.3     haad 	if (tsc == NULL)
    297   1.3     haad 		return 0;
    298   1.9     haad 
    299  1.11     haad 	while ((tlc = TAILQ_FIRST(&tsc->stripe_devs)) != NULL) {
    300  1.11     haad 		TAILQ_REMOVE(&tsc->stripe_devs, tlc, entries);
    301  1.11     haad 		dm_pdev_decr(tlc->pdev);
    302  1.11     haad 		kmem_free(tlc, sizeof(*tlc));
    303  1.11     haad 	}
    304   1.1     haad 
    305   1.1     haad 	/* Unbusy target so we can unload it */
    306   1.1     haad 	dm_target_unbusy(table_en->target);
    307   1.9     haad 
    308  1.11     haad 	kmem_free(tsc, sizeof(*tsc));
    309   1.9     haad 
    310   1.3     haad 	table_en->target_config = NULL;
    311   1.3     haad 
    312   1.1     haad 	return 0;
    313   1.1     haad }
    314   1.1     haad /* Doesn't not need to do anything here. */
    315   1.1     haad int
    316   1.9     haad dm_target_stripe_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
    317   1.9     haad {
    318   1.3     haad 	dm_target_stripe_config_t *tsc;
    319  1.11     haad 	dm_target_linear_config_t *tlc;
    320   1.3     haad 	struct vattr va;
    321   1.9     haad 
    322   1.3     haad 	int error;
    323   1.9     haad 
    324   1.3     haad 	if (table_en->target_config == NULL)
    325   1.3     haad 		return ENOENT;
    326   1.9     haad 
    327   1.3     haad 	tsc = table_en->target_config;
    328   1.9     haad 
    329  1.11     haad 	TAILQ_FOREACH(tlc, &tsc->stripe_devs, entries) {
    330  1.11     haad 		if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    331  1.11     haad 			return error;
    332   1.3     haad 
    333  1.11     haad 		prop_array_add_uint64(prop_array, (uint64_t) va.va_rdev);
    334  1.11     haad 	}
    335   1.9     haad 
    336   1.1     haad 	return 0;
    337   1.1     haad }
    338   1.1     haad /* Unsupported for this target. */
    339   1.1     haad int
    340   1.9     haad dm_target_stripe_upcall(dm_table_entry_t * table_en, struct buf * bp)
    341   1.1     haad {
    342   1.1     haad 	return 0;
    343   1.1     haad }
    344