[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Block size: who decides?
On Sun, 1 Jul 2001, Peter Chapin wrote:
> I suppose it's up to me to decide on the size of my allocation units.
> However, how do I find out, in my userspace program, how large the sectors
> are (perhaps I don't need to know that there)? Also how can my userspace
> program find out how large the partition is?
Disclaimer: I don't really know what I'm talking about here. :-)
Try looking up the source code to mke2fs, since this is the sort of thing
you're trying to do anyway. There's a file called getsize.c in the
lib/ext2fs directory.
You'll find that BLKGETSIZE is defined in linux/fs.h as:
_IO(0x12,96)
(otherwise getsize.c will define it for you)
Then the function ext2fs_get_device_size() uses this BLKGETSIZE with
ioctl() to get the size. Dramatically simplified, it basically says:
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
blk_t *retblocks)
{
int fd;
unsigned long size;
fd = open(file, O_RDONLY);
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
close(fd);
*retblocks = size / (blocksize / 512);
return 0;
}
/* else use a binary search to find the size */
}
There's a DEBUG main() function which assumes that blocksize is 1024, but
mke2fs uses a better, though more complicated approach (including the
option of letting the user define it for themselves).
The binary search part is also pretty simple. It just does a seek and a
read of a single byte to check if an offset is valid. If it's valid then
it increases the size, otherwise it decreases the size. Ted makes a
comment that this is REALLY not the way you want to do it, but if ioctl()
doesn't work then you don't have too much of a choice. :-)
Anyway, everything you need should be in there, including the option to
find the blocksize automatically.
Regards,
Paul Gearon
Software Engineer Telephone: +61 7 3876 2188
Plugged In Software Fax: +61 7 3876 4899
http://www.PIsoftware.com PGP Key available via finger
Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum
immane mittam.
(Translation from latin: "I have a catapult. Give me all the money,
or I will fling an enormous rock at your head.")
-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
IRC Channel: irc.openprojects.net / #kernelnewbies
Web Page: http://www.kernelnewbies.org/