buckets.h 7.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Code for manipulating bucket marks for garbage collection.
 *
 * Copyright 2014 Datera, Inc.
 */

#ifndef _BUCKETS_H
#define _BUCKETS_H

#include "buckets_types.h"
#include "super.h"

#define for_each_bucket(_b, _buckets)				\
	for (_b = (_buckets)->b + (_buckets)->first_bucket;	\
	     _b < (_buckets)->b + (_buckets)->nbuckets; _b++)

#define bucket_cmpxchg(g, new, expr)				\
({								\
	u64 _v = atomic64_read(&(g)->_mark.v);			\
	struct bucket_mark _old;				\
								\
	do {							\
		(new).v.counter = _old.v.counter = _v;		\
		expr;						\
	} while ((_v = atomic64_cmpxchg(&(g)->_mark.v,		\
			       _old.v.counter,			\
			       (new).v.counter)) != _old.v.counter);\
	_old;							\
})

32 33
static inline struct bucket_array *__bucket_array(struct bch_dev *ca,
						  bool gc)
34
{
35
	return rcu_dereference_check(ca->buckets[gc],
36
				     !ca->fs ||
37
				     percpu_rwsem_is_held(&ca->fs->mark_lock) ||
38 39 40 41
				     lockdep_is_held(&ca->fs->gc_lock) ||
				     lockdep_is_held(&ca->bucket_lock));
}

42 43 44 45 46 47
static inline struct bucket_array *bucket_array(struct bch_dev *ca)
{
	return __bucket_array(ca, false);
}

static inline struct bucket *__bucket(struct bch_dev *ca, size_t b, bool gc)
48
{
49
	struct bucket_array *buckets = __bucket_array(ca, gc);
50 51 52 53 54

	BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
	return buckets->b + b;
}

55 56 57 58 59
static inline struct bucket *bucket(struct bch_dev *ca, size_t b)
{
	return __bucket(ca, b, false);
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static inline void bucket_io_clock_reset(struct bch_fs *c, struct bch_dev *ca,
					 size_t b, int rw)
{
	bucket(ca, b)->io_time[rw] = c->bucket_clock[rw].hand;
}

static inline u16 bucket_last_io(struct bch_fs *c, struct bucket *g, int rw)
{
	return c->bucket_clock[rw].hand - g->io_time[rw];
}

/*
 * bucket_gc_gen() returns the difference between the bucket's current gen and
 * the oldest gen of any pointer into that bucket in the btree.
 */

static inline u8 bucket_gc_gen(struct bch_dev *ca, size_t b)
{
78 79 80
	struct bucket *g = bucket(ca, b);

	return g->mark.gen - g->oldest_gen;
81 82 83 84 85 86 87 88 89
}

static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
				   const struct bch_extent_ptr *ptr)
{
	return sector_to_bucket(ca, ptr->offset);
}

static inline struct bucket *PTR_BUCKET(struct bch_dev *ca,
90 91
					const struct bch_extent_ptr *ptr,
					bool gc)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
	return bucket(ca, PTR_BUCKET_NR(ca, ptr));
}

static inline struct bucket_mark ptr_bucket_mark(struct bch_dev *ca,
						 const struct bch_extent_ptr *ptr)
{
	struct bucket_mark m;

	rcu_read_lock();
	m = READ_ONCE(bucket(ca, PTR_BUCKET_NR(ca, ptr))->mark);
	rcu_read_unlock();

	return m;
}

static inline int gen_cmp(u8 a, u8 b)
{
	return (s8) (a - b);
}

static inline int gen_after(u8 a, u8 b)
{
	int r = gen_cmp(a, b);

	return r > 0 ? r : 0;
}

/**
 * ptr_stale() - check if a pointer points into a bucket that has been
 * invalidated.
 */
