Annotation of src/sys/dev/dm/dm_pdev.c, revision 1.23

1.23    ! andvar      1: /*        $NetBSD: dm_pdev.c,v 1.22 2019/12/20 16:16:36 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.10      christos   31: #include <sys/cdefs.h>
1.23    ! andvar     32: __KERNEL_RCSID(0, "$NetBSD: dm_pdev.c,v 1.22 2019/12/20 16:16:36 tkusumi Exp $");
1.2       haad       33: 
                     34: #include <sys/types.h>
                     35: #include <sys/param.h>
                     36: #include <sys/disk.h>
                     37: #include <sys/fcntl.h>
                     38: #include <sys/kmem.h>
                     39: #include <sys/namei.h>
                     40: 
                     41: #include <dev/dkvar.h>
                     42: 
                     43: #include "dm.h"
                     44: 
1.15      tkusumi    45: static SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
1.2       haad       46: 
1.13      tkusumi    47: static kmutex_t dm_pdev_mutex;
1.2       haad       48: 
1.10      christos   49: static dm_pdev_t *dm_pdev_alloc(const char *);
                     50: static int dm_pdev_rem(dm_pdev_t *);
                     51: static dm_pdev_t *dm_pdev_lookup_name(const char *);
1.2       haad       52: 
                     53: /*
1.10      christos   54: * Find used pdev with name == dm_pdev_name.
                     55: */
                     56: dm_pdev_t *
                     57: dm_pdev_lookup_name(const char *dm_pdev_name)
1.2       haad       58: {
                     59:        dm_pdev_t *dm_pdev;
1.10      christos   60:        size_t dlen;
                     61:        size_t slen;
1.2       haad       62: 
                     63:        KASSERT(dm_pdev_name != NULL);
                     64: 
                     65:        slen = strlen(dm_pdev_name);
                     66: 
                     67:        SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
                     68:                dlen = strlen(dm_pdev->name);
                     69: 
                     70:                if (slen != dlen)
                     71:                        continue;
1.6       haad       72: 
1.2       haad       73:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
                     74:                        return dm_pdev;
                     75:        }
                     76: 
                     77:        return NULL;
                     78: }
1.10      christos   79: 
1.2       haad       80: /*
                     81:  * Create entry for device with name dev_name and open vnode for it.
                     82:  * If entry already exists in global SLIST I will only increment
                     83:  * reference counter.
                     84:  */
1.6       haad       85: dm_pdev_t *
1.2       haad       86: dm_pdev_insert(const char *dev_name)
                     87: {
1.7       dholland   88:        struct pathbuf *dev_pb;
1.2       haad       89:        dm_pdev_t *dmp;
                     90:        int error;
1.6       haad       91: 
1.2       haad       92:        KASSERT(dev_name != NULL);
1.6       haad       93: 
1.2       haad       94:        mutex_enter(&dm_pdev_mutex);
                     95:        dmp = dm_pdev_lookup_name(dev_name);
                     96: 
                     97:        if (dmp != NULL) {
                     98:                dmp->ref_cnt++;
1.10      christos   99:                aprint_debug("%s: pdev %s already in tree\n",
                    100:                    __func__, dev_name);
1.2       haad      101:                mutex_exit(&dm_pdev_mutex);
                    102:                return dmp;
                    103:        }
1.6       haad      104: 
1.11      tkusumi   105:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL) {
                    106:                mutex_exit(&dm_pdev_mutex);
1.2       haad      107:                return NULL;
1.11      tkusumi   108:        }
1.2       haad      109: 
1.7       dholland  110:        dev_pb = pathbuf_create(dev_name);
                    111:        if (dev_pb == NULL) {
1.10      christos  112:                aprint_debug("%s: pathbuf_create on device: %s failed!\n",
                    113:                    __func__, dev_name);
1.11      tkusumi   114:                mutex_exit(&dm_pdev_mutex);
1.7       dholland  115:                kmem_free(dmp, sizeof(dm_pdev_t));
                    116:                return NULL;
                    117:        }
1.19      mlelstv   118:        error = vn_bdev_openpath(dev_pb, &dmp->pdev_vnode, curlwp);
1.7       dholland  119:        pathbuf_destroy(dev_pb);
1.2       haad      120:        if (error) {
1.19      mlelstv   121:                aprint_debug("%s: lookup on device: %s (error %d)\n",
1.10      christos  122:                    __func__, dev_name, error);
1.11      tkusumi   123:                mutex_exit(&dm_pdev_mutex);
1.2       haad      124:                kmem_free(dmp, sizeof(dm_pdev_t));
                    125:                return NULL;
                    126:        }
1.8       mlelstv   127:        getdisksize(dmp->pdev_vnode, &dmp->pdev_numsec, &dmp->pdev_secsize);
1.6       haad      128:        dmp->ref_cnt = 1;
1.2       haad      129: 
1.22      tkusumi   130:        snprintf(dmp->udev_name, sizeof(dmp->udev_name), "%d:%d",
                    131:            major(dmp->pdev_vnode->v_rdev), minor(dmp->pdev_vnode->v_rdev));
                    132:        aprint_debug("%s: %s %s\n", __func__, dev_name, dmp->udev_name);
                    133: 
