blob: bec50f209011a56b541d260545be272dcae892d9 [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH DISK 3
2.SH NAME
3opendisk, Disk \- generic disk device interface
4.SH SYNOPSIS
5.nf
6.ft L
7#include <u.h>
8#include <libc.h>
9#include <disk.h>
10.ft
11.PP
12.ft L
13typedef struct Disk {
14 char *prefix;
15 char part[NAMELEN];
16 int fd, wfd, ctlfd, rdonly;
17 int type;
18 vlong secs, secsize, size, offset;
19 int c, h, s;
20} Disk;
21.ft
22.PP
23.B
24Disk* opendisk(char *file, int rdonly, int noctl)
25.SH DESCRIPTION
26These routines provide a simple way to gather
27and use information about
28disks and disk partitions,
29as well as plain files.
30.PP
31.I Opendisk
32opens
33.I file
34for reading and stores the file descriptor in
35the
36.B fd
37field of the
38.B Disk
39structure.
40If
41.I rdonly
42is not set,
43.I opendisk
44also opens
45.I file
46for writing and stores that file descriptor in
47.BR wfd .
48The two file descriptors are kept separate to
49help prevent accidents.
50.PP
51If
52.I noctl
53is not set,
54.I opendisk
55looks for a
56.B ctl
57file in the same directory as the
58disk file;
59if it finds one, it declares
60the disk to be
61an
62.I sd
63device,
64setting the
65.B type
66field in the
67.B Disk
68structure
69to
70.BR Tsd .
71If the passed
72.I file
73is named
74.BI fd n disk \fR,
75it looks for a file
76.BI fd n ctl \fR,
77and if it finds that,
78declares the disk to be
79a floppy disk, of type
80.BR Tfloppy .
81If either
82control
83file is found, it is opened for reading
84and writing, and the resulting file descriptor
85is saved as
86.BR ctlfd .
87Otherwise the returned disk
88has type
89.BR Tfile .
90.PP
91.I Opendisk
92then stats the file and stores its length in
93.BR size .
94If the disk is an
95.I sd
96partition,
97.I opendisk
98reads the sector size from the
99control
100file and stores it in
101.BR secsize ;
102otherwise the sector size is assumed to be 512,
103as is the case for floppy disks.
104.I Opendisk
105then stores the disk size measured in sectors in
106.BR secs .
107.PP
108If the disk is an
109.I sd
110partition,
111.I opendisk
112parses the
113control
114file to find the partition's offset
115within its disk;
116otherwise it sets
117.B offset
118to zero.
119If the disk is an ATA disk,
120.I opendisk
121reads
122the disk geometry (number of cylinders, heads, and sectors)
123from the
124.B geometry
125line in the
126.I sd
127control file;
128otherwise it sets these to zero as well.
129.B Name
130is initialized with the base name of
131the disk partition, and is useful for forming messages to the
132.I sd
133control file.
134.B Prefix
135is set to the passed filename without
136the
137.B name
138suffix.
139.PP
140The IBM PC BIOS interface allocates
14110 bits for the number of cylinders, 8 for
142the number of heads, and 6 for the number of sectors per track.
143Disk geometries are not quite so simple
144anymore, but to keep the interface useful,
145modern disks and BIOSes present geometries
146that still fit within these constraints.
147These numbers are still used when partitioning
148and formatting disks.
149.I Opendisk
150employs a number of heuristics to discover this
151supposed geometry and store it in the
152.BR c ,
153.BR h ,
154and
155.B s
156fields.
157Disk offsets in partition tables and
158in FAT descriptors are stored in a form
159dependent upon these numbers, so
160.I opendisk
161works hard to report numbers that
162agree with those used by other operating
163systems; the numbers bear little or no resemblance
164to reality.
165.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000166.B \*9/src/libdisk/disk.c
rsc058b0112005-01-03 06:40:20 +0000167.SH SEE ALSO
168Plan 9's
169\fIfloppy\fR(3) and \fIsd\fR(3)
170.SH BUGS
171Disks on Unix systems do not present the interface
172that
173.I opendisk
174expects, so
175.I opendisk
176will give them type
177.BR Tfile .