2017-03-17 09:18:50 +03:00
// SPDX-License-Identifier: GPL-2.0
/*
* Code for moving data off a device .
*/
# include "bcachefs.h"
# include "btree_update.h"
2019-01-21 23:32:13 +03:00
# include "btree_update_interior.h"
2017-03-17 09:18:50 +03:00
# include "buckets.h"
# include "extents.h"
# include "io.h"
# include "journal.h"
# include "keylist.h"
# include "migrate.h"
# include "move.h"
# include "replicas.h"
# include "super-io.h"
2018-11-01 22:10:01 +03:00
static int drop_dev_ptrs ( struct bch_fs * c , struct bkey_s k ,
2017-03-17 09:18:50 +03:00
unsigned dev_idx , int flags , bool metadata )
{
unsigned replicas = metadata ? c - > opts . metadata_replicas : c - > opts . data_replicas ;
unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST ;
unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED ;
unsigned nr_good ;
2018-11-01 22:10:01 +03:00
bch2_bkey_drop_device ( k , dev_idx ) ;
2017-03-17 09:18:50 +03:00
2018-11-01 22:10:01 +03:00
nr_good = bch2_bkey_durability ( c , k . s_c ) ;
2017-03-17 09:18:50 +03:00
if ( ( ! nr_good & & ! ( flags & lost ) ) | |
( nr_good < replicas & & ! ( flags & degraded ) ) )
return - EINVAL ;
return 0 ;
}
static int bch2_dev_usrdata_drop ( struct bch_fs * c , unsigned dev_idx , int flags )
{
2019-03-14 03:49:16 +03:00
struct btree_trans trans ;
struct btree_iter * iter ;
2017-03-17 09:18:50 +03:00
struct bkey_s_c k ;
BKEY_PADDED ( key ) tmp ;
int ret = 0 ;
2019-05-15 17:54:43 +03:00
bch2_trans_init ( & trans , c , BTREE_ITER_MAX , 0 ) ;
2019-03-14 03:49:16 +03:00
iter = bch2_trans_get_iter ( & trans , BTREE_ID_EXTENTS ,
POS_MIN , BTREE_ITER_PREFETCH ) ;
while ( ( k = bch2_btree_iter_peek ( iter ) ) . k & &
2019-03-28 05:03:30 +03:00
! ( ret = bkey_err ( k ) ) ) {
2019-07-25 20:52:14 +03:00
if ( ! bch2_bkey_has_device ( k , dev_idx ) ) {
2018-11-01 22:10:01 +03:00
ret = bch2_mark_bkey_replicas ( c , k ) ;
2017-03-17 09:18:50 +03:00
if ( ret )
break ;
2019-03-14 03:49:16 +03:00
bch2_btree_iter_next ( iter ) ;
2017-03-17 09:18:50 +03:00
continue ;
}
bkey_reassemble ( & tmp . key , k ) ;
2018-11-01 22:10:01 +03:00
ret = drop_dev_ptrs ( c , bkey_i_to_s ( & tmp . key ) ,
dev_idx , flags , false ) ;
2017-03-17 09:18:50 +03:00
if ( ret )
break ;
/*
* If the new extent no longer has any pointers , bch2_extent_normalize ( )
* will do the appropriate thing with it ( turning it into a
2018-11-01 22:10:01 +03:00
* KEY_TYPE_error key , or just a discard if it was a cached extent )
2017-03-17 09:18:50 +03:00
*/
2018-11-01 22:10:01 +03:00
bch2_extent_normalize ( c , bkey_i_to_s ( & tmp . key ) ) ;
2017-03-17 09:18:50 +03:00
2019-03-14 03:49:16 +03:00
/* XXX not sketchy at all */
iter - > pos = bkey_start_pos ( & tmp . key . k ) ;
bch2_trans_update ( & trans , BTREE_INSERT_ENTRY ( iter , & tmp . key ) ) ;
2017-03-17 09:18:50 +03:00
2019-03-14 03:49:16 +03:00
ret = bch2_trans_commit ( & trans , NULL , NULL ,
BTREE_INSERT_ATOMIC |
BTREE_INSERT_NOFAIL ) ;
2017-03-17 09:18:50 +03:00
/*
* don ' t want to leave ret = = - EINTR , since if we raced and
* something else overwrote the key we could spuriously return
* - EINTR below :
*/
if ( ret = = - EINTR )
ret = 0 ;
if ( ret )
break ;
}
2019-05-01 00:15:39 +03:00
ret = bch2_trans_exit ( & trans ) ? : ret ;
2019-03-17 23:49:59 +03:00
2019-05-01 00:15:39 +03:00
BUG_ON ( ret = = - EINTR ) ;
2017-03-17 09:18:50 +03:00
return ret ;
}
static int bch2_dev_metadata_drop ( struct bch_fs * c , unsigned dev_idx , int flags )
{
2019-03-25 22:10:15 +03:00
struct btree_trans trans ;
struct btree_iter * iter ;
2017-03-17 09:18:50 +03:00
struct closure cl ;
struct btree * b ;
unsigned id ;
int ret ;
/* don't handle this yet: */
if ( flags & BCH_FORCE_IF_METADATA_LOST )
return - EINVAL ;
2019-05-15 17:54:43 +03:00
bch2_trans_init ( & trans , c , 0 , 0 ) ;
2017-03-17 09:18:50 +03:00
closure_init_stack ( & cl ) ;
for ( id = 0 ; id < BTREE_ID_NR ; id + + ) {
2019-03-25 22:10:15 +03:00
for_each_btree_node ( & trans , iter , id , POS_MIN ,
BTREE_ITER_PREFETCH , b ) {
2017-03-17 09:18:50 +03:00
__BKEY_PADDED ( k , BKEY_BTREE_PTR_VAL_U64s_MAX ) tmp ;
2018-11-01 22:10:01 +03:00
struct bkey_i_btree_ptr * new_key ;
2017-03-17 09:18:50 +03:00
retry :
2018-11-01 22:10:01 +03:00
if ( ! bch2_bkey_has_device ( bkey_i_to_s_c ( & b - > key ) ,
dev_idx ) ) {
2017-03-17 09:18:50 +03:00
/*
* we might have found a btree node key we
* needed to update , and then tried to update it
* but got - EINTR after upgrading the iter , but
* then raced and the node is now gone :
*/
2019-03-25 22:10:15 +03:00
bch2_btree_iter_downgrade ( iter ) ;
2017-03-17 09:18:50 +03:00
2018-11-01 22:10:01 +03:00
ret = bch2_mark_bkey_replicas ( c , bkey_i_to_s_c ( & b - > key ) ) ;
2017-03-17 09:18:50 +03:00
if ( ret )
goto err ;
} else {
bkey_copy ( & tmp . k , & b - > key ) ;
2018-11-01 22:10:01 +03:00
new_key = bkey_i_to_btree_ptr ( & tmp . k ) ;
2017-03-17 09:18:50 +03:00
2018-11-01 22:10:01 +03:00
ret = drop_dev_ptrs ( c , bkey_i_to_s ( & new_key - > k_i ) ,
2017-03-17 09:18:50 +03:00
dev_idx , flags , true ) ;
if ( ret )
goto err ;
2019-03-25 22:10:15 +03:00
ret = bch2_btree_node_update_key ( c , iter , b , new_key ) ;
2017-03-17 09:18:50 +03:00
if ( ret = = - EINTR ) {
2019-03-25 22:10:15 +03:00
b = bch2_btree_iter_peek_node ( iter ) ;
2017-03-17 09:18:50 +03:00
goto retry ;
}
if ( ret )
goto err ;
}
}
2019-03-25 22:10:15 +03:00
bch2_trans_iter_free ( & trans , iter ) ;
2017-03-17 09:18:50 +03:00
}
2019-01-21 23:32:13 +03:00
/* flush relevant btree updates */
while ( 1 ) {
closure_wait_event ( & c - > btree_interior_update_wait ,
! bch2_btree_interior_updates_nr_pending ( c ) | |
c - > btree_roots_dirty ) ;
if ( ! bch2_btree_interior_updates_nr_pending ( c ) )
break ;
bch2_journal_meta ( & c - > journal ) ;
}
2017-03-17 09:18:50 +03:00
ret = 0 ;
2019-03-25 22:10:15 +03:00
err :
2019-05-01 00:15:39 +03:00
ret = bch2_trans_exit ( & trans ) ? : ret ;
2019-03-25 22:10:15 +03:00
2019-05-01 00:15:39 +03:00
BUG_ON ( ret = = - EINTR ) ;
2017-03-17 09:18:50 +03:00
return ret ;
}
int bch2_dev_data_drop ( struct bch_fs * c , unsigned dev_idx , int flags )
{
return bch2_dev_usrdata_drop ( c , dev_idx , flags ) ? :
2019-05-01 00:15:39 +03:00
bch2_dev_metadata_drop ( c , dev_idx , flags ) ? :
bch2_replicas_gc2 ( c ) ;
2017-03-17 09:18:50 +03:00
}