summaryrefslogtreecommitdiff
path: root/libbcache/buckets.h
blob: 6d70103efb42a191b703972c7765dc170b2dd0e1 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * 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, ca)					\
	for (b = (ca)->buckets + (ca)->mi.first_bucket;		\
	     b < (ca)->buckets + (ca)->mi.nbuckets; b++)

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

/*
 * 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 cache *ca, struct bucket *g)
{
	unsigned long r = g - ca->buckets;
	return g->mark.gen - ca->oldest_gens[r];
}

static inline struct cache *PTR_CACHE(const struct cache_set *c,
				      const struct bch_extent_ptr *ptr)
{
	EBUG_ON(ptr->dev > rcu_dereference(c->members)->nr_devices);

	return rcu_dereference(c->cache[ptr->dev]);
}

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

/*
 * Returns 0 if no pointers or device offline - only for tracepoints!
 */
static inline size_t PTR_BUCKET_NR_TRACE(const struct cache_set *c,
					 const struct bkey_i *k,
					 unsigned ptr)
{
	size_t bucket = 0;
#if 0
	if (bkey_extent_is_data(&k->k)) {
		const struct bch_extent_ptr *ptr;
		const struct cache *ca;

		rcu_read_lock();
		extent_for_each_online_device(c, bkey_i_to_s_c_extent(k), ptr, ca) {
			bucket = PTR_BUCKET_NR(ca, ptr);
			break;
		}
		rcu_read_unlock();
	}
#endif
	return bucket;
}

static inline struct bucket *PTR_BUCKET(const struct cache *ca,
					const struct bch_extent_ptr *ptr)
{
	return ca->buckets + PTR_BUCKET_NR(ca, ptr);
}

static inline u8 __gen_after(u8 a, u8 b)
{
	u8 r = a - b;

	return r > 128U ? 0 : r;
}

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

	BUG_ON(r > 128U);

	return r;
}

/**
 * ptr_stale() - check if a pointer points into a bucket that has been
 * invalidated.
 *
 * Warning: PTR_CACHE(c, k, ptr) must equal ca.
 */
static inline u8 ptr_stale(const struct cache *ca,
			   const struct bch_extent_ptr *ptr)
{
	return gen_after(PTR_BUCKET(ca, ptr)->mark.gen, ptr->gen);
}

/* bucket heaps */

static inline bool bucket_min_cmp(struct bucket_heap_entry l,
				  struct bucket_heap_entry r)
{
	return l.val < r.val;
}

static inline bool bucket_max_cmp(struct bucket_heap_entry l,
				  struct bucket_heap_entry r)
{
	return l.val > r.val;
}

static inline void bucket_heap_push(struct cache *ca, struct bucket *g,
				    unsigned long val)
{
	struct bucket_heap_entry new = { g, val };

	if (!heap_full(&ca->heap))
		heap_add(&ca->heap, new, bucket_min_cmp);
	else if (bucket_min_cmp(new, heap_peek(&ca->heap))) {
		ca->heap.data[0] = new;
		heap_sift(&ca->heap, 0, bucket_min_cmp);
	}
}

/* bucket gc marks */

/* The dirty and cached sector counts saturate. If this occurs,
 * reference counting alone will not free the bucket, and a btree
 * GC must be performed. */
#define GC_MAX_SECTORS_USED ((1U << 15) - 1)

static inline bool bucket_unused(struct bucket *g)
{
	return !g->mark.counter;
}

static inline unsigned bucket_sectors_used(struct bucket *g)
{
	return g->mark.dirty_sectors + g->mark.cached_sectors;
}

/* Per device stats: */

struct bucket_stats_cache __bch_bucket_stats_read_cache(struct cache *);
struct bucket_stats_cache bch_bucket_stats_read_cache(struct cache *);

