Skip to content

Commit 7fd1a3f

Browse files
Josef BacikChris Mason
authored andcommitted
Btrfs: hold a ref on the inode during writepages
We can race with unlink and not actually be able to do our igrab in btrfs_add_ordered_extent. This will result in all sorts of problems. Instead of doing the complicated work to try and handle returning an error properly from btrfs_add_ordered_extent, just hold a ref to the inode during writepages. If we cannot grab a ref we know we're freeing this inode anyway and can just drop the dirty pages on the floor, because screw them we're going to invalidate them anyway. Thanks, Signed-off-by: Josef Bacik <[email protected]>
1 parent bdb7d30 commit 7fd1a3f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

fs/btrfs/extent_io.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
33243324
writepage_t writepage, void *data,
33253325
void (*flush_fn)(void *))
33263326
{
3327+
struct inode *inode = mapping->host;
33273328
int ret = 0;
33283329
int done = 0;
33293330
int nr_to_write_done = 0;
@@ -3334,6 +3335,18 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
33343335
int scanned = 0;
33353336
int tag;
33363337

3338+
/*
3339+
* We have to hold onto the inode so that ordered extents can do their
3340+
* work when the IO finishes. The alternative to this is failing to add
3341+
* an ordered extent if the igrab() fails there and that is a huge pain
3342+
* to deal with, so instead just hold onto the inode throughout the
3343+
* writepages operation. If it fails here we are freeing up the inode
3344+
* anyway and we'd rather not waste our time writing out stuff that is
3345+
* going to be truncated anyway.
3346+
*/
3347+
if (!igrab(inode))
3348+
return 0;
3349+
33373350
pagevec_init(&pvec, 0);
33383351
if (wbc->range_cyclic) {
33393352
index = mapping->writeback_index; /* Start from prev offset */
@@ -3428,6 +3441,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
34283441
index = 0;
34293442
goto retry;
34303443
}
3444+
btrfs_add_delayed_iput(inode);
34313445
return ret;
34323446
}
34333447

0 commit comments

Comments
 (0)