2017-03-16 22:18:50 -08:00
/* SPDX-License-Identifier: GPL-2.0 */
# ifndef _BCACHEFS_BTREE_UPDATE_H
# define _BCACHEFS_BTREE_UPDATE_H
# include "btree_iter.h"
# include "journal.h"
struct bch_fs ;
struct btree ;
2021-08-30 15:18:31 -04:00
void bch2_btree_node_lock_for_insert ( struct btree_trans * , struct btree_path * ,
2021-07-10 23:22:06 -04:00
struct btree * ) ;
2021-08-30 15:18:31 -04:00
bool bch2_btree_bset_insert_key ( struct btree_trans * , struct btree_path * ,
2021-08-24 21:30:06 -04:00
struct btree * , struct btree_node_iter * ,
struct bkey_i * ) ;
2020-02-08 19:06:31 -05:00
void bch2_btree_add_journal_pin ( struct bch_fs * , struct btree * , u64 ) ;
2017-03-16 22:18:50 -08:00
2019-12-31 16:17:42 -05:00
enum btree_insert_flags {
2022-03-14 21:48:42 -04:00
/* First two bits for journal watermark: */
__BTREE_INSERT_NOFAIL = 2 ,
2018-11-19 01:31:41 -05:00
__BTREE_INSERT_NOCHECK_RW ,
2019-03-21 22:19:57 -04:00
__BTREE_INSERT_LAZY_RW ,
2017-03-16 22:18:50 -08:00
__BTREE_INSERT_USE_RESERVE ,
__BTREE_INSERT_JOURNAL_REPLAY ,
2019-03-07 19:46:10 -05:00
__BTREE_INSERT_JOURNAL_RECLAIM ,
2017-03-16 22:18:50 -08:00
__BTREE_INSERT_NOWAIT ,
__BTREE_INSERT_GC_LOCK_HELD ,
__BCH_HASH_SET_MUST_CREATE ,
__BCH_HASH_SET_MUST_REPLACE ,
} ;
/* Don't check for -ENOSPC: */
# define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
2018-11-19 01:31:41 -05:00
# define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
2019-03-21 22:19:57 -04:00
# define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
2018-11-19 01:31:41 -05:00
2017-03-16 22:18:50 -08:00
/* for copygc, or when merging btree nodes */
# define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
2019-02-13 14:46:32 -05:00
/* Insert is for journal replay - don't get journal reservations: */
2017-03-16 22:18:50 -08:00
# define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
2019-03-07 19:46:10 -05:00
/* Insert is being called from journal reclaim path: */
# define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
2017-03-16 22:18:50 -08:00
/* Don't block on allocation failure (for new btree nodes: */
# define BTREE_INSERT_NOWAIT (1 << __BTREE_INSERT_NOWAIT)
# define BTREE_INSERT_GC_LOCK_HELD (1 << __BTREE_INSERT_GC_LOCK_HELD)
# define BCH_HASH_SET_MUST_CREATE (1 << __BCH_HASH_SET_MUST_CREATE)
# define BCH_HASH_SET_MUST_REPLACE (1 << __BCH_HASH_SET_MUST_REPLACE)
2022-04-09 15:07:11 -04:00
int bch2_btree_delete_extent_at ( struct btree_trans * , struct btree_iter * ,
unsigned , unsigned ) ;
2019-03-13 20:49:16 -04:00
int bch2_btree_delete_at ( struct btree_trans * , struct btree_iter * , unsigned ) ;
2017-03-16 22:18:50 -08:00
2020-02-26 15:39:46 -05:00
int __bch2_btree_insert ( struct btree_trans * , enum btree_id , struct bkey_i * ) ;
2017-03-16 22:18:50 -08:00
int bch2_btree_insert ( struct bch_fs * , enum btree_id , struct bkey_i * ,
2018-08-08 19:53:30 -04:00
struct disk_reservation * , u64 * , int flags ) ;
2017-03-16 22:18:50 -08:00
2020-11-20 21:28:55 -05:00
int bch2_btree_delete_range_trans ( struct btree_trans * , enum btree_id ,
2021-04-20 00:15:44 -04:00
struct bpos , struct bpos , unsigned , u64 * ) ;
2017-03-16 22:18:50 -08:00
int bch2_btree_delete_range ( struct bch_fs * , enum btree_id ,
2021-12-29 13:49:34 -05:00
struct bpos , struct bpos , unsigned , u64 * ) ;
2017-03-16 22:18:50 -08:00
2021-07-10 23:22:06 -04:00
int bch2_btree_node_rewrite ( struct btree_trans * , struct btree_iter * ,
2021-10-24 16:59:33 -04:00
struct btree * , unsigned ) ;
2021-04-24 02:47:41 -04:00
void bch2_btree_node_rewrite_async ( struct bch_fs * , struct btree * ) ;
2021-07-10 23:22:06 -04:00
int bch2_btree_node_update_key ( struct btree_trans * , struct btree_iter * ,
2021-07-10 13:44:42 -04:00
struct btree * , struct bkey_i * , bool ) ;
int bch2_btree_node_update_key_get_iter ( struct btree_trans * ,
struct btree * , struct bkey_i * , bool ) ;
2017-03-16 22:18:50 -08:00
2022-01-08 21:22:31 -05:00
int bch2_trans_update_extent ( struct btree_trans * , struct btree_iter * ,
struct bkey_i * , enum btree_update_flags ) ;
2021-12-05 00:30:49 -05:00
int __must_check bch2_trans_update ( struct btree_trans * , struct btree_iter * ,
struct bkey_i * , enum btree_update_flags ) ;
2022-01-08 21:22:31 -05:00
2021-02-03 21:51:56 -05:00
void bch2_trans_commit_hook ( struct btree_trans * ,
struct btree_trans_commit_hook * ) ;
2019-10-19 19:03:23 -04:00
int __bch2_trans_commit ( struct btree_trans * ) ;
2022-03-30 15:44:12 -04:00
int bch2_trans_log_msg ( struct btree_trans * , const char * ) ;
2019-10-19 19:03:23 -04:00
/**
* bch2_trans_commit - insert keys at given iterator positions
*
* This is main entry point for btree updates .
*
* Return values :
2019-12-22 23:39:28 -05:00
* - EINTR : locking changed , this function should be called again .
2019-10-19 19:03:23 -04:00
* - EROFS : filesystem read only
* - EIO : journal or btree node IO error
*/
static inline int bch2_trans_commit ( struct btree_trans * trans ,
struct disk_reservation * disk_res ,
u64 * journal_seq ,
unsigned flags )
{
trans - > disk_res = disk_res ;
trans - > journal_seq = journal_seq ;
trans - > flags = flags ;
return __bch2_trans_commit ( trans ) ;
}
2017-03-16 22:18:50 -08:00
2021-04-07 03:11:07 -04:00
# define lockrestart_do(_trans, _do) \
2017-03-16 22:18:50 -08:00
( { \
int _ret ; \
\
2021-07-28 16:17:10 -04:00
do { \
2021-07-24 20:24:10 -04:00
bch2_trans_begin ( _trans ) ; \
2021-07-28 16:17:10 -04:00
_ret = ( _do ) ; \
} while ( _ret = = - EINTR ) ; \
2017-03-16 22:18:50 -08:00
\
_ret ; \
} )
2022-07-13 05:25:29 -04:00
# define commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
2021-04-07 03:11:07 -04:00
lockrestart_do ( _trans , _do ? : bch2_trans_commit ( _trans , ( _disk_res ) , \
( _journal_seq ) , ( _flags ) ) )
2019-12-22 23:04:30 -05:00
# define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
( { \
struct btree_trans trans ; \
2021-10-19 15:08:00 -04:00
int _ret ; \
2019-12-22 23:04:30 -05:00
\
bch2_trans_init ( & trans , ( _c ) , 0 , 0 ) ; \
2022-07-13 05:25:29 -04:00
_ret = commit_do ( & trans , _disk_res , _journal_seq , _flags , _do ) ; \
2021-10-19 15:08:00 -04:00
bch2_trans_exit ( & trans ) ; \
2019-12-22 23:04:30 -05:00
\
2021-10-19 15:08:00 -04:00
_ret ; \
2019-12-22 23:04:30 -05:00
} )
2022-07-14 02:08:58 -04:00
# define bch2_trans_run(_c, _do) \
( { \
struct btree_trans trans ; \
int _ret ; \
\
bch2_trans_init ( & trans , ( _c ) , 0 , 0 ) ; \
_ret = ( _do ) ; \
bch2_trans_exit ( & trans ) ; \
\
_ret ; \
} )
2019-09-22 18:49:16 -04:00
# define trans_for_each_update(_trans, _i) \
2019-03-15 17:11:58 -04:00
for ( ( _i ) = ( _trans ) - > updates ; \
2019-09-22 18:49:16 -04:00
( _i ) < ( _trans ) - > updates + ( _trans ) - > nr_updates ; \
2019-03-15 17:11:58 -04:00
( _i ) + + )
2022-05-29 11:38:48 -04:00
static inline void bch2_trans_reset_updates ( struct btree_trans * trans )
{
struct btree_insert_entry * i ;
trans_for_each_update ( trans , i )
bch2_path_put ( trans , i - > path , true ) ;
trans - > extra_journal_res = 0 ;
trans - > nr_updates = 0 ;
trans - > hooks = NULL ;
trans - > extra_journal_entries . nr = 0 ;
}
2017-03-16 22:18:50 -08:00
# endif /* _BCACHEFS_BTREE_UPDATE_H */