Commit ffacfa7a authored by Jan Kara's avatar Jan Kara Committed by Theodore Ts'o

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: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent f91d1d04
...@@ -1513,14 +1513,14 @@ retry: ...@@ -1513,14 +1513,14 @@ retry:
* Add inode to orphan list in case we crash before * Add inode to orphan list in case we crash before
* truncate finishes * truncate finishes
*/ */
if (pos + len > inode->i_size) if (pos + len > inode->i_size && ext4_can_truncate(inode))
ext4_orphan_add(handle, inode); ext4_orphan_add(handle, inode);
ext4_journal_stop(handle); ext4_journal_stop(handle);
if (pos + len > inode->i_size) { if (pos + len > inode->i_size) {
vmtruncate(inode, inode->i_size); ext4_truncate(inode);
/* /*
* If vmtruncate failed early the inode might * If truncate failed early the inode might
* still be on the orphan list; we need to * still be on the orphan list; we need to
* make sure the inode is removed from the * make sure the inode is removed from the
* orphan list in that case. * orphan list in that case.
...@@ -1614,7 +1614,7 @@ static int ext4_ordered_write_end(struct file *file, ...@@ -1614,7 +1614,7 @@ static int ext4_ordered_write_end(struct file *file,
ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
page, fsdata); page, fsdata);
copied = ret2; copied = ret2;
if (pos + len > inode->i_size) if (pos + len > inode->i_size && ext4_can_truncate(inode))
/* if we have allocated more blocks and copied /* if we have allocated more blocks and copied
* less. We will have blocks allocated outside * less. We will have blocks allocated outside
* inode->i_size. So truncate them * inode->i_size. So truncate them
...@@ -1628,9 +1628,9 @@ static int ext4_ordered_write_end(struct file *file, ...@@ -1628,9 +1628,9 @@ static int ext4_ordered_write_end(struct file *file,
ret = ret2; ret = ret2;
if (pos + len > inode->i_size) { if (pos + len > inode->i_size) {
vmtruncate(inode, inode->i_size); ext4_truncate(inode);
/* /*
* If vmtruncate failed early the inode might still be * If truncate failed early the inode might still be
* on the orphan list; we need to make sure the inode * on the orphan list; we need to make sure the inode
* is removed from the orphan list in that case. * is removed from the orphan list in that case.
*/ */
...@@ -1655,7 +1655,7 @@ static int ext4_writeback_write_end(struct file *file, ...@@ -1655,7 +1655,7 @@ static int ext4_writeback_write_end(struct file *file,
ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
page, fsdata); page, fsdata);
copied = ret2; copied = ret2;
if (pos + len > inode->i_size) if (pos + len > inode->i_size && ext4_can_truncate(inode))
/* if we have allocated more blocks and copied /* if we have allocated more blocks and copied
* less. We will have blocks allocated outside * less. We will have blocks allocated outside
* inode->i_size. So truncate them * inode->i_size. So truncate them
...@@ -1670,9 +1670,9 @@ static int ext4_writeback_write_end(struct file *file, ...@@ -1670,9 +1670,9 @@ static int ext4_writeback_write_end(struct file *file,
ret = ret2; ret = ret2;
if (pos + len > inode->i_size) { if (pos + len > inode->i_size) {
vmtruncate(inode, inode->i_size); ext4_truncate(inode);
/* /*
* If vmtruncate failed early the inode might still be * If truncate failed early the inode might still be
* on the orphan list; we need to make sure the inode * on the orphan list; we need to make sure the inode
* is removed from the orphan list in that case. * is removed from the orphan list in that case.
*/ */
...@@ -1722,7 +1722,7 @@ static int ext4_journalled_write_end(struct file *file, ...@@ -1722,7 +1722,7 @@ static int ext4_journalled_write_end(struct file *file,
unlock_page(page); unlock_page(page);
page_cache_release(page); page_cache_release(page);
if (pos + len > inode->i_size) if (pos + len > inode->i_size && ext4_can_truncate(inode))
/* if we have allocated more blocks and copied /* if we have allocated more blocks and copied
* less. We will have blocks allocated outside * less. We will have blocks allocated outside
* inode->i_size. So truncate them * inode->i_size. So truncate them
...@@ -1733,9 +1733,9 @@ static int ext4_journalled_write_end(struct file *file, ...@@ -1733,9 +1733,9 @@ static int ext4_journalled_write_end(struct file *file,
if (!ret) if (!ret)
ret = ret2; ret = ret2;
if (pos + len > inode->i_size) { if (pos + len > inode->i_size) {
vmtruncate(inode, inode->i_size); ext4_truncate(inode);
/* /*
* If vmtruncate failed early the inode might still be * If truncate failed early the inode might still be
* on the orphan list; we need to make sure the inode * on the orphan list; we need to make sure the inode
* is removed from the orphan list in that case. * is removed from the orphan list in that case.
*/ */
...@@ -2907,7 +2907,7 @@ retry: ...@@ -2907,7 +2907,7 @@ retry:
* i_size_read because we hold i_mutex. * i_size_read because we hold i_mutex.
*/ */
if (pos + len > inode->i_size) if (pos + len > inode->i_size)
vmtruncate(inode, inode->i_size); ext4_truncate(inode);
} }
if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment