Skip to content

Commit 5795eb4

Browse files
morbidrsaaxboe
authored andcommitted
scsi: sd_zbc: emulate ZONE_APPEND commands
Emulate ZONE_APPEND for SCSI disks using a regular WRITE(16) command with a start LBA set to the target zone write pointer position. In order to always know the write pointer position of a sequential write zone, the write pointer of all zones is tracked using an array of 32bits zone write pointer offset attached to the scsi disk structure. Each entry of the array indicate a zone write pointer position relative to the zone start sector. The write pointer offsets are maintained in sync with the device as follows: 1) the write pointer offset of a zone is reset to 0 when a REQ_OP_ZONE_RESET command completes. 2) the write pointer offset of a zone is set to the zone size when a REQ_OP_ZONE_FINISH command completes. 3) the write pointer offset of a zone is incremented by the number of 512B sectors written when a write, write same or a zone append command completes. 4) the write pointer offset of all zones is reset to 0 when a REQ_OP_ZONE_RESET_ALL command completes. Since the block layer does not write lock zones for zone append commands, to ensure a sequential ordering of the regular write commands used for the emulation, the target zone of a zone append command is locked when the function sd_zbc_prepare_zone_append() is called from sd_setup_read_write_cmnd(). If the zone write lock cannot be obtained (e.g. a zone append is in-flight or a regular write has already locked the zone), the zone append command dispatching is delayed by returning BLK_STS_ZONE_RESOURCE. To avoid the need for write locking all zones for REQ_OP_ZONE_RESET_ALL requests, use a spinlock to protect accesses and modifications of the zone write pointer offsets. This spinlock is initialized from sd_probe() using the new function sd_zbc_init(). Co-developed-by: Damien Le Moal <[email protected]> Signed-off-by: Johannes Thumshirn <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 02494d3 commit 5795eb4

File tree

3 files changed

+392
-30
lines changed

3 files changed

+392
-30
lines changed

drivers/scsi/sd.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,12 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
12061206
}
12071207
}
12081208

1209+
if (req_op(rq) == REQ_OP_ZONE_APPEND) {
1210+
ret = sd_zbc_prepare_zone_append(cmd, &lba, nr_blocks);
1211+
if (ret)
1212+
return ret;
1213+
}
1214+
12091215
fua = rq->cmd_flags & REQ_FUA ? 0x8 : 0;
12101216
dix = scsi_prot_sg_count(cmd);
12111217
dif = scsi_host_dif_capable(cmd->device->host, sdkp->protection_type);
@@ -1287,6 +1293,7 @@ static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
12871293
return sd_setup_flush_cmnd(cmd);
12881294
case REQ_OP_READ:
12891295
case REQ_OP_WRITE:
1296+
case REQ_OP_ZONE_APPEND:
12901297
return sd_setup_read_write_cmnd(cmd);
12911298
case REQ_OP_ZONE_RESET:
12921299
return sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,
@@ -2055,7 +2062,7 @@ static int sd_done(struct scsi_cmnd *SCpnt)
20552062

20562063
out:
20572064
if (sd_is_zoned(sdkp))
2058-
sd_zbc_complete(SCpnt, good_bytes, &sshdr);
2065+
good_bytes = sd_zbc_complete(SCpnt, good_bytes, &sshdr);
20592066

20602067
SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
20612068
"sd_done: completed %d of %d bytes\n",
@@ -3372,6 +3379,10 @@ static int sd_probe(struct device *dev)
33723379
sdkp->first_scan = 1;
33733380
sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
33743381

3382+
error = sd_zbc_init_disk(sdkp);
3383+
if (error)
3384+
goto out_free_index;
3385+
33753386
sd_revalidate_disk(gd);
33763387

33773388
gd->flags = GENHD_FL_EXT_DEVT;
@@ -3409,6 +3420,7 @@ static int sd_probe(struct device *dev)
34093420
out_put:
34103421
put_disk(gd);
34113422
out_free:
3423+
sd_zbc_release_disk(sdkp);
34123424
kfree(sdkp);
34133425
out:
34143426
scsi_autopm_put_device(sdp);
@@ -3485,6 +3497,8 @@ static void scsi_disk_release(struct device *dev)
34853497
put_disk(disk);
34863498
put_device(&sdkp->device->sdev_gendev);
34873499

3500+
sd_zbc_release_disk(sdkp);
3501+
34883502
kfree(sdkp);
34893503
}
34903504

drivers/scsi/sd.h

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ struct scsi_disk {
7979
u32 zones_optimal_open;
8080
u32 zones_optimal_nonseq;
8181
u32 zones_max_open;
82+
u32 *zones_wp_offset;
83+
spinlock_t zones_wp_offset_lock;
84+
u32 *rev_wp_offset;
85+
struct mutex rev_mutex;
86+
struct work_struct zone_wp_offset_work;
87+
char *zone_wp_update_buf;
8288
#endif
8389
atomic_t openers;
8490
sector_t capacity; /* size in logical blocks */
@@ -207,17 +213,35 @@ static inline int sd_is_zoned(struct scsi_disk *sdkp)
207213

208214
#ifdef CONFIG_BLK_DEV_ZONED
209215

216+
int sd_zbc_init_disk(struct scsi_disk *sdkp);
217+
void sd_zbc_release_disk(struct scsi_disk *sdkp);
210218
extern int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buffer);
211219
extern void sd_zbc_print_zones(struct scsi_disk *sdkp);
212220
blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
213221
unsigned char op, bool all);
214-
extern void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
215-
struct scsi_sense_hdr *sshdr);
222+
unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
223+
struct scsi_sense_hdr *sshdr);
216224
int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
217225
unsigned int nr_zones, report_zones_cb cb, void *data);
218226

227+
blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
228+
unsigned int nr_blocks);
229+
219230
#else /* CONFIG_BLK_DEV_ZONED */
220231

232+
static inline int sd_zbc_init(void)
233+
{
234+
return 0;
235+
}
236+
237+
static inline int sd_zbc_init_disk(struct scsi_disk *sdkp)
238+
{
239+
return 0;
240+
}
241+
242+
static inline void sd_zbc_exit(void) {}
243+
static inline void sd_zbc_release_disk(struct scsi_disk *sdkp) {}
244+
221245
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
222246
unsigned char *buf)
223247
{
@@ -233,9 +257,18 @@ static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
233257
return BLK_STS_TARGET;
234258
}
235259

236-
static inline void sd_zbc_complete(struct scsi_cmnd *cmd,
237-
unsigned int good_bytes,
238-
struct scsi_sense_hdr *sshdr) {}
260+
static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,
261+
unsigned int good_bytes, struct scsi_sense_hdr *sshdr)
262+
{
263+
return 0;
264+
}
265+
266+
static inline blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd,
267+
sector_t *lba,
268+
unsigned int nr_blocks)
269+
{
270+
return BLK_STS_TARGET;
271+
}
239272

240273
#define sd_zbc_report_zones NULL
241274

0 commit comments

Comments
 (0)