@@ -2894,8 +2894,8 @@ static void ext4_invalidatepage(struct page *page, unsigned long offset)
28942894 block_invalidatepage (page , offset );
28952895}
28962896
2897- static void ext4_journalled_invalidatepage (struct page * page ,
2898- unsigned long offset )
2897+ static int __ext4_journalled_invalidatepage (struct page * page ,
2898+ unsigned long offset )
28992899{
29002900 journal_t * journal = EXT4_JOURNAL (page -> mapping -> host );
29012901
@@ -2907,7 +2907,14 @@ static void ext4_journalled_invalidatepage(struct page *page,
29072907 if (offset == 0 )
29082908 ClearPageChecked (page );
29092909
2910- jbd2_journal_invalidatepage (journal , page , offset );
2910+ return jbd2_journal_invalidatepage (journal , page , offset );
2911+ }
2912+
2913+ /* Wrapper for aops... */
2914+ static void ext4_journalled_invalidatepage (struct page * page ,
2915+ unsigned long offset )
2916+ {
2917+ WARN_ON (__ext4_journalled_invalidatepage (page , offset ) < 0 );
29112918}
29122919
29132920static int ext4_releasepage (struct page * page , gfp_t wait )
@@ -4313,6 +4320,47 @@ int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
43134320 return err ;
43144321}
43154322
4323+ /*
4324+ * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4325+ * buffers that are attached to a page stradding i_size and are undergoing
4326+ * commit. In that case we have to wait for commit to finish and try again.
4327+ */
4328+ static void ext4_wait_for_tail_page_commit (struct inode * inode )
4329+ {
4330+ struct page * page ;
4331+ unsigned offset ;
4332+ journal_t * journal = EXT4_SB (inode -> i_sb )-> s_journal ;
4333+ tid_t commit_tid = 0 ;
4334+ int ret ;
4335+
4336+ offset = inode -> i_size & (PAGE_CACHE_SIZE - 1 );
4337+ /*
4338+ * All buffers in the last page remain valid? Then there's nothing to
4339+ * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4340+ * blocksize case
4341+ */
4342+ if (offset > PAGE_CACHE_SIZE - (1 << inode -> i_blkbits ))
4343+ return ;
4344+ while (1 ) {
4345+ page = find_lock_page (inode -> i_mapping ,
4346+ inode -> i_size >> PAGE_CACHE_SHIFT );
4347+ if (!page )
4348+ return ;
4349+ ret = __ext4_journalled_invalidatepage (page , offset );
4350+ unlock_page (page );
4351+ page_cache_release (page );
4352+ if (ret != - EBUSY )
4353+ return ;
4354+ commit_tid = 0 ;
4355+ read_lock (& journal -> j_state_lock );
4356+ if (journal -> j_committing_transaction )
4357+ commit_tid = journal -> j_committing_transaction -> t_tid ;
4358+ read_unlock (& journal -> j_state_lock );
4359+ if (commit_tid )
4360+ jbd2_log_wait_commit (journal , commit_tid );
4361+ }
4362+ }
4363+
43164364/*
43174365 * ext4_setattr()
43184366 *
@@ -4426,16 +4474,28 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
44264474 }
44274475
44284476 if (attr -> ia_valid & ATTR_SIZE ) {
4429- if (attr -> ia_size != i_size_read (inode )) {
4430- truncate_setsize (inode , attr -> ia_size );
4431- /* Inode size will be reduced, wait for dio in flight.
4432- * Temporarily disable dioread_nolock to prevent
4433- * livelock. */
4477+ if (attr -> ia_size != inode -> i_size ) {
4478+ loff_t oldsize = inode -> i_size ;
4479+
4480+ i_size_write (inode , attr -> ia_size );
4481+ /*
4482+ * Blocks are going to be removed from the inode. Wait
4483+ * for dio in flight. Temporarily disable
4484+ * dioread_nolock to prevent livelock.
4485+ */
44344486 if (orphan ) {
4435- ext4_inode_block_unlocked_dio (inode );
4436- inode_dio_wait (inode );
4437- ext4_inode_resume_unlocked_dio (inode );
4487+ if (!ext4_should_journal_data (inode )) {
4488+ ext4_inode_block_unlocked_dio (inode );
4489+ inode_dio_wait (inode );
4490+ ext4_inode_resume_unlocked_dio (inode );
4491+ } else
4492+ ext4_wait_for_tail_page_commit (inode );
44384493 }
4494+ /*
4495+ * Truncate pagecache after we've waited for commit
4496+ * in data=journal mode to make pages freeable.
4497+ */
4498+ truncate_pagecache (inode , oldsize , inode -> i_size );
44394499 }
44404500 ext4_truncate (inode );
44414501 }
0 commit comments