Download Chapter 10 - Wikispaces

Document related concepts
Transcript
Chapter 10:
File-System Interface
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Chapter 10: File-System Interface
n
File Concept
n
Access Methods
n
Disk and Directory Structure
n
File-System Mounting
n
File Sharing
n
Protection
Operating System Concepts – 9th Edition
10.2
Silberschatz, Galvin and Gagne ©2013
Objectives
n
To explain the function of file systems.
n
To describe the interfaces to file systems.
n
To discuss file-system design tradeoffs, including access
methods, file sharing, file locking, and directory structures.
n
To explore file-system protection.
Operating System Concepts – 9th Edition
10.3
Silberschatz, Galvin and Gagne ©2013
File Concept
n
A partition on a secondary storage device is a contiguous logical
address space.
n
There are different categories of items to be stored:
l
l
n
Data

numeric

character

binary
Programs
The contents of each file is defined by the file’s creator
l
There are many possible types.

text file – a sequence of characters.

source file – a sequence of functions.

executable file – a series of code sections to execute.

etc.
Operating System Concepts – 9th Edition
10.4
Silberschatz, Galvin and Gagne ©2013
File Attributes
n
Each file has attributes used to provide information for human users
and for the system to maintain the file.
l
Name – only information kept in human-readable form.
l
Identifier – unique tag (number) identifies file within file system.
l
Type – needed for systems that support different file types.
l
Location – pointer to the file location on the device.
l
Size – current file size.
l
Protection – controls who can do reading, writing, executing.
l
Time, date, and user identification – data for protection,
security, and usage monitoring.
n
Information about files are kept in the directory structure, which is
maintained on the disk.
n
There are many variations on different systems including extended
file attributes such as file checksums.
Operating System Concepts – 9th Edition
10.5
Silberschatz, Galvin and Gagne ©2013
File info Window on Mac OS X
Operating System Concepts – 9th Edition
10.6
Silberschatz, Galvin and Gagne ©2013
File Operations
n
A file is an abstract data type and we need to consider the
operations supported by files.
l
Create
l
Write – at write pointer location
l
Read – at read pointer location

n
On some systems there may be two pointers (read
and write) on others there may be a single pointer
(current-file-position).
l
Reposition within file – seek (for read, write or both)
l
Delete – erase the file completely
l
Truncate – erase the contents of the file but keep its
attributes.
These are the minimum attributes for a file system.
Operating System Concepts – 9th Edition
10.7
Silberschatz, Galvin and Gagne ©2013
File Operations
n
Additional operations might include
l
Append – at information at the end of a file
l
Copy – copy both contents and attributes to another
location (or file system or I/O device)
l
Open(Fi) – search the directory structure on disk for entry
Fi, and move the content of entry to memory
l
Close (Fi) – move the content of entry Fi in memory to
directory structure on disk
Operating System Concepts – 9th Edition
10.8
Silberschatz, Galvin and Gagne ©2013
Open Files
n
Several pieces of data are needed to manage open files:
l
Open-file table: tracks the open files
l
File pointer: pointer to last read/write location, per
process that has the file open (there may be separate
read and write pointers).
l
File-open count: counter of number of times a file is
open – this is to allow removal of the file’s entry from the
open-file table when the last processes closes it.
l

Opening a file increments the counter.

Closing a file decrements the counter.
Disk location of the file

l
This information is also contained in the file system
but it is faster to keep a copy in memory.
Access rights: per-process access mode information.
Operating System Concepts – 9th Edition
10.9
Silberschatz, Galvin and Gagne ©2013
Open File Locking
n
n
Some operating systems and file systems provide the ability for a
process to lock a file.
l
These are similar to reader-writer locks.
l
Shared lock similar to reader lock – several processes can
acquire concurrently.
l
Exclusive lock similar to writer lock.
Locks can be mandatory or advisory:
l
Mandatory – access is denied depending on locks held and
requested.
l
Advisory – processes can find status of locks and decide
what to do.

It is up to the programmer to make sure that theadvisory
locks are appropriately requested and released.
Operating System Concepts – 9th Edition
10.10
Silberschatz, Galvin and Gagne ©2013
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();
Operating System Concepts – 9th Edition
10.11
Silberschatz, Galvin and Gagne ©2013
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();
}
}
}
Operating System Concepts – 9th Edition
10.12
Silberschatz, Galvin and Gagne ©2013
File Types – Name, Extension
n
File types are often included as an extension of their name.
Operating System Concepts – 9th Edition
10.13
Silberschatz, Galvin and Gagne ©2013
File Structure
The file type is often used to indicate how the data is
structured within the file.
n Some structures must be recognized by the OS (e.g.
executable file) but others are maintained by user programs.
n There are several ways to structure the data:
l None – the file is just a sequence of words or bytes.
l Simple record structure (either fixed or variable length)
n
Lines (either fixed or variable length)
l Complex Structures
 Formatted document
 Relocatable load file
