xref: /spdk/test/unit/lib/blob/bs_dev_common.c (revision b119facb65247c714030aa19f3f0528bcd28a834)
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 #define DEV_BUFFER_SIZE (64 * 1024 * 1024)
35 #define DEV_BUFFER_BLOCKLEN (4096)
36 #define DEV_BUFFER_BLOCKCNT (DEV_BUFFER_SIZE / DEV_BUFFER_BLOCKLEN)
37 uint8_t *g_dev_buffer;
38 
39 /* Define here for UT only. */
40 struct spdk_io_channel g_io_channel;
41 
42 static struct spdk_io_channel *
43 dev_create_channel(struct spdk_bs_dev *dev)
44 {
45 	return &g_io_channel;
46 }
47 
48 static void
49 dev_destroy_channel(struct spdk_bs_dev *dev, struct spdk_io_channel *channel)
50 {
51 }
52 
53 static void
54 dev_destroy(struct spdk_bs_dev *dev)
55 {
56 	free(dev);
57 }
58 
59 static void
60 dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
61 	 uint64_t lba, uint32_t lba_count,
62 	 struct spdk_bs_dev_cb_args *cb_args)
63 {
64 	uint64_t offset, length;
65 
66 	offset = lba * DEV_BUFFER_BLOCKLEN;
67 	length = lba_count * DEV_BUFFER_BLOCKLEN;
68 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
69 	memcpy(payload, &g_dev_buffer[offset], length);
70 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
71 }
72 
73 static void
74 dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
75 	  uint64_t lba, uint32_t lba_count,
76 	  struct spdk_bs_dev_cb_args *cb_args)
77 {
78 	uint64_t offset, length;
79 
80 	offset = lba * DEV_BUFFER_BLOCKLEN;
81 	length = lba_count * DEV_BUFFER_BLOCKLEN;
82 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
83 	memcpy(&g_dev_buffer[offset], payload, length);
84 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
85 }
86 
87 static void
88 __check_iov(struct iovec *iov, int iovcnt, uint64_t length)
89 {
90 	int i;
91 
92 	for (i = 0; i < iovcnt; i++) {
93 		length -= iov[i].iov_len;
94 	}
95 
96 	CU_ASSERT(length == 0);
97 }
98 
99 static void
100 dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
101 	  struct iovec *iov, int iovcnt,
102 	  uint64_t lba, uint32_t lba_count,
103 	  struct spdk_bs_dev_cb_args *cb_args)
104 {
105 	uint64_t offset, length;
106 	int i;
107 
108 	offset = lba * DEV_BUFFER_BLOCKLEN;
109 	length = lba_count * DEV_BUFFER_BLOCKLEN;
110 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
111 	__check_iov(iov, iovcnt, length);
112 
113 	for (i = 0; i < iovcnt; i++) {
114 		memcpy(iov[i].iov_base, &g_dev_buffer[offset], iov[i].iov_len);
115 		offset += iov[i].iov_len;
116 	}
117 
118 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
119 }
120 
121 static void
122 dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
123 	   struct iovec *iov, int iovcnt,
124 	   uint64_t lba, uint32_t lba_count,
125 	   struct spdk_bs_dev_cb_args *cb_args)
126 {
127 	uint64_t offset, length;
128 	int i;
129 
130 	offset = lba * DEV_BUFFER_BLOCKLEN;
131 	length = lba_count * DEV_BUFFER_BLOCKLEN;
132 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
133 	__check_iov(iov, iovcnt, length);
134 
135 	for (i = 0; i < iovcnt; i++) {
136 		memcpy(&g_dev_buffer[offset], iov[i].iov_base, iov[i].iov_len);
137 		offset += iov[i].iov_len;
138 	}
139 
140 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
141 }
142 
143 static void
144 dev_flush(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
145 	  struct spdk_bs_dev_cb_args *cb_args)
146 {
147 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
148 }
149 
150 static void
151 dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
152 	  uint64_t lba, uint32_t lba_count,
153 	  struct spdk_bs_dev_cb_args *cb_args)
154 {
155 	uint64_t offset, length;
156 
157 	offset = lba * DEV_BUFFER_BLOCKLEN;
158 	length = lba_count * DEV_BUFFER_BLOCKLEN;
159 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
160 	memset(&g_dev_buffer[offset], 0, length);
161 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
162 }
163 
164 static void
165 dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
166 		 uint64_t lba, uint32_t lba_count,
167 		 struct spdk_bs_dev_cb_args *cb_args)
168 {
169 	uint64_t offset, length;
170 
171 	offset = lba * DEV_BUFFER_BLOCKLEN;
172 	length = lba_count * DEV_BUFFER_BLOCKLEN;
173 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
174 	memset(&g_dev_buffer[offset], 0, length);
175 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
176 }
177 
178 static struct spdk_bs_dev *
179 init_dev(void)
180 {
181 	struct spdk_bs_dev *dev = calloc(1, sizeof(*dev));
182 
183 	SPDK_CU_ASSERT_FATAL(dev != NULL);
184 
185 	dev->create_channel = dev_create_channel;
186 	dev->destroy_channel = dev_destroy_channel;
187 	dev->destroy = dev_destroy;
188 	dev->read = dev_read;
189 	dev->write = dev_write;
190 	dev->readv = dev_readv;
191 	dev->writev = dev_writev;
192 	dev->flush = dev_flush;
193 	dev->unmap = dev_unmap;
194 	dev->write_zeroes = dev_write_zeroes;
195 	dev->blockcnt = DEV_BUFFER_BLOCKCNT;
196 	dev->blocklen = DEV_BUFFER_BLOCKLEN;
197 
198 	return dev;
199 }
200