udf: Fix deadlock between writeback and udf_setsize()
authorJan Kara <jack@suse.cz>
Tue, 13 Jun 2017 14:20:25 +0000 (16:20 +0200)
committerJan Kara <jack@suse.cz>
Wed, 14 Jun 2017 09:21:01 +0000 (11:21 +0200)
udf_setsize() called truncate_setsize() with i_data_sem held. Thus
truncate_pagecache() called from truncate_setsize() could lock a page
under i_data_sem which can deadlock as page lock ranks below
i_data_sem - e. g. writeback can hold page lock and try to acquire
i_data_sem to map a block.

Fix the problem by moving truncate_setsize() calls from under
i_data_sem. It is safe for us to change i_size without holding
i_data_sem as all the places that depend on i_size being stable already
hold inode_lock.

CC: stable@vger.kernel.org
Fixes: 7e49b6f2480cb9a9e7322a91592e56a5c85361f5
Signed-off-by: Jan Kara <jack@suse.cz>
fs/udf/inode.c

index 98c510e172032df27a92b296507a3c1cb3017cdb..18fdb9d90812a2849376de612ac2a52bc487324e 100644 (file)
@@ -1222,8 +1222,8 @@ int udf_setsize(struct inode *inode, loff_t newsize)
                        return err;
                }
 set_size:
                        return err;
                }
 set_size:
-               truncate_setsize(inode, newsize);
                up_write(&iinfo->i_data_sem);
                up_write(&iinfo->i_data_sem);
+               truncate_setsize(inode, newsize);
        } else {
                if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
                        down_write(&iinfo->i_data_sem);
        } else {
                if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
                        down_write(&iinfo->i_data_sem);
@@ -1240,9 +1240,9 @@ set_size:
                                          udf_get_block);
                if (err)
                        return err;
                                          udf_get_block);
                if (err)
                        return err;
+               truncate_setsize(inode, newsize);
                down_write(&iinfo->i_data_sem);
                udf_clear_extent_cache(inode);
                down_write(&iinfo->i_data_sem);
                udf_clear_extent_cache(inode);
-               truncate_setsize(inode, newsize);
                udf_truncate_extents(inode);
                up_write(&iinfo->i_data_sem);
        }
                udf_truncate_extents(inode);
                up_write(&iinfo->i_data_sem);
        }