I don't think I understand you. Do you want to:

  a, map st_dev to devicename (e.g. /dev/sda3)
or
  b, map st_dev to scsi id and lun (e.g. id 1, lun 0)

--------------------
a, if you want the devicename, you (or the lib) have to scan the disc
since the kernel don't care about devicenames, it cares about major
and minor numbers and the map between maj/min <-> devicenam is in the
filesystem, in /dev directory (though they can be anywhere).

BTW, this is ~how ttyname(fd) does it:
  if ("/proc/self/fd/" + fd) { return(readlink());}
  stat(fd);
  while(readdir("/dev")) { /* or "/dev/pts" for sysv pty's */
    if (same_ino && same_st_dev) return(name);
  }

--------------------
b, if you want scsi id and lun, test this (some code stolen from
   scsidev):

#include <errno.h>
#include <fcntl.h>
#include <scsi/scsi.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main( int argc, char *argv[]) {
  int ix;
  int fd;
  int status;
  for (ix = 1; ix < argc; ix++) { /* don't test program name */
    errno = 0;
    fd = open(argv[ix], O_RDONLY | O_NONBLOCK );
    if (fd < 0) {
      perror("in open");
    } else {
      struct stat buf;
      status = fstat(fd, &buf);
      if (status < 0) perror("in stat");
      else {
        int id[2];
        status = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &id);
        if (status < 0) perror("in ioctl");
        else {
          int h_id    =  id[0] >> 24;
          int chan    = (id[0] >> 16) & 0xff;
          int lun     = (id[0] >> 8 ) & 0xff;
          int scsi_id =  id[0]        & 0xff;
          printf("host id: %d\nid[1]: %x\nchannel: %d\nscsi id: %d\nlun: %d\n", h_id, 
id[1], chan, scsi_id, lun);
        }
      }
      close(fd);
    }
  }
  return(0);
}

# /home/karl/Internt/prg/c/scsiidlun /dev/scd0 /dev/sdc1 /dev/sda5 
/home/ftp/welcome.msg 
host id: 24
id[1]: d800
channel: 0
scsi id: 6
lun: 0
host id: 24
id[1]: c000
channel: 0
scsi id: 2
lun: 0
host id: 24
id[1]: c000
channel: 0
scsi id: 0
lun: 0
in ioctl: Inappropriate ioctl for device
#

And, yes, you have to open the device file.

So, if you want scsi id/lun, use the beforementioned ioctl()

Regards,
/Karl

-----------------------------------------------------------------------
Karl Hammar                    Asp� Data           [EMAIL PROTECTED]
Lilla Asp� 2340             +46  173 140 57                    Networks
S-742 94 �sthammar         +46  70 511 97 84                  Computers
Sweden                                                       Consulting
-----------------------------------------------------------------------


From: BenHanokh Gabriel <[EMAIL PROTECTED]>
Subject: Re: finding target id of a scsi device
Date: Wed, 09 Aug 2000 11:47:49 +0300

> hello again
> 
> sorry for bothering you again, but i still don't see how to map from dev_t to the
> scsi device name ( or info ).
> do you mean i have to scan the directory made by scsidev for the right major/minor
> combo ?
> than why do i need scsidev, i can scan /dev/sd* as well? or even  scan /etc/fstab.
> 
> in 4.4BSD there is a syscall which translate from dev_t into device name
> char* devname( dev_t );
> do you know if such a call exists in linux?
> 
> for now i implemented my own version of devname scaning /etc/fstab with
> getmntent(3), but i feel extrimly bad about
> doing all this redundent work( the kernel must have done the same when opened my
> file )
> 
> THX
> /gabi
> 
> Karl Hammar wrote:
> 
> > Sorry, misunderstood you.
> > Ok, run scsidev once (hmm, it coredumped for me) you then get a
> > /dev/scsi directory:
> >
> > # ls -l /dev/scsi/
> > total 0
> > crw-------    1 root     root       9, 128 Aug  8 21:46 rsth24-c000c0i14l0
> > brw-------    1 root     root       8,   0 Aug  8 21:46 sdh24-c000c0i0l0
> > brw-------    1 root     root       8,   1 Aug  8 21:46 sdh24-c000c0i0l0p1
> > ...
> >
> > which contains the mapping between major/minor (st_dev) and
> > device type, controller type, controller, channel, scsi id, lun, partition
> > (  sd           h24          -    c000       c0      i0     l0    p1  )
> >
> > $man scsidev
> > ...
> >        The device nodes that scsidev creates look something  like
> >        "sdh4-334c0i0l0p1".   In this case, the various components
> >        of the name represent physical attributes about the device
> >        or  the  host  adapter to which it is connected.  To begin
> >        with, the "h4"  indicates  that  it  is  connected  to  an
> >        Adaptec  1542.  The "-334" is a means of identifying which
> >        1542 the device is attached to (since linux supports  more
> >        than  one  1542 in the system at the same time).  The "c0"
> >        represents the channel number (since  some  host  adapters
> >        can  drive  multiple  scsi  busses).  The "i0l0" indicates
> >        that this device is scsi ID 0, with lun  0.   Finally  the
> >        "p1" indicated partition number 1.
> > ...
> >
> > Or, check out the source for scsidev.
> >  ftp://tsx-11.mit.edu/pub/linux/ALPHA/scsi/
> >
> > Regards,
> > /Karl
> >
> > -----------------------------------------------------------------------
> > Karl Hammar                    Asp� Data           [EMAIL PROTECTED]
> > Lilla Asp� 2340             +46  173 140 57                    Networks
> > S-742 94 �sthammar         +46  70 511 97 84                  Computers
> > Sweden                                                       Consulting
> > -----------------------------------------------------------------------
> >
> > From: BenHanokh Gabriel <[EMAIL PROTECTED]>
> > Subject: Re: finding target id of a scsi device
> > Date: Tue, 08 Aug 2000 10:28:47 +0300
> >
> > > hi
> > >
> > > i don't think this is the right function, i don't have file-descriptor to the
> > > device, but the a dev_t referance to it.
> > > it is also not a terminal device, but a disk block device( SCSI hard-disk )
> > >
> > > /gabi
> > >

-
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to [EMAIL PROTECTED]

Reply via email to