]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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.h" | |
37 | #include "spdk/conf.h" | |
38 | #include "spdk/env.h" | |
39 | #include "spdk/fd.h" | |
40 | #include "spdk/thread.h" | |
41 | #include "spdk/json.h" | |
42 | #include "spdk/util.h" | |
43 | #include "spdk/rpc.h" | |
44 | #include "spdk/string.h" | |
45 | #include "spdk/iscsi_spec.h" | |
46 | ||
47 | #include "spdk_internal/log.h" | |
48 | #include "spdk/bdev_module.h" | |
49 | ||
50 | #include "iscsi/iscsi.h" | |
51 | #include "iscsi/scsi-lowlevel.h" | |
52 | ||
53 | #include "bdev_iscsi.h" | |
54 | ||
55 | struct bdev_iscsi_lun; | |
56 | ||
57 | #define BDEV_ISCSI_CONNECTION_POLL_US 500 /* 0.5 ms */ | |
58 | #define BDEV_ISCSI_NO_MASTER_CH_POLL_US 10000 /* 10ms */ | |
59 | ||
60 | #define DEFAULT_INITIATOR_NAME "iqn.2016-06.io.spdk:init" | |
61 | ||
62 | static int bdev_iscsi_initialize(void); | |
63 | static TAILQ_HEAD(, bdev_iscsi_conn_req) g_iscsi_conn_req = TAILQ_HEAD_INITIALIZER( | |
64 | g_iscsi_conn_req); | |
65 | static struct spdk_poller *g_conn_poller = NULL; | |
66 | ||
67 | struct bdev_iscsi_io { | |
68 | struct spdk_thread *submit_td; | |
69 | enum spdk_bdev_io_status status; | |
70 | int scsi_status; | |
71 | enum spdk_scsi_sense sk; | |
72 | uint8_t asc; | |
73 | uint8_t ascq; | |
74 | }; | |
75 | ||
76 | struct bdev_iscsi_lun { | |
77 | struct spdk_bdev bdev; | |
78 | struct iscsi_context *context; | |
79 | char *initiator_iqn; | |
9f95a23c | 80 | int lun_id; |
11fdf7f2 TL |
81 | char *url; |
82 | pthread_mutex_t mutex; | |
83 | uint32_t ch_count; | |
11fdf7f2 TL |
84 | struct spdk_thread *master_td; |
85 | struct spdk_poller *no_master_ch_poller; | |
86 | struct spdk_thread *no_master_ch_poller_td; | |
87 | bool unmap_supported; | |
9f95a23c | 88 | struct spdk_poller *poller; |
11fdf7f2 TL |
89 | }; |
90 | ||
91 | struct bdev_iscsi_io_channel { | |
11fdf7f2 TL |
92 | struct bdev_iscsi_lun *lun; |
93 | }; | |
94 | ||
95 | struct bdev_iscsi_conn_req { | |
96 | char *url; | |
97 | char *bdev_name; | |
98 | char *initiator_iqn; | |
99 | struct iscsi_context *context; | |
100 | spdk_bdev_iscsi_create_cb create_cb; | |
101 | spdk_bdev_iscsi_create_cb create_cb_arg; | |
102 | bool unmap_supported; | |
9f95a23c | 103 | int lun; |
11fdf7f2 TL |
104 | TAILQ_ENTRY(bdev_iscsi_conn_req) link; |
105 | }; | |
106 | ||
107 | static void | |
108 | complete_conn_req(struct bdev_iscsi_conn_req *req, struct spdk_bdev *bdev, | |
109 | int status) | |
110 | { | |
111 | TAILQ_REMOVE(&g_iscsi_conn_req, req, link); | |
112 | req->create_cb(req->create_cb_arg, bdev, status); | |
113 | if (status) { | |
114 | /* if the request failed and no iscsi lun was | |
115 | * created then we could not hand over this | |
116 | * memory and have to free it manually now. | |
117 | */ | |
118 | iscsi_destroy_context(req->context); | |
119 | free(req->initiator_iqn); | |
120 | free(req->bdev_name); | |
121 | free(req->url); | |
122 | } | |
123 | free(req); | |
124 | } | |
125 | ||
126 | static int | |
127 | bdev_iscsi_get_ctx_size(void) | |
128 | { | |
129 | return sizeof(struct bdev_iscsi_io); | |
130 | } | |
131 | ||
132 | static void | |
133 | _iscsi_free_lun(void *arg) | |
134 | { | |
135 | struct bdev_iscsi_lun *lun = arg; | |
136 | ||
137 | assert(lun != NULL); | |
138 | iscsi_destroy_context(lun->context); | |
139 | pthread_mutex_destroy(&lun->mutex); | |
140 | free(lun->bdev.name); | |
141 | free(lun->url); | |
142 | free(lun->initiator_iqn); | |
143 | ||
144 | spdk_bdev_destruct_done(&lun->bdev, 0); | |
145 | free(lun); | |
146 | } | |
147 | ||
148 | static void | |
149 | bdev_iscsi_finish(void) | |
150 | { | |
151 | struct bdev_iscsi_conn_req *req; | |
152 | ||
153 | while (!TAILQ_EMPTY(&g_iscsi_conn_req)) { | |
154 | req = TAILQ_FIRST(&g_iscsi_conn_req); | |
155 | complete_conn_req(req, NULL, -EINTR); | |
156 | } | |
157 | ||
158 | if (g_conn_poller) { | |
159 | spdk_poller_unregister(&g_conn_poller); | |
160 | } | |
161 | } | |
162 | ||
163 | static struct spdk_bdev_module g_iscsi_bdev_module = { | |
164 | .name = "iscsi", | |
165 | .module_init = bdev_iscsi_initialize, | |
166 | .module_fini = bdev_iscsi_finish, | |
167 | .get_ctx_size = bdev_iscsi_get_ctx_size, | |
168 | .async_init = true, | |
169 | }; | |
170 | ||
9f95a23c | 171 | SPDK_BDEV_MODULE_REGISTER(iscsi, &g_iscsi_bdev_module); |
11fdf7f2 TL |
172 | |
173 | static void | |
174 | _bdev_iscsi_io_complete(void *_iscsi_io) | |
175 | { | |
176 | struct bdev_iscsi_io *iscsi_io = _iscsi_io; | |
177 | ||
178 | if (iscsi_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) { | |
179 | spdk_bdev_io_complete_scsi_status(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->scsi_status, | |
180 | iscsi_io->sk, iscsi_io->asc, iscsi_io->ascq); | |
181 | } else { | |
182 | spdk_bdev_io_complete(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->status); | |
183 | } | |
184 | } | |
185 | ||
186 | static void | |
187 | bdev_iscsi_io_complete(struct bdev_iscsi_io *iscsi_io, enum spdk_bdev_io_status status) | |
188 | { | |
189 | iscsi_io->status = status; | |
190 | if (iscsi_io->submit_td != NULL) { | |
191 | spdk_thread_send_msg(iscsi_io->submit_td, _bdev_iscsi_io_complete, iscsi_io); | |
192 | } else { | |
193 | _bdev_iscsi_io_complete(iscsi_io); | |
194 | } | |
195 | } | |
196 | ||
197 | /* Common call back function for read/write/flush command */ | |
198 | static void | |
199 | bdev_iscsi_command_cb(struct iscsi_context *context, int status, void *_task, void *_iscsi_io) | |
200 | { | |
201 | struct scsi_task *task = _task; | |
202 | struct bdev_iscsi_io *iscsi_io = _iscsi_io; | |
203 | ||
204 | iscsi_io->scsi_status = status; | |
205 | iscsi_io->sk = (uint8_t)task->sense.key; | |
206 | iscsi_io->asc = (task->sense.ascq >> 8) & 0xFF; | |
207 | iscsi_io->ascq = task->sense.ascq & 0xFF; | |
208 | ||
209 | scsi_free_scsi_task(task); | |
210 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); | |
211 | } | |
212 | ||
213 | static void | |
214 | bdev_iscsi_readv(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, | |
215 | struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) | |
216 | { | |
217 | struct scsi_task *task; | |
218 | ||
219 | SPDK_DEBUGLOG(SPDK_LOG_ISCSI_INIT, "read %d iovs size %lu to lba: %#lx\n", | |
220 | iovcnt, nbytes, lba); | |
221 | ||
9f95a23c | 222 | task = iscsi_read16_task(lun->context, lun->lun_id, lba, nbytes, lun->bdev.blocklen, 0, 0, 0, 0, 0, |
11fdf7f2 TL |
223 | bdev_iscsi_command_cb, iscsi_io); |
224 | if (task == NULL) { | |
225 | SPDK_ERRLOG("failed to get read16_task\n"); | |
226 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
227 | return; | |
228 | } | |
229 | ||
230 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
231 | scsi_task_set_iov_in(task, (struct scsi_iovec *)iov, iovcnt); | |
232 | #else | |
233 | int i; | |
234 | for (i = 0; i < iovcnt; i++) { | |
235 | scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); | |
236 | } | |
237 | #endif | |
238 | } | |
239 | ||
240 | static void | |
241 | bdev_iscsi_writev(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, | |
242 | struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) | |
243 | { | |
244 | struct scsi_task *task; | |
245 | ||
246 | SPDK_DEBUGLOG(SPDK_LOG_ISCSI_INIT, "write %d iovs size %lu to lba: %#lx\n", | |
247 | iovcnt, nbytes, lba); | |
248 | ||
9f95a23c TL |
249 | task = iscsi_write16_task(lun->context, lun->lun_id, lba, NULL, nbytes, lun->bdev.blocklen, 0, 0, 0, |
250 | 0, 0, | |
11fdf7f2 TL |
251 | bdev_iscsi_command_cb, iscsi_io); |
252 | if (task == NULL) { | |
253 | SPDK_ERRLOG("failed to get write16_task\n"); | |
254 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
255 | return; | |
256 | } | |
257 | ||
258 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
259 | scsi_task_set_iov_out(task, (struct scsi_iovec *)iov, iovcnt); | |
260 | #else | |
261 | int i; | |
262 | for (i = 0; i < iovcnt; i++) { | |
263 | scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); | |
264 | } | |
265 | #endif | |
266 | } | |
267 | ||
268 | static void | |
269 | bdev_iscsi_destruct_cb(void *ctx) | |
270 | { | |
271 | struct bdev_iscsi_lun *lun = ctx; | |
272 | ||
273 | spdk_poller_unregister(&lun->no_master_ch_poller); | |
274 | spdk_io_device_unregister(lun, _iscsi_free_lun); | |
275 | } | |
276 | ||
277 | static int | |
278 | bdev_iscsi_destruct(void *ctx) | |
279 | { | |
280 | struct bdev_iscsi_lun *lun = ctx; | |
281 | ||
282 | assert(lun->no_master_ch_poller_td); | |
283 | spdk_thread_send_msg(lun->no_master_ch_poller_td, bdev_iscsi_destruct_cb, lun); | |
284 | return 1; | |
285 | } | |
286 | ||
287 | static void | |
288 | bdev_iscsi_flush(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, uint32_t num_blocks, | |
289 | int immed, uint64_t lba) | |
290 | { | |
291 | struct scsi_task *task; | |
292 | ||
9f95a23c | 293 | task = iscsi_synchronizecache16_task(lun->context, lun->lun_id, lba, |
11fdf7f2 TL |
294 | num_blocks, 0, immed, bdev_iscsi_command_cb, iscsi_io); |
295 | if (task == NULL) { | |
296 | SPDK_ERRLOG("failed to get sync16_task\n"); | |
297 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
298 | return; | |
299 | } | |
300 | } | |
301 | ||
302 | static void | |
303 | bdev_iscsi_unmap(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, | |
304 | uint64_t lba, uint64_t num_blocks) | |
305 | { | |
306 | struct scsi_task *task; | |
307 | struct unmap_list list[1]; | |
308 | ||
309 | list[0].lba = lba; | |
310 | list[0].num = num_blocks; | |
311 | task = iscsi_unmap_task(lun->context, 0, 0, 0, list, 1, | |
312 | bdev_iscsi_command_cb, iscsi_io); | |
313 | if (task == NULL) { | |
314 | SPDK_ERRLOG("failed to get unmap_task\n"); | |
315 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
316 | return; | |
317 | } | |
318 | } | |
319 | ||
320 | static void | |
321 | bdev_iscsi_reset_cb(struct iscsi_context *context __attribute__((unused)), int status, | |
322 | void *command_data, void *private_data) | |
323 | { | |
324 | uint32_t tmf_response; | |
325 | struct bdev_iscsi_io *iscsi_io = private_data; | |
326 | ||
327 | tmf_response = *(uint32_t *)command_data; | |
328 | if (tmf_response == ISCSI_TASK_FUNC_RESP_COMPLETE) { | |
329 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); | |
330 | } else { | |
331 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
332 | } | |
333 | } | |
334 | ||
335 | static void | |
336 | _bdev_iscsi_reset(void *_bdev_io) | |
337 | { | |
338 | int rc; | |
339 | struct spdk_bdev_io *bdev_io = _bdev_io; | |
340 | struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; | |
341 | struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; | |
342 | struct iscsi_context *context = lun->context; | |
343 | ||
9f95a23c | 344 | rc = iscsi_task_mgmt_lun_reset_async(context, lun->lun_id, |
11fdf7f2 TL |
345 | bdev_iscsi_reset_cb, iscsi_io); |
346 | if (rc != 0) { | |
347 | SPDK_ERRLOG("failed to do iscsi reset\n"); | |
348 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
349 | return; | |
350 | } | |
351 | } | |
352 | ||
353 | static void | |
354 | bdev_iscsi_reset(struct spdk_bdev_io *bdev_io) | |
355 | { | |
356 | struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; | |
357 | spdk_thread_send_msg(lun->master_td, _bdev_iscsi_reset, bdev_io); | |
358 | } | |
359 | ||
360 | static int | |
9f95a23c | 361 | bdev_iscsi_poll_lun(void *_lun) |
11fdf7f2 | 362 | { |
9f95a23c | 363 | struct bdev_iscsi_lun *lun = _lun; |
11fdf7f2 TL |
364 | struct pollfd pfd = {}; |
365 | ||
366 | pfd.fd = iscsi_get_fd(lun->context); | |
367 | pfd.events = iscsi_which_events(lun->context); | |
368 | ||
369 | if (poll(&pfd, 1, 0) < 0) { | |
370 | SPDK_ERRLOG("poll failed\n"); | |
371 | return -1; | |
372 | } | |
373 | ||
374 | if (pfd.revents != 0) { | |
375 | if (iscsi_service(lun->context, pfd.revents) < 0) { | |
376 | SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(lun->context)); | |
377 | } | |
378 | } | |
379 | ||
380 | return -1; | |
381 | } | |
382 | ||
383 | static int | |
384 | bdev_iscsi_no_master_ch_poll(void *arg) | |
385 | { | |
386 | struct bdev_iscsi_lun *lun = arg; | |
387 | int rc = 0; | |
388 | ||
389 | if (pthread_mutex_trylock(&lun->mutex)) { | |
390 | /* Don't care about the error code here. */ | |
391 | return -1; | |
392 | } | |
393 | ||
394 | if (lun->ch_count == 0) { | |
395 | rc = bdev_iscsi_poll_lun(arg); | |
396 | } | |
397 | ||
398 | pthread_mutex_unlock(&lun->mutex); | |
399 | return rc; | |
400 | } | |
401 | ||
9f95a23c TL |
402 | static void |
403 | bdev_iscsi_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, | |
404 | bool success) | |
11fdf7f2 | 405 | { |
9f95a23c TL |
406 | if (!success) { |
407 | spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); | |
408 | return; | |
409 | } | |
11fdf7f2 | 410 | |
11fdf7f2 TL |
411 | bdev_iscsi_readv((struct bdev_iscsi_lun *)bdev_io->bdev->ctxt, |
412 | (struct bdev_iscsi_io *)bdev_io->driver_ctx, | |
413 | bdev_io->u.bdev.iovs, | |
414 | bdev_io->u.bdev.iovcnt, | |
415 | bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, | |
416 | bdev_io->u.bdev.offset_blocks); | |
417 | } | |
418 | ||
419 | static void _bdev_iscsi_submit_request(void *_bdev_io) | |
420 | { | |
421 | struct spdk_bdev_io *bdev_io = _bdev_io; | |
422 | struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; | |
423 | struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; | |
424 | ||
425 | switch (bdev_io->type) { | |
426 | case SPDK_BDEV_IO_TYPE_READ: | |
427 | spdk_bdev_io_get_buf(bdev_io, bdev_iscsi_get_buf_cb, | |
428 | bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); | |
429 | break; | |
430 | ||
431 | case SPDK_BDEV_IO_TYPE_WRITE: | |
432 | bdev_iscsi_writev(lun, iscsi_io, | |
433 | bdev_io->u.bdev.iovs, | |
434 | bdev_io->u.bdev.iovcnt, | |
435 | bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, | |
436 | bdev_io->u.bdev.offset_blocks); | |
437 | break; | |
438 | case SPDK_BDEV_IO_TYPE_FLUSH: | |
439 | bdev_iscsi_flush(lun, iscsi_io, | |
440 | bdev_io->u.bdev.num_blocks, | |
441 | ISCSI_IMMEDIATE_DATA_NO, | |
442 | bdev_io->u.bdev.offset_blocks); | |
443 | break; | |
444 | case SPDK_BDEV_IO_TYPE_RESET: | |
445 | bdev_iscsi_reset(bdev_io); | |
446 | break; | |
447 | case SPDK_BDEV_IO_TYPE_UNMAP: | |
448 | bdev_iscsi_unmap(lun, iscsi_io, | |
449 | bdev_io->u.bdev.offset_blocks, | |
450 | bdev_io->u.bdev.num_blocks); | |
451 | break; | |
452 | default: | |
453 | bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); | |
454 | break; | |
455 | } | |
456 | } | |
457 | ||
458 | static void bdev_iscsi_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) | |
459 | { | |
460 | struct spdk_thread *submit_td = spdk_io_channel_get_thread(_ch); | |
461 | struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; | |
462 | struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; | |
463 | ||
464 | if (lun->master_td != submit_td) { | |
465 | iscsi_io->submit_td = submit_td; | |
466 | spdk_thread_send_msg(lun->master_td, _bdev_iscsi_submit_request, bdev_io); | |
467 | return; | |
468 | } else { | |
469 | iscsi_io->submit_td = NULL; | |
470 | } | |
471 | ||
472 | _bdev_iscsi_submit_request(bdev_io); | |
473 | } | |
474 | ||
475 | static bool | |
476 | bdev_iscsi_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) | |
477 | { | |
478 | struct bdev_iscsi_lun *lun = ctx; | |
479 | ||
480 | switch (io_type) { | |
481 | case SPDK_BDEV_IO_TYPE_READ: | |
482 | case SPDK_BDEV_IO_TYPE_WRITE: | |
483 | case SPDK_BDEV_IO_TYPE_FLUSH: | |
484 | case SPDK_BDEV_IO_TYPE_RESET: | |
485 | return true; | |
486 | ||
487 | case SPDK_BDEV_IO_TYPE_UNMAP: | |
488 | return lun->unmap_supported; | |
489 | default: | |
490 | return false; | |
491 | } | |
492 | } | |
493 | ||
494 | static int | |
495 | bdev_iscsi_create_cb(void *io_device, void *ctx_buf) | |
496 | { | |
497 | struct bdev_iscsi_io_channel *ch = ctx_buf; | |
498 | struct bdev_iscsi_lun *lun = io_device; | |
499 | ||
500 | pthread_mutex_lock(&lun->mutex); | |
501 | if (lun->ch_count == 0) { | |
11fdf7f2 | 502 | assert(lun->master_td == NULL); |
11fdf7f2 | 503 | lun->master_td = spdk_get_thread(); |
9f95a23c | 504 | lun->poller = spdk_poller_register(bdev_iscsi_poll_lun, lun, 0); |
11fdf7f2 TL |
505 | ch->lun = lun; |
506 | } | |
507 | lun->ch_count++; | |
508 | pthread_mutex_unlock(&lun->mutex); | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | static void | |
514 | bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf) | |
515 | { | |
11fdf7f2 TL |
516 | struct bdev_iscsi_lun *lun = io_device; |
517 | ||
518 | pthread_mutex_lock(&lun->mutex); | |
519 | lun->ch_count--; | |
520 | if (lun->ch_count == 0) { | |
11fdf7f2 | 521 | assert(lun->master_td != NULL); |
11fdf7f2 | 522 | |
11fdf7f2 | 523 | lun->master_td = NULL; |
9f95a23c | 524 | spdk_poller_unregister(&lun->poller); |
11fdf7f2 TL |
525 | } |
526 | pthread_mutex_unlock(&lun->mutex); | |
527 | } | |
528 | ||
529 | static struct spdk_io_channel * | |
530 | bdev_iscsi_get_io_channel(void *ctx) | |
531 | { | |
532 | struct bdev_iscsi_lun *lun = ctx; | |
533 | ||
534 | return spdk_get_io_channel(lun); | |
535 | } | |
536 | ||
537 | static int | |
538 | bdev_iscsi_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) | |
539 | { | |
540 | struct bdev_iscsi_lun *lun = ctx; | |
541 | ||
9f95a23c TL |
542 | spdk_json_write_named_object_begin(w, "iscsi"); |
543 | spdk_json_write_named_string(w, "initiator_name", lun->initiator_iqn); | |
544 | spdk_json_write_named_string(w, "url", lun->url); | |
11fdf7f2 TL |
545 | spdk_json_write_object_end(w); |
546 | ||
547 | return 0; | |
548 | } | |
549 | ||
550 | static void | |
551 | bdev_iscsi_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) | |
552 | { | |
553 | struct bdev_iscsi_lun *lun = bdev->ctxt; | |
554 | ||
555 | pthread_mutex_lock(&lun->mutex); | |
556 | spdk_json_write_object_begin(w); | |
557 | ||
558 | spdk_json_write_named_string(w, "method", "construct_iscsi_bdev"); | |
559 | ||
560 | spdk_json_write_named_object_begin(w, "params"); | |
561 | spdk_json_write_named_string(w, "name", bdev->name); | |
562 | spdk_json_write_named_string(w, "initiator_iqn", lun->initiator_iqn); | |
563 | spdk_json_write_named_string(w, "url", lun->url); | |
564 | spdk_json_write_object_end(w); | |
565 | ||
566 | spdk_json_write_object_end(w); | |
567 | pthread_mutex_unlock(&lun->mutex); | |
568 | } | |
569 | ||
570 | static const struct spdk_bdev_fn_table iscsi_fn_table = { | |
571 | .destruct = bdev_iscsi_destruct, | |
572 | .submit_request = bdev_iscsi_submit_request, | |
573 | .io_type_supported = bdev_iscsi_io_type_supported, | |
574 | .get_io_channel = bdev_iscsi_get_io_channel, | |
575 | .dump_info_json = bdev_iscsi_dump_info_json, | |
576 | .write_config_json = bdev_iscsi_write_config_json, | |
577 | }; | |
578 | ||
579 | static int | |
9f95a23c TL |
580 | create_iscsi_lun(struct iscsi_context *context, int lun_id, char *url, char *initiator_iqn, |
581 | char *name, | |
11fdf7f2 TL |
582 | uint64_t num_blocks, uint32_t block_size, struct spdk_bdev **bdev, bool unmap_supported) |
583 | { | |
584 | struct bdev_iscsi_lun *lun; | |
585 | int rc; | |
586 | ||
587 | lun = calloc(sizeof(*lun), 1); | |
588 | if (!lun) { | |
589 | SPDK_ERRLOG("Unable to allocate enough memory for iscsi backend\n"); | |
590 | return -ENOMEM; | |
591 | } | |
592 | ||
593 | lun->context = context; | |
9f95a23c | 594 | lun->lun_id = lun_id; |
11fdf7f2 TL |
595 | lun->url = url; |
596 | lun->initiator_iqn = initiator_iqn; | |
597 | ||
598 | pthread_mutex_init(&lun->mutex, NULL); | |
599 | ||
600 | lun->bdev.name = name; | |
601 | lun->bdev.product_name = "iSCSI LUN"; | |
602 | lun->bdev.module = &g_iscsi_bdev_module; | |
603 | lun->bdev.blocklen = block_size; | |
604 | lun->bdev.blockcnt = num_blocks; | |
605 | lun->bdev.ctxt = lun; | |
606 | lun->unmap_supported = unmap_supported; | |
607 | ||
608 | lun->bdev.fn_table = &iscsi_fn_table; | |
609 | ||
610 | spdk_io_device_register(lun, bdev_iscsi_create_cb, bdev_iscsi_destroy_cb, | |
611 | sizeof(struct bdev_iscsi_io_channel), | |
612 | name); | |
613 | rc = spdk_bdev_register(&lun->bdev); | |
614 | if (rc) { | |
615 | spdk_io_device_unregister(lun, NULL); | |
616 | pthread_mutex_destroy(&lun->mutex); | |
617 | free(lun); | |
618 | return rc; | |
619 | } | |
620 | ||
621 | lun->no_master_ch_poller_td = spdk_get_thread(); | |
622 | lun->no_master_ch_poller = spdk_poller_register(bdev_iscsi_no_master_ch_poll, lun, | |
623 | BDEV_ISCSI_NO_MASTER_CH_POLL_US); | |
624 | ||
625 | *bdev = &lun->bdev; | |
626 | return 0; | |
627 | } | |
628 | ||
629 | static void | |
630 | iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status, | |
631 | void *command_data, void *private_data) | |
632 | { | |
633 | struct bdev_iscsi_conn_req *req = private_data; | |
634 | struct scsi_readcapacity16 *readcap16; | |
635 | struct spdk_bdev *bdev = NULL; | |
636 | struct scsi_task *task = command_data; | |
637 | ||
638 | if (status != SPDK_SCSI_STATUS_GOOD) { | |
639 | SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(iscsi)); | |
640 | goto ret; | |
641 | } | |
642 | ||
643 | readcap16 = scsi_datain_unmarshall(task); | |
644 | if (!readcap16) { | |
645 | status = -ENOMEM; | |
646 | goto ret; | |
647 | } | |
648 | ||
9f95a23c | 649 | status = create_iscsi_lun(req->context, req->lun, req->url, req->initiator_iqn, req->bdev_name, |
11fdf7f2 TL |
650 | readcap16->returned_lba + 1, readcap16->block_length, &bdev, req->unmap_supported); |
651 | if (status) { | |
652 | SPDK_ERRLOG("Unable to create iscsi bdev: %s (%d)\n", spdk_strerror(-status), status); | |
653 | } | |
654 | ||
655 | ret: | |
656 | scsi_free_scsi_task(task); | |
657 | complete_conn_req(req, bdev, status); | |
658 | } | |
659 | ||
660 | static void | |
661 | bdev_iscsi_inquiry_cb(struct iscsi_context *context, int status, void *_task, void *private_data) | |
662 | { | |
663 | struct scsi_task *task = _task; | |
664 | struct scsi_inquiry_logical_block_provisioning *lbp_inq = NULL; | |
665 | struct bdev_iscsi_conn_req *req = private_data; | |
666 | ||
667 | if (status == SPDK_SCSI_STATUS_GOOD) { | |
668 | lbp_inq = scsi_datain_unmarshall(task); | |
669 | if (lbp_inq != NULL && lbp_inq->lbpu) { | |
670 | req->unmap_supported = true; | |
671 | } | |
672 | } | |
673 | ||
9f95a23c | 674 | task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req); |
11fdf7f2 TL |
675 | if (task) { |
676 | return; | |
677 | } | |
678 | ||
679 | SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); | |
680 | complete_conn_req(req, NULL, status); | |
681 | } | |
682 | ||
683 | static void | |
684 | iscsi_connect_cb(struct iscsi_context *iscsi, int status, | |
685 | void *command_data, void *private_data) | |
686 | { | |
687 | struct bdev_iscsi_conn_req *req = private_data; | |
688 | struct scsi_task *task; | |
689 | ||
690 | if (status != SPDK_SCSI_STATUS_GOOD) { | |
691 | goto ret; | |
692 | } | |
693 | ||
9f95a23c | 694 | task = iscsi_inquiry_task(iscsi, req->lun, 1, |
11fdf7f2 TL |
695 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, |
696 | 255, bdev_iscsi_inquiry_cb, req); | |
697 | if (task) { | |
698 | return; | |
699 | } | |
700 | ||
701 | ret: | |
702 | SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); | |
703 | complete_conn_req(req, NULL, status); | |
704 | } | |
705 | ||
706 | static int | |
707 | iscsi_bdev_conn_poll(void *arg) | |
708 | { | |
709 | struct bdev_iscsi_conn_req *req, *tmp; | |
710 | struct pollfd pfd; | |
711 | struct iscsi_context *context; | |
712 | ||
713 | TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) { | |
714 | context = req->context; | |
715 | pfd.fd = iscsi_get_fd(context); | |
716 | pfd.events = iscsi_which_events(context); | |
717 | pfd.revents = 0; | |
718 | if (poll(&pfd, 1, 0) < 0) { | |
719 | SPDK_ERRLOG("poll failed\n"); | |
720 | return -1; | |
721 | } | |
722 | ||
723 | if (pfd.revents != 0) { | |
724 | if (iscsi_service(context, pfd.revents) < 0) { | |
725 | SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(context)); | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | return -1; | |
731 | } | |
732 | ||
733 | int | |
734 | create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_iqn, | |
735 | spdk_bdev_iscsi_create_cb cb_fn, void *cb_arg) | |
736 | { | |
737 | struct bdev_iscsi_conn_req *req; | |
738 | struct iscsi_url *iscsi_url = NULL; | |
739 | int rc; | |
740 | ||
741 | if (!bdev_name || !url || !initiator_iqn || strlen(initiator_iqn) == 0 || !cb_fn) { | |
742 | return -EINVAL; | |
743 | } | |
744 | ||
745 | req = calloc(1, sizeof(struct bdev_iscsi_conn_req)); | |
746 | if (!req) { | |
747 | SPDK_ERRLOG("Cannot allocate pointer of struct bdev_iscsi_conn_req\n"); | |
748 | return -ENOMEM; | |
749 | } | |
750 | ||
751 | req->bdev_name = strdup(bdev_name); | |
752 | req->url = strdup(url); | |
753 | req->initiator_iqn = strdup(initiator_iqn); | |
754 | req->context = iscsi_create_context(initiator_iqn); | |
755 | if (!req->bdev_name || !req->url || !req->initiator_iqn || !req->context) { | |
756 | SPDK_ERRLOG("Out of memory\n"); | |
757 | rc = -ENOMEM; | |
758 | goto err; | |
759 | } | |
760 | ||
761 | req->create_cb = cb_fn; | |
762 | req->create_cb_arg = cb_arg; | |
763 | ||
764 | iscsi_url = iscsi_parse_full_url(req->context, url); | |
765 | if (iscsi_url == NULL) { | |
766 | SPDK_ERRLOG("could not parse URL: %s\n", iscsi_get_error(req->context)); | |
767 | rc = -EINVAL; | |
768 | goto err; | |
769 | } | |
770 | ||
9f95a23c | 771 | req->lun = iscsi_url->lun; |
11fdf7f2 TL |
772 | rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL); |
773 | rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE); | |
774 | rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target); | |
775 | rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun, | |
776 | iscsi_connect_cb, req); | |
777 | if (rc == 0 && iscsi_url->user[0] != '\0') { | |
778 | rc = iscsi_set_initiator_username_pwd(req->context, iscsi_url->user, iscsi_url->passwd); | |
779 | } | |
780 | ||
781 | if (rc < 0) { | |
782 | SPDK_ERRLOG("Failed to connect provided URL=%s: %s\n", url, iscsi_get_error(req->context)); | |
783 | goto err; | |
784 | } | |
785 | ||
786 | iscsi_destroy_url(iscsi_url); | |
787 | TAILQ_INSERT_TAIL(&g_iscsi_conn_req, req, link); | |
788 | if (!g_conn_poller) { | |
789 | g_conn_poller = spdk_poller_register(iscsi_bdev_conn_poll, NULL, BDEV_ISCSI_CONNECTION_POLL_US); | |
790 | } | |
791 | ||
792 | return 0; | |
793 | ||
794 | err: | |
795 | /* iscsi_destroy_url() is not NULL-proof */ | |
796 | if (iscsi_url) { | |
797 | iscsi_destroy_url(iscsi_url); | |
798 | } | |
799 | ||
800 | if (req->context) { | |
801 | iscsi_destroy_context(req->context); | |
802 | } | |
803 | ||
804 | free(req->initiator_iqn); | |
805 | free(req->bdev_name); | |
806 | free(req->url); | |
807 | free(req); | |
808 | return rc; | |
809 | } | |
810 | ||
811 | void | |
812 | delete_iscsi_disk(struct spdk_bdev *bdev, spdk_delete_iscsi_complete cb_fn, void *cb_arg) | |
813 | { | |
814 | if (!bdev || bdev->module != &g_iscsi_bdev_module) { | |
815 | cb_fn(cb_arg, -ENODEV); | |
816 | return; | |
817 | } | |
818 | ||
819 | spdk_bdev_unregister(bdev, cb_fn, cb_arg); | |
820 | } | |
821 | ||
822 | static void | |
823 | bdev_iscsi_initialize_cb(void *cb_arg, struct spdk_bdev *bdev, int status) | |
824 | { | |
825 | if (TAILQ_EMPTY(&g_iscsi_conn_req)) { | |
826 | spdk_bdev_module_init_done(&g_iscsi_bdev_module); | |
827 | } | |
828 | } | |
829 | ||
830 | static int | |
831 | bdev_iscsi_initialize(void) | |
832 | { | |
833 | struct spdk_conf_section *sp; | |
834 | ||
835 | const char *url, *bdev_name, *initiator_iqn; | |
836 | int i, rc; | |
837 | ||
838 | sp = spdk_conf_find_section(NULL, "iSCSI_Initiator"); | |
839 | if (sp == NULL) { | |
840 | spdk_bdev_module_init_done(&g_iscsi_bdev_module); | |
841 | return 0; | |
842 | } | |
843 | ||
844 | initiator_iqn = spdk_conf_section_get_val(sp, "initiator_name"); | |
845 | if (!initiator_iqn) { | |
846 | initiator_iqn = DEFAULT_INITIATOR_NAME; | |
847 | } | |
848 | ||
849 | rc = 0; | |
850 | for (i = 0; (url = spdk_conf_section_get_nmval(sp, "URL", i, 0)) != NULL; i++) { | |
851 | bdev_name = spdk_conf_section_get_nmval(sp, "URL", i, 1); | |
852 | if (bdev_name == NULL) { | |
853 | SPDK_ERRLOG("no bdev name specified for URL %s\n", url); | |
854 | rc = -EINVAL; | |
855 | break; | |
856 | } | |
857 | ||
858 | rc = create_iscsi_disk(bdev_name, url, initiator_iqn, bdev_iscsi_initialize_cb, NULL); | |
859 | if (rc) { | |
860 | break; | |
861 | } | |
862 | } | |
863 | ||
864 | if (i == 0) { | |
865 | spdk_bdev_module_init_done(&g_iscsi_bdev_module); | |
866 | } | |
867 | ||
868 | return rc; | |
869 | } | |
870 | ||
871 | SPDK_LOG_REGISTER_COMPONENT("iscsi_init", SPDK_LOG_ISCSI_INIT) |