18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * random utiility code, for bcache but in theory not specific to bcache
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
68c2ecf20Sopenharmony_ci * Copyright 2012 Google, Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/bio.h>
108c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
118c2ecf20Sopenharmony_ci#include <linux/ctype.h>
128c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/sched/clock.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "util.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define simple_strtoint(c, end, base)	simple_strtol(c, end, base)
218c2ecf20Sopenharmony_ci#define simple_strtouint(c, end, base)	simple_strtoul(c, end, base)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define STRTO_H(name, type)					\
248c2ecf20Sopenharmony_ciint bch_ ## name ## _h(const char *cp, type *res)		\
258c2ecf20Sopenharmony_ci{								\
268c2ecf20Sopenharmony_ci	int u = 0;						\
278c2ecf20Sopenharmony_ci	char *e;						\
288c2ecf20Sopenharmony_ci	type i = simple_ ## name(cp, &e, 10);			\
298c2ecf20Sopenharmony_ci								\
308c2ecf20Sopenharmony_ci	switch (tolower(*e)) {					\
318c2ecf20Sopenharmony_ci	default:						\
328c2ecf20Sopenharmony_ci		return -EINVAL;					\
338c2ecf20Sopenharmony_ci	case 'y':						\
348c2ecf20Sopenharmony_ci	case 'z':						\
358c2ecf20Sopenharmony_ci		u++;						\
368c2ecf20Sopenharmony_ci		fallthrough;					\
378c2ecf20Sopenharmony_ci	case 'e':						\
388c2ecf20Sopenharmony_ci		u++;						\
398c2ecf20Sopenharmony_ci		fallthrough;					\
408c2ecf20Sopenharmony_ci	case 'p':						\
418c2ecf20Sopenharmony_ci		u++;						\
428c2ecf20Sopenharmony_ci		fallthrough;					\
438c2ecf20Sopenharmony_ci	case 't':						\
448c2ecf20Sopenharmony_ci		u++;						\
458c2ecf20Sopenharmony_ci		fallthrough;					\
468c2ecf20Sopenharmony_ci	case 'g':						\
478c2ecf20Sopenharmony_ci		u++;						\
488c2ecf20Sopenharmony_ci		fallthrough;					\
498c2ecf20Sopenharmony_ci	case 'm':						\
508c2ecf20Sopenharmony_ci		u++;						\
518c2ecf20Sopenharmony_ci		fallthrough;					\
528c2ecf20Sopenharmony_ci	case 'k':						\
538c2ecf20Sopenharmony_ci		u++;						\
548c2ecf20Sopenharmony_ci		if (e++ == cp)					\
558c2ecf20Sopenharmony_ci			return -EINVAL;				\
568c2ecf20Sopenharmony_ci		fallthrough;					\
578c2ecf20Sopenharmony_ci	case '\n':						\
588c2ecf20Sopenharmony_ci	case '\0':						\
598c2ecf20Sopenharmony_ci		if (*e == '\n')					\
608c2ecf20Sopenharmony_ci			e++;					\
618c2ecf20Sopenharmony_ci	}							\
628c2ecf20Sopenharmony_ci								\
638c2ecf20Sopenharmony_ci	if (*e)							\
648c2ecf20Sopenharmony_ci		return -EINVAL;					\
658c2ecf20Sopenharmony_ci								\
668c2ecf20Sopenharmony_ci	while (u--) {						\
678c2ecf20Sopenharmony_ci		if ((type) ~0 > 0 &&				\
688c2ecf20Sopenharmony_ci		    (type) ~0 / 1024 <= i)			\
698c2ecf20Sopenharmony_ci			return -EINVAL;				\
708c2ecf20Sopenharmony_ci		if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) ||	\
718c2ecf20Sopenharmony_ci		    (i < 0 && -ANYSINT_MAX(type) / 1024 > i))	\
728c2ecf20Sopenharmony_ci			return -EINVAL;				\
738c2ecf20Sopenharmony_ci		i *= 1024;					\
748c2ecf20Sopenharmony_ci	}							\
758c2ecf20Sopenharmony_ci								\
768c2ecf20Sopenharmony_ci	*res = i;						\
778c2ecf20Sopenharmony_ci	return 0;						\
788c2ecf20Sopenharmony_ci}								\
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciSTRTO_H(strtoint, int)
818c2ecf20Sopenharmony_ciSTRTO_H(strtouint, unsigned int)
828c2ecf20Sopenharmony_ciSTRTO_H(strtoll, long long)
838c2ecf20Sopenharmony_ciSTRTO_H(strtoull, unsigned long long)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * bch_hprint - formats @v to human readable string for sysfs.
878c2ecf20Sopenharmony_ci * @buf: the (at least 8 byte) buffer to format the result into.
888c2ecf20Sopenharmony_ci * @v: signed 64 bit integer
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * Returns the number of bytes used by format.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cissize_t bch_hprint(char *buf, int64_t v)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	static const char units[] = "?kMGTPEZY";
958c2ecf20Sopenharmony_ci	int u = 0, t;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	uint64_t q;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (v < 0)
1008c2ecf20Sopenharmony_ci		q = -v;
1018c2ecf20Sopenharmony_ci	else
1028c2ecf20Sopenharmony_ci		q = v;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* For as long as the number is more than 3 digits, but at least
1058c2ecf20Sopenharmony_ci	 * once, shift right / divide by 1024.  Keep the remainder for
1068c2ecf20Sopenharmony_ci	 * a digit after the decimal point.
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	do {
1098c2ecf20Sopenharmony_ci		u++;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		t = q & ~(~0 << 10);
1128c2ecf20Sopenharmony_ci		q >>= 10;
1138c2ecf20Sopenharmony_ci	} while (q >= 1000);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (v < 0)
1168c2ecf20Sopenharmony_ci		/* '-', up to 3 digits, '.', 1 digit, 1 character, null;
1178c2ecf20Sopenharmony_ci		 * yields 8 bytes.
1188c2ecf20Sopenharmony_ci		 */
1198c2ecf20Sopenharmony_ci		return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
1208c2ecf20Sopenharmony_ci	else
1218c2ecf20Sopenharmony_ci		return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cibool bch_is_zero(const char *p, size_t n)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	size_t i;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
1298c2ecf20Sopenharmony_ci		if (p[i])
1308c2ecf20Sopenharmony_ci			return false;
1318c2ecf20Sopenharmony_ci	return true;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint bch_parse_uuid(const char *s, char *uuid)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	size_t i, j, x;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	memset(uuid, 0, 16);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	for (i = 0, j = 0;
1418c2ecf20Sopenharmony_ci	     i < strspn(s, "-0123456789:ABCDEFabcdef") && j < 32;
1428c2ecf20Sopenharmony_ci	     i++) {
1438c2ecf20Sopenharmony_ci		x = s[i] | 32;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		switch (x) {
1468c2ecf20Sopenharmony_ci		case '0'...'9':
1478c2ecf20Sopenharmony_ci			x -= '0';
1488c2ecf20Sopenharmony_ci			break;
1498c2ecf20Sopenharmony_ci		case 'a'...'f':
1508c2ecf20Sopenharmony_ci			x -= 'a' - 10;
1518c2ecf20Sopenharmony_ci			break;
1528c2ecf20Sopenharmony_ci		default:
1538c2ecf20Sopenharmony_ci			continue;
1548c2ecf20Sopenharmony_ci		}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		if (!(j & 1))
1578c2ecf20Sopenharmony_ci			x <<= 4;
1588c2ecf20Sopenharmony_ci		uuid[j++ >> 1] |= x;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci	return i;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_civoid bch_time_stats_update(struct time_stats *stats, uint64_t start_time)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	uint64_t now, duration, last;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	spin_lock(&stats->lock);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	now		= local_clock();
1708c2ecf20Sopenharmony_ci	duration	= time_after64(now, start_time)
1718c2ecf20Sopenharmony_ci		? now - start_time : 0;
1728c2ecf20Sopenharmony_ci	last		= time_after64(now, stats->last)
1738c2ecf20Sopenharmony_ci		? now - stats->last : 0;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	stats->max_duration = max(stats->max_duration, duration);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (stats->last) {
1788c2ecf20Sopenharmony_ci		ewma_add(stats->average_duration, duration, 8, 8);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		if (stats->average_frequency)
1818c2ecf20Sopenharmony_ci			ewma_add(stats->average_frequency, last, 8, 8);
1828c2ecf20Sopenharmony_ci		else
1838c2ecf20Sopenharmony_ci			stats->average_frequency  = last << 8;
1848c2ecf20Sopenharmony_ci	} else {
1858c2ecf20Sopenharmony_ci		stats->average_duration  = duration << 8;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	stats->last = now ?: 1;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	spin_unlock(&stats->lock);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/**
1948c2ecf20Sopenharmony_ci * bch_next_delay() - update ratelimiting statistics and calculate next delay
1958c2ecf20Sopenharmony_ci * @d: the struct bch_ratelimit to update
1968c2ecf20Sopenharmony_ci * @done: the amount of work done, in arbitrary units
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * Increment @d by the amount of work done, and return how long to delay in
1998c2ecf20Sopenharmony_ci * jiffies until the next time to do some work.
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_ciuint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	uint64_t now = local_clock();
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	d->next += div_u64(done * NSEC_PER_SEC, atomic_long_read(&d->rate));
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* Bound the time.  Don't let us fall further than 2 seconds behind
2088c2ecf20Sopenharmony_ci	 * (this prevents unnecessary backlog that would make it impossible
2098c2ecf20Sopenharmony_ci	 * to catch up).  If we're ahead of the desired writeback rate,
2108c2ecf20Sopenharmony_ci	 * don't let us sleep more than 2.5 seconds (so we can notice/respond
2118c2ecf20Sopenharmony_ci	 * if the control system tells us to speed up!).
2128c2ecf20Sopenharmony_ci	 */
2138c2ecf20Sopenharmony_ci	if (time_before64(now + NSEC_PER_SEC * 5LLU / 2LLU, d->next))
2148c2ecf20Sopenharmony_ci		d->next = now + NSEC_PER_SEC * 5LLU / 2LLU;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (time_after64(now - NSEC_PER_SEC * 2, d->next))
2178c2ecf20Sopenharmony_ci		d->next = now - NSEC_PER_SEC * 2;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return time_after64(d->next, now)
2208c2ecf20Sopenharmony_ci		? div_u64(d->next - now, NSEC_PER_SEC / HZ)
2218c2ecf20Sopenharmony_ci		: 0;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * Generally it isn't good to access .bi_io_vec and .bi_vcnt directly,
2268c2ecf20Sopenharmony_ci * the preferred way is bio_add_page, but in this case, bch_bio_map()
2278c2ecf20Sopenharmony_ci * supposes that the bvec table is empty, so it is safe to access
2288c2ecf20Sopenharmony_ci * .bi_vcnt & .bi_io_vec in this way even after multipage bvec is
2298c2ecf20Sopenharmony_ci * supported.
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_civoid bch_bio_map(struct bio *bio, void *base)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	size_t size = bio->bi_iter.bi_size;
2348c2ecf20Sopenharmony_ci	struct bio_vec *bv = bio->bi_io_vec;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	BUG_ON(!bio->bi_iter.bi_size);
2378c2ecf20Sopenharmony_ci	BUG_ON(bio->bi_vcnt);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	bv->bv_offset = base ? offset_in_page(base) : 0;
2408c2ecf20Sopenharmony_ci	goto start;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	for (; size; bio->bi_vcnt++, bv++) {
2438c2ecf20Sopenharmony_ci		bv->bv_offset	= 0;
2448c2ecf20Sopenharmony_cistart:		bv->bv_len	= min_t(size_t, PAGE_SIZE - bv->bv_offset,
2458c2ecf20Sopenharmony_ci					size);
2468c2ecf20Sopenharmony_ci		if (base) {
2478c2ecf20Sopenharmony_ci			bv->bv_page = is_vmalloc_addr(base)
2488c2ecf20Sopenharmony_ci				? vmalloc_to_page(base)
2498c2ecf20Sopenharmony_ci				: virt_to_page(base);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci			base += bv->bv_len;
2528c2ecf20Sopenharmony_ci		}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		size -= bv->bv_len;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/**
2598c2ecf20Sopenharmony_ci * bch_bio_alloc_pages - allocates a single page for each bvec in a bio
2608c2ecf20Sopenharmony_ci * @bio: bio to allocate pages for
2618c2ecf20Sopenharmony_ci * @gfp_mask: flags for allocation
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci * Allocates pages up to @bio->bi_vcnt.
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
2668c2ecf20Sopenharmony_ci * freed.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_ciint bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	int i;
2718c2ecf20Sopenharmony_ci	struct bio_vec *bv;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/*
2748c2ecf20Sopenharmony_ci	 * This is called on freshly new bio, so it is safe to access the
2758c2ecf20Sopenharmony_ci	 * bvec table directly.
2768c2ecf20Sopenharmony_ci	 */
2778c2ecf20Sopenharmony_ci	for (i = 0, bv = bio->bi_io_vec; i < bio->bi_vcnt; bv++, i++) {
2788c2ecf20Sopenharmony_ci		bv->bv_page = alloc_page(gfp_mask);
2798c2ecf20Sopenharmony_ci		if (!bv->bv_page) {
2808c2ecf20Sopenharmony_ci			while (--bv >= bio->bi_io_vec)
2818c2ecf20Sopenharmony_ci				__free_page(bv->bv_page);
2828c2ecf20Sopenharmony_ci			return -ENOMEM;
2838c2ecf20Sopenharmony_ci		}
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return 0;
2878c2ecf20Sopenharmony_ci}
288