I'm still stuck on this.
Before I start I want to be sure that my design allows for multiple devices and file systems. But I'm still not sure how I can get this to fit together.
Right now I have two structures, fs_dev and fs_node. Every fs_node has an integer which contains the device ID. The device structure contains pointers to the necessary file system functions and device driver functions.
To make it more clear:
Code: Select all
typedef struct fs_dev
{
unsigned int id;
// File system functions
unsigned int (* read)(fs_node_t * node, fs_dev_t * dev, unsigned int offset, unsigned int size, char * buffer);
unsigned int (* write)(fs_node_t * node, fs_dev_t * dev, unsigned int offset, unsigned int size, char * buffer);
fs_node_t * (* readdir)(fs_node_t * node, fs_dev_t * dev, unsigned int index);
fs_node_t * (* finddir)(fs_node_t * node, fs_dev_t * dev, char * name);
// Device driver functions
unsigned int (* dev_read)(fs_dev_t * dev, unsigned int offset, unsigned int size, char * buffer);
unsigned int (* dev_write)(fs_dev_T * dev, unsigned int offset, unsigned int size, char * buffer);
} fs_dev_t;
typedef struct fs_node
{
char name[FS_NODE_NAME_LENGTH];
// File attributes
unsigned int type;
unsigned int permissions;
unsigned int size;
// Which device the node belongs to
unsigned int dev_id;
// Used if this node is a mount/symlink
fs_node_t * ptr;
} fs_node_t;
So the plan was to keep all the devices in a linked list and when one of the asbtract functions were called like vfs_write it would first find the device with the correct id (which was obtained from the fs_node passed to the function) and then pass the fs_dev structure to the file system functions. The file system functions the use the device driver functions found in the fs_dev structure to write and read raw data to the device.
Let's say that I have a HDD with file system FAT connected to the computer. I want to read from a file on the HDD. I would like every device to be mounted in a virtual file system at /dev. How do I create this virtual file system? Is it with an initrd file?
So I create a fs_node with the name /dev/hdd0. Should I write this file to the virtual file system so that it later can be found when listing the content of /dev? In James Molloy's tutorial he says something like "who would want to write files to a initrd fs", so he doesn't include the ability to write files to his initrd file system format.
I then call vfs_finddir(root_node, "/dev/hdd0/myFile.txt") to get a node structure for myFile.txt. I then pass that structure to vfs_read; vfs_read calls fat_read, fat_read uses the device driver functions (hdd_read) to search through the whole HDD until it finds the offset to the file I wanted to read from (this part i'm unsure about, imo searching through to find it would take a long long time, but how can i cache it when multiple devices might use the same function because of having the same file system).
Questions extracted from the text above:
- How should the root file system be created, is this what initrd is for?
- When I mount something, should I create a new node in the root file system? Then my initrd format will have the capability to write files, why doesn't James Molloy have this in his?
- Should I really parse the whole device (e.g. HDD) to find a file every time I want to read or write from it? How can i keep cache for multiple devices that use the same fs?
Is this a good approach at handling devices, file systems and files?
Thanks for your expertise.