]>
Commit | Line | Data |
---|---|---|
c589b249 RS |
1 | /* |
2 | * QEMU Block driver for iSCSI images | |
3 | * | |
4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]> | |
e1123a3b | 5 | * Copyright (c) 2012-2016 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" |
cea25275 | 39 | #include "qemu/uuid.h" |
5accc840 | 40 | #include "qmp-commands.h" |
d49b6836 | 41 | #include "qapi/qmp/qstring.h" |
b189346e | 42 | #include "crypto/secret.h" |
c589b249 RS |
43 | |
44 | #include <iscsi/iscsi.h> | |
45 | #include <iscsi/scsi-lowlevel.h> | |
46 | ||
98392453 RS |
47 | #ifdef __linux__ |
48 | #include <scsi/sg.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; |
d045c466 | 61 | QemuMutex mutex; |
f18a7cbb PL |
62 | struct scsi_inquiry_logical_block_provisioning lbp; |
63 | struct scsi_inquiry_block_limits bl; | |
d4cd9615 | 64 | unsigned char *zeroblock; |
e1123a3b PL |
65 | /* The allocmap tracks which clusters (pages) on the iSCSI target are |
66 | * allocated and which are not. In case a target returns zeros for | |
67 | * unallocated pages (iscsilun->lprz) we can directly return zeros instead | |
68 | * of reading zeros over the wire if a read request falls within an | |
69 | * unallocated block. As there are 3 possible states we need 2 bitmaps to | |
70 | * track. allocmap_valid keeps track if QEMU's information about a page is | |
71 | * valid. allocmap tracks if a page is allocated or not. In case QEMU has no | |
72 | * valid information about a page the corresponding allocmap entry should be | |
73 | * switched to unallocated as well to force a new lookup of the allocation | |
74 | * status as lookups are generally skipped if a page is suspect to be | |
75 | * allocated. If a iSCSI target is opened with cache.direct = on the | |
76 | * allocmap_valid does not exist turning all cached information invalid so | |
77 | * that a fresh lookup is made for any page even if allocmap entry returns | |
78 | * it's unallocated. */ | |
79 | unsigned long *allocmap; | |
80 | unsigned long *allocmap_valid; | |
81 | long allocmap_size; | |
b03c3805 | 82 | int cluster_sectors; |
9281fe9e | 83 | bool use_16_for_rw; |
43ae8fb1 | 84 | bool write_protected; |
0a386e48 PL |
85 | bool lbpme; |
86 | bool lbprz; | |
752ce451 | 87 | bool dpofua; |
0a386e48 | 88 | bool has_write_same; |
5dd7a535 | 89 | bool request_timed_out; |
c589b249 RS |
90 | } IscsiLun; |
91 | ||
54a5c1d5 PL |
92 | typedef struct IscsiTask { |
93 | int status; | |
94 | int complete; | |
95 | int retries; | |
96 | int do_retry; | |
97 | struct scsi_task *task; | |
98 | Coroutine *co; | |
80cf6257 | 99 | IscsiLun *iscsilun; |
efc6de0d | 100 | QEMUTimer retry_timer; |
e01dd3da | 101 | int err_code; |
54a5c1d5 PL |
102 | } IscsiTask; |
103 | ||
c589b249 | 104 | typedef struct IscsiAIOCB { |
7c84b1b8 | 105 | BlockAIOCB common; |
c589b249 RS |
106 | QEMUBH *bh; |
107 | IscsiLun *iscsilun; | |
108 | struct scsi_task *task; | |
109 | uint8_t *buf; | |
110 | int status; | |
1dde716e PL |
111 | int64_t sector_num; |
112 | int nb_sectors; | |
4bb17ab5 | 113 | int ret; |
98392453 RS |
114 | #ifdef __linux__ |
115 | sg_io_hdr_t *ioh; | |
116 | #endif | |
c589b249 RS |
117 | } IscsiAIOCB; |
118 | ||
5dd7a535 PL |
119 | /* libiscsi uses time_t so its enough to process events every second */ |
120 | #define EVENT_INTERVAL 1000 | |
5b5d34ec PL |
121 | #define NOP_INTERVAL 5000 |
122 | #define MAX_NOP_FAILURES 3 | |
efc6de0d | 123 | #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) |
59dd0a22 | 124 | static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768}; |
5b5d34ec | 125 | |
5d831be2 | 126 | /* this threshold is a trade-off knob to choose between |
5917af81 PL |
127 | * the potential additional overhead of an extra GET_LBA_STATUS request |
128 | * vs. unnecessarily reading a lot of zero sectors over the wire. | |
129 | * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES | |
130 | * sectors we check the allocation status of the area covered by the | |
131 | * request first if the allocationmap indicates that the area might be | |
132 | * unallocated. */ | |
133 | #define ISCSI_CHECKALLOC_THRES 64 | |
5b5d34ec | 134 | |
27cbd828 | 135 | static void |
cfb3f506 | 136 | iscsi_bh_cb(void *p) |
27cbd828 PB |
137 | { |
138 | IscsiAIOCB *acb = p; | |
139 | ||
140 | qemu_bh_delete(acb->bh); | |
141 | ||
4790b03d PB |
142 | g_free(acb->buf); |
143 | acb->buf = NULL; | |
144 | ||
722d9333 | 145 | acb->common.cb(acb->common.opaque, acb->status); |
27cbd828 | 146 | |
1bd075f2 PB |
147 | if (acb->task != NULL) { |
148 | scsi_free_scsi_task(acb->task); | |
149 | acb->task = NULL; | |
150 | } | |
151 | ||
8007429a | 152 | qemu_aio_unref(acb); |
27cbd828 PB |
153 | } |
154 | ||
cfb3f506 PB |
155 | static void |
156 | iscsi_schedule_bh(IscsiAIOCB *acb) | |
27cbd828 | 157 | { |
1bd075f2 PB |
158 | if (acb->bh) { |
159 | return; | |
160 | } | |
80cf6257 | 161 | acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); |
27cbd828 | 162 | qemu_bh_schedule(acb->bh); |
27cbd828 PB |
163 | } |
164 | ||
8b9dfe90 PL |
165 | static void iscsi_co_generic_bh_cb(void *opaque) |
166 | { | |
167 | struct IscsiTask *iTask = opaque; | |
1919631e | 168 | |
fcd470d8 | 169 | iTask->complete = 1; |
1919631e | 170 | aio_co_wake(iTask->co); |
8b9dfe90 PL |
171 | } |
172 | ||
efc6de0d PL |
173 | static void iscsi_retry_timer_expired(void *opaque) |
174 | { | |
175 | struct IscsiTask *iTask = opaque; | |
fcd470d8 | 176 | iTask->complete = 1; |
efc6de0d | 177 | if (iTask->co) { |
2f47da5f | 178 | aio_co_wake(iTask->co); |
efc6de0d PL |
179 | } |
180 | } | |
181 | ||
182 | static inline unsigned exp_random(double mean) | |
183 | { | |
184 | return -mean * log((double)rand() / RAND_MAX); | |
185 | } | |
186 | ||
e01dd3da FZ |
187 | /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in |
188 | * libiscsi 1.10.0, together with other constants we need. Use it as | |
189 | * a hint that we have to define them ourselves if needed, to keep the | |
190 | * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for | |
191 | * the test because SCSI_STATUS_* is an enum. | |
192 | * | |
193 | * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes | |
194 | * an enum, check against the LIBISCSI_API_VERSION macro, which was | |
195 | * introduced in 1.11.0. If it is present, there is no need to define | |
196 | * anything. | |
197 | */ | |
198 | #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ | |
199 | !defined(LIBISCSI_API_VERSION) | |
200 | #define SCSI_STATUS_TASK_SET_FULL 0x28 | |
201 | #define SCSI_STATUS_TIMEOUT 0x0f000002 | |
202 | #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 | |
203 | #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 | |
9049736e PL |
204 | #endif |
205 | ||
583ec22e RS |
206 | #ifndef LIBISCSI_API_VERSION |
207 | #define LIBISCSI_API_VERSION 20130701 | |
208 | #endif | |
209 | ||
e01dd3da FZ |
210 | static int iscsi_translate_sense(struct scsi_sense *sense) |
211 | { | |
212 | int ret; | |
213 | ||
214 | switch (sense->key) { | |
215 | case SCSI_SENSE_NOT_READY: | |
216 | return -EBUSY; | |
217 | case SCSI_SENSE_DATA_PROTECTION: | |
218 | return -EACCES; | |
219 | case SCSI_SENSE_COMMAND_ABORTED: | |
220 | return -ECANCELED; | |
221 | case SCSI_SENSE_ILLEGAL_REQUEST: | |
222 | /* Parse ASCQ */ | |
223 | break; | |
224 | default: | |
225 | return -EIO; | |
226 | } | |
227 | switch (sense->ascq) { | |
228 | case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR: | |
229 | case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE: | |
230 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB: | |
231 | case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST: | |
232 | ret = -EINVAL; | |
233 | break; | |
234 | case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE: | |
235 | ret = -ENOSPC; | |
236 | break; | |
237 | case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED: | |
238 | ret = -ENOTSUP; | |
239 | break; | |
240 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT: | |
241 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED: | |
242 | case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN: | |
243 | ret = -ENOMEDIUM; | |
244 | break; | |
245 | case SCSI_SENSE_ASCQ_WRITE_PROTECTED: | |
246 | ret = -EACCES; | |
247 | break; | |
248 | default: | |
249 | ret = -EIO; | |
250 | break; | |
251 | } | |
252 | return ret; | |
253 | } | |
254 | ||
d045c466 | 255 | /* Called (via iscsi_service) with QemuMutex held. */ |
54a5c1d5 PL |
256 | static void |
257 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, | |
258 | void *command_data, void *opaque) | |
259 | { | |
260 | struct IscsiTask *iTask = opaque; | |
261 | struct scsi_task *task = command_data; | |
262 | ||
54a5c1d5 PL |
263 | iTask->status = status; |
264 | iTask->do_retry = 0; | |
265 | iTask->task = task; | |
266 | ||
54a5c1d5 | 267 | if (status != SCSI_STATUS_GOOD) { |
efc6de0d PL |
268 | if (iTask->retries++ < ISCSI_CMD_RETRIES) { |
269 | if (status == SCSI_STATUS_CHECK_CONDITION | |
270 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
271 | error_report("iSCSI CheckCondition: %s", | |
272 | iscsi_get_error(iscsi)); | |
273 | iTask->do_retry = 1; | |
274 | goto out; | |
275 | } | |
9049736e | 276 | if (status == SCSI_STATUS_BUSY || |
e01dd3da FZ |
277 | status == SCSI_STATUS_TIMEOUT || |
278 | status == SCSI_STATUS_TASK_SET_FULL) { | |
efc6de0d PL |
279 | unsigned retry_time = |
280 | exp_random(iscsi_retry_times[iTask->retries - 1]); | |
e01dd3da | 281 | if (status == SCSI_STATUS_TIMEOUT) { |
5dd7a535 PL |
282 | /* make sure the request is rescheduled AFTER the |
283 | * reconnect is initiated */ | |
284 | retry_time = EVENT_INTERVAL * 2; | |
285 | iTask->iscsilun->request_timed_out = true; | |
286 | } | |
287 | error_report("iSCSI Busy/TaskSetFull/TimeOut" | |
288 | " (retry #%u in %u ms): %s", | |
efc6de0d PL |
289 | iTask->retries, retry_time, |
290 | iscsi_get_error(iscsi)); | |
291 | aio_timer_init(iTask->iscsilun->aio_context, | |
292 | &iTask->retry_timer, QEMU_CLOCK_REALTIME, | |
293 | SCALE_MS, iscsi_retry_timer_expired, iTask); | |
294 | timer_mod(&iTask->retry_timer, | |
295 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); | |
296 | iTask->do_retry = 1; | |
297 | return; | |
298 | } | |
299 | } | |
e01dd3da | 300 | iTask->err_code = iscsi_translate_sense(&task->sense); |
837c3901 | 301 | error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); |
54a5c1d5 PL |
302 | } |
303 | ||
304 | out: | |
305 | if (iTask->co) { | |
fffb6e12 PB |
306 | aio_bh_schedule_oneshot(iTask->iscsilun->aio_context, |
307 | iscsi_co_generic_bh_cb, iTask); | |
fcd470d8 PL |
308 | } else { |
309 | iTask->complete = 1; | |
54a5c1d5 PL |
310 | } |
311 | } | |
312 | ||
313 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) | |
314 | { | |
315 | *iTask = (struct IscsiTask) { | |
efc6de0d | 316 | .co = qemu_coroutine_self(), |
efc6de0d | 317 | .iscsilun = iscsilun, |
54a5c1d5 PL |
318 | }; |
319 | } | |
27cbd828 | 320 | |
c589b249 RS |
321 | static void |
322 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
323 | void *private_data) | |
324 | { | |
1bd075f2 PB |
325 | IscsiAIOCB *acb = private_data; |
326 | ||
327 | acb->status = -ECANCELED; | |
328 | iscsi_schedule_bh(acb); | |
c589b249 RS |
329 | } |
330 | ||
331 | static void | |
7c84b1b8 | 332 | iscsi_aio_cancel(BlockAIOCB *blockacb) |
c589b249 RS |
333 | { |
334 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; | |
335 | IscsiLun *iscsilun = acb->iscsilun; | |
336 | ||
1bd075f2 PB |
337 | if (acb->status != -EINPROGRESS) { |
338 | return; | |
339 | } | |
340 | ||
b2090919 | 341 | /* send a task mgmt call to the target to cancel the task on the target */ |
64e69e80 | 342 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
1bd075f2 | 343 | iscsi_abort_task_cb, acb); |
b2090919 | 344 | |
c589b249 RS |
345 | } |
346 | ||
d7331bed | 347 | static const AIOCBInfo iscsi_aiocb_info = { |
c589b249 | 348 | .aiocb_size = sizeof(IscsiAIOCB), |
722d9333 | 349 | .cancel_async = iscsi_aio_cancel, |
c589b249 RS |
350 | }; |
351 | ||
352 | ||
353 | static void iscsi_process_read(void *arg); | |
354 | static void iscsi_process_write(void *arg); | |
355 | ||
d045c466 | 356 | /* Called with QemuMutex held. */ |
c589b249 RS |
357 | static void |
358 | iscsi_set_events(IscsiLun *iscsilun) | |
359 | { | |
360 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
05b685fb | 361 | int ev = iscsi_which_events(iscsi); |
c9b9f682 | 362 | |
c9b9f682 | 363 | if (ev != iscsilun->events) { |
dca21ef2 FZ |
364 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), |
365 | false, | |
05b685fb | 366 | (ev & POLLIN) ? iscsi_process_read : NULL, |
80cf6257 | 367 | (ev & POLLOUT) ? iscsi_process_write : NULL, |
f6a51c84 | 368 | NULL, |
80cf6257 | 369 | iscsilun); |
05b685fb PL |
370 | iscsilun->events = ev; |
371 | } | |
05b685fb | 372 | } |
c9b9f682 | 373 | |
5dd7a535 | 374 | static void iscsi_timed_check_events(void *opaque) |
05b685fb PL |
375 | { |
376 | IscsiLun *iscsilun = opaque; | |
5dd7a535 PL |
377 | |
378 | /* check for timed out requests */ | |
379 | iscsi_service(iscsilun->iscsi, 0); | |
380 | ||
381 | if (iscsilun->request_timed_out) { | |
382 | iscsilun->request_timed_out = false; | |
383 | iscsi_reconnect(iscsilun->iscsi); | |
384 | } | |
385 | ||
386 | /* newer versions of libiscsi may return zero events. Ensure we are able | |
387 | * to return to service once this situation changes. */ | |
05b685fb | 388 | iscsi_set_events(iscsilun); |
5dd7a535 PL |
389 | |
390 | timer_mod(iscsilun->event_timer, | |
391 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); | |
c589b249 RS |
392 | } |
393 | ||
394 | static void | |
395 | iscsi_process_read(void *arg) | |
396 | { | |
397 | IscsiLun *iscsilun = arg; | |
398 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
399 | ||
d045c466 | 400 | qemu_mutex_lock(&iscsilun->mutex); |
c589b249 RS |
401 | iscsi_service(iscsi, POLLIN); |
402 | iscsi_set_events(iscsilun); | |
d045c466 | 403 | qemu_mutex_unlock(&iscsilun->mutex); |
c589b249 RS |
404 | } |
405 | ||
406 | static void | |
407 | iscsi_process_write(void *arg) | |
408 | { | |
409 | IscsiLun *iscsilun = arg; | |
410 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
411 | ||
d045c466 | 412 | qemu_mutex_lock(&iscsilun->mutex); |
c589b249 RS |
413 | iscsi_service(iscsi, POLLOUT); |
414 | iscsi_set_events(iscsilun); | |
d045c466 | 415 | qemu_mutex_unlock(&iscsilun->mutex); |
c589b249 RS |
416 | } |
417 | ||
0777b5dd PL |
418 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
419 | { | |
420 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; | |
421 | } | |
422 | ||
c589b249 RS |
423 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) |
424 | { | |
425 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
426 | } | |
427 | ||
94d047a3 EB |
428 | static bool is_byte_request_lun_aligned(int64_t offset, int count, |
429 | IscsiLun *iscsilun) | |
91bea4e2 | 430 | { |
94d047a3 EB |
431 | if (offset % iscsilun->block_size || count % iscsilun->block_size) { |
432 | error_report("iSCSI misaligned request: " | |
433 | "iscsilun->block_size %u, offset %" PRIi64 | |
434 | ", count %d", | |
435 | iscsilun->block_size, offset, count); | |
436 | return false; | |
437 | } | |
438 | return true; | |
439 | } | |
440 | ||
441 | static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors, | |
442 | IscsiLun *iscsilun) | |
443 | { | |
0ead9312 | 444 | assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS); |
94d047a3 EB |
445 | return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS, |
446 | nb_sectors << BDRV_SECTOR_BITS, | |
447 | iscsilun); | |
91bea4e2 PL |
448 | } |
449 | ||
e1123a3b | 450 | static void iscsi_allocmap_free(IscsiLun *iscsilun) |
a9fe4c95 | 451 | { |
e1123a3b PL |
452 | g_free(iscsilun->allocmap); |
453 | g_free(iscsilun->allocmap_valid); | |
454 | iscsilun->allocmap = NULL; | |
455 | iscsilun->allocmap_valid = NULL; | |
a9fe4c95 PL |
456 | } |
457 | ||
e1123a3b PL |
458 | |
459 | static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags) | |
b03c3805 | 460 | { |
e1123a3b PL |
461 | iscsi_allocmap_free(iscsilun); |
462 | ||
463 | iscsilun->allocmap_size = | |
464 | DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, iscsilun), | |
465 | iscsilun->cluster_sectors); | |
466 | ||
467 | iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size); | |
468 | if (!iscsilun->allocmap) { | |
469 | return -ENOMEM; | |
b03c3805 | 470 | } |
e1123a3b PL |
471 | |
472 | if (open_flags & BDRV_O_NOCACHE) { | |
473 | /* in case that cache.direct = on all allocmap entries are | |
474 | * treated as invalid to force a relookup of the block | |
475 | * status on every read request */ | |
476 | return 0; | |
477 | } | |
478 | ||
479 | iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size); | |
480 | if (!iscsilun->allocmap_valid) { | |
481 | /* if we are under memory pressure free the allocmap as well */ | |
482 | iscsi_allocmap_free(iscsilun); | |
483 | return -ENOMEM; | |
484 | } | |
485 | ||
486 | return 0; | |
b03c3805 PL |
487 | } |
488 | ||
e1123a3b PL |
489 | static void |
490 | iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num, | |
491 | int nb_sectors, bool allocated, bool valid) | |
b03c3805 | 492 | { |
e1123a3b PL |
493 | int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk; |
494 | ||
495 | if (iscsilun->allocmap == NULL) { | |
b03c3805 PL |
496 | return; |
497 | } | |
e1123a3b PL |
498 | /* expand to entirely contain all affected clusters */ |
499 | cl_num_expanded = sector_num / iscsilun->cluster_sectors; | |
500 | nb_cls_expanded = DIV_ROUND_UP(sector_num + nb_sectors, | |
501 | iscsilun->cluster_sectors) - cl_num_expanded; | |
502 | /* shrink to touch only completely contained clusters */ | |
503 | cl_num_shrunk = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors); | |
504 | nb_cls_shrunk = (sector_num + nb_sectors) / iscsilun->cluster_sectors | |
505 | - cl_num_shrunk; | |
506 | if (allocated) { | |
507 | bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded); | |
508 | } else { | |
1da45e0c PL |
509 | if (nb_cls_shrunk > 0) { |
510 | bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk); | |
511 | } | |
e1123a3b PL |
512 | } |
513 | ||
514 | if (iscsilun->allocmap_valid == NULL) { | |
515 | return; | |
516 | } | |
517 | if (valid) { | |
1da45e0c PL |
518 | if (nb_cls_shrunk > 0) { |
519 | bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk); | |
520 | } | |
e1123a3b PL |
521 | } else { |
522 | bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded, | |
523 | nb_cls_expanded); | |
524 | } | |
525 | } | |
526 | ||
527 | static void | |
528 | iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t sector_num, | |
529 | int nb_sectors) | |
530 | { | |
531 | iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, true, true); | |
532 | } | |
533 | ||
534 | static void | |
535 | iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t sector_num, | |
536 | int nb_sectors) | |
537 | { | |
538 | /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update | |
539 | * is ignored, so this will in effect be an iscsi_allocmap_set_invalid. | |
540 | */ | |
541 | iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, true); | |
542 | } | |
543 | ||
544 | static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t sector_num, | |
545 | int nb_sectors) | |
546 | { | |
547 | iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, false); | |
548 | } | |
549 | ||
550 | static void iscsi_allocmap_invalidate(IscsiLun *iscsilun) | |
551 | { | |
552 | if (iscsilun->allocmap) { | |
553 | bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size); | |
554 | } | |
555 | if (iscsilun->allocmap_valid) { | |
556 | bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size); | |
557 | } | |
558 | } | |
559 | ||
560 | static inline bool | |
561 | iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t sector_num, | |
562 | int nb_sectors) | |
563 | { | |
564 | unsigned long size; | |
565 | if (iscsilun->allocmap == NULL) { | |
566 | return true; | |
b03c3805 | 567 | } |
e1123a3b PL |
568 | size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); |
569 | return !(find_next_bit(iscsilun->allocmap, size, | |
570 | sector_num / iscsilun->cluster_sectors) == size); | |
571 | } | |
572 | ||
573 | static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun, | |
574 | int64_t sector_num, int nb_sectors) | |
575 | { | |
576 | unsigned long size; | |
577 | if (iscsilun->allocmap_valid == NULL) { | |
578 | return false; | |
579 | } | |
580 | size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); | |
581 | return (find_next_zero_bit(iscsilun->allocmap_valid, size, | |
582 | sector_num / iscsilun->cluster_sectors) == size); | |
b03c3805 PL |
583 | } |
584 | ||
9f0eb9e1 KW |
585 | static int coroutine_fn |
586 | iscsi_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
587 | QEMUIOVector *iov, int flags) | |
c589b249 | 588 | { |
063c3378 PL |
589 | IscsiLun *iscsilun = bs->opaque; |
590 | struct IscsiTask iTask; | |
f4dfa67f | 591 | uint64_t lba; |
063c3378 | 592 | uint32_t num_sectors; |
4df863f3 | 593 | bool fua = flags & BDRV_REQ_FUA; |
d045c466 | 594 | int r = 0; |
c589b249 | 595 | |
4df863f3 EB |
596 | if (fua) { |
597 | assert(iscsilun->dpofua); | |
598 | } | |
94d047a3 | 599 | if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
063c3378 PL |
600 | return -EINVAL; |
601 | } | |
7371d56f | 602 | |
6bd01f14 EB |
603 | if (bs->bl.max_transfer) { |
604 | assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); | |
dc9e7163 PL |
605 | } |
606 | ||
063c3378 PL |
607 | lba = sector_qemu2lun(sector_num, iscsilun); |
608 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
063c3378 | 609 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
d045c466 | 610 | qemu_mutex_lock(&iscsilun->mutex); |
063c3378 | 611 | retry: |
9281fe9e | 612 | if (iscsilun->use_16_for_rw) { |
583ec22e RS |
613 | #if LIBISCSI_API_VERSION >= (20160603) |
614 | iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, | |
615 | NULL, num_sectors * iscsilun->block_size, | |
616 | iscsilun->block_size, 0, 0, fua, 0, 0, | |
617 | iscsi_co_generic_cb, &iTask, | |
618 | (struct scsi_iovec *)iov->iov, iov->niov); | |
619 | } else { | |
620 | iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, | |
621 | NULL, num_sectors * iscsilun->block_size, | |
622 | iscsilun->block_size, 0, 0, fua, 0, 0, | |
623 | iscsi_co_generic_cb, &iTask, | |
624 | (struct scsi_iovec *)iov->iov, iov->niov); | |
625 | } | |
626 | #else | |
9281fe9e | 627 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, |
8c215a9f | 628 | NULL, num_sectors * iscsilun->block_size, |
73b5394e | 629 | iscsilun->block_size, 0, 0, fua, 0, 0, |
9281fe9e PL |
630 | iscsi_co_generic_cb, &iTask); |
631 | } else { | |
632 | iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
8c215a9f | 633 | NULL, num_sectors * iscsilun->block_size, |
73b5394e | 634 | iscsilun->block_size, 0, 0, fua, 0, 0, |
9281fe9e PL |
635 | iscsi_co_generic_cb, &iTask); |
636 | } | |
583ec22e | 637 | #endif |
063c3378 | 638 | if (iTask.task == NULL) { |
f6eb0b31 | 639 | qemu_mutex_unlock(&iscsilun->mutex); |
92397116 | 640 | return -ENOMEM; |
f4dfa67f | 641 | } |
583ec22e | 642 | #if LIBISCSI_API_VERSION < (20160603) |
063c3378 PL |
643 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, |
644 | iov->niov); | |
583ec22e | 645 | #endif |
063c3378 PL |
646 | while (!iTask.complete) { |
647 | iscsi_set_events(iscsilun); | |
d045c466 | 648 | qemu_mutex_unlock(&iscsilun->mutex); |
063c3378 | 649 | qemu_coroutine_yield(); |
d045c466 | 650 | qemu_mutex_lock(&iscsilun->mutex); |
c589b249 RS |
651 | } |
652 | ||
063c3378 PL |
653 | if (iTask.task != NULL) { |
654 | scsi_free_scsi_task(iTask.task); | |
655 | iTask.task = NULL; | |
91bea4e2 PL |
656 | } |
657 | ||
063c3378 | 658 | if (iTask.do_retry) { |
837c3901 | 659 | iTask.complete = 0; |
063c3378 | 660 | goto retry; |
1dde716e PL |
661 | } |
662 | ||
063c3378 | 663 | if (iTask.status != SCSI_STATUS_GOOD) { |
e1123a3b | 664 | iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors); |
d045c466 PB |
665 | r = iTask.err_code; |
666 | goto out_unlock; | |
c589b249 RS |
667 | } |
668 | ||
e1123a3b | 669 | iscsi_allocmap_set_allocated(iscsilun, sector_num, nb_sectors); |
b03c3805 | 670 | |
d045c466 PB |
671 | out_unlock: |
672 | qemu_mutex_unlock(&iscsilun->mutex); | |
673 | return r; | |
c589b249 RS |
674 | } |
675 | ||
b03c3805 | 676 | |
b03c3805 | 677 | |
b03c3805 PL |
678 | static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, |
679 | int64_t sector_num, | |
67a0fd2a FZ |
680 | int nb_sectors, int *pnum, |
681 | BlockDriverState **file) | |
b03c3805 PL |
682 | { |
683 | IscsiLun *iscsilun = bs->opaque; | |
684 | struct scsi_get_lba_status *lbas = NULL; | |
685 | struct scsi_lba_status_descriptor *lbasd = NULL; | |
686 | struct IscsiTask iTask; | |
687 | int64_t ret; | |
688 | ||
689 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
690 | ||
94d047a3 | 691 | if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
b03c3805 PL |
692 | ret = -EINVAL; |
693 | goto out; | |
694 | } | |
695 | ||
696 | /* default to all sectors allocated */ | |
697 | ret = BDRV_BLOCK_DATA; | |
698 | ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; | |
699 | *pnum = nb_sectors; | |
700 | ||
701 | /* LUN does not support logical block provisioning */ | |
0a386e48 | 702 | if (!iscsilun->lbpme) { |
b03c3805 PL |
703 | goto out; |
704 | } | |
705 | ||
d045c466 | 706 | qemu_mutex_lock(&iscsilun->mutex); |
b03c3805 PL |
707 | retry: |
708 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, | |
709 | sector_qemu2lun(sector_num, iscsilun), | |
710 | 8 + 16, iscsi_co_generic_cb, | |
711 | &iTask) == NULL) { | |
712 | ret = -ENOMEM; | |
d045c466 | 713 | goto out_unlock; |
b03c3805 PL |
714 | } |
715 | ||
716 | while (!iTask.complete) { | |
717 | iscsi_set_events(iscsilun); | |
d045c466 | 718 | qemu_mutex_unlock(&iscsilun->mutex); |
b03c3805 | 719 | qemu_coroutine_yield(); |
d045c466 | 720 | qemu_mutex_lock(&iscsilun->mutex); |
b03c3805 PL |
721 | } |
722 | ||
723 | if (iTask.do_retry) { | |
724 | if (iTask.task != NULL) { | |
725 | scsi_free_scsi_task(iTask.task); | |
726 | iTask.task = NULL; | |
727 | } | |
728 | iTask.complete = 0; | |
729 | goto retry; | |
730 | } | |
731 | ||
732 | if (iTask.status != SCSI_STATUS_GOOD) { | |
733 | /* in case the get_lba_status_callout fails (i.e. | |
734 | * because the device is busy or the cmd is not | |
735 | * supported) we pretend all blocks are allocated | |
736 | * for backwards compatibility */ | |
d045c466 | 737 | goto out_unlock; |
b03c3805 PL |
738 | } |
739 | ||
740 | lbas = scsi_datain_unmarshall(iTask.task); | |
741 | if (lbas == NULL) { | |
742 | ret = -EIO; | |
d045c466 | 743 | goto out_unlock; |
b03c3805 PL |
744 | } |
745 | ||
746 | lbasd = &lbas->descriptors[0]; | |
747 | ||
748 | if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { | |
749 | ret = -EIO; | |
d045c466 | 750 | goto out_unlock; |
b03c3805 PL |
751 | } |
752 | ||
753 | *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); | |
754 | ||
755 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || | |
756 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { | |
757 | ret &= ~BDRV_BLOCK_DATA; | |
758 | if (iscsilun->lbprz) { | |
759 | ret |= BDRV_BLOCK_ZERO; | |
760 | } | |
761 | } | |
762 | ||
763 | if (ret & BDRV_BLOCK_ZERO) { | |
e1123a3b | 764 | iscsi_allocmap_set_unallocated(iscsilun, sector_num, *pnum); |
b03c3805 | 765 | } else { |
e1123a3b | 766 | iscsi_allocmap_set_allocated(iscsilun, sector_num, *pnum); |
b03c3805 PL |
767 | } |
768 | ||
769 | if (*pnum > nb_sectors) { | |
770 | *pnum = nb_sectors; | |
771 | } | |
d045c466 PB |
772 | out_unlock: |
773 | qemu_mutex_unlock(&iscsilun->mutex); | |
b03c3805 PL |
774 | out: |
775 | if (iTask.task != NULL) { | |
776 | scsi_free_scsi_task(iTask.task); | |
777 | } | |
3399833f FZ |
778 | if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) { |
779 | *file = bs; | |
780 | } | |
b03c3805 PL |
781 | return ret; |
782 | } | |
783 | ||
063c3378 PL |
784 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, |
785 | int64_t sector_num, int nb_sectors, | |
786 | QEMUIOVector *iov) | |
c589b249 | 787 | { |
063c3378 PL |
788 | IscsiLun *iscsilun = bs->opaque; |
789 | struct IscsiTask iTask; | |
1dde716e PL |
790 | uint64_t lba; |
791 | uint32_t num_sectors; | |
c589b249 | 792 | |
94d047a3 | 793 | if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
063c3378 | 794 | return -EINVAL; |
f4dfa67f | 795 | } |
f4dfa67f | 796 | |
6bd01f14 EB |
797 | if (bs->bl.max_transfer) { |
798 | assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); | |
dc9e7163 PL |
799 | } |
800 | ||
e1123a3b PL |
801 | /* if cache.direct is off and we have a valid entry in our allocation map |
802 | * we can skip checking the block status and directly return zeroes if | |
803 | * the request falls within an unallocated area */ | |
804 | if (iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) && | |
805 | !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) { | |
806 | qemu_iovec_memset(iov, 0, 0x00, iov->size); | |
807 | return 0; | |
808 | } | |
809 | ||
810 | if (nb_sectors >= ISCSI_CHECKALLOC_THRES && | |
811 | !iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) && | |
812 | !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) { | |
b03c3805 | 813 | int pnum; |
67a0fd2a | 814 | BlockDriverState *file; |
e1123a3b PL |
815 | /* check the block status from the beginning of the cluster |
816 | * containing the start sector */ | |
817 | int64_t ret = iscsi_co_get_block_status(bs, | |
818 | sector_num - sector_num % iscsilun->cluster_sectors, | |
819 | BDRV_REQUEST_MAX_SECTORS, &pnum, &file); | |
b03c3805 PL |
820 | if (ret < 0) { |
821 | return ret; | |
822 | } | |
e1123a3b PL |
823 | /* if the whole request falls into an unallocated area we can avoid |
824 | * to read and directly return zeroes instead */ | |
825 | if (ret & BDRV_BLOCK_ZERO && | |
826 | pnum >= nb_sectors + sector_num % iscsilun->cluster_sectors) { | |
b03c3805 PL |
827 | qemu_iovec_memset(iov, 0, 0x00, iov->size); |
828 | return 0; | |
829 | } | |
830 | } | |
b03c3805 | 831 | |
063c3378 PL |
832 | lba = sector_qemu2lun(sector_num, iscsilun); |
833 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
f4dfa67f | 834 | |
063c3378 | 835 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
d045c466 | 836 | qemu_mutex_lock(&iscsilun->mutex); |
063c3378 | 837 | retry: |
9281fe9e | 838 | if (iscsilun->use_16_for_rw) { |
583ec22e RS |
839 | #if LIBISCSI_API_VERSION >= (20160603) |
840 | iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, | |
841 | num_sectors * iscsilun->block_size, | |
842 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
843 | iscsi_co_generic_cb, &iTask, | |
844 | (struct scsi_iovec *)iov->iov, iov->niov); | |
845 | } else { | |
846 | iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, | |
847 | num_sectors * iscsilun->block_size, | |
848 | iscsilun->block_size, | |
849 | 0, 0, 0, 0, 0, | |
850 | iscsi_co_generic_cb, &iTask, | |
851 | (struct scsi_iovec *)iov->iov, iov->niov); | |
852 | } | |
853 | #else | |
063c3378 PL |
854 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, |
855 | num_sectors * iscsilun->block_size, | |
856 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
857 | iscsi_co_generic_cb, &iTask); | |
9281fe9e | 858 | } else { |
063c3378 PL |
859 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, |
860 | num_sectors * iscsilun->block_size, | |
219c2521 | 861 | iscsilun->block_size, |
219c2521 | 862 | 0, 0, 0, 0, 0, |
063c3378 | 863 | iscsi_co_generic_cb, &iTask); |
f4dfa67f | 864 | } |
583ec22e | 865 | #endif |
063c3378 | 866 | if (iTask.task == NULL) { |
f6eb0b31 | 867 | qemu_mutex_unlock(&iscsilun->mutex); |
92397116 | 868 | return -ENOMEM; |
c589b249 | 869 | } |
583ec22e | 870 | #if LIBISCSI_API_VERSION < (20160603) |
063c3378 | 871 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); |
583ec22e | 872 | #endif |
063c3378 PL |
873 | while (!iTask.complete) { |
874 | iscsi_set_events(iscsilun); | |
d045c466 | 875 | qemu_mutex_unlock(&iscsilun->mutex); |
063c3378 | 876 | qemu_coroutine_yield(); |
d045c466 | 877 | qemu_mutex_lock(&iscsilun->mutex); |
1dde716e | 878 | } |
c589b249 | 879 | |
063c3378 PL |
880 | if (iTask.task != NULL) { |
881 | scsi_free_scsi_task(iTask.task); | |
882 | iTask.task = NULL; | |
c589b249 RS |
883 | } |
884 | ||
063c3378 | 885 | if (iTask.do_retry) { |
837c3901 | 886 | iTask.complete = 0; |
063c3378 | 887 | goto retry; |
c589b249 | 888 | } |
d045c466 | 889 | qemu_mutex_unlock(&iscsilun->mutex); |
c589b249 | 890 | |
063c3378 | 891 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 892 | return iTask.err_code; |
1dde716e PL |
893 | } |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
063c3378 | 898 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) |
1dde716e PL |
899 | { |
900 | IscsiLun *iscsilun = bs->opaque; | |
063c3378 | 901 | struct IscsiTask iTask; |
1dde716e | 902 | |
73b5394e | 903 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
d045c466 | 904 | qemu_mutex_lock(&iscsilun->mutex); |
063c3378 PL |
905 | retry: |
906 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, | |
907 | 0, iscsi_co_generic_cb, &iTask) == NULL) { | |
f6eb0b31 | 908 | qemu_mutex_unlock(&iscsilun->mutex); |
92397116 | 909 | return -ENOMEM; |
063c3378 | 910 | } |
1dde716e | 911 | |
063c3378 PL |
912 | while (!iTask.complete) { |
913 | iscsi_set_events(iscsilun); | |
d045c466 | 914 | qemu_mutex_unlock(&iscsilun->mutex); |
063c3378 | 915 | qemu_coroutine_yield(); |
d045c466 | 916 | qemu_mutex_lock(&iscsilun->mutex); |
063c3378 | 917 | } |
1dde716e | 918 | |
063c3378 PL |
919 | if (iTask.task != NULL) { |
920 | scsi_free_scsi_task(iTask.task); | |
921 | iTask.task = NULL; | |
c589b249 RS |
922 | } |
923 | ||
063c3378 | 924 | if (iTask.do_retry) { |
837c3901 | 925 | iTask.complete = 0; |
063c3378 PL |
926 | goto retry; |
927 | } | |
d045c466 | 928 | qemu_mutex_unlock(&iscsilun->mutex); |
c589b249 | 929 | |
063c3378 | 930 | if (iTask.status != SCSI_STATUS_GOOD) { |
e01dd3da | 931 | return iTask.err_code; |
063c3378 PL |
932 | } |
933 | ||
934 | return 0; | |
c589b249 RS |
935 | } |
936 | ||
98392453 | 937 | #ifdef __linux__ |
d045c466 | 938 | /* Called (via iscsi_service) with QemuMutex held. */ |
98392453 RS |
939 | static void |
940 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, | |
941 | void *command_data, void *opaque) | |
942 | { | |
943 | IscsiAIOCB *acb = opaque; | |
944 | ||
0a53f010 RS |
945 | g_free(acb->buf); |
946 | acb->buf = NULL; | |
947 | ||
98392453 RS |
948 | acb->status = 0; |
949 | if (status < 0) { | |
950 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", | |
951 | iscsi_get_error(iscsi)); | |
e01dd3da | 952 | acb->status = iscsi_translate_sense(&acb->task->sense); |
98392453 RS |
953 | } |
954 | ||
955 | acb->ioh->driver_status = 0; | |
956 | acb->ioh->host_status = 0; | |
957 | acb->ioh->resid = 0; | |
644c6869 | 958 | acb->ioh->status = status; |
98392453 RS |
959 | |
960 | #define SG_ERR_DRIVER_SENSE 0x08 | |
961 | ||
962 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { | |
963 | int ss; | |
964 | ||
965 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; | |
966 | ||
967 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; | |
968 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? | |
969 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; | |
970 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); | |
971 | } | |
972 | ||
cfb3f506 | 973 | iscsi_schedule_bh(acb); |
98392453 RS |
974 | } |
975 | ||
4bb17ab5 FZ |
976 | static void iscsi_ioctl_bh_completion(void *opaque) |
977 | { | |
978 | IscsiAIOCB *acb = opaque; | |
979 | ||
980 | qemu_bh_delete(acb->bh); | |
981 | acb->common.cb(acb->common.opaque, acb->ret); | |
982 | qemu_aio_unref(acb); | |
983 | } | |
984 | ||
985 | static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) | |
986 | { | |
987 | BlockDriverState *bs = acb->common.bs; | |
988 | IscsiLun *iscsilun = bs->opaque; | |
989 | int ret = 0; | |
990 | ||
991 | switch (req) { | |
992 | case SG_GET_VERSION_NUM: | |
993 | *(int *)buf = 30000; | |
994 | break; | |
995 | case SG_GET_SCSI_ID: | |
996 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; | |
997 | break; | |
998 | default: | |
999 | ret = -EINVAL; | |
1000 | } | |
1001 | assert(!acb->bh); | |
1002 | acb->bh = aio_bh_new(bdrv_get_aio_context(bs), | |
1003 | iscsi_ioctl_bh_completion, acb); | |
1004 | acb->ret = ret; | |
1005 | qemu_bh_schedule(acb->bh); | |
1006 | } | |
1007 | ||
7c84b1b8 | 1008 | static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, |
98392453 | 1009 | unsigned long int req, void *buf, |
097310b5 | 1010 | BlockCompletionFunc *cb, void *opaque) |
98392453 RS |
1011 | { |
1012 | IscsiLun *iscsilun = bs->opaque; | |
1013 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1014 | struct iscsi_data data; | |
1015 | IscsiAIOCB *acb; | |
1016 | ||
d7331bed | 1017 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); |
98392453 RS |
1018 | |
1019 | acb->iscsilun = iscsilun; | |
1bd075f2 PB |
1020 | acb->bh = NULL; |
1021 | acb->status = -EINPROGRESS; | |
98392453 RS |
1022 | acb->buf = NULL; |
1023 | acb->ioh = buf; | |
1024 | ||
4bb17ab5 FZ |
1025 | if (req != SG_IO) { |
1026 | iscsi_ioctl_handle_emulated(acb, req, buf); | |
1027 | return &acb->common; | |
1028 | } | |
1029 | ||
a6b3167f PL |
1030 | if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) { |
1031 | error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)", | |
1032 | acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE); | |
1033 | qemu_aio_unref(acb); | |
1034 | return NULL; | |
1035 | } | |
1036 | ||
98392453 RS |
1037 | acb->task = malloc(sizeof(struct scsi_task)); |
1038 | if (acb->task == NULL) { | |
1039 | error_report("iSCSI: Failed to allocate task for scsi command. %s", | |
1040 | iscsi_get_error(iscsi)); | |
8007429a | 1041 | qemu_aio_unref(acb); |
98392453 RS |
1042 | return NULL; |
1043 | } | |
1044 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
1045 | ||
1046 | switch (acb->ioh->dxfer_direction) { | |
1047 | case SG_DXFER_TO_DEV: | |
1048 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
1049 | break; | |
1050 | case SG_DXFER_FROM_DEV: | |
1051 | acb->task->xfer_dir = SCSI_XFER_READ; | |
1052 | break; | |
1053 | default: | |
1054 | acb->task->xfer_dir = SCSI_XFER_NONE; | |
1055 | break; | |
1056 | } | |
1057 | ||
1058 | acb->task->cdb_size = acb->ioh->cmd_len; | |
1059 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); | |
1060 | acb->task->expxferlen = acb->ioh->dxfer_len; | |
1061 | ||
0a53f010 | 1062 | data.size = 0; |
d045c466 | 1063 | qemu_mutex_lock(&iscsilun->mutex); |
98392453 | 1064 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { |
0a53f010 RS |
1065 | if (acb->ioh->iovec_count == 0) { |
1066 | data.data = acb->ioh->dxferp; | |
1067 | data.size = acb->ioh->dxfer_len; | |
1068 | } else { | |
0a53f010 RS |
1069 | scsi_task_set_iov_out(acb->task, |
1070 | (struct scsi_iovec *) acb->ioh->dxferp, | |
1071 | acb->ioh->iovec_count); | |
0a53f010 | 1072 | } |
98392453 | 1073 | } |
0a53f010 | 1074 | |
98392453 RS |
1075 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, |
1076 | iscsi_aio_ioctl_cb, | |
0a53f010 | 1077 | (data.size > 0) ? &data : NULL, |
98392453 | 1078 | acb) != 0) { |
d045c466 | 1079 | qemu_mutex_unlock(&iscsilun->mutex); |
98392453 | 1080 | scsi_free_scsi_task(acb->task); |
8007429a | 1081 | qemu_aio_unref(acb); |
98392453 RS |
1082 | return NULL; |
1083 | } | |
1084 | ||
1085 | /* tell libiscsi to read straight into the buffer we got from ioctl */ | |
1086 | if (acb->task->xfer_dir == SCSI_XFER_READ) { | |
0a53f010 RS |
1087 | if (acb->ioh->iovec_count == 0) { |
1088 | scsi_task_add_data_in_buffer(acb->task, | |
1089 | acb->ioh->dxfer_len, | |
1090 | acb->ioh->dxferp); | |
1091 | } else { | |
0a53f010 RS |
1092 | scsi_task_set_iov_in(acb->task, |
1093 | (struct scsi_iovec *) acb->ioh->dxferp, | |
1094 | acb->ioh->iovec_count); | |
0a53f010 | 1095 | } |
98392453 RS |
1096 | } |
1097 | ||
1098 | iscsi_set_events(iscsilun); | |
d045c466 | 1099 | qemu_mutex_unlock(&iscsilun->mutex); |
98392453 RS |
1100 | |
1101 | return &acb->common; | |
1102 | } | |
1103 | ||
98392453 RS |
1104 | #endif |
1105 | ||
c589b249 RS |
1106 | static int64_t |
1107 | iscsi_getlength(BlockDriverState *bs) | |
1108 | { | |
1109 | IscsiLun *iscsilun = bs->opaque; | |
1110 | int64_t len; | |
1111 | ||
1112 | len = iscsilun->num_blocks; | |
1113 | len *= iscsilun->block_size; | |
1114 | ||
1115 | return len; | |
1116 | } | |
1117 | ||
65f3e339 | 1118 | static int |
97c7e85c | 1119 | coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int count) |
65f3e339 PL |
1120 | { |
1121 | IscsiLun *iscsilun = bs->opaque; | |
1122 | struct IscsiTask iTask; | |
1123 | struct unmap_list list; | |
d045c466 | 1124 | int r = 0; |
65f3e339 | 1125 | |
49228d1e EB |
1126 | if (!is_byte_request_lun_aligned(offset, count, iscsilun)) { |
1127 | return -ENOTSUP; | |
1128 | } | |
65f3e339 PL |
1129 | |
1130 | if (!iscsilun->lbp.lbpu) { | |
1131 | /* UNMAP is not supported by the target */ | |
1132 | return 0; | |
1133 | } | |
1134 | ||
97c7e85c EB |
1135 | list.lba = offset / iscsilun->block_size; |
1136 | list.num = count / iscsilun->block_size; | |
65f3e339 | 1137 | |
01a6a238 | 1138 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
d045c466 | 1139 | qemu_mutex_lock(&iscsilun->mutex); |
65f3e339 | 1140 | retry: |
01a6a238 | 1141 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, |
97c7e85c | 1142 | iscsi_co_generic_cb, &iTask) == NULL) { |
d045c466 PB |
1143 | r = -ENOMEM; |
1144 | goto out_unlock; | |
01a6a238 | 1145 | } |
65f3e339 | 1146 | |
01a6a238 PL |
1147 | while (!iTask.complete) { |
1148 | iscsi_set_events(iscsilun); | |
d045c466 | 1149 | qemu_mutex_unlock(&iscsilun->mutex); |
01a6a238 | 1150 | qemu_coroutine_yield(); |
d045c466 | 1151 | qemu_mutex_lock(&iscsilun->mutex); |
01a6a238 | 1152 | } |
65f3e339 | 1153 | |
01a6a238 PL |
1154 | if (iTask.task != NULL) { |
1155 | scsi_free_scsi_task(iTask.task); | |
1156 | iTask.task = NULL; | |
1157 | } | |
65f3e339 | 1158 | |
01a6a238 | 1159 | if (iTask.do_retry) { |
837c3901 | 1160 | iTask.complete = 0; |
01a6a238 PL |
1161 | goto retry; |
1162 | } | |
65f3e339 | 1163 | |
01a6a238 PL |
1164 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { |
1165 | /* the target might fail with a check condition if it | |
1166 | is not happy with the alignment of the UNMAP request | |
1167 | we silently fail in this case */ | |
d045c466 | 1168 | goto out_unlock; |
01a6a238 | 1169 | } |
65f3e339 | 1170 | |
01a6a238 | 1171 | if (iTask.status != SCSI_STATUS_GOOD) { |
d045c466 PB |
1172 | r = iTask.err_code; |
1173 | goto out_unlock; | |
65f3e339 PL |
1174 | } |
1175 | ||
97c7e85c EB |
1176 | iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS, |
1177 | count >> BDRV_SECTOR_BITS); | |
b03c3805 | 1178 | |
d045c466 PB |
1179 | out_unlock: |
1180 | qemu_mutex_unlock(&iscsilun->mutex); | |
1181 | return r; | |
65f3e339 PL |
1182 | } |
1183 | ||
d4cd9615 | 1184 | static int |
94d047a3 EB |
1185 | coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, |
1186 | int count, BdrvRequestFlags flags) | |
d4cd9615 PL |
1187 | { |
1188 | IscsiLun *iscsilun = bs->opaque; | |
1189 | struct IscsiTask iTask; | |
1190 | uint64_t lba; | |
1191 | uint32_t nb_blocks; | |
9281fe9e | 1192 | bool use_16_for_ws = iscsilun->use_16_for_rw; |
d045c466 | 1193 | int r = 0; |
d4cd9615 | 1194 | |
94d047a3 EB |
1195 | if (!is_byte_request_lun_aligned(offset, count, iscsilun)) { |
1196 | return -ENOTSUP; | |
d4cd9615 PL |
1197 | } |
1198 | ||
9281fe9e PL |
1199 | if (flags & BDRV_REQ_MAY_UNMAP) { |
1200 | if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { | |
1201 | /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ | |
1202 | use_16_for_ws = true; | |
1203 | } | |
1204 | if (use_16_for_ws && !iscsilun->lbp.lbpws) { | |
1205 | /* WRITESAME16 with UNMAP is not supported by the target, | |
1206 | * fall back and try WRITESAME10/16 without UNMAP */ | |
1207 | flags &= ~BDRV_REQ_MAY_UNMAP; | |
1208 | use_16_for_ws = iscsilun->use_16_for_rw; | |
1209 | } | |
fa6252b0 PB |
1210 | } |
1211 | ||
dbe5c58f | 1212 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { |
9281fe9e | 1213 | /* WRITESAME without UNMAP is not supported by the target */ |
d4cd9615 PL |
1214 | return -ENOTSUP; |
1215 | } | |
1216 | ||
94d047a3 EB |
1217 | lba = offset / iscsilun->block_size; |
1218 | nb_blocks = count / iscsilun->block_size; | |
d4cd9615 PL |
1219 | |
1220 | if (iscsilun->zeroblock == NULL) { | |
4d5a3f88 KW |
1221 | iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); |
1222 | if (iscsilun->zeroblock == NULL) { | |
1223 | return -ENOMEM; | |
1224 | } | |
d4cd9615 PL |
1225 | } |
1226 | ||
d045c466 | 1227 | qemu_mutex_lock(&iscsilun->mutex); |
d4cd9615 PL |
1228 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
1229 | retry: | |
9281fe9e PL |
1230 | if (use_16_for_ws) { |
1231 | iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
1232 | iscsilun->zeroblock, iscsilun->block_size, | |
1233 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
1234 | 0, 0, iscsi_co_generic_cb, &iTask); | |
1235 | } else { | |
1236 | iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
1237 | iscsilun->zeroblock, iscsilun->block_size, | |
1238 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
1239 | 0, 0, iscsi_co_generic_cb, &iTask); | |
1240 | } | |
1241 | if (iTask.task == NULL) { | |
f6eb0b31 | 1242 | qemu_mutex_unlock(&iscsilun->mutex); |
92397116 | 1243 | return -ENOMEM; |
d4cd9615 PL |
1244 | } |
1245 | ||
1246 | while (!iTask.complete) { | |
1247 | iscsi_set_events(iscsilun); | |
d045c466 | 1248 | qemu_mutex_unlock(&iscsilun->mutex); |
d4cd9615 | 1249 | qemu_coroutine_yield(); |
d045c466 | 1250 | qemu_mutex_lock(&iscsilun->mutex); |
d4cd9615 PL |
1251 | } |
1252 | ||
d9738fd2 PL |
1253 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && |
1254 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && | |
27898a5d PB |
1255 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || |
1256 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { | |
d9738fd2 PL |
1257 | /* WRITE SAME is not supported by the target */ |
1258 | iscsilun->has_write_same = false; | |
1259 | scsi_free_scsi_task(iTask.task); | |
d045c466 PB |
1260 | r = -ENOTSUP; |
1261 | goto out_unlock; | |
d9738fd2 PL |
1262 | } |
1263 | ||
d4cd9615 PL |
1264 | if (iTask.task != NULL) { |
1265 | scsi_free_scsi_task(iTask.task); | |
1266 | iTask.task = NULL; | |
1267 | } | |
1268 | ||
1269 | if (iTask.do_retry) { | |
837c3901 | 1270 | iTask.complete = 0; |
d4cd9615 PL |
1271 | goto retry; |
1272 | } | |
1273 | ||
1274 | if (iTask.status != SCSI_STATUS_GOOD) { | |
e1123a3b PL |
1275 | iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS, |
1276 | count >> BDRV_SECTOR_BITS); | |
d045c466 PB |
1277 | r = iTask.err_code; |
1278 | goto out_unlock; | |
d4cd9615 PL |
1279 | } |
1280 | ||
b03c3805 | 1281 | if (flags & BDRV_REQ_MAY_UNMAP) { |
e1123a3b PL |
1282 | iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS, |
1283 | count >> BDRV_SECTOR_BITS); | |
b03c3805 | 1284 | } else { |
e1123a3b PL |
1285 | iscsi_allocmap_set_allocated(iscsilun, offset >> BDRV_SECTOR_BITS, |
1286 | count >> BDRV_SECTOR_BITS); | |
b03c3805 PL |
1287 | } |
1288 | ||
d045c466 PB |
1289 | out_unlock: |
1290 | qemu_mutex_unlock(&iscsilun->mutex); | |
1291 | return r; | |
d4cd9615 PL |
1292 | } |
1293 | ||
43171420 | 1294 | static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts, |
f2917853 | 1295 | Error **errp) |
f9dadc98 | 1296 | { |
f9dadc98 RS |
1297 | const char *user = NULL; |
1298 | const char *password = NULL; | |
b189346e DB |
1299 | const char *secretid; |
1300 | char *secret = NULL; | |
f9dadc98 | 1301 | |
f9dadc98 RS |
1302 | user = qemu_opt_get(opts, "user"); |
1303 | if (!user) { | |
f2917853 | 1304 | return; |
f9dadc98 RS |
1305 | } |
1306 | ||
b189346e | 1307 | secretid = qemu_opt_get(opts, "password-secret"); |
f9dadc98 | 1308 | password = qemu_opt_get(opts, "password"); |
b189346e DB |
1309 | if (secretid && password) { |
1310 | error_setg(errp, "'password' and 'password-secret' properties are " | |
1311 | "mutually exclusive"); | |
1312 | return; | |
1313 | } | |
1314 | if (secretid) { | |
1315 | secret = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
1316 | if (!secret) { | |
1317 | return; | |
1318 | } | |
1319 | password = secret; | |
1320 | } else if (!password) { | |
f2917853 PB |
1321 | error_setg(errp, "CHAP username specified but no password was given"); |
1322 | return; | |
f9dadc98 RS |
1323 | } |
1324 | ||
1325 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { | |
f2917853 | 1326 | error_setg(errp, "Failed to set initiator username and password"); |
f9dadc98 | 1327 | } |
b189346e DB |
1328 | |
1329 | g_free(secret); | |
f9dadc98 RS |
1330 | } |
1331 | ||
81aa2a0f | 1332 | static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts, |
f2917853 | 1333 | Error **errp) |
f9dadc98 | 1334 | { |
f9dadc98 RS |
1335 | const char *digest = NULL; |
1336 | ||
f9dadc98 RS |
1337 | digest = qemu_opt_get(opts, "header-digest"); |
1338 | if (!digest) { | |
81aa2a0f | 1339 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
31eb1202 | 1340 | } else if (!strcmp(digest, "crc32c")) { |
f9dadc98 | 1341 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); |
31eb1202 | 1342 | } else if (!strcmp(digest, "none")) { |
f9dadc98 | 1343 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); |
31eb1202 | 1344 | } else if (!strcmp(digest, "crc32c-none")) { |
f9dadc98 | 1345 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); |
31eb1202 | 1346 | } else if (!strcmp(digest, "none-crc32c")) { |
f9dadc98 RS |
1347 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
1348 | } else { | |
f2917853 | 1349 | error_setg(errp, "Invalid header-digest setting : %s", digest); |
f9dadc98 RS |
1350 | } |
1351 | } | |
1352 | ||
d4e79929 | 1353 | static char *get_initiator_name(QemuOpts *opts) |
f9dadc98 | 1354 | { |
5accc840 PB |
1355 | const char *name; |
1356 | char *iscsi_name; | |
1357 | UuidInfo *uuid_info; | |
f9dadc98 | 1358 | |
d4e79929 KW |
1359 | name = qemu_opt_get(opts, "initiator-name"); |
1360 | if (name) { | |
1361 | return g_strdup(name); | |
f9dadc98 RS |
1362 | } |
1363 | ||
5accc840 PB |
1364 | uuid_info = qmp_query_uuid(NULL); |
1365 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { | |
1366 | name = qemu_get_vm_name(); | |
f2ef4a6d | 1367 | } else { |
5accc840 | 1368 | name = uuid_info->UUID; |
f9dadc98 | 1369 | } |
5accc840 PB |
1370 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
1371 | name ? ":" : "", name ? name : ""); | |
1372 | qapi_free_UuidInfo(uuid_info); | |
1373 | return iscsi_name; | |
f9dadc98 RS |
1374 | } |
1375 | ||
5b5d34ec PL |
1376 | static void iscsi_nop_timed_event(void *opaque) |
1377 | { | |
1378 | IscsiLun *iscsilun = opaque; | |
1379 | ||
d045c466 | 1380 | qemu_mutex_lock(&iscsilun->mutex); |
5dd7a535 | 1381 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { |
5b5d34ec | 1382 | error_report("iSCSI: NOP timeout. Reconnecting..."); |
5dd7a535 PL |
1383 | iscsilun->request_timed_out = true; |
1384 | } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { | |
5b5d34ec | 1385 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); |
2f47da5f | 1386 | goto out; |
5b5d34ec PL |
1387 | } |
1388 | ||
bc72ad67 | 1389 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
5b5d34ec | 1390 | iscsi_set_events(iscsilun); |
2f47da5f PB |
1391 | |
1392 | out: | |
d045c466 | 1393 | qemu_mutex_unlock(&iscsilun->mutex); |
5b5d34ec | 1394 | } |
5b5d34ec | 1395 | |
f2917853 | 1396 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) |
cb1b83e7 PL |
1397 | { |
1398 | struct scsi_task *task = NULL; | |
1399 | struct scsi_readcapacity10 *rc10 = NULL; | |
1400 | struct scsi_readcapacity16 *rc16 = NULL; | |
cb1b83e7 PL |
1401 | int retries = ISCSI_CMD_RETRIES; |
1402 | ||
1288844e PB |
1403 | do { |
1404 | if (task != NULL) { | |
1405 | scsi_free_scsi_task(task); | |
1406 | task = NULL; | |
cb1b83e7 | 1407 | } |
1288844e PB |
1408 | |
1409 | switch (iscsilun->type) { | |
1410 | case TYPE_DISK: | |
1411 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); | |
1412 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1413 | rc16 = scsi_datain_unmarshall(task); | |
1414 | if (rc16 == NULL) { | |
f2917853 | 1415 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); |
1288844e PB |
1416 | } else { |
1417 | iscsilun->block_size = rc16->block_length; | |
1418 | iscsilun->num_blocks = rc16->returned_lba + 1; | |
0a386e48 PL |
1419 | iscsilun->lbpme = !!rc16->lbpme; |
1420 | iscsilun->lbprz = !!rc16->lbprz; | |
9281fe9e | 1421 | iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); |
1288844e | 1422 | } |
1cb6d137 | 1423 | break; |
1288844e | 1424 | } |
1cb6d137 ZL |
1425 | if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
1426 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
1427 | break; | |
1428 | } | |
1429 | /* Fall through and try READ CAPACITY(10) instead. */ | |
1288844e PB |
1430 | case TYPE_ROM: |
1431 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); | |
1432 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1433 | rc10 = scsi_datain_unmarshall(task); | |
1434 | if (rc10 == NULL) { | |
f2917853 | 1435 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); |
1288844e PB |
1436 | } else { |
1437 | iscsilun->block_size = rc10->block_size; | |
1438 | if (rc10->lba == 0) { | |
1439 | /* blank disk loaded */ | |
1440 | iscsilun->num_blocks = 0; | |
1441 | } else { | |
1442 | iscsilun->num_blocks = rc10->lba + 1; | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | break; | |
1447 | default: | |
f2917853 | 1448 | return; |
cb1b83e7 | 1449 | } |
1288844e PB |
1450 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
1451 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION | |
1452 | && retries-- > 0); | |
cb1b83e7 | 1453 | |
1288844e | 1454 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
240125bc | 1455 | error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); |
6d1f252d PL |
1456 | } else if (!iscsilun->block_size || |
1457 | iscsilun->block_size % BDRV_SECTOR_SIZE) { | |
1458 | error_setg(errp, "iSCSI: the target returned an invalid " | |
1459 | "block size of %d.", iscsilun->block_size); | |
1288844e | 1460 | } |
cb1b83e7 PL |
1461 | if (task) { |
1462 | scsi_free_scsi_task(task); | |
1463 | } | |
cb1b83e7 PL |
1464 | } |
1465 | ||
35cb1748 | 1466 | static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, |
24d3bd67 | 1467 | int evpd, int pc, void **inq, Error **errp) |
35cb1748 PB |
1468 | { |
1469 | int full_size; | |
1470 | struct scsi_task *task = NULL; | |
1471 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); | |
1472 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1473 | goto fail; | |
1474 | } | |
1475 | full_size = scsi_datain_getfullsize(task); | |
1476 | if (full_size > task->datain.size) { | |
1477 | scsi_free_scsi_task(task); | |
1478 | ||
1479 | /* we need more data for the full list */ | |
1480 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); | |
f18a7cbb PL |
1481 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
1482 | goto fail; | |
1483 | } | |
35cb1748 | 1484 | } |
f18a7cbb | 1485 | |
24d3bd67 PL |
1486 | *inq = scsi_datain_unmarshall(task); |
1487 | if (*inq == NULL) { | |
1488 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); | |
172fc4dd | 1489 | goto fail_with_err; |
24d3bd67 PL |
1490 | } |
1491 | ||
35cb1748 | 1492 | return task; |
f18a7cbb PL |
1493 | |
1494 | fail: | |
172fc4dd MA |
1495 | error_setg(errp, "iSCSI: Inquiry command failed : %s", |
1496 | iscsi_get_error(iscsi)); | |
1497 | fail_with_err: | |
24d3bd67 | 1498 | if (task != NULL) { |
35cb1748 | 1499 | scsi_free_scsi_task(task); |
35cb1748 PB |
1500 | } |
1501 | return NULL; | |
f18a7cbb PL |
1502 | } |
1503 | ||
80cf6257 SH |
1504 | static void iscsi_detach_aio_context(BlockDriverState *bs) |
1505 | { | |
1506 | IscsiLun *iscsilun = bs->opaque; | |
1507 | ||
dca21ef2 | 1508 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), |
f6a51c84 | 1509 | false, NULL, NULL, NULL, NULL); |
80cf6257 SH |
1510 | iscsilun->events = 0; |
1511 | ||
1512 | if (iscsilun->nop_timer) { | |
1513 | timer_del(iscsilun->nop_timer); | |
1514 | timer_free(iscsilun->nop_timer); | |
1515 | iscsilun->nop_timer = NULL; | |
1516 | } | |
05b685fb PL |
1517 | if (iscsilun->event_timer) { |
1518 | timer_del(iscsilun->event_timer); | |
1519 | timer_free(iscsilun->event_timer); | |
1520 | iscsilun->event_timer = NULL; | |
1521 | } | |
80cf6257 SH |
1522 | } |
1523 | ||
1524 | static void iscsi_attach_aio_context(BlockDriverState *bs, | |
1525 | AioContext *new_context) | |
1526 | { | |
1527 | IscsiLun *iscsilun = bs->opaque; | |
1528 | ||
1529 | iscsilun->aio_context = new_context; | |
1530 | iscsi_set_events(iscsilun); | |
1531 | ||
80cf6257 SH |
1532 | /* Set up a timer for sending out iSCSI NOPs */ |
1533 | iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, | |
1534 | QEMU_CLOCK_REALTIME, SCALE_MS, | |
1535 | iscsi_nop_timed_event, iscsilun); | |
1536 | timer_mod(iscsilun->nop_timer, | |
1537 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); | |
05b685fb | 1538 | |
5dd7a535 PL |
1539 | /* Set up a timer for periodic calls to iscsi_set_events and to |
1540 | * scan for command timeout */ | |
05b685fb PL |
1541 | iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, |
1542 | QEMU_CLOCK_REALTIME, SCALE_MS, | |
5dd7a535 PL |
1543 | iscsi_timed_check_events, iscsilun); |
1544 | timer_mod(iscsilun->event_timer, | |
1545 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); | |
80cf6257 SH |
1546 | } |
1547 | ||
7191f208 | 1548 | static void iscsi_modesense_sync(IscsiLun *iscsilun) |
c1d4096b FZ |
1549 | { |
1550 | struct scsi_task *task; | |
1551 | struct scsi_mode_sense *ms = NULL; | |
7191f208 | 1552 | iscsilun->write_protected = false; |
752ce451 | 1553 | iscsilun->dpofua = false; |
c1d4096b FZ |
1554 | |
1555 | task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, | |
1556 | 1, SCSI_MODESENSE_PC_CURRENT, | |
1557 | 0x3F, 0, 255); | |
1558 | if (task == NULL) { | |
1559 | error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", | |
1560 | iscsi_get_error(iscsilun->iscsi)); | |
1561 | goto out; | |
1562 | } | |
1563 | ||
1564 | if (task->status != SCSI_STATUS_GOOD) { | |
1565 | error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); | |
1566 | goto out; | |
1567 | } | |
1568 | ms = scsi_datain_unmarshall(task); | |
1569 | if (!ms) { | |
1570 | error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", | |
1571 | iscsi_get_error(iscsilun->iscsi)); | |
1572 | goto out; | |
1573 | } | |
7191f208 | 1574 | iscsilun->write_protected = ms->device_specific_parameter & 0x80; |
752ce451 | 1575 | iscsilun->dpofua = ms->device_specific_parameter & 0x10; |
c1d4096b FZ |
1576 | |
1577 | out: | |
1578 | if (task) { | |
1579 | scsi_free_scsi_task(task); | |
1580 | } | |
c1d4096b FZ |
1581 | } |
1582 | ||
43171420 KW |
1583 | static void iscsi_parse_iscsi_option(const char *target, QDict *options) |
1584 | { | |
1585 | QemuOptsList *list; | |
1586 | QemuOpts *opts; | |
81aa2a0f | 1587 | const char *user, *password, *password_secret, *initiator_name, |
1d560104 | 1588 | *header_digest, *timeout; |
43171420 KW |
1589 | |
1590 | list = qemu_find_opts("iscsi"); | |
1591 | if (!list) { | |
1592 | return; | |
1593 | } | |
1594 | ||
1595 | opts = qemu_opts_find(list, target); | |
1596 | if (opts == NULL) { | |
1597 | opts = QTAILQ_FIRST(&list->head); | |
1598 | if (!opts) { | |
1599 | return; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | user = qemu_opt_get(opts, "user"); | |
1604 | if (user) { | |
1605 | qdict_set_default_str(options, "user", user); | |
1606 | } | |
1607 | ||
1608 | password = qemu_opt_get(opts, "password"); | |
1609 | if (password) { | |
1610 | qdict_set_default_str(options, "password", password); | |
1611 | } | |
1612 | ||
1613 | password_secret = qemu_opt_get(opts, "password-secret"); | |
1614 | if (password_secret) { | |
1615 | qdict_set_default_str(options, "password-secret", password_secret); | |
1616 | } | |
d4e79929 KW |
1617 | |
1618 | initiator_name = qemu_opt_get(opts, "initiator-name"); | |
1619 | if (initiator_name) { | |
1620 | qdict_set_default_str(options, "initiator-name", initiator_name); | |
1621 | } | |
81aa2a0f KW |
1622 | |
1623 | header_digest = qemu_opt_get(opts, "header-digest"); | |
1624 | if (header_digest) { | |
31eb1202 KW |
1625 | /* -iscsi takes upper case values, but QAPI only supports lower case |
1626 | * enum constant names, so we have to convert here. */ | |
1627 | char *qapi_value = g_ascii_strdown(header_digest, -1); | |
1628 | qdict_set_default_str(options, "header-digest", qapi_value); | |
1629 | g_free(qapi_value); | |
81aa2a0f | 1630 | } |
1d560104 KW |
1631 | |
1632 | timeout = qemu_opt_get(opts, "timeout"); | |
1633 | if (timeout) { | |
1634 | qdict_set_default_str(options, "timeout", timeout); | |
1635 | } | |
43171420 KW |
1636 | } |
1637 | ||
c589b249 RS |
1638 | /* |
1639 | * We support iscsi url's on the form | |
1640 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> | |
1641 | */ | |
d5895fcb KW |
1642 | static void iscsi_parse_filename(const char *filename, QDict *options, |
1643 | Error **errp) | |
1644 | { | |
1645 | struct iscsi_url *iscsi_url; | |
1646 | const char *transport_name; | |
1647 | char *lun_str; | |
1648 | ||
1649 | iscsi_url = iscsi_parse_full_url(NULL, filename); | |
1650 | if (iscsi_url == NULL) { | |
1651 | error_setg(errp, "Failed to parse URL : %s", filename); | |
1652 | return; | |
1653 | } | |
1654 | ||
1655 | #if LIBISCSI_API_VERSION >= (20160603) | |
1656 | switch (iscsi_url->transport) { | |
1657 | case TCP_TRANSPORT: | |
1658 | transport_name = "tcp"; | |
1659 | break; | |
1660 | case ISER_TRANSPORT: | |
1661 | transport_name = "iser"; | |
1662 | break; | |
1663 | default: | |
1664 | error_setg(errp, "Unknown transport type (%d)", | |
1665 | iscsi_url->transport); | |
1666 | return; | |
1667 | } | |
1668 | #else | |
1669 | transport_name = "tcp"; | |
1670 | #endif | |
1671 | ||
1672 | qdict_set_default_str(options, "transport", transport_name); | |
1673 | qdict_set_default_str(options, "portal", iscsi_url->portal); | |
1674 | qdict_set_default_str(options, "target", iscsi_url->target); | |
1675 | ||
1676 | lun_str = g_strdup_printf("%d", iscsi_url->lun); | |
1677 | qdict_set_default_str(options, "lun", lun_str); | |
1678 | g_free(lun_str); | |
1679 | ||
43171420 KW |
1680 | /* User/password from -iscsi take precedence over those from the URL */ |
1681 | iscsi_parse_iscsi_option(iscsi_url->target, options); | |
1682 | ||
d5895fcb KW |
1683 | if (iscsi_url->user[0] != '\0') { |
1684 | qdict_set_default_str(options, "user", iscsi_url->user); | |
1685 | qdict_set_default_str(options, "password", iscsi_url->passwd); | |
1686 | } | |
1687 | ||
1688 | iscsi_destroy_url(iscsi_url); | |
1689 | } | |
1690 | ||
d5895fcb KW |
1691 | static QemuOptsList runtime_opts = { |
1692 | .name = "iscsi", | |
1693 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1694 | .desc = { | |
1695 | { | |
1696 | .name = "transport", | |
1697 | .type = QEMU_OPT_STRING, | |
1698 | }, | |
1699 | { | |
1700 | .name = "portal", | |
1701 | .type = QEMU_OPT_STRING, | |
1702 | }, | |
1703 | { | |
1704 | .name = "target", | |
1705 | .type = QEMU_OPT_STRING, | |
1706 | }, | |
1707 | { | |
1708 | .name = "user", | |
1709 | .type = QEMU_OPT_STRING, | |
1710 | }, | |
1711 | { | |
1712 | .name = "password", | |
1713 | .type = QEMU_OPT_STRING, | |
1714 | }, | |
43171420 KW |
1715 | { |
1716 | .name = "password-secret", | |
1717 | .type = QEMU_OPT_STRING, | |
1718 | }, | |
d5895fcb KW |
1719 | { |
1720 | .name = "lun", | |
1721 | .type = QEMU_OPT_NUMBER, | |
1722 | }, | |
d4e79929 KW |
1723 | { |
1724 | .name = "initiator-name", | |
1725 | .type = QEMU_OPT_STRING, | |
1726 | }, | |
81aa2a0f KW |
1727 | { |
1728 | .name = "header-digest", | |
1729 | .type = QEMU_OPT_STRING, | |
1730 | }, | |
1d560104 KW |
1731 | { |
1732 | .name = "timeout", | |
1733 | .type = QEMU_OPT_NUMBER, | |
1734 | }, | |
5c3ad1a6 JC |
1735 | { |
1736 | .name = "filename", | |
1737 | .type = QEMU_OPT_STRING, | |
1738 | }, | |
d5895fcb KW |
1739 | { /* end of list */ } |
1740 | }, | |
1741 | }; | |
1742 | ||
015a1036 HR |
1743 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, |
1744 | Error **errp) | |
c589b249 RS |
1745 | { |
1746 | IscsiLun *iscsilun = bs->opaque; | |
1747 | struct iscsi_context *iscsi = NULL; | |
e829b0bb PL |
1748 | struct scsi_task *task = NULL; |
1749 | struct scsi_inquiry_standard *inq = NULL; | |
24d3bd67 | 1750 | struct scsi_inquiry_supported_pages *inq_vpd; |
f9dadc98 | 1751 | char *initiator_name = NULL; |
60beb341 KW |
1752 | QemuOpts *opts; |
1753 | Error *local_err = NULL; | |
5c3ad1a6 | 1754 | const char *transport_name, *portal, *target, *filename; |
d5895fcb KW |
1755 | #if LIBISCSI_API_VERSION >= (20160603) |
1756 | enum iscsi_transport_type transport; | |
1757 | #endif | |
1758 | int i, ret = 0, timeout = 0, lun; | |
c589b249 | 1759 | |
5c3ad1a6 JC |
1760 | /* If we are given a filename, parse the filename, with precedence given to |
1761 | * filename encoded options */ | |
1762 | filename = qdict_get_try_str(options, "filename"); | |
1763 | if (filename) { | |
1764 | error_report("Warning: 'filename' option specified. " | |
1765 | "This is an unsupported option, and may be deprecated " | |
1766 | "in the future"); | |
1767 | iscsi_parse_filename(filename, options, &local_err); | |
1768 | if (local_err) { | |
1769 | ret = -EINVAL; | |
1770 | error_propagate(errp, local_err); | |
1771 | goto exit; | |
1772 | } | |
1773 | } | |
1774 | ||
87ea75d5 | 1775 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
60beb341 | 1776 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 1777 | if (local_err) { |
f2917853 | 1778 | error_propagate(errp, local_err); |
60beb341 KW |
1779 | ret = -EINVAL; |
1780 | goto out; | |
1781 | } | |
1782 | ||
d5895fcb KW |
1783 | transport_name = qemu_opt_get(opts, "transport"); |
1784 | portal = qemu_opt_get(opts, "portal"); | |
1785 | target = qemu_opt_get(opts, "target"); | |
d5895fcb | 1786 | lun = qemu_opt_get_number(opts, "lun", 0); |
60beb341 | 1787 | |
d5895fcb KW |
1788 | if (!transport_name || !portal || !target) { |
1789 | error_setg(errp, "Need all of transport, portal and target options"); | |
1790 | ret = -EINVAL; | |
1791 | goto out; | |
1792 | } | |
d5895fcb KW |
1793 | |
1794 | if (!strcmp(transport_name, "tcp")) { | |
1795 | #if LIBISCSI_API_VERSION >= (20160603) | |
1796 | transport = TCP_TRANSPORT; | |
1797 | } else if (!strcmp(transport_name, "iser")) { | |
1798 | transport = ISER_TRANSPORT; | |
1799 | #else | |
1800 | /* TCP is what older libiscsi versions always use */ | |
1801 | #endif | |
1802 | } else { | |
1803 | error_setg(errp, "Unknown transport: %s", transport_name); | |
c589b249 | 1804 | ret = -EINVAL; |
b93c94f7 | 1805 | goto out; |
c589b249 RS |
1806 | } |
1807 | ||
f9dadc98 RS |
1808 | memset(iscsilun, 0, sizeof(IscsiLun)); |
1809 | ||
d4e79929 | 1810 | initiator_name = get_initiator_name(opts); |
f9dadc98 RS |
1811 | |
1812 | iscsi = iscsi_create_context(initiator_name); | |
1813 | if (iscsi == NULL) { | |
f2917853 | 1814 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); |
f9dadc98 | 1815 | ret = -ENOMEM; |
b93c94f7 | 1816 | goto out; |
f9dadc98 | 1817 | } |
e0ae4987 | 1818 | #if LIBISCSI_API_VERSION >= (20160603) |
d5895fcb | 1819 | if (iscsi_init_transport(iscsi, transport)) { |
e0ae4987 RS |
1820 | error_setg(errp, ("Error initializing transport.")); |
1821 | ret = -EINVAL; | |
1822 | goto out; | |
1823 | } | |
1824 | #endif | |
d5895fcb | 1825 | if (iscsi_set_targetname(iscsi, target)) { |
f2917853 | 1826 | error_setg(errp, "iSCSI: Failed to set target name."); |
c589b249 | 1827 | ret = -EINVAL; |
b93c94f7 | 1828 | goto out; |
c589b249 RS |
1829 | } |
1830 | ||
f9dadc98 | 1831 | /* check if we got CHAP username/password via the options */ |
43171420 | 1832 | apply_chap(iscsi, opts, &local_err); |
f2917853 PB |
1833 | if (local_err != NULL) { |
1834 | error_propagate(errp, local_err); | |
f9dadc98 | 1835 | ret = -EINVAL; |
b93c94f7 | 1836 | goto out; |
f9dadc98 RS |
1837 | } |
1838 | ||
c589b249 | 1839 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
f2917853 | 1840 | error_setg(errp, "iSCSI: Failed to set session type to normal."); |
c589b249 | 1841 | ret = -EINVAL; |
b93c94f7 | 1842 | goto out; |
c589b249 RS |
1843 | } |
1844 | ||
f9dadc98 | 1845 | /* check if we got HEADER_DIGEST via the options */ |
81aa2a0f | 1846 | apply_header_digest(iscsi, opts, &local_err); |
f2917853 PB |
1847 | if (local_err != NULL) { |
1848 | error_propagate(errp, local_err); | |
1849 | ret = -EINVAL; | |
1850 | goto out; | |
1851 | } | |
f9dadc98 | 1852 | |
9049736e | 1853 | /* timeout handling is broken in libiscsi before 1.15.0 */ |
1d560104 | 1854 | timeout = qemu_opt_get_number(opts, "timeout", 0); |
583ec22e | 1855 | #if LIBISCSI_API_VERSION >= 20150621 |
9049736e PL |
1856 | iscsi_set_timeout(iscsi, timeout); |
1857 | #else | |
1858 | if (timeout) { | |
1859 | error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); | |
1860 | } | |
1861 | #endif | |
5dd7a535 | 1862 | |
d5895fcb | 1863 | if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) { |
f2917853 | 1864 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", |
e829b0bb PL |
1865 | iscsi_get_error(iscsi)); |
1866 | ret = -EINVAL; | |
1867 | goto out; | |
1868 | } | |
c589b249 RS |
1869 | |
1870 | iscsilun->iscsi = iscsi; | |
80cf6257 | 1871 | iscsilun->aio_context = bdrv_get_aio_context(bs); |
d5895fcb | 1872 | iscsilun->lun = lun; |
24d3bd67 | 1873 | iscsilun->has_write_same = true; |
c589b249 | 1874 | |
24d3bd67 PL |
1875 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, |
1876 | (void **) &inq, errp); | |
1877 | if (task == NULL) { | |
c589b249 | 1878 | ret = -EINVAL; |
b93c94f7 | 1879 | goto out; |
c589b249 | 1880 | } |
e829b0bb | 1881 | iscsilun->type = inq->periperal_device_type; |
24d3bd67 PL |
1882 | scsi_free_scsi_task(task); |
1883 | task = NULL; | |
e829b0bb | 1884 | |
7191f208 | 1885 | iscsi_modesense_sync(iscsilun); |
4df863f3 EB |
1886 | if (iscsilun->dpofua) { |
1887 | bs->supported_write_flags = BDRV_REQ_FUA; | |
1888 | } | |
465fe887 | 1889 | bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; |
7191f208 | 1890 | |
c1d4096b FZ |
1891 | /* Check the write protect flag of the LUN if we want to write */ |
1892 | if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && | |
43ae8fb1 | 1893 | iscsilun->write_protected) { |
c1d4096b FZ |
1894 | error_setg(errp, "Cannot open a write protected LUN as read-write"); |
1895 | ret = -EACCES; | |
1896 | goto out; | |
1897 | } | |
1898 | ||
f2917853 PB |
1899 | iscsi_readcapacity_sync(iscsilun, &local_err); |
1900 | if (local_err != NULL) { | |
1901 | error_propagate(errp, local_err); | |
cd82b6fb | 1902 | ret = -EINVAL; |
cb1b83e7 | 1903 | goto out; |
e829b0bb | 1904 | } |
0777b5dd | 1905 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); |
e829b0bb | 1906 | |
f47c3f5a KW |
1907 | /* We don't have any emulation for devices other than disks and CD-ROMs, so |
1908 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu | |
1909 | * will try to read from the device to guess the image format. | |
622695a4 | 1910 | */ |
f47c3f5a | 1911 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { |
54115412 | 1912 | bs->sg = true; |
622695a4 RS |
1913 | } |
1914 | ||
24d3bd67 PL |
1915 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
1916 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, | |
1917 | (void **) &inq_vpd, errp); | |
1918 | if (task == NULL) { | |
1919 | ret = -EINVAL; | |
1920 | goto out; | |
f18a7cbb | 1921 | } |
24d3bd67 PL |
1922 | for (i = 0; i < inq_vpd->num_pages; i++) { |
1923 | struct scsi_task *inq_task; | |
1924 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; | |
f18a7cbb | 1925 | struct scsi_inquiry_block_limits *inq_bl; |
24d3bd67 PL |
1926 | switch (inq_vpd->pages[i]) { |
1927 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: | |
1928 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1929 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, | |
1930 | (void **) &inq_lbp, errp); | |
1931 | if (inq_task == NULL) { | |
1932 | ret = -EINVAL; | |
1933 | goto out; | |
1934 | } | |
1935 | memcpy(&iscsilun->lbp, inq_lbp, | |
1936 | sizeof(struct scsi_inquiry_logical_block_provisioning)); | |
1937 | scsi_free_scsi_task(inq_task); | |
1938 | break; | |
1939 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: | |
1940 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1941 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, | |
1942 | (void **) &inq_bl, errp); | |
1943 | if (inq_task == NULL) { | |
1944 | ret = -EINVAL; | |
1945 | goto out; | |
1946 | } | |
1947 | memcpy(&iscsilun->bl, inq_bl, | |
1948 | sizeof(struct scsi_inquiry_block_limits)); | |
1949 | scsi_free_scsi_task(inq_task); | |
1950 | break; | |
1951 | default: | |
1952 | break; | |
f18a7cbb | 1953 | } |
f18a7cbb | 1954 | } |
24d3bd67 PL |
1955 | scsi_free_scsi_task(task); |
1956 | task = NULL; | |
f18a7cbb | 1957 | |
d045c466 | 1958 | qemu_mutex_init(&iscsilun->mutex); |
80cf6257 | 1959 | iscsi_attach_aio_context(bs, iscsilun->aio_context); |
5b5d34ec | 1960 | |
b03c3805 PL |
1961 | /* Guess the internal cluster (page) size of the iscsi target by the means |
1962 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a | |
1963 | * reasonable size */ | |
3d2acaa3 | 1964 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && |
b03c3805 PL |
1965 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { |
1966 | iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * | |
1967 | iscsilun->block_size) >> BDRV_SECTOR_BITS; | |
9eac3622 | 1968 | if (iscsilun->lbprz) { |
e1123a3b | 1969 | ret = iscsi_allocmap_init(iscsilun, bs->open_flags); |
b03c3805 | 1970 | } |
b03c3805 PL |
1971 | } |
1972 | ||
b93c94f7 | 1973 | out: |
60beb341 | 1974 | qemu_opts_del(opts); |
f7047c2d | 1975 | g_free(initiator_name); |
e829b0bb PL |
1976 | if (task != NULL) { |
1977 | scsi_free_scsi_task(task); | |
1978 | } | |
b93c94f7 PB |
1979 | |
1980 | if (ret) { | |
1981 | if (iscsi != NULL) { | |
20474e9a PL |
1982 | if (iscsi_is_logged_in(iscsi)) { |
1983 | iscsi_logout_sync(iscsi); | |
1984 | } | |
b93c94f7 PB |
1985 | iscsi_destroy_context(iscsi); |
1986 | } | |
1987 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
c589b249 | 1988 | } |
5c3ad1a6 | 1989 | exit: |
c589b249 RS |
1990 | return ret; |
1991 | } | |
1992 | ||
1993 | static void iscsi_close(BlockDriverState *bs) | |
1994 | { | |
1995 | IscsiLun *iscsilun = bs->opaque; | |
1996 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1997 | ||
80cf6257 | 1998 | iscsi_detach_aio_context(bs); |
20474e9a PL |
1999 | if (iscsi_is_logged_in(iscsi)) { |
2000 | iscsi_logout_sync(iscsi); | |
2001 | } | |
c589b249 | 2002 | iscsi_destroy_context(iscsi); |
d4cd9615 | 2003 | g_free(iscsilun->zeroblock); |
e1123a3b | 2004 | iscsi_allocmap_free(iscsilun); |
d045c466 | 2005 | qemu_mutex_destroy(&iscsilun->mutex); |
c589b249 RS |
2006 | memset(iscsilun, 0, sizeof(IscsiLun)); |
2007 | } | |
2008 | ||
52f6fa14 PL |
2009 | static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) |
2010 | { | |
d34682cd KW |
2011 | /* We don't actually refresh here, but just return data queried in |
2012 | * iscsi_open(): iscsi targets don't change their limits. */ | |
52f6fa14 PL |
2013 | |
2014 | IscsiLun *iscsilun = bs->opaque; | |
5def6b80 | 2015 | uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; |
95eaa785 | 2016 | unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size); |
52f6fa14 | 2017 | |
95eaa785 EB |
2018 | assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg); |
2019 | ||
2020 | bs->bl.request_alignment = block_size; | |
c8b3b998 | 2021 | |
52f6fa14 PL |
2022 | if (iscsilun->bl.max_xfer_len) { |
2023 | max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); | |
2024 | } | |
2025 | ||
95eaa785 | 2026 | if (max_xfer_len * block_size < INT_MAX) { |
5def6b80 EB |
2027 | bs->bl.max_transfer = max_xfer_len * iscsilun->block_size; |
2028 | } | |
52f6fa14 | 2029 | |
c97ca29d | 2030 | if (iscsilun->lbp.lbpu) { |
95eaa785 | 2031 | if (iscsilun->bl.max_unmap < 0xffffffff / block_size) { |
b9f7855a EB |
2032 | bs->bl.max_pdiscard = |
2033 | iscsilun->bl.max_unmap * iscsilun->block_size; | |
d34682cd | 2034 | } |
b9f7855a EB |
2035 | bs->bl.pdiscard_alignment = |
2036 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size; | |
8b184744 | 2037 | } else { |
b9f7855a | 2038 | bs->bl.pdiscard_alignment = iscsilun->block_size; |
c97ca29d | 2039 | } |
d34682cd | 2040 | |
95eaa785 | 2041 | if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) { |
cf081fca EB |
2042 | bs->bl.max_pwrite_zeroes = |
2043 | iscsilun->bl.max_ws_len * iscsilun->block_size; | |
c97ca29d PB |
2044 | } |
2045 | if (iscsilun->lbp.lbpws) { | |
cf081fca EB |
2046 | bs->bl.pwrite_zeroes_alignment = |
2047 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size; | |
8b184744 | 2048 | } else { |
cf081fca | 2049 | bs->bl.pwrite_zeroes_alignment = iscsilun->block_size; |
d34682cd | 2050 | } |
5def6b80 | 2051 | if (iscsilun->bl.opt_xfer_len && |
95eaa785 | 2052 | iscsilun->bl.opt_xfer_len < INT_MAX / block_size) { |
5def6b80 EB |
2053 | bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len * |
2054 | iscsilun->block_size); | |
2055 | } | |
e9f526ab | 2056 | } |
d34682cd | 2057 | |
43ae8fb1 FZ |
2058 | /* Note that this will not re-establish a connection with an iSCSI target - it |
2059 | * is effectively a NOP. */ | |
dc6afb99 JC |
2060 | static int iscsi_reopen_prepare(BDRVReopenState *state, |
2061 | BlockReopenQueue *queue, Error **errp) | |
2062 | { | |
43ae8fb1 FZ |
2063 | IscsiLun *iscsilun = state->bs->opaque; |
2064 | ||
2065 | if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { | |
2066 | error_setg(errp, "Cannot open a write protected LUN as read-write"); | |
2067 | return -EACCES; | |
2068 | } | |
d34682cd KW |
2069 | return 0; |
2070 | } | |
2071 | ||
e1123a3b PL |
2072 | static void iscsi_reopen_commit(BDRVReopenState *reopen_state) |
2073 | { | |
2074 | IscsiLun *iscsilun = reopen_state->bs->opaque; | |
2075 | ||
2076 | /* the cache.direct status might have changed */ | |
2077 | if (iscsilun->allocmap != NULL) { | |
2078 | iscsi_allocmap_init(iscsilun, reopen_state->flags); | |
2079 | } | |
2080 | } | |
2081 | ||
4bff28b8 | 2082 | static int iscsi_truncate(BlockDriverState *bs, int64_t offset, Error **errp) |
cb1b83e7 PL |
2083 | { |
2084 | IscsiLun *iscsilun = bs->opaque; | |
f2917853 | 2085 | Error *local_err = NULL; |
cb1b83e7 PL |
2086 | |
2087 | if (iscsilun->type != TYPE_DISK) { | |
f59adb32 | 2088 | error_setg(errp, "Cannot resize non-disk iSCSI devices"); |
cb1b83e7 PL |
2089 | return -ENOTSUP; |
2090 | } | |
2091 | ||
f2917853 PB |
2092 | iscsi_readcapacity_sync(iscsilun, &local_err); |
2093 | if (local_err != NULL) { | |
4bff28b8 | 2094 | error_propagate(errp, local_err); |
f2917853 | 2095 | return -EIO; |
cb1b83e7 PL |
2096 | } |
2097 | ||
2098 | if (offset > iscsi_getlength(bs)) { | |
f59adb32 | 2099 | error_setg(errp, "Cannot grow iSCSI devices"); |
cb1b83e7 PL |
2100 | return -EINVAL; |
2101 | } | |
2102 | ||
e1123a3b PL |
2103 | if (iscsilun->allocmap != NULL) { |
2104 | iscsi_allocmap_init(iscsilun, bs->open_flags); | |
b03c3805 PL |
2105 | } |
2106 | ||
cb1b83e7 PL |
2107 | return 0; |
2108 | } | |
2109 | ||
a59479e3 | 2110 | static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) |
de8864e5 PL |
2111 | { |
2112 | int ret = 0; | |
2113 | int64_t total_size = 0; | |
13c91cb7 | 2114 | BlockDriverState *bs; |
de8864e5 | 2115 | IscsiLun *iscsilun = NULL; |
60beb341 | 2116 | QDict *bs_options; |
2ec9a782 | 2117 | Error *local_err = NULL; |
de8864e5 | 2118 | |
e4e9986b | 2119 | bs = bdrv_new(); |
de8864e5 PL |
2120 | |
2121 | /* Read out options */ | |
c2eb918e HT |
2122 | total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
2123 | BDRV_SECTOR_SIZE); | |
5839e53b | 2124 | bs->opaque = g_new0(struct IscsiLun, 1); |
13c91cb7 | 2125 | iscsilun = bs->opaque; |
de8864e5 | 2126 | |
60beb341 | 2127 | bs_options = qdict_new(); |
2ec9a782 FZ |
2128 | iscsi_parse_filename(filename, bs_options, &local_err); |
2129 | if (local_err) { | |
2130 | error_propagate(errp, local_err); | |
2131 | ret = -EINVAL; | |
2132 | } else { | |
2133 | ret = iscsi_open(bs, bs_options, 0, NULL); | |
2134 | } | |
60beb341 KW |
2135 | QDECREF(bs_options); |
2136 | ||
de8864e5 PL |
2137 | if (ret != 0) { |
2138 | goto out; | |
2139 | } | |
80cf6257 | 2140 | iscsi_detach_aio_context(bs); |
de8864e5 PL |
2141 | if (iscsilun->type != TYPE_DISK) { |
2142 | ret = -ENODEV; | |
2143 | goto out; | |
2144 | } | |
13c91cb7 | 2145 | if (bs->total_sectors < total_size) { |
de8864e5 | 2146 | ret = -ENOSPC; |
d3bda7bc | 2147 | goto out; |
de8864e5 PL |
2148 | } |
2149 | ||
2150 | ret = 0; | |
2151 | out: | |
2152 | if (iscsilun->iscsi != NULL) { | |
2153 | iscsi_destroy_context(iscsilun->iscsi); | |
2154 | } | |
13c91cb7 FZ |
2155 | g_free(bs->opaque); |
2156 | bs->opaque = NULL; | |
4f6fd349 | 2157 | bdrv_unref(bs); |
de8864e5 PL |
2158 | return ret; |
2159 | } | |
2160 | ||
186d4f2b PL |
2161 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
2162 | { | |
2163 | IscsiLun *iscsilun = bs->opaque; | |
0a386e48 | 2164 | bdi->unallocated_blocks_are_zero = iscsilun->lbprz; |
186d4f2b | 2165 | bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; |
b03c3805 | 2166 | bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE; |
186d4f2b PL |
2167 | return 0; |
2168 | } | |
2169 | ||
e1123a3b PL |
2170 | static void iscsi_invalidate_cache(BlockDriverState *bs, |
2171 | Error **errp) | |
2172 | { | |
2173 | IscsiLun *iscsilun = bs->opaque; | |
2174 | iscsi_allocmap_invalidate(iscsilun); | |
2175 | } | |
2176 | ||
a59479e3 CL |
2177 | static QemuOptsList iscsi_create_opts = { |
2178 | .name = "iscsi-create-opts", | |
2179 | .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), | |
2180 | .desc = { | |
2181 | { | |
2182 | .name = BLOCK_OPT_SIZE, | |
2183 | .type = QEMU_OPT_SIZE, | |
2184 | .help = "Virtual disk size" | |
2185 | }, | |
2186 | { /* end of list */ } | |
2187 | } | |
de8864e5 PL |
2188 | }; |
2189 | ||
c589b249 RS |
2190 | static BlockDriver bdrv_iscsi = { |
2191 | .format_name = "iscsi", | |
2192 | .protocol_name = "iscsi", | |
2193 | ||
d5895fcb KW |
2194 | .instance_size = sizeof(IscsiLun), |
2195 | .bdrv_parse_filename = iscsi_parse_filename, | |
2196 | .bdrv_file_open = iscsi_open, | |
2197 | .bdrv_close = iscsi_close, | |
2198 | .bdrv_create = iscsi_create, | |
2199 | .create_opts = &iscsi_create_opts, | |
2200 | .bdrv_reopen_prepare = iscsi_reopen_prepare, | |
2201 | .bdrv_reopen_commit = iscsi_reopen_commit, | |
2202 | .bdrv_invalidate_cache = iscsi_invalidate_cache, | |
c589b249 RS |
2203 | |
2204 | .bdrv_getlength = iscsi_getlength, | |
186d4f2b | 2205 | .bdrv_get_info = iscsi_get_info, |
cb1b83e7 | 2206 | .bdrv_truncate = iscsi_truncate, |
d34682cd | 2207 | .bdrv_refresh_limits = iscsi_refresh_limits, |
c589b249 | 2208 | |
54a5c1d5 | 2209 | .bdrv_co_get_block_status = iscsi_co_get_block_status, |
97c7e85c | 2210 | .bdrv_co_pdiscard = iscsi_co_pdiscard, |
94d047a3 | 2211 | .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, |
063c3378 | 2212 | .bdrv_co_readv = iscsi_co_readv, |
9f0eb9e1 | 2213 | .bdrv_co_writev_flags = iscsi_co_writev_flags, |
063c3378 | 2214 | .bdrv_co_flush_to_disk = iscsi_co_flush, |
fa6acb0c | 2215 | |
98392453 | 2216 | #ifdef __linux__ |
98392453 RS |
2217 | .bdrv_aio_ioctl = iscsi_aio_ioctl, |
2218 | #endif | |
80cf6257 SH |
2219 | |
2220 | .bdrv_detach_aio_context = iscsi_detach_aio_context, | |
2221 | .bdrv_attach_aio_context = iscsi_attach_aio_context, | |
c589b249 RS |
2222 | }; |
2223 | ||
e0ae4987 RS |
2224 | #if LIBISCSI_API_VERSION >= (20160603) |
2225 | static BlockDriver bdrv_iser = { | |
2226 | .format_name = "iser", | |
2227 | .protocol_name = "iser", | |
2228 | ||
d5895fcb KW |
2229 | .instance_size = sizeof(IscsiLun), |
2230 | .bdrv_parse_filename = iscsi_parse_filename, | |
2231 | .bdrv_file_open = iscsi_open, | |
2232 | .bdrv_close = iscsi_close, | |
2233 | .bdrv_create = iscsi_create, | |
2234 | .create_opts = &iscsi_create_opts, | |
2235 | .bdrv_reopen_prepare = iscsi_reopen_prepare, | |
2236 | .bdrv_reopen_commit = iscsi_reopen_commit, | |
2237 | .bdrv_invalidate_cache = iscsi_invalidate_cache, | |
e0ae4987 RS |
2238 | |
2239 | .bdrv_getlength = iscsi_getlength, | |
2240 | .bdrv_get_info = iscsi_get_info, | |
2241 | .bdrv_truncate = iscsi_truncate, | |
2242 | .bdrv_refresh_limits = iscsi_refresh_limits, | |
2243 | ||
2244 | .bdrv_co_get_block_status = iscsi_co_get_block_status, | |
2245 | .bdrv_co_pdiscard = iscsi_co_pdiscard, | |
2246 | .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, | |
2247 | .bdrv_co_readv = iscsi_co_readv, | |
2248 | .bdrv_co_writev_flags = iscsi_co_writev_flags, | |
2249 | .bdrv_co_flush_to_disk = iscsi_co_flush, | |
2250 | ||
2251 | #ifdef __linux__ | |
2252 | .bdrv_aio_ioctl = iscsi_aio_ioctl, | |
2253 | #endif | |
2254 | ||
2255 | .bdrv_detach_aio_context = iscsi_detach_aio_context, | |
2256 | .bdrv_attach_aio_context = iscsi_attach_aio_context, | |
2257 | }; | |
2258 | #endif | |
2259 | ||
c589b249 RS |
2260 | static void iscsi_block_init(void) |
2261 | { | |
2262 | bdrv_register(&bdrv_iscsi); | |
e0ae4987 RS |
2263 | #if LIBISCSI_API_VERSION >= (20160603) |
2264 | bdrv_register(&bdrv_iser); | |
2265 | #endif | |
c589b249 RS |
2266 | } |
2267 | ||
2268 | block_init(iscsi_block_init); |