File Systems
File Systems
CHAPTER 5
FILE-SYSTEM
OUTLINE
¡ File Concept
¡ Access Methods
¡ Disk and Directory Structure
¡ File-System Mounting
¡ File Sharing
¡ Protection
FILE CONCEPT
¡ Program
5
FILE OPERATIONS
7
OPEN FILE LOCKING
8
FILE LOCKING EXAMPLE – JAVA API
¡ In the Java API, acquiring a lock requires first obtaining the FileChannel for the file to
be locked.
¡ The lock() method of the FileChannel is used to acquire the lock.
¡ The API of the lock() method is
FileLock lock(long begin, long end, boolean shared)
¡ Setting shared to true is for shared locks; setting shared to false acquires the
lock exclusively.
¡ The lock is released by invoking the release() of the FileLock returned by the lock()
operation
9
FileLock sharedLock = null;
FileLock exclusiveLock = null;
RandomAccessFile raf = new RandomAccessFile("[Link]", "rw");
// get the channel for the file
FileChannel ch = [Link]();
// this locks the first half of the file - exclusive
exclusiveLock = [Link](0, [Link]() / 2, EXCLUSIVE);
/** Now modify the data . . . */
// release the lock
[Link]();
// this locks the second half of the file - shared
sharedLock = [Link]([Link]() / 2 + 1, [Link](), SHARED);
/** Now read the data . . . */
// release the lock
[Link]();
if (exclusiveLock != null)
[Link]();
if (sharedLock != null) FILE LOCKING EXAMPLE – JAVA API
[Link]();
FILE TYPES – NAME, EXTENSION
11
FILE STRUCTURE
¡ File types also can be used to indicate the internal structure of the file.
¡ None - sequence of words, bytes
¡ Simple record structure
¡ Lines
¡ Fixed length
¡ Variable length
¡ Complex Structures
¡ Formatted document
¡ Relocatable load file
¡ Who decides:
¡ Operating system
¡ Program
12
ACCESS METHODS
13
SEQUENTIAL-ACCESS FILE
¡ A read operation - read next() - reads the next portion of the file and automatically
advances a file pointer, which tracks the I/O location.
¡ Similarly, the write operation - write next() - appends to the end of the file and
advances to the end of the newly written material (the new end of file).
14
DIRECT ACCESS (OR RELATIVE ACCESS)
¡ A file is made up of fixed-length logical records that allow programs to read and write
records rapidly in no particular order
¡ Databases are often of this type
read n
write n
n position to n
read next
write next
rewrite n
¡ read(n), where n is the block number
15
SIMULATION OF SEQUENTIAL ACCESS ON DIRECT-ACCESS FILE
¡ Not all operating systems support both sequential and direct access for files.
¡ Some systems allow only sequential file access or only direct access
¡ Some systems require defining as sequential or direct when it is created
¡ We can easily simulate sequential access on a direct-access file by simply keeping a
variable cp that defines our current position,
16
OTHER ACCESS METHODS
17
EXAMPLE OF INDEX AND RELATIVE FILES
18
DISK STRUCTURE
19
A TYPICAL FILE-SYSTEM ORGANIZATION
20
EXAMPLE: SOLARIS FILE SYSTEMS
21
TYPES OF FILE SYSTEMS
22
OPERATIONS PERFORMED ON DIRECTORY
¡ The directory can be viewed as a symbol table that translates file names into their
directory entries.
¡ Search for a file
¡ Create a file
¡ Delete a file
¡ List a directory
¡ Rename a file
¡ Traverse the file system
23
LOGICAL STRUCTURE OF A DIRECTORY
¡ The most common schemes for defining the logical structure of a directory:
¡ Single-Level Directory
¡ Two-Level Directory
¡ Tree-Structured Directories
¡ Acyclic-Graph Directories
¡ General Graph Directory
24
SINGLE-LEVEL DIRECTORY
¡ Naming problem
¡ Grouping problem
25
TWO-LEVEL DIRECTORY
27
TREE-STRUCTURED DIRECTORIES (CONT.)
¡ Efficient searching
¡ Grouping Capability
¡ In normal use, each process has a current directory that contains most of the files
that are of current interest to the process
¡ Change directories by calling change_directory()
¡ From one change_directory() system call to the next, all open() system calls search
the new current directory.
28
TREE-STRUCTURED DIRECTORIES (CONT)
29
TREE-STRUCTURED DIRECTORIES (CONT)
30
ACYCLIC-GRAPH DIRECTORIES
32
FILE SYSTEM MOUNTING
users
help doc
prog
(a) (b)
33
MOUNT POINT
34
FILE SHARING
35
FILE SHARING – REMOTE FILE SYSTEMS
¡ Client-server model allows clients to mount remote file systems from servers
¡ Server can serve multiple clients
¡ Client and user-on-client identification is insecure or complicated
¡ NFS (Network File System) is standard UNIX client-server file sharing protocol
¡ CIFS (Common Internet File System) is standard Windows protocol
¡ Standard operating system file calls are translated into remote calls
36
PROTECTION
¡ Types of access
¡ Read
¡ Write
¡ Execute
¡ Append
¡ Delete
¡ List 37
ACCESS LISTS AND GROUPS
¡ Ask manager to create a group (unique name), say G, and add some users to the
group.
¡ For a particular file (say game) or subdirectory, define an appropriate access.
38
A SAMPLE UNIX DIRECTORY LISTING
39
WINDOWS 7 ACCESS-CONTROL LIST MANAGEMENT
40
OUTLINE
¡ File-System Structure
¡ File-System Implementation
¡ Directory Implementation
¡ Allocation Methods
¡ Free-Space Management
¡ Efficiency and Performance
¡ Recovery
41
BACKGROUND
¡ To improve I/O efficiency, I/O transfers between memory and disk are performed in
units of blocks. (1-n sectors) = n*512 bytes
¡ File system resides on secondary storage (disks)
¡ Provided user interface to storage, mapping logical to physical
¡ Provides efficient and convenient access to disk by allowing data to be stored, located retrieved
easily
¡ File control block (inode in UNIX) – storage structure consisting of information
about a file
¡ Device driver controls the physical device
42
FILE SYSTEM LAYERS
43
FILE SYSTEM LAYERS (CONT.)
44
FILE SYSTEM LAYERS
45
FILE SYSTEM
¡ Many file systems, sometimes many within an operating system, Each with its own
format:
¡ CD-ROM is ISO 9660
¡ Unix has UFS, FFS;
¡ Windows has FAT, FAT32, NTFS as well as floppy, CD, DVD Blu-ray
¡ Linux has more than 40 types, the standard Linux file system is known as the extended file system,
with the most common versions being ext3 and ext4
¡ New ones still arriving – ZFS, GoogleFS, Oracle ASM, FUSE
46
FILE-SYSTEM IMPLEMENTATION
¡ To create a new file, an application program calls the logical file system.
¡ It allocates a new File Control Block (FCB) which contains many details about the
file:
47
OPEN() FILE
¡ The open() call passes a file name to the logical file system.
¡ The open() system call first searches the system-wide open-file table to see if the
file is already in use by another process.
¡ If it is, a per-process open-file table entry is created pointing to the existing system-wide open-file
table. This algorithm can save substantial overhead.
¡ If the file is not already open, the directory structure is searched for the given file name. Parts of
the directory structure are usually cached in memory to speed directory operations.
48
OPEN() FILE
¡ Once the file is found, the FCB is copied into a system-wide open-file table in
memory.
¡ This table not only stores the FCB but also tracks the number of processes that have
the file open
49
READ() & WRITE() FILE
50
CLOSE() FILE
¡ When a process closes the file, the per-process table entry is removed, and the
system-wide entry’s open count is decremented.
¡ When all users that have opened the file close it, any updated metadata is copied back
to the disk-based directory structure, and the system-wide open-file table entry is
removed.
51
ALLOCATION METHODS
52
CONTIGUOUS ALLOCATION
¡ An allocation method refers to how disk blocks are allocated for files:
¡ Contiguous allocation – each file occupies set of contiguous blocks
¡ Best performance in most cases
¡ Simple – only starting location (block #) and length (number of blocks) are required
¡ Problems include finding space for file, knowing file size, external fragmentation, need for
compaction off-line (downtime) or on-line
53
CONTIGUOUS ALLOCATION (CONT)
54
CONTIGUOUS ALLOCATION (CONT)
¡ Problems:
¡ finding space for file (determining how much space is needed for a file?
¡ external fragmentation
55
EXTENT-BASED SYSTEMS
¡ Many newer file systems (i.e.,Veritas File System) use a modified contiguous allocation
scheme
¡ Extent-based file systems allocate disk blocks in extents
¡ An extent is a contiguous block of disks
¡ Extents are allocated for file allocation
¡ A file consists of one or more extents
56
LINKED ALLOCATION
57
LINKED ALLOCATION
¡ With linked allocation, each file is a linked list of disk blocks; the disk blocks may be
scattered anywhere on the disk
¡ The directory contains a pointer to the first and last blocks of the file.
¡ For example, a file of five blocks might start at block 9 and continue at block 16, then
block 1, then block 10, and finally block 25.
¡ Each block contains a pointer to the next block.
¡ If each block is 512 bytes in size, and a disk address (the pointer) requires 4 bytes,
then the user sees blocks of 508 bytes.
58
LINKED ALLOCATION
59
LINKED ALLOCATION
61
LINKED ALLOCATION
63
INDEXED ALLOCATION
¡ Each file has its own index block(s) of pointers to its data blocks
¡ Logical view:
index table
64
INDEXED ALLOCATION
65
EXAMPLE OF INDEXED ALLOCATION
66
INDEXED ALLOCATION
¡ The pointer overhead of the index block is generally greater than the pointer
overhead of linked allocation.
¡ Consider a common case in which we have a file of only one or two blocks:
¡ With linked allocation, we lose the space of only one pointer per block
¡ With indexed allocation, an entire index block must be allocated, even if only one or two pointers
will be non-null.
67
INDEXED ALLOCATION
¡ This point raises the question of how large the index block should be.
¡ Every file must have an index block, so we want the index block to be as small as
possible.
¡ Mechanisms for this purpose include the following:
¡ Linked scheme: Link together several index blocks
¡ Multilevel index: Like multi-level paging
¡ Combined scheme: Another alternative, used in UNIX-based file systems
68
INDEXED ALLOCATION – LINKED SCHEME (CONT.)
¡ An index block might contain a small header giving a set of the first disk-block
addresses.
¡ The last address is a pointer to another index block.
69
INDEXED ALLOCATION – MAPPING (CONT.)
¡ With 4,096-byte blocks, we could store 1,024 four-byte pointers in an index block.
Two levels of indexes allow 1,048,576 data blocks and a file size of up to 4 GB.
70
COMBINED SCHEME: UNIX UFS
71
PERFORMANCE
72
DIRECTORY IMPLEMENTATION
73
FREE-SPACE MANAGEMENT
!"#
1 Þ block[i] free
bit[i] =
0 Þ block[i] occupied
¡ For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26,
and 27 are free and the rest of the blocks are allocated. The free-space bit map would
be:
001111001111110001100000011100000 ...
74
FREE-SPACE MANAGEMENT (CONT.)
¡ Given that disk size constantly increases, the problem with bit vectors will continue to
escalate as well.
75
LINKED FREE SPACE LIST ON DISK
76
FREE-SPACE MANAGEMENT (CONT.)
¡ Grouping
¡ Modify linked list to store address of next n-1 free blocks in first free block, plus a pointer to next
block that contains free-block-pointers (like this one)
¡ Counting
¡ Because space is frequently contiguously used and freed, with contiguous-allocation allocation,
extents, or clustering
¡ Keep address of first free block and count of following free blocks
¡ Free space list then has entries containing addresses and counts
77
PERFORMANCE
¡ Consistency checking – compares data in directory structure with data blocks on disk, and
tries to fix inconsistencies
¡ Can be slow and sometimes fails
¡ Use system programs to back up data from disk to another storage device (magnetic tape,
other magnetic disk, optical)
¡ Recover lost file or disk by restoring data from backup
79
RECOVERY (CONT)
¡ Using this method, we can restore an entire disk by starting restores with the full
backup and continuing through each of the incremental backups
80