]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
771e4b239c66b234883363c1d222662fdbc2948b
[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_prep_for_write(struct btree_trans *,
12                                     struct btree_path *, 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 void bch2_btree_insert_key_leaf(struct btree_trans *, struct btree_path *,
19                                 struct bkey_i *, u64);
20
21 enum btree_insert_flags {
22         /* First two bits for journal watermark: */
23         __BTREE_INSERT_NOFAIL = 2,
24         __BTREE_INSERT_NOCHECK_RW,
25         __BTREE_INSERT_LAZY_RW,
26         __BTREE_INSERT_USE_RESERVE,
27         __BTREE_INSERT_JOURNAL_REPLAY,
28         __BTREE_INSERT_JOURNAL_RECLAIM,
29         __BTREE_INSERT_NOWAIT,
30         __BTREE_INSERT_GC_LOCK_HELD,
31         __BCH_HASH_SET_MUST_CREATE,
32         __BCH_HASH_SET_MUST_REPLACE,
33 };
34
35 /* Don't check for -ENOSPC: */
36 #define BTREE_INSERT_NOFAIL             (1 << __BTREE_INSERT_NOFAIL)
37
38 #define BTREE_INSERT_NOCHECK_RW         (1 << __BTREE_INSERT_NOCHECK_RW)
39 #define BTREE_INSERT_LAZY_RW            (1 << __BTREE_INSERT_LAZY_RW)
40
41 /* for copygc, or when merging btree nodes */
42 #define BTREE_INSERT_USE_RESERVE        (1 << __BTREE_INSERT_USE_RESERVE)
43
44 /* Insert is for journal replay - don't get journal reservations: */
45 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
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_extent_at(struct btree_trans *, struct btree_iter *,
58                                 unsigned, unsigned);
59 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
60
61 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *,
62                         enum btree_update_flags);
63 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
64                      struct disk_reservation *, u64 *, int flags);
65
66 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
67                                   struct bpos, struct bpos, unsigned, u64 *);
68 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
69                             struct bpos, struct bpos, unsigned, u64 *);
70
71 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
72                             struct btree *, unsigned);
73 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
74 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
75                                struct btree *, struct bkey_i *, bool);
76 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
77                                 struct btree *, struct bkey_i *, bool);
78
79 int bch2_trans_update_extent(struct btree_trans *, struct btree_iter *,
80                              struct bkey_i *, enum btree_update_flags);
81
82 int __must_check bch2_trans_update(struct btree_trans *, struct btree_iter *,
83                                    struct bkey_i *, enum btree_update_flags);
84 int __must_check bch2_trans_update_buffered(struct btree_trans *,
85                                             enum btree_id, struct bkey_i *);
86
87 void bch2_trans_commit_hook(struct btree_trans *,
88                             struct btree_trans_commit_hook *);
89 int __bch2_trans_commit(struct btree_trans *, unsigned);
90
91 int bch2_trans_log_msg(struct btree_trans *, const char *, ...);
92 int bch2_fs_log_msg(struct bch_fs *, const char *, ...);
93
94 /**
95  * bch2_trans_commit - insert keys at given iterator positions
96  *
97  * This is main entry point for btree updates.
98  *
99  * Return values:
100  * -EROFS: filesystem read only
101  * -EIO: journal or btree node IO error
102  */
103 static inline int bch2_trans_commit(struct btree_trans *trans,
104                                     struct disk_reservation *disk_res,
105                                     u64 *journal_seq,
106                                     unsigned flags)
107 {
108         trans->disk_res         = disk_res;
109         trans->journal_seq      = journal_seq;
110
111         return __bch2_trans_commit(trans, flags);
112 }
113
114 #define commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
115         lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
116                                         (_journal_seq), (_flags)))
117
118 #define nested_commit_do(_trans, _disk_res, _journal_seq, _flags, _do)  \
119         nested_lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
120                                         (_journal_seq), (_flags)))
121
122 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
123 ({                                                                      \
124         struct btree_trans trans;                                       \
125         int _ret;                                                       \
126                                                                         \
127         bch2_trans_init(&trans, (_c), 0, 0);                            \
128         _ret = commit_do(&trans, _disk_res, _journal_seq, _flags, _do); \
129         bch2_trans_exit(&trans);                                        \
130                                                                         \
131         _ret;                                                           \
132 })
133
134 #define bch2_trans_run(_c, _do)                                         \
135 ({                                                                      \
136         struct btree_trans trans;                                       \
137         int _ret;                                                       \
138                                                                         \
139         bch2_trans_init(&trans, (_c), 0, 0);                            \
140         _ret = (_do);                                                   \
141         bch2_trans_exit(&trans);                                        \
142                                                                         \
143         _ret;                                                           \
144 })
145
146 #define trans_for_each_update(_trans, _i)                               \
147         for ((_i) = (_trans)->updates;                                  \
148              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
149              (_i)++)
150
151 #define trans_for_each_wb_update(_trans, _i)                            \
152         for ((_i) = (_trans)->wb_updates;                               \
153              (_i) < (_trans)->wb_updates + (_trans)->nr_wb_updates;     \
154              (_i)++)
155
156 static inline void bch2_trans_reset_updates(struct btree_trans *trans)
157 {
158         struct btree_insert_entry *i;
159
160         trans_for_each_update(trans, i)
161                 bch2_path_put(trans, i->path, true);
162
163         trans->extra_journal_res        = 0;
164         trans->nr_updates               = 0;
165         trans->nr_wb_updates            = 0;
166         trans->wb_updates               = NULL;
167         trans->hooks                    = NULL;
168         trans->extra_journal_entries.nr = 0;
169
170         if (trans->fs_usage_deltas) {
171                 trans->fs_usage_deltas->used = 0;
172                 memset((void *) trans->fs_usage_deltas +
173                        offsetof(struct replicas_delta_list, memset_start), 0,
174                        (void *) &trans->fs_usage_deltas->memset_end -
175                        (void *) &trans->fs_usage_deltas->memset_start);
176         }
177 }
178
179 #endif /* _BCACHEFS_BTREE_UPDATE_H */