Skip to content

Commit d4f0318

Browse files
committed
Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
Pull ext4 bugfixes from Ted Ts'o: "Ext4 bug fixes for 3.17, to provide better handling of memory allocation failures, and to fix some journaling bugs involving journal checksums and FALLOC_FL_ZERO_RANGE" * tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: ext4: fix same-dir rename when inline data directory overflows jbd2: fix descriptor block size handling errors with journal_csum jbd2: fix infinite loop when recovering corrupt journal blocks ext4: update i_disksize coherently with block allocation on error path ext4: fix transaction issues for ext4_fallocate and ext_zero_range ext4: fix incorect journal credits reservation in ext4_zero_range ext4: move i_size,i_disksize update routines to helper function ext4: fix BUG_ON in mb_free_blocks() ext4: propagate errors up to ext4_find_entry()'s callers
2 parents ef13c8a + d80d448 commit d4f0318

11 files changed

Lines changed: 233 additions & 129 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ ext4_group_first_block_no(struct super_block *sb, ext4_group_t group_no)
18251825
/*
18261826
* Special error return code only used by dx_probe() and its callers.
18271827
*/
1828-
#define ERR_BAD_DX_DIR -75000
1828+
#define ERR_BAD_DX_DIR (-(MAX_ERRNO - 1))
18291829

18301830
/*
18311831
* Timeout and state flag for lazy initialization inode thread.
@@ -2454,6 +2454,22 @@ static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
24542454
up_write(&EXT4_I(inode)->i_data_sem);
24552455
}
24562456

2457+
/* Update i_size, i_disksize. Requires i_mutex to avoid races with truncate */
2458+
static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
2459+
{
2460+
int changed = 0;
2461+
2462+
if (newsize > inode->i_size) {
2463+
i_size_write(inode, newsize);
2464+
changed = 1;
2465+
}
2466+
if (newsize > EXT4_I(inode)->i_disksize) {
2467+
ext4_update_i_disksize(inode, newsize);
2468+
changed |= 2;
2469+
}
2470+
return changed;
2471+
}
2472+
24572473
struct ext4_group_info {
24582474
unsigned long bb_state;
24592475
struct rb_root bb_free_root;

‎fs/ext4/extents.c‎

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4665,7 +4665,8 @@ void ext4_ext_truncate(handle_t *handle, struct inode *inode)
46654665
}
46664666

