]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to 8ffb42b3d0 bcachefs: Finish writing journal after journal...
[bcachefs-tools-debian] / libbcachefs / btree_update.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_H
3 #define _BCACHEFS_BTREE_UPDATE_H
4
5 #include "btree_iter.h"
6 #include "journal.h"
7
8 struct bch_fs;
9 struct btree;
10
11 void bch2_btree_node_lock_for_insert(struct btree_trans *, struct btree_path *,
12                                      struct btree *);
13 bool bch2_btree_bset_insert_key(struct btree_trans *, struct btree_path *,
14                                 struct btree *, struct btree_node_iter *,
15                                 struct bkey_i *);
16 void bch2_btree_add_journal_pin(struct bch_fs *, struct btree *, u64);
17
18 enum btree_insert_flags {
19         __BTREE_INSERT_NOFAIL,
20         __BTREE_INSERT_NOCHECK_RW,
21         __BTREE_INSERT_LAZY_RW,
22         __BTREE_INSERT_USE_RESERVE,
23         __BTREE_INSERT_JOURNAL_REPLAY,
24         __BTREE_INSERT_JOURNAL_RESERVED,
25         __BTREE_INSERT_JOURNAL_RECLAIM,
26         __BTREE_INSERT_NOWAIT,
27         __BTREE_INSERT_GC_LOCK_HELD,
28         __BCH_HASH_SET_MUST_CREATE,
29         __BCH_HASH_SET_MUST_REPLACE,
30 };
31
32 /* Don't check for -ENOSPC: */
33 #define BTREE_INSERT_NOFAIL             (1 << __BTREE_INSERT_NOFAIL)
34
35 #define BTREE_INSERT_NOCHECK_RW         (1 << __BTREE_INSERT_NOCHECK_RW)
36 #define BTREE_INSERT_LAZY_RW            (1 << __BTREE_INSERT_LAZY_RW)
37
38 /* for copygc, or when merging btree nodes */
39 #define BTREE_INSERT_USE_RESERVE        (1 << __BTREE_INSERT_USE_RESERVE)
40
41 /* Insert is for journal replay - don't get journal reservations: */
42 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
43
44 /* Indicates that we have pre-reserved space in the journal: */
45 #define BTREE_INSERT_JOURNAL_RESERVED   (1 << __BTREE_INSERT_JOURNAL_RESERVED)
46
47 /* Insert is being called from journal reclaim path: */
48 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
49
50 /* Don't block on allocation failure (for new btree nodes: */
51 #define BTREE_INSERT_NOWAIT             (1 << __BTREE_INSERT_NOWAIT)
52 #define BTREE_INSERT_GC_LOCK_HELD       (1 << __BTREE_INSERT_GC_LOCK_HELD)
53
54 #define BCH_HASH_SET_MUST_CREATE        (1 << __BCH_HASH_SET_MUST_CREATE)
55 #define BCH_HASH_SET_MUST_REPLACE       (1 << __BCH_HASH_SET_MUST_REPLACE)
56
57 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
58
59 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
60 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
61                      struct disk_reservation *, u64 *, int flags);
62
63 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
64                                   struct bpos, struct bpos, unsigned, u64 *);
65 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
66                             struct bpos, struct bpos, unsigned, u64 *);
67
68 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
69                             struct btree *, unsigned);
70 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
71 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
72                                struct btree *, struct bkey_i *, bool);
73 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
74                                 struct btree *, struct bkey_i *, bool);
75
76 int bch2_trans_update_extent(struct btree_trans *, struct btree_iter *,
77                              struct bkey_i *, enum btree_update_flags);
78
79 int __must_check bch2_trans_update(struct btree_trans *, struct btree_iter *,
80                                    struct bkey_i *, enum btree_update_flags);
81
82 void bch2_trans_commit_hook(struct btree_trans *,
83                             struct btree_trans_commit_hook *);
84 int __bch2_trans_commit(struct btree_trans *);
85
86 /**
87  * bch2_trans_commit - insert keys at given iterator positions
88  *
89  * This is main entry point for btree updates.
90  *
91  * Return values:
92  * -EINTR: locking changed, this function should be called again.
93  * -EROFS: filesystem read only
94  * -EIO: journal or btree node IO error
95  */
96 static inline int bch2_trans_commit(struct btree_trans *trans,
97                                     struct disk_reservation *disk_res,
98                                     u64 *journal_seq,
99                                     unsigned flags)
100 {
101         trans->disk_res         = disk_res;
102         trans->journal_seq      = journal_seq;
103         trans->flags            = flags;
104
105         return __bch2_trans_commit(trans);
106 }
107
108 #define lockrestart_do(_trans, _do)                                     \
109 ({                                                                      \
110         int _ret;                                                       \
111                                                                         \
112         do {                                                            \
113                 bch2_trans_begin(_trans);                               \
114                 _ret = (_do);                                           \
115         } while (_ret == -EINTR);                                       \
116                                                                         \
117         _ret;                                                           \
118 })
119
120 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do)   \
121         lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
122                                         (_journal_seq), (_flags)))
123
124 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
125 ({                                                                      \
126         struct btree_trans trans;                                       \
127         int _ret;                                                       \
128                                                                         \
129         bch2_trans_init(&trans, (_c), 0, 0);                            \
130         _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
131                                _do);                                    \
132         bch2_trans_exit(&trans);                                        \
133                                                                         \
134         _ret;                                                           \
135 })
136
137 #define trans_for_each_update(_trans, _i)                               \
138         for ((_i) = (_trans)->updates;                                  \
139              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
140              (_i)++)
141
142 #endif /* _BCACHEFS_BTREE_UPDATE_H */