xref: /spdk/lib/bdev/bdev_zone.c (revision 2f5c602574a98ede645991abe279a96e19c50196)
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/stdinc.h"
35 
36 #include "spdk/bdev_zone.h"
37 #include "spdk/bdev_module.h"
38 
39 #include "bdev_internal.h"
40 
41 uint64_t
42 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev)
43 {
44 	return bdev->zone_size;
45 }
46 
47 uint64_t
48 spdk_bdev_get_num_zones(const struct spdk_bdev *bdev)
49 {
50 	return bdev->zone_size ? bdev->blockcnt / bdev->zone_size : 0;
51 }
52 
53 uint32_t
54 spdk_bdev_get_max_zone_append_size(const struct spdk_bdev *bdev)
55 {
56 	return bdev->max_zone_append_size;
57 }
58 
59 uint32_t
60 spdk_bdev_get_max_open_zones(const struct spdk_bdev *bdev)
61 {
62 	return bdev->max_open_zones;
63 }
64 
65 uint32_t
66 spdk_bdev_get_max_active_zones(const struct spdk_bdev *bdev)
67 {
68 	return bdev->max_active_zones;
69 }
70 
71 uint32_t
72 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev)
73 {
74 	return bdev->optimal_open_zones;
75 }
76 
77 int
78 spdk_bdev_get_zone_info(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
79 			uint64_t zone_id, size_t num_zones, struct spdk_bdev_zone_info *info,
80 			spdk_bdev_io_completion_cb cb, void *cb_arg)
81 {
82 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
83 	struct spdk_bdev_io *bdev_io;
84 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
85 
86 	bdev_io = bdev_channel_get_io(channel);
87 	if (!bdev_io) {
88 		return -ENOMEM;
89 	}
90 
91 	bdev_io->internal.ch = channel;
92 	bdev_io->internal.desc = desc;
93 	bdev_io->type = SPDK_BDEV_IO_TYPE_GET_ZONE_INFO;
94 	bdev_io->u.zone_mgmt.zone_id = zone_id;
95 	bdev_io->u.zone_mgmt.num_zones = num_zones;
96 	bdev_io->u.zone_mgmt.buf = info;
97 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
98 
99 	bdev_io_submit(bdev_io);
100 	return 0;
101 }
102 
103 int
104 spdk_bdev_zone_management(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
105 			  uint64_t zone_id, enum spdk_bdev_zone_action action,
106 			  spdk_bdev_io_completion_cb cb, void *cb_arg)
107 {
108 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
109 	struct spdk_bdev_io *bdev_io;
110 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
111 
112 	bdev_io = bdev_channel_get_io(channel);
113 	if (!bdev_io) {
114 		return -ENOMEM;
115 	}
116 
117 	bdev_io->internal.ch = channel;
118 	bdev_io->internal.desc = desc;
119 	bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT;
120 	bdev_io->u.zone_mgmt.zone_action = action;
121 	bdev_io->u.zone_mgmt.zone_id = zone_id;
122 	bdev_io->u.zone_mgmt.num_zones = 1;
123 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
124 
125 	bdev_io_submit(bdev_io);
126 	return 0;
127 }
128 
129 static int
130 zone_bdev_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
131 			 void *buf, void *md_buf, uint64_t zone_id, uint64_t num_blocks,
132 			 spdk_bdev_io_completion_cb cb, void *cb_arg)
133 {
134 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
135 	struct spdk_bdev_io *bdev_io;
136 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
137 
138 	bdev_io = bdev_channel_get_io(channel);
139 	if (!bdev_io) {
140 		return -ENOMEM;
141 	}
142 
143 	bdev_io->internal.ch = channel;
144 	bdev_io->internal.desc = desc;
145 	bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND;
146 	bdev_io->u.bdev.iovs = &bdev_io->iov;
147 	bdev_io->u.bdev.iovs[0].iov_base = buf;
148 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
149 	bdev_io->u.bdev.iovcnt = 1;
150 	bdev_io->u.bdev.md_buf = md_buf;
151 	bdev_io->u.bdev.num_blocks = num_blocks;
152 	bdev_io->u.bdev.offset_blocks = zone_id;
153 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
154 
155 	bdev_io_submit(bdev_io);
156 	return 0;
157 }
158 
159 int
160 spdk_bdev_zone_append(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
161 		      void *buf, uint64_t start_lba, uint64_t num_blocks,
162 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
163 {
164 	return zone_bdev_append_with_md(desc, ch, buf, NULL, start_lba, num_blocks,
165 					cb, cb_arg);
166 }
167 
168 int
169 spdk_bdev_zone_append_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
170 			      void *buf, void *md, uint64_t start_lba, uint64_t num_blocks,
171 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
172 {
173 	return zone_bdev_append_with_md(desc, ch, buf, md, start_lba, num_blocks,
174 					cb, cb_arg);
175 }
176 
177 int
178 spdk_bdev_zone_appendv_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
179 			       struct iovec *iov, int iovcnt, void *md_buf, uint64_t zone_id,
180 			       uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
181 			       void *cb_arg)
182 {
183 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
184 	struct spdk_bdev_io *bdev_io;
185 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
186 
187 	bdev_io = bdev_channel_get_io(channel);
188 	if (!bdev_io) {
189 		return -ENOMEM;
190 	}
191 
192 	bdev_io->internal.ch = channel;
193 	bdev_io->internal.desc = desc;
194 	bdev_io->type = SPDK_BDEV_IO_TYPE_ZONE_APPEND;
195 	bdev_io->u.bdev.iovs = iov;
196 	bdev_io->u.bdev.iovcnt = iovcnt;
197 	bdev_io->u.bdev.md_buf = md_buf;
198 	bdev_io->u.bdev.num_blocks = num_blocks;
199 	bdev_io->u.bdev.offset_blocks = zone_id;
200 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
201 
202 	bdev_io_submit(bdev_io);
203 	return 0;
204 }
205 
206 int
207 spdk_bdev_zone_appendv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
208 		       struct iovec *iovs, int iovcnt, uint64_t zone_id, uint64_t num_blocks,
209 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
210 {
211 	return spdk_bdev_zone_appendv_with_md(desc, ch, iovs, iovcnt, NULL, zone_id, num_blocks,
212 					      cb, cb_arg);
213 }
214 
215 uint64_t
216 spdk_bdev_io_get_append_location(struct spdk_bdev_io *bdev_io)
217 {
218 	return bdev_io->u.bdev.offset_blocks;
219 }
220