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

1.17    ! christos    1: /*        $NetBSD: dm_target_linear.c,v 1.16 2017/06/04 08:54:38 mbalmer 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:  */
1.17    ! christos   31: #include <sys/cdefs.h>
        !            32: __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.445 2017/12/16 16:04:20 nat Exp $");
1.2       haad       33: 
                     34: /*
                     35:  * This file implements initial version of device-mapper dklinear target.
                     36:  */
                     37: 
                     38: #include <sys/types.h>
                     39: #include <sys/param.h>
                     40: 
                     41: #include <sys/buf.h>
                     42: #include <sys/kmem.h>
                     43: #include <sys/vnode.h>
1.11      uebayasi   44: #include <sys/lwp.h>
1.2       haad       45: 
                     46: #include <machine/int_fmtio.h>
                     47: 
                     48: #include "dm.h"
                     49: 
                     50: /*
                     51:  * Allocate target specific config data, and link them to table.
                     52:  * This function is called only when, flags is not READONLY and
1.9       haad       53:  * therefore we can add things to pdev list. This should not a
1.6       yamt       54:  * problem because this routine is called only from dm_table_load_ioctl.
1.2       haad       55:  * @argv[0] is name,
                     56:  * @argv[1] is physical data offset.
                     57:  */
                     58: int
1.9       haad       59: dm_target_linear_init(dm_dev_t * dmv, void **target_config, char *params)
1.2       haad       60: {
                     61:        dm_target_linear_config_t *tlc;
                     62:        dm_pdev_t *dmp;
                     63: 
1.8       haad       64:        char **ap, *argv[3];
                     65: 
1.9       haad       66:        if (params == NULL)
1.8       haad       67:                return EINVAL;
1.9       haad       68: 
1.8       haad       69:        /*
                     70:         * Parse a string, containing tokens delimited by white space,
                     71:         * into an argument vector
                     72:         */
                     73:        for (ap = argv; ap < &argv[2] &&
1.9       haad       74:            (*ap = strsep(&params, " \t")) != NULL;) {
1.8       haad       75:                if (**ap != '\0')
                     76:                        ap++;
                     77:        }
1.2       haad       78: 
1.8       haad       79:        aprint_debug("Linear target init function called %s--%s!!\n",
                     80:            argv[0], argv[1]);
1.9       haad       81: 
1.2       haad       82:        /* Insert dmp to global pdev list */
1.8       haad       83:        if ((dmp = dm_pdev_insert(argv[0])) == NULL)
1.2       haad       84:                return ENOENT;
1.9       haad       85: 
1.15      chs        86:        tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
1.2       haad       87:        tlc->pdev = dmp;
1.9       haad       88:        tlc->offset = 0;        /* default settings */
                     89: 
1.2       haad       90:        /* Check user input if it is not leave offset as 0. */
1.8       haad       91:        tlc->offset = atoi(argv[1]);
1.2       haad       92: 
1.9       haad       93:        *target_config = tlc;
1.2       haad       94: 
                     95:        dmv->dev_type = DM_LINEAR_DEV;
1.9       haad       96: 
1.2       haad       97:        return 0;
                     98: }
1.17    ! christos   99: 
1.2       haad      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: }
1.17    ! christos  123: 
1.2       haad      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: }
1.17    ! christos  144: 
1.2       haad      145: /*
1.10      haad      146:  * Sync underlying disk caches.
                    147:  */
                    148: int
                    149: dm_target_linear_sync(dm_table_entry_t * table_en)
                    150: {
                    151:        int cmd;
                    152:        dm_target_linear_config_t *tlc;
                    153: 
                    154:        tlc = table_en->target_config;
                    155: 
                    156:        cmd = 1;
                    157:        
                    158:        return VOP_IOCTL(tlc->pdev->pdev_vnode,  DIOCCACHESYNC, &cmd,
                    159:            FREAD|FWRITE, kauth_cred_get());    
                    160: }
1.17    ! christos  161: 
1.10      haad      162: /*
1.2       haad      163:  * Destroy target specific data. Decrement table pdevs.
                    164:  */
                    165: int
1.9       haad      166: dm_target_linear_destroy(dm_table_entry_t * table_en)
1.2       haad      167: {
                    168: 
                    169:        /*
                    170:         * Destroy function is called for every target even if it
                    171:         * doesn't have target_config.
                    172:         */
                    173:        if (table_en->target_config == NULL)
1.17    ! christos  174:                goto out;
1.9       haad      175: 
1.17    ! christos  176:        dm_target_linear_config_t *tlc = table_en->target_config;
        !           177:        table_en->target_config = NULL;
1.9       haad      178: 
1.2       haad      179:        /* Decrement pdev ref counter if 0 remove it */
                    180:        dm_pdev_decr(tlc->pdev);
1.9       haad      181: 
1.17    ! christos  182:        kmem_free(tlc, sizeof(*tlc));
        !           183: 
        !           184: out:
1.3       haad      185:        /* Unbusy target so we can unload it */
                    186:        dm_target_unbusy(table_en->target);
1.2       haad      187:        return 0;
                    188: }
1.17    ! christos  189: 
1.16      mbalmer   190: /* Add this target pdev dependencies to prop_array_t */
1.2       haad      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: }
1.17    ! christos  206: 
1.2       haad      207: /*
                    208:  * Register upcall device.
                    209:  * Linear target doesn't need any upcall devices but other targets like
                    210:  * mirror, snapshot, multipath, stripe will use this functionality.
                    211:  */
                    212: int
1.9       haad      213: dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      214: {
                    215:        return 0;
                    216: }
1.17    ! christos  217: 
1.2       haad      218: /*
1.12      mlelstv   219:  * Query physical block size of this target
                    220:  * For a linear target this is just the sector size of the underlying device
                    221:  */
                    222: int
                    223: dm_target_linear_secsize(dm_table_entry_t * table_en, unsigned *secsizep)
                    224: {
                    225:        dm_target_linear_config_t *tlc;
                    226:        unsigned secsize;
                    227: 
                    228:        secsize = 0;
                    229: 
                    230:        tlc = table_en->target_config;
                    231:        if (tlc != NULL)
                    232:                secsize = tlc->pdev->pdev_secsize;
                    233: 
                    234:        *secsizep = secsize;
                    235: 
                    236:        return 0;
                    237: }
1.17    ! christos  238: 
1.12      mlelstv   239: /*
1.2       haad      240:  * Transform char s to uint64_t offset number.
                    241:  */
                    242: uint64_t
                    243: atoi(const char *s)
                    244: {
                    245:        uint64_t n;
                    246:        n = 0;
                    247: 
                    248:        while (*s != '\0') {
                    249:                if (!isdigit(*s))
                    250:                        break;
                    251: 
                    252:                n = (10 * n) + (*s - '0');
                    253:                s++;
                    254:        }
                    255: 
                    256:        return n;
                    257: }

CVSweb <webmaster@jp.NetBSD.org>