2017-03-17 06:18:50 +00: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;
|
|
|
|
|
|
|
|
void bch2_btree_node_lock_for_insert(struct bch_fs *, struct btree *,
|
|
|
|
struct btree_iter *);
|
|
|
|
bool bch2_btree_bset_insert_key(struct btree_iter *, struct btree *,
|
|
|
|
struct btree_node_iter *, struct bkey_i *);
|
|
|
|
|
2019-12-31 21:17:42 +00:00
|
|
|
enum btree_insert_flags {
|
2017-03-17 06:18:50 +00:00
|
|
|
__BTREE_INSERT_NOUNLOCK,
|
|
|
|
__BTREE_INSERT_NOFAIL,
|
2018-11-19 06:31:41 +00:00
|
|
|
__BTREE_INSERT_NOCHECK_RW,
|
2019-03-22 02:19:57 +00:00
|
|
|
__BTREE_INSERT_LAZY_RW,
|
2017-03-17 06:18:50 +00:00
|
|
|
__BTREE_INSERT_USE_RESERVE,
|
|
|
|
__BTREE_INSERT_USE_ALLOC_RESERVE,
|
|
|
|
__BTREE_INSERT_JOURNAL_REPLAY,
|
2019-02-19 22:56:21 +00:00
|
|
|
__BTREE_INSERT_JOURNAL_RESERVED,
|
2017-03-17 06:18:50 +00:00
|
|
|
__BTREE_INSERT_NOWAIT,
|
|
|
|
__BTREE_INSERT_GC_LOCK_HELD,
|
|
|
|
__BCH_HASH_SET_MUST_CREATE,
|
|
|
|
__BCH_HASH_SET_MUST_REPLACE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't drop locks _after_ successfully updating btree:
|
|
|
|
*/
|
|
|
|
#define BTREE_INSERT_NOUNLOCK (1 << __BTREE_INSERT_NOUNLOCK)
|
|
|
|
|
|
|
|
/* Don't check for -ENOSPC: */
|
|
|
|
#define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
|
|
|
|
|
2018-11-19 06:31:41 +00:00
|
|
|
#define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
|
2019-03-22 02:19:57 +00:00
|
|
|
#define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
|
2018-11-19 06:31:41 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* for copygc, or when merging btree nodes */
|
|
|
|
#define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
|
|
|
|
#define BTREE_INSERT_USE_ALLOC_RESERVE (1 << __BTREE_INSERT_USE_ALLOC_RESERVE)
|
|
|
|
|
2019-02-13 19:46:32 +00:00
|
|
|
/* Insert is for journal replay - don't get journal reservations: */
|
2017-03-17 06:18:50 +00:00
|
|
|
#define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
#define BTREE_INSERT_JOURNAL_RESERVED (1 << __BTREE_INSERT_JOURNAL_RESERVED)
|
|
|
|
|
2017-03-17 06:18:50 +00: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)
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2020-02-26 20:39:46 +00:00
|
|
|
int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
|
2017-03-17 06:18:50 +00:00
|
|
|
int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
|
2018-08-08 23:53:30 +00:00
|
|
|
struct disk_reservation *, u64 *, int flags);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-05-11 21:36:31 +00:00
|
|
|
int bch2_btree_delete_at_range(struct btree_trans *, struct btree_iter *,
|
|
|
|
struct bpos, u64 *);
|
2017-03-17 06:18:50 +00:00
|
|
|
int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
|
2018-08-08 23:53:30 +00:00
|
|
|
struct bpos, struct bpos, u64 *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *,
|
|
|
|
__le64, unsigned);
|
|
|
|
int bch2_btree_node_update_key(struct bch_fs *, struct btree_iter *,
|
2020-02-18 22:15:32 +00:00
|
|
|
struct btree *, struct bkey_i *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2020-01-01 00:37:10 +00:00
|
|
|
int bch2_trans_update(struct btree_trans *, struct btree_iter *,
|
|
|
|
struct bkey_i *, enum btree_trigger_flags);
|
2019-10-19 23:03:23 +00:00
|
|
|
int __bch2_trans_commit(struct btree_trans *);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bch2_trans_commit - insert keys at given iterator positions
|
|
|
|
*
|
|
|
|
* This is main entry point for btree updates.
|
|
|
|
*
|
|
|
|
* Return values:
|
2019-12-23 04:39:28 +00:00
|
|
|
* -EINTR: locking changed, this function should be called again.
|
2019-10-19 23:03:23 +00: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-17 06:18:50 +00:00
|
|
|
|
2020-02-26 20:39:46 +00:00
|
|
|
#define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do) \
|
2017-03-17 06:18:50 +00:00
|
|
|
({ \
|
|
|
|
int _ret; \
|
|
|
|
\
|
2020-02-26 20:39:46 +00:00
|
|
|
while (1) { \
|
2019-12-23 04:04:30 +00:00
|
|
|
_ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res), \
|
2017-03-17 06:18:50 +00:00
|
|
|
(_journal_seq), (_flags)); \
|
2020-02-26 20:39:46 +00:00
|
|
|
if (_ret != -EINTR) \
|
|
|
|
break; \
|
|
|
|
bch2_trans_reset(_trans, 0); \
|
|
|
|
} \
|
2017-03-17 06:18:50 +00:00
|
|
|
\
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
2019-12-23 04:04:30 +00:00
|
|
|
#define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
|
|
|
|
({ \
|
|
|
|
struct btree_trans trans; \
|
|
|
|
int _ret, _ret2; \
|
|
|
|
\
|
|
|
|
bch2_trans_init(&trans, (_c), 0, 0); \
|
|
|
|
_ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
|
2020-02-26 20:39:46 +00:00
|
|
|
_do); \
|
2019-12-23 04:04:30 +00:00
|
|
|
_ret2 = bch2_trans_exit(&trans); \
|
|
|
|
\
|
|
|
|
_ret ?: _ret2; \
|
|
|
|
})
|
|
|
|
|
2019-09-22 22:49:16 +00:00
|
|
|
#define trans_for_each_update(_trans, _i) \
|
2019-03-15 21:11:58 +00:00
|
|
|
for ((_i) = (_trans)->updates; \
|
2019-09-22 22:49:16 +00:00
|
|
|
(_i) < (_trans)->updates + (_trans)->nr_updates; \
|
2019-03-15 21:11:58 +00:00
|
|
|
(_i)++)
|
|
|
|
|
2019-12-30 19:37:25 +00:00
|
|
|
#define trans_for_each_update2(_trans, _i) \
|
|
|
|
for ((_i) = (_trans)->updates2; \
|
|
|
|
(_i) < (_trans)->updates2 + (_trans)->nr_updates2; \
|
|
|
|
(_i)++)
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
#endif /* _BCACHEFS_BTREE_UPDATE_H */
|