xref: /spdk/test/unit/lib/blob/bs_dev_common.c (revision 2bdec64fbfb636f60c0fc6deb704f43a24c3a1eb)
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 static struct spdk_io_channel *
40 dev_create_channel(struct spdk_bs_dev *dev)
41 {
42 	return NULL;
43 }
44 
45 static void
46 dev_destroy_channel(struct spdk_bs_dev *dev, struct spdk_io_channel *channel)
47 {
48 }
49 
50 static void
51 dev_destroy(struct spdk_bs_dev *dev)
52 {
53 }
54 
55 static void
56 dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
57 	 uint64_t lba, uint32_t lba_count,
58 	 struct spdk_bs_dev_cb_args *cb_args)
59 {
60 	uint64_t offset, length;
61 
62 	offset = lba * DEV_BUFFER_BLOCKLEN;
63 	length = lba_count * DEV_BUFFER_BLOCKLEN;
64 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
65 	memcpy(payload, &g_dev_buffer[offset], length);
66 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
67 }
68 
69 static void
70 dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
71 	  uint64_t lba, uint32_t lba_count,
72 	  struct spdk_bs_dev_cb_args *cb_args)
73 {
74 	uint64_t offset, length;
75 
76 	offset = lba * DEV_BUFFER_BLOCKLEN;
77 	length = lba_count * DEV_BUFFER_BLOCKLEN;
78 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
79 	memcpy(&g_dev_buffer[offset], payload, length);
80 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
81 }
82 
83 static void
84 dev_flush(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
85 	  struct spdk_bs_dev_cb_args *cb_args)
86 {
87 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
88 }
89 
90 static void
91 dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
92 	  uint64_t lba, uint32_t lba_count,
93 	  struct spdk_bs_dev_cb_args *cb_args)
94 {
95 	uint64_t offset, length;
96 
97 	offset = lba * DEV_BUFFER_BLOCKLEN;
98 	length = lba_count * DEV_BUFFER_BLOCKLEN;
99 	SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE);
100 	memset(&g_dev_buffer[offset], 0, length);
101 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0);
102 }
103 
104 static void
105 init_dev(struct spdk_bs_dev *dev)
106 {
107 	dev->create_channel = dev_create_channel;
108 	dev->destroy_channel = dev_destroy_channel;
109 	dev->destroy = dev_destroy;
110 	dev->read = dev_read;
111 	dev->write = dev_write;
112 	dev->flush = dev_flush;
113 	dev->unmap = dev_unmap;
114 	dev->blockcnt = DEV_BUFFER_BLOCKCNT;
115 	dev->blocklen = DEV_BUFFER_BLOCKLEN;
116 }
117