Pages

Thursday, March 23, 2017

What is a Filesystem and Hierarchy of Linux File system

List of Topics

1) What is File system

2) What is Block
3) About Linux File system Hierachy
4) What is Boot block
5) What is Super block
6) What is Inode
7) What is Dentry

1) What is File system

In General a HDD is partitioned into sections. Partition is divided into number of data blocks with a fixed size which are used to store content of file. . Each section of a hard disk contains a single type of file system. Each type of file system has its own set of rules for controlling the allocation of disk, space to files and for associating data about each file(metadata)

2) What is Block

A block is a uniformly sized unit of data storage for a file system. Block size will be an important consideration when setting up a system that is designed for maximum performance. In case if file is of greater size than of block then it is stored in multiple blocks

Block size is selected at the time of formatting, i.e., preparing the hard disk drive (HDD) or other media for creation of a filesystem. If the mke2fs (i.e., make ext2 filesystem) command is used to create the filesystem, 

dumpe2fs command with the device name as an argument. Thus, for example, to find the block size for the second partition of the first HDD, the following can be used:

#/sbin/dumpe2fs /dev/hda2 | grep 'Block size'

#blockdev --getbsz /dev/sda1
4096

3) About Linux File system Hierarchy

Linux File System is mainly divided  into following 5 blocks
a) Boot block
b) Super block
c) Data Block
d) iNode
e) Dentry

4) What is Boot block

Boot Block is located in the first few sectors of a the root file system. The boot block contains the initial bootstrap program used to load the operating system. 

5) What is Superblock

Superblock is the metadata of file system and has information about the file system type, size, status, and information about structures. Superblock is very critical for the file system so it is stored in multiple places for each file system. Superblock is a very "high level" metadata structure for the file system. 

Will see what does metadata means. Metadata is a piece of information about the data. For example, if you own a Bike, then you will be having some set of information about the Bike like bike number,Manufacturer name, Model, insurance information, and so on which is not part of the Bike itself. All of that information is collectively referred to as the metadata. Similarly in Linux file systems metadata exists which holds information about file system.

Super Block: it contains info about
1. type of filesystem (ext2, ext3...)
2. the block size
3. pointers to a list of free blocks
4. the inode number of the root directory5
5. magic number

For example, if the superblock of a /opt gets corrupt then the file system cannot be mounted by the operating system. Normally in this case, you need to run fsck which will automatically select an alternate, backup copy of the superblock and attempt to recover the file system. The backup copies themselves are stored in block groups spread through the file system with the first stored at a 1 block offset from the start of the partition. 

In case if you want to do a manual recovery then you may need to know information about superblock backups. Following  command will give the location of super blocks.

#dumpe2fs /dev/<Filesystem> | grep -i superblock 


Output:
dumpe2fs 1.43-WIP (20-Jun-2013)
  Primary superblock at 0, Group descriptors at 1-1

  Backup superblock at 163840, Group descriptors at 163841-163841
  Backup superblock at 32768, Group descriptors at 32769-32769

  Backup superblock at 98304, Group descriptors at 98305-98305

Suppose if dumpe2fs command outputs the Backup superblock at 32768, Group descriptors at 32769-32769. Then to repair it we may need to the command like below.

#/sbin/fsck -b 163840  /dev/<partition name>

6) What is Inode

An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. 

1. file ownership indication
2. file type (e.g., regular, directory, special device, pipes, etc.)
3. file access permissions. May have setuid (sticky) bit set.
4. time of last access, and modification
5. number of links (aliases) to the file
6. pointers to the data blocks for the file
7. size of the file in bytes (for regular files), major and minor  numbers for special devices
8. File types ( executable, block special etc )
9. Permissions ( read, write etc )
10. UID ( Owner )
11. GID ( Group )

An inode exists in, or on, a file system and represents metadata about a file. For clarity, all objects in a Linux or UNIX system are files; actual files, directories, devices, and so on. Please note that, among the metadata contained in an inode, there is no file name as humans think of it, this will be important 

Note that the mapping from dentries to inodes given by d_inode is in general a many-to-one mapping; a single file may be pointed to by multiple paths in the same filesystem (called "hard links"), in which case it will not be deleted as long as any such path exists.

Files and directories may also be opened by processes, of course, and a struct file is used to represent this.  The struct file contains a pointer to the dentry.  The underlying file will also not be deleted as long as there are processes holding the file open, even though that file may no longer be accessible by any path in the filesystem.

The namespace that a process sees, however, is normally made up of more than just one filesystem; instead it is patched together from multiple filesystems that are mounted on top of each other.  The structure of mountpoints is represented by a tree of vfsmount structures, one for each mountpoint.

7) What is Dentry 

A dentry is the glue that holds inodes and files together by relating inode numbers to file names. Dentries also play a role in directory caching which, ideally, keeps the most frequently used files on-hand for faster access. File system traversal is another aspect of the dentry as it maintains a relationship between directories and their files.

A filesystem is represented in memory using dentries and inodes.  Inodes are the objects that represent the underlying files (and also directories).  dentry is an object with a string name (d_name), a pointer to an inode (d_inode), and a pointer to the parent dentry (d_parent).

           /

            |
            test
            |   \
            file1  file2

is represented by four inodes: one each for foo, bar, and file2, and the root; and three dentries: one linking bar to test, one linking file2 to foo, and one linking foo to the root.  The first of those dentries, for example, has a name of "fille1", a d_inode pointing to the underlying file file1, and a d_parent pointing to the dentry for test (which in turn would have a d_parent pointing to the dentry for the root).  The root dentry has a d_parent that points to itself.

No comments:

Post a Comment