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 | { |
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 | |
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 | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 42 | struct bio *bio, sector_t blba, |
| 43 | unsigned long *read_bitmap) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 44 | { |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 45 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame^] | 46 | struct ppa_addr ppas[NVM_MAX_VLBA]; |
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; |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 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)) |
Matias Bjørling | d7b6801 | 2018-10-09 13:11:32 +0200 | [diff] [blame] | 96 | rqd->is_seq = 1; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 97 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 99 | atomic_long_add(nr_secs, &pblk->inflight_reads); |
| 100 | #endif |
| 101 | } |
| 102 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 103 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 104 | static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd, |
| 105 | sector_t blba) |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 106 | { |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 107 | struct pblk_sec_meta *meta_lba_list = rqd->meta_list; |
| 108 | int nr_lbas = rqd->nr_ppas; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < nr_lbas; i++) { |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 112 | u64 lba = le64_to_cpu(meta_lba_list[i].lba); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 113 | |
| 114 | if (lba == ADDR_EMPTY) |
| 115 | continue; |
| 116 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 117 | if (lba != blba + i) { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 118 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 119 | struct ppa_addr *p; |
| 120 | |
| 121 | p = (nr_lbas == 1) ? &rqd->ppa_list[i] : &rqd->ppa_addr; |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 122 | print_ppa(pblk, p, "seq", i); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 123 | #endif |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 124 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 125 | lba, (u64)blba + i); |
| 126 | WARN_ON(1); |
| 127 | } |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 131 | /* |
| 132 | * There can be holes in the lba list. |
| 133 | */ |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 134 | static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd, |
| 135 | u64 *lba_list, int nr_lbas) |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 136 | { |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 137 | struct pblk_sec_meta *meta_lba_list = rqd->meta_list; |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 138 | int i, j; |
| 139 | |
| 140 | for (i = 0, j = 0; i < nr_lbas; i++) { |
| 141 | u64 lba = lba_list[i]; |
| 142 | u64 meta_lba; |
| 143 | |
| 144 | if (lba == ADDR_EMPTY) |
| 145 | continue; |
| 146 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 147 | meta_lba = le64_to_cpu(meta_lba_list[j].lba); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 148 | |
| 149 | if (lba != meta_lba) { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 151 | struct ppa_addr *p; |
| 152 | int nr_ppas = rqd->nr_ppas; |
| 153 | |
| 154 | p = (nr_ppas == 1) ? &rqd->ppa_list[j] : &rqd->ppa_addr; |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 155 | print_ppa(pblk, p, "seq", j); |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 156 | #endif |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 157 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 158 | lba, meta_lba); |
| 159 | WARN_ON(1); |
| 160 | } |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 161 | |
| 162 | j++; |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 163 | } |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 164 | |
| 165 | WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n"); |
Javier González | 310df58 | 2018-06-01 15:04:17 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 168 | 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] | 169 | { |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 170 | struct ppa_addr *ppa_list; |
| 171 | int i; |
| 172 | |
| 173 | ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr; |
| 174 | |
| 175 | for (i = 0; i < rqd->nr_ppas; i++) { |
| 176 | struct ppa_addr ppa = ppa_list[i]; |
| 177 | struct pblk_line *line; |
| 178 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 179 | line = &pblk->lines[pblk_ppa_to_line(ppa)]; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 180 | kref_put(&line->ref, pblk_line_put_wq); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static void pblk_end_user_read(struct bio *bio) |
| 185 | { |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 186 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 187 | WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n"); |
| 188 | #endif |
| 189 | bio_endio(bio); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd, |
| 193 | bool put_line) |
| 194 | { |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 195 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 196 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 197 | struct bio *int_bio = rqd->bio; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 198 | unsigned long start_time = r_ctx->start_time; |
| 199 | |
Michael Callahan | ddcf35d | 2018-07-18 04:47:39 -0700 | [diff] [blame] | 200 | 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] | 201 | |
| 202 | if (rqd->error) |
| 203 | pblk_log_read_err(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 204 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 205 | pblk_read_check_seq(pblk, rqd, r_ctx->lba); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 206 | |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 207 | if (int_bio) |
| 208 | bio_put(int_bio); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 209 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 210 | if (put_line) |
| 211 | pblk_read_put_rqd_kref(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 212 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 213 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 214 | atomic_long_add(rqd->nr_ppas, &pblk->sync_reads); |
| 215 | atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads); |
| 216 | #endif |
| 217 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 218 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 219 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 222 | static void pblk_end_io_read(struct nvm_rq *rqd) |
| 223 | { |
| 224 | struct pblk *pblk = rqd->private; |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 225 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 226 | struct bio *bio = (struct bio *)r_ctx->private; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 227 | |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 228 | pblk_end_user_read(bio); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 229 | __pblk_end_io_read(pblk, rqd, true); |
| 230 | } |
| 231 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 232 | static void pblk_end_partial_read(struct nvm_rq *rqd) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 233 | { |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 234 | struct pblk *pblk = rqd->private; |
| 235 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 236 | struct pblk_pr_ctx *pr_ctx = r_ctx->private; |
| 237 | struct bio *new_bio = rqd->bio; |
| 238 | struct bio *bio = pr_ctx->orig_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 239 | struct bio_vec src_bv, dst_bv; |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 240 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
| 241 | int bio_init_idx = pr_ctx->bio_init_idx; |
| 242 | unsigned long *read_bitmap = pr_ctx->bitmap; |
| 243 | int nr_secs = pr_ctx->orig_nr_secs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 244 | int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs); |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 245 | __le64 *lba_list_mem, *lba_list_media; |
| 246 | void *src_p, *dst_p; |
| 247 | int hole, i; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 248 | |
Javier González | 0f9248c | 2017-10-13 14:46:24 +0200 | [diff] [blame] | 249 | if (unlikely(nr_holes == 1)) { |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 250 | struct ppa_addr ppa; |
| 251 | |
| 252 | ppa = rqd->ppa_addr; |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 253 | rqd->ppa_list = pr_ctx->ppa_ptr; |
| 254 | rqd->dma_ppa_list = pr_ctx->dma_ppa_list; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 255 | rqd->ppa_list[0] = ppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 258 | /* Re-use allocated memory for intermediate lbas */ |
| 259 | lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size); |
| 260 | lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size); |
| 261 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 262 | for (i = 0; i < nr_secs; i++) { |
| 263 | lba_list_media[i] = meta_list[i].lba; |
| 264 | meta_list[i].lba = lba_list_mem[i]; |
| 265 | } |
| 266 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 267 | /* Fill the holes in the original bio */ |
| 268 | i = 0; |
| 269 | hole = find_first_zero_bit(read_bitmap, nr_secs); |
| 270 | do { |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 271 | int line_id = pblk_ppa_to_line(rqd->ppa_list[i]); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 272 | struct pblk_line *line = &pblk->lines[line_id]; |
| 273 | |
| 274 | kref_put(&line->ref, pblk_line_put); |
| 275 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 276 | meta_list[hole].lba = lba_list_media[i]; |
| 277 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 278 | src_bv = new_bio->bi_io_vec[i++]; |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 279 | dst_bv = bio->bi_io_vec[bio_init_idx + hole]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 280 | |
| 281 | src_p = kmap_atomic(src_bv.bv_page); |
| 282 | dst_p = kmap_atomic(dst_bv.bv_page); |
| 283 | |
| 284 | memcpy(dst_p + dst_bv.bv_offset, |
| 285 | src_p + src_bv.bv_offset, |
| 286 | PBLK_EXPOSED_PAGE_SIZE); |
| 287 | |
| 288 | kunmap_atomic(src_p); |
| 289 | kunmap_atomic(dst_p); |
| 290 | |
Kent Overstreet | b906bbb | 2018-05-20 18:25:50 -0400 | [diff] [blame] | 291 | mempool_free(src_bv.bv_page, &pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 292 | |
| 293 | hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1); |
| 294 | } while (hole < nr_secs); |
| 295 | |
| 296 | bio_put(new_bio); |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 297 | kfree(pr_ctx); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 298 | |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 299 | /* restore original request */ |
| 300 | rqd->bio = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 301 | rqd->nr_ppas = nr_secs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 302 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 303 | bio_endio(bio); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 304 | __pblk_end_io_read(pblk, rqd, false); |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 305 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 306 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 307 | static int pblk_setup_partial_read(struct pblk *pblk, struct nvm_rq *rqd, |
| 308 | unsigned int bio_init_idx, |
| 309 | unsigned long *read_bitmap, |
| 310 | int nr_holes) |
| 311 | { |
| 312 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
| 313 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 314 | struct pblk_pr_ctx *pr_ctx; |
| 315 | struct bio *new_bio, *bio = r_ctx->private; |
| 316 | __le64 *lba_list_mem; |
| 317 | int nr_secs = rqd->nr_ppas; |
| 318 | int i; |
| 319 | |
| 320 | /* Re-use allocated memory for intermediate lbas */ |
| 321 | lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size); |
| 322 | |
| 323 | new_bio = bio_alloc(GFP_KERNEL, nr_holes); |
| 324 | |
| 325 | if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes)) |
| 326 | goto fail_bio_put; |
| 327 | |
| 328 | if (nr_holes != new_bio->bi_vcnt) { |
| 329 | WARN_ONCE(1, "pblk: malformed bio\n"); |
| 330 | goto fail_free_pages; |
| 331 | } |
| 332 | |
| 333 | pr_ctx = kmalloc(sizeof(struct pblk_pr_ctx), GFP_KERNEL); |
| 334 | if (!pr_ctx) |
| 335 | goto fail_free_pages; |
| 336 | |
| 337 | for (i = 0; i < nr_secs; i++) |
| 338 | lba_list_mem[i] = meta_list[i].lba; |
| 339 | |
| 340 | new_bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 341 | bio_set_op_attrs(new_bio, REQ_OP_READ, 0); |
| 342 | |
| 343 | rqd->bio = new_bio; |
| 344 | rqd->nr_ppas = nr_holes; |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 345 | |
| 346 | pr_ctx->ppa_ptr = NULL; |
| 347 | pr_ctx->orig_bio = bio; |
| 348 | bitmap_copy(pr_ctx->bitmap, read_bitmap, NVM_MAX_VLBA); |
| 349 | pr_ctx->bio_init_idx = bio_init_idx; |
| 350 | pr_ctx->orig_nr_secs = nr_secs; |
| 351 | r_ctx->private = pr_ctx; |
| 352 | |
| 353 | if (unlikely(nr_holes == 1)) { |
| 354 | pr_ctx->ppa_ptr = rqd->ppa_list; |
| 355 | pr_ctx->dma_ppa_list = rqd->dma_ppa_list; |
| 356 | rqd->ppa_addr = rqd->ppa_list[0]; |
| 357 | } |
| 358 | return 0; |
| 359 | |
| 360 | fail_free_pages: |
Igor Konopko | fbadca7 | 2018-06-01 16:41:09 +0200 | [diff] [blame] | 361 | pblk_bio_free_pages(pblk, new_bio, 0, new_bio->bi_vcnt); |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 362 | fail_bio_put: |
| 363 | bio_put(new_bio); |
| 364 | |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | |
| 368 | static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd, |
| 369 | unsigned int bio_init_idx, |
| 370 | unsigned long *read_bitmap, int nr_secs) |
| 371 | { |
| 372 | int nr_holes; |
| 373 | int ret; |
| 374 | |
| 375 | nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs); |
| 376 | |
| 377 | if (pblk_setup_partial_read(pblk, rqd, bio_init_idx, read_bitmap, |
| 378 | nr_holes)) |
| 379 | return NVM_IO_ERR; |
| 380 | |
| 381 | rqd->end_io = pblk_end_partial_read; |
| 382 | |
| 383 | ret = pblk_submit_io(pblk, rqd); |
| 384 | if (ret) { |
| 385 | bio_put(rqd->bio); |
| 386 | pblk_err(pblk, "partial read IO submission failed\n"); |
| 387 | goto err; |
| 388 | } |
| 389 | |
| 390 | return NVM_IO_OK; |
| 391 | |
| 392 | err: |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 393 | pblk_err(pblk, "failed to perform partial read\n"); |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 394 | |
| 395 | /* Free allocated pages in new bio */ |
| 396 | pblk_bio_free_pages(pblk, rqd->bio, 0, rqd->bio->bi_vcnt); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 397 | __pblk_end_io_read(pblk, rqd, false); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 398 | return NVM_IO_ERR; |
| 399 | } |
| 400 | |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 401 | static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio, |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 402 | sector_t lba, unsigned long *read_bitmap) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 403 | { |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 404 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 405 | struct ppa_addr ppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 406 | |
| 407 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1); |
| 408 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 409 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 410 | atomic_long_inc(&pblk->inflight_reads); |
| 411 | #endif |
| 412 | |
| 413 | retry: |
| 414 | if (pblk_ppa_empty(ppa)) { |
| 415 | WARN_ON(test_and_set_bit(0, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 416 | meta_list[0].lba = cpu_to_le64(ADDR_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| 420 | /* Try to read from write buffer. The address is later checked on the |
| 421 | * write buffer to prevent retrieving overwritten data. |
| 422 | */ |
| 423 | if (pblk_addr_in_cache(ppa)) { |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 424 | if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 425 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1); |
| 426 | goto retry; |
| 427 | } |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 428 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 429 | WARN_ON(test_and_set_bit(0, read_bitmap)); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 430 | meta_list[0].lba = cpu_to_le64(lba); |
| 431 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 432 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 433 | atomic_long_inc(&pblk->cache_reads); |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 434 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 435 | } else { |
| 436 | rqd->ppa_addr = ppa; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | int pblk_submit_read(struct pblk *pblk, struct bio *bio) |
| 441 | { |
| 442 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 443 | struct request_queue *q = dev->q; |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 444 | sector_t blba = pblk_get_lba(bio); |
Dan Carpenter | 5bf1e1ee | 2017-04-21 16:48:40 -0600 | [diff] [blame] | 445 | unsigned int nr_secs = pblk_get_secs(bio); |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 446 | struct pblk_g_ctx *r_ctx; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 447 | struct nvm_rq *rqd; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 448 | unsigned int bio_init_idx; |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 449 | DECLARE_BITMAP(read_bitmap, NVM_MAX_VLBA); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 450 | int ret = NVM_IO_ERR; |
| 451 | |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 452 | /* logic error: lba out-of-bounds. Ignore read request */ |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame^] | 453 | if (blba >= pblk->rl.nr_secs || nr_secs > NVM_MAX_VLBA) { |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 454 | WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n", |
| 455 | (unsigned long long)blba, nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 456 | return NVM_IO_ERR; |
Javier González | 84454e6 | 2017-10-13 14:46:13 +0200 | [diff] [blame] | 457 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 458 | |
Michael Callahan | ddcf35d | 2018-07-18 04:47:39 -0700 | [diff] [blame] | 459 | generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio), |
| 460 | &pblk->disk->part0); |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 461 | |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 462 | bitmap_zero(read_bitmap, nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 463 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 464 | rqd = pblk_alloc_rqd(pblk, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 465 | |
| 466 | rqd->opcode = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 467 | rqd->nr_ppas = nr_secs; |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 468 | rqd->bio = NULL; /* cloned bio if needed */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 469 | rqd->private = pblk; |
| 470 | rqd->end_io = pblk_end_io_read; |
| 471 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 472 | r_ctx = nvm_rq_to_pdu(rqd); |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 473 | r_ctx->start_time = jiffies; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 474 | r_ctx->lba = blba; |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 475 | r_ctx->private = bio; /* original bio */ |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 476 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 477 | /* Save the index for this bio's start. This is needed in case |
| 478 | * we need to fill a partial read. |
| 479 | */ |
| 480 | bio_init_idx = pblk_get_bi_idx(bio); |
| 481 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 482 | rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 483 | &rqd->dma_meta_list); |
| 484 | if (!rqd->meta_list) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 485 | pblk_err(pblk, "not able to allocate ppa list\n"); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 486 | goto fail_rqd_free; |
| 487 | } |
| 488 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 489 | if (nr_secs > 1) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 490 | rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size; |
| 491 | 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] | 492 | |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 493 | pblk_read_ppalist_rq(pblk, rqd, bio, blba, read_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 494 | } else { |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 495 | pblk_read_rq(pblk, rqd, bio, blba, read_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 498 | if (bitmap_full(read_bitmap, nr_secs)) { |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 499 | atomic_inc(&pblk->inflight_io); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 500 | __pblk_end_io_read(pblk, rqd, false); |
Javier González | 87cc40b | 2018-06-01 16:41:11 +0200 | [diff] [blame] | 501 | return NVM_IO_DONE; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* All sectors are to be read from the device */ |
Matias Bjørling | 921aebf | 2018-07-13 10:48:40 +0200 | [diff] [blame] | 505 | if (bitmap_empty(read_bitmap, rqd->nr_ppas)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 506 | struct bio *int_bio = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 507 | |
| 508 | /* Clone read bio to deal with read errors internally */ |
Kent Overstreet | b906bbb | 2018-05-20 18:25:50 -0400 | [diff] [blame] | 509 | int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 510 | if (!int_bio) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 511 | pblk_err(pblk, "could not clone read bio\n"); |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 512 | goto fail_end_io; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | rqd->bio = int_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 516 | |
Javier González | e13f421 | 2018-06-01 15:04:21 +0200 | [diff] [blame] | 517 | if (pblk_submit_io(pblk, rqd)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 518 | pblk_err(pblk, "read IO submission failed\n"); |
Javier González | e13f421 | 2018-06-01 15:04:21 +0200 | [diff] [blame] | 519 | ret = NVM_IO_ERR; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 520 | goto fail_end_io; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return NVM_IO_OK; |
| 524 | } |
| 525 | |
| 526 | /* The read bio request could be partially filled by the write buffer, |
| 527 | * but there are some holes that need to be read from the drive. |
| 528 | */ |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 529 | ret = pblk_partial_read_bio(pblk, rqd, bio_init_idx, read_bitmap, |
| 530 | nr_secs); |
| 531 | if (ret) |
| 532 | goto fail_meta_free; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 533 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 534 | return NVM_IO_OK; |
| 535 | |
| 536 | fail_meta_free: |
| 537 | nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 538 | fail_rqd_free: |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 539 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 540 | return ret; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 541 | fail_end_io: |
| 542 | __pblk_end_io_read(pblk, rqd, false); |
| 543 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
| 547 | struct pblk_line *line, u64 *lba_list, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 548 | u64 *paddr_list_gc, unsigned int nr_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 549 | { |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame^] | 550 | struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA]; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 551 | struct ppa_addr ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 552 | int valid_secs = 0; |
| 553 | int i; |
| 554 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 555 | 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] | 556 | |
| 557 | for (i = 0; i < nr_secs; i++) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 558 | if (lba_list[i] == ADDR_EMPTY) |
| 559 | continue; |
| 560 | |
| 561 | ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id); |
| 562 | if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) { |
| 563 | paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 567 | rqd->ppa_list[valid_secs++] = ppa_list_l2p[i]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 570 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 571 | atomic_long_add(valid_secs, &pblk->inflight_reads); |
| 572 | #endif |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 573 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 574 | return valid_secs; |
| 575 | } |
| 576 | |
| 577 | 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] | 578 | struct pblk_line *line, sector_t lba, |
| 579 | u64 paddr_gc) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 580 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 581 | struct ppa_addr ppa_l2p, ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 582 | int valid_secs = 0; |
| 583 | |
Dan Carpenter | 659226e | 2017-04-21 16:49:56 -0600 | [diff] [blame] | 584 | if (lba == ADDR_EMPTY) |
| 585 | goto out; |
| 586 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 587 | /* logic error: lba out-of-bounds */ |
Dan Carpenter | 2a79efd | 2017-04-15 20:55:52 +0200 | [diff] [blame] | 588 | if (lba >= pblk->rl.nr_secs) { |
| 589 | WARN(1, "pblk: read lba out of bounds\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 590 | goto out; |
| 591 | } |
| 592 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 593 | spin_lock(&pblk->trans_lock); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 594 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 595 | spin_unlock(&pblk->trans_lock); |
| 596 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 597 | ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id); |
| 598 | if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 599 | goto out; |
| 600 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 601 | rqd->ppa_addr = ppa_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 602 | valid_secs = 1; |
| 603 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 604 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 605 | atomic_long_inc(&pblk->inflight_reads); |
| 606 | #endif |
| 607 | |
| 608 | out: |
| 609 | return valid_secs; |
| 610 | } |
| 611 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 612 | 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] | 613 | { |
| 614 | struct nvm_tgt_dev *dev = pblk->dev; |
| 615 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 616 | struct bio *bio; |
| 617 | struct nvm_rq rqd; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 618 | int data_len; |
| 619 | int ret = NVM_IO_OK; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 620 | |
| 621 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 622 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 623 | rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 624 | &rqd.dma_meta_list); |
| 625 | if (!rqd.meta_list) |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 626 | return -ENOMEM; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 627 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 628 | if (gc_rq->nr_secs > 1) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 629 | rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size; |
| 630 | 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] | 631 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 632 | gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line, |
| 633 | gc_rq->lba_list, |
| 634 | gc_rq->paddr_list, |
| 635 | gc_rq->nr_secs); |
| 636 | if (gc_rq->secs_to_gc == 1) |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 637 | rqd.ppa_addr = rqd.ppa_list[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 638 | } else { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 639 | gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line, |
| 640 | gc_rq->lba_list[0], |
| 641 | gc_rq->paddr_list[0]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 644 | if (!(gc_rq->secs_to_gc)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 645 | goto out; |
| 646 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 647 | data_len = (gc_rq->secs_to_gc) * geo->csecs; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 648 | 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] | 649 | PBLK_VMALLOC_META, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 650 | if (IS_ERR(bio)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 651 | pblk_err(pblk, "could not allocate GC bio (%lu)\n", |
| 652 | PTR_ERR(bio)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 653 | goto err_free_dma; |
| 654 | } |
| 655 | |
| 656 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 657 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 658 | |
| 659 | rqd.opcode = NVM_OP_PREAD; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 660 | rqd.nr_ppas = gc_rq->secs_to_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 661 | rqd.bio = bio; |
| 662 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 663 | if (pblk_submit_io_sync(pblk, &rqd)) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 664 | ret = -EIO; |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 665 | pblk_err(pblk, "GC read request failed\n"); |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 666 | goto err_free_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Javier González | 03a34b2 | 2018-06-01 15:04:18 +0200 | [diff] [blame] | 669 | 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] | 670 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 671 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 672 | |
| 673 | if (rqd.error) { |
| 674 | atomic_long_inc(&pblk->read_failed_gc); |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 675 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 676 | pblk_print_failed_rqd(pblk, &rqd, rqd.error); |
| 677 | #endif |
| 678 | } |
| 679 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 680 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 681 | atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads); |
| 682 | atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads); |
| 683 | atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 684 | #endif |
| 685 | |
| 686 | out: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 687 | 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] | 688 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 689 | |
Javier González | 7d327a9 | 2017-10-13 14:46:02 +0200 | [diff] [blame] | 690 | err_free_bio: |
| 691 | bio_put(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 692 | err_free_dma: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 693 | 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] | 694 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 695 | } |