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
, NULL
, bio
->bi_inline_vecs
,
30 meta_bucket_pages(&c
->cache
->sb
), 0);
35 void __bch_submit_bbio(struct bio
*bio
, struct cache_set
*c
)
37 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
39 bio
->bi_iter
.bi_sector
= PTR_OFFSET(&b
->key
, 0);
40 bio_set_dev(bio
, c
->cache
->bdev
);
42 b
->submit_time_us
= local_clock_us();
43 closure_bio_submit(c
, bio
, bio
->bi_private
);
46 void bch_submit_bbio(struct bio
*bio
, struct cache_set
*c
,
47 struct bkey
*k
, unsigned int ptr
)
49 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
51 bch_bkey_copy_single_ptr(&b
->key
, k
, ptr
);
52 __bch_submit_bbio(bio
, c
);
56 void bch_count_backing_io_errors(struct cached_dev
*dc
, struct bio
*bio
)
60 WARN_ONCE(!dc
, "NULL pointer of struct cached_dev");
63 * Read-ahead requests on a degrading and recovering md raid
64 * (e.g. raid6) device might be failured immediately by md
65 * raid code, which is not a real hardware media failure. So
66 * we shouldn't count failed REQ_RAHEAD bio to dc->io_errors.
68 if (bio
->bi_opf
& REQ_RAHEAD
) {
69 pr_warn_ratelimited("%pg: Read-ahead I/O failed on backing device, ignore\n",
74 errors
= atomic_add_return(1, &dc
->io_errors
);
75 if (errors
< dc
->error_limit
)
76 pr_err("%pg: IO error on backing device, unrecoverable\n",
79 bch_cached_dev_error(dc
);
82 void bch_count_io_errors(struct cache
*ca
,
88 * The halflife of an error is:
89 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
92 if (ca
->set
->error_decay
) {
93 unsigned int count
= atomic_inc_return(&ca
->io_count
);
95 while (count
> ca
->set
->error_decay
) {
97 unsigned int old
= count
;
98 unsigned int new = count
- ca
->set
->error_decay
;
101 * First we subtract refresh from count; each time we
102 * successfully do so, we rescale the errors once:
105 count
= atomic_cmpxchg(&ca
->io_count
, old
, new);
110 errors
= atomic_read(&ca
->io_errors
);
113 new = ((uint64_t) errors
* 127) / 128;
114 errors
= atomic_cmpxchg(&ca
->io_errors
,
116 } while (old
!= errors
);
122 unsigned int errors
= atomic_add_return(1 << IO_ERROR_SHIFT
,
124 errors
>>= IO_ERROR_SHIFT
;
126 if (errors
< ca
->set
->error_limit
)
127 pr_err("%pg: IO error on %s%s\n",
129 is_read
? ", recovering." : ".");
131 bch_cache_set_error(ca
->set
,
132 "%pg: too many IO errors %s\n",
137 void bch_bbio_count_io_errors(struct cache_set
*c
, struct bio
*bio
,
138 blk_status_t error
, const char *m
)
140 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
141 struct cache
*ca
= c
->cache
;
142 int is_read
= (bio_data_dir(bio
) == READ
? 1 : 0);
144 unsigned int threshold
= op_is_write(bio_op(bio
))
145 ? c
->congested_write_threshold_us
146 : c
->congested_read_threshold_us
;
149 unsigned int t
= local_clock_us();
150 int us
= t
- b
->submit_time_us
;
151 int congested
= atomic_read(&c
->congested
);
153 if (us
> (int) threshold
) {
156 c
->congested_last_us
= t
;
158 ms
= min(ms
, CONGESTED_MAX
+ congested
);
159 atomic_sub(ms
, &c
->congested
);
160 } else if (congested
< 0)
161 atomic_inc(&c
->congested
);
164 bch_count_io_errors(ca
, error
, is_read
, m
);
167 void bch_bbio_endio(struct cache_set
*c
, struct bio
*bio
,
168 blk_status_t error
, const char *m
)
170 struct closure
*cl
= bio
->bi_private
;
172 bch_bbio_count_io_errors(c
, bio
, error
, m
);