]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdisk/disk.c
kernel: fix Abind cyclic reference and mounthead leaks (thanks Alex Musolino)
[plan9front.git] / sys / src / libdisk / disk.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <disk.h>
6
7 static Disk*
8 mkwidth(Disk *disk)
9 {
10         char buf[40];
11
12         sprint(buf, "%lld", disk->size);
13         disk->width = strlen(buf);
14         return disk;
15 }
16
17 /*
18  * Discover the disk geometry by various sleazeful means.
19  * 
20  * First, if there is a partition table in sector 0,
21  * see if all the partitions have the same end head
22  * and sector; if so, we'll assume that that's the 
23  * right count.
24  * 
25  * If that fails, we'll try looking at the geometry that the ATA
26  * driver supplied, if any, and translate that as a
27  * BIOS might. 
28  * 
29  * If that too fails, which should only happen on a SCSI
30  * disk with no currently defined partitions, we'll try
31  * various common (h, s) pairs used by BIOSes when faking
32  * the geometries.
33  */
34 typedef struct Table  Table;
35 typedef struct Tentry Tentry;
36 struct Tentry {
37         uchar   active;                 /* active flag */
38         uchar   starth;                 /* starting head */
39         uchar   starts;                 /* starting sector */
40         uchar   startc;                 /* starting cylinder */
41         uchar   type;                   /* partition type */
42         uchar   endh;                   /* ending head */
43         uchar   ends;                   /* ending sector */
44         uchar   endc;                   /* ending cylinder */
45         uchar   xlba[4];                        /* starting LBA from beginning of disc */
46         uchar   xsize[4];               /* size in sectors */
47 };
48 enum {
49         Toffset         = 446,          /* offset of partition table in sector */
50         Magic0          = 0x55,
51         Magic1          = 0xAA,
52         NTentry         = 4,
53 };
54 struct Table {
55         Tentry  entry[NTentry];
56         uchar   magic[2];
57 };
58 static int
59 partitiongeometry(Disk *disk)
60 {
61         char *rawname;
62         int i, h, rawfd, s;
63         uchar buf[512];
64         Table *t;
65
66         t = (Table*)(buf + Toffset);
67
68         /*
69          * look for an MBR first in the /dev/sdXX/data partition, otherwise
70          * attempt to fall back on the current partition.
71          */
72         rawname = malloc(strlen(disk->prefix) + 5);     /* prefix + "data" + nul */
73         if(rawname == nil)
74                 return -1;
75
76         strcpy(rawname, disk->prefix);
77         strcat(rawname, "data");
78         rawfd = open(rawname, OREAD);
79         free(rawname);
80         if(rawfd >= 0
81         && seek(rawfd, 0, 0) >= 0
82         && readn(rawfd, buf, 512) == 512
83         && t->magic[0] == Magic0
84         && t->magic[1] == Magic1) {
85                 close(rawfd);
86         } else {
87                 if(rawfd >= 0)
88                         close(rawfd);
89                 if(seek(disk->fd, 0, 0) < 0
90                 || readn(disk->fd, buf, 512) != 512
91                 || t->magic[0] != Magic0
92                 || t->magic[1] != Magic1) {
93                         return -1;
94                 }
95         }
96
97         h = s = -1;
98         for(i=0; i<NTentry; i++) {
99                 if(t->entry[i].type == 0)
100                         continue;
101
102                 t->entry[i].ends &= 63;
103                 if(h == -1) {
104                         h = t->entry[i].endh;
105                         s = t->entry[i].ends;
106                 } else {
107                         /*
108                          * Only accept the partition info if every
109                          * partition is consistent.
110                          */
111                         if(h != t->entry[i].endh || s != t->entry[i].ends)
112                                 return -1;
113                 }
114         }
115
116         if(h < 0 || s <= 0)
117                 return -1;
118
119         disk->h = h+1;  /* heads count from 0 */
120         disk->s = s;    /* sectors count from 1 */
121         disk->c = disk->secs / (disk->h*disk->s);
122         disk->chssrc = Gpart;
123         return 0;
124 }
125
126 /*
127  * If there is ATA geometry, use it, perhaps massaged.
128  */
129 static int
130 drivergeometry(Disk *disk)
131 {
132         int m;
133
134         if(disk->c == 0 || disk->h == 0 || disk->s == 0)
135                 return -1;
136
137         disk->chssrc = Gdisk;
138         if(disk->c < 1024)
139                 return 0;
140
141         switch(disk->h) {
142         case 15:
143                 disk->h = 255;
144                 disk->c /= 17;
145                 return 0;
146
147         default:
148                 for(m = 2; m*disk->h < 256; m *= 2) {
149                         if(disk->c/m < 1024) {
150                                 disk->c /= m;
151                                 disk->h *= m;
152                                 return 0;
153                         }
154                 }
155
156                 /* set to 255, 63 and be done with it */
157                 disk->h = 255;
158                 disk->s = 63;
159                 disk->c = disk->secs / (disk->h * disk->s);
160                 return 0;
161         }
162 }
163
164 /*
165  * There's no ATA geometry and no partitions.
166  * Our guess is as good as anyone's.
167  */
168 static struct {
169         int h;
170         int s;
171 } guess[] = {
172         64, 32,
173         64, 63,
174         128, 63,
175         255, 63,
176 };
177 static int
178 guessgeometry(Disk *disk)
179 {
180         int i;
181         long c;
182
183         disk->chssrc = Gguess;
184         c = 1024;
185         for(i=0; i<nelem(guess); i++)
186                 if(c*guess[i].h*guess[i].s >= disk->secs) {
187                         disk->h = guess[i].h;
188                         disk->s = guess[i].s;
189                         disk->c = disk->secs / (disk->h * disk->s);
190                         return 0;
191                 }
192
193         /* use maximum values */
194         disk->h = 255;
195         disk->s = 63;
196         disk->c = disk->secs / (disk->h * disk->s);
197         return 0;
198 }
199
200 static void
201 findgeometry(Disk *disk)
202 {
203         if(partitiongeometry(disk) < 0
204         && drivergeometry(disk) < 0
205         && guessgeometry(disk) < 0) {   /* can't happen */
206                 print("we're completely confused about your disk; sorry\n");
207                 assert(0);
208         }
209 }
210
211 static Disk*
212 freedisk(Disk *d)
213 {
214         if(d->fd >= 0)
215                 close(d->fd);
216         if(d->wfd >= 0)
217                 close(d->wfd);
218         if(d->ctlfd >= 0)
219                 close(d->ctlfd);
220         free(d);
221         return nil;
222 }
223
224 static Disk*
225 openfile(Disk *disk)
226 {
227         Dir *d;
228
229         if((d = dirfstat(disk->fd)) == nil)
230                 return freedisk(disk);
231
232         disk->secsize = 512;
233         disk->size = d->length;
234         disk->secs = disk->size / disk->secsize;
235         disk->offset = 0;
236         free(d);
237
238         if(disk->secs == 0){
239                 werrstr("file too small to be a disk");
240                 return freedisk(disk);
241         }
242
243         findgeometry(disk);
244         return mkwidth(disk);
245 }
246
247 static Disk*
248 opensd(Disk *disk)
249 {
250         Biobuf b;
251         char *p, *f[10];
252         int nf;
253
254         Binit(&b, disk->ctlfd, OREAD);
255         while(p = Brdline(&b, '\n')) {
256                 p[Blinelen(&b)-1] = '\0';
257                 nf = tokenize(p, f, nelem(f));
258                 if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
259                         disk->secsize = strtoll(f[2], 0, 0);
260                         if(nf >= 6) {
261                                 disk->c = strtol(f[3], 0, 0);
262                                 disk->h = strtol(f[4], 0, 0);
263                                 disk->s = strtol(f[5], 0, 0);
264                         }
265                 }
266                 if(nf >= 3 && strcmp(f[0], "alignment") == 0) {
267                         disk->psecsize = strtol(f[1], 0, 0);
268                         disk->physalign = strtol(f[2], 0, 0);
269                 }
270                 if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
271                         disk->offset = strtoll(f[2], 0, 0);
272                         disk->secs = strtoll(f[3], 0, 0) - disk->offset;
273                 }
274         }
275
276         if (!disk->psecsize) disk->psecsize = disk->secsize;    
277         disk->size = disk->secs * disk->secsize;
278         if(disk->size <= 0) {
279                 strcpy(disk->part, "");
280                 disk->type = Tfile;
281                 return openfile(disk);
282         }
283
284         findgeometry(disk);
285         return mkwidth(disk);
286 }
287
288 Disk*
289 opendisk(char *disk, int rdonly, int noctl)
290 {
291         char *p, *q;
292         Disk *d;
293         Dir *s;
294
295         d = mallocz(sizeof(*d), 1);
296         if(d == nil)
297                 return nil;
298
299         d->fd = d->wfd = d->ctlfd = -1;
300         d->rdonly = rdonly;
301
302         d->fd = open(disk, OREAD);
303         if(d->fd < 0) {
304                 werrstr("cannot open disk file: %r");
305                 return freedisk(d);
306         }
307         if((s = dirfstat(d->fd)) == nil)
308                 return freedisk(d);
309         if((s->mode & (DMDIR|DMAPPEND)) != 0){
310                 free(s);
311                 werrstr("not a disk file: %s", disk);
312                 return freedisk(d);
313         }
314         free(s);
315
316         if(rdonly == 0) {
317                 d->wfd = open(disk, OWRITE);
318                 if(d->wfd < 0)
319                         d->rdonly = 1;
320         }
321
322         if(noctl)
323                 return openfile(d);
324
325         p = malloc(strlen(disk) + 4);   /* 4: slop for "ctl\0" */
326         if(p == nil)
327                 return freedisk(d);
328         strcpy(p, disk);
329
330         /* check for floppy(3) disk */
331         if(strlen(p) >= 7) {
332                 q = p+strlen(p)-7;
333                 if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
334                         strcpy(q+3, "ctl");
335                         if((d->ctlfd = open(p, ORDWR)) >= 0) {
336                                 *q = '\0';
337                                 d->prefix = p;
338                                 d->type = Tfloppy;
339                                 return openfile(d);
340                         }
341                 }
342         }
343
344         /* attempt to find sd(3) disk or partition */
345         if(q = strrchr(p, '/'))
346                 q++;
347         else
348                 q = p;
349
350         strcpy(q, "ctl");
351         if((d->ctlfd = open(p, ORDWR)) >= 0) {
352                 *q = '\0';
353                 d->prefix = p;
354                 d->type = Tsd;
355                 d->part = strdup(disk+(q-p));
356                 if(d->part == nil){
357                         free(p);
358                         return freedisk(d);
359                 }
360                 return opensd(d);
361         }
362
363         *q = '\0';
364         d->prefix = p;
365         /* assume we just have a normal file */
366         d->type = Tfile;
367         return openfile(d);
368 }