Skip to content

Commit 23be716

Browse files
Dave Chinnercmaiolino
authored andcommitted
xfs: don't assume perags are initialised when trimming AGs
When running fstrim immediately after mounting a V4 filesystem, the fstrim fails to trim all the free space in the filesystem. It only trims the first extent in the by-size free space tree in each AG and then returns. If a second fstrim is then run, it runs correctly and the entire free space in the filesystem is iterated and discarded correctly. The problem lies in the setup of the trim cursor - it assumes that pag->pagf_longest is valid without either reading the AGF first or checking if xfs_perag_initialised_agf(pag) is true or not. As a result, when a filesystem is mounted without reading the AGF (e.g. a clean mount on a v4 filesystem) and the first operation is a fstrim call, pag->pagf_longest is zero and so the free extent search starts at the wrong end of the by-size btree and exits after discarding the first record in the tree. Fix this by deferring the initialisation of tcur->count to after we have locked the AGF and guaranteed that the perag is properly initialised. We trigger this on tcur->count == 0 after locking the AGF, as this will only occur on the first call to xfs_trim_gather_extents() for each AG. If we need to iterate, tcur->count will be set to the length of the record we need to restart at, so we can use this to ensure we only sample a valid pag->pagf_longest value for the iteration. Signed-off-by: Dave Chinner <[email protected]> Reviewed-by: Bill O'Donnell <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Fixes: 89cfa89 ("xfs: reduce AGF hold times during fstrim operations") Cc: <[email protected]> # v6.6 Signed-off-by: Carlos Maiolino <[email protected]>
1 parent bfecc40 commit 23be716

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

fs/xfs/xfs_discard.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ xfs_discard_extents(
167167
return error;
168168
}
169169

170+
/*
171+
* Care must be taken setting up the trim cursor as the perags may not have been
172+
* initialised when the cursor is initialised. e.g. a clean mount which hasn't
173+
* read in AGFs and the first operation run on the mounted fs is a trim. This
174+
* can result in perag fields that aren't initialised until
175+
* xfs_trim_gather_extents() calls xfs_alloc_read_agf() to lock down the AG for
176+
* the free space search.
177+
*/
170178
struct xfs_trim_cur {
171179
xfs_agblock_t start;
172180
xfs_extlen_t count;
@@ -204,6 +212,14 @@ xfs_trim_gather_extents(
204212
if (error)
205213
goto out_trans_cancel;
206214

215+
/*
216+
* First time through tcur->count will not have been initialised as
217+
* pag->pagf_longest is not guaranteed to be valid before we read
218+
* the AGF buffer above.
219+
*/
220+
if (!tcur->count)
221+
tcur->count = pag->pagf_longest;
222+
207223
if (tcur->by_bno) {
208224
/* sub-AG discard request always starts at tcur->start */
209225
cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
@@ -350,7 +366,6 @@ xfs_trim_perag_extents(
350366
{
351367
struct xfs_trim_cur tcur = {
352368
.start = start,
353-
.count = pag->pagf_longest,
354369
.end = end,
355370
.minlen = minlen,
356371
};

0 commit comments

Comments
 (0)