System Programming - II
Chapter 11
File-System Interface
1
File-System Interface
• File Concept
• Access Methods
• Directory Structure
• File-System Mounting
• File Sharing
• Protection
2
Objectives
• To explain the function of file systems
• To describe the interfaces to file systems
• To discuss file-system design tradeoffs,
including access methods, file sharing, file
locking, and directory structures
• To explore file-system protection
3
File Concept
• File system consist of two distinct parts:
– Collection of files: each storing related data.
– Directory structure: organizes and provides
information about all the files in the system.
• Information stored on different storage media.
– Magnetic disk, magnetic tape, and optical disks
• Files are mapped by OS, onto physical devices.
4
File Concept (cont…)
• File is a named collection of related information that is
recorded on secondary storage.
• File is a smallest allotment of logical secondary storage.
• Data cannot be written to secondary storage unless they
are within a file.
5
File Concept (cont…)
• Types:
– Data files may be
• Numeric
• Alphabetic
• Character
• Binary
• Contiguous logical address space
• File is a sequence of bits, bytes, lines, or records whose
meaning is defined by the file’s creator and user.
6
File Structure
• Certain defined structures according to its types.
• Text file: Sequence of characters organized into lines.
• Executable: Series of code sections that the loader can bring
into memory and execute.
• Simple record structure
– Lines
– Fixed length
– Variable length
7
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.
• Required 16 to 1000 bytes to record this information.
8
File Operations
• Operating system provides system calls to;
• Create: two steps are necessary;
– Space in the file system must be found.
– Entry for the new file must be made in the directory.
• Directory entry records the name of the file and the
location in the file system.
• Write: make a system call specifying both the name of
the file and the information to be written to the file.
• System must keep a write pointer to the location in the file
where the next write is to take place.
• Write pointer must be updated whenever a write occurs.
9
File Operations (cont…)
• Read:
– system call that specifies the name of the file and
where (in memory) the next block of the file should
be put.
• Once the read has taken place, the read pointer is
updated.
• Most systems keep only one current-file-position pointer.
• Read and write operation use the same pointer, saving
space and reducing the system complexity.
10
File Operations (cont…)
• Reposition within file (file seek):
• Directory is searched for the appropriate entry, and the
current file position is set to given value.
• Does not involve any actual I/O.
• This file operation is also known as a file seek.
• Delete: search the directory for the named file.
• Truncate: user wants the attributes of a file to remain the
same, but wants to erase the contents of file, rather than
delete the file
• Attributes remain unchanged, but file length is zero.
11
File Operations (cont…)
• Other common operations include appending new
information and renaming an existing file.
• Open file table – operating system keeps a small table
containing information about all open files.
• There are several pieces of information associated with an
open file.
– File pointer.
– File open count.
– Disk location of the file.
12
File Operations (cont…)
• File pointer.
– System must track the last read/write location as a
current file position pointer.
– Pointer is unique to each process operating on the file.
– Must be kept separate from the on-disk file attributes.
• File open count.
– As files are closed, OS must reuse its open file table
entries or it could run out of space in the table.
– System must wait for the last file to close before
removing the open file table entry.
– This counter tracks the number of opens and closes, and
reaches zero on the last close.
13
File Operations (cont…)
• Disk location of the file.
– Information needed to locate the file on disk is kept in
memory to avoid having to read it from disk for each
operation.
• Memory mapping.
– Allows a part of the virtual address space to be logically
associated with a section of file.
14
Open Files
• Several pieces of data are needed to manage
open files:
– File pointer: pointer to last read/write location, per
process that has the file open
– File-open count: counter of number of times a file is
open – to allow removal of data from open-file table
when last processes closes it
– Disk location of the file: cache of data access
information
– Access rights: per-process access mode information
15
Open File Locking
• Provided by some operating systems and file
systems
• Mediates access to a file
• Mandatory or advisory:
– Mandatory – access is denied depending on
locks held and requested
– Advisory – processes can find status of locks
and decide what to do
16
File Locking Example – Java API
import java.io.*;
import java.nio.channels.*;
public class LockingExample {
public static final boolean EXCLUSIVE = false;
public static final boolean SHARED = true;
public static void main(String arsg[]) throws IOException {
FileLock sharedLock = null;
FileLock exclusiveLock = null;
try {
RandomAccessFile raf = new
RandomAccessFile("file.txt", "rw");
// get the channel for the file
FileChannel ch = raf.getChannel();
// this locks the first half of the file - exclusive
exclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);
/** Now modify the data . . . */
// release the lock
exclusiveLock.release(); 17
File Locking Example – Java API (cont.)
// this locks the second half of the file - shared
sharedLock = ch.lock(raf.length()/2+1,
raf.length(), SHARED);
/** Now read the data . . . */
// release the lock
sharedLock.release();
} catch (java.io.IOException ioe) {
System.err.println(ioe);
}finally {
if (exclusiveLock != null)
exclusiveLock.release();
if (sharedLock != null)
sharedLock.release();
}
}
} 18
File Types – Name, Extension
19
Access Methods
• Sequential Access:
– Simple access method.
– Information in the file is processed in order, one record
after the other.
– Editors and compliers usually access files in this
fashion.
read next
write next
reset
no read after last write
(rewrite)
20
Access Methods
• Direct Access (relative access)
– Allows arbitrary blocks to be read or written.
– We may read block 14, then 53, and then 7.
– No restrictions.
read n
write n
position to n
read next
write next
rewrite n
n = relative block number
21
Access Methods
• Direct Access (relative access) (Contd).
• Relative block number: is an index relative to the begging
of the file.
– The first relative block of the file is 0, the next is 1 and
so on, even though the actual absolute disk address of
block may be 14703 for the first block, and 3192 for the
second.
22
Sequential-access File
23
Simulation of Sequential Access on a Direct-
Access File
24
Other Access Methods
• Other access methods can be built on top of a direct-
access method.
• Involve in the construction of an index for the file.
• Index, like an index in the back of the book, contains
pointers to various blocks.
• To find an entry in the file, we first search the index, and
then use the pointer to access the file directly and to find
the desired entry.
25
Other Access Methods (cont…)
• Index file itself may become too large to be kept in main
memory.
• Solution:
– Create an index for the index file.
– Primary index file would contain pointers to secondary
index files, which would point to the actual data items.
26
Example of Index and Relative Files
27
Directory Structure
• A collection of nodes containing information about all files.
Directory
Files
F1 F2 F4
F3
Fn
• Both the directory structure and the files reside on disk.
• Backups of these two structures are kept on tapes.
28
Disk Structure
• Disk can be subdivided into partitions
• Disks or partitions can be RAID protected against failure
• Disk or partition can be used raw – without a file system,
or formatted with a file system
• Partitions also known as minidisks, slices
• Entity containing file system known as a volume
• Each volume containing file system also tracks that file
system’s info in device directory or volume table of
contents
• As well as general-purpose file systems there are many
special-purpose file systems, frequently all within the
same operating system or computer
29
A Typical File-system Organization
30
Information in a Device Directory
• Name
• Type
• Address
• Current length
• Maximum length
• Date last accessed
• Date last updated
• Owner ID
• Protection information
• Note: directory can be viewed as a symbol table that
translates file names into their directory entries.
31
Operations Performed on Directory
• Search for a file
• Create a file
• Delete a file
• List a directory
• Rename a file
• Traverse the file system
32
Organize the Directory (Logically) to Obtain
• Efficiency – locating a file quickly.
• Naming – convenient to users.
– Two users can have same name for different files.
– The same file can have several different names.
• Grouping – logical grouping of files by properties, (e.g.,
all Java programs, all games, …)
33
Single-Level Directory
• A single directory for all users.
Naming problem
Grouping problem
34
Two-Level Directory
• Separate directory for each user.
• Path name
• Can have the same file name for different user
• Efficient searching
• No grouping capability
35
Tree-Structured Directories
36
Tree-Structured Directories (Cont…)
• Efficient searching
• Grouping Capability
• Current directory (working directory)
– cd /spell/mail/prog
– type list
37
Tree-Structured Directories (Cont…)
• Absolute or relative path name
• Creating a new file is done in current directory.
• Delete a file
rm <file-name>
• Creating a new subdirectory is done in current directory.
mkdir <dir-name>
Example: if in current directory /mail
mkdir count mail
prog copy prt exp count
Deleting “mail” deleting the entire subtree rooted by
“mail”. 38
Acyclic-Graph Directories
• Have shared subdirectories and files
39
Acyclic-Graph Directories (Cont…)
• Two different names (aliasing)
• If dict deletes list dangling pointer
Solutions:
– Backpointers, so we can delete all pointers
Variable size records a problem
– Backpointers using a daisy chain organization
– Entry-hold-count solution
• New directory entry type
– Link – another name (pointer) to an existing file
– Resolve the link – follow pointer to locate the file
40
General Graph Directory
41
General Graph Directory (Cont...)
• How do we guarantee no cycles?
– Allow only links to file not subdirectories
– Garbage collection
– Every time a new link is added use a cycle
detection
algorithm to determine whether it is OK
42
File System Mounting
• A file system must be mounted before it can
be accessed
• A unmounted file system (i.e. Fig. 11-11(b)) is
mounted at a mount point
43
(a) Existing. (b) Unmounted Partition
44
Mount Point
45
File Sharing
• Sharing of files on multi-user systems is desirable
• Sharing may be done through a protection scheme
• On distributed systems, files may be shared across
a network
• Network File System (NFS) is a common
distributed file-sharing method
46
File Sharing – Multiple Users
• User IDs identify users, allowing
permissions and protections to be per-user
• Group IDs allow users to be in groups,
permitting group access rights
47
File Sharing – Remote File Systems
• Uses networking to allow file system access between systems
– Manually via programs like FTP
– Automatically, seamlessly using distributed file systems
– Semi automatically via the world wide web
• 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 is standard UNIX client-server file sharing protocol
– CIFS is standard Windows protocol
– Standard operating system file calls are translated into remote
calls
• Distributed Information Systems (distributed naming services)
such as LDAP, DNS, NIS, Active Directory implement unified
48
access to information needed for remote computing
File Sharing – Failure Modes
• Remote file systems add new failure modes,
due to network failure, server failure
• Recovery from failure can involve state
information about status of each remote
request
• Stateless protocols such as NFS include all
information in each request, allowing easy
recovery but less security
49
File Sharing – Consistency Semantics
• Consistency semantics specify how multiple users
are to access a shared file simultaneously
– Similar to Ch 7 process synchronization algorithms
• Tend to be less complex due to disk I/O and network
latency (for remote file systems
– Andrew File System (AFS) implemented complex
remote file sharing semantics
– Unix file system (UFS) implements:
• Writes to an open file visible immediately to other
users of the same open file
• Sharing file pointer to allow multiple users to read
and write concurrently
– AFS has session semantics
• Writes only visible to sessions starting after the file is
50
closed
Protection
• File owner/creator should be able to control:
– what can be done
– by whom
• Types of access
– Read
– Write
– Execute
– Append
– Delete
– List
51
Access Lists and Groups
• Mode of access: read, write, execute
• Three classes of users
RWX
a) owner access 7 111
RWX
b) group access 6 110
RWX
c) public access 1 001
• 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.
owner group public
chmod 761 game
Attach a group to a file
chgrp G game
52
Windows XP Access-control List Management
53
A Sample UNIX Directory Listing
54