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

1.14    ! hannken     1: /*        $NetBSD: dm_target_linear.c,v 1.13 2011/10/14 09:23:30 hannken 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>
1.11      uebayasi   43: #include <sys/lwp.h>
1.2       haad       44: 
                     45: #include <machine/int_fmtio.h>
                     46: 
                     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
1.9       haad       52:  * therefore we can add things to pdev list. This should not a
1.6       yamt       53:  * problem because this routine is called only from dm_table_load_ioctl.
1.2       haad       54:  * @argv[0] is name,
                     55:  * @argv[1] is physical data offset.
                     56:  */
                     57: int
1.9       haad       58: dm_target_linear_init(dm_dev_t * dmv, void **target_config, char *params)
1.2       haad       59: {
                     60:        dm_target_linear_config_t *tlc;
                     61:        dm_pdev_t *dmp;
                     62: 
1.8       haad       63:        char **ap, *argv[3];
                     64: 
1.9       haad       65:        if (params == NULL)
1.8       haad       66:                return EINVAL;
1.9       haad       67: 
1.8       haad       68:        /*
                     69:         * Parse a string, containing tokens delimited by white space,
                     70:         * into an argument vector
                     71:         */
                     72:        for (ap = argv; ap < &argv[2] &&
1.9       haad       73:            (*ap = strsep(&params, " \t")) != NULL;) {
1.8       haad       74:                if (**ap != '\0')
                     75:                        ap++;
                     76:        }
1.2       haad       77: 
1.8       haad       78:        aprint_debug("Linear target init function called %s--%s!!\n",
                     79:            argv[0], argv[1]);
1.9       haad       80: 
1.2       haad       81:        /* Insert dmp to global pdev list */
1.8       haad       82:        if ((dmp = dm_pdev_insert(argv[0])) == NULL)
1.2       haad       83:                return ENOENT;
1.9       haad       84: 
1.7       haad       85:        if ((tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP))
1.2       haad       86:            == NULL)
1.8       haad       87:                return ENOMEM;
1.2       haad       88: 
                     89:        tlc->pdev = dmp;
1.9       haad       90:        tlc->offset = 0;        /* default settings */
                     91: 
1.2       haad       92:        /* Check user input if it is not leave offset as 0. */
1.8       haad       93:        tlc->offset = atoi(argv[1]);
1.2       haad       94: 
1.9       haad       95:        *target_config = tlc;
1.2       haad       96: 
                     97:        dmv->dev_type = DM_LINEAR_DEV;
1.9       haad       98: 
1.2       haad       99:        return 0;
                    100: }
                    101: /*
                    102:  * Status routine is called to get params string, which is target
                    103:  * specific. When dm_table_status_ioctl is called with flag
                    104:  * DM_STATUS_TABLE_FLAG I have to sent params string back.
                    105:  */
                    106: char *
                    107: dm_target_linear_status(void *target_config)
                    108: {
                    109:        dm_target_linear_config_t *tlc;
                    110:        char *params;
1.8       haad      111:        tlc = target_config;
                    112: 
1.2       haad      113:        aprint_debug("Linear target status function called\n");
                    114: 
1.8       haad      115:        if ((params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_NOSLEEP)) == NULL)
1.2       haad      116:                return NULL;
                    117: 
1.9       haad      118:        aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
                    119:        snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
1.8       haad      120:            tlc->pdev->name, tlc->offset);
                    121: 
1.2       haad      122:        return params;
                    123: }
                    124: /*
                    125:  * Do IO operation, called from dmstrategy routine.
                    126:  */
                    127: int
