Home | History | Annotate | Line # | Download | only in dm
dm_target_linear.c revision 1.1.2.13
      1 /*        $NetBSD: dm_target_linear.c,v 1.1.2.13 2008/09/03 22:43:10 haad Exp $      */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2002 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 /*
     34  * This file implements initial version of device-mapper dklinear target.
     35  */
     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 
     44 #include <machine/int_fmtio.h>
     45 
     46 #include "dm.h"
     47 
     48 /*
     49  * Allocate target specific config data, and link them to table.
     50  * This function is called only when, flags is not READONLY and
     51  * therefore we can add things to pdev list. This function have to
     52  * be called with held dev_mtx. This should not e a problem because
     53  * this routine is called onyl from dm_table_load_ioctl.
     54  * @argv[0] is name,
     55  * @argv[1] is physical data offset.
     56  */
     57 int
     58 dm_target_linear_init(struct dm_dev *dmv, void **target_config, char *params)
     59 {
     60 	struct target_linear_config *tlc;
     61 	struct dm_pdev *dmp;
     62 
     63 	char **ap, *argv[3];
     64 
     65 	/*
     66 	 * Parse a string, containing tokens delimited by white space,
     67 	 * into an argument vector
     68 	 */
     69 	for (ap = argv; ap < &argv[9] &&
     70 		 (*ap = strsep(&params, " \t")) != NULL;) {
     71 		if (**ap != '\0')
     72 			ap++;
     73 	}
     74 
     75 	/* Insert dmp to global pdev list */
     76 	if ((dmp = dm_pdev_insert(argv[0])) == NULL)
     77 		return ENOENT;
     78 
     79 	/* Lookup for pdev entry in device pdev list and insert */
     80 	if ((dm_pdev_lookup_name_list(dmp->name, &dmv->pdevs)) == NULL)
     81 		SLIST_INSERT_HEAD(&dmv->pdevs, dmp, next_pdev);
     82 
     83 	dmp->list_ref_cnt++;
     84 
     85 	printf("Linear target init function called %s--%s!!\n",
     86 	    argv[0], argv[1]);
     87 
     88 	if ((tlc = kmem_alloc(sizeof(struct target_linear_config), KM_NOSLEEP))
     89 	    == NULL)
     90 		return 1;
     91 
     92 	tlc->pdev = dmp;
     93 	tlc->offset = 0; 	/* default settings */
     94 
     95 	/* Check user input if it is not leave offset as 0. */
     96 	tlc->offset = atoi(argv[1]);
     97 
     98 	*target_config = tlc;
     99 
    100 	dmv->dev_type = DM_LINEAR_DEV;
    101 
    102 	return 0;
    103 }
    104 
    105 /*
    106  * Status routine is called to get params string, which is target
    107  * specific. When dm_table_status_ioctl is called with flag
    108  * DM_STATUS_TABLE_FLAG I have to sent params string back.
    109  */
    110 char *
    111 dm_target_linear_status(void *target_config)
    112 {
    113 	struct target_linear_config *tlc;
    114 	char *params;
    115 	uint32_t i;
    116 	uint32_t count;
    117 	size_t prm_len;
    118 
    119 
    120 	tlc = target_config;
    121 	prm_len = 0;
    122 	count = 0;
    123 
    124 	/* count number of chars in offset */
    125 	for(i = tlc->offset; i != 0; i /= 10)
    126 		count++;
    127 
    128 	printf("Linear target status function called\n");
    129 
    130 	/* length of name + count of chars + one space and null char */
    131 	prm_len = strlen(tlc->pdev->name) + count + 2;
    132 
    133 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
    134 		return NULL;
    135 
    136 	printf("%s %"PRIu64, tlc->pdev->name, tlc->offset);
    137 	snprintf(params, prm_len,"%s %"PRIu64, tlc->pdev->name, tlc->offset);
    138 
    139 
    140 	return params;
    141 }
    142 
    143 /*
    144  * Do IO operation, called from dmstrategy routine.
    145  */
    146 int
    147 dm_target_linear_strategy(struct dm_table_entry *table_en, struct buf *bp)
    148 {
    149 	struct target_linear_config *tlc;
    150 
    151 	tlc = table_en->target_config;
    152 
    153 	printf("Linear target read function called %" PRIu64 "!!\n",
    154 	    tlc->offset);
    155 
    156 	bp->b_blkno += tlc->offset;
    157 
    158 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
    159 
    160 	return 0;
    161 
    162 }
    163 
    164 /*
    165  * Destroy target specific data. Decrement table pdevs.
    166  */
    167 int
    168 dm_target_linear_destroy(struct dm_table_entry *table_en)
    169 {
    170 	struct target_linear_config *tlc;
    171 
    172 	/*
    173 	 * Destroy function is called for every target even if it
    174 	 * doesn't have target_config.
    175 	 */
    176 
    177 	if (table_en->target_config == NULL)
    178 		return 0;
    179 
    180 	tlc = table_en->target_config;
    181 
    182 	/* Decrement device list reference counter */
    183 	tlc->pdev->list_ref_cnt--;
    184 
    185 	/* If there is no other table which reference this pdev remove it. */
    186 	if (tlc->pdev->list_ref_cnt == 0)
    187 		SLIST_REMOVE(&table_en->dm_dev->pdevs, tlc->pdev, dm_pdev, next_pdev);
    188 
    189 	/* Decrement pdev ref counter if 0 remove it */
    190 	dm_pdev_decr(tlc->pdev);
    191 
    192 	kmem_free(table_en->target_config, sizeof(struct target_linear_config));
    193 
    194 	table_en->target_config = NULL;
    195 
    196 	return 0;
    197 }
    198 
    199 /*
    200  * Register upcall device.
    201  * Linear target doesn't need any upcall devices but other targets like
    202  * mirror, snapshot, multipath, stripe will use this functionality.
    203  */
    204 int
    205 dm_target_linear_upcall(struct dm_table_entry *table_en, struct buf *bp)
    206 {
    207 
    208 	return 0;
    209 }
    210 
    211 /*
    212  * Transform char s to uint64_t offset number.
    213  */
    214 uint64_t
    215 atoi(const char *s)
    216 {
    217 	uint64_t n;
    218 
    219 	n = 0;
    220 
    221 	while (*s != '\0') {
    222 
    223 		if (!isdigit(*s))
    224 			break;
    225 
    226 		n = (10 * n) + (*s - '0');
    227 		s++;
    228 	}
    229 
    230 	return n;
    231 }
    232