Module 11: File-System Interface

43
Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.1 Module 11: File-System Interface File Concept Contiguous logical address space – Types: Data – numeric – character – binary Program

description

Module 11: File-System Interface. File Concept Contiguous logical address space Types: Data numeric character binary Program. File Structure. 내부구조에 대한 제한이 없음 – Just a sequence of words, bytes Simple record structure Lines Fixed length Variable length Complex Structures - PowerPoint PPT Presentation

Transcript of Module 11: File-System Interface

Page 1: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.1

Module 11: File-System Interface

• File Concept– Contiguous logical address space– Types:

Data– numeric– character– binary

Program

Page 2: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.2

File Structure

• 내부구조에 대한 제한이 없음 – Just a sequence of words, bytes

• Simple record structure

– Lines

– Fixed length

– Variable length

• Complex Structures

– Formatted document

– Relocatable load file – text, data, symbol table, other headers, …

• Who decides:

– Operating system

– Program

Page 3: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.3

File Attributes (file metadata)

• Name – only information kept in human-readable form.

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

Page 4: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.4

File Types – name, extension

Executable exe, com, bin ornone

ready-to-run machine-language program

Object obj, o complied, machinelanguage, not linked

Source code c, p, pas, 177,asm, a

source code in variouslanguages

Batch bat, sh commands to thecommand interpreter

Text txt, doc textual data documents

Word processor wp, tex, rrf, etc. various word-processorformats

Library lib, a libraries of routines

Print or view ps, dvi, gif ASCII or binary file

Archive arc, zip, tar related files groupedinto one file, sometimescompressed.

File Type Usual extension Function

{object modules}

Page 5: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.5

File Operations

• create

• write

• read

• reposition within file – file seek

• delete

• truncate

• open(Fi) – File 의 meta- 정보를 디스크로부터 메모리로 가져옴

Search the directory structure to do this

• close (Fi)

Page 6: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.6

Access Methods

• Sequential Access [ 카셋 테잎과 유사 ]read nextwrite next reset to BOFno read after last write

(rewrite)

• Direct Access [LP 레코드 판과 유사 ]read nwrite nposition to n

read nextwrite next

rewrite n

** n = relative block number

Page 7: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.7

Directory Structure

• A collection of nodes containing information about files.

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.

Page 8: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.8

Directory Structure (UNIX 방식 )

F 1 F 2 F 3 F 4F n

Directory

F nF 4F 3F 2

F 1

이름과

pointer

File 의

실제 Data

File 의

Meta data

Metadata

Pointers to data

( 여러 disk 섹터들 )

Page 9: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.9

Operations Performed on Directory

• Search for a file

• Create a file

• Delete a file

• List a directory

• Rename a file

• Traverse the file system

Page 10: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.10

Single-Level Directory

• A single directory for all users.

• Naming problem– 충돌– DOS 11 char, UNIX 255 char

• Grouping problem

Page 11: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.11

Two-Level Directory

• Separate directory for each user.

• “Pathname”

• 사용자간 이름출동 없음

• No grouping capability

Page 12: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.12

Tree-Structured Directories

•Subdirectory 허용 (per each user)

•Grouping 가능

•Home directory (per each user)

•Current (working) directory

•Absolute, relative pathname

eg cd / koh / ch1

–Search path ( 명령어 해석기 ) – 현재 환경

–삭제시 문제 : empty directory? ( 편의 /위험 )

•Subdirectory 허용 (per each user)

•Grouping 가능

•Home directory (per each user)

•Current (working) directory

•Absolute, relative pathname

eg cd / koh / ch1

–Search path ( 명령어 해석기 ) – 현재 환경

–삭제시 문제 : empty directory? ( 편의 /위험 )

Page 13: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.13

Acyclic-Graph Directories

• The same (subdirectory, file) may be in two different directories

•No cycles allowed (acyclic)

•Directory 공유

•“Both Koh & Lee need directory Y below”

•No cycles allowed (acyclic)

•Directory 공유

•“Both Koh & Lee need directory Y below”

X

Cycle not allowedCycle not allowed

Page 14: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.14

Acyclic-Graph Directories

• The same (subdirectory, file) may be in two different directories

1. traverse 시 문제 (visit same node twice) 2. Delete 시 문제

• 시나리오 :

1. Koh has subdirectory X (ie Koh has an entry for X)

2. Lee need to have X below his directory

3. UNIX – creates a “link” ( a new directory entry) in Lee’s directory

( 방법 – “ Hard link”)

원래 Koh directory 의 entry 를 (file 의 metadata) 물리적으로 (lee 에도 ) 복사X 를 update 할 경우 consistency 문제 발생 (eg

protection mode 변경 )

