2017-03-17 09:18:50 +03: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 ;
2022-10-09 12:04:38 +03:00
void bch2_btree_node_prep_for_write ( struct btree_trans * ,
struct btree_path * , struct btree * ) ;
2021-08-30 22:18:31 +03:00
bool bch2_btree_bset_insert_key ( struct btree_trans * , struct btree_path * ,
2021-08-25 04:30:06 +03:00
struct btree * , struct btree_node_iter * ,
struct bkey_i * ) ;
2020-02-09 03:06:31 +03:00
void bch2_btree_add_journal_pin ( struct bch_fs * , struct btree * , u64 ) ;
2017-03-17 09:18:50 +03:00
2020-01-01 00:17:42 +03:00
enum btree_insert_flags {
2022-03-15 04:48:42 +03:00
/* First two bits for journal watermark: */
__BTREE_INSERT_NOFAIL = 2 ,
2018-11-19 09:31:41 +03:00
__BTREE_INSERT_NOCHECK_RW ,
2019-03-22 05:19:57 +03:00
__BTREE_INSERT_LAZY_RW ,
2017-03-17 09:18:50 +03:00
__BTREE_INSERT_USE_RESERVE ,
__BTREE_INSERT_JOURNAL_REPLAY ,
2019-03-08 03:46:10 +03:00
__BTREE_INSERT_JOURNAL_RECLAIM ,
2017-03-17 09:18:50 +03: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 09:31:41 +03:00
# define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
2019-03-22 05:19:57 +03:00
# define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
2018-11-19 09:31:41 +03:00
2017-03-17 09:18:50 +03:00
/* for copygc, or when merging btree nodes */
# define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
2019-02-13 22:46:32 +03:00
/* Insert is for journal replay - don't get journal reservations: */
2017-03-17 09:18:50 +03:00
# define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
2019-03-08 03:46:10 +03:00
/* Insert is being called from journal reclaim path: */
# define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
2017-03-17 09:18:50 +03: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 22:07:11 +03:00
int bch2_btree_delete_extent_at ( struct btree_trans * , struct btree_iter * ,
unsigned , unsigned ) ;
2019-03-14 03:49:16 +03:00
int bch2_btree_delete_at ( struct btree_trans * , struct btree_iter * , unsigned ) ;
2017-03-17 09:18:50 +03:00
2020-02-26 23:39:46 +03:00
int __bch2_btree_insert ( struct btree_trans * , enum btree_id , struct bkey_i * ) ;
2017-03-17 09:18:50 +03:00
int bch2_btree_insert ( struct bch_fs * , enum btree_id , struct bkey_i * ,
2018-08-09 02:53:30 +03:00
struct disk_reservation * , u64 * , int flags ) ;
2017-03-17 09:18:50 +03:00
2020-11-21 05:28:55 +03:00
int bch2_btree_delete_range_trans ( struct btree_trans * , enum btree_id ,
2021-04-20 07:15:44 +03:00
struct bpos , struct bpos , unsigned , u64 * ) ;
2017-03-17 09:18:50 +03:00
int bch2_btree_delete_range ( struct bch_fs * , enum btree_id ,
2021-12-29 21:49:34 +03:00
struct bpos , struct bpos , unsigned , u64 * ) ;
2017-03-17 09:18:50 +03:00
2021-07-11 06:22:06 +03:00
int bch2_btree_node_rewrite ( struct btree_trans * , struct btree_iter * ,
2021-10-24 23:59:33 +03:00
struct btree * , unsigned ) ;
2021-04-24 09:47:41 +03:00
void bch2_btree_node_rewrite_async ( struct bch_fs * , struct btree * ) ;
2021-07-11 06:22:06 +03:00
int bch2_btree_node_update_key ( struct btree_trans * , struct btree_iter * ,
2021-07-10 20:44:42 +03: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-17 09:18:50 +03:00
2022-01-09 05:22:31 +03:00
int bch2_trans_update_extent ( struct btree_trans * , struct btree_iter * ,
struct bkey_i * , enum btree_update_flags ) ;
2021-12-05 08:30:49 +03:00
int __must_check bch2_trans_update ( struct btree_trans * , struct btree_iter * ,
struct bkey_i * , enum btree_update_flags ) ;
2022-01-09 05:22:31 +03:00
2021-02-04 05:51:56 +03:00
void bch2_trans_commit_hook ( struct btree_trans * ,
struct btree_trans_commit_hook * ) ;
2019-10-20 02:03:23 +03:00
int __bch2_trans_commit ( struct btree_trans * ) ;
2022-03-30 22:44:12 +03:00
int bch2_trans_log_msg ( struct btree_trans * , const char * ) ;
2019-10-20 02:03:23 +03:00
/**
* bch2_trans_commit - insert keys at given iterator positions
*
* This is main entry point for btree updates .
*
* Return values :
* - 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-17 09:18:50 +03:00
2022-07-13 12:25:29 +03:00
# define commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
2021-04-07 10:11:07 +03:00
lockrestart_do ( _trans , _do ? : bch2_trans_commit ( _trans , ( _disk_res ) , \
( _journal_seq ) , ( _flags ) ) )
2022-07-18 02:35:38 +03:00
# define nested_commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
nested_lockrestart_do ( _trans , _do ? : bch2_trans_commit ( _trans , ( _disk_res ) , \
( _journal_seq ) , ( _flags ) ) )
2019-12-23 07:04:30 +03:00
# define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
( { \
struct btree_trans trans ; \
2021-10-19 22:08:00 +03:00
int _ret ; \
2019-12-23 07:04:30 +03:00
\
bch2_trans_init ( & trans , ( _c ) , 0 , 0 ) ; \
2022-07-13 12:25:29 +03:00
_ret = commit_do ( & trans , _disk_res , _journal_seq , _flags , _do ) ; \
2021-10-19 22:08:00 +03:00
bch2_trans_exit ( & trans ) ; \
2019-12-23 07:04:30 +03:00
\
2021-10-19 22:08:00 +03:00
_ret ; \
2019-12-23 07:04:30 +03:00
} )
2022-07-14 09:08:58 +03: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-23 01:49:16 +03:00
# define trans_for_each_update(_trans, _i) \
2019-03-16 00:11:58 +03:00
for ( ( _i ) = ( _trans ) - > updates ; \
2019-09-23 01:49:16 +03:00
( _i ) < ( _trans ) - > updates + ( _trans ) - > nr_updates ; \
2019-03-16 00:11:58 +03:00
( _i ) + + )
2022-05-29 18:38:48 +03: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-17 09:18:50 +03:00
# endif /* _BCACHEFS_BTREE_UPDATE_H */