Home | History | Annotate | Line # | Download | only in dm
dm_target_linear.c revision 1.6
      1 /*        $NetBSD: dm_target_linear.c,v 1.6 2009/08/16 11:02:24 yamt Exp $      */
      2 
      3 /*
      4  * Copyright (c) 2008 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 "netbsd-dm.h"
     47 #include "dm.h"
     48 
     49 /*
     50  * Allocate target specific config data, and link them to table.
     51  * This function is called only when, flags is not READONLY and
     52  * therefore we can add things to pdev list. This should not a
     53  * problem because this routine is called only 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(dm_dev_t *dmv, void **target_config, prop_dictionary_t dict)
     59 {
     60 	dm_target_linear_config_t *tlc;
     61 	dm_pdev_t *dmp;
     62 
     63 	const char *device;
     64 	uint64_t offset;
     65 
     66 	if (prop_dictionary_get_cstring_nocopy(dict, DM_TARGET_LINEAR_DEVICE,
     67 		&device) == false)
     68 		return EINVAL;
     69 
     70 	if (prop_dictionary_get_uint64(dict, DM_TARGET_LINEAR_OFFSET,
     71 		&offset) == false)
     72 		return EINVAL;
     73 
     74 	/* Insert dmp to global pdev list */
     75 	if ((dmp = dm_pdev_insert(device)) == NULL)
     76 		return ENOENT;
     77 
     78 	aprint_debug("Linear target init function called %s--%"PRIu64"!!\n",
     79 	    device, offset);
     80 
     81 	if ((tlc = kmem_alloc(sizeof(dm_target_linear_config_t), 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 = offset;
     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 	dm_target_linear_config_t *tlc;
    107 	char *params;
    108 	tlc = target_config;
    109 
    110 	aprint_debug("Linear target status function called\n");
    111 
    112 	if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_NOSLEEP)) == NULL)
    113 		return NULL;
    114 
    115 	aprint_normal("%s %"PRIu64, tlc->pdev->name, tlc->offset);
    116 	snprintf(params, DM_MAX_PARAMS_SIZE,"%s %"PRIu64,
    117 		tlc->pdev->name, tlc->offset);
    118 
    119 	return params;
    120 }
    121 
    122 /*
    123  * Do IO operation, called from dmstrategy routine.
    124  */
    125 int
    126 dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
    127 {
    128 	dm_target_linear_config_t *tlc;
    129 
    130 	tlc = table_en->target_config;
    131 
    132 /*	printf("Linear target read function called %" PRIu64 "!!\n",
    133 	tlc->offset);*/
    134 
    135 	bp->b_blkno += tlc->offset;
    136 
    137 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
    138 
    139 	return 0;
    140 
    141 }
    142 
    143 /*
    144  * Destroy target specific data. Decrement table pdevs.
    145  */
    146 int
    147 dm_target_linear_destroy(dm_table_entry_t *table_en)
    148 {
    149 	dm_target_linear_config_t *tlc;
    150 
    151 	/*
    152 	 * Destroy function is called for every target even if it
    153 	 * doesn't have target_config.
    154 	 */
    155 
    156 	if (table_en->target_config == NULL)
    157 		return 0;
    158 
    159 	tlc = table_en->target_config;
    160 
    161 	/* Decrement pdev ref counter if 0 remove it */
    162 	dm_pdev_decr(tlc->pdev);
    163 
    164 	/* Unbusy target so we can unload it */
    165 	dm_target_unbusy(table_en->target);
    166 
    167 	kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
    168 
    169 	table_en->target_config = NULL;
    170 
    171 	return 0;
    172 }
    173 
    174 /* Add this target pdev dependiences to prop_array_t */
    175 int
    176 dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
    177 {
    178 	dm_target_linear_config_t *tlc;
    179 	struct vattr va;
    180 
    181 	int error;
    182 
    183 	if (table_en->target_config == NULL)
    184 		return ENOENT;
    185 
    186 	tlc = table_en->target_config;
    187 
    188 	if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
    189 		return error;
    190 
    191 	prop_array_add_uint64(prop_array, (uint64_t)va.va_rdev);
    192 
    193 	return 0;
    194 }
    195 
    196 /*
    197  * Register upcall device.
    198  * Linear target doesn't need any upcall devices but other targets like
    199  * mirror, snapshot, multipath, stripe will use this functionality.
    200  */
    201 int
    202 dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
    203 {
    204 	return 0;
    205 }
    206 
    207 /*
    208  * Transform char s to uint64_t offset number.
    209  */
    210 uint64_t
    211 atoi(const char *s)
    212 {
    213 	uint64_t n;
    214 	n = 0;
    215 
    216 	while (*s != '\0') {
    217 		if (!isdigit(*s))
    218 			break;
    219 
    220 		n = (10 * n) + (*s - '0');
    221 		s++;
    222 	}
    223 
    224 	return n;
    225 }
    226