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

1.1.2.11! haad        1: /*        $NetBSD: dm_pdev.c,v 1.1.2.10 2008/09/11 13:40:47 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>
1.1.2.8   haad       36: #include <sys/fcntl.h>
1.1.2.1   haad       37: #include <sys/kmem.h>
                     38: #include <sys/namei.h>
                     39: #include <sys/vnode.h>
                     40: 
                     41: #include <dev/dkvar.h>
                     42: 
                     43: #include "dm.h"
                     44: 
1.1.2.11! haad       45: SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
        !            46: 
        !            47: kmutex_t dm_pdev_mutex;
        !            48: 
1.1.2.1   haad       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.11! haad       51: static struct dm_pdev* dm_pdev_lookup_name(const char *);
1.1.2.1   haad       52: 
                     53: /*
                     54:  * Find used pdev with name == dm_pdev_name.
                     55:  */
                     56: struct dm_pdev*
                     57: dm_pdev_lookup_name(const char *dm_pdev_name)
                     58: {
                     59:        struct dm_pdev *dm_pdev;
                     60:        int dlen; int slen;
                     61: 
1.1.2.11! haad       62:        KASSERT(dm_pdev_name != NULL);
        !            63: 
1.1.2.3   haad       64:        slen = strlen(dm_pdev_name);
1.1.2.1   haad       65: 
                     66:        SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
1.1.2.3   haad       67:                dlen = strlen(dm_pdev->name);
1.1.2.1   haad       68: 
                     69:                if (slen != dlen)
                     70:                        continue;
                     71:                
1.1.2.8   haad       72:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0)
1.1.2.1   haad       73:                        return dm_pdev;
                     74:        }
                     75: 
                     76:        return NULL;
                     77: }
                     78: 
                     79: /*
                     80:  * Create entry for device with name dev_name and open vnode for it.
                     81:  * If entry already exists in global SLIST I will only increment
                     82:  * reference counter.
                     83:  */
                     84: struct dm_pdev*
                     85: dm_pdev_insert(const char *dev_name)
                     86: {
                     87:        struct dm_pdev *dmp;
                     88:        int error;
1.1.2.9   haad       89:        
1.1.2.11! haad       90:        KASSERT(dev_name != NULL);
1.1.2.3   haad       91:        
1.1.2.11! haad       92:        mutex_enter(&dm_pdev_mutex);
        !            93:        dmp = dm_pdev_lookup_name(dev_name);
        !            94: 
1.1.2.1   haad       95:        if (dmp != NULL) {
                     96:                dmp->ref_cnt++;
1.1.2.9   haad       97:                aprint_verbose("dmp_pdev_insert pdev %s already in tree\n",dev_name);
1.1.2.11! haad       98:                mutex_exit(&dm_pdev_mutex);
1.1.2.1   haad       99:                return dmp;
                    100:        }
1.1.2.11! haad      101:        mutex_exit(&dm_pdev_mutex);
        !           102:        
1.1.2.1   haad      103:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                    104:                return NULL;
                    105: 
                    106:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                    107:        if (error) {
1.1.2.9   haad      108:                aprint_verbose("dk_lookup on device: %s failed with error %d!\n",
1.1.2.2   haad      109:                    dev_name, error);
1.1.2.6   haad      110:                kmem_free(dmp, sizeof(struct dm_pdev));
1.1.2.1   haad      111:                return NULL;
                    112:        }
                    113: 
1.1.2.11! haad      114:        dmp->ref_cnt = 1;
        !           115:        
        !           116:        mutex_enter(&dm_pdev_mutex);
1.1.2.1   haad      117:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
1.1.2.11! haad      118:        mutex_exit(&dm_pdev_mutex);
1.1.2.1   haad      119: 
                    120:        return dmp;
                    121: }
                    122: 
                    123: /*
                    124:  * Initialize pdev subsystem.
                    125:  */
                    126: int
