Annotation of src/sys/dev/dm/dm_ioctl.c, revision 1.12

1.12    ! haad        1: /*        $NetBSD: dm_ioctl.c,v 1.11 2009/04/13 18:51:54 haad Exp $      */
1.2       haad        2: 
                      3: /*
                      4:  * Copyright (c) 2008 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: /*
                     33:  * Locking is used to synchronise between ioctl calls and between dm_table's
                     34:  * users.
                     35:  *
                     36:  * ioctl locking:
                     37:  * Simple reference counting, to count users of device will be used routines
                     38:  * dm_dev_busy/dm_dev_unbusy are used for that.
                     39:  * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
                     40:  * holder of reference_counter last).
                     41:  *
                     42:  * ioctl routines which change/remove dm_dev parameters must wait on
                     43:  * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
                     44:  * up them.
                     45:  *
                     46:  * table_head locking:
                     47:  * To access table entries dm_table_* routines must be used. 
                     48:  *
                     49:  * dm_table_get_entry will increment table users reference
                     50:  * counter. It will return active or inactive table depedns 
                     51:  * on uint8_t argument.
                     52:  *
                     53:  * dm_table_release must be called for every table_entry from
                     54:  * dm_table_get_entry. Between these to calls tables can'tbe switched
                     55:  * or destroyed. 
                     56:  *
                     57:  * dm_table_head_init initialize talbe_entries SLISTS and io_cv.
                     58:  *
                     59:  * dm_table_head_destroy destroy cv.
                     60:  *
                     61:  * There are two types of users for dm_table_head first type will
                     62:  * only read list and try to do anything with it e.g. dmstrategy,
                     63:  * dm_table_size etc. There is another user for table_head which wants
                     64:  * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
                     65:  * dm_table_clear_ioctl.
                     66:  * 
                     67:  * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
                     68:  *       with hold table reference counter. Table reference counter is hold
                     69:  *       after calling dm_table_get_entry routine. After calling this
                     70:  *       function user must call dm_table_release before any writer table
                     71:  *       operation.
                     72:  *
                     73:  * Example: dm_table_get_entry
                     74:  *          dm_table_destroy/dm_table_switch_tables
                     75:  * This exaple will lead to deadlock situation because after dm_table_get_entry
                     76:  * table reference counter is != 0 and dm_table_destroy have to wait on cv until
                     77:  * reference counter is 0.
                     78:  *
                     79:  */
                     80: 
                     81: #include <sys/types.h>
                     82: #include <sys/param.h>
                     83: 
1.6       haad       84: #include <sys/disk.h>
1.2       haad       85: #include <sys/disklabel.h>
                     86: #include <sys/kmem.h>
                     87: #include <sys/malloc.h>
                     88: #include <sys/vnode.h>
                     89: 
                     90: #include <machine/int_fmtio.h>
                     91: 
                     92: #include "netbsd-dm.h"
                     93: #include "dm.h"
                     94: 
