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