You are on page 1of 66

File System - 1

© Ravi Subramanian
File Types

© Ravi Subramanian
Overview
❑ Building block of every Operating System
❑ All the operations involve files

$<command> Get the file for the Load the program in


$ next command command from disk memory

Execute the program

Finish execution

© Ravi Subramanian 3
Unix and POSIX systems
❑ Wide range of file types
❑ Common set of interfaces to files
❑ Ease of application development

© Ravi Subramanian 4
File Types
/

/home /dev

block device files


/home/user
/dev/tty1 /dev/blk1
Directory files
character device files
projectfiles
datapipe
Pipe files

file1.c file2.c a.out

Regular files

© Ravi Subramanian 5
Regular Files
❑ Text file, Binary files
❑ No distinction
❑ Text files - created/modified by
editors,
Directory files ❑ Binary files - created by
projectfiles compilers/other programs
❑ Exeutable files - execution rights
file1.c file2.c a.out are set
Regular files $ls -l a.out
_rwxrwxrwx a.out
$
© Ravi Subramanian 6
Directory Files
❑ File folder that contains other
files
❑ Created through mkdir
$mkdir dirname
Directory files
projectfiles
❑ Listed through ls command
$ls dirname
file1.c file2.c a.out ❑ Removed using rmdir command
Regular files rmdir dirname

© Ravi Subramanian 7
Character and Block Device Files

© Ravi Subramanian
Block Device Files
❑ Written/read a block at a
time
❑ Harddisk drives, pen drives,
solid state drives
❑ Block devices are accessed as
block of bytes
❑ ls -l /dev/sda0
``
❑ brw-rw-rw- /dev/
sda0

© Ravi Subramanian 9
Character Device Files
❑ Written/read one character at a time
❑ Console terminals, line printers, Serial ports
❑ /dev/tty<x> device file associated with the terminal
$ls -l /dev/tty1 #include <stdio.h> //print1.c
crw-rw-rw /dev/tty1 main() {
$cc print1.c -o print1 printf(“Hello, world on tty\
$print1 n”);
Hello, world on tty }
$ tty #include <stdio.h> //print2.c
/dev/tty1 main`(`) {
$cc print2.c -o print2 FILE *fp =
$print2 fopen(“dev/tty1”, “w”);
Hello, world on tty fprintf(fp, “Hello, world on
$ tty\n”);
}

© Ravi Subramanian 10
Character Device Files
/dev/tty1 /dev/tty2

$ tty $ tty
/dev/tty1 /dev/tty2
$cc print3.c -o print3 $ Hello, world on tty1
$print3 <print output goes to tty2> message to tty2
$ echo message to tty2 >/dev/tty2

#include <stdio.h> //print3.c


main`(`) {
FILE *fp = fopen(“/dev/tty2”, “w”);
fprintf(fp, “Hello, world from tty1\n”);
}

© Ravi Subramanian 11
Creating a Character Device File
❑ mknod command - device file creation
❑ mknod /dev/dname c <major dev number> <minor dev number>
❑ mknod /dev/tty5 c 4 5

❑ major dev number :


❑ Index into kernel table of device driver addresses
❑ Used to invoke the particular device driver

❑ Minor device number


❑ Indicates one of the device instances for a given device
❑ tty number, disk partition etc
❑ Argument to the device driver function

© Ravi Subramanian 12
Character Device Files
/dev/tty1
$ ls -l /dev/tty*
crw-rw-rw 4, 0 tty0
crw-rw-rw 4, 1 tty1
crw-rw-rw 4, 2 tty2
$ ls -l /dev/sda*
crw-rw-rw 7, 0 sda0
crw-rw-rw 7, 1 sda1
crw-rw-rw 7, 2 sda2

`` Major number 4 devicedriver4(device number){ .......}

Major number 7 devicedriver7(devKiceernnumber){


.......}
devicedriver8(device number){ .......}
Major number 8
Kernel Device table

© Ravi Subramanian 13
Creating a Block Device File
❑ mknod /dev/dname b <major dev number> <minor dev number>
❑ mknod /dev/bda4 b 8 4

❑ Can be run only super user

minor 1 Partition 1 minor 1 Partition 1 minor 1 Partition 1


