0% found this document useful (0 votes)
13 views

File System implementation

By our teacher this pdf is helpful will help in your studies STUDY HELPFUL PDF You will find it helpful and also very easy

Uploaded by

sweetshubhi662
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

File System implementation

By our teacher this pdf is helpful will help in your studies STUDY HELPFUL PDF You will find it helpful and also very easy

Uploaded by

sweetshubhi662
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

File-System Structure

• Hard disks have two important properties that


make them suitable for secondary storage of files
in file systems: (1) Blocks of data can be rewritten
in place, and (2) they are direct access, allowing
any block of data to be accessed with only (
relatively ) minor movements of the disk heads
and rotational latency.
• Disks are usually accessed in physical blocks,
rather than a byte at a time. Block sizes may
range from 512 bytes to 4K or larger.
• File systems organize storage on disk drives, and can be viewed as a layered design:
– At the lowest layer are the physical devices, consisting of the magnetic media, motors &
controls, and the electronics connected to them and controlling them. Modern disk put
more and more of the electronic controls directly on the disk drive itself, leaving
relatively little work for the disk controller card to perform.
– I/O Control consists of device drivers, special software programs ( often written in
assembly ) which communicate with the devices by reading and writing special codes
directly to and from memory addresses corresponding to the controller card's registers.
Each controller card ( device ) on a system has a different set of addresses ( registers,
a.k.a. ports ) that it listens to, and a unique set of command codes and results codes that it
understands.
– The basic file system level works directly with the device drivers in terms of retrieving
and storing raw blocks of data, without any consideration for what is in each block.
Depending on the system, blocks may be referred to with a single block number, ( e.g.
block # 234234 ), or with head-sector-cylinder combinations.
– The file organization module knows about files and their logical blocks, and how they
map to physical blocks on the disk. In addition to translating from logical to physical
blocks, the file organization module also maintains the list of free blocks, and allocates
free blocks to files as needed.
– The logical file system deals with all of the meta data associated with a file ( UID, GID,
mode, dates, etc ), i.e. everything about the file except the data itself. This level manages
the directory structure and the mapping of file names to file control blocks, FCBs, which
contain all of the meta data as well as block number information for finding the data on
the disk.
● Application Programs: This is the topmost layer where users interact with files through
applications. It provides the user interface for file operations like creating, deleting, reading,
writing, and modifying files. Examples include text editors, file browsers, and command-line
interfaces.
● Logical File system – It manages metadata information about a file i.e includes all details about
a file except the actual contents of the file. It also maintains via file control blocks. File control
block (FCB) has information about a file – owner, size, permissions, and location of file contents.
● File Organization Module – It has information about files, the location of files and their logical
and physical blocks. Physical blocks do not match with logical numbers of logical blocks
numbered from 0 to N. It also has a free space that tracks unallocated blocks.
● Basic File system – It Issues general commands to the device driver to read and write physical
blocks on disk. It manages the memory buffers and caches. A block in the buffer can hold the
contents of the disk block and the cache stores frequently used file system metadata.
● I/O Control level – Device drivers act as an interface between devices and OS, they help to
transfer data between disk and main memory. It takes block number as input and as output, it
gives low-level hardware-specific instruction.
● Devices Layer: The bottommost layer, consisting of the actual hardware devices. It performs
the actual reading and writing of data to the physical storage medium. This includes hard
drives, SSDs, optical disks, and other storage devices.
File-System Implementation

• File systems store several important data structures on the disk:


– A boot-control block, the boot block in UNIX or the partition boot sector in
Windows contains information about how to boot the system off of this disk.
This will generally be the first sector of the volume if there is a bootable
system loaded on that volume, or the block will be left vacant otherwise.
– A volume control block, the master file table in UNIX or the superblock in
Windows, which contains information such as the partition table, number of
blocks on each filesystem, and pointers to free blocks and free FCB blocks.
– A directory structure ( per file system ), containing file names and pointers to
corresponding FCBs. UNIX uses inode numbers, and NTFS uses a master file
table.
– The File Control Block, FCB, ( per file ) containing details about ownership,
size, permissions, dates, etc. UNIX stores this information in inodes, and NTFS
in the master file table as a relational database structure.
• There are also several key data structures stored in memory:
– An in-memory mount table.
– An in-memory directory cache of recently accessed directory
information.
– A system-wide open file table, containing a copy of the FCB for
every currently open file in the system, as well as some other
related information.
– A per-process open file table, containing a pointer to the system
open file table as well as some other information. ( For example
the current file position pointer may be either here or in the
system file table, depending on the implementation and whether
the file is being shared or not. )
• Figure illustrates some of the interactions of file system components when files are
created and/or used:
– When a new file is created, a new FCB is allocated and filled out with
important information regarding the new file. The appropriate directory is
modified with the new file name and FCB information.
– When a file is accessed during a program, the open( ) system call reads in the
FCB information from disk, and stores it in the system-wide open file table. An
entry is added to the per-process open file table referencing the system-wide
table, and an index into the per-process table is returned by the open( ) system
call. UNIX refers to this index as a file descriptor, and Windows refers to it as
a file handle.
– If another process already has a file open when a new request comes in for the
same file, and it is sharable, then a counter in the system-wide table is
incremented and the per-process table is adjusted to point to the existing entry
in the system-wide table.
– When a file is closed, the per-process table entry is freed, and the counter in the
system-wide table is decremented. If that counter reaches zero, then the system
wide table is also freed. Any data currently stored in memory cache for this file
is written out to disk if necessary.
Allocation Methods
There are three major methods of storing files on disks: contiguous, linked,
and indexed.
• Contiguous Allocation
• Contiguous Allocation requires that all blocks of a file be kept together
contiguously.
• Performance is very fast, because reading successive blocks of the same
file generally requires no movement of the disk heads, or at most one small
step to the next adjacent cylinder.
• Problems can arise when files grow, or if the exact size of a file is unknown
at creation time:
– Over-estimation of the file's final size increases external fragmentation and
wastes disk space.
– Under-estimation may require that a file be moved or a process aborted if the
file grows beyond its originally allocated space.
– If a file grows slowly over a long time period and the total final space must be
allocated initially, then a lot of space becomes unusable before the file fills the
space.
• Disk files can be stored as linked lists, with the expense of the
storage space consumed by each link. ( E.g. a block may be 508
bytes instead of 512. )
• Linked allocation involves no external fragmentation, does not
require pre-known file sizes, and allows files to grow dynamically at
any time.
• Unfortunately linked allocation is only efficient for sequential
access files, as random access requires starting at the beginning of
the list for each new location access.
• Another big problem with linked allocation is reliability if a pointer
is lost or damaged. Doubly linked lists provide some protection, at
the cost of additional overhead and wasted space.
Indexed Allocation

• Indexed Allocation combines all of the


indexes for accessing each file into a common
block ( for that file ), as opposed to spreading
them all over the disk or storing them in a FAT
table.

• Indexed allocation of disk space
• Some disk space is wasted ( relative to linked
lists or FAT tables ) because an entire index
block must be allocated for each file,
regardless of how many data blocks the file
contains. This leads to questions of how big
the index block should be, and how it should
be implemented.
What is Free Space Management in Operating System?

• The operating system manages the free space in the hard disk.
• This is known as free space management in operating systems.
• The OS maintains a free space list to keep track of the free disk
space.
• The free space list consists of all free disk blocks that are not
allocated to any file or directory.
• For saving a file in the disk, the operating system searches the free
space list for the required disk space and then allocates that space
to the file.
• When a file is deleted, the space allocated to it is added to the free
space list.
Methods of Free Space Management in OS

There are four methods of doing free space


management in operating systems. These are as
follows-
● Bit Vector
● Linked List
● Grouping
● Counting
Bit Vector

• In this method, each block in the hard disk is represented by


a bit (either 0 or 1).
• If a block has a bit 0 means that the block is allocated to a
file, and if a block has a bit 1 means that the block is not
allocated to any file, i.e., the block is free.
• For example, consider a disk having 16 blocks where block
numbers 2, 3, 4, 5, 8, 9, 10, 11, 12, and 13 are free, and the
rest of the blocks, i.e., block numbers 0, 1, 6, 7, 14 and 15 are
allocated to some files. The bit vector for this disk will look
like this-
Advantages
The advantages of the bit vector method are-
● It is simple to understand.
● It is an efficient method.
● It occupies less memory.
Disadvantages
The disadvantages of the bit vector method are-
● For finding a free block, the operating system may need to search the entire bit
vector.
● To detect the first 1 in a word that is not 0 using this method, special hardware
support is needed.
● Keeping the bit vector in the main memory is possible for smaller disks but not
for larger ones. For example, a 1.3 GB disk with 512-byte blocks would need a
bit vector of over 332 KB to track its free blocks. Giving away 332 KB just to
maintain its free block space is not so efficient in the long run.
Linked List
• Another method of doing free space management in
operating systems is a linked list. In this method, all the free
blocks existing in the disk are linked together in a linked list.
• The address of the first free block is stored somewhere in the
memory.
• Each free block contains a pointer that contains the address
to the next free block.
• The last free block points to null, indicating the end of the
linked list.
• For example, consider a disk having 16 blocks where block numbers
3, 4, 5, 6, 9, 10, 11, 12, 13, and 14 are free, and the rest of the blocks,
i.e., block numbers 1, 2, 7, 8, 15 and 16 are allocated to some files. If
we maintain a linked list, then Block 3 will contain a pointer to Block
4, and Block 4 will contain a pointer to Block 5.

• Similarly, Block 5 will point to Block 6, Block 6 will point to Block


9, Block 9 will point to Block 10, Block 10 will point to Block 11,
Block 11 will point to Block 12, Block 12 will point to Block 13 and
Block 13 will point to Block 14. Block 14 will point to null. The
address of the first free block, i.e., Block 3, will be stored somewhere
in the memory. This is also represented in the following figure-
Advantages
The advantages of the linked list method are-
● External fragmentation is prevented by linked list allocation. As opposed to contiguous
allocation, this prevents the wasting of memory blocks.
● It is also quite simple to make our file bigger. All we have to do is link a new file block to our
linked list. The file can so expand as long as memory blocks are available.
● Since the directory only needs to hold the starting and ending pointers of the file, linked list
allocation places less strain on it.
Disadvantages
The disadvantages of the linked list method are-
● This method is inefficient since we need to read each block to traverse the list, which takes
more I/O time.
● There is an overhead in maintaining the pointer.
● There is no provision for random or direct memory access in linked list allocation. We must
search through the full linked list to locate the correct block if we wish to access a certain file
block.
Grouping
• The third method of free space management in
operating systems is grouping. This method is the
modification of the linked list method. In this method,
the first free block stores the addresses of the n free
blocks. The first n-1 of these blocks is free. The last
block in these n free blocks contains the addresses of
the next n free blocks, and so on.
• For example, consider a disk having 16 blocks where
block numbers 3, 4, 5, 6, 9, 10, 11, 12, 13, and 14 are
free, and the rest of the blocks, i.e., block numbers 1, 2,
7, 8, 15 and 16 are allocated to some files.
If we apply the Grouping method considering n to be 3, Block 3 will
store the addresses of Block 4, Block 5, and Block 6. Similarly, Block 6
will store the addresses of Block 9, Block 10, and Block 11. Block 11
will store the addresses of Block 12, Block 13, and Block 14. This is
also represented in the following figure-
This method overcomes the disadvantages of the linked list
method. The addresses of a large number of free blocks can be
found quickly, just by going to the first free block or the nth free
block. There is no need to traverse the whole list, which was the
situation in the linked list method.
Advantages
The advantages of the grouping method are-
● The addresses of a large number of free blocks can be found quickly.
● This method has the benefit of making it simple to locate the addresses of a
collection of empty disk blocks.
● It's a modification of the free list approach. So, there is no need to traverse
the whole list.
Disadvantages
The advantages of the grouping method are-
● The space of one block is wasted in storing addresses. Since the nth block
is used to store the addresses of next n free blocks.
● We only save the address of the first free block since we are unable to
maintain a list of all n free disk addresses.
● There is an overhead in maintaining the index of blocks.
Counting

This is the fourth method of free space management in operating systems.


This method is also a modification of the linked list method. This method
takes advantage of the fact that several contiguous blocks may be allocated
or freed simultaneously. In this method, a linked list is maintained but in
addition to the pointer to the next free block, a count of free contiguous
blocks that follow the first block is also maintained. Thus each free block
in the disk will contain two things-
1. A pointer to the next free block.
2. The number of free contiguous blocks following it.
For example, consider a disk having 16 blocks where block numbers 3,
4, 5, 6, 9, 10, 11, 12, 13, and 14 are free, and the rest of the blocks, i.e.,
block numbers 1, 2, 7, 8, 15 and 16 are allocated to some files.

If we apply the counting method, Block 3 will point to Block 4 and


store the count 4 (since Block 3, 4, 5, and 6 are contiguous). Similarly,
Block 9 will point to Block 10 and keep the count of 6 (since Block 9,
10, 11, 12, 13, and 14 are contiguous). This is also represented in the
following figure-

You might also like