static inline u8 ptr_stale(struct bch_dev *ca,
			   const struct bch_extent_ptr *ptr)
{
	return gen_after(ptr_bucket_mark(ca, ptr).gen, ptr->gen);
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143
static inline unsigned __ptr_disk_sectors(struct extent_ptr_decoded p,
					  unsigned live_size)
{
	return live_size && p.crc.compression_type
		? max(1U, DIV_ROUND_UP(live_size * p.crc.compressed_size,
				       p.crc.uncompressed_size))
		: live_size;
}

static inline unsigned ptr_disk_sectors(struct extent_ptr_decoded p)
{
	return __ptr_disk_sectors(p, p.crc.live_size);
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157
/* bucket gc marks */

static inline unsigned bucket_sectors_used(struct bucket_mark mark)
{
	return mark.dirty_sectors + mark.cached_sectors;
}

static inline bool bucket_unused(struct bucket_mark mark)
{
	return !mark.owned_by_allocator &&
		!mark.data_type &&
		!bucket_sectors_used(mark);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171
static inline bool is_available_bucket(struct bucket_mark mark)
{
	return (!mark.owned_by_allocator &&
		!mark.dirty_sectors &&
		!mark.stripe);
}

static inline bool bucket_needs_journal_commit(struct bucket_mark m,
					       u16 last_seq_ondisk)
{
	return m.journal_seq_valid &&
		((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
}

172 173 174 175
/* Device usage: */

struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *, struct bch_dev *);

176 177
void bch2_dev_usage_from_buckets(struct bch_fs *, struct bch_dev *);

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
static inline u64 __dev_buckets_available(struct bch_dev *ca,
					  struct bch_dev_usage stats)
{
	u64 total = ca->mi.nbuckets - ca->mi.first_bucket;

	if (WARN_ONCE(stats.buckets_unavailable > total,
		      "buckets_unavailable overflow (%llu > %llu)\n",
		      stats.buckets_unavailable, total))
		return 0;

	return total - stats.buckets_unavailable;
}

/*
 * Number of reclaimable buckets - only for use by the allocator thread:
 */
static inline u64 dev_buckets_available(struct bch_fs *c, struct bch_dev *ca)
{
	return __dev_buckets_available(ca, bch2_dev_usage_read(c, ca));
}

static inline u64 __dev_buckets_free(struct bch_dev *ca,
				     struct bch_dev_usage stats)
{
	return __dev_buckets_available(ca, stats) +
		fifo_used(&ca->free[RESERVE_NONE]) +
		fifo_used(&ca->free_inc);
}

static inline u64 dev_buckets_free(struct bch_fs *c, struct bch_dev *ca)
{
	return __dev_buckets_free(ca, bch2_dev_usage_read(c, ca));
}

/* Filesystem usage: */

214 215 216 217 218 219 220 221 222 223 224 225
static inline struct bch_fs_usage *bch2_fs_usage_get_scratch(struct bch_fs *c)
{
	struct bch_fs_usage *ret;

	ret = this_cpu_ptr(c->usage_scratch);

	memset(ret, 0, sizeof(*ret) + c->replicas.nr * sizeof(u64));

	return ret;
}

struct bch_fs_usage *bch2_fs_usage_read(struct bch_fs *);
226 227 228

u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage);

229 230
struct bch_fs_usage_short
bch2_fs_usage_read_short(struct bch_fs *);
231

232
static inline u64 bch2_fs_sectors_free(struct bch_fs *c)
233
{
234
	struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
235

236
	return usage.capacity - usage.used;
237 238
}

239 240
/* key/bucket marking: */

241
void bch2_bucket_seq_cleanup(struct bch_fs *);
242
void bch2_fs_usage_initialize(struct bch_fs *);
243

244
void bch2_invalidate_bucket(struct bch_fs *, struct bch_dev *,
245 246 247 248 249 250 251
			    size_t, struct bucket_mark *);
void bch2_mark_alloc_bucket(struct bch_fs *, struct bch_dev *,
			    size_t, bool, struct gc_pos, unsigned);
void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
			       size_t, enum bch_data_type, unsigned,
			       struct gc_pos, unsigned);

252 253
#define BCH_BUCKET_MARK_GC			(1 << 0)
#define BCH_BUCKET_MARK_NOATOMIC		(1 << 1)
254

255
int bch2_mark_key_locked(struct bch_fs *, struct bkey_s_c,
256 257
		  bool, s64, struct gc_pos,
		  struct bch_fs_usage *, u64, unsigned);
258
int bch2_mark_key(struct bch_fs *, struct bkey_s_c,
259 260
		  bool, s64, struct gc_pos,
		  struct bch_fs_usage *, u64, unsigned);
261
void bch2_mark_update(struct btree_insert *, struct btree_insert_entry *);
262
int bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
263
			struct disk_reservation *);
264 265

/* disk reservations: */
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

void __bch2_disk_reservation_put(struct bch_fs *, struct disk_reservation *);

static inline void bch2_disk_reservation_put(struct bch_fs *c,
					     struct disk_reservation *res)
{
	if (res->sectors)
		__bch2_disk_reservation_put(c, res);
}

#define BCH_DISK_RESERVATION_NOFAIL		(1 << 0)

int bch2_disk_reservation_add(struct bch_fs *,
			     struct disk_reservation *,
			     unsigned, int);

static inline struct disk_reservation
bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
{
	return (struct disk_reservation) {
		.sectors	= 0,
#if 0
		/* not used yet: */
		.gen		= c->capacity_gen,
#endif
		.nr_replicas	= nr_replicas,
	};
}

static inline int bch2_disk_reservation_get(struct bch_fs *c,
					    struct disk_reservation *res,
					    unsigned sectors,
					    unsigned nr_replicas,
					    int flags)
{
	*res = bch2_disk_reservation_init(c, nr_replicas);

	return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
}

int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
void bch2_dev_buckets_free(struct bch_dev *);
int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);

#endif /* _BUCKETS_H */