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