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