Saturday, August 23, 2014

Unix Prog: File Systems

1. Unix File System

Disk drive can be divided into one or more partitions, and each partition can contain a file system.
The i-node contains most of the information about one file, including the pointer pointing to the data blocks which store real file content.

At above diagram, we have 2 directories: one pointed by i_node 2, another one pointed by i-node1.

1st directory has 2 items: "." , "..", they are pointing to i-node 2 and i-node 1. 2nd directory has 3 items: ".", "..", "test", they are pointing to i-node 1, (unknown i-node), and i-node 2. 

Inside the structure i-node 1, it will point to first directory block at the right, inside the structure i-node 2, it will point to the second directory block in the middle.

Each i-node maintains one "link count", the number of how many items are pointing to this inode. i-node2 has 2 links: one from "." of own directory, another one from "test" item of parent directory.

Each directory i-node's link count = 2(one from item in parent directory, one from "." at local directory) + number of sub-directories inside this directory(each sub directory will maintain item ".." to point to parent i-node).

When i-node's link count reduced to 0, the data block content will be removed permanently.

Hard Link of file "abc"(assume its i-node is 100): create a new item pointing to i-node 100.

Symbolic Link of file "abc":(assume its i-node is 100): create a new item with own i-node who points to the data block containing the path of "abc".

i-node can only point to data block at the same file system, that's why "ln" command(create hardlink or symbolic link) can't cross the file system.

i-node contains most file basic information, like file size and permission bits. stat system call mainly get most information from file's i-node.

2. Track one file's i-node link count

fileio.c:
Write a small tool to get the link count of given file's i-node.
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<sys/stat.h>  
   
 int main(int argc, char* argv[])  
 {  
  struct stat buf;  
  if(lstat(argv[1], &buf) < 0) {  
   printf("lstat error for %s\n", argv[1]);  
   exit(1);  
  }  
   
  printf("link count: %d\n", (int)buf.st_nlink);  
   
  exit(0);  
 }  

shell:
1) List local file structure, we have one test directory, under that one, we have 2 directories called test1 and test2.
2) Run the program against test directory, link count is 4, one is from its parent's item, one is from its own ".", another two are from test1, and test2 ".." items.
3) Run the program against test1, and test2, link counts are 2. One is from parent's item, one is from local "." item.
 ubuntu@ip-172-31-23-227:~$ ls -lrt  
 total 24  
 -rw-rw-r-- 1 ubuntu ubuntu  245 Aug 23 19:01 fileio.c~  
 -rw-rw-r-- 1 ubuntu ubuntu  291 Aug 23 19:03 fileio.c  
 -rwxrwxr-x 1 ubuntu ubuntu 10592 Aug 23 19:04 io.out  
 drwxrwxr-x 4 ubuntu ubuntu 4096 Aug 23 19:04 test  
 ubuntu@ip-172-31-23-227:~$ ls -lrt test  
 total 8  
 drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 23 19:04 test1  
 drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 23 19:04 test2  
 ubuntu@ip-172-31-23-227:~$ ./io.out ./test  
 link count: 4  
 ubuntu@ip-172-31-23-227:~$ ./io.out ./test/test1  
 link count: 2  
 ubuntu@ip-172-31-23-227:~$ ./io.out ./test/test2  
 link count: 2  

No comments:

Post a Comment