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-read.c - pblk's read path
22 * There is no guarantee that the value read from cache has not been updated and
23 * resides at another location in the cache. We guarantee though that if the
24 * value is read from the cache, it belongs to the mapped lba. In order to
25 * guarantee and order between writes and reads are ordered, a flush must be
28 static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio,
29 sector_t lba, struct ppa_addr ppa,
30 int bio_iter, bool advanced_bio)
32 #ifdef CONFIG_NVM_PBLK_DEBUG
33 /* Callers must ensure that the ppa points to a cache address */
34 BUG_ON(pblk_ppa_empty(ppa));
35 BUG_ON(!pblk_addr_in_cache(ppa));
38 return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa,
39 bio_iter, advanced_bio);
42 static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd,
43 struct bio *bio, sector_t blba,
44 unsigned long *read_bitmap)
46 void *meta_list = rqd->meta_list;
47 struct ppa_addr ppas[NVM_MAX_VLBA];
48 int nr_secs = rqd->nr_ppas;
49 bool advanced_bio = false;
52 pblk_lookup_l2p_seq(pblk, ppas, blba, nr_secs);
54 for (i = 0; i < nr_secs; i++) {
55 struct ppa_addr p = ppas[i];
56 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
57 sector_t lba = blba + i;
60 if (pblk_ppa_empty(p)) {
61 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
63 WARN_ON(test_and_set_bit(i, read_bitmap));
64 meta->lba = addr_empty;
66 if (unlikely(!advanced_bio)) {
67 bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE);
74 /* Try to read from write buffer. The address is later checked
75 * on the write buffer to prevent retrieving overwritten data.
77 if (pblk_addr_in_cache(p)) {
78 if (!pblk_read_from_cache(pblk, bio, lba, p, i,
80 pblk_lookup_l2p_seq(pblk, &p, lba, 1);
83 WARN_ON(test_and_set_bit(i, read_bitmap));
84 meta->lba = cpu_to_le64(lba);
86 #ifdef CONFIG_NVM_PBLK_DEBUG
87 atomic_long_inc(&pblk->cache_reads);
90 /* Read from media non-cached sectors */
91 rqd->ppa_list[j++] = p;
96 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
99 if (pblk_io_aligned(pblk, nr_secs))
102 #ifdef CONFIG_NVM_PBLK_DEBUG
103 atomic_long_add(nr_secs, &pblk->inflight_reads);
108 static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd,
111 void *meta_list = rqd->meta_list;
112 int nr_lbas = rqd->nr_ppas;
115 if (!pblk_is_oob_meta_supported(pblk))
118 for (i = 0; i < nr_lbas; i++) {
119 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
120 u64 lba = le64_to_cpu(meta->lba);
122 if (lba == ADDR_EMPTY)
125 if (lba != blba + i) {
126 #ifdef CONFIG_NVM_PBLK_DEBUG
127 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
129 print_ppa(pblk, &ppa_list[i], "seq", i);
131 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
139 * There can be holes in the lba list.
141 static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd,
142 u64 *lba_list, int nr_lbas)
144 void *meta_lba_list = rqd->meta_list;
147 if (!pblk_is_oob_meta_supported(pblk))
150 for (i = 0, j = 0; i < nr_lbas; i++) {
151 struct pblk_sec_meta *meta = pblk_get_meta(pblk,
153 u64 lba = lba_list[i];
156 if (lba == ADDR_EMPTY)
159 meta_lba = le64_to_cpu(meta->lba);
161 if (lba != meta_lba) {
162 #ifdef CONFIG_NVM_PBLK_DEBUG
163 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
165 print_ppa(pblk, &ppa_list[j], "rnd", j);
167 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
175 WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n");
178 static void pblk_end_user_read(struct bio *bio)
180 #ifdef CONFIG_NVM_PBLK_DEBUG
181 WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n");
186 static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd,
189 struct nvm_tgt_dev *dev = pblk->dev;
190 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
191 struct bio *int_bio = rqd->bio;
192 unsigned long start_time = r_ctx->start_time;
194 generic_end_io_acct(dev->q, REQ_OP_READ, &pblk->disk->part0, start_time);
197 pblk_log_read_err(pblk, rqd);
199 pblk_read_check_seq(pblk, rqd, r_ctx->lba);
205 pblk_rq_to_line_put(pblk, rqd);
207 #ifdef CONFIG_NVM_PBLK_DEBUG
208 atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
209 atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
212 pblk_free_rqd(pblk, rqd, PBLK_READ);
213 atomic_dec(&pblk->inflight_io);
216 static void pblk_end_io_read(struct nvm_rq *rqd)
218 struct pblk *pblk = rqd->private;
219 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
220 struct bio *bio = (struct bio *)r_ctx->private;
222 pblk_end_user_read(bio);
223 __pblk_end_io_read(pblk, rqd, true);
226 static void pblk_end_partial_read(struct nvm_rq *rqd)
228 struct pblk *pblk = rqd->private;
229 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
230 struct pblk_pr_ctx *pr_ctx = r_ctx->private;
231 struct pblk_sec_meta *meta;
232 struct bio *new_bio = rqd->bio;
233 struct bio *bio = pr_ctx->orig_bio;
234 void *meta_list = rqd->meta_list;
235 unsigned long *read_bitmap = pr_ctx->bitmap;
236 struct bvec_iter orig_iter = BVEC_ITER_ALL_INIT;
237 struct bvec_iter new_iter = BVEC_ITER_ALL_INIT;
238 int nr_secs = pr_ctx->orig_nr_secs;
239 int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
243 if (unlikely(nr_holes == 1)) {
247 rqd->ppa_list = pr_ctx->ppa_ptr;
248 rqd->dma_ppa_list = pr_ctx->dma_ppa_list;
249 rqd->ppa_list[0] = ppa;
252 for (i = 0; i < nr_secs; i++) {
253 meta = pblk_get_meta(pblk, meta_list, i);
254 pr_ctx->lba_list_media[i] = le64_to_cpu(meta->lba);
255 meta->lba = cpu_to_le64(pr_ctx->lba_list_mem[i]);
258 /* Fill the holes in the original bio */
260 for (bit = 0; bit < nr_secs; bit++) {
261 if (!test_bit(bit, read_bitmap)) {
262 struct bio_vec dst_bv, src_bv;
263 struct pblk_line *line;
265 line = pblk_ppa_to_line(pblk, rqd->ppa_list[i]);
266 kref_put(&line->ref, pblk_line_put);
268 meta = pblk_get_meta(pblk, meta_list, bit);
269 meta->lba = cpu_to_le64(pr_ctx->lba_list_media[i]);
271 dst_bv = bio_iter_iovec(bio, orig_iter);
272 src_bv = bio_iter_iovec(new_bio, new_iter);
274 src_p = kmap_atomic(src_bv.bv_page);
275 dst_p = kmap_atomic(dst_bv.bv_page);
277 memcpy(dst_p + dst_bv.bv_offset,
278 src_p + src_bv.bv_offset,
279 PBLK_EXPOSED_PAGE_SIZE);
281 kunmap_atomic(src_p);
282 kunmap_atomic(dst_p);
284 flush_dcache_page(dst_bv.bv_page);
285 mempool_free(src_bv.bv_page, &pblk->page_bio_pool);
287 bio_advance_iter(new_bio, &new_iter,
288 PBLK_EXPOSED_PAGE_SIZE);
291 bio_advance_iter(bio, &orig_iter, PBLK_EXPOSED_PAGE_SIZE);
297 /* restore original request */
299 rqd->nr_ppas = nr_secs;
302 __pblk_end_io_read(pblk, rqd, false);
305 static int pblk_setup_partial_read(struct pblk *pblk, struct nvm_rq *rqd,
306 unsigned int bio_init_idx,
307 unsigned long *read_bitmap,
310 void *meta_list = rqd->meta_list;
311 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
312 struct pblk_pr_ctx *pr_ctx;
313 struct bio *new_bio, *bio = r_ctx->private;
314 int nr_secs = rqd->nr_ppas;
317 new_bio = bio_alloc(GFP_KERNEL, nr_holes);
319 if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
322 if (nr_holes != new_bio->bi_vcnt) {
323 WARN_ONCE(1, "pblk: malformed bio\n");
324 goto fail_free_pages;
327 pr_ctx = kzalloc(sizeof(struct pblk_pr_ctx), GFP_KERNEL);
329 goto fail_free_pages;
331 for (i = 0; i < nr_secs; i++) {
332 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
334 pr_ctx->lba_list_mem[i] = le64_to_cpu(meta->lba);
337 new_bio->bi_iter.bi_sector = 0; /* internal bio */
338 bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
341 rqd->nr_ppas = nr_holes;
343 pr_ctx->orig_bio = bio;
344 bitmap_copy(pr_ctx->bitmap, read_bitmap, NVM_MAX_VLBA);
345 pr_ctx->bio_init_idx = bio_init_idx;
346 pr_ctx->orig_nr_secs = nr_secs;
347 r_ctx->private = pr_ctx;
349 if (unlikely(nr_holes == 1)) {
350 pr_ctx->ppa_ptr = rqd->ppa_list;
351 pr_ctx->dma_ppa_list = rqd->dma_ppa_list;
352 rqd->ppa_addr = rqd->ppa_list[0];
357 pblk_bio_free_pages(pblk, new_bio, 0, new_bio->bi_vcnt);
364 static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
365 unsigned int bio_init_idx,
366 unsigned long *read_bitmap, int nr_secs)
371 nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
373 if (pblk_setup_partial_read(pblk, rqd, bio_init_idx, read_bitmap,
377 rqd->end_io = pblk_end_partial_read;
379 ret = pblk_submit_io(pblk, rqd);
382 pblk_err(pblk, "partial read IO submission failed\n");
389 pblk_err(pblk, "failed to perform partial read\n");
391 /* Free allocated pages in new bio */
392 pblk_bio_free_pages(pblk, rqd->bio, 0, rqd->bio->bi_vcnt);
393 __pblk_end_io_read(pblk, rqd, false);
397 static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio,
398 sector_t lba, unsigned long *read_bitmap)
400 struct pblk_sec_meta *meta = pblk_get_meta(pblk, rqd->meta_list, 0);
403 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
405 #ifdef CONFIG_NVM_PBLK_DEBUG
406 atomic_long_inc(&pblk->inflight_reads);
410 if (pblk_ppa_empty(ppa)) {
411 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
413 WARN_ON(test_and_set_bit(0, read_bitmap));
414 meta->lba = addr_empty;
418 /* Try to read from write buffer. The address is later checked on the
419 * write buffer to prevent retrieving overwritten data.
421 if (pblk_addr_in_cache(ppa)) {
422 if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
423 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
427 WARN_ON(test_and_set_bit(0, read_bitmap));
428 meta->lba = cpu_to_le64(lba);
430 #ifdef CONFIG_NVM_PBLK_DEBUG
431 atomic_long_inc(&pblk->cache_reads);
438 int pblk_submit_read(struct pblk *pblk, struct bio *bio)
440 struct nvm_tgt_dev *dev = pblk->dev;
441 struct request_queue *q = dev->q;
442 sector_t blba = pblk_get_lba(bio);
443 unsigned int nr_secs = pblk_get_secs(bio);
444 struct pblk_g_ctx *r_ctx;
446 unsigned int bio_init_idx;
447 DECLARE_BITMAP(read_bitmap, NVM_MAX_VLBA);
448 int ret = NVM_IO_ERR;
450 generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio),
453 bitmap_zero(read_bitmap, nr_secs);
455 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
457 rqd->opcode = NVM_OP_PREAD;
458 rqd->nr_ppas = nr_secs;
459 rqd->bio = NULL; /* cloned bio if needed */
461 rqd->end_io = pblk_end_io_read;
463 r_ctx = nvm_rq_to_pdu(rqd);
464 r_ctx->start_time = jiffies;
466 r_ctx->private = bio; /* original bio */
468 /* Save the index for this bio's start. This is needed in case
469 * we need to fill a partial read.
471 bio_init_idx = pblk_get_bi_idx(bio);
473 if (pblk_alloc_rqd_meta(pblk, rqd))
477 pblk_read_ppalist_rq(pblk, rqd, bio, blba, read_bitmap);
479 pblk_read_rq(pblk, rqd, bio, blba, read_bitmap);
481 if (bitmap_full(read_bitmap, nr_secs)) {
482 atomic_inc(&pblk->inflight_io);
483 __pblk_end_io_read(pblk, rqd, false);
487 /* All sectors are to be read from the device */
488 if (bitmap_empty(read_bitmap, rqd->nr_ppas)) {
489 struct bio *int_bio = NULL;
491 /* Clone read bio to deal with read errors internally */
492 int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set);
494 pblk_err(pblk, "could not clone read bio\n");
500 if (pblk_submit_io(pblk, rqd)) {
501 pblk_err(pblk, "read IO submission failed\n");
509 /* The read bio request could be partially filled by the write buffer,
510 * but there are some holes that need to be read from the drive.
512 ret = pblk_partial_read_bio(pblk, rqd, bio_init_idx, read_bitmap,
520 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
522 pblk_free_rqd(pblk, rqd, PBLK_READ);
525 __pblk_end_io_read(pblk, rqd, false);
529 static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
530 struct pblk_line *line, u64 *lba_list,
531 u64 *paddr_list_gc, unsigned int nr_secs)
533 struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA];
534 struct ppa_addr ppa_gc;
538 pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
540 for (i = 0; i < nr_secs; i++) {
541 if (lba_list[i] == ADDR_EMPTY)
544 ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
545 if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
546 paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
550 rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
553 #ifdef CONFIG_NVM_PBLK_DEBUG
554 atomic_long_add(valid_secs, &pblk->inflight_reads);
560 static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
561 struct pblk_line *line, sector_t lba,
564 struct ppa_addr ppa_l2p, ppa_gc;
567 if (lba == ADDR_EMPTY)
570 /* logic error: lba out-of-bounds */
571 if (lba >= pblk->rl.nr_secs) {
572 WARN(1, "pblk: read lba out of bounds\n");
576 spin_lock(&pblk->trans_lock);
577 ppa_l2p = pblk_trans_map_get(pblk, lba);
578 spin_unlock(&pblk->trans_lock);
580 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
581 if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
584 rqd->ppa_addr = ppa_l2p;
587 #ifdef CONFIG_NVM_PBLK_DEBUG
588 atomic_long_inc(&pblk->inflight_reads);
595 int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
597 struct nvm_tgt_dev *dev = pblk->dev;
598 struct nvm_geo *geo = &dev->geo;
604 memset(&rqd, 0, sizeof(struct nvm_rq));
606 ret = pblk_alloc_rqd_meta(pblk, &rqd);
610 if (gc_rq->nr_secs > 1) {
611 gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
615 if (gc_rq->secs_to_gc == 1)
616 rqd.ppa_addr = rqd.ppa_list[0];
618 gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
620 gc_rq->paddr_list[0]);
623 if (!(gc_rq->secs_to_gc))
626 data_len = (gc_rq->secs_to_gc) * geo->csecs;
627 bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
628 PBLK_VMALLOC_META, GFP_KERNEL);
630 pblk_err(pblk, "could not allocate GC bio (%lu)\n",
636 bio->bi_iter.bi_sector = 0; /* internal bio */
637 bio_set_op_attrs(bio, REQ_OP_READ, 0);
639 rqd.opcode = NVM_OP_PREAD;
640 rqd.nr_ppas = gc_rq->secs_to_gc;
643 if (pblk_submit_io_sync(pblk, &rqd)) {
645 pblk_err(pblk, "GC read request failed\n");
649 pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs);
651 atomic_dec(&pblk->inflight_io);
654 atomic_long_inc(&pblk->read_failed_gc);
655 #ifdef CONFIG_NVM_PBLK_DEBUG
656 pblk_print_failed_rqd(pblk, &rqd, rqd.error);
660 #ifdef CONFIG_NVM_PBLK_DEBUG
661 atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
662 atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
663 atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
667 pblk_free_rqd_meta(pblk, &rqd);
673 pblk_free_rqd_meta(pblk, &rqd);