xref: /spdk/lib/blob/request.c (revision eb8b1e20a9c8a6bc79f32fde8693d2791a74c34d)
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 "blobstore.h"
37 #include "request.h"
38 
39 #include "spdk/io_channel.h"
40 #include "spdk/queue.h"
41 
42 #include "spdk_internal/log.h"
43 
44 void
45 spdk_bs_call_cpl(struct spdk_bs_cpl *cpl, int bserrno)
46 {
47 	switch (cpl->type) {
48 	case SPDK_BS_CPL_TYPE_BS_BASIC:
49 		cpl->u.bs_basic.cb_fn(cpl->u.bs_basic.cb_arg,
50 				      bserrno);
51 		break;
52 	case SPDK_BS_CPL_TYPE_BS_HANDLE:
53 		cpl->u.bs_handle.cb_fn(cpl->u.bs_handle.cb_arg,
54 				       cpl->u.bs_handle.bs,
55 				       bserrno);
56 		break;
57 	case SPDK_BS_CPL_TYPE_BLOB_BASIC:
58 		cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg,
59 					bserrno);
60 		break;
61 	case SPDK_BS_CPL_TYPE_BLOBID:
62 		cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg,
63 				    cpl->u.blobid.blobid,
64 				    bserrno);
65 		break;
66 	case SPDK_BS_CPL_TYPE_BLOB_HANDLE:
67 		cpl->u.blob_handle.cb_fn(cpl->u.blob_handle.cb_arg,
68 					 cpl->u.blob_handle.blob,
69 					 bserrno);
70 		break;
71 	case SPDK_BS_CPL_TYPE_NESTED_SEQUENCE:
72 		cpl->u.nested_seq.cb_fn(cpl->u.nested_seq.cb_arg,
73 					cpl->u.nested_seq.parent,
74 					bserrno);
75 		break;
76 	case SPDK_BS_CPL_TYPE_NONE:
77 		/* this completion's callback is handled elsewhere */
78 		break;
79 	}
80 }
81 
82 static void
83 spdk_bs_request_set_complete(struct spdk_bs_request_set *set)
84 {
85 	struct spdk_bs_cpl cpl = set->cpl;
86 	int bserrno = set->bserrno;
87 
88 	TAILQ_INSERT_TAIL(&set->channel->reqs, set, link);
89 
90 	spdk_bs_call_cpl(&cpl, bserrno);
91 }
92 
93 static void
94 spdk_bs_sequence_completion(struct spdk_io_channel *channel, void *cb_arg, int bserrno)
95 {
96 	struct spdk_bs_request_set *set = cb_arg;
97 
98 	set->bserrno = bserrno;
99 	set->u.sequence.cb_fn((spdk_bs_sequence_t *)set, set->u.sequence.cb_arg, bserrno);
100 }
101 
102 spdk_bs_sequence_t *
103 spdk_bs_sequence_start(struct spdk_io_channel *_channel,
104 		       struct spdk_bs_cpl *cpl)
105 {
106 	struct spdk_bs_channel		*channel;
107 	struct spdk_bs_request_set	*set;
108 
109 	channel = spdk_io_channel_get_ctx(_channel);
110 
111 	set = TAILQ_FIRST(&channel->reqs);
112 	if (!set) {
113 		return NULL;
114 	}
115 	TAILQ_REMOVE(&channel->reqs, set, link);
116 
117 	set->cpl = *cpl;
118 	set->bserrno = 0;
119 	set->channel = channel;
120 
121 	set->cb_args.cb_fn = spdk_bs_sequence_completion;
122 	set->cb_args.cb_arg = set;
123 	set->cb_args.channel = channel->dev_channel;
124 
125 	return (spdk_bs_sequence_t *)set;
126 }
127 
128 void
129 spdk_bs_sequence_read(spdk_bs_sequence_t *seq, void *payload,
130 		      uint64_t lba, uint32_t lba_count,
131 		      spdk_bs_sequence_cpl cb_fn, void *cb_arg)
132 {
133 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
134 	struct spdk_bs_channel       *channel = set->channel;
135 
136 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba);
137 
138 	set->u.sequence.cb_fn = cb_fn;
139 	set->u.sequence.cb_arg = cb_arg;
140 
141 	channel->dev->read(channel->dev, channel->dev_channel, payload, lba, lba_count,
142 			   &set->cb_args);
143 }
144 
145 void
146 spdk_bs_sequence_write(spdk_bs_sequence_t *seq, void *payload,
147 		       uint64_t lba, uint32_t lba_count,
148 		       spdk_bs_sequence_cpl cb_fn, void *cb_arg)
149 {
150 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
151 	struct spdk_bs_channel       *channel = set->channel;
152 
153 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba);
154 
155 	set->u.sequence.cb_fn = cb_fn;
156 	set->u.sequence.cb_arg = cb_arg;
157 
158 	channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count,
159 			    &set->cb_args);
160 }
161 
162 void
163 spdk_bs_sequence_readv(spdk_bs_sequence_t *seq, struct iovec *iov, int iovcnt,
164 		       uint64_t lba, uint32_t lba_count,
165 		       spdk_bs_sequence_cpl cb_fn, void *cb_arg)
166 {
167 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
168 	struct spdk_bs_channel       *channel = set->channel;
169 
170 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba);
171 
172 	set->u.sequence.cb_fn = cb_fn;
173 	set->u.sequence.cb_arg = cb_arg;
174 
175 	channel->dev->readv(channel->dev, channel->dev_channel, iov, iovcnt, lba, lba_count,
176 			    &set->cb_args);
177 }
178 
179 void
180 spdk_bs_sequence_writev(spdk_bs_sequence_t *seq, struct iovec *iov, int iovcnt,
181 			uint64_t lba, uint32_t lba_count,
182 			spdk_bs_sequence_cpl cb_fn, void *cb_arg)
183 {
184 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
185 	struct spdk_bs_channel       *channel = set->channel;
186 
187 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba);
188 
189 	set->u.sequence.cb_fn = cb_fn;
190 	set->u.sequence.cb_arg = cb_arg;
191 
192 	channel->dev->writev(channel->dev, channel->dev_channel, iov, iovcnt, lba, lba_count,
193 			     &set->cb_args);
194 }
195 
196 void
197 spdk_bs_sequence_flush(spdk_bs_sequence_t *seq,
198 		       spdk_bs_sequence_cpl cb_fn, void *cb_arg)
199 {
200 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
201 	struct spdk_bs_channel       *channel = set->channel;
202 
203 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Flushing\n");
204 
205 	set->u.sequence.cb_fn = cb_fn;
206 	set->u.sequence.cb_arg = cb_arg;
207 
208 	channel->dev->flush(channel->dev, channel->dev_channel,
209 			    &set->cb_args);
210 }
211 
212 void
213 spdk_bs_sequence_unmap(spdk_bs_sequence_t *seq,
214 		       uint64_t lba, uint32_t lba_count,
215 		       spdk_bs_sequence_cpl cb_fn, void *cb_arg)
216 {
217 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
218 	struct spdk_bs_channel       *channel = set->channel;
219 
220 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba);
221 
222 	set->u.sequence.cb_fn = cb_fn;
223 	set->u.sequence.cb_arg = cb_arg;
224 
225 	channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count,
226 			    &set->cb_args);
227 }
228 
229 void
230 spdk_bs_sequence_finish(spdk_bs_sequence_t *seq, int bserrno)
231 {
232 	if (bserrno != 0) {
233 		seq->bserrno = bserrno;
234 	}
235 	spdk_bs_request_set_complete((struct spdk_bs_request_set *)seq);
236 }
237 
238 static void
239 spdk_bs_batch_completion(struct spdk_io_channel *_channel,
240 			 void *cb_arg, int bserrno)
241 {
242 	struct spdk_bs_request_set	*set = cb_arg;
243 
244 	set->u.batch.outstanding_ops--;
245 	if (bserrno != 0) {
246 		set->bserrno = bserrno;
247 	}
248 
249 	if (set->u.batch.outstanding_ops == 0 && set->u.batch.batch_closed) {
250 		if (set->u.batch.cb_fn) {
251 			set->cb_args.cb_fn = spdk_bs_sequence_completion;
252 			set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, bserrno);
253 		} else {
254 			spdk_bs_request_set_complete(set);
255 		}
256 	}
257 }
258 
259 spdk_bs_batch_t *
260 spdk_bs_batch_open(struct spdk_io_channel *_channel,
261 		   struct spdk_bs_cpl *cpl)
262 {
263 	struct spdk_bs_channel		*channel;
264 	struct spdk_bs_request_set	*set;
265 
266 	channel = spdk_io_channel_get_ctx(_channel);
267 
268 	set = TAILQ_FIRST(&channel->reqs);
269 	if (!set) {
270 		return NULL;
271 	}
272 	TAILQ_REMOVE(&channel->reqs, set, link);
273 
274 	set->cpl = *cpl;
275 	set->bserrno = 0;
276 	set->channel = channel;
277 
278 	set->u.batch.cb_fn = NULL;
279 	set->u.batch.cb_arg = NULL;
280 	set->u.batch.outstanding_ops = 0;
281 	set->u.batch.batch_closed = 0;
282 
283 	set->cb_args.cb_fn = spdk_bs_batch_completion;
284 	set->cb_args.cb_arg = set;
285 	set->cb_args.channel = channel->dev_channel;
286 
287 	return (spdk_bs_batch_t *)set;
288 }
289 
290 void
291 spdk_bs_batch_read(spdk_bs_batch_t *batch, void *payload,
292 		   uint64_t lba, uint32_t lba_count)
293 {
294 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
295 	struct spdk_bs_channel		*channel = set->channel;
296 
297 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba);
298 
299 	set->u.batch.outstanding_ops++;
300 	channel->dev->read(channel->dev, channel->dev_channel, payload, lba, lba_count,
301 			   &set->cb_args);
302 }
303 
304 void
305 spdk_bs_batch_write(spdk_bs_batch_t *batch, void *payload,
306 		    uint64_t lba, uint32_t lba_count)
307 {
308 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
309 	struct spdk_bs_channel		*channel = set->channel;
310 
311 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba);
312 
313 	set->u.batch.outstanding_ops++;
314 	channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count,
315 			    &set->cb_args);
316 }
317 
318 void
319 spdk_bs_batch_flush(spdk_bs_batch_t *batch)
320 {
321 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
322 	struct spdk_bs_channel		*channel = set->channel;
323 
324 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Flushing\n");
325 
326 	set->u.batch.outstanding_ops++;
327 	channel->dev->flush(channel->dev, channel->dev_channel,
328 			    &set->cb_args);
329 }
330 
331 void
332 spdk_bs_batch_unmap(spdk_bs_batch_t *batch,
333 		    uint64_t lba, uint32_t lba_count)
334 {
335 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
336 	struct spdk_bs_channel		*channel = set->channel;
337 
338 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba);
339 
340 	set->u.batch.outstanding_ops++;
341 	channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count,
342 			    &set->cb_args);
343 }
344 
345 void
346 spdk_bs_batch_close(spdk_bs_batch_t *batch)
347 {
348 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
349 
350 	set->u.batch.batch_closed = 1;
351 
352 	if (set->u.batch.outstanding_ops == 0) {
353 		if (set->u.batch.cb_fn) {
354 			set->cb_args.cb_fn = spdk_bs_sequence_completion;
355 			set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, set->bserrno);
356 		} else {
357 			spdk_bs_request_set_complete(set);
358 		}
359 	}
360 }
361 
362 spdk_bs_batch_t *
363 spdk_bs_sequence_to_batch(spdk_bs_sequence_t *seq, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
364 {
365 	struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq;
366 
367 	set->u.batch.cb_fn = cb_fn;
368 	set->u.batch.cb_arg = cb_arg;
369 	set->u.batch.outstanding_ops = 0;
370 	set->u.batch.batch_closed = 0;
371 
372 	set->cb_args.cb_fn = spdk_bs_batch_completion;
373 
374 	return set;
375 }
376 
377 SPDK_LOG_REGISTER_TRACE_FLAG("blob_rw", SPDK_TRACE_BLOB_RW);
378