]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to 386f00b639 bcachefs: Snapshot creation, deletion
[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, u64 *);
67
68 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
69                             __le64, 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(struct btree_trans *, struct btree_iter *,
77                       struct bkey_i *, enum btree_update_flags);
78 void bch2_trans_commit_hook(struct btree_trans *,
79                             struct btree_trans_commit_hook *);
80 int __bch2_trans_commit(struct btree_trans *);
81
82 /**
83  * bch2_trans_commit - insert keys at given iterator positions
84  *
85  * This is main entry point for btree updates.
86  *
87  * Return values:
88  * -EINTR: locking changed, this function should be called again.
89  * -EROFS: filesystem read only
90  * -EIO: journal or btree node IO error
91  */
92 static inline int bch2_trans_commit(struct btree_trans *trans,
93                                     struct disk_reservation *disk_res,
94                                     u64 *journal_seq,
95                                     unsigned flags)
96 {
97         trans->disk_res         = disk_res;
98         trans->journal_seq      = journal_seq;
99         trans->flags            = flags;
100
101         return __bch2_trans_commit(trans);
102 }
103
104 #define lockrestart_do(_trans, _do)                                     \
105 ({                                                                      \
106         int _ret;                                                       \
107                                                                         \
108         do {                                                            \
109                 bch2_trans_begin(_trans);                               \
110                 _ret = (_do);                                           \
111         } while (_ret == -EINTR);                                       \
112                                                                         \
113         _ret;                                                           \
114 })
115
116 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do)   \
117         lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
118                                         (_journal_seq), (_flags)))
119
120 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
121 ({                                                                      \
122         struct btree_trans trans;                                       \
123         int _ret, _ret2;                                                \
124                                                                         \
125         bch2_trans_init(&trans, (_c), 0, 0);                            \
126         _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
127                                _do);                                    \
128         _ret2 = bch2_trans_exit(&trans);                                \
129                                                                         \
130         _ret ?: _ret2;                                                  \
131 })
132
133 #define trans_for_each_update(_trans, _i)                               \
134         for ((_i) = (_trans)->updates;                                  \
135              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
136              (_i)++)
137
138 static inline struct bkey_i *btree_trans_peek_updates(struct btree_trans *trans,
139                                                       enum btree_id btree_id,
140                                                       struct bpos pos)
141 {
142         struct btree_insert_entry *i;
143
144         trans_for_each_update(trans, i)
145                 if ((cmp_int(btree_id,  i->btree_id) ?:
146                      bpos_cmp(pos,      i->k->k.p)) <= 0) {
147                         if (btree_id == i->btree_id)
148                                 return i->k;
149                         break;
150                 }
151
152         return NULL;
153 }
154
155 #endif /* _BCACHEFS_BTREE_UPDATE_H */