2022-06-14 07:07:19 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
|
|
|
#include "alloc_foreground.h"
|
|
|
|
#include "bkey_buf.h"
|
|
|
|
#include "btree_update.h"
|
|
|
|
#include "buckets.h"
|
|
|
|
#include "data_update.h"
|
|
|
|
#include "ec.h"
|
|
|
|
#include "extents.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "keylist.h"
|
|
|
|
#include "move.h"
|
2022-12-15 09:52:11 +08:00
|
|
|
#include "nocow_locking.h"
|
2022-06-14 07:07:19 +08:00
|
|
|
#include "subvolume.h"
|
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
static int insert_snapshot_whiteouts(struct btree_trans *trans,
|
|
|
|
enum btree_id id,
|
|
|
|
struct bpos old_pos,
|
|
|
|
struct bpos new_pos)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
2023-02-17 12:09:27 +08:00
|
|
|
struct btree_iter iter;
|
2022-06-14 07:07:19 +08:00
|
|
|
struct bkey_s_c k;
|
2022-07-14 14:34:48 +08:00
|
|
|
snapshot_id_list s;
|
2022-06-14 07:07:19 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!btree_type_has_snapshots(id))
|
|
|
|
return 0;
|
|
|
|
|
2022-07-14 14:34:48 +08:00
|
|
|
darray_init(&s);
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2022-11-24 16:12:22 +08:00
|
|
|
if (bkey_eq(old_pos, new_pos))
|
2022-06-14 07:07:19 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!snapshot_t(c, old_pos.snapshot)->children[0])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bch2_trans_iter_init(trans, &iter, id, old_pos,
|
|
|
|
BTREE_ITER_NOT_EXTENTS|
|
|
|
|
BTREE_ITER_ALL_SNAPSHOTS);
|
|
|
|
while (1) {
|
|
|
|
k = bch2_btree_iter_prev(&iter);
|
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2022-11-24 16:12:22 +08:00
|
|
|
if (!bkey_eq(old_pos, k.k->p))
|
2022-06-14 07:07:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
|
|
|
|
struct bkey_i *update;
|
|
|
|
|
2022-07-14 14:34:48 +08:00
|
|
|
if (snapshot_list_has_ancestor(c, &s, k.k->p.snapshot))
|
|
|
|
continue;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
|
|
|
update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
|
|
|
|
|
|
|
|
ret = PTR_ERR_OR_ZERO(update);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bkey_init(&update->k);
|
|
|
|
update->k.p = new_pos;
|
|
|
|
update->k.p.snapshot = k.k->p.snapshot;
|
|
|
|
|
2023-02-17 12:09:27 +08:00
|
|
|
ret = bch2_btree_insert_nonextent(trans, id, update,
|
2022-06-14 07:07:19 +08:00
|
|
|
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2022-07-14 14:34:48 +08:00
|
|
|
ret = snapshot_list_add(c, &s, k.k->p.snapshot);
|
2022-06-14 07:07:19 +08:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2022-07-14 14:34:48 +08:00
|
|
|
darray_exit(&s);
|
2022-06-14 07:07:19 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
static void bch2_bkey_mark_dev_cached(struct bkey_s k, unsigned dev)
|
|
|
|
{
|
|
|
|
struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
|
|
|
|
struct bch_extent_ptr *ptr;
|
|
|
|
|
|
|
|
bkey_for_each_ptr(ptrs, ptr)
|
|
|
|
if (ptr->dev == dev)
|
|
|
|
ptr->cached = true;
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
static int __bch2_data_update_index_update(struct btree_trans *trans,
|
|
|
|
struct bch_write_op *op)
|
2022-06-14 07:07:19 +08:00
|
|
|
{
|
|
|
|
struct bch_fs *c = op->c;
|
|
|
|
struct btree_iter iter;
|
|
|
|
struct data_update *m =
|
|
|
|
container_of(op, struct data_update, op);
|
|
|
|
struct keylist *keys = &op->insert_keys;
|
|
|
|
struct bkey_buf _new, _insert;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
bch2_bkey_buf_init(&_new);
|
|
|
|
bch2_bkey_buf_init(&_insert);
|
|
|
|
bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
bch2_trans_iter_init(trans, &iter, m->btree_id,
|
2022-06-14 07:07:19 +08:00
|
|
|
bkey_start_pos(&bch2_keylist_front(keys)->k),
|
|
|
|
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct bkey_s_c k;
|
2022-06-14 07:17:45 +08:00
|
|
|
struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
|
2022-06-14 07:07:19 +08:00
|
|
|
struct bkey_i *insert;
|
|
|
|
struct bkey_i_extent *new;
|
|
|
|
const union bch_extent_entry *entry;
|
|
|
|
struct extent_ptr_decoded p;
|
|
|
|
struct bpos next_pos;
|
|
|
|
bool did_work = false;
|
2021-11-09 01:30:47 +08:00
|
|
|
bool should_check_enospc;
|
2022-06-14 07:07:19 +08:00
|
|
|
s64 i_sectors_delta = 0, disk_sectors_delta = 0;
|
2022-06-14 07:17:45 +08:00
|
|
|
unsigned i;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
bch2_trans_begin(trans);
|
2022-06-14 07:07:19 +08:00
|
|
|
|
|
|
|
k = bch2_btree_iter_peek_slot(&iter);
|
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
new = bkey_i_to_extent(bch2_keylist_front(keys));
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
if (!bch2_extents_match(k, old))
|
2022-06-14 07:07:19 +08:00
|
|
|
goto nomatch;
|
|
|
|
|
|
|
|
bkey_reassemble(_insert.k, k);
|
|
|
|
insert = _insert.k;
|
|
|
|
|
|
|
|
bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
|
|
|
|
new = bkey_i_to_extent(_new.k);
|
|
|
|
bch2_cut_front(iter.pos, &new->k_i);
|
|
|
|
|
|
|
|
bch2_cut_front(iter.pos, insert);
|
|
|
|
bch2_cut_back(new->k.p, insert);
|
|
|
|
bch2_cut_back(insert->k.p, &new->k_i);
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
/*
|
|
|
|
* @old: extent that we read from
|
|
|
|
* @insert: key that we're going to update, initialized from
|
|
|
|
* extent currently in btree - same as @old unless we raced with
|
|
|
|
* other updates
|
|
|
|
* @new: extent with new pointers that we'll be adding to @insert
|
|
|
|
*
|
|
|
|
* Fist, drop rewrite_ptrs from @new:
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
|
|
|
|
if (((1U << i) & m->data_opts.rewrite_ptrs) &&
|
|
|
|
bch2_extent_has_ptr(old, p, bkey_i_to_s_c(insert))) {
|
|
|
|
/*
|
|
|
|
* If we're going to be adding a pointer to the
|
|
|
|
* same device, we have to drop the old one -
|
|
|
|
* otherwise, we can just mark it cached:
|
|
|
|
*/
|
|
|
|
if (bch2_bkey_has_device(bkey_i_to_s_c(&new->k_i), p.ptr.dev))
|
|
|
|
bch2_bkey_drop_device_noerror(bkey_i_to_s(insert), p.ptr.dev);
|
|
|
|
else
|
|
|
|
bch2_bkey_mark_dev_cached(bkey_i_to_s(insert), p.ptr.dev);
|
|
|
|
}
|
|
|
|
i++;
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
|
|
|
|
/* Add new ptrs: */
|
2022-06-14 07:07:19 +08:00
|
|
|
extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
|
2023-01-08 11:55:42 +08:00
|
|
|
const struct bch_extent_ptr *existing_ptr =
|
|
|
|
bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev);
|
|
|
|
|
|
|
|
if (existing_ptr && existing_ptr->cached) {
|
|
|
|
/*
|
|
|
|
* We're replacing a cached pointer with a non
|
|
|
|
* cached pointer:
|
|
|
|
*/
|
|
|
|
bch2_bkey_drop_device_noerror(bkey_i_to_s(insert),
|
|
|
|
existing_ptr->dev);
|
|
|
|
} else if (existing_ptr) {
|
2022-06-14 07:07:19 +08:00
|
|
|
/*
|
|
|
|
* raced with another move op? extent already
|
|
|
|
* has a pointer to the device we just wrote
|
|
|
|
* data to
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bch2_extent_ptr_decoded_append(insert, &p);
|
|
|
|
did_work = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!did_work)
|
|
|
|
goto nomatch;
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
|
2022-06-14 07:07:19 +08:00
|
|
|
bch2_extent_normalize(c, bkey_i_to_s(insert));
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
ret = bch2_sum_sector_overwrites(trans, &iter, insert,
|
2022-06-14 07:07:19 +08:00
|
|
|
&should_check_enospc,
|
|
|
|
&i_sectors_delta,
|
|
|
|
&disk_sectors_delta);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (disk_sectors_delta > (s64) op->res.sectors) {
|
|
|
|
ret = bch2_disk_reservation_add(c, &op->res,
|
|
|
|
disk_sectors_delta - op->res.sectors,
|
|
|
|
!should_check_enospc
|
|
|
|
? BCH_DISK_RESERVATION_NOFAIL : 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_pos = insert->k.p;
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
ret = insert_snapshot_whiteouts(trans, m->btree_id,
|
2022-06-14 07:07:19 +08:00
|
|
|
k.k->p, insert->k.p) ?:
|
2022-11-14 14:31:10 +08:00
|
|
|
bch2_trans_update(trans, &iter, insert,
|
2022-06-14 07:07:19 +08:00
|
|
|
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
|
2022-11-14 14:31:10 +08:00
|
|
|
bch2_trans_commit(trans, &op->res,
|
2022-11-03 12:29:43 +08:00
|
|
|
NULL,
|
2022-06-14 07:07:19 +08:00
|
|
|
BTREE_INSERT_NOFAIL|
|
|
|
|
m->data_opts.btree_insert_flags);
|
|
|
|
if (!ret) {
|
|
|
|
bch2_btree_iter_set_pos(&iter, next_pos);
|
2022-08-28 00:48:36 +08:00
|
|
|
|
|
|
|
this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
|
|
|
|
trace_move_extent_finish(&new->k);
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
|
|
|
err:
|
2022-07-18 11:06:38 +08:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2022-06-14 07:07:19 +08:00
|
|
|
ret = 0;
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
next:
|
2022-11-24 16:12:22 +08:00
|
|
|
while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
|
2022-06-14 07:07:19 +08:00
|
|
|
bch2_keylist_pop_front(keys);
|
|
|
|
if (bch2_keylist_empty(keys))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
nomatch:
|
|
|
|
if (m->ctxt) {
|
|
|
|
BUG_ON(k.k->p.offset <= iter.pos.offset);
|
|
|
|
atomic64_inc(&m->ctxt->stats->keys_raced);
|
|
|
|
atomic64_add(k.k->p.offset - iter.pos.offset,
|
|
|
|
&m->ctxt->stats->sectors_raced);
|
|
|
|
}
|
2022-08-28 00:48:36 +08:00
|
|
|
|
|
|
|
this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
|
|
|
|
trace_move_extent_fail(&new->k);
|
|
|
|
|
2022-06-14 07:07:19 +08:00
|
|
|
bch2_btree_iter_advance(&iter);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
out:
|
2022-11-14 14:31:10 +08:00
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2022-06-14 07:07:19 +08:00
|
|
|
bch2_bkey_buf_exit(&_insert, c);
|
|
|
|
bch2_bkey_buf_exit(&_new, c);
|
2022-07-18 11:06:38 +08:00
|
|
|
BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
|
2022-06-14 07:07:19 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
int bch2_data_update_index_update(struct bch_write_op *op)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = op->c;
|
|
|
|
struct btree_trans trans;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
|
|
|
|
ret = __bch2_data_update_index_update(&trans, op);
|
|
|
|
bch2_trans_exit(&trans);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
void bch2_data_update_read_done(struct data_update *m,
|
|
|
|
struct bch_extent_crc_unpacked crc)
|
2022-06-14 07:07:19 +08:00
|
|
|
{
|
|
|
|
/* write bio must own pages: */
|
|
|
|
BUG_ON(!m->op.wbio.bio.bi_vcnt);
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
m->op.crc = crc;
|
|
|
|
m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
closure_call(&m->op.cl, bch2_write, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_data_update_exit(struct data_update *update)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = update->op.c;
|
bcachefs: Nocow support
This adds support for nocow mode, where we do writes in-place when
possible. Patch components:
- New boolean filesystem and inode option, nocow: note that when nocow
is enabled, data checksumming and compression are implicitly disabled
- To prevent in-place writes from racing with data moves
(data_update.c) or bucket reuse (i.e. a bucket being reused and
re-allocated while a nocow write is in flight, we have a new locking
mechanism.
Buckets can be locked for either data update or data move, using a
fixed size hash table of two_state_shared locks. We don't have any
chaining, meaning updates and moves to different buckets that hash to
the same lock will wait unnecessarily - we'll want to watch for this
becoming an issue.
- The allocator path also needs to check for in-place writes in flight
to a given bucket before giving it out: thus we add another counter
to bucket_alloc_state so we can track this.
- Fsync now may need to issue cache flushes to block devices instead of
flushing the journal. We add a device bitmask to bch_inode_info,
ei_devs_need_flush, which tracks devices that need to have flushes
issued - note that this will lead to unnecessary flushes when other
codepaths have already issued flushes, we may want to replace this with
a sequence number.
- New nocow write path: look up extents, and if they're writable write
to them - otherwise fall back to the normal COW write path.
XXX: switch to sequence numbers instead of bitmask for devs needing
journal flush
XXX: ei_quota_lock being a mutex means bch2_nocow_write_done() needs to
run in process context - see if we can improve this
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2022-11-03 05:12:00 +08:00
|
|
|
struct bkey_ptrs_c ptrs =
|
|
|
|
bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
|
|
|
|
const struct bch_extent_ptr *ptr;
|
|
|
|
|
2023-02-12 01:38:28 +08:00
|
|
|
bkey_for_each_ptr(ptrs, ptr) {
|
bcachefs: Nocow support
This adds support for nocow mode, where we do writes in-place when
possible. Patch components:
- New boolean filesystem and inode option, nocow: note that when nocow
is enabled, data checksumming and compression are implicitly disabled
- To prevent in-place writes from racing with data moves
(data_update.c) or bucket reuse (i.e. a bucket being reused and
re-allocated while a nocow write is in flight, we have a new locking
mechanism.
Buckets can be locked for either data update or data move, using a
fixed size hash table of two_state_shared locks. We don't have any
chaining, meaning updates and moves to different buckets that hash to
the same lock will wait unnecessarily - we'll want to watch for this
becoming an issue.
- The allocator path also needs to check for in-place writes in flight
to a given bucket before giving it out: thus we add another counter
to bucket_alloc_state so we can track this.
- Fsync now may need to issue cache flushes to block devices instead of
flushing the journal. We add a device bitmask to bch_inode_info,
ei_devs_need_flush, which tracks devices that need to have flushes
issued - note that this will lead to unnecessary flushes when other
codepaths have already issued flushes, we may want to replace this with
a sequence number.
- New nocow write path: look up extents, and if they're writable write
to them - otherwise fall back to the normal COW write path.
XXX: switch to sequence numbers instead of bitmask for devs needing
journal flush
XXX: ei_quota_lock being a mutex means bch2_nocow_write_done() needs to
run in process context - see if we can improve this
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2022-11-03 05:12:00 +08:00
|
|
|
bch2_bucket_nocow_unlock(&c->nocow_locks,
|
2023-02-12 01:38:28 +08:00
|
|
|
PTR_BUCKET_POS(c, ptr), 0);
|
|
|
|
percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
|
|
|
|
}
|
2022-06-14 07:17:45 +08:00
|
|
|
|
|
|
|
bch2_bkey_buf_exit(&update->k, c);
|
|
|
|
bch2_disk_reservation_put(c, &update->op.res);
|
|
|
|
bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:31:10 +08:00
|
|
|
void bch2_update_unwritten_extent(struct btree_trans *trans,
|
|
|
|
struct data_update *update)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = update->op.c;
|
|
|
|
struct bio *bio = &update->op.wbio.bio;
|
|
|
|
struct bkey_i_extent *e;
|
|
|
|
struct write_point *wp;
|
|
|
|
struct bch_extent_ptr *ptr;
|
|
|
|
struct closure cl;
|
|
|
|
struct btree_iter iter;
|
|
|
|
struct bkey_s_c k;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
closure_init_stack(&cl);
|
|
|
|
bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
|
|
|
|
|
|
|
|
while (bio_sectors(bio)) {
|
|
|
|
unsigned sectors = bio_sectors(bio);
|
|
|
|
|
|
|
|
bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
|
|
|
|
BTREE_ITER_SLOTS);
|
|
|
|
ret = lockrestart_do(trans, ({
|
|
|
|
k = bch2_btree_iter_peek_slot(&iter);
|
|
|
|
bkey_err(k);
|
|
|
|
}));
|
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
|
|
|
|
|
|
|
if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
e = bkey_extent_init(update->op.insert_keys.top);
|
|
|
|
e->k.p = update->op.pos;
|
|
|
|
|
|
|
|
ret = bch2_alloc_sectors_start_trans(trans,
|
|
|
|
update->op.target,
|
|
|
|
false,
|
|
|
|
update->op.write_point,
|
|
|
|
&update->op.devs_have,
|
|
|
|
update->op.nr_replicas,
|
|
|
|
update->op.nr_replicas,
|
|
|
|
update->op.alloc_reserve,
|
|
|
|
0, &cl, &wp);
|
|
|
|
if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
|
|
|
|
bch2_trans_unlock(trans);
|
|
|
|
closure_sync(&cl);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sectors = min(sectors, wp->sectors_free);
|
|
|
|
|
|
|
|
bch2_key_resize(&e->k, sectors);
|
|
|
|
|
|
|
|
bch2_open_bucket_get(c, wp, &update->op.open_buckets);
|
|
|
|
bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
|
|
|
|
bch2_alloc_sectors_done(c, wp);
|
|
|
|
|
|
|
|
bio_advance(bio, sectors << 9);
|
|
|
|
update->op.pos.offset += sectors;
|
|
|
|
|
|
|
|
extent_for_each_ptr(extent_i_to_s(e), ptr)
|
|
|
|
ptr->unwritten = true;
|
|
|
|
bch2_keylist_push(&update->op.insert_keys);
|
|
|
|
|
|
|
|
ret = __bch2_data_update_index_update(trans, &update->op);
|
|
|
|
|
|
|
|
bch2_open_buckets_put(c, &update->op.open_buckets);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
|
|
|
|
bch2_trans_unlock(trans);
|
|
|
|
closure_sync(&cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:53:02 +08:00
|
|
|
int bch2_data_update_init(struct btree_trans *trans,
|
|
|
|
struct moving_context *ctxt,
|
|
|
|
struct data_update *m,
|
2022-06-14 07:07:19 +08:00
|
|
|
struct write_point_specifier wp,
|
|
|
|
struct bch_io_opts io_opts,
|
2022-06-14 07:17:45 +08:00
|
|
|
struct data_update_opts data_opts,
|
2022-06-14 07:07:19 +08:00
|
|
|
enum btree_id btree_id,
|
|
|
|
struct bkey_s_c k)
|
|
|
|
{
|
2023-01-03 06:53:02 +08:00
|
|
|
struct bch_fs *c = trans->c;
|
2022-06-14 07:07:19 +08:00
|
|
|
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
|
|
|
|
const union bch_extent_entry *entry;
|
|
|
|
struct extent_ptr_decoded p;
|
2023-02-12 01:38:28 +08:00
|
|
|
const struct bch_extent_ptr *ptr;
|
2022-06-14 07:17:45 +08:00
|
|
|
unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
|
2023-01-06 16:11:07 +08:00
|
|
|
unsigned int ptrs_locked = 0;
|
2022-06-14 07:07:19 +08:00
|
|
|
int ret;
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
bch2_bkey_buf_init(&m->k);
|
|
|
|
bch2_bkey_buf_reassemble(&m->k, c, k);
|
2022-06-14 07:07:19 +08:00
|
|
|
m->btree_id = btree_id;
|
|
|
|
m->data_opts = data_opts;
|
|
|
|
|
|
|
|
bch2_write_op_init(&m->op, c, io_opts);
|
2022-06-14 07:17:45 +08:00
|
|
|
m->op.pos = bkey_start_pos(k.k);
|
|
|
|
m->op.version = k.k->version;
|
2022-10-20 06:31:33 +08:00
|
|
|
m->op.target = data_opts.target;
|
2022-06-14 07:07:19 +08:00
|
|
|
m->op.write_point = wp;
|
2022-06-14 07:17:45 +08:00
|
|
|
m->op.flags |= BCH_WRITE_PAGES_STABLE|
|
2022-06-14 07:07:19 +08:00
|
|
|
BCH_WRITE_PAGES_OWNED|
|
|
|
|
BCH_WRITE_DATA_ENCODED|
|
2022-06-14 07:17:45 +08:00
|
|
|
BCH_WRITE_MOVE|
|
|
|
|
m->data_opts.write_flags;
|
|
|
|
m->op.compression_type =
|
|
|
|
bch2_compression_opt_to_type[io_opts.background_compression ?:
|
|
|
|
io_opts.compression];
|
|
|
|
if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
|
|
|
|
m->op.alloc_reserve = RESERVE_movinggc;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2023-02-12 01:38:28 +08:00
|
|
|
bkey_for_each_ptr(ptrs, ptr)
|
|
|
|
percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
i = 0;
|
|
|
|
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
|
2023-01-06 16:11:07 +08:00
|
|
|
bool locked;
|
|
|
|
|
2022-10-09 15:32:17 +08:00
|
|
|
if (((1U << i) & m->data_opts.rewrite_ptrs) &&
|
|
|
|
p.ptr.cached)
|
|
|
|
BUG();
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2023-01-08 11:55:42 +08:00
|
|
|
if (!((1U << i) & m->data_opts.rewrite_ptrs) &&
|
|
|
|
!p.ptr.cached)
|
2022-06-14 07:17:45 +08:00
|
|
|
bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
if (((1U << i) & m->data_opts.rewrite_ptrs) &&
|
|
|
|
crc_is_compressed(p.crc))
|
|
|
|
reserve_sectors += k.k->size;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
/*
|
|
|
|
* op->csum_type is normally initialized from the fs/file's
|
|
|
|
* current options - but if an extent is encrypted, we require
|
|
|
|
* that it stays encrypted:
|
|
|
|
*/
|
|
|
|
if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
|
|
|
|
m->op.nonce = p.crc.nonce + p.crc.offset;
|
|
|
|
m->op.csum_type = p.crc.csum_type;
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
|
|
|
|
m->op.incompressible = true;
|
2022-06-14 07:07:19 +08:00
|
|
|
|
2023-01-03 06:53:02 +08:00
|
|
|
if (ctxt) {
|
|
|
|
move_ctxt_wait_event(ctxt, trans,
|
|
|
|
(locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
|
|
|
|
PTR_BUCKET_POS(c, &p.ptr), 0)) ||
|
|
|
|
!atomic_read(&ctxt->read_sectors));
|
|
|
|
|
|
|
|
if (!locked)
|
|
|
|
bch2_bucket_nocow_lock(&c->nocow_locks,
|
|
|
|
PTR_BUCKET_POS(c, &p.ptr), 0);
|
|
|
|
} else {
|
2023-01-06 16:11:07 +08:00
|
|
|
if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
|
|
|
|
PTR_BUCKET_POS(c, &p.ptr), 0)) {
|
|
|
|
ret = -BCH_ERR_nocow_lock_blocked;
|
|
|
|
goto err;
|
|
|
|
}
|
2023-01-03 06:53:02 +08:00
|
|
|
}
|
2023-01-06 16:11:07 +08:00
|
|
|
ptrs_locked |= (1U << i);
|
|
|
|
i++;
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
2022-06-14 07:17:45 +08:00
|
|
|
|
|
|
|
if (reserve_sectors) {
|
|
|
|
ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
|
|
|
|
m->data_opts.extra_replicas
|
|
|
|
? 0
|
|
|
|
: BCH_DISK_RESERVATION_NOFAIL);
|
|
|
|
if (ret)
|
2022-12-09 07:37:56 +08:00
|
|
|
goto err;
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 07:17:45 +08:00
|
|
|
m->op.nr_replicas = m->op.nr_replicas_required =
|
|
|
|
hweight32(m->data_opts.rewrite_ptrs) + m->data_opts.extra_replicas;
|
2022-10-09 15:32:17 +08:00
|
|
|
|
|
|
|
BUG_ON(!m->op.nr_replicas);
|
2022-11-14 14:31:10 +08:00
|
|
|
|
|
|
|
/* Special handling required: */
|
|
|
|
if (bkey_extent_is_unwritten(k))
|
|
|
|
return -BCH_ERR_unwritten_extent_update;
|
2022-06-14 07:07:19 +08:00
|
|
|
return 0;
|
2022-12-09 07:37:56 +08:00
|
|
|
err:
|
2023-01-06 16:11:07 +08:00
|
|
|
i = 0;
|
|
|
|
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
|
|
|
|
if ((1U << i) & ptrs_locked)
|
|
|
|
bch2_bucket_nocow_unlock(&c->nocow_locks,
|
2023-02-12 01:38:28 +08:00
|
|
|
PTR_BUCKET_POS(c, &p.ptr), 0);
|
|
|
|
percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
|
2023-01-06 16:11:07 +08:00
|
|
|
i++;
|
|
|
|
}
|
2022-12-09 07:37:56 +08:00
|
|
|
|
|
|
|
bch2_bkey_buf_exit(&m->k, c);
|
|
|
|
bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
|
|
|
|
return ret;
|
2022-06-14 07:07:19 +08:00
|
|
|
}
|
2022-10-09 15:32:17 +08:00
|
|
|
|
|
|
|
void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
|
|
|
|
{
|
|
|
|
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
|
|
|
|
const struct bch_extent_ptr *ptr;
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
bkey_for_each_ptr(ptrs, ptr) {
|
|
|
|
if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
|
|
|
|
opts->kill_ptrs |= 1U << i;
|
|
|
|
opts->rewrite_ptrs ^= 1U << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|