n Many systems simulate complicated structures by inserting
appropriate control characters.
l
Operating System Concepts – 9th Edition
10.14
Silberschatz, Galvin and Gagne ©2013
File Access Methods
n
There are two different methods used to access the data in a
file.
l Sequential Access – information is processed in order
from the beginning to the end.
 Based on the tape model.
l
Direct Access – the file is made up of fixed-length
logical records and they are processed in no particular
order.
Operating System Concepts – 9th Edition
10.15
Silberschatz, Galvin and Gagne ©2013
Access Methods
n
Sequential Access
l
write next
l
read next
l
no read after last write
reset

n
n
Direct Access – file is made up of fixed length logical records, n = relative
block number (the number of blocks from the beginning of the file)
l
read n
l
write n
l
position to n
l
read next
l
write next
Block numbers are specified a relative numbers so the file can be moved
on the device without modifying programs that use it.
Operating System Concepts – 9th Edition
10.16
Silberschatz, Galvin and Gagne ©2013
Simulation of Sequential Access on Direct-access File
n
If a file is direct-access, it is still possible to simulate sequential
access.
l Suppose cp a variable that contains the block number and will
be used as position pointer.
Operating System Concepts – 9th Edition
10.17
Silberschatz, Galvin and Gagne ©2013
Other Access Methods
n
Other access methods can be built on top of these base
methods.
l These generally involve creation of an index for the file
The index is kept in memory and contains pointers to key
location in the file.
 Think about a dictionary with an index indicating
where the A’s, B’s, etc. begin.
l If the index gets too large, on strategy is to keep a
smaller index (in memory) of the full index (on disk).
n For example IBM’s indexed sequential-access method
(ISAM)
l Small master index, points to the disk blocks of
secondary index.
l
l
The file is kept sorted on a defined key.
All done by the OS
n VMS operating system provides index and relative files as
another example (see next slide)
l
Operating System Concepts – 9th Edition
10.18
Silberschatz, Galvin and Gagne ©2013
Example of Index and Relative Files
n
VMS operating system provides index and relative files as
another example.
Operating System Concepts – 9th Edition
10.19
Silberschatz, Galvin and Gagne ©2013
Disk Structure
n
Disk can be subdivided into partitions.
n
Disk or partition can be used two ways.
n
l
raw – without a file system,
l
formatted with a file system.

Disks or partitions can be combined into larger file
systems (using RAID).

An entity containing file system is known as a volume.
Each volume containing file system must contain information
about its files in a device directory or volume table of
contents.
Operating System Concepts – 9th Edition
10.20
Silberschatz, Galvin and Gagne ©2013
A Typical File-system Organization
Operating System Concepts – 9th Edition
10.21
Silberschatz, Galvin and Gagne ©2013
Types of File Systems
n
In addition to general-purpose file systems there are many
special-purpose file systems, frequently all within the same
operating system or computer.
n
Consider Solaris has
l
tmpfs – memory-based volatile FS for fast, temporary I/O
l
objfs – interface into kernel memory to get kernel symbols for
debugging
l
ctfs – contract file system for managing daemons
l
lofs – loopback file system allows one FS to be accessed in
place of another
l
procfs – kernel interface to process structures
l
ufs, zfs – general purpose file systems
Operating System Concepts – 9th Edition
10.22
Silberschatz, Galvin and Gagne ©2013
Directories
n
A file system is generally a large file table with no organization
imposed on the entries.
n
A directory is a symbol table that translates file names to table
entries.
Directory
Files
F1
F2
F3
F4
Fn
Operating System Concepts – 9th Edition
10.23
Silberschatz, Galvin and Gagne ©2013
Operations Performed on Directory
n
We need to consider the operations we can do using the directory structure.
l
Search for a file
l
Create a file
l
Delete a file
l
List a directory
l
Rename a file
l
Traverse the file system – do something to every file in the file system
(search, copy, delete, etc.)
Operating System Concepts – 9th Edition
10.24
Silberschatz, Galvin and Gagne ©2013
Directory Organization
The directory is organized logically to obtain
n
The directory structure is organized logically in several
different ways, but there are common goals for all these
methods.
l
Efficiency – locating a file quickly
l
Naming – convenient to users
l

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, …)
Operating System Concepts – 9th Edition
10.25
Silberschatz, Galvin and Gagne ©2013
Single-Level Directory
n
The simplest directory structure is to use a single-level directory
for all users.
n
All files are in the same directory and so they must all have unique
names.
l
Edvance360 has this problem – you cannot have two different
files named homework.pdf, even if they are for different
courses.
n
It is not possible to group files together so that information is
easier to find.
n
It is also difficult to identify which files belong to a particular user.
Operating System Concepts – 9th Edition
10.26
Silberschatz, Galvin and Gagne ©2013
Two-Level Directory
n
Some of the problems with a single level directory can be address by
creating a separate directory for each user.
n
Each user has a user file directory (UFD) as part of the master file
directory (MFD).
Operating System Concepts – 9th Edition
10.27
Silberschatz, Galvin and Gagne ©2013
Two-Level Directory
n
n
When a user refers to a file name, only the UFD for that user is
searched.
l
Two users can have files with the same name.
l
A user cannot accidentally (or intentionally) delete or modify the files
of another user.
When the system refers to a file it must provide the name of the owner
and the name of the file – this is known as a path name.
l
C:\usera\test.txt is a different file from
C:\userb\test.txt.

