xfs_filblks_t           rlen;
        xfs_filblks_t           unmap_len;
        xfs_off_t               newlen;
+       int64_t                 qres;
        int                     error;
 
        unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
        xfs_ilock(ip, XFS_ILOCK_EXCL);
        xfs_trans_ijoin(tp, ip, 0);
 
-       /* If we're not just clearing space, then do we have enough quota? */
-       if (real_extent) {
-               error = xfs_trans_reserve_quota_nblks(tp, ip,
-                               irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
-               if (error)
-                       goto out_cancel;
-       }
+       /*
+        * Reserve quota for this operation.  We don't know if the first unmap
+        * in the dest file will cause a bmap btree split, so we always reserve
+        * at least enough blocks for that split.  If the extent being mapped
+        * in is written, we need to reserve quota for that too.
+        */
+       qres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
+       if (real_extent)
+               qres += irec->br_blockcount;
+       error = xfs_trans_reserve_quota_nblks(tp, ip, qres, 0,
+                       XFS_QMOPT_RES_REGBLKS);
+       if (error)
+               goto out_cancel;
 
        trace_xfs_reflink_remap(ip, irec->br_startoff,
                                irec->br_blockcount, irec->br_startblock);