target: Use {get,put}_unaligned_be*() instead of open coding these functions

Introduce the function get_unaligned_be24(). Use {get,put}_unaligned_be*()
where appropriate. This patch does not change any functionality.

Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.com>
Cc: Andy Grover <agrover@redhat.com>
Cc: David Disseldorp <ddiss@suse.de>
Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
This commit is contained in:
Bart Van Assche 2017-05-23 16:48:27 -07:00 коммит произвёл Nicholas Bellinger
Родитель f2b72d6a8e
Коммит a85d667e58
9 изменённых файлов: 83 добавлений и 171 удалений

Просмотреть файл

@ -205,8 +205,8 @@ target_emulate_report_target_port_groups(struct se_cmd *cmd)
/* /*
* TARGET PORT GROUP * TARGET PORT GROUP
*/ */
buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff); put_unaligned_be16(tg_pt_gp->tg_pt_gp_id, &buf[off]);
buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff); off += 2;
off++; /* Skip over Reserved */ off++; /* Skip over Reserved */
/* /*
@ -235,8 +235,8 @@ target_emulate_report_target_port_groups(struct se_cmd *cmd)
/* /*
* Set RELATIVE TARGET PORT IDENTIFIER * Set RELATIVE TARGET PORT IDENTIFIER
*/ */
buf[off++] = ((lun->lun_rtpi >> 8) & 0xff); put_unaligned_be16(lun->lun_rtpi, &buf[off]);
buf[off++] = (lun->lun_rtpi & 0xff); off += 2;
rd_len += 4; rd_len += 4;
} }
spin_unlock(&tg_pt_gp->tg_pt_gp_lock); spin_unlock(&tg_pt_gp->tg_pt_gp_lock);

Просмотреть файл

