1 // SPDX-License-Identifier: GPL-2.0
3 * Some low level IO code, and hacks for various block layer limitations
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
13 #include <linux/blkdev.h>
15 /* Bios with headers */
17 void bch_bbio_free(struct bio
*bio
, struct cache_set
*c
)
19 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
21 mempool_free(b
, &c
->bio_meta
);
24 struct bio
*bch_bbio_alloc(struct cache_set
*c
)
26 struct bbio
*b
= mempool_alloc(&c
->bio_meta
, GFP_NOIO
);
27 struct bio
*bio
= &b
->bio
;
29 bio_init(bio
, bio
->bi_inline_vecs
, bucket_pages(c
));
34 void __bch_submit_bbio(struct bio
*bio
, struct cache_set
*c
)
36 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
38 bio
->bi_iter
.bi_sector
= PTR_OFFSET(&b
->key
, 0);
39 bio_set_dev(bio
, PTR_CACHE(c
, &b
->key
, 0)->bdev
);
41 b
->submit_time_us
= local_clock_us();
42 closure_bio_submit(c
, bio
, bio
->bi_private
);
45 void bch_submit_bbio(struct bio
*bio
, struct cache_set
*c
,
46 struct bkey
*k
, unsigned int ptr
)
48 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
50 bch_bkey_copy_single_ptr(&b
->key
, k
, ptr
);
51 __bch_submit_bbio(bio
, c
);
55 void bch_count_backing_io_errors(struct cached_dev
*dc
, struct bio
*bio
)
59 WARN_ONCE(!dc
, "NULL pointer of struct cached_dev");
62 * Read-ahead requests on a degrading and recovering md raid
63 * (e.g. raid6) device might be failured immediately by md
64 * raid code, which is not a real hardware media failure. So
65 * we shouldn't count failed REQ_RAHEAD bio to dc->io_errors.
67 if (bio
->bi_opf
& REQ_RAHEAD
) {
68 pr_warn_ratelimited("%s: Read-ahead I/O failed on backing device, ignore",
69 dc
->backing_dev_name
);
73 errors
= atomic_add_return(1, &dc
->io_errors
);
74 if (errors
< dc
->error_limit
)
75 pr_err("%s: IO error on backing device, unrecoverable",
76 dc
->backing_dev_name
);
78 bch_cached_dev_error(dc
);
81 void bch_count_io_errors(struct cache
*ca
,
87 * The halflife of an error is:
88 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
91 if (ca
->set
->error_decay
) {
92 unsigned int count
= atomic_inc_return(&ca
->io_count
);
94 while (count
> ca
->set
->error_decay
) {
96 unsigned int old
= count
;
97 unsigned int new = count
- ca
->set
->error_decay
;
100 * First we subtract refresh from count; each time we
101 * successfully do so, we rescale the errors once:
104 count
= atomic_cmpxchg(&ca
->io_count
, old
, new);
109 errors
= atomic_read(&ca
->io_errors
);
112 new = ((uint64_t) errors
* 127) / 128;
113 errors
= atomic_cmpxchg(&ca
->io_errors
,
115 } while (old
!= errors
);
121 unsigned int errors
= atomic_add_return(1 << IO_ERROR_SHIFT
,
123 errors
>>= IO_ERROR_SHIFT
;
125 if (errors
< ca
->set
->error_limit
)
126 pr_err("%s: IO error on %s%s",
127 ca
->cache_dev_name
, m
,
128 is_read
? ", recovering." : ".");
130 bch_cache_set_error(ca
->set
,
131 "%s: too many IO errors %s",
132 ca
->cache_dev_name
, m
);
136 void bch_bbio_count_io_errors(struct cache_set
*c
, struct bio
*bio
,
137 blk_status_t error
, const char *m
)
139 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
140 struct cache
*ca
= PTR_CACHE(c
, &b
->key
, 0);
141 int is_read
= (bio_data_dir(bio
) == READ
? 1 : 0);
143 unsigned int threshold
= op_is_write(bio_op(bio
))
144 ? c
->congested_write_threshold_us
145 : c
->congested_read_threshold_us
;
148 unsigned int t
= local_clock_us();
149 int us
= t
- b
->submit_time_us
;
150 int congested
= atomic_read(&c
->congested
);
152 if (us
> (int) threshold
) {
155 c
->congested_last_us
= t
;
157 ms
= min(ms
, CONGESTED_MAX
+ congested
);
158 atomic_sub(ms
, &c
->congested
);
159 } else if (congested
< 0)
160 atomic_inc(&c
->congested
);
163 bch_count_io_errors(ca
, error
, is_read
, m
);
166 void bch_bbio_endio(struct cache_set
*c
, struct bio
*bio
,
167 blk_status_t error
, const char *m
)
169 struct closure
*cl
= bio
->bi_private
;
171 bch_bbio_count_io_errors(c
, bio
, error
, m
);