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