]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/migrate.c
Update bcachefs sources to 90a9c61e2b bcachefs: Switch bch2_btree_delete_range()...
[bcachefs-tools-debian] / libbcachefs / migrate.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Code for moving data off a device.
4  */
5
6 #include "bcachefs.h"
7 #include "bkey_buf.h"
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
10 #include "buckets.h"
11 #include "errcode.h"
12 #include "extents.h"
13 #include "io.h"
14 #include "journal.h"
15 #include "keylist.h"
16 #include "migrate.h"
17 #include "move.h"
18 #include "replicas.h"
19 #include "super-io.h"
20
21 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
22                          unsigned dev_idx, int flags, bool metadata)
23 {
24         unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
25         unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
26         unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
27         unsigned nr_good;
28
29         bch2_bkey_drop_device(k, dev_idx);
30
31         nr_good = bch2_bkey_durability(c, k.s_c);
32         if ((!nr_good && !(flags & lost)) ||
33             (nr_good < replicas && !(flags & degraded)))
34                 return -EINVAL;
35
36         return 0;
37 }
38
39 static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
40                                      struct btree_iter *iter,
41                                      struct bkey_s_c k,
42                                      unsigned dev_idx,
43                                      int flags)
44 {
45         struct bch_fs *c = trans->c;
46         struct bkey_i *n;
47         int ret;
48
49         if (!bch2_bkey_has_device(k, dev_idx))
50                 return 0;
51
52         n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
53         ret = PTR_ERR_OR_ZERO(n);
54         if (ret)
55                 return ret;
56
57         bkey_reassemble(n, k);
58
59         ret = drop_dev_ptrs(c, bkey_i_to_s(n), dev_idx, flags, false);
60         if (ret)
61                 return ret;
62
63         /*
64          * If the new extent no longer has any pointers, bch2_extent_normalize()
65          * will do the appropriate thing with it (turning it into a
66          * KEY_TYPE_error key, or just a discard if it was a cached extent)
67          */
68         bch2_extent_normalize(c, bkey_i_to_s(n));
69
70         /*
71          * Since we're not inserting through an extent iterator
72          * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
73          * we aren't using the extent overwrite path to delete, we're
74          * just using the normal key deletion path:
75          */
76         if (bkey_deleted(&n->k))
77                 n->k.size = 0;
78
79         return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
80 }
81
82 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
83 {
84         struct btree_trans trans;
85         struct btree_iter iter;
86         struct bkey_s_c k;
87         enum btree_id id;
88         int ret = 0;
89
90         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
91
92         for (id = 0; id < BTREE_ID_NR; id++) {
93                 if (!btree_type_has_ptrs(id))
94                         continue;
95
96                 ret = for_each_btree_key_commit(&trans, iter, id, POS_MIN,
97                                 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
98                                 NULL, NULL, BTREE_INSERT_NOFAIL,
99                         bch2_dev_usrdata_drop_key(&trans, &iter, k, dev_idx, flags));
100                 if (ret)
101                         break;
102         }
103
104         bch2_trans_exit(&trans);
105
106         return ret;
107 }
108
109 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
110 {
111         struct btree_trans trans;
112         struct btree_iter iter;
113         struct closure cl;
114         struct btree *b;
115         struct bkey_buf k;
116         unsigned id;
117         int ret;
118
119         /* don't handle this yet: */
120         if (flags & BCH_FORCE_IF_METADATA_LOST)
121                 return -EINVAL;
122
123         bch2_bkey_buf_init(&k);
124         bch2_trans_init(&trans, c, 0, 0);
125         closure_init_stack(&cl);
126
127         for (id = 0; id < BTREE_ID_NR; id++) {
128                 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
129                                           BTREE_ITER_PREFETCH);
130 retry:
131                 ret = 0;
132                 while (bch2_trans_begin(&trans),
133                        (b = bch2_btree_iter_peek_node(&iter)) &&
134                        !(ret = PTR_ERR_OR_ZERO(b))) {
135                         if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
136                                                   dev_idx))
137                                 goto next;
138
139                         bch2_bkey_buf_copy(&k, c, &b->key);
140
141                         ret = drop_dev_ptrs(c, bkey_i_to_s(k.k),
142                                             dev_idx, flags, true);
143                         if (ret) {
144                                 bch_err(c, "Cannot drop device without losing data");
145                                 break;
146                         }
147
148                         ret = bch2_btree_node_update_key(&trans, &iter, b, k.k, false);
149                         if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
150                                 ret = 0;
151                                 continue;
152                         }
153
154                         if (ret) {
155                                 bch_err(c, "Error updating btree node key: %s",
156                                         bch2_err_str(ret));
157                                 break;
158                         }
159 next:
160                         bch2_btree_iter_next_node(&iter);
161                 }
162                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
163                         goto retry;
164
165                 bch2_trans_iter_exit(&trans, &iter);
166
167                 if (ret)
168                         goto err;
169         }
170
171         bch2_btree_interior_updates_flush(c);
172         ret = 0;
173 err:
174         bch2_trans_exit(&trans);
175         bch2_bkey_buf_exit(&k, c);
176
177         BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
178
179         return ret;
180 }
181
182 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
183 {
184         return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
185                 bch2_dev_metadata_drop(c, dev_idx, flags);
186 }