]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
rust: Fix ptr casting in Fs::open()
[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_nonextent(struct btree_trans *, enum btree_id,
62                                 struct bkey_i *, enum btree_update_flags);
63
64 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *,
65                         enum btree_update_flags);
66 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
67                      struct disk_reservation *, u64 *, int flags);
68
69 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
70                                   struct bpos, struct bpos, unsigned, u64 *);
71 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
72                             struct bpos, struct bpos, unsigned, u64 *);
73
74 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
75                             struct btree *, unsigned);
76 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
77 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
78                                struct btree *, struct bkey_i *, bool);
79 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
80                                 struct btree *, struct bkey_i *, bool);
81
82 int bch2_trans_update_extent(struct btree_trans *, struct btree_iter *,
83                              struct bkey_i *, enum btree_update_flags);
84
85 int __must_check bch2_trans_update(struct btree_trans *, struct btree_iter *,
86                                    struct bkey_i *, enum btree_update_flags);
87 int __must_check bch2_trans_update_buffered(struct btree_trans *,
88                                             enum btree_id, struct bkey_i *);
89
90 void bch2_trans_commit_hook(struct btree_trans *,
91                             struct btree_trans_commit_hook *);
92 int __bch2_trans_commit(struct btree_trans *, unsigned);
93
94 int bch2_trans_log_msg(struct btree_trans *, const char *, ...);
95 int bch2_fs_log_msg(struct bch_fs *, const char *, ...);
96
97 /**
98  * bch2_trans_commit - insert keys at given iterator positions
99  *
100  * This is main entry point for btree updates.
101  *
102  * Return values:
103  * -EROFS: filesystem read only
104  * -EIO: journal or btree node IO error
105  */
106 static inline int bch2_trans_commit(struct btree_trans *trans,
107                                     struct disk_reservation *disk_res,
108                                     u64 *journal_seq,
109                                     unsigned flags)
110 {
111         trans->disk_res         = disk_res;
112         trans->journal_seq      = journal_seq;
113
114         return __bch2_trans_commit(trans, flags);
115 }
116
117 #define commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
118         lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
119                                         (_journal_seq), (_flags)))
120
121 #define nested_commit_do(_trans, _disk_res, _journal_seq, _flags, _do)  \
122         nested_lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
123                                         (_journal_seq), (_flags)))
124
125 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
126 ({                                                                      \
127         struct btree_trans trans;                                       \
128         int _ret;                                                       \
129                                                                         \
130         bch2_trans_init(&trans, (_c), 0, 0);                            \
131         _ret = commit_do(&trans, _disk_res, _journal_seq, _flags, _do); \
132         bch2_trans_exit(&trans);                                        \
133                                                                         \
134         _ret;                                                           \
135 })
136
137 #define bch2_trans_run(_c, _do)                                         \
138 ({                                                                      \
139         struct btree_trans trans;                                       \
140         int _ret;                                                       \
141                                                                         \
142         bch2_trans_init(&trans, (_c), 0, 0);                            \
143         _ret = (_do);                                                   \
144         bch2_trans_exit(&trans);                                        \
145                                                                         \
146         _ret;                                                           \
147 })
148
149 #define trans_for_each_update(_trans, _i)                               \
150         for ((_i) = (_trans)->updates;                                  \
151              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
152              (_i)++)
153
154 #define trans_for_each_wb_update(_trans, _i)                            \
155         for ((_i) = (_trans)->wb_updates;                               \
156              (_i) < (_trans)->wb_updates + (_trans)->nr_wb_updates;     \
157              (_i)++)
158
159 static inline void bch2_trans_reset_updates(struct btree_trans *trans)
160 {
161         struct btree_insert_entry *i;
162
163         trans_for_each_update(trans, i)
164                 bch2_path_put(trans, i->path, true);
165
166         trans->extra_journal_res        = 0;
167         trans->nr_updates               = 0;
168         trans->nr_wb_updates            = 0;
169         trans->wb_updates               = NULL;
170         trans->hooks                    = NULL;
171         trans->extra_journal_entries.nr = 0;
172
173         if (trans->fs_usage_deltas) {
174                 trans->fs_usage_deltas->used = 0;
175                 memset((void *) trans->fs_usage_deltas +
176                        offsetof(struct replicas_delta_list, memset_start), 0,
177                        (void *) &trans->fs_usage_deltas->memset_end -
178                        (void *) &trans->fs_usage_deltas->memset_start);
179         }
180 }
181
182 #endif /* _BCACHEFS_BTREE_UPDATE_H */