1.1.2.5   haad      127: dm_pdev_init(void)
1.1.2.1   haad      128: {
                    129:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
1.1.2.11! haad      130:        mutex_init(&dm_pdev_mutex, MUTEX_DEFAULT, IPL_NONE);
        !           131:        
1.1.2.1   haad      132:        return 0;
                    133: }
                    134: 
                    135: /*
                    136:  * Allocat new pdev structure if is not already present and
                    137:  * set name.
                    138:  */
                    139: static struct dm_pdev*
                    140: dm_pdev_alloc(const char *name)
                    141: {
                    142:        struct dm_pdev *dmp;
                    143: 
1.1.2.9   haad      144:        if ((dmp = kmem_zalloc(sizeof(struct dm_pdev), KM_NOSLEEP)) == NULL)
1.1.2.1   haad      145:                return NULL;
                    146: 
1.1.2.2   haad      147:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      148:        
                    149:        dmp->ref_cnt = 0;
                    150:        dmp->pdev_vnode = NULL;
                    151:        
                    152:        return dmp;
                    153: }
                    154: 
                    155: /*
1.1.2.8   haad      156:  * Destroy allocated dm_pdev.
1.1.2.1   haad      157:  */
1.1.2.5   haad      158: static int
                    159: dm_pdev_rem(struct dm_pdev *dmp)
1.1.2.1   haad      160: {
1.1.2.6   haad      161:        int err;
1.1.2.9   haad      162: 
1.1.2.11! haad      163:        KASSERT(dmp != NULL);
1.1.2.9   haad      164:                
1.1.2.2   haad      165:        if (dmp->pdev_vnode != NULL) {
1.1.2.6   haad      166:                err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
                    167:                if (err != 0)
                    168:                        return err;
1.1.2.2   haad      169:        }
1.1.2.6   haad      170:        
1.1.2.5   haad      171:        kmem_free(dmp, sizeof(*dmp));
1.1.2.1   haad      172:        dmp = NULL;
                    173:        
                    174:        return 0;
                    175: }
                    176: 
                    177: /*
1.1.2.5   haad      178:  * Destroy all existing pdev's in device-mapper.
                    179:  */
                    180: int
                    181: dm_pdev_destroy(void)
                    182: {
                    183:        struct dm_pdev *dm_pdev;
                    184: 
1.1.2.11! haad      185:        mutex_enter(&dm_pdev_mutex);
1.1.2.5   haad      186:        while (!SLIST_EMPTY(&dm_pdev_list)) {           /* List Deletion. */
                    187:                
                    188:                dm_pdev = SLIST_FIRST(&dm_pdev_list);
                    189:                
                    190:                SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
                    191: 
                    192:                dm_pdev_rem(dm_pdev);
                    193:        }
1.1.2.11! haad      194:        mutex_exit(&dm_pdev_mutex);
1.1.2.5   haad      195: 
1.1.2.11! haad      196:        mutex_destroy(&dm_pdev_mutex);
1.1.2.5   haad      197:        return 0;
                    198: }
                    199: 
                    200: /*
1.1.2.1   haad      201:  * This funcion is called from dm_dev_remove_ioctl.
                    202:  * When I'm removing device from list, I have to decrement
                    203:  * reference counter. If reference counter is 0 I will remove
                    204:  * dmp from global list and from device list to. And I will CLOSE
                    205:  * dmp vnode too.
                    206:  */
                    207: 
                    208: /*
1.1.2.8   haad      209:  * Decrement pdev reference counter if 0 remove it.
1.1.2.1   haad      210:  */
                    211: int
1.1.2.5   haad      212: dm_pdev_decr(struct dm_pdev *dmp)
1.1.2.1   haad      213: {
1.1.2.11! haad      214:        KASSERT(dmp != NULL);
1.1.2.5   haad      215:        /*
                    216:         * If this was last reference remove dmp from
                    217:         * global list also.
                    218:         */
1.1.2.11! haad      219:        mutex_enter(&dm_pdev_mutex);
        !           220:        
        !           221:        if (--dmp->ref_cnt == 0) {
1.1.2.5   haad      222:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
1.1.2.11! haad      223:                mutex_exit(&dm_pdev_mutex);
1.1.2.5   haad      224:                dm_pdev_rem(dmp);
1.1.2.11! haad      225:                return 0;
1.1.2.1   haad      226:        }
1.1.2.11! haad      227:        
        !           228:        mutex_exit(&dm_pdev_mutex);
1.1.2.1   haad      229:        return 0;
                    230: }
1.1.2.9   haad      231: 
                    232: /*static int
                    233:   dm_pdev_dump_list()
                    234:   {
                    235:   struct dm_pdev *dmp;
                    236:        
                    237:   aprint_verbose("Dumping dm_pdev_list \n");
                    238:        
                    239:   SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
                    240:   aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
                    241:   dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
                    242:   }
                    243:        
                    244:   return 0;    
                    245:        
                    246:   }*/

CVSweb <webmaster@jp.NetBSD.org>