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

1.1.2.5 ! haad        1: /*        $NetBSD: dm_pdev.c,v 1.1.2.4 2008/08/19 13:30:36 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: /*
                     87:  * Create entry for device with name dev_name and open vnode for it.
                     88:  * If entry already exists in global SLIST I will only increment
                     89:  * reference counter.
                     90:  */
                     91: struct dm_pdev*
                     92: dm_pdev_insert(const char *dev_name)
                     93: {
                     94:        struct dm_pdev *dmp;
                     95:        int error;
                     96: 
                     97:        dmp = dm_pdev_lookup_name(dev_name);
                     98: 
1.1.2.3   haad       99:        
1.1.2.1   haad      100:        if (dmp != NULL) {
1.1.2.3   haad      101: 
                    102:                mutex_enter(&dm_pdev_mutex);
                    103:                
1.1.2.1   haad      104:                dmp->ref_cnt++;
1.1.2.3   haad      105: 
                    106:                mutex_exit(&dm_pdev_mutex);
                    107:                
1.1.2.1   haad      108:                return dmp;
                    109:        }
                    110: 
                    111:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                    112:                return NULL;
                    113: 
                    114:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                    115: 
                    116:        if (error) {
1.1.2.2   haad      117:                aprint_normal("dk_lookup on device: %s failed with error %d!\n",
                    118:                    dev_name, error);
1.1.2.1   haad      119: 
                    120:                return NULL;
                    121:        }
                    122:        
                    123:        dmp->ref_cnt = 1;
1.1.2.3   haad      124:        
1.1.2.1   haad      125:        mutex_enter(&dm_pdev_mutex);
                    126: 
                    127:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    128:        
                    129:        mutex_exit(&dm_pdev_mutex);
                    130: 
                    131:        return dmp;
                    132: }
                    133: 
                    134: /*
                    135:  * Initialize pdev subsystem.
                    136:  */
                    137: int
1.1.2.5 ! haad      138: dm_pdev_init(void)
1.1.2.1   haad      139: {
                    140:        
                    141:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
                    142:        
                    143:        mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
                    144: 
                    145:        return 0;
                    146: }
                    147: 
                    148: /*
                    149:  * Allocat new pdev structure if is not already present and
                    150:  * set name.
                    151:  */
                    152: static struct dm_pdev*
                    153: dm_pdev_alloc(const char *name)
                    154: {
                    155:        struct dm_pdev *dmp;
                    156: 
1.1.2.2   haad      157:        if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
                    158:                    KM_NOSLEEP)) == NULL)
1.1.2.1   haad      159:                return NULL;
                    160: 
1.1.2.2   haad      161:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      162:        
                    163:        dmp->ref_cnt = 0;
                    164:        dmp->pdev_vnode = NULL;
                    165:        
                    166:        return dmp;
                    167: }
                    168: 
                    169: /*
1.1.2.5 ! haad      170:  * Destroy allocated dm_pdev. This routine is called with held pdev mutex.
1.1.2.1   haad      171:  */
1.1.2.5 ! haad      172: static int
        !           173: dm_pdev_rem(struct dm_pdev *dmp)
1.1.2.1   haad      174: {
                    175:        if (dmp == NULL)
                    176:                return ENOENT;
                    177: 
1.1.2.2   haad      178:        if (dmp->pdev_vnode != NULL) {
                    179:                VOP_CLOSE(dmp->pdev_vnode, FREAD|FWRITE, FSCRED);
                    180:                vput(dmp->pdev_vnode);
                    181:        }
1.1.2.5 ! haad      182: 
        !           183:        kmem_free(dmp, sizeof(*dmp));
1.1.2.1   haad      184: 
                    185:        dmp = NULL;
                    186:        
                    187:        return 0;
                    188: }
                    189: 
                    190: /*
1.1.2.5 ! haad      191:  * Destroy all existing pdev's in device-mapper.
        !           192:  */
        !           193: int
        !           194: dm_pdev_destroy(void)
        !           195: {
        !           196:        struct dm_pdev *dm_pdev;
        !           197:        
        !           198:        mutex_enter(&dm_pdev_mutex);
        !           199: 
        !           200:        while (!SLIST_EMPTY(&dm_pdev_list)) {           /* List Deletion. */
        !           201:                
        !           202:                dm_pdev = SLIST_FIRST(&dm_pdev_list);
        !           203:                
        !           204:                SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
        !           205: 
        !           206:                dm_pdev_rem(dm_pdev);
        !           207:        }
        !           208:        
        !           209:        mutex_exit(&dm_pdev_mutex);
        !           210: 
        !           211:        return 0;
        !           212: }
        !           213: 
        !           214: /*
1.1.2.1   haad      215:  * This funcion is called from dm_dev_remove_ioctl.
                    216:  * When I'm removing device from list, I have to decrement
                    217:  * reference counter. If reference counter is 0 I will remove
                    218:  * dmp from global list and from device list to. And I will CLOSE
                    219:  * dmp vnode too.
                    220:  */
                    221: 
                    222: /*
                    223:  * I'm guarding only global list with mutex, device should be
                    224:  * locked elsewhere.
                    225:  */
                    226: int
1.1.2.5 ! haad      227: dm_pdev_decr(struct dm_pdev *dmp)
1.1.2.1   haad      228: {
1.1.2.5 ! haad      229:        if (dmp == NULL)
1.1.2.1   haad      230:                return ENOENT;
                    231: 
1.1.2.3   haad      232:        mutex_enter(&dm_pdev_mutex);
1.1.2.5 ! haad      233:        
        !           234:        dmp->ref_cnt--;
        !           235:        
        !           236:        /*
        !           237:         * If this was last reference remove dmp from
        !           238:         * global list also.
        !           239:         */
        !           240:        if (dmp->ref_cnt == 0) {
        !           241:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
1.1.2.1   haad      242:                
1.1.2.5 ! haad      243:                dm_pdev_rem(dmp);
1.1.2.1   haad      244:        }
                    245: 
1.1.2.3   haad      246:        mutex_exit(&dm_pdev_mutex);
                    247: 
1.1.2.1   haad      248:        return 0;
                    249: }

CVSweb <webmaster@jp.NetBSD.org>