Skip to content

Commit 695f6ae

Browse files
jankaratorvalds
authored andcommitted
ext3: avoid false EIO errors
Sometimes block_write_begin() can map buffers in a page but later we fail to copy data into those buffers (because the source page has been paged out in the mean time). We then end up with !uptodate mapped buffers. To add a bit more to the confusion, block_write_end() does not commit any data (and thus does not any mark buffers as uptodate) if we didn't succeed with copying all the data. Commit f4fc66a (ext3: convert to new aops) missed these cases and thus we were inserting non-uptodate buffers to transaction's list which confuses JBD code and it reports IO errors, aborts a transaction and generally makes users afraid about their data ;-P. This patch fixes the problem by reorganizing ext3_..._write_end() code to first call block_write_end() to mark buffers with valid data uptodate and after that we file only uptodate buffers to transaction's lists. We also fix a problem where we could leave blocks allocated beyond i_size (i_disksize in fact) because of failed write. We now add inode to orphan list when write fails (to be safe in case we crash) and then truncate blocks beyond i_size in a separate transaction. Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Cc: <linux-ext4@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent de18f3b commit 695f6ae

1 file changed

Lines changed: 74 additions & 65 deletions

File tree

‎fs/ext3/inode.c‎

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,12 +1149,15 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
11491149
struct page **pagep, void **fsdata)
11501150
{
11511151
struct inode *inode = mapping->host;
1152-
int ret, needed_blocks = ext3_writepage_trans_blocks(inode);
1152+
int ret;
11531153
handle_t *handle;
11541154
int retries = 0;
11551155
struct page *page;
11561156
pgoff_t index;
11571157
unsigned from, to;
1158+
/* Reserve one block more for addition to orphan list in case
1159+
* we allocate blocks but write fails for some reason */
1160+
int needed_blocks = ext3_writepage_trans_blocks(inode) + 1;
11581161

11591162
index = pos >> PAGE_CACHE_SHIFT;
11601163
from = pos & (PAGE_CACHE_SIZE - 1);
@@ -1184,14 +1187,19 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
11841187
}
11851188
write_begin_failed:
11861189
if (ret) {
1187-
ext3_journal_stop(handle);
1188-
unlock_page(page);
1189-
page_cache_release(page);
11901190
/*
11911191
* block_write_begin may have instantiated a few blocks
11921192
* outside i_size. Trim these off again. Don't need
11931193
* i_size_read because we hold i_mutex.
1194+
*
1195+
* Add inode to orphan list in case we crash before truncate
1196+
* finishes.
11941197
*/
1198+
if (pos + len > inode->i_size)
1199+
ext3_orphan_add(handle, inode);
1200+
ext3_journal_stop(handle);
1201+
unlock_page(page);
1202+
page_cache_release(page);
11951203
if (pos + len > inode->i_size)
11961204
vmtruncate(inode, inode->i_size);
11971205
}
@@ -1211,6 +1219,18 @@ int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
12111219
return err;
12121220
}
12131221

1222+
/* For ordered writepage and write_end functions */
1223+
static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1224+
{
1225+
/*
1226+
* Write could have mapped the buffer but it didn't copy the data in
1227+
* yet. So avoid filing such buffer into a transaction.
1228+
*/
1229+
if (buffer_mapped(bh) && buffer_uptodate(bh))
1230+
return ext3_journal_dirty_data(handle, bh);
1231+
return 0;
1232+
}
1233+
12141234
/* For write_end() in data=journal mode */
12151235
static int write_end_fn(handle_t *handle, struct buffer_head *bh)
12161236
{
@@ -1221,26 +1241,20 @@ static int write_end_fn(handle_t *handle, struct buffer_head *bh)
12211241
}
12221242

12231243
/*
1224-
* Generic write_end handler for ordered and writeback ext3 journal modes.
1225-
* We can't use generic_write_end, because that unlocks the page and we need to
1226-
* unlock the page after ext3_journal_stop, but ext3_journal_stop must run
1227-
* after block_write_end.
1244+
* This is nasty and subtle: ext3_write_begin() could have allocated blocks
1245+
* for the whole page but later we failed to copy the data in. Update inode
1246+
* size according to what we managed to copy. The rest is going to be
1247+
* truncated in write_end function.
12281248
*/
1229-
static int ext3_generic_write_end(struct file *file,
1230-
struct address_space *mapping,
1231-
loff_t pos, unsigned len, unsigned copied,
1232-
struct page *page, void *fsdata)
1249+
static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
12331250
{
1234-
struct inode *inode = file->f_mapping->host;
1235-
1236-
copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1237-
1238-
if (pos+copied > inode->i_size) {
1239-
i_size_write(inode, pos+copied);
1251+
/* What matters to us is i_disksize. We don't write i_size anywhere */
1252+
if (pos + copied > inode->i_size)
1253+
i_size_write(inode, pos + copied);
1254+
if (pos + copied > EXT3_I(inode)->i_disksize) {
1255+
EXT3_I(inode)->i_disksize = pos + copied;
12401256
mark_inode_dirty(inode);
12411257
}
1242-
1243-
return copied;
12441258
}
12451259

12461260
/*
@@ -1260,35 +1274,29 @@ static int ext3_ordered_write_end(struct file *file,
12601274
unsigned from, to;
12611275
int ret = 0, ret2;
12621276

1263-
from = pos & (PAGE_CACHE_SIZE - 1);
1264-
to = from + len;
1277+
copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
12651278

1279+
from = pos & (PAGE_CACHE_SIZE - 1);
1280+
to = from + copied;
12661281
ret = walk_page_buffers(handle, page_buffers(page),
1267-
from, to, NULL, ext3_journal_dirty_data);
1282+
from, to, NULL, journal_dirty_data_fn);
12681283

1269-
if (ret == 0) {
1270-
/*
1271-
* generic_write_end() will run mark_inode_dirty() if i_size
1272-
* changes. So let's piggyback the i_disksize mark_inode_dirty
1273-
* into that.
1274-
*/
1275-
loff_t new_i_size;
1276-
1277-
new_i_size = pos + copied;
1278-
if (new_i_size > EXT3_I(inode)->i_disksize)
1279-
EXT3_I(inode)->i_disksize = new_i_size;
1280-
ret2 = ext3_generic_write_end(file, mapping, pos, len, copied,
1281-
page, fsdata);
1282-
copied = ret2;
1283-
if (ret2 < 0)
1284-
ret = ret2;
1285-
}
1284+
if (ret == 0)
1285+
update_file_sizes(inode, pos, copied);
1286+
/*
1287+
* There may be allocated blocks outside of i_size because
1288+
* we failed to copy some data. Prepare for truncate.
1289+
*/
1290+
if (pos + len > inode->i_size)
1291+
ext3_orphan_add(handle, inode);
12861292
ret2 = ext3_journal_stop(handle);
12871293
if (!ret)
12881294
ret = ret2;
12891295
unlock_page(page);
12901296
page_cache_release(page);
12911297

