DISK(2)DISK(2)
NAME
opendisk, Disk – generic disk device interface
SYNOPSIS
#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)
DESCRIPTION
These routines provide a simple way to gather
and use information about
floppy(3)
and
sd(3)
disks and disk partitions,
as well as plain files.
Opendisk
opens
file
for reading and stores the file descriptor 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;
otherwise the sector size is assumed to be 512,
as is the case for floppy disks.
Opendisk
then stores the disk size measured 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;
otherwise it sets
offset
to zero.
If the disk is an ATA disk,
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 initialized 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 number 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 partitioning
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 numbers bear little or no resemblance
to reality.
SOURCE
/sys/src/libdisk/disk.c
SEE
floppy(3),
sd(3)