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

1.1.2.4 ! haad        1: /*        $NetBSD$      */
        !             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 *);
                     50: 
                     51: /*
                     52:  * XXX. I should use rwlock here.
                     53:  */
                     54: kmutex_t dm_pdev_mutex;
                     55: 
                     56: /*
                     57:  * Find used pdev with name == dm_pdev_name.
                     58:  */
                     59: struct dm_pdev*
                     60: dm_pdev_lookup_name(const char *dm_pdev_name)
                     61: {
                     62:        struct dm_pdev *dm_pdev;
                     63:        int dlen; int slen;
                     64: 
1.1.2.3   haad       65:        slen = strlen(dm_pdev_name);
1.1.2.1   haad       66: 
                     67:        mutex_enter(&dm_pdev_mutex);
                     68: 
                     69:        SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
1.1.2.3   haad       70:                dlen = strlen(dm_pdev->name);
1.1.2.1   haad       71: 
                     72:                if (slen != dlen)
                     73:                        continue;
                     74:                
1.1.2.2   haad       75:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0){
1.1.2.1   haad       76:                        mutex_exit(&dm_pdev_mutex);
                     77:                        return dm_pdev;
                     78:                }
                     79:        }
                     80:        mutex_exit(&dm_pdev_mutex);
                     81: 
                     82:        return NULL;
                     83: }
                     84: 
                     85: /*
                     86:  * Create entry for device with name dev_name and open vnode for it.
                     87:  * If entry already exists in global SLIST I will only increment
                     88:  * reference counter.
                     89:  */
                     90: struct dm_pdev*
                     91: dm_pdev_insert(const char *dev_name)
                     92: {
                     93:        struct dm_pdev *dmp;
                     94:        int error;
                     95: 
                     96:        dmp = dm_pdev_lookup_name(dev_name);
                     97: 
1.1.2.3   haad       98:        
1.1.2.1   haad       99:        if (dmp != NULL) {
1.1.2.3   haad      100: 
                    101:                mutex_enter(&dm_pdev_mutex);
                    102:                
1.1.2.1   haad      103:                dmp->ref_cnt++;
1.1.2.3   haad      104: 
                    105:                mutex_exit(&dm_pdev_mutex);
                    106:                
1.1.2.1   haad      107:                return dmp;
                    108:        }
                    109: 
                    110:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                    111:                return NULL;
                    112: 
                    113:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                    114: 
                    115:        if (error) {
1.1.2.2   haad      116:                aprint_normal("dk_lookup on device: %s failed with error %d!\n",
                    117:                    dev_name, error);
1.1.2.1   haad      118: 
                    119:                return NULL;
                    120:        }
                    121:        
                    122:        dmp->ref_cnt = 1;
1.1.2.3   haad      123:        
1.1.2.1   haad      124:        mutex_enter(&dm_pdev_mutex);
                    125: 
                    126:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    127:        
                    128:        mutex_exit(&dm_pdev_mutex);
                    129: 
                    130:        return dmp;
                    131: }
                    132: 
                    133: /*
                    134:  * Initialize pdev subsystem.
                    135:  */
                    136: int
                    137: dm_pdev_init()
                    138: {
                    139:        
                    140:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
                    141:        
                    142:        mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
                    143: 
                    144:        return 0;
                    145: }
                    146: 
                    147: /*
                    148:  * Allocat new pdev structure if is not already present and
                    149:  * set name.
                    150:  */
                    151: static struct dm_pdev*
                    152: dm_pdev_alloc(const char *name)
                    153: {
                    154:        struct dm_pdev *dmp;
                    155: 
1.1.2.2   haad      156:        if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
                    157:                    KM_NOSLEEP)) == NULL)
1.1.2.1   haad      158:                return NULL;
                    159: 
1.1.2.2   haad      160:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      161:        
                    162:        dmp->ref_cnt = 0;
                    163:        dmp->pdev_vnode = NULL;
                    164:        
                    165:        return dmp;
                    166: }
                    167: 
                    168: /*
                    169:  * Destroy allocated dm_pdev.
                    170:  */
                    171: int
                    172: dm_pdev_destroy(struct dm_pdev *dmp)
                    173: {
                    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.1   haad      182:        kmem_free(dmp,sizeof(*dmp));
                    183: 
                    184:        dmp = NULL;
                    185:        
                    186:        return 0;
                    187: }
                    188: 
                    189: /*
                    190:  * This funcion is called from dm_dev_remove_ioctl.
                    191:  * When I'm removing device from list, I have to decrement
                    192:  * reference counter. If reference counter is 0 I will remove
                    193:  * dmp from global list and from device list to. And I will CLOSE
                    194:  * dmp vnode too.
                    195:  */
                    196: 
                    197: /*
                    198:  * I'm guarding only global list with mutex, device should be
                    199:  * locked elsewhere.
                    200:  */
                    201: int
                    202: dm_pdev_decr(struct dm_pdevs *head)
                    203: {
                    204:        struct dm_pdev *dmp;
                    205:        if (head == NULL)
                    206:                return ENOENT;
                    207: 
1.1.2.3   haad      208:        mutex_enter(&dm_pdev_mutex);
                    209: 
1.1.2.2   haad      210:        SLIST_FOREACH(dmp, head, next_pdev) {
1.1.2.1   haad      211: 
                    212:                dmp->ref_cnt--;
                    213: 
1.1.2.2   haad      214:                SLIST_REMOVE(head, dmp, dm_pdev, next_pdev);
1.1.2.1   haad      215:                
1.1.2.2   haad      216:                /*
                    217:                 * If this was last reference remove dmp from
                    218:                 * global list also.
                    219:                 */
1.1.2.1   haad      220:                if (dmp->ref_cnt == 0) {
1.1.2.2   haad      221:                        SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
1.1.2.1   haad      222:                        dm_pdev_destroy(dmp);
                    223:                }
                    224:        }
                    225: 
1.1.2.3   haad      226:        mutex_exit(&dm_pdev_mutex);
                    227: 
1.1.2.1   haad      228:        return 0;
                    229: }
                    230: 

CVSweb <webmaster@jp.NetBSD.org>