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