2018-10-06 00:46:55 -04:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_ALLOC_FOREGROUND_H
|
|
|
|
#define _BCACHEFS_ALLOC_FOREGROUND_H
|
2017-03-16 22:18:50 -08:00
|
|
|
|
|
|
|
#include "bcachefs.h"
|
|
|
|
#include "alloc_types.h"
|
2022-11-24 18:03:55 -05:00
|
|
|
#include "extents.h"
|
2023-08-05 15:40:21 -04:00
|
|
|
#include "sb-members.h"
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2018-10-12 14:57:57 -04:00
|
|
|
#include <linux/hash.h>
|
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
struct bkey;
|
|
|
|
struct bch_dev;
|
|
|
|
struct bch_fs;
|
|
|
|
struct bch_devs_List;
|
|
|
|
|
2023-06-24 19:30:10 -04:00
|
|
|
extern const char * const bch2_watermarks[];
|
2022-03-13 19:27:55 -04:00
|
|
|
|
2022-01-09 20:48:31 -05:00
|
|
|
void bch2_reset_alloc_cursors(struct bch_fs *);
|
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
struct dev_alloc_list {
|
|
|
|
unsigned nr;
|
|
|
|
u8 devs[BCH_SB_MEMBERS_MAX];
|
|
|
|
};
|
|
|
|
|
2018-11-01 15:13:19 -04:00
|
|
|
struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
|
|
|
|
struct dev_stripe_state *,
|
|
|
|
struct bch_devs_mask *);
|
2020-07-22 13:27:00 -04:00
|
|
|
void bch2_dev_stripe_increment(struct bch_dev *, struct dev_stripe_state *);
|
2017-03-16 22:18:50 -08:00
|
|
|
|
|
|
|
long bch2_bucket_alloc_new_fs(struct bch_dev *);
|
|
|
|
|
2024-04-30 20:38:05 -04:00
|
|
|
static inline struct bch_dev *ob_dev(struct bch_fs *c, struct open_bucket *ob)
|
|
|
|
{
|
|
|
|
return bch2_dev_have_ref(c, ob->dev);
|
|
|
|
}
|
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
|
2024-04-20 16:10:40 -04:00
|
|
|
enum bch_watermark, enum bch_data_type,
|
|
|
|
struct closure *);
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
|
|
|
|
struct open_bucket *ob)
|
|
|
|
{
|
|
|
|
BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
obs->v[obs->nr++] = ob - c->open_buckets;
|
|
|
|
}
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
#define open_bucket_for_each(_c, _obs, _ob, _i) \
|
|
|
|
for ((_i) = 0; \
|
|
|
|
(_i) < (_obs)->nr && \
|
|
|
|
((_ob) = (_c)->open_buckets + (_obs)->v[_i], true); \
|
|
|
|
(_i)++)
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2018-11-01 15:13:19 -04:00
|
|
|
static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
|
|
|
|
struct open_buckets *obs)
|
|
|
|
{
|
|
|
|
struct open_bucket *ob;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
open_bucket_for_each(c, obs, ob, i)
|
|
|
|
if (ob->ec)
|
|
|
|
return ob;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_open_bucket_write_error(struct bch_fs *,
|
|
|
|
struct open_buckets *, unsigned);
|
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
|
|
|
|
|
|
|
|
static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
|
|
|
|
{
|
|
|
|
if (atomic_dec_and_test(&ob->pin))
|
|
|
|
__bch2_open_bucket_put(c, ob);
|
|
|
|
}
|
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
static inline void bch2_open_buckets_put(struct bch_fs *c,
|
|
|
|
struct open_buckets *ptrs)
|
2017-03-16 22:18:50 -08:00
|
|
|
{
|
2018-10-06 04:12:42 -04:00
|
|
|
struct open_bucket *ob;
|
2017-03-16 22:18:50 -08:00
|
|
|
unsigned i;
|
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
open_bucket_for_each(c, ptrs, ob, i)
|
|
|
|
bch2_open_bucket_put(c, ob);
|
|
|
|
ptrs->nr = 0;
|
2017-03-16 22:18:50 -08:00
|
|
|
}
|
|
|
|
|
2022-11-24 18:03:55 -05:00
|
|
|
static inline void bch2_alloc_sectors_done_inlined(struct bch_fs *c, struct write_point *wp)
|
|
|
|
{
|
|
|
|
struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
|
|
|
|
struct open_bucket *ob;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
open_bucket_for_each(c, &wp->ptrs, ob, i)
|
|
|
|
ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
|
|
|
|
wp->ptrs = keep;
|
|
|
|
|
|
|
|
mutex_unlock(&wp->lock);
|
|
|
|
|
|
|
|
bch2_open_buckets_put(c, &ptrs);
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
static inline void bch2_open_bucket_get(struct bch_fs *c,
|
|
|
|
struct write_point *wp,
|
2018-10-06 04:12:42 -04:00
|
|
|
struct open_buckets *ptrs)
|
2017-03-16 22:18:50 -08:00
|
|
|
{
|
|
|
|
struct open_bucket *ob;
|
|
|
|
unsigned i;
|
|
|
|
|
2018-10-06 04:12:42 -04:00
|
|
|
open_bucket_for_each(c, &wp->ptrs, ob, i) {
|
2021-12-25 21:21:46 -05:00
|
|
|
ob->data_type = wp->data_type;
|
2017-03-16 22:18:50 -08:00
|
|
|
atomic_inc(&ob->pin);
|
2018-10-06 04:12:42 -04:00
|
|
|
ob_push(c, ptrs, ob);
|
2017-03-16 22:18:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 21:43:29 -05:00
|
|
|
static inline open_bucket_idx_t *open_bucket_hashslot(struct bch_fs *c,
|
|
|
|
unsigned dev, u64 bucket)
|
|
|
|
{
|
|
|
|
return c->open_buckets_hash +
|
|
|
|
(jhash_3words(dev, bucket, bucket >> 32, 0) &
|
|
|
|
(OPEN_BUCKETS_COUNT - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bch2_bucket_is_open(struct bch_fs *c, unsigned dev, u64 bucket)
|
|
|
|
{
|
|
|
|
open_bucket_idx_t slot = *open_bucket_hashslot(c, dev, bucket);
|
|
|
|
|
|
|
|
while (slot) {
|
|
|
|
struct open_bucket *ob = &c->open_buckets[slot];
|
|
|
|
|
|
|
|
if (ob->dev == dev && ob->bucket == bucket)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
slot = ob->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-11 17:13:09 -05:00
|
|
|
static inline bool bch2_bucket_is_open_safe(struct bch_fs *c, unsigned dev, u64 bucket)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (bch2_bucket_is_open(c, dev, bucket))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
spin_lock(&c->freelist_lock);
|
|
|
|
ret = bch2_bucket_is_open(c, dev, bucket);
|
|
|
|
spin_unlock(&c->freelist_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-09-01 17:06:28 -04:00
|
|
|
enum bch_write_flags;
|
2023-02-17 20:50:55 -05:00
|
|
|
int bch2_bucket_alloc_set_trans(struct btree_trans *, struct open_buckets *,
|
2020-07-11 18:52:14 -04:00
|
|
|
struct dev_stripe_state *, struct bch_devs_mask *,
|
2024-09-01 17:06:28 -04:00
|
|
|
unsigned, unsigned *, bool *, enum bch_write_flags,
|
2023-06-24 19:30:10 -04:00
|
|
|
enum bch_data_type, enum bch_watermark,
|
2023-02-25 02:22:49 -05:00
|
|
|
struct closure *);
|
2020-07-06 22:33:54 -04:00
|
|
|
|
2022-01-09 20:48:31 -05:00
|
|
|
int bch2_alloc_sectors_start_trans(struct btree_trans *,
|
|
|
|
unsigned, unsigned,
|
|
|
|
struct write_point_specifier,
|
|
|
|
struct bch_devs_list *,
|
|
|
|
unsigned, unsigned,
|
2023-06-24 19:30:10 -04:00
|
|
|
enum bch_watermark,
|
2024-09-01 17:06:28 -04:00
|
|
|
enum bch_write_flags,
|
2022-01-09 20:48:31 -05:00
|
|
|
struct closure *,
|
|
|
|
struct write_point **);
|
2017-03-16 22:18:50 -08:00
|
|
|
|
2021-12-25 21:21:46 -05:00
|
|
|
struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *, struct open_bucket *);
|
2022-11-24 18:03:55 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Append pointers to the space we just allocated to @k, and mark @sectors space
|
|
|
|
* as allocated out of @ob
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
bch2_alloc_sectors_append_ptrs_inlined(struct bch_fs *c, struct write_point *wp,
|
|
|
|
struct bkey_i *k, unsigned sectors,
|
|
|
|
bool cached)
|
|
|
|
{
|
|
|
|
struct open_bucket *ob;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
BUG_ON(sectors > wp->sectors_free);
|
|
|
|
wp->sectors_free -= sectors;
|
|
|
|
wp->sectors_allocated += sectors;
|
|
|
|
|
|
|
|
open_bucket_for_each(c, &wp->ptrs, ob, i) {
|
2024-04-30 20:38:05 -04:00
|
|
|
struct bch_dev *ca = ob_dev(c, ob);
|
2022-11-24 18:03:55 -05:00
|
|
|
struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
|
|
|
|
|
|
|
|
ptr.cached = cached ||
|
|
|
|
(!ca->mi.durability &&
|
|
|
|
wp->data_type == BCH_DATA_user);
|
|
|
|
|
|
|
|
bch2_bkey_append_ptr(k, ptr);
|
|
|
|
|
|
|
|
BUG_ON(sectors > ob->sectors_free);
|
|
|
|
ob->sectors_free -= sectors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
|
2021-12-25 21:14:49 -05:00
|
|
|
struct bkey_i *, unsigned, bool);
|
2017-03-16 22:18:50 -08:00
|
|
|
void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
|
|
|
|
|
2023-03-13 22:01:47 -04:00
|
|
|
void bch2_open_buckets_stop(struct bch_fs *c, struct bch_dev *, bool);
|
2018-10-06 00:46:55 -04:00
|
|
|
|
2017-03-16 22:18:50 -08:00
|
|
|
static inline struct write_point_specifier writepoint_hashed(unsigned long v)
|
|
|
|
{
|
|
|
|
return (struct write_point_specifier) { .v = v | 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
|
|
|
|
{
|
|
|
|
return (struct write_point_specifier) { .v = (unsigned long) wp };
|
|
|
|
}
|
|
|
|
|
2018-11-04 21:55:35 -05:00
|
|
|
void bch2_fs_allocator_foreground_init(struct bch_fs *);
|
|
|
|
|
2024-07-11 16:30:41 -04:00
|
|
|
void bch2_open_bucket_to_text(struct printbuf *, struct bch_fs *, struct open_bucket *);
|
2024-08-06 21:02:34 -04:00
|
|
|
void bch2_open_buckets_to_text(struct printbuf *, struct bch_fs *, struct bch_dev *);
|
2023-02-28 23:08:48 -05:00
|
|
|
void bch2_open_buckets_partial_to_text(struct printbuf *, struct bch_fs *);
|
2021-12-25 21:21:46 -05:00
|
|
|
|
2022-10-31 16:13:05 -04:00
|
|
|
void bch2_write_points_to_text(struct printbuf *, struct bch_fs *);
|
|
|
|
|
2024-05-03 14:49:23 -04:00
|
|
|
void bch2_fs_alloc_debug_to_text(struct printbuf *, struct bch_fs *);
|
|
|
|
void bch2_dev_alloc_debug_to_text(struct printbuf *, struct bch_dev *);
|
|
|
|
|
2024-08-07 13:58:57 -04:00
|
|
|
void __bch2_wait_on_allocator(struct bch_fs *, struct closure *);
|
|
|
|
static inline void bch2_wait_on_allocator(struct bch_fs *c, struct closure *cl)
|
|
|
|
{
|
|
|
|
if (cl->closure_get_happened)
|
|
|
|
__bch2_wait_on_allocator(c, cl);
|
|
|
|
}
|
2024-05-03 14:49:23 -04:00
|
|
|
|
2018-10-06 00:46:55 -04:00
|
|
|
#endif /* _BCACHEFS_ALLOC_FOREGROUND_H */
|