2009-06-18 03:24:03 +04:00
/*
* Copyright ( c ) 2008 , 2009 NEC Software Tohoku , Ltd .
* Written by Takashi Sato < t - sato @ yk . jp . nec . com >
* Akira Fujita < a - fujita @ rs . jp . nec . com >
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*/
# include <linux/fs.h>
# include <linux/quotaops.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2009-06-18 03:24:03 +04:00
# include "ext4_jbd2.h"
# include "ext4.h"
2012-11-28 22:03:30 +04:00
# include "ext4_extents.h"
2009-06-18 03:24:03 +04:00
2009-09-16 21:46:38 +04:00
/**
* get_ext_path - Find an extent path for designated logical block number .
*
* @ inode : an inode which is searched
* @ lblock : logical block number to find an extent path
* @ path : pointer to an extent path pointer ( for output )
*
2014-09-01 22:43:09 +04:00
* ext4_find_extent wrapper . Return 0 on success , or a negative error value
2009-09-16 21:46:38 +04:00
* on failure .
*/
static inline int
get_ext_path ( struct inode * inode , ext4_lblk_t lblock ,
2014-09-01 22:42:09 +04:00
struct ext4_ext_path * * ppath )
2009-09-16 21:46:38 +04:00
{
2013-03-18 19:40:19 +04:00
struct ext4_ext_path * path ;
2009-09-16 21:46:38 +04:00
2014-09-01 22:43:09 +04:00
path = ext4_find_extent ( inode , lblock , ppath , EXT4_EX_NOCACHE ) ;
2013-03-18 19:40:19 +04:00
if ( IS_ERR ( path ) )
2014-09-01 22:42:09 +04:00
return PTR_ERR ( path ) ;
if ( path [ ext_depth ( inode ) ] . p_ext = = NULL ) {
ext4_ext_drop_refs ( path ) ;
kfree ( path ) ;
* ppath = NULL ;
return - ENODATA ;
}
* ppath = path ;
return 0 ;
2009-09-16 21:46:38 +04:00
}
2009-06-18 03:24:03 +04:00
/**
2013-04-08 20:54:05 +04:00
* ext4_double_down_write_data_sem - Acquire two inodes ' write lock
* of i_data_sem
2009-06-18 03:24:03 +04:00
*
2012-09-26 20:32:19 +04:00
* Acquire write lock of i_data_sem of the two inodes
2009-06-18 03:24:03 +04:00
*/
2013-04-08 20:54:05 +04:00
void
ext4_double_down_write_data_sem ( struct inode * first , struct inode * second )
2009-06-18 03:24:03 +04:00
{
2012-09-26 20:32:19 +04:00
if ( first < second ) {
down_write ( & EXT4_I ( first ) - > i_data_sem ) ;
down_write_nested ( & EXT4_I ( second ) - > i_data_sem , SINGLE_DEPTH_NESTING ) ;
} else {
down_write ( & EXT4_I ( second ) - > i_data_sem ) ;
down_write_nested ( & EXT4_I ( first ) - > i_data_sem , SINGLE_DEPTH_NESTING ) ;
2009-06-18 03:24:03 +04:00
}
}
/**
2013-04-08 20:54:05 +04:00
* ext4_double_up_write_data_sem - Release two inodes ' write lock of i_data_sem
2009-06-18 03:24:03 +04:00
*
* @ orig_inode : original inode structure to be released its lock first
* @ donor_inode : donor inode structure to be released its lock second
ext4: fix lock order problem in ext4_move_extents()
ext4_move_extents() checks the logical block contiguousness
of original file with ext4_find_extent() and mext_next_extent().
Therefore the extent which ext4_ext_path structure indicates
must not be changed between above functions.
But in current implementation, there is no i_data_sem protection
between ext4_ext_find_extent() and mext_next_extent(). So the extent
which ext4_ext_path structure indicates may be overwritten by
delalloc. As a result, ext4_move_extents() will exchange wrong blocks
between original and donor files. I change the place where
acquire/release i_data_sem to solve this problem.
Moreover, I changed move_extent_per_page() to start transaction first,
and then acquire i_data_sem. Without this change, there is a
possibility of the deadlock between mmap() and ext4_move_extents():
* NOTE: "A", "B" and "C" mean different processes
A-1: ext4_ext_move_extents() acquires i_data_sem of two inodes.
B: do_page_fault() starts the transaction (T),
and then tries to acquire i_data_sem.
But process "A" is already holding it, so it is kept waiting.
C: While "A" and "B" running, kjournald2 tries to commit transaction (T)
but it is under updating, so kjournald2 waits for it.
A-2: Call ext4_journal_start with holding i_data_sem,
but transaction (T) is locked.
Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-11-23 15:24:43 +03:00
* Release write lock of i_data_sem of two inodes ( orig and donor ) .
2009-06-18 03:24:03 +04:00
*/
2013-04-08 20:54:05 +04:00
void
ext4_double_up_write_data_sem ( struct inode * orig_inode ,
struct inode * donor_inode )
2009-06-18 03:24:03 +04:00
{
up_write ( & EXT4_I ( orig_inode ) - > i_data_sem ) ;
up_write ( & EXT4_I ( donor_inode ) - > i_data_sem ) ;
}
2012-09-26 20:54:52 +04:00
/**
* mext_check_coverage - Check that all extents in range has the same type
*
* @ inode : inode in question
* @ from : block offset of inode
* @ count : block count to be checked
2014-04-21 07:45:47 +04:00
* @ unwritten : extents expected to be unwritten
2012-09-26 20:54:52 +04:00
* @ err : pointer to save error value
*
* Return 1 if all extents in range has expected type , and zero otherwise .
*/
static int
mext_check_coverage ( struct inode * inode , ext4_lblk_t from , ext4_lblk_t count ,
2014-04-21 07:45:47 +04:00
int unwritten , int * err )
2012-09-26 20:54:52 +04:00
{
struct ext4_ext_path * path = NULL ;
struct ext4_extent * ext ;
2013-03-18 19:40:19 +04:00
int ret = 0 ;
2012-09-26 20:54:52 +04:00
ext4_lblk_t last = from + count ;
while ( from < last ) {
* err = get_ext_path ( inode , from , & path ) ;
if ( * err )
2013-03-18 19:40:19 +04:00
goto out ;
2012-09-26 20:54:52 +04:00
ext = path [ ext_depth ( inode ) ] . p_ext ;
2014-04-21 07:45:47 +04:00
if ( unwritten ! = ext4_ext_is_unwritten ( ext ) )
2013-03-18 19:40:19 +04:00
goto out ;
2012-09-26 20:54:52 +04:00
from + = ext4_ext_get_actual_len ( ext ) ;
ext4_ext_drop_refs ( path ) ;
}
2013-03-18 19:40:19 +04:00
ret = 1 ;
out :
2014-09-01 22:39:09 +04:00
ext4_ext_drop_refs ( path ) ;
kfree ( path ) ;
2013-03-18 19:40:19 +04:00
return ret ;
2012-09-26 20:54:52 +04:00
}
2012-09-26 20:52:07 +04:00
/**
* mext_page_double_lock - Grab and lock pages on both @ inode1 and @ inode2
*
* @ inode1 : the inode structure
* @ inode2 : the inode structure
2014-10-12 03:56:34 +04:00
* @ index1 : page index
* @ index2 : page index
2012-09-26 20:52:07 +04:00
* @ page : result page vector
*
* Grab two locked pages for inode ' s by inode order
*/
static int
mext_page_double_lock ( struct inode * inode1 , struct inode * inode2 ,
2014-08-31 07:52:19 +04:00
pgoff_t index1 , pgoff_t index2 , struct page * page [ 2 ] )
2012-09-26 20:52:07 +04:00
{
struct address_space * mapping [ 2 ] ;
unsigned fl = AOP_FLAG_NOFS ;
BUG_ON ( ! inode1 | | ! inode2 ) ;
if ( inode1 < inode2 ) {
mapping [ 0 ] = inode1 - > i_mapping ;
mapping [ 1 ] = inode2 - > i_mapping ;
} else {
2014-08-31 07:52:19 +04:00
pgoff_t tmp = index1 ;
index1 = index2 ;
index2 = tmp ;
2012-09-26 20:52:07 +04:00
mapping [ 0 ] = inode2 - > i_mapping ;
mapping [ 1 ] = inode1 - > i_mapping ;
}
2014-08-31 07:52:19 +04:00
page [ 0 ] = grab_cache_page_write_begin ( mapping [ 0 ] , index1 , fl ) ;
2012-09-26 20:52:07 +04:00
if ( ! page [ 0 ] )
return - ENOMEM ;
2014-08-31 07:52:19 +04:00
page [ 1 ] = grab_cache_page_write_begin ( mapping [ 1 ] , index2 , fl ) ;
2012-09-26 20:52:07 +04:00
if ( ! page [ 1 ] ) {
unlock_page ( page [ 0 ] ) ;
page_cache_release ( page [ 0 ] ) ;
return - ENOMEM ;
}
2013-04-12 07:24:58 +04:00
/*
* grab_cache_page_write_begin ( ) may not wait on page ' s writeback if
* BDI not demand that . But it is reasonable to be very conservative
* here and explicitly wait on page ' s writeback
*/
wait_on_page_writeback ( page [ 0 ] ) ;
wait_on_page_writeback ( page [ 1 ] ) ;
2012-09-26 20:52:07 +04:00
if ( inode1 > inode2 ) {
struct page * tmp ;
tmp = page [ 0 ] ;
page [ 0 ] = page [ 1 ] ;
page [ 1 ] = tmp ;
}
return 0 ;
}
/* Force page buffers uptodate w/o dropping page's lock */
static int
mext_page_mkuptodate ( struct page * page , unsigned from , unsigned to )
{
struct inode * inode = page - > mapping - > host ;
sector_t block ;
struct buffer_head * bh , * head , * arr [ MAX_BUF_PER_PAGE ] ;
unsigned int blocksize , block_start , block_end ;
int i , err , nr = 0 , partial = 0 ;
BUG_ON ( ! PageLocked ( page ) ) ;
BUG_ON ( PageWriteback ( page ) ) ;
if ( PageUptodate ( page ) )
return 0 ;
blocksize = 1 < < inode - > i_blkbits ;
if ( ! page_has_buffers ( page ) )
create_empty_buffers ( page , blocksize , 0 ) ;
head = page_buffers ( page ) ;
block = ( sector_t ) page - > index < < ( PAGE_CACHE_SHIFT - inode - > i_blkbits ) ;
for ( bh = head , block_start = 0 ; bh ! = head | | ! block_start ;
block + + , block_start = block_end , bh = bh - > b_this_page ) {
block_end = block_start + blocksize ;
if ( block_end < = from | | block_start > = to ) {
if ( ! buffer_uptodate ( bh ) )
partial = 1 ;
continue ;
}
if ( buffer_uptodate ( bh ) )
continue ;
if ( ! buffer_mapped ( bh ) ) {
err = ext4_get_block ( inode , block , bh , 0 ) ;
if ( err ) {
SetPageError ( page ) ;
return err ;
}
if ( ! buffer_mapped ( bh ) ) {
zero_user ( page , block_start , blocksize ) ;
2014-02-18 05:46:40 +04:00
set_buffer_uptodate ( bh ) ;
2012-09-26 20:52:07 +04:00
continue ;
}
}
BUG_ON ( nr > = MAX_BUF_PER_PAGE ) ;
arr [ nr + + ] = bh ;
}
/* No io required */
if ( ! nr )
goto out ;
for ( i = 0 ; i < nr ; i + + ) {
bh = arr [ i ] ;
if ( ! bh_uptodate_or_lock ( bh ) ) {
err = bh_submit_read ( bh ) ;
if ( err )
return err ;
}
}
out :
if ( ! partial )
SetPageUptodate ( page ) ;
return 0 ;
}
2009-06-18 03:24:03 +04:00
/**
* move_extent_per_page - Move extent data per page
*
* @ o_filp : file structure of original file
* @ donor_inode : donor inode
* @ orig_page_offset : page index on original file
2014-10-12 03:56:34 +04:00
* @ donor_page_offset : page index on donor file
2009-06-18 03:24:03 +04:00
* @ data_offset_in_page : block index where data swapping starts
* @ block_len_in_page : the number of blocks to be swapped
2014-04-21 07:45:47 +04:00
* @ unwritten : orig extent is unwritten or not
2009-11-23 15:25:48 +03:00
* @ err : pointer to save return value
2009-06-18 03:24:03 +04:00
*
* Save the data in original inode blocks and replace original inode extents
2014-10-12 03:56:34 +04:00
* with donor inode extents by calling ext4_swap_extents ( ) .
2009-11-23 15:25:48 +03:00
* Finally , write out the saved data in new original inode blocks . Return
* replaced block count .
2009-06-18 03:24:03 +04:00
*/
static int
2009-09-06 07:12:41 +04:00
move_extent_per_page ( struct file * o_filp , struct inode * donor_inode ,
2014-08-31 07:52:19 +04:00
pgoff_t orig_page_offset , pgoff_t donor_page_offset ,
int data_offset_in_page ,
int block_len_in_page , int unwritten , int * err )
2009-06-18 03:24:03 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * orig_inode = file_inode ( o_filp ) ;
2012-09-26 20:52:07 +04:00
struct page * pagep [ 2 ] = { NULL , NULL } ;
2009-06-18 03:24:03 +04:00
handle_t * handle ;
2014-08-31 07:52:19 +04:00
ext4_lblk_t orig_blk_offset , donor_blk_offset ;
2009-06-18 03:24:03 +04:00
unsigned long blocksize = orig_inode - > i_sb - > s_blocksize ;
2009-11-23 15:25:48 +03:00
unsigned int tmp_data_size , data_size , replaced_size ;
2012-09-26 20:52:07 +04:00
int err2 , jblocks , retries = 0 ;
2009-11-23 15:25:48 +03:00
int replaced_count = 0 ;
2012-09-26 20:52:07 +04:00
int from = data_offset_in_page < < orig_inode - > i_blkbits ;
2009-06-18 03:24:03 +04:00
int blocks_per_page = PAGE_CACHE_SIZE > > orig_inode - > i_blkbits ;
2014-11-05 19:52:38 +03:00
struct super_block * sb = orig_inode - > i_sb ;
2009-06-18 03:24:03 +04:00
/*
* It needs twice the amount of ordinary journal buffers because
* inode and donor_inode may change each different metadata blocks .
*/
2012-09-26 20:52:07 +04:00
again :
* err = 0 ;
2009-06-18 03:24:03 +04:00
jblocks = ext4_writepage_trans_blocks ( orig_inode ) * 2 ;
2013-02-09 06:59:22 +04:00
handle = ext4_journal_start ( orig_inode , EXT4_HT_MOVE_EXTENTS , jblocks ) ;
2009-06-18 03:24:03 +04:00
if ( IS_ERR ( handle ) ) {
2009-11-23 15:25:48 +03:00
* err = PTR_ERR ( handle ) ;
return 0 ;
2009-06-18 03:24:03 +04:00
}
orig_blk_offset = orig_page_offset * blocks_per_page +
data_offset_in_page ;
2014-08-31 07:52:19 +04:00
donor_blk_offset = donor_page_offset * blocks_per_page +
data_offset_in_page ;
2009-11-23 15:25:48 +03:00
/* Calculate data_size */
2009-06-18 03:24:03 +04:00
if ( ( orig_blk_offset + block_len_in_page - 1 ) = =
( ( orig_inode - > i_size - 1 ) > > orig_inode - > i_blkbits ) ) {
/* Replace the last block */
2009-11-23 15:25:48 +03:00
tmp_data_size = orig_inode - > i_size & ( blocksize - 1 ) ;
2009-06-18 03:24:03 +04:00
/*
2009-11-23 15:25:48 +03:00
* If data_size equal zero , it shows data_size is multiples of
2009-06-18 03:24:03 +04:00
* blocksize . So we set appropriate value .
*/
2009-11-23 15:25:48 +03:00
if ( tmp_data_size = = 0 )
tmp_data_size = blocksize ;
2009-06-18 03:24:03 +04:00
2009-11-23 15:25:48 +03:00
data_size = tmp_data_size +
2009-06-18 03:24:03 +04:00
( ( block_len_in_page - 1 ) < < orig_inode - > i_blkbits ) ;
2009-11-23 15:25:48 +03:00
} else
data_size = block_len_in_page < < orig_inode - > i_blkbits ;
replaced_size = data_size ;
2009-06-18 03:24:03 +04:00
2012-09-26 20:52:07 +04:00
* err = mext_page_double_lock ( orig_inode , donor_inode , orig_page_offset ,
2014-08-31 07:52:19 +04:00
donor_page_offset , pagep ) ;
2009-11-23 15:25:48 +03:00
if ( unlikely ( * err < 0 ) )
2012-09-26 20:52:07 +04:00
goto stop_journal ;
2012-09-26 20:54:52 +04:00
/*
2014-04-21 07:45:47 +04:00
* If orig extent was unwritten it can become initialized
2012-09-26 20:54:52 +04:00
* at any time after i_data_sem was dropped , in order to
* serialize with delalloc we have recheck extent while we
* hold page ' s lock , if it is still the case data copy is not
* necessary , just swap data blocks between orig and donor .
*/
2014-04-21 07:45:47 +04:00
if ( unwritten ) {
2013-04-08 20:54:05 +04:00
ext4_double_down_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:54:52 +04:00
/* If any of extents in range became initialized we have to
* fallback to data copying */
2014-04-21 07:45:47 +04:00
unwritten = mext_check_coverage ( orig_inode , orig_blk_offset ,
block_len_in_page , 1 , err ) ;
2012-09-26 20:54:52 +04:00
if ( * err )
goto drop_data_sem ;
2009-06-18 03:24:03 +04:00
2014-08-31 07:52:19 +04:00
unwritten & = mext_check_coverage ( donor_inode , donor_blk_offset ,
2014-04-21 07:45:47 +04:00
block_len_in_page , 1 , err ) ;
2012-09-26 20:54:52 +04:00
if ( * err )
goto drop_data_sem ;
2014-04-21 07:45:47 +04:00
if ( ! unwritten ) {
2013-04-08 20:54:05 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:54:52 +04:00
goto data_copy ;
}
if ( ( page_has_private ( pagep [ 0 ] ) & &
! try_to_release_page ( pagep [ 0 ] , 0 ) ) | |
( page_has_private ( pagep [ 1 ] ) & &
! try_to_release_page ( pagep [ 1 ] , 0 ) ) ) {
* err = - EBUSY ;
goto drop_data_sem ;
}
2014-08-31 07:52:19 +04:00
replaced_count = ext4_swap_extents ( handle , orig_inode ,
donor_inode , orig_blk_offset ,
donor_blk_offset ,
block_len_in_page , 1 , err ) ;
2012-09-26 20:54:52 +04:00
drop_data_sem :
2013-04-08 20:54:05 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:54:52 +04:00
goto unlock_pages ;
}
data_copy :
2012-09-26 20:52:07 +04:00
* err = mext_page_mkuptodate ( pagep [ 0 ] , from , from + replaced_size ) ;
if ( * err )
goto unlock_pages ;
/* At this point all buffers in range are uptodate, old mapping layout
* is no longer required , try to drop it now . */
if ( ( page_has_private ( pagep [ 0 ] ) & & ! try_to_release_page ( pagep [ 0 ] , 0 ) ) | |
( page_has_private ( pagep [ 1 ] ) & & ! try_to_release_page ( pagep [ 1 ] , 0 ) ) ) {
* err = - EBUSY ;
goto unlock_pages ;
2009-06-18 03:24:03 +04:00
}
2014-07-28 06:32:27 +04:00
ext4_double_down_write_data_sem ( orig_inode , donor_inode ) ;
2014-08-31 07:52:19 +04:00
replaced_count = ext4_swap_extents ( handle , orig_inode , donor_inode ,
orig_blk_offset , donor_blk_offset ,
block_len_in_page , 1 , err ) ;
2014-07-28 06:32:27 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:52:07 +04:00
if ( * err ) {
2009-11-23 15:25:48 +03:00
if ( replaced_count ) {
block_len_in_page = replaced_count ;
replaced_size =
block_len_in_page < < orig_inode - > i_blkbits ;
2009-11-24 18:31:56 +03:00
} else
2012-09-26 20:52:07 +04:00
goto unlock_pages ;
2009-06-18 03:24:03 +04:00
}
2012-09-26 20:52:07 +04:00
/* Perform all necessary steps similar write_begin()/write_end()
* but keeping in mind that i_size will not change */
2013-04-12 07:24:58 +04:00
* err = __block_write_begin ( pagep [ 0 ] , from , replaced_size ,
2012-09-26 20:52:07 +04:00
ext4_get_block ) ;
if ( ! * err )
* err = block_commit_write ( pagep [ 0 ] , from , from + replaced_size ) ;
2009-06-18 03:24:03 +04:00
2012-09-26 20:52:07 +04:00
if ( unlikely ( * err < 0 ) )
goto repair_branches ;
/* Even in case of data=writeback it is reasonable to pin
* inode to transaction , to prevent unexpected data loss */
* err = ext4_jbd2_file_inode ( handle , orig_inode ) ;
unlock_pages :
unlock_page ( pagep [ 0 ] ) ;
page_cache_release ( pagep [ 0 ] ) ;
unlock_page ( pagep [ 1 ] ) ;
page_cache_release ( pagep [ 1 ] ) ;
stop_journal :
2009-06-18 03:24:03 +04:00
ext4_journal_stop ( handle ) ;
2014-11-05 19:52:38 +03:00
if ( * err = = - ENOSPC & &
ext4_should_retry_alloc ( sb , & retries ) )
goto again ;
2012-09-26 20:52:07 +04:00
/* Buffer was busy because probably is pinned to journal transaction,
* force transaction commit may help to free it . */
2014-11-05 19:52:38 +03:00
if ( * err = = - EBUSY & & retries + + < 4 & & EXT4_SB ( sb ) - > s_journal & &
jbd2_journal_force_commit_nested ( EXT4_SB ( sb ) - > s_journal ) )
2012-09-26 20:52:07 +04:00
goto again ;
2009-11-23 15:25:48 +03:00
return replaced_count ;
2012-09-26 20:52:07 +04:00
repair_branches :
/*
* This should never ever happen !
* Extents are swapped already , but we are not able to copy data .
* Try to swap extents to it ' s original places
*/
2013-04-08 20:54:05 +04:00
ext4_double_down_write_data_sem ( orig_inode , donor_inode ) ;
2014-08-31 07:52:19 +04:00
replaced_count = ext4_swap_extents ( handle , donor_inode , orig_inode ,
orig_blk_offset , donor_blk_offset ,
block_len_in_page , 0 , & err2 ) ;
2013-04-08 20:54:05 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:52:07 +04:00
if ( replaced_count ! = block_len_in_page ) {
EXT4_ERROR_INODE_BLOCK ( orig_inode , ( sector_t ) ( orig_blk_offset ) ,
" Unable to copy data block, "
" data will be lost. " ) ;
* err = - EIO ;
}
replaced_count = 0 ;
goto unlock_pages ;
2009-06-18 03:24:03 +04:00
}
/**
2010-03-04 08:39:24 +03:00
* mext_check_arguments - Check whether move extent can be done
2009-06-18 03:24:03 +04:00
*
* @ orig_inode : original inode
* @ donor_inode : donor inode
* @ orig_start : logical start offset in block for orig
* @ donor_start : logical start offset in block for donor
* @ len : the number of blocks to be moved
*
* Check the arguments of ext4_move_extents ( ) whether the files can be
* exchanged with each other .
* Return 0 on success , or a negative error value on failure .
*/
static int
mext_check_arguments ( struct inode * orig_inode ,
2009-11-24 18:28:48 +03:00
struct inode * donor_inode , __u64 orig_start ,
__u64 donor_start , __u64 * len )
2009-06-18 03:24:03 +04:00
{
2014-08-31 07:52:19 +04:00
__u64 orig_eof , donor_eof ;
2009-09-16 22:28:22 +04:00
unsigned int blkbits = orig_inode - > i_blkbits ;
unsigned int blocksize = 1 < < blkbits ;
2014-08-31 07:52:19 +04:00
orig_eof = ( i_size_read ( orig_inode ) + blocksize - 1 ) > > blkbits ;
donor_eof = ( i_size_read ( donor_inode ) + blocksize - 1 ) > > blkbits ;
2009-12-07 07:38:31 +03:00
if ( donor_inode - > i_mode & ( S_ISUID | S_ISGID ) ) {
ext4_debug ( " ext4 move extent: suid or sgid is set "
" to donor file [ino:orig %lu, donor %lu] \n " ,
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
2010-06-03 06:04:39 +04:00
if ( IS_IMMUTABLE ( donor_inode ) | | IS_APPEND ( donor_inode ) )
return - EPERM ;
2009-06-18 03:24:03 +04:00
/* Ext4 move extent does not support swapfile */
if ( IS_SWAPFILE ( orig_inode ) | | IS_SWAPFILE ( donor_inode ) ) {
ext4_debug ( " ext4 move extent: The argument files should "
" not be swapfile [ino:orig %lu, donor %lu] \n " ,
orig_inode - > i_ino , donor_inode - > i_ino ) ;
2014-08-31 07:52:19 +04:00
return - EBUSY ;
2009-06-18 03:24:03 +04:00
}
/* Ext4 move extent supports only extent based file */
2010-05-17 06:00:00 +04:00
if ( ! ( ext4_test_inode_flag ( orig_inode , EXT4_INODE_EXTENTS ) ) ) {
2009-06-18 03:24:03 +04:00
ext4_debug ( " ext4 move extent: orig file is not extents "
" based file [ino:orig %lu] \n " , orig_inode - > i_ino ) ;
return - EOPNOTSUPP ;
2010-05-17 06:00:00 +04:00
} else if ( ! ( ext4_test_inode_flag ( donor_inode , EXT4_INODE_EXTENTS ) ) ) {
2009-06-18 03:24:03 +04:00
ext4_debug ( " ext4 move extent: donor file is not extents "
" based file [ino:donor %lu] \n " , donor_inode - > i_ino ) ;
return - EOPNOTSUPP ;
}
if ( ( ! orig_inode - > i_size ) | | ( ! donor_inode - > i_size ) ) {
ext4_debug ( " ext4 move extent: File size is 0 byte \n " ) ;
return - EINVAL ;
}
/* Start offset should be same */
2014-08-31 07:52:19 +04:00
if ( ( orig_start & ~ ( PAGE_MASK > > orig_inode - > i_blkbits ) ) ! =
( donor_start & ~ ( PAGE_MASK > > orig_inode - > i_blkbits ) ) ) {
2009-06-18 03:24:03 +04:00
ext4_debug ( " ext4 move extent: orig and donor's start "
2014-08-31 07:52:19 +04:00
" offset are not alligned [ino:orig %lu, donor %lu] \n " ,
2009-06-18 03:24:03 +04:00
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
2011-06-06 08:05:17 +04:00
if ( ( orig_start > = EXT_MAX_BLOCKS ) | |
2014-08-31 07:52:19 +04:00
( donor_start > = EXT_MAX_BLOCKS ) | |
2011-06-06 08:05:17 +04:00
( * len > EXT_MAX_BLOCKS ) | |
2014-08-31 07:52:19 +04:00
( donor_start + * len > = EXT_MAX_BLOCKS ) | |
2011-06-06 08:05:17 +04:00
( orig_start + * len > = EXT_MAX_BLOCKS ) ) {
2009-09-17 19:55:58 +04:00
ext4_debug ( " ext4 move extent: Can't handle over [%u] blocks "
2011-06-06 08:05:17 +04:00
" [ino:orig %lu, donor %lu] \n " , EXT_MAX_BLOCKS ,
2009-06-18 03:24:03 +04:00
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
2014-08-31 07:52:19 +04:00
if ( orig_eof < orig_start + * len - 1 )
* len = orig_eof - orig_start ;
if ( donor_eof < donor_start + * len - 1 )
* len = donor_eof - donor_start ;
2009-06-18 03:24:03 +04:00
if ( ! * len ) {
2009-11-23 15:24:50 +03:00
ext4_debug ( " ext4 move extent: len should not be 0 "
2009-06-18 03:24:03 +04:00
" [ino:orig %lu, donor %lu] \n " , orig_inode - > i_ino ,
donor_inode - > i_ino ) ;
return - EINVAL ;
}
return 0 ;
}
/**
* ext4_move_extents - Exchange the specified range of a file
*
* @ o_filp : file structure of the original file
* @ d_filp : file structure of the donor file
2014-10-12 03:56:34 +04:00
* @ orig_blk : start offset in block for orig
* @ donor_blk : start offset in block for donor
2009-06-18 03:24:03 +04:00
* @ len : the number of blocks to be moved
* @ moved_len : moved block length
*
* This function returns 0 and moved block length is set in moved_len
* if succeed , otherwise returns error value .
*
*/
int
2014-08-31 07:52:19 +04:00
ext4_move_extents ( struct file * o_filp , struct file * d_filp , __u64 orig_blk ,
__u64 donor_blk , __u64 len , __u64 * moved_len )
2009-06-18 03:24:03 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * orig_inode = file_inode ( o_filp ) ;
struct inode * donor_inode = file_inode ( d_filp ) ;
2014-08-31 07:52:19 +04:00
struct ext4_ext_path * path = NULL ;
2009-06-18 03:24:03 +04:00
int blocks_per_page = PAGE_CACHE_SIZE > > orig_inode - > i_blkbits ;
2014-08-31 07:52:19 +04:00
ext4_lblk_t o_end , o_start = orig_blk ;
ext4_lblk_t d_start = donor_blk ;
int ret ;
2009-06-18 03:24:03 +04:00
2012-09-26 20:32:19 +04:00
if ( orig_inode - > i_sb ! = donor_inode - > i_sb ) {
ext4_debug ( " ext4 move extent: The argument files "
" should be in same FS [ino:orig %lu, donor %lu] \n " ,
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
/* orig and donor should be different inodes */
if ( orig_inode = = donor_inode ) {
2009-09-28 23:58:29 +04:00
ext4_debug ( " ext4 move extent: The argument files should not "
2012-09-26 20:32:19 +04:00
" be same inode [ino:orig %lu, donor %lu] \n " ,
2009-09-28 23:58:29 +04:00
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
2010-03-04 08:34:58 +03:00
/* Regular file check */
if ( ! S_ISREG ( orig_inode - > i_mode ) | | ! S_ISREG ( donor_inode - > i_mode ) ) {
ext4_debug ( " ext4 move extent: The argument files should be "
" regular file [ino:orig %lu, donor %lu] \n " ,
orig_inode - > i_ino , donor_inode - > i_ino ) ;
return - EINVAL ;
}
2012-09-26 20:32:54 +04:00
/* TODO: This is non obvious task to swap blocks for inodes with full
jornaling enabled */
if ( ext4_should_journal_data ( orig_inode ) | |
ext4_should_journal_data ( donor_inode ) ) {
return - EINVAL ;
}
ext4: fix lock order problem in ext4_move_extents()
ext4_move_extents() checks the logical block contiguousness
of original file with ext4_find_extent() and mext_next_extent().
Therefore the extent which ext4_ext_path structure indicates
must not be changed between above functions.
But in current implementation, there is no i_data_sem protection
between ext4_ext_find_extent() and mext_next_extent(). So the extent
which ext4_ext_path structure indicates may be overwritten by
delalloc. As a result, ext4_move_extents() will exchange wrong blocks
between original and donor files. I change the place where
acquire/release i_data_sem to solve this problem.
Moreover, I changed move_extent_per_page() to start transaction first,
and then acquire i_data_sem. Without this change, there is a
possibility of the deadlock between mmap() and ext4_move_extents():
* NOTE: "A", "B" and "C" mean different processes
A-1: ext4_ext_move_extents() acquires i_data_sem of two inodes.
B: do_page_fault() starts the transaction (T),
and then tries to acquire i_data_sem.
But process "A" is already holding it, so it is kept waiting.
C: While "A" and "B" running, kjournald2 tries to commit transaction (T)
but it is under updating, so kjournald2 waits for it.
A-2: Call ext4_journal_start with holding i_data_sem,
but transaction (T) is locked.
Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-11-23 15:24:43 +03:00
/* Protect orig and donor inodes against a truncate */
2012-04-18 23:16:33 +04:00
lock_two_nondirectories ( orig_inode , donor_inode ) ;
2009-06-18 03:24:03 +04:00
2012-09-29 08:41:21 +04:00
/* Wait for all existing dio workers */
ext4_inode_block_unlocked_dio ( orig_inode ) ;
ext4_inode_block_unlocked_dio ( donor_inode ) ;
inode_dio_wait ( orig_inode ) ;
inode_dio_wait ( donor_inode ) ;
ext4: fix lock order problem in ext4_move_extents()
ext4_move_extents() checks the logical block contiguousness
of original file with ext4_find_extent() and mext_next_extent().
Therefore the extent which ext4_ext_path structure indicates
must not be changed between above functions.
But in current implementation, there is no i_data_sem protection
between ext4_ext_find_extent() and mext_next_extent(). So the extent
which ext4_ext_path structure indicates may be overwritten by
delalloc. As a result, ext4_move_extents() will exchange wrong blocks
between original and donor files. I change the place where
acquire/release i_data_sem to solve this problem.
Moreover, I changed move_extent_per_page() to start transaction first,
and then acquire i_data_sem. Without this change, there is a
possibility of the deadlock between mmap() and ext4_move_extents():
* NOTE: "A", "B" and "C" mean different processes
A-1: ext4_ext_move_extents() acquires i_data_sem of two inodes.
B: do_page_fault() starts the transaction (T),
and then tries to acquire i_data_sem.
But process "A" is already holding it, so it is kept waiting.
C: While "A" and "B" running, kjournald2 tries to commit transaction (T)
but it is under updating, so kjournald2 waits for it.
A-2: Call ext4_journal_start with holding i_data_sem,
but transaction (T) is locked.
Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-11-23 15:24:43 +03:00
/* Protect extent tree against block allocations via delalloc */
2013-04-08 20:54:05 +04:00
ext4_double_down_write_data_sem ( orig_inode , donor_inode ) ;
2009-06-18 03:24:03 +04:00
/* Check the filesystem environment whether move_extent can be done */
2014-08-31 07:52:19 +04:00
ret = mext_check_arguments ( orig_inode , donor_inode , orig_blk ,
donor_blk , & len ) ;
2012-09-26 20:32:19 +04:00
if ( ret )
2009-09-16 22:25:07 +04:00
goto out ;
2014-08-31 07:52:19 +04:00
o_end = o_start + len ;
2009-06-18 03:24:03 +04:00
2014-08-31 07:52:19 +04:00
while ( o_start < o_end ) {
struct ext4_extent * ex ;
ext4_lblk_t cur_blk , next_blk ;
pgoff_t orig_page_index , donor_page_index ;
int offset_in_page ;
int unwritten , cur_len ;
2009-06-18 03:24:03 +04:00
2014-08-31 07:52:19 +04:00
ret = get_ext_path ( orig_inode , o_start , & path ) ;
if ( ret )
2009-06-18 03:24:03 +04:00
goto out ;
2014-08-31 07:52:19 +04:00
ex = path [ path - > p_depth ] . p_ext ;
next_blk = ext4_ext_next_allocated_block ( path ) ;
cur_blk = le32_to_cpu ( ex - > ee_block ) ;
cur_len = ext4_ext_get_actual_len ( ex ) ;
/* Check hole before the start pos */
if ( cur_blk + cur_len - 1 < o_start ) {
if ( next_blk = = EXT_MAX_BLOCKS ) {
o_start = o_end ;
ret = - ENODATA ;
goto out ;
}
d_start + = next_blk - o_start ;
o_start = next_blk ;
2014-09-01 22:42:09 +04:00
continue ;
2014-08-31 07:52:19 +04:00
/* Check hole after the start pos */
} else if ( cur_blk > o_start ) {
/* Skip hole */
d_start + = cur_blk - o_start ;
o_start = cur_blk ;
/* Extent inside requested range ?*/
if ( cur_blk > = o_end )
goto out ;
} else { /* in_range(o_start, o_blk, o_len) */
cur_len + = cur_blk - o_start ;
2009-06-18 03:24:03 +04:00
}
2014-08-31 07:52:19 +04:00
unwritten = ext4_ext_is_unwritten ( ex ) ;
if ( o_end - o_start < cur_len )
cur_len = o_end - o_start ;
orig_page_index = o_start > > ( PAGE_CACHE_SHIFT -
orig_inode - > i_blkbits ) ;
donor_page_index = d_start > > ( PAGE_CACHE_SHIFT -
donor_inode - > i_blkbits ) ;
offset_in_page = o_start % blocks_per_page ;
if ( cur_len > blocks_per_page - offset_in_page )
cur_len = blocks_per_page - offset_in_page ;
ext4: fix lock order problem in ext4_move_extents()
ext4_move_extents() checks the logical block contiguousness
of original file with ext4_find_extent() and mext_next_extent().
Therefore the extent which ext4_ext_path structure indicates
must not be changed between above functions.
But in current implementation, there is no i_data_sem protection
between ext4_ext_find_extent() and mext_next_extent(). So the extent
which ext4_ext_path structure indicates may be overwritten by
delalloc. As a result, ext4_move_extents() will exchange wrong blocks
between original and donor files. I change the place where
acquire/release i_data_sem to solve this problem.
Moreover, I changed move_extent_per_page() to start transaction first,
and then acquire i_data_sem. Without this change, there is a
possibility of the deadlock between mmap() and ext4_move_extents():
* NOTE: "A", "B" and "C" mean different processes
A-1: ext4_ext_move_extents() acquires i_data_sem of two inodes.
B: do_page_fault() starts the transaction (T),
and then tries to acquire i_data_sem.
But process "A" is already holding it, so it is kept waiting.
C: While "A" and "B" running, kjournald2 tries to commit transaction (T)
but it is under updating, so kjournald2 waits for it.
A-2: Call ext4_journal_start with holding i_data_sem,
but transaction (T) is locked.
Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-11-23 15:24:43 +03:00
/*
* Up semaphore to avoid following problems :
* a . transaction deadlock among ext4_journal_start ,
* - > write_begin via pagefault , and jbd2_journal_commit
* b . racing with - > readpage , - > write_begin , and ext4_get_block
* in move_extent_per_page
*/
2013-04-08 20:54:05 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2014-08-31 07:52:19 +04:00
/* Swap original branches with new branches */
move_extent_per_page ( o_filp , donor_inode ,
orig_page_index , donor_page_index ,
offset_in_page , cur_len ,
unwritten , & ret ) ;
2013-04-08 20:54:05 +04:00
ext4_double_down_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-26 20:32:19 +04:00
if ( ret < 0 )
ext4: fix lock order problem in ext4_move_extents()
ext4_move_extents() checks the logical block contiguousness
of original file with ext4_find_extent() and mext_next_extent().
Therefore the extent which ext4_ext_path structure indicates
must not be changed between above functions.
But in current implementation, there is no i_data_sem protection
between ext4_ext_find_extent() and mext_next_extent(). So the extent
which ext4_ext_path structure indicates may be overwritten by
delalloc. As a result, ext4_move_extents() will exchange wrong blocks
between original and donor files. I change the place where
acquire/release i_data_sem to solve this problem.
Moreover, I changed move_extent_per_page() to start transaction first,
and then acquire i_data_sem. Without this change, there is a
possibility of the deadlock between mmap() and ext4_move_extents():
* NOTE: "A", "B" and "C" mean different processes
A-1: ext4_ext_move_extents() acquires i_data_sem of two inodes.
B: do_page_fault() starts the transaction (T),
and then tries to acquire i_data_sem.
But process "A" is already holding it, so it is kept waiting.
C: While "A" and "B" running, kjournald2 tries to commit transaction (T)
but it is under updating, so kjournald2 waits for it.
A-2: Call ext4_journal_start with holding i_data_sem,
but transaction (T) is locked.
Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-11-23 15:24:43 +03:00
break ;
2014-08-31 07:52:19 +04:00
o_start + = cur_len ;
d_start + = cur_len ;
2009-06-18 03:24:03 +04:00
}
2014-08-31 07:52:19 +04:00
* moved_len = o_start - orig_blk ;
if ( * moved_len > len )
* moved_len = len ;
2009-06-18 03:24:03 +04:00
out :
2009-11-24 18:19:57 +03:00
if ( * moved_len ) {
ext4_discard_preallocations ( orig_inode ) ;
ext4_discard_preallocations ( donor_inode ) ;
}
2014-09-01 22:39:09 +04:00
ext4_ext_drop_refs ( path ) ;
kfree ( path ) ;
2013-04-08 20:54:05 +04:00
ext4_double_up_write_data_sem ( orig_inode , donor_inode ) ;
2012-09-29 08:41:21 +04:00
ext4_inode_resume_unlocked_dio ( orig_inode ) ;
ext4_inode_resume_unlocked_dio ( donor_inode ) ;
2012-04-18 23:16:33 +04:00
unlock_two_nondirectories ( orig_inode , donor_inode ) ;
2009-06-18 03:24:03 +04:00
2012-09-26 20:32:19 +04:00
return ret ;
2009-06-18 03:24:03 +04:00
}