46674667
static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
4668-
ext4_lblk_t len, int flags, int mode)
4668+
ext4_lblk_t len, loff_t new_size,
4669+
int flags, int mode)
46694670
{
46704671
struct inode *inode = file_inode(file);
46714672
handle_t *handle;
@@ -4674,8 +4675,10 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
46744675
int retries = 0;
46754676
struct ext4_map_blocks map;
46764677
unsigned int credits;
4678+
loff_t epos;
46774679

46784680
map.m_lblk = offset;
4681+
map.m_len = len;
46794682
/*
46804683
* Don't normalize the request if it can fit in one extent so
46814684
* that it doesn't get unnecessarily split into multiple
@@ -4690,9 +4693,7 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
46904693
credits = ext4_chunk_trans_blocks(inode, len);
46914694

46924695
retry:
4693-
while (ret >= 0 && ret < len) {
4694-
map.m_lblk = map.m_lblk + ret;
4695-
map.m_len = len = len - ret;
4696+
while (ret >= 0 && len) {
46964697
handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
46974698
credits);
46984699
if (IS_ERR(handle)) {
@@ -4709,6 +4710,21 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
47094710
ret2 = ext4_journal_stop(handle);
47104711
break;
47114712
}
4713+
map.m_lblk += ret;
4714+
map.m_len = len = len - ret;
4715+
epos = (loff_t)map.m_lblk << inode->i_blkbits;
4716+
inode->i_ctime = ext4_current_time(inode);
4717+
if (new_size) {
4718+
if (epos > new_size)
4719+
epos = new_size;
4720+
if (ext4_update_inode_size(inode, epos) & 0x1)
4721+
inode->i_mtime = inode->i_ctime;
4722+
} else {
4723+
if (epos > inode->i_size)
4724+
ext4_set_inode_flag(inode,
4725+
EXT4_INODE_EOFBLOCKS);
4726+
}
4727+
ext4_mark_inode_dirty(handle, inode);
47124728
ret2 = ext4_journal_stop(handle);
47134729
if (ret2)
47144730
break;
@@ -4731,7 +4747,8 @@ static long ext4_zero_range(struct file *file, loff_t offset,
47314747
loff_t new_size = 0;
47324748
int ret = 0;
47334749
int flags;
4734-
int partial;
4750+
int credits;
4751+
int partial_begin, partial_end;
47354752
loff_t start, end;
47364753
ext4_lblk_t lblk;
47374754
struct address_space *mapping = inode->i_mapping;
@@ -4771,7 +4788,8 @@ static long ext4_zero_range(struct file *file, loff_t offset,
47714788

47724789
if (start < offset || end > offset + len)
47734790
return -EINVAL;
4774-
partial = (offset + len) & ((1 << blkbits) - 1);
4791+
partial_begin = offset & ((1 << blkbits) - 1);
4792+
partial_end = (offset + len) & ((1 << blkbits) - 1);
47754793

47764794
lblk = start >> blkbits;
47774795
max_blocks = (end >> blkbits);
@@ -4805,14 +4823,15 @@ static long ext4_zero_range(struct file *file, loff_t offset,
48054823
* If we have a partial block after EOF we have to allocate
48064824
* the entire block.
48074825
*/
4808-
if (partial)
4826+
if (partial_end)
48094827
max_blocks += 1;
48104828
}
48114829

48124830
if (max_blocks > 0) {
48134831

48144832
/* Now release the pages and zero block aligned part of pages*/
48154833
truncate_pagecache_range(inode, start, end - 1);
4834+
inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
48164835

48174836
/* Wait all existing dio workers, newcomers will block on i_mutex */
48184837
ext4_inode_block_unlocked_dio(inode);
@@ -4825,26 +4844,31 @@ static long ext4_zero_range(struct file *file, loff_t offset,
48254844
if (ret)
48264845
goto out_dio;
48274846

4828-
ret = ext4_alloc_file_blocks(file, lblk, max_blocks, flags,
4829-
mode);
4847+
ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4848+
flags, mode);
48304849
if (ret)
48314850
goto out_dio;
48324851
}
4852+
if (!partial_begin && !partial_end)
4853+
goto out_dio;
48334854

4834-
handle = ext4_journal_start(inode, EXT4_HT_MISC, 4);
4855+
/*
4856+
* In worst case we have to writeout two nonadjacent unwritten
4857+
* blocks and update the inode
4858+
*/
4859+
credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4860+
if (ext4_should_journal_data(inode))
4861+
credits += 2;
4862+
handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
48354863
if (IS_ERR(handle)) {
48364864
ret = PTR_ERR(handle);
48374865
ext4_std_error(inode->i_sb, ret);
48384866
goto out_dio;
48394867
}
48404868

