Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * random utiility code, for bcache but in theory not specific to bcache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2012 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define simple_strtoint(c, end, base)	simple_strtol(c, end, base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define simple_strtouint(c, end, base)	simple_strtoul(c, end, base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define STRTO_H(name, type)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) int bch_ ## name ## _h(const char *cp, type *res)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int u = 0;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	char *e;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	type i = simple_ ## name(cp, &e, 10);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	switch (tolower(*e)) {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	default:						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return -EINVAL;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	case 'y':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	case 'z':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	case 'e':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	case 'p':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	case 't':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	case 'g':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	case 'm':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	case 'k':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		u++;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (e++ == cp)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			return -EINVAL;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		fallthrough;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case '\n':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case '\0':						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (*e == '\n')					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			e++;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (*e)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return -EINVAL;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	while (u--) {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if ((type) ~0 > 0 &&				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		    (type) ~0 / 1024 <= i)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			return -EINVAL;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		    (i < 0 && -ANYSINT_MAX(type) / 1024 > i))	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			return -EINVAL;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		i *= 1024;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	*res = i;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return 0;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) STRTO_H(strtoint, int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) STRTO_H(strtouint, unsigned int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) STRTO_H(strtoll, long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) STRTO_H(strtoull, unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * bch_hprint - formats @v to human readable string for sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * @buf: the (at least 8 byte) buffer to format the result into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * @v: signed 64 bit integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Returns the number of bytes used by format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) ssize_t bch_hprint(char *buf, int64_t v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	static const char units[] = "?kMGTPEZY";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int u = 0, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	uint64_t q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		q = -v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		q = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* For as long as the number is more than 3 digits, but at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * once, shift right / divide by 1024.  Keep the remainder for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * a digit after the decimal point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		u++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		t = q & ~(~0 << 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		q >>= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	} while (q >= 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		/* '-', up to 3 digits, '.', 1 digit, 1 character, null;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		 * yields 8 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bool bch_is_zero(const char *p, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (p[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int bch_parse_uuid(const char *s, char *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	size_t i, j, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	memset(uuid, 0, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	for (i = 0, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	     i < strspn(s, "-0123456789:ABCDEFabcdef") && j < 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	     i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		x = s[i] | 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		switch (x) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		case '0'...'9':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			x -= '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		case 'a'...'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			x -= 'a' - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (!(j & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			x <<= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		uuid[j++ >> 1] |= x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void bch_time_stats_update(struct time_stats *stats, uint64_t start_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	uint64_t now, duration, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	spin_lock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	now		= local_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	duration	= time_after64(now, start_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		? now - start_time : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	last		= time_after64(now, stats->last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		? now - stats->last : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	stats->max_duration = max(stats->max_duration, duration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (stats->last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ewma_add(stats->average_duration, duration, 8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		if (stats->average_frequency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			ewma_add(stats->average_frequency, last, 8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			stats->average_frequency  = last << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		stats->average_duration  = duration << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	stats->last = now ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	spin_unlock(&stats->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * bch_next_delay() - update ratelimiting statistics and calculate next delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * @d: the struct bch_ratelimit to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * @done: the amount of work done, in arbitrary units
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * Increment @d by the amount of work done, and return how long to delay in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * jiffies until the next time to do some work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	uint64_t now = local_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	d->next += div_u64(done * NSEC_PER_SEC, atomic_long_read(&d->rate));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	/* Bound the time.  Don't let us fall further than 2 seconds behind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * (this prevents unnecessary backlog that would make it impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * to catch up).  If we're ahead of the desired writeback rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * don't let us sleep more than 2.5 seconds (so we can notice/respond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * if the control system tells us to speed up!).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (time_before64(now + NSEC_PER_SEC * 5LLU / 2LLU, d->next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		d->next = now + NSEC_PER_SEC * 5LLU / 2LLU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (time_after64(now - NSEC_PER_SEC * 2, d->next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		d->next = now - NSEC_PER_SEC * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return time_after64(d->next, now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		? div_u64(d->next - now, NSEC_PER_SEC / HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		: 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * Generally it isn't good to access .bi_io_vec and .bi_vcnt directly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * the preferred way is bio_add_page, but in this case, bch_bio_map()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * supposes that the bvec table is empty, so it is safe to access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * .bi_vcnt & .bi_io_vec in this way even after multipage bvec is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) void bch_bio_map(struct bio *bio, void *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	size_t size = bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct bio_vec *bv = bio->bi_io_vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	BUG_ON(!bio->bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	BUG_ON(bio->bi_vcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	bv->bv_offset = base ? offset_in_page(base) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	goto start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	for (; size; bio->bi_vcnt++, bv++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		bv->bv_offset	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) start:		bv->bv_len	= min_t(size_t, PAGE_SIZE - bv->bv_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 					size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			bv->bv_page = is_vmalloc_addr(base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				? vmalloc_to_page(base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				: virt_to_page(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			base += bv->bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		size -= bv->bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * bch_bio_alloc_pages - allocates a single page for each bvec in a bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * @bio: bio to allocate pages for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * @gfp_mask: flags for allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * Allocates pages up to @bio->bi_vcnt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct bio_vec *bv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * This is called on freshly new bio, so it is safe to access the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 * bvec table directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	for (i = 0, bv = bio->bi_io_vec; i < bio->bi_vcnt; bv++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		bv->bv_page = alloc_page(gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		if (!bv->bv_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			while (--bv >= bio->bi_io_vec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				__free_page(bv->bv_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }