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

1.1.2.1   haad        1: /*
                      2:  * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to The NetBSD Foundation
                      6:  * by Adam Hamsik.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     18:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     19:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     20:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     21:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     22:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     23:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     24:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     25:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     26:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     27:  * POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29: 
                     30: #include <sys/types.h>
                     31: #include <sys/param.h>
                     32: 
                     33: #include <sys/errno.h>
                     34: #include <sys/ioctl.h>
                     35: #include <sys/ioccom.h>
                     36: #include <sys/kmem.h>
                     37: #include <sys/lkm.h>
                     38: #include <sys/namei.h>
                     39: #include <sys/vnode.h>
                     40: 
                     41: #include <miscfs/specfs/specdev.h>
                     42: 
                     43: #include <machine/int_fmtio.h>
                     44: 
                     45: #include "netbsd-dm.h"
                     46: #include "dm.h"
                     47: 
                     48: extern struct dm_softc *dm_sc;
                     49: 
                     50: #define DM_REMOVE_FLAG(flag, name) do {                                           \
                     51:                prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag);  \
                     52:                flag &= ~name;                                             \
                     53:                prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag);   \
                     54: } while (/*CONSTCOND*/0)
                     55: 
                     56: #define DM_ADD_FLAG(flag, name) do {                                      \
                     57:                prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag);  \
                     58:                flag |= name;                                              \
                     59:                prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag);   \
                     60: } while (/*CONSTCOND*/0)
                     61: 
                     62: static int dm_dbg_print_flags(int);
                     63: 
                     64: /*
                     65:  * Print flags sent to the kernel from libevmapper.
                     66:  */
                     67: static int
                     68: dm_dbg_print_flags(int flags)
                     69: {
                     70:        aprint_verbose("dbg_print --- %d\n",flags);
                     71:        
                     72:        if (flags & DM_READONLY_FLAG)
                     73:                aprint_verbose("dbg_flags: DM_READONLY_FLAG set In/Out\n");
                     74: 
                     75:        if (flags & DM_SUSPEND_FLAG)
                     76:                aprint_verbose("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
                     77: 
                     78:        if (flags & DM_PERSISTENT_DEV_FLAG)
                     79:                aprint_verbose("dbg_flags: DM_PERSISTENT_DEV_FLAG set In\n");
                     80: 
                     81:        if (flags & DM_STATUS_TABLE_FLAG)
                     82:                aprint_verbose("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
                     83: 
                     84:        if (flags & DM_ACTIVE_PRESENT_FLAG)
                     85:                aprint_verbose("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
                     86: 
                     87:        if (flags & DM_INACTIVE_PRESENT_FLAG)
1.1.2.2   haad       88:                aprint_verbose("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
1.1.2.1   haad       89: 
                     90:        if (flags & DM_BUFFER_FULL_FLAG)
                     91:                aprint_verbose("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
                     92: 
                     93:        if (flags & DM_SKIP_BDGET_FLAG)
                     94:                aprint_verbose("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
                     95: 
                     96:        if (flags & DM_SKIP_LOCKFS_FLAG)
                     97:                aprint_verbose("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
                     98: 
                     99:        if (flags & DM_NOFLUSH_FLAG)
                    100:                aprint_verbose("dbg_flags: DM_NOFLUSH_FLAG set In\n");
                    101:        
                    102:        return 0;
                    103: }
                    104: 
1.1.2.2   haad      105: /*
                    106:  * Get version ioctl call I do it as default therefore this
                    107:  * function is unused now.
                    108:  */
1.1.2.1   haad      109: int
                    110: dm_get_version_ioctl(prop_dictionary_t dm_dict)
                    111: {
                    112:        int r;
                    113: 
                    114:        r = 0;
                    115: 
                    116:        aprint_verbose("dm_get_version_ioctl called\n");
                    117: 
                    118:        return 0;
                    119: }
                    120: 
                    121: /*
                    122:  * Get list of all available targets from global
                    123:  * target list and sent them back to libdevmapper.
                    124:  */
                    125: int
                    126: dm_list_versions_ioctl(prop_dictionary_t dm_dict)
                    127: {
                    128:        prop_array_t target_list;
                    129:        
                    130:        int r;
                    131:        uint32_t flags;
                    132:        
                    133:        r = 0;
                    134: 
                    135:        aprint_verbose("dm_list_versions_ioctl called\n");
                    136: 
1.1.2.2   haad      137:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      138:        
                    139:        dm_dbg_print_flags(flags);
                    140: 
                    141:        target_list = dm_target_prop_list();
                    142: 
1.1.2.2   haad      143:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
1.1.2.1   haad      144:        
                    145:        return r;
                    146: }
                    147: 
                    148: /*
                    149:  * Create in-kernel entry for device. Device attributes such as name, uuid are
                    150:  * taken from proplib dictionary.
                    151:  */
                    152: int
                    153: dm_dev_create_ioctl(prop_dictionary_t dm_dict)
                    154: {
                    155:        struct dm_dev *dmv;
                    156:        const char *name, *uuid;
                    157:        int r,flags;
                    158:        
                    159:        r = 0;
                    160:        flags = 0;
                    161:        name = NULL;    
                    162:        uuid = NULL;
                    163: 
                    164:        /* Get needed values from dictionary. */
1.1.2.2   haad      165:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    166:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    167:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      168:        
                    169:        dm_dbg_print_flags(flags);
                    170: 
                    171:        /* Lookup name and uuid if device already exist quit. */
                    172:        if ((dm_dev_lookup_name(name) != NULL) &&
                    173:            (dm_dev_lookup_uuid(uuid) != NULL)) {
                    174:                DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
                    175:                return EEXIST;
                    176:        }
                    177: 
                    178: 
                    179:        if ((dmv = dm_dev_alloc()) == NULL)
                    180:                return ENOMEM;
                    181: /*     printf("%d---%d\n",sizeof(dmv->uuid),DM_UUID_LEN);
                    182:        printf("%s\n",uuid);
                    183:        if (uuid)
                    184:        memcpy(dmv->uuid,uuid,DM_UUID_LEN);*/
                    185:        printf("%s\n",name);
                    186:        if (name)
1.1.2.2   haad      187:                strlcpy(dmv->name, name, DM_NAME_LEN);
1.1.2.1   haad      188: 
                    189:        dmv->flags = flags;
                    190: 
                    191:        dmv->minor = ++(dm_sc->sc_minor_num);
                    192: 
                    193:        dmv->ref_cnt = 0;
                    194:        dmv->event_nr = 0;
                    195:        dmv->cur_active_table = 0;
                    196: 
1.1.2.3 ! haad      197:        dmv->dev_type = 0;
        !           198:        dmv->dm_dklabel = NULL;
        !           199:        
1.1.2.1   haad      200:        /* Initialize tables. */
                    201:        SLIST_INIT(&dmv->tables[0]);
                    202:        SLIST_INIT(&dmv->tables[1]);
                    203: 
                    204:        /* init device list of physical devices. */
                    205:        SLIST_INIT(&dmv->pdevs);
                    206:        /*
                    207:         * Locking strategy here is this: this is per device mutex lock
                    208:         * and it should be taken when we work with device. Almost all
                    209:         * ioctl callbacks for tables and devices must acquire this lock.
                    210:         */
                    211: 
1.1.2.2   haad      212:        prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
1.1.2.1   haad      213:        
1.1.2.2   haad      214:        mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
1.1.2.1   haad      215:    
                    216:        /* Test readonly flag change anything only if it is not set*/
                    217:        if (flags & DM_READONLY_FLAG)
                    218:                dm_dev_free(dmv);
                    219:        else
                    220:                r = dm_dev_insert(dmv);
                    221:        
                    222:        return r;
                    223: }
                    224: 
                    225: /*
                    226:  * Get list of created device-mapper devices fromglobal list and
                    227:  * send it to kernel.
                    228:  *
                    229:  * Output dictionary:
                    230:  *
                    231:  * <key>cmd_data</key>
                    232:  *  <array>
                    233:  *   <dict>
                    234:  *    <key>name<key>
                    235:  *    <string>...</string>
                    236:  *
                    237:  *    <key>dev</key>
                    238:  *    <integer>...</integer>
                    239:  *   </dict>
                    240:  *  </array>
                    241:  * 
                    242:  */
                    243: int
                    244: dm_dev_list_ioctl(prop_dictionary_t dm_dict)
                    245: {
                    246:        prop_array_t dev_list;
                    247:        
                    248:        int r;
                    249:        uint32_t flags;
                    250:        
                    251:        r = 0;
                    252: 
                    253:        aprint_verbose("dm_dev_list called\n");
                    254: 
1.1.2.2   haad      255:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      256:        
                    257:        dm_dbg_print_flags(flags);
                    258:        
                    259:        dev_list = dm_dev_prop_list();
                    260: 
1.1.2.2   haad      261:        prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
1.1.2.1   haad      262:        
                    263:        return 0;
                    264: }
                    265: 
                    266: /*
                    267:  * Rename selected devices old name is in struct dm_ioctl.
                    268:  * newname is taken from dictionary
                    269:  *
                    270:  * <key>cmd_data</key>
                    271:  *  <array>
                    272:  *   <string>...</string>
                    273:  *  </array>
                    274:  */
                    275: int
                    276: dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
                    277: {
                    278:        prop_array_t cmd_array;
                    279:        struct dm_dev *dmv;
                    280:        
                    281:        const char *name,*uuid,*n_name;
                    282:        uint32_t flags;
                    283: 
                    284:        name = NULL;
                    285:        uuid = NULL;
                    286:        
                    287:        /* Get needed values from dictionary. */
1.1.2.2   haad      288:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    289:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    290:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    291:        cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
1.1.2.1   haad      292: 
1.1.2.2   haad      293:        prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
1.1.2.1   haad      294:        
                    295:        if (strlen(n_name) + 1 > DM_NAME_LEN)
                    296:                return EINVAL;
                    297:        
                    298:        if (((dmv = dm_dev_lookup_uuid(uuid)) == NULL) ||
                    299:            (dmv = dm_dev_lookup_name(name)) == NULL) {
                    300:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    301:                return ENOENT;
                    302:        }
                    303:        
                    304:        /* Test readonly flag change anything only if it is not set*/
                    305:        if (!(flags & DM_READONLY_FLAG))
1.1.2.2   haad      306:                strlcpy(dmv->name, n_name, DM_NAME_LEN);
1.1.2.1   haad      307:        
                    308:        return 0;
                    309: }
                    310: 
                    311: /*
                    312:  * Remove device from global list I have to remove active
                    313:  * and inactive tables first and decrement reference_counters
                    314:  * for used pdevs.
                    315:  */
                    316: int
                    317: dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
                    318: {
                    319:        struct dm_dev *dmv;
                    320: 
                    321:        const char *name, *uuid;
                    322:        uint32_t flags;
                    323: 
                    324:        name = NULL;
                    325:        uuid = NULL;
                    326:        
                    327:        /* Get needed values from dictionary. */
1.1.2.2   haad      328:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    329:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    330:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      331:        
                    332:        dm_dbg_print_flags(flags);
                    333:        
                    334:        if (((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
                    335:            (dmv = dm_dev_lookup_name(name)) == NULL) {
                    336:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    337:                return ENOENT;
                    338:        }
                    339:        
                    340:        if (!(flags & DM_READONLY_FLAG)) {
                    341:                if (dmv->ref_cnt == 0){
                    342:                        /* Destroy active table first.  */
                    343:                        dm_table_destroy(&dmv->tables[dmv->cur_active_table]);
                    344: 
                    345:                        /* Destroy unactive table if exits, too. */
                    346:                        if (!SLIST_EMPTY(&dmv->tables[1 - dmv->cur_active_table]))
                    347:                                dm_table_destroy(&dmv->tables[1 - dmv->cur_active_table]);
                    348:                        
                    349:                        /* Decrement reference counters for all pdevs from this
                    350:                           device if they are unused close vnode and remove them
                    351:                           from global pdev list, too. */
                    352:                        dm_pdev_decr(&dmv->pdevs);
                    353:        
                    354:                        /* Test readonly flag change anything only if it is not set*/
                    355:        
                    356:                        dm_dev_rem(name); /* XXX. try to use uuid, too */
                    357:                }
                    358:                
                    359:        }
                    360:        return 0;
                    361: }
                    362: 
                    363: /*
                    364:  * Return actual state of device to libdevmapper.
                    365:  */
                    366: int
                    367: dm_dev_status_ioctl(prop_dictionary_t dm_dict)
                    368: {
                    369:        struct dm_dev *dmv;
                    370:        const char *name, *uuid;
                    371:        uint32_t flags;
                    372:                
                    373:        name = NULL;
                    374:        uuid = NULL;
                    375:        
1.1.2.2   haad      376:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    377:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    378:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      379:        
                    380:        if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
                    381:            ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
                    382:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    383:                return 0;
                    384:        }
                    385:        
1.1.2.2   haad      386:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
                    387:        prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
1.1.2.1   haad      388:        
1.1.2.2   haad      389:        prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
1.1.2.1   haad      390:        
                    391:        return 0;
                    392: }
                    393: 
                    394: /*
                    395:  * Unsupported on NetBSD.
                    396:  */
                    397: int
                    398: dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
                    399: {
                    400:        return 0;
                    401: }
                    402: 
                    403: /*
                    404:  * Unsupported on NetBSD.
                    405:  */
                    406: int
                    407: dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
                    408: {
                    409: //     DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    410: 
                    411:        return 0;
                    412: }
                    413: 
                    414: /*
                    415:  * Table management routines
                    416:  * lvm2tools doens't send name/uuid to kernel with table for lookup I have to use minor number.
                    417:  */
                    418: 
                    419: 
                    420: /*
                    421:  * Remove active table from device.
                    422:  */
                    423: int
                    424: dm_table_clear_ioctl(prop_dictionary_t dm_dict)
                    425: {
                    426:        struct dm_dev *dmv;
                    427:        struct dm_table  *tbl;
                    428: 
                    429:        const char *name, *uuid;
                    430:        int flags;
                    431: 
                    432:        dmv  = NULL;
                    433:        name = NULL;
                    434:        uuid = NULL;
                    435:        
1.1.2.2   haad      436:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    437:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    438:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
1.1.2.1   haad      439:        
1.1.2.2   haad      440:        aprint_verbose("Clearing inactive table from device: %s--%s\n",
                    441:            name, uuid);
1.1.2.1   haad      442:        
                    443:        if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
                    444:            ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
                    445:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    446:                return ENOENT;
                    447:        }
                    448:        
                    449:        /* Select unused table */
                    450:        tbl = &dmv->tables[1-dmv->cur_active_table]; 
                    451: 
                    452:        /* Test readonly flag change anything only if it is not set*/
                    453:        if (!(flags & DM_READONLY_FLAG))
                    454:                        dm_table_destroy(tbl);
                    455: 
1.1.2.3 ! haad      456:        dmv->dev_type = 0;
        !           457:        
1.1.2.1   haad      458:        return 0;
                    459: }
                    460: 
                    461: /*
                    462:  * Get list of physical devices for active table.
                    463:  * Get dev_t from pdev vnode and insert it into cmd_array.
                    464:  *
                    465:  * XXX. This function is called from lvm2tools to get information
                    466:  *      about physical devices, too e.g. during vgcreate. 
                    467:  */
                    468: int
                    469: dm_table_deps_ioctl(prop_dictionary_t dm_dict)
                    470: {
                    471:        int error;
                    472:        struct dm_dev *dmv;
                    473:        struct dm_pdev *dmp;
                    474:        struct vattr va;
                    475:        
                    476:        prop_array_t cmd_array;
                    477:        
                    478:        const char *name, *uuid;
                    479:        int flags;
                    480:        uint64_t dev;
                    481:        
                    482:        size_t i;
                    483: 
                    484:        name = NULL;
                    485:        uuid = NULL;
                    486:        dmv  = NULL;
                    487:        
                    488:        i=0;
                    489: 
1.1.2.2   haad      490:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    491:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    492:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    493:        prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
1.1.2.1   haad      494:        
                    495:        cmd_array = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA);
                    496: 
                    497:        if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
                    498:            ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
                    499:            ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
                    500:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    501:                return ENOENT;
                    502:        }
                    503:        
                    504:        aprint_verbose("Getting table deps for device: %s\n",name);
                    505:                
                    506:        SLIST_FOREACH(dmp, &dmv->pdevs, next_pdev){
                    507: 
1.1.2.2   haad      508:                if ((error = VOP_GETATTR(dmp->pdev_vnode, &va, curlwp->l_cred))
                    509:                    != 0)
1.1.2.1   haad      510:                        return (error);
                    511:                
1.1.2.2   haad      512:                prop_array_set_uint64(cmd_array, i, (uint64_t)va.va_rdev);
1.1.2.1   haad      513: 
                    514:                i++;
                    515:        }
                    516:        
                    517:        return 0;
                    518: }
                    519: 
                    520: /*
                    521:  * Load new table/tables to device.
                    522:  * Call apropriate target init routine open all physical pdev's and
                    523:  * link them to device. For other targets mirror, strip, snapshot
                    524:  * etc. also add dependency devices to upcalls list.
                    525:  *
                    526:  * XXX. It would be usefull to refactorize this routine to be shorter ?
                    527:  */
                    528: int
                    529: dm_table_load_ioctl(prop_dictionary_t dm_dict)
                    530: {
                    531:        struct dm_dev *dmv;
                    532:        struct dm_pdev *dmp;
                    533:        struct dm_table_entry *table_en, *last_table;
                    534:        struct dm_table  *tbl;
                    535:        struct dm_target *target;
                    536: 
                    537:        prop_object_iterator_t iter;
                    538:        prop_array_t cmd_array;
                    539:        prop_dictionary_t target_dict;
                    540:        
                    541:        const char *name, *uuid, *type, *str_ptr;
                    542: 
                    543:        uint32_t flags;
                    544: 
                    545:        uint64_t dev;
                    546:        
                    547:        char *dev_name,*str;
                    548: 
                    549:        flags = 0;
                    550:        name = NULL;
                    551:        uuid = NULL;
                    552:        dmv = NULL;
                    553:        dmp = NULL;
                    554:        last_table = NULL;
                    555:        str_ptr = NULL;
                    556: 
                    557:        char *xml;
                    558:        xml = prop_dictionary_externalize(dm_dict);
                    559:        printf("%s\n",xml);
                    560:                
1.1.2.2   haad      561:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    562:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    563:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    564:        prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
1.1.2.1   haad      565:        
1.1.2.2   haad      566:        cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
1.1.2.1   haad      567:        iter = prop_array_iterator(cmd_array);
                    568:        
                    569:        dm_dbg_print_flags(flags);      
                    570: 
                    571:        aprint_verbose("Loading table to device: %s--%s\n",name,uuid);
                    572:        
                    573:        if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
                    574:            ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
                    575:            ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
                    576:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    577:                return ENOENT;
                    578:        }
                    579:        
                    580:        /* Select unused table */
                    581:        tbl = &dmv->tables[1-dmv->cur_active_table]; 
                    582: 
                    583:        /*
                    584:         * I have to check if this table slot is not used by another table list.
                    585:         * if it is used I should free them.
                    586:         */
                    587: 
                    588:        dm_table_destroy(tbl);
                    589:        
                    590:        while((target_dict = prop_object_iterator_next(iter)) != NULL){
                    591: 
1.1.2.2   haad      592:                prop_dictionary_get_cstring_nocopy(target_dict,
                    593:                    DM_TABLE_TYPE, &type);
1.1.2.1   haad      594:                
1.1.2.2   haad      595:                /*
                    596:                 * If we want to deny table with 2 or more different
                    597:                 * target we should do it here
                    598:                 */
1.1.2.1   haad      599:                if ((target = dm_target_lookup_name(type)) == NULL)
                    600:                        return ENOENT;
                    601:                
1.1.2.2   haad      602:                if ((table_en=kmem_alloc(sizeof(struct dm_table_entry),
                    603:                            KM_NOSLEEP)) == NULL)
1.1.2.1   haad      604:                        return ENOMEM;
                    605:                
1.1.2.2   haad      606:                prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
                    607:                    &table_en->start);
                    608:                prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
                    609:                    &table_en->length);
1.1.2.1   haad      610: 
                    611:                table_en->target=target;
                    612:                table_en->dm_dev=dmv;
                    613:                
                    614:                /*
                    615:                 * There is a parameter string after dm_target_spec
                    616:                 * structure which  points to /dev/wd0a 284 part of
                    617:                 * table. String str points to this text.
                    618:                 */
                    619: 
                    620:                prop_dictionary_get_cstring_nocopy(target_dict,
                    621:                    DM_TABLE_PARAMS, (const char**)&str);
                    622: 
                    623:                if (strlen(str) != 0) {
                    624: 
                    625:                        str_ptr = str; /* save pointer to start of string */
                    626:                        
1.1.2.2   haad      627:                        aprint_verbose("trg_str: %s\n", str);
1.1.2.1   haad      628:                        
                    629:                        dev_name = strsep(&str," ");
                    630: 
1.1.2.2   haad      631:                        aprint_verbose("namei: %s, str: %s\n", dev_name, str);
1.1.2.1   haad      632:                                                
                    633:                        if (!(flags & DM_READONLY_FLAG)){
                    634: 
                    635:                                if ((dmp = dm_pdev_insert(dev_name)) == NULL){
1.1.2.2   haad      636:                                        kmem_free(table_en,
                    637:                                            sizeof(struct dm_table_entry));
1.1.2.1   haad      638:                                        return ENOENT;
                    639:                                }
                    640: 
                    641:                                /* insert pdev to device list  */
1.1.2.2   haad      642:                                SLIST_INSERT_HEAD(&dmv->pdevs, dmp, next_pdev);
1.1.2.1   haad      643:                        }
                    644:                }
                    645:                
                    646:                /* Test readonly flag change anything only if it is not set*/
                    647:                if (!(flags & DM_READONLY_FLAG)){
                    648: 
                    649:                        if (SLIST_EMPTY(tbl))
                    650:                                /* insert this table to head */
1.1.2.2   haad      651:                                SLIST_INSERT_HEAD(tbl, table_en, next);
1.1.2.1   haad      652:                        else
1.1.2.2   haad      653:                                SLIST_INSERT_AFTER(last_table, table_en, next);
                    654: 
1.1.2.1   haad      655:                        prop_dictionary_get_cstring(target_dict,
1.1.2.2   haad      656:                            DM_TABLE_PARAMS, (char **)&table_en->params);
1.1.2.1   haad      657: 
                    658:                        /*
                    659:                         * XXX I have to calculate argc
                    660:                         */
                    661:                        if (target->init != NULL)
1.1.2.2   haad      662:                                target->init(dmv, &table_en->target_config,
                    663:                                    2, &str_ptr);
1.1.2.1   haad      664: 
                    665:                        last_table = table_en;
                    666:                        
                    667:                } else {
1.1.2.2   haad      668:                        kmem_free(table_en, sizeof(struct dm_table_entry));
1.1.2.1   haad      669:                        dm_pdev_destroy(dmp);
                    670:                }
                    671:                        
                    672:        }
                    673:        
                    674:        dmv->cur_active_table = 1 - dmv->cur_active_table;
                    675: 
                    676:        prop_object_release(cmd_array);
                    677: 
                    678:        DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
                    679:        
                    680:        return 0;
                    681: }
                    682: 
                    683: /*
                    684:  * Get description of all tables loaded to device from kernel
                    685:  * and send it to libdevmapper.
                    686:  *
                    687:  * Output dictionary for every table:
                    688:  *
                    689:  * <key>cmd_data</key>
                    690:  * <array>
                    691:  *   <dict>
                    692:  *    <key>type<key>
                    693:  *    <string>...</string>
                    694:  *
                    695:  *    <key>start</key>
                    696:  *    <integer>...</integer>
                    697:  *
                    698:  *    <key>length</key>
                    699:  *    <integer>...</integer>
                    700:  *
                    701:  *    <key>params</key>
                    702:  *    <string>...</string>
                    703:  *   </dict>
                    704:  * </array>
                    705:  *
                    706:  */
                    707: int
                    708: dm_table_status_ioctl(prop_dictionary_t dm_dict)
                    709: {
                    710:        struct dm_dev *dmv;
                    711:        struct dm_table  *tbl;
                    712:        struct dm_table_entry *table_en;
                    713: 
                    714:        prop_array_t cmd_array;
                    715:        prop_dictionary_t target_dict;
                    716:        
                    717:        uint32_t rec_size;
                    718:        uint64_t dev;
                    719:        
                    720:        const char *name, *uuid;
                    721:        int flags,i;
                    722:        
                    723:        dmv = NULL;
                    724:        uuid = NULL;
                    725:        name = NULL;
                    726: 
                    727:        flags = 0;
                    728:        rec_size = 0;
                    729:        i=0;
                    730: 
1.1.2.2   haad      731:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
                    732:        prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
                    733:        prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
                    734:        prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
1.1.2.1   haad      735: 
1.1.2.2   haad      736:        cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
1.1.2.1   haad      737:        
                    738:        if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
                    739:            ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
                    740:            ((dmv = dm_dev_lookup_minor(minor(dev)))) == NULL) {
                    741:                DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
                    742:                return ENOENT;
                    743:        }
                    744:        
1.1.2.2   haad      745:        aprint_verbose("Status of device tables: %s--%d\n",
                    746:            name, dmv->cur_active_table);
1.1.2.1   haad      747:        
                    748:        tbl = &dmv->tables[dmv->cur_active_table];
                    749: 
                    750:        SLIST_FOREACH(table_en,tbl,next)
                    751:        {
                    752:                target_dict = prop_dictionary_create();
1.1.2.2   haad      753:                aprint_verbose("%016" PRIu64 ", length %016" PRIu64
                    754:                    ", target %s\n", table_en->start, table_en->length,
                    755:                    table_en->target->name);
                    756: 
                    757:                prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
                    758:                    table_en->start);
                    759:                prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
                    760:                    table_en->length);
                    761: 
                    762:                prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
                    763:                    table_en->target->name);
                    764:                prop_dictionary_set_cstring(target_dict, DM_TABLE_PARAMS,
                    765:                    table_en->params);
