]>
Commit | Line | Data |
---|---|---|
c589b249 RS |
1 | /* |
2 | * QEMU Block driver for iSCSI images | |
3 | * | |
4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]> | |
03e40fef | 5 | * Copyright (c) 2012-2015 Peter Lieven <[email protected]> |
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 | ||
80c71a24 | 26 | #include "qemu/osdep.h" |
c589b249 RS |
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" |
0d09e41a | 37 | #include "block/scsi.h" |
0a53f010 | 38 | #include "qemu/iov.h" |
5accc840 PB |
39 | #include "sysemu/sysemu.h" |
40 | #include "qmp-commands.h" | |
d49b6836 | 41 | #include "qapi/qmp/qstring.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; |
05b685fb | 60 | QEMUTimer *event_timer; |
f18a7cbb PL |
61 | struct scsi_inquiry_logical_block_provisioning lbp; |
62 | struct scsi_inquiry_block_limits bl; | |
d4cd9615 | 63 | unsigned char *zeroblock; |
b03c3805 PL |
64 | unsigned long *allocationmap; |
65 | int cluster_sectors; | |
9281fe9e | 66 | bool use_16_for_rw; |
43ae8fb1 | 67 | bool write_protected; |
0a386e48 PL |
68 | bool lbpme; |
69 | bool lbprz; | |
752ce451 | 70 | bool dpofua; |
0a386e48 | 71 | bool has_write_same; |
73b5394e | 72 | bool force_next_flush; |
5dd7a535 | 73 | bool request_timed_out; |
c589b249 RS |
74 | } IscsiLun; |
75 | ||
54a5c1d5 PL |
76 | typedef struct IscsiTask { |
77 | int status; | |
78 | int complete; | |
79 | int retries; | |
80 | int do_retry; | |
81 | struct scsi_task *task; | |
82 | Coroutine *co; | |
8b9dfe90 | 83 | QEMUBH *bh; |
80cf6257 | 84 | IscsiLun *iscsilun; |
efc6de0d | 85 | QEMUTimer retry_timer; |
73b5394e | 86 | bool force_next_flush; |
e01dd3da | 87 | int err_code; |
54a5c1d5 PL |
88 | } IscsiTask; |
89 | ||
c589b249 | 90 | typedef struct IscsiAIOCB { |
7c84b1b8 | 91 | BlockAIOCB common; |
c589b249 RS |
92 | QEMUIOVector *qiov; |
93 | QEMUBH *bh; | |
94 | IscsiLun *iscsilun; | |
95 | struct scsi_task *task; | |
96 | uint8_t *buf; | |
97 | int status; | |
1dde716e PL |
98 | int64_t sector_num; |
99 | int nb_sectors; | |
4bb17ab5 | 100 | int ret; |
98392453 RS |
101 | #ifdef __linux__ |
102 | sg_io_hdr_t *ioh; | |
103 | #endif | |
c589b249 RS |
104 | } IscsiAIOCB; |
105 | ||
5dd7a535 PL |
106 | /* libiscsi uses time_t so its enough to process events every second */ |
107 | #define EVENT_INTERVAL 1000 | |
5b5d34ec PL |
108 | #define NOP_INTERVAL 5000 |
109 | #define MAX_NOP_FAILURES 3 | |
efc6de0d | 110 | #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) |
59dd0a22 | 111 | static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768}; |
5b5d34ec | 112 | |
5d831be2 | 113 | /* this threshold is a trade-off knob to choose between |
5917af81 PL |
114 | * the potential additional overhead of an extra GET_LBA_STATUS request |
115 | * vs. unnecessarily reading a lot of zero sectors over the wire. | |
116 | * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES | |
117 | * sectors we check the allocation status of the area covered by the | |
118 | * request first if the allocationmap indicates that the area might be | |
119 | * unallocated. */ | |
120 | #define ISCSI_CHECKALLOC_THRES 64 | |
5b5d34ec | 121 | |
27cbd828 | 122 | static void |
cfb3f506 | 123 | iscsi_bh_cb(void *p) |
27cbd828 PB |
124 | { |
125 | IscsiAIOCB *acb = p; | |
126 | ||
127 | qemu_bh_delete(acb->bh); | |
128 | ||
4790b03d PB |
129 | g_free(acb->buf); |
130 | acb->buf = NULL; | |
131 | ||
722d9333 | 132 | acb->common.cb(acb->common.opaque, acb->status); |
27cbd828 | 133 | |
1bd075f2 PB |
134 | if (acb->task != NULL) { |
135 | scsi_free_scsi_task(acb->task); | |
136 | acb->task = NULL; | |
137 | } | |
138 | ||
8007429a | 139 | qemu_aio_unref(acb); |
27cbd828 PB |
140 | } |
141 | ||
cfb3f506 PB |
142 | static void |
143 | iscsi_schedule_bh(IscsiAIOCB *acb) | |
27cbd828 | 144 | { |
1bd075f2 PB |
145 | if (acb->bh) { |
146 | return; | |
147 | } | |
80cf6257 | 148 | acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); |
27cbd828 | 149 | qemu_bh_schedule(acb->bh); |
27cbd828 PB |
150 | } |
151 | ||
8b9dfe90 PL |
152 | static void iscsi_co_generic_bh_cb(void *opaque) |
153 | { | |
154 | struct IscsiTask *iTask = opaque; | |
fcd470d8 | 155 | iTask->complete = 1; |
8b9dfe90 PL |
156 | qemu_bh_delete(iTask->bh); |
157 | qemu_coroutine_enter(iTask->co, NULL); | |
158 | } | |
159 | ||
efc6de0d PL |
160 | static void iscsi_retry_timer_expired(void *opaque) |
161 | { | |
162 | struct IscsiTask *iTask = opaque; | |
fcd470d8 | 163 | iTask->complete = 1; |
efc6de0d PL |
164 | if (iTask->co) { |
165 | qemu_coroutine_enter(iTask->co, NULL); | |
166 | } | |
167 | } | |
168 | ||
169 | static inline unsigned exp_random(double mean) | |
170 | { | |
171 | return -mean * log((double)rand() / RAND_MAX); | |
172 | } | |
173 | ||
e01dd3da FZ |
174 | /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in |
175 | * libiscsi 1.10.0, together with other constants we need. Use it as | |
176 | * a hint that we have to define them ourselves if needed, to keep the | |
177 | * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for | |
178 | * the test because SCSI_STATUS_* is an enum. | |
179 | * | |
180 | * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes | |
181 | * an enum, check against the LIBISCSI_API_VERSION macro, which was | |
182 | * introduced in 1.11.0. If it is present, there is no need to define | |
183 | * anything. | |
184 | */ | |
185 | #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ | |
186 | !defined(LIBISCSI_API_VERSION) | |
187 | #define SCSI_STATUS_TASK_SET_FULL 0x28 | |
188 | #define SCSI_STATUS_TIMEOUT 0x0f000002 | |
189 | #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 | |
190 | #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 | |
9049736e PL |
191 | #endif |
192 | ||
e01dd3da FZ |
193 | static int iscsi_translate_sense(struct scsi_sense *sense) |
194 | { | |
195 | int ret; | |
196 | ||
197 | switch (sense->key) { | |
198 | case SCSI_SENSE_NOT_READY: | |
199 | return -EBUSY; | |
200 | case SCSI_SENSE_DATA_PROTECTION: | |
201 | return -EACCES; | |
202 | case SCSI_SENSE_COMMAND_ABORTED: | |
203 | return -ECANCELED; | |
204 | case SCSI_SENSE_ILLEGAL_REQUEST: | |
205 | /* Parse ASCQ */ | |
206 | break; | |
207 | default: | |
208 | return -EIO; | |
209 | } | |
210 | switch (sense->ascq) { | |
211 | case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR: | |
212 | case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE: | |
213 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB: | |
214 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST: | |
215 | ret = -EINVAL; | |
216 | break; | |
217 | case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE: | |
218 | ret = -ENOSPC; | |
219 | break; | |
220 | case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED: | |
221 | ret = -ENOTSUP; | |
222 | break; | |
223 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT: | |
224 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED: | |
225 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN: | |
226 | ret = -ENOMEDIUM; | |
227 | break; | |
228 | case SCSI_SENSE_ASCQ_WRITE_PROTECTED: | |
229 | ret = -EACCES; | |
230 | break; | |
231 | default: | |
232 | ret = -EIO; | |
233 | break; | |
234 | } | |
235 | return ret; | |
236 | } | |
237 | ||
54a5c1d5 PL |
238 | static void |
239 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, | |
240 | void *command_data, void *opaque) | |
241 | { | |
242 | struct IscsiTask *iTask = opaque; | |
243 | struct scsi_task *task = command_data; | |
244 | ||
54a5c1d5 PL |
245 | iTask->status = status; |
246 | iTask->do_retry = 0; | |
247 | iTask->task = task; | |
248 | ||
54a5c1d5 | 249 | if (status != SCSI_STATUS_GOOD) { |
efc6de0d PL |
250 | if (iTask->retries++ < ISCSI_CMD_RETRIES) { |
251 | if (status == SCSI_STATUS_CHECK_CONDITION | |
252 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
253 | error_report("iSCSI CheckCondition: %s", | |
254 | iscsi_get_error(iscsi)); | |
255 | iTask->do_retry = 1; | |
256 | goto out; | |
257 | } | |
9049736e | 258 | if (status == SCSI_STATUS_BUSY || |
e01dd3da FZ |
259 | status == SCSI_STATUS_TIMEOUT || |
260 | status == SCSI_STATUS_TASK_SET_FULL) { | |
efc6de0d PL |
261 | unsigned retry_time = |
262 | exp_random(iscsi_retry_times[iTask->retries - 1]); | |
e01dd3da | 263 | if (status == SCSI_STATUS_TIMEOUT) { |
5dd7a535 PL |
264 | /* make sure the request is rescheduled AFTER the |
265 | * reconnect is initiated */ | |
266 | retry_time = EVENT_INTERVAL * 2; | |
267 | iTask->iscsilun->request_timed_out = true; | |
268 | } | |
269 | error_report("iSCSI Busy/TaskSetFull/TimeOut" | |
270 | " (retry #%u in %u ms): %s", | |
efc6de0d PL |
271 | iTask->retries, retry_time, |
272 | iscsi_get_error(iscsi)); | |
273 | aio_timer_init(iTask->iscsilun->aio_context, | |
274 | &iTask->retry_timer, QEMU_CLOCK_REALTIME, | |
275 | SCALE_MS, iscsi_retry_timer_expired, iTask); | |
276 | timer_mod(&iTask->retry_timer, | |
277 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); | |
278 | iTask->do_retry = 1; | |
279 | return; | |
280 | } | |
281 | } | |
e01dd3da | 282 | iTask->err_code = iscsi_translate_sense(&task->sense); |
837c3901 | 283 | error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); |
73b5394e PL |
284 | } else { |
285 | iTask->iscsilun->force_next_flush |= iTask->force_next_flush; | |
54a5c1d5 PL |
286 | } |
287 | ||
288 | out: | |
289 | if (iTask->co) { | |
80cf6257 SH |
290 | iTask->bh = aio_bh_new(iTask->iscsilun->aio_context, |
291 | iscsi_co_generic_bh_cb, iTask); | |
8b9dfe90 | 292 | qemu_bh_schedule(iTask->bh); |
fcd470d8 PL |
293 | } else { |
294 | iTask->complete = 1; | |
54a5c1d5 PL |
295 | } |
296 | } | |
297 | ||
298 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) | |
299 | { | |
300 | *iTask = (struct IscsiTask) { | |
efc6de0d | 301 | .co = qemu_coroutine_self(), |
efc6de0d | 302 | .iscsilun = iscsilun, |
54a5c1d5 PL |
303 | }; |
304 | } | |
27cbd828 | 305 | |
c589b249 RS |
306 | static void |
307 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
308 | void *private_data) | |
309 | { | |
1bd075f2 PB |
310 | IscsiAIOCB *acb = private_data; |
311 | ||
312 | acb->status = -ECANCELED; | |
313 | iscsi_schedule_bh(acb); | |
c589b249 RS |
314 | } |
315 | ||
316 | static void | |
7c84b1b8 | 317 | iscsi_aio_cancel(BlockAIOCB *blockacb) |
c589b249 RS |
318 | { |
319 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; | |
320 | IscsiLun *iscsilun = acb->iscsilun; | |
321 | ||
1bd075f2 PB |
322 | if (acb->status != -EINPROGRESS) { |
323 | return; | |
324 | } | |
325 | ||
b2090919 | 326 | /* send a task mgmt call to the target to cancel the task on the target */ |
64e69e80 | 327 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
1bd075f2 | 328 | iscsi_abort_task_cb, acb); |
b2090919 | 329 | |
c589b249 RS |
330 | } |
331 | ||
d7331bed | 332 | static const AIOCBInfo iscsi_aiocb_info = { |
c589b249 | 333 | .aiocb_size = sizeof(IscsiAIOCB), |
722d9333 | 334 | .cancel_async = iscsi_aio_cancel, |
c589b249 RS |
335 | }; |
336 | ||
337 | ||
338 | static void iscsi_process_read(void *arg); | |
339 | static void iscsi_process_write(void *arg); | |
340 | ||
c589b249 RS |
341 | static void |
342 | iscsi_set_events(IscsiLun *iscsilun) | |
343 | { | |
344 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
05b685fb | 345 | int ev = iscsi_which_events(iscsi); |
c9b9f682 | 346 | |
c9b9f682 | 347 | if (ev != iscsilun->events) { |
dca21ef2 FZ |
348 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), |
349 | false, | |
05b685fb | 350 | (ev & POLLIN) ? iscsi_process_read : NULL, |
80cf6257 SH |
351 | (ev & POLLOUT) ? iscsi_process_write : NULL, |
352 | iscsilun); | |
05b685fb PL |
353 | iscsilun->events = ev; |
354 | } | |
05b685fb | 355 | } |
c9b9f682 | 356 | |
5dd7a535 | 357 | static void iscsi_timed_check_events(void *opaque) |
05b685fb PL |
358 | { |
359 | IscsiLun *iscsilun = opaque; | |
5dd7a535 PL |
360 | |
361 | /* check for timed out requests */ | |
362 | iscsi_service(iscsilun->iscsi, 0); | |
363 | ||
364 | if (iscsilun->request_timed_out) { | |
365 | iscsilun->request_timed_out = false; | |
366 | iscsi_reconnect(iscsilun->iscsi); | |
367 | } | |
368 | ||
369 | /* newer versions of libiscsi may return zero events. Ensure we are able | |
370 | * to return to service once this situation changes. */ | |
05b685fb | 371 | iscsi_set_events(iscsilun); |
5dd7a535 PL |
372 | |
373 | timer_mod(iscsilun->event_timer, | |
374 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); | |
c589b249 RS |
375 | } |
376 | ||
377 | static void | |
378 | iscsi_process_read(void *arg) | |
379 | { | |
380 | IscsiLun *iscsilun = arg; | |
381 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
382 | ||
383 | iscsi_service(iscsi, POLLIN); | |
384 | iscsi_set_events(iscsilun); | |
385 | } | |
386 | ||
387 | static void | |
388 | iscsi_process_write(void *arg) | |
389 | { | |
390 | IscsiLun *iscsilun = arg; | |
391 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
392 | ||
393 | iscsi_service(iscsi, POLLOUT); | |
394 | iscsi_set_events(iscsilun); | |
395 | } | |
396 | ||
0777b5dd PL |
397 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
398 | { | |
399 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; | |
400 | } | |
401 | ||
c589b249 RS |
402 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) |
403 | { | |
404 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
405 | } | |
406 | ||
91bea4e2 PL |
407 | static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, |
408 | IscsiLun *iscsilun) | |
409 | { | |
410 | if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size || | |
411 | (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) { | |
f5075224 RJ |
412 | error_report("iSCSI misaligned request: " |
413 | "iscsilun->block_size %u, sector_num %" PRIi64 | |
414 | ", nb_sectors %d", | |
91bea4e2 PL |
415 | iscsilun->block_size, sector_num, nb_sectors); |
416 | return 0; | |
417 | } | |
418 | return 1; | |
419 | } | |
420 | ||
a9fe4c95 PL |
421 | static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun) |
422 | { | |
423 | return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, | |
424 | iscsilun), | |
425 | iscsilun->cluster_sectors)); | |
426 | } | |
427 | ||
b03c3805 PL |
428 | static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num, |
429 | int nb_sectors) | |
430 | { | |
431 | if (iscsilun->allocationmap == NULL) { | |
432 | return; | |
433 | } | |
434 | bitmap_set(iscsilun->allocationmap, | |
435 | sector_num / iscsilun->cluster_sectors, | |
436 | DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors)); | |
437 | } | |
438 | ||
439 | static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num, | |
440 | int nb_sectors) | |
441 | { | |
442 | int64_t cluster_num, nb_clusters; | |
443 | if (iscsilun->allocationmap == NULL) { | |
444 | return; | |
445 | } | |
446 | cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors); | |
447 | nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors | |
448 | - cluster_num; | |
449 | if (nb_clusters > 0) { | |
450 | bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters); | |
451 | } | |
452 | } | |
453 | ||
063c3378 PL |
454 | static int coroutine_fn iscsi_co_writev(BlockDriverState *bs, |
455 | int64_t sector_num, int nb_sectors, | |
456 | QEMUIOVector *iov) | |
c589b249 | 457 | { |
063c3378 PL |
458 | IscsiLun *iscsilun = bs->opaque; |
459 | struct IscsiTask iTask; | |
f4dfa67f | 460 | uint64_t lba; |
063c3378 | 461 | uint32_t num_sectors; |
73b5394e | 462 | int fua; |
c589b249 | 463 | |
063c3378 PL |
464 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
465 | return -EINVAL; | |
466 | } | |
7371d56f | 467 | |
dc9e7163 PL |
468 | if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { |
469 | error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len " | |
470 | "of %d sectors", nb_sectors, bs->bl.max_transfer_length); | |
471 | return -EINVAL; | |
472 | } | |
473 | ||
063c3378 PL |
474 | lba = sector_qemu2lun(sector_num, iscsilun); |
475 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
063c3378 PL |
476 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
477 | retry: | |
73b5394e PL |
478 | fua = iscsilun->dpofua && !bs->enable_write_cache; |
479 | iTask.force_next_flush = !fua; | |
9281fe9e PL |
480 | if (iscsilun->use_16_for_rw) { |
481 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
8c215a9f | 482 | NULL, num_sectors * iscsilun->block_size, |
73b5394e | 483 | iscsilun->block_size, 0, 0, fua, 0, 0, |
9281fe9e PL |
484 | iscsi_co_generic_cb, &iTask); |
485 | } else { | |
486 | iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
8c215a9f | 487 | NULL, num_sectors * iscsilun->block_size, |
73b5394e | 488 | iscsilun->block_size, 0, 0, fua, 0, 0, |
9281fe9e PL |
489 | iscsi_co_generic_cb, &iTask); |
490 | } | |
063c3378 | 491 | if (iTask.task == NULL) { |
92397116 | 492 | return -ENOMEM; |
f4dfa67f | 493 | } |
063c3378 PL |
494 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, |
495 | iov->niov); | |
063c3378 PL |
496 | while (!iTask.complete) { |
497 | iscsi_set_events(iscsilun); | |
498 | qemu_coroutine_yield(); | |
c589b249 RS |
499 | } |
500 | ||
063c3378 PL |
501 | if (iTask.task != NULL) { |
502 | scsi_free_scsi_task(iTask.task); | |
503 | iTask.task = NULL; | |
91bea4e2 PL |
504 | } |
505 | ||
063c3378 | 506 | if (iTask.do_retry) { |
837c3901 | 507 | iTask.complete = 0; |
063c3378 | 508 | goto retry; |
1dde716e PL |
509 | } |
510 | ||
063c3378 | 511 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 512 | return iTask.err_code; |
c589b249 RS |
513 | } |
514 | ||
b03c3805 PL |
515 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); |
516 | ||
063c3378 | 517 | return 0; |
c589b249 RS |
518 | } |
519 | ||
b03c3805 PL |
520 | |
521 | static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun, | |
522 | int64_t sector_num, int nb_sectors) | |
523 | { | |
524 | unsigned long size; | |
525 | if (iscsilun->allocationmap == NULL) { | |
526 | return true; | |
527 | } | |
528 | size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); | |
529 | return !(find_next_bit(iscsilun->allocationmap, size, | |
530 | sector_num / iscsilun->cluster_sectors) == size); | |
531 | } | |
532 | ||
b03c3805 PL |
533 | static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, |
534 | int64_t sector_num, | |
535 | int nb_sectors, int *pnum) | |
536 | { | |
537 | IscsiLun *iscsilun = bs->opaque; | |
538 | struct scsi_get_lba_status *lbas = NULL; | |
539 | struct scsi_lba_status_descriptor *lbasd = NULL; | |
540 | struct IscsiTask iTask; | |
541 | int64_t ret; | |
542 | ||
543 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
544 | ||
545 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
546 | ret = -EINVAL; | |
547 | goto out; | |
548 | } | |
549 | ||
550 | /* default to all sectors allocated */ | |
551 | ret = BDRV_BLOCK_DATA; | |
552 | ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; | |
553 | *pnum = nb_sectors; | |
554 | ||
555 | /* LUN does not support logical block provisioning */ | |
0a386e48 | 556 | if (!iscsilun->lbpme) { |
b03c3805 PL |
557 | goto out; |
558 | } | |
559 | ||
560 | retry: | |
561 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, | |
562 | sector_qemu2lun(sector_num, iscsilun), | |
563 | 8 + 16, iscsi_co_generic_cb, | |
564 | &iTask) == NULL) { | |
565 | ret = -ENOMEM; | |
566 | goto out; | |
567 | } | |
568 | ||
569 | while (!iTask.complete) { | |
570 | iscsi_set_events(iscsilun); | |
571 | qemu_coroutine_yield(); | |
572 | } | |
573 | ||
574 | if (iTask.do_retry) { | |
575 | if (iTask.task != NULL) { | |
576 | scsi_free_scsi_task(iTask.task); | |
577 | iTask.task = NULL; | |
578 | } | |
579 | iTask.complete = 0; | |
580 | goto retry; | |
581 | } | |
582 | ||
583 | if (iTask.status != SCSI_STATUS_GOOD) { | |
584 | /* in case the get_lba_status_callout fails (i.e. | |
585 | * because the device is busy or the cmd is not | |
586 | * supported) we pretend all blocks are allocated | |
587 | * for backwards compatibility */ | |
588 | goto out; | |
589 | } | |
590 | ||
591 | lbas = scsi_datain_unmarshall(iTask.task); | |
592 | if (lbas == NULL) { | |
593 | ret = -EIO; | |
594 | goto out; | |
595 | } | |
596 | ||
597 | lbasd = &lbas->descriptors[0]; | |
598 | ||
599 | if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { | |
600 | ret = -EIO; | |
601 | goto out; | |
602 | } | |
603 | ||
604 | *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); | |
605 | ||
606 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || | |
607 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { | |
608 | ret &= ~BDRV_BLOCK_DATA; | |
609 | if (iscsilun->lbprz) { | |
610 | ret |= BDRV_BLOCK_ZERO; | |
611 | } | |
612 | } | |
613 | ||
614 | if (ret & BDRV_BLOCK_ZERO) { | |
615 | iscsi_allocationmap_clear(iscsilun, sector_num, *pnum); | |
616 | } else { | |
617 | iscsi_allocationmap_set(iscsilun, sector_num, *pnum); | |
618 | } | |
619 | ||
620 | if (*pnum > nb_sectors) { | |
621 | *pnum = nb_sectors; | |
622 | } | |
623 | out: | |
624 | if (iTask.task != NULL) { | |
625 | scsi_free_scsi_task(iTask.task); | |
626 | } | |
627 | return ret; | |
628 | } | |
629 | ||
063c3378 PL |
630 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, |
631 | int64_t sector_num, int nb_sectors, | |
632 | QEMUIOVector *iov) | |
c589b249 | 633 | { |
063c3378 PL |
634 | IscsiLun *iscsilun = bs->opaque; |
635 | struct IscsiTask iTask; | |
1dde716e PL |
636 | uint64_t lba; |
637 | uint32_t num_sectors; | |
c589b249 | 638 | |
063c3378 PL |
639 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
640 | return -EINVAL; | |
f4dfa67f | 641 | } |
f4dfa67f | 642 | |
dc9e7163 PL |
643 | if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) { |
644 | error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len " | |
645 | "of %d sectors", nb_sectors, bs->bl.max_transfer_length); | |
646 | return -EINVAL; | |
647 | } | |
648 | ||
5917af81 | 649 | if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES && |
b03c3805 PL |
650 | !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) { |
651 | int64_t ret; | |
652 | int pnum; | |
653 | ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum); | |
654 | if (ret < 0) { | |
655 | return ret; | |
656 | } | |
657 | if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) { | |
658 | qemu_iovec_memset(iov, 0, 0x00, iov->size); | |
659 | return 0; | |
660 | } | |
661 | } | |
b03c3805 | 662 | |
063c3378 PL |
663 | lba = sector_qemu2lun(sector_num, iscsilun); |
664 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
f4dfa67f | 665 | |
063c3378 PL |
666 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
667 | retry: | |
9281fe9e | 668 | if (iscsilun->use_16_for_rw) { |
063c3378 PL |
669 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, |
670 | num_sectors * iscsilun->block_size, | |
671 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
672 | iscsi_co_generic_cb, &iTask); | |
9281fe9e | 673 | } else { |
063c3378 PL |
674 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, |
675 | num_sectors * iscsilun->block_size, | |
219c2521 | 676 | iscsilun->block_size, |
219c2521 | 677 | 0, 0, 0, 0, 0, |
063c3378 | 678 | iscsi_co_generic_cb, &iTask); |
f4dfa67f | 679 | } |
063c3378 | 680 | if (iTask.task == NULL) { |
92397116 | 681 | return -ENOMEM; |
c589b249 | 682 | } |
063c3378 | 683 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); |
91bea4e2 | 684 | |
063c3378 PL |
685 | while (!iTask.complete) { |
686 | iscsi_set_events(iscsilun); | |
687 | qemu_coroutine_yield(); | |
1dde716e | 688 | } |
c589b249 | 689 | |
063c3378 PL |
690 | if (iTask.task != NULL) { |
691 | scsi_free_scsi_task(iTask.task); | |
692 | iTask.task = NULL; | |
c589b249 RS |
693 | } |
694 | ||
063c3378 | 695 | if (iTask.do_retry) { |
837c3901 | 696 | iTask.complete = 0; |
063c3378 | 697 | goto retry; |
c589b249 RS |
698 | } |
699 | ||
063c3378 | 700 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 701 | return iTask.err_code; |
1dde716e PL |
702 | } |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
063c3378 | 707 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) |
1dde716e PL |
708 | { |
709 | IscsiLun *iscsilun = bs->opaque; | |
063c3378 | 710 | struct IscsiTask iTask; |
1dde716e | 711 | |
73b5394e PL |
712 | if (!iscsilun->force_next_flush) { |
713 | return 0; | |
714 | } | |
715 | iscsilun->force_next_flush = false; | |
1dde716e | 716 | |
73b5394e | 717 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
063c3378 PL |
718 | retry: |
719 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, | |
720 | 0, iscsi_co_generic_cb, &iTask) == NULL) { | |
92397116 | 721 | return -ENOMEM; |
063c3378 | 722 | } |
1dde716e | 723 | |
063c3378 PL |
724 | while (!iTask.complete) { |
725 | iscsi_set_events(iscsilun); | |
726 | qemu_coroutine_yield(); | |
727 | } | |
1dde716e | 728 | |
063c3378 PL |
729 | if (iTask.task != NULL) { |
730 | scsi_free_scsi_task(iTask.task); | |
731 | iTask.task = NULL; | |
c589b249 RS |
732 | } |
733 | ||
063c3378 | 734 | if (iTask.do_retry) { |
837c3901 | 735 | iTask.complete = 0; |
063c3378 PL |
736 | goto retry; |
737 | } | |
c589b249 | 738 | |
063c3378 | 739 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 740 | return iTask.err_code; |
063c3378 PL |
741 | } |
742 | ||
743 | return 0; | |
c589b249 RS |
744 | } |
745 | ||
98392453 RS |
746 | #ifdef __linux__ |
747 | static void | |
748 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, | |
749 | void *command_data, void *opaque) | |
750 | { | |
751 | IscsiAIOCB *acb = opaque; | |
752 | ||
0a53f010 RS |
753 | g_free(acb->buf); |
754 | acb->buf = NULL; | |
755 | ||
98392453 RS |
756 | acb->status = 0; |
757 | if (status < 0) { | |
758 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", | |
759 | iscsi_get_error(iscsi)); | |
e01dd3da | 760 | acb->status = iscsi_translate_sense(&acb->task->sense); |
98392453 RS |
761 | } |
762 | ||
763 | acb->ioh->driver_status = 0; | |
764 | acb->ioh->host_status = 0; | |
765 | acb->ioh->resid = 0; | |
766 | ||
767 | #define SG_ERR_DRIVER_SENSE 0x08 | |
768 | ||
769 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { | |
770 | int ss; | |
771 | ||
772 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; | |
773 | ||
774 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; | |
775 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? | |
776 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; | |
777 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); | |
778 | } | |
779 | ||
cfb3f506 | 780 | iscsi_schedule_bh(acb); |
98392453 RS |
781 | } |
782 | ||
4bb17ab5 FZ |
783 | static void iscsi_ioctl_bh_completion(void *opaque) |
784 | { | |
785 | IscsiAIOCB *acb = opaque; | |
786 | ||
787 | qemu_bh_delete(acb->bh); | |
788 | acb->common.cb(acb->common.opaque, acb->ret); | |
789 | qemu_aio_unref(acb); | |
790 | } | |
791 | ||
792 | static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) | |
793 | { | |
794 | BlockDriverState *bs = acb->common.bs; | |
795 | IscsiLun *iscsilun = bs->opaque; | |
796 | int ret = 0; | |
797 | ||
798 | switch (req) { | |
799 | case SG_GET_VERSION_NUM: | |
800 | *(int *)buf = 30000; | |
801 | break; | |
802 | case SG_GET_SCSI_ID: | |
803 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; | |
804 | break; | |
805 | default: | |
806 | ret = -EINVAL; | |
807 | } | |
808 | assert(!acb->bh); | |
809 | acb->bh = aio_bh_new(bdrv_get_aio_context(bs), | |
810 | iscsi_ioctl_bh_completion, acb); | |
811 | acb->ret = ret; | |
812 | qemu_bh_schedule(acb->bh); | |
813 | } | |
814 | ||
7c84b1b8 | 815 | static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, |
98392453 | 816 | unsigned long int req, void *buf, |
097310b5 | 817 | BlockCompletionFunc *cb, void *opaque) |
98392453 RS |
818 | { |
819 | IscsiLun *iscsilun = bs->opaque; | |
820 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
821 | struct iscsi_data data; | |
822 | IscsiAIOCB *acb; | |
823 | ||
d7331bed | 824 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); |
98392453 RS |
825 | |
826 | acb->iscsilun = iscsilun; | |
1bd075f2 PB |
827 | acb->bh = NULL; |
828 | acb->status = -EINPROGRESS; | |
98392453 RS |
829 | acb->buf = NULL; |
830 | acb->ioh = buf; | |
831 | ||
4bb17ab5 FZ |
832 | if (req != SG_IO) { |
833 | iscsi_ioctl_handle_emulated(acb, req, buf); | |
834 | return &acb->common; | |
835 | } | |
836 | ||
98392453 RS |
837 | acb->task = malloc(sizeof(struct scsi_task)); |
838 | if (acb->task == NULL) { | |
839 | error_report("iSCSI: Failed to allocate task for scsi command. %s", | |
840 | iscsi_get_error(iscsi)); | |
8007429a | 841 | qemu_aio_unref(acb); |
98392453 RS |
842 | return NULL; |
843 | } | |
844 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
845 | ||
846 | switch (acb->ioh->dxfer_direction) { | |
847 | case SG_DXFER_TO_DEV: | |
848 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
849 | break; | |
850 | case SG_DXFER_FROM_DEV: | |
851 | acb->task->xfer_dir = SCSI_XFER_READ; | |
852 | break; | |
853 | default: | |
854 | acb->task->xfer_dir = SCSI_XFER_NONE; | |
855 | break; | |
856 | } | |
857 | ||
858 | acb->task->cdb_size = acb->ioh->cmd_len; | |
859 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); | |
860 | acb->task->expxferlen = acb->ioh->dxfer_len; | |
861 | ||
0a53f010 | 862 | data.size = 0; |
98392453 | 863 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { |
0a53f010 RS |
864 | if (acb->ioh->iovec_count == 0) { |
865 | data.data = acb->ioh->dxferp; | |
866 | data.size = acb->ioh->dxfer_len; | |
867 | } else { | |
0a53f010 RS |
868 | scsi_task_set_iov_out(acb->task, |
869 | (struct scsi_iovec *) acb->ioh->dxferp, | |
870 | acb->ioh->iovec_count); | |
0a53f010 | 871 | } |
98392453 | 872 | } |
0a53f010 | 873 | |
98392453 RS |
874 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, |
875 | iscsi_aio_ioctl_cb, | |
0a53f010 | 876 | (data.size > 0) ? &data : NULL, |
98392453 RS |
877 | acb) != 0) { |
878 | scsi_free_scsi_task(acb->task); | |
8007429a | 879 | qemu_aio_unref(acb); |
98392453 RS |
880 | return NULL; |
881 | } | |
882 | ||
883 | /* tell libiscsi to read straight into the buffer we got from ioctl */ | |
884 | if (acb->task->xfer_dir == SCSI_XFER_READ) { | |
0a53f010 RS |
885 | if (acb->ioh->iovec_count == 0) { |
886 | scsi_task_add_data_in_buffer(acb->task, | |
887 | acb->ioh->dxfer_len, | |
888 | acb->ioh->dxferp); | |
889 | } else { | |
0a53f010 RS |
890 | scsi_task_set_iov_in(acb->task, |
891 | (struct scsi_iovec *) acb->ioh->dxferp, | |
892 | acb->ioh->iovec_count); | |
0a53f010 | 893 | } |
98392453 RS |
894 | } |
895 | ||
896 | iscsi_set_events(iscsilun); | |
897 | ||
898 | return &acb->common; | |
899 | } | |
900 | ||
98392453 RS |
901 | #endif |
902 | ||
c589b249 RS |
903 | static int64_t |
904 | iscsi_getlength(BlockDriverState *bs) | |
905 | { | |
906 | IscsiLun *iscsilun = bs->opaque; | |
907 | int64_t len; | |
908 | ||
909 | len = iscsilun->num_blocks; | |
910 | len *= iscsilun->block_size; | |
911 | ||
912 | return len; | |
913 | } | |
914 | ||
65f3e339 PL |
915 | static int |
916 | coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num, | |
917 | int nb_sectors) | |
918 | { | |
919 | IscsiLun *iscsilun = bs->opaque; | |
920 | struct IscsiTask iTask; | |
921 | struct unmap_list list; | |
65f3e339 PL |
922 | |
923 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
924 | return -EINVAL; | |
925 | } | |
926 | ||
927 | if (!iscsilun->lbp.lbpu) { | |
928 | /* UNMAP is not supported by the target */ | |
929 | return 0; | |
930 | } | |
931 | ||
932 | list.lba = sector_qemu2lun(sector_num, iscsilun); | |
01a6a238 | 933 | list.num = sector_qemu2lun(nb_sectors, iscsilun); |
65f3e339 | 934 | |
01a6a238 | 935 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
65f3e339 | 936 | retry: |
01a6a238 PL |
937 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, |
938 | iscsi_co_generic_cb, &iTask) == NULL) { | |
92397116 | 939 | return -ENOMEM; |
01a6a238 | 940 | } |
65f3e339 | 941 | |
01a6a238 PL |
942 | while (!iTask.complete) { |
943 | iscsi_set_events(iscsilun); | |
944 | qemu_coroutine_yield(); | |
945 | } | |
65f3e339 | 946 | |
01a6a238 PL |
947 | if (iTask.task != NULL) { |
948 | scsi_free_scsi_task(iTask.task); | |
949 | iTask.task = NULL; | |
950 | } | |
65f3e339 | 951 | |
01a6a238 | 952 | if (iTask.do_retry) { |
837c3901 | 953 | iTask.complete = 0; |
01a6a238 PL |
954 | goto retry; |
955 | } | |
65f3e339 | 956 | |
01a6a238 PL |
957 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { |
958 | /* the target might fail with a check condition if it | |
959 | is not happy with the alignment of the UNMAP request | |
960 | we silently fail in this case */ | |
961 | return 0; | |
962 | } | |
65f3e339 | 963 | |
01a6a238 | 964 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 965 | return iTask.err_code; |
65f3e339 PL |
966 | } |
967 | ||
b03c3805 PL |
968 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); |
969 | ||
65f3e339 PL |
970 | return 0; |
971 | } | |
972 | ||
d4cd9615 PL |
973 | static int |
974 | coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, | |
975 | int nb_sectors, BdrvRequestFlags flags) | |
976 | { | |
977 | IscsiLun *iscsilun = bs->opaque; | |
978 | struct IscsiTask iTask; | |
979 | uint64_t lba; | |
980 | uint32_t nb_blocks; | |
9281fe9e | 981 | bool use_16_for_ws = iscsilun->use_16_for_rw; |
d4cd9615 PL |
982 | |
983 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
984 | return -EINVAL; | |
985 | } | |
986 | ||
9281fe9e PL |
987 | if (flags & BDRV_REQ_MAY_UNMAP) { |
988 | if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { | |
989 | /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ | |
990 | use_16_for_ws = true; | |
991 | } | |
992 | if (use_16_for_ws && !iscsilun->lbp.lbpws) { | |
993 | /* WRITESAME16 with UNMAP is not supported by the target, | |
994 | * fall back and try WRITESAME10/16 without UNMAP */ | |
995 | flags &= ~BDRV_REQ_MAY_UNMAP; | |
996 | use_16_for_ws = iscsilun->use_16_for_rw; | |
997 | } | |
fa6252b0 PB |
998 | } |
999 | ||
dbe5c58f | 1000 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { |
9281fe9e | 1001 | /* WRITESAME without UNMAP is not supported by the target */ |
d4cd9615 PL |
1002 | return -ENOTSUP; |
1003 | } | |
1004 | ||
1005 | lba = sector_qemu2lun(sector_num, iscsilun); | |
1006 | nb_blocks = sector_qemu2lun(nb_sectors, iscsilun); | |
1007 | ||
1008 | if (iscsilun->zeroblock == NULL) { | |
4d5a3f88 KW |
1009 | iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); |
1010 | if (iscsilun->zeroblock == NULL) { | |
1011 | return -ENOMEM; | |
1012 | } | |
d4cd9615 PL |
1013 | } |
1014 | ||
1015 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
73b5394e | 1016 | iTask.force_next_flush = true; |
d4cd9615 | 1017 | retry: |
9281fe9e PL |
1018 | if (use_16_for_ws) { |
1019 | iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
1020 | iscsilun->zeroblock, iscsilun->block_size, | |
1021 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
1022 | 0, 0, iscsi_co_generic_cb, &iTask); | |
1023 | } else { | |
1024 | iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
1025 | iscsilun->zeroblock, iscsilun->block_size, | |
1026 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
1027 | 0, 0, iscsi_co_generic_cb, &iTask); | |
1028 | } | |
1029 | if (iTask.task == NULL) { | |
92397116 | 1030 | return -ENOMEM; |
d4cd9615 PL |
1031 | } |
1032 | ||
1033 | while (!iTask.complete) { | |
1034 | iscsi_set_events(iscsilun); | |
1035 | qemu_coroutine_yield(); | |
1036 | } | |
1037 | ||
d9738fd2 PL |
1038 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && |
1039 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && | |
27898a5d PB |
1040 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || |
1041 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { | |
d9738fd2 PL |
1042 | /* WRITE SAME is not supported by the target */ |
1043 | iscsilun->has_write_same = false; | |
1044 | scsi_free_scsi_task(iTask.task); | |
1045 | return -ENOTSUP; | |
1046 | } | |
1047 | ||
d4cd9615 PL |
1048 | if (iTask.task != NULL) { |
1049 | scsi_free_scsi_task(iTask.task); | |
1050 | iTask.task = NULL; | |
1051 | } | |
1052 | ||
1053 | if (iTask.do_retry) { | |
837c3901 | 1054 | iTask.complete = 0; |
d4cd9615 PL |
1055 | goto retry; |
1056 | } | |
1057 | ||
1058 | if (iTask.status != SCSI_STATUS_GOOD) { | |
e01dd3da | 1059 | return iTask.err_code; |
d4cd9615 PL |
1060 | } |
1061 | ||
b03c3805 PL |
1062 | if (flags & BDRV_REQ_MAY_UNMAP) { |
1063 | iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); | |
1064 | } else { | |
1065 | iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); | |
1066 | } | |
1067 | ||
d4cd9615 PL |
1068 | return 0; |
1069 | } | |
1070 | ||
f2917853 PB |
1071 | static void parse_chap(struct iscsi_context *iscsi, const char *target, |
1072 | Error **errp) | |
f9dadc98 RS |
1073 | { |
1074 | QemuOptsList *list; | |
1075 | QemuOpts *opts; | |
1076 | const char *user = NULL; | |
1077 | const char *password = NULL; | |
1078 | ||
1079 | list = qemu_find_opts("iscsi"); | |
1080 | if (!list) { | |
f2917853 | 1081 | return; |
f9dadc98 RS |
1082 | } |
1083 | ||
1084 | opts = qemu_opts_find(list, target); | |
1085 | if (opts == NULL) { | |
1086 | opts = QTAILQ_FIRST(&list->head); | |
1087 | if (!opts) { | |
f2917853 | 1088 | return; |
f9dadc98 RS |
1089 | } |
1090 | } | |
1091 | ||
1092 | user = qemu_opt_get(opts, "user"); | |
1093 | if (!user) { | |
f2917853 | 1094 | return; |
f9dadc98 RS |
1095 | } |
1096 | ||
1097 | password = qemu_opt_get(opts, "password"); | |
1098 | if (!password) { | |
f2917853 PB |
1099 | error_setg(errp, "CHAP username specified but no password was given"); |
1100 | return; | |
f9dadc98 RS |
1101 | } |
1102 | ||
1103 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { | |
f2917853 | 1104 | error_setg(errp, "Failed to set initiator username and password"); |
f9dadc98 | 1105 | } |
f9dadc98 RS |
1106 | } |
1107 | ||
f2917853 PB |
1108 | static void parse_header_digest(struct iscsi_context *iscsi, const char *target, |
1109 | Error **errp) | |
f9dadc98 RS |
1110 | { |
1111 | QemuOptsList *list; | |
1112 | QemuOpts *opts; | |
1113 | const char *digest = NULL; | |
1114 | ||
1115 | list = qemu_find_opts("iscsi"); | |
1116 | if (!list) { | |
1117 | return; | |
1118 | } | |
1119 | ||
1120 | opts = qemu_opts_find(list, target); | |
1121 | if (opts == NULL) { | |
1122 | opts = QTAILQ_FIRST(&list->head); | |
1123 | if (!opts) { | |
1124 | return; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | digest = qemu_opt_get(opts, "header-digest"); | |
1129 | if (!digest) { | |
1130 | return; | |
1131 | } | |
1132 | ||
1133 | if (!strcmp(digest, "CRC32C")) { | |
1134 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); | |
1135 | } else if (!strcmp(digest, "NONE")) { | |
1136 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); | |
1137 | } else if (!strcmp(digest, "CRC32C-NONE")) { | |
1138 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); | |
1139 | } else if (!strcmp(digest, "NONE-CRC32C")) { | |
1140 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
1141 | } else { | |
f2917853 | 1142 | error_setg(errp, "Invalid header-digest setting : %s", digest); |
f9dadc98 RS |
1143 | } |
1144 | } | |
1145 | ||
1146 | static char *parse_initiator_name(const char *target) | |
1147 | { | |
1148 | QemuOptsList *list; | |
1149 | QemuOpts *opts; | |
5accc840 PB |
1150 | const char *name; |
1151 | char *iscsi_name; | |
1152 | UuidInfo *uuid_info; | |
f9dadc98 RS |
1153 | |
1154 | list = qemu_find_opts("iscsi"); | |
f2ef4a6d PB |
1155 | if (list) { |
1156 | opts = qemu_opts_find(list, target); | |
f9dadc98 | 1157 | if (!opts) { |
f2ef4a6d PB |
1158 | opts = QTAILQ_FIRST(&list->head); |
1159 | } | |
1160 | if (opts) { | |
1161 | name = qemu_opt_get(opts, "initiator-name"); | |
5accc840 PB |
1162 | if (name) { |
1163 | return g_strdup(name); | |
1164 | } | |
f9dadc98 RS |
1165 | } |
1166 | } | |
1167 | ||
5accc840 PB |
1168 | uuid_info = qmp_query_uuid(NULL); |
1169 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { | |
1170 | name = qemu_get_vm_name(); | |
f2ef4a6d | 1171 | } else { |
5accc840 | 1172 | name = uuid_info->UUID; |
f9dadc98 | 1173 | } |
5accc840 PB |
1174 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
1175 | name ? ":" : "", name ? name : ""); | |
1176 | qapi_free_UuidInfo(uuid_info); | |
1177 | return iscsi_name; | |
f9dadc98 RS |
1178 | } |
1179 | ||
5dd7a535 PL |
1180 | static int parse_timeout(const char *target) |
1181 | { | |
1182 | QemuOptsList *list; | |
1183 | QemuOpts *opts; | |
1184 | const char *timeout; | |
1185 | ||
1186 | list = qemu_find_opts("iscsi"); | |
1187 | if (list) { | |
1188 | opts = qemu_opts_find(list, target); | |
1189 | if (!opts) { | |
1190 | opts = QTAILQ_FIRST(&list->head); | |
1191 | } | |
1192 | if (opts) { | |
1193 | timeout = qemu_opt_get(opts, "timeout"); | |
1194 | if (timeout) { | |
1195 | return atoi(timeout); | |
1196 | } | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | return 0; | |
1201 | } | |
1202 | ||
5b5d34ec PL |
1203 | static void iscsi_nop_timed_event(void *opaque) |
1204 | { | |
1205 | IscsiLun *iscsilun = opaque; | |
1206 | ||
5dd7a535 | 1207 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { |
5b5d34ec | 1208 | error_report("iSCSI: NOP timeout. Reconnecting..."); |
5dd7a535 PL |
1209 | iscsilun->request_timed_out = true; |
1210 | } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { | |
5b5d34ec PL |
1211 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); |
1212 | return; | |
1213 | } | |
1214 | ||
bc72ad67 | 1215 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
5b5d34ec PL |
1216 | iscsi_set_events(iscsilun); |
1217 | } | |
5b5d34ec | 1218 | |
f2917853 | 1219 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) |
cb1b83e7 PL |
1220 | { |
1221 | struct scsi_task *task = NULL; | |
1222 | struct scsi_readcapacity10 *rc10 = NULL; | |
1223 | struct scsi_readcapacity16 *rc16 = NULL; | |
cb1b83e7 PL |
1224 | int retries = ISCSI_CMD_RETRIES; |
1225 | ||
1288844e PB |
1226 | do { |
1227 | if (task != NULL) { | |
1228 | scsi_free_scsi_task(task); | |
1229 | task = NULL; | |
cb1b83e7 | 1230 | } |
1288844e PB |
1231 | |
1232 | switch (iscsilun->type) { | |
1233 | case TYPE_DISK: | |
1234 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); | |
1235 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1236 | rc16 = scsi_datain_unmarshall(task); | |
1237 | if (rc16 == NULL) { | |
f2917853 | 1238 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); |
1288844e PB |
1239 | } else { |
1240 | iscsilun->block_size = rc16->block_length; | |
1241 | iscsilun->num_blocks = rc16->returned_lba + 1; | |
0a386e48 PL |
1242 | iscsilun->lbpme = !!rc16->lbpme; |
1243 | iscsilun->lbprz = !!rc16->lbprz; | |
9281fe9e | 1244 | iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); |
1288844e | 1245 | } |
1cb6d137 | 1246 | break; |
1288844e | 1247 | } |
1cb6d137 ZL |
1248 | if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
1249 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
1250 | break; | |
1251 | } | |
1252 | /* Fall through and try READ CAPACITY(10) instead. */ | |
1288844e PB |
1253 | case TYPE_ROM: |
1254 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); | |
1255 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1256 | rc10 = scsi_datain_unmarshall(task); | |
1257 | if (rc10 == NULL) { | |
f2917853 | 1258 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); |
1288844e PB |
1259 | } else { |
1260 | iscsilun->block_size = rc10->block_size; | |
1261 | if (rc10->lba == 0) { | |
1262 | /* blank disk loaded */ | |
1263 | iscsilun->num_blocks = 0; | |
1264 | } else { | |
1265 | iscsilun->num_blocks = rc10->lba + 1; | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | break; | |
1270 | default: | |
f2917853 | 1271 | return; |
cb1b83e7 | 1272 | } |
1288844e PB |
1273 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
1274 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION | |
1275 | && retries-- > 0); | |
cb1b83e7 | 1276 | |
1288844e | 1277 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
240125bc | 1278 | error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); |
6d1f252d PL |
1279 | } else if (!iscsilun->block_size || |
1280 | iscsilun->block_size % BDRV_SECTOR_SIZE) { | |
1281 | error_setg(errp, "iSCSI: the target returned an invalid " | |
1282 | "block size of %d.", iscsilun->block_size); | |
1288844e | 1283 | } |
cb1b83e7 PL |
1284 | if (task) { |
1285 | scsi_free_scsi_task(task); | |
1286 | } | |
cb1b83e7 PL |
1287 | } |
1288 | ||
60beb341 KW |
1289 | /* TODO Convert to fine grained options */ |
1290 | static QemuOptsList runtime_opts = { | |
1291 | .name = "iscsi", | |
1292 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1293 | .desc = { | |
1294 | { | |
1295 | .name = "filename", | |
1296 | .type = QEMU_OPT_STRING, | |
1297 | .help = "URL to the iscsi image", | |
1298 | }, | |
1299 | { /* end of list */ } | |
1300 | }, | |
1301 | }; | |
1302 | ||
35cb1748 | 1303 | static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, |
24d3bd67 | 1304 | int evpd, int pc, void **inq, Error **errp) |
35cb1748 PB |
1305 | { |
1306 | int full_size; | |
1307 | struct scsi_task *task = NULL; | |
1308 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); | |
1309 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1310 | goto fail; | |
1311 | } | |
1312 | full_size = scsi_datain_getfullsize(task); | |
1313 | if (full_size > task->datain.size) { | |
1314 | scsi_free_scsi_task(task); | |
1315 | ||
1316 | /* we need more data for the full list */ | |
1317 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); | |
f18a7cbb PL |
1318 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
1319 | goto fail; | |
1320 | } | |
35cb1748 | 1321 | } |
f18a7cbb | 1322 | |
24d3bd67 PL |
1323 | *inq = scsi_datain_unmarshall(task); |
1324 | if (*inq == NULL) { | |
1325 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); | |
172fc4dd | 1326 | goto fail_with_err; |
24d3bd67 PL |
1327 | } |
1328 | ||
35cb1748 | 1329 | return task; |
f18a7cbb PL |
1330 | |
1331 | fail: | |
172fc4dd MA |
1332 | error_setg(errp, "iSCSI: Inquiry command failed : %s", |
1333 | iscsi_get_error(iscsi)); | |
1334 | fail_with_err: | |
24d3bd67 | 1335 | if (task != NULL) { |
35cb1748 | 1336 | scsi_free_scsi_task(task); |
35cb1748 PB |
1337 | } |
1338 | return NULL; | |
f18a7cbb PL |
1339 | } |
1340 | ||
80cf6257 SH |
1341 | static void iscsi_detach_aio_context(BlockDriverState *bs) |
1342 | { | |
1343 | IscsiLun *iscsilun = bs->opaque; | |
1344 | ||
dca21ef2 FZ |
1345 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), |
1346 | false, NULL, NULL, NULL); | |
80cf6257 SH |
1347 | iscsilun->events = 0; |
1348 | ||
1349 | if (iscsilun->nop_timer) { | |
1350 | timer_del(iscsilun->nop_timer); | |
1351 | timer_free(iscsilun->nop_timer); | |
1352 | iscsilun->nop_timer = NULL; | |
1353 | } | |
05b685fb PL |
1354 | if (iscsilun->event_timer) { |
1355 | timer_del(iscsilun->event_timer); | |
1356 | timer_free(iscsilun->event_timer); | |
1357 | iscsilun->event_timer = NULL; | |
1358 | } | |
80cf6257 SH |
1359 | } |
1360 | ||
1361 | static void iscsi_attach_aio_context(BlockDriverState *bs, | |
1362 | AioContext *new_context) | |
1363 | { | |
1364 | IscsiLun *iscsilun = bs->opaque; | |
1365 | ||
1366 | iscsilun->aio_context = new_context; | |
1367 | iscsi_set_events(iscsilun); | |
1368 | ||
80cf6257 SH |
1369 | /* Set up a timer for sending out iSCSI NOPs */ |
1370 | iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, | |
1371 | QEMU_CLOCK_REALTIME, SCALE_MS, | |
1372 | iscsi_nop_timed_event, iscsilun); | |
1373 | timer_mod(iscsilun->nop_timer, | |
1374 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); | |
05b685fb | 1375 | |
5dd7a535 PL |
1376 | /* Set up a timer for periodic calls to iscsi_set_events and to |
1377 | * scan for command timeout */ | |
05b685fb PL |
1378 | iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, |
1379 | QEMU_CLOCK_REALTIME, SCALE_MS, | |
5dd7a535 PL |
1380 | iscsi_timed_check_events, iscsilun); |
1381 | timer_mod(iscsilun->event_timer, | |
1382 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); | |
80cf6257 SH |
1383 | } |
1384 | ||
7191f208 | 1385 | static void iscsi_modesense_sync(IscsiLun *iscsilun) |
c1d4096b FZ |
1386 | { |
1387 | struct scsi_task *task; | |
1388 | struct scsi_mode_sense *ms = NULL; | |
7191f208 | 1389 | iscsilun->write_protected = false; |
752ce451 | 1390 | iscsilun->dpofua = false; |
c1d4096b FZ |
1391 | |
1392 | task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, | |
1393 | 1, SCSI_MODESENSE_PC_CURRENT, | |
1394 | 0x3F, 0, 255); | |
1395 | if (task == NULL) { | |
1396 | error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", | |
1397 | iscsi_get_error(iscsilun->iscsi)); | |
1398 | goto out; | |
1399 | } | |
1400 | ||
1401 | if (task->status != SCSI_STATUS_GOOD) { | |
1402 | error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); | |
1403 | goto out; | |
1404 | } | |
1405 | ms = scsi_datain_unmarshall(task); | |
1406 | if (!ms) { | |
1407 | error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", | |
1408 | iscsi_get_error(iscsilun->iscsi)); | |
1409 | goto out; | |
1410 | } | |
7191f208 | 1411 | iscsilun->write_protected = ms->device_specific_parameter & 0x80; |
752ce451 | 1412 | iscsilun->dpofua = ms->device_specific_parameter & 0x10; |
c1d4096b FZ |
1413 | |
1414 | out: | |
1415 | if (task) { | |
1416 | scsi_free_scsi_task(task); | |
1417 | } | |
c1d4096b FZ |
1418 | } |
1419 | ||
c589b249 RS |
1420 | /* |
1421 | * We support iscsi url's on the form | |
1422 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> | |
1423 | */ | |
015a1036 HR |
1424 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, |
1425 | Error **errp) | |
c589b249 RS |
1426 | { |
1427 | IscsiLun *iscsilun = bs->opaque; | |
1428 | struct iscsi_context *iscsi = NULL; | |
1429 | struct iscsi_url *iscsi_url = NULL; | |
e829b0bb PL |
1430 | struct scsi_task *task = NULL; |
1431 | struct scsi_inquiry_standard *inq = NULL; | |
24d3bd67 | 1432 | struct scsi_inquiry_supported_pages *inq_vpd; |
f9dadc98 | 1433 | char *initiator_name = NULL; |
60beb341 KW |
1434 | QemuOpts *opts; |
1435 | Error *local_err = NULL; | |
1436 | const char *filename; | |
9049736e | 1437 | int i, ret = 0, timeout = 0; |
c589b249 | 1438 | |
87ea75d5 | 1439 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
60beb341 | 1440 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 1441 | if (local_err) { |
f2917853 | 1442 | error_propagate(errp, local_err); |
60beb341 KW |
1443 | ret = -EINVAL; |
1444 | goto out; | |
1445 | } | |
1446 | ||
1447 | filename = qemu_opt_get(opts, "filename"); | |
1448 | ||
c589b249 RS |
1449 | iscsi_url = iscsi_parse_full_url(iscsi, filename); |
1450 | if (iscsi_url == NULL) { | |
f2917853 | 1451 | error_setg(errp, "Failed to parse URL : %s", filename); |
c589b249 | 1452 | ret = -EINVAL; |
b93c94f7 | 1453 | goto out; |
c589b249 RS |
1454 | } |
1455 | ||
f9dadc98 RS |
1456 | memset(iscsilun, 0, sizeof(IscsiLun)); |
1457 | ||
1458 | initiator_name = parse_initiator_name(iscsi_url->target); | |
1459 | ||
1460 | iscsi = iscsi_create_context(initiator_name); | |
1461 | if (iscsi == NULL) { | |
f2917853 | 1462 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); |
f9dadc98 | 1463 | ret = -ENOMEM; |
b93c94f7 | 1464 | goto out; |
f9dadc98 RS |
1465 | } |
1466 | ||
c589b249 | 1467 | if (iscsi_set_targetname(iscsi, iscsi_url->target)) { |
f2917853 | 1468 | error_setg(errp, "iSCSI: Failed to set target name."); |
c589b249 | 1469 | ret = -EINVAL; |
b93c94f7 | 1470 | goto out; |
c589b249 RS |
1471 | } |
1472 | ||
532cee41 | 1473 | if (iscsi_url->user[0] != '\0') { |
c589b249 RS |
1474 | ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, |
1475 | iscsi_url->passwd); | |
1476 | if (ret != 0) { | |
f2917853 | 1477 | error_setg(errp, "Failed to set initiator username and password"); |
c589b249 | 1478 | ret = -EINVAL; |
b93c94f7 | 1479 | goto out; |
c589b249 RS |
1480 | } |
1481 | } | |
f9dadc98 RS |
1482 | |
1483 | /* check if we got CHAP username/password via the options */ | |
f2917853 PB |
1484 | parse_chap(iscsi, iscsi_url->target, &local_err); |
1485 | if (local_err != NULL) { | |
1486 | error_propagate(errp, local_err); | |
f9dadc98 | 1487 | ret = -EINVAL; |
b93c94f7 | 1488 | goto out; |
f9dadc98 RS |
1489 | } |
1490 | ||
c589b249 | 1491 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
f2917853 | 1492 | error_setg(errp, "iSCSI: Failed to set session type to normal."); |
c589b249 | 1493 | ret = -EINVAL; |
b93c94f7 | 1494 | goto out; |
c589b249 RS |
1495 | } |
1496 | ||
1497 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
1498 | ||
f9dadc98 | 1499 | /* check if we got HEADER_DIGEST via the options */ |
f2917853 PB |
1500 | parse_header_digest(iscsi, iscsi_url->target, &local_err); |
1501 | if (local_err != NULL) { | |
1502 | error_propagate(errp, local_err); | |
1503 | ret = -EINVAL; | |
1504 | goto out; | |
1505 | } | |
f9dadc98 | 1506 | |
9049736e PL |
1507 | /* timeout handling is broken in libiscsi before 1.15.0 */ |
1508 | timeout = parse_timeout(iscsi_url->target); | |
1509 | #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621 | |
1510 | iscsi_set_timeout(iscsi, timeout); | |
1511 | #else | |
1512 | if (timeout) { | |
1513 | error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); | |
1514 | } | |
1515 | #endif | |
5dd7a535 | 1516 | |
e829b0bb | 1517 | if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) { |
f2917853 | 1518 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", |
e829b0bb PL |
1519 | iscsi_get_error(iscsi)); |
1520 | ret = -EINVAL; | |
1521 | goto out; | |
1522 | } | |
c589b249 RS |
1523 | |
1524 | iscsilun->iscsi = iscsi; | |
80cf6257 | 1525 | iscsilun->aio_context = bdrv_get_aio_context(bs); |
c589b249 | 1526 | iscsilun->lun = iscsi_url->lun; |
24d3bd67 | 1527 | iscsilun->has_write_same = true; |
c589b249 | 1528 | |
24d3bd67 PL |
1529 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, |
1530 | (void **) &inq, errp); | |
1531 | if (task == NULL) { | |
c589b249 | 1532 | ret = -EINVAL; |
b93c94f7 | 1533 | goto out; |
c589b249 | 1534 | } |
e829b0bb | 1535 | iscsilun->type = inq->periperal_device_type; |
24d3bd67 PL |
1536 | scsi_free_scsi_task(task); |
1537 | task = NULL; | |
e829b0bb | 1538 | |
7191f208 PL |
1539 | iscsi_modesense_sync(iscsilun); |
1540 | ||
c1d4096b FZ |
1541 | /* Check the write protect flag of the LUN if we want to write */ |
1542 | if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && | |
43ae8fb1 | 1543 | iscsilun->write_protected) { |
c1d4096b FZ |
1544 | error_setg(errp, "Cannot open a write protected LUN as read-write"); |
1545 | ret = -EACCES; | |
1546 | goto out; | |
1547 | } | |
1548 | ||
f2917853 PB |
1549 | iscsi_readcapacity_sync(iscsilun, &local_err); |
1550 | if (local_err != NULL) { | |
1551 | error_propagate(errp, local_err); | |
cd82b6fb | 1552 | ret = -EINVAL; |
cb1b83e7 | 1553 | goto out; |
e829b0bb | 1554 | } |
0777b5dd | 1555 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); |
2c9880c4 | 1556 | bs->request_alignment = iscsilun->block_size; |
e829b0bb | 1557 | |
f47c3f5a KW |
1558 | /* We don't have any emulation for devices other than disks and CD-ROMs, so |
1559 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu | |
1560 | * will try to read from the device to guess the image format. | |
622695a4 | 1561 | */ |
f47c3f5a | 1562 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { |
622695a4 RS |
1563 | bs->sg = 1; |
1564 | } | |
1565 | ||
24d3bd67 PL |
1566 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
1567 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, | |
1568 | (void **) &inq_vpd, errp); | |
1569 | if (task == NULL) { | |
1570 | ret = -EINVAL; | |
1571 | goto out; | |
f18a7cbb | 1572 | } |
24d3bd67 PL |
1573 | for (i = 0; i < inq_vpd->num_pages; i++) { |
1574 | struct scsi_task *inq_task; | |
1575 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; | |
f18a7cbb | 1576 | struct scsi_inquiry_block_limits *inq_bl; |
24d3bd67 PL |
1577 | switch (inq_vpd->pages[i]) { |
1578 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: | |
1579 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1580 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, | |
1581 | (void **) &inq_lbp, errp); | |
1582 | if (inq_task == NULL) { | |
1583 | ret = -EINVAL; | |
1584 | goto out; | |
1585 | } | |
1586 | memcpy(&iscsilun->lbp, inq_lbp, | |
1587 | sizeof(struct scsi_inquiry_logical_block_provisioning)); | |
1588 | scsi_free_scsi_task(inq_task); | |
1589 | break; | |
1590 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: | |
1591 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1592 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, | |
1593 | (void **) &inq_bl, errp); | |
1594 | if (inq_task == NULL) { | |
1595 | ret = -EINVAL; | |
1596 | goto out; | |
1597 | } | |
1598 | memcpy(&iscsilun->bl, inq_bl, | |
1599 | sizeof(struct scsi_inquiry_block_limits)); | |
1600 | scsi_free_scsi_task(inq_task); | |
1601 | break; | |
1602 | default: | |
1603 | break; | |
f18a7cbb | 1604 | } |
f18a7cbb | 1605 | } |
24d3bd67 PL |
1606 | scsi_free_scsi_task(task); |
1607 | task = NULL; | |
f18a7cbb | 1608 | |
80cf6257 | 1609 | iscsi_attach_aio_context(bs, iscsilun->aio_context); |
5b5d34ec | 1610 | |
b03c3805 PL |
1611 | /* Guess the internal cluster (page) size of the iscsi target by the means |
1612 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a | |
1613 | * reasonable size */ | |
3d2acaa3 | 1614 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && |
b03c3805 PL |
1615 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { |
1616 | iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * | |
1617 | iscsilun->block_size) >> BDRV_SECTOR_BITS; | |
9eac3622 | 1618 | if (iscsilun->lbprz) { |
a9fe4c95 PL |
1619 | iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); |
1620 | if (iscsilun->allocationmap == NULL) { | |
1621 | ret = -ENOMEM; | |
1622 | } | |
b03c3805 | 1623 | } |
b03c3805 PL |
1624 | } |
1625 | ||
b93c94f7 | 1626 | out: |
60beb341 | 1627 | qemu_opts_del(opts); |
f7047c2d | 1628 | g_free(initiator_name); |
c589b249 RS |
1629 | if (iscsi_url != NULL) { |
1630 | iscsi_destroy_url(iscsi_url); | |
1631 | } | |
e829b0bb PL |
1632 | if (task != NULL) { |
1633 | scsi_free_scsi_task(task); | |
1634 | } | |
b93c94f7 PB |
1635 | |
1636 | if (ret) { | |
1637 | if (iscsi != NULL) { | |
20474e9a PL |
1638 | if (iscsi_is_logged_in(iscsi)) { |
1639 | iscsi_logout_sync(iscsi); | |
1640 | } | |
b93c94f7 PB |
1641 | iscsi_destroy_context(iscsi); |
1642 | } | |
1643 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
c589b249 | 1644 | } |
c589b249 RS |
1645 | return ret; |
1646 | } | |
1647 | ||
1648 | static void iscsi_close(BlockDriverState *bs) | |
1649 | { | |
1650 | IscsiLun *iscsilun = bs->opaque; | |
1651 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1652 | ||
80cf6257 | 1653 | iscsi_detach_aio_context(bs); |
20474e9a PL |
1654 | if (iscsi_is_logged_in(iscsi)) { |
1655 | iscsi_logout_sync(iscsi); | |
1656 | } | |
c589b249 | 1657 | iscsi_destroy_context(iscsi); |
d4cd9615 | 1658 | g_free(iscsilun->zeroblock); |
b03c3805 | 1659 | g_free(iscsilun->allocationmap); |
c589b249 RS |
1660 | memset(iscsilun, 0, sizeof(IscsiLun)); |
1661 | } | |
1662 | ||
52f6fa14 | 1663 | static int sector_limits_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
d34682cd | 1664 | { |
52f6fa14 PL |
1665 | return MIN(sector_lun2qemu(sector, iscsilun), INT_MAX / 2 + 1); |
1666 | } | |
d34682cd | 1667 | |
52f6fa14 PL |
1668 | static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) |
1669 | { | |
d34682cd KW |
1670 | /* We don't actually refresh here, but just return data queried in |
1671 | * iscsi_open(): iscsi targets don't change their limits. */ | |
52f6fa14 PL |
1672 | |
1673 | IscsiLun *iscsilun = bs->opaque; | |
1674 | uint32_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; | |
1675 | ||
1676 | if (iscsilun->bl.max_xfer_len) { | |
1677 | max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); | |
1678 | } | |
1679 | ||
1680 | bs->bl.max_transfer_length = sector_limits_lun2qemu(max_xfer_len, iscsilun); | |
1681 | ||
c97ca29d | 1682 | if (iscsilun->lbp.lbpu) { |
d34682cd | 1683 | if (iscsilun->bl.max_unmap < 0xffffffff) { |
3dab1551 PL |
1684 | bs->bl.max_discard = |
1685 | sector_limits_lun2qemu(iscsilun->bl.max_unmap, iscsilun); | |
d34682cd | 1686 | } |
3dab1551 PL |
1687 | bs->bl.discard_alignment = |
1688 | sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun); | |
c97ca29d | 1689 | } |
d34682cd | 1690 | |
c97ca29d | 1691 | if (iscsilun->bl.max_ws_len < 0xffffffff) { |
3dab1551 PL |
1692 | bs->bl.max_write_zeroes = |
1693 | sector_limits_lun2qemu(iscsilun->bl.max_ws_len, iscsilun); | |
c97ca29d PB |
1694 | } |
1695 | if (iscsilun->lbp.lbpws) { | |
3dab1551 PL |
1696 | bs->bl.write_zeroes_alignment = |
1697 | sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun); | |
d34682cd | 1698 | } |
3dab1551 PL |
1699 | bs->bl.opt_transfer_length = |
1700 | sector_limits_lun2qemu(iscsilun->bl.opt_xfer_len, iscsilun); | |
e9f526ab | 1701 | } |
d34682cd | 1702 | |
43ae8fb1 FZ |
1703 | /* Note that this will not re-establish a connection with an iSCSI target - it |
1704 | * is effectively a NOP. */ | |
dc6afb99 JC |
1705 | static int iscsi_reopen_prepare(BDRVReopenState *state, |
1706 | BlockReopenQueue *queue, Error **errp) | |
1707 | { | |
43ae8fb1 FZ |
1708 | IscsiLun *iscsilun = state->bs->opaque; |
1709 | ||
1710 | if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { | |
1711 | error_setg(errp, "Cannot open a write protected LUN as read-write"); | |
1712 | return -EACCES; | |
1713 | } | |
d34682cd KW |
1714 | return 0; |
1715 | } | |
1716 | ||
cb1b83e7 PL |
1717 | static int iscsi_truncate(BlockDriverState *bs, int64_t offset) |
1718 | { | |
1719 | IscsiLun *iscsilun = bs->opaque; | |
f2917853 | 1720 | Error *local_err = NULL; |
cb1b83e7 PL |
1721 | |
1722 | if (iscsilun->type != TYPE_DISK) { | |
1723 | return -ENOTSUP; | |
1724 | } | |
1725 | ||
f2917853 PB |
1726 | iscsi_readcapacity_sync(iscsilun, &local_err); |
1727 | if (local_err != NULL) { | |
1728 | error_free(local_err); | |
1729 | return -EIO; | |
cb1b83e7 PL |
1730 | } |
1731 | ||
1732 | if (offset > iscsi_getlength(bs)) { | |
1733 | return -EINVAL; | |
1734 | } | |
1735 | ||
b03c3805 PL |
1736 | if (iscsilun->allocationmap != NULL) { |
1737 | g_free(iscsilun->allocationmap); | |
a9fe4c95 | 1738 | iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); |
b03c3805 PL |
1739 | } |
1740 | ||
cb1b83e7 PL |
1741 | return 0; |
1742 | } | |
1743 | ||
a59479e3 | 1744 | static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) |
de8864e5 PL |
1745 | { |
1746 | int ret = 0; | |
1747 | int64_t total_size = 0; | |
13c91cb7 | 1748 | BlockDriverState *bs; |
de8864e5 | 1749 | IscsiLun *iscsilun = NULL; |
60beb341 | 1750 | QDict *bs_options; |
de8864e5 | 1751 | |
e4e9986b | 1752 | bs = bdrv_new(); |
de8864e5 PL |
1753 | |
1754 | /* Read out options */ | |
c2eb918e HT |
1755 | total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
1756 | BDRV_SECTOR_SIZE); | |
5839e53b | 1757 | bs->opaque = g_new0(struct IscsiLun, 1); |
13c91cb7 | 1758 | iscsilun = bs->opaque; |
de8864e5 | 1759 | |
60beb341 KW |
1760 | bs_options = qdict_new(); |
1761 | qdict_put(bs_options, "filename", qstring_from_str(filename)); | |
015a1036 | 1762 | ret = iscsi_open(bs, bs_options, 0, NULL); |
60beb341 KW |
1763 | QDECREF(bs_options); |
1764 | ||
de8864e5 PL |
1765 | if (ret != 0) { |
1766 | goto out; | |
1767 | } | |
80cf6257 | 1768 | iscsi_detach_aio_context(bs); |
de8864e5 PL |
1769 | if (iscsilun->type != TYPE_DISK) { |
1770 | ret = -ENODEV; | |
1771 | goto out; | |
1772 | } | |
13c91cb7 | 1773 | if (bs->total_sectors < total_size) { |
de8864e5 | 1774 | ret = -ENOSPC; |
d3bda7bc | 1775 | goto out; |
de8864e5 PL |
1776 | } |
1777 | ||
1778 | ret = 0; | |
1779 | out: | |
1780 | if (iscsilun->iscsi != NULL) { | |
1781 | iscsi_destroy_context(iscsilun->iscsi); | |
1782 | } | |
13c91cb7 FZ |
1783 | g_free(bs->opaque); |
1784 | bs->opaque = NULL; | |
4f6fd349 | 1785 | bdrv_unref(bs); |
de8864e5 PL |
1786 | return ret; |
1787 | } | |
1788 | ||
186d4f2b PL |
1789 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1790 | { | |
1791 | IscsiLun *iscsilun = bs->opaque; | |
0a386e48 | 1792 | bdi->unallocated_blocks_are_zero = iscsilun->lbprz; |
186d4f2b | 1793 | bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; |
b03c3805 | 1794 | bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE; |
186d4f2b PL |
1795 | return 0; |
1796 | } | |
1797 | ||
a59479e3 CL |
1798 | static QemuOptsList iscsi_create_opts = { |
1799 | .name = "iscsi-create-opts", | |
1800 | .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), | |
1801 | .desc = { | |
1802 | { | |
1803 | .name = BLOCK_OPT_SIZE, | |
1804 | .type = QEMU_OPT_SIZE, | |
1805 | .help = "Virtual disk size" | |
1806 | }, | |
1807 | { /* end of list */ } | |
1808 | } | |
de8864e5 PL |
1809 | }; |
1810 | ||
c589b249 RS |
1811 | static BlockDriver bdrv_iscsi = { |
1812 | .format_name = "iscsi", | |
1813 | .protocol_name = "iscsi", | |
1814 | ||
1815 | .instance_size = sizeof(IscsiLun), | |
030be321 | 1816 | .bdrv_needs_filename = true, |
c589b249 RS |
1817 | .bdrv_file_open = iscsi_open, |
1818 | .bdrv_close = iscsi_close, | |
c282e1fd | 1819 | .bdrv_create = iscsi_create, |
a59479e3 | 1820 | .create_opts = &iscsi_create_opts, |
dc6afb99 | 1821 | .bdrv_reopen_prepare = iscsi_reopen_prepare, |
c589b249 RS |
1822 | |
1823 | .bdrv_getlength = iscsi_getlength, | |
186d4f2b | 1824 | .bdrv_get_info = iscsi_get_info, |
cb1b83e7 | 1825 | .bdrv_truncate = iscsi_truncate, |
d34682cd | 1826 | .bdrv_refresh_limits = iscsi_refresh_limits, |
c589b249 | 1827 | |
54a5c1d5 | 1828 | .bdrv_co_get_block_status = iscsi_co_get_block_status, |
65f3e339 | 1829 | .bdrv_co_discard = iscsi_co_discard, |
d4cd9615 | 1830 | .bdrv_co_write_zeroes = iscsi_co_write_zeroes, |
063c3378 PL |
1831 | .bdrv_co_readv = iscsi_co_readv, |
1832 | .bdrv_co_writev = iscsi_co_writev, | |
1833 | .bdrv_co_flush_to_disk = iscsi_co_flush, | |
fa6acb0c | 1834 | |
98392453 | 1835 | #ifdef __linux__ |
98392453 RS |
1836 | .bdrv_aio_ioctl = iscsi_aio_ioctl, |
1837 | #endif | |
80cf6257 SH |
1838 | |
1839 | .bdrv_detach_aio_context = iscsi_detach_aio_context, | |
1840 | .bdrv_attach_aio_context = iscsi_attach_aio_context, | |
c589b249 RS |
1841 | }; |
1842 | ||
4d454574 PB |
1843 | static QemuOptsList qemu_iscsi_opts = { |
1844 | .name = "iscsi", | |
1845 | .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head), | |
1846 | .desc = { | |
1847 | { | |
1848 | .name = "user", | |
1849 | .type = QEMU_OPT_STRING, | |
1850 | .help = "username for CHAP authentication to target", | |
1851 | },{ | |
1852 | .name = "password", | |
1853 | .type = QEMU_OPT_STRING, | |
1854 | .help = "password for CHAP authentication to target", | |
1855 | },{ | |
1856 | .name = "header-digest", | |
1857 | .type = QEMU_OPT_STRING, | |
1858 | .help = "HeaderDigest setting. " | |
1859 | "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}", | |
1860 | },{ | |
1861 | .name = "initiator-name", | |
1862 | .type = QEMU_OPT_STRING, | |
1863 | .help = "Initiator iqn name to use when connecting", | |
5dd7a535 PL |
1864 | },{ |
1865 | .name = "timeout", | |
1866 | .type = QEMU_OPT_NUMBER, | |
1867 | .help = "Request timeout in seconds (default 0 = no timeout)", | |
4d454574 PB |
1868 | }, |
1869 | { /* end of list */ } | |
1870 | }, | |
1871 | }; | |
1872 | ||
c589b249 RS |
1873 | static void iscsi_block_init(void) |
1874 | { | |
1875 | bdrv_register(&bdrv_iscsi); | |
4d454574 | 1876 | qemu_add_opts(&qemu_iscsi_opts); |
c589b249 RS |
1877 | } |
1878 | ||
1879 | block_init(iscsi_block_init); |