2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * pblk-read.c - pblk's read path
21 * There is no guarantee that the value read from cache has not been updated and
22 * resides at another location in the cache. We guarantee though that if the
23 * value is read from the cache, it belongs to the mapped lba. In order to
24 * guarantee and order between writes and reads are ordered, a flush must be
27 static int pblk_read_from_cache(struct pblk
*pblk
, struct bio
*bio
,
28 sector_t lba
, struct ppa_addr ppa
,
29 int bio_iter
, bool advanced_bio
)
31 #ifdef CONFIG_NVM_DEBUG
32 /* Callers must ensure that the ppa points to a cache address */
33 BUG_ON(pblk_ppa_empty(ppa
));
34 BUG_ON(!pblk_addr_in_cache(ppa
));
37 return pblk_rb_copy_to_bio(&pblk
->rwb
, bio
, lba
, ppa
,
38 bio_iter
, advanced_bio
);
41 static void pblk_read_ppalist_rq(struct pblk
*pblk
, struct nvm_rq
*rqd
,
42 struct bio
*bio
, sector_t blba
,
43 unsigned long *read_bitmap
)
45 struct pblk_sec_meta
*meta_list
= rqd
->meta_list
;
46 struct ppa_addr ppas
[PBLK_MAX_REQ_ADDRS
];
47 int nr_secs
= rqd
->nr_ppas
;
48 bool advanced_bio
= false;
51 pblk_lookup_l2p_seq(pblk
, ppas
, blba
, nr_secs
);
53 for (i
= 0; i
< nr_secs
; i
++) {
54 struct ppa_addr p
= ppas
[i
];
55 sector_t lba
= blba
+ i
;
58 if (pblk_ppa_empty(p
)) {
59 WARN_ON(test_and_set_bit(i
, read_bitmap
));
60 meta_list
[i
].lba
= cpu_to_le64(ADDR_EMPTY
);
62 if (unlikely(!advanced_bio
)) {
63 bio_advance(bio
, (i
) * PBLK_EXPOSED_PAGE_SIZE
);
70 /* Try to read from write buffer. The address is later checked
71 * on the write buffer to prevent retrieving overwritten data.
73 if (pblk_addr_in_cache(p
)) {
74 if (!pblk_read_from_cache(pblk
, bio
, lba
, p
, i
,
76 pblk_lookup_l2p_seq(pblk
, &p
, lba
, 1);
79 WARN_ON(test_and_set_bit(i
, read_bitmap
));
80 meta_list
[i
].lba
= cpu_to_le64(lba
);
82 #ifdef CONFIG_NVM_DEBUG
83 atomic_long_inc(&pblk
->cache_reads
);
86 /* Read from media non-cached sectors */
87 rqd
->ppa_list
[j
++] = p
;
92 bio_advance(bio
, PBLK_EXPOSED_PAGE_SIZE
);
95 if (pblk_io_aligned(pblk
, nr_secs
))
96 rqd
->flags
= pblk_set_read_mode(pblk
, PBLK_READ_SEQUENTIAL
);
98 rqd
->flags
= pblk_set_read_mode(pblk
, PBLK_READ_RANDOM
);
100 #ifdef CONFIG_NVM_DEBUG
101 atomic_long_add(nr_secs
, &pblk
->inflight_reads
);
106 static void pblk_read_check_seq(struct pblk
*pblk
, struct nvm_rq
*rqd
,
109 struct pblk_sec_meta
*meta_lba_list
= rqd
->meta_list
;
110 int nr_lbas
= rqd
->nr_ppas
;
113 for (i
= 0; i
< nr_lbas
; i
++) {
114 u64 lba
= le64_to_cpu(meta_lba_list
[i
].lba
);
116 if (lba
== ADDR_EMPTY
)
119 if (lba
!= blba
+ i
) {
120 #ifdef CONFIG_NVM_DEBUG
123 p
= (nr_lbas
== 1) ? &rqd
->ppa_list
[i
] : &rqd
->ppa_addr
;
124 print_ppa(&pblk
->dev
->geo
, p
, "seq", i
);
126 pr_err("pblk: corrupted read LBA (%llu/%llu)\n",
134 * There can be holes in the lba list.
136 static void pblk_read_check_rand(struct pblk
*pblk
, struct nvm_rq
*rqd
,
137 u64
*lba_list
, int nr_lbas
)
139 struct pblk_sec_meta
*meta_lba_list
= rqd
->meta_list
;
142 for (i
= 0, j
= 0; i
< nr_lbas
; i
++) {
143 u64 lba
= lba_list
[i
];
146 if (lba
== ADDR_EMPTY
)
149 meta_lba
= le64_to_cpu(meta_lba_list
[j
].lba
);
151 if (lba
!= meta_lba
) {
152 #ifdef CONFIG_NVM_DEBUG
154 int nr_ppas
= rqd
->nr_ppas
;
156 p
= (nr_ppas
== 1) ? &rqd
->ppa_list
[j
] : &rqd
->ppa_addr
;
157 print_ppa(&pblk
->dev
->geo
, p
, "seq", j
);
159 pr_err("pblk: corrupted read LBA (%llu/%llu)\n",
167 WARN_ONCE(j
!= rqd
->nr_ppas
, "pblk: corrupted random request\n");
170 static void pblk_read_put_rqd_kref(struct pblk
*pblk
, struct nvm_rq
*rqd
)
172 struct ppa_addr
*ppa_list
;
175 ppa_list
= (rqd
->nr_ppas
> 1) ? rqd
->ppa_list
: &rqd
->ppa_addr
;
177 for (i
= 0; i
< rqd
->nr_ppas
; i
++) {
178 struct ppa_addr ppa
= ppa_list
[i
];
179 struct pblk_line
*line
;
181 line
= &pblk
->lines
[pblk_ppa_to_line(ppa
)];
182 kref_put(&line
->ref
, pblk_line_put_wq
);
186 static void pblk_end_user_read(struct bio
*bio
)
188 #ifdef CONFIG_NVM_DEBUG
189 WARN_ONCE(bio
->bi_status
, "pblk: corrupted read bio\n");
194 static void __pblk_end_io_read(struct pblk
*pblk
, struct nvm_rq
*rqd
,
197 struct nvm_tgt_dev
*dev
= pblk
->dev
;
198 struct pblk_g_ctx
*r_ctx
= nvm_rq_to_pdu(rqd
);
199 struct bio
*int_bio
= rqd
->bio
;
200 unsigned long start_time
= r_ctx
->start_time
;
202 generic_end_io_acct(dev
->q
, READ
, &pblk
->disk
->part0
, start_time
);
205 pblk_log_read_err(pblk
, rqd
);
207 pblk_read_check_seq(pblk
, rqd
, r_ctx
->lba
);
213 pblk_read_put_rqd_kref(pblk
, rqd
);
215 #ifdef CONFIG_NVM_DEBUG
216 atomic_long_add(rqd
->nr_ppas
, &pblk
->sync_reads
);
217 atomic_long_sub(rqd
->nr_ppas
, &pblk
->inflight_reads
);
220 pblk_free_rqd(pblk
, rqd
, PBLK_READ
);
221 atomic_dec(&pblk
->inflight_io
);
224 static void pblk_end_io_read(struct nvm_rq
*rqd
)
226 struct pblk
*pblk
= rqd
->private;
227 struct pblk_g_ctx
*r_ctx
= nvm_rq_to_pdu(rqd
);
228 struct bio
*bio
= (struct bio
*)r_ctx
->private;
230 pblk_end_user_read(bio
);
231 __pblk_end_io_read(pblk
, rqd
, true);
234 static int pblk_partial_read(struct pblk
*pblk
, struct nvm_rq
*rqd
,
235 struct bio
*orig_bio
, unsigned int bio_init_idx
,
236 unsigned long *read_bitmap
)
238 struct pblk_sec_meta
*meta_list
= rqd
->meta_list
;
240 struct bio_vec src_bv
, dst_bv
;
241 void *ppa_ptr
= NULL
;
243 dma_addr_t dma_ppa_list
= 0;
244 __le64
*lba_list_mem
, *lba_list_media
;
245 int nr_secs
= rqd
->nr_ppas
;
246 int nr_holes
= nr_secs
- bitmap_weight(read_bitmap
, nr_secs
);
249 /* Re-use allocated memory for intermediate lbas */
250 lba_list_mem
= (((void *)rqd
->ppa_list
) + pblk_dma_ppa_size
);
251 lba_list_media
= (((void *)rqd
->ppa_list
) + 2 * pblk_dma_ppa_size
);
253 new_bio
= bio_alloc(GFP_KERNEL
, nr_holes
);
255 if (pblk_bio_add_pages(pblk
, new_bio
, GFP_KERNEL
, nr_holes
))
258 if (nr_holes
!= new_bio
->bi_vcnt
) {
259 pr_err("pblk: malformed bio\n");
263 for (i
= 0; i
< nr_secs
; i
++)
264 lba_list_mem
[i
] = meta_list
[i
].lba
;
266 new_bio
->bi_iter
.bi_sector
= 0; /* internal bio */
267 bio_set_op_attrs(new_bio
, REQ_OP_READ
, 0);
270 rqd
->nr_ppas
= nr_holes
;
271 rqd
->flags
= pblk_set_read_mode(pblk
, PBLK_READ_RANDOM
);
273 if (unlikely(nr_holes
== 1)) {
274 ppa_ptr
= rqd
->ppa_list
;
275 dma_ppa_list
= rqd
->dma_ppa_list
;
276 rqd
->ppa_addr
= rqd
->ppa_list
[0];
279 ret
= pblk_submit_io_sync(pblk
, rqd
);
282 pr_err("pblk: sync read IO submission failed\n");
287 atomic_long_inc(&pblk
->read_failed
);
288 #ifdef CONFIG_NVM_DEBUG
289 pblk_print_failed_rqd(pblk
, rqd
, rqd
->error
);
293 if (unlikely(nr_holes
== 1)) {
297 rqd
->ppa_list
= ppa_ptr
;
298 rqd
->dma_ppa_list
= dma_ppa_list
;
299 rqd
->ppa_list
[0] = ppa
;
302 for (i
= 0; i
< nr_secs
; i
++) {
303 lba_list_media
[i
] = meta_list
[i
].lba
;
304 meta_list
[i
].lba
= lba_list_mem
[i
];
307 /* Fill the holes in the original bio */
309 hole
= find_first_zero_bit(read_bitmap
, nr_secs
);
311 int line_id
= pblk_ppa_to_line(rqd
->ppa_list
[i
]);
312 struct pblk_line
*line
= &pblk
->lines
[line_id
];
314 kref_put(&line
->ref
, pblk_line_put
);
316 meta_list
[hole
].lba
= lba_list_media
[i
];
318 src_bv
= new_bio
->bi_io_vec
[i
++];
319 dst_bv
= orig_bio
->bi_io_vec
[bio_init_idx
+ hole
];
321 src_p
= kmap_atomic(src_bv
.bv_page
);
322 dst_p
= kmap_atomic(dst_bv
.bv_page
);
324 memcpy(dst_p
+ dst_bv
.bv_offset
,
325 src_p
+ src_bv
.bv_offset
,
326 PBLK_EXPOSED_PAGE_SIZE
);
328 kunmap_atomic(src_p
);
329 kunmap_atomic(dst_p
);
331 mempool_free(src_bv
.bv_page
, &pblk
->page_bio_pool
);
333 hole
= find_next_zero_bit(read_bitmap
, nr_secs
, hole
+ 1);
334 } while (hole
< nr_secs
);
338 /* restore original request */
340 rqd
->nr_ppas
= nr_secs
;
342 __pblk_end_io_read(pblk
, rqd
, false);
346 /* Free allocated pages in new bio */
347 pblk_bio_free_pages(pblk
, new_bio
, 0, new_bio
->bi_vcnt
);
349 pr_err("pblk: failed to perform partial read\n");
350 __pblk_end_io_read(pblk
, rqd
, false);
354 static void pblk_read_rq(struct pblk
*pblk
, struct nvm_rq
*rqd
, struct bio
*bio
,
355 sector_t lba
, unsigned long *read_bitmap
)
357 struct pblk_sec_meta
*meta_list
= rqd
->meta_list
;
360 pblk_lookup_l2p_seq(pblk
, &ppa
, lba
, 1);
362 #ifdef CONFIG_NVM_DEBUG
363 atomic_long_inc(&pblk
->inflight_reads
);
367 if (pblk_ppa_empty(ppa
)) {
368 WARN_ON(test_and_set_bit(0, read_bitmap
));
369 meta_list
[0].lba
= cpu_to_le64(ADDR_EMPTY
);
373 /* Try to read from write buffer. The address is later checked on the
374 * write buffer to prevent retrieving overwritten data.
376 if (pblk_addr_in_cache(ppa
)) {
377 if (!pblk_read_from_cache(pblk
, bio
, lba
, ppa
, 0, 1)) {
378 pblk_lookup_l2p_seq(pblk
, &ppa
, lba
, 1);
382 WARN_ON(test_and_set_bit(0, read_bitmap
));
383 meta_list
[0].lba
= cpu_to_le64(lba
);
385 #ifdef CONFIG_NVM_DEBUG
386 atomic_long_inc(&pblk
->cache_reads
);
392 rqd
->flags
= pblk_set_read_mode(pblk
, PBLK_READ_RANDOM
);
395 int pblk_submit_read(struct pblk
*pblk
, struct bio
*bio
)
397 struct nvm_tgt_dev
*dev
= pblk
->dev
;
398 struct request_queue
*q
= dev
->q
;
399 sector_t blba
= pblk_get_lba(bio
);
400 unsigned int nr_secs
= pblk_get_secs(bio
);
401 struct pblk_g_ctx
*r_ctx
;
403 unsigned int bio_init_idx
;
404 unsigned long read_bitmap
; /* Max 64 ppas per request */
405 int ret
= NVM_IO_ERR
;
407 /* logic error: lba out-of-bounds. Ignore read request */
408 if (blba
>= pblk
->rl
.nr_secs
|| nr_secs
> PBLK_MAX_REQ_ADDRS
) {
409 WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n",
410 (unsigned long long)blba
, nr_secs
);
414 generic_start_io_acct(q
, READ
, bio_sectors(bio
), &pblk
->disk
->part0
);
416 bitmap_zero(&read_bitmap
, nr_secs
);
418 rqd
= pblk_alloc_rqd(pblk
, PBLK_READ
);
420 rqd
->opcode
= NVM_OP_PREAD
;
421 rqd
->nr_ppas
= nr_secs
;
422 rqd
->bio
= NULL
; /* cloned bio if needed */
424 rqd
->end_io
= pblk_end_io_read
;
426 r_ctx
= nvm_rq_to_pdu(rqd
);
427 r_ctx
->start_time
= jiffies
;
429 r_ctx
->private = bio
; /* original bio */
431 /* Save the index for this bio's start. This is needed in case
432 * we need to fill a partial read.
434 bio_init_idx
= pblk_get_bi_idx(bio
);
436 rqd
->meta_list
= nvm_dev_dma_alloc(dev
->parent
, GFP_KERNEL
,
437 &rqd
->dma_meta_list
);
438 if (!rqd
->meta_list
) {
439 pr_err("pblk: not able to allocate ppa list\n");
444 rqd
->ppa_list
= rqd
->meta_list
+ pblk_dma_meta_size
;
445 rqd
->dma_ppa_list
= rqd
->dma_meta_list
+ pblk_dma_meta_size
;
447 pblk_read_ppalist_rq(pblk
, rqd
, bio
, blba
, &read_bitmap
);
449 pblk_read_rq(pblk
, rqd
, bio
, blba
, &read_bitmap
);
452 if (bitmap_full(&read_bitmap
, nr_secs
)) {
453 atomic_inc(&pblk
->inflight_io
);
454 __pblk_end_io_read(pblk
, rqd
, false);
458 /* All sectors are to be read from the device */
459 if (bitmap_empty(&read_bitmap
, rqd
->nr_ppas
)) {
460 struct bio
*int_bio
= NULL
;
462 /* Clone read bio to deal with read errors internally */
463 int_bio
= bio_clone_fast(bio
, GFP_KERNEL
, &pblk_bio_set
);
465 pr_err("pblk: could not clone read bio\n");
471 if (pblk_submit_io(pblk
, rqd
)) {
472 pr_err("pblk: read IO submission failed\n");
480 /* The read bio request could be partially filled by the write buffer,
481 * but there are some holes that need to be read from the drive.
483 return pblk_partial_read(pblk
, rqd
, bio
, bio_init_idx
, &read_bitmap
);
486 pblk_free_rqd(pblk
, rqd
, PBLK_READ
);
489 __pblk_end_io_read(pblk
, rqd
, false);
493 static int read_ppalist_rq_gc(struct pblk
*pblk
, struct nvm_rq
*rqd
,
494 struct pblk_line
*line
, u64
*lba_list
,
495 u64
*paddr_list_gc
, unsigned int nr_secs
)
497 struct ppa_addr ppa_list_l2p
[PBLK_MAX_REQ_ADDRS
];
498 struct ppa_addr ppa_gc
;
502 pblk_lookup_l2p_rand(pblk
, ppa_list_l2p
, lba_list
, nr_secs
);
504 for (i
= 0; i
< nr_secs
; i
++) {
505 if (lba_list
[i
] == ADDR_EMPTY
)
508 ppa_gc
= addr_to_gen_ppa(pblk
, paddr_list_gc
[i
], line
->id
);
509 if (!pblk_ppa_comp(ppa_list_l2p
[i
], ppa_gc
)) {
510 paddr_list_gc
[i
] = lba_list
[i
] = ADDR_EMPTY
;
514 rqd
->ppa_list
[valid_secs
++] = ppa_list_l2p
[i
];
517 #ifdef CONFIG_NVM_DEBUG
518 atomic_long_add(valid_secs
, &pblk
->inflight_reads
);
524 static int read_rq_gc(struct pblk
*pblk
, struct nvm_rq
*rqd
,
525 struct pblk_line
*line
, sector_t lba
,
528 struct ppa_addr ppa_l2p
, ppa_gc
;
531 if (lba
== ADDR_EMPTY
)
534 /* logic error: lba out-of-bounds */
535 if (lba
>= pblk
->rl
.nr_secs
) {
536 WARN(1, "pblk: read lba out of bounds\n");
540 spin_lock(&pblk
->trans_lock
);
541 ppa_l2p
= pblk_trans_map_get(pblk
, lba
);
542 spin_unlock(&pblk
->trans_lock
);
544 ppa_gc
= addr_to_gen_ppa(pblk
, paddr_gc
, line
->id
);
545 if (!pblk_ppa_comp(ppa_l2p
, ppa_gc
))
548 rqd
->ppa_addr
= ppa_l2p
;
551 #ifdef CONFIG_NVM_DEBUG
552 atomic_long_inc(&pblk
->inflight_reads
);
559 int pblk_submit_read_gc(struct pblk
*pblk
, struct pblk_gc_rq
*gc_rq
)
561 struct nvm_tgt_dev
*dev
= pblk
->dev
;
562 struct nvm_geo
*geo
= &dev
->geo
;
568 memset(&rqd
, 0, sizeof(struct nvm_rq
));
570 rqd
.meta_list
= nvm_dev_dma_alloc(dev
->parent
, GFP_KERNEL
,
575 if (gc_rq
->nr_secs
> 1) {
576 rqd
.ppa_list
= rqd
.meta_list
+ pblk_dma_meta_size
;
577 rqd
.dma_ppa_list
= rqd
.dma_meta_list
+ pblk_dma_meta_size
;
579 gc_rq
->secs_to_gc
= read_ppalist_rq_gc(pblk
, &rqd
, gc_rq
->line
,
583 if (gc_rq
->secs_to_gc
== 1)
584 rqd
.ppa_addr
= rqd
.ppa_list
[0];
586 gc_rq
->secs_to_gc
= read_rq_gc(pblk
, &rqd
, gc_rq
->line
,
588 gc_rq
->paddr_list
[0]);
591 if (!(gc_rq
->secs_to_gc
))
594 data_len
= (gc_rq
->secs_to_gc
) * geo
->csecs
;
595 bio
= pblk_bio_map_addr(pblk
, gc_rq
->data
, gc_rq
->secs_to_gc
, data_len
,
596 PBLK_VMALLOC_META
, GFP_KERNEL
);
598 pr_err("pblk: could not allocate GC bio (%lu)\n", PTR_ERR(bio
));
602 bio
->bi_iter
.bi_sector
= 0; /* internal bio */
603 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
605 rqd
.opcode
= NVM_OP_PREAD
;
606 rqd
.nr_ppas
= gc_rq
->secs_to_gc
;
607 rqd
.flags
= pblk_set_read_mode(pblk
, PBLK_READ_RANDOM
);
610 if (pblk_submit_io_sync(pblk
, &rqd
)) {
612 pr_err("pblk: GC read request failed\n");
616 pblk_read_check_rand(pblk
, &rqd
, gc_rq
->lba_list
, gc_rq
->nr_secs
);
618 atomic_dec(&pblk
->inflight_io
);
621 atomic_long_inc(&pblk
->read_failed_gc
);
622 #ifdef CONFIG_NVM_DEBUG
623 pblk_print_failed_rqd(pblk
, &rqd
, rqd
.error
);
627 #ifdef CONFIG_NVM_DEBUG
628 atomic_long_add(gc_rq
->secs_to_gc
, &pblk
->sync_reads
);
629 atomic_long_add(gc_rq
->secs_to_gc
, &pblk
->recov_gc_reads
);
630 atomic_long_sub(gc_rq
->secs_to_gc
, &pblk
->inflight_reads
);
634 nvm_dev_dma_free(dev
->parent
, rqd
.meta_list
, rqd
.dma_meta_list
);
640 nvm_dev_dma_free(dev
->parent
, rqd
.meta_list
, rqd
.dma_meta_list
);