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

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:                
        !            73:                if (strncmp(dm_pdev_name,dm_pdev->name,slen) == 0){
        !            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) {
        !           107:                printf("dk_lookup on device: %s failed with error %d!\n", dev_name, error);
        !           108: 
        !           109:                return NULL;
        !           110:        }
        !           111:        
        !           112:        dmp->ref_cnt = 1;
        !           113: 
        !           114:        mutex_enter(&dm_pdev_mutex);
        !           115: 
        !           116:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
        !           117:        
        !           118:        mutex_exit(&dm_pdev_mutex);
        !           119: 
        !           120:        printf("return\n");
        !           121: 
        !           122:        return dmp;
        !           123: }
        !           124: 
        !           125: /*
        !           126:  * Initialize pdev subsystem.
        !           127:  */
        !           128: int
        !           129: dm_pdev_init()
        !           130: {
        !           131:        
        !           132:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
        !           133:        
        !           134:        mutex_init(&dm_pdev_mutex,MUTEX_DEFAULT,IPL_NONE);
        !           135: 
        !           136:        return 0;
        !           137: }
        !           138: 
        !           139: /*
        !           140:  * Allocat new pdev structure if is not already present and
        !           141:  * set name.
        !           142:  */
        !           143: static struct dm_pdev*
        !           144: dm_pdev_alloc(const char *name)
        !           145: {
        !           146:        struct dm_pdev *dmp;
        !           147: 
        !           148:        if ((dmp = kmem_alloc(sizeof(struct dm_pdev)+strlen(name)+1,KM_NOSLEEP))
        !           149:            == NULL)
        !           150:                return NULL;
        !           151: 
        !           152:        strlcpy(dmp->name,name,MAX_DEV_NAME);
        !           153:        
        !           154:        dmp->ref_cnt = 0;
        !           155:        dmp->pdev_vnode = NULL;
        !           156:        
        !           157:        return dmp;
        !           158: }
        !           159: 
        !           160: /*
        !           161:  * Destroy allocated dm_pdev.
        !           162:  */
        !           163: int
        !           164: dm_pdev_destroy(struct dm_pdev *dmp)
        !           165: {
        !           166: 
        !           167:        if (dmp == NULL)
        !           168:                return ENOENT;
        !           169: 
        !           170:        if (dmp->pdev_vnode != NULL)
        !           171:                VOP_CLOSE(dmp->pdev_vnode,FREAD|FWRITE,FSCRED);
        !           172:        
        !           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: 
        !           199:        
        !           200:        SLIST_FOREACH(dmp,head,next_pdev) {
        !           201: 
        !           202:                dmp->ref_cnt--;
        !           203: 
        !           204:                SLIST_REMOVE(head,dmp,dm_pdev,next_pdev);
        !           205:                
        !           206:                /* If this was last reference remove dmp from global list also. */
        !           207:                if (dmp->ref_cnt == 0) {
        !           208:                        mutex_enter(&dm_pdev_mutex);
        !           209:                        SLIST_REMOVE(&dm_pdev_list,dmp, dm_pdev,next_pdev); 
        !           210:                        dm_pdev_destroy(dmp);
        !           211:                        mutex_exit(&dm_pdev_mutex);
        !           212:                }
        !           213:        }
        !           214: 
        !           215:        return 0;
        !           216: }
        !           217: 

CVSweb <webmaster@jp.NetBSD.org>