54
Sy stem Pr og ramming - II  Chapter 11 File-System Interface

(Ch11)File System Interface

Embed Size (px)

Citation preview

Page 1: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 1/54

System Programming - II

Chapter 11

File-System Interface

Page 2: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 2/54

2

File-System Interface

File ConceptAccess Methods

Directory StructureFile-System MountingFile SharingProtection

2

Page 3: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 3/54

3

Objectives

To explain the function of file systems

To descri be 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

Page 4: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 4/54

4

File Concept

File system consist of two distinct parts:² Collection of files: each storing related data.² Directory structure: organizes and provides

information a bout 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

Page 5: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 5/54

5

File Concept (cont«)

File is a named collection of related information that isrecorded on secondary storage.

File is a smallest allotment of logical secondary storage.

Data cannot be written to secondary storage unless theyare within a file.

5

Page 6: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 6/54

6

File Concept (cont«)

Types:² Data files may be

NumericAlpha betic

CharacterBinary

Contiguous logical address space

File is a sequence of bits, bytes, lines, or records whosemeaning is defined by the file·s creator and user.

6

Page 7: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 7/54

7

File StructureCertain defined structures according to its types.

T ext file: Sequence of characters organized into lines.

Executable: Series of code sections that the loader can bringinto memory and execute.

Simple record structure² Lines² Fixed length² Varia ble length

7

Page 8: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 8/54

8

File Attri butesName ² Only information kept in human-reada ble form.Identifier ² unique tag (num ber) identifies file within filesystemType ² 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 a bout files are kept in the directory structure,which is maintained on the disk.Required 16 to 1000 bytes to record this information.

8

Page 9: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 9/54

9

File OperationsOperating 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 thelocation in the file system.

Write: make a system call specifying both the name ofthe file and the information to be written to the file.System must keep a write pointer to the location in the filewhere the next write is to take place.Write pointer must be updated whenever a write occurs.

9

Page 10: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 10/54

10

File Operations (cont«)R

ead:² system call that specifies the name of the file andwhere (in memory) the next block of the file shouldbe put.

Once the read has taken place, the read pointer isupdated.

Most systems keep only one current-file-position pointer.

Read and write operation use the same pointer, savingspace and reducing the system complexity.

10

Page 11: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 11/54

11

File Operations (cont«)R

eposition within file (file seek):Directory is searched for the appropriate entry, and thecurrent 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.

T runcate: user wants the attri butes of a file to remain thesame, but wants to erase the contents of file, rather thandelete the fileAttri butes remain unchanged, but file length is zero.

11

Page 12: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 12/54

12

File Operations (cont«)

Other common operations include appending newinformation and renaming an existing file.

Open file table ² operating system keeps a small ta blecontaining information a bout all open files.

There are several pieces of information associated with anopen file.² File pointer.² File open count.² Disk location of the file.

12

Page 13: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 13/54

13

File Operations (cont«)F ile 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 attri butes.

F ile open count.² As files are closed, OS must reuse its open file ta ble

entries or it could run out of space in the ta ble.² System must wait for the last file to close before

removing the open file ta ble entry.² This counter tracks the num ber of opens and closes, and

reaches zero on the last close.13

Page 14: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 14/54

14

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 eachoperation.

Memory mapping.² Allows a part of the virtual address space to be logically

associated with a section of file.

14

Page 15: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 15/54

15

Open Files

Several pieces of data are needed to manageopen files:² File pointer: pointer to last read/write location, per

process that has the file open² File-open count: counter of num ber of times a file is

open ² to allow removal of data from open-file ta blewhen last processes closes it

² Disk location of the file: cache of data accessinformation² Access rights: per-process access mode information

Page 16: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 16/54

16

Open File Locking

Provided by some operating systems and filesystemsMediates access to a fileMandatory or advisory:² M andatory ² access is denied depending on

locks held and requested