1.9       haad      128: dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      129: {
                    130:        dm_target_linear_config_t *tlc;
                    131: 
                    132:        tlc = table_en->target_config;
1.9       haad      133: 
1.2       haad      134: /*     printf("Linear target read function called %" PRIu64 "!!\n",
                    135:        tlc->offset);*/
                    136: 
                    137:        bp->b_blkno += tlc->offset;
1.9       haad      138: 
1.2       haad      139:        VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
1.9       haad      140: 
1.2       haad      141:        return 0;
                    142: 
                    143: }
                    144: /*
1.10      haad      145:  * Sync underlying disk caches.
                    146:  */
                    147: int
                    148: dm_target_linear_sync(dm_table_entry_t * table_en)
                    149: {
                    150:        int cmd;
                    151:        dm_target_linear_config_t *tlc;
                    152: 
                    153:        tlc = table_en->target_config;
                    154: 
                    155:        cmd = 1;
                    156:        
                    157:        return VOP_IOCTL(tlc->pdev->pdev_vnode,  DIOCCACHESYNC, &cmd,
                    158:            FREAD|FWRITE, kauth_cred_get());    
                    159: }
                    160: /*
1.2       haad      161:  * Destroy target specific data. Decrement table pdevs.
                    162:  */
                    163: int
1.9       haad      164: dm_target_linear_destroy(dm_table_entry_t * table_en)
1.2       haad      165: {
                    166:        dm_target_linear_config_t *tlc;
                    167: 
                    168:        /*
                    169:         * Destroy function is called for every target even if it
                    170:         * doesn't have target_config.
                    171:         */
1.9       haad      172: 
1.2       haad      173:        if (table_en->target_config == NULL)
                    174:                return 0;
1.9       haad      175: 
1.2       haad      176:        tlc = table_en->target_config;
1.9       haad      177: 
1.2       haad      178:        /* Decrement pdev ref counter if 0 remove it */
                    179:        dm_pdev_decr(tlc->pdev);
1.9       haad      180: 
1.3       haad      181:        /* Unbusy target so we can unload it */
                    182:        dm_target_unbusy(table_en->target);
1.9       haad      183: 
1.2       haad      184:        kmem_free(table_en->target_config, sizeof(dm_target_linear_config_t));
                    185: 
                    186:        table_en->target_config = NULL;
1.9       haad      187: 
1.2       haad      188:        return 0;
                    189: }
                    190: /* Add this target pdev dependiences to prop_array_t */
                    191: int
1.9       haad      192: dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
1.2       haad      193: {
                    194:        dm_target_linear_config_t *tlc;
1.9       haad      195: 
1.2       haad      196:        if (table_en->target_config == NULL)
                    197:                return ENOENT;
1.9       haad      198: 
1.2       haad      199:        tlc = table_en->target_config;
1.9       haad      200: 
1.14    ! hannken   201:        prop_array_add_uint64(prop_array,
        !           202:            (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
1.9       haad      203: 
1.2       haad      204:        return 0;
                    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
1.9       haad      212: dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      213: {
                    214:        return 0;
                    215: }
                    216: /*
1.12      mlelstv   217:  * Query physical block size of this target
                    218:  * For a linear target this is just the sector size of the underlying device
                    219:  */
                    220: int
                    221: dm_target_linear_secsize(dm_table_entry_t * table_en, unsigned *secsizep)
                    222: {
                    223:        dm_target_linear_config_t *tlc;
                    224:        unsigned secsize;
                    225: 
                    226:        secsize = 0;
                    227: 
                    228:        tlc = table_en->target_config;
                    229:        if (tlc != NULL)
                    230:                secsize = tlc->pdev->pdev_secsize;
                    231: 
                    232:        *secsizep = secsize;
                    233: 
                    234:        return 0;
                    235: }
                    236: /*
1.2       haad      237:  * Transform char s to uint64_t offset number.
                    238:  */
                    239: uint64_t
                    240: atoi(const char *s)
                    241: {
                    242:        uint64_t n;
                    243:        n = 0;
                    244: 
                    245:        while (*s != '\0') {
                    246:                if (!isdigit(*s))
                    247:                        break;
                    248: 
                    249:                n = (10 * n) + (*s - '0');
                    250:                s++;
                    251:        }
                    252: 
                    253:        return n;
                    254: }

CVSweb <webmaster@jp.NetBSD.org>