@@ -1778,10 +1778,11 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
17781778 struct indx_node * n1 = fnd -> nodes [level ];
17791779 struct INDEX_HDR * hdr1 = & n1 -> index -> ihdr ;
17801780 struct INDEX_HDR * hdr2 ;
1781- u32 to_copy , used ;
1781+ u32 to_copy , used , used1 ;
17821782 CLST new_vbn ;
17831783 __le64 t_vbn , * sub_vbn ;
17841784 u16 sp_size ;
1785+ void * hdr1_saved = NULL ;
17851786
17861787 /* Try the most easy case. */
17871788 e = fnd -> level - 1 == level ? fnd -> de [level ] : NULL ;
@@ -1814,6 +1815,13 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
18141815 return - ENOMEM ;
18151816 memcpy (up_e , sp , sp_size );
18161817
1818+ used1 = le32_to_cpu (hdr1 -> used );
1819+ hdr1_saved = kmemdup (hdr1 , used1 , GFP_NOFS );
1820+ if (!hdr1_saved ) {
1821+ err = - ENOMEM ;
1822+ goto out ;
1823+ }
1824+
18171825 if (!hdr1 -> flags ) {
18181826 up_e -> flags |= NTFS_IE_HAS_SUBNODES ;
18191827 up_e -> size = cpu_to_le16 (sp_size + sizeof (u64 ));
@@ -1846,7 +1854,7 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
18461854 hdr_insert_head (hdr2 , de_t , to_copy );
18471855
18481856 /* Remove all entries (sp including) from hdr1. */
1849- used = le32_to_cpu ( hdr1 -> used ) - to_copy - sp_size ;
1857+ used = used1 - to_copy - sp_size ;
18501858 memmove (de_t , Add2Ptr (sp , sp_size ), used - le32_to_cpu (hdr1 -> de_off ));
18511859 hdr1 -> used = cpu_to_le32 (used );
18521860
@@ -1876,21 +1884,27 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
18761884 if (!level ) {
18771885 /* Insert in root. */
18781886 err = indx_insert_into_root (indx , ni , up_e , NULL , ctx , fnd , 0 );
1879- if (err )
1880- goto out ;
18811887 } else {
18821888 /*
18831889 * The target buffer's parent is another index buffer.
18841890 * TODO: Remove recursion.
18851891 */
18861892 err = indx_insert_into_buffer (indx , ni , root , up_e , ctx ,
18871893 level - 1 , fnd );
1888- if (err )
1889- goto out ;
1894+ }
1895+
1896+ if (err ) {
1897+ /*
1898+ * Undo critical operations.
1899+ */
1900+ indx_mark_free (indx , ni , new_vbn >> indx -> idx2vbn_bits );
1901+ memcpy (hdr1 , hdr1_saved , used1 );
1902+ indx_write (indx , ni , n1 , 0 );
18901903 }
18911904
18921905out :
18931906 kfree (up_e );
1907+ kfree (hdr1_saved );
18941908
18951909 return err ;
18961910}
@@ -1949,16 +1963,12 @@ int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
19491963 */
19501964 err = indx_insert_into_root (indx , ni , new_de , fnd -> root_de , ctx ,
19511965 fnd , undo );
1952- if (err )
1953- goto out ;
19541966 } else {
19551967 /*
19561968 * Found a leaf buffer, so we'll insert the new entry into it.
19571969 */
19581970 err = indx_insert_into_buffer (indx , ni , root , new_de , ctx ,
19591971 fnd -> level - 1 , fnd );
1960- if (err )
1961- goto out ;
19621972 }
19631973
19641974out :
0 commit comments