minor 2 Partition 2 minor 2 Partition 2
Flash Drive
minor 3 Partition 3 minor 3 Partition 3
/dev/usb0
Hard Disk 0 Hard Disk 1 Major Device Number
7
/dev/hd0 /dev/hd1
Major Device Number Major Device Number 6
5

© Ravi Subramanian 14
FIFO Files

© Ravi Subramanian
FIFO File
❑ Temporary buffer for two or more processes
❑ Processes communicating writing and reading data into the file
❑ Data read cannot be read again

Program 1
FIFO File Program 3
Program 2

© Ravi Subramanian 16
FIFO File creation
❑ mkfifo command used to create a FIFO file
/dev/tty1

$ mkfifo fifo1
$ ls -l fifo1
prw-rw-rw- 1 user user 0 fifo1
❑ mkfifo call used to create a FIFO file in a program
❑ mkfifo(char *filename, int permissions)
❑ 0666 for read and write for everyone
#include <stdio.h> //mymkfifo.c /dev/tty1
main() { $ cc mymkfifo.c -o mymkfifo
mkfifo(“fifo2”, 0666); $ mymkfifo
} $ ls -l fifo2
prw-rw-rw- 1 user user 0 fifo1

© Ravi Subramanian 17
FIFO usage example
/dev/tty1 /dev/tty2

$ cc fifowrite.c -o fifowrite $ cc fiforead.c -o fiforead


$ fifowrite $ fiforead
$ ls -l fifofile Data on the pipe
prw-rw-rw- 1 user user 0 fifofile $

#include <stdio.h> //fifowrite.c #include <stdio.h> //fiforead.c


main() { main() {
char buf[] = “Data on the pipe\n”); char buf[100];
mkfifo(“fifofile”, `0` 666); int fd = open(“fifofile”,
int fd = open(“fifofile”, O_RDONLY);
O_WRONLY); read(fd, buf, sizeof(buf));
write(fd, buf, sizeof(buf)); close(fd);
close(fd); printf(buf);
} }

© Ravi Subramanian 18
Links

© Ravi Subramanian
Hard Links
❑ Reference to the local or remote pathname
❑ Commands will deference names to original
❑ File is deleted only when all links are removed
/dev/tty1

$ ln file1 file2
$ ls -l file1 file2
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1
-rw-rw-rw- 1 user user 8 Dec 8 20:59
$ cat file2 file2
<print contents of file1>
$rm file1 # Does remove file1.
$cat file2
<print contents of file1>

© Ravi Subramanian 20
Hard Links

Directory File System

File1 Hard Link


File1
File2

❑ Directory contains file names and associated links


❑ One physical file may have more than one hard link
❑ File is deleted only when all links are removed

© Ravi Subramanian 21
Symbolic (soft) Links
❑ Reference to the local or remote pathname
❑ Commands will deference symbolic names to original
❑ Removing original file removes the links as well
/dev/tty1

$ ln -s file1 file2
$ ls -l file1 file2
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1
lrw-rw-rw- 1 user user 8 Dec 8 20:59 file2 -> file1
$ cat file2
<print contents of file1>
$rm file1 # remove file1. This removes reference of file2
$cat file2
file2 : No such file

© Ravi Subramanian 22
Symbolic Links

Directory
File System
Hard Link
File1 File1

Symbolic Link
File2

❑ File2 is a symbolic link for File1


❑ Only one hardlink for File1
❑ When File is removed, File2 doesn't

© Ravi Subramanian 23
File System Hierarchy

© Ravi Subramanian
File Path Names
❑ Absolute path name : starts from root
❑ /bin/cat
❑ Current working direcory : /home/user1/pers
❑ test1.c in /home/user1 can be named as
❑ Relative path name : ../test1.c
❑ Full path name : /home/user1/test.c
❑ NAME_MAX : Max number of characters in File name
❑ PATH_MAX : Max number of characters in path name

© Ravi Subramanian 25
File System Hierarchy
/

/bin /etc /usr /sbin /home

cat ps /user1

test,c pers

root

directory

leaf node

