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