xfs: fix failed write handling
RFC PATCH 1/1 xfs: fix failed write handling
Since the move to the new truncate sequence we call xfs_setattr to truncate down excessively instanciated blocks. As shown by the testcase in kernel.org BZ #22452 that doesn't work too well. Due to the confusion of the internal inode size, and the VFS inode i_size it zeroes data that it shouldn't.
But full blown truncate seems like overkill here. We only instanciate delayed allocations in the write path, and given that we never released the iolock we can't have converted them to real allocations yet either.
The only nasty case is pre-existing preallocation which we need to skip. The patch below does that by borrowing code from xfs_aops_discard_page. It does pass xfstests for 4k block filesystems and fixes the original bug. I'm not quite sure if we could hit a corner case with smaller block sizes when parts of a page are preallocated and some not. That could be handled by looping around bmapi as long as we find extents for our range. The path could probably also be refactored to share code with xfs_aops_discard_page. And we probably need the ilock just as in that path, but I only got to that when almost through xfstests, and the day is over for me today, so let's just get the patch out for now.
fs/xfs/linux-2.6/xfs_aops.c +68 -5
@@ -1505,11 +1505,74 @@ xfs_vm_write_failed(struct inode *inode = mapping->host;if (to > inode->i_size) { -struct iattr ia = { - .ia_valid =ATTR_SIZE |ATTR_FORCE , - .ia_size = inode->i_size, - }; -xfs_setattr (XFS_I (inode), &ia,XFS_ATTR_NOLOCK ); +struct xfs_inode *ip =XFS_I (inode); +int done; +xfs_bmbt_irec_t imap; +int nimaps =1 ; +xfs_fileoff_t start_fsb; +xfs_fileoff_t end_fsb; +int error; +xfs_fsblock_t firstblock; +xfs_bmap_free_t flist; + +truncate_pagecache (inode, to, inode->i_size); + +/* +* Check if there are any blocks that are outside of i_size +* that need to be trimmed back. +*/ + start_fsb =XFS_B_TO_FSB (ip->i_mount, inode->i_size) +1 ; + end_fsb =XFS_B_TO_FSB (ip->i_mount, to); +if (end_fsb <= start_fsb) +return ; + +/* +* Map the range first and check that it is a delalloc extent +* before trying to unmap the range. Otherwise we will be +* trying to remove a real extent (which requires a +* transaction) or a hole, which is probably a bad idea... +*/ + error =xfs_bmapi (NULL , ip, start_fsb, end_fsb - start_fsb, +XFS_BMAPI_ENTIRE ,NULL ,0 , &imap, + &nimaps,NULL ); + +if (error) { +/* something screwed, just bail */ +if (!XFS_FORCED_SHUTDOWN (ip->i_mount)) { +xfs_fs_cmn_err (CE_ALERT , ip->i_mount, +"page discard failed delalloc mapping lookup." ); + } +return ; + } +if (!nimaps) +return ; + +if (imap.br_startblock !=DELAYSTARTBLOCK ) +return ; +WARN_ON (imap.br_blockcount ==0 ); + + +/* +* Note: while we initialise the firstblock/flist pair, they +* should never be used because blocks should never be +* allocated or freed for a delalloc extent and hence we need +* don't cancel or finish them after the xfs_bunmapi() call. +*/ +xfs_bmap_init (&flist, &firstblock); + error =xfs_bunmapi (NULL , ip, start_fsb, end_fsb - start_fsb, +0 ,1 , &firstblock, &flist, &done); + +ASSERT (!flist.xbf_count && !flist.xbf_first); +if (error) { +/* something screwed, just bail */ +if (!XFS_FORCED_SHUTDOWN (ip->i_mount)) { +xfs_fs_cmn_err (CE_ALERT , ip->i_mount, +"page discard unable to remove delalloc mapping." ); + } + }else if (!done) { +xfs_fs_cmn_err (CE_ALERT , ip->i_mount, +"page discard not done." ); + } } }
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 10 lines · Christoph Hellwig
Since the move to the new truncate sequence we call
xfs_setattrto truncate down excessively instanciated blocks. As shown by the testcase in kernel.org BZ #22452 that doesn't work too well. Due to the confusion of the internal inode size, and the VFS inodei_sizeit zeroes data that it shouldn't.But full blown truncate seems like overkill here. We only instanciate delayed allocations in the write path, and given that we never released the iolock we can't have converted them to real allocations yet either.
Quoted 5 lines
The only nasty case is pre-existing preallocation which we need to skip. The patch below does that by borrowing code from
xfs_aops_discard_page. It does pass xfstests for 4k block filesystems and fixes the original bug. I'm not quite sure if we could hit a corner case with smaller block sizes when parts of a page are preallocated and some not.
Seems likely - preallocated block past EOF are not unusual.
Quoted 7 lines
That
could be handled by looping around bmapi as long as we find extents for our range. The path could probably also be refactored to share code withxfs_aops_discard_page. And we probably need the ilock just as in that path, but I only got to that when almost through xfstests, and the day is over for me today, so let's just get the patch out for now.
Yes, definitely need the ilock - that's the only lock that provides protection for the extent tree.
It looks to me that we need a general "discard delalloc blocks from range" function - I'll write one (basically the guts of xfs_aops_discard_page) and convert xfs_aops_discard_page() and this code to use it....
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 27 lines · Dave Chinner
Quoted 10 lines · Christoph Hellwig
Since the move to the new truncate sequence we call
xfs_setattrto truncate down excessively instanciated blocks. As shown by the testcase in kernel.org BZ #22452 that doesn't work too well. Due to the confusion of the internal inode size, and the VFS inodei_sizeit zeroes data that it shouldn't.But full blown truncate seems like overkill here. We only instanciate delayed allocations in the write path, and given that we never released the iolock we can't have converted them to real allocations yet either.
Quoted 5 lines
The only nasty case is pre-existing preallocation which we need to skip. The patch below does that by borrowing code from
xfs_aops_discard_page. It does pass xfstests for 4k block filesystems and fixes the original bug. I'm not quite sure if we could hit a corner case with smaller block sizes when parts of a page are preallocated and some not.Seems likely - preallocated block past EOF are not unusual.
Quoted 7 lines
That
could be handled by looping around bmapi as long as we find extents for our range. The path could probably also be refactored to share code withxfs_aops_discard_page. And we probably need the ilock just as in that path, but I only got to that when almost through xfstests, and the day is over for me today, so let's just get the patch out for now.
Quoted 7 lines
Yes, definitely need the ilock - that's the only lock that provides protection for the extent tree.
It looks to me that we need a general "discard delalloc blocks from range" function - I'll write one (basically the guts of
xfs_aops_discard_page) and convertxfs_aops_discard_page()and this code to use it....
The patch below does this. It's passed xfstests on a couple of different VMs and the provided test case. xfstests is curently running on 512 byte and 1k block size filesystems to exercise sub-page block size failure cases.
Cheers,
Dave.
Signature · 32 lines
Dave Chinner [email protected] xfs: fix failed write truncation handling. From: Dave Chinner <[email protected]> Since the move to the new truncate sequence we call xfs_setattr to truncate down excessively instanciated blocks. As shown by the testcase in kernel.org BZ #22452 that doesn't work too well. Due to the confusion of the internal inode size, and the VFS inode i_size it zeroes data that it shouldn't. But full blown truncate seems like overkill here. We only instanciate delayed allocations in the write path, and given that we never released the iolock we can't have converted them to real allocations yet either. The only nasty case is pre-existing preallocation which we need to skip. We already do this for page discard during writeback, so make the delayed allocation block punching a generic function and call it from the failed write path as well as xfs_aops_discard_page. The callers are responsible for ensuring that partial blocks are not truncated away, and that they hold the ilock. Based on a fix originally from Christoph Hellwig. Signed-off-by: Dave Chinner <[email protected]> --- fs/xfs/linux-2.6/xfs_aops.c | 91 ++++++++++++++++++------------------------ fs/xfs/xfs_bmap.c | 79 +++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_bmap.h | 5 ++ 3 files changed, 123 insertions(+), 52 deletions(-)
fs/xfs/linux-2.6/xfs_aops.c +39 -52
@@ -949,58 +949,12 @@ xfs_aops_discard_page(xfs_ilock (ip,XFS_ILOCK_EXCL ); bh = head =page_buffers (page); do { - int done; - xfs_fileoff_t offset_fsb; - xfs_bmbt_irec_t imap; - int nimaps = 1; int error; - xfs_fsblock_t firstblock; - xfs_bmap_free_t flist; if (!buffer_delay(bh)) goto next_buffer; - offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); - - /* - * Map the range first and check that it is a delalloc extent - * before trying to unmap the range. Otherwise we will be - * trying to remove a real extent (which requires a - * transaction) or a hole, which is probably a bad idea... - */ - error = xfs_bmapi(NULL, ip, offset_fsb, 1, - XFS_BMAPI_ENTIRE, NULL, 0, &imap, - &nimaps, NULL); - - if (error) { - /* something screwed, just bail */ - if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { - xfs_fs_cmn_err(CE_ALERT, ip->i_mount, - "page discard failed delalloc mapping lookup."); - } - break; - } - if (!nimaps) { - /* nothing there */ - goto next_buffer; - } - if (imap.br_startblock != DELAYSTARTBLOCK) { - /* been converted, ignore */ - goto next_buffer; - } - WARN_ON(imap.br_blockcount == 0); - - /* - * Note: while we initialise the firstblock/flist pair, they - * should never be used because blocks should never be - * allocated or freed for a delalloc extent and hence we need - * don't cancel or finish them after the xfs_bunmapi() call. - */ - xfs_bmap_init(&flist, &firstblock); - error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock, - &flist, &done); - - ASSERT(!flist.xbf_count && !flist.xbf_first); + error = xfs_bmap_punch_delalloc_range(ip, offset, offset + len); if (error) { /* something screwed, just bail */ if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
@@ -1020,6 +974,7 @@ out_invalidate:return ; } +/* * Write out a dirty page. *
@@ -1504,11 +1459,43 @@ xfs_vm_write_failed(struct inode *inode = mapping->host;if (to > inode->i_size) { -struct iattr ia = { - .ia_valid =ATTR_SIZE |ATTR_FORCE , - .ia_size = inode->i_size, - }; -xfs_setattr (XFS_I (inode), &ia,XFS_ATTR_NOLOCK ); +/* +* punch out the delalloc blocks we have already allocated. We +* don't call xfs_setattr() to do this as we may be in the +* middle of a multi-iovec write and so the vfs inode->i_size +* will not match the xfs ip->i_size and so it will zero too +* much. Hence we jus truncate the page cache to zero what is +* necessary and punch the delalloc blocks directly. +*/ +struct xfs_inode *ip =XFS_I (inode); +xfs_fileoff_t start_fsb; +xfs_fileoff_t end_fsb; +int error; + +truncate_pagecache (inode, to, inode->i_size); + +/* +* Check if there are any blocks that are outside of i_size +* that need to be trimmed back. +*/ + start_fsb =XFS_B_TO_FSB (ip->i_mount, inode->i_size) +1 ; + end_fsb =XFS_B_TO_FSB (ip->i_mount, to); +if (end_fsb <= start_fsb) +return ; + +xfs_ilock (ip,XFS_ILOCK_EXCL ); + error =xfs_bmap_punch_delalloc_range (ip, +XFS_FSB_TO_B (ip->i_mount, start_fsb), +XFS_FSB_TO_B (ip->i_mount, end_fsb)); +if (error) { +/* something screwed, just bail */ +if (!XFS_FORCED_SHUTDOWN (ip->i_mount)) { +xfs_fs_cmn_err (CE_ALERT , ip->i_mount, +"xfs_vm_write_failed: unable to clean up ino %lld" , + ip->i_ino); + } + } +xfs_iunlock (ip,XFS_ILOCK_EXCL ); } }
fs/xfs/xfs_bmap.c +79 -0
82 lines not on this page · raw message
fs/xfs/xfs_bmap.h +5 -0
@@ -394,6 +394,11 @@ xfs_bmap_count_blocks(int whichfork, int *count); +int +xfs_bmap_punch_delalloc_range ( +struct xfs_inode *ip, +xfs_fileoff_t start, +ssize_t end);#endif /* __KERNEL__ */ #endif /* __XFS_BMAP_H__ */
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
fs/xfs/linux-2.6/xfs_aops.c out_invalidate: · line 980
Quoted 8 lines
fs/xfs/linux-2.6/xfs_aops.c +1
-0@@ -1020,6 +974,7 @@ out_invalidate:return ; } +/* * Write out a dirty page. *
Spurious whitespace change.
fs/xfs/xfs_bmap.c xfs_bmap_disk_count_leaves( · line 6091
Quoted 5 lines
fs/xfs/xfs_bmap.c +5
-0+int +xfs_bmap_punch_delalloc_range ( +struct xfs_inode *ip, +xfs_fileoff_t start, +ssize_t end)
Making this helper work in terms of FSBs seem a lot cleaner than making it work in terms of bytes. Both callers already deal with FSBs, as does xfs_bunmapi.
Otherwise looks good to me.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Any plans to update it, or should I look into the small changes?
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs