xfs-4.19: fix bogus summary counters on mount
cover xfs-4.19: fix bogus summary counters on mount
Hi all,
This series enhances XFS' ability to detect and recover from obviously incorrect summary counts. Some of this is in response to recent bug reports about xfs allowing mounts with obviously incorrect counts, though the last two patches are intended to integrate with future online fsck features.
The first patch detects obviously bad summary counters at mount time, forces a recomputation of the counters after log recovery (if any) completes, and fails the mount if the counts are still obviously bad. The second and third patches deal with forcing a recalculation at the next mount if the counts are found to be slightly incorrect, which is a situation that will be detected by the fscounter online fsck in a subsequent patch series.
This series sits on top of the cleanups I sent earlier today.
Comments and questions are, as always, welcome.
--D
PATCH 1/3 xfs: detect and fix bad summary counts at mount
From: Darrick J. Wong <[email protected]>
Filippo Giunchedi complained that xfs doesn't even perform basic sanity checks of the fs summary counters at mount time. Therefore, recalculate the summary counters from the AGFs after log recovery if the counts were bad (or we had to recover the fs). Enhance the recalculation routine to fail the mount entirely if the new values are also obviously incorrect.
We use a mount state flag to record the "bad summary count" state so that the (subsequent) online fsck patches can detect subtlely incorrect counts and set the flag; clear it userspace asks for a repair; or force a recalculation at the next mount if nobody fixes it by unmount time.
--- fs/xfs/libxfs/xfs_sb.c | 21 +++++++++++-- fs/xfs/xfs_mount.c | 80 ++++++++++++++++++++++++++++++++---------------- fs/xfs/xfs_mount.h | 1 + 3 files changed, 73 insertions(+), 29 deletions(-)
fs/xfs/libxfs/xfs_sb.c +18 -3
@@ -804,6 +804,7 @@ xfs_initialize_perag_data(uint64_t bfree =0 ;uint64_t bfreelst =0 ;uint64_t btree =0 ; +uint64_t fdblocks;int error;for (index =0 ; index < agcount; index++) {
@@ -827,17 +828,31 @@ xfs_initialize_perag_data( btree += pag->pagf_btreeblks;xfs_perag_put (pag); } + fdblocks = bfree + bfreelst + btree; + +/* +* If the new summary counts are obviously incorrect, fail the +* mount operation because that implies the AGFs are also corrupt. +* Clear BAD_SUMMARY so that we don't unmount with a dirty log, which +* will prevent xfs_repair from fixing anything. +*/ +if (fdblocks > sbp->sb_dblocks || ifree > ialloc) { +xfs_alert (mp,"AGF corruption. Please run xfs_repair." ); + error = -EFSCORRUPTED ; + goto out; + }/* Overwrite incore superblock counters with just-read data */ spin_lock (&mp->m_sb_lock); sbp->sb_ifree = ifree; sbp->sb_icount = ialloc; - sbp->sb_fdblocks = bfree + bfreelst + btree; + sbp->sb_fdblocks = fdblocks;spin_unlock (&mp->m_sb_lock);xfs_reinit_percpu_counters (mp); - -return 0 ; +out: + mp->m_flags &= ~XFS_MOUNT_BAD_SUMMARY ; +return error; }/*
fs/xfs/xfs_mount.c +54 -26
92 lines not on this page · raw message
fs/xfs/xfs_mount.h +1 -0
@@ -202,6 +202,7 @@ typedef struct xfs_mount {must be synchronous except for space allocations */ #define XFS_MOUNT_UNMOUNTING (1ULL << 1)/* filesystem is unmounting */ +#define XFS_MOUNT_BAD_SUMMARY (1ULL << 2)/* summary counters are bad */ #define XFS_MOUNT_WAS_CLEAN (1ULL << 3)#define XFS_MOUNT_FS_SHUTDOWN (1ULL << 4)/* atomic stop of all filesystem operations, typically for
Re: [PATCH 1/3] xfs: detect and fix bad summary counts at mount
Looks fine:
PATCH 2/3 xfs: refactor unmount record write
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Refactor the writing of the unmount record into a separate helper. No functionality changes.
--- fs/xfs/xfs_log.c | 130 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 62 deletions(-)
fs/xfs/xfs_log.c +68 -62
@@ -826,6 +826,73 @@ xfs_log_mount_cancel(* deallocation must not be done until source-end. */ +/* Actually write the unmount record to disk. */ +static void +xfs_log_write_unmount_record ( +struct xfs_mount *mp) +{ +/* the data section must be 32 bit size aligned */ +struct { +uint16_t magic; +uint16_t pad1; +uint32_t pad2;/* may as well make it 64 bits */ + } magic = { + .magic =XLOG_UNMOUNT_TYPE , + }; +struct xfs_log_iovec reg = { + .i_addr = &magic, + .i_len =sizeof (magic), + .i_type =XLOG_REG_TYPE_UNMOUNT , + }; +struct xfs_log_vec vec = { + .lv_niovecs =1 , + .lv_iovecp = ®, + }; +struct xlog *log = mp->m_log; +struct xlog_in_core *iclog; +struct xlog_ticket *tic =NULL ; +xfs_lsn_t lsn; +int error; + + error =xfs_log_reserve (mp,600 ,1 , &tic,XFS_LOG ,0 ); +if (error) + goto out_err; + +/* remove inited flag, and account for space used */ + tic->t_flags =0 ; + tic->t_curr_res -=sizeof (magic); + error =xlog_write (log, &vec, tic, &lsn,NULL ,XLOG_UNMOUNT_TRANS ); +/* +* At this point, we're umounting anyway, +* so there's no point in transitioning log state +* to IOERROR. Just continue... +*/ +out_err: +if (error) +xfs_alert (mp,"%s: unmount record failed" , __func__); + +spin_lock (&log->l_icloglock); + iclog = log->l_iclog; +atomic_inc (&iclog->ic_refcnt); +xlog_state_want_sync (log, iclog); +spin_unlock (&log->l_icloglock); + error =xlog_state_release_iclog (log, iclog); + +spin_lock (&log->l_icloglock); +if (!(iclog->ic_state ==XLOG_STATE_ACTIVE || + iclog->ic_state ==XLOG_STATE_DIRTY ) && + !XLOG_FORCED_SHUTDOWN (log)) +xlog_wait (&iclog->ic_force_wait, &log->l_icloglock); +else +spin_unlock (&log->l_icloglock); + +if (tic) { +trace_xfs_log_umount_write (log, tic); +xlog_ungrant_log_space (log, tic); +xfs_log_ticket_put (tic); + } +} +/* * Unmount record used to have a string "Unmount filesystem--" in the * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
@@ -842,8 +909,6 @@ xfs_log_unmount_write(xfs_mount_t *mp)#ifdef DEBUG xlog_in_core_t *first_iclog;#endif -xlog_ticket_t *tic =NULL ; -xfs_lsn_t lsn;int error;/*
@@ -870,66 +935,7 @@ xfs_log_unmount_write(xfs_mount_t *mp) } while (iclog != first_iclog);#endif if (! (XLOG_FORCED_SHUTDOWN (log))) { - error =xfs_log_reserve (mp,600 ,1 , &tic,XFS_LOG ,0 ); -if (!error) { -/* the data section must be 32 bit size aligned */ -struct { -uint16_t magic; -uint16_t pad1; -uint32_t pad2;/* may as well make it 64 bits */ - } magic = { - .magic =XLOG_UNMOUNT_TYPE , - }; -struct xfs_log_iovec reg = { - .i_addr = &magic, - .i_len =sizeof (magic), - .i_type =XLOG_REG_TYPE_UNMOUNT , - }; -struct xfs_log_vec vec = { - .lv_niovecs =1 , - .lv_iovecp = ®, - }; - -/* remove inited flag, and account for space used */ - tic->t_flags =0 ; - tic->t_curr_res -=sizeof (magic); - error =xlog_write (log, &vec, tic, &lsn, -NULL ,XLOG_UNMOUNT_TRANS ); -/* -* At this point, we're umounting anyway, -* so there's no point in transitioning log state -* to IOERROR. Just continue... -*/ - } - -if (error) -xfs_alert (mp,"%s: unmount record failed" , __func__); - - -spin_lock (&log->l_icloglock); - iclog = log->l_iclog; -atomic_inc (&iclog->ic_refcnt); -xlog_state_want_sync (log, iclog); -spin_unlock (&log->l_icloglock); - error =xlog_state_release_iclog (log, iclog); - -spin_lock (&log->l_icloglock); -if (!(iclog->ic_state ==XLOG_STATE_ACTIVE || - iclog->ic_state ==XLOG_STATE_DIRTY )) { -if (!XLOG_FORCED_SHUTDOWN (log)) { -xlog_wait (&iclog->ic_force_wait, - &log->l_icloglock); - }else { -spin_unlock (&log->l_icloglock); - } - }else { -spin_unlock (&log->l_icloglock); - } -if (tic) { -trace_xfs_log_umount_write (log, tic); -xlog_ungrant_log_space (log, tic); -xfs_log_ticket_put (tic); - } +xfs_log_write_unmount_record (mp); }else {/* * We're already in forced_shutdown mode, couldn't
Re: [PATCH 2/3] xfs: refactor unmount record write
Looks generally fine, but some tiny nitpicks:
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 841
Quoted 8 lines
fs/xfs/xfs_log.c +8
-0+/* the data section must be 32 bit size aligned */ +struct { +uint16_t magic; +uint16_t pad1; +uint32_t pad2;/* may as well make it 64 bits */ + } magic = { + .magic =XLOG_UNMOUNT_TYPE , + };
Can we please give this structure type a name and move it to xfs_log_format.h? They way this had been done always irked me.
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 869
Quoted 5 lines
fs/xfs/xfs_log.c +5
-0+/* +* At this point, we're umounting anyway, +* so there's no point in transitioning log state +* to IOERROR. Just continue... +*/
Use up all 80 lines, please.
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 887
Quoted 6 lines
fs/xfs/xfs_log.c +6
-0+if (!(iclog->ic_state ==XLOG_STATE_ACTIVE || + iclog->ic_state ==XLOG_STATE_DIRTY ) && + !XLOG_FORCED_SHUTDOWN (log)) +xlog_wait (&iclog->ic_force_wait, &log->l_icloglock); +else +spin_unlock (&log->l_icloglock);
switch (iclog->ic_state) {
default:
if (!XLOG_FORCED_SHUTDOWN(log)) {
xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
break;
}
/*FALLHRU*/
case XLOG_STATE_ACTIVE:
case XLOG_STATE_DIRTY:
spin_unlock(&log->l_icloglock);
break;Re: [PATCH 2/3] xfs: refactor unmount record write
Quoted 13 lines · Christoph Hellwig
Looks generally fine, but some tiny nitpicks:
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 841
Quoted 8 lines
fs/xfs/xfs_log.c +8
-0+/* the data section must be 32 bit size aligned */ +struct { +uint16_t magic; +uint16_t pad1; +uint32_t pad2;/* may as well make it 64 bits */ + } magic = { + .magic =XLOG_UNMOUNT_TYPE , + };Can we please give this structure type a name and move it to
xfs_log_format.h? They way this had been done always irked me.
Ok. It gets written to disk, so that makes sense.
Quoted 18 lines
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 869
Quoted 5 lines
fs/xfs/xfs_log.c +5
-0+/* +* At this point, we're umounting anyway, +* so there's no point in transitioning log state +* to IOERROR. Just continue... +*/ Use up all 80 lines, please.
fs/xfs/xfs_log.c xfs_log_mount_cancel( · line 887
Quoted 6 lines
fs/xfs/xfs_log.c +6
-0+if (!(iclog->ic_state ==XLOG_STATE_ACTIVE || + iclog->ic_state ==XLOG_STATE_DIRTY ) && + !XLOG_FORCED_SHUTDOWN (log)) +xlog_wait (&iclog->ic_force_wait, &log->l_icloglock); +else +spin_unlock (&log->l_icloglock);switch (iclog->ic_state) { default: if (!XLOG_FORCED_SHUTDOWN(log)) {
Quoted 8 lines
xlog_wait(&iclog->ic_force_wait, &log->l_icloglock); break; } /*FALLHRU*/ case XLOG_STATE_ACTIVE: case XLOG_STATE_DIRTY: spin_unlock(&log->l_icloglock); break;
<nod>
--D
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v2 2/3 xfs: refactor unmount record write
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Refactor the writing of the unmount record into a separate helper. No functionality changes.
---
v2: various code cleanups and move the structure header definition
--- fs/xfs/libxfs/xfs_log_format.h | 13 ++++ fs/xfs/xfs_log.c | 131 +++++++++++++++++++++------------------- 2 files changed, 82 insertions(+), 62 deletions(-)
fs/xfs/libxfs/xfs_log_format.h +13 -0
@@ -77,6 +77,19 @@ static inline uint xlog_get_cycle(char *ptr)#define XLOG_UNMOUNT_TYPE 0x556e/* Un for Unmount */ +/* +* Log item for unmount records. +* +* The unmount record used to have a string "Unmount filesystem--" in the +* data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE). +* We just write the magic number now; see xfs_log_unmount_write. +*/ +struct xfs_unmount_log_format { +uint16_t magic;/* XLOG_UNMOUNT_TYPE */ +uint16_t pad1; +uint32_t pad2;/* may as well make it 64 bits */ +}; +/* Region types for iovec's i_type */ #define XLOG_REG_TYPE_BFORMAT 1#define XLOG_REG_TYPE_BCHUNK 2
fs/xfs/xfs_log.c +69 -62
149 lines not on this page · raw message
Re: [PATCH v2 2/3] xfs: refactor unmount record write
Quoted 6 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
Refactor the writing of the unmount record into a separate helper. No functionality changes.
Signed-off-by Darrick J. Wong <[email protected]>
Looks good,
PATCH 3/3 xfs: force summary counter recalc at next mount
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Use the "bad summary count" mount flag from the previous patch to skip writing the unmount record to force log recovery at the next mount, which will recalculate the summary counters for us.
--- fs/xfs/libxfs/xfs_errortag.h | 4 +++- fs/xfs/xfs_error.c | 3 +++ fs/xfs/xfs_log.c | 16 +++++++++++++++- fs/xfs/xfs_mount.c | 13 +++++++++++++ fs/xfs/xfs_mount.h | 1 + 5 files changed, 35 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_errortag.h +3 -1
@@ -53,7 +53,8 @@#define XFS_ERRTAG_LOG_ITEM_PIN 30#define XFS_ERRTAG_BUF_LRU_REF 31#define XFS_ERRTAG_FORCE_SCRUB_REPAIR 32 -#define XFS_ERRTAG_MAX 33 +#define XFS_ERRTAG_FORCE_SUMMARY_RECALC 33 +#define XFS_ERRTAG_MAX 34/* * Random factors for above tags, 1 means always, 2 means 1/2 time, etc.
@@ -91,5 +92,6 @@#define XFS_RANDOM_LOG_ITEM_PIN 1#define XFS_RANDOM_BUF_LRU_REF 2#define XFS_RANDOM_FORCE_SCRUB_REPAIR 1 +#define XFS_RANDOM_FORCE_SUMMARY_RECALC 1#endif /* __XFS_ERRORTAG_H_ */
fs/xfs/xfs_error.c +3 -0
@@ -50,6 +50,7 @@ static unsigned int xfs_errortag_random_default[] = {XFS_RANDOM_LOG_ITEM_PIN ,XFS_RANDOM_BUF_LRU_REF ,XFS_RANDOM_FORCE_SCRUB_REPAIR , + XFS_RANDOM_FORCE_SUMMARY_RECALC, }; struct xfs_errortag_attr {
@@ -157,6 +158,7 @@ XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);XFS_ERRORTAG_ATTR_RW (log_item_pin,XFS_ERRTAG_LOG_ITEM_PIN );XFS_ERRORTAG_ATTR_RW (buf_lru_ref,XFS_ERRTAG_BUF_LRU_REF );XFS_ERRORTAG_ATTR_RW (force_repair,XFS_ERRTAG_FORCE_SCRUB_REPAIR ); +XFS_ERRORTAG_ATTR_RW (bad_summary,XFS_ERRTAG_FORCE_SUMMARY_RECALC );static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (noerror),
@@ -192,6 +194,7 @@ static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (log_item_pin),XFS_ERRORTAG_ATTR_LIST (buf_lru_ref),XFS_ERRORTAG_ATTR_LIST (force_repair), +XFS_ERRORTAG_ATTR_LIST (bad_summary), NULL, };
fs/xfs/xfs_log.c +15 -1
@@ -852,16 +852,30 @@ xfs_log_write_unmount_record(struct xlog_in_core *iclog;struct xlog_ticket *tic =NULL ;xfs_lsn_t lsn; +uint flags =XLOG_UNMOUNT_TRANS ;int error; error =xfs_log_reserve (mp,600 ,1 , &tic,XFS_LOG ,0 );if (error) goto out_err; +/* +* If we think the summary counters are bad, clear the unmount header +* flag in the unmount record so that the summary counters will be +* recalculated during log recovery at next mount. Refer to +* xlog_check_unmount_rec for more details. +*/ +if (XFS_TEST_ERROR ((mp->m_flags &XFS_MOUNT_BAD_SUMMARY ), mp, +XFS_ERRTAG_FORCE_SUMMARY_RECALC )) { +xfs_alert (mp,"%s: will fix summary counters at next mount" , + __func__); + flags &= ~XLOG_UNMOUNT_TRANS ; + } +/* remove inited flag, and account for space used */ tic->t_flags =0 ; tic->t_curr_res -=sizeof (magic); - error =xlog_write (log, &vec, tic, &lsn,NULL ,XLOG_UNMOUNT_TRANS ); + error =xlog_write (log, &vec, tic, &lsn,NULL , flags);/* * At this point, we're umounting anyway, * so there's no point in transitioning log state
fs/xfs/xfs_mount.c +13 -0
@@ -1423,3 +1423,16 @@ xfs_dev_is_read_only( }return 0 ; } + +/* Force the summary counters to be recalculated at next mount. */ +void +xfs_force_summary_recalc ( +struct xfs_mount *mp) +{ +if (!xfs_sb_version_haslazysbcount (&mp->m_sb)) +return ; + +spin_lock (&mp->m_sb_lock); + mp->m_flags |=XFS_MOUNT_BAD_SUMMARY ; +spin_unlock (&mp->m_sb_lock); +}
fs/xfs/xfs_mount.h +1 -0
@@ -435,5 +435,6 @@ int xfs_zero_extent(struct xfs_inode *ip, xfs_fsblock_t start_fsb,struct xfs_error_cfg *xfs_error_get_cfg (struct xfs_mount *mp,int error_class,int error); +void xfs_force_summary_recalc (struct xfs_mount *mp);#endif /* __XFS_MOUNT_H__ */
Re: [PATCH 3/3] xfs: force summary counter recalc at next mount
Looks good:
I assume we are going to get a test case ASAP that is going to use this?
Re: [PATCH 3/3] xfs: force summary counter recalc at next mount
Quoted 5 lines · Christoph Hellwig
Looks good:
Reviewed-by Christoph Hellwig <[email protected]>I assume we are going to get a test case ASAP that is going to use this?
DOH. I was planning to cram that on as patch 4/3 and forgot (blah blah conference blah). Will do that now.
--D
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
[RFC PATCH 4/3] xfstests: test mount time summary counter check and fix
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
Tests to ensure that the xfs mount code can detect obviously bad fs summary counters at mount time and fix them.
--- tests/xfs/722 | 58 +++++++++++++++++++++++++++++++++++++++++++++++ tests/xfs/722.out | 4 +++ tests/xfs/723 | 58 +++++++++++++++++++++++++++++++++++++++++++++++ tests/xfs/723.out | 4 +++ tests/xfs/724 | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/xfs/724.out | 4 +++ tests/xfs/group | 3 ++ 7 files changed, 197 insertions(+) create mode 100755 tests/xfs/722 create mode 100644 tests/xfs/722.out create mode 100755 tests/xfs/723 create mode 100644 tests/xfs/723.out create mode 100755 tests/xfs/724 create mode 100644 tests/xfs/724.out
tests/xfs/722 new file +58 -0
@@ -0,0 +1,58 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2018 Oracle. All Rights Reserved. +# +# FS QA Test 722 +# +# Test detection & fixing of bad summary block counts at mount time. +# +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap '_cleanup; exit $status' 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter + +# remove previous $seqres.full before test +rm -f $seqres.full + +# real QA test starts here + +# Modify as appropriate. +_supported_fs xfs +_supported_os Linux +_require_scratch + +echo "Format and mount" +_scratch_mkfs > $seqres.full 2>&1 +_scratch_mount >> $seqres.full 2>&1 +echo "test file" > $SCRATCH_MNT/testfile + +echo "Fuzz fdblocks" +_scratch_unmount +dblocks=$(_scratch_xfs_get_metadata_field dblocks 'sb 0') +_scratch_xfs_set_metadata_field fdblocks $((dblocks * 2)) 'sb 0' > $seqres.full 2>&1 + +echo "Detection and Correction" +_scratch_mount >> $seqres.full 2>&1 +avail=$(stat -c '%a' -f $SCRATCH_MNT) +total=$(stat -c '%b' -f $SCRATCH_MNT) +echo "avail: $avail" >> $seqres.full +echo "total: $total" >> $seqres.full +test "$avail" -gt "$total" && echo "free space bad: $avail > $total" + +# success, all done +status=0 +exit
tests/xfs/722.out new file +4 -0
@@ -0,0 +1,4 @@ +QA output created by 722 +Format and mount +Fuzz fdblocks +Detection and Correction
tests/xfs/723 new file +58 -0
58 lines not on this page · raw message
tests/xfs/723.out new file +4 -0
@@ -0,0 +1,4 @@ +QA output created by 723 +Format and mount +Fuzz ifree +Detection and Correction
tests/xfs/724 new file +66 -0
66 lines not on this page · raw message
tests/xfs/724.out new file +4 -0
@@ -0,0 +1,4 @@ +QA output created by 724 +Format and mount +Fuzz fdblocks and btreeblks +Detection and Correction
tests/xfs/group +3 -0
@@ -497,4 +497,7 @@ 719 auto quick mkfs 720 auto quick mkfs 721 auto quick mkfs +722 auto quick fuzz +723 auto quick fuzz +724 auto quick fuzz 903 mount auto quick stress