Home | History | Annotate | Line # | Download | only in dm
dm_target_linear.c revision 1.1.2.13
      1  1.1.2.13  haad /*        $NetBSD: dm_target_linear.c,v 1.1.2.13 2008/09/03 22:43:10 haad Exp $      */
      2   1.1.2.8  haad 
      3   1.1.2.1  haad /*
      4   1.1.2.1  haad  * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5   1.1.2.1  haad  * All rights reserved.
      6   1.1.2.1  haad  *
      7   1.1.2.1  haad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1.2.1  haad  * by Adam Hamsik.
      9   1.1.2.1  haad  *
     10   1.1.2.1  haad  * Redistribution and use in source and binary forms, with or without
     11   1.1.2.1  haad  * modification, are permitted provided that the following conditions
     12   1.1.2.1  haad  * are met:
     13   1.1.2.1  haad  * 1. Redistributions of source code must retain the above copyright
     14   1.1.2.1  haad  *    notice, this list of conditions and the following disclaimer.
     15   1.1.2.1  haad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1.2.1  haad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1.2.1  haad  *    documentation and/or other materials provided with the distribution.
     18   1.1.2.1  haad  *
     19   1.1.2.1  haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1.2.1  haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1.2.1  haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1.2.1  haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1.2.1  haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1.2.1  haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1.2.1  haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1.2.1  haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1.2.1  haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1.2.1  haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1.2.1  haad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1.2.1  haad  */
     31   1.1.2.1  haad 
     32   1.1.2.1  haad 
     33   1.1.2.1  haad /*
     34   1.1.2.1  haad  * This file implements initial version of device-mapper dklinear target.
     35   1.1.2.1  haad  */
     36   1.1.2.1  haad 
     37   1.1.2.1  haad #include <sys/types.h>
     38   1.1.2.1  haad #include <sys/param.h>
     39   1.1.2.1  haad 
     40   1.1.2.1  haad #include <sys/buf.h>
     41   1.1.2.1  haad #include <sys/kmem.h>
     42   1.1.2.1  haad #include <sys/vnode.h>
     43   1.1.2.1  haad 
     44   1.1.2.4  haad #include <machine/int_fmtio.h>
     45   1.1.2.4  haad 
     46   1.1.2.1  haad #include "dm.h"
     47   1.1.2.1  haad 
     48   1.1.2.1  haad /*
     49   1.1.2.1  haad  * Allocate target specific config data, and link them to table.
     50   1.1.2.4  haad  * This function is called only when, flags is not READONLY and
     51   1.1.2.6  haad  * therefore we can add things to pdev list. This function have to
     52   1.1.2.6  haad  * be called with held dev_mtx. This should not e a problem because
     53   1.1.2.6  haad  * this routine is called onyl from dm_table_load_ioctl.
     54   1.1.2.4  haad  * @argv[0] is name,
     55   1.1.2.4  haad  * @argv[1] is physical data offset.
     56   1.1.2.1  haad  */
     57   1.1.2.1  haad int
     58   1.1.2.4  haad dm_target_linear_init(struct dm_dev *dmv, void **target_config, char *params)
     59   1.1.2.1  haad {
     60   1.1.2.1  haad 	struct target_linear_config *tlc;
     61   1.1.2.1  haad 	struct dm_pdev *dmp;
     62   1.1.2.4  haad 
     63   1.1.2.4  haad 	char **ap, *argv[3];
     64  1.1.2.11  haad 
     65   1.1.2.4  haad 	/*
     66   1.1.2.4  haad 	 * Parse a string, containing tokens delimited by white space,
     67   1.1.2.4  haad 	 * into an argument vector
     68   1.1.2.4  haad 	 */
     69   1.1.2.4  haad 	for (ap = argv; ap < &argv[9] &&
     70   1.1.2.4  haad 		 (*ap = strsep(&params, " \t")) != NULL;) {
     71   1.1.2.4  haad 		if (**ap != '\0')
     72   1.1.2.4  haad 			ap++;
     73   1.1.2.4  haad 	}
     74   1.1.2.4  haad 
     75  1.1.2.12  haad 	/* Insert dmp to global pdev list */
     76   1.1.2.4  haad 	if ((dmp = dm_pdev_insert(argv[0])) == NULL)
     77   1.1.2.4  haad 		return ENOENT;
     78   1.1.2.4  haad 
     79  1.1.2.12  haad 	/* Lookup for pdev entry in device pdev list and insert */
     80  1.1.2.12  haad 	if ((dm_pdev_lookup_name_list(dmp->name, &dmv->pdevs)) == NULL)
     81  1.1.2.12  haad 		SLIST_INSERT_HEAD(&dmv->pdevs, dmp, next_pdev);
     82  1.1.2.12  haad 
     83  1.1.2.12  haad 	dmp->list_ref_cnt++;
     84   1.1.2.4  haad 
     85   1.1.2.2  haad 	printf("Linear target init function called %s--%s!!\n",
     86   1.1.2.2  haad 	    argv[0], argv[1]);
     87   1.1.2.1  haad 
     88   1.1.2.2  haad 	if ((tlc = kmem_alloc(sizeof(struct target_linear_config), KM_NOSLEEP))
     89   1.1.2.1  haad 	    == NULL)
     90   1.1.2.1  haad 		return 1;
     91   1.1.2.1  haad 
     92   1.1.2.1  haad 	tlc->pdev = dmp;
     93   1.1.2.1  haad 	tlc->offset = 0; 	/* default settings */
     94   1.1.2.1  haad 
     95   1.1.2.1  haad 	/* Check user input if it is not leave offset as 0. */
     96   1.1.2.4  haad 	tlc->offset = atoi(argv[1]);
     97   1.1.2.1  haad 
     98   1.1.2.1  haad 	*target_config = tlc;
     99   1.1.2.3  haad 
    100   1.1.2.3  haad 	dmv->dev_type = DM_LINEAR_DEV;
    101   1.1.2.1  haad 
    102   1.1.2.1  haad 	return 0;
    103   1.1.2.1  haad }
    104   1.1.2.1  haad 
    105   1.1.2.1  haad /*
    106   1.1.2.5  haad  * Status routine is called to get params string, which is target
    107   1.1.2.5  haad  * specific. When dm_table_status_ioctl is called with flag
    108   1.1.2.5  haad  * DM_STATUS_TABLE_FLAG I have to sent params string back.
    109   1.1.2.5  haad  */
    110   1.1.2.5  haad char *
    111   1.1.2.5  haad dm_target_linear_status(void *target_config)
    112   1.1.2.5  haad {
    113   1.1.2.5  haad 	struct target_linear_config *tlc;
    114   1.1.2.5  haad 	char *params;
    115   1.1.2.5  haad 	uint32_t i;
    116   1.1.2.5  haad 	uint32_t count;
    117   1.1.2.5  haad 	size_t prm_len;
    118   1.1.2.5  haad 
    119   1.1.2.5  haad 
    120   1.1.2.5  haad 	tlc = target_config;
    121   1.1.2.5  haad 	prm_len = 0;
    122   1.1.2.5  haad 	count = 0;
    123   1.1.2.5  haad 
    124   1.1.2.5  haad 	/* count number of chars in offset */
    125   1.1.2.5  haad 	for(i = tlc->offset; i != 0; i /= 10)
    126   1.1.2.5  haad 		count++;
    127   1.1.2.5  haad 
    128   1.1.2.5  haad 	printf("Linear target status function called\n");
    129   1.1.2.5  haad 
    130   1.1.2.5  haad 	/* length of name + count of chars + one space and null char */
    131   1.1.2.5  haad 	prm_len = strlen(tlc->pdev->name) + count + 2;
    132   1.1.2.5  haad 
    133   1.1.2.5  haad 	if ((params = kmem_alloc(prm_len, KM_NOSLEEP)) == NULL)
    134   1.1.2.5  haad 		return NULL;
    135   1.1.2.5  haad 
    136   1.1.2.5  haad 	printf("%s %"PRIu64, tlc->pdev->name, tlc->offset);
    137   1.1.2.5  haad 	snprintf(params, prm_len,"%s %"PRIu64, tlc->pdev->name, tlc->offset);
    138   1.1.2.5  haad 
    139   1.1.2.5  haad 
    140   1.1.2.5  haad 	return params;
    141   1.1.2.5  haad }
    142   1.1.2.5  haad 
    143   1.1.2.5  haad /*
    144   1.1.2.1  haad  * Do IO operation, called from dmstrategy routine.
    145   1.1.2.1  haad  */
    146   1.1.2.1  haad int
    147   1.1.2.1  haad dm_target_linear_strategy(struct dm_table_entry *table_en, struct buf *bp)
    148   1.1.2.1  haad {
    149   1.1.2.1  haad 	struct target_linear_config *tlc;
    150   1.1.2.1  haad 
    151   1.1.2.1  haad 	tlc = table_en->target_config;
    152   1.1.2.1  haad 
    153   1.1.2.4  haad 	printf("Linear target read function called %" PRIu64 "!!\n",
    154   1.1.2.4  haad 	    tlc->offset);
    155   1.1.2.1  haad 
    156   1.1.2.1  haad 	bp->b_blkno += tlc->offset;
    157   1.1.2.1  haad 
    158   1.1.2.1  haad 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
    159   1.1.2.1  haad 
    160   1.1.2.1  haad 	return 0;
    161   1.1.2.1  haad 
    162   1.1.2.1  haad }
    163   1.1.2.1  haad 
    164   1.1.2.1  haad /*
    165   1.1.2.9  haad  * Destroy target specific data. Decrement table pdevs.
    166   1.1.2.1  haad  */
    167   1.1.2.1  haad int
    168   1.1.2.1  haad dm_target_linear_destroy(struct dm_table_entry *table_en)
    169   1.1.2.1  haad {
    170   1.1.2.9  haad 	struct target_linear_config *tlc;
    171   1.1.2.9  haad 
    172  1.1.2.11  haad 	/*
    173  1.1.2.12  haad 	 * Destroy function is called for every target even if it
    174  1.1.2.11  haad 	 * doesn't have target_config.
    175  1.1.2.11  haad 	 */
    176  1.1.2.11  haad 
    177  1.1.2.11  haad 	if (table_en->target_config == NULL)
    178  1.1.2.11  haad 		return 0;
    179  1.1.2.11  haad 
    180   1.1.2.9  haad 	tlc = table_en->target_config;
    181  1.1.2.12  haad 
    182  1.1.2.12  haad 	/* Decrement device list reference counter */
    183  1.1.2.12  haad 	tlc->pdev->list_ref_cnt--;
    184  1.1.2.12  haad 
    185  1.1.2.12  haad 	/* If there is no other table which reference this pdev remove it. */
    186  1.1.2.12  haad 	if (tlc->pdev->list_ref_cnt == 0)
    187  1.1.2.12  haad 		SLIST_REMOVE(&table_en->dm_dev->pdevs, tlc->pdev, dm_pdev, next_pdev);
    188  1.1.2.12  haad 
    189  1.1.2.11  haad 	/* Decrement pdev ref counter if 0 remove it */
    190   1.1.2.9  haad 	dm_pdev_decr(tlc->pdev);
    191   1.1.2.9  haad 
    192   1.1.2.2  haad 	kmem_free(table_en->target_config, sizeof(struct target_linear_config));
    193   1.1.2.7  haad 
    194   1.1.2.7  haad 	table_en->target_config = NULL;
    195   1.1.2.1  haad 
    196   1.1.2.1  haad 	return 0;
    197   1.1.2.1  haad }
    198   1.1.2.1  haad 
    199   1.1.2.1  haad /*
    200   1.1.2.1  haad  * Register upcall device.
    201   1.1.2.1  haad  * Linear target doesn't need any upcall devices but other targets like
    202   1.1.2.1  haad  * mirror, snapshot, multipath, stripe will use this functionality.
    203   1.1.2.1  haad  */
    204   1.1.2.1  haad int
    205   1.1.2.1  haad dm_target_linear_upcall(struct dm_table_entry *table_en, struct buf *bp)
    206   1.1.2.1  haad {
    207   1.1.2.1  haad 
    208   1.1.2.1  haad 	return 0;
    209   1.1.2.1  haad }
    210   1.1.2.1  haad 
    211   1.1.2.1  haad /*
    212   1.1.2.1  haad  * Transform char s to uint64_t offset number.
    213   1.1.2.1  haad  */
    214  1.1.2.10  haad uint64_t
    215   1.1.2.1  haad atoi(const char *s)
    216   1.1.2.1  haad {
    217   1.1.2.1  haad 	uint64_t n;
    218   1.1.2.1  haad 
    219   1.1.2.1  haad 	n = 0;
    220   1.1.2.1  haad 
    221   1.1.2.1  haad 	while (*s != '\0') {
    222   1.1.2.1  haad 
    223   1.1.2.1  haad 		if (!isdigit(*s))
    224   1.1.2.1  haad 			break;
    225   1.1.2.1  haad 
    226   1.1.2.1  haad 		n = (10 * n) + (*s - '0');
    227   1.1.2.1  haad 		s++;
    228   1.1.2.1  haad 	}
    229   1.1.2.1  haad 
    230   1.1.2.1  haad 	return n;
    231   1.1.2.1  haad }
    232