Add support for UNMAP command and add a simple test for this opcode

This commit is contained in:
Ronnie Sahlberg
2012-04-22 08:09:15 +10:00
parent d71a9d4f95
commit dd5f94b2ca
10 changed files with 260 additions and 2 deletions

View File

@@ -91,4 +91,5 @@ scsi_cdb_write10
scsi_cdb_synchronizecache10
scsi_cdb_readcapacity16
scsi_cdb_serviceactionin16
scsi_cdb_unmap

View File

@@ -794,6 +794,53 @@ iscsi_synchronizecache10_task(struct iscsi_context *iscsi, int lun, int lba,
return task;
}
struct scsi_task *
iscsi_unmap_task(struct iscsi_context *iscsi, int lun, int anchor, int group,
struct unmap_list *list, int list_len,
iscsi_command_cb cb, void *private_data)
{
struct scsi_task *task;
struct iscsi_data outdata;
unsigned char *data;
int xferlen;
int i;
xferlen = 8 + list_len * 16;
task = scsi_cdb_unmap(anchor, group, xferlen);
if (task == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to create "
"unmap cdb.");
return NULL;
}
data = scsi_malloc(task, xferlen);
if (data == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to create "
"unmap parameters.");
scsi_free_scsi_task(task);
return NULL;
}
*((uint16_t *)&data[0]) = htons(xferlen - 2);
*((uint16_t *)&data[2]) = htons(xferlen - 8);
for (i = 0; i < list_len; i++) {
*((uint32_t *)&data[8 + 16 * i]) = htonl(list[0].lba >> 32);
*((uint32_t *)&data[8 + 16 * i + 4]) = htonl(list[0].lba & 0xffffffff);
*((uint32_t *)&data[8 + 16 * i + 8]) = htonl(list[0].num);
}
outdata.data = data;
outdata.size = xferlen;
if (iscsi_scsi_command_async(iscsi, lun, task, cb, &outdata,
private_data) != 0) {
scsi_free_scsi_task(task);
return NULL;
}
return task;
}
unsigned char *
iscsi_get_user_in_buffer(struct iscsi_context *iscsi, struct iscsi_in_pdu *in, uint32_t pos, ssize_t *count)
{

View File

@@ -55,7 +55,7 @@ scsi_free_scsi_task(struct scsi_task *task)
free(task);
}
static void *
void *
scsi_malloc(struct scsi_task *task, size_t size)
{
struct scsi_allocated_memory *mem;
@@ -759,6 +759,36 @@ scsi_cdb_verify10(uint32_t lba, uint32_t xferlen, int vprotect, int dpo, int byt
}
/*
* UNMAP
*/
struct scsi_task *
scsi_cdb_unmap(int anchor, int group, uint16_t xferlen)
{
struct scsi_task *task;
task = malloc(sizeof(struct scsi_task));
if (task == NULL) {
return NULL;
}
memset(task, 0, sizeof(struct scsi_task));
task->cdb[0] = SCSI_OPCODE_UNMAP;
if (anchor) {
task->cdb[1] |= 0x01;
}
task->cdb[6] |= group & 0x1f;
*(uint16_t *)&task->cdb[7] = htons(xferlen);
task->cdb_size = 10;
task->xfer_dir = SCSI_XFER_WRITE;
task->expxferlen = xferlen;
return task;
}
/*
* MODESENSE6
*/

View File

@@ -361,6 +361,26 @@ iscsi_verify10_sync(struct iscsi_context *iscsi, int lun, unsigned char *data, u
return state.task;
}
struct scsi_task *
iscsi_unmap_sync(struct iscsi_context *iscsi, int lun, int anchor, int group,
struct unmap_list *list, int list_len)
{
struct iscsi_sync_state state;
memset(&state, 0, sizeof(state));
if (iscsi_unmap_task(iscsi, lun, anchor, group, list, list_len,
scsi_sync_cb, &state) == NULL) {
iscsi_set_error(iscsi,
"Failed to send UNMAP command");
return NULL;
}
event_loop(iscsi, &state);
return state.task;
}
struct scsi_task *
iscsi_scsi_command_sync(struct iscsi_context *iscsi, int lun,
struct scsi_task *task, struct iscsi_data *data)