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