1 UNIX File System
1 UNIX File System
Superblock
Inodes
Disc
Datablock
Fragment
Datablock
1
system / is always available on a machine while other parts can be integrated (mounted) into the file
system. Figure 2 b) shows a diskette file system mounted on the directory b, figure 2 a) shows the user
view of the file system after the diskette is mounted on directory b.
Hard
disk Hard
disk Diskette
/ /
/
a b a x y z
b
p q r
p q r
a) How it looks like b) How it is implemented
Important: A file does NOT have a name. The file is uniquely identified by its inode number (the
symbolic name of a file is stored in the encosing directory, not in the inode).
The file mode is normally referred to as permissions, but it also contains information about the type
of file. Normally when you do a ls -l, you see something like this:
2
Bootblock
Superblock
Inodes
Inode number: 15
Filemode
Owner
Datablock Timestamps
Size
Reference Count
Fragment Pointers to data
Datablock
Figure 3: An inode
Permissions are the low-order 9 bits of the mode bytes and they define who may do what with the
file. The bits are normally presented like rwx, where r, w and x stand for read, write, and execute,
respectively. For each file this is defined for:
• the owner (the first 3 bits)
• the owner’s group (the next 3 bits)
• everyone else (the last 3 bits)
The chmod command lets you change the permission mode of a file.
There are three timestamps defined:
• modification time - when the file was last changed
• access time - when the file was last read
• status time - when certain changes was made to the inode
Through pointers in the inode we can access the file’s data blocks. For reasons of disk space efficiency,
there are :
• 12 direct pointers
• 1 single indirect pointer
• 1 double indirect pointer
• 1 triple indirect pointer
Figure 4 shows how data blocks are accessed using the direct pointers and figure 5 shows how they are
accessed using the single indirect pointer.
If the file consists of 12 or fewer data blocks we can access them directly from the 12 direct pointers
in the inode. For block sizes of 4 Kbytes or 8 Kbytes, this means that files up to 48 Kbytes or 96 Kbytes,
respectively, can be accessed entirely from the information in the inode.
The single indirect pointer is necessary in order to create files of more than 12 data blocks. The
single indirect pointer points to a single data block, whose contents are treated as direct pointers to data
blocks. We can now use a couple of hundred data blocks (files of a few MB).
The double indirect pointer is necessary in order to create file of more than a few MB. The double
indirect pointer points to a data block whose contents are treated as single indirect pointers. Each of
these pointers points to a data block, whose contents are treated as direct pointers to data blocks. This
is enough to reach the file size limit on most systems.
3
inode
data blocks
single indirect
double indirect
triple indirect
data blocks
direct
single indirect pointers
double indirect
triple indirect
1.3 Directories
Directories are files, but they are treated differently. A directory can be identified by its mode bytes. A
directory is a file that consists of a number of records, each of which contains the following fields:
If you type ls -li in the directory in figure 6, you will have the following output (the numbers in
the first column are the inodes of the files):
17 -rw-r--r-- 1 (...) foo.c
29 -rwxr-xr-x 1 (...) hej
17 5 foo.c 29 3 hej
4
27 4 test 51 5 bar.c
inode 27 inode 51
ref=1 ref=1
27 4 test 27 5 bar.c
inode 27
ref=2
1.4 Links
1.4.1 Hard links
By associating a name in a directory with a file we get what is known as a hard link (sometimes called
simply link). Do not confuse these hard links with symbolic links(see below).
The directory in figure 7 contains links to inodes 27 and 51, so we can refer to file 27 as ”test” and
file 51 as ”bar.c”.
> ls -li
51 lrwxrwxrwx 1 (...) bar.c
27 -rw-r--r-- 1 (...) test
> ls -li
27 -rw-r--r-- 2 (...) bar.c
27 -rw-r--r-- 2 (...) test
Hard links can be created using the command ln in the shell or the link system call.
5
27 4 test 51 5 bar.c
inode 27 inode 51
ref=1 ref=1
"link" test
27 4 test 51 4 mail
inode 27 inode 51
ref=1 ref=2
(to parent)
51 1 . ..
6
among systems or among disks within a single system. It is also not convenient. The easy and portable
method is to use the three standard functions opendir, readdir, and closedir. This is how they are
used in C:
DIR *d;
struct dirent *f;
d=opendir("nameofdirectory");
while (f=readdir(d)) {
// (use f)
}
closedir(d);
struct stat s;
d=opendir("nameofdirectory");
while (f=readdir(d)) {
//(use f)
stat(f->d_name, &s);
//(use s)
}
closedir(d);