if (args->value || xfs_inode_hasattr(dp)) {
                error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
                                XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
+               if (error == -EFBIG)
+                       error = xfs_iext_count_upgrade(args->trans, dp,
+                                       XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
                if (error)
                        goto out_trans_cancel;
        }
 
                return error;
 
        xfs_ilock(ip, XFS_ILOCK_EXCL);
+       xfs_trans_ijoin(tp, ip, 0);
 
        error = xfs_iext_count_may_overflow(ip, whichfork,
                        XFS_IEXT_ADD_NOSPLIT_CNT);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip,
+                               XFS_IEXT_ADD_NOSPLIT_CNT);
        if (error)
                goto out_trans_cancel;
 
-       xfs_trans_ijoin(tp, ip, 0);
-
        if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
            bma.got.br_startoff > offset_fsb) {
                /*
 
 #define XFS_MAX_EXTCNT_DATA_FORK_SMALL ((xfs_extnum_t)((1ULL << 31) - 1))
 #define XFS_MAX_EXTCNT_ATTR_FORK_SMALL ((xfs_extnum_t)((1ULL << 15) - 1))
 
+/*
+ * When we upgrade an inode to the large extent counts, the maximum value by
+ * which the extent count can increase is bound by the change in size of the
+ * on-disk field. No upgrade operation should ever be adding more than a few
+ * tens of extents, so if we get a really large value it is a sign of a code bug
+ * or corruption.
+ */
+#define XFS_MAX_EXTCNT_UPGRADE_NR      \
+       min(XFS_MAX_EXTCNT_ATTR_FORK_LARGE - XFS_MAX_EXTCNT_ATTR_FORK_SMALL,    \
+           XFS_MAX_EXTCNT_DATA_FORK_LARGE - XFS_MAX_EXTCNT_DATA_FORK_SMALL)
+
 /*
  * Inode minimum and maximum sizes.
  */
 
 
        return 0;
 }
+
+/*
+ * Upgrade this inode's extent counter fields to be able to handle a potential
+ * increase in the extent count by nr_to_add.  Normally this is the same
+ * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
+ */
+int
+xfs_iext_count_upgrade(
+       struct xfs_trans        *tp,
+       struct xfs_inode        *ip,
+       uint                    nr_to_add)
+{
+       ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
+
+       if (!xfs_has_large_extent_counts(ip->i_mount) ||
+           xfs_inode_has_large_extent_counts(ip) ||
+           XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
+               return -EFBIG;
+
+       ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
+       xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
+
+       return 0;
+}
 
 int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
 int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
                int nr_to_add);
+int xfs_iext_count_upgrade(struct xfs_trans *tp, struct xfs_inode *ip,
+               uint nr_to_add);
 
 /* returns true if the fork has extents but they are not read in yet. */
 static inline bool xfs_need_iread_extents(struct xfs_ifork *ifp)
 
                iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
 
        error = xfs_iext_count_may_overflow(ip, whichfork, iext_delta);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip, iext_delta);
        if (error)
                goto err_cancel;
 
 
 
                error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                                XFS_IEXT_ADD_NOSPLIT_CNT);
+               if (error == -EFBIG)
+                       error = xfs_iext_count_upgrade(tp, ip,
+                                       XFS_IEXT_ADD_NOSPLIT_CNT);
                if (error)
                        goto error;
 
 
        error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                        XFS_IEXT_PUNCH_HOLE_CNT);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
        if (error)
                goto out_trans_cancel;
 
 
        error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                        XFS_IEXT_PUNCH_HOLE_CNT);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
        if (error)
                goto out_trans_cancel;
 
                                error = xfs_iext_count_may_overflow(ip,
                                                XFS_DATA_FORK,
                                                XFS_IEXT_SWAP_RMAP_CNT);
+                               if (error == -EFBIG)
+                                       error = xfs_iext_count_upgrade(tp, ip,
+                                                       XFS_IEXT_SWAP_RMAP_CNT);
                                if (error)
                                        goto out;
                        }
                                error = xfs_iext_count_may_overflow(tip,
                                                XFS_DATA_FORK,
                                                XFS_IEXT_SWAP_RMAP_CNT);
+                               if (error == -EFBIG)
+                                       error = xfs_iext_count_upgrade(tp, ip,
+                                                       XFS_IEXT_SWAP_RMAP_CNT);
                                if (error)
                                        goto out;
                        }
 
 
        error = xfs_iext_count_may_overflow(quotip, XFS_DATA_FORK,
                        XFS_IEXT_ADD_NOSPLIT_CNT);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, quotip,
+                               XFS_IEXT_ADD_NOSPLIT_CNT);
        if (error)
                goto err_cancel;
 
 
                return error;
 
        error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip, nr_exts);
        if (error)
                goto out_trans_cancel;
 
 
                error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                                XFS_IEXT_WRITE_UNWRITTEN_CNT);
+               if (error == -EFBIG)
+                       error = xfs_iext_count_upgrade(tp, ip,
+                                       XFS_IEXT_WRITE_UNWRITTEN_CNT);
                if (error)
                        goto error_on_bmapi_transaction;
 
 
 
        error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                        XFS_IEXT_REFLINK_END_COW_CNT);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip,
+                               XFS_IEXT_REFLINK_END_COW_CNT);
        if (error)
                goto out_cancel;
 
                ++iext_delta;
 
        error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, iext_delta);
+       if (error == -EFBIG)
+               error = xfs_iext_count_upgrade(tp, ip, iext_delta);
        if (error)
                goto out_cancel;
 
 
 
                error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
                                XFS_IEXT_ADD_NOSPLIT_CNT);
+               if (error == -EFBIG)
+                       error = xfs_iext_count_upgrade(tp, ip,
+                                       XFS_IEXT_ADD_NOSPLIT_CNT);
                if (error)
                        goto out_trans_cancel;