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

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

CVSweb <webmaster@jp.NetBSD.org>