Kernel SCSI driver - how to get a struct block_device given a struct scsi_target?
by monomial from LinuxQuestions.org on (#6NRP8)
I am working with kernel 6.1.41 and trying to get a block device associated with a given struct scsi_target. Specifically, all I really need is the name of the primary block device. This must be possible somehow, as from user space I can list:
ls /sys/bus/scsi/devices/target1:0:0/1:0:0:0/block
and it will show me the block device node. So sysfs at least knows how to do this.
However, inside the kernel, given the struct scsi_target that represents 1:0:0:0, I can't figure out how to recover this information.
I understand how to go the other direction, from a block device to the scsi_target. Given a struct block_device *bdev, I can simply do:
Code:struct gendisk *disk = bdev->bd_disk;
struct scsi_disk *sdkp = scsi_disk(disk);
struct scsi_device *sdp = sdkp->device;
struct scsi_target *starget = sdp->sdev_target;But I just can't figure out how to work it in reverse. Can anyone offer a suggestion on how to do this? I get that not every scsi_target can map to a block_device, but in this case I know it does, and sysfs apparently knows it does as well. The question is how?
Thank you for any assistance.
ls /sys/bus/scsi/devices/target1:0:0/1:0:0:0/block
and it will show me the block device node. So sysfs at least knows how to do this.
However, inside the kernel, given the struct scsi_target that represents 1:0:0:0, I can't figure out how to recover this information.
I understand how to go the other direction, from a block device to the scsi_target. Given a struct block_device *bdev, I can simply do:
Code:struct gendisk *disk = bdev->bd_disk;
struct scsi_disk *sdkp = scsi_disk(disk);
struct scsi_device *sdp = sdkp->device;
struct scsi_target *starget = sdp->sdev_target;But I just can't figure out how to work it in reverse. Can anyone offer a suggestion on how to do this? I get that not every scsi_target can map to a block_device, but in this case I know it does, and sysfs apparently knows it does as well. The question is how?
Thank you for any assistance.