1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * linux/fs/ext4/truncate.h 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Common inline functions needed for truncate support 6*4882a593Smuzhiyun */ 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun /* 9*4882a593Smuzhiyun * Truncate blocks that were not used by write. We have to truncate the 10*4882a593Smuzhiyun * pagecache as well so that corresponding buffers get properly unmapped. 11*4882a593Smuzhiyun */ ext4_truncate_failed_write(struct inode * inode)12*4882a593Smuzhiyunstatic inline void ext4_truncate_failed_write(struct inode *inode) 13*4882a593Smuzhiyun { 14*4882a593Smuzhiyun /* 15*4882a593Smuzhiyun * We don't need to call ext4_break_layouts() because the blocks we 16*4882a593Smuzhiyun * are truncating were never visible to userspace. 17*4882a593Smuzhiyun */ 18*4882a593Smuzhiyun down_write(&EXT4_I(inode)->i_mmap_sem); 19*4882a593Smuzhiyun truncate_inode_pages(inode->i_mapping, inode->i_size); 20*4882a593Smuzhiyun ext4_truncate(inode); 21*4882a593Smuzhiyun up_write(&EXT4_I(inode)->i_mmap_sem); 22*4882a593Smuzhiyun } 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun /* 25*4882a593Smuzhiyun * Work out how many blocks we need to proceed with the next chunk of a 26*4882a593Smuzhiyun * truncate transaction. 27*4882a593Smuzhiyun */ ext4_blocks_for_truncate(struct inode * inode)28*4882a593Smuzhiyunstatic inline unsigned long ext4_blocks_for_truncate(struct inode *inode) 29*4882a593Smuzhiyun { 30*4882a593Smuzhiyun ext4_lblk_t needed; 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun /* Give ourselves just enough room to cope with inodes in which 35*4882a593Smuzhiyun * i_blocks is corrupt: we've seen disk corruptions in the past 36*4882a593Smuzhiyun * which resulted in random data in an inode which looked enough 37*4882a593Smuzhiyun * like a regular file for ext4 to try to delete it. Things 38*4882a593Smuzhiyun * will go a bit crazy if that happens, but at least we should 39*4882a593Smuzhiyun * try not to panic the whole kernel. */ 40*4882a593Smuzhiyun if (needed < 2) 41*4882a593Smuzhiyun needed = 2; 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* But we need to bound the transaction so we don't overflow the 44*4882a593Smuzhiyun * journal. */ 45*4882a593Smuzhiyun if (needed > EXT4_MAX_TRANS_DATA) 46*4882a593Smuzhiyun needed = EXT4_MAX_TRANS_DATA; 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 49*4882a593Smuzhiyun } 50*4882a593Smuzhiyun 51