Annotation of src/sys/dev/dm/dm_dev.c, revision 1.1.2.3

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: 
                     31: #include <sys/types.h>
                     32: #include <sys/param.h>
                     33: 
1.1.2.3 ! haad       34: #include <sys/disklabel.h>
1.1.2.1   haad       35: #include <sys/errno.h>
                     36: #include <sys/ioctl.h>
                     37: #include <sys/ioccom.h>
                     38: #include <sys/kmem.h>
                     39: #include <sys/lkm.h>
                     40: #include <sys/queue.h>
                     41: 
                     42: #include "netbsd-dm.h"
                     43: #include "dm.h"
                     44: 
                     45: /*TAILQ_HEAD(dm_dev_head, dm_dev);*/
                     46: 
                     47: static struct dm_dev_head dm_dev_list =
                     48: TAILQ_HEAD_INITIALIZER(dm_dev_list);
                     49: /*static struct db_cmd_tbl_en db_base_cmd_builtins =
                     50:   { .db_cmd = db_command_table };*/
                     51: 
                     52: kmutex_t dm_dev_mutex;
                     53: 
                     54: /*
                     55:  * Locking architecture, for now I use mutexes later we can convert them to
1.1.2.2   haad       56:  * rw_locks. I use IPL_NONE for specifing IPL type for mutex.
                     57:  * I will enter into mutex everytime I'm working with dm_dev_list.
1.1.2.1   haad       58:  */
                     59: 
                     60: /*
                     61:  * Lookup device with its minor number.
                     62:  */
                     63: struct dm_dev*
                     64: dm_dev_lookup_minor(int dm_dev_minor)
                     65: {
                     66:        struct dm_dev *dm_dev;
                     67: 
                     68:        mutex_enter(&dm_dev_mutex);
                     69:        
1.1.2.2   haad       70:        TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist){
1.1.2.1   haad       71:                if (dm_dev_minor == dm_dev->minor){
                     72:                        mutex_exit(&dm_dev_mutex);
                     73:                        return dm_dev;
                     74:                }
                     75:        }
                     76:                
                     77: 
                     78:        mutex_exit(&dm_dev_mutex);
                     79:        
                     80:        return NULL;
                     81: }
                     82: 
                     83: /*
                     84:  * Lookup device with it's device name.
                     85:  */
                     86: struct dm_dev*
                     87: dm_dev_lookup_name(const char *dm_dev_name)
                     88: {
                     89:        struct dm_dev *dm_dev;
                     90:        int dlen; int slen;
                     91: 
                     92:        if (dm_dev_name == NULL)
                     93:                return NULL;
                     94:        
                     95:        slen = strlen(dm_dev_name);
                     96: 
                     97:        mutex_enter(&dm_dev_mutex);
                     98:        
1.1.2.2   haad       99:        TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist){
1.1.2.1   haad      100: 
                    101:                dlen = strlen(dm_dev->name);
                    102: 
                    103:                if(slen != dlen)
                    104:                        continue;
                    105:                
1.1.2.2   haad      106:                if (strncmp(dm_dev_name, dm_dev->name, slen) == 0) {
1.1.2.1   haad      107:                        mutex_exit(&dm_dev_mutex);
                    108:                        return dm_dev;
                    109:                }       
                    110:        }
                    111: 
                    112:        mutex_exit(&dm_dev_mutex);
                    113:        
                    114:        return NULL;
                    115: }
                    116: 
                    117: /*
                    118:  * Lookup device with it's device uuid. Used mostly by LVM2tools.
                    119:  */
                    120: struct dm_dev*
                    121: dm_dev_lookup_uuid(const char *dm_dev_uuid)
                    122: {
                    123:        struct dm_dev *dm_dev;
                    124: 
                    125:        if (dm_dev_uuid == NULL)
                    126:                return NULL;
                    127: 
                    128:        mutex_enter(&dm_dev_mutex);
                    129:        
1.1.2.2   haad      130:        TAILQ_FOREACH(dm_dev, &dm_dev_list, next_devlist)
                    131:            if (strncmp(dm_dev_uuid, dm_dev->uuid, strlen(dm_dev->uuid)) == 0){
1.1.2.1   haad      132:                    mutex_exit(&dm_dev_mutex);
                    133:                    return dm_dev;
                    134:            }
                    135:        mutex_exit(&dm_dev_mutex);
                    136:        
                    137:        return NULL;
                    138: }
                    139: 
                    140: /*
                    141:  * Insert new device to the global list of devices.
                    142:  */
                    143: int
                    144: dm_dev_insert(struct dm_dev *dev)
                    145: {
                    146:        struct dm_dev *dmt;
                    147:        int r;
                    148: 
                    149:        dmt = NULL;
                    150:        r = 0;
                    151: 
                    152:        /* XXX dev->name can be NULL if uuid is used */
                    153:        dmt = dm_dev_lookup_name(dev->name);
                    154: 
                    155:        if (dmt == NULL) {
                    156:                mutex_enter(&dm_dev_mutex);
                    157:                TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
                    158:                mutex_exit(&dm_dev_mutex);
                    159:        } else
                    160:                r = EEXIST;
                    161:        
                    162:        return r;
                    163: }
                    164: 
                    165: 
                    166: /* 
                    167:  * Remove device selected with name. 
                    168:  */
                    169: int
                    170: dm_dev_rem(const char *dm_dev_name)
                    171: {
                    172:        struct dm_dev *dm_dev;
                    173:        int r;
                    174: 
                    175:        r = ENOENT;
                    176:        
                    177:        if(dm_dev_name == NULL)
                    178:                goto out;
                    179:                    
                    180:        if((dm_dev = dm_dev_lookup_name(dm_dev_name)) == NULL)
                    181:            dm_dev = dm_dev_lookup_uuid(dm_dev_name);
                    182: 
                    183:        if (dm_dev == NULL)
                    184:                goto out;
                    185:        
                    186:        mutex_enter(&dm_dev_mutex);
                    187:        
1.1.2.2   haad      188:        TAILQ_REMOVE(&dm_dev_list, dm_dev, next_devlist);
1.1.2.1   haad      189:        
                    190:        mutex_exit(&dm_dev_mutex);
                    191: 
                    192:        dm_dev_free(dm_dev);
                    193: 
                    194:        r = 0;
                    195:  out:
                    196:        return r;
                    197: }
                    198: 
                    199: /*
                    200:  * Allocate new device entry.
                    201:  */
                    202: struct dm_dev*
                    203: dm_dev_alloc()
                    204: {
                    205:        struct dm_dev *dmv;
                    206:        
1.1.2.2   haad      207:        if ((dmv = kmem_alloc(sizeof(struct dm_dev), KM_NOSLEEP)) == NULL)
1.1.2.1   haad      208:                return NULL;
                    209:        
                    210:        return dmv;
                    211: }
                    212: 
                    213: /*
                    214:  * Freed device entry.
                    215:  */
                    216: int
                    217: dm_dev_free(struct dm_dev *dmv)
                    218: {
                    219:        if (dmv != NULL){
1.1.2.3 ! haad      220:                if (dmv->dm_dklabel != NULL)
        !           221:                        (void)kmem_free(dmv->dm_dklabel,sizeof(struct disklabel));
        !           222:                (void)kmem_free(dmv,sizeof(struct dm_dev));
1.1.2.1   haad      223:        }
                    224:        
                    225:        return 0;
                    226: }
                    227: 
                    228: 
                    229: /*
                    230:  * Return prop_array of dm_targer_list dictionaries.
                    231:  */
                    232: prop_array_t
                    233: dm_dev_prop_list(void)
                    234: {
                    235:        struct dm_dev *dmd;
                    236:        
                    237:        int j;
                    238:        
                    239:        prop_array_t dev_array;
                    240:        prop_dictionary_t dev_dict;
                    241:        
                    242:        j =0;
                    243:        
                    244:        dev_array = prop_array_create();
                    245:        
                    246:        mutex_enter(&dm_dev_mutex);
                    247:        
1.1.2.2   haad      248:        TAILQ_FOREACH(dmd, &dm_dev_list,next_devlist) {
1.1.2.1   haad      249:                dev_dict  = prop_dictionary_create();
                    250:                
1.1.2.2   haad      251:                prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmd->name);
1.1.2.1   haad      252:                
1.1.2.2   haad      253:                prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmd->minor);
1.1.2.1   haad      254: 
1.1.2.2   haad      255:                prop_array_set(dev_array, j, dev_dict);
1.1.2.1   haad      256:                
                    257:                prop_object_release(dev_dict);
                    258:                
                    259:                j++;
                    260:        }
                    261: 
                    262:        mutex_exit(&dm_dev_mutex);
                    263:        
                    264:        return dev_array;
                    265: }
                    266: 
                    267: /*
                    268:  *Initialize global device mutex.
                    269:  */
                    270: int
                    271: dm_dev_init()
                    272: {
1.1.2.2   haad      273:        mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE);
1.1.2.1   haad      274: 
                    275:        return 0;
                    276: }

CVSweb <webmaster@jp.NetBSD.org>