48414869
inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4842-
48434870
if (new_size) {
4844-
if (new_size > i_size_read(inode))
4845-
i_size_write(inode, new_size);
4846-
if (new_size > EXT4_I(inode)->i_disksize)
4847-
ext4_update_i_disksize(inode, new_size);
4871+
ext4_update_inode_size(inode, new_size);
48484872
} else {
48494873
/*
48504874
* Mark that we allocate beyond EOF so the subsequent truncate
@@ -4853,7 +4877,6 @@ static long ext4_zero_range(struct file *file, loff_t offset,
48534877
if ((offset + len) > i_size_read(inode))
48544878
ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
48554879
}
4856-
48574880
ext4_mark_inode_dirty(handle, inode);
48584881

48594882
/* Zero out partial block at the edges of the range */
@@ -4880,13 +4903,11 @@ static long ext4_zero_range(struct file *file, loff_t offset,
48804903
long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
48814904
{
48824905
struct inode *inode = file_inode(file);
4883-
handle_t *handle;
48844906
loff_t new_size = 0;
48854907
unsigned int max_blocks;
48864908
int ret = 0;
48874909
int flags;
48884910
ext4_lblk_t lblk;
4889-
struct timespec tv;
48904911
unsigned int blkbits = inode->i_blkbits;
48914912

48924913
/* Return error if mode is not supported */
@@ -4937,36 +4958,15 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
49374958
goto out;
49384959
}
49394960

4940-
ret = ext4_alloc_file_blocks(file, lblk, max_blocks, flags, mode);
4961+
ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4962+
flags, mode);
49414963
if (ret)
49424964
goto out;
49434965

4944-
handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
4945-
if (IS_ERR(handle))
4946-
goto out;
4947-
4948-
tv = inode->i_ctime = ext4_current_time(inode);
4949-
4950-
if (new_size) {
4951-
if (new_size > i_size_read(inode)) {
4952-
i_size_write(inode, new_size);
4953-
inode->i_mtime = tv;
4954-
}
4955-
if (new_size > EXT4_I(inode)->i_disksize)
4956-
ext4_update_i_disksize(inode, new_size);
4957-
} else {
4958-
/*
4959-
* Mark that we allocate beyond EOF so the subsequent truncate
4960-
* can proceed even if the new size is the same as i_size.
4961-
*/
4962-
if ((offset + len) > i_size_read(inode))
4963-
ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4966+
if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4967+
ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4968+
EXT4_I(inode)->i_sync_tid);
49644969
}
4965-
ext4_mark_inode_dirty(handle, inode);
4966-
if (file->f_flags & O_SYNC)
4967-
ext4_handle_sync(handle);
4968-
4969-
ext4_journal_stop(handle);
49704970
out:
49714971
mutex_unlock(&inode->i_mutex);
49724972
trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);

‎fs/ext4/inode.c‎

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,27 +1055,11 @@ static int ext4_write_end(struct file *file,
10551055
} else
10561056
copied = block_write_end(file, mapping, pos,
10571057
len, copied, page, fsdata);
1058-
10591058
/*
1060-
* No need to use i_size_read() here, the i_size
1061-
* cannot change under us because we hole i_mutex.
1062-
*
1063-
* But it's important to update i_size while still holding page lock:
1059+
* it's important to update i_size while still holding page lock:
10641060
* page writeout could otherwise come in and zero beyond i_size.
10651061
*/
1066-
if (pos + copied > inode->i_size) {
1067-
i_size_write(inode, pos + copied);
1068-
i_size_changed = 1;
1069-
}
1070-
1071-
if (pos + copied > EXT4_I(inode)->i_disksize) {
1072-
/* We need to mark inode dirty even if
1073-
* new_i_size is less that inode->i_size
1074-
* but greater than i_disksize. (hint delalloc)
1075-
*/
1076-
ext4_update_i_disksize(inode, (pos + copied));
1077-
i_size_changed = 1;
1078-
}
1062+
i_size_changed = ext4_update_inode_size(inode, pos + copied);
10791063
unlock_page(page);
10801064
page_cache_release(page);
10811065

