diff --git a/fs/xfs/xfs_discard.c b/fs/xfs/xfs_discard.c index 906221ffe9ba..8120f136af93 100644 --- a/fs/xfs/xfs_discard.c +++ b/fs/xfs/xfs_discard.c @@ -155,6 +155,8 @@ struct xfs_trim_cur { xfs_agblock_t end; xfs_extlen_t minlen; bool by_bno; + /* restart point (with count) for the by-length walk */ + xfs_agblock_t restart_bno; }; static int @@ -198,14 +200,15 @@ xfs_trim_gather_extents( error = xfs_alloc_lookup_le(cur, tcur->start, 0, &i); if (!error && !i) error = xfs_alloc_lookup_ge(cur, tcur->start, 0, &i); - } else if (tcur->start == 0) { + } else if (tcur->restart_bno == NULLAGBLOCK) { /* first time through a by-len starts with max length */ cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag); error = xfs_alloc_lookup_ge(cur, 0, tcur->count, &i); } else { /* nth time through a by-len starts where we left off */ cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag); - error = xfs_alloc_lookup_le(cur, tcur->start, tcur->count, &i); + error = xfs_alloc_lookup_le(cur, tcur->restart_bno, + tcur->count, &i); } if (error) goto out_del_cursor; @@ -235,9 +238,15 @@ xfs_trim_gather_extents( if (--batch <= 0) { /* * Update the cursor to point at this extent so we - * restart the next batch from this extent. + * restart the next batch from this extent. The + * by-length walk does not visit extents in block + * order, so it must keep the start of the range and + * restart from restart_bno instead. */ - tcur->start = fbno; + if (tcur->by_bno) + tcur->start = fbno; + else + tcur->restart_bno = fbno; tcur->count = flen; break; } @@ -340,6 +349,7 @@ xfs_trim_perag_extents( .start = start, .end = end, .minlen = minlen, + .restart_bno = NULLAGBLOCK, }; int error = 0;