Home | History | Annotate | Line # | Download | only in dm
dm_table.c revision 1.5.4.1
      1  1.5.4.1     rmind /*        $NetBSD: dm_table.c,v 1.5.4.1 2011/03/05 20:53:07 rmind Exp $      */
      2      1.2      haad 
      3      1.2      haad /*
      4      1.2      haad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5      1.2      haad  * All rights reserved.
      6      1.2      haad  *
      7      1.2      haad  * This code is derived from software contributed to The NetBSD Foundation
      8      1.2      haad  * by Adam Hamsik.
      9      1.2      haad  *
     10      1.2      haad  * Redistribution and use in source and binary forms, with or without
     11      1.2      haad  * modification, are permitted provided that the following conditions
     12      1.2      haad  * are met:
     13      1.2      haad  * 1. Redistributions of source code must retain the above copyright
     14      1.2      haad  *    notice, this list of conditions and the following disclaimer.
     15      1.2      haad  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.2      haad  *    notice, this list of conditions and the following disclaimer in the
     17      1.2      haad  *    documentation and/or other materials provided with the distribution.
     18      1.2      haad  *
     19      1.2      haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.2      haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.2      haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.2      haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.2      haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.2      haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.2      haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.2      haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.2      haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.2      haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.2      haad  * POSSIBILITY OF SUCH DAMAGE.
     30      1.2      haad  */
     31      1.2      haad 
     32      1.2      haad #include <sys/types.h>
     33      1.2      haad #include <sys/param.h>
     34      1.2      haad 
     35      1.2      haad #include <sys/kmem.h>
     36      1.2      haad 
     37      1.2      haad #include "dm.h"
     38      1.2      haad 
     39      1.2      haad /*
     40      1.2      haad  * There are two types of users of this interface:
     41      1.5      haad  *
     42      1.2      haad  * a) Readers such as
     43      1.2      haad  *    dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
     44      1.2      haad  *    dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl
     45      1.2      haad  *
     46      1.2      haad  * b) Writers such as
     47      1.2      haad  *    dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl
     48      1.2      haad  *
     49      1.2      haad  * Writers can work with table_head only when there are no readers. I
     50      1.2      haad  * use reference counting on io_cnt.
     51      1.2      haad  *
     52      1.2      haad  */
     53      1.2      haad 
     54      1.5      haad static int dm_table_busy(dm_table_head_t *, uint8_t);
     55      1.2      haad static void dm_table_unbusy(dm_table_head_t *);
     56      1.2      haad 
     57      1.2      haad /*
     58      1.2      haad  * Function to increment table user reference counter. Return id
     59      1.2      haad  * of table_id table.
     60      1.2      haad  * DM_TABLE_ACTIVE will return active table id.
     61      1.2      haad  * DM_TABLE_INACTIVE will return inactive table id.
     62      1.2      haad  */
     63      1.2      haad static int
     64      1.5      haad dm_table_busy(dm_table_head_t * head, uint8_t table_id)
     65      1.2      haad {
     66      1.2      haad 	uint8_t id;
     67      1.2      haad 
     68      1.2      haad 	id = 0;
     69      1.2      haad 
     70      1.2      haad 	mutex_enter(&head->table_mtx);
     71      1.2      haad 
     72      1.2      haad 	if (table_id == DM_TABLE_ACTIVE)
     73      1.2      haad 		id = head->cur_active_table;
     74      1.2      haad 	else
     75      1.2      haad 		id = 1 - head->cur_active_table;
     76      1.2      haad 
     77      1.2      haad 	head->io_cnt++;
     78      1.2      haad 
     79      1.2      haad 	mutex_exit(&head->table_mtx);
     80      1.2      haad 	return id;
     81      1.2      haad }
     82      1.2      haad /*
     83      1.2      haad  * Function release table lock and eventually wakeup all waiters.
     84      1.2      haad  */
     85      1.2      haad static void
     86      1.5      haad dm_table_unbusy(dm_table_head_t * head)
     87      1.2      haad {
     88      1.2      haad 	KASSERT(head->io_cnt != 0);
     89      1.2      haad 
     90      1.2      haad 	mutex_enter(&head->table_mtx);
     91      1.5      haad 
     92      1.2      haad 	if (--head->io_cnt == 0)
     93      1.2      haad 		cv_broadcast(&head->table_cv);
     94      1.5      haad 
     95      1.2      haad 	mutex_exit(&head->table_mtx);
     96      1.2      haad }
     97      1.2      haad /*
     98      1.2      haad  * Return current active table to caller, increment io_cnt reference counter.
     99      1.2      haad  */
    100      1.2      haad dm_table_t *
    101      1.5      haad dm_table_get_entry(dm_table_head_t * head, uint8_t table_id)
    102      1.2      haad {
    103      1.2      haad 	uint8_t id;
    104      1.2      haad 
    105      1.2      haad 	id = dm_table_busy(head, table_id);
    106      1.5      haad 
    107      1.2      haad 	return &head->tables[id];
    108      1.2      haad }
    109      1.2      haad /*
    110      1.2      haad  * Decrement io reference counter and wake up all callers, with table_head cv.
    111      1.2      haad  */
    112      1.2      haad void
    113      1.5      haad dm_table_release(dm_table_head_t * head, uint8_t table_id)
    114      1.2      haad {
    115      1.2      haad 	dm_table_unbusy(head);
    116      1.2      haad }
    117      1.2      haad /*
    118      1.2      haad  * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
    119      1.2      haad  */
    120      1.2      haad void
    121      1.5      haad dm_table_switch_tables(dm_table_head_t * head)
    122      1.2      haad {
    123      1.2      haad 	mutex_enter(&head->table_mtx);
    124      1.2      haad 
    125      1.2      haad 	while (head->io_cnt != 0)
    126      1.2      haad 		cv_wait(&head->table_cv, &head->table_mtx);
    127      1.2      haad 
    128      1.2      haad 	head->cur_active_table = 1 - head->cur_active_table;
    129      1.2      haad 
    130      1.2      haad 	mutex_exit(&head->table_mtx);
    131      1.2      haad }
    132      1.2      haad /*
    133      1.2      haad  * Destroy all table data. This function can run when there are no
    134      1.2      haad  * readers on table lists.
    135      1.2      haad  *
    136      1.2      haad  * XXX Is it ok to call kmem_free and potentialy VOP_CLOSE with held mutex ?xs
    137      1.2      haad  */
    138      1.2      haad int
    139      1.5      haad dm_table_destroy(dm_table_head_t * head, uint8_t table_id)
    140      1.2      haad {
    141      1.5      haad 	dm_table_t *tbl;
    142      1.2      haad 	dm_table_entry_t *table_en;
    143      1.2      haad 	uint8_t id;
    144      1.2      haad 
    145      1.2      haad 	mutex_enter(&head->table_mtx);
    146      1.2      haad 
    147      1.4     joerg 	aprint_debug("dm_Table_destroy called with %d--%d\n", table_id, head->io_cnt);
    148      1.4     joerg 
    149      1.2      haad 	while (head->io_cnt != 0)
    150      1.2      haad 		cv_wait(&head->table_cv, &head->table_mtx);
    151      1.2      haad 
    152      1.2      haad 	if (table_id == DM_TABLE_ACTIVE)
    153      1.2      haad 		id = head->cur_active_table;
    154      1.2      haad 	else
    155      1.2      haad 		id = 1 - head->cur_active_table;
    156      1.5      haad 
    157      1.2      haad 	tbl = &head->tables[id];
    158      1.5      haad 
    159      1.5      haad 	while (!SLIST_EMPTY(tbl)) {	/* List Deletion. */
    160      1.2      haad 		table_en = SLIST_FIRST(tbl);
    161      1.2      haad 		/*
    162      1.2      haad 		 * Remove target specific config data. After successfull
    163      1.2      haad 		 * call table_en->target_config must be set to NULL.
    164      1.2      haad 		 */
    165      1.5      haad 		table_en->target->destroy(table_en);
    166      1.5      haad 
    167      1.2      haad 		SLIST_REMOVE_HEAD(tbl, next);
    168      1.2      haad 
    169      1.2      haad 		kmem_free(table_en, sizeof(*table_en));
    170      1.2      haad 	}
    171      1.2      haad 
    172      1.2      haad 	mutex_exit(&head->table_mtx);
    173      1.5      haad 
    174      1.2      haad 	return 0;
    175      1.2      haad }
    176      1.2      haad /*
    177      1.2      haad  * Return length of active table in device.
    178      1.2      haad  */
    179      1.2      haad uint64_t
    180      1.5      haad dm_table_size(dm_table_head_t * head)
    181      1.2      haad {
    182      1.5      haad 	dm_table_t *tbl;
    183      1.2      haad 	dm_table_entry_t *table_en;
    184      1.2      haad 	uint64_t length;
    185      1.2      haad 	uint8_t id;
    186      1.5      haad 
    187      1.2      haad 	length = 0;
    188      1.2      haad 
    189      1.2      haad 	id = dm_table_busy(head, DM_TABLE_ACTIVE);
    190      1.5      haad 
    191      1.2      haad 	/* Select active table */
    192      1.2      haad 	tbl = &head->tables[id];
    193      1.5      haad 
    194      1.2      haad 	/*
    195      1.2      haad 	 * Find out what tables I want to select.
    196      1.2      haad 	 * if length => rawblkno then we should used that table.
    197      1.2      haad 	 */
    198      1.2      haad 	SLIST_FOREACH(table_en, tbl, next)
    199      1.2      haad 	    length += table_en->length;
    200      1.2      haad 
    201      1.2      haad 	dm_table_unbusy(head);
    202      1.5      haad 
    203      1.3  jakllsch 	return length;
    204      1.2      haad }
    205      1.2      haad /*
    206  1.5.4.1     rmind  * Return combined disk geometry
    207  1.5.4.1     rmind  */
    208  1.5.4.1     rmind void
    209  1.5.4.1     rmind dm_table_disksize(dm_table_head_t * head, uint64_t *numsecp, unsigned *secsizep)
    210  1.5.4.1     rmind {
    211  1.5.4.1     rmind 	dm_table_t *tbl;
    212  1.5.4.1     rmind 	dm_table_entry_t *table_en;
    213  1.5.4.1     rmind 	uint64_t length;
    214  1.5.4.1     rmind 	unsigned secsize, tsecsize;
    215  1.5.4.1     rmind 	uint8_t id;
    216  1.5.4.1     rmind 
    217  1.5.4.1     rmind 	length = 0;
    218  1.5.4.1     rmind 
    219  1.5.4.1     rmind 	id = dm_table_busy(head, DM_TABLE_ACTIVE);
    220  1.5.4.1     rmind 
    221  1.5.4.1     rmind 	/* Select active table */
    222  1.5.4.1     rmind 	tbl = &head->tables[id];
    223  1.5.4.1     rmind 
    224  1.5.4.1     rmind 	/*
    225  1.5.4.1     rmind 	 * Find out what tables I want to select.
    226  1.5.4.1     rmind 	 * if length => rawblkno then we should used that table.
    227  1.5.4.1     rmind 	 */
    228  1.5.4.1     rmind 	secsize = 0;
    229  1.5.4.1     rmind 	SLIST_FOREACH(table_en, tbl, next) {
    230  1.5.4.1     rmind 	    length += table_en->length;
    231  1.5.4.1     rmind 	    (void)table_en->target->secsize(table_en, &tsecsize);
    232  1.5.4.1     rmind 	    if (secsize < tsecsize)
    233  1.5.4.1     rmind 	    	secsize = tsecsize;
    234  1.5.4.1     rmind 	}
    235  1.5.4.1     rmind 	*numsecp = secsize > 0 ? dbtob(length) / secsize : 0;
    236  1.5.4.1     rmind 	*secsizep = secsize;
    237  1.5.4.1     rmind 
    238  1.5.4.1     rmind 	dm_table_unbusy(head);
    239  1.5.4.1     rmind }
    240  1.5.4.1     rmind /*
    241      1.2      haad  * Return > 0 if table is at least one table entry (returns number of entries)
    242      1.2      haad  * and return 0 if there is not. Target count returned from this function
    243      1.2      haad  * doesn't need to be true when userspace user receive it (after return
    244      1.2      haad  * there can be dm_dev_resume_ioctl), therfore this isonly informative.
    245      1.2      haad  */
    246      1.2      haad int
    247      1.5      haad dm_table_get_target_count(dm_table_head_t * head, uint8_t table_id)
    248      1.5      haad {
    249      1.2      haad 	dm_table_entry_t *table_en;
    250      1.2      haad 	dm_table_t *tbl;
    251      1.2      haad 	uint32_t target_count;
    252      1.2      haad 	uint8_t id;
    253      1.2      haad 
    254      1.2      haad 	target_count = 0;
    255      1.2      haad 
    256      1.2      haad 	id = dm_table_busy(head, table_id);
    257      1.2      haad 
    258      1.2      haad 	tbl = &head->tables[id];
    259      1.2      haad 
    260      1.2      haad 	SLIST_FOREACH(table_en, tbl, next)
    261      1.2      haad 	    target_count++;
    262      1.2      haad 
    263      1.2      haad 	dm_table_unbusy(head);
    264      1.5      haad 
    265      1.2      haad 	return target_count;
    266      1.2      haad }
    267      1.2      haad 
    268      1.2      haad 
    269      1.2      haad /*
    270      1.2      haad  * Initialize table_head structures, I'm trying to keep this structure as
    271      1.2      haad  * opaque as possible.
    272      1.2      haad  */
    273      1.2      haad void
    274      1.5      haad dm_table_head_init(dm_table_head_t * head)
    275      1.2      haad {
    276      1.2      haad 	head->cur_active_table = 0;
    277      1.2      haad 	head->io_cnt = 0;
    278      1.5      haad 
    279      1.2      haad 	/* Initialize tables. */
    280      1.2      haad 	SLIST_INIT(&head->tables[0]);
    281      1.2      haad 	SLIST_INIT(&head->tables[1]);
    282      1.2      haad 
    283      1.2      haad 	mutex_init(&head->table_mtx, MUTEX_DEFAULT, IPL_NONE);
    284      1.2      haad 	cv_init(&head->table_cv, "dm_io");
    285      1.2      haad }
    286      1.2      haad /*
    287      1.2      haad  * Destroy all variables in table_head
    288      1.2      haad  */
    289      1.2      haad void
    290      1.5      haad dm_table_head_destroy(dm_table_head_t * head)
    291      1.2      haad {
    292      1.2      haad 	KASSERT(!mutex_owned(&head->table_mtx));
    293      1.2      haad 	KASSERT(!cv_has_waiters(&head->table_cv));
    294      1.5      haad 	/* tables doens't exists when I call this routine, therefore it
    295      1.5      haad 	 * doesn't make sense to have io_cnt != 0 */
    296      1.5      haad 	KASSERT(head->io_cnt == 0);
    297      1.5      haad 
    298      1.2      haad 	cv_destroy(&head->table_cv);
    299      1.2      haad 	mutex_destroy(&head->table_mtx);
    300      1.2      haad }
    301