Notice in this example that C identifies the volume.
n
When a user searches for a file, it is only necessary to search a single
UFD.
n
An individual user still has no grouping capability to help organize files.
Operating System Concepts – 9th Edition
10.28
Silberschatz, Galvin and Gagne ©2013
Tree-Structured Directories
n
Most systems implement their directory structure as a tree.
n
A directory contains a set of files and/or subdirectories.
n
l
A subdirectory is just a file which contains links to the table entries
for its contents.
l
Subdirectories are distinguish for ordinary files by a single bit in its
directory entry.
In normal use each process will have a current directory.
l
If the process needs access to a file that is not in its current directory
then a path to the file must be provided.
l
In order to change the current directory a chage_directory()
system call is provided.
n
Now a user can have multiple files that share a name as long as they
are in different subdirectories.
n
The user can create and destroy subdirectories as needed to help
organize their data.
Operating System Concepts – 9th Edition
10.29
Silberschatz, Galvin and Gagne ©2013
Tree-Structured Directories
Operating System Concepts – 9th Edition
10.30
Silberschatz, Galvin and Gagne ©2013
Tree-Structured Directories (Cont)
n
There two ways to identify a file.
l Absolute path name – the path of the file from the root
directory of the file system.
 /Users/hsuters/Spring2016/CSC303/test1.pdf
l
Relative path name – the path to the file from the current
directory.
 Assuming the current directory is /Users/hsuters this
would be Spring2016/CSC303/test1.pdf.

It is also possible to go upward in the directory tree. If the
current directory is /Users/hsuters/Fall2016 then the
relative path would be
../Spring2016/CSC303/test1.pdf.
Operating System Concepts – 9th Edition
10.31
Silberschatz, Galvin and Gagne ©2013
Tree-Structured Directories (Cont)
n
Creating new files, deleting files and creating new directories are all
done in the current directory.
l rm <file-name> (delete file)
l
n
mkdir <dir-name> (create directory)
Deleting a directory however must be done carefully.
l Example: Suppose we want to delete the directory/mail.
l
Deleting /mail will not necessarily delete the files in its subtree,
but it will render them inaccessible.
l
Two solutions:
When we delete a directory we need to be sure to delete all
the entries from the master file table so their storage space
can be reused.
 Only allow empty directories to be deleted.

Operating System Concepts – 9th Edition
10.32
Silberschatz, Galvin and Gagne ©2013
Acyclic Graphs Cycles
n
Two users might want to share a file or directory for a joint
project.
l
This produces what are called an acyclic directory tree.
Operating System Concepts – 9th Edition
10.33
Silberschatz, Galvin and Gagne ©2013
Cycles (Cont.)
n
Another way of producing a cycle is having two different names for
the same file (aliasing).
n
Cycles can produce problems for the files system.
l
n
For example if dict deletes list then spell will have a dangling
pointer (words).
There are several solutions to this problem:
1.
The file system to prohibit aliases and file sharing.
2.
Every file has backpointers so that when we delete the file we
can also delete all references to it in the directory tree.

One user could delete a file before the other user is done.

The records for different files would have different number of
backpointers, resulting is variable sized records and more
complex file data structures.
Operating System Concepts – 9th Edition
10.34
Silberschatz, Galvin and Gagne ©2013
Cycles (Cont.)
3.
Every file entry to has a counter that keeps track of how many
links there are to the file – the file is only truly deleted when its
last link is deleted.

