Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 CNEX Labs |
| 3 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> |
| 4 | * Matias Bjorling <matias@cnexlabs.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version |
| 8 | * 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * pblk-read.c - pblk's read path |
| 16 | */ |
| 17 | |
| 18 | #include "pblk.h" |
| 19 | |
| 20 | /* |
| 21 | * There is no guarantee that the value read from cache has not been updated and |
| 22 | * resides at another location in the cache. We guarantee though that if the |
| 23 | * value is read from the cache, it belongs to the mapped lba. In order to |
| 24 | * guarantee and order between writes and reads are ordered, a flush must be |
| 25 | * issued. |
| 26 | */ |
| 27 | static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio, |
| 28 | sector_t lba, struct ppa_addr ppa, |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 29 | int bio_iter, bool advanced_bio) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 30 | { |
| 31 | #ifdef CONFIG_NVM_DEBUG |
| 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 | |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 37 | return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa, |
| 38 | bio_iter, advanced_bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 42 | sector_t blba, unsigned long *read_bitmap) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 43 | { |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 44 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 45 | struct bio *bio = rqd->bio; |
| 46 | struct ppa_addr ppas[PBLK_MAX_REQ_ADDRS]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 47 | int nr_secs = rqd->nr_ppas; |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 48 | bool advanced_bio = false; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 49 | int i, j = 0; |
| 50 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 51 | pblk_lookup_l2p_seq(pblk, ppas, blba, nr_secs); |
| 52 | |
| 53 | for (i = 0; i < nr_secs; i++) { |
| 54 | struct ppa_addr p = ppas[i]; |
| 55 | sector_t lba = blba + i; |
| 56 | |
| 57 | retry: |
| 58 | if (pblk_ppa_empty(p)) { |
| 59 | WARN_ON(test_and_set_bit(i, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 60 | meta_list[i].lba = cpu_to_le64(ADDR_EMPTY); |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 61 | |
| 62 | if (unlikely(!advanced_bio)) { |
| 63 | bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE); |
| 64 | advanced_bio = true; |
| 65 | } |
| 66 | |
| 67 | goto next; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* Try to read from write buffer. The address is later checked |
| 71 | * on the write buffer to prevent retrieving overwritten data. |
| 72 | */ |
| 73 | if (pblk_addr_in_cache(p)) { |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 74 | if (!pblk_read_from_cache(pblk, bio, lba, p, i, |
| 75 | advanced_bio)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 76 | pblk_lookup_l2p_seq(pblk, &p, lba, 1); |
| 77 | goto retry; |
| 78 | } |
| 79 | WARN_ON(test_and_set_bit(i, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 80 | meta_list[i].lba = cpu_to_le64(lba); |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 81 | advanced_bio = true; |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_NVM_DEBUG |
| 83 | atomic_long_inc(&pblk->cache_reads); |
| 84 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 85 | } else { |
| 86 | /* Read from media non-cached sectors */ |
| 87 | rqd->ppa_list[j++] = p; |
| 88 | } |
| 89 | |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 90 | next: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 91 | if (advanced_bio) |
| 92 | bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE); |
| 93 | } |
| 94 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 95 | if (pblk_io_aligned(pblk, nr_secs)) |
| 96 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
| 97 | else |
| 98 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
| 99 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_NVM_DEBUG |
| 101 | atomic_long_add(nr_secs, &pblk->inflight_reads); |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | static int pblk_submit_read_io(struct pblk *pblk, struct nvm_rq *rqd) |
| 106 | { |
| 107 | int err; |
| 108 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 109 | err = pblk_submit_io(pblk, rqd); |
| 110 | if (err) |
| 111 | return NVM_IO_ERR; |
| 112 | |
| 113 | return NVM_IO_OK; |
| 114 | } |
| 115 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 116 | static void pblk_read_check(struct pblk *pblk, struct nvm_rq *rqd, |
| 117 | sector_t blba) |
| 118 | { |
| 119 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
| 120 | int nr_lbas = rqd->nr_ppas; |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < nr_lbas; i++) { |
| 124 | u64 lba = le64_to_cpu(meta_list[i].lba); |
| 125 | |
| 126 | if (lba == ADDR_EMPTY) |
| 127 | continue; |
| 128 | |
| 129 | WARN(lba != blba + i, "pblk: corrupted read LBA\n"); |
| 130 | } |
| 131 | } |
| 132 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 133 | static void pblk_read_put_rqd_kref(struct pblk *pblk, struct nvm_rq *rqd) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 134 | { |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 135 | struct ppa_addr *ppa_list; |
| 136 | int i; |
| 137 | |
| 138 | ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr; |
| 139 | |
| 140 | for (i = 0; i < rqd->nr_ppas; i++) { |
| 141 | struct ppa_addr ppa = ppa_list[i]; |
| 142 | struct pblk_line *line; |
| 143 | |
| 144 | line = &pblk->lines[pblk_dev_ppa_to_line(ppa)]; |
| 145 | kref_put(&line->ref, pblk_line_put_wq); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void pblk_end_user_read(struct bio *bio) |
| 150 | { |
| 151 | #ifdef CONFIG_NVM_DEBUG |
| 152 | WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n"); |
| 153 | #endif |
| 154 | bio_endio(bio); |
| 155 | bio_put(bio); |
| 156 | } |
| 157 | |
| 158 | static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd, |
| 159 | bool put_line) |
| 160 | { |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 161 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 162 | struct bio *bio = rqd->bio; |
| 163 | |
| 164 | if (rqd->error) |
| 165 | pblk_log_read_err(pblk, rqd); |
| 166 | #ifdef CONFIG_NVM_DEBUG |
| 167 | else |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 168 | WARN_ONCE(bio->bi_status, "pblk: corrupted read error\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 169 | #endif |
| 170 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 171 | pblk_read_check(pblk, rqd, r_ctx->lba); |
| 172 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 173 | bio_put(bio); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 174 | if (r_ctx->private) |
| 175 | pblk_end_user_read((struct bio *)r_ctx->private); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 176 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 177 | if (put_line) |
| 178 | pblk_read_put_rqd_kref(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 179 | |
| 180 | #ifdef CONFIG_NVM_DEBUG |
| 181 | atomic_long_add(rqd->nr_ppas, &pblk->sync_reads); |
| 182 | atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads); |
| 183 | #endif |
| 184 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 185 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 186 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 189 | static void pblk_end_io_read(struct nvm_rq *rqd) |
| 190 | { |
| 191 | struct pblk *pblk = rqd->private; |
| 192 | |
| 193 | __pblk_end_io_read(pblk, rqd, true); |
| 194 | } |
| 195 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 196 | static int pblk_fill_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd, |
| 197 | unsigned int bio_init_idx, |
| 198 | unsigned long *read_bitmap) |
| 199 | { |
| 200 | struct bio *new_bio, *bio = rqd->bio; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 201 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 202 | struct bio_vec src_bv, dst_bv; |
| 203 | void *ppa_ptr = NULL; |
| 204 | void *src_p, *dst_p; |
| 205 | dma_addr_t dma_ppa_list = 0; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 206 | __le64 *lba_list_mem, *lba_list_media; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 207 | int nr_secs = rqd->nr_ppas; |
| 208 | int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs); |
| 209 | int i, ret, hole; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 210 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 211 | /* Re-use allocated memory for intermediate lbas */ |
| 212 | lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size); |
| 213 | lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size); |
| 214 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 215 | new_bio = bio_alloc(GFP_KERNEL, nr_holes); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 216 | |
| 217 | if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes)) |
| 218 | goto err; |
| 219 | |
| 220 | if (nr_holes != new_bio->bi_vcnt) { |
| 221 | pr_err("pblk: malformed bio\n"); |
| 222 | goto err; |
| 223 | } |
| 224 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 225 | for (i = 0; i < nr_secs; i++) |
| 226 | lba_list_mem[i] = meta_list[i].lba; |
| 227 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 228 | new_bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 229 | bio_set_op_attrs(new_bio, REQ_OP_READ, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 230 | |
| 231 | rqd->bio = new_bio; |
| 232 | rqd->nr_ppas = nr_holes; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 233 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 234 | |
Javier González | 0f9248c | 2017-10-13 14:46:24 +0200 | [diff] [blame] | 235 | if (unlikely(nr_holes == 1)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 236 | ppa_ptr = rqd->ppa_list; |
| 237 | dma_ppa_list = rqd->dma_ppa_list; |
| 238 | rqd->ppa_addr = rqd->ppa_list[0]; |
| 239 | } |
| 240 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 241 | ret = pblk_submit_io_sync(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 242 | if (ret) { |
| 243 | bio_put(rqd->bio); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 244 | pr_err("pblk: sync read IO submission failed\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 245 | goto err; |
| 246 | } |
| 247 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 248 | if (rqd->error) { |
| 249 | atomic_long_inc(&pblk->read_failed); |
| 250 | #ifdef CONFIG_NVM_DEBUG |
| 251 | pblk_print_failed_rqd(pblk, rqd, rqd->error); |
| 252 | #endif |
| 253 | } |
| 254 | |
Javier González | 0f9248c | 2017-10-13 14:46:24 +0200 | [diff] [blame] | 255 | if (unlikely(nr_holes == 1)) { |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 256 | struct ppa_addr ppa; |
| 257 | |
| 258 | ppa = rqd->ppa_addr; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 259 | rqd->ppa_list = ppa_ptr; |
| 260 | rqd->dma_ppa_list = dma_ppa_list; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 261 | rqd->ppa_list[0] = ppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 264 | for (i = 0; i < nr_secs; i++) { |
| 265 | lba_list_media[i] = meta_list[i].lba; |
| 266 | meta_list[i].lba = lba_list_mem[i]; |
| 267 | } |
| 268 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 269 | /* Fill the holes in the original bio */ |
| 270 | i = 0; |
| 271 | hole = find_first_zero_bit(read_bitmap, nr_secs); |
| 272 | do { |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 273 | int line_id = pblk_dev_ppa_to_line(rqd->ppa_list[i]); |
| 274 | struct pblk_line *line = &pblk->lines[line_id]; |
| 275 | |
| 276 | kref_put(&line->ref, pblk_line_put); |
| 277 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 278 | meta_list[hole].lba = lba_list_media[i]; |
| 279 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 280 | src_bv = new_bio->bi_io_vec[i++]; |
| 281 | dst_bv = bio->bi_io_vec[bio_init_idx + hole]; |
| 282 | |
| 283 | src_p = kmap_atomic(src_bv.bv_page); |
| 284 | dst_p = kmap_atomic(dst_bv.bv_page); |
| 285 | |
| 286 | memcpy(dst_p + dst_bv.bv_offset, |
| 287 | src_p + src_bv.bv_offset, |
| 288 | PBLK_EXPOSED_PAGE_SIZE); |
| 289 | |
| 290 | kunmap_atomic(src_p); |
| 291 | kunmap_atomic(dst_p); |
| 292 | |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 293 | mempool_free(src_bv.bv_page, pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 294 | |
| 295 | hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1); |
| 296 | } while (hole < nr_secs); |
| 297 | |
| 298 | bio_put(new_bio); |
| 299 | |
| 300 | /* Complete the original bio and associated request */ |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 301 | bio_endio(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 302 | rqd->bio = bio; |
| 303 | rqd->nr_ppas = nr_secs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 304 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 305 | __pblk_end_io_read(pblk, rqd, false); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 306 | return NVM_IO_OK; |
| 307 | |
| 308 | err: |
| 309 | /* Free allocated pages in new bio */ |
| 310 | pblk_bio_free_pages(pblk, bio, 0, new_bio->bi_vcnt); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 311 | __pblk_end_io_read(pblk, rqd, false); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 312 | return NVM_IO_ERR; |
| 313 | } |
| 314 | |
| 315 | static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 316 | sector_t lba, unsigned long *read_bitmap) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 317 | { |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 318 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 319 | struct bio *bio = rqd->bio; |
| 320 | struct ppa_addr ppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 321 | |
| 322 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1); |
| 323 | |
| 324 | #ifdef CONFIG_NVM_DEBUG |
| 325 | atomic_long_inc(&pblk->inflight_reads); |
| 326 | #endif |
| 327 | |
| 328 | retry: |
| 329 | if (pblk_ppa_empty(ppa)) { |
| 330 | WARN_ON(test_and_set_bit(0, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 331 | meta_list[0].lba = cpu_to_le64(ADDR_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | |
| 335 | /* Try to read from write buffer. The address is later checked on the |
| 336 | * write buffer to prevent retrieving overwritten data. |
| 337 | */ |
| 338 | if (pblk_addr_in_cache(ppa)) { |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 339 | if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 340 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1); |
| 341 | goto retry; |
| 342 | } |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 343 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 344 | WARN_ON(test_and_set_bit(0, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 345 | meta_list[0].lba = cpu_to_le64(lba); |
| 346 | |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 347 | #ifdef CONFIG_NVM_DEBUG |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 348 | atomic_long_inc(&pblk->cache_reads); |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 349 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 350 | } else { |
| 351 | rqd->ppa_addr = ppa; |
| 352 | } |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 353 | |
| 354 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int pblk_submit_read(struct pblk *pblk, struct bio *bio) |
| 358 | { |
| 359 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 360 | sector_t blba = pblk_get_lba(bio); |
Dan Carpenter | 5bf1e1ee | 2017-04-21 16:48:40 -0600 | [diff] [blame] | 361 | unsigned int nr_secs = pblk_get_secs(bio); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 362 | struct pblk_g_ctx *r_ctx; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 363 | struct nvm_rq *rqd; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 364 | unsigned int bio_init_idx; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 365 | unsigned long read_bitmap; /* Max 64 ppas per request */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 366 | int ret = NVM_IO_ERR; |
| 367 | |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 368 | /* logic error: lba out-of-bounds. Ignore read request */ |
| 369 | if (blba >= pblk->rl.nr_secs || nr_secs > PBLK_MAX_REQ_ADDRS) { |
| 370 | WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n", |
| 371 | (unsigned long long)blba, nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 372 | return NVM_IO_ERR; |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 373 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 374 | |
| 375 | bitmap_zero(&read_bitmap, nr_secs); |
| 376 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 377 | rqd = pblk_alloc_rqd(pblk, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 378 | |
| 379 | rqd->opcode = NVM_OP_PREAD; |
| 380 | rqd->bio = bio; |
| 381 | rqd->nr_ppas = nr_secs; |
| 382 | rqd->private = pblk; |
| 383 | rqd->end_io = pblk_end_io_read; |
| 384 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 385 | r_ctx = nvm_rq_to_pdu(rqd); |
| 386 | r_ctx->lba = blba; |
| 387 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 388 | /* Save the index for this bio's start. This is needed in case |
| 389 | * we need to fill a partial read. |
| 390 | */ |
| 391 | bio_init_idx = pblk_get_bi_idx(bio); |
| 392 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 393 | rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 394 | &rqd->dma_meta_list); |
| 395 | if (!rqd->meta_list) { |
| 396 | pr_err("pblk: not able to allocate ppa list\n"); |
| 397 | goto fail_rqd_free; |
| 398 | } |
| 399 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 400 | if (nr_secs > 1) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 401 | rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size; |
| 402 | rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 403 | |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 404 | pblk_read_ppalist_rq(pblk, rqd, blba, &read_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 405 | } else { |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 406 | pblk_read_rq(pblk, rqd, blba, &read_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | bio_get(bio); |
| 410 | if (bitmap_full(&read_bitmap, nr_secs)) { |
| 411 | bio_endio(bio); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 412 | atomic_inc(&pblk->inflight_io); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 413 | __pblk_end_io_read(pblk, rqd, false); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 414 | return NVM_IO_OK; |
| 415 | } |
| 416 | |
| 417 | /* All sectors are to be read from the device */ |
| 418 | if (bitmap_empty(&read_bitmap, rqd->nr_ppas)) { |
| 419 | struct bio *int_bio = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 420 | |
| 421 | /* Clone read bio to deal with read errors internally */ |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 422 | int_bio = bio_clone_fast(bio, GFP_KERNEL, pblk_bio_set); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 423 | if (!int_bio) { |
| 424 | pr_err("pblk: could not clone read bio\n"); |
| 425 | return NVM_IO_ERR; |
| 426 | } |
| 427 | |
| 428 | rqd->bio = int_bio; |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 429 | r_ctx->private = bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 430 | |
| 431 | ret = pblk_submit_read_io(pblk, rqd); |
| 432 | if (ret) { |
| 433 | pr_err("pblk: read IO submission failed\n"); |
| 434 | if (int_bio) |
| 435 | bio_put(int_bio); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | return NVM_IO_OK; |
| 440 | } |
| 441 | |
| 442 | /* The read bio request could be partially filled by the write buffer, |
| 443 | * but there are some holes that need to be read from the drive. |
| 444 | */ |
| 445 | ret = pblk_fill_partial_read_bio(pblk, rqd, bio_init_idx, &read_bitmap); |
| 446 | if (ret) { |
| 447 | pr_err("pblk: failed to perform partial read\n"); |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | return NVM_IO_OK; |
| 452 | |
| 453 | fail_rqd_free: |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 454 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
| 459 | struct pblk_line *line, u64 *lba_list, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 460 | u64 *paddr_list_gc, unsigned int nr_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 461 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 462 | struct ppa_addr ppa_list_l2p[PBLK_MAX_REQ_ADDRS]; |
| 463 | struct ppa_addr ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 464 | int valid_secs = 0; |
| 465 | int i; |
| 466 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 467 | 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] | 468 | |
| 469 | for (i = 0; i < nr_secs; i++) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 470 | if (lba_list[i] == ADDR_EMPTY) |
| 471 | continue; |
| 472 | |
| 473 | ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id); |
| 474 | if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) { |
| 475 | paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 476 | continue; |
| 477 | } |
| 478 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 479 | rqd->ppa_list[valid_secs++] = ppa_list_l2p[i]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | #ifdef CONFIG_NVM_DEBUG |
| 483 | atomic_long_add(valid_secs, &pblk->inflight_reads); |
| 484 | #endif |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 485 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 486 | return valid_secs; |
| 487 | } |
| 488 | |
| 489 | 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] | 490 | struct pblk_line *line, sector_t lba, |
| 491 | u64 paddr_gc) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 492 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 493 | struct ppa_addr ppa_l2p, ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 494 | int valid_secs = 0; |
| 495 | |
Dan Carpenter | 659226e | 2017-04-21 16:49:56 -0600 | [diff] [blame] | 496 | if (lba == ADDR_EMPTY) |
| 497 | goto out; |
| 498 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 499 | /* logic error: lba out-of-bounds */ |
Dan Carpenter | 2a79efd | 2017-04-15 20:55:52 +0200 | [diff] [blame] | 500 | if (lba >= pblk->rl.nr_secs) { |
| 501 | WARN(1, "pblk: read lba out of bounds\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 502 | goto out; |
| 503 | } |
| 504 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 505 | spin_lock(&pblk->trans_lock); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 506 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 507 | spin_unlock(&pblk->trans_lock); |
| 508 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 509 | ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id); |
| 510 | if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 511 | goto out; |
| 512 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 513 | rqd->ppa_addr = ppa_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 514 | valid_secs = 1; |
| 515 | |
| 516 | #ifdef CONFIG_NVM_DEBUG |
| 517 | atomic_long_inc(&pblk->inflight_reads); |
| 518 | #endif |
| 519 | |
| 520 | out: |
| 521 | return valid_secs; |
| 522 | } |
| 523 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 524 | 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] | 525 | { |
| 526 | struct nvm_tgt_dev *dev = pblk->dev; |
| 527 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 528 | struct bio *bio; |
| 529 | struct nvm_rq rqd; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 530 | int data_len; |
| 531 | int ret = NVM_IO_OK; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 532 | |
| 533 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 534 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 535 | rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 536 | &rqd.dma_meta_list); |
| 537 | if (!rqd.meta_list) |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 538 | return -ENOMEM; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 539 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 540 | if (gc_rq->nr_secs > 1) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 541 | rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size; |
| 542 | rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 543 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 544 | gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line, |
| 545 | gc_rq->lba_list, |
| 546 | gc_rq->paddr_list, |
| 547 | gc_rq->nr_secs); |
| 548 | if (gc_rq->secs_to_gc == 1) |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 549 | rqd.ppa_addr = rqd.ppa_list[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 550 | } else { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 551 | gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line, |
| 552 | gc_rq->lba_list[0], |
| 553 | gc_rq->paddr_list[0]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 556 | if (!(gc_rq->secs_to_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 557 | goto out; |
| 558 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 559 | data_len = (gc_rq->secs_to_gc) * geo->sec_size; |
| 560 | 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] | 561 | PBLK_VMALLOC_META, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 562 | if (IS_ERR(bio)) { |
| 563 | pr_err("pblk: could not allocate GC bio (%lu)\n", PTR_ERR(bio)); |
| 564 | goto err_free_dma; |
| 565 | } |
| 566 | |
| 567 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 568 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 569 | |
| 570 | rqd.opcode = NVM_OP_PREAD; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 571 | rqd.nr_ppas = gc_rq->secs_to_gc; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 572 | rqd.flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 573 | rqd.bio = bio; |
| 574 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 575 | if (pblk_submit_io_sync(pblk, &rqd)) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 576 | ret = -EIO; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 577 | pr_err("pblk: GC read request failed\n"); |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 578 | goto err_free_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 581 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 582 | |
| 583 | if (rqd.error) { |
| 584 | atomic_long_inc(&pblk->read_failed_gc); |
| 585 | #ifdef CONFIG_NVM_DEBUG |
| 586 | pblk_print_failed_rqd(pblk, &rqd, rqd.error); |
| 587 | #endif |
| 588 | } |
| 589 | |
| 590 | #ifdef CONFIG_NVM_DEBUG |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 591 | atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads); |
| 592 | atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads); |
| 593 | atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 594 | #endif |
| 595 | |
| 596 | out: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 597 | nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 598 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 599 | |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 600 | err_free_bio: |
| 601 | bio_put(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 602 | err_free_dma: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 603 | nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 604 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 605 | } |