Javier González | 02a1520 | 2018-10-09 13:12:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 CNEX Labs |
| 4 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> |
| 5 | * Matias Bjorling <matias@cnexlabs.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * pblk-read.c - pblk's read path |
| 17 | */ |
| 18 | |
| 19 | #include "pblk.h" |
| 20 | |
| 21 | /* |
| 22 | * There is no guarantee that the value read from cache has not been updated and |
| 23 | * resides at another location in the cache. We guarantee though that if the |
| 24 | * value is read from the cache, it belongs to the mapped lba. In order to |
| 25 | * guarantee and order between writes and reads are ordered, a flush must be |
| 26 | * issued. |
| 27 | */ |
| 28 | static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio, |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 29 | sector_t lba, struct ppa_addr ppa) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 30 | { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 32 | /* Callers must ensure that the ppa points to a cache address */ |
| 33 | BUG_ON(pblk_ppa_empty(ppa)); |
| 34 | BUG_ON(!pblk_addr_in_cache(ppa)); |
| 35 | #endif |
| 36 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 37 | return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 40 | static int pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 41 | struct bio *bio, sector_t blba, |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 42 | bool *from_cache) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 43 | { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 44 | void *meta_list = rqd->meta_list; |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 45 | int nr_secs, i; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 46 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 47 | retry: |
| 48 | nr_secs = pblk_lookup_l2p_seq(pblk, rqd->ppa_list, blba, rqd->nr_ppas, |
| 49 | from_cache); |
| 50 | |
| 51 | if (!*from_cache) |
| 52 | goto end; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 53 | |
| 54 | for (i = 0; i < nr_secs; i++) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 55 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 56 | sector_t lba = blba + i; |
| 57 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 58 | if (pblk_ppa_empty(rqd->ppa_list[i])) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 59 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 60 | |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 61 | meta->lba = addr_empty; |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 62 | } else if (pblk_addr_in_cache(rqd->ppa_list[i])) { |
| 63 | /* |
| 64 | * Try to read from write buffer. The address is later |
| 65 | * checked on the write buffer to prevent retrieving |
| 66 | * overwritten data. |
| 67 | */ |
| 68 | if (!pblk_read_from_cache(pblk, bio, lba, |
| 69 | rqd->ppa_list[i])) { |
| 70 | if (i == 0) { |
| 71 | /* |
| 72 | * We didn't call with bio_advance() |
| 73 | * yet, so we can just retry. |
| 74 | */ |
| 75 | goto retry; |
| 76 | } else { |
| 77 | /* |
| 78 | * We already call bio_advance() |
| 79 | * so we cannot retry and we need |
| 80 | * to quit that function in order |
| 81 | * to allow caller to handle the bio |
| 82 | * splitting in the current sector |
| 83 | * position. |
| 84 | */ |
| 85 | nr_secs = i; |
| 86 | goto end; |
| 87 | } |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 88 | } |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 89 | meta->lba = cpu_to_le64(lba); |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 90 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 91 | atomic_long_inc(&pblk->cache_reads); |
| 92 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 93 | } |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 94 | bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 97 | end: |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 98 | if (pblk_io_aligned(pblk, nr_secs)) |
Matias Bjørling | d7b6801 | 2018-10-09 13:11:32 +0200 | [diff] [blame] | 99 | rqd->is_seq = 1; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 100 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 102 | atomic_long_add(nr_secs, &pblk->inflight_reads); |
| 103 | #endif |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 104 | |
| 105 | return nr_secs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 108 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 109 | static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd, |
| 110 | sector_t blba) |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 111 | { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 112 | void *meta_list = rqd->meta_list; |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 113 | int nr_lbas = rqd->nr_ppas; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 114 | int i; |
| 115 | |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 116 | if (!pblk_is_oob_meta_supported(pblk)) |
| 117 | return; |
| 118 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 119 | for (i = 0; i < nr_lbas; i++) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 120 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i); |
| 121 | u64 lba = le64_to_cpu(meta->lba); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 122 | |
| 123 | if (lba == ADDR_EMPTY) |
| 124 | continue; |
| 125 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 126 | if (lba != blba + i) { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 127 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 128 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 129 | |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 130 | print_ppa(pblk, &ppa_list[i], "seq", i); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 131 | #endif |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 132 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 133 | lba, (u64)blba + i); |
| 134 | WARN_ON(1); |
| 135 | } |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 139 | /* |
| 140 | * There can be holes in the lba list. |
| 141 | */ |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 142 | static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd, |
| 143 | u64 *lba_list, int nr_lbas) |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 144 | { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 145 | void *meta_lba_list = rqd->meta_list; |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 146 | int i, j; |
| 147 | |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 148 | if (!pblk_is_oob_meta_supported(pblk)) |
| 149 | return; |
| 150 | |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 151 | for (i = 0, j = 0; i < nr_lbas; i++) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 152 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, |
| 153 | meta_lba_list, j); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 154 | u64 lba = lba_list[i]; |
| 155 | u64 meta_lba; |
| 156 | |
| 157 | if (lba == ADDR_EMPTY) |
| 158 | continue; |
| 159 | |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 160 | meta_lba = le64_to_cpu(meta->lba); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 161 | |
| 162 | if (lba != meta_lba) { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 164 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 165 | |
Hans Holmberg | ea1d24b | 2018-10-09 13:11:49 +0200 | [diff] [blame] | 166 | print_ppa(pblk, &ppa_list[j], "rnd", j); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 167 | #endif |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 168 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
Hans Holmberg | ea1d24b | 2018-10-09 13:11:49 +0200 | [diff] [blame] | 169 | meta_lba, lba); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 170 | WARN_ON(1); |
| 171 | } |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 172 | |
| 173 | j++; |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 174 | } |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 175 | |
| 176 | WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n"); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Igor Konopko | d38954e | 2019-05-04 20:37:54 +0200 | [diff] [blame] | 179 | static void pblk_end_user_read(struct bio *bio, int error) |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 180 | { |
Igor Konopko | d38954e | 2019-05-04 20:37:54 +0200 | [diff] [blame] | 181 | if (error && error != NVM_RSP_WARN_HIGHECC) |
| 182 | bio_io_error(bio); |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 183 | else |
| 184 | bio_endio(bio); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd, |
| 188 | bool put_line) |
| 189 | { |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 190 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 191 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 192 | struct bio *int_bio = rqd->bio; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 193 | unsigned long start_time = r_ctx->start_time; |
| 194 | |
Michael Callahan | ddcf35d | 2018-07-18 04:47:39 -0700 | [diff] [blame] | 195 | generic_end_io_acct(dev->q, REQ_OP_READ, &pblk->disk->part0, start_time); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 196 | |
| 197 | if (rqd->error) |
| 198 | pblk_log_read_err(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 199 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 200 | pblk_read_check_seq(pblk, rqd, r_ctx->lba); |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 201 | bio_put(int_bio); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 202 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 203 | if (put_line) |
Matias Bjørling | ae14cc0 | 2018-10-09 13:11:40 +0200 | [diff] [blame] | 204 | pblk_rq_to_line_put(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 205 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 206 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 207 | atomic_long_add(rqd->nr_ppas, &pblk->sync_reads); |
| 208 | atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads); |
| 209 | #endif |
| 210 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 211 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 212 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 215 | static void pblk_end_io_read(struct nvm_rq *rqd) |
| 216 | { |
| 217 | struct pblk *pblk = rqd->private; |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 218 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 219 | struct bio *bio = (struct bio *)r_ctx->private; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 220 | |
Igor Konopko | d38954e | 2019-05-04 20:37:54 +0200 | [diff] [blame] | 221 | pblk_end_user_read(bio, rqd->error); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 222 | __pblk_end_io_read(pblk, rqd, true); |
| 223 | } |
| 224 | |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 225 | static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio, |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 226 | sector_t lba, bool *from_cache) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 227 | { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 228 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, rqd->meta_list, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 229 | struct ppa_addr ppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 230 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 231 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1, from_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 232 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 233 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 234 | atomic_long_inc(&pblk->inflight_reads); |
| 235 | #endif |
| 236 | |
| 237 | retry: |
| 238 | if (pblk_ppa_empty(ppa)) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 239 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 240 | |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 241 | meta->lba = addr_empty; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
| 245 | /* Try to read from write buffer. The address is later checked on the |
| 246 | * write buffer to prevent retrieving overwritten data. |
| 247 | */ |
| 248 | if (pblk_addr_in_cache(ppa)) { |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 249 | if (!pblk_read_from_cache(pblk, bio, lba, ppa)) { |
| 250 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1, from_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 251 | goto retry; |
| 252 | } |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 253 | |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 254 | meta->lba = cpu_to_le64(lba); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 255 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 256 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 257 | atomic_long_inc(&pblk->cache_reads); |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 258 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 259 | } else { |
| 260 | rqd->ppa_addr = ppa; |
| 261 | } |
| 262 | } |
| 263 | |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 264 | void pblk_submit_read(struct pblk *pblk, struct bio *bio) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 265 | { |
| 266 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 267 | struct request_queue *q = dev->q; |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 268 | sector_t blba = pblk_get_lba(bio); |
Dan Carpenter | 5bf1e1ee | 2017-04-21 16:48:40 -0600 | [diff] [blame] | 269 | unsigned int nr_secs = pblk_get_secs(bio); |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 270 | bool from_cache; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 271 | struct pblk_g_ctx *r_ctx; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 272 | struct nvm_rq *rqd; |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 273 | struct bio *int_bio, *split_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 274 | |
Michael Callahan | ddcf35d | 2018-07-18 04:47:39 -0700 | [diff] [blame] | 275 | generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio), |
| 276 | &pblk->disk->part0); |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 277 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 278 | rqd = pblk_alloc_rqd(pblk, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 279 | |
| 280 | rqd->opcode = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 281 | rqd->nr_ppas = nr_secs; |
| 282 | rqd->private = pblk; |
| 283 | rqd->end_io = pblk_end_io_read; |
| 284 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 285 | r_ctx = nvm_rq_to_pdu(rqd); |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 286 | r_ctx->start_time = jiffies; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 287 | r_ctx->lba = blba; |
| 288 | |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 289 | if (pblk_alloc_rqd_meta(pblk, rqd)) { |
| 290 | bio_io_error(bio); |
| 291 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | /* Clone read bio to deal internally with: |
| 296 | * -read errors when reading from drive |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 297 | * -bio_advance() calls during cache reads |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 298 | */ |
| 299 | int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 300 | |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 301 | if (nr_secs > 1) |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 302 | nr_secs = pblk_read_ppalist_rq(pblk, rqd, int_bio, blba, |
| 303 | &from_cache); |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 304 | else |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 305 | pblk_read_rq(pblk, rqd, int_bio, blba, &from_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 306 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 307 | split_retry: |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 308 | r_ctx->private = bio; /* original bio */ |
| 309 | rqd->bio = int_bio; /* internal bio */ |
| 310 | |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 311 | if (from_cache && nr_secs == rqd->nr_ppas) { |
| 312 | /* All data was read from cache, we can complete the IO. */ |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 313 | pblk_end_user_read(bio, 0); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 314 | atomic_inc(&pblk->inflight_io); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 315 | __pblk_end_io_read(pblk, rqd, false); |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 316 | } else if (nr_secs != rqd->nr_ppas) { |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 317 | /* The read bio request could be partially filled by the write |
| 318 | * buffer, but there are some holes that need to be read from |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 319 | * the drive. In order to handle this, we will use block layer |
| 320 | * mechanism to split this request in to smaller ones and make |
| 321 | * a chain of it. |
| 322 | */ |
| 323 | split_bio = bio_split(bio, nr_secs * NR_PHY_IN_LOG, GFP_KERNEL, |
| 324 | &pblk_bio_set); |
| 325 | bio_chain(split_bio, bio); |
| 326 | generic_make_request(bio); |
| 327 | |
| 328 | /* New bio contains first N sectors of the previous one, so |
| 329 | * we can continue to use existing rqd, but we need to shrink |
| 330 | * the number of PPAs in it. New bio is also guaranteed that |
| 331 | * it contains only either data from cache or from drive, newer |
| 332 | * mix of them. |
| 333 | */ |
| 334 | bio = split_bio; |
| 335 | rqd->nr_ppas = nr_secs; |
| 336 | if (rqd->nr_ppas == 1) |
| 337 | rqd->ppa_addr = rqd->ppa_list[0]; |
| 338 | |
| 339 | /* Recreate int_bio - existing might have some needed internal |
| 340 | * fields modified already. |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame] | 341 | */ |
| 342 | bio_put(int_bio); |
Igor Konopko | a96de64 | 2019-05-04 20:38:10 +0200 | [diff] [blame] | 343 | int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set); |
| 344 | goto split_retry; |
| 345 | } else if (pblk_submit_io(pblk, rqd)) { |
| 346 | /* Submitting IO to drive failed, let's report an error */ |
| 347 | rqd->error = -ENODEV; |
| 348 | pblk_end_io_read(rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 349 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
| 353 | struct pblk_line *line, u64 *lba_list, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 354 | u64 *paddr_list_gc, unsigned int nr_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 355 | { |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame] | 356 | struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA]; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 357 | struct ppa_addr ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 358 | int valid_secs = 0; |
| 359 | int i; |
| 360 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 361 | pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 362 | |
| 363 | for (i = 0; i < nr_secs; i++) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 364 | if (lba_list[i] == ADDR_EMPTY) |
| 365 | continue; |
| 366 | |
| 367 | ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id); |
| 368 | if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) { |
| 369 | paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 370 | continue; |
| 371 | } |
| 372 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 373 | rqd->ppa_list[valid_secs++] = ppa_list_l2p[i]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 376 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 377 | atomic_long_add(valid_secs, &pblk->inflight_reads); |
| 378 | #endif |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 379 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 380 | return valid_secs; |
| 381 | } |
| 382 | |
| 383 | static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 384 | struct pblk_line *line, sector_t lba, |
| 385 | u64 paddr_gc) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 386 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 387 | struct ppa_addr ppa_l2p, ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 388 | int valid_secs = 0; |
| 389 | |
Dan Carpenter | 659226e | 2017-04-21 16:49:56 -0600 | [diff] [blame] | 390 | if (lba == ADDR_EMPTY) |
| 391 | goto out; |
| 392 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 393 | /* logic error: lba out-of-bounds */ |
Igor Konopko | 847a3a2 | 2019-05-04 20:37:48 +0200 | [diff] [blame] | 394 | if (lba >= pblk->capacity) { |
Dan Carpenter | 2a79efd | 2017-04-15 20:55:52 +0200 | [diff] [blame] | 395 | WARN(1, "pblk: read lba out of bounds\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 396 | goto out; |
| 397 | } |
| 398 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 399 | spin_lock(&pblk->trans_lock); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 400 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 401 | spin_unlock(&pblk->trans_lock); |
| 402 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 403 | ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id); |
| 404 | if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 405 | goto out; |
| 406 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 407 | rqd->ppa_addr = ppa_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 408 | valid_secs = 1; |
| 409 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 410 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 411 | atomic_long_inc(&pblk->inflight_reads); |
| 412 | #endif |
| 413 | |
| 414 | out: |
| 415 | return valid_secs; |
| 416 | } |
| 417 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 418 | int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 419 | { |
| 420 | struct nvm_tgt_dev *dev = pblk->dev; |
| 421 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 422 | struct bio *bio; |
| 423 | struct nvm_rq rqd; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 424 | int data_len; |
| 425 | int ret = NVM_IO_OK; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 426 | |
| 427 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 428 | |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 429 | ret = pblk_alloc_rqd_meta(pblk, &rqd); |
| 430 | if (ret) |
| 431 | return ret; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 432 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 433 | if (gc_rq->nr_secs > 1) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 434 | gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line, |
| 435 | gc_rq->lba_list, |
| 436 | gc_rq->paddr_list, |
| 437 | gc_rq->nr_secs); |
| 438 | if (gc_rq->secs_to_gc == 1) |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 439 | rqd.ppa_addr = rqd.ppa_list[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 440 | } else { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 441 | gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line, |
| 442 | gc_rq->lba_list[0], |
| 443 | gc_rq->paddr_list[0]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 446 | if (!(gc_rq->secs_to_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 447 | goto out; |
| 448 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 449 | data_len = (gc_rq->secs_to_gc) * geo->csecs; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 450 | bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len, |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 451 | PBLK_VMALLOC_META, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 452 | if (IS_ERR(bio)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 453 | pblk_err(pblk, "could not allocate GC bio (%lu)\n", |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 454 | PTR_ERR(bio)); |
| 455 | ret = PTR_ERR(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 456 | goto err_free_dma; |
| 457 | } |
| 458 | |
| 459 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 460 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 461 | |
| 462 | rqd.opcode = NVM_OP_PREAD; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 463 | rqd.nr_ppas = gc_rq->secs_to_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 464 | rqd.bio = bio; |
| 465 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 466 | if (pblk_submit_io_sync(pblk, &rqd)) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 467 | ret = -EIO; |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 468 | goto err_free_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 471 | pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 472 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 473 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 474 | |
| 475 | if (rqd.error) { |
| 476 | atomic_long_inc(&pblk->read_failed_gc); |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 477 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 478 | pblk_print_failed_rqd(pblk, &rqd, rqd.error); |
| 479 | #endif |
| 480 | } |
| 481 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 482 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 483 | atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads); |
| 484 | atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads); |
| 485 | atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 486 | #endif |
| 487 | |
| 488 | out: |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 489 | pblk_free_rqd_meta(pblk, &rqd); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 490 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 491 | |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 492 | err_free_bio: |
| 493 | bio_put(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 494 | err_free_dma: |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 495 | pblk_free_rqd_meta(pblk, &rqd); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 496 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 497 | } |