2

I have learnt from some sources (such as: this), the following things:

  • An inode is a data structure that stores relevant information about a file.
  • An inode number points to an inode.
  • There is a separate inode table which maintains the mapping information of inode number with corresponding inodes.
  • On creation of a file, the inode number and filename are assigned to a file.
  • When a file is accessed with a filename, internally, the name is first mapped with its inode number and the corresponding inode is accessed.

What I do not understand is mostly the last line. The inode accesses the inode, but the inode is a separate entity apart from the file. And the descriptions given do not say that an inode in any way is associated with the file, just that an inode is a data structure that contains the metadata related with the file. How is the file accessed then? Does the inode invoke the file?

  • I recommend reading the book "Understanding the Linux Kernel" by Neil Mathew and Richard Stones. – Emmanuel Rosa Jul 7 '17 at 3:40
4

The inode is the file, which is identified solely by its inode number. A file name is just metadata in the file system that refers to a file. A single file/inode can have multiple file names referring to it:

$ touch foo
$ ln foo bar
$ ls -li foo bar
28098391 -rw-r--r--  2 xxxxxxx  xxxxx  0 Jul  6 22:15 bar
28098391 -rw-r--r--  2 xxxxxxx  xxxxx  0 Jul  6 22:15 foo

The first column is the inode number; note that it is the same for both files. The first number after the permissions is the link count; both foo and bar have a link count of 2 because each of the two names refers to the same file.

$ rm foo
$ ls -li bar
28098391 -rw-r--r--  1 xxxxxxx  xxxxx  0 Jul  6 22:15 bar

rm foo only removes that specific link to the underlying file; bar still refers to it (note the inode number has not changed, but the link count is now 1 instead of 2). Also note that neither foo nor bar was the "real" name of the file; the fact that foo was created first didn't make it special in any way. A file is not actually removed until all links are removed (i.e., until the link count is reduced to 0). Even then, the file is not actually removed; the blocks allocated to that inode are simply marked as available for reuse by the file system.

  • "the inode is the file"? but, it says that it is a data structure, that contains the metadata of the file. How is the metadata contained within the file? How is the file subdivided to contain such information? – mathmaniage Jul 7 '17 at 3:29
  • 2
    The inode is the file in the following sense: if you are given the inode, you know what type of file this is, how big it is, what permission bits it has and where on the disk all its data are stored. IOW, everything about the file is known if you are given the inode, except its name (and as @chepner points out, that may not be unique). What you should work through is what happens when you say cat myfile. Briefly: the string "myfile" is looked up in the current directory and the inumber of the file is obtained. From the inumber, we can get the inode. From that we can get everything else. – NickD Jul 7 '17 at 3:54
  • @Nick, Then, what distinction does there lie between the inode and the file, meaning, how can we separate them from each other(more practically)? – mathmaniage Jul 7 '17 at 5:22
  • You cannot. You are thinking of the directory entry as being the file, but it isn't. Directory entries are just non-unique bits of filesystem metadata that refer to anonymous files/inodes. – chepner Jul 7 '17 at 12:04
1

Here is a simplified explanation for a regular file ...

Humans use filenames to refer to files. The kernel uses numbers called inode numbers to refer to files. A directory is a mapping between a filename and an inode number. An inode contains the metadata associated with the file and a pointer to the first data block of the file. The first data block contains a pointer to the second data block, and so on until the last data block in read.

  • 1
    Inodes are not numbers. There is an inode number (inumber for short), which is stored along with the name in a directory entry. The inumber is essentially an index into an array of inodes on disk. This is really how early Unix filesystems worked, although now things are more complicated. – NickD Jul 7 '17 at 4:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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