Commit 4d1adc2a authored by Jan Kara's avatar Jan Kara Committed by Sasha Levin

ext4: fix SEEK_HOLE

[ Upstream commit 7d95eddf ]

Currently, SEEK_HOLE implementation in ext4 may both return that there's
a hole at some offset although that offset already has data and skip
some holes during a search for the next hole. The first problem is
demostrated by:

xfs_io -c "falloc 0 256k" -c "pwrite 0 56k" -c "seek -h 0" file
wrote 57344/57344 bytes at offset 0
56 KiB, 14 ops; 0.0000 sec (2.054 GiB/sec and 538461.5385 ops/sec)
Whence	Result
HOLE	0

Where we can see that SEEK_HOLE wrongly returned offset 0 as containing
a hole although we have written data there. The second problem can be
demonstrated by:

xfs_io -c "falloc 0 256k" -c "pwrite 0 56k" -c "pwrite 128k 8k"
       -c "seek -h 0" file

wrote 57344/57344 bytes at offset 0
56 KiB, 14 ops; 0.0000 sec (1.978 GiB/sec and 518518.5185 ops/sec)
wrote 8192/8192 bytes at offset 131072
8 KiB, 2 ops; 0.0000 sec (2 GiB/sec and 500000.0000 ops/sec)
Whence	Result
HOLE	139264

Where we can see that hole at offsets 56k..128k has been ignored by the
SEEK_HOLE call.

The underlying problem is in the ext4_find_unwritten_pgoff() which is
just buggy. In some cases it fails to update returned offset when it
finds a hole (when no pages are found or when the first found page has
higher index than expected), in some cases conditions for detecting hole
are just missing (we fail to detect a situation where indices of
returned pages are not contiguous).

Fix ext4_find_unwritten_pgoff() to properly detect non-contiguous page
indices and also handle all cases where we got less pages then expected
in one place and handle it properly there.

CC: stable@vger.kernel.org
Fixes: c8c0df24
CC: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
Signed-off-by: default avatarSasha Levin <alexander.levin@verizon.com>
parent 8406f302
...@@ -354,47 +354,27 @@ static int ext4_find_unwritten_pgoff(struct inode *inode, ...@@ -354,47 +354,27 @@ static int ext4_find_unwritten_pgoff(struct inode *inode,
num = min_t(pgoff_t, end - index, PAGEVEC_SIZE); num = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index, nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
(pgoff_t)num); (pgoff_t)num);
if (nr_pages == 0) { if (nr_pages == 0)
if (whence == SEEK_DATA)
break;
BUG_ON(whence != SEEK_HOLE);
/*
* If this is the first time to go into the loop and
* offset is not beyond the end offset, it will be a
* hole at this offset
*/
if (lastoff == startoff || lastoff < endoff)
found = 1;
break; break;
}
/*
* If this is the first time to go into the loop and
* offset is smaller than the first page offset, it will be a
* hole at this offset.
*/
if (lastoff == startoff && whence == SEEK_HOLE &&
lastoff < page_offset(pvec.pages[0])) {
found = 1;
break;
}
for (i = 0; i < nr_pages; i++) { for (i = 0; i < nr_pages; i++) {
struct page *page = pvec.pages[i]; struct page *page = pvec.pages[i];
struct buffer_head *bh, *head; struct buffer_head *bh, *head;
/* /*
* If the current offset is not beyond the end of given * If current offset is smaller than the page offset,
* range, it will be a hole. * there is a hole at this offset.
*/ */
if (lastoff < endoff && whence == SEEK_HOLE && if (whence == SEEK_HOLE && lastoff < endoff &&
page->index > end) { lastoff < page_offset(pvec.pages[i])) {
found = 1; found = 1;
*offset = lastoff; *offset = lastoff;
goto out; goto out;
} }
if (page->index > end)
goto out;
lock_page(page); lock_page(page);
if (unlikely(page->mapping != inode->i_mapping)) { if (unlikely(page->mapping != inode->i_mapping)) {
...@@ -434,20 +414,18 @@ static int ext4_find_unwritten_pgoff(struct inode *inode, ...@@ -434,20 +414,18 @@ static int ext4_find_unwritten_pgoff(struct inode *inode,
unlock_page(page); unlock_page(page);
} }
/* /* The no. of pages is less than our desired, we are done. */
* The no. of pages is less than our desired, that would be a if (nr_pages < num)
* hole in there.
*/
if (nr_pages < num && whence == SEEK_HOLE) {
found = 1;
*offset = lastoff;
break; break;
}
index = pvec.pages[i - 1]->index + 1; index = pvec.pages[i - 1]->index + 1;
pagevec_release(&pvec); pagevec_release(&pvec);
} while (index <= end); } while (index <= end);
if (whence == SEEK_HOLE && lastoff < endoff) {
found = 1;
*offset = lastoff;
}
out: out:
pagevec_release(&pvec); pagevec_release(&pvec);
return found; return found;
......
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