}
 
        /*
-        * wait for ordered IO before we have any locks.  We'll loop again
-        * below with the locks held.
+        * We have locked the inode at the VFS level (in exclusive mode) and we
+        * have locked the i_mmap_lock lock (in exclusive mode). Now before
+        * locking the file range, flush all dealloc in the range and wait for
+        * all ordered extents in the range to complete. After this we can lock
+        * the file range and, due to the previous locking we did, we know there
+        * can't be more delalloc or ordered extents in the range.
         */
        ret = btrfs_wait_ordered_range(inode, alloc_start,
                                       alloc_end - alloc_start);
        }
 
        locked_end = alloc_end - 1;
-       while (1) {
-               struct btrfs_ordered_extent *ordered;
-
-               /* the extent lock is ordered inside the running
-                * transaction
-                */
-               lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
-                                locked_end, &cached_state);
-               ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
-                                                           locked_end);
-
-               if (ordered &&
-                   ordered->file_offset + ordered->num_bytes > alloc_start &&
-                   ordered->file_offset < alloc_end) {
-                       btrfs_put_ordered_extent(ordered);
-                       unlock_extent_cached(&BTRFS_I(inode)->io_tree,
-                                            alloc_start, locked_end,
-                                            &cached_state);
-                       /*
-                        * we can't wait on the range with the transaction
-                        * running or with the extent lock held
-                        */
-                       ret = btrfs_wait_ordered_range(inode, alloc_start,
-                                                      alloc_end - alloc_start);
-                       if (ret)
-                               goto out;
-               } else {
-                       if (ordered)
-                               btrfs_put_ordered_extent(ordered);
-                       break;
-               }
-       }
+       lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
+                        &cached_state);
 
        /* First, check if we exceed the qgroup limit */
        INIT_LIST_HEAD(&reserve_list);