blob: 6944aac43b015de6625ca08d0b50a8514f13768d [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * pblk-core.c - pblk's core functionality
17 *
18 */
19
Hans Holmberg4c44abf2018-10-09 13:11:52 +020020#define CREATE_TRACE_POINTS
21
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020022#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020023#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020024
Javier González8bd40022017-10-13 14:46:44 +020025static void pblk_line_mark_bb(struct work_struct *work)
26{
27 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
28 ws);
29 struct pblk *pblk = line_ws->pblk;
30 struct nvm_tgt_dev *dev = pblk->dev;
31 struct ppa_addr *ppa = line_ws->priv;
32 int ret;
33
Matias Bjørlingaff3fb12018-10-09 13:11:36 +020034 ret = nvm_set_chunk_meta(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
Javier González8bd40022017-10-13 14:46:44 +020035 if (ret) {
36 struct pblk_line *line;
37 int pos;
38
Javier Gonzálezcb216652018-10-09 13:11:42 +020039 line = pblk_ppa_to_line(pblk, *ppa);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010040 pos = pblk_ppa_to_pos(&dev->geo, *ppa);
Javier González8bd40022017-10-13 14:46:44 +020041
Matias Bjørling4e495a42018-07-13 10:48:42 +020042 pblk_err(pblk, "failed to mark bb, line:%d, pos:%d\n",
Javier González8bd40022017-10-13 14:46:44 +020043 line->id, pos);
44 }
45
46 kfree(ppa);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040047 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier González8bd40022017-10-13 14:46:44 +020048}
49
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020050static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +020051 struct ppa_addr ppa_addr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020052{
53 struct nvm_tgt_dev *dev = pblk->dev;
54 struct nvm_geo *geo = &dev->geo;
Javier González32ef9412018-03-30 00:05:20 +020055 struct ppa_addr *ppa;
56 int pos = pblk_ppa_to_pos(geo, ppa_addr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057
Matias Bjørling4e495a42018-07-13 10:48:42 +020058 pblk_debug(pblk, "erase failed: line:%d, pos:%d\n", line->id, pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059 atomic_long_inc(&pblk->erase_failed);
60
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020061 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020062 if (test_and_set_bit(pos, line->blk_bitmap))
Matias Bjørling4e495a42018-07-13 10:48:42 +020063 pblk_err(pblk, "attempted to erase bb: line:%d, pos:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020064 line->id, pos);
65
Javier González32ef9412018-03-30 00:05:20 +020066 /* Not necessary to mark bad blocks on 2.0 spec. */
67 if (geo->version == NVM_OCSSD_SPEC_20)
68 return;
69
70 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
71 if (!ppa)
72 return;
73
74 *ppa = ppa_addr;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020075 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
76 GFP_ATOMIC, pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020077}
78
79static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
80{
Javier González32ef9412018-03-30 00:05:20 +020081 struct nvm_tgt_dev *dev = pblk->dev;
82 struct nvm_geo *geo = &dev->geo;
83 struct nvm_chk_meta *chunk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020084 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +020085 int pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020086
Javier Gonzálezcb216652018-10-09 13:11:42 +020087 line = pblk_ppa_to_line(pblk, rqd->ppa_addr);
Javier González32ef9412018-03-30 00:05:20 +020088 pos = pblk_ppa_to_pos(geo, rqd->ppa_addr);
89 chunk = &line->chks[pos];
90
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 atomic_dec(&line->left_seblks);
92
93 if (rqd->error) {
Hans Holmberg4209c312018-10-09 13:11:55 +020094 trace_pblk_chunk_reset(pblk_disk_name(pblk),
95 &rqd->ppa_addr, PBLK_CHUNK_RESET_FAILED);
96
Javier González32ef9412018-03-30 00:05:20 +020097 chunk->state = NVM_CHK_ST_OFFLINE;
98 pblk_mark_bb(pblk, line, rqd->ppa_addr);
99 } else {
Hans Holmberg4209c312018-10-09 13:11:55 +0200100 trace_pblk_chunk_reset(pblk_disk_name(pblk),
101 &rqd->ppa_addr, PBLK_CHUNK_RESET_DONE);
102
Javier González32ef9412018-03-30 00:05:20 +0200103 chunk->state = NVM_CHK_ST_FREE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104 }
Javier González588726d32017-06-26 11:57:29 +0200105
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200106 trace_pblk_chunk_state(pblk_disk_name(pblk), &rqd->ppa_addr,
107 chunk->state);
108
Javier González588726d32017-06-26 11:57:29 +0200109 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200110}
111
112/* Erase completion assumes that only one block is erased at the time */
113static void pblk_end_io_erase(struct nvm_rq *rqd)
114{
115 struct pblk *pblk = rqd->private;
116
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200117 __pblk_end_io_erase(pblk, rqd);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400118 mempool_free(rqd, &pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200119}
120
Javier González32ef9412018-03-30 00:05:20 +0200121/*
122 * Get information for all chunks from the device.
123 *
Javier González090ee262018-10-09 13:12:01 +0200124 * The caller is responsible for freeing (vmalloc) the returned structure
Javier González32ef9412018-03-30 00:05:20 +0200125 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200126struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk)
Javier González32ef9412018-03-30 00:05:20 +0200127{
128 struct nvm_tgt_dev *dev = pblk->dev;
129 struct nvm_geo *geo = &dev->geo;
130 struct nvm_chk_meta *meta;
131 struct ppa_addr ppa;
132 unsigned long len;
133 int ret;
134
135 ppa.ppa = 0;
136
137 len = geo->all_chunks * sizeof(*meta);
Javier González090ee262018-10-09 13:12:01 +0200138 meta = vzalloc(len);
Javier González32ef9412018-03-30 00:05:20 +0200139 if (!meta)
140 return ERR_PTR(-ENOMEM);
141
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200142 ret = nvm_get_chunk_meta(dev, ppa, geo->all_chunks, meta);
Javier González32ef9412018-03-30 00:05:20 +0200143 if (ret) {
144 kfree(meta);
145 return ERR_PTR(-EIO);
146 }
147
148 return meta;
149}
150
151struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
152 struct nvm_chk_meta *meta,
153 struct ppa_addr ppa)
154{
155 struct nvm_tgt_dev *dev = pblk->dev;
156 struct nvm_geo *geo = &dev->geo;
157 int ch_off = ppa.m.grp * geo->num_chk * geo->num_lun;
158 int lun_off = ppa.m.pu * geo->num_chk;
159 int chk_off = ppa.m.chk;
160
161 return meta + ch_off + lun_off + chk_off;
162}
163
Javier González0880a9a2017-06-26 11:57:19 +0200164void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
165 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166{
167 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
168 struct list_head *move_list = NULL;
169
170 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
171 * table is modified with reclaimed sectors, a check is done to endure
172 * that newer updates are not overwritten.
173 */
174 spin_lock(&line->lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200175 WARN_ON(line->state == PBLK_LINESTATE_FREE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200176
177 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
178 WARN_ONCE(1, "pblk: double invalidate\n");
179 spin_unlock(&line->lock);
180 return;
181 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200182 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183
184 if (line->state == PBLK_LINESTATE_CLOSED)
185 move_list = pblk_line_gc_list(pblk, line);
186 spin_unlock(&line->lock);
187
188 if (move_list) {
189 spin_lock(&l_mg->gc_lock);
190 spin_lock(&line->lock);
191 /* Prevent moving a line that has just been chosen for GC */
Javier Gonzálezd3401212017-10-13 14:46:14 +0200192 if (line->state == PBLK_LINESTATE_GC) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200193 spin_unlock(&line->lock);
194 spin_unlock(&l_mg->gc_lock);
195 return;
196 }
197 spin_unlock(&line->lock);
198
199 list_move_tail(&line->list, move_list);
200 spin_unlock(&l_mg->gc_lock);
201 }
202}
203
204void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
205{
206 struct pblk_line *line;
207 u64 paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208
Matias Bjørling880eda52018-07-13 10:48:37 +0200209#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200210 /* Callers must ensure that the ppa points to a device address */
211 BUG_ON(pblk_addr_in_cache(ppa));
212 BUG_ON(pblk_ppa_empty(ppa));
213#endif
214
Javier Gonzálezcb216652018-10-09 13:11:42 +0200215 line = pblk_ppa_to_line(pblk, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
217
218 __pblk_map_invalidate(pblk, line, paddr);
219}
220
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
222 unsigned int nr_secs)
223{
224 sector_t lba;
225
226 spin_lock(&pblk->trans_lock);
227 for (lba = slba; lba < slba + nr_secs; lba++) {
228 struct ppa_addr ppa;
229
230 ppa = pblk_trans_map_get(pblk, lba);
231
232 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
233 pblk_map_invalidate(pblk, ppa);
234
235 pblk_ppa_set_empty(&ppa);
236 pblk_trans_map_set(pblk, lba, ppa);
237 }
238 spin_unlock(&pblk->trans_lock);
239}
240
Javier González45dcf292018-10-09 13:12:02 +0200241int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
242{
243 struct nvm_tgt_dev *dev = pblk->dev;
244
245 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
246 &rqd->dma_meta_list);
247 if (!rqd->meta_list)
248 return -ENOMEM;
249
250 if (rqd->nr_ppas == 1)
251 return 0;
252
253 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
254 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
255
256 return 0;
257}
258
259void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
260{
261 struct nvm_tgt_dev *dev = pblk->dev;
262
263 if (rqd->meta_list)
264 nvm_dev_dma_free(dev->parent, rqd->meta_list,
265 rqd->dma_meta_list);
266}
267
Javier González67bf26a2017-10-13 14:46:20 +0200268/* Caller must guarantee that the request is a valid type */
269struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200270{
271 mempool_t *pool;
272 struct nvm_rq *rqd;
273 int rq_size;
274
Javier González67bf26a2017-10-13 14:46:20 +0200275 switch (type) {
276 case PBLK_WRITE:
277 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400278 pool = &pblk->w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279 rq_size = pblk_w_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200280 break;
281 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400282 pool = &pblk->r_rq_pool;
Javier González084ec9b2017-06-26 16:27:13 -0600283 rq_size = pblk_g_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200284 break;
285 default:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400286 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200287 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288 }
289
290 rqd = mempool_alloc(pool, GFP_KERNEL);
291 memset(rqd, 0, rq_size);
292
293 return rqd;
294}
295
Javier González67bf26a2017-10-13 14:46:20 +0200296/* Typically used on completion path. Cannot guarantee request consistency */
297void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298{
299 mempool_t *pool;
300
Javier González67bf26a2017-10-13 14:46:20 +0200301 switch (type) {
302 case PBLK_WRITE:
303 kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
Gustavo A. R. Silva884b0312018-07-13 10:48:43 +0200304 /* fall through */
Javier González67bf26a2017-10-13 14:46:20 +0200305 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400306 pool = &pblk->w_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200307 break;
308 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400309 pool = &pblk->r_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200310 break;
311 case PBLK_ERASE:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400312 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200313 break;
314 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200315 pblk_err(pblk, "trying to free unknown rqd type\n");
Javier González67bf26a2017-10-13 14:46:20 +0200316 return;
317 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200318
Javier González45dcf292018-10-09 13:12:02 +0200319 pblk_free_rqd_meta(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320 mempool_free(rqd, pool);
321}
322
323void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
324 int nr_pages)
325{
326 struct bio_vec bv;
327 int i;
328
329 WARN_ON(off + nr_pages != bio->bi_vcnt);
330
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200331 for (i = off; i < nr_pages + off; i++) {
332 bv = bio->bi_io_vec[i];
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400333 mempool_free(bv.bv_page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200334 }
335}
336
337int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
338 int nr_pages)
339{
340 struct request_queue *q = pblk->dev->q;
341 struct page *page;
342 int i, ret;
343
344 for (i = 0; i < nr_pages; i++) {
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400345 page = mempool_alloc(&pblk->page_bio_pool, flags);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200346
347 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
348 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200349 pblk_err(pblk, "could not add page to bio\n");
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400350 mempool_free(page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200351 goto err;
352 }
353 }
354
355 return 0;
356err:
Igor Konopkof142ac02018-06-01 16:41:08 +0200357 pblk_bio_free_pages(pblk, bio, (bio->bi_vcnt - i), i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358 return -1;
359}
360
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200361void pblk_write_kick(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362{
363 wake_up_process(pblk->writer_ts);
364 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
365}
366
Kees Cook87c1d2d2017-10-17 21:10:19 -0700367void pblk_write_timer_fn(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200368{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700369 struct pblk *pblk = from_timer(pblk, t, wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370
371 /* kick the write thread every tick to flush outstanding data */
372 pblk_write_kick(pblk);
373}
374
375void pblk_write_should_kick(struct pblk *pblk)
376{
377 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
378
379 if (secs_avail >= pblk->min_write_pgs)
380 pblk_write_kick(pblk);
381}
382
Javier González8bd40022017-10-13 14:46:44 +0200383static void pblk_wait_for_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384{
Javier González588726d32017-06-26 11:57:29 +0200385 do {
386 if (!atomic_read(&pblk->inflight_io))
387 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200388
Javier González588726d32017-06-26 11:57:29 +0200389 schedule();
390 } while (1);
391}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200392
Javier González588726d32017-06-26 11:57:29 +0200393static void pblk_flush_writer(struct pblk *pblk)
394{
395 pblk_rb_flush(&pblk->rwb);
396 do {
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200397 if (!pblk_rb_sync_count(&pblk->rwb))
Javier González588726d32017-06-26 11:57:29 +0200398 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200399
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200400 pblk_write_kick(pblk);
Javier González588726d32017-06-26 11:57:29 +0200401 schedule();
402 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403}
404
405struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
406{
407 struct pblk_line_meta *lm = &pblk->lm;
408 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
409 struct list_head *move_list = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200410 int vsc = le32_to_cpu(*line->vsc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200411
Javier González476118c2017-06-26 11:57:26 +0200412 lockdep_assert_held(&line->lock);
413
Hans Holmberg48b8d202018-06-01 16:41:06 +0200414 if (line->w_err_gc->has_write_err) {
415 if (line->gc_group != PBLK_LINEGC_WERR) {
416 line->gc_group = PBLK_LINEGC_WERR;
417 move_list = &l_mg->gc_werr_list;
418 pblk_rl_werr_line_in(&pblk->rl);
419 }
420 } else if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200421 if (line->gc_group != PBLK_LINEGC_FULL) {
422 line->gc_group = PBLK_LINEGC_FULL;
423 move_list = &l_mg->gc_full_list;
424 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200425 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426 if (line->gc_group != PBLK_LINEGC_HIGH) {
427 line->gc_group = PBLK_LINEGC_HIGH;
428 move_list = &l_mg->gc_high_list;
429 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200430 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 if (line->gc_group != PBLK_LINEGC_MID) {
432 line->gc_group = PBLK_LINEGC_MID;
433 move_list = &l_mg->gc_mid_list;
434 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200435 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436 if (line->gc_group != PBLK_LINEGC_LOW) {
437 line->gc_group = PBLK_LINEGC_LOW;
438 move_list = &l_mg->gc_low_list;
439 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200440 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200441 if (line->gc_group != PBLK_LINEGC_EMPTY) {
442 line->gc_group = PBLK_LINEGC_EMPTY;
443 move_list = &l_mg->gc_empty_list;
444 }
445 } else {
446 line->state = PBLK_LINESTATE_CORRUPT;
Hans Holmbergf2937232018-10-09 13:11:53 +0200447 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
448 line->state);
449
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200450 line->gc_group = PBLK_LINEGC_NONE;
451 move_list = &l_mg->corrupt_list;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200452 pblk_err(pblk, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200453 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 line->sec_in_line,
455 lm->high_thrs, lm->mid_thrs);
456 }
457
458 return move_list;
459}
460
461void pblk_discard(struct pblk *pblk, struct bio *bio)
462{
463 sector_t slba = pblk_get_lba(bio);
464 sector_t nr_secs = pblk_get_secs(bio);
465
466 pblk_invalidate_range(pblk, slba, nr_secs);
467}
468
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200469void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
470{
471 atomic_long_inc(&pblk->write_failed);
Matias Bjørling880eda52018-07-13 10:48:37 +0200472#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200473 pblk_print_failed_rqd(pblk, rqd, rqd->error);
474#endif
475}
476
477void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
478{
479 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
480 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
481 atomic_long_inc(&pblk->read_empty);
482 return;
483 }
484
485 switch (rqd->error) {
486 case NVM_RSP_WARN_HIGHECC:
487 atomic_long_inc(&pblk->read_high_ecc);
488 break;
489 case NVM_RSP_ERR_FAILECC:
490 case NVM_RSP_ERR_FAILCRC:
491 atomic_long_inc(&pblk->read_failed);
492 break;
493 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200494 pblk_err(pblk, "unknown read error:%d\n", rqd->error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495 }
Matias Bjørling880eda52018-07-13 10:48:37 +0200496#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497 pblk_print_failed_rqd(pblk, rqd, rqd->error);
498#endif
499}
500
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200501void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
502{
503 pblk->sec_per_write = sec_per_write;
504}
505
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
507{
508 struct nvm_tgt_dev *dev = pblk->dev;
509
Javier González588726d32017-06-26 11:57:29 +0200510 atomic_inc(&pblk->inflight_io);
511
Matias Bjørling880eda52018-07-13 10:48:37 +0200512#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200513 if (pblk_check_io(pblk, rqd))
514 return NVM_IO_ERR;
515#endif
516
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200517 return nvm_submit_io(dev, rqd);
518}
519
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200520void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd)
521{
522 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
523
524 int i;
525
526 for (i = 0; i < rqd->nr_ppas; i++) {
527 struct ppa_addr *ppa = &ppa_list[i];
528 struct nvm_chk_meta *chunk = pblk_dev_ppa_to_chunk(pblk, *ppa);
529 u64 caddr = pblk_dev_ppa_to_chunk_addr(pblk, *ppa);
530
531 if (caddr == 0)
532 trace_pblk_chunk_state(pblk_disk_name(pblk),
533 ppa, NVM_CHK_ST_OPEN);
534 else if (caddr == chunk->cnlb)
535 trace_pblk_chunk_state(pblk_disk_name(pblk),
536 ppa, NVM_CHK_ST_CLOSED);
537 }
538}
539
Javier González1a94b2d2017-10-13 14:46:47 +0200540int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
541{
542 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200543 int ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200544
Javier González1a94b2d2017-10-13 14:46:47 +0200545 atomic_inc(&pblk->inflight_io);
546
Matias Bjørling880eda52018-07-13 10:48:37 +0200547#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200548 if (pblk_check_io(pblk, rqd))
549 return NVM_IO_ERR;
550#endif
551
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200552 ret = nvm_submit_io_sync(dev, rqd);
553
554 if (trace_pblk_chunk_state_enabled() && !ret &&
555 rqd->opcode == NVM_OP_PWRITE)
556 pblk_check_chunk_state_update(pblk, rqd);
557
558 return ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200559}
560
Javier González253babc2018-10-09 13:12:04 +0200561int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd)
562{
563 struct ppa_addr *ppa_list;
564 int ret;
565
566 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
567
568 pblk_down_chunk(pblk, ppa_list[0]);
569 ret = pblk_submit_io_sync(pblk, rqd);
570 pblk_up_chunk(pblk, ppa_list[0]);
571
572 return ret;
573}
574
Javier González55e836d2017-10-13 14:46:16 +0200575static void pblk_bio_map_addr_endio(struct bio *bio)
576{
577 bio_put(bio);
578}
579
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200580struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
581 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200582 int alloc_type, gfp_t gfp_mask)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200583{
584 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200585 void *kaddr = data;
586 struct page *page;
587 struct bio *bio;
588 int i, ret;
589
Javier Gonzálezde54e702017-06-30 17:56:39 +0200590 if (alloc_type == PBLK_KMALLOC_META)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
592
593 bio = bio_kmalloc(gfp_mask, nr_secs);
594 if (!bio)
595 return ERR_PTR(-ENOMEM);
596
597 for (i = 0; i < nr_secs; i++) {
598 page = vmalloc_to_page(kaddr);
599 if (!page) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200600 pblk_err(pblk, "could not map vmalloc bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601 bio_put(bio);
602 bio = ERR_PTR(-ENOMEM);
603 goto out;
604 }
605
606 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
607 if (ret != PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200608 pblk_err(pblk, "could not add page to bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609 bio_put(bio);
610 bio = ERR_PTR(-ENOMEM);
611 goto out;
612 }
613
614 kaddr += PAGE_SIZE;
615 }
Javier González55e836d2017-10-13 14:46:16 +0200616
617 bio->bi_end_io = pblk_bio_map_addr_endio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200618out:
619 return bio;
620}
621
622int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
623 unsigned long secs_to_flush)
624{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200625 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626 int min = pblk->min_write_pgs;
627 int secs_to_sync = 0;
628
629 if (secs_avail >= max)
630 secs_to_sync = max;
631 else if (secs_avail >= min)
632 secs_to_sync = min * (secs_avail / min);
633 else if (secs_to_flush)
634 secs_to_sync = min;
635
636 return secs_to_sync;
637}
638
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200639void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
640{
641 u64 addr;
642 int i;
643
Rakesh Pandite57903f2017-10-13 14:45:56 +0200644 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200645 addr = find_next_zero_bit(line->map_bitmap,
646 pblk->lm.sec_per_line, line->cur_sec);
647 line->cur_sec = addr - nr_secs;
648
649 for (i = 0; i < nr_secs; i++, line->cur_sec--)
650 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
Rakesh Pandite57903f2017-10-13 14:45:56 +0200651 spin_unlock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200652}
653
654u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655{
656 u64 addr;
657 int i;
658
Javier González476118c2017-06-26 11:57:26 +0200659 lockdep_assert_held(&line->lock);
660
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200661 /* logic error: ppa out-of-bounds. Prevent generating bad address */
662 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
663 WARN(1, "pblk: page allocation out of bounds\n");
664 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
665 }
666
667 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
668 pblk->lm.sec_per_line, line->cur_sec);
669 for (i = 0; i < nr_secs; i++, line->cur_sec++)
670 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
671
672 return addr;
673}
674
675u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
676{
677 u64 addr;
678
679 /* Lock needed in case a write fails and a recovery needs to remap
680 * failed write buffer entries
681 */
682 spin_lock(&line->lock);
683 addr = __pblk_alloc_page(pblk, line, nr_secs);
684 line->left_msecs -= nr_secs;
685 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
686 spin_unlock(&line->lock);
687
688 return addr;
689}
690
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200691u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
692{
693 u64 paddr;
694
695 spin_lock(&line->lock);
696 paddr = find_next_zero_bit(line->map_bitmap,
697 pblk->lm.sec_per_line, line->cur_sec);
698 spin_unlock(&line->lock);
699
700 return paddr;
701}
702
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200703u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
704{
705 struct nvm_tgt_dev *dev = pblk->dev;
706 struct nvm_geo *geo = &dev->geo;
707 struct pblk_line_meta *lm = &pblk->lm;
708 int bit;
709
710 /* This usually only happens on bad lines */
711 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
712 if (bit >= lm->blk_per_line)
713 return -1;
714
715 return bit * geo->ws_opt;
716}
717
718int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line)
719{
720 struct nvm_tgt_dev *dev = pblk->dev;
721 struct pblk_line_meta *lm = &pblk->lm;
722 struct bio *bio;
723 struct nvm_rq rqd;
724 u64 paddr = pblk_line_smeta_start(pblk, line);
725 int i, ret;
726
727 memset(&rqd, 0, sizeof(struct nvm_rq));
728
729 ret = pblk_alloc_rqd_meta(pblk, &rqd);
730 if (ret)
731 return ret;
732
733 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
734 if (IS_ERR(bio)) {
735 ret = PTR_ERR(bio);
736 goto clear_rqd;
737 }
738
739 bio->bi_iter.bi_sector = 0; /* internal bio */
740 bio_set_op_attrs(bio, REQ_OP_READ, 0);
741
742 rqd.bio = bio;
743 rqd.opcode = NVM_OP_PREAD;
744 rqd.nr_ppas = lm->smeta_sec;
745 rqd.is_seq = 1;
746
747 for (i = 0; i < lm->smeta_sec; i++, paddr++)
748 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
749
750 ret = pblk_submit_io_sync(pblk, &rqd);
751 if (ret) {
752 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
753 bio_put(bio);
754 goto clear_rqd;
755 }
756
757 atomic_dec(&pblk->inflight_io);
758
759 if (rqd.error)
760 pblk_log_read_err(pblk, &rqd);
761
762clear_rqd:
763 pblk_free_rqd_meta(pblk, &rqd);
764 return ret;
765}
766
767static int pblk_line_smeta_write(struct pblk *pblk, struct pblk_line *line,
768 u64 paddr)
769{
770 struct nvm_tgt_dev *dev = pblk->dev;
771 struct pblk_line_meta *lm = &pblk->lm;
772 struct bio *bio;
773 struct nvm_rq rqd;
774 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
775 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
776 int i, ret;
777
778 memset(&rqd, 0, sizeof(struct nvm_rq));
779
780 ret = pblk_alloc_rqd_meta(pblk, &rqd);
781 if (ret)
782 return ret;
783
784 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
785 if (IS_ERR(bio)) {
786 ret = PTR_ERR(bio);
787 goto clear_rqd;
788 }
789
790 bio->bi_iter.bi_sector = 0; /* internal bio */
791 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
792
793 rqd.bio = bio;
794 rqd.opcode = NVM_OP_PWRITE;
795 rqd.nr_ppas = lm->smeta_sec;
796 rqd.is_seq = 1;
797
798 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
799 struct pblk_sec_meta *meta_list = rqd.meta_list;
800
801 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
802 meta_list[i].lba = lba_list[paddr] = addr_empty;
803 }
804
Javier González253babc2018-10-09 13:12:04 +0200805 ret = pblk_submit_io_sync_sem(pblk, &rqd);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200806 if (ret) {
807 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
808 bio_put(bio);
809 goto clear_rqd;
810 }
811
812 atomic_dec(&pblk->inflight_io);
813
814 if (rqd.error) {
815 pblk_log_write_err(pblk, &rqd);
816 ret = -EIO;
817 }
818
819clear_rqd:
820 pblk_free_rqd_meta(pblk, &rqd);
821 return ret;
822}
823
824int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
825 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200826{
827 struct nvm_tgt_dev *dev = pblk->dev;
828 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezde54e702017-06-30 17:56:39 +0200829 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830 struct pblk_line_meta *lm = &pblk->lm;
Javier González63e38092017-06-26 11:57:24 +0200831 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200832 struct bio *bio;
833 struct nvm_rq rqd;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200834 u64 paddr = line->emeta_ssec;
Javier González63e38092017-06-26 11:57:24 +0200835 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200837 int left_ppas = lm->emeta_sec[0];
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200838 int line_id = line->id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200839 int rq_ppas, rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200840 int i, j;
841 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842
Javier González63e38092017-06-26 11:57:24 +0200843 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
844 &dma_meta_list);
845 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200846 return -ENOMEM;
847
Javier González63e38092017-06-26 11:57:24 +0200848 ppa_list = meta_list + pblk_dma_meta_size;
849 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
850
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200851next_rq:
852 memset(&rqd, 0, sizeof(struct nvm_rq));
853
854 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200855 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200856
Javier Gonzálezde54e702017-06-30 17:56:39 +0200857 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
858 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200859 if (IS_ERR(bio)) {
860 ret = PTR_ERR(bio);
861 goto free_rqd_dma;
862 }
863
864 bio->bi_iter.bi_sector = 0; /* internal bio */
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200865 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866
867 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200868 rqd.meta_list = meta_list;
869 rqd.ppa_list = ppa_list;
870 rqd.dma_meta_list = dma_meta_list;
871 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200872 rqd.opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200873 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200874
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200875 for (i = 0; i < rqd.nr_ppas; ) {
876 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, line_id);
877 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González63e38092017-06-26 11:57:24 +0200878
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200879 if (pblk_io_aligned(pblk, rq_ppas))
880 rqd.is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200881
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200882 while (test_bit(pos, line->blk_bitmap)) {
883 paddr += min;
884 if (pblk_boundary_paddr_checks(pblk, paddr)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200885 bio_put(bio);
886 ret = -EINTR;
887 goto free_rqd_dma;
888 }
889
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200890 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
891 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892 }
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200893
894 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
895 bio_put(bio);
896 ret = -EINTR;
897 goto free_rqd_dma;
898 }
899
900 for (j = 0; j < min; j++, i++, paddr++)
901 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line_id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902 }
903
Javier González1a94b2d2017-10-13 14:46:47 +0200904 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200906 pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200907 bio_put(bio);
908 goto free_rqd_dma;
909 }
910
Javier González588726d32017-06-26 11:57:29 +0200911 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200912
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200913 if (rqd.error)
914 pblk_log_read_err(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200915
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200916 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200917 left_ppas -= rq_ppas;
918 if (left_ppas)
919 goto next_rq;
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200920
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200922 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200923 return ret;
924}
925
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200926static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
927 struct ppa_addr ppa)
928{
929 rqd->opcode = NVM_OP_ERASE;
930 rqd->ppa_addr = ppa;
931 rqd->nr_ppas = 1;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200932 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200933 rqd->bio = NULL;
934}
935
936static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
937{
Matias Bjørling4b5d56e2018-10-09 13:11:33 +0200938 struct nvm_rq rqd = {NULL};
939 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200940
Hans Holmberg4209c312018-10-09 13:11:55 +0200941 trace_pblk_chunk_reset(pblk_disk_name(pblk), &ppa,
942 PBLK_CHUNK_RESET_START);
943
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200944 pblk_setup_e_rq(pblk, &rqd, ppa);
945
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200946 /* The write thread schedules erases so that it minimizes disturbances
947 * with writes. Thus, there is no need to take the LUN semaphore.
948 */
Javier González1a94b2d2017-10-13 14:46:47 +0200949 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200950 rqd.private = pblk;
951 __pblk_end_io_erase(pblk, &rqd);
952
Javier González588726d32017-06-26 11:57:29 +0200953 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954}
955
956int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
957{
958 struct pblk_line_meta *lm = &pblk->lm;
959 struct ppa_addr ppa;
Javier González588726d32017-06-26 11:57:29 +0200960 int ret, bit = -1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200962 /* Erase only good blocks, one at a time */
963 do {
964 spin_lock(&line->lock);
965 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
966 bit + 1);
967 if (bit >= lm->blk_per_line) {
968 spin_unlock(&line->lock);
969 break;
970 }
971
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200972 ppa = pblk->luns[bit].bppa; /* set ch and lun */
Javier González69471512018-03-30 00:05:15 +0200973 ppa.a.blk = line->id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200974
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200975 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200977 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200978
Javier González588726d32017-06-26 11:57:29 +0200979 ret = pblk_blk_erase_sync(pblk, ppa);
980 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200981 pblk_err(pblk, "failed to erase line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +0200982 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200983 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200984 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985
986 return 0;
987}
988
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200989static void pblk_line_setup_metadata(struct pblk_line *line,
990 struct pblk_line_mgmt *l_mg,
991 struct pblk_line_meta *lm)
992{
993 int meta_line;
994
Javier González588726d32017-06-26 11:57:29 +0200995 lockdep_assert_held(&l_mg->free_lock);
996
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200997retry_meta:
998 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
999 if (meta_line == PBLK_DATA_LINES) {
1000 spin_unlock(&l_mg->free_lock);
1001 io_schedule();
1002 spin_lock(&l_mg->free_lock);
1003 goto retry_meta;
1004 }
1005
1006 set_bit(meta_line, &l_mg->meta_bitmap);
1007 line->meta_line = meta_line;
1008
1009 line->smeta = l_mg->sline_meta[meta_line];
1010 line->emeta = l_mg->eline_meta[meta_line];
1011
1012 memset(line->smeta, 0, lm->smeta_len);
1013 memset(line->emeta->buf, 0, lm->emeta_len[0]);
1014
1015 line->emeta->mem = 0;
1016 atomic_set(&line->emeta->sync, 0);
1017}
1018
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001019/* For now lines are always assumed full lines. Thus, smeta former and current
1020 * lun bitmaps are omitted.
1021 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001022static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001023 struct pblk_line *cur)
1024{
1025 struct nvm_tgt_dev *dev = pblk->dev;
1026 struct nvm_geo *geo = &dev->geo;
1027 struct pblk_line_meta *lm = &pblk->lm;
1028 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001029 struct pblk_emeta *emeta = line->emeta;
1030 struct line_emeta *emeta_buf = emeta->buf;
1031 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001032 int nr_blk_line;
1033
1034 /* After erasing the line, new bad blocks might appear and we risk
1035 * having an invalid line
1036 */
1037 nr_blk_line = lm->blk_per_line -
1038 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
1039 if (nr_blk_line < lm->min_blk_line) {
1040 spin_lock(&l_mg->free_lock);
1041 spin_lock(&line->lock);
1042 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001043 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1044 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001045 spin_unlock(&line->lock);
1046
1047 list_add_tail(&line->list, &l_mg->bad_list);
1048 spin_unlock(&l_mg->free_lock);
1049
Matias Bjørling4e495a42018-07-13 10:48:42 +02001050 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001051
1052 return 0;
1053 }
1054
1055 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001056 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001057
1058 /* Mark LUNs allocated in this line (all for now) */
1059 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
1060
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001061 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1062 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
1063 smeta_buf->header.id = cpu_to_le32(line->id);
1064 smeta_buf->header.type = cpu_to_le16(line->type);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001065 smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
1066 smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001067
1068 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001069 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001070 smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001071
1072 /* Fill metadata among lines */
1073 if (cur) {
1074 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001075 smeta_buf->prev_id = cpu_to_le32(cur->id);
1076 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001078 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001079 }
1080
1081 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001082 smeta_buf->header.crc = cpu_to_le32(
1083 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
1084 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001085
1086 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001087 memcpy(&emeta_buf->header, &smeta_buf->header,
1088 sizeof(struct line_header));
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001089
1090 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1091 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1092 emeta_buf->header.crc = cpu_to_le32(
1093 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1094
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001095 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1096 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1097 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1098 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1099 emeta_buf->crc = cpu_to_le32(0);
1100 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001101
1102 return 1;
1103}
1104
Javier González9cfd5a92018-06-01 16:41:14 +02001105static int pblk_line_alloc_bitmaps(struct pblk *pblk, struct pblk_line *line)
1106{
1107 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg53d82db2018-10-09 13:11:47 +02001108 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González9cfd5a92018-06-01 16:41:14 +02001109
Hans Holmberg53d82db2018-10-09 13:11:47 +02001110 line->map_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001111 if (!line->map_bitmap)
1112 return -ENOMEM;
1113
Hans Holmberg53d82db2018-10-09 13:11:47 +02001114 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1115
Javier González9cfd5a92018-06-01 16:41:14 +02001116 /* will be initialized using bb info from map_bitmap */
Hans Holmberg53d82db2018-10-09 13:11:47 +02001117 line->invalid_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001118 if (!line->invalid_bitmap) {
Hans Holmberg53d82db2018-10-09 13:11:47 +02001119 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001120 line->map_bitmap = NULL;
1121 return -ENOMEM;
1122 }
1123
1124 return 0;
1125}
1126
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001127/* For now lines are always assumed full lines. Thus, smeta former and current
1128 * lun bitmaps are omitted.
1129 */
1130static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1131 int init)
1132{
1133 struct nvm_tgt_dev *dev = pblk->dev;
1134 struct nvm_geo *geo = &dev->geo;
1135 struct pblk_line_meta *lm = &pblk->lm;
1136 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137 u64 off;
1138 int bit = -1;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001139 int emeta_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001140
1141 line->sec_in_line = lm->sec_per_line;
1142
1143 /* Capture bad block information on line mapping bitmaps */
1144 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1145 bit + 1)) < lm->blk_per_line) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001146 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001147 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1148 lm->sec_per_line);
1149 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1150 lm->sec_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001151 line->sec_in_line -= geo->clba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001152 }
1153
1154 /* Mark smeta metadata sectors as bad sectors */
1155 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001156 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001157 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1158 line->sec_in_line -= lm->smeta_sec;
1159 line->smeta_ssec = off;
1160 line->cur_sec = off + lm->smeta_sec;
1161
Javier Gonzálezaf3fac12018-10-09 13:12:03 +02001162 if (init && pblk_line_smeta_write(pblk, line, off)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001163 pblk_debug(pblk, "line smeta I/O failed. Retry\n");
Hans Holmberg6cf17a22018-06-01 16:41:07 +02001164 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001165 }
1166
1167 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1168
1169 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1170 * blocks to make sure that there are enough sectors to store emeta
1171 */
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001172 emeta_secs = lm->emeta_sec[0];
1173 off = lm->sec_per_line;
1174 while (emeta_secs) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001175 off -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001176 if (!test_bit(off, line->invalid_bitmap)) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001177 bitmap_set(line->invalid_bitmap, off, geo->ws_opt);
1178 emeta_secs -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001179 }
1180 }
1181
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182 line->emeta_ssec = off;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001183 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001184 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001185 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001186 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001187
1188 if (lm->sec_per_line - line->sec_in_line !=
1189 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1190 spin_lock(&line->lock);
1191 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001192 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1193 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001194 spin_unlock(&line->lock);
1195
1196 list_add_tail(&line->list, &l_mg->bad_list);
Matias Bjørling4e495a42018-07-13 10:48:42 +02001197 pblk_err(pblk, "unexpected line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001198
1199 return 0;
1200 }
1201
1202 return 1;
1203}
1204
Javier González32ef9412018-03-30 00:05:20 +02001205static int pblk_prepare_new_line(struct pblk *pblk, struct pblk_line *line)
1206{
1207 struct pblk_line_meta *lm = &pblk->lm;
1208 struct nvm_tgt_dev *dev = pblk->dev;
1209 struct nvm_geo *geo = &dev->geo;
1210 int blk_to_erase = atomic_read(&line->blk_in_line);
1211 int i;
1212
1213 for (i = 0; i < lm->blk_per_line; i++) {
1214 struct pblk_lun *rlun = &pblk->luns[i];
1215 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1216 int state = line->chks[pos].state;
1217
1218 /* Free chunks should not be erased */
1219 if (state & NVM_CHK_ST_FREE) {
1220 set_bit(pblk_ppa_to_pos(geo, rlun->bppa),
1221 line->erase_bitmap);
1222 blk_to_erase--;
1223 }
1224 }
1225
1226 return blk_to_erase;
1227}
1228
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001229static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1230{
1231 struct pblk_line_meta *lm = &pblk->lm;
Javier González1d8b33e2018-06-01 15:04:16 +02001232 int blk_in_line = atomic_read(&line->blk_in_line);
Javier González9cfd5a92018-06-01 16:41:14 +02001233 int blk_to_erase;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234
Javier González32ef9412018-03-30 00:05:20 +02001235 /* Bad blocks do not need to be erased */
1236 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
1237
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001238 spin_lock(&line->lock);
Javier González32ef9412018-03-30 00:05:20 +02001239
1240 /* If we have not written to this line, we need to mark up free chunks
1241 * as already erased
1242 */
1243 if (line->state == PBLK_LINESTATE_NEW) {
1244 blk_to_erase = pblk_prepare_new_line(pblk, line);
1245 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001246 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1247 line->state);
Javier González32ef9412018-03-30 00:05:20 +02001248 } else {
Javier González1d8b33e2018-06-01 15:04:16 +02001249 blk_to_erase = blk_in_line;
1250 }
1251
1252 if (blk_in_line < lm->min_blk_line) {
Javier González9cfd5a92018-06-01 16:41:14 +02001253 spin_unlock(&line->lock);
1254 return -EAGAIN;
Javier González32ef9412018-03-30 00:05:20 +02001255 }
1256
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001257 if (line->state != PBLK_LINESTATE_FREE) {
Javier González588726d32017-06-26 11:57:29 +02001258 WARN(1, "pblk: corrupted line %d, state %d\n",
1259 line->id, line->state);
Javier González9cfd5a92018-06-01 16:41:14 +02001260 spin_unlock(&line->lock);
1261 return -EINTR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001262 }
Javier González588726d32017-06-26 11:57:29 +02001263
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001264 line->state = PBLK_LINESTATE_OPEN;
Hans Holmbergf2937232018-10-09 13:11:53 +02001265 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1266 line->state);
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001267
Javier González32ef9412018-03-30 00:05:20 +02001268 atomic_set(&line->left_eblks, blk_to_erase);
1269 atomic_set(&line->left_seblks, blk_to_erase);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001270
1271 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001272 spin_unlock(&line->lock);
1273
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001274 kref_init(&line->ref);
1275
1276 return 0;
1277}
1278
1279int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1280{
1281 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1282 int ret;
1283
1284 spin_lock(&l_mg->free_lock);
1285 l_mg->data_line = line;
1286 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287
1288 ret = pblk_line_prepare(pblk, line);
1289 if (ret) {
1290 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001291 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001292 return ret;
1293 }
Javier González3dc001f2017-04-22 01:32:45 +02001294 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001295
Javier González9cfd5a92018-06-01 16:41:14 +02001296 ret = pblk_line_alloc_bitmaps(pblk, line);
1297 if (ret)
1298 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001299
1300 if (!pblk_line_init_bb(pblk, line, 0)) {
1301 list_add(&line->list, &l_mg->free_list);
1302 return -EINTR;
1303 }
1304
Javier González9cfd5a92018-06-01 16:41:14 +02001305 pblk_rl_free_lines_dec(&pblk->rl, line, true);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001306 return 0;
1307}
1308
1309void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1310{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001311 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1312
1313 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001314 line->map_bitmap = NULL;
1315 line->smeta = NULL;
1316 line->emeta = NULL;
1317}
1318
Javier González9cfd5a92018-06-01 16:41:14 +02001319static void pblk_line_reinit(struct pblk_line *line)
1320{
1321 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1322
1323 line->map_bitmap = NULL;
1324 line->invalid_bitmap = NULL;
1325 line->smeta = NULL;
1326 line->emeta = NULL;
1327}
1328
1329void pblk_line_free(struct pblk_line *line)
1330{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001331 struct pblk *pblk = line->pblk;
1332 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1333
1334 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1335 mempool_free(line->invalid_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001336
1337 pblk_line_reinit(line);
1338}
1339
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001340struct pblk_line *pblk_line_get(struct pblk *pblk)
1341{
1342 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1343 struct pblk_line_meta *lm = &pblk->lm;
Javier González588726d32017-06-26 11:57:29 +02001344 struct pblk_line *line;
1345 int ret, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001346
1347 lockdep_assert_held(&l_mg->free_lock);
1348
Javier González588726d32017-06-26 11:57:29 +02001349retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001350 if (list_empty(&l_mg->free_list)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001351 pblk_err(pblk, "no free lines\n");
Javier González588726d32017-06-26 11:57:29 +02001352 return NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001353 }
1354
1355 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1356 list_del(&line->list);
1357 l_mg->nr_free_lines--;
1358
1359 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1360 if (unlikely(bit >= lm->blk_per_line)) {
1361 spin_lock(&line->lock);
1362 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001363 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1364 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001365 spin_unlock(&line->lock);
1366
1367 list_add_tail(&line->list, &l_mg->bad_list);
1368
Matias Bjørling4e495a42018-07-13 10:48:42 +02001369 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001370 goto retry;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001371 }
1372
Javier González588726d32017-06-26 11:57:29 +02001373 ret = pblk_line_prepare(pblk, line);
1374 if (ret) {
Javier González1d8b33e2018-06-01 15:04:16 +02001375 switch (ret) {
1376 case -EAGAIN:
1377 list_add(&line->list, &l_mg->bad_list);
1378 goto retry;
1379 case -EINTR:
Javier González588726d32017-06-26 11:57:29 +02001380 list_add(&line->list, &l_mg->corrupt_list);
1381 goto retry;
Javier González1d8b33e2018-06-01 15:04:16 +02001382 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +02001383 pblk_err(pblk, "failed to prepare line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001384 list_add(&line->list, &l_mg->free_list);
1385 l_mg->nr_free_lines++;
1386 return NULL;
1387 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001388 }
1389
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001390 return line;
1391}
1392
1393static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1394 struct pblk_line *line)
1395{
1396 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1397 struct pblk_line *retry_line;
1398
Javier González588726d32017-06-26 11:57:29 +02001399retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001400 spin_lock(&l_mg->free_lock);
1401 retry_line = pblk_line_get(pblk);
1402 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001403 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001404 spin_unlock(&l_mg->free_lock);
1405 return NULL;
1406 }
1407
Javier González9cfd5a92018-06-01 16:41:14 +02001408 retry_line->map_bitmap = line->map_bitmap;
1409 retry_line->invalid_bitmap = line->invalid_bitmap;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001410 retry_line->smeta = line->smeta;
1411 retry_line->emeta = line->emeta;
1412 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001413
Javier González9cfd5a92018-06-01 16:41:14 +02001414 pblk_line_reinit(line);
1415
Javier González3dc001f2017-04-22 01:32:45 +02001416 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001417 spin_unlock(&l_mg->free_lock);
1418
Javier Gonzáleza7689932018-01-05 14:16:13 +01001419 pblk_rl_free_lines_dec(&pblk->rl, line, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001420
Javier González588726d32017-06-26 11:57:29 +02001421 if (pblk_line_erase(pblk, retry_line))
1422 goto retry;
1423
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001424 return retry_line;
1425}
1426
Javier González588726d32017-06-26 11:57:29 +02001427static void pblk_set_space_limit(struct pblk *pblk)
1428{
1429 struct pblk_rl *rl = &pblk->rl;
1430
1431 atomic_set(&rl->rb_space, 0);
1432}
1433
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001434struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1435{
1436 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1437 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001438
1439 spin_lock(&l_mg->free_lock);
1440 line = pblk_line_get(pblk);
1441 if (!line) {
1442 spin_unlock(&l_mg->free_lock);
1443 return NULL;
1444 }
1445
1446 line->seq_nr = l_mg->d_seq_nr++;
1447 line->type = PBLK_LINETYPE_DATA;
1448 l_mg->data_line = line;
1449
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001450 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001451
1452 /* Allocate next line for preparation */
1453 l_mg->data_next = pblk_line_get(pblk);
Javier González588726d32017-06-26 11:57:29 +02001454 if (!l_mg->data_next) {
1455 /* If we cannot get a new line, we need to stop the pipeline.
1456 * Only allow as many writes in as we can store safely and then
1457 * fail gracefully
1458 */
1459 pblk_set_space_limit(pblk);
1460
1461 l_mg->data_next = NULL;
1462 } else {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001463 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1464 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001465 }
1466 spin_unlock(&l_mg->free_lock);
1467
Javier González9cfd5a92018-06-01 16:41:14 +02001468 if (pblk_line_alloc_bitmaps(pblk, line))
1469 return NULL;
1470
Javier González588726d32017-06-26 11:57:29 +02001471 if (pblk_line_erase(pblk, line)) {
1472 line = pblk_line_retry(pblk, line);
1473 if (!line)
1474 return NULL;
1475 }
1476
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001477retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001478 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001479 line = pblk_line_retry(pblk, line);
1480 if (!line)
1481 return NULL;
1482
1483 goto retry_setup;
1484 }
1485
1486 if (!pblk_line_init_bb(pblk, line, 1)) {
1487 line = pblk_line_retry(pblk, line);
1488 if (!line)
1489 return NULL;
1490
1491 goto retry_setup;
1492 }
1493
Javier Gonzáleza7689932018-01-05 14:16:13 +01001494 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1495
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001496 return line;
1497}
1498
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001499void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa)
1500{
1501 struct pblk_line *line;
1502
Javier Gonzálezcb216652018-10-09 13:11:42 +02001503 line = pblk_ppa_to_line(pblk, ppa);
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001504 kref_put(&line->ref, pblk_line_put_wq);
1505}
1506
1507void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd)
1508{
1509 struct ppa_addr *ppa_list;
1510 int i;
1511
1512 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1513
1514 for (i = 0; i < rqd->nr_ppas; i++)
1515 pblk_ppa_to_line_put(pblk, ppa_list[i]);
1516}
1517
Javier González588726d32017-06-26 11:57:29 +02001518static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1519{
1520 lockdep_assert_held(&pblk->l_mg.free_lock);
1521
1522 pblk_set_space_limit(pblk);
1523 pblk->state = PBLK_STATE_STOPPING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001524 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001525}
1526
Javier González8bd40022017-10-13 14:46:44 +02001527static void pblk_line_close_meta_sync(struct pblk *pblk)
1528{
1529 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1530 struct pblk_line_meta *lm = &pblk->lm;
1531 struct pblk_line *line, *tline;
1532 LIST_HEAD(list);
1533
1534 spin_lock(&l_mg->close_lock);
1535 if (list_empty(&l_mg->emeta_list)) {
1536 spin_unlock(&l_mg->close_lock);
1537 return;
1538 }
1539
1540 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1541 spin_unlock(&l_mg->close_lock);
1542
1543 list_for_each_entry_safe(line, tline, &list, list) {
1544 struct pblk_emeta *emeta = line->emeta;
1545
1546 while (emeta->mem < lm->emeta_len[0]) {
1547 int ret;
1548
1549 ret = pblk_submit_meta_io(pblk, line);
1550 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001551 pblk_err(pblk, "sync meta line %d failed (%d)\n",
Javier González8bd40022017-10-13 14:46:44 +02001552 line->id, ret);
1553 return;
1554 }
1555 }
1556 }
1557
1558 pblk_wait_for_meta(pblk);
1559 flush_workqueue(pblk->close_wq);
1560}
1561
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001562void __pblk_pipeline_flush(struct pblk *pblk)
Javier González588726d32017-06-26 11:57:29 +02001563{
1564 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1565 int ret;
1566
1567 spin_lock(&l_mg->free_lock);
1568 if (pblk->state == PBLK_STATE_RECOVERING ||
1569 pblk->state == PBLK_STATE_STOPPED) {
1570 spin_unlock(&l_mg->free_lock);
1571 return;
1572 }
1573 pblk->state = PBLK_STATE_RECOVERING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001574 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001575 spin_unlock(&l_mg->free_lock);
1576
1577 pblk_flush_writer(pblk);
1578 pblk_wait_for_meta(pblk);
1579
1580 ret = pblk_recov_pad(pblk);
1581 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001582 pblk_err(pblk, "could not close data on teardown(%d)\n", ret);
Javier González588726d32017-06-26 11:57:29 +02001583 return;
1584 }
1585
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001586 flush_workqueue(pblk->bb_wq);
Javier González588726d32017-06-26 11:57:29 +02001587 pblk_line_close_meta_sync(pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001588}
1589
1590void __pblk_pipeline_stop(struct pblk *pblk)
1591{
1592 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +02001593
1594 spin_lock(&l_mg->free_lock);
1595 pblk->state = PBLK_STATE_STOPPED;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001596 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001597 l_mg->data_line = NULL;
1598 l_mg->data_next = NULL;
1599 spin_unlock(&l_mg->free_lock);
1600}
1601
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001602void pblk_pipeline_stop(struct pblk *pblk)
1603{
1604 __pblk_pipeline_flush(pblk);
1605 __pblk_pipeline_stop(pblk);
1606}
1607
Javier González21d22872017-10-13 14:46:26 +02001608struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001609{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001610 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González21d22872017-10-13 14:46:26 +02001611 struct pblk_line *cur, *new = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001612 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001613
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001614 new = l_mg->data_next;
1615 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001616 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001617
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001618 spin_lock(&l_mg->free_lock);
Javier González44cdbdc2018-10-09 13:12:07 +02001619 cur = l_mg->data_line;
1620 l_mg->data_line = new;
1621
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001622 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001623 spin_unlock(&l_mg->free_lock);
1624
Javier González588726d32017-06-26 11:57:29 +02001625retry_erase:
1626 left_seblks = atomic_read(&new->left_seblks);
1627 if (left_seblks) {
1628 /* If line is not fully erased, erase it */
1629 if (atomic_read(&new->left_eblks)) {
1630 if (pblk_line_erase(pblk, new))
Javier González21d22872017-10-13 14:46:26 +02001631 goto out;
Javier González588726d32017-06-26 11:57:29 +02001632 } else {
1633 io_schedule();
1634 }
1635 goto retry_erase;
1636 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001637
Javier González9cfd5a92018-06-01 16:41:14 +02001638 if (pblk_line_alloc_bitmaps(pblk, new))
1639 return NULL;
1640
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001641retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001642 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001643 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001644 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001645 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001646
1647 goto retry_setup;
1648 }
1649
1650 if (!pblk_line_init_bb(pblk, new, 1)) {
1651 new = pblk_line_retry(pblk, new);
1652 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001653 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001654
1655 goto retry_setup;
1656 }
1657
Javier Gonzáleza7689932018-01-05 14:16:13 +01001658 pblk_rl_free_lines_dec(&pblk->rl, new, true);
1659
Javier González588726d32017-06-26 11:57:29 +02001660 /* Allocate next line for preparation */
1661 spin_lock(&l_mg->free_lock);
1662 l_mg->data_next = pblk_line_get(pblk);
1663 if (!l_mg->data_next) {
1664 /* If we cannot get a new line, we need to stop the pipeline.
1665 * Only allow as many writes in as we can store safely and then
1666 * fail gracefully
1667 */
1668 pblk_stop_writes(pblk, new);
1669 l_mg->data_next = NULL;
1670 } else {
1671 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1672 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier González588726d32017-06-26 11:57:29 +02001673 }
1674 spin_unlock(&l_mg->free_lock);
1675
Javier González21d22872017-10-13 14:46:26 +02001676out:
1677 return new;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001678}
1679
Javier González7bd4d372017-10-13 14:46:23 +02001680static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001681{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001682 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001683 struct pblk_gc *gc = &pblk->gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001684
1685 spin_lock(&line->lock);
1686 WARN_ON(line->state != PBLK_LINESTATE_GC);
1687 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001688 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1689 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001690 line->gc_group = PBLK_LINEGC_NONE;
Javier González8e55c072018-06-01 15:04:22 +02001691 pblk_line_free(line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001692
Hans Holmberg48b8d202018-06-01 16:41:06 +02001693 if (line->w_err_gc->has_write_err) {
1694 pblk_rl_werr_line_out(&pblk->rl);
1695 line->w_err_gc->has_write_err = 0;
1696 }
1697
1698 spin_unlock(&line->lock);
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001699 atomic_dec(&gc->pipeline_gc);
1700
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001701 spin_lock(&l_mg->free_lock);
1702 list_add_tail(&line->list, &l_mg->free_list);
1703 l_mg->nr_free_lines++;
1704 spin_unlock(&l_mg->free_lock);
1705
1706 pblk_rl_free_lines_inc(&pblk->rl, line);
1707}
1708
Javier González7bd4d372017-10-13 14:46:23 +02001709static void pblk_line_put_ws(struct work_struct *work)
1710{
1711 struct pblk_line_ws *line_put_ws = container_of(work,
1712 struct pblk_line_ws, ws);
1713 struct pblk *pblk = line_put_ws->pblk;
1714 struct pblk_line *line = line_put_ws->line;
1715
1716 __pblk_line_put(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001717 mempool_free(line_put_ws, &pblk->gen_ws_pool);
Javier González7bd4d372017-10-13 14:46:23 +02001718}
1719
1720void pblk_line_put(struct kref *ref)
1721{
1722 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1723 struct pblk *pblk = line->pblk;
1724
1725 __pblk_line_put(pblk, line);
1726}
1727
1728void pblk_line_put_wq(struct kref *ref)
1729{
1730 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1731 struct pblk *pblk = line->pblk;
1732 struct pblk_line_ws *line_put_ws;
1733
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001734 line_put_ws = mempool_alloc(&pblk->gen_ws_pool, GFP_ATOMIC);
Javier González7bd4d372017-10-13 14:46:23 +02001735 if (!line_put_ws)
1736 return;
1737
1738 line_put_ws->pblk = pblk;
1739 line_put_ws->line = line;
1740 line_put_ws->priv = NULL;
1741
1742 INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
1743 queue_work(pblk->r_end_wq, &line_put_ws->ws);
1744}
1745
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001746int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1747{
1748 struct nvm_rq *rqd;
1749 int err;
1750
Javier González67bf26a2017-10-13 14:46:20 +02001751 rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001752
1753 pblk_setup_e_rq(pblk, rqd, ppa);
1754
1755 rqd->end_io = pblk_end_io_erase;
1756 rqd->private = pblk;
1757
Hans Holmberg4209c312018-10-09 13:11:55 +02001758 trace_pblk_chunk_reset(pblk_disk_name(pblk),
1759 &ppa, PBLK_CHUNK_RESET_START);
1760
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001761 /* The write thread schedules erases so that it minimizes disturbances
1762 * with writes. Thus, there is no need to take the LUN semaphore.
1763 */
1764 err = pblk_submit_io(pblk, rqd);
1765 if (err) {
1766 struct nvm_tgt_dev *dev = pblk->dev;
1767 struct nvm_geo *geo = &dev->geo;
1768
Matias Bjørling4e495a42018-07-13 10:48:42 +02001769 pblk_err(pblk, "could not async erase line:%d,blk:%d\n",
Javier Gonzálezcb216652018-10-09 13:11:42 +02001770 pblk_ppa_to_line_id(ppa),
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001771 pblk_ppa_to_pos(geo, ppa));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001772 }
1773
1774 return err;
1775}
1776
1777struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1778{
1779 return pblk->l_mg.data_line;
1780}
1781
Javier Gonzálezd624f372017-06-26 11:57:15 +02001782/* For now, always erase next line */
1783struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001784{
1785 return pblk->l_mg.data_next;
1786}
1787
1788int pblk_line_is_full(struct pblk_line *line)
1789{
1790 return (line->left_msecs == 0);
1791}
1792
Javier González588726d32017-06-26 11:57:29 +02001793static void pblk_line_should_sync_meta(struct pblk *pblk)
1794{
1795 if (pblk_rl_is_limit(&pblk->rl))
1796 pblk_line_close_meta_sync(pblk);
1797}
1798
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001799void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1800{
Javier González32ef9412018-03-30 00:05:20 +02001801 struct nvm_tgt_dev *dev = pblk->dev;
1802 struct nvm_geo *geo = &dev->geo;
1803 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001804 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1805 struct list_head *move_list;
Javier González32ef9412018-03-30 00:05:20 +02001806 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001807
Matias Bjørling880eda52018-07-13 10:48:37 +02001808#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001809 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001810 "pblk: corrupt closed line %d\n", line->id);
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001811#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001812
1813 spin_lock(&l_mg->free_lock);
1814 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1815 spin_unlock(&l_mg->free_lock);
1816
1817 spin_lock(&l_mg->gc_lock);
1818 spin_lock(&line->lock);
1819 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1820 line->state = PBLK_LINESTATE_CLOSED;
1821 move_list = pblk_line_gc_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001822 list_add_tail(&line->list, move_list);
1823
Hans Holmberg53d82db2018-10-09 13:11:47 +02001824 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001825 line->map_bitmap = NULL;
1826 line->smeta = NULL;
1827 line->emeta = NULL;
1828
Javier González32ef9412018-03-30 00:05:20 +02001829 for (i = 0; i < lm->blk_per_line; i++) {
1830 struct pblk_lun *rlun = &pblk->luns[i];
1831 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1832 int state = line->chks[pos].state;
1833
1834 if (!(state & NVM_CHK_ST_OFFLINE))
1835 state = NVM_CHK_ST_CLOSED;
1836 }
1837
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001838 spin_unlock(&line->lock);
1839 spin_unlock(&l_mg->gc_lock);
Hans Holmbergf2937232018-10-09 13:11:53 +02001840
1841 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1842 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001843}
1844
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001845void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1846{
1847 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1848 struct pblk_line_meta *lm = &pblk->lm;
1849 struct pblk_emeta *emeta = line->emeta;
1850 struct line_emeta *emeta_buf = emeta->buf;
Hans Holmberg76758392018-03-30 00:04:52 +02001851 struct wa_counters *wa = emeta_to_wa(lm, emeta_buf);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001852
Javier González588726d32017-06-26 11:57:29 +02001853 /* No need for exact vsc value; avoid a big line lock and take aprox. */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001854 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1855 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1856
Hans Holmberg76758392018-03-30 00:04:52 +02001857 wa->user = cpu_to_le64(atomic64_read(&pblk->user_wa));
1858 wa->pad = cpu_to_le64(atomic64_read(&pblk->pad_wa));
1859 wa->gc = cpu_to_le64(atomic64_read(&pblk->gc_wa));
1860
Javier González9cc85bc2018-10-09 13:11:45 +02001861 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) {
1862 emeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1863 memcpy(emeta_buf->header.uuid, pblk->instance_uuid, 16);
1864 emeta_buf->header.id = cpu_to_le32(line->id);
1865 emeta_buf->header.type = cpu_to_le16(line->type);
1866 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1867 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1868 emeta_buf->header.crc = cpu_to_le32(
1869 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1870 }
1871
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001872 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1873 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1874
1875 spin_lock(&l_mg->close_lock);
1876 spin_lock(&line->lock);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001877
1878 /* Update the in-memory start address for emeta, in case it has
1879 * shifted due to write errors
1880 */
1881 if (line->emeta_ssec != line->cur_sec)
1882 line->emeta_ssec = line->cur_sec;
1883
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001884 list_add_tail(&line->list, &l_mg->emeta_list);
1885 spin_unlock(&line->lock);
1886 spin_unlock(&l_mg->close_lock);
Javier González588726d32017-06-26 11:57:29 +02001887
1888 pblk_line_should_sync_meta(pblk);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001889}
1890
1891static void pblk_save_lba_list(struct pblk *pblk, struct pblk_line *line)
1892{
1893 struct pblk_line_meta *lm = &pblk->lm;
1894 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1895 unsigned int lba_list_size = lm->emeta_len[2];
1896 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1897 struct pblk_emeta *emeta = line->emeta;
1898
1899 w_err_gc->lba_list = pblk_malloc(lba_list_size,
1900 l_mg->emeta_alloc_type, GFP_KERNEL);
1901 memcpy(w_err_gc->lba_list, emeta_to_lbas(pblk, emeta->buf),
1902 lba_list_size);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001903}
1904
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001905void pblk_line_close_ws(struct work_struct *work)
1906{
1907 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1908 ws);
1909 struct pblk *pblk = line_ws->pblk;
1910 struct pblk_line *line = line_ws->line;
Hans Holmberg48b8d202018-06-01 16:41:06 +02001911 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1912
1913 /* Write errors makes the emeta start address stored in smeta invalid,
1914 * so keep a copy of the lba list until we've gc'd the line
1915 */
1916 if (w_err_gc->has_write_err)
1917 pblk_save_lba_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001918
1919 pblk_line_close(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001920 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001921}
1922
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001923void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1924 void (*work)(struct work_struct *), gfp_t gfp_mask,
Javier Gonzálezef576492017-06-26 11:57:28 +02001925 struct workqueue_struct *wq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001926{
1927 struct pblk_line_ws *line_ws;
1928
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001929 line_ws = mempool_alloc(&pblk->gen_ws_pool, gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001930
1931 line_ws->pblk = pblk;
1932 line_ws->line = line;
1933 line_ws->priv = priv;
1934
1935 INIT_WORK(&line_ws->ws, work);
Javier Gonzálezef576492017-06-26 11:57:28 +02001936 queue_work(wq, &line_ws->ws);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001937}
1938
Matias Bjørling43241cf2018-10-09 13:11:51 +02001939static void __pblk_down_chunk(struct pblk *pblk, int pos)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001940{
Javier González3eaa11e2017-07-07 21:08:52 +02001941 struct pblk_lun *rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001942 int ret;
1943
1944 /*
1945 * Only send one inflight I/O per LUN. Since we map at a page
1946 * granurality, all ppas in the I/O will map to the same LUN
1947 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001948
Javier González3eaa11e2017-07-07 21:08:52 +02001949 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
Rakesh Panditc5493842017-10-13 14:45:58 +02001950 if (ret == -ETIME || ret == -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001951 pblk_err(pblk, "taking lun semaphore timed out: err %d\n",
1952 -ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001953}
1954
Matias Bjørling43241cf2018-10-09 13:11:51 +02001955void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001956{
1957 struct nvm_tgt_dev *dev = pblk->dev;
1958 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001959 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001960
Matias Bjørling43241cf2018-10-09 13:11:51 +02001961 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001962}
1963
Matias Bjørling43241cf2018-10-09 13:11:51 +02001964void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier González3eaa11e2017-07-07 21:08:52 +02001965 unsigned long *lun_bitmap)
1966{
1967 struct nvm_tgt_dev *dev = pblk->dev;
1968 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001969 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001970
1971 /* If the LUN has been locked for this same request, do no attempt to
1972 * lock it again
1973 */
1974 if (test_and_set_bit(pos, lun_bitmap))
1975 return;
1976
Matias Bjørling43241cf2018-10-09 13:11:51 +02001977 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001978}
1979
Matias Bjørling43241cf2018-10-09 13:11:51 +02001980void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001981{
1982 struct nvm_tgt_dev *dev = pblk->dev;
1983 struct nvm_geo *geo = &dev->geo;
1984 struct pblk_lun *rlun;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001985 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001986
1987 rlun = &pblk->luns[pos];
1988 up(&rlun->wr_sem);
1989}
1990
Hans Holmberge99e8022018-10-09 13:11:48 +02001991void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001992{
1993 struct nvm_tgt_dev *dev = pblk->dev;
1994 struct nvm_geo *geo = &dev->geo;
1995 struct pblk_lun *rlun;
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001996 int num_lun = geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001997 int bit = -1;
1998
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001999 while ((bit = find_next_bit(lun_bitmap, num_lun, bit + 1)) < num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002000 rlun = &pblk->luns[bit];
2001 up(&rlun->wr_sem);
2002 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002003}
2004
2005void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2006{
Javier González9f6cb132017-10-13 14:46:12 +02002007 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002008
2009 /* logic error: lba out-of-bounds. Ignore update */
2010 if (!(lba < pblk->rl.nr_secs)) {
2011 WARN(1, "pblk: corrupted L2P map request\n");
2012 return;
2013 }
2014
2015 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002016 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002017
Javier González9f6cb132017-10-13 14:46:12 +02002018 if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
2019 pblk_map_invalidate(pblk, ppa_l2p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002020
2021 pblk_trans_map_set(pblk, lba, ppa);
2022 spin_unlock(&pblk->trans_lock);
2023}
2024
2025void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2026{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002027
Matias Bjørling880eda52018-07-13 10:48:37 +02002028#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002029 /* Callers must ensure that the ppa points to a cache address */
2030 BUG_ON(!pblk_addr_in_cache(ppa));
2031 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
2032#endif
2033
2034 pblk_update_map(pblk, lba, ppa);
2035}
2036
Javier González9f6cb132017-10-13 14:46:12 +02002037int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
Javier Gonzálezd3401212017-10-13 14:46:14 +02002038 struct pblk_line *gc_line, u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002039{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002040 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002041 int ret = 1;
2042
Matias Bjørling880eda52018-07-13 10:48:37 +02002043#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002044 /* Callers must ensure that the ppa points to a cache address */
Javier González9f6cb132017-10-13 14:46:12 +02002045 BUG_ON(!pblk_addr_in_cache(ppa_new));
2046 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002047#endif
2048
2049 /* logic error: lba out-of-bounds. Ignore update */
2050 if (!(lba < pblk->rl.nr_secs)) {
2051 WARN(1, "pblk: corrupted L2P map request\n");
2052 return 0;
2053 }
2054
2055 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002056 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzálezd3401212017-10-13 14:46:14 +02002057 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002058
Javier Gonzálezd3401212017-10-13 14:46:14 +02002059 if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
2060 spin_lock(&gc_line->lock);
2061 WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
2062 "pblk: corrupted GC update");
2063 spin_unlock(&gc_line->lock);
Javier González9f6cb132017-10-13 14:46:12 +02002064
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002065 ret = 0;
2066 goto out;
2067 }
2068
Javier González9f6cb132017-10-13 14:46:12 +02002069 pblk_trans_map_set(pblk, lba, ppa_new);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002070out:
2071 spin_unlock(&pblk->trans_lock);
2072 return ret;
2073}
2074
Javier González9f6cb132017-10-13 14:46:12 +02002075void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
2076 struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002077{
Javier González9f6cb132017-10-13 14:46:12 +02002078 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002079
Matias Bjørling880eda52018-07-13 10:48:37 +02002080#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002081 /* Callers must ensure that the ppa points to a device address */
Javier González9f6cb132017-10-13 14:46:12 +02002082 BUG_ON(pblk_addr_in_cache(ppa_mapped));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002083#endif
2084 /* Invalidate and discard padded entries */
2085 if (lba == ADDR_EMPTY) {
Hans Holmberg76758392018-03-30 00:04:52 +02002086 atomic64_inc(&pblk->pad_wa);
Matias Bjørling880eda52018-07-13 10:48:37 +02002087#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002088 atomic_long_inc(&pblk->padded_wb);
2089#endif
Javier González9f6cb132017-10-13 14:46:12 +02002090 if (!pblk_ppa_empty(ppa_mapped))
2091 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002092 return;
2093 }
2094
2095 /* logic error: lba out-of-bounds. Ignore update */
2096 if (!(lba < pblk->rl.nr_secs)) {
2097 WARN(1, "pblk: corrupted L2P map request\n");
2098 return;
2099 }
2100
2101 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002102 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002103
2104 /* Do not update L2P if the cacheline has been updated. In this case,
2105 * the mapped ppa must be invalidated
2106 */
Javier González9f6cb132017-10-13 14:46:12 +02002107 if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
2108 if (!pblk_ppa_empty(ppa_mapped))
2109 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002110 goto out;
2111 }
2112
Matias Bjørling880eda52018-07-13 10:48:37 +02002113#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González9f6cb132017-10-13 14:46:12 +02002114 WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002115#endif
2116
Javier González9f6cb132017-10-13 14:46:12 +02002117 pblk_trans_map_set(pblk, lba, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002118out:
2119 spin_unlock(&pblk->trans_lock);
2120}
2121
2122void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
2123 sector_t blba, int nr_secs)
2124{
2125 int i;
2126
2127 spin_lock(&pblk->trans_lock);
Javier González7bd4d372017-10-13 14:46:23 +02002128 for (i = 0; i < nr_secs; i++) {
2129 struct ppa_addr ppa;
2130
2131 ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
2132
2133 /* If the L2P entry maps to a line, the reference is valid */
2134 if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02002135 struct pblk_line *line = pblk_ppa_to_line(pblk, ppa);
Javier González7bd4d372017-10-13 14:46:23 +02002136
2137 kref_get(&line->ref);
2138 }
2139 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002140 spin_unlock(&pblk->trans_lock);
2141}
2142
2143void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
2144 u64 *lba_list, int nr_secs)
2145{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002146 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002147 int i;
2148
2149 spin_lock(&pblk->trans_lock);
2150 for (i = 0; i < nr_secs; i++) {
2151 lba = lba_list[i];
Javier Gonzálezd3401212017-10-13 14:46:14 +02002152 if (lba != ADDR_EMPTY) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002153 /* logic error: lba out-of-bounds. Ignore update */
2154 if (!(lba < pblk->rl.nr_secs)) {
2155 WARN(1, "pblk: corrupted L2P map request\n");
2156 continue;
2157 }
2158 ppas[i] = pblk_trans_map_get(pblk, lba);
2159 }
2160 }
2161 spin_unlock(&pblk->trans_lock);
2162}