return error;
 }
 
+/*
+ * Care must be taken setting up the trim cursor as the perags may not have been
+ * initialised when the cursor is initialised. e.g. a clean mount which hasn't
+ * read in AGFs and the first operation run on the mounted fs is a trim. This
+ * can result in perag fields that aren't initialised until
+ * xfs_trim_gather_extents() calls xfs_alloc_read_agf() to lock down the AG for
+ * the free space search.
+ */
 struct xfs_trim_cur {
        xfs_agblock_t   start;
        xfs_extlen_t    count;
        if (error)
                goto out_trans_cancel;
 
+       /*
+        * First time through tcur->count will not have been initialised as
+        * pag->pagf_longest is not guaranteed to be valid before we read
+        * the AGF buffer above.
+        */
+       if (!tcur->count)
+               tcur->count = pag->pagf_longest;
+
        if (tcur->by_bno) {
                /* sub-AG discard request always starts at tcur->start */
                cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
 {
        struct xfs_trim_cur     tcur = {
                .start          = start,
-               .count          = pag->pagf_longest,
                .end            = end,
                .minlen         = minlen,
        };