md: fix use-after-free bug when dropping an rdev from an md array
[pandora-kernel.git] / fs / partitions / acorn.c
1 /*
2  *  linux/fs/partitions/acorn.c
3  *
4  *  Copyright (c) 1996-2000 Russell King.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Scan ADFS partitions on hard disk drives.  Unfortunately, there
11  *  isn't a standard for partitioning drives on Acorn machines, so
12  *  every single manufacturer of SCSI and IDE cards created their own
13  *  method.
14  */
15 #include <linux/buffer_head.h>
16 #include <linux/adfs_fs.h>
17
18 #include "check.h"
19 #include "acorn.h"
20
21 /*
22  * Partition types. (Oh for reusability)
23  */
24 #define PARTITION_RISCIX_MFM    1
25 #define PARTITION_RISCIX_SCSI   2
26 #define PARTITION_LINUX         9
27
28 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
29         defined(CONFIG_ACORN_PARTITION_ADFS)
30 static struct adfs_discrecord *
31 adfs_partition(struct parsed_partitions *state, char *name, char *data,
32                unsigned long first_sector, int slot)
33 {
34         struct adfs_discrecord *dr;
35         unsigned int nr_sects;
36
37         if (adfs_checkbblk(data))
38                 return NULL;
39
40         dr = (struct adfs_discrecord *)(data + 0x1c0);
41
42         if (dr->disc_size == 0 && dr->disc_size_high == 0)
43                 return NULL;
44
45         nr_sects = (le32_to_cpu(dr->disc_size_high) << 23) |
46                    (le32_to_cpu(dr->disc_size) >> 9);
47
48         if (name)
49                 printk(" [%s]", name);
50         put_partition(state, slot, first_sector, nr_sects);
51         return dr;
52 }
53 #endif
54
55 #ifdef CONFIG_ACORN_PARTITION_RISCIX
56
57 struct riscix_part {
58         __le32  start;
59         __le32  length;
60         __le32  one;
61         char    name[16];
62 };
63
64 struct riscix_record {
65         __le32  magic;
66 #define RISCIX_MAGIC    cpu_to_le32(0x4a657320)
67         __le32  date;
68         struct riscix_part part[8];
69 };
70
71 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
72         defined(CONFIG_ACORN_PARTITION_ADFS)
73 static int
74 riscix_partition(struct parsed_partitions *state, struct block_device *bdev,
75                 unsigned long first_sect, int slot, unsigned long nr_sects)
76 {
77         Sector sect;
78         struct riscix_record *rr;
79         
80         rr = (struct riscix_record *)read_dev_sector(bdev, first_sect, &sect);
81         if (!rr)
82                 return -1;
83
84         printk(" [RISCiX]");
85
86
87         if (rr->magic == RISCIX_MAGIC) {
88                 unsigned long size = nr_sects > 2 ? 2 : nr_sects;
89                 int part;
90
91                 printk(" <");
92
93                 put_partition(state, slot++, first_sect, size);
94                 for (part = 0; part < 8; part++) {
95                         if (rr->part[part].one &&
96                             memcmp(rr->part[part].name, "All\0", 4)) {
97                                 put_partition(state, slot++,
98                                         le32_to_cpu(rr->part[part].start),
99                                         le32_to_cpu(rr->part[part].length));
100                                 printk("(%s)", rr->part[part].name);
101                         }
102                 }
103
104                 printk(" >\n");
105         } else {
106                 put_partition(state, slot++, first_sect, nr_sects);
107         }
108
109         put_dev_sector(sect);
110         return slot;
111 }
112 #endif
113 #endif
114
115 #define LINUX_NATIVE_MAGIC 0xdeafa1de
116 #define LINUX_SWAP_MAGIC   0xdeafab1e
117
118 struct linux_part {
119         __le32 magic;
120         __le32 start_sect;
121         __le32 nr_sects;
122 };
123
124 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
125         defined(CONFIG_ACORN_PARTITION_ADFS)
126 static int
127 linux_partition(struct parsed_partitions *state, struct block_device *bdev,
128                 unsigned long first_sect, int slot, unsigned long nr_sects)
129 {
130         Sector sect;
131         struct linux_part *linuxp;
132         unsigned long size = nr_sects > 2 ? 2 : nr_sects;
133
134         printk(" [Linux]");
135
136         put_partition(state, slot++, first_sect, size);
137
138         linuxp = (struct linux_part *)read_dev_sector(bdev, first_sect, &sect);
139         if (!linuxp)
140                 return -1;
141
142         printk(" <");
143         while (linuxp->magic == cpu_to_le32(LINUX_NATIVE_MAGIC) ||
144                linuxp->magic == cpu_to_le32(LINUX_SWAP_MAGIC)) {
145                 if (slot == state->limit)
146                         break;
147                 put_partition(state, slot++, first_sect +
148                                  le32_to_cpu(linuxp->start_sect),
149                                  le32_to_cpu(linuxp->nr_sects));
150                 linuxp ++;
151         }
152         printk(" >");
153
154         put_dev_sector(sect);
155         return slot;
156 }
157 #endif
158
159 #ifdef CONFIG_ACORN_PARTITION_CUMANA
160 int
161 adfspart_check_CUMANA(struct parsed_partitions *state, struct block_device *bdev)
162 {
163         unsigned long first_sector = 0;
164         unsigned int start_blk = 0;
165         Sector sect;
166         unsigned char *data;
167         char *name = "CUMANA/ADFS";
168         int first = 1;
169         int slot = 1;
170
171         /*
172          * Try Cumana style partitions - sector 6 contains ADFS boot block
173          * with pointer to next 'drive'.
174          *
175          * There are unknowns in this code - is the 'cylinder number' of the
176          * next partition relative to the start of this one - I'm assuming
177          * it is.
178          *
179          * Also, which ID did Cumana use?
180          *
181          * This is totally unfinished, and will require more work to get it
182          * going. Hence it is totally untested.
183          */
184         do {
185                 struct adfs_discrecord *dr;
186                 unsigned int nr_sects;
187
188                 data = read_dev_sector(bdev, start_blk * 2 + 6, &sect);
189                 if (!data)
190                         return -1;
191
192                 if (slot == state->limit)
193                         break;
194
195                 dr = adfs_partition(state, name, data, first_sector, slot++);
196                 if (!dr)
197                         break;
198
199                 name = NULL;
200
201                 nr_sects = (data[0x1fd] + (data[0x1fe] << 8)) *
202                            (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) *
203                            dr->secspertrack;
204
205                 if (!nr_sects)
206                         break;
207
208                 first = 0;
209                 first_sector += nr_sects;
210                 start_blk += nr_sects >> (BLOCK_SIZE_BITS - 9);
211                 nr_sects = 0; /* hmm - should be partition size */
212
213                 switch (data[0x1fc] & 15) {
214                 case 0: /* No partition / ADFS? */
215                         break;
216
217 #ifdef CONFIG_ACORN_PARTITION_RISCIX
218                 case PARTITION_RISCIX_SCSI:
219                         /* RISCiX - we don't know how to find the next one. */
220                         slot = riscix_partition(state, bdev, first_sector,
221                                                  slot, nr_sects);
222                         break;
223 #endif
224
225                 case PARTITION_LINUX:
226                         slot = linux_partition(state, bdev, first_sector,
227                                                 slot, nr_sects);
228                         break;
229                 }
230                 put_dev_sector(sect);
231                 if (slot == -1)
232                         return -1;
233         } while (1);
234         put_dev_sector(sect);
235         return first ? 0 : 1;
236 }
237 #endif
238
239 #ifdef CONFIG_ACORN_PARTITION_ADFS
240 /*
241  * Purpose: allocate ADFS partitions.
242  *
243  * Params : hd          - pointer to gendisk structure to store partition info.
244  *          dev         - device number to access.
245  *
246  * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
247  *
248  * Alloc  : hda  = whole drive
249  *          hda1 = ADFS partition on first drive.
250  *          hda2 = non-ADFS partition.
251  */
252 int
253 adfspart_check_ADFS(struct parsed_partitions *state, struct block_device *bdev)
254 {
255         unsigned long start_sect, nr_sects, sectscyl, heads;
256         Sector sect;
257         unsigned char *data;
258         struct adfs_discrecord *dr;
259         unsigned char id;
260         int slot = 1;
261
262         data = read_dev_sector(bdev, 6, &sect);
263         if (!data)
264                 return -1;
265
266         dr = adfs_partition(state, "ADFS", data, 0, slot++);
267         if (!dr) {
268                 put_dev_sector(sect);
269                 return 0;
270         }
271
272         heads = dr->heads + ((dr->lowsector >> 6) & 1);
273         sectscyl = dr->secspertrack * heads;
274         start_sect = ((data[0x1fe] << 8) + data[0x1fd]) * sectscyl;
275         id = data[0x1fc] & 15;
276         put_dev_sector(sect);
277
278 #ifdef CONFIG_BLK_DEV_MFM
279         if (MAJOR(bdev->bd_dev) == MFM_ACORN_MAJOR) {
280                 extern void xd_set_geometry(struct block_device *,
281                         unsigned char, unsigned char, unsigned int);
282                 xd_set_geometry(bdev, dr->secspertrack, heads, 1);
283                 invalidate_bh_lrus();
284                 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
285         }
286 #endif
287
288         /*
289          * Work out start of non-adfs partition.
290          */
291         nr_sects = (bdev->bd_inode->i_size >> 9) - start_sect;
292
293         if (start_sect) {
294                 switch (id) {
295 #ifdef CONFIG_ACORN_PARTITION_RISCIX
296                 case PARTITION_RISCIX_SCSI:
297                 case PARTITION_RISCIX_MFM:
298                         slot = riscix_partition(state, bdev, start_sect,
299                                                  slot, nr_sects);
300                         break;
301 #endif
302
303                 case PARTITION_LINUX:
304                         slot = linux_partition(state, bdev, start_sect,
305                                                 slot, nr_sects);
306                         break;
307                 }
308         }
309         printk("\n");
310         return 1;
311 }
312 #endif
313
314 #ifdef CONFIG_ACORN_PARTITION_ICS
315
316 struct ics_part {
317         __le32 start;
318         __le32 size;
319 };
320
321 static int adfspart_check_ICSLinux(struct block_device *bdev, unsigned long block)
322 {
323         Sector sect;
324         unsigned char *data = read_dev_sector(bdev, block, &sect);
325         int result = 0;
326
327         if (data) {
328                 if (memcmp(data, "LinuxPart", 9) == 0)
329                         result = 1;
330                 put_dev_sector(sect);
331         }
332
333         return result;
334 }
335
336 /*
337  * Check for a valid ICS partition using the checksum.
338  */
339 static inline int valid_ics_sector(const unsigned char *data)
340 {
341         unsigned long sum;
342         int i;
343
344         for (i = 0, sum = 0x50617274; i < 508; i++)
345                 sum += data[i];
346
347         sum -= le32_to_cpu(*(__le32 *)(&data[508]));
348
349         return sum == 0;
350 }
351
352 /*
353  * Purpose: allocate ICS partitions.
354  * Params : hd          - pointer to gendisk structure to store partition info.
355  *          dev         - device number to access.
356  * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
357  * Alloc  : hda  = whole drive
358  *          hda1 = ADFS partition 0 on first drive.
359  *          hda2 = ADFS partition 1 on first drive.
360  *              ..etc..
361  */
362 int
363 adfspart_check_ICS(struct parsed_partitions *state, struct block_device *bdev)
364 {
365         const unsigned char *data;
366         const struct ics_part *p;
367         int slot;
368         Sector sect;
369
370         /*
371          * Try ICS style partitions - sector 0 contains partition info.
372          */
373         data = read_dev_sector(bdev, 0, &sect);
374         if (!data)
375                 return -1;
376
377         if (!valid_ics_sector(data)) {
378                 put_dev_sector(sect);
379                 return 0;
380         }
381
382         printk(" [ICS]");
383
384         for (slot = 1, p = (const struct ics_part *)data; p->size; p++) {
385                 u32 start = le32_to_cpu(p->start);
386                 s32 size = le32_to_cpu(p->size); /* yes, it's signed. */
387
388                 if (slot == state->limit)
389                         break;
390
391                 /*
392                  * Negative sizes tell the RISC OS ICS driver to ignore
393                  * this partition - in effect it says that this does not
394                  * contain an ADFS filesystem.
395                  */
396                 if (size < 0) {
397                         size = -size;
398
399                         /*
400                          * Our own extension - We use the first sector
401                          * of the partition to identify what type this
402                          * partition is.  We must not make this visible
403                          * to the filesystem.
404                          */
405                         if (size > 1 && adfspart_check_ICSLinux(bdev, start)) {
406                                 start += 1;
407                                 size -= 1;
408                         }
409                 }
410
411                 if (size)
412                         put_partition(state, slot++, start, size);
413         }
414
415         put_dev_sector(sect);
416         printk("\n");
417         return 1;
418 }
419 #endif
420
421 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
422 struct ptec_part {
423         __le32 unused1;
424         __le32 unused2;
425         __le32 start;
426         __le32 size;
427         __le32 unused5;
428         char type[8];
429 };
430
431 static inline int valid_ptec_sector(const unsigned char *data)
432 {
433         unsigned char checksum = 0x2a;
434         int i;
435
436         /*
437          * If it looks like a PC/BIOS partition, then it
438          * probably isn't PowerTec.
439          */
440         if (data[510] == 0x55 && data[511] == 0xaa)
441                 return 0;
442
443         for (i = 0; i < 511; i++)
444                 checksum += data[i];
445
446         return checksum == data[511];
447 }
448
449 /*
450  * Purpose: allocate ICS partitions.
451  * Params : hd          - pointer to gendisk structure to store partition info.
452  *          dev         - device number to access.
453  * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
454  * Alloc  : hda  = whole drive
455  *          hda1 = ADFS partition 0 on first drive.
456  *          hda2 = ADFS partition 1 on first drive.
457  *              ..etc..
458  */
459 int
460 adfspart_check_POWERTEC(struct parsed_partitions *state, struct block_device *bdev)
461 {
462         Sector sect;
463         const unsigned char *data;
464         const struct ptec_part *p;
465         int slot = 1;
466         int i;
467
468         data = read_dev_sector(bdev, 0, &sect);
469         if (!data)
470                 return -1;
471
472         if (!valid_ptec_sector(data)) {
473                 put_dev_sector(sect);
474                 return 0;
475         }
476
477         printk(" [POWERTEC]");
478
479         for (i = 0, p = (const struct ptec_part *)data; i < 12; i++, p++) {
480                 u32 start = le32_to_cpu(p->start);
481                 u32 size = le32_to_cpu(p->size);
482
483                 if (size)
484                         put_partition(state, slot++, start, size);
485         }
486
487         put_dev_sector(sect);
488         printk("\n");
489         return 1;
490 }
491 #endif
492
493 #ifdef CONFIG_ACORN_PARTITION_EESOX
494 struct eesox_part {
495         char    magic[6];
496         char    name[10];
497         __le32  start;
498         __le32  unused6;
499         __le32  unused7;
500         __le32  unused8;
501 };
502
503 /*
504  * Guess who created this format?
505  */
506 static const char eesox_name[] = {
507         'N', 'e', 'i', 'l', ' ',
508         'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
509 };
510
511 /*
512  * EESOX SCSI partition format.
513  *
514  * This is a goddamned awful partition format.  We don't seem to store
515  * the size of the partition in this table, only the start addresses.
516  *
517  * There are two possibilities where the size comes from:
518  *  1. The individual ADFS boot block entries that are placed on the disk.
519  *  2. The start address of the next entry.
520  */
521 int
522 adfspart_check_EESOX(struct parsed_partitions *state, struct block_device *bdev)
523 {
524         Sector sect;
525         const unsigned char *data;
526         unsigned char buffer[256];
527         struct eesox_part *p;
528         sector_t start = 0;
529         int i, slot = 1;
530
531         data = read_dev_sector(bdev, 7, &sect);
532         if (!data)
533                 return -1;
534
535         /*
536          * "Decrypt" the partition table.  God knows why...
537          */
538         for (i = 0; i < 256; i++)
539                 buffer[i] = data[i] ^ eesox_name[i & 15];
540
541         put_dev_sector(sect);
542
543         for (i = 0, p = (struct eesox_part *)buffer; i < 8; i++, p++) {
544                 sector_t next;
545
546                 if (memcmp(p->magic, "Eesox", 6))
547                         break;
548
549                 next = le32_to_cpu(p->start);
550                 if (i)
551                         put_partition(state, slot++, start, next - start);
552                 start = next;
553         }
554
555         if (i != 0) {
556                 sector_t size;
557
558                 size = get_capacity(bdev->bd_disk);
559                 put_partition(state, slot++, start, size - start);
560                 printk("\n");
561         }
562
563         return i ? 1 : 0;
564 }
565 #endif