Question

A very basic question. What is the exact sequence of steps executed when I try to open a file-descriptor, by providing fully-qualified path to the file.

I am looking for

  • how this works on Linux
  • given a file name - how is it translated to inode and disk-sector
  • does linux file system (specifically ext3 and ext4) maintain some kind of filename to inode mapping?
Was it helpful?

Solution 2

In addition of tmp's answer:

Files really are inodes.

Usually, a given file has some entry in some directory pointing to its inode. Directories are mapping names to inodes, and directories are a kind of file. See stat(2) for what an inode contains (and can be queried by application code), in particular for the various file types (plain file, directory, char or block device, fifo, symlink, ....). So a directory is often a dictionnary (implemented in various, file system specific, ways) mapping strings to inodes. So in the directory /bin/ there is usually an entry associating bash to the inode for the ELF executable of the bash shell (i.e. /bin/bash). Use readdir(3) -which in turn calls getdents(2)- to read the entries in a directory.

It may happen that a given inode is not any more reachable by some name. This happens in particular when a process is open(2)-ing a file, then unlink(2)-ing it (while keep the opened file descriptor). This is the preferred way to make temporary files. (They will be released by the kernel when no more processes use them).

It may also happen that a given inode has several directories entries pointing to it. (i.e. a file has "several names") This happens with link(2) syscall. (symlink files are created with symlink(2)).

See also path_resolution(7) and unix file system & file system & ext2 & ext3 & ext4 & btrfs & comparison of file systems wikipages. Read also this old file system description (some details are rotten, but the general idea is here, notably the role of VFS)

OTHER TIPS

Kernel executes the following steps when we call "open(filepath)":

  1. convert the filename to inode.
  2. allocate a new file table entry for the inode, initialize various stuff(per process).
  3. allocate user file descriptor entry and store into the file table entry(global data structure).
  4. return user file descriptor to caller.

For complete information on internal algorithm you may want to refer the great great classic book "The Design Of UNIX Operating System" By Maurice J Bach which describes these concepts and the implementation in detailed way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top