Page 1 of 1

Filesystem Detection

Posted: Mon Nov 17, 2008 7:08 am
by AJ
Hi All,

I am currently playing around with a filesystem factory class system for my (C++) VFS. At the moment, I read the first sector of a given file system I would like to mount and the factory should return an appropriate registered file system. It does this by passing this first sector to a static member of the registered file system (the file system is responsible for detecting whether that system actually exists at the given location).

Does this seem a reasonable way to do things? I am generally familiar with FAT file systems, but are there circumstances where more informaiton than sector 0 will be needed to detect a file system type? If so, it would be possible to give the FS static member direct device access so that it can read from wherever it likes, but I was hoping to avoid each filesystem doing its own reads purely for detection purposes. (NB: I have taken in to account HD partition offsets, but am not trusting the MBR to tell me the FS type!).

I think I have previously seen some Linux FS detection code that I thought was called fsdetect, but Googling for this does not get the results I was hoping for - I should have bookmarked the code when I saw it ages ago!

Cheers,
Adam

Re: Filesystem Detection

Posted: Mon Nov 17, 2008 7:46 am
by jal
AJ wrote:but are there circumstances where more informaiton than sector 0 will be needed to detect a file system type?
Appearently yes. A quick Google turned up this and related this, discussing ways parted does stuff. From what I read there, it follows that sector 0 is not enough.


JAL

Re: Filesystem Detection

Posted: Mon Nov 17, 2008 7:59 am
by AJ
:oops:

Yup - thanks. I should have found that. Most of the stuff I got with Google was to do with detecting changes on file systems.

At the moment, I'm thinking that I should perhaps pass the first sector and a reference to the host device. FS's that can detect themselves using the initial sector can do a quick check, but other file systems can access the device directly. I guess I could just cache all disk reads associated with detection to reduce the necessity for repetetive disk accesses and speed up detection a bit.

Cheers,
Adam

Re: Filesystem Detection

Posted: Mon Nov 17, 2008 8:03 am
by c0x
Why don't you ask the filesystem driver which part of the partition is interested for it?

When you have the information you read the data and pass it to the FS driver which will tell you if it can mount the partition or not :)

Re: Filesystem Detection

Posted: Mon Nov 17, 2008 8:25 am
by AJ
That's pretty much what I mean I'll have to do from my previous post (although I may not have been very clear). I was just hoping that I could read a small area (one sector) of a HDD and that would be enough - this would avoid repetetive reads because each FS would be passed the same cached data.

It looks like I am out of luck, though. Jal's reply indicates that I will have to allow FS detection routines to directly r/w from the device (whether they do it via another part of the kernel or not).

Cheers,
Adam

Re: Filesystem Detection

Posted: Mon Nov 17, 2008 10:22 pm
by quok
In a past kernel of mine I had a generic "struct filesystem" which contained a number of members. I included things like the filesystem's magic number, and pointers to various routines, one of which was called read_superblock(). Each filesystem that was available registered a "struct filesystem" with the VFS that contained these members. All the structs were kept in a linked list, not sorted at all. The list was ordered by the way the filesystems registered themselves, most recent first.

When the VFS needed to detect what filesystem is located on a device, it would loop through the list of registered filesystems, calling read_superblock() and passing it the magic number it was looking for (or 0x0 for 'auto'), and the device node it was interested in (/dev/sdb1, for instance). The filesystem's read_superblock() method was allowed to interface with the block layer to read the superblock from anywhere on the device, and compare it to what was passed in (or to it's own idea of what the magic number should be). If the read in superblock passed all the filesystem-specific tests, the function returned 0, and the VFS would then use that filesystem's mount_fs() function.

I had planned on passing in just sector 0, but it's usually not enough, so I ended up not bothering with passing sector 0 at all and just allowed the filesystem driver to interface with the block layer, which was needed anyway in my design.

Re: Filesystem Detection

Posted: Tue Nov 18, 2008 2:43 am
by AJ
Thanks for this, quok.

After a couple more days of reading around, I think I'm going to do basically the C++ equivalent of what you suggest, without trying to pass a superblock to the driver.

A new FS will register a static mount function with the VFS helper. The mount function will double as a factory, so if the FS can be mounted, a pointer to the FS (with a FileSystem base class) will be returned. If not, a NULL pointer is returned.

I'm still just working on whether I will do any caching. Most FS's will want to read the first (or first few) sectors. It seems a waste to have each FS actually read from the disk individually. For this reason, I may cache all disk data during the mount and flush this data after a successful mount. Basically, I'm still working on it, but thanks for sharing the ideas!

Cheers,
Adam

Re: Filesystem Detection

Posted: Tue Nov 18, 2008 4:20 am
by JamesM
AJ wrote:Thanks for this, quok.

After a couple more days of reading around, I think I'm going to do basically the C++ equivalent of what you suggest, without trying to pass a superblock to the driver.

A new FS will register a static mount function with the VFS helper. The mount function will double as a factory, so if the FS can be mounted, a pointer to the FS (with a FileSystem base class) will be returned. If not, a NULL pointer is returned.

I'm still just working on whether I will do any caching. Most FS's will want to read the first (or first few) sectors. It seems a waste to have each FS actually read from the disk individually. For this reason, I may cache all disk data during the mount and flush this data after a successful mount. Basically, I'm still working on it, but thanks for sharing the ideas!

Cheers,
Adam

Hi,

You've just descibed verbatim the way I do filesystem detection. I also intend to implement caching at the sector level, but haven't at the moment.

I was going to give an in-depth reply but seeing as you've arrived at the same solution I arrived at, there doesn't seem much point!

Cheers,

James

Re: Filesystem Detection

Posted: Tue Nov 18, 2008 6:28 am
by AJ
JamesM wrote:You've just descibed verbatim the way I do filesystem detection.
Good to hear - that's just given me a whole lot more confidence to persist along this route :)

Cheers,
Adam

Re: Filesystem Detection

Posted: Tue Nov 18, 2008 6:25 pm
by quok
AJ wrote: I'm still just working on whether I will do any caching. Most FS's will want to read the first (or first few) sectors. It seems a waste to have each FS actually read from the disk individually. For this reason, I may cache all disk data during the mount and flush this data after a successful mount. Basically, I'm still working on it, but thanks for sharing the ideas!
I plan on not worrying about caching at all at the FS (or even VFS) level. I'll handle all caching in the block layer (when caching is needed, depending on filesystem mount and file open/creation flags), and handle all things like readahead there as well. So if a filesystem tells the block layer to read 2 sectors, it may just as well read 4, or even 8, so when the filesystem comes back and says "Give me the next 2 sectors" I don't have to hit the disk. Of course there's also reasons to not use caching at all and I plan on fully supporting that as well.