]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/alloc_foreground.c
New upstream snapshot
[bcachefs-tools-debian] / libbcachefs / alloc_foreground.c
index 412fed47948278060b77516e6c5a4801df124a78..9b81ed2665c8d93324d19dc1e5f8e5f0e4930eae 100644 (file)
  * reference _after_ doing the index update that makes its allocation reachable.
  */
 
+static void bch2_open_bucket_hash_add(struct bch_fs *c, struct open_bucket *ob)
+{
+       open_bucket_idx_t idx = ob - c->open_buckets;
+       open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
+
+       ob->hash = *slot;
+       *slot = idx;
+}
+
+static void bch2_open_bucket_hash_remove(struct bch_fs *c, struct open_bucket *ob)
+{
+       open_bucket_idx_t idx = ob - c->open_buckets;
+       open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
+
+       while (*slot != idx) {
+               BUG_ON(!*slot);
+               slot = &c->open_buckets[*slot].hash;
+       }
+
+       *slot = ob->hash;
+       ob->hash = 0;
+}
+
 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
 {
-       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
 
        if (ob->ec) {
                bch2_ec_bucket_written(c, ob);
@@ -55,14 +78,16 @@ void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
        percpu_down_read(&c->mark_lock);
        spin_lock(&ob->lock);
 
-       bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr), false);
+       bch2_mark_alloc_bucket(c, ca, ob->bucket, false);
        ob->valid = false;
-       ob->type = 0;
+       ob->data_type = 0;
 
        spin_unlock(&ob->lock);
        percpu_up_read(&c->mark_lock);
 
        spin_lock(&c->freelist_lock);
+       bch2_open_bucket_hash_remove(c, ob);
+
        ob->freelist = c->open_buckets_freelist;
        c->open_buckets_freelist = ob - c->open_buckets;
 
@@ -81,8 +106,7 @@ void bch2_open_bucket_write_error(struct bch_fs *c,
        unsigned i;
 
        open_bucket_for_each(c, obs, ob, i)
-               if (ob->ptr.dev == dev &&
-                   ob->ec)
+               if (ob->dev == dev && ob->ec)
                        bch2_ec_bucket_cancel(c, ob);
 }
 
@@ -95,7 +119,7 @@ static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
        ob = c->open_buckets + c->open_buckets_freelist;
        c->open_buckets_freelist = ob->freelist;
        atomic_set(&ob->pin, 1);
-       ob->type = 0;
+       ob->data_type = 0;
 
        c->open_buckets_nr_free--;
        return ob;
@@ -105,8 +129,8 @@ static void open_bucket_free_unused(struct bch_fs *c,
                                    struct write_point *wp,
                                    struct open_bucket *ob)
 {
-       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-       bool may_realloc = wp->type == BCH_DATA_user;
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+       bool may_realloc = wp->data_type == BCH_DATA_user;
 
        BUG_ON(ca->open_buckets_partial_nr >
               ARRAY_SIZE(ca->open_buckets_partial));
@@ -127,37 +151,18 @@ static void open_bucket_free_unused(struct bch_fs *c,
        }
 }
 
-static void verify_not_stale(struct bch_fs *c, const struct open_buckets *obs)
-{
-#ifdef CONFIG_BCACHEFS_DEBUG
-       struct open_bucket *ob;
-       unsigned i;
-
-       open_bucket_for_each(c, obs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-
-               BUG_ON(ptr_stale(ca, &ob->ptr));
-       }
-#endif
-}
-
 /* _only_ for allocating the journal on a new device: */
 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
 {
-       struct bucket_array *buckets;
-       ssize_t b;
+       while (ca->new_fs_bucket_idx < ca->mi.nbuckets) {
+               u64 b = ca->new_fs_bucket_idx++;
 
-       rcu_read_lock();
-       buckets = bucket_array(ca);
-
-       for (b = buckets->first_bucket; b < buckets->nbuckets; b++)
-               if (is_available_bucket(buckets->b[b].mark) &&
-                   !buckets->b[b].mark.owned_by_allocator)
-                       goto success;
-       b = -1;
-success:
-       rcu_read_unlock();
-       return b;
+               if (!is_superblock_bucket(ca, b) &&
+                   (!ca->buckets_nouse || !test_bit(b, ca->buckets_nouse)))
+                       return b;
+       }
+
+       return -1;
 }
 
 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
