1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0 29f125d64STheodore Ts'o /* 39f125d64STheodore Ts'o * linux/fs/ext4/truncate.h 49f125d64STheodore Ts'o * 59f125d64STheodore Ts'o * Common inline functions needed for truncate support 69f125d64STheodore Ts'o */ 79f125d64STheodore Ts'o 89f125d64STheodore Ts'o /* 99f125d64STheodore Ts'o * Truncate blocks that were not used by write. We have to truncate the 109f125d64STheodore Ts'o * pagecache as well so that corresponding buffers get properly unmapped. 119f125d64STheodore Ts'o */ 129f125d64STheodore Ts'o static inline void ext4_truncate_failed_write(struct inode *inode) 139f125d64STheodore Ts'o { 14*430657b6SRoss Zwisler /* 15*430657b6SRoss Zwisler * We don't need to call ext4_break_layouts() because the blocks we 16*430657b6SRoss Zwisler * are truncating were never visible to userspace. 17*430657b6SRoss Zwisler */ 18ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 199f125d64STheodore Ts'o truncate_inode_pages(inode->i_mapping, inode->i_size); 209f125d64STheodore Ts'o ext4_truncate(inode); 21ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 229f125d64STheodore Ts'o } 239f125d64STheodore Ts'o 249f125d64STheodore Ts'o /* 259f125d64STheodore Ts'o * Work out how many blocks we need to proceed with the next chunk of a 269f125d64STheodore Ts'o * truncate transaction. 279f125d64STheodore Ts'o */ 289f125d64STheodore Ts'o static inline unsigned long ext4_blocks_for_truncate(struct inode *inode) 299f125d64STheodore Ts'o { 309f125d64STheodore Ts'o ext4_lblk_t needed; 319f125d64STheodore Ts'o 329f125d64STheodore Ts'o needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 339f125d64STheodore Ts'o 349f125d64STheodore Ts'o /* Give ourselves just enough room to cope with inodes in which 359f125d64STheodore Ts'o * i_blocks is corrupt: we've seen disk corruptions in the past 369f125d64STheodore Ts'o * which resulted in random data in an inode which looked enough 379f125d64STheodore Ts'o * like a regular file for ext4 to try to delete it. Things 389f125d64STheodore Ts'o * will go a bit crazy if that happens, but at least we should 399f125d64STheodore Ts'o * try not to panic the whole kernel. */ 409f125d64STheodore Ts'o if (needed < 2) 419f125d64STheodore Ts'o needed = 2; 429f125d64STheodore Ts'o 439f125d64STheodore Ts'o /* But we need to bound the transaction so we don't overflow the 449f125d64STheodore Ts'o * journal. */ 459f125d64STheodore Ts'o if (needed > EXT4_MAX_TRANS_DATA) 469f125d64STheodore Ts'o needed = EXT4_MAX_TRANS_DATA; 479f125d64STheodore Ts'o 489f125d64STheodore Ts'o return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 499f125d64STheodore Ts'o } 509f125d64STheodore Ts'o 51