xref: /linux/fs/bcachefs/util.c (revision 8f602276d3902642fdc3429b548d73c745446601)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet /*
3fd60f7feSKuan-Wei Chiu  * random utility code, for bcache but in theory not specific to bcache
41c6fdbd8SKent Overstreet  *
51c6fdbd8SKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
61c6fdbd8SKent Overstreet  * Copyright 2012 Google, Inc.
71c6fdbd8SKent Overstreet  */
81c6fdbd8SKent Overstreet 
91c6fdbd8SKent Overstreet #include <linux/bio.h>
101c6fdbd8SKent Overstreet #include <linux/blkdev.h>
11a8f35428SKent Overstreet #include <linux/console.h>
121c6fdbd8SKent Overstreet #include <linux/ctype.h>
131c6fdbd8SKent Overstreet #include <linux/debugfs.h>
141c6fdbd8SKent Overstreet #include <linux/freezer.h>
151c6fdbd8SKent Overstreet #include <linux/kthread.h>
161c6fdbd8SKent Overstreet #include <linux/log2.h>
171c6fdbd8SKent Overstreet #include <linux/math64.h>
181c6fdbd8SKent Overstreet #include <linux/percpu.h>
191c6fdbd8SKent Overstreet #include <linux/preempt.h>
201c6fdbd8SKent Overstreet #include <linux/random.h>
211c6fdbd8SKent Overstreet #include <linux/seq_file.h>
221c6fdbd8SKent Overstreet #include <linux/string.h>
231c6fdbd8SKent Overstreet #include <linux/types.h>
241c6fdbd8SKent Overstreet #include <linux/sched/clock.h>
251c6fdbd8SKent Overstreet 
261c6fdbd8SKent Overstreet #include "eytzinger.h"
27bf8f8b20SDaniel Hill #include "mean_and_variance.h"
281c6fdbd8SKent Overstreet #include "util.h"
291c6fdbd8SKent Overstreet 
301c6fdbd8SKent Overstreet static const char si_units[] = "?kMGTPEZY";
311c6fdbd8SKent Overstreet 
32a5d18f9eSKent Overstreet /* string_get_size units: */
33a5d18f9eSKent Overstreet static const char *const units_2[] = {
34a5d18f9eSKent Overstreet 	"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
35a5d18f9eSKent Overstreet };
36a5d18f9eSKent Overstreet static const char *const units_10[] = {
37a5d18f9eSKent Overstreet 	"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
38a5d18f9eSKent Overstreet };
391c6fdbd8SKent Overstreet 
parse_u64(const char * cp,u64 * res)40a5d18f9eSKent Overstreet static int parse_u64(const char *cp, u64 *res)
41a5d18f9eSKent Overstreet {
42a5d18f9eSKent Overstreet 	const char *start = cp;
43a5d18f9eSKent Overstreet 	u64 v = 0;
441c6fdbd8SKent Overstreet 
451c6fdbd8SKent Overstreet 	if (!isdigit(*cp))
461c6fdbd8SKent Overstreet 		return -EINVAL;
471c6fdbd8SKent Overstreet 
481c6fdbd8SKent Overstreet 	do {
491c6fdbd8SKent Overstreet 		if (v > U64_MAX / 10)
501c6fdbd8SKent Overstreet 			return -ERANGE;
511c6fdbd8SKent Overstreet 		v *= 10;
521c6fdbd8SKent Overstreet 		if (v > U64_MAX - (*cp - '0'))
531c6fdbd8SKent Overstreet 			return -ERANGE;
541c6fdbd8SKent Overstreet 		v += *cp - '0';
551c6fdbd8SKent Overstreet 		cp++;
561c6fdbd8SKent Overstreet 	} while (isdigit(*cp));
571c6fdbd8SKent Overstreet 
58a5d18f9eSKent Overstreet 	*res = v;
59a5d18f9eSKent Overstreet 	return cp - start;
60a5d18f9eSKent Overstreet }
61a5d18f9eSKent Overstreet 
bch2_pow(u64 n,u64 p,u64 * res)62a5d18f9eSKent Overstreet static int bch2_pow(u64 n, u64 p, u64 *res)
63a5d18f9eSKent Overstreet {
64a5d18f9eSKent Overstreet 	*res = 1;
65a5d18f9eSKent Overstreet 
66a5d18f9eSKent Overstreet 	while (p--) {
6727663d77SReed Riley 		if (*res > div64_u64(U64_MAX, n))
68a5d18f9eSKent Overstreet 			return -ERANGE;
69a5d18f9eSKent Overstreet 		*res *= n;
70a5d18f9eSKent Overstreet 	}
71a5d18f9eSKent Overstreet 	return 0;
72a5d18f9eSKent Overstreet }
73a5d18f9eSKent Overstreet 
parse_unit_suffix(const char * cp,u64 * res)74a5d18f9eSKent Overstreet static int parse_unit_suffix(const char *cp, u64 *res)
75a5d18f9eSKent Overstreet {
76a5d18f9eSKent Overstreet 	const char *start = cp;
77a5d18f9eSKent Overstreet 	u64 base = 1024;
78a5d18f9eSKent Overstreet 	unsigned u;
79a5d18f9eSKent Overstreet 	int ret;
80a5d18f9eSKent Overstreet 
81a5d18f9eSKent Overstreet 	if (*cp == ' ')
82a5d18f9eSKent Overstreet 		cp++;
83a5d18f9eSKent Overstreet 
841c6fdbd8SKent Overstreet 	for (u = 1; u < strlen(si_units); u++)
851c6fdbd8SKent Overstreet 		if (*cp == si_units[u]) {
861c6fdbd8SKent Overstreet 			cp++;
871c6fdbd8SKent Overstreet 			goto got_unit;
881c6fdbd8SKent Overstreet 		}
89a5d18f9eSKent Overstreet 
90a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_2); u++)
91a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
92a5d18f9eSKent Overstreet 			cp += strlen(units_2[u]);
93a5d18f9eSKent Overstreet 			goto got_unit;
94a5d18f9eSKent Overstreet 		}
95a5d18f9eSKent Overstreet 
96a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_10); u++)
97a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
98a5d18f9eSKent Overstreet 			cp += strlen(units_10[u]);
99a5d18f9eSKent Overstreet 			base = 1000;
100a5d18f9eSKent Overstreet 			goto got_unit;
101a5d18f9eSKent Overstreet 		}
102a5d18f9eSKent Overstreet 
103a5d18f9eSKent Overstreet 	*res = 1;
104a5d18f9eSKent Overstreet 	return 0;
1051c6fdbd8SKent Overstreet got_unit:
106a5d18f9eSKent Overstreet 	ret = bch2_pow(base, u, res);
107a5d18f9eSKent Overstreet 	if (ret)
108a5d18f9eSKent Overstreet 		return ret;
109a5d18f9eSKent Overstreet 
110a5d18f9eSKent Overstreet 	return cp - start;
111a5d18f9eSKent Overstreet }
112a5d18f9eSKent Overstreet 
113a5d18f9eSKent Overstreet #define parse_or_ret(cp, _f)			\
114a5d18f9eSKent Overstreet do {						\
11596dea3d5SKent Overstreet 	int _ret = _f;				\
11696dea3d5SKent Overstreet 	if (_ret < 0)				\
11796dea3d5SKent Overstreet 		return _ret;			\
11896dea3d5SKent Overstreet 	cp += _ret;				\
119a5d18f9eSKent Overstreet } while (0)
120a5d18f9eSKent Overstreet 
__bch2_strtou64_h(const char * cp,u64 * res)121a5d18f9eSKent Overstreet static int __bch2_strtou64_h(const char *cp, u64 *res)
122a5d18f9eSKent Overstreet {
123a5d18f9eSKent Overstreet 	const char *start = cp;
124a5d18f9eSKent Overstreet 	u64 v = 0, b, f_n = 0, f_d = 1;
125a5d18f9eSKent Overstreet 	int ret;
126a5d18f9eSKent Overstreet 
127a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_u64(cp, &v));
128a5d18f9eSKent Overstreet 
129a5d18f9eSKent Overstreet 	if (*cp == '.') {
130a5d18f9eSKent Overstreet 		cp++;
131a5d18f9eSKent Overstreet 		ret = parse_u64(cp, &f_n);
132a5d18f9eSKent Overstreet 		if (ret < 0)
133a5d18f9eSKent Overstreet 			return ret;
134a5d18f9eSKent Overstreet 		cp += ret;
135a5d18f9eSKent Overstreet 
136a5d18f9eSKent Overstreet 		ret = bch2_pow(10, ret, &f_d);
137a5d18f9eSKent Overstreet 		if (ret)
138a5d18f9eSKent Overstreet 			return ret;
139a5d18f9eSKent Overstreet 	}
140a5d18f9eSKent Overstreet 
141a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_unit_suffix(cp, &b));
142a5d18f9eSKent Overstreet 
14327663d77SReed Riley 	if (v > div64_u64(U64_MAX, b))
144a5d18f9eSKent Overstreet 		return -ERANGE;
145a5d18f9eSKent Overstreet 	v *= b;
146a5d18f9eSKent Overstreet 
14727663d77SReed Riley 	if (f_n > div64_u64(U64_MAX, b))
148a5d18f9eSKent Overstreet 		return -ERANGE;
149a5d18f9eSKent Overstreet 
15027663d77SReed Riley 	f_n = div64_u64(f_n * b, f_d);
151a5d18f9eSKent Overstreet 	if (v + f_n < v)
152a5d18f9eSKent Overstreet 		return -ERANGE;
153a5d18f9eSKent Overstreet 	v += f_n;
154a5d18f9eSKent Overstreet 
155a5d18f9eSKent Overstreet 	*res = v;
156a5d18f9eSKent Overstreet 	return cp - start;
157a5d18f9eSKent Overstreet }
158a5d18f9eSKent Overstreet 
__bch2_strtoh(const char * cp,u64 * res,u64 t_max,bool t_signed)159a5d18f9eSKent Overstreet static int __bch2_strtoh(const char *cp, u64 *res,
160a5d18f9eSKent Overstreet 			 u64 t_max, bool t_signed)
161a5d18f9eSKent Overstreet {
162a5d18f9eSKent Overstreet 	bool positive = *cp != '-';
163a5d18f9eSKent Overstreet 	u64 v = 0;
164a5d18f9eSKent Overstreet 
165a5d18f9eSKent Overstreet 	if (*cp == '+' || *cp == '-')
166a5d18f9eSKent Overstreet 		cp++;
167a5d18f9eSKent Overstreet 
168a5d18f9eSKent Overstreet 	parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
169a5d18f9eSKent Overstreet 
1701c6fdbd8SKent Overstreet 	if (*cp == '\n')
1711c6fdbd8SKent Overstreet 		cp++;
1721c6fdbd8SKent Overstreet 	if (*cp)
1731c6fdbd8SKent Overstreet 		return -EINVAL;
1741c6fdbd8SKent Overstreet 
1751c6fdbd8SKent Overstreet 	if (positive) {
1761c6fdbd8SKent Overstreet 		if (v > t_max)
1771c6fdbd8SKent Overstreet 			return -ERANGE;
1781c6fdbd8SKent Overstreet 	} else {
1791c6fdbd8SKent Overstreet 		if (v && !t_signed)
1801c6fdbd8SKent Overstreet 			return -ERANGE;
1811c6fdbd8SKent Overstreet 
1821c6fdbd8SKent Overstreet 		if (v > t_max + 1)
1831c6fdbd8SKent Overstreet 			return -ERANGE;
1841c6fdbd8SKent Overstreet 		v = -v;
1851c6fdbd8SKent Overstreet 	}
1861c6fdbd8SKent Overstreet 
1871c6fdbd8SKent Overstreet 	*res = v;
1881c6fdbd8SKent Overstreet 	return 0;
1891c6fdbd8SKent Overstreet }
1901c6fdbd8SKent Overstreet 
1911c6fdbd8SKent Overstreet #define STRTO_H(name, type)					\
1921c6fdbd8SKent Overstreet int bch2_ ## name ## _h(const char *cp, type *res)		\
1931c6fdbd8SKent Overstreet {								\
194a5d18f9eSKent Overstreet 	u64 v = 0;						\
1951c6fdbd8SKent Overstreet 	int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),	\
1961c6fdbd8SKent Overstreet 			ANYSINT_MAX(type) != ((type) ~0ULL));	\
1971c6fdbd8SKent Overstreet 	*res = v;						\
1981c6fdbd8SKent Overstreet 	return ret;						\
1991c6fdbd8SKent Overstreet }
2001c6fdbd8SKent Overstreet 
STRTO_H(strtoint,int)2011c6fdbd8SKent Overstreet STRTO_H(strtoint, int)
2021c6fdbd8SKent Overstreet STRTO_H(strtouint, unsigned int)
2031c6fdbd8SKent Overstreet STRTO_H(strtoll, long long)
2041c6fdbd8SKent Overstreet STRTO_H(strtoull, unsigned long long)
2050b847a19SKent Overstreet STRTO_H(strtou64, u64)
2061c6fdbd8SKent Overstreet 
207afefc986SKent Overstreet u64 bch2_read_flag_list(const char *opt, const char * const list[])
2081c6fdbd8SKent Overstreet {
2091c6fdbd8SKent Overstreet 	u64 ret = 0;
2109d8022dbSKent Overstreet 	char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
2111c6fdbd8SKent Overstreet 
2121c6fdbd8SKent Overstreet 	if (!d)
2131c6fdbd8SKent Overstreet 		return -ENOMEM;
2141c6fdbd8SKent Overstreet 
2151c6fdbd8SKent Overstreet 	s = strim(d);
2161c6fdbd8SKent Overstreet 
2174f19a60cSKent Overstreet 	while ((p = strsep(&s, ",;"))) {
2181c6fdbd8SKent Overstreet 		int flag = match_string(list, -1, p);
2191e81f89bSKent Overstreet 
2201c6fdbd8SKent Overstreet 		if (flag < 0) {
2211c6fdbd8SKent Overstreet 			ret = -1;
2221c6fdbd8SKent Overstreet 			break;
2231c6fdbd8SKent Overstreet 		}
2241c6fdbd8SKent Overstreet 
225*e764e681SKent Overstreet 		ret |= BIT_ULL(flag);
2261c6fdbd8SKent Overstreet 	}
2271c6fdbd8SKent Overstreet 
2281c6fdbd8SKent Overstreet 	kfree(d);
2291c6fdbd8SKent Overstreet 
2301c6fdbd8SKent Overstreet 	return ret;
2311c6fdbd8SKent Overstreet }
2321c6fdbd8SKent Overstreet 
bch2_is_zero(const void * _p,size_t n)2331c6fdbd8SKent Overstreet bool bch2_is_zero(const void *_p, size_t n)
2341c6fdbd8SKent Overstreet {
2351c6fdbd8SKent Overstreet 	const char *p = _p;
2361c6fdbd8SKent Overstreet 	size_t i;
2371c6fdbd8SKent Overstreet 
2381c6fdbd8SKent Overstreet 	for (i = 0; i < n; i++)
2391c6fdbd8SKent Overstreet 		if (p[i])
2401c6fdbd8SKent Overstreet 			return false;
2411c6fdbd8SKent Overstreet 	return true;
2421c6fdbd8SKent Overstreet }
2431c6fdbd8SKent Overstreet 
bch2_prt_u64_base2_nbits(struct printbuf * out,u64 v,unsigned nr_bits)244189c176cSKent Overstreet void bch2_prt_u64_base2_nbits(struct printbuf *out, u64 v, unsigned nr_bits)
245d0b50524SKent Overstreet {
246d0b50524SKent Overstreet 	while (nr_bits)
247d0b50524SKent Overstreet 		prt_char(out, '0' + ((v >> --nr_bits) & 1));
248d0b50524SKent Overstreet }
249d0b50524SKent Overstreet 
bch2_prt_u64_base2(struct printbuf * out,u64 v)250189c176cSKent Overstreet void bch2_prt_u64_base2(struct printbuf *out, u64 v)
251189c176cSKent Overstreet {
252189c176cSKent Overstreet 	bch2_prt_u64_base2_nbits(out, v, fls64(v) ?: 1);
253189c176cSKent Overstreet }
254189c176cSKent Overstreet 
__bch2_print_string_as_lines(const char * prefix,const char * lines,bool nonblocking)255fd80d140SKent Overstreet static void __bch2_print_string_as_lines(const char *prefix, const char *lines,
256fd80d140SKent Overstreet 					 bool nonblocking)
257a8f35428SKent Overstreet {
258fd80d140SKent Overstreet 	bool locked = false;
259a8f35428SKent Overstreet 	const char *p;
260a8f35428SKent Overstreet 
261a8f35428SKent Overstreet 	if (!lines) {
262a8f35428SKent Overstreet 		printk("%s (null)\n", prefix);
263a8f35428SKent Overstreet 		return;
264a8f35428SKent Overstreet 	}
265a8f35428SKent Overstreet 
266fd80d140SKent Overstreet 	if (!nonblocking) {
267a8f35428SKent Overstreet 		console_lock();
268fd80d140SKent Overstreet 		locked = true;
269fd80d140SKent Overstreet 	} else {
270fd80d140SKent Overstreet 		locked = console_trylock();
271fd80d140SKent Overstreet 	}
272fd80d140SKent Overstreet 
273a8f35428SKent Overstreet 	while (1) {
274a8f35428SKent Overstreet 		p = strchrnul(lines, '\n');
275a8f35428SKent Overstreet 		printk("%s%.*s\n", prefix, (int) (p - lines), lines);
276a8f35428SKent Overstreet 		if (!*p)
277a8f35428SKent Overstreet 			break;
278a8f35428SKent Overstreet 		lines = p + 1;
279a8f35428SKent Overstreet 	}
280fd80d140SKent Overstreet 	if (locked)
281a8f35428SKent Overstreet 		console_unlock();
282a8f35428SKent Overstreet }
283a8f35428SKent Overstreet 
bch2_print_string_as_lines(const char * prefix,const char * lines)284fd80d140SKent Overstreet void bch2_print_string_as_lines(const char *prefix, const char *lines)
285fd80d140SKent Overstreet {
286fd80d140SKent Overstreet 	return __bch2_print_string_as_lines(prefix, lines, false);
287fd80d140SKent Overstreet }
288fd80d140SKent Overstreet 
bch2_print_string_as_lines_nonblocking(const char * prefix,const char * lines)289fd80d140SKent Overstreet void bch2_print_string_as_lines_nonblocking(const char *prefix, const char *lines)
290fd80d140SKent Overstreet {
291fd80d140SKent Overstreet 	return __bch2_print_string_as_lines(prefix, lines, true);
292fd80d140SKent Overstreet }
293fd80d140SKent Overstreet 
bch2_save_backtrace(bch_stacktrace * stack,struct task_struct * task,unsigned skipnr,gfp_t gfp)294612e1110SKent Overstreet int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task, unsigned skipnr,
295612e1110SKent Overstreet 			gfp_t gfp)
2961148a97fSKent Overstreet {
297e5570df2SKent Overstreet #ifdef CONFIG_STACKTRACE
2983ea4219dSKent Overstreet 	unsigned nr_entries = 0;
2993ea4219dSKent Overstreet 
3003ea4219dSKent Overstreet 	stack->nr = 0;
301612e1110SKent Overstreet 	int ret = darray_make_room_gfp(stack, 32, gfp);
3023ea4219dSKent Overstreet 	if (ret)
3033ea4219dSKent Overstreet 		return ret;
3041148a97fSKent Overstreet 
3053e57db65SKent Overstreet 	if (!down_read_trylock(&task->signal->exec_update_lock))
3063ea4219dSKent Overstreet 		return -1;
3071148a97fSKent Overstreet 
3083ea4219dSKent Overstreet 	do {
309c13fbb7dSKent Overstreet 		nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, skipnr + 1);
3103ea4219dSKent Overstreet 	} while (nr_entries == stack->size &&
3115197728fSKent Overstreet 		 !(ret = darray_make_room_gfp(stack, stack->size * 2, gfp)));
3123ea4219dSKent Overstreet 
3133ea4219dSKent Overstreet 	stack->nr = nr_entries;
3143ea4219dSKent Overstreet 	up_read(&task->signal->exec_update_lock);
3153ea4219dSKent Overstreet 
3163ea4219dSKent Overstreet 	return ret;
317e5570df2SKent Overstreet #else
318e5570df2SKent Overstreet 	return 0;
319e5570df2SKent Overstreet #endif
3201148a97fSKent Overstreet }
3211148a97fSKent Overstreet 
bch2_prt_backtrace(struct printbuf * out,bch_stacktrace * stack)3223ea4219dSKent Overstreet void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
3233ea4219dSKent Overstreet {
3243ea4219dSKent Overstreet 	darray_for_each(*stack, i) {
3253ea4219dSKent Overstreet 		prt_printf(out, "[<0>] %pB", (void *) *i);
3263ea4219dSKent Overstreet 		prt_newline(out);
3273ea4219dSKent Overstreet 	}
3283ea4219dSKent Overstreet }
3293ea4219dSKent Overstreet 
bch2_prt_task_backtrace(struct printbuf * out,struct task_struct * task,unsigned skipnr,gfp_t gfp)330612e1110SKent Overstreet int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task, unsigned skipnr, gfp_t gfp)
3313ea4219dSKent Overstreet {
3323ea4219dSKent Overstreet 	bch_stacktrace stack = { 0 };
333612e1110SKent Overstreet 	int ret = bch2_save_backtrace(&stack, task, skipnr + 1, gfp);
3343ea4219dSKent Overstreet 
3353ea4219dSKent Overstreet 	bch2_prt_backtrace(out, &stack);
3363ea4219dSKent Overstreet 	darray_exit(&stack);
3373ea4219dSKent Overstreet 	return ret;
3381148a97fSKent Overstreet }
3391148a97fSKent Overstreet 
340066a2646SKent Overstreet #ifndef __KERNEL__
341066a2646SKent Overstreet #include <time.h>
bch2_prt_datetime(struct printbuf * out,time64_t sec)342066a2646SKent Overstreet void bch2_prt_datetime(struct printbuf *out, time64_t sec)
343066a2646SKent Overstreet {
344066a2646SKent Overstreet 	time_t t = sec;
345066a2646SKent Overstreet 	char buf[64];
346066a2646SKent Overstreet 	ctime_r(&t, buf);
3475fd24cafSKent Overstreet 	strim(buf);
348066a2646SKent Overstreet 	prt_str(out, buf);
349066a2646SKent Overstreet }
350066a2646SKent Overstreet #else
bch2_prt_datetime(struct printbuf * out,time64_t sec)351066a2646SKent Overstreet void bch2_prt_datetime(struct printbuf *out, time64_t sec)
352066a2646SKent Overstreet {
353066a2646SKent Overstreet 	char buf[64];
354066a2646SKent Overstreet 	snprintf(buf, sizeof(buf), "%ptT", &sec);
355066a2646SKent Overstreet 	prt_u64(out, sec);
356066a2646SKent Overstreet }
357066a2646SKent Overstreet #endif
358066a2646SKent Overstreet 
bch2_pr_time_units(struct printbuf * out,u64 ns)359066a2646SKent Overstreet void bch2_pr_time_units(struct printbuf *out, u64 ns)
360066a2646SKent Overstreet {
361f1ca1abfSKent Overstreet 	const struct time_unit *u = bch2_pick_time_units(ns);
362066a2646SKent Overstreet 
36336f0af4fSFeiko Nanninga 	prt_printf(out, "%llu %s", div64_u64(ns, u->nsecs), u->name);
364066a2646SKent Overstreet }
365066a2646SKent Overstreet 
bch2_pr_time_units_aligned(struct printbuf * out,u64 ns)366bf8f8b20SDaniel Hill static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
367bf8f8b20SDaniel Hill {
368f1ca1abfSKent Overstreet 	const struct time_unit *u = bch2_pick_time_units(ns);
369bf8f8b20SDaniel Hill 
3707423330eSKent Overstreet 	prt_printf(out, "%llu \r%s", div64_u64(ns, u->nsecs), u->name);
371bf8f8b20SDaniel Hill }
372bf8f8b20SDaniel Hill 
pr_name_and_units(struct printbuf * out,const char * name,u64 ns)373bf8f8b20SDaniel Hill static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
374bf8f8b20SDaniel Hill {
3757423330eSKent Overstreet 	prt_printf(out, "%s\t", name);
376bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, ns);
377bf8f8b20SDaniel Hill 	prt_newline(out);
378bf8f8b20SDaniel Hill }
379bf8f8b20SDaniel Hill 
380066a2646SKent Overstreet #define TABSTOP_SIZE 12
381066a2646SKent Overstreet 
bch2_time_stats_to_text(struct printbuf * out,struct bch2_time_stats * stats)3827807e143SKent Overstreet void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
3831c6fdbd8SKent Overstreet {
384273960b8SDarrick J. Wong 	struct quantiles *quantiles = time_stats_to_quantiles(stats);
385bf8f8b20SDaniel Hill 	s64 f_mean = 0, d_mean = 0;
386f1ca1abfSKent Overstreet 	u64 f_stddev = 0, d_stddev = 0;
387066a2646SKent Overstreet 
388066a2646SKent Overstreet 	if (stats->buffer) {
389066a2646SKent Overstreet 		int cpu;
390066a2646SKent Overstreet 
391066a2646SKent Overstreet 		spin_lock_irq(&stats->lock);
392066a2646SKent Overstreet 		for_each_possible_cpu(cpu)
393066a2646SKent Overstreet 			__bch2_time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu));
394066a2646SKent Overstreet 		spin_unlock_irq(&stats->lock);
395066a2646SKent Overstreet 	}
396066a2646SKent Overstreet 
397bf8f8b20SDaniel Hill 	/*
398bf8f8b20SDaniel Hill 	 * avoid divide by zero
399bf8f8b20SDaniel Hill 	 */
400bf8f8b20SDaniel Hill 	if (stats->freq_stats.n) {
401bf8f8b20SDaniel Hill 		f_mean = mean_and_variance_get_mean(stats->freq_stats);
402bf8f8b20SDaniel Hill 		f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
403bf8f8b20SDaniel Hill 		d_mean = mean_and_variance_get_mean(stats->duration_stats);
404bf8f8b20SDaniel Hill 		d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
405bf8f8b20SDaniel Hill 	}
4061c6fdbd8SKent Overstreet 
407bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
4087423330eSKent Overstreet 	prt_printf(out, "count:\t%llu\n", stats->duration_stats.n);
409bf8f8b20SDaniel Hill 	printbuf_tabstop_pop(out);
4101c6fdbd8SKent Overstreet 
411bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
4121c6fdbd8SKent Overstreet 
413bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + 20);
414bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
415bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, 0);
416bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
4171c6fdbd8SKent Overstreet 
4187423330eSKent Overstreet 	prt_printf(out, "\tsince mount\r\trecent\r\n");
419bf8f8b20SDaniel Hill 
420bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
421bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + 20);
422bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE);
423bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, 2);
424bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE);
425bf8f8b20SDaniel Hill 
4267423330eSKent Overstreet 	prt_printf(out, "duration of events\n");
427bf8f8b20SDaniel Hill 	printbuf_indent_add(out, 2);
428bf8f8b20SDaniel Hill 
429bf8f8b20SDaniel Hill 	pr_name_and_units(out, "min:", stats->min_duration);
430bf8f8b20SDaniel Hill 	pr_name_and_units(out, "max:", stats->max_duration);
431066a2646SKent Overstreet 	pr_name_and_units(out, "total:", stats->total_duration);
432bf8f8b20SDaniel Hill 
4337423330eSKent Overstreet 	prt_printf(out, "mean:\t");
434bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, d_mean);
435bf8f8b20SDaniel Hill 	prt_tab(out);
4364b4f0876SDarrick J. Wong 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
437bf8f8b20SDaniel Hill 	prt_newline(out);
438bf8f8b20SDaniel Hill 
4397423330eSKent Overstreet 	prt_printf(out, "stddev:\t");
440bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, d_stddev);
441bf8f8b20SDaniel Hill 	prt_tab(out);
4424b4f0876SDarrick J. Wong 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
443bf8f8b20SDaniel Hill 
444bf8f8b20SDaniel Hill 	printbuf_indent_sub(out, 2);
445bf8f8b20SDaniel Hill 	prt_newline(out);
446bf8f8b20SDaniel Hill 
4477423330eSKent Overstreet 	prt_printf(out, "time between events\n");
448bf8f8b20SDaniel Hill 	printbuf_indent_add(out, 2);
449bf8f8b20SDaniel Hill 
450bf8f8b20SDaniel Hill 	pr_name_and_units(out, "min:", stats->min_freq);
451bf8f8b20SDaniel Hill 	pr_name_and_units(out, "max:", stats->max_freq);
452bf8f8b20SDaniel Hill 
4537423330eSKent Overstreet 	prt_printf(out, "mean:\t");
454bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, f_mean);
455bf8f8b20SDaniel Hill 	prt_tab(out);
4564b4f0876SDarrick J. Wong 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
457bf8f8b20SDaniel Hill 	prt_newline(out);
458bf8f8b20SDaniel Hill 
4597423330eSKent Overstreet 	prt_printf(out, "stddev:\t");
460bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, f_stddev);
461bf8f8b20SDaniel Hill 	prt_tab(out);
4624b4f0876SDarrick J. Wong 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
463bf8f8b20SDaniel Hill 
464bf8f8b20SDaniel Hill 	printbuf_indent_sub(out, 2);
465bf8f8b20SDaniel Hill 	prt_newline(out);
466bf8f8b20SDaniel Hill 
467bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
4681c6fdbd8SKent Overstreet 
469273960b8SDarrick J. Wong 	if (quantiles) {
470f1ca1abfSKent Overstreet 		int i = eytzinger0_first(NR_QUANTILES);
471f1ca1abfSKent Overstreet 		const struct time_unit *u =
472273960b8SDarrick J. Wong 			bch2_pick_time_units(quantiles->entries[i].m);
473f1ca1abfSKent Overstreet 		u64 last_q = 0;
4741c6fdbd8SKent Overstreet 
47525055c69SDaniel Hill 		prt_printf(out, "quantiles (%s):\t", u->name);
4761c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, NR_QUANTILES) {
4771c6fdbd8SKent Overstreet 			bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
4781c6fdbd8SKent Overstreet 
479273960b8SDarrick J. Wong 			u64 q = max(quantiles->entries[i].m, last_q);
48027663d77SReed Riley 			prt_printf(out, "%llu ", div64_u64(q, u->nsecs));
48125055c69SDaniel Hill 			if (is_last)
48225055c69SDaniel Hill 				prt_newline(out);
4831c6fdbd8SKent Overstreet 			last_q = q;
4841c6fdbd8SKent Overstreet 		}
4851c6fdbd8SKent Overstreet 	}
4861c6fdbd8SKent Overstreet }
4871c6fdbd8SKent Overstreet 
4881c6fdbd8SKent Overstreet /* ratelimit: */
4891c6fdbd8SKent Overstreet 
4901c6fdbd8SKent Overstreet /**
4911c6fdbd8SKent Overstreet  * bch2_ratelimit_delay() - return how long to delay until the next time to do
4921c6fdbd8SKent Overstreet  *		some work
49396dea3d5SKent Overstreet  * @d:		the struct bch_ratelimit to update
49496dea3d5SKent Overstreet  * Returns:	the amount of time to delay by, in jiffies
4951c6fdbd8SKent Overstreet  */
bch2_ratelimit_delay(struct bch_ratelimit * d)4961c6fdbd8SKent Overstreet u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
4971c6fdbd8SKent Overstreet {
4981c6fdbd8SKent Overstreet 	u64 now = local_clock();
4991c6fdbd8SKent Overstreet 
5001c6fdbd8SKent Overstreet 	return time_after64(d->next, now)
5011c6fdbd8SKent Overstreet 		? nsecs_to_jiffies(d->next - now)
5021c6fdbd8SKent Overstreet 		: 0;
5031c6fdbd8SKent Overstreet }
5041c6fdbd8SKent Overstreet 
5051c6fdbd8SKent Overstreet /**
5061c6fdbd8SKent Overstreet  * bch2_ratelimit_increment() - increment @d by the amount of work done
50796dea3d5SKent Overstreet  * @d:		the struct bch_ratelimit to update
50896dea3d5SKent Overstreet  * @done:	the amount of work done, in arbitrary units
5091c6fdbd8SKent Overstreet  */
bch2_ratelimit_increment(struct bch_ratelimit * d,u64 done)5101c6fdbd8SKent Overstreet void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
5111c6fdbd8SKent Overstreet {
5121c6fdbd8SKent Overstreet 	u64 now = local_clock();
5131c6fdbd8SKent Overstreet 
5141c6fdbd8SKent Overstreet 	d->next += div_u64(done * NSEC_PER_SEC, d->rate);
5151c6fdbd8SKent Overstreet 
5161c6fdbd8SKent Overstreet 	if (time_before64(now + NSEC_PER_SEC, d->next))
5171c6fdbd8SKent Overstreet 		d->next = now + NSEC_PER_SEC;
5181c6fdbd8SKent Overstreet 
5191c6fdbd8SKent Overstreet 	if (time_after64(now - NSEC_PER_SEC * 2, d->next))
5201c6fdbd8SKent Overstreet 		d->next = now - NSEC_PER_SEC * 2;
5211c6fdbd8SKent Overstreet }
5221c6fdbd8SKent Overstreet 
5231c6fdbd8SKent Overstreet /* pd controller: */
5241c6fdbd8SKent Overstreet 
5251c6fdbd8SKent Overstreet /*
5261c6fdbd8SKent Overstreet  * Updates pd_controller. Attempts to scale inputed values to units per second.
5271c6fdbd8SKent Overstreet  * @target: desired value
5281c6fdbd8SKent Overstreet  * @actual: current value
5291c6fdbd8SKent Overstreet  *
5301c6fdbd8SKent Overstreet  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
5311c6fdbd8SKent Overstreet  * it makes actual go down.
5321c6fdbd8SKent Overstreet  */
bch2_pd_controller_update(struct bch_pd_controller * pd,s64 target,s64 actual,int sign)5331c6fdbd8SKent Overstreet void bch2_pd_controller_update(struct bch_pd_controller *pd,
5341c6fdbd8SKent Overstreet 			      s64 target, s64 actual, int sign)
5351c6fdbd8SKent Overstreet {
5361c6fdbd8SKent Overstreet 	s64 proportional, derivative, change;
5371c6fdbd8SKent Overstreet 
5381c6fdbd8SKent Overstreet 	unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
5391c6fdbd8SKent Overstreet 
5401c6fdbd8SKent Overstreet 	if (seconds_since_update == 0)
5411c6fdbd8SKent Overstreet 		return;
5421c6fdbd8SKent Overstreet 
5431c6fdbd8SKent Overstreet 	pd->last_update = jiffies;
5441c6fdbd8SKent Overstreet 
5451c6fdbd8SKent Overstreet 	proportional = actual - target;
5461c6fdbd8SKent Overstreet 	proportional *= seconds_since_update;
5471c6fdbd8SKent Overstreet 	proportional = div_s64(proportional, pd->p_term_inverse);
5481c6fdbd8SKent Overstreet 
5491c6fdbd8SKent Overstreet 	derivative = actual - pd->last_actual;
5501c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, seconds_since_update);
5511c6fdbd8SKent Overstreet 	derivative = ewma_add(pd->smoothed_derivative, derivative,
5521c6fdbd8SKent Overstreet 			      (pd->d_term / seconds_since_update) ?: 1);
5531c6fdbd8SKent Overstreet 	derivative = derivative * pd->d_term;
5541c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, pd->p_term_inverse);
5551c6fdbd8SKent Overstreet 
5561c6fdbd8SKent Overstreet 	change = proportional + derivative;
5571c6fdbd8SKent Overstreet 
5581c6fdbd8SKent Overstreet 	/* Don't increase rate if not keeping up */
5591c6fdbd8SKent Overstreet 	if (change > 0 &&
5601c6fdbd8SKent Overstreet 	    pd->backpressure &&
5611c6fdbd8SKent Overstreet 	    time_after64(local_clock(),
5621c6fdbd8SKent Overstreet 			 pd->rate.next + NSEC_PER_MSEC))
5631c6fdbd8SKent Overstreet 		change = 0;
5641c6fdbd8SKent Overstreet 
5651c6fdbd8SKent Overstreet 	change *= (sign * -1);
5661c6fdbd8SKent Overstreet 
5671c6fdbd8SKent Overstreet 	pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
5681c6fdbd8SKent Overstreet 				1, UINT_MAX);
5691c6fdbd8SKent Overstreet 
5701c6fdbd8SKent Overstreet 	pd->last_actual		= actual;
5711c6fdbd8SKent Overstreet 	pd->last_derivative	= derivative;
5721c6fdbd8SKent Overstreet 	pd->last_proportional	= proportional;
5731c6fdbd8SKent Overstreet 	pd->last_change		= change;
5741c6fdbd8SKent Overstreet 	pd->last_target		= target;
5751c6fdbd8SKent Overstreet }
5761c6fdbd8SKent Overstreet 
bch2_pd_controller_init(struct bch_pd_controller * pd)5771c6fdbd8SKent Overstreet void bch2_pd_controller_init(struct bch_pd_controller *pd)
5781c6fdbd8SKent Overstreet {
5791c6fdbd8SKent Overstreet 	pd->rate.rate		= 1024;
5801c6fdbd8SKent Overstreet 	pd->last_update		= jiffies;
5811c6fdbd8SKent Overstreet 	pd->p_term_inverse	= 6000;
5821c6fdbd8SKent Overstreet 	pd->d_term		= 30;
5831c6fdbd8SKent Overstreet 	pd->d_smooth		= pd->d_term;
5841c6fdbd8SKent Overstreet 	pd->backpressure	= 1;
5851c6fdbd8SKent Overstreet }
5861c6fdbd8SKent Overstreet 
bch2_pd_controller_debug_to_text(struct printbuf * out,struct bch_pd_controller * pd)5872be7b16eSKent Overstreet void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
5881c6fdbd8SKent Overstreet {
589401ec4dbSKent Overstreet 	if (!out->nr_tabstops)
590401ec4dbSKent Overstreet 		printbuf_tabstop_push(out, 20);
5911c6fdbd8SKent Overstreet 
5927423330eSKent Overstreet 	prt_printf(out, "rate:\t");
593401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->rate.rate);
594401ec4dbSKent Overstreet 	prt_newline(out);
5951c6fdbd8SKent Overstreet 
5967423330eSKent Overstreet 	prt_printf(out, "target:\t");
597401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_target);
598401ec4dbSKent Overstreet 	prt_newline(out);
5991c6fdbd8SKent Overstreet 
6007423330eSKent Overstreet 	prt_printf(out, "actual:\t");
601401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_actual);
602401ec4dbSKent Overstreet 	prt_newline(out);
6032be7b16eSKent Overstreet 
6047423330eSKent Overstreet 	prt_printf(out, "proportional:\t");
605401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_proportional);
606401ec4dbSKent Overstreet 	prt_newline(out);
6072be7b16eSKent Overstreet 
6087423330eSKent Overstreet 	prt_printf(out, "derivative:\t");
609401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_derivative);
610401ec4dbSKent Overstreet 	prt_newline(out);
6112be7b16eSKent Overstreet 
6127423330eSKent Overstreet 	prt_printf(out, "change:\t");
613401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_change);
614401ec4dbSKent Overstreet 	prt_newline(out);
6152be7b16eSKent Overstreet 
6167423330eSKent Overstreet 	prt_printf(out, "next io:\t%llims\n", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
6171c6fdbd8SKent Overstreet }
6181c6fdbd8SKent Overstreet 
6191c6fdbd8SKent Overstreet /* misc: */
6201c6fdbd8SKent Overstreet 
bch2_bio_map(struct bio * bio,void * base,size_t size)621885678f6SKent Overstreet void bch2_bio_map(struct bio *bio, void *base, size_t size)
6221c6fdbd8SKent Overstreet {
623885678f6SKent Overstreet 	while (size) {
624885678f6SKent Overstreet 		struct page *page = is_vmalloc_addr(base)
6251c6fdbd8SKent Overstreet 				? vmalloc_to_page(base)
6261c6fdbd8SKent Overstreet 				: virt_to_page(base);
627885678f6SKent Overstreet 		unsigned offset = offset_in_page(base);
628885678f6SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
6291c6fdbd8SKent Overstreet 
630885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, len, offset));
631885678f6SKent Overstreet 		size -= len;
632885678f6SKent Overstreet 		base += len;
6331c6fdbd8SKent Overstreet 	}
6341c6fdbd8SKent Overstreet }
6351c6fdbd8SKent Overstreet 
bch2_bio_alloc_pages(struct bio * bio,size_t size,gfp_t gfp_mask)6361c6fdbd8SKent Overstreet int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
6371c6fdbd8SKent Overstreet {
6381c6fdbd8SKent Overstreet 	while (size) {
6391c6fdbd8SKent Overstreet 		struct page *page = alloc_pages(gfp_mask, 0);
6401c6fdbd8SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE, size);
6411c6fdbd8SKent Overstreet 
6421c6fdbd8SKent Overstreet 		if (!page)
6431c6fdbd8SKent Overstreet 			return -ENOMEM;
6441c6fdbd8SKent Overstreet 
6451c6fdbd8SKent Overstreet 		if (unlikely(!bio_add_page(bio, page, len, 0))) {
6461c6fdbd8SKent Overstreet 			__free_page(page);
6471c6fdbd8SKent Overstreet 			break;
6481c6fdbd8SKent Overstreet 		}
6491c6fdbd8SKent Overstreet 
6501c6fdbd8SKent Overstreet 		size -= len;
6511c6fdbd8SKent Overstreet 	}
6521c6fdbd8SKent Overstreet 
6531c6fdbd8SKent Overstreet 	return 0;
6541c6fdbd8SKent Overstreet }
6551c6fdbd8SKent Overstreet 
bch2_rand_range(size_t max)6561c6fdbd8SKent Overstreet size_t bch2_rand_range(size_t max)
6571c6fdbd8SKent Overstreet {
6581c6fdbd8SKent Overstreet 	size_t rand;
6591c6fdbd8SKent Overstreet 
6601c6fdbd8SKent Overstreet 	if (!max)
6611c6fdbd8SKent Overstreet 		return 0;
6621c6fdbd8SKent Overstreet 
6631c6fdbd8SKent Overstreet 	do {
6641c6fdbd8SKent Overstreet 		rand = get_random_long();
6651c6fdbd8SKent Overstreet 		rand &= roundup_pow_of_two(max) - 1;
6661c6fdbd8SKent Overstreet 	} while (rand >= max);
6671c6fdbd8SKent Overstreet 
6681c6fdbd8SKent Overstreet 	return rand;
6691c6fdbd8SKent Overstreet }
6701c6fdbd8SKent Overstreet 
memcpy_to_bio(struct bio * dst,struct bvec_iter dst_iter,const void * src)67103c8c747SKent Overstreet void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
6721c6fdbd8SKent Overstreet {
6731c6fdbd8SKent Overstreet 	struct bio_vec bv;
6741c6fdbd8SKent Overstreet 	struct bvec_iter iter;
6751c6fdbd8SKent Overstreet 
6761c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, dst, iter, dst_iter) {
6771e81f89bSKent Overstreet 		void *dstp = kmap_local_page(bv.bv_page);
6781e81f89bSKent Overstreet 
6791c6fdbd8SKent Overstreet 		memcpy(dstp + bv.bv_offset, src, bv.bv_len);
6801e81f89bSKent Overstreet 		kunmap_local(dstp);
6811c6fdbd8SKent Overstreet 
6821c6fdbd8SKent Overstreet 		src += bv.bv_len;
6831c6fdbd8SKent Overstreet 	}
6841c6fdbd8SKent Overstreet }
6851c6fdbd8SKent Overstreet 
memcpy_from_bio(void * dst,struct bio * src,struct bvec_iter src_iter)6861c6fdbd8SKent Overstreet void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
6871c6fdbd8SKent Overstreet {
6881c6fdbd8SKent Overstreet 	struct bio_vec bv;
6891c6fdbd8SKent Overstreet 	struct bvec_iter iter;
6901c6fdbd8SKent Overstreet 
6911c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, src, iter, src_iter) {
6921e81f89bSKent Overstreet 		void *srcp = kmap_local_page(bv.bv_page);
6931e81f89bSKent Overstreet 
6941c6fdbd8SKent Overstreet 		memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
6951e81f89bSKent Overstreet 		kunmap_local(srcp);
6961c6fdbd8SKent Overstreet 
6971c6fdbd8SKent Overstreet 		dst += bv.bv_len;
6981c6fdbd8SKent Overstreet 	}
6991c6fdbd8SKent Overstreet }
7001c6fdbd8SKent Overstreet 
7011c6fdbd8SKent Overstreet #if 0
7021c6fdbd8SKent Overstreet void eytzinger1_test(void)
7031c6fdbd8SKent Overstreet {
7041c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
7051c6fdbd8SKent Overstreet 
7061c6fdbd8SKent Overstreet 	pr_info("1 based eytzinger test:");
7071c6fdbd8SKent Overstreet 
7081c6fdbd8SKent Overstreet 	for (size = 2;
7091c6fdbd8SKent Overstreet 	     size < 65536;
7101c6fdbd8SKent Overstreet 	     size++) {
7111c6fdbd8SKent Overstreet 		unsigned extra = eytzinger1_extra(size);
7121c6fdbd8SKent Overstreet 
7131c6fdbd8SKent Overstreet 		if (!(size % 4096))
7141c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
7151c6fdbd8SKent Overstreet 
7161c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
7171c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
7181c6fdbd8SKent Overstreet 
7191c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)	!= 0);
7201c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(eytzinger1_last(size), size)	!= 0);
7211c6fdbd8SKent Overstreet 
7221c6fdbd8SKent Overstreet 		inorder = 1;
7231c6fdbd8SKent Overstreet 		eytzinger1_for_each(eytz, size) {
7241c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
7251c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
7261c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger1_last(size) &&
7271c6fdbd8SKent Overstreet 			       eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
7281c6fdbd8SKent Overstreet 
7291c6fdbd8SKent Overstreet 			inorder++;
7301c6fdbd8SKent Overstreet 		}
7311c6fdbd8SKent Overstreet 	}
7321c6fdbd8SKent Overstreet }
7331c6fdbd8SKent Overstreet 
7341c6fdbd8SKent Overstreet void eytzinger0_test(void)
7351c6fdbd8SKent Overstreet {
7361c6fdbd8SKent Overstreet 
7371c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
7381c6fdbd8SKent Overstreet 
7391c6fdbd8SKent Overstreet 	pr_info("0 based eytzinger test:");
7401c6fdbd8SKent Overstreet 
7411c6fdbd8SKent Overstreet 	for (size = 1;
7421c6fdbd8SKent Overstreet 	     size < 65536;
7431c6fdbd8SKent Overstreet 	     size++) {
7441c6fdbd8SKent Overstreet 		unsigned extra = eytzinger0_extra(size);
7451c6fdbd8SKent Overstreet 
7461c6fdbd8SKent Overstreet 		if (!(size % 4096))
7471c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
7481c6fdbd8SKent Overstreet 
7491c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
7501c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
7511c6fdbd8SKent Overstreet 
7521c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)	!= -1);
7531c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(eytzinger0_last(size), size)	!= -1);
7541c6fdbd8SKent Overstreet 
7551c6fdbd8SKent Overstreet 		inorder = 0;
7561c6fdbd8SKent Overstreet 		eytzinger0_for_each(eytz, size) {
7571c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
7581c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
7591c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger0_last(size) &&
7601c6fdbd8SKent Overstreet 			       eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
7611c6fdbd8SKent Overstreet 
7621c6fdbd8SKent Overstreet 			inorder++;
7631c6fdbd8SKent Overstreet 		}
7641c6fdbd8SKent Overstreet 	}
7651c6fdbd8SKent Overstreet }
7661c6fdbd8SKent Overstreet 
7671c6fdbd8SKent Overstreet static inline int cmp_u16(const void *_l, const void *_r, size_t size)
7681c6fdbd8SKent Overstreet {
7691c6fdbd8SKent Overstreet 	const u16 *l = _l, *r = _r;
7701c6fdbd8SKent Overstreet 
7711c6fdbd8SKent Overstreet 	return (*l > *r) - (*r - *l);
7721c6fdbd8SKent Overstreet }
7731c6fdbd8SKent Overstreet 
7741c6fdbd8SKent Overstreet static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
7751c6fdbd8SKent Overstreet {
7761c6fdbd8SKent Overstreet 	int i, c1 = -1, c2 = -1;
7771c6fdbd8SKent Overstreet 	ssize_t r;
7781c6fdbd8SKent Overstreet 
7791c6fdbd8SKent Overstreet 	r = eytzinger0_find_le(test_array, nr,
7801c6fdbd8SKent Overstreet 			       sizeof(test_array[0]),
7811c6fdbd8SKent Overstreet 			       cmp_u16, &search);
7821c6fdbd8SKent Overstreet 	if (r >= 0)
7831c6fdbd8SKent Overstreet 		c1 = test_array[r];
7841c6fdbd8SKent Overstreet 
7851c6fdbd8SKent Overstreet 	for (i = 0; i < nr; i++)
7861c6fdbd8SKent Overstreet 		if (test_array[i] <= search && test_array[i] > c2)
7871c6fdbd8SKent Overstreet 			c2 = test_array[i];
7881c6fdbd8SKent Overstreet 
7891c6fdbd8SKent Overstreet 	if (c1 != c2) {
7901c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
7911c6fdbd8SKent Overstreet 			pr_info("[%3u] = %12u", i, test_array[i]);
7921c6fdbd8SKent Overstreet 		pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
7931c6fdbd8SKent Overstreet 			i, r, c1, c2);
7941c6fdbd8SKent Overstreet 	}
7951c6fdbd8SKent Overstreet }
7961c6fdbd8SKent Overstreet 
7971c6fdbd8SKent Overstreet void eytzinger0_find_test(void)
7981c6fdbd8SKent Overstreet {
7991c6fdbd8SKent Overstreet 	unsigned i, nr, allocated = 1 << 12;
8001c6fdbd8SKent Overstreet 	u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
8011c6fdbd8SKent Overstreet 
8021c6fdbd8SKent Overstreet 	for (nr = 1; nr < allocated; nr++) {
8031c6fdbd8SKent Overstreet 		pr_info("testing %u elems", nr);
8041c6fdbd8SKent Overstreet 
8051c6fdbd8SKent Overstreet 		get_random_bytes(test_array, nr * sizeof(test_array[0]));
8061c6fdbd8SKent Overstreet 		eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
8071c6fdbd8SKent Overstreet 
8081c6fdbd8SKent Overstreet 		/* verify array is sorted correctly: */
8091c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
8101c6fdbd8SKent Overstreet 			BUG_ON(i != eytzinger0_last(nr) &&
8111c6fdbd8SKent Overstreet 			       test_array[i] > test_array[eytzinger0_next(i, nr)]);
8121c6fdbd8SKent Overstreet 
8131c6fdbd8SKent Overstreet 		for (i = 0; i < U16_MAX; i += 1 << 12)
8141c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, i);
8151c6fdbd8SKent Overstreet 
8161c6fdbd8SKent Overstreet 		for (i = 0; i < nr; i++) {
8171c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
8181c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i]);
8191c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
8201c6fdbd8SKent Overstreet 		}
8211c6fdbd8SKent Overstreet 	}
8221c6fdbd8SKent Overstreet 
8231c6fdbd8SKent Overstreet 	kfree(test_array);
8241c6fdbd8SKent Overstreet }
8251c6fdbd8SKent Overstreet #endif
8267ef2a73aSKent Overstreet 
8277ef2a73aSKent Overstreet /*
8287ef2a73aSKent Overstreet  * Accumulate percpu counters onto one cpu's copy - only valid when access
8297ef2a73aSKent Overstreet  * against any percpu counter is guarded against
8307ef2a73aSKent Overstreet  */
bch2_acc_percpu_u64s(u64 __percpu * p,unsigned nr)8317ef2a73aSKent Overstreet u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
8327ef2a73aSKent Overstreet {
833107fe5afSKent Overstreet 	u64 *ret;
8347ef2a73aSKent Overstreet 	int cpu;
8357ef2a73aSKent Overstreet 
836107fe5afSKent Overstreet 	/* access to pcpu vars has to be blocked by other locking */
837107fe5afSKent Overstreet 	preempt_disable();
838107fe5afSKent Overstreet 	ret = this_cpu_ptr(p);
839107fe5afSKent Overstreet 	preempt_enable();
840107fe5afSKent Overstreet 
8417ef2a73aSKent Overstreet 	for_each_possible_cpu(cpu) {
8427ef2a73aSKent Overstreet 		u64 *i = per_cpu_ptr(p, cpu);
8437ef2a73aSKent Overstreet 
8447ef2a73aSKent Overstreet 		if (i != ret) {
8457ef2a73aSKent Overstreet 			acc_u64s(ret, i, nr);
8467ef2a73aSKent Overstreet 			memset(i, 0, nr * sizeof(u64));
8477ef2a73aSKent Overstreet 		}
8487ef2a73aSKent Overstreet 	}
8497ef2a73aSKent Overstreet 
8507ef2a73aSKent Overstreet 	return ret;
8517ef2a73aSKent Overstreet }
852806ebf2aSKent Overstreet 
bch2_darray_str_exit(darray_str * d)853806ebf2aSKent Overstreet void bch2_darray_str_exit(darray_str *d)
854806ebf2aSKent Overstreet {
855806ebf2aSKent Overstreet 	darray_for_each(*d, i)
856806ebf2aSKent Overstreet 		kfree(*i);
857806ebf2aSKent Overstreet 	darray_exit(d);
858806ebf2aSKent Overstreet }
859806ebf2aSKent Overstreet 
bch2_split_devs(const char * _dev_name,darray_str * ret)860806ebf2aSKent Overstreet int bch2_split_devs(const char *_dev_name, darray_str *ret)
861806ebf2aSKent Overstreet {
862806ebf2aSKent Overstreet 	darray_init(ret);
863806ebf2aSKent Overstreet 
864e240c1b3SSu Yue 	char *dev_name, *s, *orig;
865e240c1b3SSu Yue 
866e240c1b3SSu Yue 	dev_name = orig = kstrdup(_dev_name, GFP_KERNEL);
867806ebf2aSKent Overstreet 	if (!dev_name)
868806ebf2aSKent Overstreet 		return -ENOMEM;
869806ebf2aSKent Overstreet 
870806ebf2aSKent Overstreet 	while ((s = strsep(&dev_name, ":"))) {
871806ebf2aSKent Overstreet 		char *p = kstrdup(s, GFP_KERNEL);
872806ebf2aSKent Overstreet 		if (!p)
873806ebf2aSKent Overstreet 			goto err;
874806ebf2aSKent Overstreet 
875806ebf2aSKent Overstreet 		if (darray_push(ret, p)) {
876806ebf2aSKent Overstreet 			kfree(p);
877806ebf2aSKent Overstreet 			goto err;
878806ebf2aSKent Overstreet 		}
879806ebf2aSKent Overstreet 	}
880806ebf2aSKent Overstreet 
881e240c1b3SSu Yue 	kfree(orig);
882806ebf2aSKent Overstreet 	return 0;
883806ebf2aSKent Overstreet err:
884806ebf2aSKent Overstreet 	bch2_darray_str_exit(ret);
885e240c1b3SSu Yue 	kfree(orig);
886806ebf2aSKent Overstreet 	return -ENOMEM;
887806ebf2aSKent Overstreet }
888