X 를 delete 하면 ? Rerefence count 를 운영하여 0 이 되면 file delete.

( 방법 - “Symbolic link”)

Link is pathname -- 원장은 물리적으로 한부만 (X- 원장 소재의 pathname)

문제 : koh 가 X - 원장을 삭제하면 lee 의 link 는 dangling reference 가 된다 ?

Lee 가 access 시도할 때 Just say “…. illegal file ….”

Koh Lee X

Page 15: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.15

General Graph Directory

Cycle

Page 16: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.16

General Graph Directory (Cont.)

• Cycle 허용

• 대신 traverse, delete 등의 알고리즘이 까다로와 짐

• Traverse– Cycle 내에서 infinite loop traverse 가 파행이 됨

• Delete– 즉 X 의 reference count 는 결코 0 이 안됨 (0 면 delete)– (self-referencing) – Koh 에서 X link 를 지워도 X 는 deallocate 가 안됨– ( 즉 외부로부터 X 를 access 하는 길이 없어져도 ..)– 이 경우 Garbage collection 에서 처리 가능 .

1st phase -- access 불가능한 node 들을 marking 2 nd phase – delete marked nodes그러나 Disk 경우 garbage collection 이 매우 느림 – 자주 못함

• How do we guarantee no cycles?– Allow links only to files (not to subdirectories).– Every time a new link is added, use a cycle detection algorithm to determine whether it is OK.

Koh X

Page 17: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.17

Protection

• File owner/creator should be able to control:– what operation can be performed on the file– by whom

• Types of access– Read– Write– Execute– Append– Delete– List (name, attribute)

• Access Control 방법– Access control matrix me group all

Too big

• Unix – 전체 user 를 세 구분으로 ( 여러 group?) rwxrwxrwx

• Password per file -- all-or-nothing, 관리 , 암기 , … 문제

Page 18: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.18

Groups

• Mode of access: read, write, execute

• Three classes of users Octal RWXa) owner access ‘7’ 1 1 1

RWXb) group access ‘6’ 1 1 0

RWXc) public ( 아무나 ) access ‘1’ 0 0 1

• Ask super-user to create a group (unique name), say G,

and add users to the group.

• Attach a group to a file

chgrp G game

• For a particular file (say game) or subdirectory, define an appropriate access.

owner group public

chmod 761 Game 761 = 111 110 001

Page 19: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.19

File-System Structure

• File system resides on secondary storage (disks) == (data structure + code)

• File system organized into layers. (logical file system I/O control device)

• Open() system call– 디스크로부터 지정된 파일의 메타정보를 메모리로 가지고 옴

이를 위하여 directory structure 를 search– Open-file table

메모리에 있는 ( 현재 open 된 ) 파일들의 메타정보 보관소– File descriptor (file handle, file control block)

Index into this table– Why?

Directory search is expensive 예 : /a/b/c/d/e.hwp So many files in disk

• Mounting (see next two pages) disk I/O’s

Page 20: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.20

Filemetadata

File Content

Filemetadata

Open() retrieves metadata

from disk to main memory

File-System Structure

Page 21: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.21

Filecontent

Filecontent

Filecontent

Filemetadata

Filecontent

Filemetadata

This metadata has pointers to data sectors

Page 22: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.22

Filecontent

Filecontent

Filecontent

File Ametadata

This metadata has pointers to data sectors

Filecontent

Filecontent File

content

File Bmetadata

Index(File handle)

(File descriptor)

Page 23: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.23

HD

CD

FD

“root file system”

/

bin etc usr

date sh getty passwd

Now all files under root file system can be accessed

Question: how do we access files in other FD?

Mounting File-System (UNIX)

HD: Hard DiskCD: Compact DiskFD: Floppy Disk

(Boot 시 선택된 file system)

Page 24: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.24

HD

CD

FD

/

bin etc usr

date sh getty passwd

/

bin include src

utsstudio.hbanner yacc

Mount it!

Answer: (1) Windows 경우 -- 각자 root (C: D: E: )

(2) UNIX 경우 ---- mount them ( 구분 불가 )

새 pathname /usr/src/uts

Page 25: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.25

Allocation of File Data in Disk

1. Contiguous – 한 덩어리로2. Linked – sector 들을 link 로 연결3. Indexed – sector pointer 들

Page 26: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.26

Contiguous Allocation (1/3)

• Each file occupies a set of contiguous blocks on the disk.