1298+
if (pos + len > inode->i_size)
1299+
vmtruncate(inode, inode->i_size);
12921300
return ret ? ret : copied;
12931301
}
12941302

@@ -1299,25 +1307,22 @@ static int ext3_writeback_write_end(struct file *file,
12991307
{
13001308
handle_t *handle = ext3_journal_current_handle();
13011309
struct inode *inode = file->f_mapping->host;
1302-
int ret = 0, ret2;
1303-
loff_t new_i_size;
1304-
1305-
new_i_size = pos + copied;
1306-
if (new_i_size > EXT3_I(inode)->i_disksize)
1307-
EXT3_I(inode)->i_disksize = new_i_size;
1308-
1309-
ret2 = ext3_generic_write_end(file, mapping, pos, len, copied,
1310-
page, fsdata);
1311-
copied = ret2;
1312-
if (ret2 < 0)
1313-
ret = ret2;
1310+
int ret;
13141311

1315-
ret2 = ext3_journal_stop(handle);
1316-
if (!ret)
1317-
ret = ret2;
1312+
copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1313+
update_file_sizes(inode, pos, copied);
1314+
/*
1315+
* There may be allocated blocks outside of i_size because
1316+
* we failed to copy some data. Prepare for truncate.
1317+
*/
1318+
if (pos + len > inode->i_size)
1319+
ext3_orphan_add(handle, inode);
1320+
ret = ext3_journal_stop(handle);
13181321
unlock_page(page);
13191322
page_cache_release(page);
13201323

1324+
if (pos + len > inode->i_size)
1325+
vmtruncate(inode, inode->i_size);
13211326
return ret ? ret : copied;
13221327
}
13231328

@@ -1338,15 +1343,23 @@ static int ext3_journalled_write_end(struct file *file,
13381343
if (copied < len) {
13391344
if (!PageUptodate(page))
13401345
copied = 0;
1341-
page_zero_new_buffers(page, from+copied, to);
1346+
page_zero_new_buffers(page, from + copied, to);
1347+
to = from + copied;
13421348
}
13431349

13441350
ret = walk_page_buffers(handle, page_buffers(page), from,
13451351
to, &partial, write_end_fn);
13461352
if (!partial)
13471353
SetPageUptodate(page);
1348-
if (pos+copied > inode->i_size)
1349-
i_size_write(inode, pos+copied);
1354+
1355+
if (pos + copied > inode->i_size)
1356+
i_size_write(inode, pos + copied);
1357+
/*
1358+
* There may be allocated blocks outside of i_size because
1359+
* we failed to copy some data. Prepare for truncate.
1360+
*/
1361+
if (pos + len > inode->i_size)
1362+
ext3_orphan_add(handle, inode);
13501363
EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
13511364
if (inode->i_size > EXT3_I(inode)->i_disksize) {
13521365
EXT3_I(inode)->i_disksize = inode->i_size;
@@ -1361,6 +1374,8 @@ static int ext3_journalled_write_end(struct file *file,
13611374
unlock_page(page);
13621375
page_cache_release(page);
13631376

1377+
if (pos + len > inode->i_size)
1378+
vmtruncate(inode, inode->i_size);
13641379
return ret ? ret : copied;
13651380
}
13661381

@@ -1428,17 +1443,11 @@ static int bput_one(handle_t *handle, struct buffer_head *bh)
14281443
return 0;
14291444
}
14301445

1431-
static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1432-
{
1433-
if (buffer_mapped(bh))
1434-
return ext3_journal_dirty_data(handle, bh);
1435-
return 0;
1436-
}
1437-
14381446
static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
14391447
{
14401448
return !buffer_mapped(bh);
14411449
}
1450+
14421451
/*
14431452
* Note that we always start a transaction even if we're not journalling
14441453
* data. This is to preserve ordering: any hole instantiation within

0 commit comments

Comments
 (0)