]>
Commit | Line | Data |
---|---|---|
c589b249 RS |
1 | /* |
2 | * QEMU Block driver for iSCSI images | |
3 | * | |
4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "config-host.h" | |
26 | ||
27 | #include <poll.h> | |
f4dfa67f | 28 | #include <arpa/inet.h> |
c589b249 RS |
29 | #include "qemu-common.h" |
30 | #include "qemu-error.h" | |
31 | #include "block_int.h" | |
32 | #include "trace.h" | |
dbfff6d7 | 33 | #include "hw/scsi-defs.h" |
c589b249 RS |
34 | |
35 | #include <iscsi/iscsi.h> | |
36 | #include <iscsi/scsi-lowlevel.h> | |
37 | ||
98392453 RS |
38 | #ifdef __linux__ |
39 | #include <scsi/sg.h> | |
40 | #include <hw/scsi-defs.h> | |
41 | #endif | |
c589b249 RS |
42 | |
43 | typedef struct IscsiLun { | |
44 | struct iscsi_context *iscsi; | |
45 | int lun; | |
dbfff6d7 | 46 | enum scsi_inquiry_peripheral_device_type type; |
c589b249 | 47 | int block_size; |
c7b4a952 | 48 | uint64_t num_blocks; |
c9b9f682 | 49 | int events; |
c589b249 RS |
50 | } IscsiLun; |
51 | ||
52 | typedef struct IscsiAIOCB { | |
53 | BlockDriverAIOCB common; | |
54 | QEMUIOVector *qiov; | |
55 | QEMUBH *bh; | |
56 | IscsiLun *iscsilun; | |
57 | struct scsi_task *task; | |
58 | uint8_t *buf; | |
59 | int status; | |
60 | int canceled; | |
61 | size_t read_size; | |
62 | size_t read_offset; | |
98392453 RS |
63 | #ifdef __linux__ |
64 | sg_io_hdr_t *ioh; | |
65 | #endif | |
c589b249 RS |
66 | } IscsiAIOCB; |
67 | ||
68 | struct IscsiTask { | |
69 | IscsiLun *iscsilun; | |
70 | BlockDriverState *bs; | |
71 | int status; | |
72 | int complete; | |
73 | }; | |
74 | ||
27cbd828 | 75 | static void |
cfb3f506 | 76 | iscsi_bh_cb(void *p) |
27cbd828 PB |
77 | { |
78 | IscsiAIOCB *acb = p; | |
79 | ||
80 | qemu_bh_delete(acb->bh); | |
81 | ||
82 | if (acb->canceled == 0) { | |
83 | acb->common.cb(acb->common.opaque, acb->status); | |
84 | } | |
85 | ||
1bd075f2 PB |
86 | if (acb->task != NULL) { |
87 | scsi_free_scsi_task(acb->task); | |
88 | acb->task = NULL; | |
89 | } | |
90 | ||
27cbd828 PB |
91 | qemu_aio_release(acb); |
92 | } | |
93 | ||
cfb3f506 PB |
94 | static void |
95 | iscsi_schedule_bh(IscsiAIOCB *acb) | |
27cbd828 | 96 | { |
1bd075f2 PB |
97 | if (acb->bh) { |
98 | return; | |
99 | } | |
cfb3f506 | 100 | acb->bh = qemu_bh_new(iscsi_bh_cb, acb); |
27cbd828 | 101 | qemu_bh_schedule(acb->bh); |
27cbd828 PB |
102 | } |
103 | ||
104 | ||
c589b249 RS |
105 | static void |
106 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
107 | void *private_data) | |
108 | { | |
1bd075f2 PB |
109 | IscsiAIOCB *acb = private_data; |
110 | ||
111 | acb->status = -ECANCELED; | |
112 | iscsi_schedule_bh(acb); | |
c589b249 RS |
113 | } |
114 | ||
115 | static void | |
116 | iscsi_aio_cancel(BlockDriverAIOCB *blockacb) | |
117 | { | |
118 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; | |
119 | IscsiLun *iscsilun = acb->iscsilun; | |
120 | ||
1bd075f2 PB |
121 | if (acb->status != -EINPROGRESS) { |
122 | return; | |
123 | } | |
124 | ||
b2090919 | 125 | acb->canceled = 1; |
c589b249 | 126 | |
b2090919 | 127 | /* send a task mgmt call to the target to cancel the task on the target */ |
64e69e80 | 128 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
1bd075f2 | 129 | iscsi_abort_task_cb, acb); |
b2090919 | 130 | |
1bd075f2 PB |
131 | while (acb->status == -EINPROGRESS) { |
132 | qemu_aio_wait(); | |
133 | } | |
c589b249 RS |
134 | } |
135 | ||
136 | static AIOPool iscsi_aio_pool = { | |
137 | .aiocb_size = sizeof(IscsiAIOCB), | |
138 | .cancel = iscsi_aio_cancel, | |
139 | }; | |
140 | ||
141 | ||
142 | static void iscsi_process_read(void *arg); | |
143 | static void iscsi_process_write(void *arg); | |
144 | ||
145 | static int iscsi_process_flush(void *arg) | |
146 | { | |
147 | IscsiLun *iscsilun = arg; | |
148 | ||
149 | return iscsi_queue_length(iscsilun->iscsi) > 0; | |
150 | } | |
151 | ||
152 | static void | |
153 | iscsi_set_events(IscsiLun *iscsilun) | |
154 | { | |
155 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
c9b9f682 RS |
156 | int ev; |
157 | ||
158 | /* We always register a read handler. */ | |
159 | ev = POLLIN; | |
160 | ev |= iscsi_which_events(iscsi); | |
161 | if (ev != iscsilun->events) { | |
162 | qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), | |
163 | iscsi_process_read, | |
164 | (ev & POLLOUT) ? iscsi_process_write : NULL, | |
165 | iscsi_process_flush, | |
166 | iscsilun); | |
167 | ||
168 | } | |
169 | ||
170 | /* If we just added an event, the callback might be delayed | |
171 | * unless we call qemu_notify_event(). | |
172 | */ | |
173 | if (ev & ~iscsilun->events) { | |
174 | qemu_notify_event(); | |
175 | } | |
176 | iscsilun->events = ev; | |
c589b249 RS |
177 | } |
178 | ||
179 | static void | |
180 | iscsi_process_read(void *arg) | |
181 | { | |
182 | IscsiLun *iscsilun = arg; | |
183 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
184 | ||
185 | iscsi_service(iscsi, POLLIN); | |
186 | iscsi_set_events(iscsilun); | |
187 | } | |
188 | ||
189 | static void | |
190 | iscsi_process_write(void *arg) | |
191 | { | |
192 | IscsiLun *iscsilun = arg; | |
193 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
194 | ||
195 | iscsi_service(iscsi, POLLOUT); | |
196 | iscsi_set_events(iscsilun); | |
197 | } | |
198 | ||
199 | ||
c589b249 | 200 | static void |
f4dfa67f | 201 | iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status, |
c589b249 RS |
202 | void *command_data, void *opaque) |
203 | { | |
204 | IscsiAIOCB *acb = opaque; | |
205 | ||
f4dfa67f | 206 | trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled); |
c589b249 RS |
207 | |
208 | g_free(acb->buf); | |
209 | ||
b2090919 | 210 | if (acb->canceled != 0) { |
c589b249 RS |
211 | return; |
212 | } | |
213 | ||
214 | acb->status = 0; | |
215 | if (status < 0) { | |
f4dfa67f | 216 | error_report("Failed to write16 data to iSCSI lun. %s", |
c589b249 RS |
217 | iscsi_get_error(iscsi)); |
218 | acb->status = -EIO; | |
219 | } | |
220 | ||
cfb3f506 | 221 | iscsi_schedule_bh(acb); |
c589b249 RS |
222 | } |
223 | ||
224 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) | |
225 | { | |
226 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
227 | } | |
228 | ||
229 | static BlockDriverAIOCB * | |
230 | iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num, | |
231 | QEMUIOVector *qiov, int nb_sectors, | |
232 | BlockDriverCompletionFunc *cb, | |
233 | void *opaque) | |
234 | { | |
235 | IscsiLun *iscsilun = bs->opaque; | |
236 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
237 | IscsiAIOCB *acb; | |
238 | size_t size; | |
f4dfa67f RS |
239 | uint32_t num_sectors; |
240 | uint64_t lba; | |
241 | struct iscsi_data data; | |
c589b249 RS |
242 | |
243 | acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque); | |
244 | trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb); | |
245 | ||
246 | acb->iscsilun = iscsilun; | |
247 | acb->qiov = qiov; | |
248 | ||
249 | acb->canceled = 0; | |
1bd075f2 PB |
250 | acb->bh = NULL; |
251 | acb->status = -EINPROGRESS; | |
c589b249 | 252 | |
f4dfa67f | 253 | /* XXX we should pass the iovec to write16 to avoid the extra copy */ |
c589b249 RS |
254 | /* this will allow us to get rid of 'buf' completely */ |
255 | size = nb_sectors * BDRV_SECTOR_SIZE; | |
256 | acb->buf = g_malloc(size); | |
d5e6b161 | 257 | qemu_iovec_to_buf(acb->qiov, 0, acb->buf, size); |
f4dfa67f RS |
258 | |
259 | acb->task = malloc(sizeof(struct scsi_task)); | |
c589b249 | 260 | if (acb->task == NULL) { |
f4dfa67f RS |
261 | error_report("iSCSI: Failed to allocate task for scsi WRITE16 " |
262 | "command. %s", iscsi_get_error(iscsi)); | |
263 | qemu_aio_release(acb); | |
264 | return NULL; | |
265 | } | |
266 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
267 | ||
268 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
269 | acb->task->cdb_size = 16; | |
270 | acb->task->cdb[0] = 0x8a; | |
271 | if (!(bs->open_flags & BDRV_O_CACHE_WB)) { | |
272 | /* set FUA on writes when cache mode is write through */ | |
273 | acb->task->cdb[1] |= 0x04; | |
274 | } | |
275 | lba = sector_qemu2lun(sector_num, iscsilun); | |
276 | *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32); | |
277 | *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff); | |
278 | num_sectors = size / iscsilun->block_size; | |
279 | *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors); | |
280 | acb->task->expxferlen = size; | |
281 | ||
282 | data.data = acb->buf; | |
283 | data.size = size; | |
284 | ||
285 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, | |
286 | iscsi_aio_write16_cb, | |
287 | &data, | |
288 | acb) != 0) { | |
289 | scsi_free_scsi_task(acb->task); | |
c589b249 RS |
290 | g_free(acb->buf); |
291 | qemu_aio_release(acb); | |
292 | return NULL; | |
293 | } | |
294 | ||
295 | iscsi_set_events(iscsilun); | |
296 | ||
297 | return &acb->common; | |
298 | } | |
299 | ||
300 | static void | |
f4dfa67f | 301 | iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status, |
c589b249 RS |
302 | void *command_data, void *opaque) |
303 | { | |
304 | IscsiAIOCB *acb = opaque; | |
305 | ||
f4dfa67f | 306 | trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled); |
c589b249 | 307 | |
b2090919 | 308 | if (acb->canceled != 0) { |
c589b249 RS |
309 | return; |
310 | } | |
311 | ||
312 | acb->status = 0; | |
313 | if (status != 0) { | |
f4dfa67f | 314 | error_report("Failed to read16 data from iSCSI lun. %s", |
c589b249 RS |
315 | iscsi_get_error(iscsi)); |
316 | acb->status = -EIO; | |
317 | } | |
318 | ||
cfb3f506 | 319 | iscsi_schedule_bh(acb); |
c589b249 RS |
320 | } |
321 | ||
322 | static BlockDriverAIOCB * | |
323 | iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num, | |
324 | QEMUIOVector *qiov, int nb_sectors, | |
325 | BlockDriverCompletionFunc *cb, | |
326 | void *opaque) | |
327 | { | |
328 | IscsiLun *iscsilun = bs->opaque; | |
329 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
330 | IscsiAIOCB *acb; | |
f4dfa67f | 331 | size_t qemu_read_size; |
c589b249 | 332 | int i; |
f4dfa67f RS |
333 | uint64_t lba; |
334 | uint32_t num_sectors; | |
c589b249 RS |
335 | |
336 | qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors; | |
337 | ||
338 | acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque); | |
339 | trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb); | |
340 | ||
341 | acb->iscsilun = iscsilun; | |
342 | acb->qiov = qiov; | |
343 | ||
344 | acb->canceled = 0; | |
1bd075f2 PB |
345 | acb->bh = NULL; |
346 | acb->status = -EINPROGRESS; | |
c589b249 RS |
347 | acb->read_size = qemu_read_size; |
348 | acb->buf = NULL; | |
349 | ||
350 | /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU | |
351 | * may be misaligned to the LUN, so we may need to read some extra | |
352 | * data. | |
353 | */ | |
354 | acb->read_offset = 0; | |
355 | if (iscsilun->block_size > BDRV_SECTOR_SIZE) { | |
356 | uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num; | |
357 | ||
358 | acb->read_offset = bdrv_offset % iscsilun->block_size; | |
359 | } | |
360 | ||
f4dfa67f RS |
361 | num_sectors = (qemu_read_size + iscsilun->block_size |
362 | + acb->read_offset - 1) | |
363 | / iscsilun->block_size; | |
364 | ||
365 | acb->task = malloc(sizeof(struct scsi_task)); | |
c589b249 | 366 | if (acb->task == NULL) { |
f4dfa67f RS |
367 | error_report("iSCSI: Failed to allocate task for scsi READ16 " |
368 | "command. %s", iscsi_get_error(iscsi)); | |
369 | qemu_aio_release(acb); | |
370 | return NULL; | |
371 | } | |
372 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
373 | ||
374 | acb->task->xfer_dir = SCSI_XFER_READ; | |
375 | lba = sector_qemu2lun(sector_num, iscsilun); | |
376 | acb->task->expxferlen = qemu_read_size; | |
377 | ||
378 | switch (iscsilun->type) { | |
379 | case TYPE_DISK: | |
380 | acb->task->cdb_size = 16; | |
381 | acb->task->cdb[0] = 0x88; | |
382 | *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32); | |
383 | *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff); | |
384 | *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors); | |
385 | break; | |
386 | default: | |
387 | acb->task->cdb_size = 10; | |
388 | acb->task->cdb[0] = 0x28; | |
389 | *(uint32_t *)&acb->task->cdb[2] = htonl(lba); | |
390 | *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors); | |
391 | break; | |
392 | } | |
393 | ||
394 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, | |
395 | iscsi_aio_read16_cb, | |
396 | NULL, | |
397 | acb) != 0) { | |
398 | scsi_free_scsi_task(acb->task); | |
c589b249 RS |
399 | qemu_aio_release(acb); |
400 | return NULL; | |
401 | } | |
402 | ||
403 | for (i = 0; i < acb->qiov->niov; i++) { | |
404 | scsi_task_add_data_in_buffer(acb->task, | |
405 | acb->qiov->iov[i].iov_len, | |
406 | acb->qiov->iov[i].iov_base); | |
407 | } | |
408 | ||
409 | iscsi_set_events(iscsilun); | |
410 | ||
411 | return &acb->common; | |
412 | } | |
413 | ||
414 | ||
415 | static void | |
416 | iscsi_synccache10_cb(struct iscsi_context *iscsi, int status, | |
417 | void *command_data, void *opaque) | |
418 | { | |
419 | IscsiAIOCB *acb = opaque; | |
420 | ||
b2090919 | 421 | if (acb->canceled != 0) { |
c589b249 RS |
422 | return; |
423 | } | |
424 | ||
425 | acb->status = 0; | |
426 | if (status < 0) { | |
427 | error_report("Failed to sync10 data on iSCSI lun. %s", | |
428 | iscsi_get_error(iscsi)); | |
429 | acb->status = -EIO; | |
430 | } | |
431 | ||
cfb3f506 | 432 | iscsi_schedule_bh(acb); |
c589b249 RS |
433 | } |
434 | ||
435 | static BlockDriverAIOCB * | |
436 | iscsi_aio_flush(BlockDriverState *bs, | |
437 | BlockDriverCompletionFunc *cb, void *opaque) | |
438 | { | |
439 | IscsiLun *iscsilun = bs->opaque; | |
440 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
441 | IscsiAIOCB *acb; | |
442 | ||
443 | acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque); | |
444 | ||
445 | acb->iscsilun = iscsilun; | |
446 | acb->canceled = 0; | |
1bd075f2 PB |
447 | acb->bh = NULL; |
448 | acb->status = -EINPROGRESS; | |
c589b249 RS |
449 | |
450 | acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun, | |
451 | 0, 0, 0, 0, | |
452 | iscsi_synccache10_cb, | |
453 | acb); | |
454 | if (acb->task == NULL) { | |
455 | error_report("iSCSI: Failed to send synchronizecache10 command. %s", | |
456 | iscsi_get_error(iscsi)); | |
457 | qemu_aio_release(acb); | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | iscsi_set_events(iscsilun); | |
462 | ||
463 | return &acb->common; | |
464 | } | |
465 | ||
fa6acb0c RS |
466 | static void |
467 | iscsi_unmap_cb(struct iscsi_context *iscsi, int status, | |
468 | void *command_data, void *opaque) | |
469 | { | |
470 | IscsiAIOCB *acb = opaque; | |
471 | ||
b2090919 | 472 | if (acb->canceled != 0) { |
fa6acb0c RS |
473 | return; |
474 | } | |
475 | ||
476 | acb->status = 0; | |
477 | if (status < 0) { | |
478 | error_report("Failed to unmap data on iSCSI lun. %s", | |
479 | iscsi_get_error(iscsi)); | |
480 | acb->status = -EIO; | |
481 | } | |
482 | ||
cfb3f506 | 483 | iscsi_schedule_bh(acb); |
fa6acb0c RS |
484 | } |
485 | ||
486 | static BlockDriverAIOCB * | |
487 | iscsi_aio_discard(BlockDriverState *bs, | |
488 | int64_t sector_num, int nb_sectors, | |
489 | BlockDriverCompletionFunc *cb, void *opaque) | |
490 | { | |
491 | IscsiLun *iscsilun = bs->opaque; | |
492 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
493 | IscsiAIOCB *acb; | |
494 | struct unmap_list list[1]; | |
495 | ||
496 | acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque); | |
497 | ||
498 | acb->iscsilun = iscsilun; | |
499 | acb->canceled = 0; | |
1bd075f2 PB |
500 | acb->bh = NULL; |
501 | acb->status = -EINPROGRESS; | |
fa6acb0c RS |
502 | |
503 | list[0].lba = sector_qemu2lun(sector_num, iscsilun); | |
504 | list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
505 | ||
506 | acb->task = iscsi_unmap_task(iscsi, iscsilun->lun, | |
507 | 0, 0, &list[0], 1, | |
508 | iscsi_unmap_cb, | |
509 | acb); | |
510 | if (acb->task == NULL) { | |
511 | error_report("iSCSI: Failed to send unmap command. %s", | |
512 | iscsi_get_error(iscsi)); | |
513 | qemu_aio_release(acb); | |
514 | return NULL; | |
515 | } | |
516 | ||
517 | iscsi_set_events(iscsilun); | |
518 | ||
519 | return &acb->common; | |
520 | } | |
521 | ||
98392453 RS |
522 | #ifdef __linux__ |
523 | static void | |
524 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, | |
525 | void *command_data, void *opaque) | |
526 | { | |
527 | IscsiAIOCB *acb = opaque; | |
528 | ||
b2090919 | 529 | if (acb->canceled != 0) { |
98392453 RS |
530 | return; |
531 | } | |
532 | ||
533 | acb->status = 0; | |
534 | if (status < 0) { | |
535 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", | |
536 | iscsi_get_error(iscsi)); | |
537 | acb->status = -EIO; | |
538 | } | |
539 | ||
540 | acb->ioh->driver_status = 0; | |
541 | acb->ioh->host_status = 0; | |
542 | acb->ioh->resid = 0; | |
543 | ||
544 | #define SG_ERR_DRIVER_SENSE 0x08 | |
545 | ||
546 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { | |
547 | int ss; | |
548 | ||
549 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; | |
550 | ||
551 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; | |
552 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? | |
553 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; | |
554 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); | |
555 | } | |
556 | ||
cfb3f506 | 557 | iscsi_schedule_bh(acb); |
98392453 RS |
558 | } |
559 | ||
560 | static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, | |
561 | unsigned long int req, void *buf, | |
562 | BlockDriverCompletionFunc *cb, void *opaque) | |
563 | { | |
564 | IscsiLun *iscsilun = bs->opaque; | |
565 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
566 | struct iscsi_data data; | |
567 | IscsiAIOCB *acb; | |
568 | ||
569 | assert(req == SG_IO); | |
570 | ||
571 | acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque); | |
572 | ||
573 | acb->iscsilun = iscsilun; | |
574 | acb->canceled = 0; | |
1bd075f2 PB |
575 | acb->bh = NULL; |
576 | acb->status = -EINPROGRESS; | |
98392453 RS |
577 | acb->buf = NULL; |
578 | acb->ioh = buf; | |
579 | ||
580 | acb->task = malloc(sizeof(struct scsi_task)); | |
581 | if (acb->task == NULL) { | |
582 | error_report("iSCSI: Failed to allocate task for scsi command. %s", | |
583 | iscsi_get_error(iscsi)); | |
584 | qemu_aio_release(acb); | |
585 | return NULL; | |
586 | } | |
587 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
588 | ||
589 | switch (acb->ioh->dxfer_direction) { | |
590 | case SG_DXFER_TO_DEV: | |
591 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
592 | break; | |
593 | case SG_DXFER_FROM_DEV: | |
594 | acb->task->xfer_dir = SCSI_XFER_READ; | |
595 | break; | |
596 | default: | |
597 | acb->task->xfer_dir = SCSI_XFER_NONE; | |
598 | break; | |
599 | } | |
600 | ||
601 | acb->task->cdb_size = acb->ioh->cmd_len; | |
602 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); | |
603 | acb->task->expxferlen = acb->ioh->dxfer_len; | |
604 | ||
605 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { | |
606 | data.data = acb->ioh->dxferp; | |
607 | data.size = acb->ioh->dxfer_len; | |
608 | } | |
609 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, | |
610 | iscsi_aio_ioctl_cb, | |
611 | (acb->task->xfer_dir == SCSI_XFER_WRITE) ? | |
612 | &data : NULL, | |
613 | acb) != 0) { | |
614 | scsi_free_scsi_task(acb->task); | |
615 | qemu_aio_release(acb); | |
616 | return NULL; | |
617 | } | |
618 | ||
619 | /* tell libiscsi to read straight into the buffer we got from ioctl */ | |
620 | if (acb->task->xfer_dir == SCSI_XFER_READ) { | |
621 | scsi_task_add_data_in_buffer(acb->task, | |
622 | acb->ioh->dxfer_len, | |
623 | acb->ioh->dxferp); | |
624 | } | |
625 | ||
626 | iscsi_set_events(iscsilun); | |
627 | ||
628 | return &acb->common; | |
629 | } | |
630 | ||
631 | static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
632 | { | |
633 | IscsiLun *iscsilun = bs->opaque; | |
634 | ||
635 | switch (req) { | |
636 | case SG_GET_VERSION_NUM: | |
637 | *(int *)buf = 30000; | |
638 | break; | |
639 | case SG_GET_SCSI_ID: | |
640 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; | |
641 | break; | |
642 | default: | |
643 | return -1; | |
644 | } | |
645 | return 0; | |
646 | } | |
647 | #endif | |
648 | ||
c589b249 RS |
649 | static int64_t |
650 | iscsi_getlength(BlockDriverState *bs) | |
651 | { | |
652 | IscsiLun *iscsilun = bs->opaque; | |
653 | int64_t len; | |
654 | ||
655 | len = iscsilun->num_blocks; | |
656 | len *= iscsilun->block_size; | |
657 | ||
658 | return len; | |
659 | } | |
660 | ||
661 | static void | |
fa6acb0c | 662 | iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status, |
c589b249 RS |
663 | void *command_data, void *opaque) |
664 | { | |
665 | struct IscsiTask *itask = opaque; | |
fa6acb0c | 666 | struct scsi_readcapacity16 *rc16; |
c589b249 RS |
667 | struct scsi_task *task = command_data; |
668 | ||
669 | if (status != 0) { | |
670 | error_report("iSCSI: Failed to read capacity of iSCSI lun. %s", | |
671 | iscsi_get_error(iscsi)); | |
672 | itask->status = 1; | |
673 | itask->complete = 1; | |
674 | scsi_free_scsi_task(task); | |
675 | return; | |
676 | } | |
677 | ||
fa6acb0c RS |
678 | rc16 = scsi_datain_unmarshall(task); |
679 | if (rc16 == NULL) { | |
680 | error_report("iSCSI: Failed to unmarshall readcapacity16 data."); | |
c589b249 RS |
681 | itask->status = 1; |
682 | itask->complete = 1; | |
683 | scsi_free_scsi_task(task); | |
684 | return; | |
685 | } | |
686 | ||
fa6acb0c RS |
687 | itask->iscsilun->block_size = rc16->block_length; |
688 | itask->iscsilun->num_blocks = rc16->returned_lba + 1; | |
689 | itask->bs->total_sectors = itask->iscsilun->num_blocks * | |
690 | itask->iscsilun->block_size / BDRV_SECTOR_SIZE ; | |
c589b249 RS |
691 | |
692 | itask->status = 0; | |
693 | itask->complete = 1; | |
694 | scsi_free_scsi_task(task); | |
695 | } | |
696 | ||
6bcd1346 RS |
697 | static void |
698 | iscsi_readcapacity10_cb(struct iscsi_context *iscsi, int status, | |
699 | void *command_data, void *opaque) | |
700 | { | |
701 | struct IscsiTask *itask = opaque; | |
702 | struct scsi_readcapacity10 *rc10; | |
703 | struct scsi_task *task = command_data; | |
704 | ||
705 | if (status != 0) { | |
706 | error_report("iSCSI: Failed to read capacity of iSCSI lun. %s", | |
707 | iscsi_get_error(iscsi)); | |
708 | itask->status = 1; | |
709 | itask->complete = 1; | |
710 | scsi_free_scsi_task(task); | |
711 | return; | |
712 | } | |
713 | ||
714 | rc10 = scsi_datain_unmarshall(task); | |
715 | if (rc10 == NULL) { | |
716 | error_report("iSCSI: Failed to unmarshall readcapacity10 data."); | |
717 | itask->status = 1; | |
718 | itask->complete = 1; | |
719 | scsi_free_scsi_task(task); | |
720 | return; | |
721 | } | |
722 | ||
723 | itask->iscsilun->block_size = rc10->block_size; | |
724 | itask->iscsilun->num_blocks = rc10->lba + 1; | |
725 | itask->bs->total_sectors = itask->iscsilun->num_blocks * | |
726 | itask->iscsilun->block_size / BDRV_SECTOR_SIZE ; | |
727 | ||
728 | itask->status = 0; | |
729 | itask->complete = 1; | |
730 | scsi_free_scsi_task(task); | |
731 | } | |
732 | ||
c589b249 | 733 | static void |
dbfff6d7 | 734 | iscsi_inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data, |
c589b249 RS |
735 | void *opaque) |
736 | { | |
737 | struct IscsiTask *itask = opaque; | |
dbfff6d7 RS |
738 | struct scsi_task *task = command_data; |
739 | struct scsi_inquiry_standard *inq; | |
c589b249 RS |
740 | |
741 | if (status != 0) { | |
742 | itask->status = 1; | |
743 | itask->complete = 1; | |
dbfff6d7 | 744 | scsi_free_scsi_task(task); |
c589b249 RS |
745 | return; |
746 | } | |
747 | ||
dbfff6d7 RS |
748 | inq = scsi_datain_unmarshall(task); |
749 | if (inq == NULL) { | |
750 | error_report("iSCSI: Failed to unmarshall inquiry data."); | |
751 | itask->status = 1; | |
752 | itask->complete = 1; | |
753 | scsi_free_scsi_task(task); | |
754 | return; | |
755 | } | |
756 | ||
757 | itask->iscsilun->type = inq->periperal_device_type; | |
758 | ||
759 | scsi_free_scsi_task(task); | |
760 | ||
6bcd1346 RS |
761 | switch (itask->iscsilun->type) { |
762 | case TYPE_DISK: | |
763 | task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun, | |
fa6acb0c | 764 | iscsi_readcapacity16_cb, opaque); |
6bcd1346 RS |
765 | if (task == NULL) { |
766 | error_report("iSCSI: failed to send readcapacity16 command."); | |
767 | itask->status = 1; | |
768 | itask->complete = 1; | |
769 | return; | |
770 | } | |
771 | break; | |
772 | case TYPE_ROM: | |
773 | task = iscsi_readcapacity10_task(iscsi, itask->iscsilun->lun, | |
774 | 0, 0, | |
775 | iscsi_readcapacity10_cb, opaque); | |
776 | if (task == NULL) { | |
777 | error_report("iSCSI: failed to send readcapacity16 command."); | |
778 | itask->status = 1; | |
779 | itask->complete = 1; | |
780 | return; | |
781 | } | |
782 | break; | |
783 | default: | |
784 | itask->status = 0; | |
c589b249 | 785 | itask->complete = 1; |
c589b249 RS |
786 | } |
787 | } | |
788 | ||
dbfff6d7 RS |
789 | static void |
790 | iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
791 | void *opaque) | |
792 | { | |
793 | struct IscsiTask *itask = opaque; | |
794 | struct scsi_task *task; | |
795 | ||
796 | if (status != 0) { | |
797 | itask->status = 1; | |
798 | itask->complete = 1; | |
799 | return; | |
800 | } | |
801 | ||
802 | task = iscsi_inquiry_task(iscsi, itask->iscsilun->lun, | |
803 | 0, 0, 36, | |
804 | iscsi_inquiry_cb, opaque); | |
805 | if (task == NULL) { | |
806 | error_report("iSCSI: failed to send inquiry command."); | |
807 | itask->status = 1; | |
808 | itask->complete = 1; | |
809 | return; | |
810 | } | |
811 | } | |
812 | ||
f9dadc98 RS |
813 | static int parse_chap(struct iscsi_context *iscsi, const char *target) |
814 | { | |
815 | QemuOptsList *list; | |
816 | QemuOpts *opts; | |
817 | const char *user = NULL; | |
818 | const char *password = NULL; | |
819 | ||
820 | list = qemu_find_opts("iscsi"); | |
821 | if (!list) { | |
822 | return 0; | |
823 | } | |
824 | ||
825 | opts = qemu_opts_find(list, target); | |
826 | if (opts == NULL) { | |
827 | opts = QTAILQ_FIRST(&list->head); | |
828 | if (!opts) { | |
829 | return 0; | |
830 | } | |
831 | } | |
832 | ||
833 | user = qemu_opt_get(opts, "user"); | |
834 | if (!user) { | |
835 | return 0; | |
836 | } | |
837 | ||
838 | password = qemu_opt_get(opts, "password"); | |
839 | if (!password) { | |
840 | error_report("CHAP username specified but no password was given"); | |
841 | return -1; | |
842 | } | |
843 | ||
844 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { | |
845 | error_report("Failed to set initiator username and password"); | |
846 | return -1; | |
847 | } | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | static void parse_header_digest(struct iscsi_context *iscsi, const char *target) | |
853 | { | |
854 | QemuOptsList *list; | |
855 | QemuOpts *opts; | |
856 | const char *digest = NULL; | |
857 | ||
858 | list = qemu_find_opts("iscsi"); | |
859 | if (!list) { | |
860 | return; | |
861 | } | |
862 | ||
863 | opts = qemu_opts_find(list, target); | |
864 | if (opts == NULL) { | |
865 | opts = QTAILQ_FIRST(&list->head); | |
866 | if (!opts) { | |
867 | return; | |
868 | } | |
869 | } | |
870 | ||
871 | digest = qemu_opt_get(opts, "header-digest"); | |
872 | if (!digest) { | |
873 | return; | |
874 | } | |
875 | ||
876 | if (!strcmp(digest, "CRC32C")) { | |
877 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); | |
878 | } else if (!strcmp(digest, "NONE")) { | |
879 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); | |
880 | } else if (!strcmp(digest, "CRC32C-NONE")) { | |
881 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); | |
882 | } else if (!strcmp(digest, "NONE-CRC32C")) { | |
883 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
884 | } else { | |
885 | error_report("Invalid header-digest setting : %s", digest); | |
886 | } | |
887 | } | |
888 | ||
889 | static char *parse_initiator_name(const char *target) | |
890 | { | |
891 | QemuOptsList *list; | |
892 | QemuOpts *opts; | |
893 | const char *name = NULL; | |
31459f46 | 894 | const char *iscsi_name = qemu_get_vm_name(); |
f9dadc98 RS |
895 | |
896 | list = qemu_find_opts("iscsi"); | |
f2ef4a6d PB |
897 | if (list) { |
898 | opts = qemu_opts_find(list, target); | |
f9dadc98 | 899 | if (!opts) { |
f2ef4a6d PB |
900 | opts = QTAILQ_FIRST(&list->head); |
901 | } | |
902 | if (opts) { | |
903 | name = qemu_opt_get(opts, "initiator-name"); | |
f9dadc98 RS |
904 | } |
905 | } | |
906 | ||
f2ef4a6d PB |
907 | if (name) { |
908 | return g_strdup(name); | |
909 | } else { | |
31459f46 RS |
910 | return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
911 | iscsi_name ? ":" : "", | |
912 | iscsi_name ? iscsi_name : ""); | |
f9dadc98 | 913 | } |
f9dadc98 RS |
914 | } |
915 | ||
c589b249 RS |
916 | /* |
917 | * We support iscsi url's on the form | |
918 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> | |
919 | */ | |
920 | static int iscsi_open(BlockDriverState *bs, const char *filename, int flags) | |
921 | { | |
922 | IscsiLun *iscsilun = bs->opaque; | |
923 | struct iscsi_context *iscsi = NULL; | |
924 | struct iscsi_url *iscsi_url = NULL; | |
925 | struct IscsiTask task; | |
f9dadc98 | 926 | char *initiator_name = NULL; |
c589b249 RS |
927 | int ret; |
928 | ||
929 | if ((BDRV_SECTOR_SIZE % 512) != 0) { | |
930 | error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. " | |
931 | "BDRV_SECTOR_SIZE(%lld) is not a multiple " | |
932 | "of 512", BDRV_SECTOR_SIZE); | |
933 | return -EINVAL; | |
934 | } | |
935 | ||
c589b249 RS |
936 | iscsi_url = iscsi_parse_full_url(iscsi, filename); |
937 | if (iscsi_url == NULL) { | |
938 | error_report("Failed to parse URL : %s %s", filename, | |
939 | iscsi_get_error(iscsi)); | |
940 | ret = -EINVAL; | |
b93c94f7 | 941 | goto out; |
c589b249 RS |
942 | } |
943 | ||
f9dadc98 RS |
944 | memset(iscsilun, 0, sizeof(IscsiLun)); |
945 | ||
946 | initiator_name = parse_initiator_name(iscsi_url->target); | |
947 | ||
948 | iscsi = iscsi_create_context(initiator_name); | |
949 | if (iscsi == NULL) { | |
950 | error_report("iSCSI: Failed to create iSCSI context."); | |
951 | ret = -ENOMEM; | |
b93c94f7 | 952 | goto out; |
f9dadc98 RS |
953 | } |
954 | ||
c589b249 RS |
955 | if (iscsi_set_targetname(iscsi, iscsi_url->target)) { |
956 | error_report("iSCSI: Failed to set target name."); | |
957 | ret = -EINVAL; | |
b93c94f7 | 958 | goto out; |
c589b249 RS |
959 | } |
960 | ||
961 | if (iscsi_url->user != NULL) { | |
962 | ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, | |
963 | iscsi_url->passwd); | |
964 | if (ret != 0) { | |
965 | error_report("Failed to set initiator username and password"); | |
966 | ret = -EINVAL; | |
b93c94f7 | 967 | goto out; |
c589b249 RS |
968 | } |
969 | } | |
f9dadc98 RS |
970 | |
971 | /* check if we got CHAP username/password via the options */ | |
972 | if (parse_chap(iscsi, iscsi_url->target) != 0) { | |
973 | error_report("iSCSI: Failed to set CHAP user/password"); | |
974 | ret = -EINVAL; | |
b93c94f7 | 975 | goto out; |
f9dadc98 RS |
976 | } |
977 | ||
c589b249 RS |
978 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
979 | error_report("iSCSI: Failed to set session type to normal."); | |
980 | ret = -EINVAL; | |
b93c94f7 | 981 | goto out; |
c589b249 RS |
982 | } |
983 | ||
984 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
985 | ||
f9dadc98 RS |
986 | /* check if we got HEADER_DIGEST via the options */ |
987 | parse_header_digest(iscsi, iscsi_url->target); | |
988 | ||
c589b249 RS |
989 | task.iscsilun = iscsilun; |
990 | task.status = 0; | |
991 | task.complete = 0; | |
992 | task.bs = bs; | |
993 | ||
994 | iscsilun->iscsi = iscsi; | |
995 | iscsilun->lun = iscsi_url->lun; | |
996 | ||
997 | if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun, | |
998 | iscsi_connect_cb, &task) | |
999 | != 0) { | |
1000 | error_report("iSCSI: Failed to start async connect."); | |
1001 | ret = -EINVAL; | |
b93c94f7 | 1002 | goto out; |
c589b249 RS |
1003 | } |
1004 | ||
1005 | while (!task.complete) { | |
1006 | iscsi_set_events(iscsilun); | |
1007 | qemu_aio_wait(); | |
1008 | } | |
1009 | if (task.status != 0) { | |
1010 | error_report("iSCSI: Failed to connect to LUN : %s", | |
1011 | iscsi_get_error(iscsi)); | |
1012 | ret = -EINVAL; | |
b93c94f7 | 1013 | goto out; |
c589b249 | 1014 | } |
622695a4 RS |
1015 | |
1016 | /* Medium changer or tape. We dont have any emulation for this so this must | |
1017 | * be sg ioctl compatible. We force it to be sg, otherwise qemu will try | |
1018 | * to read from the device to guess the image format. | |
1019 | */ | |
1020 | if (iscsilun->type == TYPE_MEDIUM_CHANGER || | |
1021 | iscsilun->type == TYPE_TAPE) { | |
1022 | bs->sg = 1; | |
1023 | } | |
1024 | ||
b93c94f7 | 1025 | ret = 0; |
c589b249 | 1026 | |
b93c94f7 | 1027 | out: |
f9dadc98 RS |
1028 | if (initiator_name != NULL) { |
1029 | g_free(initiator_name); | |
1030 | } | |
c589b249 RS |
1031 | if (iscsi_url != NULL) { |
1032 | iscsi_destroy_url(iscsi_url); | |
1033 | } | |
b93c94f7 PB |
1034 | |
1035 | if (ret) { | |
1036 | if (iscsi != NULL) { | |
1037 | iscsi_destroy_context(iscsi); | |
1038 | } | |
1039 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
c589b249 | 1040 | } |
c589b249 RS |
1041 | return ret; |
1042 | } | |
1043 | ||
1044 | static void iscsi_close(BlockDriverState *bs) | |
1045 | { | |
1046 | IscsiLun *iscsilun = bs->opaque; | |
1047 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1048 | ||
bafbd6a1 | 1049 | qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL); |
c589b249 RS |
1050 | iscsi_destroy_context(iscsi); |
1051 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
1052 | } | |
1053 | ||
1054 | static BlockDriver bdrv_iscsi = { | |
1055 | .format_name = "iscsi", | |
1056 | .protocol_name = "iscsi", | |
1057 | ||
1058 | .instance_size = sizeof(IscsiLun), | |
1059 | .bdrv_file_open = iscsi_open, | |
1060 | .bdrv_close = iscsi_close, | |
1061 | ||
1062 | .bdrv_getlength = iscsi_getlength, | |
1063 | ||
1064 | .bdrv_aio_readv = iscsi_aio_readv, | |
1065 | .bdrv_aio_writev = iscsi_aio_writev, | |
1066 | .bdrv_aio_flush = iscsi_aio_flush, | |
fa6acb0c RS |
1067 | |
1068 | .bdrv_aio_discard = iscsi_aio_discard, | |
98392453 RS |
1069 | |
1070 | #ifdef __linux__ | |
1071 | .bdrv_ioctl = iscsi_ioctl, | |
1072 | .bdrv_aio_ioctl = iscsi_aio_ioctl, | |
1073 | #endif | |
c589b249 RS |
1074 | }; |
1075 | ||
1076 | static void iscsi_block_init(void) | |
1077 | { | |
1078 | bdrv_register(&bdrv_iscsi); | |
1079 | } | |
1080 | ||
1081 | block_init(iscsi_block_init); |