btree_update.h 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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 *);
15
void bch2_btree_journal_key(struct btree_trans *, struct btree_iter *,
16 17
			    struct bkey_i *);

18
enum btree_insert_flags {
19 20
	__BTREE_INSERT_NOUNLOCK,
	__BTREE_INSERT_NOFAIL,
21
	__BTREE_INSERT_NOCHECK_RW,
Kent Overstreet's avatar
Kent Overstreet committed
22
	__BTREE_INSERT_LAZY_RW,
23 24 25
	__BTREE_INSERT_USE_RESERVE,
	__BTREE_INSERT_USE_ALLOC_RESERVE,
	__BTREE_INSERT_JOURNAL_REPLAY,
26
	__BTREE_INSERT_JOURNAL_RESERVED,
27 28 29 30 31 32 33 34 35 36 37 38 39 40
	__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)

41
#define BTREE_INSERT_NOCHECK_RW		(1 << __BTREE_INSERT_NOCHECK_RW)
Kent Overstreet's avatar
Kent Overstreet committed
42
#define BTREE_INSERT_LAZY_RW		(1 << __BTREE_INSERT_LAZY_RW)
43

44 45 46 47
/* 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)

48
/* Insert is for journal replay - don't get journal reservations: */
49 50
#define BTREE_INSERT_JOURNAL_REPLAY	(1 << __BTREE_INSERT_JOURNAL_REPLAY)

51 52
#define BTREE_INSERT_JOURNAL_RESERVED	(1 << __BTREE_INSERT_JOURNAL_RESERVED)

53 54 55 56 57 58 59
/* 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)

60
int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
61 62

int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
63
		     struct disk_reservation *, u64 *, int flags);
64

65 66
int bch2_btree_delete_at_range(struct btree_trans *, struct btree_iter *,
			       struct bpos, u64 *);
67
int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
68
			    struct bpos, struct bpos, u64 *);
69 70 71 72

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 *,
73
			       struct btree *, struct bkey_i *);
74

75 76
int bch2_trans_update(struct btree_trans *, struct btree_iter *,
		      struct bkey_i *, enum btree_trigger_flags);
77 78 79 80 81 82 83 84
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:
85
 * -EINTR: locking changed, this function should be called again.
86 87 88 89 90 91 92 93 94 95 96 97 98 99
 * -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);
}
100

101 102
#define __bch2_trans_do(_trans, _disk_res, _journal_seq,		\
			_flags,	_reset_flags, _do)			\
103 104 105 106
({									\
	int _ret;							\
									\
	do {								\
107
		bch2_trans_reset(_trans, _reset_flags);			\
108
									\
109
		_ret = (_do) ?:	bch2_trans_commit(_trans, (_disk_res),	\
110 111 112 113 114 115
					(_journal_seq), (_flags));	\
	} while (_ret == -EINTR);					\
									\
	_ret;								\
})

116 117 118 119 120 121 122 123 124 125 126 127 128
#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,	\
			       TRANS_RESET_MEM|TRANS_RESET_ITERS, _do);	\
	_ret2 = bch2_trans_exit(&trans);				\
									\
	_ret ?: _ret2;							\
})

129
#define trans_for_each_update(_trans, _i)				\
130
	for ((_i) = (_trans)->updates;					\
131
	     (_i) < (_trans)->updates + (_trans)->nr_updates;		\
132 133
	     (_i)++)

134
#endif /* _BCACHEFS_BTREE_UPDATE_H */