18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/fs/ext4/truncate.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Common inline functions needed for truncate support
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * Truncate blocks that were not used by write. We have to truncate the
108c2ecf20Sopenharmony_ci * pagecache as well so that corresponding buffers get properly unmapped.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_cistatic inline void ext4_truncate_failed_write(struct inode *inode)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	/*
158c2ecf20Sopenharmony_ci	 * We don't need to call ext4_break_layouts() because the blocks we
168c2ecf20Sopenharmony_ci	 * are truncating were never visible to userspace.
178c2ecf20Sopenharmony_ci	 */
188c2ecf20Sopenharmony_ci	down_write(&EXT4_I(inode)->i_mmap_sem);
198c2ecf20Sopenharmony_ci	truncate_inode_pages(inode->i_mapping, inode->i_size);
208c2ecf20Sopenharmony_ci	ext4_truncate(inode);
218c2ecf20Sopenharmony_ci	up_write(&EXT4_I(inode)->i_mmap_sem);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * Work out how many blocks we need to proceed with the next chunk of a
268c2ecf20Sopenharmony_ci * truncate transaction.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistatic inline unsigned long ext4_blocks_for_truncate(struct inode *inode)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	ext4_lblk_t needed;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	/* Give ourselves just enough room to cope with inodes in which
358c2ecf20Sopenharmony_ci	 * i_blocks is corrupt: we've seen disk corruptions in the past
368c2ecf20Sopenharmony_ci	 * which resulted in random data in an inode which looked enough
378c2ecf20Sopenharmony_ci	 * like a regular file for ext4 to try to delete it.  Things
388c2ecf20Sopenharmony_ci	 * will go a bit crazy if that happens, but at least we should
398c2ecf20Sopenharmony_ci	 * try not to panic the whole kernel. */
408c2ecf20Sopenharmony_ci	if (needed < 2)
418c2ecf20Sopenharmony_ci		needed = 2;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* But we need to bound the transaction so we don't overflow the
448c2ecf20Sopenharmony_ci	 * journal. */
458c2ecf20Sopenharmony_ci	if (needed > EXT4_MAX_TRANS_DATA)
468c2ecf20Sopenharmony_ci		needed = EXT4_MAX_TRANS_DATA;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
51