© Ravi Subramanian 26
Important directories and files
❑ /etc : Admin files and programs
❑ /etc/password : User information
❑ /etc/shadow : User passwords
❑ /etc/group : Group information
❑ /bin : System programs
❑ /dev : Character and block device files
❑ /usr/include : Standard header files
❑ /usr/lib : standard libraries
❑ /tmp : Temporary files used by applications

© Ravi Subramanian 27
File Attributes

© Ravi Subramanian
File Types

File type Type of file (character, block, pipe, link)

Access Access permissions for owner, group and others


Permission
Hard link count Number of hard links to a file

User ID (UID) User ID of file owner

Group ID (GID) Group ID of file owner

File Size File size in bytes

Last access time Last time file was accessed

Last modify time Time when file was modified for any of access permission, UID,
GID or hard link count
Inode number System inode number of the file

File system ID File system ID where the file is stored

© Ravi Subramanian 29
File Attribute Listing
/dev/tty1

$ ls -al /dev/tty
crw-rw---- 1 ravi tty 4, 1 Dec 8 20:25 /dev/tty1 #character device
$ ls -al /dev/sda
brw-rw-rw- 5, 1 /dev/sda0 #block device major 5, minor
1 ls -l fifofile
$
prw-rw-rw- 1 user user 0 fifofile #FIFO File
$ls -l f1
-rw-rw-rw- 1 ravi ravi 107 Dec 8 23:37 x1 # Link count 1
$link f1 f2 # Create a new hard link
$ls -al f1 f2
-rw-rw-rw- 2 ravi ravi
`` 107 Dec 8 23:37 x1 # Hard link count = 2
rw-rw-rw- 2 ravi ravi 107 Dec 8 23:37 x2
$link f1 f3
$ls -al f1 f2 f3
-rw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x1 # Hard link count = 3
-rw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x2
-lrw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x3
© Ravi Subramanian 30
File Attribute - Access Permissions
 3 Bits for read, write, execute <r><w><x>
❑ One set of 3 bits for user, group and others
❑ <user rwx><group rwx><others rwx>
❑ 000 - No Read, No Write, No execute
❑ 100 (4) - Only read
❑ 110 (6) - Only read and write
❑ 111 (7) - Read, write and execute
❑ 777 - Owner, group and others can read, write and execute
❑ 666 - Owner, group and others can read and write

© Ravi Subramanian 31
Access Permissions

$ ls -l file1
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1 Disk
#all can read and write
Memory
$ ls -l file2
-rwxr-xr-x 1 user user 294 Nov 25 10:24 file2
Owner can read, write and execute, group can read and execute, ohers can
read and execute
$ ls -l file3
-rw-rw-r-- 1 user user 294 Nov 25 10:24 file3
Owner can read and write, group can read and write , ohers can read
$ chmod 777 file3 #change mode to 777
$ ls -l file3
-rwxrwxrwx 1 user user 294 Nov 25 10:24 file3
All can read, write and execute

© Ravi Subramanian 32
File Attributes that don't change
 File type : regular, directory, block, character,
pipe
❑ File inode number
❑ File system ID
❑ Major and Minor device numbers

© Ravi Subramanian 33
Changing File Attributes
Command System Call Attributes Changed

chmod chmod Changes file permissions

chown chown Changes UID, last change time

chgrp chgrp Changes GID, last change time

touch utime Changes last access time, modification time

ln link Increases hard link coumt

rm unlink Decreases hard link count. If count becomes 0, file will


be removed
editors (vi) Changes file size, last access time and last
modification time

© Ravi Subramanian 34
INODES

© Ravi Subramanian
Inodes
 Each file will have an Inode

 Each file system has an Inode Table

 Inode for a file contains file attributes


 Unique Inode number
 Physical disk addresses
 No file name stored

 Directory contains
 File names and associated inode numbers

© Ravi Subramanian 36
Inode

Directory File System

Inode Table
File1 23598 23598

File2 43578 43578

❑ Directory contains link between file name and


inode
❑ Inode contains file attributes and storage
locations

© Ravi Subramanian 37
inode listing

$ ls -i /usr
3940649674189555 bin Disk Memory
3377699720769213 lib
5348024557776511 share
1688849860505250 games
7318349394750941 local
12947848929298260 src
2251799813926563 include
4222124650933744 sbin
$