² Advisory ² processes can find status of locksand decide what to do

Page 17: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 17/54

17

File Locking Example ² Java APIimport java.io.*;import java.nio.channels.*;pu blic class LockingExample {

pu blic static final boolean EXCLUSIVE = false;pu blic static final boolean SHARED = true;pu blic static void main(String arsg[]) throws I OException {

FileLock sharedLock = null;FileLock exclusiveLock = null;try {

RandomAccessFile raf = newRandomAccessFile("file.txt", "rw");

// get the channel for the file

FileChannel ch = raf.getChannel();// this locks the first half of the file - exclusiveexclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);/** Now modify the data . . . */// release the lockexclusiveLock.release();

Page 18: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 18/54

18

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 locksharedLock.release();

} catch ( java.io.I OException ioe) {System.err.println(ioe);

}finally {if (exclusiveLock != null)exclusiveLock.release();if (sharedLock != null)sharedLock.release();

}}

}

Page 19: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 19/54

19

File Types ² Name, Extension

19

Page 20: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 20/54

20

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 thisfashion.

read nextwrite next

resetno read after last write(rewrite)

20

Page 21: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 21/54

21

Access MethodsD

irect Access (relative access)² Allows ar bitrary blocks to be read or written.² We may read block 14, then 53, and then 7.² No restrictions.

read nwrite n position to n

read nextwrite next

rewrite nn = relative block num ber

21

Page 22: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 22/54

22

Access MethodsD

irect Access (relative access) (Contd).R elative block number: is an index relative to the beggingof the file.

² The first relative block of the file is 0, the next is 1 andso on, even though the actual a bsolute disk address ofblock may be 14703 for the first block, and 3192 for thesecond.

22

Page 23: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 23/54

23

Sequential-access File

23

Page 24: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 24/54

24

Simulation of Sequential Access on a Direct-Access File

24

Page 25: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 25/54

25

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, containspointers to various blocks.

To find an entry in the file, we first search the index, andthen use the pointer to access the file directly and to findthe desired entry.

25

Page 26: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 26/54

26

Other Access Methods (cont«)

Index file itself may become too large to be kept in mainmemory.

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

Page 27: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 27/54

27

Example of Index and Relative Files

27

Page 28: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 28/54

28

Directory Structure

A collection of nodes containing information a bout allfiles.

F 1 F 2F 3

F 4

F n

Directory

Files

Both the directory structure and the files reside on disk.Backups of these two structures are kept on tapes.

28

Page 29: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 29/54

29

Disk Structure

Disk can be su bdivided into partitionsDisks or partitions can be RAID protected against failureDisk or partition can be used raw ² without a file system,or formatted with a file systemPartitions also known as minidisks, slicesEntity containing file system known as a volumeEach volume containing file system also tracks that filesystem·s info in device directory or volume ta ble ofcontentsAs well as general-purpose file systems there are manyspecial-purpose file systems , frequently all within thesame operating system or computer

Page 30: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 30/54

30

A Typical File-system Organization

30

Page 31: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 31/54

31

Information in a Device Directory

NameTypeAddressCurrent length

Maximum lengthDate last accessedDate last updatedOwner ID

Protection information

Note: directory can be viewed as a sym bol ta ble thattranslates file names into their directory entries.

31

Page 32: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 32/54

32

Operations Performed on Directory

Search for a fileCreate a fileDelete a file

List a directoryRename a fileTraverse the file system

32

Page 33: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 33/54

33

Organize the Directory (Logically) to Obtain

E fficiency ² 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

Page 34: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 34/54

34

Single-Level Directory

A single directory for all users.

Naming pro blem

Grouping pro blem

34

Page 35: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 35/54

35

Two-Level Directory

Separate directory for each user.

Path nameCan have the same file name for different userEfficient searchingNo grouping capa bility

35

Page 36: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 36/54

36

Tree-Structured Directories

36

Page 37: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 37/54