@@ -1123,7 +1107,7 @@ static int ext4_journalled_write_end(struct file *file,
11231107
int ret = 0, ret2;
11241108
int partial = 0;
11251109
unsigned from, to;
1126-
loff_t new_i_size;
1110+
int size_changed = 0;
11271111

11281112
trace_ext4_journalled_write_end(inode, pos, len, copied);
11291113
from = pos & (PAGE_CACHE_SIZE - 1);
@@ -1146,20 +1130,18 @@ static int ext4_journalled_write_end(struct file *file,
11461130
if (!partial)
11471131
SetPageUptodate(page);
11481132
}
1149-
new_i_size = pos + copied;
1150-
if (new_i_size > inode->i_size)
1151-
i_size_write(inode, pos+copied);
1133+
size_changed = ext4_update_inode_size(inode, pos + copied);
11521134
ext4_set_inode_state(inode, EXT4_STATE_JDATA);
11531135
EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1154-
if (new_i_size > EXT4_I(inode)->i_disksize) {
1155-
ext4_update_i_disksize(inode, new_i_size);
1136+
unlock_page(page);
1137+
page_cache_release(page);
1138+
1139+
if (size_changed) {
11561140
ret2 = ext4_mark_inode_dirty(handle, inode);
11571141
if (!ret)
11581142
ret = ret2;
11591143
}
11601144

1161-
unlock_page(page);
1162-
page_cache_release(page);
11631145
if (pos + len > inode->i_size && ext4_can_truncate(inode))
11641146
/* if we have allocated more blocks and copied
11651147
* less. We will have blocks allocated outside
@@ -2095,6 +2077,7 @@ static int mpage_map_and_submit_extent(handle_t *handle,
20952077
struct ext4_map_blocks *map = &mpd->map;
20962078
int err;
20972079
loff_t disksize;
2080+
int progress = 0;
20982081

20992082
mpd->io_submit.io_end->offset =
21002083
((loff_t)map->m_lblk) << inode->i_blkbits;
@@ -2111,8 +2094,11 @@ static int mpage_map_and_submit_extent(handle_t *handle,
21112094
* is non-zero, a commit should free up blocks.
21122095
*/
21132096
if ((err == -ENOMEM) ||
2114-
(err == -ENOSPC && ext4_count_free_clusters(sb)))
2097+
(err == -ENOSPC && ext4_count_free_clusters(sb))) {
2098+
if (progress)
2099+
goto update_disksize;
21152100
return err;
2101+
}
21162102
ext4_msg(sb, KERN_CRIT,
21172103
"Delayed block allocation failed for "
21182104
"inode %lu at logical offset %llu with"
@@ -2129,15 +2115,17 @@ static int mpage_map_and_submit_extent(handle_t *handle,
21292115
*give_up_on_write = true;
21302116
return err;
21312117
}
2118+
progress = 1;
21322119
/*
21332120
* Update buffer state, submit mapped pages, and get us new
21342121
* extent to map
21352122
*/
21362123
err = mpage_map_and_submit_buffers(mpd);
21372124
if (err < 0)
2138-
return err;
2125+
goto update_disksize;
21392126
} while (map->m_len);
21402127

2128+
update_disksize:
21412129
/*
21422130
* Update on-disk size after IO is submitted. Races with
21432131
* truncate are avoided by checking i_size under i_data_sem.

‎fs/ext4/mballoc.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,8 @@ static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
14121412
int last = first + count - 1;
14131413
struct super_block *sb = e4b->bd_sb;
14141414

1415+
if (WARN_ON(count == 0))
1416+
return;
14151417
BUG_ON(last >= (sb->s_blocksize << 3));
14161418
assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
14171419
/* Don't bother if the block group is corrupt. */
@@ -3221,6 +3223,8 @@ static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
32213223
int err;
32223224

32233225
if (pa == NULL) {
3226+
if (ac->ac_f_ex.fe_len == 0)
3227+
return;
32243228
err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
32253229
if (err) {
32263230
/*
@@ -3235,6 +3239,7 @@ static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
32353239
mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
32363240
ac->ac_f_ex.fe_len);
32373241
ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3242+
ext4_mb_unload_buddy(&e4b);
32383243
return;
32393244
}
32403245
if (pa->pa_type == MB_INODE_PA)

0 commit comments

Comments
 (0)