187418ef9SColy Li // SPDX-License-Identifier: GPL-2.0
2cafe5635SKent Overstreet /*
3cafe5635SKent Overstreet * random utiility code, for bcache but in theory not specific to bcache
4cafe5635SKent Overstreet *
5cafe5635SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6cafe5635SKent Overstreet * Copyright 2012 Google, Inc.
7cafe5635SKent Overstreet */
8cafe5635SKent Overstreet
9cafe5635SKent Overstreet #include <linux/bio.h>
10cafe5635SKent Overstreet #include <linux/blkdev.h>
11cafe5635SKent Overstreet #include <linux/ctype.h>
12cafe5635SKent Overstreet #include <linux/debugfs.h>
13cafe5635SKent Overstreet #include <linux/module.h>
14cafe5635SKent Overstreet #include <linux/seq_file.h>
15cafe5635SKent Overstreet #include <linux/types.h>
16e6017571SIngo Molnar #include <linux/sched/clock.h>
17cafe5635SKent Overstreet
18cafe5635SKent Overstreet #include "util.h"
19cafe5635SKent Overstreet
20cafe5635SKent Overstreet #define simple_strtoint(c, end, base) simple_strtol(c, end, base)
21cafe5635SKent Overstreet #define simple_strtouint(c, end, base) simple_strtoul(c, end, base)
22cafe5635SKent Overstreet
23cafe5635SKent Overstreet #define STRTO_H(name, type) \
24169ef1cfSKent Overstreet int bch_ ## name ## _h(const char *cp, type *res) \
25cafe5635SKent Overstreet { \
26cafe5635SKent Overstreet int u = 0; \
27cafe5635SKent Overstreet char *e; \
28cafe5635SKent Overstreet type i = simple_ ## name(cp, &e, 10); \
29cafe5635SKent Overstreet \
30cafe5635SKent Overstreet switch (tolower(*e)) { \
31cafe5635SKent Overstreet default: \
32cafe5635SKent Overstreet return -EINVAL; \
33cafe5635SKent Overstreet case 'y': \
34cafe5635SKent Overstreet case 'z': \
35cafe5635SKent Overstreet u++; \
36*df561f66SGustavo A. R. Silva fallthrough; \
37cafe5635SKent Overstreet case 'e': \
38cafe5635SKent Overstreet u++; \
39*df561f66SGustavo A. R. Silva fallthrough; \
40cafe5635SKent Overstreet case 'p': \
41cafe5635SKent Overstreet u++; \
42*df561f66SGustavo A. R. Silva fallthrough; \
43cafe5635SKent Overstreet case 't': \
44cafe5635SKent Overstreet u++; \
45*df561f66SGustavo A. R. Silva fallthrough; \
46cafe5635SKent Overstreet case 'g': \
47cafe5635SKent Overstreet u++; \
48*df561f66SGustavo A. R. Silva fallthrough; \
49cafe5635SKent Overstreet case 'm': \
50cafe5635SKent Overstreet u++; \
51*df561f66SGustavo A. R. Silva fallthrough; \
52cafe5635SKent Overstreet case 'k': \
53cafe5635SKent Overstreet u++; \
54cafe5635SKent Overstreet if (e++ == cp) \
55cafe5635SKent Overstreet return -EINVAL; \
56*df561f66SGustavo A. R. Silva fallthrough; \
57cafe5635SKent Overstreet case '\n': \
58cafe5635SKent Overstreet case '\0': \
59cafe5635SKent Overstreet if (*e == '\n') \
60cafe5635SKent Overstreet e++; \
61cafe5635SKent Overstreet } \
62cafe5635SKent Overstreet \
63cafe5635SKent Overstreet if (*e) \
64cafe5635SKent Overstreet return -EINVAL; \
65cafe5635SKent Overstreet \
66cafe5635SKent Overstreet while (u--) { \
67cafe5635SKent Overstreet if ((type) ~0 > 0 && \
68cafe5635SKent Overstreet (type) ~0 / 1024 <= i) \
69cafe5635SKent Overstreet return -EINVAL; \
70cafe5635SKent Overstreet if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) || \
71cafe5635SKent Overstreet (i < 0 && -ANYSINT_MAX(type) / 1024 > i)) \
72cafe5635SKent Overstreet return -EINVAL; \
73cafe5635SKent Overstreet i *= 1024; \
74cafe5635SKent Overstreet } \
75cafe5635SKent Overstreet \
76cafe5635SKent Overstreet *res = i; \
77cafe5635SKent Overstreet return 0; \
78cafe5635SKent Overstreet } \
79cafe5635SKent Overstreet
STRTO_H(strtoint,int)80cafe5635SKent Overstreet STRTO_H(strtoint, int)
81cafe5635SKent Overstreet STRTO_H(strtouint, unsigned int)
82cafe5635SKent Overstreet STRTO_H(strtoll, long long)
83cafe5635SKent Overstreet STRTO_H(strtoull, unsigned long long)
84cafe5635SKent Overstreet
859276717bSMichael Lyle /**
8647344e33SBart Van Assche * bch_hprint - formats @v to human readable string for sysfs.
8747344e33SBart Van Assche * @buf: the (at least 8 byte) buffer to format the result into.
8847344e33SBart Van Assche * @v: signed 64 bit integer
899276717bSMichael Lyle *
909276717bSMichael Lyle * Returns the number of bytes used by format.
919276717bSMichael Lyle */
92169ef1cfSKent Overstreet ssize_t bch_hprint(char *buf, int64_t v)
93cafe5635SKent Overstreet {
94cafe5635SKent Overstreet static const char units[] = "?kMGTPEZY";
959276717bSMichael Lyle int u = 0, t;
96cafe5635SKent Overstreet
979276717bSMichael Lyle uint64_t q;
98cafe5635SKent Overstreet
999276717bSMichael Lyle if (v < 0)
1009276717bSMichael Lyle q = -v;
1019276717bSMichael Lyle else
1029276717bSMichael Lyle q = v;
103cafe5635SKent Overstreet
1049276717bSMichael Lyle /* For as long as the number is more than 3 digits, but at least
1059276717bSMichael Lyle * once, shift right / divide by 1024. Keep the remainder for
1069276717bSMichael Lyle * a digit after the decimal point.
1079276717bSMichael Lyle */
1089276717bSMichael Lyle do {
1099276717bSMichael Lyle u++;
110cafe5635SKent Overstreet
1119276717bSMichael Lyle t = q & ~(~0 << 10);
1129276717bSMichael Lyle q >>= 10;
1139276717bSMichael Lyle } while (q >= 1000);
1149276717bSMichael Lyle
1159276717bSMichael Lyle if (v < 0)
1169276717bSMichael Lyle /* '-', up to 3 digits, '.', 1 digit, 1 character, null;
1179276717bSMichael Lyle * yields 8 bytes.
1189276717bSMichael Lyle */
1199276717bSMichael Lyle return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
1209276717bSMichael Lyle else
1219276717bSMichael Lyle return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
122cafe5635SKent Overstreet }
123cafe5635SKent Overstreet
bch_is_zero(const char * p,size_t n)124169ef1cfSKent Overstreet bool bch_is_zero(const char *p, size_t n)
125cafe5635SKent Overstreet {
126cafe5635SKent Overstreet size_t i;
127cafe5635SKent Overstreet
128cafe5635SKent Overstreet for (i = 0; i < n; i++)
129cafe5635SKent Overstreet if (p[i])
130cafe5635SKent Overstreet return false;
131cafe5635SKent Overstreet return true;
132cafe5635SKent Overstreet }
133cafe5635SKent Overstreet
bch_parse_uuid(const char * s,char * uuid)134169ef1cfSKent Overstreet int bch_parse_uuid(const char *s, char *uuid)
135cafe5635SKent Overstreet {
136cafe5635SKent Overstreet size_t i, j, x;
1371fae7cf0SColy Li
138cafe5635SKent Overstreet memset(uuid, 0, 16);
139cafe5635SKent Overstreet
140cafe5635SKent Overstreet for (i = 0, j = 0;
141cafe5635SKent Overstreet i < strspn(s, "-0123456789:ABCDEFabcdef") && j < 32;
142cafe5635SKent Overstreet i++) {
143cafe5635SKent Overstreet x = s[i] | 32;
144cafe5635SKent Overstreet
145cafe5635SKent Overstreet switch (x) {
146cafe5635SKent Overstreet case '0'...'9':
147cafe5635SKent Overstreet x -= '0';
148cafe5635SKent Overstreet break;
149cafe5635SKent Overstreet case 'a'...'f':
150cafe5635SKent Overstreet x -= 'a' - 10;
151cafe5635SKent Overstreet break;
152cafe5635SKent Overstreet default:
153cafe5635SKent Overstreet continue;
154cafe5635SKent Overstreet }
155cafe5635SKent Overstreet
156cafe5635SKent Overstreet if (!(j & 1))
157cafe5635SKent Overstreet x <<= 4;
158cafe5635SKent Overstreet uuid[j++ >> 1] |= x;
159cafe5635SKent Overstreet }
160cafe5635SKent Overstreet return i;
161cafe5635SKent Overstreet }
162cafe5635SKent Overstreet
bch_time_stats_update(struct time_stats * stats,uint64_t start_time)163169ef1cfSKent Overstreet void bch_time_stats_update(struct time_stats *stats, uint64_t start_time)
164cafe5635SKent Overstreet {
16565d22e91SKent Overstreet uint64_t now, duration, last;
16665d22e91SKent Overstreet
16765d22e91SKent Overstreet spin_lock(&stats->lock);
16865d22e91SKent Overstreet
16965d22e91SKent Overstreet now = local_clock();
17065d22e91SKent Overstreet duration = time_after64(now, start_time)
171cafe5635SKent Overstreet ? now - start_time : 0;
17265d22e91SKent Overstreet last = time_after64(now, stats->last)
173cafe5635SKent Overstreet ? now - stats->last : 0;
174cafe5635SKent Overstreet
175cafe5635SKent Overstreet stats->max_duration = max(stats->max_duration, duration);
176cafe5635SKent Overstreet
177cafe5635SKent Overstreet if (stats->last) {
178cafe5635SKent Overstreet ewma_add(stats->average_duration, duration, 8, 8);
179cafe5635SKent Overstreet
180cafe5635SKent Overstreet if (stats->average_frequency)
181cafe5635SKent Overstreet ewma_add(stats->average_frequency, last, 8, 8);
182cafe5635SKent Overstreet else
183cafe5635SKent Overstreet stats->average_frequency = last << 8;
184cafe5635SKent Overstreet } else {
185cafe5635SKent Overstreet stats->average_duration = duration << 8;
186cafe5635SKent Overstreet }
187cafe5635SKent Overstreet
188cafe5635SKent Overstreet stats->last = now ?: 1;
18965d22e91SKent Overstreet
19065d22e91SKent Overstreet spin_unlock(&stats->lock);
191cafe5635SKent Overstreet }
192cafe5635SKent Overstreet
193c2a4f318SKent Overstreet /**
19447344e33SBart Van Assche * bch_next_delay() - update ratelimiting statistics and calculate next delay
19547344e33SBart Van Assche * @d: the struct bch_ratelimit to update
19647344e33SBart Van Assche * @done: the amount of work done, in arbitrary units
197c2a4f318SKent Overstreet *
19847344e33SBart Van Assche * Increment @d by the amount of work done, and return how long to delay in
19947344e33SBart Van Assche * jiffies until the next time to do some work.
200c2a4f318SKent Overstreet */
bch_next_delay(struct bch_ratelimit * d,uint64_t done)201c2a4f318SKent Overstreet uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done)
202cafe5635SKent Overstreet {
203cafe5635SKent Overstreet uint64_t now = local_clock();
204cafe5635SKent Overstreet
205ea8c5356SColy Li d->next += div_u64(done * NSEC_PER_SEC, atomic_long_read(&d->rate));
20616749c23SKent Overstreet
207ae82ddbfSMichael Lyle /* Bound the time. Don't let us fall further than 2 seconds behind
208ae82ddbfSMichael Lyle * (this prevents unnecessary backlog that would make it impossible
209ae82ddbfSMichael Lyle * to catch up). If we're ahead of the desired writeback rate,
210ae82ddbfSMichael Lyle * don't let us sleep more than 2.5 seconds (so we can notice/respond
211ae82ddbfSMichael Lyle * if the control system tells us to speed up!).
212ae82ddbfSMichael Lyle */
2139ce762e8SMichael Lyle if (time_before64(now + NSEC_PER_SEC * 5LLU / 2LLU, d->next))
2149ce762e8SMichael Lyle d->next = now + NSEC_PER_SEC * 5LLU / 2LLU;
21516749c23SKent Overstreet
21616749c23SKent Overstreet if (time_after64(now - NSEC_PER_SEC * 2, d->next))
21716749c23SKent Overstreet d->next = now - NSEC_PER_SEC * 2;
218cafe5635SKent Overstreet
219cafe5635SKent Overstreet return time_after64(d->next, now)
220cafe5635SKent Overstreet ? div_u64(d->next - now, NSEC_PER_SEC / HZ)
221cafe5635SKent Overstreet : 0;
222cafe5635SKent Overstreet }
223cafe5635SKent Overstreet
224c2421edfSMing Lei /*
225c2421edfSMing Lei * Generally it isn't good to access .bi_io_vec and .bi_vcnt directly,
226c2421edfSMing Lei * the preferred way is bio_add_page, but in this case, bch_bio_map()
227c2421edfSMing Lei * supposes that the bvec table is empty, so it is safe to access
228c2421edfSMing Lei * .bi_vcnt & .bi_io_vec in this way even after multipage bvec is
229c2421edfSMing Lei * supported.
230c2421edfSMing Lei */
bch_bio_map(struct bio * bio,void * base)231169ef1cfSKent Overstreet void bch_bio_map(struct bio *bio, void *base)
232cafe5635SKent Overstreet {
2334f024f37SKent Overstreet size_t size = bio->bi_iter.bi_size;
234cafe5635SKent Overstreet struct bio_vec *bv = bio->bi_io_vec;
235cafe5635SKent Overstreet
2364f024f37SKent Overstreet BUG_ON(!bio->bi_iter.bi_size);
237cafe5635SKent Overstreet BUG_ON(bio->bi_vcnt);
238cafe5635SKent Overstreet
23993bbf583SAl Viro bv->bv_offset = base ? offset_in_page(base) : 0;
240cafe5635SKent Overstreet goto start;
241cafe5635SKent Overstreet
242cafe5635SKent Overstreet for (; size; bio->bi_vcnt++, bv++) {
243cafe5635SKent Overstreet bv->bv_offset = 0;
244cafe5635SKent Overstreet start: bv->bv_len = min_t(size_t, PAGE_SIZE - bv->bv_offset,
245cafe5635SKent Overstreet size);
246cafe5635SKent Overstreet if (base) {
247cafe5635SKent Overstreet bv->bv_page = is_vmalloc_addr(base)
248cafe5635SKent Overstreet ? vmalloc_to_page(base)
249cafe5635SKent Overstreet : virt_to_page(base);
250cafe5635SKent Overstreet
251cafe5635SKent Overstreet base += bv->bv_len;
252cafe5635SKent Overstreet }
253cafe5635SKent Overstreet
254cafe5635SKent Overstreet size -= bv->bv_len;
255cafe5635SKent Overstreet }
256cafe5635SKent Overstreet }
257cafe5635SKent Overstreet
25825d8be77SMing Lei /**
25925d8be77SMing Lei * bch_bio_alloc_pages - allocates a single page for each bvec in a bio
26025d8be77SMing Lei * @bio: bio to allocate pages for
26125d8be77SMing Lei * @gfp_mask: flags for allocation
26225d8be77SMing Lei *
26325d8be77SMing Lei * Allocates pages up to @bio->bi_vcnt.
26425d8be77SMing Lei *
26525d8be77SMing Lei * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
26625d8be77SMing Lei * freed.
26725d8be77SMing Lei */
bch_bio_alloc_pages(struct bio * bio,gfp_t gfp_mask)26825d8be77SMing Lei int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
26925d8be77SMing Lei {
27025d8be77SMing Lei int i;
27125d8be77SMing Lei struct bio_vec *bv;
27225d8be77SMing Lei
2732e1f4f4dSMing Lei /*
2742e1f4f4dSMing Lei * This is called on freshly new bio, so it is safe to access the
2752e1f4f4dSMing Lei * bvec table directly.
2762e1f4f4dSMing Lei */
2772e1f4f4dSMing Lei for (i = 0, bv = bio->bi_io_vec; i < bio->bi_vcnt; bv++, i++) {
27825d8be77SMing Lei bv->bv_page = alloc_page(gfp_mask);
27925d8be77SMing Lei if (!bv->bv_page) {
28025d8be77SMing Lei while (--bv >= bio->bi_io_vec)
28125d8be77SMing Lei __free_page(bv->bv_page);
28225d8be77SMing Lei return -ENOMEM;
28325d8be77SMing Lei }
28425d8be77SMing Lei }
28525d8be77SMing Lei
28625d8be77SMing Lei return 0;
28725d8be77SMing Lei }
288