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

1.1.2.1   haad        1: /*
                      2:  * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to The NetBSD Foundation
                      6:  * by Adam Hamsik.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     18:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     19:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     20:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     21:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     22:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     23:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     24:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     25:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     26:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     27:  * POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29: 
                     30: #include <sys/types.h>
                     31: #include <sys/param.h>
                     32: 
                     33: #include <sys/disk.h>
                     34: #include <sys/errno.h>
                     35: #include <sys/ioctl.h>
                     36: #include <sys/ioccom.h>
                     37: #include <sys/kmem.h>
                     38: #include <sys/queue.h>
                     39: 
                     40: #include <sys/namei.h>
                     41: #include <sys/vnode.h>
                     42: 
                     43: #include <dev/dkvar.h>
                     44: 
                     45: #include "dm.h"
                     46: 
                     47: static struct dm_pdev *dm_pdev_alloc(const char *);
                     48: 
                     49: /*
                     50:  * XXX. I should use rwlock here.
                     51:  */
                     52: kmutex_t dm_pdev_mutex;
                     53: 
                     54: /*
                     55:  * Find used pdev with name == dm_pdev_name.
                     56:  */
                     57: struct dm_pdev*
                     58: dm_pdev_lookup_name(const char *dm_pdev_name)
                     59: {
                     60:        struct dm_pdev *dm_pdev;
                     61:        int dlen; int slen;
                     62: 
                     63:        slen = strlen(dm_pdev_name)+1;
                     64: 
                     65:        mutex_enter(&dm_pdev_mutex);
                     66: 
                     67:        SLIST_FOREACH(dm_pdev, &dm_pdev_list, next_pdev) {
                     68:                dlen = strlen(dm_pdev->name)+1;
                     69: 
                     70:                if (slen != dlen)
                     71:                        continue;
                     72:                
1.1.2.2 ! haad       73:                if (strncmp(dm_pdev_name, dm_pdev->name, slen) == 0){
1.1.2.1   haad       74:                        mutex_exit(&dm_pdev_mutex);
                     75:                        return dm_pdev;
                     76:                }
                     77:        }
                     78:        mutex_exit(&dm_pdev_mutex);
                     79: 
                     80:        return NULL;
                     81: }
                     82: 
                     83: /*
                     84:  * Create entry for device with name dev_name and open vnode for it.
                     85:  * If entry already exists in global SLIST I will only increment
                     86:  * reference counter.
                     87:  */
                     88: struct dm_pdev*
                     89: dm_pdev_insert(const char *dev_name)
                     90: {
                     91:        struct dm_pdev *dmp;
                     92:        int error;
                     93: 
                     94:        dmp = dm_pdev_lookup_name(dev_name);
                     95: 
                     96:        if (dmp != NULL) {
                     97:                dmp->ref_cnt++;
                     98:                return dmp;
                     99:        }
                    100: 
                    101:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                    102:                return NULL;
                    103: 
                    104:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                    105: 
                    106:        if (error) {
1.1.2.2 ! haad      107:                aprint_normal("dk_lookup on device: %s failed with error %d!\n",
        !           108:                    dev_name, error);
1.1.2.1   haad      109: 
                    110:                return NULL;
                    111:        }
                    112:        
                    113:        dmp->ref_cnt = 1;
                    114: 
                    115:        mutex_enter(&dm_pdev_mutex);
                    116: 
                    117:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    118:        
                    119:        mutex_exit(&dm_pdev_mutex);
                    120: 
                    121:        return dmp;
                    122: }
                    123: 
                    124: /*
                    125:  * Initialize pdev subsystem.
                    126:  */
                    127: int
                    128: dm_pdev_init()
                    129: {
                    130:        
                    131:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
                    132:        
                    133:        mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
                    134: 
                    135:        return 0;
                    136: }
                    137: 
                    138: /*
                    139:  * Allocat new pdev structure if is not already present and
                    140:  * set name.
                    141:  */
                    142: static struct dm_pdev*
                    143: dm_pdev_alloc(const char *name)
                    144: {
                    145:        struct dm_pdev *dmp;
                    146: 
1.1.2.2 ! haad      147:        if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,
        !           148:                    KM_NOSLEEP)) == NULL)
1.1.2.1   haad      149:                return NULL;
                    150: 
1.1.2.2 ! haad      151:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      152:        
                    153:        dmp->ref_cnt = 0;
                    154:        dmp->pdev_vnode = NULL;
                    155:        
                    156:        return dmp;
                    157: }
                    158: 
                    159: /*
                    160:  * Destroy allocated dm_pdev.
                    161:  */
                    162: int
                    163: dm_pdev_destroy(struct dm_pdev *dmp)
                    164: {
                    165: 
                    166:        if (dmp == NULL)
                    167:                return ENOENT;
                    168: 
1.1.2.2 ! haad      169:        if (dmp->pdev_vnode != NULL) {
        !           170:                VOP_CLOSE(dmp->pdev_vnode, FREAD|FWRITE, FSCRED);
        !           171:                vput(dmp->pdev_vnode);
        !           172:        }
1.1.2.1   haad      173:        kmem_free(dmp,sizeof(*dmp));
                    174: 
                    175:        dmp = NULL;
                    176:        
                    177:        return 0;
                    178: }
                    179: 
                    180: /*
                    181:  * This funcion is called from dm_dev_remove_ioctl.
                    182:  * When I'm removing device from list, I have to decrement
                    183:  * reference counter. If reference counter is 0 I will remove
                    184:  * dmp from global list and from device list to. And I will CLOSE
                    185:  * dmp vnode too.
                    186:  */
                    187: 
                    188: /*
                    189:  * I'm guarding only global list with mutex, device should be
                    190:  * locked elsewhere.
                    191:  */
                    192: int
                    193: dm_pdev_decr(struct dm_pdevs *head)
                    194: {
                    195:        struct dm_pdev *dmp;
                    196:        if (head == NULL)
                    197:                return ENOENT;
                    198: 
1.1.2.2 ! haad      199:        SLIST_FOREACH(dmp, head, next_pdev) {
1.1.2.1   haad      200: 
                    201:                dmp->ref_cnt--;
                    202: 
1.1.2.2 ! haad      203:                SLIST_REMOVE(head, dmp, dm_pdev, next_pdev);
1.1.2.1   haad      204:                
1.1.2.2 ! haad      205:                /*
        !           206:                 * If this was last reference remove dmp from
        !           207:                 * global list also.
        !           208:                 */
1.1.2.1   haad      209:                if (dmp->ref_cnt == 0) {
                    210:                        mutex_enter(&dm_pdev_mutex);
1.1.2.2 ! haad      211:                        SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
1.1.2.1   haad      212:                        dm_pdev_destroy(dmp);
                    213:                        mutex_exit(&dm_pdev_mutex);
                    214:                }
                    215:        }
                    216: 
                    217:        return 0;
                    218: }
                    219: 

CVSweb <webmaster@jp.NetBSD.org>