Commit a6995736 authored by Omar Sandoval's avatar Omar Sandoval Committed by Sasha Levin

ext4: fix indirect punch hole corruption

[ Upstream commit 6f30b7e3 ]

Commit 4f579ae7 (ext4: fix punch hole on files with indirect
mapping) rewrote FALLOC_FL_PUNCH_HOLE for ext4 files with indirect
mapping. However, there are bugs in several corner cases. This fixes 5
distinct bugs:

1. When there is at least one entire level of indirection between the
start and end of the punch range and the end of the punch range is the
first block of its level, we can't return early; we have to free the
intervening levels.

2. When the end is at a higher level of indirection than the start and
ext4_find_shared returns a top branch for the end, we still need to free
the rest of the shared branch it returns; we can't decrement partial2.

3. When a punch happens within one level of indirection, we need to
converge on an indirect block that contains the start and end. However,
because the branches returned from ext4_find_shared do not necessarily
start at the same level (e.g., the partial2 chain will be shallower if
the last block occurs at the beginning of an indirect group), the walk
of the two chains can end up "missing" each other and freeing a bunch of
extra blocks in the process. This mismatch can be handled by first
making sure that the chains are at the same level, then walking them
together until they converge.

4. When the punch happens within one level of indirection and
ext4_find_shared returns a top branch for the start, we must free it,
but only if the end does not occur within that branch.

5. When the punch happens within one level of indirection and
ext4_find_shared returns a top branch for the end, then we shouldn't
free the block referenced by the end of the returned chain (this mirrors
the different levels case).
Signed-off-by: default avatarOmar Sandoval <osandov@osandov.com>
Signed-off-by: default avatarSasha Levin <sasha.levin@oracle.com>
parent b27b4b79
...@@ -1393,10 +1393,7 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode, ...@@ -1393,10 +1393,7 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
* to free. Everything was covered by the start * to free. Everything was covered by the start
* of the range. * of the range.
*/ */
return 0; goto do_indirects;
} else {
/* Shared branch grows from an indirect block */
partial2--;
} }
} else { } else {
/* /*
...@@ -1427,6 +1424,39 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode, ...@@ -1427,6 +1424,39 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
/* Punch happened within the same level (n == n2) */ /* Punch happened within the same level (n == n2) */
partial = ext4_find_shared(inode, n, offsets, chain, &nr); partial = ext4_find_shared(inode, n, offsets, chain, &nr);
partial2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2); partial2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
/* Free top, but only if partial2 isn't its subtree. */
if (nr) {
int level = min(partial - chain, partial2 - chain2);
int i;
int subtree = 1;
for (i = 0; i <= level; i++) {
if (offsets[i] != offsets2[i]) {
subtree = 0;
break;
}
}
if (!subtree) {
if (partial == chain) {
/* Shared branch grows from the inode */
ext4_free_branches(handle, inode, NULL,
&nr, &nr+1,
(chain+n-1) - partial);
*partial->p = 0;
} else {
/* Shared branch grows from an indirect block */
BUFFER_TRACE(partial->bh, "get_write_access");
ext4_free_branches(handle, inode, partial->bh,
partial->p,
partial->p+1,
(chain+n-1) - partial);
}
}
}
if (!nr2) {
/* /*
* ext4_find_shared returns Indirect structure which * ext4_find_shared returns Indirect structure which
* points to the last element which should not be * points to the last element which should not be
...@@ -1434,11 +1464,18 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode, ...@@ -1434,11 +1464,18 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
* in punch_hole so we need to point to the next element * in punch_hole so we need to point to the next element
*/ */
partial2->p++; partial2->p++;
while ((partial > chain) || (partial2 > chain2)) { }
/* We're at the same block, so we're almost finished */
if ((partial->bh && partial2->bh) && while (partial > chain || partial2 > chain2) {
(partial->bh->b_blocknr == partial2->bh->b_blocknr)) { int depth = (chain+n-1) - partial;
if ((partial > chain) && (partial2 > chain2)) { int depth2 = (chain2+n2-1) - partial2;
if (partial > chain && partial2 > chain2 &&
partial->bh->b_blocknr == partial2->bh->b_blocknr) {
/*
* We've converged on the same block. Clear the range,
* then we're done.
*/
ext4_free_branches(handle, inode, partial->bh, ext4_free_branches(handle, inode, partial->bh,
partial->p + 1, partial->p + 1,
partial2->p, partial2->p,
...@@ -1447,14 +1484,17 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode, ...@@ -1447,14 +1484,17 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
brelse(partial->bh); brelse(partial->bh);
BUFFER_TRACE(partial2->bh, "call brelse"); BUFFER_TRACE(partial2->bh, "call brelse");
brelse(partial2->bh); brelse(partial2->bh);
}
return 0; return 0;
} }
/* /*
* Clear the ends of indirect blocks on the shared branch * The start and end partial branches may not be at the same
* at the start of the range * level even though the punch happened within one level. So, we
* give them a chance to arrive at the same level, then walk
* them in step with each other until we converge on the same
* block.
*/ */
if (partial > chain) { if (partial > chain && depth <= depth2) {
ext4_free_branches(handle, inode, partial->bh, ext4_free_branches(handle, inode, partial->bh,
partial->p + 1, partial->p + 1,
(__le32 *)partial->bh->b_data+addr_per_block, (__le32 *)partial->bh->b_data+addr_per_block,
...@@ -1463,20 +1503,17 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode, ...@@ -1463,20 +1503,17 @@ int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
brelse(partial->bh); brelse(partial->bh);
partial--; partial--;
} }
/* if (partial2 > chain2 && depth2 <= depth) {
* Clear the ends of indirect blocks on the shared branch
* at the end of the range
*/
if (partial2 > chain2) {
ext4_free_branches(handle, inode, partial2->bh, ext4_free_branches(handle, inode, partial2->bh,
(__le32 *)partial2->bh->b_data, (__le32 *)partial2->bh->b_data,
partial2->p, partial2->p,
(chain2+n-1) - partial2); (chain2+n2-1) - partial2);
BUFFER_TRACE(partial2->bh, "call brelse"); BUFFER_TRACE(partial2->bh, "call brelse");
brelse(partial2->bh); brelse(partial2->bh);
partial2--; partial2--;
} }
} }
return 0;
do_indirects: do_indirects:
/* Kill the remaining (whole) subtrees */ /* Kill the remaining (whole) subtrees */
......
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