1.5       haad       95: static uint64_t      sc_minor_num; 
1.10      haad       96: extern const struct dkdriver dmdkdriver;
1.6       haad       97: uint64_t dev_counter;
1.2       haad       98: 
                     99: #define DM_REMOVE_FLAG(flag, name) do {                                        \
                    100:                prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
                    101:                flag &= ~name;                                          \
                    102:                prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
                    103:        } while (/*CONSTCOND*/0)
                    104: 
                    105: #define DM_ADD_FLAG(flag, name) do {                                   \
                    106:                prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
                    107:                flag |= name;                                           \
                    108:                prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
                    109:        } while (/*CONSTCOND*/0)
                    110: 
                    111: static int dm_dbg_print_flags(int);
                    112: 
                    113: /*
                    114:  * Print flags sent to the kernel from libevmapper.
                    115:  */
                    116: static int
                    117: dm_dbg_print_flags(int flags)
                    118: {
                    119:        aprint_debug("dbg_print --- %d\n",flags);
                    120:        
                    121:        if (flags & DM_READONLY_FLAG)
                    122:                aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
                    123: 
                    124:        if (flags & DM_SUSPEND_FLAG)
                    125:                aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
                    126: 
                    127:        if (flags & DM_PERSISTENT_DEV_FLAG)
                    128:                aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
                    129: 
                    130:        if (flags & DM_STATUS_TABLE_FLAG)
                    131:                aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
                    132: 
                    133:        if (flags & DM_ACTIVE_PRESENT_FLAG)
                    134:                aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
                    135: 
                    136:        if (flags & DM_INACTIVE_PRESENT_FLAG)
                    137:                aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
                    138: 
                    139:        if (flags & DM_BUFFER_FULL_FLAG)
                    140:                aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
                    141: 
                    142:        if (flags & DM_SKIP_BDGET_FLAG)
                    143:                aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
                    144: 
                    145:        if (flags & DM_SKIP_LOCKFS_FLAG)
                    146:                aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
                    147: 
                    148:        if (flags & DM_NOFLUSH_FLAG)
                    149:                aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
                    150:        
                    151:        return 0;
                    152: }
                    153: 
                    154: /*
                    155:  * Get version ioctl call I do it as default therefore this
                    156:  * function is unused now.
                    157:  */
                    158: int
                    159: dm_get_version_ioctl(prop_dictionary_t dm_dict)
                    160: {
                    161:        return 0;
                    162: }
                    163: 
                    164: /*
                    165:  * Get list of all available targets from global
                    166:  * target list and sent them back to libdevmapper.
                    167:  */
                    168: int
                    169: dm_list_versions_ioctl(prop_dictionary_t dm_dict)
                    170: {
                    171:        prop_array_t target_list;
                    172:        uint32_t flags;
                    173: 
                    174:        flags = 0;
                    175: 
                    176:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    177:        
                    178:        dm_dbg_print_flags(flags);
                    179: 
                    180:        target_list = dm_target_prop_list();
                    181: 
                    182:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
                    183: 
                    184:        prop_object_release(target_list);
                    185:        
                    186:        return 0;
                    187: }
                    188: 
                    189: /*
                    190:  * Create in-kernel entry for device. Device attributes such as name, uuid are
                    191:  * taken from proplib dictionary.
                    192:  *
                    193:  */
                    194: int
                    195: dm_dev_create_ioctl(prop_dictionary_t dm_dict)
                    196: {
                    197:        dm_dev_t *dmv;
                    198:        const char *name, *uuid;
                    199:        int r, flags;
                    200:        
                    201:        r = 0;
                    202:        flags = 0;
                    203:        name = NULL;    
                    204:        uuid = NULL;
                    205: 
                    206:        /* Get needed values from dictionary. */
                    207:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    208:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    209:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    210:        
                    211:        dm_dbg_print_flags(flags);
                    212: 
                    213:        /* Lookup name and uuid if device already exist quit. */
                    214:        if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
                    215:                DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
                    216:                dm_dev_unbusy(dmv);
                    217:                return EEXIST;
                    218:        }
                    219: 
                    220:        if ((dmv = dm_dev_alloc()) == NULL)
                    221:                return ENOMEM;
1.11      haad      222:                
1.2       haad      223:        if (uuid)
                    224:                strncpy(dmv->uuid, uuid, DM_UUID_LEN);
                    225:        else 
                    226:                dmv->uuid[0] = '\0';
                    227:                
                    228:        if (name)
                    229:                strlcpy(dmv->name, name, DM_NAME_LEN);
                    230: 
1.5       haad      231:        dmv->minor = atomic_inc_64_nv(&sc_minor_num);
1.2       haad      232: 
                    233:        dmv->flags = 0; /* device flags are set when needed */
                    234:        dmv->ref_cnt = 0;
                    235:        dmv->event_nr = 0;
                    236:        dmv->dev_type = 0;
                    237:        
                    238:        mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
                    239:        cv_init(&dmv->dev_cv, "dm_dev");
                    240: 
                    241:        dm_table_head_init(&dmv->table_head);
                    242:        
                    243:        if (flags & DM_READONLY_FLAG)
                    244:                dmv->flags |= DM_READONLY_FLAG;
                    245: 
                    246:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
1.6       haad      247: 
1.10      haad      248:        disk_init(dmv->diskp, dmv->name, &dmdkdriver);
                    249:        disk_attach(dmv->diskp);
                    250:        
1.11      haad      251:        if ((r = dm_dev_insert(dmv)) != 0)              
1.2       haad      252:                dm_dev_free(dmv);
                    253:        
                    254:        DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
                    255:        DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
1.6       haad      256: 
                    257:        /* Increment device counter After creating device */
                    258:        atomic_inc_64(&dev_counter);
