Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/ext4/truncate.h
0004  *
0005  * Common inline functions needed for truncate support
0006  */
0007 
0008 /*
0009  * Truncate blocks that were not used by write. We have to truncate the
0010  * pagecache as well so that corresponding buffers get properly unmapped.
0011  */
0012 static inline void ext4_truncate_failed_write(struct inode *inode)
0013 {
0014     struct address_space *mapping = inode->i_mapping;
0015 
0016     /*
0017      * We don't need to call ext4_break_layouts() because the blocks we
0018      * are truncating were never visible to userspace.
0019      */
0020     filemap_invalidate_lock(mapping);
0021     truncate_inode_pages(mapping, inode->i_size);
0022     ext4_truncate(inode);
0023     filemap_invalidate_unlock(mapping);
0024 }
0025 
0026 /*
0027  * Work out how many blocks we need to proceed with the next chunk of a
0028  * truncate transaction.
0029  */
0030 static inline unsigned long ext4_blocks_for_truncate(struct inode *inode)
0031 {
0032     ext4_lblk_t needed;
0033 
0034     needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
0035 
0036     /* Give ourselves just enough room to cope with inodes in which
0037      * i_blocks is corrupt: we've seen disk corruptions in the past
0038      * which resulted in random data in an inode which looked enough
0039      * like a regular file for ext4 to try to delete it.  Things
0040      * will go a bit crazy if that happens, but at least we should
0041      * try not to panic the whole kernel. */
0042     if (needed < 2)
0043         needed = 2;
0044 
0045     /* But we need to bound the transaction so we don't overflow the
0046      * journal. */
0047     if (needed > EXT4_MAX_TRANS_DATA)
0048         needed = EXT4_MAX_TRANS_DATA;
0049 
0050     return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
0051 }
0052