diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 3f89d0ab08fc..c8bd68ae2d14 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1005,7 +1005,9 @@ struct ext4_inode_info {

  	/* extents status tree */
  	struct ext4_es_tree i_es_tree;
+	struct io_unwritten_tree i_io_unwritten_tree;
  	rwlock_t i_es_lock;
+	rwlock_t i_ue_lock;
  	struct list_head i_es_list;
  	unsigned int i_es_all_nr;	/* protected by i_es_lock */
  	unsigned int i_es_shk_nr;	/* protected by i_es_lock */
@@ -3224,6 +3226,7 @@ static inline void 
ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
  	if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  		io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
  		/* Wake up anyone waiting on unwritten extent conversion */
+		ext4_remove_io_unwritten_extent(inode, io_end->offset >> 
inode->i_blkbits);
  		if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  			wake_up_all(ext4_ioend_wq(inode));
  	}
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 240b6dea5441..b13b555d0318 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -1713,6 +1713,26 @@ static int ext4_ext_correct_indexes(handle_t 
*handle, struct inode *inode,
  	return err;
  }

+static inline int ext4_unwritten_extents_in_io(struct inode *inode,
+			struct ext4_extent *ex1, struct ext4_extent *ex2)
+{
+	int ret, ret1;
+
+	ret = atomic_read(&EXT4_I(inode)->i_unwritten);
+	if (ret == 0)
+		return 0;
+
+	ret = ext4_lookup_io_unwritten_extent(inode,
+			le32_to_cpu(ex1->ee_block));
+	ret1 = ext4_lookup_io_unwritten_extent(inode,
+			le32_to_cpu(ex2->ee_block));
+
+	if (ret || ret1)
+		return 1;
+
+	return 0;
+}
+
  int
  ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  				struct ext4_extent *ex2)