1.2       haad      259:        
                    260:        return r;
                    261: }
                    262: 
                    263: /*
                    264:  * Get list of created device-mapper devices fromglobal list and
                    265:  * send it to kernel.
                    266:  *
                    267:  * Output dictionary:
                    268:  *
                    269:  * <key>cmd_data</key>
                    270:  *  <array>
                    271:  *   <dict>
                    272:  *    <key>name<key>
                    273:  *    <string>...</string>
                    274:  *
                    275:  *    <key>dev</key>
                    276:  *    <integer>...</integer>
                    277:  *   </dict>
                    278:  *  </array>
                    279:  * 
                    280:  */
                    281: int
                    282: dm_dev_list_ioctl(prop_dictionary_t dm_dict)
                    283: {
                    284:        prop_array_t dev_list;
                    285: 
                    286:        uint32_t flags;
                    287:        
                    288:        flags = 0;
                    289:        
                    290:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    291:        
                    292:        dm_dbg_print_flags(flags);
                    293:        
                    294:        dev_list = dm_dev_prop_list();
                    295: 
                    296:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
                    297:        prop_object_release(dev_list);
                    298:        
                    299:        return 0;
                    300: }
                    301: 
                    302: /*
                    303:  * Rename selected devices old name is in struct dm_ioctl.
                    304:  * newname is taken from dictionary
                    305:  *
                    306:  * <key>cmd_data</key>
                    307:  *  <array>
                    308:  *   <string>...</string>
                    309:  *  </array>
                    310:  */
                    311: int
                    312: dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
                    313: {
                    314:        prop_array_t cmd_array;
                    315:        dm_dev_t *dmv;
                    316:        
                    317:        const char *name, *uuid, *n_name;
                    318:        uint32_t flags, minor;
                    319: 
                    320:        name = NULL;
                    321:        uuid = NULL;
                    322:        minor = 0;
                    323:        
                    324:        /* Get needed values from dictionary. */
                    325:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    326:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    327:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    328:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    329: 
                    330:        dm_dbg_print_flags(flags);
                    331:        
                    332:        cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
                    333: 
                    334:        prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
                    335:        
                    336:        if (strlen(n_name) + 1 > DM_NAME_LEN)
                    337:                return EINVAL;
                    338:        
                    339:        if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
                    340:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    341:                return ENOENT;
                    342:        }
                    343: 
                    344:        /* change device name */
                    345:        /* XXX How to deal with this change, name only used in
                    346:         * dm_dev_routines, should I add dm_dev_change_name which will run under the
                    347:         * dm_dev_list mutex ?
                    348:         */
                    349:        strlcpy(dmv->name, n_name, DM_NAME_LEN);
                    350:        
                    351:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
                    352:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    353:        prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
                    354: 
                    355:        dm_dev_insert(dmv);
                    356:        
                    357:        return 0;
                    358: }
                    359: 
                    360: /*
                    361:  * Remove device from global list I have to remove active
                    362:  * and inactive tables first.
                    363:  */
                    364: int
                    365: dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
                    366: {
                    367:        dm_dev_t *dmv;
                    368:        const char *name, *uuid;
                    369:        uint32_t flags, minor;
                    370:        
                    371:        flags = 0;
                    372:        name = NULL;
                    373:        uuid = NULL;
                    374:        
                    375:        /* Get needed values from dictionary. */
                    376:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    377:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    378:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    379:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    380:        
                    381:        dm_dbg_print_flags(flags);
                    382:        
                    383:        /* Remove device from global device list */
                    384:        if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL){
                    385:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    386:                return ENOENT;
                    387:        }
                    388: 
                    389:        /* Destroy active table first.  */
                    390:        dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
                    391:        
                    392:        /* Destroy inactive table if exits, too. */
                    393:        dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
                    394:        
                    395:        dm_table_head_destroy(&dmv->table_head);
                    396: 
1.10      haad      397:        /* Destroy disk device structure */
                    398:        disk_detach(dmv->diskp);
                    399:        disk_destroy(dmv->diskp);
1.2       haad      400:        
                    401:        /* Destroy device */
                    402:        (void)dm_dev_free(dmv);
                    403: 
1.6       haad      404:        /* Decrement device counter After removing device */
                    405:        atomic_dec_64(&dev_counter);
                    406:        
