Page 1 of 1

VFS, file handles & stdio

Posted: Sun Jan 18, 2015 12:22 pm
by bubach
Hi there, I've been over this in my head about 500 times or so - but still think I need some input. Back in 2012 I wrote my VFS and FAT12 code, which I then lost while breaking an USB stick in half. Recently I've been going back to an older copy of my code and started to rebuild. I'm pretty sure that my old code had the VFS and FAT12 completed enough for file handles and all that to be done, it's just I can't really remember what I decided on. :roll:

Also, back then I hadn't really thought through how the VFS and file handles corresponds to stdin, stdout and stderr. How I'm thinking right now is that my code for VFS will have to register some kind of rudimentary file i/o to the stdio, which will be to busy dealing with screens and keyboards to really care about how stuff is outputted to file.

So far so good? Now to file handles, the most obvious thing would be to stick them into the VFS, making them FS independent. The problem with that is that it would need information like first file cluster and possibly the directory entry if it's FAT12, while probably inode number and stuff if it's ext2/3?

I've been heavily inspired but what I've found on this page: http://stanislavs.org/helppc/file_handles.html since my OS will never be much more then a DOS copy running in 32-bit mode. But it's pretty hardcoded for FAT12/16 in DOS.

It might be better for the filesystem to define filehandles themselves, for a more specific structure based on need. Now, I'm thinking that perhaps the FS driver should define the file handle structure, and be responsible for parsing it, while the VFS just takes a copy of the pre-defined filehandle structure and allocates a buffer for that format and sticks a pointer into the mounted drive structure, allocating more buffer space if it runs out of free handles?

Actually that last part dawned on me as I was typing, so perhaps I'll sort this out after all. I'd still like some input on how you handle stuff like this, and if your OS does things differently from *nix or DOS - what differs? :lol:

Re: VFS, file handles & stdio

Posted: Wed Jan 21, 2015 12:07 pm
by mariuszp
My OS which is a UNIX clone has a File structure in the VFS which stores information that VFS itself needs, and contains an "fsdata" field which is a "void *" and points to whatever data the FS driver may need.

You can see the structure here:
https://github.com/madd-games/glidix/bl ... vfs.h#L125

EDIT: showed the exact line number.

Re: VFS, file handles & stdio

Posted: Wed Jan 21, 2015 12:21 pm
by alexfru
A file handle is just a numeric value (some kind of index or a pointer, but I'd advise against the latter as it may disclose the kernel memory layout (may weaken security) and it probably isn't the most convenient data type to work with in case it needs to be sent across the network (consider RPC, file servers, etc) and be usable by computers with different architectures (e.g. 32-bit and 64-bit PCs)). You need additional data structures and code to be able to translate handle values to the underlying FS-specific things (drive, directory, cluster, etc).

Re: VFS, file handles & stdio

Posted: Wed Jan 21, 2015 12:26 pm
by bubach
Thanks for the link, it's always interesting to see how others do things. I read up more on DOS structures and discovered that it actually had all STDIO devices as ghost files too, which I had no idea of. Suddenly the "copy con target.txt" command makes much more sense. :P

It's terrifying how much time I can spend thinking about, and trying to make a decision on, how my file handle structure should look, how big buffers I should use and so on. A simple thing as different filesystems allowed path or filename length can really mess up my design. I can actually spend an hour or more thinking about if I should declare another 512bytes worth of space inside my executable, or if I should dynamically allocate it. And if so, how inefficient all thos dynamic allocations would be with my current memory manager's linked list approach, giving each block a header of maybe 12-16bytes. :P

I think I might be in need of professional help. :lol:

Re: VFS, file handles & stdio

Posted: Wed Jan 21, 2015 12:33 pm
by bubach
@alexfru Well yes, that much I know - was thinking more about the structure used internally by either the VFS or the FS directly when I was mentioning file handles. If I use a direct memory pointer to the structure or an index for which number in a linked list doesn't matter that much since I have no security goals whatsoever with my OS. I welcome any crash with open arms. :D