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

1.1.2.7 ! haad        1: /*        $NetBSD: dm_pdev.c,v 1.1.2.6 2008/08/20 16:04:01 haad Exp $      */
1.1.2.4   haad        2: 
1.1.2.1   haad        3: /*
                      4:  * Copyright (c) 1996, 1997, 1998, 1999, 2002 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: #include <sys/types.h>
                     33: #include <sys/param.h>
                     34: 
                     35: #include <sys/disk.h>
                     36: #include <sys/errno.h>
                     37: #include <sys/ioctl.h>
                     38: #include <sys/ioccom.h>
                     39: #include <sys/kmem.h>
                     40: #include <sys/queue.h>
                     41: 
                     42: #include <sys/namei.h>
                     43: #include <sys/vnode.h>
                     44: 
                     45: #include <dev/dkvar.h>
                     46: 
                     47: #include "dm.h"
                     48: 
                     49: static struct dm_pdev *dm_pdev_alloc(const char *);
1.1.2.5   haad       50: static int dm_pdev_rem(struct dm_pdev *);
1.1.2.1   haad       51: 
                     52: /*
                     53:  * XXX. I should use rwlock here.
                     54:  */
                     55: kmutex_t dm_pdev_mutex;
                     56: 
                     57: /*
                     58:  * Find used pdev with name == dm_pdev_name.
                     59:  */
                     60: struct dm_pdev*
                     61: dm_pdev_lookup_name(const char *dm_pdev_name)
                     62: {
                     63:        struct dm_pdev *dm_pdev;
                     64:        int dlen; int slen;
                     65: 
1.1.2.3   haad       66:        slen = strlen(dm_pdev_name);
1.1.2.1   haad       67: 
                     68:        mutex_enter(&dm_pdev_mutex);
                     69: 
                     70:        SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
1.1.2.3   haad       71:                dlen = strlen(dm_pdev->name);
1.1.2.1   haad       72: 
                     73:                if (slen != dlen)
                     74:                        continue;
                     75:                
1.1.2.2   haad       76:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0){
1.1.2.1   haad       77:                        mutex_exit(&dm_pdev_mutex);
                     78:                        return dm_pdev;
                     79:                }
                     80:        }
                     81:        mutex_exit(&dm_pdev_mutex);
                     82: 
                     83:        return NULL;
                     84: }
                     85: 
                     86: /*
1.1.2.7 ! haad       87:  * Lookup dm_pdev_name in general pdev list. This is used to fo device pdevs 
        !            88:  * list.
        !            89:  */
        !            90: struct dm_pdev*
        !            91: dm_pdev_lookup_name_list(const char *dm_pdev_name, struct dm_pdevs *pdev_list)
        !            92: {
        !            93:        struct dm_pdev *dm_pdev;
        !            94:        int dlen; int slen;
        !            95:        
        !            96:        slen = strlen(dm_pdev_name);
        !            97:        
        !            98:        SLIST_FOREACH(dm_pdev, pdev_list, next_pdev) {
        !            99:                dlen = strlen(dm_pdev->name);   
        !           100:                
        !           101:                if (slen != dlen)
        !           102:                        continue;
        !           103:                
        !           104:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
        !           105:                        return dm_pdev;
        !           106:        }
        !           107:        
        !           108:        return NULL;
        !           109: }
        !           110: 
        !           111: /*
1.1.2.1   haad      112:  * Create entry for device with name dev_name and open vnode for it.
                    113:  * If entry already exists in global SLIST I will only increment
                    114:  * reference counter.
                    115:  */
                    116: struct dm_pdev*
                    117: dm_pdev_insert(const char *dev_name)
                    118: {
                    119:        struct dm_pdev *dmp;
                    120:        int error;
                    121: 
                    122:        dmp = dm_pdev_lookup_name(dev_name);
1.1.2.3   haad      123:        
1.1.2.1   haad      124:        if (dmp != NULL) {
1.1.2.3   haad      125: 
                    126:                mutex_enter(&dm_pdev_mutex);
                    127:                
1.1.2.1   haad      128:                dmp->ref_cnt++;
1.1.2.3   haad      129: 
                    130:                mutex_exit(&dm_pdev_mutex);
                    131:                
1.1.2.1   haad      132:                return dmp;
                    133:        }
                    134: 
                    135:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                    136:                return NULL;
                    137: 
                    138:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                    139: 
                    140:        if (error) {
1.1.2.2   haad      141:                aprint_normal("dk_lookup on device: %s failed with error %d!\n",
                    142:                    dev_name, error);
1.1.2.1   haad      143: 
1.1.2.6   haad      144:                kmem_free(dmp, sizeof(struct dm_pdev));
                    145:                
1.1.2.1   haad      146:                return NULL;
                    147:        }
                    148:        
                    149:        dmp->ref_cnt = 1;
1.1.2.3   haad      150:        
1.1.2.1   haad      151:        mutex_enter(&dm_pdev_mutex);
                    152: 
                    153:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    154:        
                    155:        mutex_exit(&dm_pdev_mutex);
                    156: 
                    157:        return dmp;
                    158: }
                    159: 
                    160: /*
                    161:  * Initialize pdev subsystem.
                    162:  */
                    163: int
