Linux Audio

Check our new training course

Loading...
v6.2
  1/* SPDX-License-Identifier: GPL-2.0 */
  2
  3#ifndef _BCACHE_UTIL_H
  4#define _BCACHE_UTIL_H
  5
  6#include <linux/blkdev.h>
  7#include <linux/errno.h>
  8#include <linux/kernel.h>
  9#include <linux/sched/clock.h>
 10#include <linux/llist.h>
 11#include <linux/ratelimit.h>
 12#include <linux/vmalloc.h>
 13#include <linux/workqueue.h>
 14#include <linux/crc64.h>
 15
 16#include "closure.h"
 17
 
 
 18struct closure;
 19
 20#ifdef CONFIG_BCACHE_DEBUG
 21
 22#define EBUG_ON(cond)			BUG_ON(cond)
 23#define atomic_dec_bug(v)	BUG_ON(atomic_dec_return(v) < 0)
 24#define atomic_inc_bug(v, i)	BUG_ON(atomic_inc_return(v) <= i)
 25
 26#else /* DEBUG */
 27
 28#define EBUG_ON(cond)		do { if (cond) do {} while (0); } while (0)
 29#define atomic_dec_bug(v)	atomic_dec(v)
 30#define atomic_inc_bug(v, i)	atomic_inc(v)
 31
 32#endif
 33
 34#define DECLARE_HEAP(type, name)					\
 35	struct {							\
 36		size_t size, used;					\
 37		type *data;						\
 38	} name
 39
 40#define init_heap(heap, _size, gfp)					\
 41({									\
 42	size_t _bytes;							\
 43	(heap)->used = 0;						\
 44	(heap)->size = (_size);						\
 45	_bytes = (heap)->size * sizeof(*(heap)->data);			\
 46	(heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);		\
 47	(heap)->data;							\
 48})
 49
 50#define free_heap(heap)							\
 51do {									\
 52	kvfree((heap)->data);						\
 53	(heap)->data = NULL;						\
 54} while (0)
 55
 56#define heap_swap(h, i, j)	swap((h)->data[i], (h)->data[j])
 57
 58#define heap_sift(h, i, cmp)						\
 59do {									\
 60	size_t _r, _j = i;						\
 61									\
 62	for (; _j * 2 + 1 < (h)->used; _j = _r) {			\
 63		_r = _j * 2 + 1;					\
 64		if (_r + 1 < (h)->used &&				\
 65		    cmp((h)->data[_r], (h)->data[_r + 1]))		\
 66			_r++;						\
 67									\
 68		if (cmp((h)->data[_r], (h)->data[_j]))			\
 69			break;						\
 70		heap_swap(h, _r, _j);					\
 71	}								\
 72} while (0)
 73
 74#define heap_sift_down(h, i, cmp)					\
 75do {									\
 76	while (i) {							\
 77		size_t p = (i - 1) / 2;					\
 78		if (cmp((h)->data[i], (h)->data[p]))			\
 79			break;						\
 80		heap_swap(h, i, p);					\
 81		i = p;							\
 82	}								\
 83} while (0)
 84
 85#define heap_add(h, d, cmp)						\
 86({									\
 87	bool _r = !heap_full(h);					\
 88	if (_r) {							\
 89		size_t _i = (h)->used++;				\
 90		(h)->data[_i] = d;					\
 91									\
 92		heap_sift_down(h, _i, cmp);				\
 93		heap_sift(h, _i, cmp);					\
 94	}								\
 95	_r;								\
 96})
 97
 98#define heap_pop(h, d, cmp)						\
 99({									\
100	bool _r = (h)->used;						\
101	if (_r) {							\
102		(d) = (h)->data[0];					\
103		(h)->used--;						\
104		heap_swap(h, 0, (h)->used);				\
105		heap_sift(h, 0, cmp);					\
106	}								\
107	_r;								\
108})
109
110#define heap_peek(h)	((h)->used ? (h)->data[0] : NULL)
111
112#define heap_full(h)	((h)->used == (h)->size)
113
 
 
114#define DECLARE_FIFO(type, name)					\
115	struct {							\
116		size_t front, back, size, mask;				\
117		type *data;						\
118	} name
119
120#define fifo_for_each(c, fifo, iter)					\
121	for (iter = (fifo)->front;					\
122	     c = (fifo)->data[iter], iter != (fifo)->back;		\
123	     iter = (iter + 1) & (fifo)->mask)
124
125#define __init_fifo(fifo, gfp)						\
126({									\
127	size_t _allocated_size, _bytes;					\
128	BUG_ON(!(fifo)->size);						\
129									\
130	_allocated_size = roundup_pow_of_two((fifo)->size + 1);		\
131	_bytes = _allocated_size * sizeof(*(fifo)->data);		\
132									\
133	(fifo)->mask = _allocated_size - 1;				\
134	(fifo)->front = (fifo)->back = 0;				\
135									\
136	(fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);		\
137	(fifo)->data;							\
138})
139
140#define init_fifo_exact(fifo, _size, gfp)				\
141({									\
142	(fifo)->size = (_size);						\
143	__init_fifo(fifo, gfp);						\
144})
145
146#define init_fifo(fifo, _size, gfp)					\
147({									\
148	(fifo)->size = (_size);						\
149	if ((fifo)->size > 4)						\
150		(fifo)->size = roundup_pow_of_two((fifo)->size) - 1;	\
151	__init_fifo(fifo, gfp);						\
152})
153
154#define free_fifo(fifo)							\
155do {									\
156	kvfree((fifo)->data);						\
157	(fifo)->data = NULL;						\
158} while (0)
159
160#define fifo_used(fifo)		(((fifo)->back - (fifo)->front) & (fifo)->mask)
161#define fifo_free(fifo)		((fifo)->size - fifo_used(fifo))
162
163#define fifo_empty(fifo)	(!fifo_used(fifo))
164#define fifo_full(fifo)		(!fifo_free(fifo))
165
166#define fifo_front(fifo)	((fifo)->data[(fifo)->front])
167#define fifo_back(fifo)							\
168	((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
169
170#define fifo_idx(fifo, p)	(((p) - &fifo_front(fifo)) & (fifo)->mask)
171
172#define fifo_push_back(fifo, i)						\
173({									\
174	bool _r = !fifo_full((fifo));					\
175	if (_r) {							\
176		(fifo)->data[(fifo)->back++] = (i);			\
177		(fifo)->back &= (fifo)->mask;				\
178	}								\
179	_r;								\
180})
181
182#define fifo_pop_front(fifo, i)						\
183({									\
184	bool _r = !fifo_empty((fifo));					\
185	if (_r) {							\
186		(i) = (fifo)->data[(fifo)->front++];			\
187		(fifo)->front &= (fifo)->mask;				\
188	}								\
189	_r;								\
190})
191
192#define fifo_push_front(fifo, i)					\
193({									\
194	bool _r = !fifo_full((fifo));					\
195	if (_r) {							\
196		--(fifo)->front;					\
197		(fifo)->front &= (fifo)->mask;				\
198		(fifo)->data[(fifo)->front] = (i);			\
199	}								\
200	_r;								\
201})
202
203#define fifo_pop_back(fifo, i)						\
204({									\
205	bool _r = !fifo_empty((fifo));					\
206	if (_r) {							\
207		--(fifo)->back;						\
208		(fifo)->back &= (fifo)->mask;				\
209		(i) = (fifo)->data[(fifo)->back]			\
210	}								\
211	_r;								\
212})
213
214#define fifo_push(fifo, i)	fifo_push_back(fifo, (i))
215#define fifo_pop(fifo, i)	fifo_pop_front(fifo, (i))
216
217#define fifo_swap(l, r)							\
218do {									\
219	swap((l)->front, (r)->front);					\
220	swap((l)->back, (r)->back);					\
221	swap((l)->size, (r)->size);					\
222	swap((l)->mask, (r)->mask);					\
223	swap((l)->data, (r)->data);					\
224} while (0)
225
226#define fifo_move(dest, src)						\
227do {									\
228	typeof(*((dest)->data)) _t;					\
229	while (!fifo_full(dest) &&					\
230	       fifo_pop(src, _t))					\
231		fifo_push(dest, _t);					\
232} while (0)
233
234/*
235 * Simple array based allocator - preallocates a number of elements and you can
236 * never allocate more than that, also has no locking.
237 *
238 * Handy because if you know you only need a fixed number of elements you don't
239 * have to worry about memory allocation failure, and sometimes a mempool isn't
240 * what you want.
241 *
242 * We treat the free elements as entries in a singly linked list, and the
243 * freelist as a stack - allocating and freeing push and pop off the freelist.
244 */
245
246#define DECLARE_ARRAY_ALLOCATOR(type, name, size)			\
247	struct {							\
248		type	*freelist;					\
249		type	data[size];					\
250	} name
251
252#define array_alloc(array)						\
253({									\
254	typeof((array)->freelist) _ret = (array)->freelist;		\
255									\
256	if (_ret)							\
257		(array)->freelist = *((typeof((array)->freelist) *) _ret);\
258									\
259	_ret;								\
260})
261
262#define array_free(array, ptr)						\
263do {									\
264	typeof((array)->freelist) _ptr = ptr;				\
265									\
266	*((typeof((array)->freelist) *) _ptr) = (array)->freelist;	\
267	(array)->freelist = _ptr;					\
268} while (0)
269
270#define array_allocator_init(array)					\
271do {									\
272	typeof((array)->freelist) _i;					\
273									\
274	BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *));	\
275	(array)->freelist = NULL;					\
276									\
277	for (_i = (array)->data;					\
278	     _i < (array)->data + ARRAY_SIZE((array)->data);		\
279	     _i++)							\
280		array_free(array, _i);					\
281} while (0)
282
283#define array_freelist_empty(array)	((array)->freelist == NULL)
284
285#define ANYSINT_MAX(t)							\
286	((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
287
288int bch_strtoint_h(const char *cp, int *res);
289int bch_strtouint_h(const char *cp, unsigned int *res);
290int bch_strtoll_h(const char *cp, long long *res);
291int bch_strtoull_h(const char *cp, unsigned long long *res);
292
293static inline int bch_strtol_h(const char *cp, long *res)
294{
295#if BITS_PER_LONG == 32
296	return bch_strtoint_h(cp, (int *) res);
297#else
298	return bch_strtoll_h(cp, (long long *) res);
299#endif
300}
301
302static inline int bch_strtoul_h(const char *cp, long *res)
303{
304#if BITS_PER_LONG == 32
305	return bch_strtouint_h(cp, (unsigned int *) res);
306#else
307	return bch_strtoull_h(cp, (unsigned long long *) res);
308#endif
309}
310
311#define strtoi_h(cp, res)						\
312	(__builtin_types_compatible_p(typeof(*res), int)		\
313	? bch_strtoint_h(cp, (void *) res)				\
314	: __builtin_types_compatible_p(typeof(*res), long)		\
315	? bch_strtol_h(cp, (void *) res)				\
316	: __builtin_types_compatible_p(typeof(*res), long long)		\
317	? bch_strtoll_h(cp, (void *) res)				\
318	: __builtin_types_compatible_p(typeof(*res), unsigned int)	\
319	? bch_strtouint_h(cp, (void *) res)				\
320	: __builtin_types_compatible_p(typeof(*res), unsigned long)	\
321	? bch_strtoul_h(cp, (void *) res)				\
322	: __builtin_types_compatible_p(typeof(*res), unsigned long long)\
323	? bch_strtoull_h(cp, (void *) res) : -EINVAL)
324
325#define strtoul_safe(cp, var)						\
326({									\
327	unsigned long _v;						\
328	int _r = kstrtoul(cp, 10, &_v);					\
329	if (!_r)							\
330		var = _v;						\
331	_r;								\
332})
333
334#define strtoul_safe_clamp(cp, var, min, max)				\
335({									\
336	unsigned long _v;						\
337	int _r = kstrtoul(cp, 10, &_v);					\
338	if (!_r)							\
339		var = clamp_t(typeof(var), _v, min, max);		\
340	_r;								\
341})
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343ssize_t bch_hprint(char *buf, int64_t v);
344
345bool bch_is_zero(const char *p, size_t n);
346int bch_parse_uuid(const char *s, char *uuid);
347
 
 
 
 
 
348struct time_stats {
349	spinlock_t	lock;
350	/*
351	 * all fields are in nanoseconds, averages are ewmas stored left shifted
352	 * by 8
353	 */
354	uint64_t	max_duration;
355	uint64_t	average_duration;
356	uint64_t	average_frequency;
357	uint64_t	last;
358};
359
360void bch_time_stats_update(struct time_stats *stats, uint64_t time);
361
362static inline unsigned int local_clock_us(void)
363{
364	return local_clock() >> 10;
365}
366
367#define NSEC_PER_ns			1L
368#define NSEC_PER_us			NSEC_PER_USEC
369#define NSEC_PER_ms			NSEC_PER_MSEC
370#define NSEC_PER_sec			NSEC_PER_SEC
371
372#define __print_time_stat(stats, name, stat, units)			\
373	sysfs_print(name ## _ ## stat ## _ ## units,			\
374		    div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
375
376#define sysfs_print_time_stats(stats, name,				\
377			       frequency_units,				\
378			       duration_units)				\
379do {									\
380	__print_time_stat(stats, name,					\
381			  average_frequency,	frequency_units);	\
382	__print_time_stat(stats, name,					\
383			  average_duration,	duration_units);	\
384	sysfs_print(name ## _ ##max_duration ## _ ## duration_units,	\
385			div_u64((stats)->max_duration,			\
386				NSEC_PER_ ## duration_units));		\
387									\
388	sysfs_print(name ## _last_ ## frequency_units, (stats)->last	\
389		    ? div_s64(local_clock() - (stats)->last,		\
390			      NSEC_PER_ ## frequency_units)		\
391		    : -1LL);						\
392} while (0)
393
394#define sysfs_time_stats_attribute(name,				\
395				   frequency_units,			\
396				   duration_units)			\
397read_attribute(name ## _average_frequency_ ## frequency_units);		\
398read_attribute(name ## _average_duration_ ## duration_units);		\
399read_attribute(name ## _max_duration_ ## duration_units);		\
400read_attribute(name ## _last_ ## frequency_units)
401
402#define sysfs_time_stats_attribute_list(name,				\
403					frequency_units,		\
404					duration_units)			\
405&sysfs_ ## name ## _average_frequency_ ## frequency_units,		\
406&sysfs_ ## name ## _average_duration_ ## duration_units,		\
407&sysfs_ ## name ## _max_duration_ ## duration_units,			\
408&sysfs_ ## name ## _last_ ## frequency_units,
409
410#define ewma_add(ewma, val, weight, factor)				\
411({									\
412	(ewma) *= (weight) - 1;						\
413	(ewma) += (val) << factor;					\
414	(ewma) /= (weight);						\
415	(ewma) >> factor;						\
416})
417
418struct bch_ratelimit {
419	/* Next time we want to do some work, in nanoseconds */
420	uint64_t		next;
421
422	/*
423	 * Rate at which we want to do work, in units per second
424	 * The units here correspond to the units passed to bch_next_delay()
425	 */
426	atomic_long_t		rate;
427};
428
429static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
430{
431	d->next = local_clock();
432}
433
434uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
435
436#define __DIV_SAFE(n, d, zero)						\
437({									\
438	typeof(n) _n = (n);						\
439	typeof(d) _d = (d);						\
440	_d ? _n / _d : zero;						\
441})
442
443#define DIV_SAFE(n, d)	__DIV_SAFE(n, d, 0)
444
445#define container_of_or_null(ptr, type, member)				\
446({									\
447	typeof(ptr) _ptr = ptr;						\
448	_ptr ? container_of(_ptr, type, member) : NULL;			\
449})
450
451#define RB_INSERT(root, new, member, cmp)				\
452({									\
453	__label__ dup;							\
454	struct rb_node **n = &(root)->rb_node, *parent = NULL;		\
455	typeof(new) this;						\
456	int res, ret = -1;						\
457									\
458	while (*n) {							\
459		parent = *n;						\
460		this = container_of(*n, typeof(*(new)), member);	\
461		res = cmp(new, this);					\
462		if (!res)						\
463			goto dup;					\
464		n = res < 0						\
465			? &(*n)->rb_left				\
466			: &(*n)->rb_right;				\
467	}								\
468									\
469	rb_link_node(&(new)->member, parent, n);			\
470	rb_insert_color(&(new)->member, root);				\
471	ret = 0;							\
472dup:									\
473	ret;								\
474})
475
476#define RB_SEARCH(root, search, member, cmp)				\
477({									\
478	struct rb_node *n = (root)->rb_node;				\
479	typeof(&(search)) this, ret = NULL;				\
480	int res;							\
481									\
482	while (n) {							\
483		this = container_of(n, typeof(search), member);		\
484		res = cmp(&(search), this);				\
485		if (!res) {						\
486			ret = this;					\
487			break;						\
488		}							\
489		n = res < 0						\
490			? n->rb_left					\
491			: n->rb_right;					\
492	}								\
493	ret;								\
494})
495
496#define RB_GREATER(root, search, member, cmp)				\
497({									\
498	struct rb_node *n = (root)->rb_node;				\
499	typeof(&(search)) this, ret = NULL;				\
500	int res;							\
501									\
502	while (n) {							\
503		this = container_of(n, typeof(search), member);		\
504		res = cmp(&(search), this);				\
505		if (res < 0) {						\
506			ret = this;					\
507			n = n->rb_left;					\
508		} else							\
509			n = n->rb_right;				\
510	}								\
511	ret;								\
512})
513
514#define RB_FIRST(root, type, member)					\
515	container_of_or_null(rb_first(root), type, member)
516
517#define RB_LAST(root, type, member)					\
518	container_of_or_null(rb_last(root), type, member)
519
520#define RB_NEXT(ptr, member)						\
521	container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
522
523#define RB_PREV(ptr, member)						\
524	container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
525
526static inline uint64_t bch_crc64(const void *p, size_t len)
 
527{
528	uint64_t crc = 0xffffffffffffffffULL;
529
530	crc = crc64_be(crc, p, len);
531	return crc ^ 0xffffffffffffffffULL;
532}
533
534/*
535 * A stepwise-linear pseudo-exponential.  This returns 1 << (x >>
536 * frac_bits), with the less-significant bits filled in by linear
537 * interpolation.
538 *
539 * This can also be interpreted as a floating-point number format,
540 * where the low frac_bits are the mantissa (with implicit leading
541 * 1 bit), and the more significant bits are the exponent.
542 * The return value is 1.mantissa * 2^exponent.
543 *
544 * The way this is used, fract_bits is 6 and the largest possible
545 * input is CONGESTED_MAX-1 = 1023 (exponent 16, mantissa 0x1.fc),
546 * so the maximum output is 0x1fc00.
547 */
548static inline unsigned int fract_exp_two(unsigned int x,
549					 unsigned int fract_bits)
550{
551	unsigned int mantissa = 1 << fract_bits;	/* Implicit bit */
552
553	mantissa += x & (mantissa - 1);
554	x >>= fract_bits;	/* The exponent */
555	/* Largest intermediate value 0x7f0000 */
556	return mantissa << x >> fract_bits;
557}
558
559void bch_bio_map(struct bio *bio, void *base);
560int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
 
 
 
 
 
 
 
 
561
562#endif /* _BCACHE_UTIL_H */
v4.17
  1/* SPDX-License-Identifier: GPL-2.0 */
  2
  3#ifndef _BCACHE_UTIL_H
  4#define _BCACHE_UTIL_H
  5
  6#include <linux/blkdev.h>
  7#include <linux/errno.h>
  8#include <linux/kernel.h>
  9#include <linux/sched/clock.h>
 10#include <linux/llist.h>
 11#include <linux/ratelimit.h>
 12#include <linux/vmalloc.h>
 13#include <linux/workqueue.h>
 
 14
 15#include "closure.h"
 16
 17#define PAGE_SECTORS		(PAGE_SIZE / 512)
 18
 19struct closure;
 20
 21#ifdef CONFIG_BCACHE_DEBUG
 22
 23#define EBUG_ON(cond)			BUG_ON(cond)
 24#define atomic_dec_bug(v)	BUG_ON(atomic_dec_return(v) < 0)
 25#define atomic_inc_bug(v, i)	BUG_ON(atomic_inc_return(v) <= i)
 26
 27#else /* DEBUG */
 28
 29#define EBUG_ON(cond)			do { if (cond); } while (0)
 30#define atomic_dec_bug(v)	atomic_dec(v)
 31#define atomic_inc_bug(v, i)	atomic_inc(v)
 32
 33#endif
 34
 35#define DECLARE_HEAP(type, name)					\
 36	struct {							\
 37		size_t size, used;					\
 38		type *data;						\
 39	} name
 40
 41#define init_heap(heap, _size, gfp)					\
 42({									\
 43	size_t _bytes;							\
 44	(heap)->used = 0;						\
 45	(heap)->size = (_size);						\
 46	_bytes = (heap)->size * sizeof(*(heap)->data);			\
 47	(heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);		\
 48	(heap)->data;							\
 49})
 50
 51#define free_heap(heap)							\
 52do {									\
 53	kvfree((heap)->data);						\
 54	(heap)->data = NULL;						\
 55} while (0)
 56
 57#define heap_swap(h, i, j)	swap((h)->data[i], (h)->data[j])
 58
 59#define heap_sift(h, i, cmp)						\
 60do {									\
 61	size_t _r, _j = i;						\
 62									\
 63	for (; _j * 2 + 1 < (h)->used; _j = _r) {			\
 64		_r = _j * 2 + 1;					\
 65		if (_r + 1 < (h)->used &&				\
 66		    cmp((h)->data[_r], (h)->data[_r + 1]))		\
 67			_r++;						\
 68									\
 69		if (cmp((h)->data[_r], (h)->data[_j]))			\
 70			break;						\
 71		heap_swap(h, _r, _j);					\
 72	}								\
 73} while (0)
 74
 75#define heap_sift_down(h, i, cmp)					\
 76do {									\
 77	while (i) {							\
 78		size_t p = (i - 1) / 2;					\
 79		if (cmp((h)->data[i], (h)->data[p]))			\
 80			break;						\
 81		heap_swap(h, i, p);					\
 82		i = p;							\
 83	}								\
 84} while (0)
 85
 86#define heap_add(h, d, cmp)						\
 87({									\
 88	bool _r = !heap_full(h);					\
 89	if (_r) {							\
 90		size_t _i = (h)->used++;				\
 91		(h)->data[_i] = d;					\
 92									\
 93		heap_sift_down(h, _i, cmp);				\
 94		heap_sift(h, _i, cmp);					\
 95	}								\
 96	_r;								\
 97})
 98
 99#define heap_pop(h, d, cmp)						\
100({									\
101	bool _r = (h)->used;						\
102	if (_r) {							\
103		(d) = (h)->data[0];					\
104		(h)->used--;						\
105		heap_swap(h, 0, (h)->used);				\
106		heap_sift(h, 0, cmp);					\
107	}								\
108	_r;								\
109})
110
111#define heap_peek(h)	((h)->used ? (h)->data[0] : NULL)
112
113#define heap_full(h)	((h)->used == (h)->size)
114
115#define heap_empty(h)	((h)->used == 0)
116
117#define DECLARE_FIFO(type, name)					\
118	struct {							\
119		size_t front, back, size, mask;				\
120		type *data;						\
121	} name
122
123#define fifo_for_each(c, fifo, iter)					\
124	for (iter = (fifo)->front;					\
125	     c = (fifo)->data[iter], iter != (fifo)->back;		\
126	     iter = (iter + 1) & (fifo)->mask)
127
128#define __init_fifo(fifo, gfp)						\
129({									\
130	size_t _allocated_size, _bytes;					\
131	BUG_ON(!(fifo)->size);						\
132									\
133	_allocated_size = roundup_pow_of_two((fifo)->size + 1);		\
134	_bytes = _allocated_size * sizeof(*(fifo)->data);		\
135									\
136	(fifo)->mask = _allocated_size - 1;				\
137	(fifo)->front = (fifo)->back = 0;				\
138									\
139	(fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);		\
140	(fifo)->data;							\
141})
142
143#define init_fifo_exact(fifo, _size, gfp)				\
144({									\
145	(fifo)->size = (_size);						\
146	__init_fifo(fifo, gfp);						\
147})
148
149#define init_fifo(fifo, _size, gfp)					\
150({									\
151	(fifo)->size = (_size);						\
152	if ((fifo)->size > 4)						\
153		(fifo)->size = roundup_pow_of_two((fifo)->size) - 1;	\
154	__init_fifo(fifo, gfp);						\
155})
156
157#define free_fifo(fifo)							\
158do {									\
159	kvfree((fifo)->data);						\
160	(fifo)->data = NULL;						\
161} while (0)
162
163#define fifo_used(fifo)		(((fifo)->back - (fifo)->front) & (fifo)->mask)
164#define fifo_free(fifo)		((fifo)->size - fifo_used(fifo))
165
166#define fifo_empty(fifo)	(!fifo_used(fifo))
167#define fifo_full(fifo)		(!fifo_free(fifo))
168
169#define fifo_front(fifo)	((fifo)->data[(fifo)->front])
170#define fifo_back(fifo)							\
171	((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
172
173#define fifo_idx(fifo, p)	(((p) - &fifo_front(fifo)) & (fifo)->mask)
174
175#define fifo_push_back(fifo, i)						\
176({									\
177	bool _r = !fifo_full((fifo));					\
178	if (_r) {							\
179		(fifo)->data[(fifo)->back++] = (i);			\
180		(fifo)->back &= (fifo)->mask;				\
181	}								\
182	_r;								\
183})
184
185#define fifo_pop_front(fifo, i)						\
186({									\
187	bool _r = !fifo_empty((fifo));					\
188	if (_r) {							\
189		(i) = (fifo)->data[(fifo)->front++];			\
190		(fifo)->front &= (fifo)->mask;				\
191	}								\
192	_r;								\
193})
194
195#define fifo_push_front(fifo, i)					\
196({									\
197	bool _r = !fifo_full((fifo));					\
198	if (_r) {							\
199		--(fifo)->front;					\
200		(fifo)->front &= (fifo)->mask;				\
201		(fifo)->data[(fifo)->front] = (i);			\
202	}								\
203	_r;								\
204})
205
206#define fifo_pop_back(fifo, i)						\
207({									\
208	bool _r = !fifo_empty((fifo));					\
209	if (_r) {							\
210		--(fifo)->back;						\
211		(fifo)->back &= (fifo)->mask;				\
212		(i) = (fifo)->data[(fifo)->back]			\
213	}								\
214	_r;								\
215})
216
217#define fifo_push(fifo, i)	fifo_push_back(fifo, (i))
218#define fifo_pop(fifo, i)	fifo_pop_front(fifo, (i))
219
220#define fifo_swap(l, r)							\
221do {									\
222	swap((l)->front, (r)->front);					\
223	swap((l)->back, (r)->back);					\
224	swap((l)->size, (r)->size);					\
225	swap((l)->mask, (r)->mask);					\
226	swap((l)->data, (r)->data);					\
227} while (0)
228
229#define fifo_move(dest, src)						\
230do {									\
231	typeof(*((dest)->data)) _t;					\
232	while (!fifo_full(dest) &&					\
233	       fifo_pop(src, _t))					\
234		fifo_push(dest, _t);					\
235} while (0)
236
237/*
238 * Simple array based allocator - preallocates a number of elements and you can
239 * never allocate more than that, also has no locking.
240 *
241 * Handy because if you know you only need a fixed number of elements you don't
242 * have to worry about memory allocation failure, and sometimes a mempool isn't
243 * what you want.
244 *
245 * We treat the free elements as entries in a singly linked list, and the
246 * freelist as a stack - allocating and freeing push and pop off the freelist.
247 */
248
249#define DECLARE_ARRAY_ALLOCATOR(type, name, size)			\
250	struct {							\
251		type	*freelist;					\
252		type	data[size];					\
253	} name
254
255#define array_alloc(array)						\
256({									\
257	typeof((array)->freelist) _ret = (array)->freelist;		\
258									\
259	if (_ret)							\
260		(array)->freelist = *((typeof((array)->freelist) *) _ret);\
261									\
262	_ret;								\
263})
264
265#define array_free(array, ptr)						\
266do {									\
267	typeof((array)->freelist) _ptr = ptr;				\
268									\
269	*((typeof((array)->freelist) *) _ptr) = (array)->freelist;	\
270	(array)->freelist = _ptr;					\
271} while (0)
272
273#define array_allocator_init(array)					\
274do {									\
275	typeof((array)->freelist) _i;					\
276									\
277	BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *));	\
278	(array)->freelist = NULL;					\
279									\
280	for (_i = (array)->data;					\
281	     _i < (array)->data + ARRAY_SIZE((array)->data);		\
282	     _i++)							\
283		array_free(array, _i);					\
284} while (0)
285
286#define array_freelist_empty(array)	((array)->freelist == NULL)
287
288#define ANYSINT_MAX(t)							\
289	((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
290
291int bch_strtoint_h(const char *, int *);
292int bch_strtouint_h(const char *, unsigned int *);
293int bch_strtoll_h(const char *, long long *);
294int bch_strtoull_h(const char *, unsigned long long *);
295
296static inline int bch_strtol_h(const char *cp, long *res)
297{
298#if BITS_PER_LONG == 32
299	return bch_strtoint_h(cp, (int *) res);
300#else
301	return bch_strtoll_h(cp, (long long *) res);
302#endif
303}
304
305static inline int bch_strtoul_h(const char *cp, long *res)
306{
307#if BITS_PER_LONG == 32
308	return bch_strtouint_h(cp, (unsigned int *) res);
309#else
310	return bch_strtoull_h(cp, (unsigned long long *) res);
311#endif
312}
313
314#define strtoi_h(cp, res)						\
315	(__builtin_types_compatible_p(typeof(*res), int)		\
316	? bch_strtoint_h(cp, (void *) res)				\
317	: __builtin_types_compatible_p(typeof(*res), long)		\
318	? bch_strtol_h(cp, (void *) res)				\
319	: __builtin_types_compatible_p(typeof(*res), long long)		\
320	? bch_strtoll_h(cp, (void *) res)				\
321	: __builtin_types_compatible_p(typeof(*res), unsigned int)	\
322	? bch_strtouint_h(cp, (void *) res)				\
323	: __builtin_types_compatible_p(typeof(*res), unsigned long)	\
324	? bch_strtoul_h(cp, (void *) res)				\
325	: __builtin_types_compatible_p(typeof(*res), unsigned long long)\
326	? bch_strtoull_h(cp, (void *) res) : -EINVAL)
327
328#define strtoul_safe(cp, var)						\
329({									\
330	unsigned long _v;						\
331	int _r = kstrtoul(cp, 10, &_v);					\
332	if (!_r)							\
333		var = _v;						\
334	_r;								\
335})
336
337#define strtoul_safe_clamp(cp, var, min, max)				\
338({									\
339	unsigned long _v;						\
340	int _r = kstrtoul(cp, 10, &_v);					\
341	if (!_r)							\
342		var = clamp_t(typeof(var), _v, min, max);		\
343	_r;								\
344})
345
346#define snprint(buf, size, var)						\
347	snprintf(buf, size,						\
348		__builtin_types_compatible_p(typeof(var), int)		\
349		     ? "%i\n" :						\
350		__builtin_types_compatible_p(typeof(var), unsigned)	\
351		     ? "%u\n" :						\
352		__builtin_types_compatible_p(typeof(var), long)		\
353		     ? "%li\n" :					\
354		__builtin_types_compatible_p(typeof(var), unsigned long)\
355		     ? "%lu\n" :					\
356		__builtin_types_compatible_p(typeof(var), int64_t)	\
357		     ? "%lli\n" :					\
358		__builtin_types_compatible_p(typeof(var), uint64_t)	\
359		     ? "%llu\n" :					\
360		__builtin_types_compatible_p(typeof(var), const char *)	\
361		     ? "%s\n" : "%i\n", var)
362
363ssize_t bch_hprint(char *buf, int64_t v);
364
365bool bch_is_zero(const char *p, size_t n);
366int bch_parse_uuid(const char *s, char *uuid);
367
368ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
369			    size_t selected);
370
371ssize_t bch_read_string_list(const char *buf, const char * const list[]);
372
373struct time_stats {
374	spinlock_t	lock;
375	/*
376	 * all fields are in nanoseconds, averages are ewmas stored left shifted
377	 * by 8
378	 */
379	uint64_t	max_duration;
380	uint64_t	average_duration;
381	uint64_t	average_frequency;
382	uint64_t	last;
383};
384
385void bch_time_stats_update(struct time_stats *stats, uint64_t time);
386
387static inline unsigned local_clock_us(void)
388{
389	return local_clock() >> 10;
390}
391
392#define NSEC_PER_ns			1L
393#define NSEC_PER_us			NSEC_PER_USEC
394#define NSEC_PER_ms			NSEC_PER_MSEC
395#define NSEC_PER_sec			NSEC_PER_SEC
396
397#define __print_time_stat(stats, name, stat, units)			\
398	sysfs_print(name ## _ ## stat ## _ ## units,			\
399		    div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
400
401#define sysfs_print_time_stats(stats, name,				\
402			       frequency_units,				\
403			       duration_units)				\
404do {									\
405	__print_time_stat(stats, name,					\
406			  average_frequency,	frequency_units);	\
407	__print_time_stat(stats, name,					\
408			  average_duration,	duration_units);	\
409	sysfs_print(name ## _ ##max_duration ## _ ## duration_units,	\
410			div_u64((stats)->max_duration, NSEC_PER_ ## duration_units));\
 
411									\
412	sysfs_print(name ## _last_ ## frequency_units, (stats)->last	\
413		    ? div_s64(local_clock() - (stats)->last,		\
414			      NSEC_PER_ ## frequency_units)		\
415		    : -1LL);						\
416} while (0)
417
418#define sysfs_time_stats_attribute(name,				\
419				   frequency_units,			\
420				   duration_units)			\
421read_attribute(name ## _average_frequency_ ## frequency_units);		\
422read_attribute(name ## _average_duration_ ## duration_units);		\
423read_attribute(name ## _max_duration_ ## duration_units);		\
424read_attribute(name ## _last_ ## frequency_units)
425
426#define sysfs_time_stats_attribute_list(name,				\
427					frequency_units,		\
428					duration_units)			\
429&sysfs_ ## name ## _average_frequency_ ## frequency_units,		\
430&sysfs_ ## name ## _average_duration_ ## duration_units,		\
431&sysfs_ ## name ## _max_duration_ ## duration_units,			\
432&sysfs_ ## name ## _last_ ## frequency_units,
433
434#define ewma_add(ewma, val, weight, factor)				\
435({									\
436	(ewma) *= (weight) - 1;						\
437	(ewma) += (val) << factor;					\
438	(ewma) /= (weight);						\
439	(ewma) >> factor;						\
440})
441
442struct bch_ratelimit {
443	/* Next time we want to do some work, in nanoseconds */
444	uint64_t		next;
445
446	/*
447	 * Rate at which we want to do work, in units per second
448	 * The units here correspond to the units passed to bch_next_delay()
449	 */
450	uint32_t		rate;
451};
452
453static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
454{
455	d->next = local_clock();
456}
457
458uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
459
460#define __DIV_SAFE(n, d, zero)						\
461({									\
462	typeof(n) _n = (n);						\
463	typeof(d) _d = (d);						\
464	_d ? _n / _d : zero;						\
465})
466
467#define DIV_SAFE(n, d)	__DIV_SAFE(n, d, 0)
468
469#define container_of_or_null(ptr, type, member)				\
470({									\
471	typeof(ptr) _ptr = ptr;						\
472	_ptr ? container_of(_ptr, type, member) : NULL;			\
473})
474
475#define RB_INSERT(root, new, member, cmp)				\
476({									\
477	__label__ dup;							\
478	struct rb_node **n = &(root)->rb_node, *parent = NULL;		\
479	typeof(new) this;						\
480	int res, ret = -1;						\
481									\
482	while (*n) {							\
483		parent = *n;						\
484		this = container_of(*n, typeof(*(new)), member);	\
485		res = cmp(new, this);					\
486		if (!res)						\
487			goto dup;					\
488		n = res < 0						\
489			? &(*n)->rb_left				\
490			: &(*n)->rb_right;				\
491	}								\
492									\
493	rb_link_node(&(new)->member, parent, n);			\
494	rb_insert_color(&(new)->member, root);				\
495	ret = 0;							\
496dup:									\
497	ret;								\
498})
499
500#define RB_SEARCH(root, search, member, cmp)				\
501({									\
502	struct rb_node *n = (root)->rb_node;				\
503	typeof(&(search)) this, ret = NULL;				\
504	int res;							\
505									\
506	while (n) {							\
507		this = container_of(n, typeof(search), member);		\
508		res = cmp(&(search), this);				\
509		if (!res) {						\
510			ret = this;					\
511			break;						\
512		}							\
513		n = res < 0						\
514			? n->rb_left					\
515			: n->rb_right;					\
516	}								\
517	ret;								\
518})
519
520#define RB_GREATER(root, search, member, cmp)				\
521({									\
522	struct rb_node *n = (root)->rb_node;				\
523	typeof(&(search)) this, ret = NULL;				\
524	int res;							\
525									\
526	while (n) {							\
527		this = container_of(n, typeof(search), member);		\
528		res = cmp(&(search), this);				\
529		if (res < 0) {						\
530			ret = this;					\
531			n = n->rb_left;					\
532		} else							\
533			n = n->rb_right;				\
534	}								\
535	ret;								\
536})
537
538#define RB_FIRST(root, type, member)					\
539	container_of_or_null(rb_first(root), type, member)
540
541#define RB_LAST(root, type, member)					\
542	container_of_or_null(rb_last(root), type, member)
543
544#define RB_NEXT(ptr, member)						\
545	container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
546
547#define RB_PREV(ptr, member)						\
548	container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
549
550/* Does linear interpolation between powers of two */
551static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
552{
553	unsigned fract = x & ~(~0 << fract_bits);
 
 
 
 
554
555	x >>= fract_bits;
556	x   = 1 << x;
557	x  += (x * fract) >> fract_bits;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
559	return x;
 
 
 
560}
561
562void bch_bio_map(struct bio *bio, void *base);
563int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
564
565static inline sector_t bdev_sectors(struct block_device *bdev)
566{
567	return bdev->bd_inode->i_size >> 9;
568}
569
570uint64_t bch_crc64_update(uint64_t, const void *, size_t);
571uint64_t bch_crc64(const void *, size_t);
572
573#endif /* _BCACHE_UTIL_H */