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

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

CVSweb <webmaster@jp.NetBSD.org>