4.
n
It is possible to have a “persistent” file that is had to delete
because there is a hidden link somewhere.
Introduce a new directory entry type.

Link – another name for a pointer to an existing file.

Resolving the link means following pointer to locate the file.

A file could have one true entry and many links
–
Deleting a link has no effect on the file
–
Deleing the true entry for the file leaves all the links
dangling.
Unix systems use solutions 3 (hard links) and 4 (soft links).
Operating System Concepts – 9th Edition
10.35
Silberschatz, Galvin and Gagne ©2013
General Graph Directories
n
In general directories can get even more complicated.
n
In a general graph it is possible for a directory to have itself
somewhere in its directory tree producing a cycle.
Operating System Concepts – 9th Edition
10.36
Silberschatz, Galvin and Gagne ©2013
General Graph Directory (Cont.)
n
If we limit the system to only acyclic directory trees it makes
searching and traversing the tree significantly easier.
l
With cycles we are likely to get into an infinite loop.
l
It is also difficult to determine when a directory can be
deleted.

There might be references to the directory that come
from within the directory. The number of reference might
not be 0 but there is no valid way to get to the directory.
–
This can be dealt with using a technique called
garbage collection – traverse the tree and mark the
files that can be accessed, delete the others.
Operating System Concepts – 9th Edition
10.37
Silberschatz, Galvin and Gagne ©2013
General Graph Directory (Cont.)
n
If we want to guarantee there are no cycles then there are a
couple of strategies we could use.
l
Allow only links to files that are not subdirectories.
l
Every time a new link is added use we could use a cycle
detection algorithm to determine if it would produce a cycle.

n
Only allow links that do not produce cycles.
Many file systems simply allow cycles to form and deal with the
consequences in the form more complicated algorithms for
searching, directory traversal and garbage collection.
Operating System Concepts – 9th Edition
10.38
Silberschatz, Galvin and Gagne ©2013
File System Mounting
n
A file system must be mounted before it can be accessed
n
Some OSs approach this problem by providing a separate
directory structure for each file system.
l
n
Think MSDOS with volumes labeled A:, B:, C:, etc.
Another approach is to build a larger directory structure out of
individual volumes.
l
There is a root volume and special file (usually directories)
are designated as mount points.
l
When an unmounted file system is mounted at particular
mount point
Operating System Concepts – 9th Edition
10.39
Silberschatz, Galvin and Gagne ©2013
File System Mounting
n
Consider the following situation:
l
Figure (a) represents an existing file system.
l
Figure (b) represents an unmounted file system residing on
/device/dsk.
l
If the directory /users is used as a mount point for the unmounted
file system, then the directories bill and fred will no longer be
accessible but will be replaced with sue and jane.
Operating System Concepts – 9th Edition
10.40
Silberschatz, Galvin and Gagne ©2013
Mount Point
n
This will be the resulting file system.
Operating System Concepts – 9th Edition
10.41
Silberschatz, Galvin and Gagne ©2013
Mount Point
n
Some systems may only allow empty directories to be used as mount
points.
n
It may be possible to mount the same file system at multiple mount
points simultaneously.
l
If a group is working on a big project, the project could be stored on
its own volume and mounted in a the directory tree of each group
member.
Operating System Concepts – 9th Edition
10.42
Silberschatz, Galvin and Gagne ©2013
File Sharing
n
Sharing of files on multi-user systems is desirable, but it needs
to be done carefully.
l
n
On distributed systems, files may be shared across a network.
l
n
Sharing may be done through a protection scheme
Network File System (NFS) is a common distributed filesharing method.
A protection scheme requires the system to keep information
about each file and directory.
l
User IDs identify users and each file has an owner that is
identified by storing this ID.

l
Group IDs allow users to be in groups.

n
allowing permissions and protections to be per-user.
This allows use to set up group access rights.
A file may be set up to allow certain actions by particular users
or groups.
Operating System Concepts – 9th Edition
10.43
Silberschatz, Galvin and Gagne ©2013
Protection
n
n
If a file is to be shared, the file owner/creator should be able to
control:
l
what can be done
l
by whom
Types of access:
l
Read – read from the file
l
Write – write or rewrite the file
l
Execute – load the file into memory and run it
l
Append – add new information at the end of the file
l
Delete – erase the file and free its space for future use
l
List – see the name and attributes of the file
Operating System Concepts – 9th Edition
10.44
Silberschatz, Galvin and Gagne ©2013
Access Lists and Groups in Unix
n
In Unix/Linux there are three modes of access:
l Read
Write
l Execute
n Each file belongs to an owner and a group.
n From the file’s point of view there are three types of access.
l
l
l
l
Access by the owner
Access by a member of the files group
Access by anyone else
Operating System Concepts – 9th Edition
10.45
Silberschatz, Galvin and Gagne ©2013
Access Lists and Groups in Unix
n
The file record stores 9 bits to indicate the 3 types of access by the
3 different groups.
n
These bits are translated into integer values and combined to
produce and access number.
a) owner access
7

