Delete System Call



Arguments: Filename (String)

Return Value:

0 Success/File does not exist
-1 Permission denied
-2 File is open

Description : The Delete operation takes as input a filename and deletes it. It returns with an error if any instance of the file is open in the system or if the file is not a DATA file. Delete command fails also if the file to be deleted does not belong to the current user and it has exclusive permissions. Otherwise, it deletes the root entry for the file name, invalidates the Inode Table entry for the file, releases the disk blocks allocated to the file and returns 0.



Control flow diagram for Delete system call


Algorithm :

Set the MODE_FLAG in the process table entry to 4, 
indicating that the process is in the delete system call.

//Switch to Kernel Stack - See Kernel Stack Management during System Calls. 
Save the value of SP to the USER SP field in the Process Table entry of the process.
Set the value of SP to the beginning of User Area Page.

Find the index of the file in the Inode Table.
            
If file is not present in the Inode Table, return 0. 

If the file is not a DATA file, return -1.

If the exclusive permission is set
	if the current user is not root and the current user does not own the file
		return -1. 

Acquire a lock on the file by calling the acquire_inode() function in the Resource Manager module.

Check if the the file open count is -1 in the  File Status Table . If not, release the lock and return -2.    
/* File is open, cannot be deleted */
 
For each disk block allocated to the file, do { 	/* Check Inode Table */
	If the disk block is loaded into a buffer, and the DIRTY BIT is set, reset the dirty bit. 
	/* Check the Buffer Table */ 

	Call the release_block() function in the Memory Manager module to free the disk block.        
}

Invalidate (set to -1) the Inode Table of the file.

Update the Root file by invalidating the entry for the file.

Release the lock on the file by calling the release_inode() function in the Resource Manager module.

Switch back to the user stack by reseting USER SP from the process table.
Set the MODE_FLAG in the process table entry of the parent process to 0.

Return from system call with 0.    /* indicating success */

Note:  At each point of return from the system call, remember to reset the MODE FLAG and switch back to the user stack.
	     

Questions

1. Why are we not updating the Open File Table when a file is being deleted?

2. Why can't an open file be deleted?

3. Why is the dirty bit in the buffer table cleared before the file is deleted?

4. Does disk blocks get freed anywhere else other than the delete system call?