1.2       haad      407:        return 0;
                    408: }
                    409: 
                    410: /*
                    411:  * Return actual state of device to libdevmapper.
                    412:  */
                    413: int
                    414: dm_dev_status_ioctl(prop_dictionary_t dm_dict)
                    415: {
                    416:        dm_dev_t *dmv;
                    417:        const char *name, *uuid;
                    418:        uint32_t flags, j, minor;
                    419:        
                    420:        name = NULL;
                    421:        uuid = NULL;
                    422:        flags = 0;
                    423:        j = 0;
                    424:        
                    425:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    426:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    427:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    428:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    429:                
                    430:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
                    431:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    432:                return ENOENT;
                    433:        }
                    434: 
                    435:        dm_dbg_print_flags(dmv->flags);
                    436:        
                    437:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
                    438:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    439:        prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
                    440: 
                    441:        if (dmv->flags & DM_SUSPEND_FLAG)
                    442:                DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
                    443: 
                    444:        /* Add status flags for tables I have to check both 
                    445:           active and inactive tables. */
                    446:        if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
                    447:                DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    448:        } else
                    449:                DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    450:        
                    451:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
                    452:        
                    453:        if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
                    454:                DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    455:        else
                    456:                DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    457: 
                    458:        dm_dev_unbusy(dmv);
                    459:        
                    460:        return 0;
                    461: }
                    462: 
                    463: /*
                    464:  * Set only flag to suggest that device is suspended. This call is
                    465:  * not supported in NetBSD.
                    466:  *
                    467:  */
                    468: int
                    469: dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
                    470: {
                    471:        dm_dev_t *dmv;
                    472:        const char *name, *uuid;
                    473:        uint32_t flags, minor;
                    474:                
                    475:        name = NULL;
                    476:        uuid = NULL;
                    477:        flags = 0;
                    478:        
                    479:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    480:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    481:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    482:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    483:        
                    484:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    485:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    486:                return ENOENT;
                    487:        }
                    488:        
                    489:        atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
                    490:        
                    491:        dm_dbg_print_flags(dmv->flags);
                    492:        
                    493:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
                    494:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
                    495:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    496:        
                    497:        dm_dev_unbusy(dmv);
                    498:                
                    499:        /* Add flags to dictionary flag after dmv -> dict copy */
                    500:        DM_ADD_FLAG(flags, DM_EXISTS_FLAG); 
                    501:        
                    502:        return 0;
                    503: }
                    504: 
                    505: /*
                    506:  * Simulate Linux behaviour better and switch tables here and not in
                    507:  * dm_table_load_ioctl. 
                    508:  */
                    509: int
                    510: dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
                    511: {
                    512:        dm_dev_t *dmv;
                    513:        const char *name, *uuid;
                    514:        uint32_t flags, minor;
                    515: 
                    516:        name = NULL;
                    517:        uuid = NULL;
                    518:        flags = 0;
                    519:        
                    520: /*     char *xml;
                    521:        xml = prop_dictionary_externalize(dm_dict);
                    522:        printf("%s\n",xml);*/
                    523:        
                    524:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    525:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    526:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    527:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    528:        
                    529:        /* Remove device from global device list */
                    530:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    531:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    532:                return ENOENT;
                    533:        }
                    534: 
                    535:        atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
                    536:        atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
1.6       haad      537:        
1.2       haad      538:        dm_table_switch_tables(&dmv->table_head);
                    539:                
                    540:        DM_ADD_FLAG(flags, DM_EXISTS_FLAG);     
                    541: 
