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