Home > Article > Operation and Maintenance > Analysis of truncate.c source code of file system in Linux
static void free_ind(int dev,int block)
The function of this function is to release all one-time indirect blocks.
This function first reads an indirect block into bh, and 512 disk block numbers are stored in the bh block.
struct buffer_head * bh; unsigned short * p; int i; if (!block) return; if ((bh=bread(dev,block))) {
Next, the 512 disk block numbers are traversed. If the disk block number is not 0, call free_block (in bitmap.c) to release the disk block. After the traversal is completed, the bh block reference count of an indirect block is decremented by 1. Finally, this one-time indirect block is also released.
for (i=0;i<512;i++,p++) if (*p) free_block(dev,*p); brelse(bh); free_block(dev,block);
static void free_dind(int dev,int block)
The function of this function is torelease all secondary indirect blocks.
This function first verifies the validity of the disk block number.
struct buffer_head * bh; unsigned short * p; int i; if (!block) return;
Then read the secondary indirect block into bh. The bh block stores the disk block numbers of 512 primary indirect blocks.
Next, the disk block numbers of these 512 one-time indirect blocks are traversed. If the disk block number is not 0, call free_ind to release all the blocks of the one-time indirect block. After the traversal is completed, the bh block reference count of the secondary indirect block is decremented by 1. Finally, this secondary indirect block is also released.
if ((bh=bread(dev,block))) { p = (unsigned short *) bh->b_data; for (i=0;i<512;i++,p++) if (*p) free_ind(dev,*p); brelse(bh); } free_block(dev,block);
void truncate(struct m_inode * inode)
The function of this function is to release the disk space occupied by the inode. When the number of file links is 0, the iput function (inode.c) will call this function.
The code initially checks if it is not a regular file or a directory file, then skip it.
int i; if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) return;
Release the directly referenced block.
for (i=0;i<7;i++) if (inode->i_zone[i]) { free_block(inode->i_dev,inode->i_zone[i]); inode->i_zone[i]=0; }
Release primary indirect block and secondary indirect block.
free_ind(inode->i_dev,inode->i_zone[7]); free_dind(inode->i_dev,inode->i_zone[8]);
Set the addresses of the primary indirect block and the secondary indirect block to 0. Set the size of the inode to 0, set the inode to contain dirty data, and finally change the modification time and creation time of the inode to the current time.
inode->i_zone[7] = inode->i_zone[8] = 0; inode->i_size = 0; inode->i_dirt = 1; inode->i_mtime = inode->i_ctime = CURRENT_TIME;
The above is the detailed content of Analysis of truncate.c source code of file system in Linux. For more information, please follow other related articles on the PHP Chinese website!