@ -1091,19 +1091,19 @@ passthrough_parse_cdb(struct se_cmd *cmd,
TRANSPORT_FLAG_PASSTHROUGH_PGR)) { TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
if (cdb[0] == PERSISTENT_RESERVE_IN) { if (cdb[0] == PERSISTENT_RESERVE_IN) {
cmd->execute_cmd = target_scsi3_emulate_pr_in; cmd->execute_cmd = target_scsi3_emulate_pr_in;
size = (cdb[7] << 8) + cdb[8]; size = get_unaligned_be16(&cdb[7]);
return target_cmd_size_check(cmd, size); return target_cmd_size_check(cmd, size);
} }
if (cdb[0] == PERSISTENT_RESERVE_OUT) { if (cdb[0] == PERSISTENT_RESERVE_OUT) {
cmd->execute_cmd = target_scsi3_emulate_pr_out; cmd->execute_cmd = target_scsi3_emulate_pr_out;
size = (cdb[7] << 8) + cdb[8]; size = get_unaligned_be16(&cdb[7]);
return target_cmd_size_check(cmd, size); return target_cmd_size_check(cmd, size);
} }
if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) { if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
cmd->execute_cmd = target_scsi2_reservation_release; cmd->execute_cmd = target_scsi2_reservation_release;
if (cdb[0] == RELEASE_10) if (cdb[0] == RELEASE_10)
size = (cdb[7] << 8) | cdb[8]; size = get_unaligned_be16(&cdb[7]);
else else
size = cmd->data_length; size = cmd->data_length;
return target_cmd_size_check(cmd, size); return target_cmd_size_check(cmd, size);
@ -1111,7 +1111,7 @@ passthrough_parse_cdb(struct se_cmd *cmd,
if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) { if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
cmd->execute_cmd = target_scsi2_reservation_reserve; cmd->execute_cmd = target_scsi2_reservation_reserve;
if (cdb[0] == RESERVE_10) if (cdb[0] == RESERVE_10)
size = (cdb[7] << 8) | cdb[8]; size = get_unaligned_be16(&cdb[7]);
else else
size = cmd->data_length; size = cmd->data_length;
return target_cmd_size_check(cmd, size); return target_cmd_size_check(cmd, size);

Просмотреть файл

@ -34,6 +34,7 @@
#include <linux/ctype.h> #include <linux/ctype.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/export.h> #include <linux/export.h>
#include <asm/unaligned.h>
#include <scsi/scsi_proto.h> #include <scsi/scsi_proto.h>
@ -216,8 +217,7 @@ static int iscsi_get_pr_transport_id(
if (padding != 0) if (padding != 0)
len += padding; len += padding;
buf[2] = ((len >> 8) & 0xff); put_unaligned_be16(len, &buf[2]);
buf[3] = (len & 0xff);
/* /*
* Increment value for total payload + header length for * Increment value for total payload + header length for
* full status descriptor * full status descriptor
@ -306,7 +306,7 @@ static char *iscsi_parse_pr_out_transport_id(
*/ */
if (out_tid_len) { if (out_tid_len) {
/* The shift works thanks to integer promotion rules */ /* The shift works thanks to integer promotion rules */
add_len = (buf[2] << 8) | buf[3]; add_len = get_unaligned_be16(&buf[2]);
tid_len = strlen(&buf[4]); tid_len = strlen(&buf[4]);
tid_len += 4; /* Add four bytes for iSCSI Transport ID header */ tid_len += 4; /* Add four bytes for iSCSI Transport ID header */

Просмотреть файл

@ -1562,10 +1562,7 @@ core_scsi3_decode_spec_i_port(
* first extract TransportID Parameter Data Length, and make sure * first extract TransportID Parameter Data Length, and make sure
* the value matches up to the SCSI expected data transfer length. * the value matches up to the SCSI expected data transfer length.
*/ */
tpdl = (buf[24] & 0xff) << 24; tpdl = get_unaligned_be32(&buf[24]);
tpdl |= (buf[25] & 0xff) << 16;
tpdl |= (buf[26] & 0xff) << 8;
tpdl |= buf[27] & 0xff;
if ((tpdl + 28) != cmd->data_length) { if ((tpdl + 28) != cmd->data_length) {
pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header" pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
@ -3221,12 +3218,8 @@ core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
goto out_put_pr_reg; goto out_put_pr_reg;
} }
rtpi = (buf[18] & 0xff) << 8; rtpi = get_unaligned_be16(&buf[18]);
rtpi |= buf[19] & 0xff; tid_len = get_unaligned_be32(&buf[20]);
tid_len = (buf[20] & 0xff) << 24;
tid_len |= (buf[21] & 0xff) << 16;
tid_len |= (buf[22] & 0xff) << 8;
tid_len |= buf[23] & 0xff;
transport_kunmap_data_sg(cmd); transport_kunmap_data_sg(cmd);
buf = NULL; buf = NULL;
@ -3552,16 +3545,6 @@ out_put_pr_reg:
return ret; return ret;
} }
static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
{
unsigned int __v1, __v2;
__v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
__v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
}
/* /*
* See spc4r17 section 6.14 Table 170 * See spc4r17 section 6.14 Table 170
*/ */
@ -3619,8 +3602,8 @@ target_scsi3_emulate_pr_out(struct se_cmd *cmd)
/* /*
* From PERSISTENT_RESERVE_OUT parameter list (payload) * From PERSISTENT_RESERVE_OUT parameter list (payload)
*/ */
res_key = core_scsi3_extract_reservation_key(&buf[0]); res_key = get_unaligned_be64(&buf[0]);
sa_res_key = core_scsi3_extract_reservation_key(&buf[8]); sa_res_key = get_unaligned_be64(&buf[8]);
/* /*
* REGISTER_AND_MOVE uses a different SA parameter list containing * REGISTER_AND_MOVE uses a different SA parameter list containing
* SCSI TransportIDs. * SCSI TransportIDs.
@ -3734,10 +3717,7 @@ core_scsi3_pri_read_keys(struct se_cmd *cmd)
if (!buf) if (!buf)
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); put_unaligned_be32(dev->t10_pr.pr_generation, buf);
buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
buf[3] = (dev->t10_pr.pr_generation & 0xff);
spin_lock(&dev->t10_pr.registration_lock); spin_lock(&dev->t10_pr.registration_lock);
list_for_each_entry(pr_reg, &dev->t10_pr.registration_list, list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
@ -3749,23 +3729,13 @@ core_scsi3_pri_read_keys(struct se_cmd *cmd)
if ((add_len + 8) > (cmd->data_length - 8)) if ((add_len + 8) > (cmd->data_length - 8))
break; break;
buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); off += 8;
buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
buf[off++] = (pr_reg->pr_res_key & 0xff);
add_len += 8; add_len += 8;
} }
spin_unlock(&dev->t10_pr.registration_lock); spin_unlock(&dev->t10_pr.registration_lock);
buf[4] = ((add_len >> 24) & 0xff); put_unaligned_be32(add_len, &buf[4]);
buf[5] = ((add_len >> 16) & 0xff);
buf[6] = ((add_len >> 8) & 0xff);
buf[7] = (add_len & 0xff);
transport_kunmap_data_sg(cmd); transport_kunmap_data_sg(cmd);
@ -3796,10 +3766,7 @@ core_scsi3_pri_read_reservation(struct se_cmd *cmd)
if (!buf) if (!buf)
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
buf[3] = (dev->t10_pr.pr_generation & 0xff);
spin_lock(&dev->dev_reservation_lock); spin_lock(&dev->dev_reservation_lock);
pr_reg = dev->dev_pr_res_holder; pr_reg = dev->dev_pr_res_holder;
@ -3807,10 +3774,7 @@ core_scsi3_pri_read_reservation(struct se_cmd *cmd)
/* /*
* Set the hardcoded Additional Length * Set the hardcoded Additional Length
*/ */
buf[4] = ((add_len >> 24) & 0xff); put_unaligned_be32(add_len, &buf[4]);
buf[5] = ((add_len >> 16) & 0xff);
buf[6] = ((add_len >> 8) & 0xff);
buf[7] = (add_len & 0xff);
if (cmd->data_length < 22) if (cmd->data_length < 22)
goto err; goto err;
@ -3837,14 +3801,7 @@ core_scsi3_pri_read_reservation(struct se_cmd *cmd)
else else
pr_res_key = pr_reg->pr_res_key; pr_res_key = pr_reg->pr_res_key;
buf[8] = ((pr_res_key >> 56) & 0xff); put_unaligned_be64(pr_res_key, &buf[8]);
buf[9] = ((pr_res_key >> 48) & 0xff);
buf[10] = ((pr_res_key >> 40) & 0xff);
buf[11] = ((pr_res_key >> 32) & 0xff);
buf[12] = ((pr_res_key >> 24) & 0xff);
buf[13] = ((pr_res_key >> 16) & 0xff);
buf[14] = ((pr_res_key >> 8) & 0xff);
buf[15] = (pr_res_key & 0xff);
/* /*
* Set the SCOPE and TYPE * Set the SCOPE and TYPE
*/ */
@ -3882,8 +3839,7 @@ core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
if (!buf) if (!buf)
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
buf[0] = ((add_len >> 8) & 0xff); put_unaligned_be16(add_len, &buf[0]);
buf[1] = (add_len & 0xff);
buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */ buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */ buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */ buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
@ -3947,10 +3903,7 @@ core_scsi3_pri_read_full_status(struct se_cmd *cmd)
if (!buf) if (!buf)
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff); put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
buf[3] = (dev->t10_pr.pr_generation & 0xff);
spin_lock(&dev->dev_reservation_lock); spin_lock(&dev->dev_reservation_lock);
if (dev->dev_pr_res_holder) { if (dev->dev_pr_res_holder) {
@ -3992,14 +3945,8 @@ core_scsi3_pri_read_full_status(struct se_cmd *cmd)
/* /*
* Set RESERVATION KEY * Set RESERVATION KEY
*/ */
buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff); put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff); off += 8;
buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
buf[off++] = (pr_reg->pr_res_key & 0xff);
off += 4; /* Skip Over Reserved area */ off += 4; /* Skip Over Reserved area */
/* /*
@ -4041,8 +3988,8 @@ core_scsi3_pri_read_full_status(struct se_cmd *cmd)
if (!pr_reg->pr_reg_all_tg_pt) { if (!pr_reg->pr_reg_all_tg_pt) {
u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi; u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
buf[off++] = ((sep_rtpi >> 8) & 0xff); put_unaligned_be16(sep_rtpi, &buf[off]);
buf[off++] = (sep_rtpi & 0xff); off += 2;
} else } else
off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */ off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
@ -4062,10 +4009,7 @@ core_scsi3_pri_read_full_status(struct se_cmd *cmd)
/* /*
* Set the ADDITIONAL DESCRIPTOR LENGTH * Set the ADDITIONAL DESCRIPTOR LENGTH
*/ */
buf[off++] = ((desc_len >> 24) & 0xff); put_unaligned_be32(desc_len, &buf[off]);
buf[off++] = ((desc_len >> 16) & 0xff);
buf[off++] = ((desc_len >> 8) & 0xff);
buf[off++] = (desc_len & 0xff);
/* /*
* Size of full desctipor header minus TransportID * Size of full desctipor header minus TransportID
* containing $FABRIC_MOD specific) initiator device/port * containing $FABRIC_MOD specific) initiator device/port
@ -4082,10 +4026,7 @@ core_scsi3_pri_read_full_status(struct se_cmd *cmd)
/* /*
* Set ADDITIONAL_LENGTH * Set ADDITIONAL_LENGTH
*/ */
buf[4] = ((add_len >> 24) & 0xff); put_unaligned_be32(add_len, &buf[4]);
buf[5] = ((add_len >> 16) & 0xff);
buf[6] = ((add_len >> 8) & 0xff);
buf[7] = (add_len & 0xff);
transport_kunmap_data_sg(cmd); transport_kunmap_data_sg(cmd);

Просмотреть файл

@ -168,7 +168,7 @@ static void pscsi_tape_read_blocksize(struct se_device *dev,
/* /*
* If MODE_SENSE still returns zero, set the default value to 1024. * If MODE_SENSE still returns zero, set the default value to 1024.
*/ */
sdev->sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]); sdev->sector_size = get_unaligned_be24(&buf[9]);
out_free: out_free:
if (!sdev->sector_size) if (!sdev->sector_size)
sdev->sector_size = 1024; sdev->sector_size = 1024;
@ -209,8 +209,7 @@ pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
cdb[0] = INQUIRY; cdb[0] = INQUIRY;
cdb[1] = 0x01; /* Query VPD */ cdb[1] = 0x01; /* Query VPD */
cdb[2] = 0x80; /* Unit Serial Number */ cdb[2] = 0x80; /* Unit Serial Number */
cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff; put_unaligned_be16(INQUIRY_VPD_SERIAL_LEN, &cdb[3]);
cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL); INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
@ -245,8 +244,7 @@ pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
cdb[0] = INQUIRY; cdb[0] = INQUIRY;
cdb[1] = 0x01; /* Query VPD */ cdb[1] = 0x01; /* Query VPD */
cdb[2] = 0x83; /* Device Identifier */ cdb[2] = 0x83; /* Device Identifier */
cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff; put_unaligned_be16(INQUIRY_VPD_DEVICE_IDENTIFIER_LEN, &cdb[3]);
cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
INQUIRY_VPD_DEVICE_IDENTIFIER_LEN, INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
@ -254,7 +252,7 @@ pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
if (ret) if (ret)
goto out; goto out;
page_len = (buf[2] << 8) | buf[3]; page_len = get_unaligned_be16(&buf[2]);
while (page_len > 0) { while (page_len > 0) {
/* Grab a pointer to the Identification descriptor */ /* Grab a pointer to the Identification descriptor */
page_83 = &buf[off]; page_83 = &buf[off];
@ -669,19 +667,17 @@ after_mode_sense:
} }
if (cdb[0] == MODE_SELECT) if (cdb[0] == MODE_SELECT)
bdl = (buf[3]); bdl = buf[3];
else else
bdl = (buf[6] << 8) | (buf[7]); bdl = get_unaligned_be16(&buf[6]);
if (!bdl) if (!bdl)
goto after_mode_select; goto after_mode_select;
if (cdb[0] == MODE_SELECT) if (cdb[0] == MODE_SELECT)
blocksize = (buf[9] << 16) | (buf[10] << 8) | blocksize = get_unaligned_be24(&buf[9]);
(buf[11]);
else else
blocksize = (buf[13] << 16) | (buf[14] << 8) | blocksize = get_unaligned_be24(&buf[13]);
(buf[15]);
sd->sector_size = blocksize; sd->sector_size = blocksize;
} }