1.9       agc       542:        dmgetdisklabel(dmv->diskp->dk_label, &dmv->table_head);
1.2       haad      543:        
                    544:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
                    545:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
                    546:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    547: 
                    548:        dm_dev_unbusy(dmv);
                    549: 
                    550:        /* Destroy inactive table after resume. */
                    551:        dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
                    552:        
                    553:        return 0;
                    554: }
                    555: 
                    556: /*
                    557:  * Table management routines
                    558:  * lvm2tools doens't send name/uuid to kernel with table
                    559:  * for lookup I have to use minor number.
                    560:  */
                    561: 
                    562: /*
                    563:  * Remove inactive table from device. Routines which work's with inactive tables
                    564:  * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
                    565:  *
                    566:  */
                    567: int
                    568: dm_table_clear_ioctl(prop_dictionary_t dm_dict)
                    569: {
                    570:        dm_dev_t *dmv;
                    571:        const char *name, *uuid;
                    572:        uint32_t flags, minor;
                    573: 
                    574:        dmv  = NULL;
                    575:        name = NULL;
                    576:        uuid = NULL;
                    577:        flags = 0;
                    578:        minor = 0;
                    579:        
                    580:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    581:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    582:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    583:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    584:        
                    585:        aprint_debug("Clearing inactive table from device: %s--%s\n",
                    586:            name, uuid);
                    587:        
                    588:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    589:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    590:                return ENOENT;
                    591:        }
                    592:        
                    593:        /* Select unused table */
                    594:        dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
                    595: 
                    596:        atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
                    597: 
                    598:        dm_dev_unbusy(dmv);
                    599:        
                    600:        return 0;
                    601: }
                    602: 
                    603: /*
                    604:  * Get list of physical devices for active table.
                    605:  * Get dev_t from pdev vnode and insert it into cmd_array.
                    606:  *
                    607:  * XXX. This function is called from lvm2tools to get information
                    608:  *      about physical devices, too e.g. during vgcreate. 
                    609:  */
                    610: int
                    611: dm_table_deps_ioctl(prop_dictionary_t dm_dict)
                    612: {
                    613:        dm_dev_t *dmv;
                    614:        dm_table_t *tbl;
                    615:        dm_table_entry_t *table_en;
                    616: 
                    617:        prop_array_t cmd_array;
                    618:        const char *name, *uuid;
                    619:        uint32_t flags, minor;
                    620:        
                    621:        size_t i;
                    622: 
                    623:        name = NULL;
                    624:        uuid = NULL;
                    625:        dmv  = NULL;
                    626:        flags = 0;
                    627:        
                    628:        i = 0;
                    629: 
                    630:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    631:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    632:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    633:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    634: 
                    635:        /* create array for dev_t's */
                    636:        cmd_array = prop_array_create();
                    637: 
                    638:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    639:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    640:                return ENOENT;
                    641:        }
                    642: 
                    643:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    644:        prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
                    645:        prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
                    646:        
                    647:        aprint_debug("Getting table deps for device: %s\n", dmv->name);
                    648: 
                    649:        tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
                    650: 
                    651:        SLIST_FOREACH(table_en, tbl, next)
                    652:                        table_en->target->deps(table_en, cmd_array);
                    653: 
                    654:        dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
                    655:        dm_dev_unbusy(dmv);
                    656:                
                    657:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
                    658:        prop_object_release(cmd_array);
                    659:        
                    660:        return 0;
                    661: }
                    662: 
                    663: /*
                    664:  * Load new table/tables to device.
                    665:  * Call apropriate target init routine open all physical pdev's and
                    666:  * link them to device. For other targets mirror, strip, snapshot
                    667:  * etc. also add dependency devices to upcalls list.
                    668:  *
                    669:  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
                    670:  * This simulates Linux behaviour better there should not be any difference.
                    671:  *
                    672:  */
                    673: int
                    674: dm_table_load_ioctl(prop_dictionary_t dm_dict)
                    675: {
                    676:        dm_dev_t *dmv;
                    677:        dm_table_entry_t *table_en, *last_table;
                    678:        dm_table_t  *tbl;
                    679:        dm_target_t *target;
                    680: 
                    681:        prop_object_iterator_t iter;
                    682:        prop_array_t cmd_array;
1.12    ! haad      683:        prop_dictionary_t target_dict, param_dict;
1.2       haad      684:        
                    685:        const char *name, *uuid, *type;
                    686: 
                    687:        uint32_t flags, ret, minor;
                    688: 
                    689:        ret = 0;
                    690:        flags = 0;
                    691:        name = NULL;
                    692:        uuid = NULL;
                    693:        dmv = NULL;
                    694:        last_table = NULL;
                    695: 
                    696: /*     char *xml;
                    697:        xml = prop_dictionary_externalize(dm_dict);
                    698:        printf("%s\n",xml);*/
                    699:                
                    700:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    701:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    702:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    703:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    704:        
                    705:        cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
                    706:        iter = prop_array_iterator(cmd_array);
                    707:        dm_dbg_print_flags(flags);      
                    708:        
                    709:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    710:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    711:                return ENOENT;
                    712:        }
                    713:        
