Home | History | Annotate | Line # | Download | only in dm
dm_target_linear.c revision 1.1.2.15
      1 /*        $NetBSD: dm_target_linear.c,v 1.1.2.15 2008/09/11 13:40:47 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 	printf("Linear target init function called %s--%s!!\n",
     79 	    argv[0], argv[1]);
     80 
     81 	if ((tlc = kmem_alloc(sizeof(struct target_linear_config), KM_NOSLEEP))
     82 	    == NULL)
     83 		return 1;
     84 
     85 	tlc->pdev = dmp;
     86 	tlc->offset = 0; 	/* default settings */
     87 
     88 	/* Check user input if it is not leave offset as 0. */
     89 	tlc->offset = atoi(argv[1]);
     90 
     91 	*target_config = tlc;
     92 
     93 	dmv->dev_type = DM_LINEAR_DEV;
     94 
     95 	return 0;
     96 }
     97 
     98 /*
     99  * Status routine is called to get params string, which is target
    100  * specific. When dm_table_status_ioctl is called with flag
    101  * DM_STATUS_TABLE_FLAG I have to sent params string back.
    102  */
    103 char *
    104 dm_target_linear_status(void *target_config)
    105 {
    106 	struct target_linear_config *tlc;
    107 	char *params;
    108 	uint32_t i;
    109 	uint32_t count;
    110 	size_t prm_len;
    111 
    112 
    113 	tlc = target_config;
    114 	prm_len = 0;
    115 	count = 0;
    116 
    117 	/* count number of chars in offset */
    118 	for(i = tlc->offset; i != 0; i /= 10)
    119 		count++;
    120 
    121 	printf("Linear target status function called\n");
    122 
    123 	/* length of name + count of chars + one space and null char */
    124 	prm_len = strlen(tlc->pdev->name) + count + 2;
    125 
    126 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
    127 		return NULL;
    128 
    129 	printf("%s %"PRIu64, tlc->pdev->name, tlc->offset);
    130 	snprintf(params, prm_len,"%s %"PRIu64, tlc->pdev->name, tlc->offset);
    131 
    132 
    133 	return params;
    134 }
    135 
    136 /*
    137  * Do IO operation, called from dmstrategy routine.
    138  */
    139 int
    140 dm_target_linear_strategy(struct dm_table_entry *table_en, struct buf *bp)
    141 {
    142 	struct target_linear_config *tlc;
    143 
    144 	tlc = table_en->target_config;
    145 
    146 	printf("Linear target read function called %" PRIu64 "!!\n",
    147 	    tlc->offset);
    148 
    149 	bp->b_blkno += tlc->offset;
    150 
    151 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
    152 
    153 	return 0;
    154 
    155 }
    156 
    157 /*
    158  * Destroy target specific data. Decrement table pdevs.
    159  */
    160 int
    161 dm_target_linear_destroy(struct dm_table_entry *table_en)
    162 {
    163 	struct target_linear_config *tlc;
    164 
    165 	/*
    166 	 * Destroy function is called for every target even if it
    167 	 * doesn't have target_config.
    168 	 */
    169 
    170 	if (table_en->target_config == NULL)
    171 		return 0;
    172 
    173 	tlc = table_en->target_config;
    174 
    175 	/* Decrement pdev ref counter if 0 remove it */
    176 	dm_pdev_decr(tlc->pdev);
    177 
    178 	kmem_free(table_en->target_config, sizeof(struct target_linear_config));
    179 
    180 	table_en->target_config = NULL;
    181 
    182 	return 0;
    183 }
    184 
    185 /* Add this target pdev dependiences to prop_array_t */
    186 int
    187 dm_target_linear_deps(struct dm_table_entry *table_en, prop_array_t prop_array)
    188 {
    189 	struct target_linear_config *tlc;
    190 	struct vattr va;
    191 
    192 	int error;
    193 
    194 	if (table_en->target_config == NULL)
    195 		return ENOENT;
    196 
    197 	tlc = table_en->target_config;
    198 
    199 	if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    200 		return error;
    201 
    202 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    203 
    204 	return 0;
    205 }
    206 
    207 /*
    208  * Register upcall device.
    209  * Linear target doesn't need any upcall devices but other targets like
    210  * mirror, snapshot, multipath, stripe will use this functionality.
    211  */
    212 int
    213 dm_target_linear_upcall(struct dm_table_entry *table_en, struct buf *bp)
    214 {
    215 
    216 	return 0;
    217 }
    218 
    219 /*
    220  * Transform char s to uint64_t offset number.
    221  */
    222 uint64_t
    223 atoi(const char *s)
    224 {
    225 	uint64_t n;
    226 
    227 	n = 0;
    228 
    229 	while (*s != '\0') {
    230 
    231 		if (!isdigit(*s))
    232 			break;
    233 
    234 		n = (10 * n) + (*s - '0');
    235 		s++;
    236 	}
    237 
    238 	return n;
    239 }
    240