static inline u64 __buckets_available_cache(struct cache *ca,
					    struct bucket_stats_cache stats)
{
	return max_t(s64, 0,
		     ca->mi.nbuckets - ca->mi.first_bucket -
		     stats.buckets_dirty -
		     stats.buckets_alloc -
		     stats.buckets_meta);
}

/*
 * Number of reclaimable buckets - only for use by the allocator thread:
 */
static inline u64 buckets_available_cache(struct cache *ca)
{
	return __buckets_available_cache(ca, bch_bucket_stats_read_cache(ca));
}

static inline u64 __buckets_free_cache(struct cache *ca,
				       struct bucket_stats_cache stats)
{
	return __buckets_available_cache(ca, stats) +
		fifo_used(&ca->free[RESERVE_NONE]) +
		fifo_used(&ca->free_inc);
}

static inline u64 buckets_free_cache(struct cache *ca)
{
	return __buckets_free_cache(ca, bch_bucket_stats_read_cache(ca));
}

/* Cache set stats: */

struct bucket_stats_cache_set __bch_bucket_stats_read_cache_set(struct cache_set *);
struct bucket_stats_cache_set bch_bucket_stats_read_cache_set(struct cache_set *);
void bch_fs_stats_apply(struct cache_set *,
			struct bucket_stats_cache_set *,
			struct disk_reservation *,
			       struct gc_pos);

static inline u64 __bch_fs_sectors_used(struct cache_set *c)
{
	struct bucket_stats_cache_set stats = __bch_bucket_stats_read_cache_set(c);
	u64 reserved = stats.persistent_reserved +
		stats.online_reserved;

	return stats.s[S_COMPRESSED][S_META] +
		stats.s[S_COMPRESSED][S_DIRTY] +
		reserved +
		(reserved >> 7);
}

static inline u64 bch_fs_sectors_used(struct cache_set *c)
{
	return min(c->capacity, __bch_fs_sectors_used(c));
}

/* XXX: kill? */
static inline u64 sectors_available(struct cache_set *c)
{
	struct cache *ca;
	unsigned i;
	u64 ret = 0;

	rcu_read_lock();
	for_each_cache_rcu(ca, c, i)
		ret += buckets_available_cache(ca) << ca->bucket_bits;
	rcu_read_unlock();

	return ret;
}

static inline bool is_available_bucket(struct bucket_mark mark)
{
	return (!mark.owned_by_allocator &&
		mark.data_type == BUCKET_DATA &&
		!mark.dirty_sectors &&
		!mark.nouse);
}

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);
}

void bch_bucket_seq_cleanup(struct cache_set *);

void bch_invalidate_bucket(struct cache *, struct bucket *);
void bch_mark_free_bucket(struct cache *, struct bucket *);
void bch_mark_alloc_bucket(struct cache *, struct bucket *, bool);
void bch_mark_metadata_bucket(struct cache *, struct bucket *,
			      enum bucket_data_type, bool);

void __bch_gc_mark_key(struct cache_set *, struct bkey_s_c, s64, bool,
		       struct bucket_stats_cache_set *);
void bch_gc_mark_key(struct cache_set *, struct bkey_s_c, s64, bool);
void bch_mark_key(struct cache_set *, struct bkey_s_c, s64, bool,
		  struct gc_pos, struct bucket_stats_cache_set *, u64);

void bch_recalc_sectors_available(struct cache_set *);

void bch_disk_reservation_put(struct cache_set *,
			      struct disk_reservation *);

#define BCH_DISK_RESERVATION_NOFAIL		(1 << 0)
#define BCH_DISK_RESERVATION_METADATA		(1 << 1)
#define BCH_DISK_RESERVATION_GC_LOCK_HELD	(1 << 2)
#define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD	(1 << 3)

int bch_disk_reservation_add(struct cache_set *,
			     struct disk_reservation *,
			     unsigned, int);
int bch_disk_reservation_get(struct cache_set *,
			     struct disk_reservation *,
			     unsigned, int);

#endif /* _BUCKETS_H */