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