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-core.c - pblk's core functionality |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "pblk.h" |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 20 | |
| 21 | static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line, |
| 22 | struct ppa_addr *ppa) |
| 23 | { |
| 24 | struct nvm_tgt_dev *dev = pblk->dev; |
| 25 | struct nvm_geo *geo = &dev->geo; |
| 26 | int pos = pblk_dev_ppa_to_pos(geo, *ppa); |
| 27 | |
| 28 | pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos); |
| 29 | atomic_long_inc(&pblk->erase_failed); |
| 30 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 31 | atomic_dec(&line->blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 32 | if (test_and_set_bit(pos, line->blk_bitmap)) |
| 33 | pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n", |
| 34 | line->id, pos); |
| 35 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 36 | pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb, |
| 37 | GFP_ATOMIC, pblk->bb_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd) |
| 41 | { |
| 42 | struct pblk_line *line; |
| 43 | |
| 44 | line = &pblk->lines[pblk_dev_ppa_to_line(rqd->ppa_addr)]; |
| 45 | atomic_dec(&line->left_seblks); |
| 46 | |
| 47 | if (rqd->error) { |
| 48 | struct ppa_addr *ppa; |
| 49 | |
| 50 | ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC); |
| 51 | if (!ppa) |
| 52 | return; |
| 53 | |
| 54 | *ppa = rqd->ppa_addr; |
| 55 | pblk_mark_bb(pblk, line, ppa); |
| 56 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 57 | |
| 58 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Erase completion assumes that only one block is erased at the time */ |
| 62 | static void pblk_end_io_erase(struct nvm_rq *rqd) |
| 63 | { |
| 64 | struct pblk *pblk = rqd->private; |
| 65 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 66 | __pblk_end_io_erase(pblk, rqd); |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame^] | 67 | mempool_free(rqd, pblk->e_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 70 | void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line, |
| 71 | u64 paddr) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 72 | { |
| 73 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 74 | struct list_head *move_list = NULL; |
| 75 | |
| 76 | /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P |
| 77 | * table is modified with reclaimed sectors, a check is done to endure |
| 78 | * that newer updates are not overwritten. |
| 79 | */ |
| 80 | spin_lock(&line->lock); |
| 81 | if (line->state == PBLK_LINESTATE_GC || |
| 82 | line->state == PBLK_LINESTATE_FREE) { |
| 83 | spin_unlock(&line->lock); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (test_and_set_bit(paddr, line->invalid_bitmap)) { |
| 88 | WARN_ONCE(1, "pblk: double invalidate\n"); |
| 89 | spin_unlock(&line->lock); |
| 90 | return; |
| 91 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 92 | le32_add_cpu(line->vsc, -1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 93 | |
| 94 | if (line->state == PBLK_LINESTATE_CLOSED) |
| 95 | move_list = pblk_line_gc_list(pblk, line); |
| 96 | spin_unlock(&line->lock); |
| 97 | |
| 98 | if (move_list) { |
| 99 | spin_lock(&l_mg->gc_lock); |
| 100 | spin_lock(&line->lock); |
| 101 | /* Prevent moving a line that has just been chosen for GC */ |
| 102 | if (line->state == PBLK_LINESTATE_GC || |
| 103 | line->state == PBLK_LINESTATE_FREE) { |
| 104 | spin_unlock(&line->lock); |
| 105 | spin_unlock(&l_mg->gc_lock); |
| 106 | return; |
| 107 | } |
| 108 | spin_unlock(&line->lock); |
| 109 | |
| 110 | list_move_tail(&line->list, move_list); |
| 111 | spin_unlock(&l_mg->gc_lock); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa) |
| 116 | { |
| 117 | struct pblk_line *line; |
| 118 | u64 paddr; |
| 119 | int line_id; |
| 120 | |
| 121 | #ifdef CONFIG_NVM_DEBUG |
| 122 | /* Callers must ensure that the ppa points to a device address */ |
| 123 | BUG_ON(pblk_addr_in_cache(ppa)); |
| 124 | BUG_ON(pblk_ppa_empty(ppa)); |
| 125 | #endif |
| 126 | |
| 127 | line_id = pblk_tgt_ppa_to_line(ppa); |
| 128 | line = &pblk->lines[line_id]; |
| 129 | paddr = pblk_dev_ppa_to_line_addr(pblk, ppa); |
| 130 | |
| 131 | __pblk_map_invalidate(pblk, line, paddr); |
| 132 | } |
| 133 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 134 | static void pblk_invalidate_range(struct pblk *pblk, sector_t slba, |
| 135 | unsigned int nr_secs) |
| 136 | { |
| 137 | sector_t lba; |
| 138 | |
| 139 | spin_lock(&pblk->trans_lock); |
| 140 | for (lba = slba; lba < slba + nr_secs; lba++) { |
| 141 | struct ppa_addr ppa; |
| 142 | |
| 143 | ppa = pblk_trans_map_get(pblk, lba); |
| 144 | |
| 145 | if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa)) |
| 146 | pblk_map_invalidate(pblk, ppa); |
| 147 | |
| 148 | pblk_ppa_set_empty(&ppa); |
| 149 | pblk_trans_map_set(pblk, lba, ppa); |
| 150 | } |
| 151 | spin_unlock(&pblk->trans_lock); |
| 152 | } |
| 153 | |
| 154 | struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw) |
| 155 | { |
| 156 | mempool_t *pool; |
| 157 | struct nvm_rq *rqd; |
| 158 | int rq_size; |
| 159 | |
| 160 | if (rw == WRITE) { |
| 161 | pool = pblk->w_rq_pool; |
| 162 | rq_size = pblk_w_rq_size; |
| 163 | } else { |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame^] | 164 | pool = pblk->r_rq_pool; |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 165 | rq_size = pblk_g_rq_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | rqd = mempool_alloc(pool, GFP_KERNEL); |
| 169 | memset(rqd, 0, rq_size); |
| 170 | |
| 171 | return rqd; |
| 172 | } |
| 173 | |
| 174 | void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw) |
| 175 | { |
| 176 | mempool_t *pool; |
| 177 | |
| 178 | if (rw == WRITE) |
| 179 | pool = pblk->w_rq_pool; |
| 180 | else |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame^] | 181 | pool = pblk->r_rq_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 182 | |
| 183 | mempool_free(rqd, pool); |
| 184 | } |
| 185 | |
| 186 | void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off, |
| 187 | int nr_pages) |
| 188 | { |
| 189 | struct bio_vec bv; |
| 190 | int i; |
| 191 | |
| 192 | WARN_ON(off + nr_pages != bio->bi_vcnt); |
| 193 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 194 | for (i = off; i < nr_pages + off; i++) { |
| 195 | bv = bio->bi_io_vec[i]; |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 196 | mempool_free(bv.bv_page, pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags, |
| 201 | int nr_pages) |
| 202 | { |
| 203 | struct request_queue *q = pblk->dev->q; |
| 204 | struct page *page; |
| 205 | int i, ret; |
| 206 | |
| 207 | for (i = 0; i < nr_pages; i++) { |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 208 | page = mempool_alloc(pblk->page_bio_pool, flags); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 209 | if (!page) |
| 210 | goto err; |
| 211 | |
| 212 | ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0); |
| 213 | if (ret != PBLK_EXPOSED_PAGE_SIZE) { |
| 214 | pr_err("pblk: could not add page to bio\n"); |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 215 | mempool_free(page, pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 216 | goto err; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | err: |
| 222 | pblk_bio_free_pages(pblk, bio, 0, i - 1); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | static void pblk_write_kick(struct pblk *pblk) |
| 227 | { |
| 228 | wake_up_process(pblk->writer_ts); |
| 229 | mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000)); |
| 230 | } |
| 231 | |
| 232 | void pblk_write_timer_fn(unsigned long data) |
| 233 | { |
| 234 | struct pblk *pblk = (struct pblk *)data; |
| 235 | |
| 236 | /* kick the write thread every tick to flush outstanding data */ |
| 237 | pblk_write_kick(pblk); |
| 238 | } |
| 239 | |
| 240 | void pblk_write_should_kick(struct pblk *pblk) |
| 241 | { |
| 242 | unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb); |
| 243 | |
| 244 | if (secs_avail >= pblk->min_write_pgs) |
| 245 | pblk_write_kick(pblk); |
| 246 | } |
| 247 | |
| 248 | void pblk_end_bio_sync(struct bio *bio) |
| 249 | { |
| 250 | struct completion *waiting = bio->bi_private; |
| 251 | |
| 252 | complete(waiting); |
| 253 | } |
| 254 | |
| 255 | void pblk_end_io_sync(struct nvm_rq *rqd) |
| 256 | { |
| 257 | struct completion *waiting = rqd->private; |
| 258 | |
| 259 | complete(waiting); |
| 260 | } |
| 261 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 262 | void pblk_wait_for_meta(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 263 | { |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 264 | do { |
| 265 | if (!atomic_read(&pblk->inflight_io)) |
| 266 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 267 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 268 | schedule(); |
| 269 | } while (1); |
| 270 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 271 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 272 | static void pblk_flush_writer(struct pblk *pblk) |
| 273 | { |
| 274 | pblk_rb_flush(&pblk->rwb); |
| 275 | do { |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 276 | if (!pblk_rb_sync_count(&pblk->rwb)) |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 277 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 278 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 279 | pblk_write_kick(pblk); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 280 | schedule(); |
| 281 | } while (1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line) |
| 285 | { |
| 286 | struct pblk_line_meta *lm = &pblk->lm; |
| 287 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 288 | struct list_head *move_list = NULL; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 289 | int vsc = le32_to_cpu(*line->vsc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 290 | |
Javier González | 476118c | 2017-06-26 11:57:26 +0200 | [diff] [blame] | 291 | lockdep_assert_held(&line->lock); |
| 292 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 293 | if (!vsc) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 294 | if (line->gc_group != PBLK_LINEGC_FULL) { |
| 295 | line->gc_group = PBLK_LINEGC_FULL; |
| 296 | move_list = &l_mg->gc_full_list; |
| 297 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 298 | } else if (vsc < lm->high_thrs) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 299 | if (line->gc_group != PBLK_LINEGC_HIGH) { |
| 300 | line->gc_group = PBLK_LINEGC_HIGH; |
| 301 | move_list = &l_mg->gc_high_list; |
| 302 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 303 | } else if (vsc < lm->mid_thrs) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 304 | if (line->gc_group != PBLK_LINEGC_MID) { |
| 305 | line->gc_group = PBLK_LINEGC_MID; |
| 306 | move_list = &l_mg->gc_mid_list; |
| 307 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 308 | } else if (vsc < line->sec_in_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 309 | if (line->gc_group != PBLK_LINEGC_LOW) { |
| 310 | line->gc_group = PBLK_LINEGC_LOW; |
| 311 | move_list = &l_mg->gc_low_list; |
| 312 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 313 | } else if (vsc == line->sec_in_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 314 | if (line->gc_group != PBLK_LINEGC_EMPTY) { |
| 315 | line->gc_group = PBLK_LINEGC_EMPTY; |
| 316 | move_list = &l_mg->gc_empty_list; |
| 317 | } |
| 318 | } else { |
| 319 | line->state = PBLK_LINESTATE_CORRUPT; |
| 320 | line->gc_group = PBLK_LINEGC_NONE; |
| 321 | move_list = &l_mg->corrupt_list; |
| 322 | pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n", |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 323 | line->id, vsc, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 324 | line->sec_in_line, |
| 325 | lm->high_thrs, lm->mid_thrs); |
| 326 | } |
| 327 | |
| 328 | return move_list; |
| 329 | } |
| 330 | |
| 331 | void pblk_discard(struct pblk *pblk, struct bio *bio) |
| 332 | { |
| 333 | sector_t slba = pblk_get_lba(bio); |
| 334 | sector_t nr_secs = pblk_get_secs(bio); |
| 335 | |
| 336 | pblk_invalidate_range(pblk, slba, nr_secs); |
| 337 | } |
| 338 | |
| 339 | struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba) |
| 340 | { |
| 341 | struct ppa_addr ppa; |
| 342 | |
| 343 | spin_lock(&pblk->trans_lock); |
| 344 | ppa = pblk_trans_map_get(pblk, lba); |
| 345 | spin_unlock(&pblk->trans_lock); |
| 346 | |
| 347 | return ppa; |
| 348 | } |
| 349 | |
| 350 | void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd) |
| 351 | { |
| 352 | atomic_long_inc(&pblk->write_failed); |
| 353 | #ifdef CONFIG_NVM_DEBUG |
| 354 | pblk_print_failed_rqd(pblk, rqd, rqd->error); |
| 355 | #endif |
| 356 | } |
| 357 | |
| 358 | void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd) |
| 359 | { |
| 360 | /* Empty page read is not necessarily an error (e.g., L2P recovery) */ |
| 361 | if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) { |
| 362 | atomic_long_inc(&pblk->read_empty); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | switch (rqd->error) { |
| 367 | case NVM_RSP_WARN_HIGHECC: |
| 368 | atomic_long_inc(&pblk->read_high_ecc); |
| 369 | break; |
| 370 | case NVM_RSP_ERR_FAILECC: |
| 371 | case NVM_RSP_ERR_FAILCRC: |
| 372 | atomic_long_inc(&pblk->read_failed); |
| 373 | break; |
| 374 | default: |
| 375 | pr_err("pblk: unknown read error:%d\n", rqd->error); |
| 376 | } |
| 377 | #ifdef CONFIG_NVM_DEBUG |
| 378 | pblk_print_failed_rqd(pblk, rqd, rqd->error); |
| 379 | #endif |
| 380 | } |
| 381 | |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 382 | void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write) |
| 383 | { |
| 384 | pblk->sec_per_write = sec_per_write; |
| 385 | } |
| 386 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 387 | int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd) |
| 388 | { |
| 389 | struct nvm_tgt_dev *dev = pblk->dev; |
| 390 | |
| 391 | #ifdef CONFIG_NVM_DEBUG |
| 392 | struct ppa_addr *ppa_list; |
| 393 | |
| 394 | ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr; |
| 395 | if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) { |
| 396 | WARN_ON(1); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | if (rqd->opcode == NVM_OP_PWRITE) { |
| 401 | struct pblk_line *line; |
| 402 | struct ppa_addr ppa; |
| 403 | int i; |
| 404 | |
| 405 | for (i = 0; i < rqd->nr_ppas; i++) { |
| 406 | ppa = ppa_list[i]; |
| 407 | line = &pblk->lines[pblk_dev_ppa_to_line(ppa)]; |
| 408 | |
| 409 | spin_lock(&line->lock); |
| 410 | if (line->state != PBLK_LINESTATE_OPEN) { |
| 411 | pr_err("pblk: bad ppa: line:%d,state:%d\n", |
| 412 | line->id, line->state); |
| 413 | WARN_ON(1); |
| 414 | spin_unlock(&line->lock); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | spin_unlock(&line->lock); |
| 418 | } |
| 419 | } |
| 420 | #endif |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 421 | |
| 422 | atomic_inc(&pblk->inflight_io); |
| 423 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 424 | return nvm_submit_io(dev, rqd); |
| 425 | } |
| 426 | |
| 427 | struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data, |
| 428 | unsigned int nr_secs, unsigned int len, |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 429 | int alloc_type, gfp_t gfp_mask) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 430 | { |
| 431 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 432 | void *kaddr = data; |
| 433 | struct page *page; |
| 434 | struct bio *bio; |
| 435 | int i, ret; |
| 436 | |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 437 | if (alloc_type == PBLK_KMALLOC_META) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 438 | return bio_map_kern(dev->q, kaddr, len, gfp_mask); |
| 439 | |
| 440 | bio = bio_kmalloc(gfp_mask, nr_secs); |
| 441 | if (!bio) |
| 442 | return ERR_PTR(-ENOMEM); |
| 443 | |
| 444 | for (i = 0; i < nr_secs; i++) { |
| 445 | page = vmalloc_to_page(kaddr); |
| 446 | if (!page) { |
| 447 | pr_err("pblk: could not map vmalloc bio\n"); |
| 448 | bio_put(bio); |
| 449 | bio = ERR_PTR(-ENOMEM); |
| 450 | goto out; |
| 451 | } |
| 452 | |
| 453 | ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0); |
| 454 | if (ret != PAGE_SIZE) { |
| 455 | pr_err("pblk: could not add page to bio\n"); |
| 456 | bio_put(bio); |
| 457 | bio = ERR_PTR(-ENOMEM); |
| 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | kaddr += PAGE_SIZE; |
| 462 | } |
| 463 | out: |
| 464 | return bio; |
| 465 | } |
| 466 | |
| 467 | int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail, |
| 468 | unsigned long secs_to_flush) |
| 469 | { |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 470 | int max = pblk->sec_per_write; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 471 | int min = pblk->min_write_pgs; |
| 472 | int secs_to_sync = 0; |
| 473 | |
| 474 | if (secs_avail >= max) |
| 475 | secs_to_sync = max; |
| 476 | else if (secs_avail >= min) |
| 477 | secs_to_sync = min * (secs_avail / min); |
| 478 | else if (secs_to_flush) |
| 479 | secs_to_sync = min; |
| 480 | |
| 481 | return secs_to_sync; |
| 482 | } |
| 483 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 484 | void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
| 485 | { |
| 486 | u64 addr; |
| 487 | int i; |
| 488 | |
Rakesh Pandit | e57903f | 2017-10-13 14:45:56 +0200 | [diff] [blame] | 489 | spin_lock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 490 | addr = find_next_zero_bit(line->map_bitmap, |
| 491 | pblk->lm.sec_per_line, line->cur_sec); |
| 492 | line->cur_sec = addr - nr_secs; |
| 493 | |
| 494 | for (i = 0; i < nr_secs; i++, line->cur_sec--) |
| 495 | WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap)); |
Rakesh Pandit | e57903f | 2017-10-13 14:45:56 +0200 | [diff] [blame] | 496 | spin_unlock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 500 | { |
| 501 | u64 addr; |
| 502 | int i; |
| 503 | |
Javier González | 476118c | 2017-06-26 11:57:26 +0200 | [diff] [blame] | 504 | lockdep_assert_held(&line->lock); |
| 505 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 506 | /* logic error: ppa out-of-bounds. Prevent generating bad address */ |
| 507 | if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) { |
| 508 | WARN(1, "pblk: page allocation out of bounds\n"); |
| 509 | nr_secs = pblk->lm.sec_per_line - line->cur_sec; |
| 510 | } |
| 511 | |
| 512 | line->cur_sec = addr = find_next_zero_bit(line->map_bitmap, |
| 513 | pblk->lm.sec_per_line, line->cur_sec); |
| 514 | for (i = 0; i < nr_secs; i++, line->cur_sec++) |
| 515 | WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap)); |
| 516 | |
| 517 | return addr; |
| 518 | } |
| 519 | |
| 520 | u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
| 521 | { |
| 522 | u64 addr; |
| 523 | |
| 524 | /* Lock needed in case a write fails and a recovery needs to remap |
| 525 | * failed write buffer entries |
| 526 | */ |
| 527 | spin_lock(&line->lock); |
| 528 | addr = __pblk_alloc_page(pblk, line, nr_secs); |
| 529 | line->left_msecs -= nr_secs; |
| 530 | WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n"); |
| 531 | spin_unlock(&line->lock); |
| 532 | |
| 533 | return addr; |
| 534 | } |
| 535 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 536 | u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line) |
| 537 | { |
| 538 | u64 paddr; |
| 539 | |
| 540 | spin_lock(&line->lock); |
| 541 | paddr = find_next_zero_bit(line->map_bitmap, |
| 542 | pblk->lm.sec_per_line, line->cur_sec); |
| 543 | spin_unlock(&line->lock); |
| 544 | |
| 545 | return paddr; |
| 546 | } |
| 547 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 548 | /* |
| 549 | * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when |
| 550 | * taking the per LUN semaphore. |
| 551 | */ |
| 552 | static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line, |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 553 | void *emeta_buf, u64 paddr, int dir) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 554 | { |
| 555 | struct nvm_tgt_dev *dev = pblk->dev; |
| 556 | struct nvm_geo *geo = &dev->geo; |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 557 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 558 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 559 | void *ppa_list, *meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 560 | struct bio *bio; |
| 561 | struct nvm_rq rqd; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 562 | dma_addr_t dma_ppa_list, dma_meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 563 | int min = pblk->min_write_pgs; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 564 | int left_ppas = lm->emeta_sec[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 565 | int id = line->id; |
| 566 | int rq_ppas, rq_len; |
| 567 | int cmd_op, bio_op; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 568 | int i, j; |
| 569 | int ret; |
| 570 | DECLARE_COMPLETION_ONSTACK(wait); |
| 571 | |
| 572 | if (dir == WRITE) { |
| 573 | bio_op = REQ_OP_WRITE; |
| 574 | cmd_op = NVM_OP_PWRITE; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 575 | } else if (dir == READ) { |
| 576 | bio_op = REQ_OP_READ; |
| 577 | cmd_op = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 578 | } else |
| 579 | return -EINVAL; |
| 580 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 581 | meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 582 | &dma_meta_list); |
| 583 | if (!meta_list) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 584 | return -ENOMEM; |
| 585 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 586 | ppa_list = meta_list + pblk_dma_meta_size; |
| 587 | dma_ppa_list = dma_meta_list + pblk_dma_meta_size; |
| 588 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 589 | next_rq: |
| 590 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 591 | |
| 592 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
| 593 | rq_len = rq_ppas * geo->sec_size; |
| 594 | |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 595 | bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len, |
| 596 | l_mg->emeta_alloc_type, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 597 | if (IS_ERR(bio)) { |
| 598 | ret = PTR_ERR(bio); |
| 599 | goto free_rqd_dma; |
| 600 | } |
| 601 | |
| 602 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 603 | bio_set_op_attrs(bio, bio_op, 0); |
| 604 | |
| 605 | rqd.bio = bio; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 606 | rqd.meta_list = meta_list; |
| 607 | rqd.ppa_list = ppa_list; |
| 608 | rqd.dma_meta_list = dma_meta_list; |
| 609 | rqd.dma_ppa_list = dma_ppa_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 610 | rqd.opcode = cmd_op; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 611 | rqd.nr_ppas = rq_ppas; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 612 | rqd.end_io = pblk_end_io_sync; |
| 613 | rqd.private = &wait; |
| 614 | |
| 615 | if (dir == WRITE) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 616 | struct pblk_sec_meta *meta_list = rqd.meta_list; |
| 617 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 618 | rqd.flags = pblk_set_progr_mode(pblk, WRITE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 619 | for (i = 0; i < rqd.nr_ppas; ) { |
| 620 | spin_lock(&line->lock); |
| 621 | paddr = __pblk_alloc_page(pblk, line, min); |
| 622 | spin_unlock(&line->lock); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 623 | for (j = 0; j < min; j++, i++, paddr++) { |
| 624 | meta_list[i].lba = cpu_to_le64(ADDR_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 625 | rqd.ppa_list[i] = |
| 626 | addr_to_gen_ppa(pblk, paddr, id); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 627 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 628 | } |
| 629 | } else { |
| 630 | for (i = 0; i < rqd.nr_ppas; ) { |
| 631 | struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id); |
| 632 | int pos = pblk_dev_ppa_to_pos(geo, ppa); |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 633 | int read_type = PBLK_READ_RANDOM; |
| 634 | |
| 635 | if (pblk_io_aligned(pblk, rq_ppas)) |
| 636 | read_type = PBLK_READ_SEQUENTIAL; |
| 637 | rqd.flags = pblk_set_read_mode(pblk, read_type); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 638 | |
| 639 | while (test_bit(pos, line->blk_bitmap)) { |
| 640 | paddr += min; |
| 641 | if (pblk_boundary_paddr_checks(pblk, paddr)) { |
| 642 | pr_err("pblk: corrupt emeta line:%d\n", |
| 643 | line->id); |
| 644 | bio_put(bio); |
| 645 | ret = -EINTR; |
| 646 | goto free_rqd_dma; |
| 647 | } |
| 648 | |
| 649 | ppa = addr_to_gen_ppa(pblk, paddr, id); |
| 650 | pos = pblk_dev_ppa_to_pos(geo, ppa); |
| 651 | } |
| 652 | |
| 653 | if (pblk_boundary_paddr_checks(pblk, paddr + min)) { |
| 654 | pr_err("pblk: corrupt emeta line:%d\n", |
| 655 | line->id); |
| 656 | bio_put(bio); |
| 657 | ret = -EINTR; |
| 658 | goto free_rqd_dma; |
| 659 | } |
| 660 | |
| 661 | for (j = 0; j < min; j++, i++, paddr++) |
| 662 | rqd.ppa_list[i] = |
| 663 | addr_to_gen_ppa(pblk, paddr, line->id); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | ret = pblk_submit_io(pblk, &rqd); |
| 668 | if (ret) { |
| 669 | pr_err("pblk: emeta I/O submission failed: %d\n", ret); |
| 670 | bio_put(bio); |
| 671 | goto free_rqd_dma; |
| 672 | } |
| 673 | |
| 674 | if (!wait_for_completion_io_timeout(&wait, |
| 675 | msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) { |
| 676 | pr_err("pblk: emeta I/O timed out\n"); |
| 677 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 678 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 679 | reinit_completion(&wait); |
| 680 | |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 681 | if (likely(pblk->l_mg.emeta_alloc_type == PBLK_VMALLOC_META)) |
| 682 | bio_put(bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 683 | |
| 684 | if (rqd.error) { |
| 685 | if (dir == WRITE) |
| 686 | pblk_log_write_err(pblk, &rqd); |
| 687 | else |
| 688 | pblk_log_read_err(pblk, &rqd); |
| 689 | } |
| 690 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 691 | emeta_buf += rq_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 692 | left_ppas -= rq_ppas; |
| 693 | if (left_ppas) |
| 694 | goto next_rq; |
| 695 | free_rqd_dma: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 696 | 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] | 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line) |
| 701 | { |
| 702 | struct nvm_tgt_dev *dev = pblk->dev; |
| 703 | struct nvm_geo *geo = &dev->geo; |
| 704 | struct pblk_line_meta *lm = &pblk->lm; |
| 705 | int bit; |
| 706 | |
| 707 | /* This usually only happens on bad lines */ |
| 708 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 709 | if (bit >= lm->blk_per_line) |
| 710 | return -1; |
| 711 | |
| 712 | return bit * geo->sec_per_pl; |
| 713 | } |
| 714 | |
| 715 | static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line, |
| 716 | u64 paddr, int dir) |
| 717 | { |
| 718 | struct nvm_tgt_dev *dev = pblk->dev; |
| 719 | struct pblk_line_meta *lm = &pblk->lm; |
| 720 | struct bio *bio; |
| 721 | struct nvm_rq rqd; |
| 722 | __le64 *lba_list = NULL; |
| 723 | int i, ret; |
| 724 | int cmd_op, bio_op; |
| 725 | int flags; |
| 726 | DECLARE_COMPLETION_ONSTACK(wait); |
| 727 | |
| 728 | if (dir == WRITE) { |
| 729 | bio_op = REQ_OP_WRITE; |
| 730 | cmd_op = NVM_OP_PWRITE; |
| 731 | flags = pblk_set_progr_mode(pblk, WRITE); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 732 | lba_list = emeta_to_lbas(pblk, line->emeta->buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 733 | } else if (dir == READ) { |
| 734 | bio_op = REQ_OP_READ; |
| 735 | cmd_op = NVM_OP_PREAD; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 736 | flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 737 | } else |
| 738 | return -EINVAL; |
| 739 | |
| 740 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 741 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 742 | rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 743 | &rqd.dma_meta_list); |
| 744 | if (!rqd.meta_list) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 745 | return -ENOMEM; |
| 746 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 747 | rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size; |
| 748 | rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size; |
| 749 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 750 | bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL); |
| 751 | if (IS_ERR(bio)) { |
| 752 | ret = PTR_ERR(bio); |
| 753 | goto free_ppa_list; |
| 754 | } |
| 755 | |
| 756 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 757 | bio_set_op_attrs(bio, bio_op, 0); |
| 758 | |
| 759 | rqd.bio = bio; |
| 760 | rqd.opcode = cmd_op; |
| 761 | rqd.flags = flags; |
| 762 | rqd.nr_ppas = lm->smeta_sec; |
| 763 | rqd.end_io = pblk_end_io_sync; |
| 764 | rqd.private = &wait; |
| 765 | |
| 766 | for (i = 0; i < lm->smeta_sec; i++, paddr++) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 767 | struct pblk_sec_meta *meta_list = rqd.meta_list; |
| 768 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 769 | rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 770 | |
| 771 | if (dir == WRITE) { |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 772 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 773 | |
| 774 | meta_list[i].lba = lba_list[paddr] = addr_empty; |
| 775 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* |
| 779 | * This I/O is sent by the write thread when a line is replace. Since |
| 780 | * the write thread is the only one sending write and erase commands, |
| 781 | * there is no need to take the LUN semaphore. |
| 782 | */ |
| 783 | ret = pblk_submit_io(pblk, &rqd); |
| 784 | if (ret) { |
| 785 | pr_err("pblk: smeta I/O submission failed: %d\n", ret); |
| 786 | bio_put(bio); |
| 787 | goto free_ppa_list; |
| 788 | } |
| 789 | |
| 790 | if (!wait_for_completion_io_timeout(&wait, |
| 791 | msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) { |
| 792 | pr_err("pblk: smeta I/O timed out\n"); |
| 793 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 794 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 795 | |
| 796 | if (rqd.error) { |
| 797 | if (dir == WRITE) |
| 798 | pblk_log_write_err(pblk, &rqd); |
| 799 | else |
| 800 | pblk_log_read_err(pblk, &rqd); |
| 801 | } |
| 802 | |
| 803 | free_ppa_list: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 804 | 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] | 805 | |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line) |
| 810 | { |
| 811 | u64 bpaddr = pblk_line_smeta_start(pblk, line); |
| 812 | |
| 813 | return pblk_line_submit_smeta_io(pblk, line, bpaddr, READ); |
| 814 | } |
| 815 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 816 | int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line, |
| 817 | void *emeta_buf) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 818 | { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 819 | return pblk_line_submit_emeta_io(pblk, line, emeta_buf, |
| 820 | line->emeta_ssec, READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd, |
| 824 | struct ppa_addr ppa) |
| 825 | { |
| 826 | rqd->opcode = NVM_OP_ERASE; |
| 827 | rqd->ppa_addr = ppa; |
| 828 | rqd->nr_ppas = 1; |
| 829 | rqd->flags = pblk_set_progr_mode(pblk, ERASE); |
| 830 | rqd->bio = NULL; |
| 831 | } |
| 832 | |
| 833 | static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa) |
| 834 | { |
| 835 | struct nvm_rq rqd; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 836 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 837 | DECLARE_COMPLETION_ONSTACK(wait); |
| 838 | |
| 839 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 840 | |
| 841 | pblk_setup_e_rq(pblk, &rqd, ppa); |
| 842 | |
| 843 | rqd.end_io = pblk_end_io_sync; |
| 844 | rqd.private = &wait; |
| 845 | |
| 846 | /* The write thread schedules erases so that it minimizes disturbances |
| 847 | * with writes. Thus, there is no need to take the LUN semaphore. |
| 848 | */ |
| 849 | ret = pblk_submit_io(pblk, &rqd); |
| 850 | if (ret) { |
| 851 | struct nvm_tgt_dev *dev = pblk->dev; |
| 852 | struct nvm_geo *geo = &dev->geo; |
| 853 | |
| 854 | pr_err("pblk: could not sync erase line:%d,blk:%d\n", |
| 855 | pblk_dev_ppa_to_line(ppa), |
| 856 | pblk_dev_ppa_to_pos(geo, ppa)); |
| 857 | |
| 858 | rqd.error = ret; |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | if (!wait_for_completion_io_timeout(&wait, |
| 863 | msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) { |
| 864 | pr_err("pblk: sync erase timed out\n"); |
| 865 | } |
| 866 | |
| 867 | out: |
| 868 | rqd.private = pblk; |
| 869 | __pblk_end_io_erase(pblk, &rqd); |
| 870 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 871 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | int pblk_line_erase(struct pblk *pblk, struct pblk_line *line) |
| 875 | { |
| 876 | struct pblk_line_meta *lm = &pblk->lm; |
| 877 | struct ppa_addr ppa; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 878 | int ret, bit = -1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 879 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 880 | /* Erase only good blocks, one at a time */ |
| 881 | do { |
| 882 | spin_lock(&line->lock); |
| 883 | bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line, |
| 884 | bit + 1); |
| 885 | if (bit >= lm->blk_per_line) { |
| 886 | spin_unlock(&line->lock); |
| 887 | break; |
| 888 | } |
| 889 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 890 | ppa = pblk->luns[bit].bppa; /* set ch and lun */ |
| 891 | ppa.g.blk = line->id; |
| 892 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 893 | atomic_dec(&line->left_eblks); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 894 | WARN_ON(test_and_set_bit(bit, line->erase_bitmap)); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 895 | spin_unlock(&line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 896 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 897 | ret = pblk_blk_erase_sync(pblk, ppa); |
| 898 | if (ret) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 899 | pr_err("pblk: failed to erase line %d\n", line->id); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 900 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 901 | } |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 902 | } while (1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 907 | static void pblk_line_setup_metadata(struct pblk_line *line, |
| 908 | struct pblk_line_mgmt *l_mg, |
| 909 | struct pblk_line_meta *lm) |
| 910 | { |
| 911 | int meta_line; |
| 912 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 913 | lockdep_assert_held(&l_mg->free_lock); |
| 914 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 915 | retry_meta: |
| 916 | meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES); |
| 917 | if (meta_line == PBLK_DATA_LINES) { |
| 918 | spin_unlock(&l_mg->free_lock); |
| 919 | io_schedule(); |
| 920 | spin_lock(&l_mg->free_lock); |
| 921 | goto retry_meta; |
| 922 | } |
| 923 | |
| 924 | set_bit(meta_line, &l_mg->meta_bitmap); |
| 925 | line->meta_line = meta_line; |
| 926 | |
| 927 | line->smeta = l_mg->sline_meta[meta_line]; |
| 928 | line->emeta = l_mg->eline_meta[meta_line]; |
| 929 | |
| 930 | memset(line->smeta, 0, lm->smeta_len); |
| 931 | memset(line->emeta->buf, 0, lm->emeta_len[0]); |
| 932 | |
| 933 | line->emeta->mem = 0; |
| 934 | atomic_set(&line->emeta->sync, 0); |
| 935 | } |
| 936 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 937 | /* For now lines are always assumed full lines. Thus, smeta former and current |
| 938 | * lun bitmaps are omitted. |
| 939 | */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 940 | static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 941 | struct pblk_line *cur) |
| 942 | { |
| 943 | struct nvm_tgt_dev *dev = pblk->dev; |
| 944 | struct nvm_geo *geo = &dev->geo; |
| 945 | struct pblk_line_meta *lm = &pblk->lm; |
| 946 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 947 | struct pblk_emeta *emeta = line->emeta; |
| 948 | struct line_emeta *emeta_buf = emeta->buf; |
| 949 | struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 950 | int nr_blk_line; |
| 951 | |
| 952 | /* After erasing the line, new bad blocks might appear and we risk |
| 953 | * having an invalid line |
| 954 | */ |
| 955 | nr_blk_line = lm->blk_per_line - |
| 956 | bitmap_weight(line->blk_bitmap, lm->blk_per_line); |
| 957 | if (nr_blk_line < lm->min_blk_line) { |
| 958 | spin_lock(&l_mg->free_lock); |
| 959 | spin_lock(&line->lock); |
| 960 | line->state = PBLK_LINESTATE_BAD; |
| 961 | spin_unlock(&line->lock); |
| 962 | |
| 963 | list_add_tail(&line->list, &l_mg->bad_list); |
| 964 | spin_unlock(&l_mg->free_lock); |
| 965 | |
| 966 | pr_debug("pblk: line %d is bad\n", line->id); |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | /* Run-time metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 972 | line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 973 | |
| 974 | /* Mark LUNs allocated in this line (all for now) */ |
| 975 | bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len); |
| 976 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 977 | smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC); |
| 978 | memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16); |
| 979 | smeta_buf->header.id = cpu_to_le32(line->id); |
| 980 | smeta_buf->header.type = cpu_to_le16(line->type); |
Rakesh Pandit | c79819b | 2017-10-13 14:45:59 +0200 | [diff] [blame] | 981 | smeta_buf->header.version = SMETA_VERSION; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 982 | |
| 983 | /* Start metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 984 | smeta_buf->seq_nr = cpu_to_le64(line->seq_nr); |
| 985 | smeta_buf->window_wr_lun = cpu_to_le32(geo->nr_luns); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 986 | |
| 987 | /* Fill metadata among lines */ |
| 988 | if (cur) { |
| 989 | memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 990 | smeta_buf->prev_id = cpu_to_le32(cur->id); |
| 991 | cur->emeta->buf->next_id = cpu_to_le32(line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 992 | } else { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 993 | smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* All smeta must be set at this point */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 997 | smeta_buf->header.crc = cpu_to_le32( |
| 998 | pblk_calc_meta_header_crc(pblk, &smeta_buf->header)); |
| 999 | smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* End metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1002 | memcpy(&emeta_buf->header, &smeta_buf->header, |
| 1003 | sizeof(struct line_header)); |
| 1004 | emeta_buf->seq_nr = cpu_to_le64(line->seq_nr); |
| 1005 | emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line); |
| 1006 | emeta_buf->nr_valid_lbas = cpu_to_le64(0); |
| 1007 | emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY); |
| 1008 | emeta_buf->crc = cpu_to_le32(0); |
| 1009 | emeta_buf->prev_id = smeta_buf->prev_id; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1010 | |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
| 1014 | /* For now lines are always assumed full lines. Thus, smeta former and current |
| 1015 | * lun bitmaps are omitted. |
| 1016 | */ |
| 1017 | static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line, |
| 1018 | int init) |
| 1019 | { |
| 1020 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1021 | struct nvm_geo *geo = &dev->geo; |
| 1022 | struct pblk_line_meta *lm = &pblk->lm; |
| 1023 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1024 | int nr_bb = 0; |
| 1025 | u64 off; |
| 1026 | int bit = -1; |
| 1027 | |
| 1028 | line->sec_in_line = lm->sec_per_line; |
| 1029 | |
| 1030 | /* Capture bad block information on line mapping bitmaps */ |
| 1031 | while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line, |
| 1032 | bit + 1)) < lm->blk_per_line) { |
| 1033 | off = bit * geo->sec_per_pl; |
| 1034 | bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off, |
| 1035 | lm->sec_per_line); |
| 1036 | bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux, |
| 1037 | lm->sec_per_line); |
| 1038 | line->sec_in_line -= geo->sec_per_blk; |
| 1039 | if (bit >= lm->emeta_bb) |
| 1040 | nr_bb++; |
| 1041 | } |
| 1042 | |
| 1043 | /* Mark smeta metadata sectors as bad sectors */ |
| 1044 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 1045 | off = bit * geo->sec_per_pl; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1046 | bitmap_set(line->map_bitmap, off, lm->smeta_sec); |
| 1047 | line->sec_in_line -= lm->smeta_sec; |
| 1048 | line->smeta_ssec = off; |
| 1049 | line->cur_sec = off + lm->smeta_sec; |
| 1050 | |
| 1051 | if (init && pblk_line_submit_smeta_io(pblk, line, off, WRITE)) { |
| 1052 | pr_debug("pblk: line smeta I/O failed. Retry\n"); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1053 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line); |
| 1057 | |
| 1058 | /* Mark emeta metadata sectors as bad sectors. We need to consider bad |
| 1059 | * blocks to make sure that there are enough sectors to store emeta |
| 1060 | */ |
| 1061 | bit = lm->sec_per_line; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1062 | off = lm->sec_per_line - lm->emeta_sec[0]; |
| 1063 | bitmap_set(line->invalid_bitmap, off, lm->emeta_sec[0]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1064 | while (nr_bb) { |
| 1065 | off -= geo->sec_per_pl; |
| 1066 | if (!test_bit(off, line->invalid_bitmap)) { |
| 1067 | bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl); |
| 1068 | nr_bb--; |
| 1069 | } |
| 1070 | } |
| 1071 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1072 | line->sec_in_line -= lm->emeta_sec[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1073 | line->emeta_ssec = off; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1074 | line->nr_valid_lbas = 0; |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 1075 | line->left_msecs = line->sec_in_line; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1076 | *line->vsc = cpu_to_le32(line->sec_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1077 | |
| 1078 | if (lm->sec_per_line - line->sec_in_line != |
| 1079 | bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) { |
| 1080 | spin_lock(&line->lock); |
| 1081 | line->state = PBLK_LINESTATE_BAD; |
| 1082 | spin_unlock(&line->lock); |
| 1083 | |
| 1084 | list_add_tail(&line->list, &l_mg->bad_list); |
| 1085 | pr_err("pblk: unexpected line %d is bad\n", line->id); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | return 1; |
| 1091 | } |
| 1092 | |
| 1093 | static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line) |
| 1094 | { |
| 1095 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 1096 | int blk_in_line = atomic_read(&line->blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1097 | |
| 1098 | line->map_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC); |
| 1099 | if (!line->map_bitmap) |
| 1100 | return -ENOMEM; |
| 1101 | memset(line->map_bitmap, 0, lm->sec_bitmap_len); |
| 1102 | |
| 1103 | /* invalid_bitmap is special since it is used when line is closed. No |
| 1104 | * need to zeroized; it will be initialized using bb info form |
| 1105 | * map_bitmap |
| 1106 | */ |
| 1107 | line->invalid_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC); |
| 1108 | if (!line->invalid_bitmap) { |
| 1109 | mempool_free(line->map_bitmap, pblk->line_meta_pool); |
| 1110 | return -ENOMEM; |
| 1111 | } |
| 1112 | |
| 1113 | spin_lock(&line->lock); |
| 1114 | if (line->state != PBLK_LINESTATE_FREE) { |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1115 | mempool_free(line->invalid_bitmap, pblk->line_meta_pool); |
| 1116 | mempool_free(line->map_bitmap, pblk->line_meta_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1117 | spin_unlock(&line->lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1118 | WARN(1, "pblk: corrupted line %d, state %d\n", |
| 1119 | line->id, line->state); |
| 1120 | return -EAGAIN; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1121 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1122 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1123 | line->state = PBLK_LINESTATE_OPEN; |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 1124 | |
| 1125 | atomic_set(&line->left_eblks, blk_in_line); |
| 1126 | atomic_set(&line->left_seblks, blk_in_line); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1127 | |
| 1128 | line->meta_distance = lm->meta_distance; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1129 | spin_unlock(&line->lock); |
| 1130 | |
| 1131 | /* Bad blocks do not need to be erased */ |
| 1132 | bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1133 | |
| 1134 | kref_init(&line->ref); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line) |
| 1140 | { |
| 1141 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1142 | int ret; |
| 1143 | |
| 1144 | spin_lock(&l_mg->free_lock); |
| 1145 | l_mg->data_line = line; |
| 1146 | list_del(&line->list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1147 | |
| 1148 | ret = pblk_line_prepare(pblk, line); |
| 1149 | if (ret) { |
| 1150 | list_add(&line->list, &l_mg->free_list); |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1151 | spin_unlock(&l_mg->free_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1152 | return ret; |
| 1153 | } |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1154 | spin_unlock(&l_mg->free_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1155 | |
| 1156 | pblk_rl_free_lines_dec(&pblk->rl, line); |
| 1157 | |
| 1158 | if (!pblk_line_init_bb(pblk, line, 0)) { |
| 1159 | list_add(&line->list, &l_mg->free_list); |
| 1160 | return -EINTR; |
| 1161 | } |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line) |
| 1167 | { |
| 1168 | mempool_free(line->map_bitmap, pblk->line_meta_pool); |
| 1169 | line->map_bitmap = NULL; |
| 1170 | line->smeta = NULL; |
| 1171 | line->emeta = NULL; |
| 1172 | } |
| 1173 | |
| 1174 | struct pblk_line *pblk_line_get(struct pblk *pblk) |
| 1175 | { |
| 1176 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1177 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1178 | struct pblk_line *line; |
| 1179 | int ret, bit; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1180 | |
| 1181 | lockdep_assert_held(&l_mg->free_lock); |
| 1182 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1183 | retry: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1184 | if (list_empty(&l_mg->free_list)) { |
| 1185 | pr_err("pblk: no free lines\n"); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1186 | return NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | line = list_first_entry(&l_mg->free_list, struct pblk_line, list); |
| 1190 | list_del(&line->list); |
| 1191 | l_mg->nr_free_lines--; |
| 1192 | |
| 1193 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 1194 | if (unlikely(bit >= lm->blk_per_line)) { |
| 1195 | spin_lock(&line->lock); |
| 1196 | line->state = PBLK_LINESTATE_BAD; |
| 1197 | spin_unlock(&line->lock); |
| 1198 | |
| 1199 | list_add_tail(&line->list, &l_mg->bad_list); |
| 1200 | |
| 1201 | pr_debug("pblk: line %d is bad\n", line->id); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1202 | goto retry; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1205 | ret = pblk_line_prepare(pblk, line); |
| 1206 | if (ret) { |
| 1207 | if (ret == -EAGAIN) { |
| 1208 | list_add(&line->list, &l_mg->corrupt_list); |
| 1209 | goto retry; |
| 1210 | } else { |
| 1211 | pr_err("pblk: failed to prepare line %d\n", line->id); |
| 1212 | list_add(&line->list, &l_mg->free_list); |
| 1213 | l_mg->nr_free_lines++; |
| 1214 | return NULL; |
| 1215 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1218 | return line; |
| 1219 | } |
| 1220 | |
| 1221 | static struct pblk_line *pblk_line_retry(struct pblk *pblk, |
| 1222 | struct pblk_line *line) |
| 1223 | { |
| 1224 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1225 | struct pblk_line *retry_line; |
| 1226 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1227 | retry: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1228 | spin_lock(&l_mg->free_lock); |
| 1229 | retry_line = pblk_line_get(pblk); |
| 1230 | if (!retry_line) { |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1231 | l_mg->data_line = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1232 | spin_unlock(&l_mg->free_lock); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | retry_line->smeta = line->smeta; |
| 1237 | retry_line->emeta = line->emeta; |
| 1238 | retry_line->meta_line = line->meta_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1239 | |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1240 | pblk_line_free(pblk, line); |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1241 | l_mg->data_line = retry_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1242 | spin_unlock(&l_mg->free_lock); |
| 1243 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1244 | pblk_rl_free_lines_dec(&pblk->rl, retry_line); |
| 1245 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1246 | if (pblk_line_erase(pblk, retry_line)) |
| 1247 | goto retry; |
| 1248 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1249 | return retry_line; |
| 1250 | } |
| 1251 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1252 | static void pblk_set_space_limit(struct pblk *pblk) |
| 1253 | { |
| 1254 | struct pblk_rl *rl = &pblk->rl; |
| 1255 | |
| 1256 | atomic_set(&rl->rb_space, 0); |
| 1257 | } |
| 1258 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1259 | struct pblk_line *pblk_line_get_first_data(struct pblk *pblk) |
| 1260 | { |
| 1261 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1262 | struct pblk_line *line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1263 | int is_next = 0; |
| 1264 | |
| 1265 | spin_lock(&l_mg->free_lock); |
| 1266 | line = pblk_line_get(pblk); |
| 1267 | if (!line) { |
| 1268 | spin_unlock(&l_mg->free_lock); |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | |
| 1272 | line->seq_nr = l_mg->d_seq_nr++; |
| 1273 | line->type = PBLK_LINETYPE_DATA; |
| 1274 | l_mg->data_line = line; |
| 1275 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1276 | pblk_line_setup_metadata(line, l_mg, &pblk->lm); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1277 | |
| 1278 | /* Allocate next line for preparation */ |
| 1279 | l_mg->data_next = pblk_line_get(pblk); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1280 | if (!l_mg->data_next) { |
| 1281 | /* If we cannot get a new line, we need to stop the pipeline. |
| 1282 | * Only allow as many writes in as we can store safely and then |
| 1283 | * fail gracefully |
| 1284 | */ |
| 1285 | pblk_set_space_limit(pblk); |
| 1286 | |
| 1287 | l_mg->data_next = NULL; |
| 1288 | } else { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1289 | l_mg->data_next->seq_nr = l_mg->d_seq_nr++; |
| 1290 | l_mg->data_next->type = PBLK_LINETYPE_DATA; |
| 1291 | is_next = 1; |
| 1292 | } |
| 1293 | spin_unlock(&l_mg->free_lock); |
| 1294 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1295 | if (pblk_line_erase(pblk, line)) { |
| 1296 | line = pblk_line_retry(pblk, line); |
| 1297 | if (!line) |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1301 | pblk_rl_free_lines_dec(&pblk->rl, line); |
| 1302 | if (is_next) |
| 1303 | pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next); |
| 1304 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1305 | retry_setup: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1306 | if (!pblk_line_init_metadata(pblk, line, NULL)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1307 | line = pblk_line_retry(pblk, line); |
| 1308 | if (!line) |
| 1309 | return NULL; |
| 1310 | |
| 1311 | goto retry_setup; |
| 1312 | } |
| 1313 | |
| 1314 | if (!pblk_line_init_bb(pblk, line, 1)) { |
| 1315 | line = pblk_line_retry(pblk, line); |
| 1316 | if (!line) |
| 1317 | return NULL; |
| 1318 | |
| 1319 | goto retry_setup; |
| 1320 | } |
| 1321 | |
| 1322 | return line; |
| 1323 | } |
| 1324 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1325 | static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line) |
| 1326 | { |
| 1327 | lockdep_assert_held(&pblk->l_mg.free_lock); |
| 1328 | |
| 1329 | pblk_set_space_limit(pblk); |
| 1330 | pblk->state = PBLK_STATE_STOPPING; |
| 1331 | } |
| 1332 | |
| 1333 | void pblk_pipeline_stop(struct pblk *pblk) |
| 1334 | { |
| 1335 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1336 | int ret; |
| 1337 | |
| 1338 | spin_lock(&l_mg->free_lock); |
| 1339 | if (pblk->state == PBLK_STATE_RECOVERING || |
| 1340 | pblk->state == PBLK_STATE_STOPPED) { |
| 1341 | spin_unlock(&l_mg->free_lock); |
| 1342 | return; |
| 1343 | } |
| 1344 | pblk->state = PBLK_STATE_RECOVERING; |
| 1345 | spin_unlock(&l_mg->free_lock); |
| 1346 | |
| 1347 | pblk_flush_writer(pblk); |
| 1348 | pblk_wait_for_meta(pblk); |
| 1349 | |
| 1350 | ret = pblk_recov_pad(pblk); |
| 1351 | if (ret) { |
| 1352 | pr_err("pblk: could not close data on teardown(%d)\n", ret); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 1356 | flush_workqueue(pblk->bb_wq); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1357 | pblk_line_close_meta_sync(pblk); |
| 1358 | |
| 1359 | spin_lock(&l_mg->free_lock); |
| 1360 | pblk->state = PBLK_STATE_STOPPED; |
| 1361 | l_mg->data_line = NULL; |
| 1362 | l_mg->data_next = NULL; |
| 1363 | spin_unlock(&l_mg->free_lock); |
| 1364 | } |
| 1365 | |
| 1366 | void pblk_line_replace_data(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1367 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1368 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1369 | struct pblk_line *cur, *new; |
| 1370 | unsigned int left_seblks; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1371 | int is_next = 0; |
| 1372 | |
| 1373 | cur = l_mg->data_line; |
| 1374 | new = l_mg->data_next; |
| 1375 | if (!new) |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1376 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1377 | l_mg->data_line = new; |
| 1378 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1379 | spin_lock(&l_mg->free_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1380 | if (pblk->state != PBLK_STATE_RUNNING) { |
| 1381 | l_mg->data_line = NULL; |
| 1382 | l_mg->data_next = NULL; |
| 1383 | spin_unlock(&l_mg->free_lock); |
| 1384 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1387 | pblk_line_setup_metadata(new, l_mg, &pblk->lm); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1388 | spin_unlock(&l_mg->free_lock); |
| 1389 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1390 | retry_erase: |
| 1391 | left_seblks = atomic_read(&new->left_seblks); |
| 1392 | if (left_seblks) { |
| 1393 | /* If line is not fully erased, erase it */ |
| 1394 | if (atomic_read(&new->left_eblks)) { |
| 1395 | if (pblk_line_erase(pblk, new)) |
| 1396 | return; |
| 1397 | } else { |
| 1398 | io_schedule(); |
| 1399 | } |
| 1400 | goto retry_erase; |
| 1401 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1402 | |
| 1403 | retry_setup: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1404 | if (!pblk_line_init_metadata(pblk, new, cur)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1405 | new = pblk_line_retry(pblk, new); |
Javier González | f3236cef | 2017-04-22 01:32:46 +0200 | [diff] [blame] | 1406 | if (!new) |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1407 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1408 | |
| 1409 | goto retry_setup; |
| 1410 | } |
| 1411 | |
| 1412 | if (!pblk_line_init_bb(pblk, new, 1)) { |
| 1413 | new = pblk_line_retry(pblk, new); |
| 1414 | if (!new) |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1415 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1416 | |
| 1417 | goto retry_setup; |
| 1418 | } |
| 1419 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1420 | /* Allocate next line for preparation */ |
| 1421 | spin_lock(&l_mg->free_lock); |
| 1422 | l_mg->data_next = pblk_line_get(pblk); |
| 1423 | if (!l_mg->data_next) { |
| 1424 | /* If we cannot get a new line, we need to stop the pipeline. |
| 1425 | * Only allow as many writes in as we can store safely and then |
| 1426 | * fail gracefully |
| 1427 | */ |
| 1428 | pblk_stop_writes(pblk, new); |
| 1429 | l_mg->data_next = NULL; |
| 1430 | } else { |
| 1431 | l_mg->data_next->seq_nr = l_mg->d_seq_nr++; |
| 1432 | l_mg->data_next->type = PBLK_LINETYPE_DATA; |
| 1433 | is_next = 1; |
| 1434 | } |
| 1435 | spin_unlock(&l_mg->free_lock); |
| 1436 | |
| 1437 | if (is_next) |
| 1438 | pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | void pblk_line_free(struct pblk *pblk, struct pblk_line *line) |
| 1442 | { |
| 1443 | if (line->map_bitmap) |
| 1444 | mempool_free(line->map_bitmap, pblk->line_meta_pool); |
| 1445 | if (line->invalid_bitmap) |
| 1446 | mempool_free(line->invalid_bitmap, pblk->line_meta_pool); |
| 1447 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1448 | *line->vsc = cpu_to_le32(EMPTY_ENTRY); |
| 1449 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1450 | line->map_bitmap = NULL; |
| 1451 | line->invalid_bitmap = NULL; |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1452 | line->smeta = NULL; |
| 1453 | line->emeta = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | void pblk_line_put(struct kref *ref) |
| 1457 | { |
| 1458 | struct pblk_line *line = container_of(ref, struct pblk_line, ref); |
| 1459 | struct pblk *pblk = line->pblk; |
| 1460 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1461 | |
| 1462 | spin_lock(&line->lock); |
| 1463 | WARN_ON(line->state != PBLK_LINESTATE_GC); |
| 1464 | line->state = PBLK_LINESTATE_FREE; |
| 1465 | line->gc_group = PBLK_LINEGC_NONE; |
| 1466 | pblk_line_free(pblk, line); |
| 1467 | spin_unlock(&line->lock); |
| 1468 | |
| 1469 | spin_lock(&l_mg->free_lock); |
| 1470 | list_add_tail(&line->list, &l_mg->free_list); |
| 1471 | l_mg->nr_free_lines++; |
| 1472 | spin_unlock(&l_mg->free_lock); |
| 1473 | |
| 1474 | pblk_rl_free_lines_inc(&pblk->rl, line); |
| 1475 | } |
| 1476 | |
| 1477 | int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa) |
| 1478 | { |
| 1479 | struct nvm_rq *rqd; |
| 1480 | int err; |
| 1481 | |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame^] | 1482 | rqd = mempool_alloc(pblk->e_rq_pool, GFP_KERNEL); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 1483 | memset(rqd, 0, pblk_g_rq_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1484 | |
| 1485 | pblk_setup_e_rq(pblk, rqd, ppa); |
| 1486 | |
| 1487 | rqd->end_io = pblk_end_io_erase; |
| 1488 | rqd->private = pblk; |
| 1489 | |
| 1490 | /* The write thread schedules erases so that it minimizes disturbances |
| 1491 | * with writes. Thus, there is no need to take the LUN semaphore. |
| 1492 | */ |
| 1493 | err = pblk_submit_io(pblk, rqd); |
| 1494 | if (err) { |
| 1495 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1496 | struct nvm_geo *geo = &dev->geo; |
| 1497 | |
| 1498 | pr_err("pblk: could not async erase line:%d,blk:%d\n", |
| 1499 | pblk_dev_ppa_to_line(ppa), |
| 1500 | pblk_dev_ppa_to_pos(geo, ppa)); |
| 1501 | } |
| 1502 | |
| 1503 | return err; |
| 1504 | } |
| 1505 | |
| 1506 | struct pblk_line *pblk_line_get_data(struct pblk *pblk) |
| 1507 | { |
| 1508 | return pblk->l_mg.data_line; |
| 1509 | } |
| 1510 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 1511 | /* For now, always erase next line */ |
| 1512 | struct pblk_line *pblk_line_get_erase(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1513 | { |
| 1514 | return pblk->l_mg.data_next; |
| 1515 | } |
| 1516 | |
| 1517 | int pblk_line_is_full(struct pblk_line *line) |
| 1518 | { |
| 1519 | return (line->left_msecs == 0); |
| 1520 | } |
| 1521 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1522 | void pblk_line_close_meta_sync(struct pblk *pblk) |
| 1523 | { |
| 1524 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1525 | struct pblk_line_meta *lm = &pblk->lm; |
| 1526 | struct pblk_line *line, *tline; |
| 1527 | LIST_HEAD(list); |
| 1528 | |
| 1529 | spin_lock(&l_mg->close_lock); |
| 1530 | if (list_empty(&l_mg->emeta_list)) { |
| 1531 | spin_unlock(&l_mg->close_lock); |
| 1532 | return; |
| 1533 | } |
| 1534 | |
| 1535 | list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev); |
| 1536 | spin_unlock(&l_mg->close_lock); |
| 1537 | |
| 1538 | list_for_each_entry_safe(line, tline, &list, list) { |
| 1539 | struct pblk_emeta *emeta = line->emeta; |
| 1540 | |
| 1541 | while (emeta->mem < lm->emeta_len[0]) { |
| 1542 | int ret; |
| 1543 | |
| 1544 | ret = pblk_submit_meta_io(pblk, line); |
| 1545 | if (ret) { |
| 1546 | pr_err("pblk: sync meta line %d failed (%d)\n", |
| 1547 | line->id, ret); |
| 1548 | return; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | pblk_wait_for_meta(pblk); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 1554 | flush_workqueue(pblk->close_wq); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static void pblk_line_should_sync_meta(struct pblk *pblk) |
| 1558 | { |
| 1559 | if (pblk_rl_is_limit(&pblk->rl)) |
| 1560 | pblk_line_close_meta_sync(pblk); |
| 1561 | } |
| 1562 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1563 | void pblk_line_close(struct pblk *pblk, struct pblk_line *line) |
| 1564 | { |
| 1565 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1566 | struct list_head *move_list; |
| 1567 | |
Javier González | a84ebb8 | 2017-06-30 17:56:43 +0200 | [diff] [blame] | 1568 | #ifdef CONFIG_NVM_DEBUG |
| 1569 | struct pblk_line_meta *lm = &pblk->lm; |
| 1570 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1571 | WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line), |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1572 | "pblk: corrupt closed line %d\n", line->id); |
Javier González | a84ebb8 | 2017-06-30 17:56:43 +0200 | [diff] [blame] | 1573 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1574 | |
| 1575 | spin_lock(&l_mg->free_lock); |
| 1576 | WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap)); |
| 1577 | spin_unlock(&l_mg->free_lock); |
| 1578 | |
| 1579 | spin_lock(&l_mg->gc_lock); |
| 1580 | spin_lock(&line->lock); |
| 1581 | WARN_ON(line->state != PBLK_LINESTATE_OPEN); |
| 1582 | line->state = PBLK_LINESTATE_CLOSED; |
| 1583 | move_list = pblk_line_gc_list(pblk, line); |
| 1584 | |
| 1585 | list_add_tail(&line->list, move_list); |
| 1586 | |
| 1587 | mempool_free(line->map_bitmap, pblk->line_meta_pool); |
| 1588 | line->map_bitmap = NULL; |
| 1589 | line->smeta = NULL; |
| 1590 | line->emeta = NULL; |
| 1591 | |
| 1592 | spin_unlock(&line->lock); |
| 1593 | spin_unlock(&l_mg->gc_lock); |
| 1594 | } |
| 1595 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1596 | void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line) |
| 1597 | { |
| 1598 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1599 | struct pblk_line_meta *lm = &pblk->lm; |
| 1600 | struct pblk_emeta *emeta = line->emeta; |
| 1601 | struct line_emeta *emeta_buf = emeta->buf; |
| 1602 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1603 | /* No need for exact vsc value; avoid a big line lock and take aprox. */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1604 | memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len); |
| 1605 | memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len); |
| 1606 | |
| 1607 | emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas); |
| 1608 | emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf)); |
| 1609 | |
| 1610 | spin_lock(&l_mg->close_lock); |
| 1611 | spin_lock(&line->lock); |
| 1612 | list_add_tail(&line->list, &l_mg->emeta_list); |
| 1613 | spin_unlock(&line->lock); |
| 1614 | spin_unlock(&l_mg->close_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1615 | |
| 1616 | pblk_line_should_sync_meta(pblk); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1617 | } |
| 1618 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1619 | void pblk_line_close_ws(struct work_struct *work) |
| 1620 | { |
| 1621 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 1622 | ws); |
| 1623 | struct pblk *pblk = line_ws->pblk; |
| 1624 | struct pblk_line *line = line_ws->line; |
| 1625 | |
| 1626 | pblk_line_close(pblk, line); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1627 | mempool_free(line_ws, pblk->gen_ws_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | void pblk_line_mark_bb(struct work_struct *work) |
| 1631 | { |
| 1632 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 1633 | ws); |
| 1634 | struct pblk *pblk = line_ws->pblk; |
| 1635 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1636 | struct ppa_addr *ppa = line_ws->priv; |
| 1637 | int ret; |
| 1638 | |
| 1639 | ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD); |
| 1640 | if (ret) { |
| 1641 | struct pblk_line *line; |
| 1642 | int pos; |
| 1643 | |
| 1644 | line = &pblk->lines[pblk_dev_ppa_to_line(*ppa)]; |
| 1645 | pos = pblk_dev_ppa_to_pos(&dev->geo, *ppa); |
| 1646 | |
| 1647 | pr_err("pblk: failed to mark bb, line:%d, pos:%d\n", |
| 1648 | line->id, pos); |
| 1649 | } |
| 1650 | |
| 1651 | kfree(ppa); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1652 | mempool_free(line_ws, pblk->gen_ws_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1655 | void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv, |
| 1656 | void (*work)(struct work_struct *), gfp_t gfp_mask, |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 1657 | struct workqueue_struct *wq) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1658 | { |
| 1659 | struct pblk_line_ws *line_ws; |
| 1660 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1661 | line_ws = mempool_alloc(pblk->gen_ws_pool, gfp_mask); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1662 | if (!line_ws) |
| 1663 | return; |
| 1664 | |
| 1665 | line_ws->pblk = pblk; |
| 1666 | line_ws->line = line; |
| 1667 | line_ws->priv = priv; |
| 1668 | |
| 1669 | INIT_WORK(&line_ws->ws, work); |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 1670 | queue_work(wq, &line_ws->ws); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1673 | static void __pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, |
| 1674 | int nr_ppas, int pos) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1675 | { |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1676 | struct pblk_lun *rlun = &pblk->luns[pos]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1677 | int ret; |
| 1678 | |
| 1679 | /* |
| 1680 | * Only send one inflight I/O per LUN. Since we map at a page |
| 1681 | * granurality, all ppas in the I/O will map to the same LUN |
| 1682 | */ |
| 1683 | #ifdef CONFIG_NVM_DEBUG |
| 1684 | int i; |
| 1685 | |
| 1686 | for (i = 1; i < nr_ppas; i++) |
| 1687 | WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun || |
| 1688 | ppa_list[0].g.ch != ppa_list[i].g.ch); |
| 1689 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1690 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1691 | ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000)); |
Rakesh Pandit | c549384 | 2017-10-13 14:45:58 +0200 | [diff] [blame] | 1692 | if (ret == -ETIME || ret == -EINTR) |
| 1693 | pr_err("pblk: taking lun semaphore timed out: err %d\n", -ret); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1696 | void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas) |
| 1697 | { |
| 1698 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1699 | struct nvm_geo *geo = &dev->geo; |
| 1700 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1701 | |
| 1702 | __pblk_down_page(pblk, ppa_list, nr_ppas, pos); |
| 1703 | } |
| 1704 | |
| 1705 | void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
| 1706 | unsigned long *lun_bitmap) |
| 1707 | { |
| 1708 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1709 | struct nvm_geo *geo = &dev->geo; |
| 1710 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1711 | |
| 1712 | /* If the LUN has been locked for this same request, do no attempt to |
| 1713 | * lock it again |
| 1714 | */ |
| 1715 | if (test_and_set_bit(pos, lun_bitmap)) |
| 1716 | return; |
| 1717 | |
| 1718 | __pblk_down_page(pblk, ppa_list, nr_ppas, pos); |
| 1719 | } |
| 1720 | |
| 1721 | void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas) |
| 1722 | { |
| 1723 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1724 | struct nvm_geo *geo = &dev->geo; |
| 1725 | struct pblk_lun *rlun; |
| 1726 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1727 | |
| 1728 | #ifdef CONFIG_NVM_DEBUG |
| 1729 | int i; |
| 1730 | |
| 1731 | for (i = 1; i < nr_ppas; i++) |
| 1732 | WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun || |
| 1733 | ppa_list[0].g.ch != ppa_list[i].g.ch); |
| 1734 | #endif |
| 1735 | |
| 1736 | rlun = &pblk->luns[pos]; |
| 1737 | up(&rlun->wr_sem); |
| 1738 | } |
| 1739 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1740 | void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
| 1741 | unsigned long *lun_bitmap) |
| 1742 | { |
| 1743 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1744 | struct nvm_geo *geo = &dev->geo; |
| 1745 | struct pblk_lun *rlun; |
| 1746 | int nr_luns = geo->nr_luns; |
| 1747 | int bit = -1; |
| 1748 | |
| 1749 | while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) { |
| 1750 | rlun = &pblk->luns[bit]; |
| 1751 | up(&rlun->wr_sem); |
| 1752 | } |
| 1753 | |
| 1754 | kfree(lun_bitmap); |
| 1755 | } |
| 1756 | |
| 1757 | void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa) |
| 1758 | { |
| 1759 | struct ppa_addr l2p_ppa; |
| 1760 | |
| 1761 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1762 | if (!(lba < pblk->rl.nr_secs)) { |
| 1763 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1764 | return; |
| 1765 | } |
| 1766 | |
| 1767 | spin_lock(&pblk->trans_lock); |
| 1768 | l2p_ppa = pblk_trans_map_get(pblk, lba); |
| 1769 | |
| 1770 | if (!pblk_addr_in_cache(l2p_ppa) && !pblk_ppa_empty(l2p_ppa)) |
| 1771 | pblk_map_invalidate(pblk, l2p_ppa); |
| 1772 | |
| 1773 | pblk_trans_map_set(pblk, lba, ppa); |
| 1774 | spin_unlock(&pblk->trans_lock); |
| 1775 | } |
| 1776 | |
| 1777 | void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa) |
| 1778 | { |
| 1779 | #ifdef CONFIG_NVM_DEBUG |
| 1780 | /* Callers must ensure that the ppa points to a cache address */ |
| 1781 | BUG_ON(!pblk_addr_in_cache(ppa)); |
| 1782 | BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa))); |
| 1783 | #endif |
| 1784 | |
| 1785 | pblk_update_map(pblk, lba, ppa); |
| 1786 | } |
| 1787 | |
| 1788 | int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa, |
| 1789 | struct pblk_line *gc_line) |
| 1790 | { |
| 1791 | struct ppa_addr l2p_ppa; |
| 1792 | int ret = 1; |
| 1793 | |
| 1794 | #ifdef CONFIG_NVM_DEBUG |
| 1795 | /* Callers must ensure that the ppa points to a cache address */ |
| 1796 | BUG_ON(!pblk_addr_in_cache(ppa)); |
| 1797 | BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa))); |
| 1798 | #endif |
| 1799 | |
| 1800 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1801 | if (!(lba < pblk->rl.nr_secs)) { |
| 1802 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | spin_lock(&pblk->trans_lock); |
| 1807 | l2p_ppa = pblk_trans_map_get(pblk, lba); |
| 1808 | |
| 1809 | /* Prevent updated entries to be overwritten by GC */ |
| 1810 | if (pblk_addr_in_cache(l2p_ppa) || pblk_ppa_empty(l2p_ppa) || |
| 1811 | pblk_tgt_ppa_to_line(l2p_ppa) != gc_line->id) { |
| 1812 | ret = 0; |
| 1813 | goto out; |
| 1814 | } |
| 1815 | |
| 1816 | pblk_trans_map_set(pblk, lba, ppa); |
| 1817 | out: |
| 1818 | spin_unlock(&pblk->trans_lock); |
| 1819 | return ret; |
| 1820 | } |
| 1821 | |
| 1822 | void pblk_update_map_dev(struct pblk *pblk, sector_t lba, struct ppa_addr ppa, |
| 1823 | struct ppa_addr entry_line) |
| 1824 | { |
| 1825 | struct ppa_addr l2p_line; |
| 1826 | |
| 1827 | #ifdef CONFIG_NVM_DEBUG |
| 1828 | /* Callers must ensure that the ppa points to a device address */ |
| 1829 | BUG_ON(pblk_addr_in_cache(ppa)); |
| 1830 | #endif |
| 1831 | /* Invalidate and discard padded entries */ |
| 1832 | if (lba == ADDR_EMPTY) { |
| 1833 | #ifdef CONFIG_NVM_DEBUG |
| 1834 | atomic_long_inc(&pblk->padded_wb); |
| 1835 | #endif |
| 1836 | pblk_map_invalidate(pblk, ppa); |
| 1837 | return; |
| 1838 | } |
| 1839 | |
| 1840 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1841 | if (!(lba < pblk->rl.nr_secs)) { |
| 1842 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1843 | return; |
| 1844 | } |
| 1845 | |
| 1846 | spin_lock(&pblk->trans_lock); |
| 1847 | l2p_line = pblk_trans_map_get(pblk, lba); |
| 1848 | |
| 1849 | /* Do not update L2P if the cacheline has been updated. In this case, |
| 1850 | * the mapped ppa must be invalidated |
| 1851 | */ |
| 1852 | if (l2p_line.ppa != entry_line.ppa) { |
| 1853 | if (!pblk_ppa_empty(ppa)) |
| 1854 | pblk_map_invalidate(pblk, ppa); |
| 1855 | goto out; |
| 1856 | } |
| 1857 | |
| 1858 | #ifdef CONFIG_NVM_DEBUG |
| 1859 | WARN_ON(!pblk_addr_in_cache(l2p_line) && !pblk_ppa_empty(l2p_line)); |
| 1860 | #endif |
| 1861 | |
| 1862 | pblk_trans_map_set(pblk, lba, ppa); |
| 1863 | out: |
| 1864 | spin_unlock(&pblk->trans_lock); |
| 1865 | } |
| 1866 | |
| 1867 | void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas, |
| 1868 | sector_t blba, int nr_secs) |
| 1869 | { |
| 1870 | int i; |
| 1871 | |
| 1872 | spin_lock(&pblk->trans_lock); |
| 1873 | for (i = 0; i < nr_secs; i++) |
| 1874 | ppas[i] = pblk_trans_map_get(pblk, blba + i); |
| 1875 | spin_unlock(&pblk->trans_lock); |
| 1876 | } |
| 1877 | |
| 1878 | void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas, |
| 1879 | u64 *lba_list, int nr_secs) |
| 1880 | { |
| 1881 | sector_t lba; |
| 1882 | int i; |
| 1883 | |
| 1884 | spin_lock(&pblk->trans_lock); |
| 1885 | for (i = 0; i < nr_secs; i++) { |
| 1886 | lba = lba_list[i]; |
| 1887 | if (lba == ADDR_EMPTY) { |
| 1888 | ppas[i].ppa = ADDR_EMPTY; |
| 1889 | } else { |
| 1890 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1891 | if (!(lba < pblk->rl.nr_secs)) { |
| 1892 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1893 | continue; |
| 1894 | } |
| 1895 | ppas[i] = pblk_trans_map_get(pblk, lba); |
| 1896 | } |
| 1897 | } |
| 1898 | spin_unlock(&pblk->trans_lock); |
| 1899 | } |