bch2_alloc_read_key(c, bkey_i_to_s_c(k));
}
+ for_each_member_device(ca, c, i)
+ bch2_dev_usage_from_buckets(c, ca);
+
mutex_lock(&c->bucket_clock[READ].lock);
for_each_member_device(ca, c, i) {
down_read(&ca->bucket_lock);
#endif
struct bkey_i_alloc *a = bkey_alloc_init(&alloc_key.k);
struct bucket *g;
- struct bucket_mark m;
+ struct bucket_mark m, new;
int ret;
BUG_ON(BKEY_ALLOC_VAL_U64s_MAX > 8);
a->k.p = POS(ca->dev_idx, b);
+ bch2_btree_iter_set_pos(iter, a->k.p);
+
+ ret = bch2_btree_iter_traverse(iter);
+ if (ret)
+ return ret;
+
percpu_down_read(&c->mark_lock);
g = bucket(ca, b);
- m = bucket_cmpxchg(g, m, m.dirty = false);
+ m = READ_ONCE(g->mark);
+
+ if (!m.dirty) {
+ percpu_up_read(&c->mark_lock);
+ return 0;
+ }
__alloc_write_key(a, g, m);
percpu_up_read(&c->mark_lock);
bch2_btree_iter_cond_resched(iter);
- bch2_btree_iter_set_pos(iter, a->k.p);
-
ret = bch2_btree_insert_at(c, NULL, journal_seq,
+ BTREE_INSERT_NOCHECK_RW|
BTREE_INSERT_NOFAIL|
BTREE_INSERT_USE_RESERVE|
BTREE_INSERT_USE_ALLOC_RESERVE|
flags,
BTREE_INSERT_ENTRY(iter, &a->k_i));
+ if (ret)
+ return ret;
- if (!ret && ca->buckets_written)
+ new = m;
+ new.dirty = false;
+ atomic64_cmpxchg(&g->_mark.v, m.v.counter, new.v.counter);
+
+ if (ca->buckets_written)
set_bit(b, ca->buckets_written);
- return ret;
+ return 0;
}
int bch2_alloc_replay_key(struct bch_fs *c, struct bkey_i *k)
for (i = 0; i < RESERVE_NR; i++)
if (fifo_push(&ca->free[i], bucket)) {
fifo_pop(&ca->free_inc, bucket);
+
closure_wake_up(&c->freelist_wait);
+ ca->allocator_blocked_full = false;
+
spin_unlock(&c->freelist_lock);
goto out;
}
+
+ if (!ca->allocator_blocked_full) {
+ ca->allocator_blocked_full = true;
+ closure_wake_up(&c->freelist_wait);
+ }
+
spin_unlock(&c->freelist_lock);
if ((current->flags & PF_KTHREAD) &&
set_bit(ca->dev_idx, c->rw_devs[i].d);
}
+void bch2_dev_allocator_quiesce(struct bch_fs *c, struct bch_dev *ca)
+{
+ closure_wait_event(&c->freelist_wait, ca->allocator_blocked_full);
+}
+
/* stop allocator thread: */
void bch2_dev_allocator_stop(struct bch_dev *ca)
{
void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *);
void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *);
+void bch2_dev_allocator_quiesce(struct bch_fs *, struct bch_dev *);
void bch2_dev_allocator_stop(struct bch_dev *);
int bch2_dev_allocator_start(struct bch_dev *);
size_t inc_gen_needs_gc;
size_t inc_gen_really_needs_gc;
+
+ /*
+ * XXX: this should be an enum for allocator state, so as to include
+ * error state
+ */
bool allocator_blocked;
+ bool allocator_blocked_full;
alloc_heap alloc_heap;
__BTREE_INSERT_ATOMIC,
__BTREE_INSERT_NOUNLOCK,
__BTREE_INSERT_NOFAIL,
+ __BTREE_INSERT_NOCHECK_RW,
__BTREE_INSERT_USE_RESERVE,
__BTREE_INSERT_USE_ALLOC_RESERVE,
__BTREE_INSERT_JOURNAL_REPLAY,
/* Don't check for -ENOSPC: */
#define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
+#define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
+
/* for copygc, or when merging btree nodes */
#define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
#define BTREE_INSERT_USE_ALLOC_RESERVE (1 << __BTREE_INSERT_USE_ALLOC_RESERVE)
mutex_lock(&c->btree_interior_update_lock);
percpu_down_read(&c->mark_lock);
+ preempt_disable();
fs_usage = bch2_fs_usage_get_scratch(c);
bch2_mark_key_locked(c, bkey_i_to_s_c(insert),
bch2_fs_usage_apply(c, fs_usage, &as->reserve->disk_res,
gc_pos_btree_node(b));
+ preempt_enable();
percpu_up_read(&c->mark_lock);
mutex_unlock(&c->btree_interior_update_lock);
trans_for_each_entry(trans, i)
btree_insert_entry_checks(c, i);
- if (unlikely(!percpu_ref_tryget(&c->writes)))
+ if (unlikely(!(trans->flags & BTREE_INSERT_NOCHECK_RW) &&
+ !percpu_ref_tryget(&c->writes)))
return -EROFS;
retry:
trans_for_each_iter(trans, i) {
trans_for_each_iter(trans, i)
bch2_btree_iter_downgrade(i->iter);
out:
- percpu_ref_put(&c->writes);
+ if (unlikely(!(trans->flags & BTREE_INSERT_NOCHECK_RW)))
+ percpu_ref_put(&c->writes);
/* make sure we didn't drop or screw up locks: */
trans_for_each_iter(trans, i) {
*old = bucket_data_cmpxchg(c, ca, fs_usage, g, new, ({
BUG_ON(!is_available_bucket(new));
- new.owned_by_allocator = 1;
+ new.owned_by_allocator = true;
+ new.dirty = true;
new.data_type = 0;
new.cached_sectors = 0;
new.dirty_sectors = 0;
type != BCH_DATA_JOURNAL);
bucket_data_cmpxchg(c, ca, fs_usage, g, new, ({
+ new.dirty = true;
new.data_type = type;
checked_add(new.dirty_sectors, sectors);
}));
true);
} else {
struct bucket *g;
- struct bucket_mark old, new;
+ struct bucket_mark new;
rcu_read_lock();
g = bucket(ca, b);
- old = bucket_cmpxchg(g, new, ({
- new.data_type = type;
+ bucket_cmpxchg(g, new, ({
+ new.dirty = true;
+ new.data_type = type;
checked_add(new.dirty_sectors, sectors);
}));
do {
new.v.counter = old.v.counter = v;
+ new.dirty = true;
+
/*
* Check this after reading bucket mark to guard against
* the allocator invalidating a bucket after we've already
BUG_ON(ptr_stale(ca, ptr));
old = bucket_data_cmpxchg(c, ca, fs_usage, g, new, ({
+ new.dirty = true;
new.stripe = enabled;
if (journal_seq) {
new.journal_seq_valid = 1;
struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *, struct bch_dev *);
+void bch2_dev_usage_from_buckets(struct bch_fs *, struct bch_dev *);
+
static inline u64 __dev_buckets_available(struct bch_dev *ca,
struct bch_dev_usage stats)
{
static void __bch2_fs_read_only(struct bch_fs *c)
{
struct bch_dev *ca;
+ bool wrote;
unsigned i;
+ int ret;
bch2_rebalance_stop(c);
*/
bch2_journal_flush_all_pins(&c->journal);
- for_each_member_device(ca, c, i)
- bch2_dev_allocator_stop(ca);
+ do {
+ ret = bch2_alloc_write(c, false, &wrote);
+ if (ret) {
+ bch2_fs_inconsistent(c, "error writing out alloc info %i", ret);
+ break;
+ }
- bch2_journal_flush_all_pins(&c->journal);
+ for_each_member_device(ca, c, i)
+ bch2_dev_allocator_quiesce(c, ca);
- /*
- * We need to explicitly wait on btree interior updates to complete
- * before stopping the journal, flushing all journal pins isn't
- * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
- * interior updates have to drop their journal pin before they're
- * fully complete:
- */
- closure_wait_event(&c->btree_interior_update_wait,
- !bch2_btree_interior_updates_nr_pending(c));
+ bch2_journal_flush_all_pins(&c->journal);
+
+ /*
+ * We need to explicitly wait on btree interior updates to complete
+ * before stopping the journal, flushing all journal pins isn't
+ * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
+ * interior updates have to drop their journal pin before they're
+ * fully complete:
+ */
+ closure_wait_event(&c->btree_interior_update_wait,
+ !bch2_btree_interior_updates_nr_pending(c));
+ } while (wrote);
+
+ for_each_member_device(ca, c, i)
+ bch2_dev_allocator_stop(ca);
bch2_fs_journal_stop(&c->journal);
+ /* XXX: mark super that alloc info is persistent */
+
/*
* the journal kicks off btree writes via reclaim - wait for in flight
* writes after stopping journal: