xref: /spdk/test/unit/lib/ftl/common/utils.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/ftl.h"
35 #include "ftl/ftl_core.h"
36 #include "thread/thread_internal.h"
37 
38 struct base_bdev_geometry {
39 	size_t write_unit_size;
40 	size_t zone_size;
41 	size_t optimal_open_zones;
42 	size_t blockcnt;
43 };
44 
45 extern struct base_bdev_geometry g_geo;
46 
47 struct spdk_ftl_dev *test_init_ftl_dev(const struct base_bdev_geometry *geo);
48 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size);
49 void test_free_ftl_dev(struct spdk_ftl_dev *dev);
50 void test_free_ftl_band(struct ftl_band *band);
51 uint64_t test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band);
52 
53 DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL);
54 
55 uint64_t
56 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev)
57 {
58 	return g_geo.zone_size;
59 }
60 
61 uint32_t
62 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev)
63 {
64 	return g_geo.optimal_open_zones;
65 }
66 
67 struct spdk_ftl_dev *
68 test_init_ftl_dev(const struct base_bdev_geometry *geo)
69 {
70 	struct spdk_ftl_dev *dev;
71 
72 	dev = calloc(1, sizeof(*dev));
73 	SPDK_CU_ASSERT_FATAL(dev != NULL);
74 
75 	dev->xfer_size = geo->write_unit_size;
76 	dev->core_thread = spdk_thread_create("unit_test_thread", NULL);
77 	spdk_set_thread(dev->core_thread);
78 	dev->ioch = calloc(1, sizeof(*dev->ioch)
79 			   + sizeof(struct ftl_io_channel *));
80 	dev->num_bands = geo->blockcnt / (geo->zone_size * geo->optimal_open_zones);
81 	dev->bands = calloc(dev->num_bands, sizeof(*dev->bands));
82 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
83 
84 	dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000,
85 					    SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
86 					    SPDK_ENV_SOCKET_ID_ANY);
87 	SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL);
88 
89 	LIST_INIT(&dev->free_bands);
90 	LIST_INIT(&dev->shut_bands);
91 
92 	return dev;
93 }
94 
95 struct ftl_band *
96 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size)
97 {
98 	struct ftl_band *band;
99 	struct ftl_zone *zone;
100 
101 	SPDK_CU_ASSERT_FATAL(dev != NULL);
102 	SPDK_CU_ASSERT_FATAL(id < dev->num_bands);
103 
104 	band = &dev->bands[id];
105 	band->dev = dev;
106 	band->id = id;
107 
108 	band->state = FTL_BAND_STATE_CLOSED;
109 	LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry);
110 	CIRCLEQ_INIT(&band->zones);
111 
112 	band->lba_map.vld = spdk_bit_array_create(ftl_get_num_blocks_in_band(dev));
113 	SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL);
114 
115 	band->zone_buf = calloc(ftl_get_num_punits(dev), sizeof(*band->zone_buf));
116 	SPDK_CU_ASSERT_FATAL(band->zone_buf != NULL);
117 
118 	band->reloc_bitmap = spdk_bit_array_create(ftl_get_num_bands(dev));
119 	SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL);
120 
121 	for (size_t i = 0; i < ftl_get_num_punits(dev); ++i) {
122 		zone = &band->zone_buf[i];
123 		zone->info.state = SPDK_BDEV_ZONE_STATE_FULL;
124 		zone->info.zone_id = zone_size * (id * ftl_get_num_punits(dev) + i);
125 		CIRCLEQ_INSERT_TAIL(&band->zones, zone, circleq);
126 		band->num_zones++;
127 	}
128 
129 	pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE);
130 	return band;
131 }
132 
133 void
134 test_free_ftl_dev(struct spdk_ftl_dev *dev)
135 {
136 	struct spdk_thread *thread;
137 
138 	SPDK_CU_ASSERT_FATAL(dev != NULL);
139 	free(dev->ioch);
140 
141 	thread = dev->core_thread;
142 
143 	spdk_set_thread(thread);
144 	spdk_thread_exit(thread);
145 	while (!spdk_thread_is_exited(thread)) {
146 		spdk_thread_poll(thread, 0, 0);
147 	}
148 	spdk_thread_destroy(thread);
149 	spdk_mempool_free(dev->lba_pool);
150 	free(dev->bands);
151 	free(dev);
152 }
153 
154 void
155 test_free_ftl_band(struct ftl_band *band)
156 {
157 	SPDK_CU_ASSERT_FATAL(band != NULL);
158 	spdk_bit_array_free(&band->lba_map.vld);
159 	spdk_bit_array_free(&band->reloc_bitmap);
160 	free(band->zone_buf);
161 	spdk_dma_free(band->lba_map.dma_buf);
162 }
163 
164 uint64_t
165 test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band)
166 {
167 	struct spdk_ftl_dev *dev = band->dev;
168 
169 	CU_ASSERT_EQUAL(ftl_addr_get_band(dev, addr), band->id);
170 
171 	return addr.offset - band->id * ftl_get_num_blocks_in_band(dev);
172 }
173