1.7       haad      714:        aprint_debug("Loading table to device: %s--%d\n", name,
                    715:            dmv->table_head.cur_active_table);
1.2       haad      716:        
                    717:        /*
                    718:         * I have to check if this table slot is not used by another table list.
                    719:         * if it is used I should free them.
                    720:         */
                    721:        if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
                    722:                dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
                    723: 
                    724:        dm_dbg_print_flags(dmv->flags);
                    725:        tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
                    726:        
                    727:        aprint_debug("dmv->name = %s\n", dmv->name);
                    728:        
                    729:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    730: 
                    731:        while((target_dict = prop_object_iterator_next(iter)) != NULL){
                    732: 
                    733:                prop_dictionary_get_cstring_nocopy(target_dict,
                    734:                    DM_TABLE_TYPE, &type);
                    735:                /*
                    736:                 * If we want to deny table with 2 or more different
                    737:                 * target we should do it here
                    738:                 */
1.7       haad      739:                if (((target = dm_target_lookup(type)) == NULL) &&
                    740:                    ((target = dm_target_autoload(type)) == NULL)) {
1.8       haad      741:                        dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
1.7       haad      742:                        dm_dev_unbusy(dmv);
1.2       haad      743:                        return ENOENT;
1.7       haad      744:                }
1.2       haad      745:                
1.3       haad      746:                if ((table_en = kmem_alloc(sizeof(dm_table_entry_t),
1.7       haad      747:                            KM_NOSLEEP)) == NULL) {
1.8       haad      748:                        dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
1.7       haad      749:                        dm_dev_unbusy(dmv);
1.2       haad      750:                        return ENOMEM;
1.7       haad      751:                }
1.2       haad      752:                
                    753:                prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
                    754:                    &table_en->start);
                    755:                prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
                    756:                    &table_en->length);
                    757: 
                    758:                table_en->target = target;
                    759:                table_en->dm_dev = dmv;
                    760:                table_en->target_config = NULL;
                    761:                
                    762:                /*
                    763:                 * There is a parameter string after dm_target_spec
                    764:                 * structure which  points to /dev/wd0a 284 part of
                    765:                 * table. String str points to this text. This can be
                    766:                 * null and therefore it should be checked before we try to
                    767:                 * use it.
                    768:                 */
