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 */ ext4_truncate_failed_write(struct inode * inode)129f125d64STheodore Ts'ostatic inline void ext4_truncate_failed_write(struct inode *inode) 139f125d64STheodore Ts'o { 14*d4f5258eSJan Kara struct address_space *mapping = inode->i_mapping; 15*d4f5258eSJan Kara 16430657b6SRoss Zwisler /* 17430657b6SRoss Zwisler * We don't need to call ext4_break_layouts() because the blocks we 18430657b6SRoss Zwisler * are truncating were never visible to userspace. 19430657b6SRoss Zwisler */ 20*d4f5258eSJan Kara filemap_invalidate_lock(mapping); 21*d4f5258eSJan Kara truncate_inode_pages(mapping, inode->i_size); 229f125d64STheodore Ts'o ext4_truncate(inode); 23*d4f5258eSJan Kara filemap_invalidate_unlock(mapping); 249f125d64STheodore Ts'o } 259f125d64STheodore Ts'o 269f125d64STheodore Ts'o /* 279f125d64STheodore Ts'o * Work out how many blocks we need to proceed with the next chunk of a 289f125d64STheodore Ts'o * truncate transaction. 299f125d64STheodore Ts'o */ ext4_blocks_for_truncate(struct inode * inode)309f125d64STheodore Ts'ostatic inline unsigned long ext4_blocks_for_truncate(struct inode *inode) 319f125d64STheodore Ts'o { 329f125d64STheodore Ts'o ext4_lblk_t needed; 339f125d64STheodore Ts'o 349f125d64STheodore Ts'o needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 359f125d64STheodore Ts'o 369f125d64STheodore Ts'o /* Give ourselves just enough room to cope with inodes in which 379f125d64STheodore Ts'o * i_blocks is corrupt: we've seen disk corruptions in the past 389f125d64STheodore Ts'o * which resulted in random data in an inode which looked enough 399f125d64STheodore Ts'o * like a regular file for ext4 to try to delete it. Things 409f125d64STheodore Ts'o * will go a bit crazy if that happens, but at least we should 419f125d64STheodore Ts'o * try not to panic the whole kernel. */ 429f125d64STheodore Ts'o if (needed < 2) 439f125d64STheodore Ts'o needed = 2; 449f125d64STheodore Ts'o 459f125d64STheodore Ts'o /* But we need to bound the transaction so we don't overflow the 469f125d64STheodore Ts'o * journal. */ 479f125d64STheodore Ts'o if (needed > EXT4_MAX_TRANS_DATA) 489f125d64STheodore Ts'o needed = EXT4_MAX_TRANS_DATA; 499f125d64STheodore Ts'o 509f125d64STheodore Ts'o return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 519f125d64STheodore Ts'o } 529f125d64STheodore Ts'o 53