Skip to contentSkip to conversation

bios_param() incorrect for large disks on 32-bit arch for many SCSI drivers?

DISCUSSION Tony Battersby 1 participant 2 messages Dec 23, 2004 – Dec 27, 2004 SCSI

lore
Share & tools
lore
#0 lore raw

In reviewing the LBD (large block devices) patch for lk 2.4
(http://www.gelato.unsw.edu.au/patches/lbd/), there are quite a number of changes to SCSI driver bios_param() functions that look like:

- cylinders = capacity / (heads * sectors);
+ cylinders = (long) capacity / (heads * sectors);

I noticed that many of these changes have made it into the lk 2.6 series:

cd linux-2.6.10-rc3
find . -type f -name "*.c" | xargs grep "(.*long).*capacity" | grep -v "long long"

This prints out numerous places where disk capacity is casted to long or unsigned long, mostly in bios_param() functions of SCSI drivers.

Why are these casts used? Disk capacity is of type sector_t (64-bit), but on 32-bit platforms like i386, long and unsigned long are 32-bit. These casts truncate the capacity of disks >= 2 TB on 32-bit platforms. The only benefit that I can see is using 32/32 division rather than 64/32 division (something like this is mentioned at
http://www.gelato.unsw.edu.au/IA64wiki/LBDKernel), but the answer is wrong, so what's the point?

Are these typecasts incorrect, or am I missing something?

Anthony J. Battersby
Cybernetics

#1 lore raw
Quoted 11 lines

- cylinders = capacity / (heads * sectors);
+ cylinders = (long) capacity / (heads * sectors);

Why are these casts used? Disk capacity is of type sector_t (64-bit), but on 32-bit platforms like i386, long and unsigned long are 32-bit. These casts truncate the capacity of disks >= 2 TB on 32-bit platforms.
The only benefit that I can see is using 32/32 division rather than 64/32 division (something like this is mentioned at
http://www.gelato.unsw.edu.au/IA64wiki/LBDKernel), but the answer is wrong, so what's the point?

OK, so the partial answer is that 64/32 division in the kernel requires use of a special macro (sector_div which uses do_div from asm/div64.h), so one cannot simply remove the casts or else the build will have unresolved symbols from gcc. The correct solution is something like:

- cylinders = (long) capacity / (heads * sectors);
+ sector_t cyl = capacity;
+ u32 hs = heads * sectors;
+ sector_div(cyl, hs);
+ cylinders = cyl;

I am not sure why it was not done this way to begin with. Does anyone think that these should be fixed, or does it not matter?

Anthony J. Battersby
Cybernetics