1.1.2.1   haad      766:                
1.1.2.2   haad      767:                prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
                    768:                    dmv->cur_active_table);
1.1.2.1   haad      769: 
1.1.2.2   haad      770:                prop_array_set(cmd_array, i, target_dict);
1.1.2.1   haad      771: 
                    772:                prop_object_release(target_dict);
                    773: 
                    774:                DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
                    775:                                
                    776:                i++;
                    777:        }
                    778: 
                    779:        return 0;
                    780: }
                    781: 
                    782: 
                    783: /*
                    784:  * For every call I have to set kernel driver version.
                    785:  * Because I can have commands supported only in other
                    786:  * newer/later version. This routine is called for every 
                    787:  * ioctl command.
                    788:  */
                    789: int
                    790: dm_check_version(prop_dictionary_t dm_dict)
                    791: {
                    792:        size_t i;
                    793:        int dm_version[3];
                    794:        prop_array_t ver;
                    795:        int r;
                    796: 
                    797:        r = 0;
                    798:        
1.1.2.2   haad      799:        ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
1.1.2.1   haad      800:        
                    801:        for(i=0; i < 3; i++) 
1.1.2.2   haad      802:                prop_array_get_uint32(ver, i, &dm_version[i]);
1.1.2.1   haad      803:        
                    804: 
1.1.2.2   haad      805:        if (DM_VERSION_MAJOR != dm_version[0] ||
                    806:            DM_VERSION_MINOR < dm_version[1])
1.1.2.1   haad      807:        {
                    808:                aprint_verbose("libdevmapper/kernel version mismatch "
                    809:                    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
1.1.2.2   haad      810:                    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
                    811:                    dm_version[0], dm_version[1], dm_version[2]);
                    812:                
1.1.2.1   haad      813:                r = EIO;
                    814:                goto out;
                    815:        }
                    816: 
1.1.2.2   haad      817:        prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
                    818:        prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
                    819:        prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
1.1.2.1   haad      820: 
                    821: out:   
                    822:        return r;
                    823: }

CVSweb <webmaster@jp.NetBSD.org>