System Call for File System
-
Upload
vijal-chokshi -
Category
Documents
-
view
222 -
download
0
Transcript of System Call for File System
-
8/3/2019 System Call for File System
1/35
Click to edit Master subtitle style
4/27/12
Chapter 5System Calls for File
System
-
8/3/2019 System Call for File System
2/35
4/27/12
System calls that return file descriptors for use inother system call;
System calls that use the namei algorithm toparse a path name;
System calls that assign and free inodes , using
algorithm ialloc and ifree; System calls that set or change the attribute of a
file;
System calls that do I/O to and from a process ,using algorithms alloc ,free, and the bufferallocation algorithms;
System calls that change the structure of the filesystem;
S stem calls that allow a rocess to chan e its
-
8/3/2019 System Call for File System
3/35
-
8/3/2019 System Call for File System
4/35
4/27/12
The kernel searches the file system for the file
name parameters using namei algorithm; It checks permissions for opening the file after it
finds the in-core inode and allocates an entry inthe file table for open file;
The file table entry contains a pointer to the inodeof the open file and a field that indicates the byteoffset in the file where the kernel expects the nextread or write begin;
The kernel initializes the offset 0 during the opensystem call;
The kernel allocate an entry in a private table inthe process u area called the user file descriptor
table;
-
8/3/2019 System Call for File System
5/35
4/27/12
Algorithm open
Inputs : file name
type of open
file permissions (for creation type ofopen)
Output : file descriptor
1) convert file name to inode(algorithm namei);
2) if file does not exist or not permitted access
return error;
allocate the table entry for inode , initialize count,offset;
allocate user file descriptor entry , set pointer to
file table entry;
-
8/3/2019 System Call for File System
6/35
4/27/12
Example:
fd1=open(/etc/passwd, O_RDONLY);
fd2=open(local, O_RDWR);
fd3=open(/etc/passwd, O_WRONLY);
-
8/3/2019 System Call for File System
7/35
4/27/12
01
2
3
4
5
6
7
User filedescriptor table
::
count1read
::
count
1 rd-wr
::
count1
write
Filetable
Count : 2/etc/passwd
Count:1Local
Inodetable
-
8/3/2019 System Call for File System
8/35
4/27/12
Example
process 1
fd1=open(/etc/passwd,O_RDONLY);
fd2=open(local, O_RDWR); fd3=open(/etc/passwd,
O_WRONLY);
Process 2 fd1=open(/etc/passwd,
O_RDONLY);
fd2=open(private, O_RDONLY);
-
8/3/2019 System Call for File System
9/35
4/27/12
0
1
2
3
4
5
:
:
User file descriptortable
::
count:1
read
::
count:1
rd-write
::
count:1
read
count:1write
Filetable
Count : 3/etc/passwd
Count:1Local
Count:1private
Inodetable
0
1
2
3
4
:
:
:
-
8/3/2019 System Call for File System
10/35
4/27/12
Read Syntax :
Number=read( fd , buffer, count)
fd : the file descriptor return by open
Buffer : address of a data structure in the userprocess that will contain the read data onsuccessful completion of the call
Count: number of bytes the user wants to read
Number : number of bytes actually read
-
8/3/2019 System Call for File System
11/35
4/27/12
The kernel gets the file table entry that corresponds to the userfile descriptor
It now sets the I/O parameters in the u area.
Mode indicates read or writeCount count of bytes to read or writeOffset byte offset in file
Address target address to copy data, inuser or kernel memoryFlag indicates if address is in user or
kernel memory
figure 5.6 I/O parameters saved in U area
-
8/3/2019 System Call for File System
12/35
4/27/12
After the kernel sets the I/O parameters in the uarea;
It follows the pointer from the file table entry tothe inode ,looking the inode before it reads the file;
the algorithm now goes into loop until the read issatisfied;
The kernel converts the file byte offset into a blocknumber, using algorithm bmap
And it notes the byte offset in the block where theI/O should begin and how many bytes in the block
it should read.
-
8/3/2019 System Call for File System
13/35
4/27/12
Algorithm read
Input : user file descriptor
address of buffer in user processnumber of bytes to read
Output : count of bytes into user space
1) get file table entry from user file descriptor; 2) check file accessibility;
3) set parameters in u area for user address ,bytecount , I/O to user;
4) get inode from file table;
5) lock inode;
6) set byte offset in u area from file table offset;
-
8/3/2019 System Call for File System
14/35
4/27/12
7) while could not satisfied
convert file offset to disk block (algorithmbmap);
calculate offset into block , number of bytes toread;
8) if number bytes to read is 0
/*trying to read end of file */
break;
9) read block (algorithm breada if with read
ahead , algorithm bread otherwise); 10) copy data from system buffer to user address;
11) update u area fields for file offset ,read count ,address to write into user space;
Release buffer;
-
8/3/2019 System Call for File System
15/35
4/27/12
Unlock inode
Update file table offset for next read;
return (total number of bytes read);
-
8/3/2019 System Call for File System
16/35
4/27/12
Write Syntax :
Number=write( fd , buffer , count);
fd : the file descriptor return by open
Buffer : address of a data structure in the userprocess that will contain the read data on
successful completion of the call Count: number of bytes the user wants to read
Number : number of bytes actually read
-
8/3/2019 System Call for File System
17/35
4/27/12
The algorithm for writing a regular file is similar tothat for reading a regular file.
However , if the file dose not contain a block thatcorresponds to the byte offset to be written, thekernel allocates a new block using algorithm alloc
And assigns the block number to the correctposition in the inode s table of contents.
if the byte offset is that of an indirect block , thekernel may have to allocate several blocks for use
as indirect blocks and data blocks. The inode is locked for the duration of the write;
Because the kernel may change the inode whenallocating new blocks;
-
8/3/2019 System Call for File System
18/35
4/27/12
Allowing other processes to access to the file could
corrupt the inode ,if several processes allocateblocks simultaneously for the same byte offsets;
When the write is complete, the kernel updatesthe file size entry in the inode if the file has grownlarger;
-
8/3/2019 System Call for File System
19/35
-
8/3/2019 System Call for File System
20/35
4/27/12
Process 1
fd=open(/home/stu/p.c , O_WRONLY);
Write(fd,hello,5);
Process 2
fd=open(/home/stu/p.c, O_RDONLY);
read(fd,buf,5);
-
8/3/2019 System Call for File System
21/35
4/27/12
Adjusting the position of file I/O LSEEK
The ordinary use of read and write system callsprovide sequential access to a file ,but processes
can use the lseek system call to position the I/Oand allow random access to a file .
Syntax :
position = lseek (fd , offset,reference);
fd : file descriptor identifying the file
offset : a byte offset reference : indicate whether offset should be
considered from the file , from the currentposition of the read/write offset , or
from end of file.
-
8/3/2019 System Call for File System
22/35
4/27/12
The lseek system call has nothing to do with theseek operation that positions a disk arm over aparticular disk sector;
To implement lseek , the kernel simply adjusts theoffset value in the file table;
Subsequent read or write system calls use the filetable as their starting byte offset;
-
8/3/2019 System Call for File System
23/35
4/27/12
Close A process closes an open file when it no longer
wants to access it; syntax:
Close(fd);
fd : file descriptor for open file
The kernel does the close operation bymanipulating the file descriptor and thecorresponding file table and inode table entries.
-
8/3/2019 System Call for File System
24/35
4/27/12
If the reference count of the file table entry isgreater than 1 because of dup or fork systemcalls , then other user file descriptors reference the
file table entries; The kernel decrement count and close completes;
User file descriptor File Inode
-
8/3/2019 System Call for File System
25/35
4/27/12
0
1
2
3
4
5
:
:
User file descriptortable
::
count:1
read
::
count:1
rd-write
::
count:0
count:1write
Filetable
Count : 2/etc/passwd
Count:1Local
Count:0private
Inodetable
0
1
2
NULL
NULL
:
:
:
-
8/3/2019 System Call for File System
26/35
4/27/12
FILE CREATION The open system call gives a process access to an
existing file, but the creat system call creates anew file in the system.
Syntax :
fd=creat(pathname , modes);
Pathname: file name modes: file permission
File descriptor: open system call returns an integercalled user file descriptor
-
8/3/2019 System Call for File System
27/35
4/27/12
If no such file previously existed , the kernelcreates a new file with the specified name andpermission mode;
If the file already existed , the kernel truncates thefile (releases all existing data blocks and sets thefile size to 0);
-
8/3/2019 System Call for File System
28/35
4/27/12
Algorithm Creat
Input : file name
permission settings
Output : file descriptor
1) get inode for file name ( algorithm namei);
2) if file already exists
if not permitted access
release inode(algorithm iput);
return(error);
3) else
assign free inode from file system(algorithm
ialloc)
-
8/3/2019 System Call for File System
29/35
4/27/12
4) allocate file table entry for inode , initializedcount;
5) if file did exist at time of create
free all file blocks (algorithm free)
unlock(inode);
return(user file descriptor);
-
8/3/2019 System Call for File System
30/35
4/27/12
CREATION OF SPECIAL FILES
The system call mknod creates special file in thesystem , including :
named pipes;
Device files;
Directories;
It similar to creat in that the kernel allocates an
inode for the file.
-
8/3/2019 System Call for File System
31/35
4/27/12
Algorithm makenew node
Inputs : node(filename)
file type
permissions
-
8/3/2019 System Call for File System
32/35
4/27/12
Assign free inode from file system for new node( ialloc );
Create new directory entry in parent directory;include new inode name and newly assignedinode number;
Release parent directory inode number(iput);
3)If (new node is block or char special file)
write major , minor numbers into inodestructure;
release new node inode(iput);
-
8/3/2019 System Call for File System
33/35
4/27/12
-
8/3/2019 System Call for File System
34/35
4/27/12
-
8/3/2019 System Call for File System
35/35
4/27/12