Skip to content

Commit 801cb2b

Browse files
author
Kent Overstreet
committed
bcachefs: bch2_get_snapshot_overwrites()
New helper for getting a list of snapshot IDs that have overwritten a given key. Signed-off-by: Kent Overstreet <[email protected]>
1 parent d21262d commit 801cb2b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

fs/bcachefs/btree_update.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,19 @@ int __bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
180180
}
181181
bch2_trans_iter_exit(trans, &new_iter);
182182
bch2_trans_iter_exit(trans, &old_iter);
183-
darray_exit(&s);
184183

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));
193+
194+
darray_exit(&s2);
195+
darray_exit(&s);
185196
return ret;
186197
}
187198

fs/bcachefs/snapshot.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,35 @@ int __bch2_check_key_has_snapshot(struct btree_trans *trans,
10791079
return ret;
10801080
}
10811081

1082+
int __bch2_get_snapshot_overwrites(struct btree_trans *trans,
1083+
enum btree_id btree, struct bpos pos,
1084+
snapshot_id_list *s)
1085+
{
1086+
struct bch_fs *c = trans->c;
1087+
struct btree_iter iter;
1088+
struct bkey_s_c k;
1089+
int ret = 0;
1090+
1091+
for_each_btree_key_reverse_norestart(trans, iter, btree, bpos_predecessor(pos),
1092+
BTREE_ITER_all_snapshots, k, ret) {
1093+
if (!bkey_eq(k.k->p, pos))
1094+
break;
1095+
1096+
if (!bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot) ||
1097+
snapshot_list_has_ancestor(c, s, k.k->p.snapshot))
1098+
continue;
1099+
1100+
ret = snapshot_list_add(c, s, k.k->p.snapshot);
1101+
if (ret)
1102+
break;
1103+
}
1104+
bch2_trans_iter_exit(trans, &iter);
1105+
if (ret)
1106+
darray_exit(s);
1107+
1108+
return ret;
1109+
}
1110+
10821111
/*
10831112
* Mark a snapshot as deleted, for future cleanup:
10841113
*/

fs/bcachefs/snapshot.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ static inline int bch2_check_key_has_snapshot(struct btree_trans *trans,
258258
: __bch2_check_key_has_snapshot(trans, iter, k);
259259
}
260260

261+
int __bch2_get_snapshot_overwrites(struct btree_trans *,
262+
enum btree_id, struct bpos,
263+
snapshot_id_list *);
264+
265+
/*
266+
* Get a list of snapshot IDs that have overwritten a given key:
267+
*/
268+
static inline int bch2_get_snapshot_overwrites(struct btree_trans *trans,
269+
enum btree_id btree, struct bpos pos,
270+
snapshot_id_list *s)
271+
{
272+
darray_init(s);
273+
274+
return bch2_snapshot_has_children(trans->c, pos.snapshot)
275+
? __bch2_get_snapshot_overwrites(trans, btree, pos, s)
276+
: 0;
277+
278+
}
279+
261280
int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
262281

263282
int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);

0 commit comments

Comments
 (0)