Re: Help - SCSI passthrough ioctl calls vs. sd/sg devices
From: Josef Möllers (josef.moellers_at_fujitsu-siemens.com)
Date: Thu, 22 Apr 2004 09:28:10 +0200
> I would appreciate it if some one could clarify this. It's a bit confusing.
> <<The Linux SCSI Generic (sg) HOWTO>> page 2 says "The sg driver permits
> user applications to send SCSI commands to devices that understand them.
> SCSI commands are 6, 10, 12 or 16 bytes long. The SCSI disk driver (sd),
> once device initialization is complete, only sends SCSI READ and WRITE
> <<The Linux 2.4 SCSI subsystem HOWTO>> page 3 says "Sg is a SCSI command
> pass through device that uses a char device interface. General purpose
> Linux commands should not be used on sg devices."
> My questions are:
> - sounds like SCSI passthrough ioctl calls can only apply
> to sg devices. Is it right, or can we apply SCSI
> passthrough ioctl calls to sd devices as well?
Each SCSI device found by the SCSI mid-layer is assigned an sg device.
If it happens to be a Direct-Access device, it will also be assigned an
sd device, if it is a Sequential-Access device, it will also be assigned
an st device.
So: each sd device also has an associated sg device. As a rule of thumb,
I always scan /proc/scsi/scsi and "mentally" assign an sg device to each
SCSI device found and an sd device to each Direct-Access device found. I
was told that this algorithm is not generally safe, e.g. if you remove
and add devices by hand.
> - what are the so-called "General purpose Linux commands"?
> Are they read()/write() or 'mount' / 'dd' or something else?
A command is a command, e.g. cp, cat, dd, mount. You can dd from/to
/dev/sda but you cannot dd from/to /dev/sg1.
System calls are used to access sg devices, you'll have to open(2)
/dev/sg..., the use ioctl(2) (or write(2)/read(2) with the old
interface) to issue commands, the close(2) the device afterwards.
> What should we do if we need to 'mount' a file system on a disk and also
> need to issue SCSI passthrough ioctl calls to the same disk? Maybe we
> should use both sd and sg (i.e. mount the file system to /dev/sda1, and
> issue SCSI passthrough ioctl calls to /dev/sg0 that maps to the same
> disk/lun)? I was told 'mount' doesn't work on sg devices, right?
Right, as stated above.
You can issue SCSI commands to a SCSI disk using the appropriate
/dev/sg... However, you must be veeeery carefully not to confuse the
disk driver! Better first issue the generic command(s), then mount the disk.
-- Josef Möllers (Pinguinpfleger bei FSC) If failure had no penalty success would not be a prize -- T. Pratchett