Просмотреть файл

@ -71,14 +71,8 @@ sbc_emulate_readcapacity(struct se_cmd *cmd)
else else
blocks = (u32)blocks_long; blocks = (u32)blocks_long;
buf[0] = (blocks >> 24) & 0xff; put_unaligned_be32(blocks, &buf[0]);
buf[1] = (blocks >> 16) & 0xff; put_unaligned_be32(dev->dev_attrib.block_size, &buf[4]);
buf[2] = (blocks >> 8) & 0xff;
buf[3] = blocks & 0xff;
buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff;
buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff;
buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff;
buf[7] = dev->dev_attrib.block_size & 0xff;
rbuf = transport_kmap_data_sg(cmd); rbuf = transport_kmap_data_sg(cmd);
if (rbuf) { if (rbuf) {
@ -102,18 +96,8 @@ sbc_emulate_readcapacity_16(struct se_cmd *cmd)
unsigned long long blocks = dev->transport->get_blocks(dev); unsigned long long blocks = dev->transport->get_blocks(dev);
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
buf[0] = (blocks >> 56) & 0xff; put_unaligned_be64(blocks, &buf[0]);
buf[1] = (blocks >> 48) & 0xff; put_unaligned_be32(dev->dev_attrib.block_size, &buf[8]);
buf[2] = (blocks >> 40) & 0xff;
buf[3] = (blocks >> 32) & 0xff;
buf[4] = (blocks >> 24) & 0xff;
buf[5] = (blocks >> 16) & 0xff;
buf[6] = (blocks >> 8) & 0xff;
buf[7] = blocks & 0xff;
buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff;
buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff;
buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff;
buf[11] = dev->dev_attrib.block_size & 0xff;
/* /*
* Set P_TYPE and PROT_EN bits for DIF support * Set P_TYPE and PROT_EN bits for DIF support
*/ */
@ -134,8 +118,8 @@ sbc_emulate_readcapacity_16(struct se_cmd *cmd)
if (dev->transport->get_alignment_offset_lbas) { if (dev->transport->get_alignment_offset_lbas) {
u16 lalba = dev->transport->get_alignment_offset_lbas(dev); u16 lalba = dev->transport->get_alignment_offset_lbas(dev);
buf[14] = (lalba >> 8) & 0x3f;
buf[15] = lalba & 0xff; put_unaligned_be16(lalba, &buf[14]);
} }
/* /*
@ -262,18 +246,17 @@ static inline u32 transport_get_sectors_6(unsigned char *cdb)
static inline u32 transport_get_sectors_10(unsigned char *cdb) static inline u32 transport_get_sectors_10(unsigned char *cdb)
{ {
return (u32)(cdb[7] << 8) + cdb[8]; return get_unaligned_be16(&cdb[7]);
} }
static inline u32 transport_get_sectors_12(unsigned char *cdb) static inline u32 transport_get_sectors_12(unsigned char *cdb)
{ {
return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; return get_unaligned_be32(&cdb[6]);
} }
static inline u32 transport_get_sectors_16(unsigned char *cdb) static inline u32 transport_get_sectors_16(unsigned char *cdb)
{ {
return (u32)(cdb[10] << 24) + (cdb[11] << 16) + return get_unaligned_be32(&cdb[10]);
(cdb[12] << 8) + cdb[13];
} }
/* /*
@ -281,29 +264,23 @@ static inline u32 transport_get_sectors_16(unsigned char *cdb)
*/ */
static inline u32 transport_get_sectors_32(unsigned char *cdb) static inline u32 transport_get_sectors_32(unsigned char *cdb)
{ {
return (u32)(cdb[28] << 24) + (cdb[29] << 16) + return get_unaligned_be32(&cdb[28]);
(cdb[30] << 8) + cdb[31];
} }
static inline u32 transport_lba_21(unsigned char *cdb) static inline u32 transport_lba_21(unsigned char *cdb)
{ {
return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; return get_unaligned_be24(&cdb[1]) & 0x1fffff;
} }
static inline u32 transport_lba_32(unsigned char *cdb) static inline u32 transport_lba_32(unsigned char *cdb)
{ {
return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; return get_unaligned_be32(&cdb[2]);
} }
static inline unsigned long long transport_lba_64(unsigned char *cdb) static inline unsigned long long transport_lba_64(unsigned char *cdb)
{ {
unsigned int __v1, __v2; return get_unaligned_be64(&cdb[2]);
__v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
__v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
} }
/* /*
@ -311,12 +288,7 @@ static inline unsigned long long transport_lba_64(unsigned char *cdb)
*/ */
static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
{ {
unsigned int __v1, __v2; return get_unaligned_be64(&cdb[12]);
__v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
__v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
} }
static sense_reason_t static sense_reason_t
@ -1051,8 +1023,7 @@ sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
cmd->t_task_cdb[1] & 0x1f); cmd->t_task_cdb[1] & 0x1f);
return TCM_INVALID_CDB_FIELD; return TCM_INVALID_CDB_FIELD;
} }
size = (cdb[10] << 24) | (cdb[11] << 16) | size = get_unaligned_be32(&cdb[10]);
(cdb[12] << 8) | cdb[13];
break; break;
case SYNCHRONIZE_CACHE: case SYNCHRONIZE_CACHE:
case SYNCHRONIZE_CACHE_16: case SYNCHRONIZE_CACHE_16:

