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