/* Where do we start the traversal? */
        xfs_ino_t                       startino;
 
+       /* What was the last inode number we saw when iterating the inobt? */
+       xfs_ino_t                       lastino;
+
        /* Array of inobt records we cache. */
        struct xfs_inobt_rec_incore     *recs;
 
        if (XFS_IS_CORRUPT(mp, *has_more != 1))
                return -EFSCORRUPTED;
 
+       iwag->lastino = XFS_AGINO_TO_INO(mp, agno,
+                               irec->ir_startino + XFS_INODES_PER_CHUNK - 1);
+
        /*
         * If the LE lookup yielded an inobt record before the cursor position,
         * skip it and see if there's another one after it.
        struct xfs_mount                *mp = iwag->mp;
        struct xfs_trans                *tp = iwag->tp;
        struct xfs_inobt_rec_incore     *irec;
-       xfs_agino_t                     restart;
+       xfs_agino_t                     next_agino;
        int                             error;
 
+       next_agino = XFS_INO_TO_AGINO(mp, iwag->lastino) + 1;
+
        ASSERT(iwag->nr_recs > 0);
 
        /* Delete cursor but remember the last record we cached... */
        xfs_iwalk_del_inobt(tp, curpp, agi_bpp, 0);
        irec = &iwag->recs[iwag->nr_recs - 1];
-       restart = irec->ir_startino + XFS_INODES_PER_CHUNK - 1;
+       ASSERT(next_agino == irec->ir_startino + XFS_INODES_PER_CHUNK);
 
        error = xfs_iwalk_ag_recs(iwag);
        if (error)
        if (error)
                return error;
 
-       return xfs_inobt_lookup(*curpp, restart, XFS_LOOKUP_GE, has_more);
+       return xfs_inobt_lookup(*curpp, next_agino, XFS_LOOKUP_GE, has_more);
 }
 
 /* Walk all inodes in a single AG, from @iwag->startino to the end of the AG. */
 
        while (!error && has_more) {
                struct xfs_inobt_rec_incore     *irec;
+               xfs_ino_t                       rec_fsino;
 
                cond_resched();
                if (xfs_pwork_want_abort(&iwag->pwork))
                if (error || !has_more)
                        break;
 
+               /* Make sure that we always move forward. */
+               rec_fsino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino);
+               if (iwag->lastino != NULLFSINO &&
+                   XFS_IS_CORRUPT(mp, iwag->lastino >= rec_fsino)) {
+                       error = -EFSCORRUPTED;
+                       goto out;
+               }
+               iwag->lastino = rec_fsino + XFS_INODES_PER_CHUNK - 1;
+
                /* No allocated inodes in this chunk; skip it. */
                if (iwag->skip_empty && irec->ir_freecount == irec->ir_count) {
                        error = xfs_btree_increment(cur, 0, &has_more);
                .trim_start     = 1,
                .skip_empty     = 1,
                .pwork          = XFS_PWORK_SINGLE_THREADED,
+               .lastino        = NULLFSINO,
        };
        xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
        int                     error;
                iwag->data = data;
                iwag->startino = startino;
                iwag->sz_recs = xfs_iwalk_prefetch(inode_records);
+               iwag->lastino = NULLFSINO;
                xfs_pwork_queue(&pctl, &iwag->pwork);
                startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
                if (flags & XFS_INOBT_WALK_SAME_AG)
                .startino       = startino,
                .sz_recs        = xfs_inobt_walk_prefetch(inobt_records),
                .pwork          = XFS_PWORK_SINGLE_THREADED,
+               .lastino        = NULLFSINO,
        };
        xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
        int                     error;