Linux filesystem
===
###### tags: `hcsm`, `linux`, `kernel`
# original design
* (ancient)FFS,
* ext2, ext3, ext4
* How to access file
~~~
fd = open("/home/...", ...flags);
~~~
* tree structure for the path
```
/ => home
=> etc
=> usr
```
* use unique number(inode number) to stand for the path
* There is something like TOC
* FD -> inode numbers -> read data
* root inode number
* fixed for each filesystem
~~~
disk partition
| boot | superblock | ....|
~~~
* superblock (per partition)
* where is
* inode data
* inode bmap for management inode
* used or not
* design from 1974. ken tompson
~~~
/ => 2
home => 35
~~~
* blocks
* 2: superblock
* 3: inode bitmap
* 4: data bitmap
* 5: inode data
* 6: data
* allocation group
http://web.cs.ucla.edu/classes/spring14/cs111/scribe/12e/bsd_ffs.gif![disk layout](https://i.imgur.com/M8AMGuD.png)
# journaling filesystem
* not atomic among writing bitmap/data/inode
* fsck
* journaling
* write operation into disk. e.g.
~~~
inode 53 checksum
Add Monkey 53 checksum
....
~~~
* parse journal if there is any error
* It's fast than parse every disk blocks
* The length of journal can be fixed or variant
* checksum for journaling error
* log-structed filesystem
* allocate new bitmap block when creating file
* faster than modify original block
* e.g. filesystem
* jffs2
* logffs
* where is the new bitmap
* fs reserve some blocks(e.g. 3~6)
* block version
* nilfs2
* can do snapshot
# data partition
* data block (ext2 => ext4)
* data
* pointer of data block
* up to 3 level
# VFS
* history
* data structure
* inode
* dcache
* page cache
* dereference entry
* cache mapping between name to inode
* hardlink vs softlink
* hardlink: same inode number
* softlink:
http://i.stack.imgur.com/f7Ijz.jpg![](https://i.imgur.com/6x8CBG5.png)
* ext4
* Original it's cached in page
* alloc data block only when writing to disk
* We can find better disk layout
# Q & A
1.
2. struct inode is the structure for one file in Linux
* when mapping to other filesystem, just fill file attribute struct data to it
3. IO scheduler
* CFQ for traditional disk to reduce disk rotate
* We don't need it (use NOP)