19

Can please summarize the events/steps that happen when I try to execute a read()/write() system call. How does the kernel know which file system to issue these commands.

Lets say a process calls write(). Then It will call sys_write().

Now probably, since sys_write() is executed on behalf of the current process, it can access the struct task_struct and hence it can access the struct files_struct and struct fs_struct which contains file system information.

But after that I am not seeing, how this fs_struct is helping to identify the file system.

Edit: Now that Alex has described the flow...I have still doubt how the read/write are getting routed to a FS, since the VFS does not do it, then it must be happening somewhere else, Also how is the underlying block device and then finally the hardware protocol PCI/USB getting attached.

A simple flow chart involving actual data structures would be helpful

Please help.

3
  • 1
    "...when I try to execute a read()/write() system call" -- The file has to be opened first. See stackoverflow.com/questions/14501437/…
    – sawdust
    Commented Mar 24, 2015 at 18:14
  • If you are looking for a chain of function calls from all the way from read() to the appropriate file-system/hardware driver, its unlikely you will find one. Its mostly function-pointers and callbacks. One interesting technique a senior of mine often used was to deliberately perform a null-pointer dereference in the last/deepest/lowest function in the flow and get the stack-trace (list of last-to-first function calls) from the resulting Linux kernel oops messages. Commented Apr 29, 2015 at 4:04
  • @xeonphi, your original question here was "how the VFS identifies which file system should handle a read/write call". How that call propagates down into the block device layer and then to ATA/AHCI/etc. is a different question. If you are having trouble figuring it out, you can post another question for that.
    – Alex D
    Commented May 13, 2015 at 6:25

2 Answers 2

14
+50

This answer is based on kernel version 4.0. I traced out some of the code which handles a read syscall. I recommend you clone the Linux source repo and follow along in the source code.

  1. Syscall handler for read, at fs/read_write.c:620 is called. It receives a file descriptor (integer) as an argument, and calls fdget_pos to convert it to a struct fd.
  2. fdget_pos calls __fdget_pos calls __fdget calls __fget_light. __fget_light uses current->files, the file descriptor table for the current process, to look up the struct file which corresponds to the passed file descriptor number.
  3. Back in the syscall handler, the file struct is passed to vfs_read, at fs/read_write.c:478.
  4. vfs_read calls __vfs_read, which calls file->f_op->read. From here on, you are in filesystem-specific code.

So the VFS doesn't really bother "identifying" the filesystem which a file lives on; it simply uses the table of "file operation" function pointers which is stored in its struct file. When that struct file is initialized, it is given the correct f_op function pointer table which implements all the filesystem-specific operations for its filesystem.

4
  • Hi, thanks I went through the flow on Free electrons linux source...But I still don't quite understand....There has to be a mechanism to identify which file system type the read should be routed...may be not in VFS...then some where else...Can you please help with that. So essentially, how does the read get routed to particular FS, then to the underlying block device and finally the Hardware protocol, PCI, USB etc.
    – Haswell
    Commented Apr 29, 2015 at 3:43
  • 2
    Pay close attention to the last sentence of my answer. Each struct file has a table of function pointers which are called for reads, writes, and so on. The table which is used depends on the filesystem which the struct file belongs to. If the file is on an ext2 filesystem, then it will get the function pointer table for ext2. If it is on an NTFS filesystem, then it will get the function pointer table for NTFS. So calling file->f_op->read() will "automatically" get you into the correct read function for the filesystem you are using.
    – Alex D
    Commented Apr 29, 2015 at 5:36
  • 1
    Let me give you an example. Look at fs/ext2/file.c:93. (I really recommend you clone your own copy of the kernel source rather than using Free Electrons. Make sure you install cscope as well for searching the source.) This is the f_op table for ext2 files. As you can see, when file->f_op->read() is called for an ext2 file, the call will actually go to new_sync_read. That goes through the f_op table again to generic_file_read_iter, and from there to do_generic_file_read. If the needed page is already cached in memory, do_generic_file_read uses it.
    – Alex D
    Commented Apr 29, 2015 at 5:55
  • If not, it goes through the address_space_operations table defined at fs/ext2/inode.c:880 to ext2_readpage. That goes into the generic block read function mpage_readpage, which is passed a function pointer to ext2_get_block. It calls ext2_get_block to identify the exact location of the desired block on the underlying block device.
    – Alex D
    Commented Apr 29, 2015 at 5:55
0

Each filesystem registers itself to VFS. When a filesystem is mounted, its superblock is read and VFS superblock is populated with this information. Function pointer table for this filesystem is also populated at this time. when file->f_op->read call happens, registered function from the filesystem is actually called. You can refer to text in http://www.science.unitn.it/~fiorella/guidelinux/tlk/node102.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.