ocfs2: clean up bh null checks
[linux-2.6] / fs / partitions / amiga.c
1 /*
2  *  fs/partitions/amiga.c
3  *
4  *  Code extracted from drivers/block/genhd.c
5  *
6  *  Copyright (C) 1991-1998  Linus Torvalds
7  *  Re-organised Feb 1998 Russell King
8  */
9
10 #include <linux/types.h>
11 #include <linux/affs_hardblocks.h>
12
13 #include "check.h"
14 #include "amiga.h"
15
16 static __inline__ u32
17 checksum_block(__be32 *m, int size)
18 {
19         u32 sum = 0;
20
21         while (size--)
22                 sum += be32_to_cpu(*m++);
23         return sum;
24 }
25
26 int
27 amiga_partition(struct parsed_partitions *state, struct block_device *bdev)
28 {
29         Sector sect;
30         unsigned char *data;
31         struct RigidDiskBlock *rdb;
32         struct PartitionBlock *pb;
33         int start_sect, nr_sects, blk, part, res = 0;
34         int blksize = 1;        /* Multiplier for disk block size */
35         int slot = 1;
36         char b[BDEVNAME_SIZE];
37
38         for (blk = 0; ; blk++, put_dev_sector(sect)) {
39                 if (blk == RDB_ALLOCATION_LIMIT)
40                         goto rdb_done;
41                 data = read_dev_sector(bdev, blk, &sect);
42                 if (!data) {
43                         if (warn_no_part)
44                                 printk("Dev %s: unable to read RDB block %d\n",
45                                        bdevname(bdev, b), blk);
46                         res = -1;
47                         goto rdb_done;
48                 }
49                 if (*(__be32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
50                         continue;
51
52                 rdb = (struct RigidDiskBlock *)data;
53                 if (checksum_block((__be32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
54                         break;
55                 /* Try again with 0xdc..0xdf zeroed, Windows might have
56                  * trashed it.
57                  */
58                 *(__be32 *)(data+0xdc) = 0;
59                 if (checksum_block((__be32 *)data,
60                                 be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
61                         printk("Warning: Trashed word at 0xd0 in block %d "
62                                 "ignored in checksum calculation\n",blk);
63                         break;
64                 }
65
66                 printk("Dev %s: RDB in block %d has bad checksum\n",
67                                bdevname(bdev, b), blk);
68         }
69
70         /* blksize is blocks per 512 byte standard block */
71         blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
72
73         printk(" RDSK (%d)", blksize * 512);    /* Be more informative */
74         blk = be32_to_cpu(rdb->rdb_PartitionList);
75         put_dev_sector(sect);
76         for (part = 1; blk>0 && part<=16; part++, put_dev_sector(sect)) {
77                 blk *= blksize; /* Read in terms partition table understands */
78                 data = read_dev_sector(bdev, blk, &sect);
79                 if (!data) {
80                         if (warn_no_part)
81                                 printk("Dev %s: unable to read partition block %d\n",
82                                        bdevname(bdev, b), blk);
83                         res = -1;
84                         goto rdb_done;
85                 }
86                 pb  = (struct PartitionBlock *)data;
87                 blk = be32_to_cpu(pb->pb_Next);
88                 if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
89                         continue;
90                 if (checksum_block((__be32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
91                         continue;
92
93                 /* Tell Kernel about it */
94
95                 nr_sects = (be32_to_cpu(pb->pb_Environment[10]) + 1 -
96                             be32_to_cpu(pb->pb_Environment[9])) *
97                            be32_to_cpu(pb->pb_Environment[3]) *
98                            be32_to_cpu(pb->pb_Environment[5]) *
99                            blksize;
100                 if (!nr_sects)
101                         continue;
102                 start_sect = be32_to_cpu(pb->pb_Environment[9]) *
103                              be32_to_cpu(pb->pb_Environment[3]) *
104                              be32_to_cpu(pb->pb_Environment[5]) *
105                              blksize;
106                 put_partition(state,slot++,start_sect,nr_sects);
107                 {
108                         /* Be even more informative to aid mounting */
109                         char dostype[4];
110                         __be32 *dt = (__be32 *)dostype;
111                         *dt = pb->pb_Environment[16];
112                         if (dostype[3] < ' ')
113                                 printk(" (%c%c%c^%c)",
114                                         dostype[0], dostype[1],
115                                         dostype[2], dostype[3] + '@' );
116                         else
117                                 printk(" (%c%c%c%c)",
118                                         dostype[0], dostype[1],
119                                         dostype[2], dostype[3]);
120                         printk("(res %d spb %d)",
121                                 be32_to_cpu(pb->pb_Environment[6]),
122                                 be32_to_cpu(pb->pb_Environment[4]));
123                 }
124                 res = 1;
125         }
126         printk("\n");
127
128 rdb_done:
129         return res;
130 }