{
        xfs_mount_t             *mp = ip->i_mount;
        xfs_off_t               count;
-       xfs_filblks_t           allocated_fsb;
        xfs_filblks_t           allocatesize_fsb;
        xfs_extlen_t            extsz, temp;
        xfs_fileoff_t           startoffset_fsb;
        xfs_fileoff_t           endoffset_fsb;
-       int                     nimaps;
        int                     rt;
        xfs_trans_t             *tp;
        xfs_bmbt_irec_t         imaps[1], *imapp;
 
        count = len;
        imapp = &imaps[0];
-       nimaps = 1;
        startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
        endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
        allocatesize_fsb = endoffset_fsb - startoffset_fsb;
        while (allocatesize_fsb && !error) {
                xfs_fileoff_t   s, e;
                unsigned int    dblocks, rblocks, resblks;
+               int             nimaps = 1;
 
                /*
                 * Determine space reservations for data/realtime.
                if (error)
                        break;
 
-               allocated_fsb = imapp->br_blockcount;
-
-               if (nimaps == 0) {
-                       error = -ENOSPC;
-                       break;
+               /*
+                * If the allocator cannot find a single free extent large
+                * enough to cover the start block of the requested range,
+                * xfs_bmapi_write will return 0 but leave *nimaps set to 0.
+                *
+                * In that case we simply need to keep looping with the same
+                * startoffset_fsb so that one of the following allocations
+                * will eventually reach the requested range.
+                */
+               if (nimaps) {
+                       startoffset_fsb += imapp->br_blockcount;
+                       allocatesize_fsb -= imapp->br_blockcount;
                }
-
-               startoffset_fsb += allocated_fsb;
-               allocatesize_fsb -= allocated_fsb;
        }
 
        return error;