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

1.1.2.10! haad        1: /*        $NetBSD: dm_pdev.c,v 1.1.2.9 2008/09/08 11:34:01 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: /*
                     74:  * Create entry for device with name dev_name and open vnode for it.
                     75:  * If entry already exists in global SLIST I will only increment
                     76:  * reference counter.
                     77:  */
                     78: struct dm_pdev*
                     79: dm_pdev_insert(const char *dev_name)
                     80: {
                     81:        struct dm_pdev *dmp;
                     82:        int error;
1.1.2.9   haad       83:        
1.1.2.1   haad       84:        dmp = dm_pdev_lookup_name(dev_name);
1.1.2.3   haad       85:        
1.1.2.1   haad       86:        if (dmp != NULL) {
                     87:                dmp->ref_cnt++;
1.1.2.9   haad       88:                aprint_verbose("dmp_pdev_insert pdev %s already in tree\n",dev_name);
1.1.2.1   haad       89:                return dmp;
                     90:        }
                     91: 
                     92:        if ((dmp = dm_pdev_alloc(dev_name)) == NULL)
                     93:                return NULL;
                     94: 
                     95:        error = dk_lookup(dev_name, curlwp, &dmp->pdev_vnode, UIO_SYSSPACE);
                     96: 
                     97:        if (error) {
1.1.2.9   haad       98:                aprint_verbose("dk_lookup on device: %s failed with error %d!\n",
1.1.2.2   haad       99:                    dev_name, error);
1.1.2.1   haad      100: 
1.1.2.6   haad      101:                kmem_free(dmp, sizeof(struct dm_pdev));
                    102:                
1.1.2.1   haad      103:                return NULL;
                    104:        }
                    105:        
                    106:        dmp->ref_cnt = 1;
                    107: 
                    108:        SLIST_INSERT_HEAD(&dm_pdev_list, dmp, next_pdev);
                    109: 
                    110:        return dmp;
                    111: }
                    112: 
                    113: /*
                    114:  * Initialize pdev subsystem.
                    115:  */
                    116: int
1.1.2.5   haad      117: dm_pdev_init(void)
1.1.2.1   haad      118: {
                    119:        
                    120:        SLIST_INIT(&dm_pdev_list); /* initialize global pdev list */
                    121: 
                    122:        return 0;
                    123: }
                    124: 
                    125: /*
                    126:  * Allocat new pdev structure if is not already present and
                    127:  * set name.
                    128:  */
                    129: static struct dm_pdev*
                    130: dm_pdev_alloc(const char *name)
                    131: {
                    132:        struct dm_pdev *dmp;
                    133: 
1.1.2.9   haad      134:        if ((dmp = kmem_zalloc(sizeof(struct dm_pdev), KM_NOSLEEP)) == NULL)
1.1.2.1   haad      135:                return NULL;
                    136: 
1.1.2.2   haad      137:        strlcpy(dmp->name, name, MAX_DEV_NAME);
1.1.2.1   haad      138:        
                    139:        dmp->ref_cnt = 0;
                    140:        dmp->pdev_vnode = NULL;
                    141:        
                    142:        return dmp;
                    143: }
                    144: 
                    145: /*
1.1.2.8   haad      146:  * Destroy allocated dm_pdev.
1.1.2.1   haad      147:  */
1.1.2.5   haad      148: static int
                    149: dm_pdev_rem(struct dm_pdev *dmp)
1.1.2.1   haad      150: {
1.1.2.6   haad      151:        int err;
1.1.2.9   haad      152: 
1.1.2.1   haad      153:        if (dmp == NULL)
                    154:                return ENOENT;
1.1.2.9   haad      155:                
1.1.2.2   haad      156:        if (dmp->pdev_vnode != NULL) {
1.1.2.6   haad      157:                err = vn_close(dmp->pdev_vnode, FREAD | FWRITE, FSCRED);
                    158:                if (err != 0)
                    159:                        return err;
1.1.2.2   haad      160:        }
1.1.2.6   haad      161:        
1.1.2.5   haad      162:        kmem_free(dmp, sizeof(*dmp));
1.1.2.1   haad      163:        dmp = NULL;
                    164:        
                    165:        return 0;
                    166: }
                    167: 
                    168: /*
1.1.2.5   haad      169:  * Destroy all existing pdev's in device-mapper.
                    170:  */
                    171: int
                    172: dm_pdev_destroy(void)
                    173: {
                    174:        struct dm_pdev *dm_pdev;
                    175: 
                    176:        while (!SLIST_EMPTY(&dm_pdev_list)) {           /* List Deletion. */
                    177:                
                    178:                dm_pdev = SLIST_FIRST(&dm_pdev_list);
                    179:                
                    180:                SLIST_REMOVE_HEAD(&dm_pdev_list, next_pdev);
                    181: 
                    182:                dm_pdev_rem(dm_pdev);
                    183:        }
                    184: 
                    185:        return 0;
                    186: }
                    187: 
                    188: /*
1.1.2.1   haad      189:  * This funcion is called from dm_dev_remove_ioctl.
                    190:  * When I'm removing device from list, I have to decrement
                    191:  * reference counter. If reference counter is 0 I will remove
                    192:  * dmp from global list and from device list to. And I will CLOSE
                    193:  * dmp vnode too.
                    194:  */
                    195: 
                    196: /*
1.1.2.8   haad      197:  * Decrement pdev reference counter if 0 remove it.
1.1.2.1   haad      198:  */
                    199: int
1.1.2.5   haad      200: dm_pdev_decr(struct dm_pdev *dmp)
1.1.2.1   haad      201: {
1.1.2.5   haad      202:        if (dmp == NULL)
1.1.2.1   haad      203:                return ENOENT;
1.1.2.5   haad      204:        
                    205:        dmp->ref_cnt--;
1.1.2.6   haad      206: 
1.1.2.5   haad      207:        /*
                    208:         * If this was last reference remove dmp from
                    209:         * global list also.
                    210:         */
                    211:        if (dmp->ref_cnt == 0) {
                    212:                SLIST_REMOVE(&dm_pdev_list, dmp, dm_pdev, next_pdev); 
                    213:                dm_pdev_rem(dmp);
1.1.2.1   haad      214:        }
                    215: 
                    216:        return 0;
                    217: }
1.1.2.9   haad      218: 
                    219: /*static int
                    220:   dm_pdev_dump_list()
                    221:   {
                    222:   struct dm_pdev *dmp;
                    223:        
                    224:   aprint_verbose("Dumping dm_pdev_list \n");
                    225:        
                    226:   SLIST_FOREACH(dmp, &dm_pdev_list, next_pdev) {
                    227:   aprint_verbose("dm_pdev_name %s ref_cnt %d list_rf_cnt %d\n",
                    228:   dmp->name, dmp->ref_cnt, dmp->list_ref_cnt);
                    229:   }
                    230:        
                    231:   return 0;    
                    232:        
                    233:   }*/

CVSweb <webmaster@jp.NetBSD.org>