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