File Management
Operating System Components File Manager Process & Resource Manager Memory Manager Device Manager Processor(s) Main Memory Devices Computer Hardware
Why Programmers Need Files <head> … </head> <body> </body> HTML Editor Web Browser Structured information Can be read by any applic Accessibility Protocol <head> … </head> <body> </body> foo.html File Manager Persistent storage Shared device
Fig 13-2: The External View of the File Manager Application Program CreateFile() ReadFile() CloseHandle() SetFilePointer() WriteFile() mount() write() open() close() read() lseek() File Mgr Device Mgr Memory Mgr Process Mgr File Mgr Device Mgr Memory Mgr Process Mgr UNIX Windows Hardware
Introduction What is a file? Where is a file located physically? What are the steps to access a file?
File Management File is a named, ordered collection of information The file manager administers the collection by: Storing the information on a device Mapping the block storage to a logical view Allocating/deallocating storage Providing file directories What abstraction should be presented to programmer?
File system context
Levels in a file system
Levels of data abstraction
Logical structures in a file
Information Structure Applications Records Structured Record Files Record-Stream Translation Byte Stream Files Stream-Block Translation Storage device
Byte Stream File Interface Implements the block-stream interface Info on file held in file descriptor described later Typical operations on file fileID = open(fileName) close(fileID) read(fileID, buffer, length) write(fileID, buffer, length) seek(fileID, filePosition)
Low Level Files ... ... b0 b1 b2 bi Stream-Block Translation fid = open(“fileName”,…); … read(fid, buf, buflen); close(fid); ... ... b0 b1 b2 bi int open(…) {…} int close(…) {…} int read(…) {…} int write(…) {…} int seek(…) {…} Stream-Block Translation Storage device response to commands
File meta-data File contain data plus information about the data, that is, meta-data Meta-data is kept in a file descriptor
File Descriptor Information External name Current state Sharable Owner User Locks Protection settings Length Time of creation Time of last modification Time of last access Reference count Storage device details
File Descriptor in Unix File descriptor in UNIX is called an inode (index node), containing the following entries
Structured Files A file is a stream of bytes Usually want to access in a structured manner May have no structure imposed (UNIX) Must be provided by application May have a structure imposed (VMS) Need to maintain additional information Type of file Access methods Other information
Block Record Translation Records Record-Block Translation
Record-Oriented Sequential Files Logical Record fileID = open(fileName) close(fileID) getRecord(fileID, record) putRecord(fileID, record) seek(fileID, position) A structured sequential file is a named sequence of logical records, indexed by nonnegative integers Records may be of fixed size, or variable size This is determined by file manager
Record-Oriented Sequential Files Logical Record H byte header k byte logical record Next Header ... Header contains record descriptor information (occupies H bytes) Logical record takes up k bytes – fixed size
Record-Oriented Sequential Files Logical Record H byte header k byte logical record ... ... Physical Storage Blocks Fragment
Electronic Mail Example struct message { /* The mail message */ address to; address from; line subject; address cc; string body; }; struct message *getRecord(void) { struct message *msg; msg = allocate(sizeof(message)); msg->to = getAddress(...); msg->from = getAddress(...); msg->cc = getAddress(...); msg->subject = getLine(); msg->body = getString(); return(msg); } putRecord(struct message *msg) { putAddress(msg->to); putAddress(msg->from); putAddress(msg->cc); putLine(msg->subject); putString(msg->body); }
Record-Oriented Sequential Files Fixed size records can be a problem Applications requiring large record sizes would require that the programmer break the records into smaller pieces Applications only requiring small record sizes would waste space A solution is for the file system to be enhanced to include a function to define the record size for a file – encoded in header
Indexed Sequential File Suppose we want to directly access records Add an index to the file fileID = open(fileName) close(fileID) getRecord(fileID, index) index = putRecord(fileID, record) deleteRecord(fileID, index)
Indexed Sequential File (cont) Application structure index = i Account # 012345 123456 294376 ... 529366 965987 Index i k j index = k index = j
More Abstract Files Inverted files Multimedia storage System index for each datum in the file Records accessed based on appearance in table rather than their logical location Company accounts may be accessed by customer name, but customer may have several accounts Set up external index table by name with pointers to the main table Multimedia storage Records contain radically different types Access methods must be general
Database Management Systems A database is a very highly structured set of information Stored across different files Optimized to minimize access time DBMSs implementation Some DBMSs use the normal files provided by the OS for generic use Some use their own storage device block
File systems File system A data structure on a disk that holds files actually a file system is in a disk partition a technical term different from a “file system” as the part of the OS that implements files File systems in different OSs have different internal structures
A file system layout
Implementing Low Level Files Process needs to be able to read from and write to storage devices Simplest system is byte stream file system (will consider record-oriented systems later) Storage device may be accessed 2 ways Sequentially – like a tape drive Randomly – like a magnetic disk
Low-level File System Architecture Block 0 b0 b1 b2 b3 … … bn-1 . . . Sequential Device Randomly Accessed Device
Low Level Files Management Secondary storage device contains: Volume directory (sometimes a root directory for a file system) External file descriptor for each file The file contents Manages blocks Assigns blocks to files (descriptor keeps track) Keeps track of available blocks Maps to/from byte stream
File Manager Data Structures Keep the state of the process-file session 2 Copy info from external to the open file descriptor 1 Open File Descriptor Process-File Session 3 Return a reference to the data structure External File Descriptor
An open Operation Locate the on-device (external) file descriptor Extract info needed to read/write file Authenticate that process can access the file Create an internal file descriptor in primary memory Create an entry in a “per process” open file status table Allocate resources, e.g., buffers, to support file usage
A close Operation Completes all pending operations Release I/O buffers Release locks process holds on file Update external file descriptor Deallocate file status table entry
Opening a UNIX File On-Device File Descriptor Open File Table fid = open(“fileA”, flags); … read(fid, buffer, len); On-Device File Descriptor 0 stdin 1 stdout 2 stderr 3 ... File structure inode Open File Table Internal File Descriptor
Block Management The job of selecting & assigning storage blocks to the file For a fixed sized file of k blocks File of length m requires N = m/k blocks Byte bi is stored in block i/k The logical file is divided into logical blocks Each logical block is mapped to a physical disk block
Locating file data The file descriptor contains data on how to perform this mapping there are many methods for performing this mapping Three basic strategies: Contiguous allocation Linked lists Indexed allocation
Dividing a file into blocks
Disk Organization … … … … … … … … Boot Sector Volume Directory Blk0 Blk1 … Blkk-1 Track 0, Cylinder 0 … Blkk Blkk+1 Blk2k-1 Track 0, Cylinder 1 … … Blk Blk Blk Track 1, Cylinder 0 … … Blk Blk Blk Track N-1, Cylinder 0 … … Blk Blk Blk Track N-1, Cylinder M-1
Contiguous Allocation Maps the N blocks into N contiguous blocks on the secondary storage device Simple to implement Random access Does not provide for dynamic file sizes If you want to extend a file, hope there is an empty block following, or recopy the entire file to a larger group of unallocated contiguous blocks Head position 237 … First block 785 Number of blocks 25 File descriptor
A contiguous file
Keeping a file in pieces We need a block pointer for each logical block, an array of block pointers block mapping indexes into this array Each file is a linked list of disk blocks But where do we keep this array? usually it is not kept as contiguous array the array of disk pointers is like a second related file (that is 1/1024 as big)
Block pointers in the file descriptor
Block pointers in contiguous disk blocks
Linked Lists Each block contains a header with Number of bytes in the block Pointer to next block Blocks need not be contiguous Files can expand and contract Seeks can be slow First block … Head: 417 ... NULL Length Length Length Byte 0 Byte 0 Byte 0 ... ... ... Byte 4095 Byte 4095 Byte 4095 Block 0 Block 1 Block N-1
Linked Lists – cont.
Indexed Allocation Extract headers and put them in an index Simplify seeks May link indices together (for large files) Byte 0 ... Index block … Head: 417 ... Byte 4095 Length Block 0 Length Byte 0 ... Byte 4095 Block 1 Byte 0 ... Length Byte 4095 Block N-1
Block pointers in an index block
Block pointers in an index block – cont.
Chained index blocks
Two-level index blocks
Two-level index blocks – cont. primary index secondary index table data blocks
File system layout variations New UNIX file systems use cylinder groups (mini-file systems) to achieve better locality of file data MS/DOS uses a FAT (file allocation table) file system so does the Macintosh OS (although the MacOS layout is different)
UNIX Files inode Data Data Data Index Data Index Data Index Index mode owner … Direct block 0 Direct block 1 Direct block 11 Single indirect Double indirect Triple indirect Data Data Data Index Data Index Data Index Index Index Data Index Data Index Index Data Index Data
DOS FAT Files … Logical Linked List File Descriptor Disk Disk Block 43 254 … 107 Disk Block Disk Block Disk Block Logical Linked List
DOS FAT Files … … File Descriptor Disk Block Disk Block Disk Block 43 254 … 107 Disk Block Disk Block Disk Block File Descriptor 43 43 254 … 107 Disk Block Disk Block 107 Disk Block 254 File Access Table (FAT)
Unallocated Blocks How should unallocated blocks be managed? Need a data structure to keep track of them Block status map (or disk bitmap) Small enough to be held in primary memory Linked list (or free list) Very large Hard to manage spatial locality (need to scan list to find blocks ‘close to’ each other)
Free-Space Management Bit vector (n blocks) 1 2 n-1 … 1 block[i] free 0 block[i] occupied bit[i] = First free block number (number of bits per word) * (number of 0-value words) + offset of first 1 bit
Free-Space Management - cont. Bit map requires extra space. Example: block size = 212 bytes disk size = 230 bytes (1 gigabyte) n = 230/212 = 218 bits (or 32K bytes) Easy to get contiguous files Linked list (free list) Cannot get contiguous space easily No waste of space
Free list organization
Free-Space Management – cont. Need to protect: Pointer to free list Bit map Must be kept on disk Copy in memory and disk may differ. Cannot allow for block[i] to have a situation where bit[i] = 0 in memory and bit[i] = 1 on disk. Solution: Set bit[i] = 0 in disk. Allocate block[i] Set bit[i] = 0 in memory
Marshalling the Byte Stream Must read at least one buffer ahead on input Must write at least one buffer behind on output Seek flushing the current buffer and finding the correct one to load into memory Inserting/deleting bytes in the interior of the stream
Buffering Storage devices use Block I/O Files place an explicit order on the bytes Therefore, it is possible to predict what will be read after bytei When file is opened, manager reads as many blocks ahead as feasible After a block is logically written, it is queued for writing behind, whenever the disk is available Buffer pool – usually variably sized, depending on virtual memory needs Interaction with the device manager and memory manager
Supporting Other Storage Abstractions Low-level file systems avoid encoding record-level functionality If applications use very large or very small records, a generic file manager may be efficient Some operating systems provide a higher-layer file system to support applications with large or small files Database management systems and multimedia documents are examples
Other Storage Abstractions Modern, open operating systems tend towards low-level file systems Proprietary operating systems designed for specific applications implement higher layer files systems Structured Sequential Records Contain collections of logical records Need to read from or write to entire records
Other Storage Abstractions Indexed sequential files File manager keeps table for each open file and maps index to block containing the record Consumes space Read/write operations more complex Buffering is not of much value (records accessed in arbitrary order) Multimedia Requires large files and high bandwidth Use larger block sizes Try to use contiguous block allocation
Directories A directory is a set of logically associated files and other directories of files Directories are the mechanism we use to organize files The file manager provides a set of commands to manage directories Traverse a directory Enumerate a list of all files and nested directories
Directories Directory commands enumerate copy rename delete traverse etc.
Directory Structures How should files be organized within directory? Flat name space All files appear in a single directory Hierarchical name space Directory contains files and subdirectories Each file/directory appears as an entry in exactly one other directory -- a tree Popular variant: All directories form a tree, but a file can have multiple parents.
Directory Structures
Directory Structures – cont.
A directory tree
Directory Implementation Device Directory A device can contain a collection of files Easier to manage if there is a root for every file on the device -- the device root directory File Directory Typical implementations have directories implemented as a file with a special format Entries in a file directory are handles for other files (which can be files or subdirectories)
Directory Implementation – cont. Sorted linear list of file names with pointers to the data blocks simple to program time-consuming to execute Hash Table – linear list with hash data structure decreases directory search time collisions – situations where two file names hash to the same location fixed size
Directory Implementation – cont. Physical disk may be divided into two or more logical disks Bitmap table doesn’t need to be as large Easier to archive Can handle several operating systems Requires partitioning at device driver level
Mounting file systems Each file system has a root directory We can combine file systems by mounting that is, link a directory in one file system to the root directory of another file system This allows us to build a single tree out of several file systems This can also be done across a network, mounting file systems on other machines
Mounting a file system
UNIX mount Command / bin usr etc foo bill nutt bar abc cde xyz blah
UNIX mount Command mount bar at foo / / bin usr etc foo bin usr etc bill nutt bill nutt abc cde xyz bar blah abc cde xyz mount bar at foo blah
More on Files
File names Directory Path name Maps component names into objects (files or directories) Path name A sequence of component names specifying a path of directories absolute path: starts at the root directory relative path: starts at the working directory File name extension: suffix of a component names that indicate the type of the file Alias: alternate path names for a file
File name space topologies
Some common file extensions file.c -- a C program file file.txt -- a text file file.s -- an assembly language file file.obj -- an object file (in MS/DOS) file.o -- an object file (in UNIX) file.exe -- an executable file (in MS/DOS) file.wk1 -- spreadsheet worksheet file.tex -- tex or latex (a text formatter) file file.mif -- Framemaker interchange file file.scm -- Scheme program file file.tmp -- a temporary file
Path name examples /home/faculty/egle/os/book/ch02 – UNIX /home/student/jdoe/os/proj2 – UNIX book/ch02 –UNIX relative path name E:\egle\class\os\book\ch02 – MS/DOS users:u1:egle:os:book:ch02 – Macintosh disk$faculty:[egle.os.book]ch02 – VMS [.book]ch02 – VMS relative path name
Open Files When a file is opened, the file manager keeps additional dynamic information File position
File Operations openFile = open(file name) openFile = create(file name) fileMetaData = status(file name) okay = access(file name, access type) okay = change mode(file name, new mode) changes protection information okay = change owner(file name,new owner)
Open file operations bytesRead = read(open file) bytesWritten = write(open file) newFilePos = seek(open file, how much, how) -- move file position close(open file) openFile = duplicate(open file) fileLock(open file) fileControl(open file) twoOpenFiles = pipe()
Directory operations in UNIX link(file name, alias name) unlink(file name): delete file rename(old name, new name) makeDirectory(directory name) removeDirectory(directory name)
Two major parts of a file system The file manager needs to map a filename to a collection of physical blocks on the storage devices
File system data structures
Flow of control for an open
Flow of control for a read
Connecting files and devices
Special files Special files are not ordinary files e.g. directories, devices, pipes, message queues, remote files, mounted directories, etc. They are marked by flags in the file descriptor The read and write operations are directed to code for that type of special file a case statement determines how to handle a read or write operation
Fork data structure changes
System call data structure changes
Duplicate data structure changes
Pipe data structure changes
Avoiding data copies
Path name lookup algorithm