1 From a1925624dff2f9cbf1551c652b0357ab0af5c918 Mon Sep 17 00:00:00 2001
2 From: Douglas Gilbert <dgilbert@interlog.com>
3 Date: Fri, 25 Jan 2019 12:46:09 -0500
4 Subject: scsi: scsi_debug: fix write_same with virtual_gb problem
6 [ Upstream commit 40d07b523cf434f252b134c86b1f8f2d907ffb0b ]
8 The WRITE SAME(10) and (16) implementations didn't take account of the
9 buffer wrap required when the virtual_gb parameter is greater than 0.
11 Fix that and rename the fake_store() function to lba2fake_store() to lessen
12 confusion with the global fake_storep pointer. Bump version date.
14 Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
15 Reported-by: Bart Van Assche <bvanassche@acm.org>
16 Tested by: Bart Van Assche <bvanassche@acm.org>
17 Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
18 Signed-off-by: Sasha Levin <sashal@kernel.org>
20 drivers/scsi/scsi_debug.c | 41 ++++++++++++++++++++-------------------
21 1 file changed, 21 insertions(+), 20 deletions(-)
23 diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
24 index 60bcc6df97a9..65305b3848bc 100644
25 --- a/drivers/scsi/scsi_debug.c
26 +++ b/drivers/scsi/scsi_debug.c
29 /* make sure inq_product_rev string corresponds to this version */
30 #define SDEBUG_VERSION "0188" /* format to fit INQUIRY revision field */
31 -static const char *sdebug_version_date = "20180128";
32 +static const char *sdebug_version_date = "20190125";
34 #define MY_NAME "scsi_debug"
36 @@ -735,7 +735,7 @@ static inline bool scsi_debug_lbp(void)
37 (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
40 -static void *fake_store(unsigned long long lba)
41 +static void *lba2fake_store(unsigned long long lba)
43 lba = do_div(lba, sdebug_store_sectors);
45 @@ -2514,8 +2514,8 @@ static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba,
49 -/* If fake_store(lba,num) compares equal to arr(num), then copy top half of
50 - * arr into fake_store(lba,num) and return true. If comparison fails then
51 +/* If lba2fake_store(lba,num) compares equal to arr(num), then copy top half of
52 + * arr into lba2fake_store(lba,num) and return true. If comparison fails then
54 static bool comp_write_worker(u64 lba, u32 num, const u8 *arr)
56 @@ -2643,7 +2643,7 @@ static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
57 if (sdt->app_tag == cpu_to_be16(0xffff))
60 - ret = dif_verify(sdt, fake_store(sector), sector, ei_lba);
61 + ret = dif_verify(sdt, lba2fake_store(sector), sector, ei_lba);
65 @@ -3261,10 +3261,12 @@ static int resp_write_scat(struct scsi_cmnd *scp,
66 static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
67 u32 ei_lba, bool unmap, bool ndob)
74 + u32 lb_size = sdebug_sector_size;
78 ret = check_device_access_params(scp, lba, num);
80 @@ -3276,31 +3278,30 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
81 unmap_region(lba, num);
85 - lba_off = lba * sdebug_sector_size;
87 + block = do_div(lbaa, sdebug_store_sectors);
88 /* if ndob then zero 1 logical block, else fetch 1 logical block */
89 + fs1p = fake_storep + (block * lb_size);
91 - memset(fake_storep + lba_off, 0, sdebug_sector_size);
92 + memset(fs1p, 0, lb_size);
95 - ret = fetch_to_dev_buffer(scp, fake_storep + lba_off,
96 - sdebug_sector_size);
97 + ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
100 write_unlock_irqrestore(&atomic_rw, iflags);
101 return DID_ERROR << 16;
102 - } else if (sdebug_verbose && !ndob && (ret < sdebug_sector_size))
103 + } else if (sdebug_verbose && !ndob && (ret < lb_size))
104 sdev_printk(KERN_INFO, scp->device,
105 "%s: %s: lb size=%u, IO sent=%d bytes\n",
106 - my_name, "write same",
107 - sdebug_sector_size, ret);
108 + my_name, "write same", lb_size, ret);
110 /* Copy first sector to remaining blocks */
111 - for (i = 1 ; i < num ; i++)
112 - memcpy(fake_storep + ((lba + i) * sdebug_sector_size),
113 - fake_storep + lba_off,
114 - sdebug_sector_size);
116 + for (i = 1 ; i < num ; i++) {
118 + block = do_div(lbaa, sdebug_store_sectors);
119 + memmove(fake_storep + (block * lb_size), fs1p, lb_size);
121 if (scsi_debug_lbp())
122 map_region(lba, num);