xref: /spdk/lib/blob/request.c (revision f86f10757912918b8ba7b4b3bfdab1cd4c2d180c)
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_LOG_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_LOG_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_LOG_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_LOG_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_LOG_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_LOG_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_write_zeroes(spdk_bs_sequence_t *seq,
231 			      uint64_t lba, uint32_t lba_count,
232 			      spdk_bs_sequence_cpl cb_fn, void *cb_arg)
233 {
234 	struct spdk_bs_request_set      *set = (struct spdk_bs_request_set *)seq;
235 	struct spdk_bs_channel       *channel = set->channel;
236 
237 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "writing zeroes to %u blocks at LBA %lu\n", lba_count, lba);
238 
239 	set->u.sequence.cb_fn = cb_fn;
240 	set->u.sequence.cb_arg = cb_arg;
241 
242 	channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count,
243 				   &set->cb_args);
244 }
245 
246 void
247 spdk_bs_sequence_finish(spdk_bs_sequence_t *seq, int bserrno)
248 {
249 	if (bserrno != 0) {
250 		seq->bserrno = bserrno;
251 	}
252 	spdk_bs_request_set_complete((struct spdk_bs_request_set *)seq);
253 }
254 
255 static void
256 spdk_bs_batch_completion(struct spdk_io_channel *_channel,
257 			 void *cb_arg, int bserrno)
258 {
259 	struct spdk_bs_request_set	*set = cb_arg;
260 
261 	set->u.batch.outstanding_ops--;
262 	if (bserrno != 0) {
263 		set->bserrno = bserrno;
264 	}
265 
266 	if (set->u.batch.outstanding_ops == 0 && set->u.batch.batch_closed) {
267 		if (set->u.batch.cb_fn) {
268 			set->cb_args.cb_fn = spdk_bs_sequence_completion;
269 			set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, bserrno);
270 		} else {
271 			spdk_bs_request_set_complete(set);
272 		}
273 	}
274 }
275 
276 spdk_bs_batch_t *
277 spdk_bs_batch_open(struct spdk_io_channel *_channel,
278 		   struct spdk_bs_cpl *cpl)
279 {
280 	struct spdk_bs_channel		*channel;
281 	struct spdk_bs_request_set	*set;
282 
283 	channel = spdk_io_channel_get_ctx(_channel);
284 
285 	set = TAILQ_FIRST(&channel->reqs);
286 	if (!set) {
287 		return NULL;
288 	}
289 	TAILQ_REMOVE(&channel->reqs, set, link);
290 
291 	set->cpl = *cpl;
292 	set->bserrno = 0;
293 	set->channel = channel;
294 
295 	set->u.batch.cb_fn = NULL;
296 	set->u.batch.cb_arg = NULL;
297 	set->u.batch.outstanding_ops = 0;
298 	set->u.batch.batch_closed = 0;
299 
300 	set->cb_args.cb_fn = spdk_bs_batch_completion;
301 	set->cb_args.cb_arg = set;
302 	set->cb_args.channel = channel->dev_channel;
303 
304 	return (spdk_bs_batch_t *)set;
305 }
306 
307 void
308 spdk_bs_batch_read(spdk_bs_batch_t *batch, void *payload,
309 		   uint64_t lba, uint32_t lba_count)
310 {
311 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
312 	struct spdk_bs_channel		*channel = set->channel;
313 
314 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba);
315 
316 	set->u.batch.outstanding_ops++;
317 	channel->dev->read(channel->dev, channel->dev_channel, payload, lba, lba_count,
318 			   &set->cb_args);
319 }
320 
321 void
322 spdk_bs_batch_write(spdk_bs_batch_t *batch, void *payload,
323 		    uint64_t lba, uint32_t lba_count)
324 {
325 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
326 	struct spdk_bs_channel		*channel = set->channel;
327 
328 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba);
329 
330 	set->u.batch.outstanding_ops++;
331 	channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count,
332 			    &set->cb_args);
333 }
334 
335 void
336 spdk_bs_batch_flush(spdk_bs_batch_t *batch)
337 {
338 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
339 	struct spdk_bs_channel		*channel = set->channel;
340 
341 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Flushing\n");
342 
343 	set->u.batch.outstanding_ops++;
344 	channel->dev->flush(channel->dev, channel->dev_channel,
345 			    &set->cb_args);
346 }
347 
348 void
349 spdk_bs_batch_unmap(spdk_bs_batch_t *batch,
350 		    uint64_t lba, uint32_t lba_count)
351 {
352 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
353 	struct spdk_bs_channel		*channel = set->channel;
354 
355 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba);
356 
357 	set->u.batch.outstanding_ops++;
358 	channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count,
359 			    &set->cb_args);
360 }
361 
362 void
363 spdk_bs_batch_write_zeroes(spdk_bs_batch_t *batch,
364 			   uint64_t lba, uint32_t lba_count)
365 {
366 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
367 	struct spdk_bs_channel		*channel = set->channel;
368 
369 	SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Zeroing %u blocks at LBA %lu\n", lba_count, lba);
370 
371 	set->u.batch.outstanding_ops++;
372 	channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count,
373 				   &set->cb_args);
374 }
375 
376 void
377 spdk_bs_batch_close(spdk_bs_batch_t *batch)
378 {
379 	struct spdk_bs_request_set	*set = (struct spdk_bs_request_set *)batch;
380 
381 	set->u.batch.batch_closed = 1;
382 
383 	if (set->u.batch.outstanding_ops == 0) {
384 		if (set->u.batch.cb_fn) {
385 			set->cb_args.cb_fn = spdk_bs_sequence_completion;
386 			set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, set->bserrno);
387 		} else {
388 			spdk_bs_request_set_complete(set);
389 		}
390 	}
391 }
392 
393 spdk_bs_batch_t *
394 spdk_bs_sequence_to_batch(spdk_bs_sequence_t *seq, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
395 {
396 	struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq;
397 
398 	set->u.batch.cb_fn = cb_fn;
399 	set->u.batch.cb_arg = cb_arg;
400 	set->u.batch.outstanding_ops = 0;
401 	set->u.batch.batch_closed = 0;
402 
403 	set->cb_args.cb_fn = spdk_bs_batch_completion;
404 
405 	return set;
406 }
407 
408 SPDK_LOG_REGISTER_COMPONENT("blob_rw", SPDK_LOG_BLOB_RW)
409