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

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

CVSweb <webmaster@jp.NetBSD.org>