]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/md/bcache/io.c
Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-jammy-kernel.git] / drivers / md / bcache / io.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
cafe5635
KO
2/*
3 * Some low level IO code, and hacks for various block layer limitations
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
9#include "bcache.h"
10#include "bset.h"
11#include "debug.h"
12
c37511b8
KO
13#include <linux/blkdev.h>
14
cafe5635
KO
15/* Bios with headers */
16
17void bch_bbio_free(struct bio *bio, struct cache_set *c)
18{
19 struct bbio *b = container_of(bio, struct bbio, bio);
20 mempool_free(b, c->bio_meta);
21}
22
23struct bio *bch_bbio_alloc(struct cache_set *c)
24{
25 struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
26 struct bio *bio = &b->bio;
27
3a83f467 28 bio_init(bio, bio->bi_inline_vecs, bucket_pages(c));
cafe5635
KO
29
30 return bio;
31}
32
33void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
34{
35 struct bbio *b = container_of(bio, struct bbio, bio);
36
4f024f37 37 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
74d46992 38 bio_set_dev(bio, PTR_CACHE(c, &b->key, 0)->bdev);
cafe5635
KO
39
40 b->submit_time_us = local_clock_us();
749b61da 41 closure_bio_submit(bio, bio->bi_private);
cafe5635
KO
42}
43
44void bch_submit_bbio(struct bio *bio, struct cache_set *c,
45 struct bkey *k, unsigned ptr)
46{
47 struct bbio *b = container_of(bio, struct bbio, bio);
48 bch_bkey_copy_single_ptr(&b->key, k, ptr);
49 __bch_submit_bbio(bio, c);
50}
51
52/* IO errors */
53
5138ac67
CL
54void bch_count_io_errors(struct cache *ca,
55 blk_status_t error,
56 int is_read,
57 const char *m)
cafe5635
KO
58{
59 /*
60 * The halflife of an error is:
61 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
62 */
63
64 if (ca->set->error_decay) {
65 unsigned count = atomic_inc_return(&ca->io_count);
66
67 while (count > ca->set->error_decay) {
68 unsigned errors;
69 unsigned old = count;
70 unsigned new = count - ca->set->error_decay;
71
72 /*
73 * First we subtract refresh from count; each time we
74 * succesfully do so, we rescale the errors once:
75 */
76
77 count = atomic_cmpxchg(&ca->io_count, old, new);
78
79 if (count == old) {
80 count = new;
81
82 errors = atomic_read(&ca->io_errors);
83 do {
84 old = errors;
85 new = ((uint64_t) errors * 127) / 128;
86 errors = atomic_cmpxchg(&ca->io_errors,
87 old, new);
88 } while (old != errors);
89 }
90 }
91 }
92
93 if (error) {
94 char buf[BDEVNAME_SIZE];
95 unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
96 &ca->io_errors);
97 errors >>= IO_ERROR_SHIFT;
98
99 if (errors < ca->set->error_limit)
5138ac67
CL
100 pr_err("%s: IO error on %s%s",
101 bdevname(ca->bdev, buf), m,
102 is_read ? ", recovering." : ".");
cafe5635
KO
103 else
104 bch_cache_set_error(ca->set,
105 "%s: too many IO errors %s",
106 bdevname(ca->bdev, buf), m);
107 }
108}
109
110void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
4e4cbee9 111 blk_status_t error, const char *m)
cafe5635
KO
112{
113 struct bbio *b = container_of(bio, struct bbio, bio);
114 struct cache *ca = PTR_CACHE(c, &b->key, 0);
5138ac67 115 int is_read = (bio_data_dir(bio) == READ ? 1 : 0);
cafe5635 116
c8d93247 117 unsigned threshold = op_is_write(bio_op(bio))
cafe5635
KO
118 ? c->congested_write_threshold_us
119 : c->congested_read_threshold_us;
120
121 if (threshold) {
122 unsigned t = local_clock_us();
123
124 int us = t - b->submit_time_us;
125 int congested = atomic_read(&c->congested);
126
127 if (us > (int) threshold) {
128 int ms = us / 1024;
129 c->congested_last_us = t;
130
131 ms = min(ms, CONGESTED_MAX + congested);
132 atomic_sub(ms, &c->congested);
133 } else if (congested < 0)
134 atomic_inc(&c->congested);
135 }
136
5138ac67 137 bch_count_io_errors(ca, error, is_read, m);
cafe5635
KO
138}
139
140void bch_bbio_endio(struct cache_set *c, struct bio *bio,
4e4cbee9 141 blk_status_t error, const char *m)
cafe5635
KO
142{
143 struct closure *cl = bio->bi_private;
144
145 bch_bbio_count_io_errors(c, bio, error, m);
146 bio_put(bio);
147 closure_put(cl);
148}