xfs COW cleanups v3
Subsystems (2)
Hi all,
Changes since v2:
- rebased on the latest iomap-for-next and dropped patches already merged
Changes since v1:
- renumber IOMAP_HOLE to 0 and avoid the reserved 0 value
- fix minor typos and update comments
PATCH 1/12 xfs: also call xfs_file_iomap_end_delalloc for zeroing operations
There is no reason not to punch out stale delalloc blocks for zeroing operations, as they otherwise behave exactly like normal writes.
--- fs/xfs/xfs_iomap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/xfs/xfs_iomap.c +2 -1
@@ -1145,7 +1145,8 @@ xfs_file_iomap_end( unsigned flags, struct iomap *iomap) { -if ((flags &IOMAP_WRITE ) && iomap->type ==IOMAP_DELALLOC ) +if ((flags & (IOMAP_WRITE |IOMAP_ZERO )) && + iomap->type ==IOMAP_DELALLOC )return xfs_file_iomap_end_delalloc (XFS_I (inode), offset, length, written, iomap);return 0 ;
Signature · 1 line
2.20.1
PATCH 2/12 xfs: remove xfs_reflink_dirty_extents
In reply to Christoph HellwigNow that xfs_file_unshare is not completely dumb we can just call it directly without iterating the extent and reflink btrees ourselves.
--- fs/xfs/xfs_reflink.c | 103 +++---------------------------------------- 1 file changed, 5 insertions(+), 98 deletions(-)
fs/xfs/xfs_reflink.c +5 -98
@@ -1381,85 +1381,6 @@ xfs_reflink_remap_prep(return ret; } -/* -* The user wants to preemptively CoW all shared blocks in this file, -* which enables us to turn off the reflink flag. Iterate all -* extents which are not prealloc/delalloc to see which ranges are -* mentioned in the refcount tree, then read those blocks into the -* pagecache, dirty them, fsync them back out, and then we can update -* the inode flag. What happens if we run out of memory? :) -*/ -STATIC int -xfs_reflink_dirty_extents ( -struct xfs_inode *ip, -xfs_fileoff_t fbno, -xfs_filblks_t end, -xfs_off_t isize) -{ -struct xfs_mount *mp = ip->i_mount; -xfs_agnumber_t agno; -xfs_agblock_t agbno; -xfs_extlen_t aglen; -xfs_agblock_t rbno; -xfs_extlen_t rlen; -xfs_off_t fpos; -xfs_off_t flen; -struct xfs_bmbt_irec map[2 ]; -int nmaps; -int error =0 ; - -while (end - fbno >0 ) { - nmaps =1 ; -/* -* Look for extents in the file. Skip holes, delalloc, or -* unwritten extents; they can't be reflinked. -*/ - error =xfs_bmapi_read (ip, fbno, end - fbno, map, &nmaps,0 ); -if (error) - goto out; -if (nmaps ==0 ) -break ; -if (!xfs_bmap_is_real_extent (&map[0 ])) - goto next; - - map[1 ] = map[0 ]; -while (map[1 ].br_blockcount) { - agno =XFS_FSB_TO_AGNO (mp, map[1 ].br_startblock); - agbno =XFS_FSB_TO_AGBNO (mp, map[1 ].br_startblock); - aglen = map[1 ].br_blockcount; - - error =xfs_reflink_find_shared (mp,NULL , agno, agbno, - aglen, &rbno, &rlen, true); -if (error) - goto out; -if (rbno ==NULLAGBLOCK ) -break ; - -/* Dirty the pages */ -xfs_iunlock (ip,XFS_ILOCK_EXCL ); - fpos =XFS_FSB_TO_B (mp, map[1 ].br_startoff + - (rbno - agbno)); - flen =XFS_FSB_TO_B (mp, rlen); -if (fpos + flen > isize) - flen = isize - fpos; - error =iomap_file_unshare (VFS_I (ip), fpos, flen, - &xfs_iomap_ops); -xfs_ilock (ip,XFS_ILOCK_EXCL ); -if (error) - goto out; - - map[1 ].br_blockcount -= (rbno - agbno + rlen); - map[1 ].br_startoff += (rbno - agbno + rlen); - map[1 ].br_startblock += (rbno - agbno + rlen); - } - -next: - fbno = map[0 ].br_startoff + map[0 ].br_blockcount; - } -out: -return error; -} -/* Does this inode need the reflink flag? */ int xfs_reflink_inode_has_shared_extents(
@@ -1596,10 +1517,7 @@ xfs_reflink_unshare( xfs_off_t offset, xfs_off_t len) { -struct xfs_mount *mp = ip->i_mount; -xfs_fileoff_t fbno; -xfs_filblks_t end; -xfs_off_t isize; +struct inode *inode =VFS_I (ip);int error;if (!xfs_is_reflink_inode (ip))
@@ -1607,20 +1525,12 @@ xfs_reflink_unshare(trace_xfs_reflink_unshare (ip, offset, len); -inode_dio_wait (VFS_I (ip)); +inode_dio_wait (inode); -/* Try to CoW the selected ranges */ -xfs_ilock (ip,XFS_ILOCK_EXCL ); - fbno =XFS_B_TO_FSBT (mp, offset); - isize =i_size_read (VFS_I (ip)); - end =XFS_B_TO_FSB (mp, offset + len); - error =xfs_reflink_dirty_extents (ip, fbno, end, isize); + error =iomap_file_unshare (inode, offset, len, &xfs_iomap_ops);if (error) - goto out_unlock; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); - -/* Wait for the IO to finish */ - error =filemap_write_and_wait (VFS_I (ip)->i_mapping); + goto out; + error =filemap_write_and_wait (inode->i_mapping);if (error) goto out;
@@ -1628,11 +1538,8 @@ xfs_reflink_unshare( error =xfs_reflink_try_clear_inode_flag (ip);if (error) goto out; -return 0 ; -out_unlock: -xfs_iunlock (ip,XFS_ILOCK_EXCL ); out:trace_xfs_reflink_unshare_error (ip, error, _RET_IP_);return error;
Signature · 1 line
2.20.1
PATCH 3/12 xfs: pass two imaps to xfs_reflink_allocate_cow
In reply to Christoph Hellwigxfs_reflink_allocate_cow consumes the source data fork imap, and potentially returns the COW fork imap. Split the arguments in two to clear up the calling conventions and to prepare for returning a source iomap from ->iomap_begin.
--- fs/xfs/xfs_iomap.c | 8 ++++---- fs/xfs/xfs_reflink.c | 30 +++++++++++++++--------------- fs/xfs/xfs_reflink.h | 4 ++-- 3 files changed, 21 insertions(+), 21 deletions(-)
fs/xfs/xfs_iomap.c +4 -4
@@ -996,9 +996,8 @@ xfs_file_iomap_begin( goto out_found;/* may drop and re-acquire the ilock */ - cmap = imap; - error =xfs_reflink_allocate_cow (ip, &cmap, &shared, &lockmode, - directio); + error =xfs_reflink_allocate_cow (ip, &imap, &cmap, &shared, + &lockmode, directio);if (error) goto out_unlock;
@@ -1011,7 +1010,8 @@ xfs_file_iomap_begin(* newly allocated address. If the data fork has a hole, copy * the COW fork mapping to avoid allocating to the data fork. */ -if (directio || imap.br_startblock ==HOLESTARTBLOCK ) +if (shared && + (directio || imap.br_startblock ==HOLESTARTBLOCK )) imap = cmap; end_fsb = imap.br_startoff + imap.br_blockcount;
fs/xfs/xfs_reflink.c +15 -15
@@ -308,13 +308,13 @@ static intxfs_find_trim_cow_extent ( struct xfs_inode *ip, struct xfs_bmbt_irec *imap, + struct xfs_bmbt_irec *cmap, bool *shared, bool *found) { xfs_fileoff_t offset_fsb = imap->br_startoff;xfs_filblks_t count_fsb = imap->br_blockcount;struct xfs_iext_cursor icur; -struct xfs_bmbt_irec got; *found = false;
@@ -322,23 +322,22 @@ xfs_find_trim_cow_extent(* If we don't find an overlapping extent, trim the range we need to * allocate to fit the hole we found. */ -if (!xfs_iext_lookup_extent (ip, ip->i_cowfp, offset_fsb, &icur, &got)) - got.br_startoff = offset_fsb + count_fsb; -if (got.br_startoff > offset_fsb) { +if (!xfs_iext_lookup_extent (ip, ip->i_cowfp, offset_fsb, &icur, cmap)) + cmap->br_startoff = offset_fsb + count_fsb; +if (cmap->br_startoff > offset_fsb) {xfs_trim_extent (imap, imap->br_startoff, - got.br_startoff - imap->br_startoff); + cmap->br_startoff - imap->br_startoff);return xfs_inode_need_cow (ip, imap, shared); } *shared = true; -if (isnullstartblock (got.br_startblock)) { -xfs_trim_extent (imap, got.br_startoff, got.br_blockcount); +if (isnullstartblock (cmap->br_startblock)) { +xfs_trim_extent (imap, cmap->br_startoff, cmap->br_blockcount);return 0 ; }/* real extent found - no need to allocate */ -xfs_trim_extent (&got, offset_fsb, count_fsb); - *imap = got; +xfs_trim_extent (cmap, offset_fsb, count_fsb); *found = true;return 0 ; }
@@ -348,6 +347,7 @@ intxfs_reflink_allocate_cow ( struct xfs_inode *ip, struct xfs_bmbt_irec *imap, + struct xfs_bmbt_irec *cmap, bool *shared, uint *lockmode, bool convert_now)
@@ -367,7 +367,7 @@ xfs_reflink_allocate_cow(xfs_ifork_init_cow (ip); } - error =xfs_find_trim_cow_extent (ip, imap, shared, &found); + error =xfs_find_trim_cow_extent (ip, imap, cmap, shared, &found);if (error || !*shared)return error;if (found)
@@ -392,7 +392,7 @@ xfs_reflink_allocate_cow(/* * Check for an overlapping extent again now that we dropped the ilock. */ - error =xfs_find_trim_cow_extent (ip, imap, shared, &found); + error =xfs_find_trim_cow_extent (ip, imap, cmap, shared, &found);if (error || !*shared) goto out_trans_cancel;if (found) {
@@ -411,7 +411,7 @@ xfs_reflink_allocate_cow( nimaps =1 ; error =xfs_bmapi_write (tp, ip, imap->br_startoff, imap->br_blockcount,XFS_BMAPI_COWFORK |XFS_BMAPI_PREALLOC , - resblks, imap, &nimaps); + resblks, cmap, &nimaps);if (error) goto out_unreserve;
@@ -427,15 +427,15 @@ xfs_reflink_allocate_cow(if (nimaps ==0 )return -ENOSPC ; convert: -xfs_trim_extent (imap, offset_fsb, count_fsb); +xfs_trim_extent (cmap, offset_fsb, count_fsb);/* * COW fork extents are supposed to remain unwritten until we're ready * to initiate a disk write. For direct I/O we are going to write the * data and need the conversion, but for buffered writes we're done. */ -if (!convert_now || imap->br_state ==XFS_EXT_NORM ) +if (!convert_now || cmap->br_state ==XFS_EXT_NORM )return 0 ; -trace_xfs_reflink_convert_cow (ip, imap); +trace_xfs_reflink_convert_cow (ip, cmap);return xfs_reflink_convert_cow_locked (ip, offset_fsb, count_fsb); out_unreserve:
fs/xfs/xfs_reflink.h +2 -2
@@ -25,8 +25,8 @@ extern int xfs_reflink_trim_around_shared(struct xfs_inode *ip,bool xfs_inode_need_cow (struct xfs_inode *ip,struct xfs_bmbt_irec *imap,bool *shared); -extern int xfs_reflink_allocate_cow (struct xfs_inode *ip, -struct xfs_bmbt_irec *imap,bool *shared,uint *lockmode, +int xfs_reflink_allocate_cow (struct xfs_inode *ip,struct xfs_bmbt_irec *imap, +struct xfs_bmbt_irec *cmap,bool *shared,uint *lockmode,bool convert_now);extern int xfs_reflink_convert_cow (struct xfs_inode *ip,xfs_off_t offset,xfs_off_t count);
Signature · 1 line
2.20.1
PATCH 4/12 xfs: refactor xfs_file_iomap_begin_delay
In reply to Christoph HellwigRejuggle the return path to prepare for filling out a source iomap.
--- fs/xfs/xfs_iomap.c | 48 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-)
fs/xfs/xfs_iomap.c +24 -24
@@ -539,7 +539,6 @@ xfs_file_iomap_begin_delay(struct xfs_iext_cursor icur, ccur;xfs_fsblock_t prealloc_blocks =0 ;bool eof = false, cow_eof = false, shared = false; -u16 iomap_flags =0 ;int whichfork =XFS_DATA_FORK ;int error =0 ;
@@ -600,8 +599,7 @@ xfs_file_iomap_begin_delay( &ccur, &cmap);if (!cow_eof && cmap.br_startoff <= offset_fsb) {trace_xfs_reflink_cow_found (ip, &cmap); - whichfork =XFS_COW_FORK ; - goto done; + goto found_cow; } }
@@ -615,7 +613,7 @@ xfs_file_iomap_begin_delay( ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , &imap); - goto done; + goto found_imap; }xfs_trim_extent (&imap, offset_fsb, end_fsb - offset_fsb);
@@ -629,7 +627,7 @@ xfs_file_iomap_begin_delay(if (!shared) {trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , &imap); - goto done; + goto found_imap; }/*
@@ -703,35 +701,37 @@ xfs_file_iomap_begin_delay( goto out_unlock; } +if (whichfork ==XFS_COW_FORK ) { +trace_xfs_iomap_alloc (ip, offset, count, whichfork, &cmap); + goto found_cow; + } +/* * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch * them out if the write happens to fail. */ -if (whichfork ==XFS_DATA_FORK ) { - iomap_flags |=IOMAP_F_NEW ; -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &imap); - }else { -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &cmap); - } -done: -if (whichfork ==XFS_COW_FORK ) { -if (imap.br_startoff > offset_fsb) { -xfs_trim_extent (&cmap, offset_fsb, - imap.br_startoff - offset_fsb); - error =xfs_bmbt_to_iomap (ip, iomap, &cmap, -IOMAP_F_SHARED ); - goto out_unlock; - } +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +trace_xfs_iomap_alloc (ip, offset, count, whichfork, &imap); +return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_NEW ); + +found_imap: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +return xfs_bmbt_to_iomap (ip, iomap, &imap,0 ); + +found_cow: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +if (imap.br_startoff <= offset_fsb) {/* ensure we only report blocks we have a reservation for */ xfs_trim_extent (&imap, cmap.br_startoff, cmap.br_blockcount); - shared = true; +return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_SHARED ); } -if (shared) - iomap_flags |=IOMAP_F_SHARED ; - error =xfs_bmbt_to_iomap (ip, iomap, &imap, iomap_flags); +xfs_trim_extent (&cmap, offset_fsb, imap.br_startoff - offset_fsb); +return xfs_bmbt_to_iomap (ip, iomap, &cmap,IOMAP_F_SHARED ); + out_unlock:xfs_iunlock (ip,XFS_ILOCK_EXCL );return error; + }int
Signature · 1 line
2.20.1
PATCH 5/12 xfs: fill out the srcmap in iomap_begin
In reply to Christoph HellwigReplace our local hacks to report the source block in the main iomap with the proper scrmap reporting.
--- fs/xfs/xfs_iomap.c | 49 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 25 deletions(-)
fs/xfs/xfs_iomap.c +24 -25
@@ -527,7 +527,8 @@ xfs_file_iomap_begin_delay(loff_t offset, loff_t count, unsigned flags, - struct iomap *iomap) + struct iomap *iomap, + struct iomap *srcmap) { struct xfs_inode *ip = XFS_I(inode);struct xfs_mount *mp = ip->i_mount;
@@ -721,11 +722,13 @@ xfs_file_iomap_begin_delay( found_cow:xfs_iunlock (ip,XFS_ILOCK_EXCL );if (imap.br_startoff <= offset_fsb) { -/* ensure we only report blocks we have a reservation for */ -xfs_trim_extent (&imap, cmap.br_startoff, cmap.br_blockcount); -return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_SHARED ); + error =xfs_bmbt_to_iomap (ip, srcmap, &imap,0 ); +if (error) +return error; + }else { +xfs_trim_extent (&cmap, offset_fsb, + imap.br_startoff - offset_fsb); } -xfs_trim_extent (&cmap, offset_fsb, imap.br_startoff - offset_fsb);return xfs_bmbt_to_iomap (ip, iomap, &cmap,IOMAP_F_SHARED ); out_unlock:
@@ -933,7 +936,7 @@ xfs_file_iomap_begin( {struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -struct xfs_bmbt_irec imap; +struct xfs_bmbt_irec imap, cmap;xfs_fileoff_t offset_fsb, end_fsb;int nimaps =1 , error =0 ;bool shared = false;
@@ -947,7 +950,7 @@ xfs_file_iomap_begin( !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {/* Reserve delalloc blocks for regular writeback. */ return xfs_file_iomap_begin_delay (inode, offset, length, flags, - iomap); + iomap, srcmap); }/*
@@ -987,9 +990,6 @@ xfs_file_iomap_begin(* been done up front, so we don't need to do them here. */ if (xfs_is_cow_inode (ip)) { -struct xfs_bmbt_irec cmap; -bool directio = (flags &IOMAP_DIRECT ); -/* if zeroing doesn't need COW allocation, then we are done. */ if ((flags &IOMAP_ZERO ) && !needs_cow_for_zeroing (&imap, nimaps))
@@ -997,23 +997,11 @@ xfs_file_iomap_begin(/* may drop and re-acquire the ilock */ error =xfs_reflink_allocate_cow (ip, &imap, &cmap, &shared, - &lockmode, directio); + &lockmode, flags &IOMAP_DIRECT );if (error) goto out_unlock; - -/* -* For buffered writes we need to report the address of the -* previous block (if there was any) so that the higher level -* write code can perform read-modify-write operations; we -* won't need the CoW fork mapping until writeback. For direct -* I/O, which must be block aligned, we need to report the -* newly allocated address. If the data fork has a hole, copy -* the COW fork mapping to avoid allocating to the data fork. -*/ -if (shared && - (directio || imap.br_startblock ==HOLESTARTBLOCK )) - imap = cmap; - +if (shared) + goto out_found_cow; end_fsb = imap.br_startoff + imap.br_blockcount; length =XFS_FSB_TO_B (mp, end_fsb) - offset; }
@@ -1074,6 +1062,17 @@ xfs_file_iomap_begin(trace_xfs_iomap_found (ip, offset, length,XFS_DATA_FORK , &imap); goto out_finish; +out_found_cow: +xfs_iunlock (ip, lockmode); + length =XFS_FSB_TO_B (mp, cmap.br_startoff + cmap.br_blockcount); +trace_xfs_iomap_found (ip, offset, length - offset,XFS_COW_FORK , &cmap); +if (imap.br_startblock !=HOLESTARTBLOCK ) { + error =xfs_bmbt_to_iomap (ip, srcmap, &imap,0 ); +if (error) +return error; + } +return xfs_bmbt_to_iomap (ip, iomap, &cmap,IOMAP_F_SHARED ); + out_unlock:xfs_iunlock (ip, lockmode);return error;
Signature · 1 line
2.20.1
PATCH 6/12 xfs: factor out a helper to calculate the end_fsb
In reply to Christoph HellwigWe have lots of places that want to calculate the final fsb for a offset + count in bytes and check that the result fits into s_maxbytes. Factor out a helper for that.
--- fs/xfs/xfs_iomap.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
fs/xfs/xfs_iomap.c +20 -20
@@ -102,6 +102,17 @@ xfs_hole_to_iomap( iomap->dax_dev =xfs_find_daxdev_for_inode (VFS_I (ip)); } +static inline xfs_fileoff_t +xfs_iomap_end_fsb ( +struct xfs_mount *mp, +loff_t offset, +loff_t count) +{ +ASSERT (offset <= mp->m_super->s_maxbytes); +return min (XFS_B_TO_FSB (mp, offset + count), +XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes)); +} + xfs_extlen_t xfs_eof_alignment( struct xfs_inode *ip,
@@ -172,8 +183,8 @@ xfs_iomap_write_direct( int nmaps) {xfs_mount_t *mp = ip->i_mount; -xfs_fileoff_t offset_fsb; -xfs_fileoff_t last_fsb; +xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); +xfs_fileoff_t last_fsb =xfs_iomap_end_fsb (mp, offset, count);xfs_filblks_t count_fsb, resaligned;xfs_extlen_t extsz;int nimaps;
@@ -192,8 +203,6 @@ xfs_iomap_write_direct(ASSERT (xfs_isilocked (ip, lockmode)); - offset_fsb =XFS_B_TO_FSBT (mp, offset); - last_fsb =XFS_B_TO_FSB (mp, ((xfs_ufsize_t)(offset + count)));if ((offset + count) >XFS_ISIZE (ip)) {/* * Assert that the in-core extent list is present since this can
@@ -533,9 +542,7 @@ xfs_file_iomap_begin_delay(struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount;xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); -xfs_fileoff_t maxbytes_fsb = -XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes); -xfs_fileoff_t end_fsb; +xfs_fileoff_t end_fsb =xfs_iomap_end_fsb (mp, offset, count);struct xfs_bmbt_irec imap, cmap;struct xfs_iext_cursor icur, ccur;xfs_fsblock_t prealloc_blocks =0 ;
@@ -565,8 +572,6 @@ xfs_file_iomap_begin_delay( goto out_unlock; } - end_fsb =min (XFS_B_TO_FSB (mp, offset + count), maxbytes_fsb); -/* * Search the data fork fork first to look up our source mapping. We * always need the data fork map, as we have to return it to the
@@ -648,7 +653,7 @@ xfs_file_iomap_begin_delay(* the lower level functions are updated. */ count =min_t (loff_t, count,1024 *PAGE_SIZE ); - end_fsb =min (XFS_B_TO_FSB (mp, offset + count), maxbytes_fsb); + end_fsb =xfs_iomap_end_fsb (mp, offset, count);if (xfs_is_always_cow_inode (ip)) whichfork =XFS_COW_FORK ;
@@ -674,7 +679,8 @@ xfs_file_iomap_begin_delay(if (align) p_end_fsb =roundup_64 (p_end_fsb, align); - p_end_fsb =min (p_end_fsb, maxbytes_fsb); + p_end_fsb =min (p_end_fsb, +XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes));ASSERT (p_end_fsb > offset_fsb); prealloc_blocks = p_end_fsb - end_fsb; }
@@ -937,7 +943,8 @@ xfs_file_iomap_begin(struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount;struct xfs_bmbt_irec imap, cmap; -xfs_fileoff_t offset_fsb, end_fsb; +xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); +xfs_fileoff_t end_fsb =xfs_iomap_end_fsb (mp, offset, length);int nimaps =1 , error =0 ;bool shared = false;u16 iomap_flags =0 ;
@@ -963,12 +970,6 @@ xfs_file_iomap_begin(if (error)return error; -ASSERT (offset <= mp->m_super->s_maxbytes); -if (offset > mp->m_super->s_maxbytes - length) - length = mp->m_super->s_maxbytes - offset; - offset_fsb =XFS_B_TO_FSBT (mp, offset); - end_fsb =XFS_B_TO_FSB (mp, offset + length); - error =xfs_bmapi_read (ip, offset_fsb, end_fsb - offset_fsb, &imap, &nimaps,0 );if (error)
@@ -1196,8 +1197,7 @@ xfs_seek_iomap_begin(/* * Fake a hole until the end of the file. */ - data_fsb =min (XFS_B_TO_FSB (mp, offset + length), -XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes)); + data_fsb =xfs_iomap_end_fsb (mp, offset, length); }/*
Signature · 1 line
2.20.1
PATCH 7/12 xfs: split out a new set of read-only iomap ops
In reply to Christoph HellwigStart untangling xfs_file_iomap_begin by splitting out the read-only case into its own set of iomap_ops with a very simply iomap_begin helper.
--- fs/xfs/xfs_aops.c | 9 ++++--- fs/xfs/xfs_file.c | 9 ++++--- fs/xfs/xfs_iomap.c | 63 ++++++++++++++++++++++++++++++++++------------ fs/xfs/xfs_iomap.h | 1 + fs/xfs/xfs_iops.c | 2 +- 5 files changed, 60 insertions(+), 24 deletions(-)
fs/xfs/xfs_aops.c +5 -4
@@ -634,7 +634,7 @@ xfs_vm_bmap(*/ if (xfs_is_cow_inode (ip) ||XFS_IS_REALTIME_INODE (ip))return 0 ; -return iomap_bmap (mapping, block, &xfs_iomap_ops); +return iomap_bmap (mapping, block, &xfs_read_iomap_ops); }STATIC int
@@ -642,7 +642,7 @@ xfs_vm_readpage(struct file *unused, struct page *page) { - return iomap_readpage(page, &xfs_iomap_ops); + return iomap_readpage(page, &xfs_read_iomap_ops); }STATIC int
@@ -652,7 +652,7 @@ xfs_vm_readpages( struct list_head *pages, unsigned nr_pages) { -return iomap_readpages (mapping, pages, nr_pages, &xfs_iomap_ops); +return iomap_readpages (mapping, pages, nr_pages, &xfs_read_iomap_ops); } static int
@@ -662,7 +662,8 @@ xfs_iomap_swapfile_activate( sector_t *span) { sis->bdev =xfs_find_bdev_for_inode (file_inode (swap_file)); -return iomap_swapfile_activate (sis, swap_file, span, &xfs_iomap_ops); +return iomap_swapfile_activate (sis, swap_file, span, + &xfs_read_iomap_ops); }const struct address_space_operations xfs_address_space_operations = {
fs/xfs/xfs_file.c +6 -3
@@ -188,7 +188,8 @@ xfs_file_dio_aio_read(file_accessed (iocb->ki_filp);xfs_ilock (ip,XFS_IOLOCK_SHARED ); - ret =iomap_dio_rw (iocb, to, &xfs_iomap_ops,NULL ,is_sync_kiocb (iocb)); + ret =iomap_dio_rw (iocb, to, &xfs_read_iomap_ops,NULL , +is_sync_kiocb (iocb));xfs_iunlock (ip,XFS_IOLOCK_SHARED );return ret;
@@ -215,7 +216,7 @@ xfs_file_dax_read(xfs_ilock (ip,XFS_IOLOCK_SHARED ); } - ret =dax_iomap_rw (iocb, to, &xfs_iomap_ops); + ret =dax_iomap_rw (iocb, to, &xfs_read_iomap_ops);xfs_iunlock (ip,XFS_IOLOCK_SHARED );file_accessed (iocb->ki_filp);
@@ -1153,7 +1154,9 @@ __xfs_filemap_fault(if (IS_DAX (inode)) {pfn_t pfn; - ret =dax_iomap_fault (vmf, pe_size, &pfn,NULL , &xfs_iomap_ops); + ret =dax_iomap_fault (vmf, pe_size, &pfn,NULL , + (write_fault && !vmf->cow_page) ? + &xfs_iomap_ops : &xfs_read_iomap_ops);if (ret &VM_FAULT_NEEDDSYNC ) ret =dax_finish_sync_fault (vmf, pe_size, pfn); }else {
fs/xfs/xfs_iomap.c +47 -16
91 lines not on this page · raw message
fs/xfs/xfs_iomap.h +1 -0
@@ -40,6 +40,7 @@ xfs_aligned_fsb_count( }extern const struct iomap_ops xfs_iomap_ops; +extern const struct iomap_ops xfs_read_iomap_ops;extern const struct iomap_ops xfs_seek_iomap_ops;extern const struct iomap_ops xfs_xattr_iomap_ops;
fs/xfs/xfs_iops.c +1 -1
@@ -1114,7 +1114,7 @@ xfs_vn_fiemap( &xfs_xattr_iomap_ops); }else { error =iomap_fiemap (inode, fieinfo, start, length, - &xfs_iomap_ops); + &xfs_read_iomap_ops); }xfs_iunlock (XFS_I (inode),XFS_IOLOCK_SHARED );
Signature · 1 line
2.20.1
PATCH 8/12 xfs: move xfs_file_iomap_begin_delay around
In reply to Christoph HellwigMove xfs_file_iomap_begin_delay near the end of the file next to the other iomap functions to prepare for additional refactoring.
--- fs/xfs/xfs_iomap.c | 434 +++++++++++++++++++++++---------------------- 1 file changed, 221 insertions(+), 213 deletions(-)
fs/xfs/xfs_iomap.c +221 -213
@@ -530,219 +530,6 @@ xfs_iomap_prealloc_size(return alloc_blocks; } -static int -xfs_file_iomap_begin_delay ( -struct inode *inode, -loff_t offset, -loff_t count, -unsigned flags, -struct iomap *iomap, -struct iomap *srcmap) -{ -struct xfs_inode *ip =XFS_I (inode); -struct xfs_mount *mp = ip->i_mount; -xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); -xfs_fileoff_t end_fsb =xfs_iomap_end_fsb (mp, offset, count); -struct xfs_bmbt_irec imap, cmap; -struct xfs_iext_cursor icur, ccur; -xfs_fsblock_t prealloc_blocks =0 ; -bool eof = false, cow_eof = false, shared = false; -int whichfork =XFS_DATA_FORK ; -int error =0 ; - -ASSERT (!XFS_IS_REALTIME_INODE (ip)); -ASSERT (!xfs_get_extsz_hint (ip)); - -xfs_ilock (ip,XFS_ILOCK_EXCL ); - -if (unlikely (XFS_TEST_ERROR ( - (XFS_IFORK_FORMAT (ip,XFS_DATA_FORK ) !=XFS_DINODE_FMT_EXTENTS && -XFS_IFORK_FORMAT (ip,XFS_DATA_FORK ) !=XFS_DINODE_FMT_BTREE ), - mp,XFS_ERRTAG_BMAPIFORMAT ))) { -XFS_ERROR_REPORT (__func__,XFS_ERRLEVEL_LOW , mp); - error = -EFSCORRUPTED ; - goto out_unlock; - } - -XFS_STATS_INC (mp, xs_blk_mapw); - -if (!(ip->i_df.if_flags &XFS_IFEXTENTS )) { - error =xfs_iread_extents (NULL , ip,XFS_DATA_FORK ); -if (error) - goto out_unlock; - } - -/* -* Search the data fork fork first to look up our source mapping. We -* always need the data fork map, as we have to return it to the -* iomap code so that the higher level write code can read data in to -* perform read-modify-write cycles for unaligned writes. -*/ - eof = !xfs_iext_lookup_extent (ip, &ip->i_df, offset_fsb, &icur, &imap); -if (eof) - imap.br_startoff = end_fsb;/* fake hole until the end */ - -/* We never need to allocate blocks for zeroing a hole. */ -if ((flags &IOMAP_ZERO ) && imap.br_startoff > offset_fsb) { -xfs_hole_to_iomap (ip, iomap, offset_fsb, imap.br_startoff); - goto out_unlock; - } - -/* -* Search the COW fork extent list even if we did not find a data fork -* extent. This serves two purposes: first this implements the -* speculative preallocation using cowextsize, so that we also unshare -* block adjacent to shared blocks instead of just the shared blocks -* themselves. Second the lookup in the extent list is generally faster -* than going out to the shared extent tree. -*/ -if (xfs_is_cow_inode (ip)) { -if (!ip->i_cowfp) { -ASSERT (!xfs_is_reflink_inode (ip)); -xfs_ifork_init_cow (ip); - } - cow_eof = !xfs_iext_lookup_extent (ip, ip->i_cowfp, offset_fsb, - &ccur, &cmap); -if (!cow_eof && cmap.br_startoff <= offset_fsb) { -trace_xfs_reflink_cow_found (ip, &cmap); - goto found_cow; - } - } - -if (imap.br_startoff <= offset_fsb) { -/* -* For reflink files we may need a delalloc reservation when -* overwriting shared extents. This includes zeroing of -* existing extents that contain data. -*/ -if (!xfs_is_cow_inode (ip) || - ((flags &IOMAP_ZERO ) && imap.br_state !=XFS_EXT_NORM )) { -trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , - &imap); - goto found_imap; - } - -xfs_trim_extent (&imap, offset_fsb, end_fsb - offset_fsb); - -/* Trim the mapping to the nearest shared extent boundary. */ - error =xfs_inode_need_cow (ip, &imap, &shared); -if (error) - goto out_unlock; - -/* Not shared? Just report the (potentially capped) extent. */ -if (!shared) { -trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , - &imap); - goto found_imap; - } - -/* -* Fork all the shared blocks from our write offset until the -* end of the extent. -*/ - whichfork =XFS_COW_FORK ; - end_fsb = imap.br_startoff + imap.br_blockcount; - }else { -/* -* We cap the maximum length we map here to MAX_WRITEBACK_PAGES -* pages to keep the chunks of work done where somewhat -* symmetric with the work writeback does. This is a completely -* arbitrary number pulled out of thin air. -* -* Note that the values needs to be less than 32-bits wide until -* the lower level functions are updated. -*/ - count =min_t (loff_t, count,1024 *PAGE_SIZE ); - end_fsb =xfs_iomap_end_fsb (mp, offset, count); - -if (xfs_is_always_cow_inode (ip)) - whichfork =XFS_COW_FORK ; - } - - error =xfs_qm_dqattach_locked (ip, false); -if (error) - goto out_unlock; - -if (eof) { - prealloc_blocks =xfs_iomap_prealloc_size (ip, whichfork, offset, - count, &icur); -if (prealloc_blocks) { -xfs_extlen_t align; -xfs_off_t end_offset; -xfs_fileoff_t p_end_fsb; - - end_offset =XFS_WRITEIO_ALIGN (mp, offset + count -1 ); - p_end_fsb =XFS_B_TO_FSBT (mp, end_offset) + - prealloc_blocks; - - align =xfs_eof_alignment (ip,0 ); -if (align) - p_end_fsb =roundup_64 (p_end_fsb, align); - - p_end_fsb =min (p_end_fsb, -XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes)); -ASSERT (p_end_fsb > offset_fsb); - prealloc_blocks = p_end_fsb - end_fsb; - } - } - -retry: - error =xfs_bmapi_reserve_delalloc (ip, whichfork, offset_fsb, - end_fsb - offset_fsb, prealloc_blocks, - whichfork ==XFS_DATA_FORK ? &imap : &cmap, - whichfork ==XFS_DATA_FORK ? &icur : &ccur, - whichfork ==XFS_DATA_FORK ? eof : cow_eof); -switch (error) { -case 0 : -break ; -case -ENOSPC : -case -EDQUOT : -/* retry without any preallocation */ -trace_xfs_delalloc_enospc (ip, offset, count); -if (prealloc_blocks) { - prealloc_blocks =0 ; - goto retry; - } -/*FALLTHRU*/ -default : - goto out_unlock; - } - -if (whichfork ==XFS_COW_FORK ) { -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &cmap); - goto found_cow; - } - -/* -* Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch -* them out if the write happens to fail. -*/ -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &imap); -return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_NEW ); - -found_imap: -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -return xfs_bmbt_to_iomap (ip, iomap, &imap,0 ); - -found_cow: -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -if (imap.br_startoff <= offset_fsb) { - error =xfs_bmbt_to_iomap (ip, srcmap, &imap,0 ); -if (error) -return error; - }else { -xfs_trim_extent (&cmap, offset_fsb, - imap.br_startoff - offset_fsb); - } -return xfs_bmbt_to_iomap (ip, iomap, &cmap,IOMAP_F_SHARED ); - -out_unlock: -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -return error; - -} - int xfs_iomap_write_unwritten( xfs_inode_t *ip,
@@ -931,6 +718,15 @@ xfs_ilock_for_iomap(return 0 ; } +static int +xfs_file_iomap_begin_delay ( +struct inode *inode, +loff_t offset, +loff_t count, +unsigned flags, +struct iomap *iomap, +struct iomap *srcmap); + static int xfs_file_iomap_begin( struct inode *inode,
@@ -1068,6 +864,218 @@ xfs_file_iomap_begin(return error; } +static int +xfs_file_iomap_begin_delay ( +struct inode *inode, +loff_t offset, +loff_t count, +unsigned flags, +struct iomap *iomap, +struct iomap *srcmap) +{ +struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount; +xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); +xfs_fileoff_t end_fsb =xfs_iomap_end_fsb (mp, offset, count); +struct xfs_bmbt_irec imap, cmap; +struct xfs_iext_cursor icur, ccur; +xfs_fsblock_t prealloc_blocks =0 ; +bool eof = false, cow_eof = false, shared = false; +int whichfork =XFS_DATA_FORK ; +int error =0 ; + +ASSERT (!XFS_IS_REALTIME_INODE (ip)); +ASSERT (!xfs_get_extsz_hint (ip)); + +xfs_ilock (ip,XFS_ILOCK_EXCL ); + +if (unlikely (XFS_TEST_ERROR ( + (XFS_IFORK_FORMAT (ip,XFS_DATA_FORK ) !=XFS_DINODE_FMT_EXTENTS && +XFS_IFORK_FORMAT (ip,XFS_DATA_FORK ) !=XFS_DINODE_FMT_BTREE ), + mp,XFS_ERRTAG_BMAPIFORMAT ))) { +XFS_ERROR_REPORT (__func__,XFS_ERRLEVEL_LOW , mp); + error = -EFSCORRUPTED ; + goto out_unlock; + } + +XFS_STATS_INC (mp, xs_blk_mapw); + +if (!(ip->i_df.if_flags &XFS_IFEXTENTS )) { + error =xfs_iread_extents (NULL , ip,XFS_DATA_FORK ); +if (error) + goto out_unlock; + } + +/* +* Search the data fork fork first to look up our source mapping. We +* always need the data fork map, as we have to return it to the +* iomap code so that the higher level write code can read data in to +* perform read-modify-write cycles for unaligned writes. +*/ + eof = !xfs_iext_lookup_extent (ip, &ip->i_df, offset_fsb, &icur, &imap); +if (eof) + imap.br_startoff = end_fsb;/* fake hole until the end */ + +/* We never need to allocate blocks for zeroing a hole. */ +if ((flags &IOMAP_ZERO ) && imap.br_startoff > offset_fsb) { +xfs_hole_to_iomap (ip, iomap, offset_fsb, imap.br_startoff); + goto out_unlock; + } + +/* +* Search the COW fork extent list even if we did not find a data fork +* extent. This serves two purposes: first this implements the +* speculative preallocation using cowextsize, so that we also unshare +* block adjacent to shared blocks instead of just the shared blocks +* themselves. Second the lookup in the extent list is generally faster +* than going out to the shared extent tree. +*/ +if (xfs_is_cow_inode (ip)) { +if (!ip->i_cowfp) { +ASSERT (!xfs_is_reflink_inode (ip)); +xfs_ifork_init_cow (ip); + } + cow_eof = !xfs_iext_lookup_extent (ip, ip->i_cowfp, offset_fsb, + &ccur, &cmap); +if (!cow_eof && cmap.br_startoff <= offset_fsb) { +trace_xfs_reflink_cow_found (ip, &cmap); + goto found_cow; + } + } + +if (imap.br_startoff <= offset_fsb) { +/* +* For reflink files we may need a delalloc reservation when +* overwriting shared extents. This includes zeroing of +* existing extents that contain data. +*/ +if (!xfs_is_cow_inode (ip) || + ((flags &IOMAP_ZERO ) && imap.br_state !=XFS_EXT_NORM )) { +trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , + &imap); + goto found_imap; + } + +xfs_trim_extent (&imap, offset_fsb, end_fsb - offset_fsb); + +/* Trim the mapping to the nearest shared extent boundary. */ + error =xfs_inode_need_cow (ip, &imap, &shared); +if (error) + goto out_unlock; + +/* Not shared? Just report the (potentially capped) extent. */ +if (!shared) { +trace_xfs_iomap_found (ip, offset, count,XFS_DATA_FORK , + &imap); + goto found_imap; + } + +/* +* Fork all the shared blocks from our write offset until the +* end of the extent. +*/ + whichfork =XFS_COW_FORK ; + end_fsb = imap.br_startoff + imap.br_blockcount; + }else { +/* +* We cap the maximum length we map here to MAX_WRITEBACK_PAGES +* pages to keep the chunks of work done where somewhat +* symmetric with the work writeback does. This is a completely +* arbitrary number pulled out of thin air. +* +* Note that the values needs to be less than 32-bits wide until +* the lower level functions are updated. +*/ + count =min_t (loff_t, count,1024 *PAGE_SIZE ); + end_fsb =xfs_iomap_end_fsb (mp, offset, count); + +if (xfs_is_always_cow_inode (ip)) + whichfork =XFS_COW_FORK ; + } + + error =xfs_qm_dqattach_locked (ip, false); +if (error) + goto out_unlock; + +if (eof) { + prealloc_blocks =xfs_iomap_prealloc_size (ip, whichfork, offset, + count, &icur); +if (prealloc_blocks) { +xfs_extlen_t align; +xfs_off_t end_offset; +xfs_fileoff_t p_end_fsb; + + end_offset =XFS_WRITEIO_ALIGN (mp, offset + count -1 ); + p_end_fsb =XFS_B_TO_FSBT (mp, end_offset) + + prealloc_blocks; + + align =xfs_eof_alignment (ip,0 ); +if (align) + p_end_fsb =roundup_64 (p_end_fsb, align); + + p_end_fsb =min (p_end_fsb, +XFS_B_TO_FSB (mp, mp->m_super->s_maxbytes)); +ASSERT (p_end_fsb > offset_fsb); + prealloc_blocks = p_end_fsb - end_fsb; + } + } + +retry: + error =xfs_bmapi_reserve_delalloc (ip, whichfork, offset_fsb, + end_fsb - offset_fsb, prealloc_blocks, + whichfork ==XFS_DATA_FORK ? &imap : &cmap, + whichfork ==XFS_DATA_FORK ? &icur : &ccur, + whichfork ==XFS_DATA_FORK ? eof : cow_eof); +switch (error) { +case 0 : +break ; +case -ENOSPC : +case -EDQUOT : +/* retry without any preallocation */ +trace_xfs_delalloc_enospc (ip, offset, count); +if (prealloc_blocks) { + prealloc_blocks =0 ; + goto retry; + } +/*FALLTHRU*/ +default : + goto out_unlock; + } + +if (whichfork ==XFS_COW_FORK ) { +trace_xfs_iomap_alloc (ip, offset, count, whichfork, &cmap); + goto found_cow; + } + +/* +* Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch +* them out if the write happens to fail. +*/ +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +trace_xfs_iomap_alloc (ip, offset, count, whichfork, &imap); +return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_NEW ); + +found_imap: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +return xfs_bmbt_to_iomap (ip, iomap, &imap,0 ); + +found_cow: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +if (imap.br_startoff <= offset_fsb) { + error =xfs_bmbt_to_iomap (ip, srcmap, &imap,0 ); +if (error) +return error; + }else { +xfs_trim_extent (&cmap, offset_fsb, + imap.br_startoff - offset_fsb); + } +return xfs_bmbt_to_iomap (ip, iomap, &cmap,IOMAP_F_SHARED ); + +out_unlock: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +return error; +} + static int xfs_file_iomap_end_delalloc( struct xfs_inode *ip,
Signature · 1 line
2.20.1
PATCH 9/12 xfs: split the iomap ops for buffered vs direct writes
In reply to Christoph HellwigInstead of lots of magic conditionals in the main write_begin handler this make the intent very clear. Thing will become even better once we support delayed allocations for extent size hints and realtime allocations.
--- fs/xfs/xfs_bmap_util.c | 3 ++- fs/xfs/xfs_file.c | 16 ++++++----- fs/xfs/xfs_iomap.c | 61 +++++++++++++++--------------------------- fs/xfs/xfs_iomap.h | 3 ++- fs/xfs/xfs_iops.c | 4 +-- fs/xfs/xfs_reflink.c | 5 ++-- 6 files changed, 40 insertions(+), 52 deletions(-)
fs/xfs/xfs_bmap_util.c +2 -1
@@ -1113,7 +1113,8 @@ xfs_free_file_space(return 0 ;if (offset + len >XFS_ISIZE (ip)) len =XFS_ISIZE (ip) - offset; - error =iomap_zero_range (VFS_I (ip), offset, len,NULL , &xfs_iomap_ops); + error =iomap_zero_range (VFS_I (ip), offset, len,NULL , + &xfs_buffered_write_iomap_ops);if (error)return error;
fs/xfs/xfs_file.c +10 -6
@@ -352,7 +352,7 @@ xfs_file_aio_write_checks(trace_xfs_zero_eof (ip, isize, iocb->ki_pos - isize); error =iomap_zero_range (inode, isize, iocb->ki_pos - isize, -NULL , &xfs_iomap_ops); +NULL , &xfs_buffered_write_iomap_ops);if (error)return error; }else
@@ -552,7 +552,8 @@ xfs_file_dio_aio_write(* If unaligned, this is the only IO in-flight. Wait on it before we * release the iolock to prevent subsequent overlapping IO. */ - ret =iomap_dio_rw (iocb, from, &xfs_iomap_ops, &xfs_dio_write_ops, + ret =iomap_dio_rw (iocb, from, &xfs_direct_write_iomap_ops, + &xfs_dio_write_ops,is_sync_kiocb (iocb) || unaligned_io); out:xfs_iunlock (ip, iolock);
@@ -592,7 +593,7 @@ xfs_file_dax_write( count =iov_iter_count (from);trace_xfs_file_dax_write (ip, count, pos); - ret =dax_iomap_rw (iocb, from, &xfs_iomap_ops); + ret =dax_iomap_rw (iocb, from, &xfs_direct_write_iomap_ops);if (ret >0 && iocb->ki_pos >i_size_read (inode)) {i_size_write (inode, iocb->ki_pos); error =xfs_setfilesize (ip, pos, ret);
@@ -639,7 +640,8 @@ xfs_file_buffered_aio_write( current->backing_dev_info =inode_to_bdi (inode);trace_xfs_file_buffered_write (ip,iov_iter_count (from), iocb->ki_pos); - ret =iomap_file_buffered_write (iocb, from, &xfs_iomap_ops); + ret =iomap_file_buffered_write (iocb, from, + &xfs_buffered_write_iomap_ops);if (likely (ret >=0 )) iocb->ki_pos += ret;
@@ -1156,12 +1158,14 @@ __xfs_filemap_fault( ret =dax_iomap_fault (vmf, pe_size, &pfn,NULL , (write_fault && !vmf->cow_page) ? - &xfs_iomap_ops : &xfs_read_iomap_ops); + &xfs_direct_write_iomap_ops : + &xfs_read_iomap_ops);if (ret &VM_FAULT_NEEDDSYNC ) ret =dax_finish_sync_fault (vmf, pe_size, pfn); }else {if (write_fault) - ret =iomap_page_mkwrite (vmf, &xfs_iomap_ops); + ret =iomap_page_mkwrite (vmf, + &xfs_buffered_write_iomap_ops);else ret =filemap_fault (vmf); }
fs/xfs/xfs_iomap.c +21 -40
109 lines not on this page · raw message
fs/xfs/xfs_iomap.h +2 -1
@@ -39,7 +39,8 @@ xfs_aligned_fsb_count(return count_fsb; } -extern const struct iomap_ops xfs_iomap_ops; +extern const struct iomap_ops xfs_buffered_write_iomap_ops; +extern const struct iomap_ops xfs_direct_write_iomap_ops;extern const struct iomap_ops xfs_read_iomap_ops;extern const struct iomap_ops xfs_seek_iomap_ops;extern const struct iomap_ops xfs_xattr_iomap_ops;
fs/xfs/xfs_iops.c +2 -2
@@ -883,10 +883,10 @@ xfs_setattr_size(if (newsize > oldsize) {trace_xfs_zero_eof (ip, oldsize, newsize - oldsize); error =iomap_zero_range (inode, oldsize, newsize - oldsize, - &did_zeroing, &xfs_iomap_ops); + &did_zeroing, &xfs_buffered_write_iomap_ops); }else { error =iomap_truncate_page (inode, newsize, &did_zeroing, - &xfs_iomap_ops); + &xfs_buffered_write_iomap_ops); }if (error)
fs/xfs/xfs_reflink.c +3 -2
@@ -1270,7 +1270,7 @@ xfs_reflink_zero_posteof(trace_xfs_zero_eof (ip, isize, pos - isize);return iomap_zero_range (VFS_I (ip), isize, pos - isize,NULL , - &xfs_iomap_ops); + &xfs_buffered_write_iomap_ops); }/*
@@ -1527,7 +1527,8 @@ xfs_reflink_unshare(inode_dio_wait (inode); - error =iomap_file_unshare (inode, offset, len, &xfs_iomap_ops); + error =iomap_file_unshare (inode, offset, len, + &xfs_buffered_write_iomap_ops);if (error) goto out; error =filemap_write_and_wait (inode->i_mapping);
Signature · 1 line
2.20.1
PATCH 10/12 xfs: rename the whichfork variable in xfs_buffered_write_iomap_begin
In reply to Christoph HellwigRenaming whichfork to allocfork in xfs_buffered_write_iomap_begin makes the usage of this variable a little more clear.
--- fs/xfs/xfs_iomap.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
fs/xfs/xfs_iomap.c +11 -11
@@ -869,7 +869,7 @@ xfs_buffered_write_iomap_begin(struct xfs_iext_cursor icur, ccur;xfs_fsblock_t prealloc_blocks =0 ;bool eof = false, cow_eof = false, shared = false; -int whichfork =XFS_DATA_FORK ; +int allocfork =XFS_DATA_FORK ;int error =0 ;/* we can't use delayed allocations when using extent size hints */
@@ -966,7 +966,7 @@ xfs_buffered_write_iomap_begin(* Fork all the shared blocks from our write offset until the * end of the extent. */ - whichfork =XFS_COW_FORK ; + allocfork =XFS_COW_FORK ; end_fsb = imap.br_startoff + imap.br_blockcount; }else {/*
@@ -982,7 +982,7 @@ xfs_buffered_write_iomap_begin( end_fsb =xfs_iomap_end_fsb (mp, offset, count);if (xfs_is_always_cow_inode (ip)) - whichfork =XFS_COW_FORK ; + allocfork =XFS_COW_FORK ; } error =xfs_qm_dqattach_locked (ip, false);
@@ -990,7 +990,7 @@ xfs_buffered_write_iomap_begin( goto out_unlock;if (eof) { - prealloc_blocks =xfs_iomap_prealloc_size (ip, whichfork, offset, + prealloc_blocks =xfs_iomap_prealloc_size (ip, allocfork, offset, count, &icur);if (prealloc_blocks) {xfs_extlen_t align;
@@ -1013,11 +1013,11 @@ xfs_buffered_write_iomap_begin( } retry: - error =xfs_bmapi_reserve_delalloc (ip, whichfork, offset_fsb, + error =xfs_bmapi_reserve_delalloc (ip, allocfork, offset_fsb, end_fsb - offset_fsb, prealloc_blocks, - whichfork ==XFS_DATA_FORK ? &imap : &cmap, - whichfork ==XFS_DATA_FORK ? &icur : &ccur, - whichfork ==XFS_DATA_FORK ? eof : cow_eof); + allocfork ==XFS_DATA_FORK ? &imap : &cmap, + allocfork ==XFS_DATA_FORK ? &icur : &ccur, + allocfork ==XFS_DATA_FORK ? eof : cow_eof);switch (error) {case 0 :break ;
@@ -1034,8 +1034,8 @@ xfs_buffered_write_iomap_begin( goto out_unlock; } -if (whichfork ==XFS_COW_FORK ) { -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &cmap); +if (allocfork ==XFS_COW_FORK ) { +trace_xfs_iomap_alloc (ip, offset, count, allocfork, &cmap); goto found_cow; }
@@ -1044,7 +1044,7 @@ xfs_buffered_write_iomap_begin(* them out if the write happens to fail. */ xfs_iunlock (ip,XFS_ILOCK_EXCL ); -trace_xfs_iomap_alloc (ip, offset, count, whichfork, &imap); +trace_xfs_iomap_alloc (ip, offset, count, allocfork, &imap);return xfs_bmbt_to_iomap (ip, iomap, &imap,IOMAP_F_NEW ); found_imap:
Signature · 1 line
2.20.1
PATCH 11/12 xfs: cleanup xfs_direct_write_iomap_begin
In reply to Christoph HellwigMove more checks into the helpers that determine if we need a COW operation or allocation and split the return path for when an existing data for allocation has been found versus a new allocation.
--- fs/xfs/xfs_iomap.c | 88 ++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 42 deletions(-)
fs/xfs/xfs_iomap.c +46 -42
@@ -642,23 +642,42 @@ xfs_iomap_write_unwritten(static inline bool imap_needs_alloc (struct inode *inode, +unsigned flags,struct xfs_bmbt_irec *imap,int nimaps) { -return !nimaps || - imap->br_startblock ==HOLESTARTBLOCK || - imap->br_startblock ==DELAYSTARTBLOCK || - (IS_DAX (inode) && imap->br_state ==XFS_EXT_UNWRITTEN ); +/* don't allocate blocks when just zeroing */ +if (flags &IOMAP_ZERO ) +return false; +if (!nimaps || + imap->br_startblock ==HOLESTARTBLOCK || + imap->br_startblock ==DELAYSTARTBLOCK ) +return true; +/* we convert unwritten extents before copying the data for DAX */ +if (IS_DAX (inode) && imap->br_state ==XFS_EXT_UNWRITTEN ) +return true; +return false; }static inline bool -needs_cow_for_zeroing ( +imap_needs_cow ( +struct xfs_inode *ip, +unsigned int flags,struct xfs_bmbt_irec *imap,int nimaps) { -return nimaps && - imap->br_startblock !=HOLESTARTBLOCK && - imap->br_state !=XFS_EXT_UNWRITTEN ; +if (!xfs_is_cow_inode (ip)) +return false; + +/* when zeroing we don't have to COW holes or unwritten extents */ +if (flags &IOMAP_ZERO ) { +if (!nimaps || + imap->br_startblock ==HOLESTARTBLOCK || + imap->br_state ==XFS_EXT_UNWRITTEN ) +return false; + } + +return true; } static int
@@ -742,6 +761,14 @@ xfs_direct_write_iomap_begin(if (XFS_FORCED_SHUTDOWN (mp))return -EIO ; +/* +* Writes that span EOF might trigger an IO size update on completion, +* so consider them to be dirty for the purposes of O_DSYNC even if +* there is no other metadata changes pending or have been made here. +*/ +if (offset + length >i_size_read (inode)) + iomap_flags |=IOMAP_F_DIRTY ; +/* * Lock the inode in the manner required for the specified operation and * check for as many conditions that would result in blocking as
@@ -761,12 +788,7 @@ xfs_direct_write_iomap_begin(* Break shared extents if necessary. Checks for non-blocking IO have * been done up front, so we don't need to do them here. */ -if (xfs_is_cow_inode (ip)) { -/* if zeroing doesn't need COW allocation, then we are done. */ -if ((flags &IOMAP_ZERO ) && - !needs_cow_for_zeroing (&imap, nimaps)) - goto out_found; - +if (imap_needs_cow (ip, flags, &imap, nimaps)) {/* may drop and re-acquire the ilock */ error =xfs_reflink_allocate_cow (ip, &imap, &cmap, &shared, &lockmode, flags &IOMAP_DIRECT );
@@ -778,18 +800,17 @@ xfs_direct_write_iomap_begin( length =XFS_FSB_TO_B (mp, end_fsb) - offset; } -/* Don't need to allocate over holes when doing zeroing operations. */ -if (flags &IOMAP_ZERO ) - goto out_found; +if (imap_needs_alloc (inode, flags, &imap, nimaps)) + goto allocate_blocks; -if (!imap_needs_alloc (inode, &imap, nimaps)) - goto out_found; +xfs_iunlock (ip, lockmode); +trace_xfs_iomap_found (ip, offset, length,XFS_DATA_FORK , &imap); +return xfs_bmbt_to_iomap (ip, iomap, &imap, iomap_flags); -/* If nowait is set bail since we are going to make allocations. */ -if (flags &IOMAP_NOWAIT ) { - error = -EAGAIN ; +allocate_blocks: + error = -EAGAIN ; +if (flags &IOMAP_NOWAIT ) goto out_unlock; - }/* * We cap the maximum length we map to a sane size to keep the chunks
@@ -808,29 +829,12 @@ xfs_direct_write_iomap_begin(*/ if (lockmode ==XFS_ILOCK_EXCL )xfs_ilock_demote (ip, lockmode); - error =xfs_iomap_write_direct (ip, offset, length, &imap, - nimaps); + error =xfs_iomap_write_direct (ip, offset, length, &imap, nimaps);if (error)return error; - iomap_flags |=IOMAP_F_NEW ;trace_xfs_iomap_alloc (ip, offset, length,XFS_DATA_FORK , &imap); - -out_finish: -/* -* Writes that span EOF might trigger an IO size update on completion, -* so consider them to be dirty for the purposes of O_DSYNC even if -* there is no other metadata changes pending or have been made here. -*/ -if (offset + length >i_size_read (inode)) - iomap_flags |=IOMAP_F_DIRTY ; -return xfs_bmbt_to_iomap (ip, iomap, &imap, iomap_flags); - -out_found: -ASSERT (nimaps); -xfs_iunlock (ip, lockmode); -trace_xfs_iomap_found (ip, offset, length,XFS_DATA_FORK , &imap); - goto out_finish; +return xfs_bmbt_to_iomap (ip, iomap, &imap, iomap_flags |IOMAP_F_NEW ); out_found_cow:xfs_iunlock (ip, lockmode);
Signature · 1 line
2.20.1
PATCH 12/12 xfs: improve the IOMAP_NOWAIT check for COW inodes
In reply to Christoph HellwigOnly bail out once we know that a COW allocation is actually required, similar to how we handle normal data fork allocations.
--- fs/xfs/xfs_iomap.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-)
fs/xfs/xfs_iomap.c +5 -18
@@ -693,15 +693,8 @@ xfs_ilock_for_iomap(* COW writes may allocate delalloc space or convert unwritten COW * extents, so we need to make sure to take the lock exclusively here. */ -if (xfs_is_cow_inode (ip) && is_write) { -/* -* FIXME: It could still overwrite on unshared extents and not -* need allocation. -*/ -if (flags &IOMAP_NOWAIT ) -return -EAGAIN ; +if (xfs_is_cow_inode (ip) && is_write) mode =XFS_ILOCK_EXCL ; - }/* * Extents not yet cached requires exclusive access, don't block. This
@@ -769,12 +762,6 @@ xfs_direct_write_iomap_begin(if (offset + length >i_size_read (inode)) iomap_flags |=IOMAP_F_DIRTY ; -/* -* Lock the inode in the manner required for the specified operation and -* check for as many conditions that would result in blocking as -* possible. This removes most of the non-blocking checks from the -* mapping code below. -*/ error =xfs_ilock_for_iomap (ip, flags, &lockmode);if (error)return error;
@@ -784,11 +771,11 @@ xfs_direct_write_iomap_begin(if (error) goto out_unlock; -/* -* Break shared extents if necessary. Checks for non-blocking IO have -* been done up front, so we don't need to do them here. -*/ if (imap_needs_cow (ip, flags, &imap, nimaps)) { + error = -EAGAIN ; +if (flags &IOMAP_NOWAIT ) + goto out_unlock; +/* may drop and re-acquire the ilock */ error =xfs_reflink_allocate_cow (ip, &imap, &cmap, &shared, &lockmode, flags &IOMAP_DIRECT );
Signature · 1 line
2.20.1
Darrick J. Wong
AppliedQuoted 9 lines · Christoph Hellwig
Hi all,
Changes since v2:
- rebased on the latest iomap-for-next and dropped patches already mergedChanges since v1:
- renumberIOMAP_HOLEto 0 and avoid the reserved 0 value
- fix minor typos and update comments
Merged, thanks.
--D