]>
Commit | Line | Data |
---|---|---|
c589b249 RS |
1 | /* |
2 | * QEMU Block driver for iSCSI images | |
3 | * | |
4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com> | |
ec209aca | 5 | * Copyright (c) 2012-2014 Peter Lieven <pl@kamp.de> |
c589b249 RS |
6 | * |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include "config-host.h" | |
27 | ||
28 | #include <poll.h> | |
efc6de0d | 29 | #include <math.h> |
f4dfa67f | 30 | #include <arpa/inet.h> |
c589b249 | 31 | #include "qemu-common.h" |
1de7afc9 PB |
32 | #include "qemu/config-file.h" |
33 | #include "qemu/error-report.h" | |
b03c3805 PL |
34 | #include "qemu/bitops.h" |
35 | #include "qemu/bitmap.h" | |
737e150e | 36 | #include "block/block_int.h" |
c589b249 | 37 | #include "trace.h" |
0d09e41a | 38 | #include "block/scsi.h" |
0a53f010 | 39 | #include "qemu/iov.h" |
5accc840 PB |
40 | #include "sysemu/sysemu.h" |
41 | #include "qmp-commands.h" | |
c589b249 RS |
42 | |
43 | #include <iscsi/iscsi.h> | |
44 | #include <iscsi/scsi-lowlevel.h> | |
45 | ||
98392453 RS |
46 | #ifdef __linux__ |
47 | #include <scsi/sg.h> | |
0d09e41a | 48 | #include <block/scsi.h> |
98392453 | 49 | #endif |
c589b249 RS |
50 | |
51 | typedef struct IscsiLun { | |
52 | struct iscsi_context *iscsi; | |
80cf6257 | 53 | AioContext *aio_context; |
c589b249 | 54 | int lun; |
dbfff6d7 | 55 | enum scsi_inquiry_peripheral_device_type type; |
c589b249 | 56 | int block_size; |
c7b4a952 | 57 | uint64_t num_blocks; |
c9b9f682 | 58 | int events; |
5b5d34ec | 59 | QEMUTimer *nop_timer; |
f18a7cbb PL |
60 | uint8_t lbpme; |
61 | uint8_t lbprz; | |
fa6252b0 | 62 | uint8_t has_write_same; |
f18a7cbb PL |
63 | struct scsi_inquiry_logical_block_provisioning lbp; |
64 | struct scsi_inquiry_block_limits bl; | |
d4cd9615 | 65 | unsigned char *zeroblock; |
b03c3805 PL |
66 | unsigned long *allocationmap; |
67 | int cluster_sectors; | |
9281fe9e | 68 | bool use_16_for_rw; |
c589b249 RS |
69 | } IscsiLun; |
70 | ||
54a5c1d5 PL |
71 | typedef struct IscsiTask { |
72 | int status; | |
73 | int complete; | |
74 | int retries; | |
75 | int do_retry; | |
76 | struct scsi_task *task; | |
77 | Coroutine *co; | |
8b9dfe90 | 78 | QEMUBH *bh; |
80cf6257 | 79 | IscsiLun *iscsilun; |
efc6de0d | 80 | QEMUTimer retry_timer; |
54a5c1d5 PL |
81 | } IscsiTask; |
82 | ||
c589b249 RS |
83 | typedef struct IscsiAIOCB { |
84 | BlockDriverAIOCB common; | |
85 | QEMUIOVector *qiov; | |
86 | QEMUBH *bh; | |
87 | IscsiLun *iscsilun; | |
88 | struct scsi_task *task; | |
89 | uint8_t *buf; | |
90 | int status; | |
91 | int canceled; | |
1dde716e PL |
92 | int64_t sector_num; |
93 | int nb_sectors; | |
98392453 RS |
94 | #ifdef __linux__ |
95 | sg_io_hdr_t *ioh; | |
96 | #endif | |
c589b249 RS |
97 | } IscsiAIOCB; |
98 | ||
5b5d34ec PL |
99 | #define NOP_INTERVAL 5000 |
100 | #define MAX_NOP_FAILURES 3 | |
efc6de0d PL |
101 | #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) |
102 | static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048}; | |
5b5d34ec | 103 | |
5917af81 PL |
104 | /* this threshhold is a trade-off knob to choose between |
105 | * the potential additional overhead of an extra GET_LBA_STATUS request | |
106 | * vs. unnecessarily reading a lot of zero sectors over the wire. | |
107 | * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES | |
108 | * sectors we check the allocation status of the area covered by the | |
109 | * request first if the allocationmap indicates that the area might be | |
110 | * unallocated. */ | |
111 | #define ISCSI_CHECKALLOC_THRES 64 | |
5b5d34ec | 112 | |
27cbd828 | 113 | static void |
cfb3f506 | 114 | iscsi_bh_cb(void *p) |
27cbd828 PB |
115 | { |
116 | IscsiAIOCB *acb = p; | |
117 | ||
118 | qemu_bh_delete(acb->bh); | |
119 | ||
4790b03d PB |
120 | g_free(acb->buf); |
121 | acb->buf = NULL; | |
122 | ||
27cbd828 PB |
123 | if (acb->canceled == 0) { |
124 | acb->common.cb(acb->common.opaque, acb->status); | |
125 | } | |
126 | ||
1bd075f2 PB |
127 | if (acb->task != NULL) { |
128 | scsi_free_scsi_task(acb->task); | |
129 | acb->task = NULL; | |
130 | } | |
131 | ||
27cbd828 PB |
132 | qemu_aio_release(acb); |
133 | } | |
134 | ||
cfb3f506 PB |
135 | static void |
136 | iscsi_schedule_bh(IscsiAIOCB *acb) | |
27cbd828 | 137 | { |
1bd075f2 PB |
138 | if (acb->bh) { |
139 | return; | |
140 | } | |
80cf6257 | 141 | acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); |
27cbd828 | 142 | qemu_bh_schedule(acb->bh); |
27cbd828 PB |
143 | } |
144 | ||
8b9dfe90 PL |
145 | static void iscsi_co_generic_bh_cb(void *opaque) |
146 | { | |
147 | struct IscsiTask *iTask = opaque; | |
fcd470d8 | 148 | iTask->complete = 1; |
8b9dfe90 PL |
149 | qemu_bh_delete(iTask->bh); |
150 | qemu_coroutine_enter(iTask->co, NULL); | |
151 | } | |
152 | ||
efc6de0d PL |
153 | static void iscsi_retry_timer_expired(void *opaque) |
154 | { | |
155 | struct IscsiTask *iTask = opaque; | |
fcd470d8 | 156 | iTask->complete = 1; |
efc6de0d PL |
157 | if (iTask->co) { |
158 | qemu_coroutine_enter(iTask->co, NULL); | |
159 | } | |
160 | } | |
161 | ||
162 | static inline unsigned exp_random(double mean) | |
163 | { | |
164 | return -mean * log((double)rand() / RAND_MAX); | |
165 | } | |
166 | ||
54a5c1d5 PL |
167 | static void |
168 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, | |
169 | void *command_data, void *opaque) | |
170 | { | |
171 | struct IscsiTask *iTask = opaque; | |
172 | struct scsi_task *task = command_data; | |
173 | ||
54a5c1d5 PL |
174 | iTask->status = status; |
175 | iTask->do_retry = 0; | |
176 | iTask->task = task; | |
177 | ||
54a5c1d5 | 178 | if (status != SCSI_STATUS_GOOD) { |
efc6de0d PL |
179 | if (iTask->retries++ < ISCSI_CMD_RETRIES) { |
180 | if (status == SCSI_STATUS_CHECK_CONDITION | |
181 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
182 | error_report("iSCSI CheckCondition: %s", | |
183 | iscsi_get_error(iscsi)); | |
184 | iTask->do_retry = 1; | |
185 | goto out; | |
186 | } | |
187 | if (status == SCSI_STATUS_BUSY) { | |
188 | unsigned retry_time = | |
189 | exp_random(iscsi_retry_times[iTask->retries - 1]); | |
190 | error_report("iSCSI Busy (retry #%u in %u ms): %s", | |
191 | iTask->retries, retry_time, | |
192 | iscsi_get_error(iscsi)); | |
193 | aio_timer_init(iTask->iscsilun->aio_context, | |
194 | &iTask->retry_timer, QEMU_CLOCK_REALTIME, | |
195 | SCALE_MS, iscsi_retry_timer_expired, iTask); | |
196 | timer_mod(&iTask->retry_timer, | |
197 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); | |
198 | iTask->do_retry = 1; | |
199 | return; | |
200 | } | |
201 | } | |
837c3901 | 202 | error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); |
54a5c1d5 PL |
203 | } |
204 | ||
205 | out: | |
206 | if (iTask->co) { | |
80cf6257 SH |
207 | iTask->bh = aio_bh_new(iTask->iscsilun->aio_context, |
208 | iscsi_co_generic_bh_cb, iTask); | |
8b9dfe90 | 209 | qemu_bh_schedule(iTask->bh); |
fcd470d8 PL |
210 | } else { |
211 | iTask->complete = 1; | |
54a5c1d5 PL |
212 | } |
213 | } | |
214 | ||
215 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) | |
216 | { | |
217 | *iTask = (struct IscsiTask) { | |
efc6de0d | 218 | .co = qemu_coroutine_self(), |
efc6de0d | 219 | .iscsilun = iscsilun, |
54a5c1d5 PL |
220 | }; |
221 | } | |
27cbd828 | 222 | |
c589b249 RS |
223 | static void |
224 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
225 | void *private_data) | |
226 | { | |
1bd075f2 PB |
227 | IscsiAIOCB *acb = private_data; |
228 | ||
229 | acb->status = -ECANCELED; | |
230 | iscsi_schedule_bh(acb); | |
c589b249 RS |
231 | } |
232 | ||
233 | static void | |
234 | iscsi_aio_cancel(BlockDriverAIOCB *blockacb) | |
235 | { | |
236 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; | |
237 | IscsiLun *iscsilun = acb->iscsilun; | |
238 | ||
1bd075f2 PB |
239 | if (acb->status != -EINPROGRESS) { |
240 | return; | |
241 | } | |
242 | ||
b2090919 | 243 | acb->canceled = 1; |
c589b249 | 244 | |
b2090919 | 245 | /* send a task mgmt call to the target to cancel the task on the target */ |
64e69e80 | 246 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
1bd075f2 | 247 | iscsi_abort_task_cb, acb); |
b2090919 | 248 | |
1bd075f2 | 249 | while (acb->status == -EINPROGRESS) { |
80cf6257 | 250 | aio_poll(iscsilun->aio_context, true); |
1bd075f2 | 251 | } |
c589b249 RS |
252 | } |
253 | ||
d7331bed | 254 | static const AIOCBInfo iscsi_aiocb_info = { |
c589b249 RS |
255 | .aiocb_size = sizeof(IscsiAIOCB), |
256 | .cancel = iscsi_aio_cancel, | |
257 | }; | |
258 | ||
259 | ||
260 | static void iscsi_process_read(void *arg); | |
261 | static void iscsi_process_write(void *arg); | |
262 | ||
c589b249 RS |
263 | static void |
264 | iscsi_set_events(IscsiLun *iscsilun) | |
265 | { | |
266 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
c9b9f682 RS |
267 | int ev; |
268 | ||
269 | /* We always register a read handler. */ | |
270 | ev = POLLIN; | |
271 | ev |= iscsi_which_events(iscsi); | |
272 | if (ev != iscsilun->events) { | |
80cf6257 SH |
273 | aio_set_fd_handler(iscsilun->aio_context, |
274 | iscsi_get_fd(iscsi), | |
275 | iscsi_process_read, | |
276 | (ev & POLLOUT) ? iscsi_process_write : NULL, | |
277 | iscsilun); | |
c9b9f682 RS |
278 | |
279 | } | |
280 | ||
c9b9f682 | 281 | iscsilun->events = ev; |
c589b249 RS |
282 | } |
283 | ||
284 | static void | |
285 | iscsi_process_read(void *arg) | |
286 | { | |
287 | IscsiLun *iscsilun = arg; | |
288 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
289 | ||
290 | iscsi_service(iscsi, POLLIN); | |
291 | iscsi_set_events(iscsilun); | |
292 | } | |
293 | ||
294 | static void | |
295 | iscsi_process_write(void *arg) | |
296 | { | |
297 | IscsiLun *iscsilun = arg; | |
298 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
299 | ||
300 | iscsi_service(iscsi, POLLOUT); | |
301 | iscsi_set_events(iscsilun); | |
302 | } | |
303 | ||
0777b5dd PL |
304 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
305 | { | |
306 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; | |
307 | } | |
308 | ||
c589b249 RS |
309 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) |
310 | { | |
311 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
312 | } | |
313 | ||
91bea4e2 PL |
314 | static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, |
315 | IscsiLun *iscsilun) | |
316 | { | |
317 | if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size || | |
318 | (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) { | |
f5075224 RJ |
319 | error_report("iSCSI misaligned request: " |
320 | "iscsilun->block_size %u, sector_num %" PRIi64 | |
321 | ", nb_sectors %d", | |
91bea4e2 PL |
322 | iscsilun->block_size, sector_num, nb_sectors); |
323 | return 0; | |
324 | } | |
325 | return 1; | |
326 | } | |
327 | ||
b03c3805 PL |
328 | static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num, |
329 | int nb_sectors) | |
330 | { | |
331 | if (iscsilun->allocationmap == NULL) { | |
332 | return; | |
333 | } | |
334 | bitmap_set(iscsilun->allocationmap, | |
335 | sector_num / iscsilun->cluster_sectors, | |
336 | DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors)); | |
337 | } | |
338 | ||
339 | static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num, | |
340 | int nb_sectors) | |
341 | { | |
342 | int64_t cluster_num, nb_clusters; | |
343 | if (iscsilun->allocationmap == NULL) { | |
344 | return; | |
345 | } | |
346 | cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors); | |
347 | nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors | |
348 | - cluster_num; | |
349 | if (nb_clusters > 0) { | |
350 | bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters); | |
351 | } | |
352 | } | |
353 | ||
063c3378 PL |
354 | static int coroutine_fn iscsi_co_writev(BlockDriverState *bs, |
355 | int64_t sector_num, int nb_sectors, | |
356 | QEMUIOVector *iov) | |
c589b249 | 357 | { |
063c3378 PL |
358 | IscsiLun *iscsilun = bs->opaque; |
359 | struct IscsiTask iTask; | |
f4dfa67f | 360 | uint64_t lba; |
063c3378 PL |
361 | uint32_t num_sectors; |
362 | uint8_t *data = NULL; | |
363 | uint8_t *buf = NULL; | |
c589b249 | 364 | |
063c3378 PL |
365 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
366 | return -EINVAL; | |
367 | } | |
7371d56f | 368 | |
063c3378 PL |
369 | lba = sector_qemu2lun(sector_num, iscsilun); |
370 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
063c3378 PL |
371 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
372 | retry: | |
9281fe9e PL |
373 | if (iscsilun->use_16_for_rw) { |
374 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
375 | data, num_sectors * iscsilun->block_size, | |
376 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
377 | iscsi_co_generic_cb, &iTask); | |
378 | } else { | |
379 | iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
380 | data, num_sectors * iscsilun->block_size, | |
381 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
382 | iscsi_co_generic_cb, &iTask); | |
383 | } | |
063c3378 PL |
384 | if (iTask.task == NULL) { |
385 | g_free(buf); | |
92397116 | 386 | return -ENOMEM; |
f4dfa67f | 387 | } |
063c3378 PL |
388 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, |
389 | iov->niov); | |
063c3378 PL |
390 | while (!iTask.complete) { |
391 | iscsi_set_events(iscsilun); | |
392 | qemu_coroutine_yield(); | |
c589b249 RS |
393 | } |
394 | ||
063c3378 PL |
395 | if (iTask.task != NULL) { |
396 | scsi_free_scsi_task(iTask.task); | |
397 | iTask.task = NULL; | |
91bea4e2 PL |
398 | } |
399 | ||
063c3378 | 400 | if (iTask.do_retry) { |
837c3901 | 401 | iTask.complete = 0; |
063c3378 | 402 | goto retry; |
1dde716e PL |
403 | } |
404 | ||
063c3378 | 405 | g_free(buf); |
c589b249 | 406 | |
063c3378 PL |
407 | if (iTask.status != SCSI_STATUS_GOOD) { |
408 | return -EIO; | |
c589b249 RS |
409 | } |
410 | ||
b03c3805 PL |
411 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); |
412 | ||
063c3378 | 413 | return 0; |
c589b249 RS |
414 | } |
415 | ||
b03c3805 PL |
416 | |
417 | static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun, | |
418 | int64_t sector_num, int nb_sectors) | |
419 | { | |
420 | unsigned long size; | |
421 | if (iscsilun->allocationmap == NULL) { | |
422 | return true; | |
423 | } | |
424 | size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); | |
425 | return !(find_next_bit(iscsilun->allocationmap, size, | |
426 | sector_num / iscsilun->cluster_sectors) == size); | |
427 | } | |
428 | ||
b03c3805 PL |
429 | static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, |
430 | int64_t sector_num, | |
431 | int nb_sectors, int *pnum) | |
432 | { | |
433 | IscsiLun *iscsilun = bs->opaque; | |
434 | struct scsi_get_lba_status *lbas = NULL; | |
435 | struct scsi_lba_status_descriptor *lbasd = NULL; | |
436 | struct IscsiTask iTask; | |
437 | int64_t ret; | |
438 | ||
439 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
440 | ||
441 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
442 | ret = -EINVAL; | |
443 | goto out; | |
444 | } | |
445 | ||
446 | /* default to all sectors allocated */ | |
447 | ret = BDRV_BLOCK_DATA; | |
448 | ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; | |
449 | *pnum = nb_sectors; | |
450 | ||
451 | /* LUN does not support logical block provisioning */ | |
452 | if (iscsilun->lbpme == 0) { | |
453 | goto out; | |
454 | } | |
455 | ||
456 | retry: | |
457 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, | |
458 | sector_qemu2lun(sector_num, iscsilun), | |
459 | 8 + 16, iscsi_co_generic_cb, | |
460 | &iTask) == NULL) { | |
461 | ret = -ENOMEM; | |
462 | goto out; | |
463 | } | |
464 | ||
465 | while (!iTask.complete) { | |
466 | iscsi_set_events(iscsilun); | |
467 | qemu_coroutine_yield(); | |
468 | } | |
469 | ||
470 | if (iTask.do_retry) { | |
471 | if (iTask.task != NULL) { | |
472 | scsi_free_scsi_task(iTask.task); | |
473 | iTask.task = NULL; | |
474 | } | |
475 | iTask.complete = 0; | |
476 | goto retry; | |
477 | } | |
478 | ||
479 | if (iTask.status != SCSI_STATUS_GOOD) { | |
480 | /* in case the get_lba_status_callout fails (i.e. | |
481 | * because the device is busy or the cmd is not | |
482 | * supported) we pretend all blocks are allocated | |
483 | * for backwards compatibility */ | |
484 | goto out; | |
485 | } | |
486 | ||
487 | lbas = scsi_datain_unmarshall(iTask.task); | |
488 | if (lbas == NULL) { | |
489 | ret = -EIO; | |
490 | goto out; | |
491 | } | |
492 | ||
493 | lbasd = &lbas->descriptors[0]; | |
494 | ||
495 | if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { | |
496 | ret = -EIO; | |
497 | goto out; | |
498 | } | |
499 | ||
500 | *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); | |
501 | ||
502 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || | |
503 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { | |
504 | ret &= ~BDRV_BLOCK_DATA; | |
505 | if (iscsilun->lbprz) { | |
506 | ret |= BDRV_BLOCK_ZERO; | |
507 | } | |
508 | } | |
509 | ||
510 | if (ret & BDRV_BLOCK_ZERO) { | |
511 | iscsi_allocationmap_clear(iscsilun, sector_num, *pnum); | |
512 | } else { | |
513 | iscsi_allocationmap_set(iscsilun, sector_num, *pnum); | |
514 | } | |
515 | ||
516 | if (*pnum > nb_sectors) { | |
517 | *pnum = nb_sectors; | |
518 | } | |
519 | out: | |
520 | if (iTask.task != NULL) { | |
521 | scsi_free_scsi_task(iTask.task); | |
522 | } | |
523 | return ret; | |
524 | } | |
525 | ||
063c3378 PL |
526 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, |
527 | int64_t sector_num, int nb_sectors, | |
528 | QEMUIOVector *iov) | |
c589b249 | 529 | { |
063c3378 PL |
530 | IscsiLun *iscsilun = bs->opaque; |
531 | struct IscsiTask iTask; | |
1dde716e PL |
532 | uint64_t lba; |
533 | uint32_t num_sectors; | |
c589b249 | 534 | |
063c3378 PL |
535 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
536 | return -EINVAL; | |
f4dfa67f | 537 | } |
f4dfa67f | 538 | |
5917af81 | 539 | if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES && |
b03c3805 PL |
540 | !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) { |
541 | int64_t ret; | |
542 | int pnum; | |
543 | ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum); | |
544 | if (ret < 0) { | |
545 | return ret; | |
546 | } | |
547 | if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) { | |
548 | qemu_iovec_memset(iov, 0, 0x00, iov->size); | |
549 | return 0; | |
550 | } | |
551 | } | |
b03c3805 | 552 | |
063c3378 PL |
553 | lba = sector_qemu2lun(sector_num, iscsilun); |
554 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
f4dfa67f | 555 | |
063c3378 PL |
556 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
557 | retry: | |
9281fe9e | 558 | if (iscsilun->use_16_for_rw) { |
063c3378 PL |
559 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, |
560 | num_sectors * iscsilun->block_size, | |
561 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
562 | iscsi_co_generic_cb, &iTask); | |
9281fe9e | 563 | } else { |
063c3378 PL |
564 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, |
565 | num_sectors * iscsilun->block_size, | |
219c2521 | 566 | iscsilun->block_size, |
219c2521 | 567 | 0, 0, 0, 0, 0, |
063c3378 | 568 | iscsi_co_generic_cb, &iTask); |
f4dfa67f | 569 | } |
063c3378 | 570 | if (iTask.task == NULL) { |
92397116 | 571 | return -ENOMEM; |
c589b249 | 572 | } |
063c3378 | 573 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); |
91bea4e2 | 574 | |
063c3378 PL |
575 | while (!iTask.complete) { |
576 | iscsi_set_events(iscsilun); | |
577 | qemu_coroutine_yield(); | |
1dde716e | 578 | } |
c589b249 | 579 | |
063c3378 PL |
580 | if (iTask.task != NULL) { |
581 | scsi_free_scsi_task(iTask.task); | |
582 | iTask.task = NULL; | |
c589b249 RS |
583 | } |
584 | ||
063c3378 | 585 | if (iTask.do_retry) { |
837c3901 | 586 | iTask.complete = 0; |
063c3378 | 587 | goto retry; |
c589b249 RS |
588 | } |
589 | ||
063c3378 PL |
590 | if (iTask.status != SCSI_STATUS_GOOD) { |
591 | return -EIO; | |
1dde716e PL |
592 | } |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
063c3378 | 597 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) |
1dde716e PL |
598 | { |
599 | IscsiLun *iscsilun = bs->opaque; | |
063c3378 | 600 | struct IscsiTask iTask; |
1dde716e | 601 | |
b2f9c08a PB |
602 | if (bs->sg) { |
603 | return 0; | |
604 | } | |
605 | ||
063c3378 | 606 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
1dde716e | 607 | |
063c3378 PL |
608 | retry: |
609 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, | |
610 | 0, iscsi_co_generic_cb, &iTask) == NULL) { | |
92397116 | 611 | return -ENOMEM; |
063c3378 | 612 | } |
1dde716e | 613 | |
063c3378 PL |
614 | while (!iTask.complete) { |
615 | iscsi_set_events(iscsilun); | |
616 | qemu_coroutine_yield(); | |
617 | } | |
1dde716e | 618 | |
063c3378 PL |
619 | if (iTask.task != NULL) { |
620 | scsi_free_scsi_task(iTask.task); | |
621 | iTask.task = NULL; | |
c589b249 RS |
622 | } |
623 | ||
063c3378 | 624 | if (iTask.do_retry) { |
837c3901 | 625 | iTask.complete = 0; |
063c3378 PL |
626 | goto retry; |
627 | } | |
c589b249 | 628 | |
063c3378 PL |
629 | if (iTask.status != SCSI_STATUS_GOOD) { |
630 | return -EIO; | |
631 | } | |
632 | ||
633 | return 0; | |
c589b249 RS |
634 | } |
635 | ||
98392453 RS |
636 | #ifdef __linux__ |
637 | static void | |
638 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, | |
639 | void *command_data, void *opaque) | |
640 | { | |
641 | IscsiAIOCB *acb = opaque; | |
642 | ||
0a53f010 RS |
643 | g_free(acb->buf); |
644 | acb->buf = NULL; | |
645 | ||
b2090919 | 646 | if (acb->canceled != 0) { |
98392453 RS |
647 | return; |
648 | } | |
649 | ||
650 | acb->status = 0; | |
651 | if (status < 0) { | |
652 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", | |
653 | iscsi_get_error(iscsi)); | |
654 | acb->status = -EIO; | |
655 | } | |
656 | ||
657 | acb->ioh->driver_status = 0; | |
658 | acb->ioh->host_status = 0; | |
659 | acb->ioh->resid = 0; | |
660 | ||
661 | #define SG_ERR_DRIVER_SENSE 0x08 | |
662 | ||
663 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { | |
664 | int ss; | |
665 | ||
666 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; | |
667 | ||
668 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; | |
669 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? | |
670 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; | |
671 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); | |
672 | } | |
673 | ||
cfb3f506 | 674 | iscsi_schedule_bh(acb); |
98392453 RS |
675 | } |
676 | ||
677 | static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, | |
678 | unsigned long int req, void *buf, | |
679 | BlockDriverCompletionFunc *cb, void *opaque) | |
680 | { | |
681 | IscsiLun *iscsilun = bs->opaque; | |
682 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
683 | struct iscsi_data data; | |
684 | IscsiAIOCB *acb; | |
685 | ||
686 | assert(req == SG_IO); | |
687 | ||
d7331bed | 688 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); |
98392453 RS |
689 | |
690 | acb->iscsilun = iscsilun; | |
691 | acb->canceled = 0; | |
1bd075f2 PB |
692 | acb->bh = NULL; |
693 | acb->status = -EINPROGRESS; | |
98392453 RS |
694 | acb->buf = NULL; |
695 | acb->ioh = buf; | |
696 | ||
697 | acb->task = malloc(sizeof(struct scsi_task)); | |
698 | if (acb->task == NULL) { | |
699 | error_report("iSCSI: Failed to allocate task for scsi command. %s", | |
700 | iscsi_get_error(iscsi)); | |
701 | qemu_aio_release(acb); | |
702 | return NULL; | |
703 | } | |
704 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
705 | ||
706 | switch (acb->ioh->dxfer_direction) { | |
707 | case SG_DXFER_TO_DEV: | |
708 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
709 | break; | |
710 | case SG_DXFER_FROM_DEV: | |
711 | acb->task->xfer_dir = SCSI_XFER_READ; | |
712 | break; | |
713 | default: | |
714 | acb->task->xfer_dir = SCSI_XFER_NONE; | |
715 | break; | |
716 | } | |
717 | ||
718 | acb->task->cdb_size = acb->ioh->cmd_len; | |
719 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); | |
720 | acb->task->expxferlen = acb->ioh->dxfer_len; | |
721 | ||
0a53f010 | 722 | data.size = 0; |
98392453 | 723 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { |
0a53f010 RS |
724 | if (acb->ioh->iovec_count == 0) { |
725 | data.data = acb->ioh->dxferp; | |
726 | data.size = acb->ioh->dxfer_len; | |
727 | } else { | |
0a53f010 RS |
728 | scsi_task_set_iov_out(acb->task, |
729 | (struct scsi_iovec *) acb->ioh->dxferp, | |
730 | acb->ioh->iovec_count); | |
0a53f010 | 731 | } |
98392453 | 732 | } |
0a53f010 | 733 | |
98392453 RS |
734 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, |
735 | iscsi_aio_ioctl_cb, | |
0a53f010 | 736 | (data.size > 0) ? &data : NULL, |
98392453 RS |
737 | acb) != 0) { |
738 | scsi_free_scsi_task(acb->task); | |
739 | qemu_aio_release(acb); | |
740 | return NULL; | |
741 | } | |
742 | ||
743 | /* tell libiscsi to read straight into the buffer we got from ioctl */ | |
744 | if (acb->task->xfer_dir == SCSI_XFER_READ) { | |
0a53f010 RS |
745 | if (acb->ioh->iovec_count == 0) { |
746 | scsi_task_add_data_in_buffer(acb->task, | |
747 | acb->ioh->dxfer_len, | |
748 | acb->ioh->dxferp); | |
749 | } else { | |
0a53f010 RS |
750 | scsi_task_set_iov_in(acb->task, |
751 | (struct scsi_iovec *) acb->ioh->dxferp, | |
752 | acb->ioh->iovec_count); | |
0a53f010 | 753 | } |
98392453 RS |
754 | } |
755 | ||
756 | iscsi_set_events(iscsilun); | |
757 | ||
758 | return &acb->common; | |
759 | } | |
760 | ||
f1a12821 RS |
761 | static void ioctl_cb(void *opaque, int status) |
762 | { | |
763 | int *p_status = opaque; | |
764 | *p_status = status; | |
765 | } | |
766 | ||
98392453 RS |
767 | static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
768 | { | |
769 | IscsiLun *iscsilun = bs->opaque; | |
f1a12821 | 770 | int status; |
98392453 RS |
771 | |
772 | switch (req) { | |
773 | case SG_GET_VERSION_NUM: | |
774 | *(int *)buf = 30000; | |
775 | break; | |
776 | case SG_GET_SCSI_ID: | |
777 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; | |
778 | break; | |
f1a12821 RS |
779 | case SG_IO: |
780 | status = -EINPROGRESS; | |
781 | iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status); | |
782 | ||
783 | while (status == -EINPROGRESS) { | |
80cf6257 | 784 | aio_poll(iscsilun->aio_context, true); |
f1a12821 RS |
785 | } |
786 | ||
787 | return 0; | |
98392453 RS |
788 | default: |
789 | return -1; | |
790 | } | |
791 | return 0; | |
792 | } | |
793 | #endif | |
794 | ||
c589b249 RS |
795 | static int64_t |
796 | iscsi_getlength(BlockDriverState *bs) | |
797 | { | |
798 | IscsiLun *iscsilun = bs->opaque; | |
799 | int64_t len; | |
800 | ||
801 | len = iscsilun->num_blocks; | |
802 | len *= iscsilun->block_size; | |
803 | ||
804 | return len; | |
805 | } | |
806 | ||
65f3e339 PL |
807 | static int |
808 | coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num, | |
809 | int nb_sectors) | |
810 | { | |
811 | IscsiLun *iscsilun = bs->opaque; | |
812 | struct IscsiTask iTask; | |
813 | struct unmap_list list; | |
65f3e339 PL |
814 | |
815 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
816 | return -EINVAL; | |
817 | } | |
818 | ||
819 | if (!iscsilun->lbp.lbpu) { | |
820 | /* UNMAP is not supported by the target */ | |
821 | return 0; | |
822 | } | |
823 | ||
824 | list.lba = sector_qemu2lun(sector_num, iscsilun); | |
01a6a238 | 825 | list.num = sector_qemu2lun(nb_sectors, iscsilun); |
65f3e339 | 826 | |
01a6a238 | 827 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
65f3e339 | 828 | retry: |
01a6a238 PL |
829 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, |
830 | iscsi_co_generic_cb, &iTask) == NULL) { | |
92397116 | 831 | return -ENOMEM; |
01a6a238 | 832 | } |
65f3e339 | 833 | |
01a6a238 PL |
834 | while (!iTask.complete) { |
835 | iscsi_set_events(iscsilun); | |
836 | qemu_coroutine_yield(); | |
837 | } | |
65f3e339 | 838 | |
01a6a238 PL |
839 | if (iTask.task != NULL) { |
840 | scsi_free_scsi_task(iTask.task); | |
841 | iTask.task = NULL; | |
842 | } | |
65f3e339 | 843 | |
01a6a238 | 844 | if (iTask.do_retry) { |
837c3901 | 845 | iTask.complete = 0; |
01a6a238 PL |
846 | goto retry; |
847 | } | |
65f3e339 | 848 | |
01a6a238 PL |
849 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { |
850 | /* the target might fail with a check condition if it | |
851 | is not happy with the alignment of the UNMAP request | |
852 | we silently fail in this case */ | |
853 | return 0; | |
854 | } | |
65f3e339 | 855 | |
01a6a238 PL |
856 | if (iTask.status != SCSI_STATUS_GOOD) { |
857 | return -EIO; | |
65f3e339 PL |
858 | } |
859 | ||
b03c3805 PL |
860 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); |
861 | ||
65f3e339 PL |
862 | return 0; |
863 | } | |
864 | ||
d4cd9615 PL |
865 | static int |
866 | coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, | |
867 | int nb_sectors, BdrvRequestFlags flags) | |
868 | { | |
869 | IscsiLun *iscsilun = bs->opaque; | |
870 | struct IscsiTask iTask; | |
871 | uint64_t lba; | |
872 | uint32_t nb_blocks; | |
9281fe9e | 873 | bool use_16_for_ws = iscsilun->use_16_for_rw; |
d4cd9615 PL |
874 | |
875 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
876 | return -EINVAL; | |
877 | } | |
878 | ||
9281fe9e PL |
879 | if (flags & BDRV_REQ_MAY_UNMAP) { |
880 | if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { | |
881 | /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ | |
882 | use_16_for_ws = true; | |
883 | } | |
884 | if (use_16_for_ws && !iscsilun->lbp.lbpws) { | |
885 | /* WRITESAME16 with UNMAP is not supported by the target, | |
886 | * fall back and try WRITESAME10/16 without UNMAP */ | |
887 | flags &= ~BDRV_REQ_MAY_UNMAP; | |
888 | use_16_for_ws = iscsilun->use_16_for_rw; | |
889 | } | |
fa6252b0 PB |
890 | } |
891 | ||
dbe5c58f | 892 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { |
9281fe9e | 893 | /* WRITESAME without UNMAP is not supported by the target */ |
d4cd9615 PL |
894 | return -ENOTSUP; |
895 | } | |
896 | ||
897 | lba = sector_qemu2lun(sector_num, iscsilun); | |
898 | nb_blocks = sector_qemu2lun(nb_sectors, iscsilun); | |
899 | ||
900 | if (iscsilun->zeroblock == NULL) { | |
901 | iscsilun->zeroblock = g_malloc0(iscsilun->block_size); | |
902 | } | |
903 | ||
904 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
905 | retry: | |
9281fe9e PL |
906 | if (use_16_for_ws) { |
907 | iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
908 | iscsilun->zeroblock, iscsilun->block_size, | |
909 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
910 | 0, 0, iscsi_co_generic_cb, &iTask); | |
911 | } else { | |
912 | iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
913 | iscsilun->zeroblock, iscsilun->block_size, | |
914 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
915 | 0, 0, iscsi_co_generic_cb, &iTask); | |
916 | } | |
917 | if (iTask.task == NULL) { | |
92397116 | 918 | return -ENOMEM; |
d4cd9615 PL |
919 | } |
920 | ||
921 | while (!iTask.complete) { | |
922 | iscsi_set_events(iscsilun); | |
923 | qemu_coroutine_yield(); | |
924 | } | |
925 | ||
d9738fd2 PL |
926 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && |
927 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && | |
27898a5d PB |
928 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || |
929 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { | |
d9738fd2 PL |
930 | /* WRITE SAME is not supported by the target */ |
931 | iscsilun->has_write_same = false; | |
932 | scsi_free_scsi_task(iTask.task); | |
933 | return -ENOTSUP; | |
934 | } | |
935 | ||
d4cd9615 PL |
936 | if (iTask.task != NULL) { |
937 | scsi_free_scsi_task(iTask.task); | |
938 | iTask.task = NULL; | |
939 | } | |
940 | ||
941 | if (iTask.do_retry) { | |
837c3901 | 942 | iTask.complete = 0; |
d4cd9615 PL |
943 | goto retry; |
944 | } | |
945 | ||
946 | if (iTask.status != SCSI_STATUS_GOOD) { | |
947 | return -EIO; | |
948 | } | |
949 | ||
b03c3805 PL |
950 | if (flags & BDRV_REQ_MAY_UNMAP) { |
951 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); | |
952 | } else { | |
953 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); | |
954 | } | |
955 | ||
d4cd9615 PL |
956 | return 0; |
957 | } | |
958 | ||
f2917853 PB |
959 | static void parse_chap(struct iscsi_context *iscsi, const char *target, |
960 | Error **errp) | |
f9dadc98 RS |
961 | { |
962 | QemuOptsList *list; | |
963 | QemuOpts *opts; | |
964 | const char *user = NULL; | |
965 | const char *password = NULL; | |
966 | ||
967 | list = qemu_find_opts("iscsi"); | |
968 | if (!list) { | |
f2917853 | 969 | return; |
f9dadc98 RS |
970 | } |
971 | ||
972 | opts = qemu_opts_find(list, target); | |
973 | if (opts == NULL) { | |
974 | opts = QTAILQ_FIRST(&list->head); | |
975 | if (!opts) { | |
f2917853 | 976 | return; |
f9dadc98 RS |
977 | } |
978 | } | |
979 | ||
980 | user = qemu_opt_get(opts, "user"); | |
981 | if (!user) { | |
f2917853 | 982 | return; |
f9dadc98 RS |
983 | } |
984 | ||
985 | password = qemu_opt_get(opts, "password"); | |
986 | if (!password) { | |
f2917853 PB |
987 | error_setg(errp, "CHAP username specified but no password was given"); |
988 | return; | |
f9dadc98 RS |
989 | } |
990 | ||
991 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { | |
f2917853 | 992 | error_setg(errp, "Failed to set initiator username and password"); |
f9dadc98 | 993 | } |
f9dadc98 RS |
994 | } |
995 | ||
f2917853 PB |
996 | static void parse_header_digest(struct iscsi_context *iscsi, const char *target, |
997 | Error **errp) | |
f9dadc98 RS |
998 | { |
999 | QemuOptsList *list; | |
1000 | QemuOpts *opts; | |
1001 | const char *digest = NULL; | |
1002 | ||
1003 | list = qemu_find_opts("iscsi"); | |
1004 | if (!list) { | |
1005 | return; | |
1006 | } | |
1007 | ||
1008 | opts = qemu_opts_find(list, target); | |
1009 | if (opts == NULL) { | |
1010 | opts = QTAILQ_FIRST(&list->head); | |
1011 | if (!opts) { | |
1012 | return; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | digest = qemu_opt_get(opts, "header-digest"); | |
1017 | if (!digest) { | |
1018 | return; | |
1019 | } | |
1020 | ||
1021 | if (!strcmp(digest, "CRC32C")) { | |
1022 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); | |
1023 | } else if (!strcmp(digest, "NONE")) { | |
1024 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); | |
1025 | } else if (!strcmp(digest, "CRC32C-NONE")) { | |
1026 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); | |
1027 | } else if (!strcmp(digest, "NONE-CRC32C")) { | |
1028 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
1029 | } else { | |
f2917853 | 1030 | error_setg(errp, "Invalid header-digest setting : %s", digest); |
f9dadc98 RS |
1031 | } |
1032 | } | |
1033 | ||
1034 | static char *parse_initiator_name(const char *target) | |
1035 | { | |
1036 | QemuOptsList *list; | |
1037 | QemuOpts *opts; | |
5accc840 PB |
1038 | const char *name; |
1039 | char *iscsi_name; | |
1040 | UuidInfo *uuid_info; | |
f9dadc98 RS |
1041 | |
1042 | list = qemu_find_opts("iscsi"); | |
f2ef4a6d PB |
1043 | if (list) { |
1044 | opts = qemu_opts_find(list, target); | |
f9dadc98 | 1045 | if (!opts) { |
f2ef4a6d PB |
1046 | opts = QTAILQ_FIRST(&list->head); |
1047 | } | |
1048 | if (opts) { | |
1049 | name = qemu_opt_get(opts, "initiator-name"); | |
5accc840 PB |
1050 | if (name) { |
1051 | return g_strdup(name); | |
1052 | } | |
f9dadc98 RS |
1053 | } |
1054 | } | |
1055 | ||
5accc840 PB |
1056 | uuid_info = qmp_query_uuid(NULL); |
1057 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { | |
1058 | name = qemu_get_vm_name(); | |
f2ef4a6d | 1059 | } else { |
5accc840 | 1060 | name = uuid_info->UUID; |
f9dadc98 | 1061 | } |
5accc840 PB |
1062 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
1063 | name ? ":" : "", name ? name : ""); | |
1064 | qapi_free_UuidInfo(uuid_info); | |
1065 | return iscsi_name; | |
f9dadc98 RS |
1066 | } |
1067 | ||
5b5d34ec PL |
1068 | static void iscsi_nop_timed_event(void *opaque) |
1069 | { | |
1070 | IscsiLun *iscsilun = opaque; | |
1071 | ||
1072 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) { | |
1073 | error_report("iSCSI: NOP timeout. Reconnecting..."); | |
1074 | iscsi_reconnect(iscsilun->iscsi); | |
1075 | } | |
1076 | ||
1077 | if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { | |
1078 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); | |
1079 | return; | |
1080 | } | |
1081 | ||
bc72ad67 | 1082 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
5b5d34ec PL |
1083 | iscsi_set_events(iscsilun); |
1084 | } | |
5b5d34ec | 1085 | |
f2917853 | 1086 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) |
cb1b83e7 PL |
1087 | { |
1088 | struct scsi_task *task = NULL; | |
1089 | struct scsi_readcapacity10 *rc10 = NULL; | |
1090 | struct scsi_readcapacity16 *rc16 = NULL; | |
cb1b83e7 PL |
1091 | int retries = ISCSI_CMD_RETRIES; |
1092 | ||
1288844e PB |
1093 | do { |
1094 | if (task != NULL) { | |
1095 | scsi_free_scsi_task(task); | |
1096 | task = NULL; | |
cb1b83e7 | 1097 | } |
1288844e PB |
1098 | |
1099 | switch (iscsilun->type) { | |
1100 | case TYPE_DISK: | |
1101 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); | |
1102 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1103 | rc16 = scsi_datain_unmarshall(task); | |
1104 | if (rc16 == NULL) { | |
f2917853 | 1105 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); |
1288844e PB |
1106 | } else { |
1107 | iscsilun->block_size = rc16->block_length; | |
1108 | iscsilun->num_blocks = rc16->returned_lba + 1; | |
f18a7cbb PL |
1109 | iscsilun->lbpme = rc16->lbpme; |
1110 | iscsilun->lbprz = rc16->lbprz; | |
9281fe9e | 1111 | iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); |
1288844e PB |
1112 | } |
1113 | } | |
1114 | break; | |
1115 | case TYPE_ROM: | |
1116 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); | |
1117 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1118 | rc10 = scsi_datain_unmarshall(task); | |
1119 | if (rc10 == NULL) { | |
f2917853 | 1120 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); |
1288844e PB |
1121 | } else { |
1122 | iscsilun->block_size = rc10->block_size; | |
1123 | if (rc10->lba == 0) { | |
1124 | /* blank disk loaded */ | |
1125 | iscsilun->num_blocks = 0; | |
1126 | } else { | |
1127 | iscsilun->num_blocks = rc10->lba + 1; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | break; | |
1132 | default: | |
f2917853 | 1133 | return; |
cb1b83e7 | 1134 | } |
1288844e PB |
1135 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
1136 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION | |
1137 | && retries-- > 0); | |
cb1b83e7 | 1138 | |
1288844e | 1139 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
f2917853 | 1140 | error_setg(errp, "iSCSI: failed to send readcapacity10 command."); |
1288844e | 1141 | } |
cb1b83e7 PL |
1142 | if (task) { |
1143 | scsi_free_scsi_task(task); | |
1144 | } | |
cb1b83e7 PL |
1145 | } |
1146 | ||
60beb341 KW |
1147 | /* TODO Convert to fine grained options */ |
1148 | static QemuOptsList runtime_opts = { | |
1149 | .name = "iscsi", | |
1150 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1151 | .desc = { | |
1152 | { | |
1153 | .name = "filename", | |
1154 | .type = QEMU_OPT_STRING, | |
1155 | .help = "URL to the iscsi image", | |
1156 | }, | |
1157 | { /* end of list */ } | |
1158 | }, | |
1159 | }; | |
1160 | ||
35cb1748 | 1161 | static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, |
24d3bd67 | 1162 | int evpd, int pc, void **inq, Error **errp) |
35cb1748 PB |
1163 | { |
1164 | int full_size; | |
1165 | struct scsi_task *task = NULL; | |
1166 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); | |
1167 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1168 | goto fail; | |
1169 | } | |
1170 | full_size = scsi_datain_getfullsize(task); | |
1171 | if (full_size > task->datain.size) { | |
1172 | scsi_free_scsi_task(task); | |
1173 | ||
1174 | /* we need more data for the full list */ | |
1175 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); | |
f18a7cbb PL |
1176 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
1177 | goto fail; | |
1178 | } | |
35cb1748 | 1179 | } |
f18a7cbb | 1180 | |
24d3bd67 PL |
1181 | *inq = scsi_datain_unmarshall(task); |
1182 | if (*inq == NULL) { | |
1183 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); | |
172fc4dd | 1184 | goto fail_with_err; |
24d3bd67 PL |
1185 | } |
1186 | ||
35cb1748 | 1187 | return task; |
f18a7cbb PL |
1188 | |
1189 | fail: | |
172fc4dd MA |
1190 | error_setg(errp, "iSCSI: Inquiry command failed : %s", |
1191 | iscsi_get_error(iscsi)); | |
1192 | fail_with_err: | |
24d3bd67 | 1193 | if (task != NULL) { |
35cb1748 | 1194 | scsi_free_scsi_task(task); |
35cb1748 PB |
1195 | } |
1196 | return NULL; | |
f18a7cbb PL |
1197 | } |
1198 | ||
80cf6257 SH |
1199 | static void iscsi_detach_aio_context(BlockDriverState *bs) |
1200 | { | |
1201 | IscsiLun *iscsilun = bs->opaque; | |
1202 | ||
1203 | aio_set_fd_handler(iscsilun->aio_context, | |
1204 | iscsi_get_fd(iscsilun->iscsi), | |
1205 | NULL, NULL, NULL); | |
1206 | iscsilun->events = 0; | |
1207 | ||
1208 | if (iscsilun->nop_timer) { | |
1209 | timer_del(iscsilun->nop_timer); | |
1210 | timer_free(iscsilun->nop_timer); | |
1211 | iscsilun->nop_timer = NULL; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | static void iscsi_attach_aio_context(BlockDriverState *bs, | |
1216 | AioContext *new_context) | |
1217 | { | |
1218 | IscsiLun *iscsilun = bs->opaque; | |
1219 | ||
1220 | iscsilun->aio_context = new_context; | |
1221 | iscsi_set_events(iscsilun); | |
1222 | ||
80cf6257 SH |
1223 | /* Set up a timer for sending out iSCSI NOPs */ |
1224 | iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, | |
1225 | QEMU_CLOCK_REALTIME, SCALE_MS, | |
1226 | iscsi_nop_timed_event, iscsilun); | |
1227 | timer_mod(iscsilun->nop_timer, | |
1228 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); | |
80cf6257 SH |
1229 | } |
1230 | ||
c589b249 RS |
1231 | /* |
1232 | * We support iscsi url's on the form | |
1233 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> | |
28f106af JC |
1234 | * |
1235 | * Note: flags are currently not used by iscsi_open. If this function | |
1236 | * is changed such that flags are used, please examine iscsi_reopen_prepare() | |
1237 | * to see if needs to be changed as well. | |
c589b249 | 1238 | */ |
015a1036 HR |
1239 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, |
1240 | Error **errp) | |
c589b249 RS |
1241 | { |
1242 | IscsiLun *iscsilun = bs->opaque; | |
1243 | struct iscsi_context *iscsi = NULL; | |
1244 | struct iscsi_url *iscsi_url = NULL; | |
e829b0bb PL |
1245 | struct scsi_task *task = NULL; |
1246 | struct scsi_inquiry_standard *inq = NULL; | |
24d3bd67 | 1247 | struct scsi_inquiry_supported_pages *inq_vpd; |
f9dadc98 | 1248 | char *initiator_name = NULL; |
60beb341 KW |
1249 | QemuOpts *opts; |
1250 | Error *local_err = NULL; | |
1251 | const char *filename; | |
24d3bd67 | 1252 | int i, ret; |
c589b249 RS |
1253 | |
1254 | if ((BDRV_SECTOR_SIZE % 512) != 0) { | |
f2917853 PB |
1255 | error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. " |
1256 | "BDRV_SECTOR_SIZE(%lld) is not a multiple " | |
1257 | "of 512", BDRV_SECTOR_SIZE); | |
c589b249 RS |
1258 | return -EINVAL; |
1259 | } | |
1260 | ||
87ea75d5 | 1261 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
60beb341 | 1262 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 1263 | if (local_err) { |
f2917853 | 1264 | error_propagate(errp, local_err); |
60beb341 KW |
1265 | ret = -EINVAL; |
1266 | goto out; | |
1267 | } | |
1268 | ||
1269 | filename = qemu_opt_get(opts, "filename"); | |
1270 | ||
c589b249 RS |
1271 | iscsi_url = iscsi_parse_full_url(iscsi, filename); |
1272 | if (iscsi_url == NULL) { | |
f2917853 | 1273 | error_setg(errp, "Failed to parse URL : %s", filename); |
c589b249 | 1274 | ret = -EINVAL; |
b93c94f7 | 1275 | goto out; |
c589b249 RS |
1276 | } |
1277 | ||
f9dadc98 RS |
1278 | memset(iscsilun, 0, sizeof(IscsiLun)); |
1279 | ||
1280 | initiator_name = parse_initiator_name(iscsi_url->target); | |
1281 | ||
1282 | iscsi = iscsi_create_context(initiator_name); | |
1283 | if (iscsi == NULL) { | |
f2917853 | 1284 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); |
f9dadc98 | 1285 | ret = -ENOMEM; |
b93c94f7 | 1286 | goto out; |
f9dadc98 RS |
1287 | } |
1288 | ||
c589b249 | 1289 | if (iscsi_set_targetname(iscsi, iscsi_url->target)) { |
f2917853 | 1290 | error_setg(errp, "iSCSI: Failed to set target name."); |
c589b249 | 1291 | ret = -EINVAL; |
b93c94f7 | 1292 | goto out; |
c589b249 RS |
1293 | } |
1294 | ||
1295 | if (iscsi_url->user != NULL) { | |
1296 | ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, | |
1297 | iscsi_url->passwd); | |
1298 | if (ret != 0) { | |
f2917853 | 1299 | error_setg(errp, "Failed to set initiator username and password"); |
c589b249 | 1300 | ret = -EINVAL; |
b93c94f7 | 1301 | goto out; |
c589b249 RS |
1302 | } |
1303 | } | |
f9dadc98 RS |
1304 | |
1305 | /* check if we got CHAP username/password via the options */ | |
f2917853 PB |
1306 | parse_chap(iscsi, iscsi_url->target, &local_err); |
1307 | if (local_err != NULL) { | |
1308 | error_propagate(errp, local_err); | |
f9dadc98 | 1309 | ret = -EINVAL; |
b93c94f7 | 1310 | goto out; |
f9dadc98 RS |
1311 | } |
1312 | ||
c589b249 | 1313 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
f2917853 | 1314 | error_setg(errp, "iSCSI: Failed to set session type to normal."); |
c589b249 | 1315 | ret = -EINVAL; |
b93c94f7 | 1316 | goto out; |
c589b249 RS |
1317 | } |
1318 | ||
1319 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
1320 | ||
f9dadc98 | 1321 | /* check if we got HEADER_DIGEST via the options */ |
f2917853 PB |
1322 | parse_header_digest(iscsi, iscsi_url->target, &local_err); |
1323 | if (local_err != NULL) { | |
1324 | error_propagate(errp, local_err); | |
1325 | ret = -EINVAL; | |
1326 | goto out; | |
1327 | } | |
f9dadc98 | 1328 | |
e829b0bb | 1329 | if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) { |
f2917853 | 1330 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", |
e829b0bb PL |
1331 | iscsi_get_error(iscsi)); |
1332 | ret = -EINVAL; | |
1333 | goto out; | |
1334 | } | |
c589b249 RS |
1335 | |
1336 | iscsilun->iscsi = iscsi; | |
80cf6257 | 1337 | iscsilun->aio_context = bdrv_get_aio_context(bs); |
c589b249 | 1338 | iscsilun->lun = iscsi_url->lun; |
24d3bd67 | 1339 | iscsilun->has_write_same = true; |
c589b249 | 1340 | |
24d3bd67 PL |
1341 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, |
1342 | (void **) &inq, errp); | |
1343 | if (task == NULL) { | |
c589b249 | 1344 | ret = -EINVAL; |
b93c94f7 | 1345 | goto out; |
c589b249 | 1346 | } |
e829b0bb | 1347 | iscsilun->type = inq->periperal_device_type; |
24d3bd67 PL |
1348 | scsi_free_scsi_task(task); |
1349 | task = NULL; | |
e829b0bb | 1350 | |
f2917853 PB |
1351 | iscsi_readcapacity_sync(iscsilun, &local_err); |
1352 | if (local_err != NULL) { | |
1353 | error_propagate(errp, local_err); | |
cd82b6fb | 1354 | ret = -EINVAL; |
cb1b83e7 | 1355 | goto out; |
e829b0bb | 1356 | } |
0777b5dd | 1357 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); |
2c9880c4 | 1358 | bs->request_alignment = iscsilun->block_size; |
e829b0bb | 1359 | |
f47c3f5a KW |
1360 | /* We don't have any emulation for devices other than disks and CD-ROMs, so |
1361 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu | |
1362 | * will try to read from the device to guess the image format. | |
622695a4 | 1363 | */ |
f47c3f5a | 1364 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { |
622695a4 RS |
1365 | bs->sg = 1; |
1366 | } | |
1367 | ||
24d3bd67 PL |
1368 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
1369 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, | |
1370 | (void **) &inq_vpd, errp); | |
1371 | if (task == NULL) { | |
1372 | ret = -EINVAL; | |
1373 | goto out; | |
f18a7cbb | 1374 | } |
24d3bd67 PL |
1375 | for (i = 0; i < inq_vpd->num_pages; i++) { |
1376 | struct scsi_task *inq_task; | |
1377 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; | |
f18a7cbb | 1378 | struct scsi_inquiry_block_limits *inq_bl; |
24d3bd67 PL |
1379 | switch (inq_vpd->pages[i]) { |
1380 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: | |
1381 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1382 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, | |
1383 | (void **) &inq_lbp, errp); | |
1384 | if (inq_task == NULL) { | |
1385 | ret = -EINVAL; | |
1386 | goto out; | |
1387 | } | |
1388 | memcpy(&iscsilun->lbp, inq_lbp, | |
1389 | sizeof(struct scsi_inquiry_logical_block_provisioning)); | |
1390 | scsi_free_scsi_task(inq_task); | |
1391 | break; | |
1392 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: | |
1393 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1394 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, | |
1395 | (void **) &inq_bl, errp); | |
1396 | if (inq_task == NULL) { | |
1397 | ret = -EINVAL; | |
1398 | goto out; | |
1399 | } | |
1400 | memcpy(&iscsilun->bl, inq_bl, | |
1401 | sizeof(struct scsi_inquiry_block_limits)); | |
1402 | scsi_free_scsi_task(inq_task); | |
1403 | break; | |
1404 | default: | |
1405 | break; | |
f18a7cbb | 1406 | } |
f18a7cbb | 1407 | } |
24d3bd67 PL |
1408 | scsi_free_scsi_task(task); |
1409 | task = NULL; | |
f18a7cbb | 1410 | |
80cf6257 | 1411 | iscsi_attach_aio_context(bs, iscsilun->aio_context); |
5b5d34ec | 1412 | |
b03c3805 PL |
1413 | /* Guess the internal cluster (page) size of the iscsi target by the means |
1414 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a | |
1415 | * reasonable size */ | |
3d2acaa3 | 1416 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && |
b03c3805 PL |
1417 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { |
1418 | iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * | |
1419 | iscsilun->block_size) >> BDRV_SECTOR_BITS; | |
b03c3805 PL |
1420 | if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) { |
1421 | iscsilun->allocationmap = | |
1422 | bitmap_new(DIV_ROUND_UP(bs->total_sectors, | |
1423 | iscsilun->cluster_sectors)); | |
1424 | } | |
b03c3805 PL |
1425 | } |
1426 | ||
b93c94f7 | 1427 | out: |
60beb341 | 1428 | qemu_opts_del(opts); |
f7047c2d | 1429 | g_free(initiator_name); |
c589b249 RS |
1430 | if (iscsi_url != NULL) { |
1431 | iscsi_destroy_url(iscsi_url); | |
1432 | } | |
e829b0bb PL |
1433 | if (task != NULL) { |
1434 | scsi_free_scsi_task(task); | |
1435 | } | |
b93c94f7 PB |
1436 | |
1437 | if (ret) { | |
1438 | if (iscsi != NULL) { | |
1439 | iscsi_destroy_context(iscsi); | |
1440 | } | |
1441 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
c589b249 | 1442 | } |
c589b249 RS |
1443 | return ret; |
1444 | } | |
1445 | ||
1446 | static void iscsi_close(BlockDriverState *bs) | |
1447 | { | |
1448 | IscsiLun *iscsilun = bs->opaque; | |
1449 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1450 | ||
80cf6257 | 1451 | iscsi_detach_aio_context(bs); |
c589b249 | 1452 | iscsi_destroy_context(iscsi); |
d4cd9615 | 1453 | g_free(iscsilun->zeroblock); |
b03c3805 | 1454 | g_free(iscsilun->allocationmap); |
c589b249 RS |
1455 | memset(iscsilun, 0, sizeof(IscsiLun)); |
1456 | } | |
1457 | ||
d34682cd KW |
1458 | static int iscsi_refresh_limits(BlockDriverState *bs) |
1459 | { | |
1460 | IscsiLun *iscsilun = bs->opaque; | |
1461 | ||
1462 | /* We don't actually refresh here, but just return data queried in | |
1463 | * iscsi_open(): iscsi targets don't change their limits. */ | |
c97ca29d | 1464 | if (iscsilun->lbp.lbpu) { |
d34682cd KW |
1465 | if (iscsilun->bl.max_unmap < 0xffffffff) { |
1466 | bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap, | |
1467 | iscsilun); | |
1468 | } | |
1469 | bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran, | |
1470 | iscsilun); | |
c97ca29d | 1471 | } |
d34682cd | 1472 | |
c97ca29d PB |
1473 | if (iscsilun->bl.max_ws_len < 0xffffffff) { |
1474 | bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len, | |
1475 | iscsilun); | |
1476 | } | |
1477 | if (iscsilun->lbp.lbpws) { | |
d34682cd KW |
1478 | bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran, |
1479 | iscsilun); | |
d34682cd | 1480 | } |
5d259fc7 PL |
1481 | bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len, |
1482 | iscsilun); | |
e9f526ab AL |
1483 | return 0; |
1484 | } | |
d34682cd | 1485 | |
28f106af JC |
1486 | /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in |
1487 | * prepare. Note that this will not re-establish a connection with an iSCSI | |
1488 | * target - it is effectively a NOP. */ | |
dc6afb99 JC |
1489 | static int iscsi_reopen_prepare(BDRVReopenState *state, |
1490 | BlockReopenQueue *queue, Error **errp) | |
1491 | { | |
28f106af | 1492 | /* NOP */ |
d34682cd KW |
1493 | return 0; |
1494 | } | |
1495 | ||
cb1b83e7 PL |
1496 | static int iscsi_truncate(BlockDriverState *bs, int64_t offset) |
1497 | { | |
1498 | IscsiLun *iscsilun = bs->opaque; | |
f2917853 | 1499 | Error *local_err = NULL; |
cb1b83e7 PL |
1500 | |
1501 | if (iscsilun->type != TYPE_DISK) { | |
1502 | return -ENOTSUP; | |
1503 | } | |
1504 | ||
f2917853 PB |
1505 | iscsi_readcapacity_sync(iscsilun, &local_err); |
1506 | if (local_err != NULL) { | |
1507 | error_free(local_err); | |
1508 | return -EIO; | |
cb1b83e7 PL |
1509 | } |
1510 | ||
1511 | if (offset > iscsi_getlength(bs)) { | |
1512 | return -EINVAL; | |
1513 | } | |
1514 | ||
b03c3805 PL |
1515 | if (iscsilun->allocationmap != NULL) { |
1516 | g_free(iscsilun->allocationmap); | |
1517 | iscsilun->allocationmap = | |
1518 | bitmap_new(DIV_ROUND_UP(bs->total_sectors, | |
1519 | iscsilun->cluster_sectors)); | |
1520 | } | |
1521 | ||
cb1b83e7 PL |
1522 | return 0; |
1523 | } | |
1524 | ||
a59479e3 | 1525 | static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) |
de8864e5 PL |
1526 | { |
1527 | int ret = 0; | |
1528 | int64_t total_size = 0; | |
13c91cb7 | 1529 | BlockDriverState *bs; |
de8864e5 | 1530 | IscsiLun *iscsilun = NULL; |
60beb341 | 1531 | QDict *bs_options; |
de8864e5 | 1532 | |
98522f63 | 1533 | bs = bdrv_new("", &error_abort); |
de8864e5 PL |
1534 | |
1535 | /* Read out options */ | |
a59479e3 CL |
1536 | total_size = |
1537 | qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE; | |
13c91cb7 FZ |
1538 | bs->opaque = g_malloc0(sizeof(struct IscsiLun)); |
1539 | iscsilun = bs->opaque; | |
de8864e5 | 1540 | |
60beb341 KW |
1541 | bs_options = qdict_new(); |
1542 | qdict_put(bs_options, "filename", qstring_from_str(filename)); | |
015a1036 | 1543 | ret = iscsi_open(bs, bs_options, 0, NULL); |
60beb341 KW |
1544 | QDECREF(bs_options); |
1545 | ||
de8864e5 PL |
1546 | if (ret != 0) { |
1547 | goto out; | |
1548 | } | |
80cf6257 | 1549 | iscsi_detach_aio_context(bs); |
de8864e5 PL |
1550 | if (iscsilun->type != TYPE_DISK) { |
1551 | ret = -ENODEV; | |
1552 | goto out; | |
1553 | } | |
13c91cb7 | 1554 | if (bs->total_sectors < total_size) { |
de8864e5 | 1555 | ret = -ENOSPC; |
d3bda7bc | 1556 | goto out; |
de8864e5 PL |
1557 | } |
1558 | ||
1559 | ret = 0; | |
1560 | out: | |
1561 | if (iscsilun->iscsi != NULL) { | |
1562 | iscsi_destroy_context(iscsilun->iscsi); | |
1563 | } | |
13c91cb7 FZ |
1564 | g_free(bs->opaque); |
1565 | bs->opaque = NULL; | |
4f6fd349 | 1566 | bdrv_unref(bs); |
de8864e5 PL |
1567 | return ret; |
1568 | } | |
1569 | ||
186d4f2b PL |
1570 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1571 | { | |
1572 | IscsiLun *iscsilun = bs->opaque; | |
1573 | bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz; | |
1574 | bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; | |
b03c3805 | 1575 | bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE; |
186d4f2b PL |
1576 | return 0; |
1577 | } | |
1578 | ||
a59479e3 CL |
1579 | static QemuOptsList iscsi_create_opts = { |
1580 | .name = "iscsi-create-opts", | |
1581 | .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), | |
1582 | .desc = { | |
1583 | { | |
1584 | .name = BLOCK_OPT_SIZE, | |
1585 | .type = QEMU_OPT_SIZE, | |
1586 | .help = "Virtual disk size" | |
1587 | }, | |
1588 | { /* end of list */ } | |
1589 | } | |
de8864e5 PL |
1590 | }; |
1591 | ||
c589b249 RS |
1592 | static BlockDriver bdrv_iscsi = { |
1593 | .format_name = "iscsi", | |
1594 | .protocol_name = "iscsi", | |
1595 | ||
1596 | .instance_size = sizeof(IscsiLun), | |
030be321 | 1597 | .bdrv_needs_filename = true, |
c589b249 RS |
1598 | .bdrv_file_open = iscsi_open, |
1599 | .bdrv_close = iscsi_close, | |
c282e1fd | 1600 | .bdrv_create = iscsi_create, |
a59479e3 | 1601 | .create_opts = &iscsi_create_opts, |
dc6afb99 | 1602 | .bdrv_reopen_prepare = iscsi_reopen_prepare, |
c589b249 RS |
1603 | |
1604 | .bdrv_getlength = iscsi_getlength, | |
186d4f2b | 1605 | .bdrv_get_info = iscsi_get_info, |
cb1b83e7 | 1606 | .bdrv_truncate = iscsi_truncate, |
d34682cd | 1607 | .bdrv_refresh_limits = iscsi_refresh_limits, |
c589b249 | 1608 | |
54a5c1d5 | 1609 | .bdrv_co_get_block_status = iscsi_co_get_block_status, |
65f3e339 | 1610 | .bdrv_co_discard = iscsi_co_discard, |
d4cd9615 | 1611 | .bdrv_co_write_zeroes = iscsi_co_write_zeroes, |
063c3378 PL |
1612 | .bdrv_co_readv = iscsi_co_readv, |
1613 | .bdrv_co_writev = iscsi_co_writev, | |
1614 | .bdrv_co_flush_to_disk = iscsi_co_flush, | |
fa6acb0c | 1615 | |
98392453 RS |
1616 | #ifdef __linux__ |
1617 | .bdrv_ioctl = iscsi_ioctl, | |
1618 | .bdrv_aio_ioctl = iscsi_aio_ioctl, | |
1619 | #endif | |
80cf6257 SH |
1620 | |
1621 | .bdrv_detach_aio_context = iscsi_detach_aio_context, | |
1622 | .bdrv_attach_aio_context = iscsi_attach_aio_context, | |
c589b249 RS |
1623 | }; |
1624 | ||
4d454574 PB |
1625 | static QemuOptsList qemu_iscsi_opts = { |
1626 | .name = "iscsi", | |
1627 | .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head), | |
1628 | .desc = { | |
1629 | { | |
1630 | .name = "user", | |
1631 | .type = QEMU_OPT_STRING, | |
1632 | .help = "username for CHAP authentication to target", | |
1633 | },{ | |
1634 | .name = "password", | |
1635 | .type = QEMU_OPT_STRING, | |
1636 | .help = "password for CHAP authentication to target", | |
1637 | },{ | |
1638 | .name = "header-digest", | |
1639 | .type = QEMU_OPT_STRING, | |
1640 | .help = "HeaderDigest setting. " | |
1641 | "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}", | |
1642 | },{ | |
1643 | .name = "initiator-name", | |
1644 | .type = QEMU_OPT_STRING, | |
1645 | .help = "Initiator iqn name to use when connecting", | |
1646 | }, | |
1647 | { /* end of list */ } | |
1648 | }, | |
1649 | }; | |
1650 | ||
c589b249 RS |
1651 | static void iscsi_block_init(void) |
1652 | { | |
1653 | bdrv_register(&bdrv_iscsi); | |
4d454574 | 1654 | qemu_add_opts(&qemu_iscsi_opts); |
c589b249 RS |
1655 | } |
1656 | ||
1657 | block_init(iscsi_block_init); |