© Ravi Subramanian 38
File Access Using Inode
 Steps applied when user tries to access /usr/krish/myfile.c

1. System gets the inode number of /


2. System gets the inode number of usr from the contents of the
directory file /
3. System gets the inode number of krish from the contents of the
directory file /usr/
4. System gets the inode number of myfile.c from the contents of
the directory file /usr/krish
5. Get the physical data location of of myfile.c from its inode

© Ravi Subramanian 39
Inode
File System
/ 78532 Inode Table
78532

/ Directory 12321
usr 12321 42423
etc 42423 45333

/usr directory 52524

krish 45333 78787

david 52524 77767

/krish
myfile.c 78787

test1,c 77767

© Ravi Subramanian 40
Application Programming
Interface

© Ravi Subramanian
File System APIs
 Unix and Posix systems provide a set of Application Programming
Interfaces to access files
Type of Files Programs System Calls

Regular Files cat, vi, rm, cp, mv open, create, read, write, unlink, lseek,
fcntl, fstat, stat
Directory Files mkdir mkdir, mknod, rmdir

FIFO files mkfifo mkfifo, mknod, unlink

Device Files mknod mknod

Symbolic Links ln -s symlink, unlink

© Ravi Subramanian 42
Common File APIs
 File open : This can be done using open or creat API

 A process can open maximum of OPEN_MAX files as defined in


limits.h

 File attributes can be queried using fstat or stat

 File attributes can be changed using chmod, chown, utime and


link

 File can be removed using unlink

© Ravi Subramanian 43
stat structure
 Contains key information about file provide by stat or fstat call
 File system id
 File inode
 Hard link count
 User id of the owner
 Group id of the owner
 Major and minor device numbers
 Size of the file
 Access time
 Modification time
 Status change time

© Ravi Subramanian 44
Unix Kernel Support For Files

© Ravi Subramanian
Kernel Structures
 File table - keeps track of all the open files

 inode table - copy of the inodes of the files recently accessed

 Process maintains its own file descriptor table

© Ravi Subramanian 46
Inode
File System
/ 78532 Inode Table
78532

/ Directory 12321
usr 12321 42423
etc 42423 45333

/usr directory 52524

krish 45333 78787

david 52524 77767

/krish
myfile.c 78787

test1,c 77767

© Ravi Subramanian 47
Inode
File Descriptor
File Table
Table FIniloedSeyT File System

0 file1, r, rc = 1 satebmle Inode Table


78532
78532
1 file2, r rc = 2 12321
12321
2 file3, rw, rc = 1 42423
42423
3 file1, r file4, w, rc = 2 45333
45333
4 file2, r
52524
5 file3, rw
78787
6, file4, w 0
77767
1
Process 1
2

4 file2, r
Process 2 File
Descriptor 5, file4, w
Table

© Ravi Subramanian 48
File Open
 Steps executed when a process opens a file

 Kernel gets the inode for the file

 Kernel creates an entry in the process file descriptor table. This


number is returned to the open call

 Kernel creates an entry in the kernel file table

 Kernel connects the file descriptor table, kernel file table and
the inode table

© Ravi Subramanian 49
File Open
 Create an entry in the process file descriptor table
 Update the current pointer entry in the kernel file table
 Set the reference count to 1. This will be incremented if one more
process accesses the file
 Increment the reference count of the inode entry in the inode
table. This indicates how many file table entries are pointing to the
inode.

3 file1, r , r, rc = 1 ,78532 rc = 1 ,78532


File Kernel File File System
Kernel
Descriptor Table Inode Table
Inode Table
Table

© Ravi Subramanian 50
File Operations
 read and write calls update the file pointer

 Checks if the file can be accessed in the desired way based on file
permissions

 If permissible, kernel invokes the corresponding low level drivers


to read/write the data on the physical device.

© Ravi Subramanian 51
File Operations - lseek
 lseek is performed on a file descriptor
 Moves the file pointer to desired location
 Kernel checks if the file is a block device
 character, FIFO devices cannot use lseek operation
 If the lseek is compatible, kernel updates the file pointer in the file
table entry.