@@ -251,15 +256,14 @@ out:
        ob->valid       = true;
        ob->sectors_free = ca->mi.bucket_size;
        ob->alloc_reserve = reserve;
-       ob->ptr         = (struct bch_extent_ptr) {
-               .type   = 1 << BCH_EXTENT_ENTRY_ptr,
-               .gen    = bucket(ca, b)->mark.gen,
-               .offset = bucket_to_sector(ca, b),
-               .dev    = ca->dev_idx,
-       };
-
+       ob->dev         = ca->dev_idx;
+       ob->gen         = *bucket_gen(ca, b);
+       ob->bucket      = b;
        spin_unlock(&ob->lock);
 
+       ca->nr_open_buckets++;
+       bch2_open_bucket_hash_add(c, ob);
+
        if (c->blocked_allocate_open_bucket) {
                bch2_time_stats_update(
                        &c->times[BCH_TIME_blocked_allocate_open_bucket],
@@ -274,7 +278,6 @@ out:
                c->blocked_allocate = 0;
        }
 
-       ca->nr_open_buckets++;
        spin_unlock(&c->freelist_lock);
 
        bch2_wake_allocator(ca);
@@ -338,9 +341,9 @@ static void add_new_bucket(struct bch_fs *c,
                           struct open_bucket *ob)
 {
        unsigned durability =
-               bch_dev_bkey_exists(c, ob->ptr.dev)->mi.durability;
+               bch_dev_bkey_exists(c, ob->dev)->mi.durability;
 
-       __clear_bit(ob->ptr.dev, devs_may_alloc->d);
+       __clear_bit(ob->dev, devs_may_alloc->d);
        *nr_effective   += (flags & BUCKET_ALLOC_USE_DURABILITY)
                ? durability : 1;
        *have_cache     |= !durability;
@@ -348,8 +351,7 @@ static void add_new_bucket(struct bch_fs *c,
        ob_push(c, ptrs, ob);
 }
 
-enum bucket_alloc_ret
-bch2_bucket_alloc_set(struct bch_fs *c,
+int bch2_bucket_alloc_set(struct bch_fs *c,
                      struct open_buckets *ptrs,
                      struct dev_stripe_state *stripe,
                      struct bch_devs_mask *devs_may_alloc,
@@ -363,7 +365,7 @@ bch2_bucket_alloc_set(struct bch_fs *c,
        struct dev_alloc_list devs_sorted =
                bch2_dev_alloc_list(c, stripe, devs_may_alloc);
        struct bch_dev *ca;
-       enum bucket_alloc_ret ret = INSUFFICIENT_DEVICES;
+       int ret = -INSUFFICIENT_DEVICES;
        unsigned i;
 
        BUG_ON(*nr_effective >= nr_replicas);
@@ -381,7 +383,7 @@ bch2_bucket_alloc_set(struct bch_fs *c,
                ob = bch2_bucket_alloc(c, ca, reserve,
                                flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
                if (IS_ERR(ob)) {
-                       ret = -PTR_ERR(ob);
+                       ret = PTR_ERR(ob);
 
                        if (cl)
                                return ret;
@@ -394,7 +396,7 @@ bch2_bucket_alloc_set(struct bch_fs *c,
                bch2_dev_stripe_increment(ca, stripe);
 
                if (*nr_effective >= nr_replicas)
-                       return ALLOC_SUCCESS;
+                       return 0;
        }
 
        return ret;
@@ -408,8 +410,7 @@ bch2_bucket_alloc_set(struct bch_fs *c,
  * it's to a device we don't want:
  */
 
-static enum bucket_alloc_ret
-bucket_alloc_from_stripe(struct bch_fs *c,
+static int bucket_alloc_from_stripe(struct bch_fs *c,
                         struct open_buckets *ptrs,
                         struct write_point *wp,
                         struct bch_devs_mask *devs_may_alloc,
@@ -452,13 +453,13 @@ bucket_alloc_from_stripe(struct bch_fs *c,
                                continue;
 
                        ob = c->open_buckets + h->s->blocks[ec_idx];
-                       if (ob->ptr.dev == devs_sorted.devs[i] &&
+                       if (ob->dev == devs_sorted.devs[i] &&
                            !test_and_set_bit(ec_idx, h->s->blocks_allocated))
                                goto got_bucket;
                }
        goto out_put_head;
 got_bucket:
-       ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+       ca = bch_dev_bkey_exists(c, ob->dev);
 
        ob->ec_idx      = ec_idx;
        ob->ec          = h->s;
@@ -488,12 +489,12 @@ static void get_buckets_from_writepoint(struct bch_fs *c,
        unsigned i;
 
        open_bucket_for_each(c, &wp->ptrs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
 
                if (*nr_effective < nr_replicas &&
-                   test_bit(ob->ptr.dev, devs_may_alloc->d) &&
+                   test_bit(ob->dev, devs_may_alloc->d) &&
                    (ca->mi.durability ||
-                    (wp->type == BCH_DATA_user && !*have_cache)) &&
+                    (wp->data_type == BCH_DATA_user && !*have_cache)) &&
                    (ob->ec || !need_ec)) {
                        add_new_bucket(c, ptrs, devs_may_alloc,
                                       nr_effective, have_cache,
@@ -505,8 +506,7 @@ static void get_buckets_from_writepoint(struct bch_fs *c,
        wp->ptrs = ptrs_skip;
 }
 
-static enum bucket_alloc_ret
-open_bucket_add_buckets(struct bch_fs *c,
+static int open_bucket_add_buckets(struct bch_fs *c,
                        struct open_buckets *ptrs,
                        struct write_point *wp,
                        struct bch_devs_list *devs_have,
@@ -522,11 +522,11 @@ open_bucket_add_buckets(struct bch_fs *c,
        struct bch_devs_mask devs;
        struct open_bucket *ob;
        struct closure *cl = NULL;
-       enum bucket_alloc_ret ret;
+       int ret;
        unsigned i;
 
        rcu_read_lock();
-       devs = target_rw_devs(c, wp->type, target);
+       devs = target_rw_devs(c, wp->data_type, target);
        rcu_read_unlock();
 
        /* Don't allocate from devices we already have pointers to: */
@@ -534,7 +534,7 @@ open_bucket_add_buckets(struct bch_fs *c,
                __clear_bit(devs_have->devs[i], devs.d);
 
        open_bucket_for_each(c, ptrs, ob, i)
-               __clear_bit(ob->ptr.dev, devs.d);
+               __clear_bit(ob->dev, devs.d);
 
        if (erasure_code) {
                if (!ec_open_bucket(c, ptrs)) {
@@ -550,8 +550,8 @@ open_bucket_add_buckets(struct bch_fs *c,
                                                 target, erasure_code,
                                                 nr_replicas, nr_effective,
                                                 have_cache, flags, _cl);
-                       if (ret == FREELIST_EMPTY ||
-                           ret == OPEN_BUCKETS_EMPTY)
+                       if (ret == -FREELIST_EMPTY ||
+                           ret == -OPEN_BUCKETS_EMPTY)
                                return ret;
                        if (*nr_effective >= nr_replicas)
                                return 0;
@@ -575,7 +575,7 @@ retry_blocking:
        ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
                                nr_replicas, nr_effective, have_cache,
                                reserve, flags, cl);
-       if (ret && ret != INSUFFICIENT_DEVICES && !cl && _cl) {
+       if (ret && ret != -INSUFFICIENT_DEVICES && !cl && _cl) {
                cl = _cl;
                goto retry_blocking;
        }
@@ -594,7 +594,7 @@ void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
        unsigned i, j;
 
        open_bucket_for_each(c, obs, ob, i) {
-               bool drop = !ca || ob->ptr.dev == ca->dev_idx;
+               bool drop = !ca || ob->dev == ca->dev_idx;
 
                if (!drop && ob->ec) {
                        mutex_lock(&ob->ec->lock);
@@ -603,7 +603,7 @@ void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
                                        continue;
 
                                ob2 = c->open_buckets + ob->ec->blocks[j];
-                               drop |= ob2->ptr.dev == ca->dev_idx;
+                               drop |= ob2->dev == ca->dev_idx;
                        }
                        mutex_unlock(&ob->ec->lock);
                }
@@ -772,7 +772,7 @@ struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
        unsigned nr_effective, write_points_nr;
        unsigned ob_flags = 0;
        bool have_cache;
-       enum bucket_alloc_ret ret;
+       int ret;
        int i;
 
        if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
@@ -787,11 +787,11 @@ retry:
 
        wp = writepoint_find(c, write_point.v);
 
-       if (wp->type == BCH_DATA_user)
+       if (wp->data_type == BCH_DATA_user)
                ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
 
        /* metadata may not allocate on cache devices: */
-       if (wp->type != BCH_DATA_user)
+       if (wp->data_type != BCH_DATA_user)
                have_cache = true;
 
        if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
@@ -821,7 +821,7 @@ alloc_done:
        if (erasure_code && !ec_open_bucket(c, &ptrs))
                pr_debug("failed to get ec bucket: ret %u", ret);
 
-       if (ret == INSUFFICIENT_DEVICES &&
+       if (ret == -INSUFFICIENT_DEVICES &&
            nr_effective >= nr_replicas_required)
                ret = 0;
 
@@ -841,8 +841,6 @@ alloc_done:
 
        BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
 
-       verify_not_stale(c, &wp->ptrs);
-
        return wp;
 err:
        open_bucket_for_each(c, &wp->ptrs, ob, i)
@@ -854,27 +852,42 @@ err:
 
        mutex_unlock(&wp->lock);
 
-       if (ret == FREELIST_EMPTY &&
+       if (ret == -FREELIST_EMPTY &&
            try_decrease_writepoints(c, write_points_nr))
                goto retry;
 
        switch (ret) {
-       case OPEN_BUCKETS_EMPTY:
-       case FREELIST_EMPTY:
+       case -OPEN_BUCKETS_EMPTY:
+       case -FREELIST_EMPTY:
                return cl ? ERR_PTR(-EAGAIN) : ERR_PTR(-ENOSPC);
-       case INSUFFICIENT_DEVICES:
+       case -INSUFFICIENT_DEVICES:
                return ERR_PTR(-EROFS);
        default:
                BUG();
        }
 }
 
+struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
+{
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+
+       return (struct bch_extent_ptr) {
+               .type   = 1 << BCH_EXTENT_ENTRY_ptr,
+               .gen    = ob->gen,
+               .dev    = ob->dev,
+               .offset = bucket_to_sector(ca, ob->bucket) +
+                       ca->mi.bucket_size -
+                       ob->sectors_free,
+       };
+}
+
 /*
  * Append pointers to the space we just allocated to @k, and mark @sectors space
  * as allocated out of @ob
  */
 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
-                                   struct bkey_i *k, unsigned sectors)
+                                   struct bkey_i *k, unsigned sectors,
+                                   bool cached)
 
 {
        struct open_bucket *ob;
@@ -884,14 +897,14 @@ void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
        wp->sectors_free -= sectors;
 
        open_bucket_for_each(c, &wp->ptrs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-               struct bch_extent_ptr tmp = ob->ptr;
+               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+               struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
 
-               tmp.cached = !ca->mi.durability &&
-                       wp->type == BCH_DATA_user;
+               ptr.cached = cached ||
+                       (!ca->mi.durability &&
+                        wp->data_type == BCH_DATA_user);
 
-               tmp.offset += ca->mi.bucket_size - ob->sectors_free;
-               bch2_bkey_append_ptr(k, tmp);
+               bch2_bkey_append_ptr(k, ptr);
 
                BUG_ON(sectors > ob->sectors_free);
                ob->sectors_free -= sectors;
@@ -921,7 +934,7 @@ static inline void writepoint_init(struct write_point *wp,
                                   enum bch_data_type type)
 {
        mutex_init(&wp->lock);
-       wp->type = type;
+       wp->data_type = type;
 }
 
 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
@@ -958,3 +971,22 @@ void bch2_fs_allocator_foreground_init(struct bch_fs *c)
                                   writepoint_hash(c, wp->write_point));
        }
 }
+
+void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
+{
+       struct open_bucket *ob;
+
+       for (ob = c->open_buckets;
+            ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
+            ob++) {
+               spin_lock(&ob->lock);
+               if (ob->valid && !ob->on_partial_list) {
+                       pr_buf(out, "%zu ref %u type %s\n",
+                              ob - c->open_buckets,
+                              atomic_read(&ob->pin),
+                              bch2_data_types[ob->data_type]);
+               }
+               spin_unlock(&ob->lock);
+       }
+
+}