13-Operating Systems - File systems
13-Operating Systems - File systems
Dr. M. Brindha
Associate Professor
Department of CSE
NIT, Trichy-15
Two views of a file
system
protection
rw
attributes
system calls
x
Application View
Look & Feel
File system
Hardware view
2
File
s
• From a user’s perspective,
– A byte array
– Persistent across reboots and power failures
• From OS perspective,
– Secondary (non-volatile) storage device
• Hard disks, USB, CD, etc.
– Map bytes as collection of blocks on storage device
3
Application View
4
Files
• named...
• accessed...
• used... L.
Tarrataca
• protected...
• implemented...
• and managed
– Solution : Use open system call to open the file before any
access
(and close system call to close the file after all accesses are
complete)
9
File
Naming File Naming
names;
File Structure
Files can be structured in any of several ways
(1/3):
L.
sequences:
• Maximum flexibility;
• Unix, Linux, OS X and
Windows use this model;
File
Structure
Files can be structured in any of several ways
(2/3):
File
Structure
Chapter 4 - File
File File Typ
s es
File
Types
Chapter 4 - File
File File Acces
s s
File
Access
Chapter 4 - File
File Attributes
• Name – only information kept in human-readable form
• Identifier – unique tag (number) identifies file within file system
• Type – needed for systems that support different types
• Location – pointer to file location on device
• Size – current file size
• Protection – controls who can do reading, writing, executing
• Time, date, and user identification – data for protection, security,
and usage monitoring
• Information about files are kept in the directory structure, which is
maintained on the disk
• Many variations, including extended file attributes such as file
checksum
• Information kept in the directory structure
A File’s Metadata
(inodes)
• Name. the only information kept in human readable
form.
• Identifier. A number that uniquely identifies the file within
the file system. Also called the inode number
• Type. File type (inode based file, pipe, etc.)
• Location. Pointer to location of file on device.
• Size.
• Protection. Access control information. Owner, group
(r,w,x) permissions, etc. a
• Monitoring. Creation time, access time, etc.
File Attributes
OS keep track of a wide range of information regarding a
file:
L.
21 / 161
Tarrataca
File
Operations
Chapter 4 - File
File File
s Operations
File
Operations
• Caller must specify how many bytes to read and buffer to place
data;
Chapter 4 - File
File File
s Operations
• If the current position is in the middle of the file, existing data are
overwritten;
Chapter 4 - File
File File
s Operations
• Append: restricted form of write. It can add data only to the end of
the file;
L.
27 / 161
Tarrataca
Chapter 4 - File
File File
s Operations
Chapter 4 - File
Example Program Using File System
Calls (1)
...
Figure 4-5. A simple program to copy a
file.
Opening a
File
• Steps involved
– Resolve Name : search directories for file
names and check permissions
– Read file metadata into open file table
– Return index in the open file table (this is the
familiar file descriptor)
30
Open file
tables
• Two open file tables used
– system wide table
• Contains information about inode, size, access
dates, permission, location, etc.
• Reference count (tracks number of processes that
have opened the file)
– per process table
• Part of PCBs proc structure
• Pointer to entry in the system wide table
31
Open file
• tables
When a process calls open(), a new entry is created
in the System-wide open file table.
• A pointer to this entry is stored in the process's file
descriptor table.
• The file descriptor table is a simple array of pointers
into the open file table.
• The index into the file descriptor table - a file
descriptor.
• It is this file descriptor that is returned by open().
• When a process accesses a file, it uses the file
descriptor to index into the file descriptor table and
locate the corresponding entry in the open file table.
32
Open file
tables
33
Open file
tables
The open file table contains several pieces of information
about each file:
•the current offset (the next position to be accessed in the
file)
•a reference count
•the file mode (permissions),
•the flags passed into the open() (read-only, write-only,
create, &c),
•a pointer to an in-RAM version of the inode (a slightly light-
weight version of the inode for each open file is kept in RAM
-- others are on disk),
•A pointer to the structure containing pointers to the
functions that implement the behaviors like read(), write(),
close(), lseek(), &c on the file system that contains this file.
34
Open file
Each entry in tables
the open file table maintains its own
read/write pointer for three important reasons:
•Reads by one process don't affect the file position in
another process
•Write are visible to all processes, if the file pointer
subsequently reaches the location of the write
•The program doesn't have to supply this information
each call.
35
inode
• Every inode in the Linux structure has a unique
number identified with it. It is also called the index
number and has the following attributes:
• Size
• Owner
• Date/time
• Permissions and access control
• Location on the disk
• File types
• Number of links
• Additional metadata about the file
36
File System
Operations
Directory operations:
• modify the names space of files.
• Examples include mkdir(), rename(), creat(), mount(),
link() and unlink()
File operations:
• obtain or modify the characteristics of objects.
• Examples include stat(), chmod(), and chown()
I/O operations
• access the contents of a file
• I/O operations, unlike file operations, modify the
actual contents of the file, not metadata associated
with the file.
• Examples include read(), write(), and lseek().
• These operations are typically much longer than the
other two. 37
A File System
Organization
39
Directori Hierarchical Directory
es Systems
Directori
es
L.
Tarrataca
• Tree of directories;
Directori Hierarchical Directory
es Systems
L.
41 / 161
Tarrataca
Chapter 4 - File
Directori Path
es Names
Path
Names
Chapter 4 - File
Directori Path
es Names
Chapter 4 - File
Directori Directory
es Operations
Directory
Operations
Chapter 4 - File
Directori Directory
es Operations
Directory
Operations
Chapter 4 - File
Directori Directory
es Operations
Directory
Operations
Chapter 4 - File
Protectio
n
• Types of access
– Read, write, execute, …
• Access Control
– Which user can use which file!
• Classification of users
– User, group, others
47
Mounting a File
System
• Just like a file needs to be opened, a file system needs
to be mounted
• Mounting a file system attaches that file system to a
directory (mount point -root) and makes it available to
the system.
OS needs to know
– The location of the device (partition) to be mounted
• – The location in the current file structure where the file system is
to be attached
$ mount /dev/sda3 /media/xyz -t ext3
OS does,
– Verify that the device has a valid file system
– Add new file system to the mount point (/media/xyz)
48
Implementing a File System
49
FS
Layers
Application View
51
file system : in-memory
contents
• Mount table : contains information about each mounted
volume
$ cat /etc/fstab
52
File System
Implementation
Now that we know all the main file system concepts:
Chapter 4 - File
File System File
Implementation System Layout
Chapter 4 - File
File System File
Implementation System Layout
Chapter 4 - File
File System File
Implementation System Layout
L.
56 / 161
Tarrataca
Chapter 4 - File
File System File
Implementation System Layout
• Number of blocks;
L.
57 / 161
Tarrataca
• Etc...
Chapter 4 - File
File System File
Implementation System Layout
Chapter 4 - File
File operations
(create)
• File Creation
1. Create FCB for the new file
2. Update directory contents
• .3. Write new directory contents to disk (and may cache it as well)
59
File Operations
•
(open)
File Open
1. Application passes file name through open system call
2. sys_open searches the system-wide open file table to see if the file is
already in use by another process
• If yes, then increment usage count and add pointer in per-process open
file table
• If no, search directory structure for file name (either in the cache or disk)
add to system-wide open file table and per-process open file table
3. The pointer (or index) in the per-process open file table is returned to
application. This becomes the file descriptor
60
File operations
(close)
• file close
– Per process open table entry is removed
– System wide open table reference count
decremented by 1.
• If this value becomes 0 then updates copied back
to disk (if needed)
• Remove system wide open table entry
61
File Operations
(read/write)
• File Read
62
File Access
Methods
• Sequential Access
– Information processed one block after the
other
– Typical usage
• Direct Access
– Suitable for database systems
– When query arrives, compute the
corresponding block number, and directly
access block
63
Tracking Free
Space
• Bitmap of blocks
– 1 indicates used, 0 indicates free
• Linked list of free blocks
64
Allocation
Methods
• How does the OS allocate blocks in the
disk?
– Contiguous allocation
– Linked allocation
– Indexed allocation
65
Contiguous
Allocation
• Each file is allocated contiguous blocks on the disk
• Directory entry keeps the start and length
• Allocation
– First fit / best fit ?
• Advantages
– Easy / simple
• Disadvantages
– External fragmentation
(may need regular defagmentation)
– Users need to specify the
maximum file size at creation
(may lead to internal fragmentation – a file may request a much large space
and not use it)
66
File System Implementing the
Implementation files
Contiguous
Allocation
Idea: Store each file as a contiguous run of disk
blocks:
Figure: Contiguous allocation of disk space for seven files (Source: [Tanenbaum and Bos,
2015])
Chapter 4 - File
File System Implementing the
Implementation files
• If file A was 3 1/2 blocks, some space is wasted at the end of the last
68 / 161
Tarrataca
block;
Chapter 4 - File
File System Implementing the
Implementation files
Chapter 4 - File
File System Implementing the
Implementation files
operation;
Chapter 4 - File
File System Implementing the
Implementation files
Figure: The state of the disk after files D and F have been removed. (Source: [Tanenbaum and Bos,
2015])
Chapter 4 - File
File System Implementing the
Implementation files
immediately:
• Potentially millions of blocks to compact...
• Disastrous perfomance;
Chapter 4 - File
File System Implementing the
Implementation files
• However, eventually the disk will fill up, then two solutions
exist: L.
Tarrataca
73 / 161
Chapter 4 - File
File System Implementing the
Implementation files
Can you think of any other type of method for implementing files?
Chapter 4 - File
Linked
Allocation
• Directory stores link of start and end block
(optionally)
• Pointer in block store link to next block
• Advantages
– Solves external fragmentation
• problems
– Not suited for direct access of files (all
Disadvantages
pointers need to be accessed)
– Pointer needs to be stored ..
overheads!!
• Overheads reduced by using clusters (ie. cluster of
– sequential blocks associated with one pointer)
Reliability.
• If a pointer is damaged (or lost), rest of file is lost.
• A bug in the OS may result in a wrong pointer
being picked up.
75
File System Implementing the
Implementation files
Linked-List Allocation
L.
76 / 161
Tarrataca
Figure: Storing a file as a linked list of disk blocks. (Source: [Tanenbaum and Bos,
2015])
Chapter 4 - File
File System Implementing the
Implementation files
• Directory entries merely need to store the disk address of the first
block:
• Rest can be found starting there.
Chapter 4 - File
File System Implementing the
Implementation files
Chapter 4 - File
File System Implementing the
Implementation files
Can you think of any other methods for implementing a file? Any
ideas?
L.
79 / 161
Tarrataca
Chapter 4 - File
Indexed
Allocation
• Advantages,
– Supports direct access
– No external fragmentation
– Easy to grow files
• How large should the index block be?
– Files typically, one or two blocks long
• The index block will therefore have only one or two
entries
• A large index block huge wastage
– A small index block will limit the size of
file
• Need an additional mechanism to deal with large
files
• Disadvantage, Use disk blocks as index blocks
– Sequential access may be that don't hold file data, but hold
• May use clusters
slow pointers to the disk blocks that
hold file data.
80
FAT
File
(a variation linked allocation scheme)
• Invented by Marc McDonald and Bill Gates
• FAT is a table that
– contains one entry for each block
– and is indexed by block number.
• Files represented by linking pointers in
the FAT
• FAT table generally cached
• Advantages,
– Solves direct access
problems of linked
allocation
– Easy to grow files
– Greater reliability
•
• A bad block implies only
one block is corrupted
Disadvantages,
– Volume size determined
by FAT size
81
File System Implementing the
Implementation files
L.
82 / 161
Tarrataca
Chapter 4 - File
File System Implementing the
Implementation files
end;
• Chain is terminated with a special marker (e.g., -1)
• File B: start with block 6 and follow the chain until the
end;
• Chain is terminated with a special marker (e.g., -1)
Chapter 4 - File
File System Implementing the
Implementation files
Can you see any any advantages with linked-list allocation using a
table in memory? Any ideas?
L.
84 / 161
Tarrataca
Chapter 4 - File
File System Implementing the
Implementation files
Can you see any any advantages with linked-list allocation using a
table in memory? Any ideas?
Chapter 4 - File
File System Implementing the
Implementation files
Chapter 4 - File
File System Implementing the
Implementation files
Chapter 4 - File
File System Implementing the
Implementation files
I-
Nodes
Chapter 4 - File
File System Implementing the
Implementation files
L.
89 / 161
Tarrataca
How does this scheme compare against linked files using an in-
memory table? Any ideas?
L.
file is open:
Chapter 4 - File
File System Implementing the
Implementation files
I.node array:
• Usually far smaller than the space occupied by the file table
approach;
• Reason is simple:
• Table holding all disk blocks is proportional in size to the disk itself;
L.
Tarrataca
91 / 161
Chapter 4 - File
File System Implementing the
Implementation files
Can you see any problem with the i-nodes approach? Any
ideas?
One solution: reserve the last disk address not for a data block:
Chapter 4 - File
File System Implementing the
Implementation files
What happens when a file grows beyond this limit? Any ideas?
One solution: reserve the last disk address not for a data block:
L.
93 / 161
Tarrataca
Chapter 4 - File
File System Implementing the
Implementation files
L.
95 / 161
Tarrataca
Chapter 4 - File
Multi Level
Indexing
• Block index has multiple levels
96
File System Implementing Directo
Implementation ries
Implementing
Directories
Before a file can be read:
blocks:
entry:
Figure: Simple directory containing fixed-size entries with the disk addresses and attributes in the
directory entry. (Source: [Tanenbaum and Bos, 2015])
Chapter 4 - File
File System Implementing Directo
Implementation ries
Figure: Simple directory containing fixed-size entries with the disk addresses and attributes in the
directory entry. (Source: [Tanenbaum and Bos, 2015])
L.
99 / 161
Tarrataca
• Attributes:
• Creator, Time, etc.
Chapter 4 - File
File System Implementing Directo
Implementation ries
L. 100 /
Tarrataca 161
Figure: A directory in which each entry just refers to an i-node. (Source: [Tanenbaum and Bos,
2015])
Chapter 4 - File
Magnetic Disks
101
Magnetic
Disks Tracks and Sectors in a platter
10
Disk
Controllers
Processor Processor Processor Processor
1 2 3 4
10
Access
Time
• Seek Time
– Time it takes the head assembly to travel to the desired track
– Seek time may vary depending on the current head location
– Average seek time generally considered.(Typically 4ms high end
servers to 15ms in external drives)
• Rotational Latency
– Delay waiting for the rotation of the disk to bring the required disk
sector under the head
– Depends on the speed of the spindle motor
• CLV vs CAV
• Data Rate
– Time to get data off the
disk
10
Disk
Scheduling
• Objectives Access time and bandwidth can be
– Access time managed by the order in which
Disk I/O requests are serviced
• Two components
– Minimize Seek time
– Minimize Rotational latency
– Bandwidth
• Bytes transferred / (total time taken)
10
Disk
Scheduling
• Read/write accesses have the following cylinder
queue :
95, 180, 34, 119, 11, 123, 62, 64
• The current position of the head is 50
• FCFS Track number
time
Wild Oscillations
Track number
time
• Counterpart of SJF
• Could lead to starvation
10
Elevators
95, 180, 34, 119, 11, 123, 62, 64at 50
Starting
SCAN
• Start scanning toward the nearest end
and goes till 0
• Then goes all the way till the other
end
Total head movements = 230
C-SCAN
• Start scanning toward the nearest end
and go till the 0
• Then go all the way to the other end
Total head movements = 187
•Useful if tracks accessed with uniform
distribution
•Shifting one extreme not included in
head movement count
11
C-LOOK
• 95, 180, 34, 119, 11, 123, 62, 64
• Starting at 50
• Like C-SCAN, but don’t go to the extreme.
• Stop at the minimum (or maximum)
11
Thank
you