Page 1 of 1
Structure to Contain Partition Information
Posted: Tue Oct 25, 2011 2:16 am
by mark3094
Hi,
I'm at a stage where I'm reading from the MBR to get partition information (I'm working with FAT). This is successful so far, but I'm wondering what the best way to store this information is.
I can easily create a structure that stores information such as:
- Whether the partition is active
The system ID
Starting sector
Total sectors
Total size in MB
However, if there are potentially four primary partitions per hard disk, and there are potentially four ATA disks attached, there is a possible 16 structures I would need to create (not counting extended partitions as yet).
That's a lot of structures to create statically, especially since it's a rare (yet possible) case for there to be 16 primary partitions.
What is the best way to handle this?
Re: Structure to Contain Partition Information
Posted: Tue Oct 25, 2011 2:32 am
by mark3094
Would an option be to just create a structure with 16 entries (one for each potential partition), and each entry represents whether that partition exists?
Then I just read the MBR each time it needs to be accessed to find the start, end, etc.
This would save creating multiple structures, but would be slower I guess.
Re: Structure to Contain Partition Information
Posted: Tue Oct 25, 2011 2:42 am
by gerryg400
You can malloc a structure for each partition as you discover it. You need to be able to find those structures so you should keep a linked list in each device structure that holds its partitions.
In addition each partition structure should be registered with your name service so that the user can find the partitions (like /dev/sda1) and mount them. The filesystem mounted on a partition should also have a pointer to the partition. And of course files have pointers to their filesystem. This means that whenever a file is operated on it is a simple matter to follow the pointers and find the fs, partition and untimately the device that needs to be read.
Re: Structure to Contain Partition Information
Posted: Tue Oct 25, 2011 9:09 am
by egos
My partition manager registers every partition as virtual device providing to the kernel the pointers to device data structure and device handler. When kernel calls the handler it transfer back a pointer to device data structure associated with this device. To connect with whole device the structure holds the link to it. It's a virtual device index (handle). To well translate requests to whole device the structure holds partition location and size too.
Storage device can have not only primary partitions. I have support for maximum 255 partitions on each MBR-partitioned device.
Re: Structure to Contain Partition Information
Posted: Wed Oct 26, 2011 4:53 am
by mark3094
gerryg400 wrote:You can malloc a structure for each partition as you discover it
I thought that the pointers had to be declared before malloc could be used, and that malloc only dynamically reserved memory for them? eg:
Code: Select all
typedef struct partitions {
int start;
int sectors;
} partition;
partition *disk1_partition1;
disk1_partition1 = malloc(sizeof(partition));
Wouldn't that then mean that I would have to declare each pointer before beginning? eg:
Code: Select all
partition *disk1_partition1
partition *disk1_partition2
partition *disk2_partition1
...
There's no way to declare pointers or structs at runtime is there?
Re: Structure to Contain Partition Information
Posted: Wed Oct 26, 2011 5:05 am
by gerryg400
The most basic method is with a linked list.
Code: Select all
typedef struct partitions {
int start;
int sectors;
struct partitions *next;
} partition;
partition *partition_list = NULL;
while (partitions_are_found) {
partition *p;
p = malloc(sizeof(partition));
p->start = ... ;
p->sectors = ... ;
p->next = partition_list;
partition_list = p;
}
But as I said the partitions are usually found by following pointers from open file control blocks.
Re: Structure to Contain Partition Information
Posted: Fri Oct 28, 2011 2:09 am
by mark3094
Thanks. This has put me on the right track