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