Skip to content

Commit

Permalink
scsi: don't use disk->private_data to find the scsi_driver
Browse files Browse the repository at this point in the history
Requiring every ULP to have the scsi_drive as first member of the
private data is rather fragile and not necessary anyway.  Just use
the driver hanging off the SCSI device instead.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Link: https://lore.kernel.org/r/20220308055200.735835-4-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Christoph Hellwig authored and Jens Axboe committed Mar 9, 2022
1 parent e02657e commit e7f7655
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 21 deletions.
3 changes: 1 addition & 2 deletions drivers/scsi/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3515,7 +3515,6 @@ static int sd_probe(struct device *dev)
}

sdkp->device = sdp;
sdkp->driver = &sd_template;
sdkp->disk = gd;
sdkp->index = index;
sdkp->max_retries = SD_MAX_RETRIES;
Expand Down Expand Up @@ -3548,7 +3547,7 @@ static int sd_probe(struct device *dev)
gd->minors = SD_MINORS;

gd->fops = &sd_fops;
gd->private_data = &sdkp->driver;
gd->private_data = sdkp;

/* defaults, until the device tells us otherwise */
sdp->sector_size = 512;
Expand Down
3 changes: 1 addition & 2 deletions drivers/scsi/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ enum {
};

struct scsi_disk {
struct scsi_driver *driver; /* always &sd_template */
struct scsi_device *device;
struct device dev;
struct gendisk *disk;
Expand Down Expand Up @@ -131,7 +130,7 @@ struct scsi_disk {

static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
{
return container_of(disk->private_data, struct scsi_disk, driver);
return disk->private_data;
}

#define sd_printk(prefix, sdsk, fmt, a...) \
Expand Down
5 changes: 2 additions & 3 deletions drivers/scsi/sr.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static void sr_kref_release(struct kref *kref);

static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
{
return container_of(disk->private_data, struct scsi_cd, driver);
return disk->private_data;
}

static int sr_runtime_suspend(struct device *dev)
Expand Down Expand Up @@ -692,7 +692,6 @@ static int sr_probe(struct device *dev)

cd->device = sdev;
cd->disk = disk;
cd->driver = &sr_template;
cd->capacity = 0x1fffff;
cd->device->changed = 1; /* force recheck CD type */
cd->media_present = 1;
Expand All @@ -713,7 +712,7 @@ static int sr_probe(struct device *dev)
sr_vendor_init(cd);

set_capacity(disk, cd->capacity);
disk->private_data = &cd->driver;
disk->private_data = cd;

if (register_cdrom(disk, &cd->cdi))
goto fail_minor;
Expand Down
1 change: 0 additions & 1 deletion drivers/scsi/sr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct scsi_device;


typedef struct scsi_cd {
struct scsi_driver *driver;
unsigned capacity; /* size in blocks */
struct scsi_device *device;
unsigned int vendor; /* vendor code, see sr_vendor.c */
Expand Down
1 change: 0 additions & 1 deletion drivers/scsi/st.c
Original file line number Diff line number Diff line change
Expand Up @@ -4276,7 +4276,6 @@ static int st_probe(struct device *dev)
goto out_buffer_free;
}
kref_init(&tpnt->kref);
tpnt->driver = &st_template;

tpnt->device = SDp;
if (SDp->scsi_level <= 2)
Expand Down
1 change: 0 additions & 1 deletion drivers/scsi/st.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ struct scsi_tape_stats {

/* The tape drive descriptor */
struct scsi_tape {
struct scsi_driver *driver;
struct scsi_device *device;
struct mutex lock; /* For serialization */
struct completion wait; /* For SCSI commands */
Expand Down
9 changes: 0 additions & 9 deletions include/scsi/scsi_cmnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <scsi/scsi_request.h>

struct Scsi_Host;
struct scsi_driver;

/*
* MAX_COMMAND_SIZE is:
Expand Down Expand Up @@ -159,14 +158,6 @@ static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
return cmd + 1;
}

/* make sure not to use it with passthrough commands */
static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
{
struct request *rq = scsi_cmd_to_rq(cmd);

return *(struct scsi_driver **)rq->q->disk->private_data;
}

void scsi_done(struct scsi_cmnd *cmd);

extern void scsi_finish_command(struct scsi_cmnd *cmd);
Expand Down
9 changes: 7 additions & 2 deletions include/scsi/scsi_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

#include <linux/blk_types.h>
#include <linux/device.h>
#include <scsi/scsi_cmnd.h>

struct module;
struct request;
struct scsi_cmnd;
struct scsi_device;

struct scsi_driver {
struct device_driver gendrv;
Expand All @@ -31,4 +30,10 @@ extern int scsi_register_interface(struct class_interface *);
#define scsi_unregister_interface(intf) \
class_interface_unregister(intf)

/* make sure not to use it with passthrough commands */
static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
{
return to_scsi_driver(cmd->device->sdev_gendev.driver);
}

#endif /* _SCSI_SCSI_DRIVER_H */

0 comments on commit e7f7655

Please sign in to comment.