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