misc cleanups
Just a couple random cleanups from invesigating a larger project.
PATCH 1/3 xfs: add a xfs_valid_startblock helper
Add a helper that validates the startblock is valid. This checks for a non-zero block on the main device, but skips that check for blocks on the realtime device.
--- fs/xfs/libxfs/xfs_bmap.c | 2 +- fs/xfs/libxfs/xfs_bmap.h | 3 +++ fs/xfs/xfs_iomap.c | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_bmap.c +1 -1
@@ -4519,7 +4519,7 @@ xfs_bmapi_convert_delalloc(if (WARN_ON_ONCE (bma.blkno ==NULLFSBLOCK )) goto out_finish; error = -EFSCORRUPTED ; -if (WARN_ON_ONCE (!bma.got.br_startblock && !XFS_IS_REALTIME_INODE (ip))) +if (WARN_ON_ONCE (!xfs_valid_startblock (ip, bma.got.br_startblock))) goto out_finish;XFS_STATS_ADD (mp, xs_xstrat_bytes,XFS_FSB_TO_B (mp, bma.length));
fs/xfs/libxfs/xfs_bmap.h +3 -0
@@ -171,6 +171,9 @@ static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) !isnullstartblock (irec->br_startblock); } +#define xfs_valid_startblock (ip, startblock) \ + ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip)) +void xfs_trim_extent (struct xfs_bmbt_irec *irec,xfs_fileoff_t bno,xfs_filblks_t len);int xfs_bmap_add_attrfork (struct xfs_inode *ip,int size,int rsvd);
fs/xfs/xfs_iomap.c +3 -3
@@ -58,7 +58,7 @@ xfs_bmbt_to_iomap( {struct xfs_mount *mp = ip->i_mount; -if (unlikely (!imap->br_startblock && !XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap->br_startblock)))return xfs_alert_fsblock_zero (ip, imap);if (imap->br_startblock ==HOLESTARTBLOCK ) {
@@ -297,7 +297,7 @@ xfs_iomap_write_direct( goto out_unlock; } -if (!(imap->br_startblock ||XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap->br_startblock))) error =xfs_alert_fsblock_zero (ip, imap); out_unlock:
@@ -814,7 +814,7 @@ xfs_iomap_write_unwritten(if (error)return error; -if (!(imap.br_startblock ||XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap.br_startblock)))return xfs_alert_fsblock_zero (ip, &imap);if ((numblks_fsb = imap.br_blockcount) ==0 ) {
Signature · 1 line
2.20.1
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 29 lines · Christoph Hellwig
Add a helper that validates the startblock is valid. This checks for a non-zero block on the main device, but skips that check for blocks on the realtime device.
Signed-off-by Christoph Hellwig <[email protected]>--- fs/xfs/libxfs/xfs_bmap.c | 2 +- fs/xfs/libxfs/xfs_bmap.h | 3 +++ fs/xfs/xfs_iomap.c | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-)fs/xfs/libxfs/xfs_bmap.c +1
-1@@ -4519,7 +4519,7 @@ xfs_bmapi_convert_delalloc(if (WARN_ON_ONCE (bma.blkno ==NULLFSBLOCK )) goto out_finish; error = -EFSCORRUPTED ; -if (WARN_ON_ONCE (!bma.got.br_startblock && !XFS_IS_REALTIME_INODE (ip))) +if (WARN_ON_ONCE (!xfs_valid_startblock (ip, bma.got.br_startblock))) goto out_finish;XFS_STATS_ADD (mp, xs_xstrat_bytes,XFS_FSB_TO_B (mp, bma.length));fs/xfs/libxfs/xfs_bmap.h +0
-0
fs/xfs/libxfs/xfs_bmap.h static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) · line 175
Quoted 5 lines
fs/xfs/libxfs/xfs_bmap.h +2
-0@@ -171,6 +171,9 @@ static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) !isnullstartblock (irec->br_startblock); } +#define xfs_valid_startblock (ip, startblock) \ + ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))
We have more robust validators for data/rtdev fsblock_t, so why not:
#define xfs_valid_startblock(ip, startblock) \ (XFS_IS_REALTIME_INODE(ip) ? xfs_verify_rtbno(startblock) : \ xfs_verify_fsbno(startblock))
and why not make it a static inline function too?
--D
Quoted 38 lines
fs/xfs/libxfs/xfs_bmap.h +1
-0+void xfs_trim_extent (struct xfs_bmbt_irec *irec,xfs_fileoff_t bno,xfs_filblks_t len);int xfs_bmap_add_attrfork (struct xfs_inode *ip,int size,int rsvd);fs/xfs/xfs_iomap.c +3
-3@@ -58,7 +58,7 @@ xfs_bmbt_to_iomap( {struct xfs_mount *mp = ip->i_mount; -if (unlikely (!imap->br_startblock && !XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap->br_startblock)))return xfs_alert_fsblock_zero (ip, imap);if (imap->br_startblock ==HOLESTARTBLOCK ) {@@ -297,7 +297,7 @@ xfs_iomap_write_direct( goto out_unlock; } -if (!(imap->br_startblock ||XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap->br_startblock))) error =xfs_alert_fsblock_zero (ip, imap); out_unlock:@@ -814,7 +814,7 @@ xfs_iomap_write_unwritten(if (error)return error; -if (!(imap.br_startblock ||XFS_IS_REALTIME_INODE (ip))) +if (unlikely (!xfs_valid_startblock (ip, imap.br_startblock)))return xfs_alert_fsblock_zero (ip, &imap);if ((numblks_fsb = imap.br_blockcount) ==0 ) {Signature · 1 line
2.20.1
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 11 lines · Darrick J. Wong
fs/xfs/libxfs/xfs_bmap.h static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) · line 175
Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.h +2
-0@@ -171,6 +171,9 @@ static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) !isnullstartblock (irec->br_startblock); } +#define xfs_valid_startblock (ip, startblock) \ + ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))We have more robust validators for data/rtdev
fsblock_t, so why not:
Quoted 5 lines
#define xfs_valid_startblock(ip, startblock) \ (XFS_IS_REALTIME_INODE(ip) ? xfs_verify_rtbno(startblock) : \ xfs_verify_fsbno(startblock))and why not make it a static inline function too?
I tried an inline function, but I could not find a header to place it that would actually easily compile everywhere... Maybe we should just make that a xfs_verify_bno(mp, startblock) and move that out of line such in a way that a smart compiler avoids the function call overhead for xfs_verify_rtbno / xfs_verify_fsbno. I'll take another stab at this.
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 16 lines · Christoph Hellwig
Quoted 15 lines · Darrick J. Wong
fs/xfs/libxfs/xfs_bmap.h static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) · line 175
Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.h +2
-0@@ -171,6 +171,9 @@ static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) !isnullstartblock (irec->br_startblock); } +#define xfs_valid_startblock (ip, startblock) \ + ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))We have more robust validators for data/rtdev
fsblock_t, so why not:#define xfs_valid_startblock(ip, startblock) \ (XFS_IS_REALTIME_INODE(ip) ? xfs_verify_rtbno(startblock) : \ xfs_verify_fsbno(startblock))
Quoted 8 lines
Quoted 1 line
and why not make it a static inline function too?
I tried an inline function, but I could not find a header to place it that would actually easily compile everywhere... Maybe we should just make that a
xfs_verify_bno(mp, startblock)and move that out of line such in a way that a smart compiler avoids the function call overhead forxfs_verify_rtbno/xfs_verify_fsbno. I'll take another stab at this.
So I looked into your suggestion, but xfs_verify_rtbno / xfs_verify_fsbno do a lot of validity checking, but they don't actually contain the check that was in the existing code. The bmap code just checks that there is a startblock of 0 for non-rt devices, probably this was added to find some old bug where a irec structure that was zeroed was returned.
So replacing it with xfs_verify_rtbno / xfs_verify_fsbno would not help in any way. But the big question is if keeping the 0 check is even worth it.
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 32 lines · Christoph Hellwig
Quoted 16 lines · Christoph Hellwig
Quoted 15 lines · Darrick J. Wong
fs/xfs/libxfs/xfs_bmap.h static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) · line 175
Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.h +2
-0@@ -171,6 +171,9 @@ static inline bool xfs_bmap_is_real_extent(struct xfs_bmbt_irec *irec) !isnullstartblock (irec->br_startblock); } +#define xfs_valid_startblock (ip, startblock) \ + ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))We have more robust validators for data/rtdev
fsblock_t, so why not:#define xfs_valid_startblock(ip, startblock) \ (XFS_IS_REALTIME_INODE(ip) ? xfs_verify_rtbno(startblock) : \ xfs_verify_fsbno(startblock))Quoted 8 lines
Quoted 1 line
and why not make it a static inline function too?
I tried an inline function, but I could not find a header to place it that would actually easily compile everywhere... Maybe we should just make that a
xfs_verify_bno(mp, startblock)and move that out of line such in a way that a smart compiler avoids the function call overhead forxfs_verify_rtbno/xfs_verify_fsbno. I'll take another stab at this.So I looked into your suggestion, but
xfs_verify_rtbno/xfs_verify_fsbnodo a lot of validity checking, but they don't actually contain the check that was in the existing code. The bmap code just checks that there is a startblock of 0 for non-rt devices, probably this was added to find some old bug where a irec structure that was zeroed was returned.
Quoted 3 lines
So replacing it with
xfs_verify_rtbno/xfs_verify_fsbnowould not help in any way. But the big question is if keeping the 0 check is even worth it.
It's been mildly helpful for noticing when my online/offline repair prototype code totally screws up, but at that point so much magic smoke is already pouring out everywhere that it's hard not to notice. :)
--D
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 3 lines · Darrick J. Wong
It's been mildly helpful for noticing when my online/offline repair prototype code totally screws up, but at that point so much magic smoke is already pouring out everywhere that it's hard not to notice. :)
That suggests to just keep the macro as I submitted it, maybe with a big fat comment explaining the usage.
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Darrick J. Wong
It's been mildly helpful for noticing when my online/offline repair prototype code totally screws up, but at that point so much magic smoke is already pouring out everywhere that it's hard not to notice. :)
That suggests to just keep the macro as I submitted it, maybe with a big fat comment explaining the usage.
Ok. Do you want to resubmit with a comment of your choosing, or let me write in whatever:
/*
* Check the mapping for obviously garbage allocations that could trash
* the filesystem immediately.
*/
?
--D
Re: [PATCH 1/3] xfs: add a xfs_valid_startblock helper
Quoted 9 lines · Darrick J. Wong
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Darrick J. Wong
It's been mildly helpful for noticing when my online/offline repair prototype code totally screws up, but at that point so much magic smoke is already pouring out everywhere that it's hard not to notice. :)
That suggests to just keep the macro as I submitted it, maybe with a big fat comment explaining the usage.
Quoted 7 lines
Ok. Do you want to resubmit with a comment of your choosing, or let me write in whatever:
/*
* Check the mapping for obviously garbage allocations that could trash
* the filesystem immediately.
*/
I was going to resend it, but now that you've written the comment for me feel free to just apply it with that added.
PATCH 2/3 xfs: cleanup xfs_fsb_to_db
In reply to Christoph HellwigThis function isn't a macro anymore, so remove various superflous braces, and explicit cast that is done implicitly due to the return value, use a normal if statement instead of trying to squeeze everything together.
--- fs/xfs/xfs_bmap_util.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/xfs_bmap_util.c +3 -3
@@ -39,9 +39,9 @@xfs_daddr_t xfs_fsb_to_db (struct xfs_inode *ip,xfs_fsblock_t fsb) { -return (XFS_IS_REALTIME_INODE (ip) ? \ - (xfs_daddr_t )XFS_FSB_TO_BB ((ip)->i_mount, (fsb)) : \ -XFS_FSB_TO_DADDR ((ip)->i_mount, (fsb))); +if (XFS_IS_REALTIME_INODE (ip)) +return XFS_FSB_TO_BB (ip->i_mount, fsb); +return XFS_FSB_TO_DADDR (ip->i_mount, fsb); }/*
Signature · 1 line
2.20.1
Re: [PATCH 2/3] xfs: cleanup xfs_fsb_to_db
Quoted 5 lines · Christoph Hellwig
This function isn't a macro anymore, so remove various superflous braces, and explicit cast that is done implicitly due to the return value, use a normal if statement instead of trying to squeeze everything together.
Signed-off-by Christoph Hellwig <[email protected]>
Looks ok,
--D
Quoted 24 lines
--- fs/xfs/xfs_bmap_util.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/xfs/xfs_bmap_util.c +3
-3@@ -39,9 +39,9 @@xfs_daddr_t xfs_fsb_to_db (struct xfs_inode *ip,xfs_fsblock_t fsb) { -return (XFS_IS_REALTIME_INODE (ip) ? \ - (xfs_daddr_t )XFS_FSB_TO_BB ((ip)->i_mount, (fsb)) : \ -XFS_FSB_TO_DADDR ((ip)->i_mount, (fsb))); +if (XFS_IS_REALTIME_INODE (ip)) +return XFS_FSB_TO_BB (ip->i_mount, fsb); +return XFS_FSB_TO_DADDR (ip->i_mount, fsb); }/* Signature · 1 line
2.20.1
PATCH 3/3 xfs: remove the unused XFS_ALLOC_USERDATA flag
In reply to Christoph Hellwig--- fs/xfs/libxfs/xfs_alloc.h | 7 +++---- fs/xfs/libxfs/xfs_bmap.c | 8 ++------ 2 files changed, 5 insertions(+), 10 deletions(-)
fs/xfs/libxfs/xfs_alloc.h +3 -4
@@ -81,10 +81,9 @@ typedef struct xfs_alloc_arg {/* * Defines for datatype */ -#define XFS_ALLOC_USERDATA (1 << 0)/* allocation is for user data*/ -#define XFS_ALLOC_INITIAL_USER_DATA (1 << 1)/* special case start of file */ -#define XFS_ALLOC_USERDATA_ZERO (1 << 2)/* zero extent on allocation */ -#define XFS_ALLOC_NOBUSY (1 << 3)/* Busy extents not allowed */ +#define XFS_ALLOC_INITIAL_USER_DATA (1 << 0)/* special case start of file */ +#define XFS_ALLOC_USERDATA_ZERO (1 << 1)/* zero extent on allocation */ +#define XFS_ALLOC_NOBUSY (1 << 2)/* Busy extents not allowed */ static inline bool xfs_alloc_is_userdata (int datatype)
fs/xfs/libxfs/xfs_bmap.c +2 -6
@@ -4042,12 +4042,8 @@ xfs_bmapi_allocate(*/ if (!(bma->flags &XFS_BMAPI_METADATA )) { bma->datatype =XFS_ALLOC_NOBUSY ; -if (whichfork ==XFS_DATA_FORK ) { -if (bma->offset ==0 ) - bma->datatype |=XFS_ALLOC_INITIAL_USER_DATA ; -else - bma->datatype |=XFS_ALLOC_USERDATA ; - } +if (whichfork ==XFS_DATA_FORK && bma->offset ==0 ) + bma->datatype |=XFS_ALLOC_INITIAL_USER_DATA ;if (bma->flags &XFS_BMAPI_ZERO ) bma->datatype |=XFS_ALLOC_USERDATA_ZERO ; }
Signature · 1 line
2.20.1
Re: [PATCH 3/3] xfs: remove the unused XFS_ALLOC_USERDATA flag
Quoted 1 line · Christoph Hellwig
Signed-off-by Christoph Hellwig <[email protected]>
Looks ok,
--D
Quoted 45 lines
--- fs/xfs/libxfs/xfs_alloc.h | 7 +++---- fs/xfs/libxfs/xfs_bmap.c | 8 ++------ 2 files changed, 5 insertions(+), 10 deletions(-)fs/xfs/libxfs/xfs_alloc.h +3
-4@@ -81,10 +81,9 @@ typedef struct xfs_alloc_arg {/* * Defines for datatype */ -#define XFS_ALLOC_USERDATA (1 << 0)/* allocation is for user data*/ -#define XFS_ALLOC_INITIAL_USER_DATA (1 << 1)/* special case start of file */ -#define XFS_ALLOC_USERDATA_ZERO (1 << 2)/* zero extent on allocation */ -#define XFS_ALLOC_NOBUSY (1 << 3)/* Busy extents not allowed */ +#define XFS_ALLOC_INITIAL_USER_DATA (1 << 0)/* special case start of file */ +#define XFS_ALLOC_USERDATA_ZERO (1 << 1)/* zero extent on allocation */ +#define XFS_ALLOC_NOBUSY (1 << 2)/* Busy extents not allowed */ static inline bool xfs_alloc_is_userdata (int datatype)fs/xfs/libxfs/xfs_bmap.c +2
-6@@ -4042,12 +4042,8 @@ xfs_bmapi_allocate(*/ if (!(bma->flags &XFS_BMAPI_METADATA )) { bma->datatype =XFS_ALLOC_NOBUSY ; -if (whichfork ==XFS_DATA_FORK ) { -if (bma->offset ==0 ) - bma->datatype |=XFS_ALLOC_INITIAL_USER_DATA ; -else - bma->datatype |=XFS_ALLOC_USERDATA ; - } +if (whichfork ==XFS_DATA_FORK && bma->offset ==0 ) + bma->datatype |=XFS_ALLOC_INITIAL_USER_DATA ;if (bma->flags &XFS_BMAPI_ZERO ) bma->datatype |=XFS_ALLOC_USERDATA_ZERO ; }Signature · 1 line
2.20.1