1.12    ! haad      769:                param_dict = prop_dictionary_get(target_dict, DM_TABLE_PARAMS);
1.2       haad      770:                
                    771:                if (SLIST_EMPTY(tbl))
                    772:                        /* insert this table to head */
                    773:                        SLIST_INSERT_HEAD(tbl, table_en, next);
                    774:                else
                    775:                        SLIST_INSERT_AFTER(last_table, table_en, next);
                    776:                
                    777:                /*
                    778:                 * Params string is different for every target,
                    779:                 * therfore I have to pass it to target init
                    780:                 * routine and parse parameters there.
                    781:                 */
                    782:                if ((ret = target->init(dmv, &table_en->target_config,
1.12    ! haad      783:                        param_dict)) != 0) {
1.2       haad      784: 
1.4       haad      785:                        dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
1.2       haad      786:                        dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
1.12    ! haad      787:                        
1.4       haad      788:                        dm_dev_unbusy(dmv);
1.2       haad      789:                        return ret;
                    790:                }
                    791:                last_table = table_en;
                    792:        }
                    793:        prop_object_iterator_release(iter);
                    794:        
                    795:        DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    796:        atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
                    797:        
                    798:        dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
                    799:        dm_dev_unbusy(dmv);
                    800:        return 0;
                    801: }
                    802: 
                    803: /*
                    804:  * Get description of all tables loaded to device from kernel
                    805:  * and send it to libdevmapper.
                    806:  *
                    807:  * Output dictionary for every table:
                    808:  *
                    809:  * <key>cmd_data</key>
                    810:  * <array>
                    811:  *   <dict>
                    812:  *    <key>type<key>
                    813:  *    <string>...</string>
                    814:  *
                    815:  *    <key>start</key>
                    816:  *    <integer>...</integer>
                    817:  *
                    818:  *    <key>length</key>
                    819:  *    <integer>...</integer>
                    820:  *
                    821:  *    <key>params</key>
                    822:  *    <string>...</string>
                    823:  *   </dict>
                    824:  * </array>
                    825:  *
                    826:  */
                    827: int
                    828: dm_table_status_ioctl(prop_dictionary_t dm_dict)
                    829: {
                    830:        dm_dev_t *dmv;
                    831:        dm_table_t *tbl;
                    832:        dm_table_entry_t *table_en;
                    833: 
                    834:        prop_array_t cmd_array;
                    835:        prop_dictionary_t target_dict;
                    836:        
                    837:        uint32_t rec_size, minor;
                    838:        
                    839:        const char *name, *uuid;
                    840:        char *params;
                    841:        int flags;
                    842:        
                    843:        dmv = NULL;
                    844:        uuid = NULL;
                    845:        name = NULL;
                    846:        params = NULL;
                    847:        flags = 0;
                    848:        rec_size = 0;
                    849: 
                    850:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    851:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    852:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    853:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
                    854: 
                    855:        cmd_array = prop_array_create();
                    856:                
                    857:        if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
                    858:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    859:                return ENOENT;
                    860:        }
                    861: 
                    862:        if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
                    863:                DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    864:        else {
                    865:                DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    866:                
                    867:                if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
                    868:                        DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    869:                else {
                    870:                        DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    871:                }
                    872:        }
                    873:        
                    874:        if (dmv->flags & DM_SUSPEND_FLAG)
                    875:                DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
                    876:        
                    877:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
                    878:        
                    879:        /* I should use mutex here and not rwlock there can be IO operation
                    880:           during this ioctl on device. */
                    881: 
                    882:        aprint_debug("Status of device tables: %s--%d\n",
                    883:            name, dmv->table_head.cur_active_table);
                    884:        
                    885:        tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
                    886:        
                    887:        SLIST_FOREACH(table_en, tbl, next)
                    888:        {
                    889:                target_dict = prop_dictionary_create();
                    890:                aprint_debug("%016" PRIu64 ", length %016" PRIu64
                    891:                    ", target %s\n", table_en->start, table_en->length,
                    892:                    table_en->target->name);
                    893: 
                    894:                prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
                    895:                    table_en->start);
                    896:                prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
                    897:                    table_en->length);
                    898: 
                    899:                prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
                    900:                    table_en->target->name);
                    901: 
                    902:                /* dm_table_get_cur_actv.table ?? */
                    903:                prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
                    904:                    dmv->table_head.cur_active_table);
                    905: 
                    906:                if (flags |= DM_STATUS_TABLE_FLAG) {
                    907:                        params = table_en->target->status
                    908:                            (table_en->target_config);
                    909: 
                    910:                        if(params != NULL){
                    911:                                prop_dictionary_set_cstring(target_dict,
                    912:                                    DM_TABLE_PARAMS, params);
                    913:                                
                    914:                                kmem_free(params, strlen(params) + 1);
                    915:                        }
                    916:                }
                    917: 
                    918:                prop_array_add(cmd_array, target_dict);
                    919:                prop_object_release(target_dict);
                    920:        }
                    921: 
                    922:        dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
                    923:        dm_dev_unbusy(dmv);
                    924:        
                    925:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
                    926:        prop_object_release(cmd_array);
                    927:        
                    928:        return 0;
                    929: }
                    930: 
                    931: 
                    932: /*
                    933:  * For every call I have to set kernel driver version.
                    934:  * Because I can have commands supported only in other
                    935:  * newer/later version. This routine is called for every 
                    936:  * ioctl command.
                    937:  */
                    938: int
                    939: dm_check_version(prop_dictionary_t dm_dict)
                    940: {
                    941:        size_t i;
                    942:        int dm_version[3];
                    943:        prop_array_t ver;
                    944:        
                    945:        ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
                    946:        
                    947:        for(i=0; i < 3; i++) 
                    948:                prop_array_get_uint32(ver, i, &dm_version[i]);
                    949: 
                    950:        if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]){
                    951:                aprint_debug("libdevmapper/kernel version mismatch "
                    952:                    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
                    953:                    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
                    954:                    dm_version[0], dm_version[1], dm_version[2]);
                    955:                
                    956:                return EIO;
                    957:        }
                    958: 
                    959:        prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
                    960:        prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
                    961:        prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
                    962: 
                    963:        return 0;
                    964: }

CVSweb <webmaster@jp.NetBSD.org>