Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * random utiility code, for bcache but in theory not specific to bcache
0004  *
0005  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
0006  * Copyright 2012 Google, Inc.
0007  */
0008 
0009 #include <linux/bio.h>
0010 #include <linux/blkdev.h>
0011 #include <linux/ctype.h>
0012 #include <linux/debugfs.h>
0013 #include <linux/module.h>
0014 #include <linux/seq_file.h>
0015 #include <linux/types.h>
0016 #include <linux/sched/clock.h>
0017 
0018 #include "util.h"
0019 
0020 #define simple_strtoint(c, end, base)   simple_strtol(c, end, base)
0021 #define simple_strtouint(c, end, base)  simple_strtoul(c, end, base)
0022 
0023 #define STRTO_H(name, type)                 \
0024 int bch_ ## name ## _h(const char *cp, type *res)       \
0025 {                               \
0026     int u = 0;                      \
0027     char *e;                        \
0028     type i = simple_ ## name(cp, &e, 10);           \
0029                                 \
0030     switch (tolower(*e)) {                  \
0031     default:                        \
0032         return -EINVAL;                 \
0033     case 'y':                       \
0034     case 'z':                       \
0035         u++;                        \
0036         fallthrough;                    \
0037     case 'e':                       \
0038         u++;                        \
0039         fallthrough;                    \
0040     case 'p':                       \
0041         u++;                        \
0042         fallthrough;                    \
0043     case 't':                       \
0044         u++;                        \
0045         fallthrough;                    \
0046     case 'g':                       \
0047         u++;                        \
0048         fallthrough;                    \
0049     case 'm':                       \
0050         u++;                        \
0051         fallthrough;                    \
0052     case 'k':                       \
0053         u++;                        \
0054         if (e++ == cp)                  \
0055             return -EINVAL;             \
0056         fallthrough;                    \
0057     case '\n':                      \
0058     case '\0':                      \
0059         if (*e == '\n')                 \
0060             e++;                    \
0061     }                           \
0062                                 \
0063     if (*e)                         \
0064         return -EINVAL;                 \
0065                                 \
0066     while (u--) {                       \
0067         if ((type) ~0 > 0 &&                \
0068             (type) ~0 / 1024 <= i)          \
0069             return -EINVAL;             \
0070         if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) ||  \
0071             (i < 0 && -ANYSINT_MAX(type) / 1024 > i))   \
0072             return -EINVAL;             \
0073         i *= 1024;                  \
0074     }                           \
0075                                 \
0076     *res = i;                       \
0077     return 0;                       \
0078 }                               \
0079 
0080 STRTO_H(strtoint, int)
0081 STRTO_H(strtouint, unsigned int)
0082 STRTO_H(strtoll, long long)
0083 STRTO_H(strtoull, unsigned long long)
0084 
0085 /**
0086  * bch_hprint - formats @v to human readable string for sysfs.
0087  * @buf: the (at least 8 byte) buffer to format the result into.
0088  * @v: signed 64 bit integer
0089  *
0090  * Returns the number of bytes used by format.
0091  */
0092 ssize_t bch_hprint(char *buf, int64_t v)
0093 {
0094     static const char units[] = "?kMGTPEZY";
0095     int u = 0, t;
0096 
0097     uint64_t q;
0098 
0099     if (v < 0)
0100         q = -v;
0101     else
0102         q = v;
0103 
0104     /* For as long as the number is more than 3 digits, but at least
0105      * once, shift right / divide by 1024.  Keep the remainder for
0106      * a digit after the decimal point.
0107      */
0108     do {
0109         u++;
0110 
0111         t = q & ~(~0 << 10);
0112         q >>= 10;
0113     } while (q >= 1000);
0114 
0115     if (v < 0)
0116         /* '-', up to 3 digits, '.', 1 digit, 1 character, null;
0117          * yields 8 bytes.
0118          */
0119         return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
0120     else
0121         return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
0122 }
0123 
0124 bool bch_is_zero(const char *p, size_t n)
0125 {
0126     size_t i;
0127 
0128     for (i = 0; i < n; i++)
0129         if (p[i])
0130             return false;
0131     return true;
0132 }
0133 
0134 int bch_parse_uuid(const char *s, char *uuid)
0135 {
0136     size_t i, j, x;
0137 
0138     memset(uuid, 0, 16);
0139 
0140     for (i = 0, j = 0;
0141          i < strspn(s, "-0123456789:ABCDEFabcdef") && j < 32;
0142          i++) {
0143         x = s[i] | 32;
0144 
0145         switch (x) {
0146         case '0'...'9':
0147             x -= '0';
0148             break;
0149         case 'a'...'f':
0150             x -= 'a' - 10;
0151             break;
0152         default:
0153             continue;
0154         }
0155 
0156         if (!(j & 1))
0157             x <<= 4;
0158         uuid[j++ >> 1] |= x;
0159     }
0160     return i;
0161 }
0162 
0163 void bch_time_stats_update(struct time_stats *stats, uint64_t start_time)
0164 {
0165     uint64_t now, duration, last;
0166 
0167     spin_lock(&stats->lock);
0168 
0169     now     = local_clock();
0170     duration    = time_after64(now, start_time)
0171         ? now - start_time : 0;
0172     last        = time_after64(now, stats->last)
0173         ? now - stats->last : 0;
0174 
0175     stats->max_duration = max(stats->max_duration, duration);
0176 
0177     if (stats->last) {
0178         ewma_add(stats->average_duration, duration, 8, 8);
0179 
0180         if (stats->average_frequency)
0181             ewma_add(stats->average_frequency, last, 8, 8);
0182         else
0183             stats->average_frequency  = last << 8;
0184     } else {
0185         stats->average_duration  = duration << 8;
0186     }
0187 
0188     stats->last = now ?: 1;
0189 
0190     spin_unlock(&stats->lock);
0191 }
0192 
0193 /**
0194  * bch_next_delay() - update ratelimiting statistics and calculate next delay
0195  * @d: the struct bch_ratelimit to update
0196  * @done: the amount of work done, in arbitrary units
0197  *
0198  * Increment @d by the amount of work done, and return how long to delay in
0199  * jiffies until the next time to do some work.
0200  */
0201 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done)
0202 {
0203     uint64_t now = local_clock();
0204 
0205     d->next += div_u64(done * NSEC_PER_SEC, atomic_long_read(&d->rate));
0206 
0207     /* Bound the time.  Don't let us fall further than 2 seconds behind
0208      * (this prevents unnecessary backlog that would make it impossible
0209      * to catch up).  If we're ahead of the desired writeback rate,
0210      * don't let us sleep more than 2.5 seconds (so we can notice/respond
0211      * if the control system tells us to speed up!).
0212      */
0213     if (time_before64(now + NSEC_PER_SEC * 5LLU / 2LLU, d->next))
0214         d->next = now + NSEC_PER_SEC * 5LLU / 2LLU;
0215 
0216     if (time_after64(now - NSEC_PER_SEC * 2, d->next))
0217         d->next = now - NSEC_PER_SEC * 2;
0218 
0219     return time_after64(d->next, now)
0220         ? div_u64(d->next - now, NSEC_PER_SEC / HZ)
0221         : 0;
0222 }
0223 
0224 /*
0225  * Generally it isn't good to access .bi_io_vec and .bi_vcnt directly,
0226  * the preferred way is bio_add_page, but in this case, bch_bio_map()
0227  * supposes that the bvec table is empty, so it is safe to access
0228  * .bi_vcnt & .bi_io_vec in this way even after multipage bvec is
0229  * supported.
0230  */
0231 void bch_bio_map(struct bio *bio, void *base)
0232 {
0233     size_t size = bio->bi_iter.bi_size;
0234     struct bio_vec *bv = bio->bi_io_vec;
0235 
0236     BUG_ON(!bio->bi_iter.bi_size);
0237     BUG_ON(bio->bi_vcnt);
0238 
0239     bv->bv_offset = base ? offset_in_page(base) : 0;
0240     goto start;
0241 
0242     for (; size; bio->bi_vcnt++, bv++) {
0243         bv->bv_offset   = 0;
0244 start:      bv->bv_len  = min_t(size_t, PAGE_SIZE - bv->bv_offset,
0245                     size);
0246         if (base) {
0247             bv->bv_page = is_vmalloc_addr(base)
0248                 ? vmalloc_to_page(base)
0249                 : virt_to_page(base);
0250 
0251             base += bv->bv_len;
0252         }
0253 
0254         size -= bv->bv_len;
0255     }
0256 }
0257 
0258 /**
0259  * bch_bio_alloc_pages - allocates a single page for each bvec in a bio
0260  * @bio: bio to allocate pages for
0261  * @gfp_mask: flags for allocation
0262  *
0263  * Allocates pages up to @bio->bi_vcnt.
0264  *
0265  * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
0266  * freed.
0267  */
0268 int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
0269 {
0270     int i;
0271     struct bio_vec *bv;
0272 
0273     /*
0274      * This is called on freshly new bio, so it is safe to access the
0275      * bvec table directly.
0276      */
0277     for (i = 0, bv = bio->bi_io_vec; i < bio->bi_vcnt; bv++, i++) {
0278         bv->bv_page = alloc_page(gfp_mask);
0279         if (!bv->bv_page) {
0280             while (--bv >= bio->bi_io_vec)
0281                 __free_page(bv->bv_page);
0282             return -ENOMEM;
0283         }
0284     }
0285 
0286     return 0;
0287 }