Home | History | Annotate | Line # | Download | only in dm
dm_target_linear.c revision 1.31
      1  1.31   tkusumi /*        $NetBSD: dm_target_linear.c,v 1.31 2019/12/15 14:39:42 tkusumi Exp $      */
      2   1.2      haad 
      3   1.2      haad /*
      4   1.2      haad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5   1.2      haad  * All rights reserved.
      6   1.2      haad  *
      7   1.2      haad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2      haad  * by Adam Hamsik.
      9   1.2      haad  *
     10   1.2      haad  * Redistribution and use in source and binary forms, with or without
     11   1.2      haad  * modification, are permitted provided that the following conditions
     12   1.2      haad  * are met:
     13   1.2      haad  * 1. Redistributions of source code must retain the above copyright
     14   1.2      haad  *    notice, this list of conditions and the following disclaimer.
     15   1.2      haad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2      haad  *    notice, this list of conditions and the following disclaimer in the
     17   1.2      haad  *    documentation and/or other materials provided with the distribution.
     18   1.2      haad  *
     19   1.2      haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2      haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2      haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2      haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2      haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2      haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2      haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2      haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2      haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2      haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2      haad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2      haad  */
     31  1.17  christos #include <sys/cdefs.h>
     32  1.31   tkusumi __KERNEL_RCSID(0, "$NetBSD: dm_target_linear.c,v 1.31 2019/12/15 14:39:42 tkusumi Exp $");
     33   1.2      haad 
     34   1.2      haad /*
     35   1.2      haad  * This file implements initial version of device-mapper dklinear target.
     36   1.2      haad  */
     37   1.2      haad 
     38   1.2      haad #include <sys/types.h>
     39   1.2      haad #include <sys/param.h>
     40   1.2      haad #include <sys/buf.h>
     41   1.2      haad #include <sys/kmem.h>
     42  1.11  uebayasi #include <sys/lwp.h>
     43   1.2      haad 
     44   1.2      haad #include <machine/int_fmtio.h>
     45   1.2      haad 
     46   1.2      haad #include "dm.h"
     47   1.2      haad 
     48   1.2      haad /*
     49   1.2      haad  * Allocate target specific config data, and link them to table.
     50   1.2      haad  * This function is called only when, flags is not READONLY and
     51   1.9      haad  * therefore we can add things to pdev list. This should not a
     52   1.6      yamt  * problem because this routine is called only from dm_table_load_ioctl.
     53   1.2      haad  * @argv[0] is name,
     54   1.2      haad  * @argv[1] is physical data offset.
     55   1.2      haad  */
     56   1.2      haad int
     57  1.27   tkusumi dm_target_linear_init(dm_table_entry_t *table_en, int argc, char **argv)
     58   1.2      haad {
     59   1.2      haad 	dm_target_linear_config_t *tlc;
     60   1.2      haad 	dm_pdev_t *dmp;
     61   1.2      haad 
     62  1.27   tkusumi 	/*
     63  1.27   tkusumi 	if (argc != 2) {
     64  1.27   tkusumi 		printf("Linear target takes 2 args\n");
     65   1.8      haad 		return EINVAL;
     66   1.8      haad 	}
     67  1.27   tkusumi 	*/
     68   1.2      haad 
     69   1.8      haad 	aprint_debug("Linear target init function called %s--%s!!\n",
     70   1.8      haad 	    argv[0], argv[1]);
     71   1.9      haad 
     72   1.2      haad 	/* Insert dmp to global pdev list */
     73   1.8      haad 	if ((dmp = dm_pdev_insert(argv[0])) == NULL)
     74   1.2      haad 		return ENOENT;
     75   1.9      haad 
     76  1.15       chs 	tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
     77   1.2      haad 	tlc->pdev = dmp;
     78   1.8      haad 	tlc->offset = atoi(argv[1]);
     79   1.2      haad 
     80  1.25   tkusumi 	table_en->target_config = tlc;
     81   1.2      haad 
     82   1.2      haad 	return 0;
     83   1.2      haad }
     84  1.17  christos 
     85   1.2      haad /*
     86  1.29   tkusumi  * Table routine is called to get params string, which is target
     87   1.2      haad  * specific. When dm_table_status_ioctl is called with flag
     88   1.2      haad  * DM_STATUS_TABLE_FLAG I have to sent params string back.
     89   1.2      haad  */
     90   1.2      haad char *
     91  1.29   tkusumi dm_target_linear_table(void *target_config)
     92   1.2      haad {
     93   1.2      haad 	dm_target_linear_config_t *tlc;
     94   1.2      haad 	char *params;
     95   1.8      haad 	tlc = target_config;
     96   1.8      haad 
     97  1.29   tkusumi 	aprint_debug("Linear target table function called\n");
     98   1.2      haad 
     99  1.18       chs 	params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
    100   1.9      haad 	snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
    101   1.8      haad 	    tlc->pdev->name, tlc->offset);
    102   1.8      haad 
    103   1.2      haad 	return params;
    104   1.2      haad }
    105  1.17  christos 
    106   1.2      haad /*
    107   1.2      haad  * Do IO operation, called from dmstrategy routine.
    108   1.2      haad  */
    109   1.2      haad int
    110  1.24   tkusumi dm_target_linear_strategy(dm_table_entry_t *table_en, struct buf *bp)
    111   1.2      haad {
    112   1.2      haad 	dm_target_linear_config_t *tlc;
    113   1.2      haad 
    114   1.2      haad 	tlc = table_en->target_config;
    115   1.9      haad 
    116   1.2      haad 	bp->b_blkno += tlc->offset;
    117   1.9      haad 
    118   1.2      haad 	VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
    119   1.9      haad 
    120   1.2      haad 	return 0;
    121   1.2      haad }
    122  1.17  christos 
    123   1.2      haad /*
    124  1.10      haad  * Sync underlying disk caches.
    125  1.10      haad  */
    126  1.10      haad int
    127  1.24   tkusumi dm_target_linear_sync(dm_table_entry_t *table_en)
    128  1.10      haad {
    129  1.10      haad 	int cmd;
    130  1.10      haad 	dm_target_linear_config_t *tlc;
    131  1.10      haad 
    132  1.10      haad 	tlc = table_en->target_config;
    133  1.10      haad 
    134  1.10      haad 	cmd = 1;
    135  1.23   tkusumi 
    136  1.10      haad 	return VOP_IOCTL(tlc->pdev->pdev_vnode,  DIOCCACHESYNC, &cmd,
    137  1.23   tkusumi 	    FREAD|FWRITE, kauth_cred_get());
    138  1.10      haad }
    139  1.17  christos 
    140  1.10      haad /*
    141   1.2      haad  * Destroy target specific data. Decrement table pdevs.
    142   1.2      haad  */
    143   1.2      haad int
    144  1.24   tkusumi dm_target_linear_destroy(dm_table_entry_t *table_en)
    145   1.2      haad {
    146   1.2      haad 
    147   1.2      haad 	/*
    148   1.2      haad 	 * Destroy function is called for every target even if it
    149   1.2      haad 	 * doesn't have target_config.
    150   1.2      haad 	 */
    151   1.2      haad 	if (table_en->target_config == NULL)
    152  1.17  christos 		goto out;
    153   1.9      haad 
    154  1.17  christos 	dm_target_linear_config_t *tlc = table_en->target_config;
    155   1.9      haad 
    156   1.2      haad 	/* Decrement pdev ref counter if 0 remove it */
    157   1.2      haad 	dm_pdev_decr(tlc->pdev);
    158   1.9      haad 
    159  1.17  christos 	kmem_free(tlc, sizeof(*tlc));
    160  1.17  christos 
    161  1.17  christos out:
    162   1.3      haad 	/* Unbusy target so we can unload it */
    163   1.3      haad 	dm_target_unbusy(table_en->target);
    164   1.2      haad 	return 0;
    165   1.2      haad }
    166  1.17  christos 
    167  1.16   mbalmer /* Add this target pdev dependencies to prop_array_t */
    168   1.2      haad int
    169  1.24   tkusumi dm_target_linear_deps(dm_table_entry_t *table_en, prop_array_t prop_array)
    170   1.2      haad {
    171   1.2      haad 	dm_target_linear_config_t *tlc;
    172   1.9      haad 
    173   1.2      haad 	if (table_en->target_config == NULL)
    174   1.2      haad 		return ENOENT;
    175   1.9      haad 
    176   1.2      haad 	tlc = table_en->target_config;
    177   1.9      haad 
    178  1.14   hannken 	prop_array_add_uint64(prop_array,
    179  1.14   hannken 	    (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
    180   1.9      haad 
    181   1.2      haad 	return 0;
    182   1.2      haad }
    183  1.17  christos 
    184   1.2      haad /*
    185   1.2      haad  * Register upcall device.
    186   1.2      haad  * Linear target doesn't need any upcall devices but other targets like
    187   1.2      haad  * mirror, snapshot, multipath, stripe will use this functionality.
    188   1.2      haad  */
    189   1.2      haad int
    190  1.24   tkusumi dm_target_linear_upcall(dm_table_entry_t *table_en, struct buf *bp)
    191   1.2      haad {
    192  1.31   tkusumi 
    193   1.2      haad 	return 0;
    194   1.2      haad }
    195  1.17  christos 
    196   1.2      haad /*
    197  1.12   mlelstv  * Query physical block size of this target
    198  1.12   mlelstv  * For a linear target this is just the sector size of the underlying device
    199  1.12   mlelstv  */
    200  1.12   mlelstv int
    201  1.30   tkusumi dm_target_linear_secsize(dm_table_entry_t *table_en, unsigned int *secsizep)
    202  1.12   mlelstv {
    203  1.12   mlelstv 	dm_target_linear_config_t *tlc;
    204  1.30   tkusumi 	unsigned int secsize;
    205  1.12   mlelstv 
    206  1.12   mlelstv 	secsize = 0;
    207  1.12   mlelstv 
    208  1.12   mlelstv 	tlc = table_en->target_config;
    209  1.12   mlelstv 	if (tlc != NULL)
    210  1.12   mlelstv 		secsize = tlc->pdev->pdev_secsize;
    211  1.12   mlelstv 
    212  1.12   mlelstv 	*secsizep = secsize;
    213  1.12   mlelstv 
    214  1.12   mlelstv 	return 0;
    215  1.12   mlelstv }
    216  1.17  christos 
    217  1.12   mlelstv /*
    218   1.2      haad  * Transform char s to uint64_t offset number.
    219   1.2      haad  */
    220   1.2      haad uint64_t
    221   1.2      haad atoi(const char *s)
    222   1.2      haad {
    223   1.2      haad 	uint64_t n;
    224   1.2      haad 	n = 0;
    225   1.2      haad 
    226   1.2      haad 	while (*s != '\0') {
    227   1.2      haad 		if (!isdigit(*s))
    228   1.2      haad 			break;
    229   1.2      haad 
    230   1.2      haad 		n = (10 * n) + (*s - '0');
    231   1.2      haad 		s++;
    232   1.2      haad 	}
    233   1.2      haad 
    234   1.2      haad 	return n;
    235   1.2      haad }
    236