1.2       haad      134:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    135:        mutex_exit(&dm_pdev_mutex);
                    136: 
                    137:        return dmp;
                    138: }
1.10      christos  139: 
1.2       haad      140: /*
                    141:  * Initialize pdev subsystem.
                    142:  */
                    143: int
                    144: dm_pdev_init(void)
                    145: {
1.21      tkusumi   146: 
1.6       haad      147:        SLIST_INIT(&dm_pdev_list);      /* initialize global pdev list */
1.2       haad      148:        mutex_init(&dm_pdev_mutex, MUTEX_DEFAULT, IPL_NONE);
1.6       haad      149: 
1.2       haad      150:        return 0;
                    151: }
1.10      christos  152: 
1.2       haad      153: /*
                    154:  * Allocat new pdev structure if is not already present and
                    155:  * set name.
                    156:  */
1.6       haad      157: static dm_pdev_t *
1.2       haad      158: dm_pdev_alloc(const char *name)
                    159: {
                    160:        dm_pdev_t *dmp;
                    161: 
1.10      christos  162:        dmp = kmem_zalloc(sizeof(*dmp), KM_SLEEP);
                    163:        strlcpy(dmp->name, name, sizeof(dmp->name));
1.2       haad      164:        dmp->ref_cnt = 0;
                    165:        dmp->pdev_vnode = NULL;
1.6       haad      166: 
1.2       haad      167:        return dmp;
                    168: }
1.10      christos  169: 
1.2       haad      170: /*
                    171:  * Destroy allocated dm_pdev.
                    172:  */
                    173: static int
1.18      tkusumi   174: dm_pdev_rem(dm_pdev_t *dmp)
1.2       haad      175: {
                    176: 
                    177:        KASSERT(dmp != NULL);
1.6       haad      178: 
1.2       haad      179:        if (dmp->pdev_vnode != NULL) {
1.10      christos  180:                int error = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
1.20      tkusumi   181:                if (error != 0) {
                    182:                        kmem_free(dmp, sizeof(*dmp));
1.10      christos  183:                        return error;
1.20      tkusumi   184:                }
1.2       haad      185:        }
                    186:        kmem_free(dmp, sizeof(*dmp));
1.6       haad      187: 
1.2       haad      188:        return 0;
                    189: }
1.10      christos  190: 
1.2       haad      191: /*
                    192:  * Destroy all existing pdev's in device-mapper.
                    193:  */
                    194: int
                    195: dm_pdev_destroy(void)
                    196: {
1.17      tkusumi   197:        dm_pdev_t *dmp;
1.2       haad      198: 
                    199:        mutex_enter(&dm_pdev_mutex);
1.6       haad      200: 
1.17      tkusumi   201:        while ((dmp = SLIST_FIRST(&dm_pdev_list)) != NULL) {
                    202:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
                    203:                dm_pdev_rem(dmp);
                    204:        }
                    205:        KASSERT(SLIST_EMPTY(&dm_pdev_list));
1.2       haad      206: 
                    207:        mutex_exit(&dm_pdev_mutex);
                    208: 
                    209:        mutex_destroy(&dm_pdev_mutex);
                    210:        return 0;
                    211: }
1.10      christos  212: 
1.2       haad      213: /*
1.23    ! andvar    214:  * This function is called from dm_dev_remove_ioctl.
1.2       haad      215:  * When I'm removing device from list, I have to decrement
                    216:  * reference counter. If reference counter is 0 I will remove
                    217:  * dmp from global list and from device list to. And I will CLOSE
                    218:  * dmp vnode too.
                    219:  */
                    220: 
                    221: /*
                    222:  * Decrement pdev reference counter if 0 remove it.
                    223:  */
                    224: int
1.18      tkusumi   225: dm_pdev_decr(dm_pdev_t *dmp)
1.2       haad      226: {
1.21      tkusumi   227: 
1.2       haad      228:        KASSERT(dmp != NULL);
                    229:        /*
                    230:         * If this was last reference remove dmp from
                    231:         * global list also.
                    232:         */
                    233:        mutex_enter(&dm_pdev_mutex);
1.6       haad      234: 
1.2       haad      235:        if (--dmp->ref_cnt == 0) {
1.6       haad      236:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev);
1.2       haad      237:                mutex_exit(&dm_pdev_mutex);
                    238:                dm_pdev_rem(dmp);
                    239:                return 0;
                    240:        }
1.21      tkusumi   241: 
1.2       haad      242:        mutex_exit(&dm_pdev_mutex);
                    243:        return 0;
                    244: }
1.10      christos  245: 
                    246: #if 0
                    247: static int
                    248: dm_pdev_dump_list(void)
                    249: {
                    250:        dm_pdev_t *dmp;
1.16      tkusumi   251: 
1.10      christos  252:        aprint_verbose("Dumping dm_pdev_list\n");
1.16      tkusumi   253: 
1.10      christos  254:        SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
                    255:                aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
                    256:                dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
                    257:        }
1.16      tkusumi   258: 
1.10      christos  259:        return 0;
                    260: }
                    261: #endif

CVSweb <webmaster@jp.NetBSD.org>