@@ -1744,7 +1764,7 @@ ext4_can_extents_be_merged(struct inode *inode, 
struct ext4_extent *ex1,
  	 */
  	if (ext4_ext_is_unwritten(ex1) &&
  	    (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
-	     atomic_read(&EXT4_I(inode)->i_unwritten) ||
+	    ext4_unwritten_extents_in_io(inode, ex1, ex2) ||
  	     (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
  		return 0;
  #ifdef AGGRESSIVE_TEST
diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index 2b439afafe13..62dfd4a4c02e 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -1870,3 +1870,136 @@ void ext4_es_remove_blks(struct inode *inode, 
ext4_lblk_t lblk,

  	ext4_da_release_space(inode, reserved);
  }
+
+
+static struct kmem_cache *ext4_io_unwritten_cachep;
+
+int ext4_lookup_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk)
+{
+	struct io_unwritten_tree *tree;
+	struct io_unwritten_extent *ue;
+	struct rb_node *node;
+	int found = 0;
+
+	tree = &EXT4_I(inode)->i_io_unwritten_tree;
+	read_lock(&EXT4_I(inode)->i_ue_lock);
+
+	node = tree->root.rb_node;
+	while (node) {
+		ue = rb_entry(node, struct io_unwritten_extent, rb_node);
+		if (lblk < ue->es_lblk)
+			node = node->rb_left;
+		else if (lblk >= ue->es_lblk + ue->es_len)
+			node = node->rb_right;
+		else {
+			found = 1;
+			break;
+		}
+	}
+	read_unlock(&EXT4_I(inode)->i_ue_lock);
+
+	return found;
+}
+
+static struct io_unwritten_extent *
+ext4_alloc_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk,
+			       ext4_lblk_t len)
+{
+	struct io_unwritten_extent *ue;
+
+	ue = kmem_cache_alloc(ext4_io_unwritten_cachep, GFP_ATOMIC);
+	if (ue == NULL)
+		return NULL;
+	ue->es_lblk = lblk;
+	ue->es_len = len;
+	return ue;
+}
+
+
+int ext4_insert_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk,
+				    ext4_lblk_t len)
+{
+	struct io_unwritten_tree *tree = &EXT4_I(inode)->i_io_unwritten_tree;
+	struct rb_node **p = &tree->root.rb_node;
+	struct rb_node *parent = NULL;
+	struct io_unwritten_extent *ue;
+
+	write_lock(&EXT4_I(inode)->i_ue_lock);
+	while (*p) {
+		parent = *p;
+		ue = rb_entry(parent, struct io_unwritten_extent, rb_node);
+
+		if (lblk < ue->es_lblk)
+			p = &(*p)->rb_left;
+		else if (lblk > ue->es_lblk) {
+			p = &(*p)->rb_right;
+		} else {
+			BUG_ON(1);
+			return -EINVAL;
+		}
+	}
+
+	ue = ext4_alloc_io_unwritten_extent(inode, lblk, len);
+	if (!ue) {
+		write_unlock(&EXT4_I(inode)->i_ue_lock);
+		return -ENOMEM;
+	}
+
+	rb_link_node(&ue->rb_node, parent, p);
+	rb_insert_color(&ue->rb_node, &tree->root);
+	write_unlock(&EXT4_I(inode)->i_ue_lock);
+
+	return 0;
+}
+
+int ext4_remove_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk)
+{
+	struct io_unwritten_tree *tree = &EXT4_I(inode)->i_io_unwritten_tree;
+	struct rb_node **p = &tree->root.rb_node;
+	struct rb_node *parent = NULL;
+	struct io_unwritten_extent *ue;
+	int found = 0;
+
+	write_lock(&EXT4_I(inode)->i_ue_lock);
+	while (*p) {
+		parent = *p;
+		ue = rb_entry(parent, struct io_unwritten_extent, rb_node);
+
+		if (lblk < ue->es_lblk)
+			p = &(*p)->rb_left;
+		else if (lblk > ue->es_lblk) {
+			p = &(*p)->rb_right;
+		} else {
+			found = 1;
+			break;
+		}
+	}
+
+	if (found) {
+		rb_erase(&ue->rb_node, &tree->root);
+		kmem_cache_free(ext4_io_unwritten_cachep, ue);
+	}
+	write_unlock(&EXT4_I(inode)->i_ue_lock);
+	return 0;
+}
+
+void ext4_init_io_unwritten_tree(struct io_unwritten_tree *tree)
+{
+	tree->root = RB_ROOT;
+}
+
+int __init ext4_io_unwritten_extent_init(void)
+{
+	ext4_io_unwritten_cachep = kmem_cache_create("ext4_io_unwritten_status",
+					sizeof(struct io_unwritten_extent),
+					0, (SLAB_RECLAIM_ACCOUNT), NULL);
+	if (ext4_io_unwritten_cachep == NULL)
+		return -ENOMEM;
+	return 0;
+}
+
+void ext4_exit_io_unwritten_extent(void)
+{
+	kmem_cache_destroy(ext4_io_unwritten_cachep);
+}
+
diff --git a/fs/ext4/extents_status.h b/fs/ext4/extents_status.h
index 131a8b7df265..e65878b17999 100644
--- a/fs/ext4/extents_status.h
+++ b/fs/ext4/extents_status.h
@@ -249,4 +249,23 @@ extern unsigned int ext4_es_delayed_clu(struct 
inode *inode, ext4_lblk_t lblk,
  extern void ext4_es_remove_blks(struct inode *inode, ext4_lblk_t lblk,
  				ext4_lblk_t len);

+struct io_unwritten_extent {
+	struct rb_node rb_node;
+	ext4_lblk_t es_lblk;    /* first logical block extent covers */
+	ext4_lblk_t es_len;     /* length of extent in block */
+};
+
+struct io_unwritten_tree {
+	struct rb_root root;
+};
+
+int ext4_lookup_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk);
+int ext4_insert_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk,
+				    ext4_lblk_t len);
+int ext4_remove_io_unwritten_extent(struct inode *inode, ext4_lblk_t lblk);
+
+void ext4_init_io_unwritten_tree(struct io_unwritten_tree *tree);
+int __init ext4_io_unwritten_extent_init(void);
+void ext4_exit_io_unwritten_extent(void);
+
  #endif /* _EXT4_EXTENTS_STATUS_H */
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 22a9d8159720..6315b9907820 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2487,6 +2487,7 @@ static int mpage_map_one_extent(handle_t *handle, 
struct mpage_da_data *mpd)
  			handle->h_rsv_handle = NULL;
  		}
  		ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
+		ext4_insert_io_unwritten_extent(inode, map->m_lblk, map->m_len);
  	}

  	BUG_ON(map->m_len == 0);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 53ff6c2a26ed..ecac98925a0c 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1043,7 +1043,9 @@ static struct inode *ext4_alloc_inode(struct 
super_block *sb)
  	INIT_LIST_HEAD(&ei->i_prealloc_list);
  	spin_lock_init(&ei->i_prealloc_lock);
  	ext4_es_init_tree(&ei->i_es_tree);
+	ext4_init_io_unwritten_tree(&ei->i_io_unwritten_tree);
  	rwlock_init(&ei->i_es_lock);
+	rwlock_init(&ei->i_ue_lock);
  	INIT_LIST_HEAD(&ei->i_es_list);
  	ei->i_es_all_nr = 0;
