Home > Operation and Maintenance > Linux Operation and Maintenance > Analysis of truncate.c source code of file system in Linux

Analysis of truncate.c source code of file system in Linux

WBOY
Release: 2023-05-21 18:01:12
forward
1305 people have browsed it

Linux-0.11 File system truncate.c detailed explanation

free_ind

static void free_ind(int dev,int block)
Copy after login

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))) {
Copy after login

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);
Copy after login

free_dind

static void free_dind(int dev,int block)
Copy after login

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;
Copy after login

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);
Copy after login

truncate

void truncate(struct m_inode * inode)
Copy after login

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;
Copy after login

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;
    }
Copy after login

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]);
Copy after login

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;
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template