b) group access
6

c) public access
1

RWX
111
RWX
110
RWX
001
Suppose the manager creates a group (unique name), say G, and
adds some users to the group.
n For a particular file (say game) or subdirectory, define an
appropriate access.
n
n
Attach a group to the file
Operating System Concepts – 9th Edition
chgrp G game
10.46
Silberschatz, Galvin and Gagne ©2013
A Sample UNIX Directory Listing
n
A listing can show the access permissions for each file (ls –l).
d – directory
l r – read
l w – write
l e – execute
n The order of the groups is owner, group, universe.
n The group of the file is also listed.
l
Operating System Concepts – 9th Edition
10.47
Silberschatz, Galvin and Gagne ©2013
Windows 7 Access-Control List Management
n
Windows users manage access-control through the GUI
Operating System Concepts – 9th Edition
10.48
Silberschatz, Galvin and Gagne ©2013
File Sharing – Remote File Systems
n
Networking allows remote file sharing.
l
This can be done manually via programs like FTP.

l
It can also be done automatically using distributed file systems
(DFS).

l
This transfer could be anonymous – anyone can have access to
the file or the file could be protected so only certain users have
access to it.
Remote files and directories appear to be locally.
A third option is to share files semi automatically via the world wide
web.

Basically automate a manual process like FTP.
Operating System Concepts – 9th Edition
10.49
Silberschatz, Galvin and Gagne ©2013
File Sharing – Remote File Systems
n
A Client-server model allows client machines to mount remote file
systems from servers.
l
A server can serve multiple clients and a client can use multiple
servers.
l
It can be difficult to verify the client by name, IP number, network
name, etc.

These can be imitated (spoofed).
l
It is also difficult to verify which user is on a client machine.
l
NFS is the standard UNIX client-server file sharing protocol.

User IDs on the client and server must match to access secured
data.
l
CIFS is the standard Windows protocol
l
Standard operating system file calls are translated into remote calls.
Operating System Concepts – 9th Edition
10.50
Silberschatz, Galvin and Gagne ©2013
File Sharing – Remote File Systems
n
So that client-server systems are easier to manage, distributed
information systems (distributed naming services) are used to
provide unified access to information needed for remote computing.
n
The domain name system (DNS) provides host-name-to-network
address translation
n
Other distributed information system provide user
name/password/user ID/group ID information.
l
Yellow Pages (NIS)
l
Active Directory
l
Lightweight Directory-Access Protocol (LDAP) – attempts to
provide more security than other methods.
Operating System Concepts – 9th Edition
10.51
Silberschatz, Galvin and Gagne ©2013
File Sharing – Failure Modes
n
All file systems have failure modes.
l
n
Remote file systems add new failure modes, due to network
failure and/or server failure.
l
n
For example there could be corruption of directory
structures or other non-user data, called metadata.
In order to recover from a failure system needs state
information about status of each remote request.

What system is making the request?

What user is making the request?
Stateless protocols such as NFS v3 include all information in
each request, allowing easy recovery but less security.
Operating System Concepts – 9th Edition
10.52
Silberschatz, Galvin and Gagne ©2013
File Sharing – Consistency Semantics
n
In a manner similar to chapter 5, there are synchronization issues
when multiple users try to access a remote file.
l
It is expensive to use the more complex algorithms due to disk
I/O and network latency on a remote file system.

l
Andrew File System (AFS) has a successful implementation of
a complex remote file sharing semantics.

l
It is difficult to implement atomic instructions for instance.
Writes are to a file are only visible after a file is closed and
reopened.
Unix file system (UFS) implements:

Writes to an open file are visible immediately to other users
of the same open file.

One mode of file sharing also allows the users to share the
current location file pointer.
–
When one user changes the read/write position it
changes for all.
Operating System Concepts – 9th Edition
10.53
Silberschatz, Galvin and Gagne ©2013
End of Chapter 11
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Homework
n
Chapter 10, page 513
l
4, 5, 6, 8, 9, 11, 12, 13, 14, 16, 17
Operating System Concepts – 9th Edition
10.55
Silberschatz, Galvin and Gagne ©2013