Просмотреть файл

@ -287,8 +287,8 @@ check_t10_vend_desc:
/* Skip over Obsolete field in RTPI payload /* Skip over Obsolete field in RTPI payload
* in Table 472 */ * in Table 472 */
off += 2; off += 2;
buf[off++] = ((lun->lun_rtpi >> 8) & 0xff); put_unaligned_be16(lun->lun_rtpi, &buf[off]);
buf[off++] = (lun->lun_rtpi & 0xff); off += 2;
len += 8; /* Header size + Designation descriptor */ len += 8; /* Header size + Designation descriptor */
/* /*
* Target port group identifier, see spc4r17 * Target port group identifier, see spc4r17
@ -316,8 +316,8 @@ check_t10_vend_desc:
off++; /* Skip over Reserved */ off++; /* Skip over Reserved */
buf[off++] = 4; /* DESIGNATOR LENGTH */ buf[off++] = 4; /* DESIGNATOR LENGTH */
off += 2; /* Skip over Reserved Field */ off += 2; /* Skip over Reserved Field */
buf[off++] = ((tg_pt_gp_id >> 8) & 0xff); put_unaligned_be16(tg_pt_gp_id, &buf[off]);
buf[off++] = (tg_pt_gp_id & 0xff); off += 2;
len += 8; /* Header size + Designation descriptor */ len += 8; /* Header size + Designation descriptor */
/* /*
* Logical Unit Group identifier, see spc4r17 * Logical Unit Group identifier, see spc4r17
@ -343,8 +343,8 @@ check_lu_gp:
off++; /* Skip over Reserved */ off++; /* Skip over Reserved */
buf[off++] = 4; /* DESIGNATOR LENGTH */ buf[off++] = 4; /* DESIGNATOR LENGTH */
off += 2; /* Skip over Reserved Field */ off += 2; /* Skip over Reserved Field */
buf[off++] = ((lu_gp_id >> 8) & 0xff); put_unaligned_be16(lu_gp_id, &buf[off]);
buf[off++] = (lu_gp_id & 0xff); off += 2;
len += 8; /* Header size + Designation descriptor */ len += 8; /* Header size + Designation descriptor */
/* /*
* SCSI name string designator, see spc4r17 * SCSI name string designator, see spc4r17
@ -431,8 +431,7 @@ check_scsi_name:
/* Header size + Designation descriptor */ /* Header size + Designation descriptor */
len += (scsi_target_len + 4); len += (scsi_target_len + 4);
} }
buf[2] = ((len >> 8) & 0xff); put_unaligned_be16(len, &buf[2]); /* Page Length for VPD 0x83 */
buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
return 0; return 0;
} }
EXPORT_SYMBOL(spc_emulate_evpd_83); EXPORT_SYMBOL(spc_emulate_evpd_83);
@ -1288,7 +1287,7 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
cmd->execute_cmd = spc_emulate_modeselect; cmd->execute_cmd = spc_emulate_modeselect;
break; break;
case MODE_SELECT_10: case MODE_SELECT_10:
*size = (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be16(&cdb[7]);
cmd->execute_cmd = spc_emulate_modeselect; cmd->execute_cmd = spc_emulate_modeselect;
break; break;
case MODE_SENSE: case MODE_SENSE:
@ -1296,25 +1295,25 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
cmd->execute_cmd = spc_emulate_modesense; cmd->execute_cmd = spc_emulate_modesense;
break; break;
case MODE_SENSE_10: case MODE_SENSE_10:
*size = (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be16(&cdb[7]);
cmd->execute_cmd = spc_emulate_modesense; cmd->execute_cmd = spc_emulate_modesense;
break; break;
case LOG_SELECT: case LOG_SELECT:
case LOG_SENSE: case LOG_SENSE:
*size = (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be16(&cdb[7]);
break; break;
case PERSISTENT_RESERVE_IN: case PERSISTENT_RESERVE_IN:
*size = (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be16(&cdb[7]);
cmd->execute_cmd = target_scsi3_emulate_pr_in; cmd->execute_cmd = target_scsi3_emulate_pr_in;
break; break;
case PERSISTENT_RESERVE_OUT: case PERSISTENT_RESERVE_OUT:
*size = (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be16(&cdb[7]);
cmd->execute_cmd = target_scsi3_emulate_pr_out; cmd->execute_cmd = target_scsi3_emulate_pr_out;
break; break;
case RELEASE: case RELEASE:
case RELEASE_10: case RELEASE_10:
if (cdb[0] == RELEASE_10) if (cdb[0] == RELEASE_10)
*size = (cdb[7] << 8) | cdb[8]; *size = get_unaligned_be16(&cdb[7]);
else else
*size = cmd->data_length; *size = cmd->data_length;
@ -1327,7 +1326,7 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
* Assume the passthrough or $FABRIC_MOD will tell us about it. * Assume the passthrough or $FABRIC_MOD will tell us about it.
*/ */
if (cdb[0] == RESERVE_10) if (cdb[0] == RESERVE_10)
*size = (cdb[7] << 8) | cdb[8]; *size = get_unaligned_be16(&cdb[7]);
else else
*size = cmd->data_length; *size = cmd->data_length;
@ -1338,7 +1337,7 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
cmd->execute_cmd = spc_emulate_request_sense; cmd->execute_cmd = spc_emulate_request_sense;
break; break;
case INQUIRY: case INQUIRY:
*size = (cdb[3] << 8) + cdb[4]; *size = get_unaligned_be16(&cdb[3]);
/* /*
* Do implicit HEAD_OF_QUEUE processing for INQUIRY. * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
@ -1349,7 +1348,7 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
break; break;
case SECURITY_PROTOCOL_IN: case SECURITY_PROTOCOL_IN:
case SECURITY_PROTOCOL_OUT: case SECURITY_PROTOCOL_OUT:
*size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; *size = get_unaligned_be32(&cdb[6]);
break; break;
case EXTENDED_COPY: case EXTENDED_COPY:
*size = get_unaligned_be32(&cdb[10]); *size = get_unaligned_be32(&cdb[10]);
@ -1361,19 +1360,18 @@ spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
break; break;
case READ_ATTRIBUTE: case READ_ATTRIBUTE:
case WRITE_ATTRIBUTE: case WRITE_ATTRIBUTE:
*size = (cdb[10] << 24) | (cdb[11] << 16) | *size = get_unaligned_be32(&cdb[10]);
(cdb[12] << 8) | cdb[13];
break; break;
case RECEIVE_DIAGNOSTIC: case RECEIVE_DIAGNOSTIC:
case SEND_DIAGNOSTIC: case SEND_DIAGNOSTIC:
*size = (cdb[3] << 8) | cdb[4]; *size = get_unaligned_be16(&cdb[3]);
break; break;
case WRITE_BUFFER: case WRITE_BUFFER:
*size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; *size = get_unaligned_be24(&cdb[6]);
break; break;
case REPORT_LUNS: case REPORT_LUNS:
cmd->execute_cmd = spc_emulate_report_luns; cmd->execute_cmd = spc_emulate_report_luns;
*size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; *size = get_unaligned_be32(&cdb[6]);
/* /*
* Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
* See spc4r17 section 5.3 * See spc4r17 section 5.3

Просмотреть файл

@ -311,9 +311,7 @@ static int target_xcopy_parse_segdesc_02(struct se_cmd *se_cmd, struct xcopy_op
(unsigned long long)xop->dst_lba); (unsigned long long)xop->dst_lba);
if (dc != 0) { if (dc != 0) {
xop->dbl = (desc[29] & 0xff) << 16; xop->dbl = get_unaligned_be24(&desc[29]);
xop->dbl |= (desc[30] & 0xff) << 8;
xop->dbl |= desc[31] & 0xff;
pr_debug("XCOPY seg desc 0x02: DC=1 w/ dbl: %u\n", xop->dbl); pr_debug("XCOPY seg desc 0x02: DC=1 w/ dbl: %u\n", xop->dbl);
} }

Просмотреть файл

@ -2,6 +2,7 @@
#define TARGET_CORE_BACKEND_H #define TARGET_CORE_BACKEND_H
#include <linux/types.h> #include <linux/types.h>
#include <asm/unaligned.h>
#include <target/target_core_base.h> #include <target/target_core_base.h>
#define TRANSPORT_FLAG_PASSTHROUGH 0x1 #define TRANSPORT_FLAG_PASSTHROUGH 0x1
@ -109,4 +110,11 @@ sector_t target_to_linux_sector(struct se_device *dev, sector_t lb);
bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib, bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
struct request_queue *q); struct request_queue *q);
/* Only use get_unaligned_be24() if reading p - 1 is allowed. */
static inline uint32_t get_unaligned_be24(const uint8_t *const p)
{
return get_unaligned_be32(p - 1) & 0xffffffU;
}
#endif /* TARGET_CORE_BACKEND_H */ #endif /* TARGET_CORE_BACKEND_H */