DISK(2)                                                   DISK(2)

          opendisk, Disk - generic disk device interface

          #include <u.h>
          #include <libc.h>
          #include <disk.h>

          typedef struct Disk {
               char *prefix;
               char part[NAMELEN];
               int fd, wfd, ctlfd, rdonly;
               int type;
               vlong secs, secsize, size, offset;
               int c, h, s;
          } Disk;

          Disk* opendisk(char *file, int rdonly, int noctl)

          These routines provide a simple way to gather and use infor-
          mation about floppy(3) and sd(3) disks and disk partitions,
          as well as plain files.

          Opendisk opens file for reading and stores the file descrip-
          tor in the fd field of the Disk structure.  If rdonly is not
          set, opendisk also opens file for writing and stores that
          file descriptor in wfd.  The two file descriptors are kept
          separate to help prevent accidents.

          If noctl is not set, opendisk looks for a ctl file in the
          same directory as the disk file; if it finds one, it
          declares the disk to be an sd device, setting the type field
          in the Disk structure to Tsd.  If the passed file is named
          fdndisk, it looks for a file fdnctl, and if it finds that,
          declares the disk to be a floppy disk, of type Tfloppy.  If
          either control file is found, it is opened for reading and
          writing, and the resulting file descriptor is saved as
          ctlfd.  Otherwise the returned disk has type Tfile.

          Opendisk then stats the file and stores its length in size.
          If the disk is an sd partition, opendisk reads the sector
          size from the control file and stores it in secsize; other-
          wise the sector size is assumed to be 512, as is the case
          for floppy disks.  Opendisk then stores the disk size mea-
          sured in sectors in secs.

          If the disk is an sd partition, opendisk parses the control
          file to find the partition's offset within its disk; other-
          wise it sets offset to zero.  If the disk is an ATA disk,

     Page 1                       Plan 9             (printed 4/13/24)

     DISK(2)                                                   DISK(2)

          opendisk reads the disk geometry (number of cylinders,
          heads, and sectors) from the geometry line in the sd control
          file; otherwise it sets these to zero as well.  Name is ini-
          tialized with the base name of the disk partition, and is
          useful for forming messages to the sd control file.  Prefix
          is set to the passed filename without the name suffix.

          The IBM PC BIOS interface allocates 10 bits for the number
          of cylinders, 8 for the number of heads, and 6 for the num-
          ber of sectors per track.  Disk geometries are not quite so
          simple anymore, but to keep the interface useful, modern
          disks and BIOSes present geometries that still fit within
          these constraints.  These numbers are still used when parti-
          tioning and formatting disks.  Opendisk employs a number of
          heuristics to discover this supposed geometry and store it
          in the c, h, and s fields.  Disk offsets in partition tables
          and in FAT descriptors are stored in a form dependent upon
          these numbers, so opendisk works hard to report numbers that
          agree with those used by other operating systems; the num-
          bers bear little or no resemblance to reality.


          floppy(3), sd(3)

     Page 2                       Plan 9             (printed 4/13/24)