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

1.21    ! tkusumi     1: /*        $NetBSD: dm_target_linear.c,v 1.20 2019/12/02 16:10:34 tkusumi 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>
1.21    ! tkusumi    32: __KERNEL_RCSID(0, "$NetBSD: dm_target_linear.c,v 1.20 2019/12/02 16:10:34 tkusumi 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>
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.15      chs        85:        tlc = kmem_alloc(sizeof(dm_target_linear_config_t), KM_SLEEP);
1.2       haad       86:        tlc->pdev = dmp;
1.8       haad       87:        tlc->offset = atoi(argv[1]);
1.2       haad       88: 
1.9       haad       89:        *target_config = tlc;
1.2       haad       90: 
                     91:        return 0;
                     92: }
1.17      christos   93: 
1.2       haad       94: /*
                     95:  * Status routine is called to get params string, which is target
                     96:  * specific. When dm_table_status_ioctl is called with flag
                     97:  * DM_STATUS_TABLE_FLAG I have to sent params string back.
                     98:  */
                     99: char *
                    100: dm_target_linear_status(void *target_config)
                    101: {
                    102:        dm_target_linear_config_t *tlc;
                    103:        char *params;
1.8       haad      104:        tlc = target_config;
                    105: 
1.2       haad      106:        aprint_debug("Linear target status function called\n");
                    107: 
1.18      chs       108:        params = kmem_alloc(DM_MAX_PARAMS_SIZE, KM_SLEEP);
1.9       haad      109:        aprint_normal("%s %" PRIu64, tlc->pdev->name, tlc->offset);
                    110:        snprintf(params, DM_MAX_PARAMS_SIZE, "%s %" PRIu64,
1.8       haad      111:            tlc->pdev->name, tlc->offset);
                    112: 
1.2       haad      113:        return params;
                    114: }
1.17      christos  115: 
1.2       haad      116: /*
                    117:  * Do IO operation, called from dmstrategy routine.
                    118:  */
                    119: int
1.9       haad      120: dm_target_linear_strategy(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      121: {
                    122:        dm_target_linear_config_t *tlc;
                    123: 
                    124:        tlc = table_en->target_config;
1.9       haad      125: 
1.2       haad      126: /*     printf("Linear target read function called %" PRIu64 "!!\n",
                    127:        tlc->offset);*/
                    128: 
                    129:        bp->b_blkno += tlc->offset;
1.9       haad      130: 
1.2       haad      131:        VOP_STRATEGY(tlc->pdev->pdev_vnode, bp);
1.9       haad      132: 
1.2       haad      133:        return 0;
                    134: 
                    135: }
1.17      christos  136: 
1.2       haad      137: /*
1.10      haad      138:  * Sync underlying disk caches.
                    139:  */
                    140: int
                    141: dm_target_linear_sync(dm_table_entry_t * table_en)
                    142: {
                    143:        int cmd;
                    144:        dm_target_linear_config_t *tlc;
                    145: 
                    146:        tlc = table_en->target_config;
                    147: 
                    148:        cmd = 1;
                    149:        
                    150:        return VOP_IOCTL(tlc->pdev->pdev_vnode,  DIOCCACHESYNC, &cmd,
                    151:            FREAD|FWRITE, kauth_cred_get());    
                    152: }
1.17      christos  153: 
1.10      haad      154: /*
1.2       haad      155:  * Destroy target specific data. Decrement table pdevs.
                    156:  */
                    157: int
1.9       haad      158: dm_target_linear_destroy(dm_table_entry_t * table_en)
1.2       haad      159: {
                    160: 
                    161:        /*
                    162:         * Destroy function is called for every target even if it
                    163:         * doesn't have target_config.
                    164:         */
                    165:        if (table_en->target_config == NULL)
1.17      christos  166:                goto out;
1.9       haad      167: 
1.17      christos  168:        dm_target_linear_config_t *tlc = table_en->target_config;
                    169:        table_en->target_config = NULL;
1.9       haad      170: 
1.2       haad      171:        /* Decrement pdev ref counter if 0 remove it */
                    172:        dm_pdev_decr(tlc->pdev);
1.9       haad      173: 
1.17      christos  174:        kmem_free(tlc, sizeof(*tlc));
                    175: 
                    176: out:
1.3       haad      177:        /* Unbusy target so we can unload it */
                    178:        dm_target_unbusy(table_en->target);
1.2       haad      179:        return 0;
                    180: }
1.17      christos  181: 
1.16      mbalmer   182: /* Add this target pdev dependencies to prop_array_t */
1.2       haad      183: int
1.9       haad      184: dm_target_linear_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
1.2       haad      185: {
                    186:        dm_target_linear_config_t *tlc;
1.9       haad      187: 
1.2       haad      188:        if (table_en->target_config == NULL)
                    189:                return ENOENT;
1.9       haad      190: 
1.2       haad      191:        tlc = table_en->target_config;
1.9       haad      192: 
1.14      hannken   193:        prop_array_add_uint64(prop_array,
                    194:            (uint64_t) tlc->pdev->pdev_vnode->v_rdev);
1.9       haad      195: 
1.2       haad      196:        return 0;
                    197: }
1.17      christos  198: 
1.2       haad      199: /*
                    200:  * Register upcall device.
                    201:  * Linear target doesn't need any upcall devices but other targets like
                    202:  * mirror, snapshot, multipath, stripe will use this functionality.
                    203:  */
                    204: int
1.9       haad      205: dm_target_linear_upcall(dm_table_entry_t * table_en, struct buf * bp)
1.2       haad      206: {
                    207:        return 0;
                    208: }
1.17      christos  209: 
1.2       haad      210: /*
1.12      mlelstv   211:  * Query physical block size of this target
                    212:  * For a linear target this is just the sector size of the underlying device
                    213:  */
                    214: int
                    215: dm_target_linear_secsize(dm_table_entry_t * table_en, unsigned *secsizep)
                    216: {
                    217:        dm_target_linear_config_t *tlc;
                    218:        unsigned secsize;
                    219: 
                    220:        secsize = 0;
                    221: 
                    222:        tlc = table_en->target_config;
                    223:        if (tlc != NULL)
                    224:                secsize = tlc->pdev->pdev_secsize;
                    225: 
                    226:        *secsizep = secsize;
                    227: 
                    228:        return 0;
                    229: }
1.17      christos  230: 
1.12      mlelstv   231: /*
1.2       haad      232:  * Transform char s to uint64_t offset number.
                    233:  */
                    234: uint64_t
                    235: atoi(const char *s)
                    236: {
                    237:        uint64_t n;
                    238:        n = 0;
                    239: 
                    240:        while (*s != '\0') {
                    241:                if (!isdigit(*s))
                    242:                        break;
                    243: 
                    244:                n = (10 * n) + (*s - '0');
                    245:                s++;
                    246:        }
                    247: 
                    248:        return n;
                    249: }

CVSweb <webmaster@jp.NetBSD.org>