Annotation of src/sys/dev/dm/dm_target_linear.c, revision 1.9

1.9     ! haad        1: /*        $NetBSD: dm_target_linear.c,v 1.8 2009/12/01 23:12:10 haad Exp $      */
1.2       haad        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 "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
1.9     ! haad       51:  * therefore we can add things to pdev list. This should not a
1.6       yamt       52:  * problem because this routine is called only from dm_table_load_ioctl.
1.2       haad       53:  * @argv[0] is name,
                     54:  * @argv[1] is physical data offset.
                     55:  */
                     56: int
1.9     ! haad       57: dm_target_linear_init(dm_dev_t * dmv, void **target_config, char *params)
1.2       haad       58: {
                     59:        dm_target_linear_config_t *tlc;
                     60:        dm_pdev_t *dmp;
                     61: 
1.8       haad       62:        char **ap, *argv[3];
                     63: 
1.9     ! haad       64:        if (params == NULL)
1.8       haad       65:                return EINVAL;
1.9     ! haad       66: 
1.8       haad       67:        /*
                     68:         * Parse a string, containing tokens delimited by white space,
                     69:         * into an argument vector
                     70:         */
                     71:        for (ap = argv; ap < &argv[2] &&
1.9     ! haad       72:            (*ap = strsep(&params, " \t")) != NULL;) {
1.8       haad       73:                if (**ap != '\0')
                     74:                        ap++;
                     75:        }
1.2       haad       76: 
1.8       haad       77:        aprint_debug("Linear target init function called %s--%s!!\n",
                     78:            argv[0], argv[1]);
1.9     ! haad       79: 
1.2       haad       80:        /* Insert dmp to global pdev list */
1.8       haad       81:        if ((dmp = dm_pdev_insert(argv[0])) == NULL)
1.2       haad       82:                return ENOENT;
1.9     ! haad       83: 
1.7       haad       84:        if ((tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP))
1.2       haad       85:            == NULL)
1.8       haad       86:                return ENOMEM;
1.2       haad       87: 
                     88:        tlc->pdev = dmp;
1.9     ! haad       89:        tlc->offset = 0;        /* default settings */
        !            90: 
1.2       haad       91:        /* Check user input if it is not leave offset as 0. */
1.8       haad       92:        tlc->offset = atoi(argv[1]);
1.2       haad       93: 
1.9     ! haad       94:        *target_config = tlc;
1.2       haad       95: 
                     96:        dmv->dev_type = DM_LINEAR_DEV;
1.9     ! haad       97: 
1.2       haad       98:        return 0;
                     99: }
                    100: /*
                    101:  * Status routine is called to get params string, which is target
                    102:  * specific. When dm_table_status_ioctl is called with flag
                    103:  * DM_STATUS_TABLE_FLAG I have to sent params string back.
                    104:  */
                    105: char *
                    106: dm_target_linear_status(void *target_config)
                    107: {
                    108:        dm_target_linear_config_t *tlc;
                    109:        char *params;
1.8       haad      110:        tlc = target_config;
                    111: 
1.2       haad      112:        aprint_debug("Linear target status function called\n");
                    113: 
1.8       haad      114:        if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_NOSLEEP)) == NULL)
1.2       haad      115:                return NULL;
                    116: 
1.9     ! haad      117:        aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
        !           118:        snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
1.8       haad      119:            tlc->pdev->name, tlc->offset);
                    120: 
1.2       haad      121:        return params;
                    122: }
                    123: /*
                    124:  * Do IO operation, called from dmstrategy routine.
                    125:  */
                    126: int
1.9     ! haad      127: dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      128: {
                    129:        dm_target_linear_config_t *tlc;
                    130: 
                    131:        tlc = table_en->target_config;
1.9     ! haad      132: 
1.2       haad      133: /*     printf("Linear target read function called %" PRIu64 "!!\n",
                    134:        tlc->offset);*/
                    135: 
                    136:        bp->b_blkno += tlc->offset;
1.9     ! haad      137: 
1.2       haad      138:        VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
1.9     ! haad      139: 
1.2       haad      140:        return 0;
                    141: 
                    142: }
                    143: /*
                    144:  * Destroy target specific data. Decrement table pdevs.
                    145:  */
                    146: int
1.9     ! haad      147: dm_target_linear_destroy(dm_table_entry_t * table_en)
1.2       haad      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:         */
1.9     ! haad      155: 
1.2       haad      156:        if (table_en->target_config == NULL)
                    157:                return 0;
1.9     ! haad      158: 
1.2       haad      159:        tlc = table_en->target_config;
1.9     ! haad      160: 
1.2       haad      161:        /* Decrement pdev ref counter if 0 remove it */
                    162:        dm_pdev_decr(tlc->pdev);
1.9     ! haad      163: 
1.3       haad      164:        /* Unbusy target so we can unload it */
                    165:        dm_target_unbusy(table_en->target);
1.9     ! haad      166: 
1.2       haad      167:        kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
                    168: 
                    169:        table_en->target_config = NULL;
1.9     ! haad      170: 
1.2       haad      171:        return 0;
                    172: }
                    173: /* Add this target pdev dependiences to prop_array_t */
                    174: int
1.9     ! haad      175: dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
1.2       haad      176: {
                    177:        dm_target_linear_config_t *tlc;
                    178:        struct vattr va;
1.9     ! haad      179: 
1.2       haad      180:        int error;
1.9     ! haad      181: 
1.2       haad      182:        if (table_en->target_config == NULL)
                    183:                return ENOENT;
1.9     ! haad      184: 
1.2       haad      185:        tlc = table_en->target_config;
1.9     ! haad      186: 
1.2       haad      187:        if ((error = VOP_GETATTR(tlc->pdev->pdev_vnode, &va, curlwp->l_cred)) != 0)
                    188:                return error;
1.9     ! haad      189: 
        !           190:        prop_array_add_uint64(prop_array, (uint64_t) va.va_rdev);
        !           191: 
1.2       haad      192:        return 0;
                    193: }
                    194: /*
                    195:  * Register upcall device.
                    196:  * Linear target doesn't need any upcall devices but other targets like
                    197:  * mirror, snapshot, multipath, stripe will use this functionality.
                    198:  */
                    199: int
1.9     ! haad      200: dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      201: {
                    202:        return 0;
                    203: }
                    204: /*
                    205:  * Transform char s to uint64_t offset number.
                    206:  */
                    207: uint64_t
                    208: atoi(const char *s)
                    209: {
                    210:        uint64_t n;
                    211:        n = 0;
                    212: 
                    213:        while (*s != '\0') {
                    214:                if (!isdigit(*s))
                    215:                        break;
                    216: 
                    217:                n = (10 * n) + (*s - '0');
                    218:                s++;
                    219:        }
                    220: 
                    221:        return n;
                    222: }

CVSweb <webmaster@jp.NetBSD.org>