blob: ddc187fb693d1441fec6ecb39b0b49cec848dc6f [file] [log] [blame]
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Code for moving data off a device.
4 */
5
6#include "bcachefs.h"
Kent Overstreet07a10062020-12-17 15:08:58 -05007#include "bkey_buf.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08008#include "btree_update.h"
Kent Overstreet7ef2a732019-01-21 15:32:13 -05009#include "btree_update_interior.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080010#include "buckets.h"
Kent Overstreetd4bf5ee2022-07-18 19:42:58 -040011#include "errcode.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080012#include "extents.h"
Kent Overstreet1809b8c2023-09-10 18:05:17 -040013#include "io_write.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080014#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
Kent Overstreet26609b62018-11-01 15:10:01 -040021static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080022 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
Kent Overstreet26609b62018-11-01 15:10:01 -040029 bch2_bkey_drop_device(k, dev_idx);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080030
Kent Overstreet26609b62018-11-01 15:10:01 -040031 nr_good = bch2_bkey_durability(c, k.s_c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080032 if ((!nr_good && !(flags & lost)) ||
33 (nr_good < replicas && !(flags & degraded)))
Kent Overstreetce3e9282024-02-05 21:44:23 -050034 return -BCH_ERR_remove_would_lose_data;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080035
36 return 0;
37}
38
Kent Overstreet89333152022-07-17 00:31:40 -040039static 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)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080044{
Kent Overstreet89333152022-07-17 00:31:40 -040045 struct bch_fs *c = trans->c;
46 struct bkey_i *n;
47 int ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080048
Kent Overstreet702ffea2023-03-10 16:28:37 -050049 if (!bch2_bkey_has_device_c(k, dev_idx))
Kent Overstreet89333152022-07-17 00:31:40 -040050 return 0;
Kent Overstreet0564b162019-03-13 20:49:16 -040051
Kent Overstreet5dd8c602024-04-07 18:05:34 -040052 n = bch2_bkey_make_mut(trans, iter, &k, BTREE_UPDATE_internal_snapshot_node);
Kent Overstreet89333152022-07-17 00:31:40 -040053 ret = PTR_ERR_OR_ZERO(n);
54 if (ret)
55 return ret;
Kent Overstreet0564b162019-03-13 20:49:16 -040056
Kent Overstreet89333152022-07-17 00:31:40 -040057 ret = drop_dev_ptrs(c, bkey_i_to_s(n), dev_idx, flags, false);
58 if (ret)
59 return ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080060
Kent Overstreet89333152022-07-17 00:31:40 -040061 /*
62 * If the new extent no longer has any pointers, bch2_extent_normalize()
63 * will do the appropriate thing with it (turning it into a
64 * KEY_TYPE_error key, or just a discard if it was a cached extent)
65 */
66 bch2_extent_normalize(c, bkey_i_to_s(n));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080067
Kent Overstreet89333152022-07-17 00:31:40 -040068 /*
69 * Since we're not inserting through an extent iterator
Kent Overstreet5dd8c602024-04-07 18:05:34 -040070 * (BTREE_ITER_all_snapshots iterators aren't extent iterators),
Kent Overstreet89333152022-07-17 00:31:40 -040071 * we aren't using the extent overwrite path to delete, we're
72 * just using the normal key deletion path:
73 */
74 if (bkey_deleted(&n->k))
75 n->k.size = 0;
Kent Overstreetdbda63b2023-04-30 19:21:06 -040076 return 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080077}
78
Kent Overstreet76426092019-08-16 09:59:56 -040079static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
80{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040081 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet89333152022-07-17 00:31:40 -040082 enum btree_id id;
83 int ret = 0;
84
Kent Overstreet89333152022-07-17 00:31:40 -040085 for (id = 0; id < BTREE_ID_NR; id++) {
86 if (!btree_type_has_ptrs(id))
87 continue;
88
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040089 ret = for_each_btree_key_commit(trans, iter, id, POS_MIN,
Kent Overstreet5dd8c602024-04-07 18:05:34 -040090 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
Kent Overstreetcb52d23e2023-11-11 16:31:50 -050091 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040092 bch2_dev_usrdata_drop_key(trans, &iter, k, dev_idx, flags));
Kent Overstreet89333152022-07-17 00:31:40 -040093 if (ret)
94 break;
95 }
96
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040097 bch2_trans_put(trans);
Kent Overstreet89333152022-07-17 00:31:40 -040098
99 return ret;
Kent Overstreet76426092019-08-16 09:59:56 -0400100}
101
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800102static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
103{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400104 struct btree_trans *trans;
Kent Overstreet67e0dd82021-08-30 15:18:31 -0400105 struct btree_iter iter;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800106 struct closure cl;
107 struct btree *b;
Kent Overstreet07a10062020-12-17 15:08:58 -0500108 struct bkey_buf k;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800109 unsigned id;
110 int ret;
111
112 /* don't handle this yet: */
113 if (flags & BCH_FORCE_IF_METADATA_LOST)
Kent Overstreetce3e9282024-02-05 21:44:23 -0500114 return -BCH_ERR_remove_with_metadata_missing_unimplemented;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800115
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400116 trans = bch2_trans_get(c);
Kent Overstreet07a10062020-12-17 15:08:58 -0500117 bch2_bkey_buf_init(&k);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800118 closure_init_stack(&cl);
119
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800120 for (id = 0; id < BTREE_ID_NR; id++) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400121 bch2_trans_node_iter_init(trans, &iter, id, POS_MIN, 0, 0,
Kent Overstreet5dd8c602024-04-07 18:05:34 -0400122 BTREE_ITER_prefetch);
Kent Overstreetd355c6f2021-10-19 14:20:50 -0400123retry:
Kent Overstreetb71717d2021-10-19 15:11:45 -0400124 ret = 0;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400125 while (bch2_trans_begin(trans),
Kent Overstreetd355c6f2021-10-19 14:20:50 -0400126 (b = bch2_btree_iter_peek_node(&iter)) &&
127 !(ret = PTR_ERR_OR_ZERO(b))) {
Kent Overstreet702ffea2023-03-10 16:28:37 -0500128 if (!bch2_bkey_has_device_c(bkey_i_to_s_c(&b->key), dev_idx))
Kent Overstreet56767d62021-10-07 14:59:00 -0400129 goto next;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800130
Kent Overstreet07a10062020-12-17 15:08:58 -0500131 bch2_bkey_buf_copy(&k, c, &b->key);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800132
Kent Overstreet07a10062020-12-17 15:08:58 -0500133 ret = drop_dev_ptrs(c, bkey_i_to_s(k.k),
Kent Overstreet31ba2cd2020-01-03 22:38:14 -0500134 dev_idx, flags, true);
Kent Overstreetce3e9282024-02-05 21:44:23 -0500135 if (ret)
Kent Overstreet50dc0f62021-03-19 20:29:11 -0400136 break;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800137
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400138 ret = bch2_btree_node_update_key(trans, &iter, b, k.k, 0, false);
Kent Overstreet549d1732022-07-17 23:06:38 -0400139 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
Kent Overstreet50dc0f62021-03-19 20:29:11 -0400140 ret = 0;
Kent Overstreet56767d62021-10-07 14:59:00 -0400141 continue;
Kent Overstreet31ba2cd2020-01-03 22:38:14 -0500142 }
Kent Overstreet56767d62021-10-07 14:59:00 -0400143
Kent Overstreetcf904c82023-12-16 22:43:41 -0500144 bch_err_msg(c, ret, "updating btree node key");
145 if (ret)
Kent Overstreet50dc0f62021-03-19 20:29:11 -0400146 break;
Kent Overstreet56767d62021-10-07 14:59:00 -0400147next:
148 bch2_btree_iter_next_node(&iter);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800149 }
Kent Overstreet549d1732022-07-17 23:06:38 -0400150 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreetd355c6f2021-10-19 14:20:50 -0400151 goto retry;
152
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400153 bch2_trans_iter_exit(trans, &iter);
Kent Overstreet50dc0f62021-03-19 20:29:11 -0400154
155 if (ret)
156 goto err;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800157 }
158
Kent Overstreetc0960602022-04-17 17:30:49 -0400159 bch2_btree_interior_updates_flush(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800160 ret = 0;
Kent Overstreet424eb882019-03-25 15:10:15 -0400161err:
Kent Overstreet07a10062020-12-17 15:08:58 -0500162 bch2_bkey_buf_exit(&k, c);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400163 bch2_trans_put(trans);
Kent Overstreet424eb882019-03-25 15:10:15 -0400164
Kent Overstreet549d1732022-07-17 23:06:38 -0400165 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800166
167 return ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800168}
169
170int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
171{
172 return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
Kent Overstreet31ba2cd2020-01-03 22:38:14 -0500173 bch2_dev_metadata_drop(c, dev_idx, flags);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800174}