scsi: target/core: Use sg_alloc_table() instead of open-coding it

The purpose of sg_alloc_table() is to allocate and initialize an
sg-list. Use that function instead of open-coding it. This patch will
make it easier to share code for caching sg-list allocations between the
SCSI and NVMe target cores.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Nicholas Bellinger <nab@linux-iscsi.org>
Cc: Mike Christie <mchristi@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Hannes Reinecke <hare@suse.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
This commit is contained in:
Bart Van Assche 2018-10-15 08:51:38 -07:00 committed by Martin K. Petersen
parent 80b045b385
commit 81b6ca6dba
1 changed files with 5 additions and 6 deletions

View File

@ -447,7 +447,8 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
int *post_ret) int *post_ret)
{ {
struct se_device *dev = cmd->se_dev; struct se_device *dev = cmd->se_dev;
struct scatterlist *write_sg = NULL, *sg; struct sg_table write_tbl = { };
struct scatterlist *write_sg, *sg;
unsigned char *buf = NULL, *addr; unsigned char *buf = NULL, *addr;
struct sg_mapping_iter m; struct sg_mapping_iter m;
unsigned int offset = 0, len; unsigned int offset = 0, len;
@ -488,14 +489,12 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
goto out; goto out;
} }
write_sg = kmalloc_array(cmd->t_data_nents, sizeof(*write_sg), if (sg_alloc_table(&write_tbl, cmd->t_data_nents, GFP_KERNEL) < 0) {
GFP_KERNEL);
if (!write_sg) {
pr_err("Unable to allocate compare_and_write sg\n"); pr_err("Unable to allocate compare_and_write sg\n");
ret = TCM_OUT_OF_RESOURCES; ret = TCM_OUT_OF_RESOURCES;
goto out; goto out;
} }
sg_init_table(write_sg, cmd->t_data_nents); write_sg = write_tbl.sgl;
/* /*
* Setup verify and write data payloads from total NumberLBAs. * Setup verify and write data payloads from total NumberLBAs.
*/ */
@ -591,7 +590,7 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
* sbc_compare_and_write() before the original READ I/O submission. * sbc_compare_and_write() before the original READ I/O submission.
*/ */
up(&dev->caw_sem); up(&dev->caw_sem);
kfree(write_sg); sg_free_table(&write_tbl);
kfree(buf); kfree(buf);
return ret; return ret;
} }