Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Goals
System calls for the file System and Algorithm Understanding System calls internal procedure
Introduction (cont.)
Advanced system call
implementation of pipes : pipe , dup extending file system tree : mount , umount change the structure of the file system hierarchy : link
and unlink
mount table
Introduction (cont.)
File System Calls
File I/O
read write lseek
stat creat link unlink mknod mknod link mount umount unlink namei iget iput getblk
mount umount
chdir chown
Lower Level File System Algorithms Ialloc ifree alloc free bmap
Buffer allocation algorithms brelse bread breada bwirte File System Calls and Relation to Other Algorithms
OPEN
Syntax
fd = open(pathname, flags, modes); pathname: file name flags : type of open modes : file permission if file is being created fd : File Descriptor
File Descriptor
Used
in file operation ( reading, writing, seeking, duplicating the file descriptor, setting file I/O parameters, determining file status, and closing file )
OPEN Algorithm
{
convert file name to inode(algorithm namei); if(file does not exist or not permitted access) return (error); allocate file table entry for inode, initialize count, offset; allocate user file descriptor entry, set pointer to file table entry; if(type of open specifies truncate file) free all file blocks (algorithm free); unlock(inode) /* locked above in namei */ return(user file descriptor); }
file table
inode table
count (/etc/passwd) 2 . . .
. . .
count (local) 1
. . .
Data Structure after Open fd1 = open("/etc/passwd", O_RDONLY); fd2 = open("local", O_RDWR); fd3 = open("etc/passwd", O_WRONLY);
Note
Why Does not User File Table entry contain file offset for
the position of the next I/O operation and point directly to the in-core inode entry for the File ? Tompson said
To share of the offset pointer between several user file
READ
syntax
number = read(fd, buffer, count) fd : file descriptor returned by open buffer : address of structure in the user process that will contain the read data count : number of bytes the user wants to read number : number of bytes actually read
READ Algorithm
get file table entry from user file descriptor; check file accessibility; set parameters in u area for user address, byte count, I/O to user; get inode from file table; lock inode; set byte offset in u area from file table offset while(count not satisfied) {
convert file offset to disk block (algorithm bmap); calculate offset into block, number of bytes to read; if(number of bytes to read is 0) /* trying to read end of file */ read block(algorithm breada if with read-ahead, algorithm bread otherwise); copy data from system buffer to user address; update u area fields for byte offset,read count,address to write into user space; release buffer; /* locked in bread */
} unlock inode; update file table offset for next read; return(total number of bytes read);
number for the data and competing with other processes for access to the buffer pool.
READ Example
Sample Souce code
#include<fcntl.h> Main() { int fd; char lilbuf[20], bigbuf[1024]; fd = open(etc/passwd, O_RDONLY); read(fd,lilbuf, 20); read(fd, bigbuf, 1024); read(fd, lilbuf, 20); }
Sample Analysis
First read call Second read call
#include<fcntl.h> main() { int fd; char buf[512]; fd = open(/etc/passwd,O_RDONLY); read(fd, buf, sizeof(buf)); read(fd, buf, sizeof(buf)); }
#include<fcntl.h> main() { int fd; char buf[512]; fd = open(/etc/passwd,O_WRONLY); write(fd, buf, sizeof(buf)); write(fd, buf, sizeof(buf)); }
WRITE
Syntax
number = write(fd, buffer, count);
algorithm
same as read however if the file does not contain a block that
correspond to the byte offset to be writeen, the kernel allocates a new block using algorithm alloc and assigns the block number to the correct position in the inode's table of contents. through internal loop, write one block to disk during each iteration.
delayed write
Record Locking
Capability to prevent other processes from reading or
Goal
To position the I/O and allow random access to a file
Syntax
position = lseek(fd, offset, reference); Reference :-> set the beginning of offset position
beginning of the file(0) current position of read/write offset(1) end of the file(2)
position: byte offset where the next read or write will start
Function
kernel simply adjusts the offset value in the file table
CLOSE
Goal
Close an open file
Syntax
close(fd);
file table
inode table
Count 1
. . .
. . .
. . . . . . . . .
. . .
count (/etc/passwd) 2 . . .
. . .
Count 1 Count 0
0 1 2 3 4 5
count (local) 1 . . .
count (local) 1 . . .
NULL NULL
Count 1 Count 0
. . .
FILE CREATION
Goal
Create a New File
Syntax
fd = creat(pathname, modes);
Function
If File does not exist kernel create new file with specified name and permission mode If File exists truncates the file( release all existing data blocks and sets the file size to 0 ) subject to suitable file access permissions.
CREATE Algorithm
get inode for file name (algorithm namei); if(file already exists) { if(not permitted access) { release inode (algorithm iput); return(error); } } else /* file does not exist yet */ { assign free inode from file system(algorithm ialloc); create new directory entry in parent directory; include new file name and newly assigned inode number; } allocate file table entry for inode, initialize count; if(file did exist at time of create) free all file blocks (algorithm free); unlock(inode); return(user file descriptor)
Syntax
mknod(pathname, type and permissions, dev) pathname: name of the node to be created type and permissions : node type and access permission dev: specifies the major and minor device numbers for block and character special files
Function
allocates inode for the file
Algorithm
if(new node not named pipe and user not super user) return(error); get inode of parent of new node ( algorithm namei ); if(new node already exists) { release parent inode ( algorithm iput ); return(error); } assign free inode from file system for new node (algorithm ialloc); create new directory entry in parent directory : include new node name and newly asigned inode nubmer; release parent directory inode (algorithm iput); if(new node is block or character special file) write major, minor numbers into inode structure; release new node inode (algorithm iput);
CHANGE DIRECTORY
Goal
Change the current directory of a process
Syntax
chdir(pathname); pathname : directory that becomes the new current direcotry of the process
Algorithm
get inode for new directory name (algorithm namei); if(inode not that of directory or process not permitted access to file) { release inode (algorithm iput); return(error); } unlock inode; release "old" current directory inode (algorithm iput); replace new inode into current directory slot in u area;
CHANGE ROOT
Goal
Change the notion of the file system root
Syntax
chroot(pathname); pathname: Directory that the kernel subsequently treats as the processs root directory.
Algorithm
Same with chdir
Notes
The new inode is the logical root of the file system for
Syntax
chown(pathname, owner, group); chmod(pathname, mode);
Function
Chown Convert the file name to inode(using algorithm namei) Assign the new owner and group Clear the set user and set group flags Release the inode via algorithm iput Chmod
the file type, file owner, access permissions, file size , number of links, inode number, and file access times.
Syntax
stat(pathname, statbuffer); fstat(fd, statbuffer);
Function
simply write the fields of the inode into statbuffer( which
PIPES
Goal
Transfer of data between processes in FIFO Synchronization of process execution
Unnamed Pipe By pipe system call Only related processes can share access
Usage
Use the regular system calls for files read, write, close
Function
Assign inode Allocate a pair of user file descriptor and
Link
It links a file to a new name in the file system dire ctory structure, creating a new directory entry for an existing inode. Syntax: link(source file name, target file name); -Source file name is name of existing file -target file name is new name of file which create d after completion of link call
if (new file name already exists or existing file, new fi le on different file systems) { undo update done above; return (error); } create new directory entry in parent directory of new filename: include new file name, inode number of existing file name; release parent directory inode (algorithm iput); release inode of existing file (algorithm iput); }
Unlink
ked from the directory hierarchy. Example: Unlink(myfile); Open(myfile, O_RDONLY); Here open call will fail
Unlink cont
algorithm unlink input: file name output: none { get parent inode of file to be unlinked (algorithm na mei); /* if unlinking the current directory... */ if (last component of file name is ".") increment inode reference count; else
get inode of file to be unlinked (algorithm iget); if (file is directory but user is not super user) { release inodes (algorithm iput); return (error); } if (shared text file and link count currently 1) remove from region table; write parent directory: zero inode number of unlinked file; release inode parent directory (algorithm iput); decrement file link count; release file inode (algorithm iput); /* iput checks if link count is 0: if so, * releases file blocks (alg orithm free) and * frees inode (algorithm ifree); */ }
Unlink cont