3 file1, r, lseek(fd, 100, 0) 3 file1, r,


fp = 345 fp = 100
File
Descriptor
Table 3 file1, r, lseek(fd, 100, 1) 3 file1, r,
fp = 345 fp = 445

© Ravi Subramanian 52
File Operations - close
 Sets the file descriptor entry as unused
 Decrements reference count in file table entry. If reference count
is 0, the entry is marked as unused
 Decrements reference count entry in the inode table entry
 If hardlink count in the inode is 0, all physical space occupied by
the file is freed up.

3 file1, r, 3 file1, r, 78532


unused rc = rc - rc = rc - 1
1
File Descriptor File Table Inode Table
Table

© Ravi Subramanian 53
Directory Files

© Ravi Subramanian
Directory File
 File of records - each record is an entry for a file

 Record structure
 struct dirent in Unix system V and POSIX
 struct direct in BSD UNIX
 Implementation dependent

struct dirent Inode Table


{
ino_t d_ino; // Inode Number
char // File Name
d_name[];
} File System

© Ravi Subramanian 55
Directory Functions
 Directory files can be accessed using open/read/lseek functions
 Use the structure defined in dirent.h
 Not portable as dirent structure is system dependent

 Portable functions for directory access


 opendir - opens a directory file
 readdir - gets the next entry in the directory
 rewinddir - positions to the beginning of the directory
 closedir - closes the directory file
 telldir - tells the position of the file pointer in the directory
 seekdir - seeks to a position in the directory file

© Ravi Subramanian 56
Directory Listing Code
#include <stdio.h>
#include <dirent.h>

int main(void)
{
struct dirent *dentry; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");

while ((dentry = readdir(dr)) != NULL)


printf("%s %ld\n", dentry->d_name, dentry->d_ino);
closedir(dr);
}

© Ravi Subramanian 57
Directory Search for a File Name
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
struct dirent *dentry; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
while ((detry = readdir(dr)) != NULL)
{
if (strcmp(dentry->d_name, argv[1]) == 0)
{
printf("%s is found in current directy\n", argv[1]);
break;
}
}
closedir(dr);
}

© Ravi Subramanian 58
Hard Links and Symbolic Links

© Ravi Subramanian
Hard Link
 Link : Path name for a file
 Link in the directory to the inode
 Typically, only one link for a given file

 New hard link can be created using ln command


 ln test1.c test2.c
 test1.c and test2.c will refer to the same file

© Ravi Subramanian 60
Hard Link
 Same inode for the linked file
$ ln test1.c test2.c
$ ls -i test1.c test2.c
12345 test1.c
12345 test2.c
Inode Table
inode 12345

test1.c 12345
test2.c 12345
abcd 34457
a.out 87765 File
File System
Directory Contents

© Ravi Subramanian 61
Difference between cp and ln
 cp creates new file in the system
$ cp test1.c test3.c
$ ls -i test1.c test2.c test3.c
12345 test1.c
12345 test2.c
Inode Table
34459 test3.c inode 12345
inode 34459
test1.c 12345
File
test2.c 12345
test3.c 34459
abcd 34458 File
a.out 87765
File System
Directory Contents

© Ravi Subramanian 62
Symbolic Link
 ln -s creates a symbolic link. It is a new file that contains the name
of the linked file
$ ln -s test1.c test4.c
$ ls -i test1.c test4.c
12345 test1.c
Inode Table
78524 test4.c inode 12345
inode 78524
test1.c 12345
File
test2.c 12345
test4.c 78524
abcd 34458 File
a.out 87765
File System

Directory Contents

© Ravi Subramanian 63
Constraints
 No hardlinks for directories. Prevents cyclic
links
 ln /user/krish/source/cfiles /user/krish

 No hardlinks for files across file systems


 Inode number is unique to file system

 Symbolic links can link files across file system

© Ravi Subramanian 64
Differences between Hard and Symbolic Links

Hardlinks Symbolic Links

No new inode is created New inode is created

Cannot link directories Can link directories

Cannot link across file systems Can link across file systems

Increases the hardlink count of the No change in the hard link count of the
inode inode

© Ravi Subramanian 65
Thank You

© Ravi Subramanian

You might also like