ext4: Fix truncation of symlinks after failed write

Contents of long symlinks is written via standard write methods. So
when the write fails, we add inode to orphan list. But symlinks don't
have .truncate method defined so nobody properly removes them from the
on disk orphan list.

Fix this by calling ext4_truncate() directly instead of calling
vmtruncate() (which is saner anyway since we don't need anything
vmtruncate() does except from calling .truncate in these paths). We
also add inode to orphan list only if ext4_can_truncate() is true
(currently, it can be false for symlinks when there are no blocks
allocated) - otherwise orphan list processing will complain and
ext4_truncate() will not remove inode from on-disk orphan list.

Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>

authored by Jan Kara and committed by Theodore Ts'o ffacfa7a f91d1d04

+13 -13
+13 -13
fs/ext4/inode.c
··· 1513 1513 * Add inode to orphan list in case we crash before 1514 1514 * truncate finishes 1515 1515 */ 1516 - if (pos + len > inode->i_size) 1516 + if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1517 1517 ext4_orphan_add(handle, inode); 1518 1518 1519 1519 ext4_journal_stop(handle); 1520 1520 if (pos + len > inode->i_size) { 1521 - vmtruncate(inode, inode->i_size); 1521 + ext4_truncate(inode); 1522 1522 /* 1523 - * If vmtruncate failed early the inode might 1523 + * If truncate failed early the inode might 1524 1524 * still be on the orphan list; we need to 1525 1525 * make sure the inode is removed from the 1526 1526 * orphan list in that case. ··· 1614 1614 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1615 1615 page, fsdata); 1616 1616 copied = ret2; 1617 - if (pos + len > inode->i_size) 1617 + if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1618 1618 /* if we have allocated more blocks and copied 1619 1619 * less. We will have blocks allocated outside 1620 1620 * inode->i_size. So truncate them ··· 1628 1628 ret = ret2; 1629 1629 1630 1630 if (pos + len > inode->i_size) { 1631 - vmtruncate(inode, inode->i_size); 1631 + ext4_truncate(inode); 1632 1632 /* 1633 - * If vmtruncate failed early the inode might still be 1633 + * If truncate failed early the inode might still be 1634 1634 * on the orphan list; we need to make sure the inode 1635 1635 * is removed from the orphan list in that case. 1636 1636 */ ··· 1655 1655 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1656 1656 page, fsdata); 1657 1657 copied = ret2; 1658 - if (pos + len > inode->i_size) 1658 + if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1659 1659 /* if we have allocated more blocks and copied 1660 1660 * less. We will have blocks allocated outside 1661 1661 * inode->i_size. So truncate them ··· 1670 1670 ret = ret2; 1671 1671 1672 1672 if (pos + len > inode->i_size) { 1673 - vmtruncate(inode, inode->i_size); 1673 + ext4_truncate(inode); 1674 1674 /* 1675 - * If vmtruncate failed early the inode might still be 1675 + * If truncate failed early the inode might still be 1676 1676 * on the orphan list; we need to make sure the inode 1677 1677 * is removed from the orphan list in that case. 1678 1678 */ ··· 1722 1722 1723 1723 unlock_page(page); 1724 1724 page_cache_release(page); 1725 - if (pos + len > inode->i_size) 1725 + if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1726 1726 /* if we have allocated more blocks and copied 1727 1727 * less. We will have blocks allocated outside 1728 1728 * inode->i_size. So truncate them ··· 1733 1733 if (!ret) 1734 1734 ret = ret2; 1735 1735 if (pos + len > inode->i_size) { 1736 - vmtruncate(inode, inode->i_size); 1736 + ext4_truncate(inode); 1737 1737 /* 1738 - * If vmtruncate failed early the inode might still be 1738 + * If truncate failed early the inode might still be 1739 1739 * on the orphan list; we need to make sure the inode 1740 1740 * is removed from the orphan list in that case. 1741 1741 */ ··· 2907 2907 * i_size_read because we hold i_mutex. 2908 2908 */ 2909 2909 if (pos + len > inode->i_size) 2910 - vmtruncate(inode, inode->i_size); 2910 + ext4_truncate(inode); 2911 2911 } 2912 2912 2913 2913 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))