Skip to content

Commit cb6f5d0

Browse files
author
Kent Overstreet
committed
bcachefs: __bch2_insert_snapshot_whiteouts() refactoring
Now uses bch2_get_snapshot_overwrites(), and much shorter. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 801cb2b commit cb6f5d0

File tree

2 files changed

+30
-52
lines changed

2 files changed

+30
-52
lines changed

fs/bcachefs/btree_update.c

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -123,76 +123,44 @@ static int need_whiteout_for_snapshot(struct btree_trans *trans,
123123
}
124124

125125
int __bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
126-
enum btree_id id,
127-
struct bpos old_pos,
128-
struct bpos new_pos)
126+
enum btree_id btree, struct bpos pos,
127+
snapshot_id_list *s)
129128
{
130-
struct bch_fs *c = trans->c;
131-
struct btree_iter old_iter, new_iter = {};
132-
struct bkey_s_c old_k, new_k;
133-
snapshot_id_list s;
134-
struct bkey_i *update;
135129
int ret = 0;
136130

137-
if (!bch2_snapshot_has_children(c, old_pos.snapshot))
138-
return 0;
139-
140-
darray_init(&s);
131+
darray_for_each(*s, id) {
132+
pos.snapshot = *id;
141133

142-
bch2_trans_iter_init(trans, &old_iter, id, old_pos,
143-
BTREE_ITER_not_extents|
144-
BTREE_ITER_all_snapshots);
145-
while ((old_k = bch2_btree_iter_prev(trans, &old_iter)).k &&
146-
!(ret = bkey_err(old_k)) &&
147-
bkey_eq(old_pos, old_k.k->p)) {
148-
struct bpos whiteout_pos =
149-
SPOS(new_pos.inode, new_pos.offset, old_k.k->p.snapshot);
150-
151-
if (!bch2_snapshot_is_ancestor(c, old_k.k->p.snapshot, old_pos.snapshot) ||
152-
snapshot_list_has_ancestor(c, &s, old_k.k->p.snapshot))
153-
continue;
154-
155-
new_k = bch2_bkey_get_iter(trans, &new_iter, id, whiteout_pos,
156-
BTREE_ITER_not_extents|
157-
BTREE_ITER_intent);
158-
ret = bkey_err(new_k);
134+
struct btree_iter iter;
135+
struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, btree, pos,
136+
BTREE_ITER_not_extents|
137+
BTREE_ITER_intent);
138+
ret = bkey_err(k);
159139
if (ret)
160140
break;
161141

162-
if (new_k.k->type == KEY_TYPE_deleted) {
163-
update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
142+
if (k.k->type == KEY_TYPE_deleted) {
143+
struct bkey_i *update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
164144
ret = PTR_ERR_OR_ZERO(update);
165-
if (ret)
145+
if (ret) {
146+
bch2_trans_iter_exit(trans, &iter);
166147
break;
148+
}
167149

168150
bkey_init(&update->k);
169-
update->k.p = whiteout_pos;
151+
update->k.p = pos;
170152
update->k.type = KEY_TYPE_whiteout;
171153

172-
ret = bch2_trans_update(trans, &new_iter, update,
154+
ret = bch2_trans_update(trans, &iter, update,
173155
BTREE_UPDATE_internal_snapshot_node);
174156
}
175-
bch2_trans_iter_exit(trans, &new_iter);
157+
bch2_trans_iter_exit(trans, &iter);
176158

177-
ret = snapshot_list_add(c, &s, old_k.k->p.snapshot);
178159
if (ret)
179160
break;
180161
}
181-
bch2_trans_iter_exit(trans, &new_iter);
182-
bch2_trans_iter_exit(trans, &old_iter);
183-
184-
snapshot_id_list s2;
185-
ret = bch2_get_snapshot_overwrites(trans, id, old_pos, &s2);
186-
if (ret) {
187-
darray_exit(&s);
188-
return ret;
189-
}
190-
191-
BUG_ON(s.nr != s2.nr);
192-
BUG_ON(memcmp(s.data, s2.data, sizeof(s.data[0]) * s.nr));
193162

194-
darray_exit(&s2);
195-
darray_exit(&s);
163+
darray_exit(s);
196164
return ret;
197165
}
198166

fs/bcachefs/btree_update.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "btree_iter.h"
66
#include "journal.h"
7+
#include "snapshot.h"
78

89
struct bch_fs;
910
struct btree;
@@ -74,7 +75,7 @@ static inline int bch2_btree_delete_at_buffered(struct btree_trans *trans,
7475
}
7576

7677
int __bch2_insert_snapshot_whiteouts(struct btree_trans *, enum btree_id,
77-
struct bpos, struct bpos);
78+
struct bpos, snapshot_id_list *);
7879

7980
/*
8081
* For use when splitting extents in existing snapshots:
@@ -88,11 +89,20 @@ static inline int bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
8889
struct bpos old_pos,
8990
struct bpos new_pos)
9091
{
92+
BUG_ON(old_pos.snapshot != new_pos.snapshot);
93+
9194
if (!btree_type_has_snapshots(btree) ||
9295
bkey_eq(old_pos, new_pos))
9396
return 0;
9497

95-
return __bch2_insert_snapshot_whiteouts(trans, btree, old_pos, new_pos);
98+
snapshot_id_list s;
99+
int ret = bch2_get_snapshot_overwrites(trans, btree, old_pos, &s);
100+
if (ret)
101+
return ret;
102+
103+
return s.nr
104+
? __bch2_insert_snapshot_whiteouts(trans, btree, new_pos, &s)
105+
: 0;
96106
}
97107

98108
int bch2_trans_update_extent_overwrite(struct btree_trans *, struct btree_iter *,

0 commit comments

Comments
 (0)