• Simple – only starting location (block #) and length (# of blocks) are required.

• Wasteful of space – dynamic storage-allocation external fragmentation numerous small holes

• File grow 가 어려움 .– file 생성시 얼마나 큰 hole 을 배당할 것인가 ?– grow 가능 vs 낭비 (internal fragmentation)

• Fast I/O– 한번의 seek/rotation으로 많은 바이트 transfer– Realtime file 용으로 , 또는 – 이미 run 중이던 process 의 swapping 용 (total R/W 보장 , no grow)

어려움 : 공간관리이득 : 속도

Page 27: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.27

Linked Allocation (2/3)

• Each file is a linked list of disk blocks:

• blocks may be scattered anywhere on the disk.

pointerEach block =

FileContent

Page 28: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.28

• Allocate as needed, link together; e.g., file starts at block 9

Head pointer

한개만 관리

Page 29: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.29

Linked Allocation (Cont.)

• Simple – need only starting address

• Free-space management system – no waste of space

• 단점– No random access– Disk I/O efficiency ( 매 sector I/O 마다 seek/rotation)– Reliability 문제

( 한 sector 가 고장나 pointer 가 유실되면 … . 많은 부분을 잃음 )– Space for pointers (0.78% for pointers – 512 B/sector, 4 B/pointer)

• 변형 : File-allocation table (FAT)

– disk-space allocation used by MS-DOS and OS/2.

Page 30: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.30

F A T

Directory entry

file_name start-block

Koh 217 217 339

339 618

618 EOF

0

F A T

( 이 table 에 link point 들만 모아 놓음 )(One entry for each disk block)

(Unused block)

** 장점 : random access time is improved

1st DataSector

217

1st block of file2nd DataSector

339

3rd DataSector

618

Page 31: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.31

Indexed Allocation (3/3)

• Each file has its own index block (part of file metadata)

• Index block is an array of pointers to disk blocks

• Speed up direct access.

• Logical view.

index table

disk blocks

Page 32: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.32

Example of Indexed Allocation

pointer

여러 개 필요

Page 33: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.33

Indexed Allocation – Mapping (Cont.)

• What if file is too large? one disk block is too small for index table

1. Linked scheme – Link blocks of index table (no limit on size).

2. Two-level index

3. Combined (UNIX 예 )

d

d

d

each index nodefan out N

i

ii

Page 34: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.34

Combined Scheme: UNIX (4K bytes per block)

Page 35: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.35

Management of Free Space

Page 36: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.36

Free-Space Management

• Bit map or bit vector

0 1 2 n-1

bit[i] =0 block[i] free

1 block[i] occupied

– Block number calculation

– Bit map requires extra space. – Easy to get contiguous files

(number of bits per word) *(number of 0-value words) +offset of first 1 bit

01 1 1

Page 37: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.37

Free-Space Management (Cont.)

• Linked list – Link all the free blocks (free list)– Traversing this list is slow (disk I/O), but is not frequent – Cannot get contiguous space easily– No waste of space

• Grouping – Modification of linked list approach– First free block has n pointers

n-1 pointers point to free block Last block same kind (another n pointers)

• Counting– Keep track of (first free block, # of contiguous free blocks)– Generally, several contiguous blocks are allocated & freed together

…. These are data structure problems …….

Page 38: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.38

• So …

• “File System” means data structure on disk

• “logical formatting” – initializing this data structure

• Accessing a disk file is OS dependent operation

Page 39: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.39

Directory Implementation

• Linear list of file names with pointers to the data blocks.– simple to program– Linear search to find a particular entry– which is time-consuming

• Hash Table– linear list (but has extra hash table)– Hash table converts (file name) to pointer to (this file’s linear list).– decreases directory search time– collisions – situations where two file names hash to the same

location

File 이름 Pointers

숙제 .hwp그림 .jpeg강의 .pptgame.exe

1223 984

34

Page 40: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.40

Efficiency and Performance

• Efficient use of disk space is dependent on:

– disk allocation and directory algorithms

– types of data kept in file’s directory entry

• Performance

– disk cache separate section of main memory for frequently used blocks

– free-behind and read-ahead techniques to optimize sequential access Sequential access 이면 next block request 할때 현 block 을

free

– virtual disk, or RAM disk. improve PC performance by dedicating section of memory

Page 41: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.41

Various Disk-Caching Locations

Page 42: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.42

Recovery

• Consistency checker

– Some part of file system is kept in memory

– What if system crashes? Not all the info can be saved to disk

– If directory or file control block (metadata) are lost?

– At boot time -- compare data in directory structure

with data blocks on disk,

and tries to fix inconsistencies.

• Use system programs to back up data from disk to another storage device (floppy disk, magnetic tape).

• Recover lost file or disk by restoring data from backup.

Page 43: Module 11:  File-System Interface

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 11.43

We skip

Chapter 12, 14-17.