Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 CNEX Labs |
| 3 | * Initial: Javier Gonzalez <javier@cnexlabs.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License version |
| 7 | * 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * pblk-recovery.c - pblk's recovery path |
| 15 | */ |
| 16 | |
| 17 | #include "pblk.h" |
| 18 | |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 19 | int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta_buf) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 20 | { |
| 21 | u32 crc; |
| 22 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 23 | crc = pblk_calc_emeta_crc(pblk, emeta_buf); |
| 24 | if (le32_to_cpu(emeta_buf->crc) != crc) |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 25 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 26 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 27 | if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 28 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 29 | |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 30 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static int pblk_recov_l2p_from_emeta(struct pblk *pblk, struct pblk_line *line) |
| 34 | { |
| 35 | struct nvm_tgt_dev *dev = pblk->dev; |
| 36 | struct nvm_geo *geo = &dev->geo; |
| 37 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 38 | struct pblk_emeta *emeta = line->emeta; |
| 39 | struct line_emeta *emeta_buf = emeta->buf; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 40 | __le64 *lba_list; |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 41 | u64 data_start, data_end; |
| 42 | u64 nr_valid_lbas, nr_lbas = 0; |
| 43 | u64 i; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 44 | |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 45 | lba_list = emeta_to_lbas(pblk, emeta_buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 46 | if (!lba_list) |
| 47 | return 1; |
| 48 | |
| 49 | data_start = pblk_line_smeta_start(pblk, line) + lm->smeta_sec; |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 50 | data_end = line->emeta_ssec; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 51 | nr_valid_lbas = le64_to_cpu(emeta_buf->nr_valid_lbas); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 52 | |
Hans Holmberg | 9295709 | 2017-10-13 14:46:35 +0200 | [diff] [blame] | 53 | for (i = data_start; i < data_end; i++) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 54 | struct ppa_addr ppa; |
| 55 | int pos; |
| 56 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 57 | ppa = addr_to_gen_ppa(pblk, i, line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 58 | pos = pblk_ppa_to_pos(geo, ppa); |
| 59 | |
| 60 | /* Do not update bad blocks */ |
| 61 | if (test_bit(pos, line->blk_bitmap)) |
| 62 | continue; |
| 63 | |
| 64 | if (le64_to_cpu(lba_list[i]) == ADDR_EMPTY) { |
| 65 | spin_lock(&line->lock); |
| 66 | if (test_and_set_bit(i, line->invalid_bitmap)) |
Dan Carpenter | 2a79efd | 2017-04-15 20:55:52 +0200 | [diff] [blame] | 67 | WARN_ONCE(1, "pblk: rec. double invalidate:\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 68 | else |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 69 | le32_add_cpu(line->vsc, -1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 70 | spin_unlock(&line->lock); |
| 71 | |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | pblk_update_map(pblk, le64_to_cpu(lba_list[i]), ppa); |
| 76 | nr_lbas++; |
| 77 | } |
| 78 | |
| 79 | if (nr_valid_lbas != nr_lbas) |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 80 | pr_err("pblk: line %d - inconsistent lba list(%llu/%llu)\n", |
| 81 | line->id, nr_valid_lbas, nr_lbas); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 82 | |
| 83 | line->left_msecs = 0; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int pblk_calc_sec_in_line(struct pblk *pblk, struct pblk_line *line) |
| 89 | { |
| 90 | struct nvm_tgt_dev *dev = pblk->dev; |
| 91 | struct nvm_geo *geo = &dev->geo; |
| 92 | struct pblk_line_meta *lm = &pblk->lm; |
| 93 | int nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line); |
| 94 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 95 | return lm->sec_per_line - lm->smeta_sec - lm->emeta_sec[0] - |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 96 | nr_bb * geo->clba; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | struct pblk_recov_alloc { |
| 100 | struct ppa_addr *ppa_list; |
| 101 | struct pblk_sec_meta *meta_list; |
| 102 | struct nvm_rq *rqd; |
| 103 | void *data; |
| 104 | dma_addr_t dma_ppa_list; |
| 105 | dma_addr_t dma_meta_list; |
| 106 | }; |
| 107 | |
| 108 | static int pblk_recov_read_oob(struct pblk *pblk, struct pblk_line *line, |
| 109 | struct pblk_recov_alloc p, u64 r_ptr) |
| 110 | { |
| 111 | struct nvm_tgt_dev *dev = pblk->dev; |
| 112 | struct nvm_geo *geo = &dev->geo; |
| 113 | struct ppa_addr *ppa_list; |
| 114 | struct pblk_sec_meta *meta_list; |
| 115 | struct nvm_rq *rqd; |
| 116 | struct bio *bio; |
| 117 | void *data; |
| 118 | dma_addr_t dma_ppa_list, dma_meta_list; |
| 119 | u64 r_ptr_int; |
| 120 | int left_ppas; |
| 121 | int rq_ppas, rq_len; |
| 122 | int i, j; |
| 123 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 124 | |
| 125 | ppa_list = p.ppa_list; |
| 126 | meta_list = p.meta_list; |
| 127 | rqd = p.rqd; |
| 128 | data = p.data; |
| 129 | dma_ppa_list = p.dma_ppa_list; |
| 130 | dma_meta_list = p.dma_meta_list; |
| 131 | |
| 132 | left_ppas = line->cur_sec - r_ptr; |
| 133 | if (!left_ppas) |
| 134 | return 0; |
| 135 | |
| 136 | r_ptr_int = r_ptr; |
| 137 | |
| 138 | next_read_rq: |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 139 | memset(rqd, 0, pblk_g_rq_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 140 | |
| 141 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
| 142 | if (!rq_ppas) |
| 143 | rq_ppas = pblk->min_write_pgs; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 144 | rq_len = rq_ppas * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 145 | |
| 146 | bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL); |
| 147 | if (IS_ERR(bio)) |
| 148 | return PTR_ERR(bio); |
| 149 | |
| 150 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 151 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 152 | |
| 153 | rqd->bio = bio; |
| 154 | rqd->opcode = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 155 | rqd->meta_list = meta_list; |
| 156 | rqd->nr_ppas = rq_ppas; |
| 157 | rqd->ppa_list = ppa_list; |
| 158 | rqd->dma_ppa_list = dma_ppa_list; |
| 159 | rqd->dma_meta_list = dma_meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 160 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 161 | if (pblk_io_aligned(pblk, rq_ppas)) |
| 162 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
| 163 | else |
| 164 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
| 165 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 166 | for (i = 0; i < rqd->nr_ppas; ) { |
| 167 | struct ppa_addr ppa; |
| 168 | int pos; |
| 169 | |
| 170 | ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 171 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 172 | |
| 173 | while (test_bit(pos, line->blk_bitmap)) { |
| 174 | r_ptr_int += pblk->min_write_pgs; |
| 175 | ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 176 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | for (j = 0; j < pblk->min_write_pgs; j++, i++, r_ptr_int++) |
| 180 | rqd->ppa_list[i] = |
| 181 | addr_to_gen_ppa(pblk, r_ptr_int, line->id); |
| 182 | } |
| 183 | |
| 184 | /* If read fails, more padding is needed */ |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 185 | ret = pblk_submit_io_sync(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 186 | if (ret) { |
| 187 | pr_err("pblk: I/O submission failed: %d\n", ret); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 191 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 192 | |
| 193 | /* At this point, the read should not fail. If it does, it is a problem |
| 194 | * we cannot recover from here. Need FTL log. |
| 195 | */ |
Javier González | 5d201f0 | 2018-01-05 14:16:15 +0100 | [diff] [blame] | 196 | if (rqd->error && rqd->error != NVM_RSP_WARN_HIGHECC) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 197 | pr_err("pblk: L2P recovery failed (%d)\n", rqd->error); |
| 198 | return -EINTR; |
| 199 | } |
| 200 | |
| 201 | for (i = 0; i < rqd->nr_ppas; i++) { |
| 202 | u64 lba = le64_to_cpu(meta_list[i].lba); |
| 203 | |
| 204 | if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs) |
| 205 | continue; |
| 206 | |
| 207 | pblk_update_map(pblk, lba, rqd->ppa_list[i]); |
| 208 | } |
| 209 | |
| 210 | left_ppas -= rq_ppas; |
| 211 | if (left_ppas > 0) |
| 212 | goto next_read_rq; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 217 | static void pblk_recov_complete(struct kref *ref) |
| 218 | { |
| 219 | struct pblk_pad_rq *pad_rq = container_of(ref, struct pblk_pad_rq, ref); |
| 220 | |
| 221 | complete(&pad_rq->wait); |
| 222 | } |
| 223 | |
| 224 | static void pblk_end_io_recov(struct nvm_rq *rqd) |
| 225 | { |
| 226 | struct pblk_pad_rq *pad_rq = rqd->private; |
| 227 | struct pblk *pblk = pad_rq->pblk; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 228 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 229 | pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas); |
| 230 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 231 | pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 232 | |
| 233 | atomic_dec(&pblk->inflight_io); |
| 234 | kref_put(&pad_rq->ref, pblk_recov_complete); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 237 | static int pblk_recov_pad_oob(struct pblk *pblk, struct pblk_line *line, |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 238 | int left_ppas) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 239 | { |
| 240 | struct nvm_tgt_dev *dev = pblk->dev; |
| 241 | struct nvm_geo *geo = &dev->geo; |
| 242 | struct ppa_addr *ppa_list; |
| 243 | struct pblk_sec_meta *meta_list; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 244 | struct pblk_pad_rq *pad_rq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 245 | struct nvm_rq *rqd; |
| 246 | struct bio *bio; |
| 247 | void *data; |
| 248 | dma_addr_t dma_ppa_list, dma_meta_list; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 249 | __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 250 | u64 w_ptr = line->cur_sec; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 251 | int left_line_ppas, rq_ppas, rq_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 252 | int i, j; |
| 253 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 254 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 255 | spin_lock(&line->lock); |
| 256 | left_line_ppas = line->left_msecs; |
| 257 | spin_unlock(&line->lock); |
| 258 | |
| 259 | pad_rq = kmalloc(sizeof(struct pblk_pad_rq), GFP_KERNEL); |
| 260 | if (!pad_rq) |
| 261 | return -ENOMEM; |
| 262 | |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame^] | 263 | data = vzalloc(array_size(pblk->max_write_pgs, geo->csecs)); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 264 | if (!data) { |
| 265 | ret = -ENOMEM; |
| 266 | goto free_rq; |
| 267 | } |
| 268 | |
| 269 | pad_rq->pblk = pblk; |
| 270 | init_completion(&pad_rq->wait); |
| 271 | kref_init(&pad_rq->ref); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 272 | |
| 273 | next_pad_rq: |
| 274 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 275 | if (rq_ppas < pblk->min_write_pgs) { |
| 276 | pr_err("pblk: corrupted pad line %d\n", line->id); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 277 | goto fail_free_pad; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 280 | rq_len = rq_ppas * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 281 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 282 | meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list); |
| 283 | if (!meta_list) { |
| 284 | ret = -ENOMEM; |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 285 | goto fail_free_pad; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | ppa_list = (void *)(meta_list) + pblk_dma_meta_size; |
| 289 | dma_ppa_list = dma_meta_list + pblk_dma_meta_size; |
| 290 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 291 | bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len, |
| 292 | PBLK_VMALLOC_META, GFP_KERNEL); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 293 | if (IS_ERR(bio)) { |
| 294 | ret = PTR_ERR(bio); |
Javier González | 2942f50 | 2017-10-13 14:46:10 +0200 | [diff] [blame] | 295 | goto fail_free_meta; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 296 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 297 | |
| 298 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 299 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
| 300 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 301 | rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT); |
Javier González | 2942f50 | 2017-10-13 14:46:10 +0200 | [diff] [blame] | 302 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 303 | rqd->bio = bio; |
| 304 | rqd->opcode = NVM_OP_PWRITE; |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 305 | rqd->flags = pblk_set_progr_mode(pblk, PBLK_WRITE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 306 | rqd->meta_list = meta_list; |
| 307 | rqd->nr_ppas = rq_ppas; |
| 308 | rqd->ppa_list = ppa_list; |
| 309 | rqd->dma_ppa_list = dma_ppa_list; |
| 310 | rqd->dma_meta_list = dma_meta_list; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 311 | rqd->end_io = pblk_end_io_recov; |
| 312 | rqd->private = pad_rq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 313 | |
| 314 | for (i = 0; i < rqd->nr_ppas; ) { |
| 315 | struct ppa_addr ppa; |
| 316 | int pos; |
| 317 | |
| 318 | w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 319 | ppa = addr_to_gen_ppa(pblk, w_ptr, line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 320 | pos = pblk_ppa_to_pos(geo, ppa); |
| 321 | |
| 322 | while (test_bit(pos, line->blk_bitmap)) { |
| 323 | w_ptr += pblk->min_write_pgs; |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 324 | ppa = addr_to_gen_ppa(pblk, w_ptr, line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 325 | pos = pblk_ppa_to_pos(geo, ppa); |
| 326 | } |
| 327 | |
| 328 | for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) { |
| 329 | struct ppa_addr dev_ppa; |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 330 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 331 | |
| 332 | dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id); |
| 333 | |
| 334 | pblk_map_invalidate(pblk, dev_ppa); |
Javier González | caa69fa | 2017-06-26 11:57:12 +0200 | [diff] [blame] | 335 | lba_list[w_ptr] = meta_list[i].lba = addr_empty; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 336 | rqd->ppa_list[i] = dev_ppa; |
| 337 | } |
| 338 | } |
| 339 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 340 | kref_get(&pad_rq->ref); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 341 | pblk_down_page(pblk, rqd->ppa_list, rqd->nr_ppas); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 342 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 343 | ret = pblk_submit_io(pblk, rqd); |
| 344 | if (ret) { |
| 345 | pr_err("pblk: I/O submission failed: %d\n", ret); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 346 | pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas); |
| 347 | goto fail_free_bio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 350 | left_line_ppas -= rq_ppas; |
| 351 | left_ppas -= rq_ppas; |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 352 | if (left_ppas && left_line_ppas) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 353 | goto next_pad_rq; |
| 354 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 355 | kref_put(&pad_rq->ref, pblk_recov_complete); |
| 356 | |
| 357 | if (!wait_for_completion_io_timeout(&pad_rq->wait, |
| 358 | msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) { |
| 359 | pr_err("pblk: pad write timed out\n"); |
| 360 | ret = -ETIME; |
| 361 | } |
| 362 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 363 | if (!pblk_line_is_full(line)) |
| 364 | pr_err("pblk: corrupted padded line: %d\n", line->id); |
| 365 | |
| 366 | vfree(data); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 367 | free_rq: |
| 368 | kfree(pad_rq); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 369 | return ret; |
| 370 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 371 | fail_free_bio: |
| 372 | bio_put(bio); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 373 | fail_free_meta: |
| 374 | nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 375 | fail_free_pad: |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 376 | kfree(pad_rq); |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 377 | vfree(data); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 378 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* When this function is called, it means that not all upper pages have been |
| 382 | * written in a page that contains valid data. In order to recover this data, we |
| 383 | * first find the write pointer on the device, then we pad all necessary |
| 384 | * sectors, and finally attempt to read the valid data |
| 385 | */ |
| 386 | static int pblk_recov_scan_all_oob(struct pblk *pblk, struct pblk_line *line, |
| 387 | struct pblk_recov_alloc p) |
| 388 | { |
| 389 | struct nvm_tgt_dev *dev = pblk->dev; |
| 390 | struct nvm_geo *geo = &dev->geo; |
| 391 | struct ppa_addr *ppa_list; |
| 392 | struct pblk_sec_meta *meta_list; |
| 393 | struct nvm_rq *rqd; |
| 394 | struct bio *bio; |
| 395 | void *data; |
| 396 | dma_addr_t dma_ppa_list, dma_meta_list; |
| 397 | u64 w_ptr = 0, r_ptr; |
| 398 | int rq_ppas, rq_len; |
| 399 | int i, j; |
| 400 | int ret = 0; |
| 401 | int rec_round; |
| 402 | int left_ppas = pblk_calc_sec_in_line(pblk, line) - line->cur_sec; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 403 | |
| 404 | ppa_list = p.ppa_list; |
| 405 | meta_list = p.meta_list; |
| 406 | rqd = p.rqd; |
| 407 | data = p.data; |
| 408 | dma_ppa_list = p.dma_ppa_list; |
| 409 | dma_meta_list = p.dma_meta_list; |
| 410 | |
| 411 | /* we could recover up until the line write pointer */ |
| 412 | r_ptr = line->cur_sec; |
| 413 | rec_round = 0; |
| 414 | |
| 415 | next_rq: |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 416 | memset(rqd, 0, pblk_g_rq_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 417 | |
| 418 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
| 419 | if (!rq_ppas) |
| 420 | rq_ppas = pblk->min_write_pgs; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 421 | rq_len = rq_ppas * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 422 | |
| 423 | bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL); |
| 424 | if (IS_ERR(bio)) |
| 425 | return PTR_ERR(bio); |
| 426 | |
| 427 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 428 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 429 | |
| 430 | rqd->bio = bio; |
| 431 | rqd->opcode = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 432 | rqd->meta_list = meta_list; |
| 433 | rqd->nr_ppas = rq_ppas; |
| 434 | rqd->ppa_list = ppa_list; |
| 435 | rqd->dma_ppa_list = dma_ppa_list; |
| 436 | rqd->dma_meta_list = dma_meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 437 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 438 | if (pblk_io_aligned(pblk, rq_ppas)) |
| 439 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
| 440 | else |
| 441 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
| 442 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 443 | for (i = 0; i < rqd->nr_ppas; ) { |
| 444 | struct ppa_addr ppa; |
| 445 | int pos; |
| 446 | |
| 447 | w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs); |
| 448 | ppa = addr_to_gen_ppa(pblk, w_ptr, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 449 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 450 | |
| 451 | while (test_bit(pos, line->blk_bitmap)) { |
| 452 | w_ptr += pblk->min_write_pgs; |
| 453 | ppa = addr_to_gen_ppa(pblk, w_ptr, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 454 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) |
| 458 | rqd->ppa_list[i] = |
| 459 | addr_to_gen_ppa(pblk, w_ptr, line->id); |
| 460 | } |
| 461 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 462 | ret = pblk_submit_io_sync(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 463 | if (ret) { |
| 464 | pr_err("pblk: I/O submission failed: %d\n", ret); |
| 465 | return ret; |
| 466 | } |
| 467 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 468 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 469 | |
| 470 | /* This should not happen since the read failed during normal recovery, |
| 471 | * but the media works funny sometimes... |
| 472 | */ |
| 473 | if (!rec_round++ && !rqd->error) { |
| 474 | rec_round = 0; |
| 475 | for (i = 0; i < rqd->nr_ppas; i++, r_ptr++) { |
| 476 | u64 lba = le64_to_cpu(meta_list[i].lba); |
| 477 | |
| 478 | if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs) |
| 479 | continue; |
| 480 | |
| 481 | pblk_update_map(pblk, lba, rqd->ppa_list[i]); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Reached the end of the written line */ |
| 486 | if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) { |
| 487 | int pad_secs, nr_error_bits, bit; |
| 488 | int ret; |
| 489 | |
| 490 | bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas); |
| 491 | nr_error_bits = rqd->nr_ppas - bit; |
| 492 | |
| 493 | /* Roll back failed sectors */ |
| 494 | line->cur_sec -= nr_error_bits; |
| 495 | line->left_msecs += nr_error_bits; |
| 496 | bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits); |
| 497 | |
| 498 | pad_secs = pblk_pad_distance(pblk); |
| 499 | if (pad_secs > line->left_msecs) |
| 500 | pad_secs = line->left_msecs; |
| 501 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 502 | ret = pblk_recov_pad_oob(pblk, line, pad_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 503 | if (ret) |
| 504 | pr_err("pblk: OOB padding failed (err:%d)\n", ret); |
| 505 | |
| 506 | ret = pblk_recov_read_oob(pblk, line, p, r_ptr); |
| 507 | if (ret) |
| 508 | pr_err("pblk: OOB read failed (err:%d)\n", ret); |
| 509 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 510 | left_ppas = 0; |
| 511 | } |
| 512 | |
| 513 | left_ppas -= rq_ppas; |
| 514 | if (left_ppas > 0) |
| 515 | goto next_rq; |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line, |
| 521 | struct pblk_recov_alloc p, int *done) |
| 522 | { |
| 523 | struct nvm_tgt_dev *dev = pblk->dev; |
| 524 | struct nvm_geo *geo = &dev->geo; |
| 525 | struct ppa_addr *ppa_list; |
| 526 | struct pblk_sec_meta *meta_list; |
| 527 | struct nvm_rq *rqd; |
| 528 | struct bio *bio; |
| 529 | void *data; |
| 530 | dma_addr_t dma_ppa_list, dma_meta_list; |
| 531 | u64 paddr; |
| 532 | int rq_ppas, rq_len; |
| 533 | int i, j; |
| 534 | int ret = 0; |
| 535 | int left_ppas = pblk_calc_sec_in_line(pblk, line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 536 | |
| 537 | ppa_list = p.ppa_list; |
| 538 | meta_list = p.meta_list; |
| 539 | rqd = p.rqd; |
| 540 | data = p.data; |
| 541 | dma_ppa_list = p.dma_ppa_list; |
| 542 | dma_meta_list = p.dma_meta_list; |
| 543 | |
| 544 | *done = 1; |
| 545 | |
| 546 | next_rq: |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 547 | memset(rqd, 0, pblk_g_rq_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 548 | |
| 549 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
| 550 | if (!rq_ppas) |
| 551 | rq_ppas = pblk->min_write_pgs; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 552 | rq_len = rq_ppas * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 553 | |
| 554 | bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL); |
| 555 | if (IS_ERR(bio)) |
| 556 | return PTR_ERR(bio); |
| 557 | |
| 558 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 559 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 560 | |
| 561 | rqd->bio = bio; |
| 562 | rqd->opcode = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 563 | rqd->meta_list = meta_list; |
| 564 | rqd->nr_ppas = rq_ppas; |
| 565 | rqd->ppa_list = ppa_list; |
| 566 | rqd->dma_ppa_list = dma_ppa_list; |
| 567 | rqd->dma_meta_list = dma_meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 568 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 569 | if (pblk_io_aligned(pblk, rq_ppas)) |
| 570 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
| 571 | else |
| 572 | rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM); |
| 573 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 574 | for (i = 0; i < rqd->nr_ppas; ) { |
| 575 | struct ppa_addr ppa; |
| 576 | int pos; |
| 577 | |
| 578 | paddr = pblk_alloc_page(pblk, line, pblk->min_write_pgs); |
| 579 | ppa = addr_to_gen_ppa(pblk, paddr, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 580 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 581 | |
| 582 | while (test_bit(pos, line->blk_bitmap)) { |
| 583 | paddr += pblk->min_write_pgs; |
| 584 | ppa = addr_to_gen_ppa(pblk, paddr, line->id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 585 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | for (j = 0; j < pblk->min_write_pgs; j++, i++, paddr++) |
| 589 | rqd->ppa_list[i] = |
| 590 | addr_to_gen_ppa(pblk, paddr, line->id); |
| 591 | } |
| 592 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 593 | ret = pblk_submit_io_sync(pblk, rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 594 | if (ret) { |
| 595 | pr_err("pblk: I/O submission failed: %d\n", ret); |
| 596 | bio_put(bio); |
| 597 | return ret; |
| 598 | } |
| 599 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 600 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 601 | |
| 602 | /* Reached the end of the written line */ |
| 603 | if (rqd->error) { |
| 604 | int nr_error_bits, bit; |
| 605 | |
| 606 | bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas); |
| 607 | nr_error_bits = rqd->nr_ppas - bit; |
| 608 | |
| 609 | /* Roll back failed sectors */ |
| 610 | line->cur_sec -= nr_error_bits; |
| 611 | line->left_msecs += nr_error_bits; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 612 | bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits); |
| 613 | |
| 614 | left_ppas = 0; |
| 615 | rqd->nr_ppas = bit; |
| 616 | |
| 617 | if (rqd->error != NVM_RSP_ERR_EMPTYPAGE) |
| 618 | *done = 0; |
| 619 | } |
| 620 | |
| 621 | for (i = 0; i < rqd->nr_ppas; i++) { |
| 622 | u64 lba = le64_to_cpu(meta_list[i].lba); |
| 623 | |
| 624 | if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs) |
| 625 | continue; |
| 626 | |
| 627 | pblk_update_map(pblk, lba, rqd->ppa_list[i]); |
| 628 | } |
| 629 | |
| 630 | left_ppas -= rq_ppas; |
| 631 | if (left_ppas > 0) |
| 632 | goto next_rq; |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | /* Scan line for lbas on out of bound area */ |
| 638 | static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line) |
| 639 | { |
| 640 | struct nvm_tgt_dev *dev = pblk->dev; |
| 641 | struct nvm_geo *geo = &dev->geo; |
| 642 | struct nvm_rq *rqd; |
| 643 | struct ppa_addr *ppa_list; |
| 644 | struct pblk_sec_meta *meta_list; |
| 645 | struct pblk_recov_alloc p; |
| 646 | void *data; |
| 647 | dma_addr_t dma_ppa_list, dma_meta_list; |
| 648 | int done, ret = 0; |
| 649 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 650 | meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list); |
Javier González | 2942f50 | 2017-10-13 14:46:10 +0200 | [diff] [blame] | 651 | if (!meta_list) |
| 652 | return -ENOMEM; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 653 | |
| 654 | ppa_list = (void *)(meta_list) + pblk_dma_meta_size; |
| 655 | dma_ppa_list = dma_meta_list + pblk_dma_meta_size; |
| 656 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 657 | data = kcalloc(pblk->max_write_pgs, geo->csecs, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 658 | if (!data) { |
| 659 | ret = -ENOMEM; |
| 660 | goto free_meta_list; |
| 661 | } |
| 662 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 663 | rqd = pblk_alloc_rqd(pblk, PBLK_READ); |
Javier González | 2942f50 | 2017-10-13 14:46:10 +0200 | [diff] [blame] | 664 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 665 | p.ppa_list = ppa_list; |
| 666 | p.meta_list = meta_list; |
| 667 | p.rqd = rqd; |
| 668 | p.data = data; |
| 669 | p.dma_ppa_list = dma_ppa_list; |
| 670 | p.dma_meta_list = dma_meta_list; |
| 671 | |
| 672 | ret = pblk_recov_scan_oob(pblk, line, p, &done); |
| 673 | if (ret) { |
| 674 | pr_err("pblk: could not recover L2P from OOB\n"); |
| 675 | goto out; |
| 676 | } |
| 677 | |
| 678 | if (!done) { |
| 679 | ret = pblk_recov_scan_all_oob(pblk, line, p); |
| 680 | if (ret) { |
| 681 | pr_err("pblk: could not recover L2P from OOB\n"); |
| 682 | goto out; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (pblk_line_is_full(line)) |
| 687 | pblk_line_recov_close(pblk, line); |
| 688 | |
| 689 | out: |
| 690 | kfree(data); |
| 691 | free_meta_list: |
| 692 | nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 693 | |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | /* Insert lines ordered by sequence number (seq_num) on list */ |
| 698 | static void pblk_recov_line_add_ordered(struct list_head *head, |
| 699 | struct pblk_line *line) |
| 700 | { |
| 701 | struct pblk_line *t = NULL; |
| 702 | |
| 703 | list_for_each_entry(t, head, list) |
| 704 | if (t->seq_nr > line->seq_nr) |
| 705 | break; |
| 706 | |
| 707 | __list_add(&line->list, t->list.prev, &t->list); |
| 708 | } |
| 709 | |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 710 | static u64 pblk_line_emeta_start(struct pblk *pblk, struct pblk_line *line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 711 | { |
| 712 | struct nvm_tgt_dev *dev = pblk->dev; |
| 713 | struct nvm_geo *geo = &dev->geo; |
| 714 | struct pblk_line_meta *lm = &pblk->lm; |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 715 | unsigned int emeta_secs; |
| 716 | u64 emeta_start; |
| 717 | struct ppa_addr ppa; |
| 718 | int pos; |
| 719 | |
| 720 | emeta_secs = lm->emeta_sec[0]; |
| 721 | emeta_start = lm->sec_per_line; |
| 722 | |
| 723 | while (emeta_secs) { |
| 724 | emeta_start--; |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 725 | ppa = addr_to_gen_ppa(pblk, emeta_start, line->id); |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 726 | pos = pblk_ppa_to_pos(geo, ppa); |
| 727 | if (!test_bit(pos, line->blk_bitmap)) |
| 728 | emeta_secs--; |
| 729 | } |
| 730 | |
| 731 | return emeta_start; |
| 732 | } |
| 733 | |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 734 | static int pblk_recov_check_line_version(struct pblk *pblk, |
| 735 | struct line_emeta *emeta) |
| 736 | { |
| 737 | struct line_header *header = &emeta->header; |
| 738 | |
| 739 | if (header->version_major != EMETA_VERSION_MAJOR) { |
| 740 | pr_err("pblk: line major version mismatch: %d, expected: %d\n", |
| 741 | header->version_major, EMETA_VERSION_MAJOR); |
| 742 | return 1; |
| 743 | } |
| 744 | |
| 745 | #ifdef NVM_DEBUG |
| 746 | if (header->version_minor > EMETA_VERSION_MINOR) |
| 747 | pr_info("pblk: newer line minor version found: %d\n", line_v); |
| 748 | #endif |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 753 | static void pblk_recov_wa_counters(struct pblk *pblk, |
| 754 | struct line_emeta *emeta) |
| 755 | { |
| 756 | struct pblk_line_meta *lm = &pblk->lm; |
| 757 | struct line_header *header = &emeta->header; |
| 758 | struct wa_counters *wa = emeta_to_wa(lm, emeta); |
| 759 | |
| 760 | /* WA counters were introduced in emeta version 0.2 */ |
| 761 | if (header->version_major > 0 || header->version_minor >= 2) { |
| 762 | u64 user = le64_to_cpu(wa->user); |
| 763 | u64 pad = le64_to_cpu(wa->pad); |
| 764 | u64 gc = le64_to_cpu(wa->gc); |
| 765 | |
| 766 | atomic64_set(&pblk->user_wa, user); |
| 767 | atomic64_set(&pblk->pad_wa, pad); |
| 768 | atomic64_set(&pblk->gc_wa, gc); |
| 769 | |
| 770 | pblk->user_rst_wa = user; |
| 771 | pblk->pad_rst_wa = pad; |
| 772 | pblk->gc_rst_wa = gc; |
| 773 | } |
| 774 | } |
| 775 | |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 776 | static int pblk_line_was_written(struct pblk_line *line, |
Hans Holmberg | b06be28 | 2018-06-01 16:41:12 +0200 | [diff] [blame] | 777 | struct pblk *pblk) |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 778 | { |
| 779 | |
Hans Holmberg | b06be28 | 2018-06-01 16:41:12 +0200 | [diff] [blame] | 780 | struct pblk_line_meta *lm = &pblk->lm; |
| 781 | struct nvm_tgt_dev *dev = pblk->dev; |
| 782 | struct nvm_geo *geo = &dev->geo; |
| 783 | struct nvm_chk_meta *chunk; |
| 784 | struct ppa_addr bppa; |
| 785 | int smeta_blk; |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 786 | |
Hans Holmberg | b06be28 | 2018-06-01 16:41:12 +0200 | [diff] [blame] | 787 | if (line->state == PBLK_LINESTATE_BAD) |
| 788 | return 0; |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 789 | |
Hans Holmberg | b06be28 | 2018-06-01 16:41:12 +0200 | [diff] [blame] | 790 | smeta_blk = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 791 | if (smeta_blk >= lm->blk_per_line) |
| 792 | return 0; |
| 793 | |
| 794 | bppa = pblk->luns[smeta_blk].bppa; |
| 795 | chunk = &line->chks[pblk_ppa_to_pos(geo, bppa)]; |
| 796 | |
| 797 | if (chunk->state & NVM_CHK_ST_FREE) |
| 798 | return 0; |
| 799 | |
| 800 | return 1; |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 803 | struct pblk_line *pblk_recov_l2p(struct pblk *pblk) |
| 804 | { |
| 805 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 806 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 807 | struct pblk_line *line, *tline, *data_line = NULL; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 808 | struct pblk_smeta *smeta; |
| 809 | struct pblk_emeta *emeta; |
| 810 | struct line_smeta *smeta_buf; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 811 | int found_lines = 0, recovered_lines = 0, open_lines = 0; |
| 812 | int is_next = 0; |
| 813 | int meta_line; |
| 814 | int i, valid_uuid = 0; |
| 815 | LIST_HEAD(recov_list); |
| 816 | |
| 817 | /* TODO: Implement FTL snapshot */ |
| 818 | |
| 819 | /* Scan recovery - takes place when FTL snapshot fails */ |
| 820 | spin_lock(&l_mg->free_lock); |
| 821 | meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES); |
| 822 | set_bit(meta_line, &l_mg->meta_bitmap); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 823 | smeta = l_mg->sline_meta[meta_line]; |
| 824 | emeta = l_mg->eline_meta[meta_line]; |
Javier González | 8224cbd | 2017-06-30 17:56:38 +0200 | [diff] [blame] | 825 | smeta_buf = (struct line_smeta *)smeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 826 | spin_unlock(&l_mg->free_lock); |
| 827 | |
| 828 | /* Order data lines using their sequence number */ |
| 829 | for (i = 0; i < l_mg->nr_lines; i++) { |
| 830 | u32 crc; |
| 831 | |
| 832 | line = &pblk->lines[i]; |
| 833 | |
| 834 | memset(smeta, 0, lm->smeta_len); |
| 835 | line->smeta = smeta; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 836 | line->lun_bitmap = ((void *)(smeta_buf)) + |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 837 | sizeof(struct line_smeta); |
| 838 | |
Hans Holmberg | b06be28 | 2018-06-01 16:41:12 +0200 | [diff] [blame] | 839 | if (!pblk_line_was_written(line, pblk)) |
Hans Holmberg | 9156f36 | 2018-03-30 00:05:22 +0200 | [diff] [blame] | 840 | continue; |
| 841 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 842 | /* Lines that cannot be read are assumed as not written here */ |
| 843 | if (pblk_line_read_smeta(pblk, line)) |
| 844 | continue; |
| 845 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 846 | crc = pblk_calc_smeta_crc(pblk, smeta_buf); |
| 847 | if (le32_to_cpu(smeta_buf->crc) != crc) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 848 | continue; |
| 849 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 850 | if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 851 | continue; |
| 852 | |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 853 | if (smeta_buf->header.version_major != SMETA_VERSION_MAJOR) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 854 | pr_err("pblk: found incompatible line version %u\n", |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 855 | smeta_buf->header.version_major); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 856 | return ERR_PTR(-EINVAL); |
| 857 | } |
| 858 | |
| 859 | /* The first valid instance uuid is used for initialization */ |
| 860 | if (!valid_uuid) { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 861 | memcpy(pblk->instance_uuid, smeta_buf->header.uuid, 16); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 862 | valid_uuid = 1; |
| 863 | } |
| 864 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 865 | if (memcmp(pblk->instance_uuid, smeta_buf->header.uuid, 16)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 866 | pr_debug("pblk: ignore line %u due to uuid mismatch\n", |
| 867 | i); |
| 868 | continue; |
| 869 | } |
| 870 | |
| 871 | /* Update line metadata */ |
| 872 | spin_lock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 873 | line->id = le32_to_cpu(smeta_buf->header.id); |
| 874 | line->type = le16_to_cpu(smeta_buf->header.type); |
| 875 | line->seq_nr = le64_to_cpu(smeta_buf->seq_nr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 876 | spin_unlock(&line->lock); |
| 877 | |
| 878 | /* Update general metadata */ |
| 879 | spin_lock(&l_mg->free_lock); |
| 880 | if (line->seq_nr >= l_mg->d_seq_nr) |
| 881 | l_mg->d_seq_nr = line->seq_nr + 1; |
| 882 | l_mg->nr_free_lines--; |
| 883 | spin_unlock(&l_mg->free_lock); |
| 884 | |
| 885 | if (pblk_line_recov_alloc(pblk, line)) |
| 886 | goto out; |
| 887 | |
| 888 | pblk_recov_line_add_ordered(&recov_list, line); |
| 889 | found_lines++; |
| 890 | pr_debug("pblk: recovering data line %d, seq:%llu\n", |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 891 | line->id, smeta_buf->seq_nr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (!found_lines) { |
| 895 | pblk_setup_uuid(pblk); |
| 896 | |
| 897 | spin_lock(&l_mg->free_lock); |
| 898 | WARN_ON_ONCE(!test_and_clear_bit(meta_line, |
| 899 | &l_mg->meta_bitmap)); |
| 900 | spin_unlock(&l_mg->free_lock); |
| 901 | |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | /* Verify closed blocks and recover this portion of L2P table*/ |
| 906 | list_for_each_entry_safe(line, tline, &recov_list, list) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 907 | recovered_lines++; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 908 | |
Hans Holmberg | 75610cd | 2017-10-13 14:46:38 +0200 | [diff] [blame] | 909 | line->emeta_ssec = pblk_line_emeta_start(pblk, line); |
Javier González | 8224cbd | 2017-06-30 17:56:38 +0200 | [diff] [blame] | 910 | line->emeta = emeta; |
| 911 | memset(line->emeta->buf, 0, lm->emeta_len[0]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 912 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 913 | if (pblk_line_read_emeta(pblk, line, line->emeta->buf)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 914 | pblk_recov_l2p_from_oob(pblk, line); |
| 915 | goto next; |
| 916 | } |
| 917 | |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 918 | if (pblk_recov_check_emeta(pblk, line->emeta->buf)) { |
| 919 | pblk_recov_l2p_from_oob(pblk, line); |
| 920 | goto next; |
| 921 | } |
| 922 | |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 923 | if (pblk_recov_check_line_version(pblk, line->emeta->buf)) |
| 924 | return ERR_PTR(-EINVAL); |
| 925 | |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 926 | pblk_recov_wa_counters(pblk, line->emeta->buf); |
| 927 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 928 | if (pblk_recov_l2p_from_emeta(pblk, line)) |
| 929 | pblk_recov_l2p_from_oob(pblk, line); |
| 930 | |
| 931 | next: |
| 932 | if (pblk_line_is_full(line)) { |
| 933 | struct list_head *move_list; |
| 934 | |
| 935 | spin_lock(&line->lock); |
| 936 | line->state = PBLK_LINESTATE_CLOSED; |
| 937 | move_list = pblk_line_gc_list(pblk, line); |
| 938 | spin_unlock(&line->lock); |
| 939 | |
| 940 | spin_lock(&l_mg->gc_lock); |
| 941 | list_move_tail(&line->list, move_list); |
| 942 | spin_unlock(&l_mg->gc_lock); |
| 943 | |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 944 | kfree(line->map_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 945 | line->map_bitmap = NULL; |
| 946 | line->smeta = NULL; |
| 947 | line->emeta = NULL; |
| 948 | } else { |
| 949 | if (open_lines > 1) |
| 950 | pr_err("pblk: failed to recover L2P\n"); |
| 951 | |
| 952 | open_lines++; |
| 953 | line->meta_line = meta_line; |
| 954 | data_line = line; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | spin_lock(&l_mg->free_lock); |
| 959 | if (!open_lines) { |
| 960 | WARN_ON_ONCE(!test_and_clear_bit(meta_line, |
| 961 | &l_mg->meta_bitmap)); |
| 962 | pblk_line_replace_data(pblk); |
| 963 | } else { |
| 964 | /* Allocate next line for preparation */ |
| 965 | l_mg->data_next = pblk_line_get(pblk); |
| 966 | if (l_mg->data_next) { |
| 967 | l_mg->data_next->seq_nr = l_mg->d_seq_nr++; |
| 968 | l_mg->data_next->type = PBLK_LINETYPE_DATA; |
| 969 | is_next = 1; |
| 970 | } |
| 971 | } |
| 972 | spin_unlock(&l_mg->free_lock); |
| 973 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 974 | if (is_next) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 975 | pblk_line_erase(pblk, l_mg->data_next); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 976 | |
| 977 | out: |
| 978 | if (found_lines != recovered_lines) |
| 979 | pr_err("pblk: failed to recover all found lines %d/%d\n", |
| 980 | found_lines, recovered_lines); |
| 981 | |
| 982 | return data_line; |
| 983 | } |
| 984 | |
| 985 | /* |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 986 | * Pad current line |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 987 | */ |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 988 | int pblk_recov_pad(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 989 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 990 | struct pblk_line *line; |
| 991 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 992 | int left_msecs; |
| 993 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 994 | |
| 995 | spin_lock(&l_mg->free_lock); |
| 996 | line = l_mg->data_line; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 997 | left_msecs = line->left_msecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 998 | spin_unlock(&l_mg->free_lock); |
| 999 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 1000 | ret = pblk_recov_pad_oob(pblk, line, left_msecs); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1001 | if (ret) { |
| 1002 | pr_err("pblk: Tear down padding failed (%d)\n", ret); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 1003 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1006 | pblk_line_close_meta(pblk, line); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1007 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1008 | } |