1.1.2.5   haad      164: dm_pdev_init(void)
1.1.2.1   haad      165: {
                    166:        
                    167:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
                    168:        
                    169:        mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
                    170: 
                    171:        return 0;
                    172: }
                    173: 
                    174: /*
                    175:  * Allocat new pdev structure if is not already present and
                    176:  * set name.
                    177:  */
                    178: static struct dm_pdev*
                    179: dm_pdev_alloc(const char *name)
                    180: {
                    181:        struct dm_pdev *dmp;
                    182: 
1.1.2.2   haad      183:        if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
                    184:                    KM_NOSLEEP)) == NULL)
1.1.2.1   haad      185:                return NULL;
                    186: 
1.1.2.2   haad      187:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      188:        
                    189:        dmp->ref_cnt = 0;
1.1.2.7 ! haad      190:        dmp->list_ref_cnt = 0;
1.1.2.1   haad      191:        dmp->pdev_vnode = NULL;
                    192:        
                    193:        return dmp;
                    194: }
                    195: 
                    196: /*
1.1.2.5   haad      197:  * Destroy allocated dm_pdev. This routine is called with held pdev mutex.
1.1.2.1   haad      198:  */
1.1.2.5   haad      199: static int
                    200: dm_pdev_rem(struct dm_pdev *dmp)
1.1.2.1   haad      201: {
1.1.2.6   haad      202:        int err;
                    203:        
1.1.2.1   haad      204:        if (dmp == NULL)
                    205:                return ENOENT;
1.1.2.2   haad      206:        if (dmp->pdev_vnode != NULL) {
1.1.2.6   haad      207:                err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
                    208:                if (err != 0)
                    209:                        return err;
1.1.2.2   haad      210:        }
1.1.2.6   haad      211:        
1.1.2.5   haad      212:        kmem_free(dmp, sizeof(*dmp));
1.1.2.1   haad      213:        dmp = NULL;
                    214:        
                    215:        return 0;
                    216: }
                    217: 
                    218: /*
1.1.2.5   haad      219:  * Destroy all existing pdev's in device-mapper.
                    220:  */
                    221: int
                    222: dm_pdev_destroy(void)
                    223: {
                    224:        struct dm_pdev *dm_pdev;
                    225:        
                    226:        mutex_enter(&dm_pdev_mutex);
                    227: 
                    228:        while (!SLIST_EMPTY(&dm_pdev_list)) {           /* List Deletion. */
                    229:                
                    230:                dm_pdev = SLIST_FIRST(&dm_pdev_list);
                    231:                
                    232:                SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
                    233: 
                    234:                dm_pdev_rem(dm_pdev);
                    235:        }
                    236:        
                    237:        mutex_exit(&dm_pdev_mutex);
                    238: 
                    239:        return 0;
                    240: }
                    241: 
                    242: /*
1.1.2.1   haad      243:  * This funcion is called from dm_dev_remove_ioctl.
                    244:  * When I'm removing device from list, I have to decrement
                    245:  * reference counter. If reference counter is 0 I will remove
                    246:  * dmp from global list and from device list to. And I will CLOSE
                    247:  * dmp vnode too.
                    248:  */
                    249: 
                    250: /*
                    251:  * I'm guarding only global list with mutex, device should be
                    252:  * locked elsewhere.
                    253:  */
                    254: int
1.1.2.5   haad      255: dm_pdev_decr(struct dm_pdev *dmp)
1.1.2.1   haad      256: {
1.1.2.5   haad      257:        if (dmp == NULL)
1.1.2.1   haad      258:                return ENOENT;
                    259: 
1.1.2.3   haad      260:        mutex_enter(&dm_pdev_mutex);
1.1.2.5   haad      261:        
                    262:        dmp->ref_cnt--;
1.1.2.6   haad      263: 
1.1.2.5   haad      264:        /*
                    265:         * If this was last reference remove dmp from
                    266:         * global list also.
                    267:         */
                    268:        if (dmp->ref_cnt == 0) {
                    269:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
                    270:                dm_pdev_rem(dmp);
1.1.2.1   haad      271:        }
                    272: 
1.1.2.3   haad      273:        mutex_exit(&dm_pdev_mutex);
                    274: 
1.1.2.1   haad      275:        return 0;
                    276: }

CVSweb <webmaster@jp.NetBSD.org>