How to implement a filesystem

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
mateuszb
Member
Member
Posts: 32
Joined: Sun Jan 16, 2011 1:27 am

Re: How to implement a filesystem

Post by mateuszb »

User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

Ok thanks to you all. I now know what I am dealing with, but how do I write to the FAT? Do I export a byte? Can you give me a snippet of code as to how I go about doing this?

Once again thanks to you all!!
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: How to implement a filesystem

Post by Bender »

If you're talking about disks, you don't write bytes, you write sectors. A sector is the fundamental unit of a disk (usually 512 or 2048 bytes in size). There are different types of disks (IDE, SATA, FDD etc.) and thus the procedure would vary with each type of disk, hence there is no such code that can work for all n situations.

I hope you do know what a ramdisk is. If you just want a filesystem implementation to only load configuration files or modules a ramdisk is better and efficient. (Although a ramdisk can use sh*tload of memory if you keep stuffing it. :))
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

Alright, thanks. I think I might use a ram disk. But for future reference, how do I write a sector? Do you have a method? outport_sector()?
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: How to implement a filesystem

Post by Bender »

wxwsk8er wrote:Alright, thanks. I think I might use a ram disk. But for future reference, how do I write a sector? Do you have a method? outport_sector()?
:?
There is no such standard method for writing sectors. Every device has it's own mechanism.
Sorry, but I'm not supposed to post blocks of code here. The only hint I can give you is to look at other implementations (e.g. Linux or other open-source OSes), refer to the manuals, and the wiki at OSDev.org which has excellent information on different types of storage devices.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
embryo

Re: How to implement a filesystem

Post by embryo »

Evans wrote:Embryo how do you make it so that the hdd provides the library with raw bytes?
Like this:

Code: Select all

int startSector=xxx; // points to FAT table
int sectorCount=yyy; // should be enough to contain an information we need
byte bs[]=driver.readSectors(startSector,sectorCount);
FileInformation fs[]=fatLibrary.getRootFiles(bs);
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

Ok thanks guys. I will take another look :)
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

Ok, so does anyone who has a working FAT driver want to show me how you
write to a sector, or even the FAT itself? Thanks in advance -- wxwsk8er
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

So how do you write sectors if you don't send a byte at a time using the io bus, then do you write to the disc using a fputs kinda thing?
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

Re: How to implement a filesystem

Post by madanra »

fputs is part of the C library, which depends on the filesystem you're asking about how to implement - so by definition it's not available.

There are broadly-speaking three ways to read or write to an IDE/ATA hard disk:
  • Using the BIOS. This is the slowest method, and only works in real mode. Typically you would only use it in a bootloader. Have a look at ATA in x86 RealMode (BIOS) for more information. (Unless you're using very old computers, ignore the section about CHS and skip to LBA)
  • PIO (Programmed Input/Output). This is still pretty slow & CPU intensive, but faster than BIOS, and can be used in protected/long mode. See ATA PIO Mode.
  • DMA (Direct Memory Access). This is the fastest & best way to read a disk. See ATA/ATAPI using DMA
All of these involve reading or writing a whole number of sectors.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How to implement a filesystem

Post by Brendan »

Hi,
wxwsk8er wrote:So how do you write sectors if you don't send a byte at a time using the io bus, then do you write to the disc using a fputs kinda thing?
In general; you need to:
  • Design some sort of interface for storage device drivers to implement. This may or may not look like whatever the OS uses for normal file IO (which may or may not look like POSIX-ish "open()", "read()" and "write()", functions).
  • Implement (possibly extremely simple) "device detection" code capable of starting device driver/s. This typically begins with code to scan PCI buses.
  • Write one or more storage device drivers that implement the "storage device interface" you previously designed.
  • Use the "storage device interface" you previously designed to write to the disk.
Note that to write individual byte/s, you have to read a whole sector (if it's not cached), modify it in RAM, then write a whole sector. This may be done by the storage device driver itself (e.g. if your "storage device interface" allows byte access), or it might be done by the file system code (e.g. if your "storage device interface" only supports blocks/sectors).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

Ok thanks, you and JamesM need to have a solid tutorial on this stuff :wink:

Also the link in your signature is broken [-X :)

Thanks!!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to implement a filesystem

Post by Combuster »

Ok thanks, you and JamesM need to have a solid tutorial on this stuff
See the Beginner Mistakes why not.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4709
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to implement a filesystem

Post by iansjack »

wxwsk8er wrote:Ok thanks, you and JamesM need to have a solid tutorial on this stuff :wink:
OS development tutorials cause more problems than they solve IMO. They encourage people who aren't prepared to put in much effort to copy code parrot-fashion with no understanding of what they are doing. And they are always going to contain errors which people take as gospel and repeat.

To have any success in this sort of low-level programming you must first learn how to do research. All the information that you need is readily available in the form of documentation of hardware and source code for operating systems ranging from the simplest to Linux, FreeBSD, OS X, and the like.

You could do worse than buying one of the many books explaining how the Linux kernel works (similar are available for BSD and OS X) and studying them.
User avatar
deleted
Member
Member
Posts: 82
Joined: Mon Jul 21, 2014 7:23 pm

Re: How to implement a filesystem

Post by deleted »

I agree with you, I meant more of a theory tutorial. But anyway...
Locked