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

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

CVSweb <webmaster@jp.NetBSD.org>