1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
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.
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.
16 * pblk-core.c - pblk's core functionality
20 #define CREATE_TRACE_POINTS
23 #include "pblk-trace.h"
25 static void pblk_line_mark_bb(struct work_struct
*work
)
27 struct pblk_line_ws
*line_ws
= container_of(work
, struct pblk_line_ws
,
29 struct pblk
*pblk
= line_ws
->pblk
;
30 struct nvm_tgt_dev
*dev
= pblk
->dev
;
31 struct ppa_addr
*ppa
= line_ws
->priv
;
34 ret
= nvm_set_chunk_meta(dev
, ppa
, 1, NVM_BLK_T_GRWN_BAD
);
36 struct pblk_line
*line
;
39 line
= pblk_ppa_to_line(pblk
, *ppa
);
40 pos
= pblk_ppa_to_pos(&dev
->geo
, *ppa
);
42 pblk_err(pblk
, "failed to mark bb, line:%d, pos:%d\n",
47 mempool_free(line_ws
, &pblk
->gen_ws_pool
);
50 static void pblk_mark_bb(struct pblk
*pblk
, struct pblk_line
*line
,
51 struct ppa_addr ppa_addr
)
53 struct nvm_tgt_dev
*dev
= pblk
->dev
;
54 struct nvm_geo
*geo
= &dev
->geo
;
56 int pos
= pblk_ppa_to_pos(geo
, ppa_addr
);
58 pblk_debug(pblk
, "erase failed: line:%d, pos:%d\n", line
->id
, pos
);
59 atomic_long_inc(&pblk
->erase_failed
);
61 atomic_dec(&line
->blk_in_line
);
62 if (test_and_set_bit(pos
, line
->blk_bitmap
))
63 pblk_err(pblk
, "attempted to erase bb: line:%d, pos:%d\n",
66 /* Not necessary to mark bad blocks on 2.0 spec. */
67 if (geo
->version
== NVM_OCSSD_SPEC_20
)
70 ppa
= kmalloc(sizeof(struct ppa_addr
), GFP_ATOMIC
);
75 pblk_gen_run_ws(pblk
, NULL
, ppa
, pblk_line_mark_bb
,
76 GFP_ATOMIC
, pblk
->bb_wq
);
79 static void __pblk_end_io_erase(struct pblk
*pblk
, struct nvm_rq
*rqd
)
81 struct nvm_tgt_dev
*dev
= pblk
->dev
;
82 struct nvm_geo
*geo
= &dev
->geo
;
83 struct nvm_chk_meta
*chunk
;
84 struct pblk_line
*line
;
87 line
= pblk_ppa_to_line(pblk
, rqd
->ppa_addr
);
88 pos
= pblk_ppa_to_pos(geo
, rqd
->ppa_addr
);
89 chunk
= &line
->chks
[pos
];
91 atomic_dec(&line
->left_seblks
);
94 trace_pblk_chunk_reset(pblk_disk_name(pblk
),
95 &rqd
->ppa_addr
, PBLK_CHUNK_RESET_FAILED
);
97 chunk
->state
= NVM_CHK_ST_OFFLINE
;
98 pblk_mark_bb(pblk
, line
, rqd
->ppa_addr
);
100 trace_pblk_chunk_reset(pblk_disk_name(pblk
),
101 &rqd
->ppa_addr
, PBLK_CHUNK_RESET_DONE
);
103 chunk
->state
= NVM_CHK_ST_FREE
;
106 trace_pblk_chunk_state(pblk_disk_name(pblk
), &rqd
->ppa_addr
,
109 atomic_dec(&pblk
->inflight_io
);
112 /* Erase completion assumes that only one block is erased at the time */
113 static void pblk_end_io_erase(struct nvm_rq
*rqd
)
115 struct pblk
*pblk
= rqd
->private;
117 __pblk_end_io_erase(pblk
, rqd
);
118 mempool_free(rqd
, &pblk
->e_rq_pool
);
122 * Get information for all chunks from the device.
124 * The caller is responsible for freeing (vmalloc) the returned structure
126 struct nvm_chk_meta
*pblk_get_chunk_meta(struct pblk
*pblk
)
128 struct nvm_tgt_dev
*dev
= pblk
->dev
;
129 struct nvm_geo
*geo
= &dev
->geo
;
130 struct nvm_chk_meta
*meta
;
137 len
= geo
->all_chunks
* sizeof(*meta
);
140 return ERR_PTR(-ENOMEM
);
142 ret
= nvm_get_chunk_meta(dev
, ppa
, geo
->all_chunks
, meta
);
145 return ERR_PTR(-EIO
);
151 struct nvm_chk_meta
*pblk_chunk_get_off(struct pblk
*pblk
,
152 struct nvm_chk_meta
*meta
,
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
;
161 return meta
+ ch_off
+ lun_off
+ chk_off
;
164 void __pblk_map_invalidate(struct pblk
*pblk
, struct pblk_line
*line
,
167 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
168 struct list_head
*move_list
= NULL
;
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.
174 spin_lock(&line
->lock
);
175 WARN_ON(line
->state
== PBLK_LINESTATE_FREE
);
177 if (test_and_set_bit(paddr
, line
->invalid_bitmap
)) {
178 WARN_ONCE(1, "pblk: double invalidate\n");
179 spin_unlock(&line
->lock
);
182 le32_add_cpu(line
->vsc
, -1);
184 if (line
->state
== PBLK_LINESTATE_CLOSED
)
185 move_list
= pblk_line_gc_list(pblk
, line
);
186 spin_unlock(&line
->lock
);
189 spin_lock(&l_mg
->gc_lock
);
190 spin_lock(&line
->lock
);
191 /* Prevent moving a line that has just been chosen for GC */
192 if (line
->state
== PBLK_LINESTATE_GC
) {
193 spin_unlock(&line
->lock
);
194 spin_unlock(&l_mg
->gc_lock
);
197 spin_unlock(&line
->lock
);
199 list_move_tail(&line
->list
, move_list
);
200 spin_unlock(&l_mg
->gc_lock
);
204 void pblk_map_invalidate(struct pblk
*pblk
, struct ppa_addr ppa
)
206 struct pblk_line
*line
;
209 #ifdef CONFIG_NVM_PBLK_DEBUG
210 /* 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
));
215 line
= pblk_ppa_to_line(pblk
, ppa
);
216 paddr
= pblk_dev_ppa_to_line_addr(pblk
, ppa
);
218 __pblk_map_invalidate(pblk
, line
, paddr
);
221 static void pblk_invalidate_range(struct pblk
*pblk
, sector_t slba
,
222 unsigned int nr_secs
)
226 spin_lock(&pblk
->trans_lock
);
227 for (lba
= slba
; lba
< slba
+ nr_secs
; lba
++) {
230 ppa
= pblk_trans_map_get(pblk
, lba
);
232 if (!pblk_addr_in_cache(ppa
) && !pblk_ppa_empty(ppa
))
233 pblk_map_invalidate(pblk
, ppa
);
235 pblk_ppa_set_empty(&ppa
);
236 pblk_trans_map_set(pblk
, lba
, ppa
);
238 spin_unlock(&pblk
->trans_lock
);
241 int pblk_alloc_rqd_meta(struct pblk
*pblk
, struct nvm_rq
*rqd
)
243 struct nvm_tgt_dev
*dev
= pblk
->dev
;
245 rqd
->meta_list
= nvm_dev_dma_alloc(dev
->parent
, GFP_KERNEL
,
246 &rqd
->dma_meta_list
);
250 if (rqd
->nr_ppas
== 1)
253 rqd
->ppa_list
= rqd
->meta_list
+ pblk_dma_meta_size(pblk
);
254 rqd
->dma_ppa_list
= rqd
->dma_meta_list
+ pblk_dma_meta_size(pblk
);
259 void pblk_free_rqd_meta(struct pblk
*pblk
, struct nvm_rq
*rqd
)
261 struct nvm_tgt_dev
*dev
= pblk
->dev
;
264 nvm_dev_dma_free(dev
->parent
, rqd
->meta_list
,
268 /* Caller must guarantee that the request is a valid type */
269 struct nvm_rq
*pblk_alloc_rqd(struct pblk
*pblk
, int type
)
278 pool
= &pblk
->w_rq_pool
;
279 rq_size
= pblk_w_rq_size
;
282 pool
= &pblk
->r_rq_pool
;
283 rq_size
= pblk_g_rq_size
;
286 pool
= &pblk
->e_rq_pool
;
287 rq_size
= pblk_g_rq_size
;
290 rqd
= mempool_alloc(pool
, GFP_KERNEL
);
291 memset(rqd
, 0, rq_size
);
296 /* Typically used on completion path. Cannot guarantee request consistency */
297 void pblk_free_rqd(struct pblk
*pblk
, struct nvm_rq
*rqd
, int type
)
303 kfree(((struct pblk_c_ctx
*)nvm_rq_to_pdu(rqd
))->lun_bitmap
);
306 pool
= &pblk
->w_rq_pool
;
309 pool
= &pblk
->r_rq_pool
;
312 pool
= &pblk
->e_rq_pool
;
315 pblk_err(pblk
, "trying to free unknown rqd type\n");
319 pblk_free_rqd_meta(pblk
, rqd
);
320 mempool_free(rqd
, pool
);
323 void pblk_bio_free_pages(struct pblk
*pblk
, struct bio
*bio
, int off
,
329 WARN_ON(off
+ nr_pages
!= bio
->bi_vcnt
);
331 for (i
= off
; i
< nr_pages
+ off
; i
++) {
332 bv
= bio
->bi_io_vec
[i
];
333 mempool_free(bv
.bv_page
, &pblk
->page_bio_pool
);
337 int pblk_bio_add_pages(struct pblk
*pblk
, struct bio
*bio
, gfp_t flags
,
340 struct request_queue
*q
= pblk
->dev
->q
;
344 for (i
= 0; i
< nr_pages
; i
++) {
345 page
= mempool_alloc(&pblk
->page_bio_pool
, flags
);
347 ret
= bio_add_pc_page(q
, bio
, page
, PBLK_EXPOSED_PAGE_SIZE
, 0);
348 if (ret
!= PBLK_EXPOSED_PAGE_SIZE
) {
349 pblk_err(pblk
, "could not add page to bio\n");
350 mempool_free(page
, &pblk
->page_bio_pool
);
357 pblk_bio_free_pages(pblk
, bio
, (bio
->bi_vcnt
- i
), i
);
361 void pblk_write_kick(struct pblk
*pblk
)
363 wake_up_process(pblk
->writer_ts
);
364 mod_timer(&pblk
->wtimer
, jiffies
+ msecs_to_jiffies(1000));
367 void pblk_write_timer_fn(struct timer_list
*t
)
369 struct pblk
*pblk
= from_timer(pblk
, t
, wtimer
);
371 /* kick the write thread every tick to flush outstanding data */
372 pblk_write_kick(pblk
);
375 void pblk_write_should_kick(struct pblk
*pblk
)
377 unsigned int secs_avail
= pblk_rb_read_count(&pblk
->rwb
);
379 if (secs_avail
>= pblk
->min_write_pgs_data
)
380 pblk_write_kick(pblk
);
383 static void pblk_wait_for_meta(struct pblk
*pblk
)
386 if (!atomic_read(&pblk
->inflight_io
))
393 static void pblk_flush_writer(struct pblk
*pblk
)
395 pblk_rb_flush(&pblk
->rwb
);
397 if (!pblk_rb_sync_count(&pblk
->rwb
))
400 pblk_write_kick(pblk
);
405 struct list_head
*pblk_line_gc_list(struct pblk
*pblk
, struct pblk_line
*line
)
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
;
410 int packed_meta
= (le32_to_cpu(*line
->vsc
) / pblk
->min_write_pgs_data
)
411 * (pblk
->min_write_pgs
- pblk
->min_write_pgs_data
);
412 int vsc
= le32_to_cpu(*line
->vsc
) + packed_meta
;
414 lockdep_assert_held(&line
->lock
);
416 if (line
->w_err_gc
->has_write_err
) {
417 if (line
->gc_group
!= PBLK_LINEGC_WERR
) {
418 line
->gc_group
= PBLK_LINEGC_WERR
;
419 move_list
= &l_mg
->gc_werr_list
;
420 pblk_rl_werr_line_in(&pblk
->rl
);
423 if (line
->gc_group
!= PBLK_LINEGC_FULL
) {
424 line
->gc_group
= PBLK_LINEGC_FULL
;
425 move_list
= &l_mg
->gc_full_list
;
427 } else if (vsc
< lm
->high_thrs
) {
428 if (line
->gc_group
!= PBLK_LINEGC_HIGH
) {
429 line
->gc_group
= PBLK_LINEGC_HIGH
;
430 move_list
= &l_mg
->gc_high_list
;
432 } else if (vsc
< lm
->mid_thrs
) {
433 if (line
->gc_group
!= PBLK_LINEGC_MID
) {
434 line
->gc_group
= PBLK_LINEGC_MID
;
435 move_list
= &l_mg
->gc_mid_list
;
437 } else if (vsc
< line
->sec_in_line
) {
438 if (line
->gc_group
!= PBLK_LINEGC_LOW
) {
439 line
->gc_group
= PBLK_LINEGC_LOW
;
440 move_list
= &l_mg
->gc_low_list
;
442 } else if (vsc
== line
->sec_in_line
) {
443 if (line
->gc_group
!= PBLK_LINEGC_EMPTY
) {
444 line
->gc_group
= PBLK_LINEGC_EMPTY
;
445 move_list
= &l_mg
->gc_empty_list
;
448 line
->state
= PBLK_LINESTATE_CORRUPT
;
449 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
452 line
->gc_group
= PBLK_LINEGC_NONE
;
453 move_list
= &l_mg
->corrupt_list
;
454 pblk_err(pblk
, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
457 lm
->high_thrs
, lm
->mid_thrs
);
463 void pblk_discard(struct pblk
*pblk
, struct bio
*bio
)
465 sector_t slba
= pblk_get_lba(bio
);
466 sector_t nr_secs
= pblk_get_secs(bio
);
468 pblk_invalidate_range(pblk
, slba
, nr_secs
);
471 void pblk_log_write_err(struct pblk
*pblk
, struct nvm_rq
*rqd
)
473 atomic_long_inc(&pblk
->write_failed
);
474 #ifdef CONFIG_NVM_PBLK_DEBUG
475 pblk_print_failed_rqd(pblk
, rqd
, rqd
->error
);
479 void pblk_log_read_err(struct pblk
*pblk
, struct nvm_rq
*rqd
)
481 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
482 if (rqd
->error
== NVM_RSP_ERR_EMPTYPAGE
) {
483 atomic_long_inc(&pblk
->read_empty
);
487 switch (rqd
->error
) {
488 case NVM_RSP_WARN_HIGHECC
:
489 atomic_long_inc(&pblk
->read_high_ecc
);
491 case NVM_RSP_ERR_FAILECC
:
492 case NVM_RSP_ERR_FAILCRC
:
493 atomic_long_inc(&pblk
->read_failed
);
496 pblk_err(pblk
, "unknown read error:%d\n", rqd
->error
);
498 #ifdef CONFIG_NVM_PBLK_DEBUG
499 pblk_print_failed_rqd(pblk
, rqd
, rqd
->error
);
503 void pblk_set_sec_per_write(struct pblk
*pblk
, int sec_per_write
)
505 pblk
->sec_per_write
= sec_per_write
;
508 int pblk_submit_io(struct pblk
*pblk
, struct nvm_rq
*rqd
)
510 struct nvm_tgt_dev
*dev
= pblk
->dev
;
512 atomic_inc(&pblk
->inflight_io
);
514 #ifdef CONFIG_NVM_PBLK_DEBUG
515 if (pblk_check_io(pblk
, rqd
))
519 return nvm_submit_io(dev
, rqd
);
522 void pblk_check_chunk_state_update(struct pblk
*pblk
, struct nvm_rq
*rqd
)
524 struct ppa_addr
*ppa_list
= nvm_rq_to_ppa_list(rqd
);
528 for (i
= 0; i
< rqd
->nr_ppas
; i
++) {
529 struct ppa_addr
*ppa
= &ppa_list
[i
];
530 struct nvm_chk_meta
*chunk
= pblk_dev_ppa_to_chunk(pblk
, *ppa
);
531 u64 caddr
= pblk_dev_ppa_to_chunk_addr(pblk
, *ppa
);
534 trace_pblk_chunk_state(pblk_disk_name(pblk
),
535 ppa
, NVM_CHK_ST_OPEN
);
536 else if (caddr
== (chunk
->cnlb
- 1))
537 trace_pblk_chunk_state(pblk_disk_name(pblk
),
538 ppa
, NVM_CHK_ST_CLOSED
);
542 int pblk_submit_io_sync(struct pblk
*pblk
, struct nvm_rq
*rqd
)
544 struct nvm_tgt_dev
*dev
= pblk
->dev
;
547 atomic_inc(&pblk
->inflight_io
);
549 #ifdef CONFIG_NVM_PBLK_DEBUG
550 if (pblk_check_io(pblk
, rqd
))
554 ret
= nvm_submit_io_sync(dev
, rqd
);
556 if (trace_pblk_chunk_state_enabled() && !ret
&&
557 rqd
->opcode
== NVM_OP_PWRITE
)
558 pblk_check_chunk_state_update(pblk
, rqd
);
563 int pblk_submit_io_sync_sem(struct pblk
*pblk
, struct nvm_rq
*rqd
)
565 struct ppa_addr
*ppa_list
;
568 ppa_list
= (rqd
->nr_ppas
> 1) ? rqd
->ppa_list
: &rqd
->ppa_addr
;
570 pblk_down_chunk(pblk
, ppa_list
[0]);
571 ret
= pblk_submit_io_sync(pblk
, rqd
);
572 pblk_up_chunk(pblk
, ppa_list
[0]);
577 static void pblk_bio_map_addr_endio(struct bio
*bio
)
582 struct bio
*pblk_bio_map_addr(struct pblk
*pblk
, void *data
,
583 unsigned int nr_secs
, unsigned int len
,
584 int alloc_type
, gfp_t gfp_mask
)
586 struct nvm_tgt_dev
*dev
= pblk
->dev
;
592 if (alloc_type
== PBLK_KMALLOC_META
)
593 return bio_map_kern(dev
->q
, kaddr
, len
, gfp_mask
);
595 bio
= bio_kmalloc(gfp_mask
, nr_secs
);
597 return ERR_PTR(-ENOMEM
);
599 for (i
= 0; i
< nr_secs
; i
++) {
600 page
= vmalloc_to_page(kaddr
);
602 pblk_err(pblk
, "could not map vmalloc bio\n");
604 bio
= ERR_PTR(-ENOMEM
);
608 ret
= bio_add_pc_page(dev
->q
, bio
, page
, PAGE_SIZE
, 0);
609 if (ret
!= PAGE_SIZE
) {
610 pblk_err(pblk
, "could not add page to bio\n");
612 bio
= ERR_PTR(-ENOMEM
);
619 bio
->bi_end_io
= pblk_bio_map_addr_endio
;
624 int pblk_calc_secs(struct pblk
*pblk
, unsigned long secs_avail
,
625 unsigned long secs_to_flush
, bool skip_meta
)
627 int max
= pblk
->sec_per_write
;
628 int min
= pblk
->min_write_pgs
;
629 int secs_to_sync
= 0;
631 if (skip_meta
&& pblk
->min_write_pgs_data
!= pblk
->min_write_pgs
)
632 min
= max
= pblk
->min_write_pgs_data
;
634 if (secs_avail
>= max
)
636 else if (secs_avail
>= min
)
637 secs_to_sync
= min
* (secs_avail
/ min
);
638 else if (secs_to_flush
)
644 void pblk_dealloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
)
649 spin_lock(&line
->lock
);
650 addr
= find_next_zero_bit(line
->map_bitmap
,
651 pblk
->lm
.sec_per_line
, line
->cur_sec
);
652 line
->cur_sec
= addr
- nr_secs
;
654 for (i
= 0; i
< nr_secs
; i
++, line
->cur_sec
--)
655 WARN_ON(!test_and_clear_bit(line
->cur_sec
, line
->map_bitmap
));
656 spin_unlock(&line
->lock
);
659 u64
__pblk_alloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
)
664 lockdep_assert_held(&line
->lock
);
666 /* logic error: ppa out-of-bounds. Prevent generating bad address */
667 if (line
->cur_sec
+ nr_secs
> pblk
->lm
.sec_per_line
) {
668 WARN(1, "pblk: page allocation out of bounds\n");
669 nr_secs
= pblk
->lm
.sec_per_line
- line
->cur_sec
;
672 line
->cur_sec
= addr
= find_next_zero_bit(line
->map_bitmap
,
673 pblk
->lm
.sec_per_line
, line
->cur_sec
);
674 for (i
= 0; i
< nr_secs
; i
++, line
->cur_sec
++)
675 WARN_ON(test_and_set_bit(line
->cur_sec
, line
->map_bitmap
));
680 u64
pblk_alloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
)
684 /* Lock needed in case a write fails and a recovery needs to remap
685 * failed write buffer entries
687 spin_lock(&line
->lock
);
688 addr
= __pblk_alloc_page(pblk
, line
, nr_secs
);
689 line
->left_msecs
-= nr_secs
;
690 WARN(line
->left_msecs
< 0, "pblk: page allocation out of bounds\n");
691 spin_unlock(&line
->lock
);
696 u64
pblk_lookup_page(struct pblk
*pblk
, struct pblk_line
*line
)
700 spin_lock(&line
->lock
);
701 paddr
= find_next_zero_bit(line
->map_bitmap
,
702 pblk
->lm
.sec_per_line
, line
->cur_sec
);
703 spin_unlock(&line
->lock
);
708 u64
pblk_line_smeta_start(struct pblk
*pblk
, struct pblk_line
*line
)
710 struct nvm_tgt_dev
*dev
= pblk
->dev
;
711 struct nvm_geo
*geo
= &dev
->geo
;
712 struct pblk_line_meta
*lm
= &pblk
->lm
;
715 /* This usually only happens on bad lines */
716 bit
= find_first_zero_bit(line
->blk_bitmap
, lm
->blk_per_line
);
717 if (bit
>= lm
->blk_per_line
)
720 return bit
* geo
->ws_opt
;
723 int pblk_line_smeta_read(struct pblk
*pblk
, struct pblk_line
*line
)
725 struct nvm_tgt_dev
*dev
= pblk
->dev
;
726 struct pblk_line_meta
*lm
= &pblk
->lm
;
729 u64 paddr
= pblk_line_smeta_start(pblk
, line
);
732 memset(&rqd
, 0, sizeof(struct nvm_rq
));
734 ret
= pblk_alloc_rqd_meta(pblk
, &rqd
);
738 bio
= bio_map_kern(dev
->q
, line
->smeta
, lm
->smeta_len
, GFP_KERNEL
);
744 bio
->bi_iter
.bi_sector
= 0; /* internal bio */
745 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
748 rqd
.opcode
= NVM_OP_PREAD
;
749 rqd
.nr_ppas
= lm
->smeta_sec
;
752 for (i
= 0; i
< lm
->smeta_sec
; i
++, paddr
++)
753 rqd
.ppa_list
[i
] = addr_to_gen_ppa(pblk
, paddr
, line
->id
);
755 ret
= pblk_submit_io_sync(pblk
, &rqd
);
757 pblk_err(pblk
, "smeta I/O submission failed: %d\n", ret
);
762 atomic_dec(&pblk
->inflight_io
);
765 pblk_log_read_err(pblk
, &rqd
);
768 pblk_free_rqd_meta(pblk
, &rqd
);
772 static int pblk_line_smeta_write(struct pblk
*pblk
, struct pblk_line
*line
,
775 struct nvm_tgt_dev
*dev
= pblk
->dev
;
776 struct pblk_line_meta
*lm
= &pblk
->lm
;
779 __le64
*lba_list
= emeta_to_lbas(pblk
, line
->emeta
->buf
);
780 __le64 addr_empty
= cpu_to_le64(ADDR_EMPTY
);
783 memset(&rqd
, 0, sizeof(struct nvm_rq
));
785 ret
= pblk_alloc_rqd_meta(pblk
, &rqd
);
789 bio
= bio_map_kern(dev
->q
, line
->smeta
, lm
->smeta_len
, GFP_KERNEL
);
795 bio
->bi_iter
.bi_sector
= 0; /* internal bio */
796 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
799 rqd
.opcode
= NVM_OP_PWRITE
;
800 rqd
.nr_ppas
= lm
->smeta_sec
;
803 for (i
= 0; i
< lm
->smeta_sec
; i
++, paddr
++) {
804 struct pblk_sec_meta
*meta
= pblk_get_meta(pblk
,
807 rqd
.ppa_list
[i
] = addr_to_gen_ppa(pblk
, paddr
, line
->id
);
808 meta
->lba
= lba_list
[paddr
] = addr_empty
;
811 ret
= pblk_submit_io_sync_sem(pblk
, &rqd
);
813 pblk_err(pblk
, "smeta I/O submission failed: %d\n", ret
);
818 atomic_dec(&pblk
->inflight_io
);
821 pblk_log_write_err(pblk
, &rqd
);
826 pblk_free_rqd_meta(pblk
, &rqd
);
830 int pblk_line_emeta_read(struct pblk
*pblk
, struct pblk_line
*line
,
833 struct nvm_tgt_dev
*dev
= pblk
->dev
;
834 struct nvm_geo
*geo
= &dev
->geo
;
835 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
836 struct pblk_line_meta
*lm
= &pblk
->lm
;
837 void *ppa_list
, *meta_list
;
840 u64 paddr
= line
->emeta_ssec
;
841 dma_addr_t dma_ppa_list
, dma_meta_list
;
842 int min
= pblk
->min_write_pgs
;
843 int left_ppas
= lm
->emeta_sec
[0];
844 int line_id
= line
->id
;
849 meta_list
= nvm_dev_dma_alloc(dev
->parent
, GFP_KERNEL
,
854 ppa_list
= meta_list
+ pblk_dma_meta_size(pblk
);
855 dma_ppa_list
= dma_meta_list
+ pblk_dma_meta_size(pblk
);
858 memset(&rqd
, 0, sizeof(struct nvm_rq
));
860 rq_ppas
= pblk_calc_secs(pblk
, left_ppas
, 0, false);
861 rq_len
= rq_ppas
* geo
->csecs
;
863 bio
= pblk_bio_map_addr(pblk
, emeta_buf
, rq_ppas
, rq_len
,
864 l_mg
->emeta_alloc_type
, GFP_KERNEL
);
870 bio
->bi_iter
.bi_sector
= 0; /* internal bio */
871 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
874 rqd
.meta_list
= meta_list
;
875 rqd
.ppa_list
= ppa_list
;
876 rqd
.dma_meta_list
= dma_meta_list
;
877 rqd
.dma_ppa_list
= dma_ppa_list
;
878 rqd
.opcode
= NVM_OP_PREAD
;
879 rqd
.nr_ppas
= rq_ppas
;
881 for (i
= 0; i
< rqd
.nr_ppas
; ) {
882 struct ppa_addr ppa
= addr_to_gen_ppa(pblk
, paddr
, line_id
);
883 int pos
= pblk_ppa_to_pos(geo
, ppa
);
885 if (pblk_io_aligned(pblk
, rq_ppas
))
888 while (test_bit(pos
, line
->blk_bitmap
)) {
890 if (pblk_boundary_paddr_checks(pblk
, paddr
)) {
896 ppa
= addr_to_gen_ppa(pblk
, paddr
, line_id
);
897 pos
= pblk_ppa_to_pos(geo
, ppa
);
900 if (pblk_boundary_paddr_checks(pblk
, paddr
+ min
)) {
906 for (j
= 0; j
< min
; j
++, i
++, paddr
++)
907 rqd
.ppa_list
[i
] = addr_to_gen_ppa(pblk
, paddr
, line_id
);
910 ret
= pblk_submit_io_sync(pblk
, &rqd
);
912 pblk_err(pblk
, "emeta I/O submission failed: %d\n", ret
);
917 atomic_dec(&pblk
->inflight_io
);
920 pblk_log_read_err(pblk
, &rqd
);
923 left_ppas
-= rq_ppas
;
928 nvm_dev_dma_free(dev
->parent
, rqd
.meta_list
, rqd
.dma_meta_list
);
932 static void pblk_setup_e_rq(struct pblk
*pblk
, struct nvm_rq
*rqd
,
935 rqd
->opcode
= NVM_OP_ERASE
;
942 static int pblk_blk_erase_sync(struct pblk
*pblk
, struct ppa_addr ppa
)
944 struct nvm_rq rqd
= {NULL
};
947 trace_pblk_chunk_reset(pblk_disk_name(pblk
), &ppa
,
948 PBLK_CHUNK_RESET_START
);
950 pblk_setup_e_rq(pblk
, &rqd
, ppa
);
952 /* The write thread schedules erases so that it minimizes disturbances
953 * with writes. Thus, there is no need to take the LUN semaphore.
955 ret
= pblk_submit_io_sync(pblk
, &rqd
);
957 __pblk_end_io_erase(pblk
, &rqd
);
962 int pblk_line_erase(struct pblk
*pblk
, struct pblk_line
*line
)
964 struct pblk_line_meta
*lm
= &pblk
->lm
;
968 /* Erase only good blocks, one at a time */
970 spin_lock(&line
->lock
);
971 bit
= find_next_zero_bit(line
->erase_bitmap
, lm
->blk_per_line
,
973 if (bit
>= lm
->blk_per_line
) {
974 spin_unlock(&line
->lock
);
978 ppa
= pblk
->luns
[bit
].bppa
; /* set ch and lun */
979 ppa
.a
.blk
= line
->id
;
981 atomic_dec(&line
->left_eblks
);
982 WARN_ON(test_and_set_bit(bit
, line
->erase_bitmap
));
983 spin_unlock(&line
->lock
);
985 ret
= pblk_blk_erase_sync(pblk
, ppa
);
987 pblk_err(pblk
, "failed to erase line %d\n", line
->id
);
995 static void pblk_line_setup_metadata(struct pblk_line
*line
,
996 struct pblk_line_mgmt
*l_mg
,
997 struct pblk_line_meta
*lm
)
1001 lockdep_assert_held(&l_mg
->free_lock
);
1004 meta_line
= find_first_zero_bit(&l_mg
->meta_bitmap
, PBLK_DATA_LINES
);
1005 if (meta_line
== PBLK_DATA_LINES
) {
1006 spin_unlock(&l_mg
->free_lock
);
1008 spin_lock(&l_mg
->free_lock
);
1012 set_bit(meta_line
, &l_mg
->meta_bitmap
);
1013 line
->meta_line
= meta_line
;
1015 line
->smeta
= l_mg
->sline_meta
[meta_line
];
1016 line
->emeta
= l_mg
->eline_meta
[meta_line
];
1018 memset(line
->smeta
, 0, lm
->smeta_len
);
1019 memset(line
->emeta
->buf
, 0, lm
->emeta_len
[0]);
1021 line
->emeta
->mem
= 0;
1022 atomic_set(&line
->emeta
->sync
, 0);
1025 /* For now lines are always assumed full lines. Thus, smeta former and current
1026 * lun bitmaps are omitted.
1028 static int pblk_line_init_metadata(struct pblk
*pblk
, struct pblk_line
*line
,
1029 struct pblk_line
*cur
)
1031 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1032 struct nvm_geo
*geo
= &dev
->geo
;
1033 struct pblk_line_meta
*lm
= &pblk
->lm
;
1034 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1035 struct pblk_emeta
*emeta
= line
->emeta
;
1036 struct line_emeta
*emeta_buf
= emeta
->buf
;
1037 struct line_smeta
*smeta_buf
= (struct line_smeta
*)line
->smeta
;
1040 /* After erasing the line, new bad blocks might appear and we risk
1041 * having an invalid line
1043 nr_blk_line
= lm
->blk_per_line
-
1044 bitmap_weight(line
->blk_bitmap
, lm
->blk_per_line
);
1045 if (nr_blk_line
< lm
->min_blk_line
) {
1046 spin_lock(&l_mg
->free_lock
);
1047 spin_lock(&line
->lock
);
1048 line
->state
= PBLK_LINESTATE_BAD
;
1049 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1051 spin_unlock(&line
->lock
);
1053 list_add_tail(&line
->list
, &l_mg
->bad_list
);
1054 spin_unlock(&l_mg
->free_lock
);
1056 pblk_debug(pblk
, "line %d is bad\n", line
->id
);
1061 /* Run-time metadata */
1062 line
->lun_bitmap
= ((void *)(smeta_buf
)) + sizeof(struct line_smeta
);
1064 /* Mark LUNs allocated in this line (all for now) */
1065 bitmap_set(line
->lun_bitmap
, 0, lm
->lun_bitmap_len
);
1067 smeta_buf
->header
.identifier
= cpu_to_le32(PBLK_MAGIC
);
1068 memcpy(smeta_buf
->header
.uuid
, pblk
->instance_uuid
, 16);
1069 smeta_buf
->header
.id
= cpu_to_le32(line
->id
);
1070 smeta_buf
->header
.type
= cpu_to_le16(line
->type
);
1071 smeta_buf
->header
.version_major
= SMETA_VERSION_MAJOR
;
1072 smeta_buf
->header
.version_minor
= SMETA_VERSION_MINOR
;
1074 /* Start metadata */
1075 smeta_buf
->seq_nr
= cpu_to_le64(line
->seq_nr
);
1076 smeta_buf
->window_wr_lun
= cpu_to_le32(geo
->all_luns
);
1078 /* Fill metadata among lines */
1080 memcpy(line
->lun_bitmap
, cur
->lun_bitmap
, lm
->lun_bitmap_len
);
1081 smeta_buf
->prev_id
= cpu_to_le32(cur
->id
);
1082 cur
->emeta
->buf
->next_id
= cpu_to_le32(line
->id
);
1084 smeta_buf
->prev_id
= cpu_to_le32(PBLK_LINE_EMPTY
);
1087 /* All smeta must be set at this point */
1088 smeta_buf
->header
.crc
= cpu_to_le32(
1089 pblk_calc_meta_header_crc(pblk
, &smeta_buf
->header
));
1090 smeta_buf
->crc
= cpu_to_le32(pblk_calc_smeta_crc(pblk
, smeta_buf
));
1093 memcpy(&emeta_buf
->header
, &smeta_buf
->header
,
1094 sizeof(struct line_header
));
1096 emeta_buf
->header
.version_major
= EMETA_VERSION_MAJOR
;
1097 emeta_buf
->header
.version_minor
= EMETA_VERSION_MINOR
;
1098 emeta_buf
->header
.crc
= cpu_to_le32(
1099 pblk_calc_meta_header_crc(pblk
, &emeta_buf
->header
));
1101 emeta_buf
->seq_nr
= cpu_to_le64(line
->seq_nr
);
1102 emeta_buf
->nr_lbas
= cpu_to_le64(line
->sec_in_line
);
1103 emeta_buf
->nr_valid_lbas
= cpu_to_le64(0);
1104 emeta_buf
->next_id
= cpu_to_le32(PBLK_LINE_EMPTY
);
1105 emeta_buf
->crc
= cpu_to_le32(0);
1106 emeta_buf
->prev_id
= smeta_buf
->prev_id
;
1111 static int pblk_line_alloc_bitmaps(struct pblk
*pblk
, struct pblk_line
*line
)
1113 struct pblk_line_meta
*lm
= &pblk
->lm
;
1114 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1116 line
->map_bitmap
= mempool_alloc(l_mg
->bitmap_pool
, GFP_KERNEL
);
1117 if (!line
->map_bitmap
)
1120 memset(line
->map_bitmap
, 0, lm
->sec_bitmap_len
);
1122 /* will be initialized using bb info from map_bitmap */
1123 line
->invalid_bitmap
= mempool_alloc(l_mg
->bitmap_pool
, GFP_KERNEL
);
1124 if (!line
->invalid_bitmap
) {
1125 mempool_free(line
->map_bitmap
, l_mg
->bitmap_pool
);
1126 line
->map_bitmap
= NULL
;
1133 /* For now lines are always assumed full lines. Thus, smeta former and current
1134 * lun bitmaps are omitted.
1136 static int pblk_line_init_bb(struct pblk
*pblk
, struct pblk_line
*line
,
1139 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1140 struct nvm_geo
*geo
= &dev
->geo
;
1141 struct pblk_line_meta
*lm
= &pblk
->lm
;
1142 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1147 line
->sec_in_line
= lm
->sec_per_line
;
1149 /* Capture bad block information on line mapping bitmaps */
1150 while ((bit
= find_next_bit(line
->blk_bitmap
, lm
->blk_per_line
,
1151 bit
+ 1)) < lm
->blk_per_line
) {
1152 off
= bit
* geo
->ws_opt
;
1153 bitmap_shift_left(l_mg
->bb_aux
, l_mg
->bb_template
, off
,
1155 bitmap_or(line
->map_bitmap
, line
->map_bitmap
, l_mg
->bb_aux
,
1157 line
->sec_in_line
-= geo
->clba
;
1160 /* Mark smeta metadata sectors as bad sectors */
1161 bit
= find_first_zero_bit(line
->blk_bitmap
, lm
->blk_per_line
);
1162 off
= bit
* geo
->ws_opt
;
1163 bitmap_set(line
->map_bitmap
, off
, lm
->smeta_sec
);
1164 line
->sec_in_line
-= lm
->smeta_sec
;
1165 line
->smeta_ssec
= off
;
1166 line
->cur_sec
= off
+ lm
->smeta_sec
;
1168 if (init
&& pblk_line_smeta_write(pblk
, line
, off
)) {
1169 pblk_debug(pblk
, "line smeta I/O failed. Retry\n");
1173 bitmap_copy(line
->invalid_bitmap
, line
->map_bitmap
, lm
->sec_per_line
);
1175 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1176 * blocks to make sure that there are enough sectors to store emeta
1178 emeta_secs
= lm
->emeta_sec
[0];
1179 off
= lm
->sec_per_line
;
1180 while (emeta_secs
) {
1182 if (!test_bit(off
, line
->invalid_bitmap
)) {
1183 bitmap_set(line
->invalid_bitmap
, off
, geo
->ws_opt
);
1184 emeta_secs
-= geo
->ws_opt
;
1188 line
->emeta_ssec
= off
;
1189 line
->sec_in_line
-= lm
->emeta_sec
[0];
1190 line
->nr_valid_lbas
= 0;
1191 line
->left_msecs
= line
->sec_in_line
;
1192 *line
->vsc
= cpu_to_le32(line
->sec_in_line
);
1194 if (lm
->sec_per_line
- line
->sec_in_line
!=
1195 bitmap_weight(line
->invalid_bitmap
, lm
->sec_per_line
)) {
1196 spin_lock(&line
->lock
);
1197 line
->state
= PBLK_LINESTATE_BAD
;
1198 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1200 spin_unlock(&line
->lock
);
1202 list_add_tail(&line
->list
, &l_mg
->bad_list
);
1203 pblk_err(pblk
, "unexpected line %d is bad\n", line
->id
);
1211 static int pblk_prepare_new_line(struct pblk
*pblk
, struct pblk_line
*line
)
1213 struct pblk_line_meta
*lm
= &pblk
->lm
;
1214 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1215 struct nvm_geo
*geo
= &dev
->geo
;
1216 int blk_to_erase
= atomic_read(&line
->blk_in_line
);
1219 for (i
= 0; i
< lm
->blk_per_line
; i
++) {
1220 struct pblk_lun
*rlun
= &pblk
->luns
[i
];
1221 int pos
= pblk_ppa_to_pos(geo
, rlun
->bppa
);
1222 int state
= line
->chks
[pos
].state
;
1224 /* Free chunks should not be erased */
1225 if (state
& NVM_CHK_ST_FREE
) {
1226 set_bit(pblk_ppa_to_pos(geo
, rlun
->bppa
),
1227 line
->erase_bitmap
);
1232 return blk_to_erase
;
1235 static int pblk_line_prepare(struct pblk
*pblk
, struct pblk_line
*line
)
1237 struct pblk_line_meta
*lm
= &pblk
->lm
;
1238 int blk_in_line
= atomic_read(&line
->blk_in_line
);
1241 /* Bad blocks do not need to be erased */
1242 bitmap_copy(line
->erase_bitmap
, line
->blk_bitmap
, lm
->blk_per_line
);
1244 spin_lock(&line
->lock
);
1246 /* If we have not written to this line, we need to mark up free chunks
1249 if (line
->state
== PBLK_LINESTATE_NEW
) {
1250 blk_to_erase
= pblk_prepare_new_line(pblk
, line
);
1251 line
->state
= PBLK_LINESTATE_FREE
;
1252 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1255 blk_to_erase
= blk_in_line
;
1258 if (blk_in_line
< lm
->min_blk_line
) {
1259 spin_unlock(&line
->lock
);
1263 if (line
->state
!= PBLK_LINESTATE_FREE
) {
1264 WARN(1, "pblk: corrupted line %d, state %d\n",
1265 line
->id
, line
->state
);
1266 spin_unlock(&line
->lock
);
1270 line
->state
= PBLK_LINESTATE_OPEN
;
1271 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1274 atomic_set(&line
->left_eblks
, blk_to_erase
);
1275 atomic_set(&line
->left_seblks
, blk_to_erase
);
1277 line
->meta_distance
= lm
->meta_distance
;
1278 spin_unlock(&line
->lock
);
1280 kref_init(&line
->ref
);
1285 /* Line allocations in the recovery path are always single threaded */
1286 int pblk_line_recov_alloc(struct pblk
*pblk
, struct pblk_line
*line
)
1288 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1291 spin_lock(&l_mg
->free_lock
);
1292 l_mg
->data_line
= line
;
1293 list_del(&line
->list
);
1295 ret
= pblk_line_prepare(pblk
, line
);
1297 list_add(&line
->list
, &l_mg
->free_list
);
1298 spin_unlock(&l_mg
->free_lock
);
1301 spin_unlock(&l_mg
->free_lock
);
1303 ret
= pblk_line_alloc_bitmaps(pblk
, line
);
1307 if (!pblk_line_init_bb(pblk
, line
, 0)) {
1312 pblk_rl_free_lines_dec(&pblk
->rl
, line
, true);
1316 spin_lock(&l_mg
->free_lock
);
1317 list_add(&line
->list
, &l_mg
->free_list
);
1318 spin_unlock(&l_mg
->free_lock
);
1323 void pblk_line_recov_close(struct pblk
*pblk
, struct pblk_line
*line
)
1325 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1327 mempool_free(line
->map_bitmap
, l_mg
->bitmap_pool
);
1328 line
->map_bitmap
= NULL
;
1333 static void pblk_line_reinit(struct pblk_line
*line
)
1335 *line
->vsc
= cpu_to_le32(EMPTY_ENTRY
);
1337 line
->map_bitmap
= NULL
;
1338 line
->invalid_bitmap
= NULL
;
1343 void pblk_line_free(struct pblk_line
*line
)
1345 struct pblk
*pblk
= line
->pblk
;
1346 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1348 mempool_free(line
->map_bitmap
, l_mg
->bitmap_pool
);
1349 mempool_free(line
->invalid_bitmap
, l_mg
->bitmap_pool
);
1351 pblk_line_reinit(line
);
1354 struct pblk_line
*pblk_line_get(struct pblk
*pblk
)
1356 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1357 struct pblk_line_meta
*lm
= &pblk
->lm
;
1358 struct pblk_line
*line
;
1361 lockdep_assert_held(&l_mg
->free_lock
);
1364 if (list_empty(&l_mg
->free_list
)) {
1365 pblk_err(pblk
, "no free lines\n");
1369 line
= list_first_entry(&l_mg
->free_list
, struct pblk_line
, list
);
1370 list_del(&line
->list
);
1371 l_mg
->nr_free_lines
--;
1373 bit
= find_first_zero_bit(line
->blk_bitmap
, lm
->blk_per_line
);
1374 if (unlikely(bit
>= lm
->blk_per_line
)) {
1375 spin_lock(&line
->lock
);
1376 line
->state
= PBLK_LINESTATE_BAD
;
1377 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1379 spin_unlock(&line
->lock
);
1381 list_add_tail(&line
->list
, &l_mg
->bad_list
);
1383 pblk_debug(pblk
, "line %d is bad\n", line
->id
);
1387 ret
= pblk_line_prepare(pblk
, line
);
1391 list_add(&line
->list
, &l_mg
->bad_list
);
1394 list_add(&line
->list
, &l_mg
->corrupt_list
);
1397 pblk_err(pblk
, "failed to prepare line %d\n", line
->id
);
1398 list_add(&line
->list
, &l_mg
->free_list
);
1399 l_mg
->nr_free_lines
++;
1407 static struct pblk_line
*pblk_line_retry(struct pblk
*pblk
,
1408 struct pblk_line
*line
)
1410 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1411 struct pblk_line
*retry_line
;
1414 spin_lock(&l_mg
->free_lock
);
1415 retry_line
= pblk_line_get(pblk
);
1417 l_mg
->data_line
= NULL
;
1418 spin_unlock(&l_mg
->free_lock
);
1422 retry_line
->map_bitmap
= line
->map_bitmap
;
1423 retry_line
->invalid_bitmap
= line
->invalid_bitmap
;
1424 retry_line
->smeta
= line
->smeta
;
1425 retry_line
->emeta
= line
->emeta
;
1426 retry_line
->meta_line
= line
->meta_line
;
1428 pblk_line_reinit(line
);
1430 l_mg
->data_line
= retry_line
;
1431 spin_unlock(&l_mg
->free_lock
);
1433 pblk_rl_free_lines_dec(&pblk
->rl
, line
, false);
1435 if (pblk_line_erase(pblk
, retry_line
))
1441 static void pblk_set_space_limit(struct pblk
*pblk
)
1443 struct pblk_rl
*rl
= &pblk
->rl
;
1445 atomic_set(&rl
->rb_space
, 0);
1448 struct pblk_line
*pblk_line_get_first_data(struct pblk
*pblk
)
1450 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1451 struct pblk_line
*line
;
1453 spin_lock(&l_mg
->free_lock
);
1454 line
= pblk_line_get(pblk
);
1456 spin_unlock(&l_mg
->free_lock
);
1460 line
->seq_nr
= l_mg
->d_seq_nr
++;
1461 line
->type
= PBLK_LINETYPE_DATA
;
1462 l_mg
->data_line
= line
;
1464 pblk_line_setup_metadata(line
, l_mg
, &pblk
->lm
);
1466 /* Allocate next line for preparation */
1467 l_mg
->data_next
= pblk_line_get(pblk
);
1468 if (!l_mg
->data_next
) {
1469 /* If we cannot get a new line, we need to stop the pipeline.
1470 * Only allow as many writes in as we can store safely and then
1473 pblk_set_space_limit(pblk
);
1475 l_mg
->data_next
= NULL
;
1477 l_mg
->data_next
->seq_nr
= l_mg
->d_seq_nr
++;
1478 l_mg
->data_next
->type
= PBLK_LINETYPE_DATA
;
1480 spin_unlock(&l_mg
->free_lock
);
1482 if (pblk_line_alloc_bitmaps(pblk
, line
))
1485 if (pblk_line_erase(pblk
, line
)) {
1486 line
= pblk_line_retry(pblk
, line
);
1492 if (!pblk_line_init_metadata(pblk
, line
, NULL
)) {
1493 line
= pblk_line_retry(pblk
, line
);
1500 if (!pblk_line_init_bb(pblk
, line
, 1)) {
1501 line
= pblk_line_retry(pblk
, line
);
1508 pblk_rl_free_lines_dec(&pblk
->rl
, line
, true);
1513 void pblk_ppa_to_line_put(struct pblk
*pblk
, struct ppa_addr ppa
)
1515 struct pblk_line
*line
;
1517 line
= pblk_ppa_to_line(pblk
, ppa
);
1518 kref_put(&line
->ref
, pblk_line_put_wq
);
1521 void pblk_rq_to_line_put(struct pblk
*pblk
, struct nvm_rq
*rqd
)
1523 struct ppa_addr
*ppa_list
;
1526 ppa_list
= (rqd
->nr_ppas
> 1) ? rqd
->ppa_list
: &rqd
->ppa_addr
;
1528 for (i
= 0; i
< rqd
->nr_ppas
; i
++)
1529 pblk_ppa_to_line_put(pblk
, ppa_list
[i
]);
1532 static void pblk_stop_writes(struct pblk
*pblk
, struct pblk_line
*line
)
1534 lockdep_assert_held(&pblk
->l_mg
.free_lock
);
1536 pblk_set_space_limit(pblk
);
1537 pblk
->state
= PBLK_STATE_STOPPING
;
1538 trace_pblk_state(pblk_disk_name(pblk
), pblk
->state
);
1541 static void pblk_line_close_meta_sync(struct pblk
*pblk
)
1543 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1544 struct pblk_line_meta
*lm
= &pblk
->lm
;
1545 struct pblk_line
*line
, *tline
;
1548 spin_lock(&l_mg
->close_lock
);
1549 if (list_empty(&l_mg
->emeta_list
)) {
1550 spin_unlock(&l_mg
->close_lock
);
1554 list_cut_position(&list
, &l_mg
->emeta_list
, l_mg
->emeta_list
.prev
);
1555 spin_unlock(&l_mg
->close_lock
);
1557 list_for_each_entry_safe(line
, tline
, &list
, list
) {
1558 struct pblk_emeta
*emeta
= line
->emeta
;
1560 while (emeta
->mem
< lm
->emeta_len
[0]) {
1563 ret
= pblk_submit_meta_io(pblk
, line
);
1565 pblk_err(pblk
, "sync meta line %d failed (%d)\n",
1572 pblk_wait_for_meta(pblk
);
1573 flush_workqueue(pblk
->close_wq
);
1576 void __pblk_pipeline_flush(struct pblk
*pblk
)
1578 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1581 spin_lock(&l_mg
->free_lock
);
1582 if (pblk
->state
== PBLK_STATE_RECOVERING
||
1583 pblk
->state
== PBLK_STATE_STOPPED
) {
1584 spin_unlock(&l_mg
->free_lock
);
1587 pblk
->state
= PBLK_STATE_RECOVERING
;
1588 trace_pblk_state(pblk_disk_name(pblk
), pblk
->state
);
1589 spin_unlock(&l_mg
->free_lock
);
1591 pblk_flush_writer(pblk
);
1592 pblk_wait_for_meta(pblk
);
1594 ret
= pblk_recov_pad(pblk
);
1596 pblk_err(pblk
, "could not close data on teardown(%d)\n", ret
);
1600 flush_workqueue(pblk
->bb_wq
);
1601 pblk_line_close_meta_sync(pblk
);
1604 void __pblk_pipeline_stop(struct pblk
*pblk
)
1606 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1608 spin_lock(&l_mg
->free_lock
);
1609 pblk
->state
= PBLK_STATE_STOPPED
;
1610 trace_pblk_state(pblk_disk_name(pblk
), pblk
->state
);
1611 l_mg
->data_line
= NULL
;
1612 l_mg
->data_next
= NULL
;
1613 spin_unlock(&l_mg
->free_lock
);
1616 void pblk_pipeline_stop(struct pblk
*pblk
)
1618 __pblk_pipeline_flush(pblk
);
1619 __pblk_pipeline_stop(pblk
);
1622 struct pblk_line
*pblk_line_replace_data(struct pblk
*pblk
)
1624 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1625 struct pblk_line
*cur
, *new = NULL
;
1626 unsigned int left_seblks
;
1628 new = l_mg
->data_next
;
1632 spin_lock(&l_mg
->free_lock
);
1633 cur
= l_mg
->data_line
;
1634 l_mg
->data_line
= new;
1636 pblk_line_setup_metadata(new, l_mg
, &pblk
->lm
);
1637 spin_unlock(&l_mg
->free_lock
);
1640 left_seblks
= atomic_read(&new->left_seblks
);
1642 /* If line is not fully erased, erase it */
1643 if (atomic_read(&new->left_eblks
)) {
1644 if (pblk_line_erase(pblk
, new))
1652 if (pblk_line_alloc_bitmaps(pblk
, new))
1656 if (!pblk_line_init_metadata(pblk
, new, cur
)) {
1657 new = pblk_line_retry(pblk
, new);
1664 if (!pblk_line_init_bb(pblk
, new, 1)) {
1665 new = pblk_line_retry(pblk
, new);
1672 pblk_rl_free_lines_dec(&pblk
->rl
, new, true);
1674 /* Allocate next line for preparation */
1675 spin_lock(&l_mg
->free_lock
);
1676 l_mg
->data_next
= pblk_line_get(pblk
);
1677 if (!l_mg
->data_next
) {
1678 /* If we cannot get a new line, we need to stop the pipeline.
1679 * Only allow as many writes in as we can store safely and then
1682 pblk_stop_writes(pblk
, new);
1683 l_mg
->data_next
= NULL
;
1685 l_mg
->data_next
->seq_nr
= l_mg
->d_seq_nr
++;
1686 l_mg
->data_next
->type
= PBLK_LINETYPE_DATA
;
1688 spin_unlock(&l_mg
->free_lock
);
1694 static void __pblk_line_put(struct pblk
*pblk
, struct pblk_line
*line
)
1696 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1697 struct pblk_gc
*gc
= &pblk
->gc
;
1699 spin_lock(&line
->lock
);
1700 WARN_ON(line
->state
!= PBLK_LINESTATE_GC
);
1701 line
->state
= PBLK_LINESTATE_FREE
;
1702 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1704 line
->gc_group
= PBLK_LINEGC_NONE
;
1705 pblk_line_free(line
);
1707 if (line
->w_err_gc
->has_write_err
) {
1708 pblk_rl_werr_line_out(&pblk
->rl
);
1709 line
->w_err_gc
->has_write_err
= 0;
1712 spin_unlock(&line
->lock
);
1713 atomic_dec(&gc
->pipeline_gc
);
1715 spin_lock(&l_mg
->free_lock
);
1716 list_add_tail(&line
->list
, &l_mg
->free_list
);
1717 l_mg
->nr_free_lines
++;
1718 spin_unlock(&l_mg
->free_lock
);
1720 pblk_rl_free_lines_inc(&pblk
->rl
, line
);
1723 static void pblk_line_put_ws(struct work_struct
*work
)
1725 struct pblk_line_ws
*line_put_ws
= container_of(work
,
1726 struct pblk_line_ws
, ws
);
1727 struct pblk
*pblk
= line_put_ws
->pblk
;
1728 struct pblk_line
*line
= line_put_ws
->line
;
1730 __pblk_line_put(pblk
, line
);
1731 mempool_free(line_put_ws
, &pblk
->gen_ws_pool
);
1734 void pblk_line_put(struct kref
*ref
)
1736 struct pblk_line
*line
= container_of(ref
, struct pblk_line
, ref
);
1737 struct pblk
*pblk
= line
->pblk
;
1739 __pblk_line_put(pblk
, line
);
1742 void pblk_line_put_wq(struct kref
*ref
)
1744 struct pblk_line
*line
= container_of(ref
, struct pblk_line
, ref
);
1745 struct pblk
*pblk
= line
->pblk
;
1746 struct pblk_line_ws
*line_put_ws
;
1748 line_put_ws
= mempool_alloc(&pblk
->gen_ws_pool
, GFP_ATOMIC
);
1752 line_put_ws
->pblk
= pblk
;
1753 line_put_ws
->line
= line
;
1754 line_put_ws
->priv
= NULL
;
1756 INIT_WORK(&line_put_ws
->ws
, pblk_line_put_ws
);
1757 queue_work(pblk
->r_end_wq
, &line_put_ws
->ws
);
1760 int pblk_blk_erase_async(struct pblk
*pblk
, struct ppa_addr ppa
)
1765 rqd
= pblk_alloc_rqd(pblk
, PBLK_ERASE
);
1767 pblk_setup_e_rq(pblk
, rqd
, ppa
);
1769 rqd
->end_io
= pblk_end_io_erase
;
1770 rqd
->private = pblk
;
1772 trace_pblk_chunk_reset(pblk_disk_name(pblk
),
1773 &ppa
, PBLK_CHUNK_RESET_START
);
1775 /* The write thread schedules erases so that it minimizes disturbances
1776 * with writes. Thus, there is no need to take the LUN semaphore.
1778 err
= pblk_submit_io(pblk
, rqd
);
1780 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1781 struct nvm_geo
*geo
= &dev
->geo
;
1783 pblk_err(pblk
, "could not async erase line:%d,blk:%d\n",
1784 pblk_ppa_to_line_id(ppa
),
1785 pblk_ppa_to_pos(geo
, ppa
));
1791 struct pblk_line
*pblk_line_get_data(struct pblk
*pblk
)
1793 return pblk
->l_mg
.data_line
;
1796 /* For now, always erase next line */
1797 struct pblk_line
*pblk_line_get_erase(struct pblk
*pblk
)
1799 return pblk
->l_mg
.data_next
;
1802 int pblk_line_is_full(struct pblk_line
*line
)
1804 return (line
->left_msecs
== 0);
1807 static void pblk_line_should_sync_meta(struct pblk
*pblk
)
1809 if (pblk_rl_is_limit(&pblk
->rl
))
1810 pblk_line_close_meta_sync(pblk
);
1813 void pblk_line_close(struct pblk
*pblk
, struct pblk_line
*line
)
1815 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1816 struct nvm_geo
*geo
= &dev
->geo
;
1817 struct pblk_line_meta
*lm
= &pblk
->lm
;
1818 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1819 struct list_head
*move_list
;
1822 #ifdef CONFIG_NVM_PBLK_DEBUG
1823 WARN(!bitmap_full(line
->map_bitmap
, lm
->sec_per_line
),
1824 "pblk: corrupt closed line %d\n", line
->id
);
1827 spin_lock(&l_mg
->free_lock
);
1828 WARN_ON(!test_and_clear_bit(line
->meta_line
, &l_mg
->meta_bitmap
));
1829 spin_unlock(&l_mg
->free_lock
);
1831 spin_lock(&l_mg
->gc_lock
);
1832 spin_lock(&line
->lock
);
1833 WARN_ON(line
->state
!= PBLK_LINESTATE_OPEN
);
1834 line
->state
= PBLK_LINESTATE_CLOSED
;
1835 move_list
= pblk_line_gc_list(pblk
, line
);
1836 list_add_tail(&line
->list
, move_list
);
1838 mempool_free(line
->map_bitmap
, l_mg
->bitmap_pool
);
1839 line
->map_bitmap
= NULL
;
1843 for (i
= 0; i
< lm
->blk_per_line
; i
++) {
1844 struct pblk_lun
*rlun
= &pblk
->luns
[i
];
1845 int pos
= pblk_ppa_to_pos(geo
, rlun
->bppa
);
1846 int state
= line
->chks
[pos
].state
;
1848 if (!(state
& NVM_CHK_ST_OFFLINE
))
1849 state
= NVM_CHK_ST_CLOSED
;
1852 spin_unlock(&line
->lock
);
1853 spin_unlock(&l_mg
->gc_lock
);
1855 trace_pblk_line_state(pblk_disk_name(pblk
), line
->id
,
1859 void pblk_line_close_meta(struct pblk
*pblk
, struct pblk_line
*line
)
1861 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1862 struct pblk_line_meta
*lm
= &pblk
->lm
;
1863 struct pblk_emeta
*emeta
= line
->emeta
;
1864 struct line_emeta
*emeta_buf
= emeta
->buf
;
1865 struct wa_counters
*wa
= emeta_to_wa(lm
, emeta_buf
);
1867 /* No need for exact vsc value; avoid a big line lock and take aprox. */
1868 memcpy(emeta_to_vsc(pblk
, emeta_buf
), l_mg
->vsc_list
, lm
->vsc_list_len
);
1869 memcpy(emeta_to_bb(emeta_buf
), line
->blk_bitmap
, lm
->blk_bitmap_len
);
1871 wa
->user
= cpu_to_le64(atomic64_read(&pblk
->user_wa
));
1872 wa
->pad
= cpu_to_le64(atomic64_read(&pblk
->pad_wa
));
1873 wa
->gc
= cpu_to_le64(atomic64_read(&pblk
->gc_wa
));
1875 if (le32_to_cpu(emeta_buf
->header
.identifier
) != PBLK_MAGIC
) {
1876 emeta_buf
->header
.identifier
= cpu_to_le32(PBLK_MAGIC
);
1877 memcpy(emeta_buf
->header
.uuid
, pblk
->instance_uuid
, 16);
1878 emeta_buf
->header
.id
= cpu_to_le32(line
->id
);
1879 emeta_buf
->header
.type
= cpu_to_le16(line
->type
);
1880 emeta_buf
->header
.version_major
= EMETA_VERSION_MAJOR
;
1881 emeta_buf
->header
.version_minor
= EMETA_VERSION_MINOR
;
1882 emeta_buf
->header
.crc
= cpu_to_le32(
1883 pblk_calc_meta_header_crc(pblk
, &emeta_buf
->header
));
1886 emeta_buf
->nr_valid_lbas
= cpu_to_le64(line
->nr_valid_lbas
);
1887 emeta_buf
->crc
= cpu_to_le32(pblk_calc_emeta_crc(pblk
, emeta_buf
));
1889 spin_lock(&l_mg
->close_lock
);
1890 spin_lock(&line
->lock
);
1892 /* Update the in-memory start address for emeta, in case it has
1893 * shifted due to write errors
1895 if (line
->emeta_ssec
!= line
->cur_sec
)
1896 line
->emeta_ssec
= line
->cur_sec
;
1898 list_add_tail(&line
->list
, &l_mg
->emeta_list
);
1899 spin_unlock(&line
->lock
);
1900 spin_unlock(&l_mg
->close_lock
);
1902 pblk_line_should_sync_meta(pblk
);
1905 static void pblk_save_lba_list(struct pblk
*pblk
, struct pblk_line
*line
)
1907 struct pblk_line_meta
*lm
= &pblk
->lm
;
1908 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
1909 unsigned int lba_list_size
= lm
->emeta_len
[2];
1910 struct pblk_w_err_gc
*w_err_gc
= line
->w_err_gc
;
1911 struct pblk_emeta
*emeta
= line
->emeta
;
1913 w_err_gc
->lba_list
= pblk_malloc(lba_list_size
,
1914 l_mg
->emeta_alloc_type
, GFP_KERNEL
);
1915 memcpy(w_err_gc
->lba_list
, emeta_to_lbas(pblk
, emeta
->buf
),
1919 void pblk_line_close_ws(struct work_struct
*work
)
1921 struct pblk_line_ws
*line_ws
= container_of(work
, struct pblk_line_ws
,
1923 struct pblk
*pblk
= line_ws
->pblk
;
1924 struct pblk_line
*line
= line_ws
->line
;
1925 struct pblk_w_err_gc
*w_err_gc
= line
->w_err_gc
;
1927 /* Write errors makes the emeta start address stored in smeta invalid,
1928 * so keep a copy of the lba list until we've gc'd the line
1930 if (w_err_gc
->has_write_err
)
1931 pblk_save_lba_list(pblk
, line
);
1933 pblk_line_close(pblk
, line
);
1934 mempool_free(line_ws
, &pblk
->gen_ws_pool
);
1937 void pblk_gen_run_ws(struct pblk
*pblk
, struct pblk_line
*line
, void *priv
,
1938 void (*work
)(struct work_struct
*), gfp_t gfp_mask
,
1939 struct workqueue_struct
*wq
)
1941 struct pblk_line_ws
*line_ws
;
1943 line_ws
= mempool_alloc(&pblk
->gen_ws_pool
, gfp_mask
);
1945 line_ws
->pblk
= pblk
;
1946 line_ws
->line
= line
;
1947 line_ws
->priv
= priv
;
1949 INIT_WORK(&line_ws
->ws
, work
);
1950 queue_work(wq
, &line_ws
->ws
);
1953 static void __pblk_down_chunk(struct pblk
*pblk
, int pos
)
1955 struct pblk_lun
*rlun
= &pblk
->luns
[pos
];
1959 * Only send one inflight I/O per LUN. Since we map at a page
1960 * granurality, all ppas in the I/O will map to the same LUN
1963 ret
= down_timeout(&rlun
->wr_sem
, msecs_to_jiffies(30000));
1964 if (ret
== -ETIME
|| ret
== -EINTR
)
1965 pblk_err(pblk
, "taking lun semaphore timed out: err %d\n",
1969 void pblk_down_chunk(struct pblk
*pblk
, struct ppa_addr ppa
)
1971 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1972 struct nvm_geo
*geo
= &dev
->geo
;
1973 int pos
= pblk_ppa_to_pos(geo
, ppa
);
1975 __pblk_down_chunk(pblk
, pos
);
1978 void pblk_down_rq(struct pblk
*pblk
, struct ppa_addr ppa
,
1979 unsigned long *lun_bitmap
)
1981 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1982 struct nvm_geo
*geo
= &dev
->geo
;
1983 int pos
= pblk_ppa_to_pos(geo
, ppa
);
1985 /* If the LUN has been locked for this same request, do no attempt to
1988 if (test_and_set_bit(pos
, lun_bitmap
))
1991 __pblk_down_chunk(pblk
, pos
);
1994 void pblk_up_chunk(struct pblk
*pblk
, struct ppa_addr ppa
)
1996 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1997 struct nvm_geo
*geo
= &dev
->geo
;
1998 struct pblk_lun
*rlun
;
1999 int pos
= pblk_ppa_to_pos(geo
, ppa
);
2001 rlun
= &pblk
->luns
[pos
];
2005 void pblk_up_rq(struct pblk
*pblk
, unsigned long *lun_bitmap
)
2007 struct nvm_tgt_dev
*dev
= pblk
->dev
;
2008 struct nvm_geo
*geo
= &dev
->geo
;
2009 struct pblk_lun
*rlun
;
2010 int num_lun
= geo
->all_luns
;
2013 while ((bit
= find_next_bit(lun_bitmap
, num_lun
, bit
+ 1)) < num_lun
) {
2014 rlun
= &pblk
->luns
[bit
];
2019 void pblk_update_map(struct pblk
*pblk
, sector_t lba
, struct ppa_addr ppa
)
2021 struct ppa_addr ppa_l2p
;
2023 /* logic error: lba out-of-bounds. Ignore update */
2024 if (!(lba
< pblk
->rl
.nr_secs
)) {
2025 WARN(1, "pblk: corrupted L2P map request\n");
2029 spin_lock(&pblk
->trans_lock
);
2030 ppa_l2p
= pblk_trans_map_get(pblk
, lba
);
2032 if (!pblk_addr_in_cache(ppa_l2p
) && !pblk_ppa_empty(ppa_l2p
))
2033 pblk_map_invalidate(pblk
, ppa_l2p
);
2035 pblk_trans_map_set(pblk
, lba
, ppa
);
2036 spin_unlock(&pblk
->trans_lock
);
2039 void pblk_update_map_cache(struct pblk
*pblk
, sector_t lba
, struct ppa_addr ppa
)
2042 #ifdef CONFIG_NVM_PBLK_DEBUG
2043 /* Callers must ensure that the ppa points to a cache address */
2044 BUG_ON(!pblk_addr_in_cache(ppa
));
2045 BUG_ON(pblk_rb_pos_oob(&pblk
->rwb
, pblk_addr_to_cacheline(ppa
)));
2048 pblk_update_map(pblk
, lba
, ppa
);
2051 int pblk_update_map_gc(struct pblk
*pblk
, sector_t lba
, struct ppa_addr ppa_new
,
2052 struct pblk_line
*gc_line
, u64 paddr_gc
)
2054 struct ppa_addr ppa_l2p
, ppa_gc
;
2057 #ifdef CONFIG_NVM_PBLK_DEBUG
2058 /* Callers must ensure that the ppa points to a cache address */
2059 BUG_ON(!pblk_addr_in_cache(ppa_new
));
2060 BUG_ON(pblk_rb_pos_oob(&pblk
->rwb
, pblk_addr_to_cacheline(ppa_new
)));
2063 /* logic error: lba out-of-bounds. Ignore update */
2064 if (!(lba
< pblk
->rl
.nr_secs
)) {
2065 WARN(1, "pblk: corrupted L2P map request\n");
2069 spin_lock(&pblk
->trans_lock
);
2070 ppa_l2p
= pblk_trans_map_get(pblk
, lba
);
2071 ppa_gc
= addr_to_gen_ppa(pblk
, paddr_gc
, gc_line
->id
);
2073 if (!pblk_ppa_comp(ppa_l2p
, ppa_gc
)) {
2074 spin_lock(&gc_line
->lock
);
2075 WARN(!test_bit(paddr_gc
, gc_line
->invalid_bitmap
),
2076 "pblk: corrupted GC update");
2077 spin_unlock(&gc_line
->lock
);
2083 pblk_trans_map_set(pblk
, lba
, ppa_new
);
2085 spin_unlock(&pblk
->trans_lock
);
2089 void pblk_update_map_dev(struct pblk
*pblk
, sector_t lba
,
2090 struct ppa_addr ppa_mapped
, struct ppa_addr ppa_cache
)
2092 struct ppa_addr ppa_l2p
;
2094 #ifdef CONFIG_NVM_PBLK_DEBUG
2095 /* Callers must ensure that the ppa points to a device address */
2096 BUG_ON(pblk_addr_in_cache(ppa_mapped
));
2098 /* Invalidate and discard padded entries */
2099 if (lba
== ADDR_EMPTY
) {
2100 atomic64_inc(&pblk
->pad_wa
);
2101 #ifdef CONFIG_NVM_PBLK_DEBUG
2102 atomic_long_inc(&pblk
->padded_wb
);
2104 if (!pblk_ppa_empty(ppa_mapped
))
2105 pblk_map_invalidate(pblk
, ppa_mapped
);
2109 /* logic error: lba out-of-bounds. Ignore update */
2110 if (!(lba
< pblk
->rl
.nr_secs
)) {
2111 WARN(1, "pblk: corrupted L2P map request\n");
2115 spin_lock(&pblk
->trans_lock
);
2116 ppa_l2p
= pblk_trans_map_get(pblk
, lba
);
2118 /* Do not update L2P if the cacheline has been updated. In this case,
2119 * the mapped ppa must be invalidated
2121 if (!pblk_ppa_comp(ppa_l2p
, ppa_cache
)) {
2122 if (!pblk_ppa_empty(ppa_mapped
))
2123 pblk_map_invalidate(pblk
, ppa_mapped
);
2127 #ifdef CONFIG_NVM_PBLK_DEBUG
2128 WARN_ON(!pblk_addr_in_cache(ppa_l2p
) && !pblk_ppa_empty(ppa_l2p
));
2131 pblk_trans_map_set(pblk
, lba
, ppa_mapped
);
2133 spin_unlock(&pblk
->trans_lock
);
2136 void pblk_lookup_l2p_seq(struct pblk
*pblk
, struct ppa_addr
*ppas
,
2137 sector_t blba
, int nr_secs
)
2141 spin_lock(&pblk
->trans_lock
);
2142 for (i
= 0; i
< nr_secs
; i
++) {
2143 struct ppa_addr ppa
;
2145 ppa
= ppas
[i
] = pblk_trans_map_get(pblk
, blba
+ i
);
2147 /* If the L2P entry maps to a line, the reference is valid */
2148 if (!pblk_ppa_empty(ppa
) && !pblk_addr_in_cache(ppa
)) {
2149 struct pblk_line
*line
= pblk_ppa_to_line(pblk
, ppa
);
2151 kref_get(&line
->ref
);
2154 spin_unlock(&pblk
->trans_lock
);
2157 void pblk_lookup_l2p_rand(struct pblk
*pblk
, struct ppa_addr
*ppas
,
2158 u64
*lba_list
, int nr_secs
)
2163 spin_lock(&pblk
->trans_lock
);
2164 for (i
= 0; i
< nr_secs
; i
++) {
2166 if (lba
!= ADDR_EMPTY
) {
2167 /* logic error: lba out-of-bounds. Ignore update */
2168 if (!(lba
< pblk
->rl
.nr_secs
)) {
2169 WARN(1, "pblk: corrupted L2P map request\n");
2172 ppas
[i
] = pblk_trans_map_get(pblk
, lba
);
2175 spin_unlock(&pblk
->trans_lock
);
2178 void *pblk_get_meta_for_writes(struct pblk
*pblk
, struct nvm_rq
*rqd
)
2182 if (pblk_is_oob_meta_supported(pblk
)) {
2183 /* Just use OOB metadata buffer as always */
2184 buffer
= rqd
->meta_list
;
2186 /* We need to reuse last page of request (packed metadata)
2187 * in similar way as traditional oob metadata
2189 buffer
= page_to_virt(
2190 rqd
->bio
->bi_io_vec
[rqd
->bio
->bi_vcnt
- 1].bv_page
);
2196 void pblk_get_packed_meta(struct pblk
*pblk
, struct nvm_rq
*rqd
)
2198 void *meta_list
= rqd
->meta_list
;
2202 if (pblk_is_oob_meta_supported(pblk
))
2205 page
= page_to_virt(rqd
->bio
->bi_io_vec
[rqd
->bio
->bi_vcnt
- 1].bv_page
);
2206 /* We need to fill oob meta buffer with data from packed metadata */
2207 for (; i
< rqd
->nr_ppas
; i
++)
2208 memcpy(pblk_get_meta(pblk
, meta_list
, i
),
2209 page
+ (i
* sizeof(struct pblk_sec_meta
)),
2210 sizeof(struct pblk_sec_meta
));