37

Tree-Structured Directories (Cont«)

Efficient searching

Grouping Capa bility

Current directory (working directory)² cd /spell/mail/prog² type list

37

Page 38: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 38/54

38

Tree-Structured Directories (Cont«)

Absolute or relative path nameCreating a new file is done in current directory.Delete a file

rm <file-name>

Creating a new su bdirectory is done in current directory.mkdir <dir-name>

Example: if in current directory /mailmkdir count

mail

prog copy prt exp count

Deleting ´mailµ deleting the entire su btree rooted by´mailµ. 38

Page 39: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 39/54

39

Acyclic-Graph Directories

Have shared su bdirectories and files

Page 40: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 40/54

40

Acyclic-Graph Directories (Cont«)

Two different names (aliasing)

If dict deletes list dangling pointerSolutions:

² Backpointers, so we can delete all pointersVaria ble size records a pro blem

² 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

Page 41: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 41/54

41

General Graph Directory

Page 42: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 42/54

42

General Graph Directory (Cont...)

How do we guarantee no cycles?² Allow only links to file not su bdirectories

² Gar bage collection² Every time a new link is added use a cycle

detectionalgorithm to determine whether it is OK

Page 43: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 43/54

43

File System Mounting

A file system must be mounted before it canbe accessed

A unmounted file system (i.e. Fig. 11-11( b)) ismounted at a mount point

Page 44: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 44/54

44

(a) Existing. ( b) Unmounted Partition

Page 45: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 45/54

45

Mount Point

Page 46: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 46/54

46

File Sharing

Sharing of files on multi-user systems is desira ble

Sharing may be done through a protection scheme

On distri buted systems, files may be shared acrossa network

Network File System (NFS) is a commondistri buted file-sharing method

Page 47: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 47/54

47

File Sharing ² Multiple Users

U ser I D s identify users, allowingpermissions and protections to be per-user

Group I D s allow users to be in groups,permitting group access rights

Page 48: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 48/54

48

File Sharing ² Remote File SystemsUses 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 callsDistri buted Information Systems (distributed naming services)such as LDAP, DNS, NIS, Active Directory implement unifiedaccess to information needed for remote computing

Page 49: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 49/54

49

File Sharing ² Failure Modes

Remote file systems add new failure modes,due to network failure, server failureRecovery from failure can involve stateinformation a bout status of each remoterequestStateless protocols such as NFS include allinformation in each request, allowing easyrecovery but less security

Page 50: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 50/54

50

File Sharing ² Consistency SemanticsConsistency semantics specify how multiple usersare to access a shared file simultaneously² Similar to Ch 7 process synchronization algorithms

Tend to be less complex due to disk I/ O and networklatency (for remote file systems

² Andrew File System (AFS) implemented complexremote file sharing semantics

² Unix file system (UFS) implements:Writes to an open file visi ble immediately to other

users of the same open fileSharing file pointer to allow multiple users to readand write concurrently

² AFS has session semanticsWrites only visi ble to sessions starting after the file isclosed

Page 51: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 51/54

51

Protection

File owner/creator should be able to control:² what can be done² by whom

Types of access² Read² Write

² Execute² Append² Delete² List

Page 52: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 52/54

52

Access Lists and GroupsMode of access: read, write, executeThree classes of users

RWXa) owner access 7 1 1 1

RWXb) group access 6 1 1 0

RWXc) public access 1 0 0 1Ask manager to create a group (unique name), say G, and add someusers to the group.For a particular file (say game) or su bdirectory, define an appropriateaccess.

owner group public

chmod 761 game

Attach a group to a filechgrp G game

Page 53: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 53/54

53

Windows XP Access-control List Management

Page 54: (Ch11)File System Interface

8/8/2019 (Ch11)File System Interface

http://slidepdf.com/reader/full/ch11file-system-interface 54/54

54

A Sample UNIX Directory Listing