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