blob: 829e92857289d2923c6a158124893290a72b1bf2 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-read.c - pblk's read path
16 */
17
18#include "pblk.h"
19
20/*
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
25 * issued.
26 */
27static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio,
28 sector_t lba, struct ppa_addr ppa,
Javier González75cb8e92017-07-28 15:13:16 +020029 int bio_iter, bool advanced_bio)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020030{
Matias Bjørling880eda52018-07-13 10:48:37 +020031#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020032 /* 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));
35#endif
36
Javier González75cb8e92017-07-28 15:13:16 +020037 return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa,
38 bio_iter, advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020039}
40
41static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier González87cc40b2018-06-01 16:41:11 +020042 struct bio *bio, sector_t blba,
43 unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020045 struct pblk_sec_meta *meta_list = rqd->meta_list;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +020046 struct ppa_addr ppas[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020047 int nr_secs = rqd->nr_ppas;
Javier González75cb8e92017-07-28 15:13:16 +020048 bool advanced_bio = false;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049 int i, j = 0;
50
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051 pblk_lookup_l2p_seq(pblk, ppas, blba, nr_secs);
52
53 for (i = 0; i < nr_secs; i++) {
54 struct ppa_addr p = ppas[i];
55 sector_t lba = blba + i;
56
57retry:
58 if (pblk_ppa_empty(p)) {
59 WARN_ON(test_and_set_bit(i, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020060 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier González75cb8e92017-07-28 15:13:16 +020061
62 if (unlikely(!advanced_bio)) {
63 bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE);
64 advanced_bio = true;
65 }
66
67 goto next;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 }
69
70 /* Try to read from write buffer. The address is later checked
71 * on the write buffer to prevent retrieving overwritten data.
72 */
73 if (pblk_addr_in_cache(p)) {
Javier González75cb8e92017-07-28 15:13:16 +020074 if (!pblk_read_from_cache(pblk, bio, lba, p, i,
75 advanced_bio)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020076 pblk_lookup_l2p_seq(pblk, &p, lba, 1);
77 goto retry;
78 }
79 WARN_ON(test_and_set_bit(i, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020080 meta_list[i].lba = cpu_to_le64(lba);
Javier González75cb8e92017-07-28 15:13:16 +020081 advanced_bio = true;
Matias Bjørling880eda52018-07-13 10:48:37 +020082#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezdb7ada32017-06-26 11:57:13 +020083 atomic_long_inc(&pblk->cache_reads);
84#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020085 } else {
86 /* Read from media non-cached sectors */
87 rqd->ppa_list[j++] = p;
88 }
89
Javier González75cb8e92017-07-28 15:13:16 +020090next:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 if (advanced_bio)
92 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
93 }
94
Javier Gonzálezf9c10152017-06-26 11:57:20 +020095 if (pblk_io_aligned(pblk, nr_secs))
Matias Bjørlingd7b68012018-10-09 13:11:32 +020096 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +020097
Matias Bjørling880eda52018-07-13 10:48:37 +020098#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020099 atomic_long_add(nr_secs, &pblk->inflight_reads);
100#endif
101}
102
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103
Javier González03a34b22018-06-01 15:04:18 +0200104static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd,
105 sector_t blba)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200106{
Javier González03a34b22018-06-01 15:04:18 +0200107 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
108 int nr_lbas = rqd->nr_ppas;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200109 int i;
110
111 for (i = 0; i < nr_lbas; i++) {
Javier González310df582018-06-01 15:04:17 +0200112 u64 lba = le64_to_cpu(meta_lba_list[i].lba);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200113
114 if (lba == ADDR_EMPTY)
115 continue;
116
Javier González03a34b22018-06-01 15:04:18 +0200117 if (lba != blba + i) {
Matias Bjørling880eda52018-07-13 10:48:37 +0200118#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd68a9342018-10-09 13:11:46 +0200119 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González03a34b22018-06-01 15:04:18 +0200120
Hans Holmbergd68a9342018-10-09 13:11:46 +0200121 print_ppa(pblk, &ppa_list[i], "seq", i);
Javier González03a34b22018-06-01 15:04:18 +0200122#endif
Matias Bjørling4e495a42018-07-13 10:48:42 +0200123 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
Javier González03a34b22018-06-01 15:04:18 +0200124 lba, (u64)blba + i);
125 WARN_ON(1);
126 }
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200127 }
128}
129
Javier González310df582018-06-01 15:04:17 +0200130/*
131 * There can be holes in the lba list.
132 */
Javier González03a34b22018-06-01 15:04:18 +0200133static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd,
134 u64 *lba_list, int nr_lbas)
Javier González310df582018-06-01 15:04:17 +0200135{
Javier González03a34b22018-06-01 15:04:18 +0200136 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
Javier González310df582018-06-01 15:04:17 +0200137 int i, j;
138
139 for (i = 0, j = 0; i < nr_lbas; i++) {
140 u64 lba = lba_list[i];
141 u64 meta_lba;
142
143 if (lba == ADDR_EMPTY)
144 continue;
145
Javier González03a34b22018-06-01 15:04:18 +0200146 meta_lba = le64_to_cpu(meta_lba_list[j].lba);
Javier González310df582018-06-01 15:04:17 +0200147
148 if (lba != meta_lba) {
Matias Bjørling880eda52018-07-13 10:48:37 +0200149#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd68a9342018-10-09 13:11:46 +0200150 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González03a34b22018-06-01 15:04:18 +0200151
Hans Holmbergea1d24b2018-10-09 13:11:49 +0200152 print_ppa(pblk, &ppa_list[j], "rnd", j);
Javier González03a34b22018-06-01 15:04:18 +0200153#endif
Matias Bjørling4e495a42018-07-13 10:48:42 +0200154 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
Hans Holmbergea1d24b2018-10-09 13:11:49 +0200155 meta_lba, lba);
Javier González310df582018-06-01 15:04:17 +0200156 WARN_ON(1);
157 }
Javier González03a34b22018-06-01 15:04:18 +0200158
159 j++;
Javier González310df582018-06-01 15:04:17 +0200160 }
Javier González03a34b22018-06-01 15:04:18 +0200161
162 WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n");
Javier González310df582018-06-01 15:04:17 +0200163}
164
Javier González7bd4d372017-10-13 14:46:23 +0200165static void pblk_end_user_read(struct bio *bio)
166{
Matias Bjørling880eda52018-07-13 10:48:37 +0200167#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González7bd4d372017-10-13 14:46:23 +0200168 WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n");
169#endif
170 bio_endio(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200171}
172
173static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd,
174 bool put_line)
175{
Javier González998ba622018-01-05 14:16:20 +0100176 struct nvm_tgt_dev *dev = pblk->dev;
Javier González084ec9b2017-06-26 16:27:13 -0600177 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
Javier González87cc40b2018-06-01 16:41:11 +0200178 struct bio *int_bio = rqd->bio;
Javier González998ba622018-01-05 14:16:20 +0100179 unsigned long start_time = r_ctx->start_time;
180
Michael Callahanddcf35d2018-07-18 04:47:39 -0700181 generic_end_io_acct(dev->q, REQ_OP_READ, &pblk->disk->part0, start_time);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200182
183 if (rqd->error)
184 pblk_log_read_err(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200185
Javier González03a34b22018-06-01 15:04:18 +0200186 pblk_read_check_seq(pblk, rqd, r_ctx->lba);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200187
Javier González87cc40b2018-06-01 16:41:11 +0200188 if (int_bio)
189 bio_put(int_bio);
Javier González084ec9b2017-06-26 16:27:13 -0600190
Javier González7bd4d372017-10-13 14:46:23 +0200191 if (put_line)
Matias Bjørlingae14cc02018-10-09 13:11:40 +0200192 pblk_rq_to_line_put(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200193
Matias Bjørling880eda52018-07-13 10:48:37 +0200194#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200195 atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
196 atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
197#endif
198
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200199 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier González588726d32017-06-26 11:57:29 +0200200 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201}
202
Javier González7bd4d372017-10-13 14:46:23 +0200203static void pblk_end_io_read(struct nvm_rq *rqd)
204{
205 struct pblk *pblk = rqd->private;
Javier González87cc40b2018-06-01 16:41:11 +0200206 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
207 struct bio *bio = (struct bio *)r_ctx->private;
Javier González7bd4d372017-10-13 14:46:23 +0200208
Javier González87cc40b2018-06-01 16:41:11 +0200209 pblk_end_user_read(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200210 __pblk_end_io_read(pblk, rqd, true);
211}
212
Heiner Litz11f6ad62018-07-13 10:48:44 +0200213static void pblk_end_partial_read(struct nvm_rq *rqd)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214{
Heiner Litz11f6ad62018-07-13 10:48:44 +0200215 struct pblk *pblk = rqd->private;
216 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
217 struct pblk_pr_ctx *pr_ctx = r_ctx->private;
218 struct bio *new_bio = rqd->bio;
219 struct bio *bio = pr_ctx->orig_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220 struct bio_vec src_bv, dst_bv;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200221 struct pblk_sec_meta *meta_list = rqd->meta_list;
222 int bio_init_idx = pr_ctx->bio_init_idx;
223 unsigned long *read_bitmap = pr_ctx->bitmap;
224 int nr_secs = pr_ctx->orig_nr_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200225 int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200226 __le64 *lba_list_mem, *lba_list_media;
227 void *src_p, *dst_p;
228 int hole, i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229
Javier González0f9248c2017-10-13 14:46:24 +0200230 if (unlikely(nr_holes == 1)) {
Javier González7bd4d372017-10-13 14:46:23 +0200231 struct ppa_addr ppa;
232
233 ppa = rqd->ppa_addr;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200234 rqd->ppa_list = pr_ctx->ppa_ptr;
235 rqd->dma_ppa_list = pr_ctx->dma_ppa_list;
Javier González7bd4d372017-10-13 14:46:23 +0200236 rqd->ppa_list[0] = ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200237 }
238
Heiner Litz11f6ad62018-07-13 10:48:44 +0200239 /* Re-use allocated memory for intermediate lbas */
240 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
241 lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size);
242
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200243 for (i = 0; i < nr_secs; i++) {
244 lba_list_media[i] = meta_list[i].lba;
245 meta_list[i].lba = lba_list_mem[i];
246 }
247
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200248 /* Fill the holes in the original bio */
249 i = 0;
250 hole = find_first_zero_bit(read_bitmap, nr_secs);
251 do {
Javier Gonzálezcb216652018-10-09 13:11:42 +0200252 struct pblk_line *line;
Javier González7bd4d372017-10-13 14:46:23 +0200253
Javier Gonzálezcb216652018-10-09 13:11:42 +0200254 line = pblk_ppa_to_line(pblk, rqd->ppa_list[i]);
Javier González7bd4d372017-10-13 14:46:23 +0200255 kref_put(&line->ref, pblk_line_put);
256
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200257 meta_list[hole].lba = lba_list_media[i];
258
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259 src_bv = new_bio->bi_io_vec[i++];
Heiner Litz11f6ad62018-07-13 10:48:44 +0200260 dst_bv = bio->bi_io_vec[bio_init_idx + hole];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200261
262 src_p = kmap_atomic(src_bv.bv_page);
263 dst_p = kmap_atomic(dst_bv.bv_page);
264
265 memcpy(dst_p + dst_bv.bv_offset,
266 src_p + src_bv.bv_offset,
267 PBLK_EXPOSED_PAGE_SIZE);
268
269 kunmap_atomic(src_p);
270 kunmap_atomic(dst_p);
271
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400272 mempool_free(src_bv.bv_page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273
274 hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1);
275 } while (hole < nr_secs);
276
277 bio_put(new_bio);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200278 kfree(pr_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279
Javier González87cc40b2018-06-01 16:41:11 +0200280 /* restore original request */
281 rqd->bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282 rqd->nr_ppas = nr_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200283
Heiner Litz11f6ad62018-07-13 10:48:44 +0200284 bio_endio(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200285 __pblk_end_io_read(pblk, rqd, false);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200286}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200287
Heiner Litz11f6ad62018-07-13 10:48:44 +0200288static int pblk_setup_partial_read(struct pblk *pblk, struct nvm_rq *rqd,
289 unsigned int bio_init_idx,
290 unsigned long *read_bitmap,
291 int nr_holes)
292{
293 struct pblk_sec_meta *meta_list = rqd->meta_list;
294 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
295 struct pblk_pr_ctx *pr_ctx;
296 struct bio *new_bio, *bio = r_ctx->private;
297 __le64 *lba_list_mem;
298 int nr_secs = rqd->nr_ppas;
299 int i;
300
301 /* Re-use allocated memory for intermediate lbas */
302 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
303
304 new_bio = bio_alloc(GFP_KERNEL, nr_holes);
305
306 if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
307 goto fail_bio_put;
308
309 if (nr_holes != new_bio->bi_vcnt) {
310 WARN_ONCE(1, "pblk: malformed bio\n");
311 goto fail_free_pages;
312 }
313
314 pr_ctx = kmalloc(sizeof(struct pblk_pr_ctx), GFP_KERNEL);
315 if (!pr_ctx)
316 goto fail_free_pages;
317
318 for (i = 0; i < nr_secs; i++)
319 lba_list_mem[i] = meta_list[i].lba;
320
321 new_bio->bi_iter.bi_sector = 0; /* internal bio */
322 bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
323
324 rqd->bio = new_bio;
325 rqd->nr_ppas = nr_holes;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200326
327 pr_ctx->ppa_ptr = NULL;
328 pr_ctx->orig_bio = bio;
329 bitmap_copy(pr_ctx->bitmap, read_bitmap, NVM_MAX_VLBA);
330 pr_ctx->bio_init_idx = bio_init_idx;
331 pr_ctx->orig_nr_secs = nr_secs;
332 r_ctx->private = pr_ctx;
333
334 if (unlikely(nr_holes == 1)) {
335 pr_ctx->ppa_ptr = rqd->ppa_list;
336 pr_ctx->dma_ppa_list = rqd->dma_ppa_list;
337 rqd->ppa_addr = rqd->ppa_list[0];
338 }
339 return 0;
340
341fail_free_pages:
Igor Konopkofbadca72018-06-01 16:41:09 +0200342 pblk_bio_free_pages(pblk, new_bio, 0, new_bio->bi_vcnt);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200343fail_bio_put:
344 bio_put(new_bio);
345
346 return -ENOMEM;
347}
348
349static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
350 unsigned int bio_init_idx,
351 unsigned long *read_bitmap, int nr_secs)
352{
353 int nr_holes;
354 int ret;
355
356 nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
357
358 if (pblk_setup_partial_read(pblk, rqd, bio_init_idx, read_bitmap,
359 nr_holes))
360 return NVM_IO_ERR;
361
362 rqd->end_io = pblk_end_partial_read;
363
364 ret = pblk_submit_io(pblk, rqd);
365 if (ret) {
366 bio_put(rqd->bio);
367 pblk_err(pblk, "partial read IO submission failed\n");
368 goto err;
369 }
370
371 return NVM_IO_OK;
372
373err:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200374 pblk_err(pblk, "failed to perform partial read\n");
Heiner Litz11f6ad62018-07-13 10:48:44 +0200375
376 /* Free allocated pages in new bio */
377 pblk_bio_free_pages(pblk, rqd->bio, 0, rqd->bio->bi_vcnt);
Javier González7bd4d372017-10-13 14:46:23 +0200378 __pblk_end_io_read(pblk, rqd, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200379 return NVM_IO_ERR;
380}
381
Javier González87cc40b2018-06-01 16:41:11 +0200382static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio,
Javier González84454e62017-10-13 14:46:13 +0200383 sector_t lba, unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200385 struct pblk_sec_meta *meta_list = rqd->meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200386 struct ppa_addr ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387
388 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
389
Matias Bjørling880eda52018-07-13 10:48:37 +0200390#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200391 atomic_long_inc(&pblk->inflight_reads);
392#endif
393
394retry:
395 if (pblk_ppa_empty(ppa)) {
396 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200397 meta_list[0].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398 return;
399 }
400
401 /* Try to read from write buffer. The address is later checked on the
402 * write buffer to prevent retrieving overwritten data.
403 */
404 if (pblk_addr_in_cache(ppa)) {
Javier González75cb8e92017-07-28 15:13:16 +0200405 if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200406 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
407 goto retry;
408 }
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200409
Javier González7bd4d372017-10-13 14:46:23 +0200410 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200411 meta_list[0].lba = cpu_to_le64(lba);
412
Matias Bjørling880eda52018-07-13 10:48:37 +0200413#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González7bd4d372017-10-13 14:46:23 +0200414 atomic_long_inc(&pblk->cache_reads);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200415#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416 } else {
417 rqd->ppa_addr = ppa;
418 }
419}
420
421int pblk_submit_read(struct pblk *pblk, struct bio *bio)
422{
423 struct nvm_tgt_dev *dev = pblk->dev;
Javier González998ba622018-01-05 14:16:20 +0100424 struct request_queue *q = dev->q;
Javier González84454e62017-10-13 14:46:13 +0200425 sector_t blba = pblk_get_lba(bio);
Dan Carpenter5bf1e1ee2017-04-21 16:48:40 -0600426 unsigned int nr_secs = pblk_get_secs(bio);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200427 struct pblk_g_ctx *r_ctx;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200429 unsigned int bio_init_idx;
Matias Bjørling921aebf2018-07-13 10:48:40 +0200430 DECLARE_BITMAP(read_bitmap, NVM_MAX_VLBA);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 int ret = NVM_IO_ERR;
432
Michael Callahanddcf35d2018-07-18 04:47:39 -0700433 generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio),
434 &pblk->disk->part0);
Javier González998ba622018-01-05 14:16:20 +0100435
Matias Bjørling921aebf2018-07-13 10:48:40 +0200436 bitmap_zero(read_bitmap, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200437
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200438 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439
440 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200441 rqd->nr_ppas = nr_secs;
Javier González87cc40b2018-06-01 16:41:11 +0200442 rqd->bio = NULL; /* cloned bio if needed */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200443 rqd->private = pblk;
444 rqd->end_io = pblk_end_io_read;
445
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200446 r_ctx = nvm_rq_to_pdu(rqd);
Javier González998ba622018-01-05 14:16:20 +0100447 r_ctx->start_time = jiffies;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200448 r_ctx->lba = blba;
Javier González87cc40b2018-06-01 16:41:11 +0200449 r_ctx->private = bio; /* original bio */
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200450
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451 /* Save the index for this bio's start. This is needed in case
452 * we need to fill a partial read.
453 */
454 bio_init_idx = pblk_get_bi_idx(bio);
455
Javier González63e38092017-06-26 11:57:24 +0200456 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
457 &rqd->dma_meta_list);
458 if (!rqd->meta_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200459 pblk_err(pblk, "not able to allocate ppa list\n");
Javier González63e38092017-06-26 11:57:24 +0200460 goto fail_rqd_free;
461 }
462
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463 if (nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200464 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
465 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200466
Matias Bjørling921aebf2018-07-13 10:48:40 +0200467 pblk_read_ppalist_rq(pblk, rqd, bio, blba, read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468 } else {
Matias Bjørling921aebf2018-07-13 10:48:40 +0200469 pblk_read_rq(pblk, rqd, bio, blba, read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470 }
471
Matias Bjørling921aebf2018-07-13 10:48:40 +0200472 if (bitmap_full(read_bitmap, nr_secs)) {
Javier González588726d32017-06-26 11:57:29 +0200473 atomic_inc(&pblk->inflight_io);
Javier González7bd4d372017-10-13 14:46:23 +0200474 __pblk_end_io_read(pblk, rqd, false);
Javier González87cc40b2018-06-01 16:41:11 +0200475 return NVM_IO_DONE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200476 }
477
478 /* All sectors are to be read from the device */
Matias Bjørling921aebf2018-07-13 10:48:40 +0200479 if (bitmap_empty(read_bitmap, rqd->nr_ppas)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480 struct bio *int_bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200481
482 /* Clone read bio to deal with read errors internally */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400483 int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 if (!int_bio) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200485 pblk_err(pblk, "could not clone read bio\n");
Javier González998ba622018-01-05 14:16:20 +0100486 goto fail_end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487 }
488
489 rqd->bio = int_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200490
Javier Gonzáleze13f4212018-06-01 15:04:21 +0200491 if (pblk_submit_io(pblk, rqd)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200492 pblk_err(pblk, "read IO submission failed\n");
Javier Gonzáleze13f4212018-06-01 15:04:21 +0200493 ret = NVM_IO_ERR;
Javier González998ba622018-01-05 14:16:20 +0100494 goto fail_end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495 }
496
497 return NVM_IO_OK;
498 }
499
500 /* The read bio request could be partially filled by the write buffer,
501 * but there are some holes that need to be read from the drive.
502 */
Heiner Litz11f6ad62018-07-13 10:48:44 +0200503 ret = pblk_partial_read_bio(pblk, rqd, bio_init_idx, read_bitmap,
504 nr_secs);
505 if (ret)
506 goto fail_meta_free;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507
Heiner Litz11f6ad62018-07-13 10:48:44 +0200508 return NVM_IO_OK;
509
510fail_meta_free:
511 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200512fail_rqd_free:
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200513 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200514 return ret;
Javier González998ba622018-01-05 14:16:20 +0100515fail_end_io:
516 __pblk_end_io_read(pblk, rqd, false);
517 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518}
519
520static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
521 struct pblk_line *line, u64 *lba_list,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200522 u64 *paddr_list_gc, unsigned int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523{
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200524 struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA];
Javier Gonzálezd3401212017-10-13 14:46:14 +0200525 struct ppa_addr ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200526 int valid_secs = 0;
527 int i;
528
Javier Gonzálezd3401212017-10-13 14:46:14 +0200529 pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200530
531 for (i = 0; i < nr_secs; i++) {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200532 if (lba_list[i] == ADDR_EMPTY)
533 continue;
534
535 ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
536 if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
537 paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538 continue;
539 }
540
Javier Gonzálezd3401212017-10-13 14:46:14 +0200541 rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542 }
543
Matias Bjørling880eda52018-07-13 10:48:37 +0200544#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545 atomic_long_add(valid_secs, &pblk->inflight_reads);
546#endif
Javier Gonzálezd3401212017-10-13 14:46:14 +0200547
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548 return valid_secs;
549}
550
551static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200552 struct pblk_line *line, sector_t lba,
553 u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200554{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200555 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556 int valid_secs = 0;
557
Dan Carpenter659226e2017-04-21 16:49:56 -0600558 if (lba == ADDR_EMPTY)
559 goto out;
560
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200561 /* logic error: lba out-of-bounds */
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200562 if (lba >= pblk->rl.nr_secs) {
563 WARN(1, "pblk: read lba out of bounds\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564 goto out;
565 }
566
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200567 spin_lock(&pblk->trans_lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200568 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569 spin_unlock(&pblk->trans_lock);
570
Javier Gonzálezd3401212017-10-13 14:46:14 +0200571 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
572 if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200573 goto out;
574
Javier Gonzálezd3401212017-10-13 14:46:14 +0200575 rqd->ppa_addr = ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200576 valid_secs = 1;
577
Matias Bjørling880eda52018-07-13 10:48:37 +0200578#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579 atomic_long_inc(&pblk->inflight_reads);
580#endif
581
582out:
583 return valid_secs;
584}
585
Javier Gonzálezd3401212017-10-13 14:46:14 +0200586int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587{
588 struct nvm_tgt_dev *dev = pblk->dev;
589 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200590 struct bio *bio;
591 struct nvm_rq rqd;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200592 int data_len;
593 int ret = NVM_IO_OK;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594
595 memset(&rqd, 0, sizeof(struct nvm_rq));
596
Javier González63e38092017-06-26 11:57:24 +0200597 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
598 &rqd.dma_meta_list);
599 if (!rqd.meta_list)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200600 return -ENOMEM;
Javier González63e38092017-06-26 11:57:24 +0200601
Javier Gonzálezd3401212017-10-13 14:46:14 +0200602 if (gc_rq->nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200603 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
604 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605
Javier Gonzálezd3401212017-10-13 14:46:14 +0200606 gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
607 gc_rq->lba_list,
608 gc_rq->paddr_list,
609 gc_rq->nr_secs);
610 if (gc_rq->secs_to_gc == 1)
Javier González63e38092017-06-26 11:57:24 +0200611 rqd.ppa_addr = rqd.ppa_list[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612 } else {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200613 gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
614 gc_rq->lba_list[0],
615 gc_rq->paddr_list[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616 }
617
Javier Gonzálezd3401212017-10-13 14:46:14 +0200618 if (!(gc_rq->secs_to_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619 goto out;
620
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200621 data_len = (gc_rq->secs_to_gc) * geo->csecs;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200622 bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
Javier González7d327a92017-10-13 14:46:02 +0200623 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624 if (IS_ERR(bio)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200625 pblk_err(pblk, "could not allocate GC bio (%lu)\n",
626 PTR_ERR(bio));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200627 goto err_free_dma;
628 }
629
630 bio->bi_iter.bi_sector = 0; /* internal bio */
631 bio_set_op_attrs(bio, REQ_OP_READ, 0);
632
633 rqd.opcode = NVM_OP_PREAD;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200634 rqd.nr_ppas = gc_rq->secs_to_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 rqd.bio = bio;
636
Javier González1a94b2d2017-10-13 14:46:47 +0200637 if (pblk_submit_io_sync(pblk, &rqd)) {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200638 ret = -EIO;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200639 pblk_err(pblk, "GC read request failed\n");
Javier González7d327a92017-10-13 14:46:02 +0200640 goto err_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200641 }
642
Javier González03a34b22018-06-01 15:04:18 +0200643 pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs);
Javier González310df582018-06-01 15:04:17 +0200644
Javier González588726d32017-06-26 11:57:29 +0200645 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200646
647 if (rqd.error) {
648 atomic_long_inc(&pblk->read_failed_gc);
Matias Bjørling880eda52018-07-13 10:48:37 +0200649#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650 pblk_print_failed_rqd(pblk, &rqd, rqd.error);
651#endif
652 }
653
Matias Bjørling880eda52018-07-13 10:48:37 +0200654#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezd3401212017-10-13 14:46:14 +0200655 atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
656 atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
657 atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200658#endif
659
660out:
Javier González63e38092017-06-26 11:57:24 +0200661 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200662 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200663
Javier González7d327a92017-10-13 14:46:02 +0200664err_free_bio:
665 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200666err_free_dma:
Javier González63e38092017-06-26 11:57:24 +0200667 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200668 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200669}