2008-01-29 16:53:40 +03:00
/*
2010-09-03 13:56:17 +04:00
* Functions to sequence FLUSH and FUA writes .
2008-01-29 16:53:40 +03:00
*/
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/bio.h>
# include <linux/blkdev.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/gfp.h>
2008-01-29 16:53:40 +03:00
# include "blk.h"
2010-09-03 13:56:17 +04:00
/* FLUSH/FUA sequences */
enum {
QUEUE_FSEQ_STARTED = ( 1 < < 0 ) , /* flushing in progress */
QUEUE_FSEQ_PREFLUSH = ( 1 < < 1 ) , /* pre-flushing in progress */
QUEUE_FSEQ_DATA = ( 1 < < 2 ) , /* data write in progress */
QUEUE_FSEQ_POSTFLUSH = ( 1 < < 3 ) , /* post-flushing in progress */
QUEUE_FSEQ_DONE = ( 1 < < 4 ) ,
} ;
2010-09-03 13:56:16 +04:00
static struct request * queue_next_fseq ( struct request_queue * q ) ;
2010-09-03 13:56:16 +04:00
2010-09-03 13:56:16 +04:00
unsigned blk_flush_cur_seq ( struct request_queue * q )
2008-01-29 16:53:40 +03:00
{
2010-09-03 13:56:16 +04:00
if ( ! q - > flush_seq )
2008-01-29 16:53:40 +03:00
return 0 ;
2010-09-03 13:56:16 +04:00
return 1 < < ffz ( q - > flush_seq ) ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:16 +04:00
static struct request * blk_flush_complete_seq ( struct request_queue * q ,
unsigned seq , int error )
2008-01-29 16:53:40 +03:00
{
2010-09-03 13:56:16 +04:00
struct request * next_rq = NULL ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:16 +04:00
if ( error & & ! q - > flush_err )
q - > flush_err = error ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:16 +04:00
BUG_ON ( q - > flush_seq & seq ) ;
q - > flush_seq | = seq ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:16 +04:00
if ( blk_flush_cur_seq ( q ) ! = QUEUE_FSEQ_DONE ) {
/* not complete yet, queue the next flush sequence */
next_rq = queue_next_fseq ( q ) ;
2010-09-03 13:56:16 +04:00
} else {
2010-09-03 13:56:16 +04:00
/* complete this flush request */
__blk_end_request_all ( q - > orig_flush_rq , q - > flush_err ) ;
q - > orig_flush_rq = NULL ;
q - > flush_seq = 0 ;
/* dispatch the next flush if there's one */
if ( ! list_empty ( & q - > pending_flushes ) ) {
next_rq = list_entry_rq ( q - > pending_flushes . next ) ;
2010-09-03 13:56:16 +04:00
list_move ( & next_rq - > queuelist , & q - > queue_head ) ;
}
}
return next_rq ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:17 +04:00
static void blk_flush_complete_seq_end_io ( struct request_queue * q ,
unsigned seq , int error )
{
bool was_empty = elv_queue_empty ( q ) ;
struct request * next_rq ;
next_rq = blk_flush_complete_seq ( q , seq , error ) ;
/*
* Moving a request silently to empty queue_head may stall the
* queue . Kick the queue in those cases .
*/
if ( was_empty & & next_rq )
__blk_run_queue ( q ) ;
}
2008-01-29 16:53:40 +03:00
static void pre_flush_end_io ( struct request * rq , int error )
{
elv_completed_request ( rq - > q , rq ) ;
2010-09-03 13:56:17 +04:00
blk_flush_complete_seq_end_io ( rq - > q , QUEUE_FSEQ_PREFLUSH , error ) ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:16 +04:00
static void flush_data_end_io ( struct request * rq , int error )
2008-01-29 16:53:40 +03:00
{
elv_completed_request ( rq - > q , rq ) ;
2010-09-03 13:56:17 +04:00
blk_flush_complete_seq_end_io ( rq - > q , QUEUE_FSEQ_DATA , error ) ;
2008-01-29 16:53:40 +03:00
}
static void post_flush_end_io ( struct request * rq , int error )
{
elv_completed_request ( rq - > q , rq ) ;
2010-09-03 13:56:17 +04:00
blk_flush_complete_seq_end_io ( rq - > q , QUEUE_FSEQ_POSTFLUSH , error ) ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:17 +04:00
static void init_flush_request ( struct request * rq , struct gendisk * disk )
2008-01-29 16:53:40 +03:00
{
2010-07-09 04:38:24 +04:00
rq - > cmd_type = REQ_TYPE_FS ;
2010-09-03 13:56:17 +04:00
rq - > cmd_flags = WRITE_FLUSH ;
2010-09-03 13:56:17 +04:00
rq - > rq_disk = disk ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:16 +04:00
static struct request * queue_next_fseq ( struct request_queue * q )
2008-01-29 16:53:40 +03:00
{
2010-09-03 13:56:17 +04:00
struct request * orig_rq = q - > orig_flush_rq ;
2010-09-03 13:56:16 +04:00
struct request * rq = & q - > flush_rq ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:17 +04:00
blk_rq_init ( q , rq ) ;
2010-09-03 13:56:16 +04:00
switch ( blk_flush_cur_seq ( q ) ) {
case QUEUE_FSEQ_PREFLUSH :
2010-09-03 13:56:17 +04:00
init_flush_request ( rq , orig_rq - > rq_disk ) ;
rq - > end_io = pre_flush_end_io ;
2010-09-03 13:56:16 +04:00
break ;
2010-09-03 13:56:16 +04:00
case QUEUE_FSEQ_DATA :
2010-09-03 13:56:17 +04:00
init_request_from_bio ( rq , orig_rq - > bio ) ;
2010-09-03 13:56:17 +04:00
/*
* orig_rq - > rq_disk may be different from
* bio - > bi_bdev - > bd_disk if orig_rq got here through
* remapping drivers . Make sure rq - > rq_disk points
* to the same one as orig_rq .
*/
rq - > rq_disk = orig_rq - > rq_disk ;
2010-09-03 13:56:17 +04:00
rq - > cmd_flags & = ~ ( REQ_FLUSH | REQ_FUA ) ;
rq - > cmd_flags | = orig_rq - > cmd_flags & ( REQ_FLUSH | REQ_FUA ) ;
2010-09-03 13:56:16 +04:00
rq - > end_io = flush_data_end_io ;
2010-09-03 13:56:16 +04:00
break ;
2010-09-03 13:56:16 +04:00
case QUEUE_FSEQ_POSTFLUSH :
2010-09-03 13:56:17 +04:00
init_flush_request ( rq , orig_rq - > rq_disk ) ;
rq - > end_io = post_flush_end_io ;
2010-09-03 13:56:16 +04:00
break ;
default :
BUG ( ) ;
}
2010-09-03 13:56:17 +04:00
elv_insert ( q , rq , ELEVATOR_INSERT_FRONT ) ;
2010-09-03 13:56:16 +04:00
return rq ;
2008-01-29 16:53:40 +03:00
}
2010-09-03 13:56:16 +04:00
struct request * blk_do_flush ( struct request_queue * q , struct request * rq )
2008-01-29 16:53:40 +03:00
{
2010-09-03 13:56:17 +04:00
unsigned int fflags = q - > flush_flags ; /* may change, cache it */
bool has_flush = fflags & REQ_FLUSH , has_fua = fflags & REQ_FUA ;
bool do_preflush = has_flush & & ( rq - > cmd_flags & REQ_FLUSH ) ;
bool do_postflush = has_flush & & ! has_fua & & ( rq - > cmd_flags & REQ_FUA ) ;
2010-09-03 13:56:16 +04:00
unsigned skip = 0 ;
2010-09-03 13:56:17 +04:00
/*
* Special case . If there ' s data but flush is not necessary ,
* the request can be issued directly .
*
* Flush w / o data should be able to be issued directly too but
* currently some drivers assume that rq - > bio contains
* non - zero data if it isn ' t NULL and empty FLUSH requests
* getting here usually have bio ' s without data .
*/
if ( blk_rq_sectors ( rq ) & & ! do_preflush & & ! do_postflush ) {
rq - > cmd_flags & = ~ REQ_FLUSH ;
if ( ! has_fua )
rq - > cmd_flags & = ~ REQ_FUA ;
2010-09-03 13:56:16 +04:00
return rq ;
2010-09-03 13:56:17 +04:00
}
2010-09-03 13:56:16 +04:00
2010-09-03 13:56:17 +04:00
/*
* Sequenced flushes can ' t be processed in parallel . If
* another one is already in progress , queue for later
* processing .
*/
2010-09-03 13:56:16 +04:00
if ( q - > flush_seq ) {
list_move_tail ( & rq - > queuelist , & q - > pending_flushes ) ;
2010-09-03 13:56:16 +04:00
return NULL ;
}
2008-01-29 16:53:40 +03:00
/*
2010-09-03 13:56:16 +04:00
* Start a new flush sequence
2008-01-29 16:53:40 +03:00
*/
2010-09-03 13:56:16 +04:00
q - > flush_err = 0 ;
q - > flush_seq | = QUEUE_FSEQ_STARTED ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:17 +04:00
/* adjust FLUSH/FUA of the original request and stash it away */
rq - > cmd_flags & = ~ REQ_FLUSH ;
if ( ! has_fua )
rq - > cmd_flags & = ~ REQ_FUA ;
2010-09-03 13:56:16 +04:00
blk_dequeue_request ( rq ) ;
2010-09-03 13:56:16 +04:00
q - > orig_flush_rq = rq ;
2008-01-29 16:53:40 +03:00
2010-09-03 13:56:17 +04:00
/* skip unneded sequences and return the first one */
if ( ! do_preflush )
2010-09-03 13:56:16 +04:00
skip | = QUEUE_FSEQ_PREFLUSH ;
2010-09-03 13:56:17 +04:00
if ( ! blk_rq_sectors ( rq ) )
2010-09-03 13:56:16 +04:00
skip | = QUEUE_FSEQ_DATA ;
2010-09-03 13:56:17 +04:00
if ( ! do_postflush )
2010-09-03 13:56:16 +04:00
skip | = QUEUE_FSEQ_POSTFLUSH ;
return blk_flush_complete_seq ( q , skip , 0 ) ;
2008-01-29 16:53:40 +03:00
}
static void bio_end_empty_barrier ( struct bio * bio , int err )
{
2008-03-04 13:47:46 +03:00
if ( err ) {
if ( err = = - EOPNOTSUPP )
set_bit ( BIO_EOPNOTSUPP , & bio - > bi_flags ) ;
2008-01-29 16:53:40 +03:00
clear_bit ( BIO_UPTODATE , & bio - > bi_flags ) ;
2008-03-04 13:47:46 +03:00
}
2010-04-28 17:55:07 +04:00
if ( bio - > bi_private )
complete ( bio - > bi_private ) ;
bio_put ( bio ) ;
2008-01-29 16:53:40 +03:00
}
/**
* blkdev_issue_flush - queue a flush
* @ bdev : blockdev to issue flush for
2010-04-28 17:55:06 +04:00
* @ gfp_mask : memory allocation flags ( for bio_alloc )
2008-01-29 16:53:40 +03:00
* @ error_sector : error sector
2010-04-28 17:55:06 +04:00
* @ flags : BLKDEV_IFL_ * flags to control behaviour
2008-01-29 16:53:40 +03:00
*
* Description :
* Issue a flush for the block device in question . Caller can supply
* room for storing the error offset in case of a flush error , if they
2010-04-28 17:55:07 +04:00
* wish to . If WAIT flag is not passed then caller may check only what
* request was pushed in some internal queue for later handling .
2008-01-29 16:53:40 +03:00
*/
2010-04-28 17:55:06 +04:00
int blkdev_issue_flush ( struct block_device * bdev , gfp_t gfp_mask ,
sector_t * error_sector , unsigned long flags )
2008-01-29 16:53:40 +03:00
{
DECLARE_COMPLETION_ONSTACK ( wait ) ;
struct request_queue * q ;
struct bio * bio ;
2010-04-28 17:55:06 +04:00
int ret = 0 ;
2008-01-29 16:53:40 +03:00
if ( bdev - > bd_disk = = NULL )
return - ENXIO ;
q = bdev_get_queue ( bdev ) ;
if ( ! q )
return - ENXIO ;
2010-07-13 11:50:50 +04:00
/*
* some block devices may not have their queue correctly set up here
* ( e . g . loop device without a backing file ) and so issuing a flush
* here will panic . Ensure there is a request function before issuing
* the barrier .
*/
if ( ! q - > make_request_fn )
return - ENXIO ;
2010-04-28 17:55:06 +04:00
bio = bio_alloc ( gfp_mask , 0 ) ;
2008-01-29 16:53:40 +03:00
bio - > bi_end_io = bio_end_empty_barrier ;
bio - > bi_bdev = bdev ;
2010-04-28 17:55:07 +04:00
if ( test_bit ( BLKDEV_WAIT , & flags ) )
bio - > bi_private = & wait ;
2008-01-29 16:53:40 +03:00
2010-04-28 17:55:07 +04:00
bio_get ( bio ) ;
submit_bio ( WRITE_BARRIER , bio ) ;
if ( test_bit ( BLKDEV_WAIT , & flags ) ) {
wait_for_completion ( & wait ) ;
/*
* The driver must store the error location in - > bi_sector , if
* it supports it . For non - stacked drivers , this should be
* copied from blk_rq_pos ( rq ) .
*/
if ( error_sector )
* error_sector = bio - > bi_sector ;
}
2008-01-29 16:53:40 +03:00
2008-03-04 13:47:46 +03:00
if ( bio_flagged ( bio , BIO_EOPNOTSUPP ) )
ret = - EOPNOTSUPP ;
else if ( ! bio_flagged ( bio , BIO_UPTODATE ) )
2008-01-29 16:53:40 +03:00
ret = - EIO ;
bio_put ( bio ) ;
return ret ;
}
EXPORT_SYMBOL ( blkdev_issue_flush ) ;