pblk-read.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  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. #include "pblk.h"
  18. /*
  19. * There is no guarantee that the value read from cache has not been updated and
  20. * resides at another location in the cache. We guarantee though that if the
  21. * value is read from the cache, it belongs to the mapped lba. In order to
  22. * guarantee and order between writes and reads are ordered, a flush must be
  23. * issued.
  24. */
  25. static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio,
  26. sector_t lba, struct ppa_addr ppa,
  27. int bio_iter, bool advanced_bio)
  28. {
  29. #ifdef CONFIG_NVM_PBLK_DEBUG
  30. /* Callers must ensure that the ppa points to a cache address */
  31. BUG_ON(pblk_ppa_empty(ppa));
  32. BUG_ON(!pblk_addr_in_cache(ppa));
  33. #endif
  34. return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa,
  35. bio_iter, advanced_bio);
  36. }
  37. static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd,
  38. struct bio *bio, sector_t blba,
  39. unsigned long *read_bitmap)
  40. {
  41. struct pblk_sec_meta *meta_list = rqd->meta_list;
  42. struct ppa_addr ppas[PBLK_MAX_REQ_ADDRS];
  43. int nr_secs = rqd->nr_ppas;
  44. bool advanced_bio = false;
  45. int i, j = 0;
  46. pblk_lookup_l2p_seq(pblk, ppas, blba, nr_secs);
  47. for (i = 0; i < nr_secs; i++) {
  48. struct ppa_addr p = ppas[i];
  49. sector_t lba = blba + i;
  50. retry:
  51. if (pblk_ppa_empty(p)) {
  52. WARN_ON(test_and_set_bit(i, read_bitmap));
  53. meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
  54. if (unlikely(!advanced_bio)) {
  55. bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE);
  56. advanced_bio = true;
  57. }
  58. goto next;
  59. }
  60. /* Try to read from write buffer. The address is later checked
  61. * on the write buffer to prevent retrieving overwritten data.
  62. */
  63. if (pblk_addr_in_cache(p)) {
  64. if (!pblk_read_from_cache(pblk, bio, lba, p, i,
  65. advanced_bio)) {
  66. pblk_lookup_l2p_seq(pblk, &p, lba, 1);
  67. goto retry;
  68. }
  69. WARN_ON(test_and_set_bit(i, read_bitmap));
  70. meta_list[i].lba = cpu_to_le64(lba);
  71. advanced_bio = true;
  72. #ifdef CONFIG_NVM_PBLK_DEBUG
  73. atomic_long_inc(&pblk->cache_reads);
  74. #endif
  75. } else {
  76. /* Read from media non-cached sectors */
  77. rqd->ppa_list[j++] = p;
  78. }
  79. next:
  80. if (advanced_bio)
  81. bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
  82. }
  83. if (pblk_io_aligned(pblk, nr_secs))
  84. rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
  85. else
  86. rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
  87. #ifdef CONFIG_NVM_PBLK_DEBUG
  88. atomic_long_add(nr_secs, &pblk->inflight_reads);
  89. #endif
  90. }
  91. static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd,
  92. sector_t blba)
  93. {
  94. struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
  95. int nr_lbas = rqd->nr_ppas;
  96. int i;
  97. for (i = 0; i < nr_lbas; i++) {
  98. u64 lba = le64_to_cpu(meta_lba_list[i].lba);
  99. if (lba == ADDR_EMPTY)
  100. continue;
  101. if (lba != blba + i) {
  102. #ifdef CONFIG_NVM_PBLK_DEBUG
  103. struct ppa_addr *p;
  104. p = (nr_lbas == 1) ? &rqd->ppa_list[i] : &rqd->ppa_addr;
  105. print_ppa(pblk, p, "seq", i);
  106. #endif
  107. pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
  108. lba, (u64)blba + i);
  109. WARN_ON(1);
  110. }
  111. }
  112. }
  113. /*
  114. * There can be holes in the lba list.
  115. */
  116. static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd,
  117. u64 *lba_list, int nr_lbas)
  118. {
  119. struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
  120. int i, j;
  121. for (i = 0, j = 0; i < nr_lbas; i++) {
  122. u64 lba = lba_list[i];
  123. u64 meta_lba;
  124. if (lba == ADDR_EMPTY)
  125. continue;
  126. meta_lba = le64_to_cpu(meta_lba_list[j].lba);
  127. if (lba != meta_lba) {
  128. #ifdef CONFIG_NVM_PBLK_DEBUG
  129. struct ppa_addr *p;
  130. int nr_ppas = rqd->nr_ppas;
  131. p = (nr_ppas == 1) ? &rqd->ppa_list[j] : &rqd->ppa_addr;
  132. print_ppa(pblk, p, "seq", j);
  133. #endif
  134. pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
  135. lba, meta_lba);
  136. WARN_ON(1);
  137. }
  138. j++;
  139. }
  140. WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n");
  141. }
  142. static void pblk_read_put_rqd_kref(struct pblk *pblk, struct nvm_rq *rqd)
  143. {
  144. struct ppa_addr *ppa_list;
  145. int i;
  146. ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
  147. for (i = 0; i < rqd->nr_ppas; i++) {
  148. struct ppa_addr ppa = ppa_list[i];
  149. struct pblk_line *line;
  150. line = &pblk->lines[pblk_ppa_to_line(ppa)];
  151. kref_put(&line->ref, pblk_line_put_wq);
  152. }
  153. }
  154. static void pblk_end_user_read(struct bio *bio)
  155. {
  156. #ifdef CONFIG_NVM_PBLK_DEBUG
  157. WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n");
  158. #endif
  159. bio_endio(bio);
  160. }
  161. static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd,
  162. bool put_line)
  163. {
  164. struct nvm_tgt_dev *dev = pblk->dev;
  165. struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
  166. struct bio *int_bio = rqd->bio;
  167. unsigned long start_time = r_ctx->start_time;
  168. generic_end_io_acct(dev->q, REQ_OP_READ, &pblk->disk->part0, start_time);
  169. if (rqd->error)
  170. pblk_log_read_err(pblk, rqd);
  171. pblk_read_check_seq(pblk, rqd, r_ctx->lba);
  172. if (int_bio)
  173. bio_put(int_bio);
  174. if (put_line)
  175. pblk_read_put_rqd_kref(pblk, rqd);
  176. #ifdef CONFIG_NVM_PBLK_DEBUG
  177. atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
  178. atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
  179. #endif
  180. pblk_free_rqd(pblk, rqd, PBLK_READ);
  181. atomic_dec(&pblk->inflight_io);
  182. }
  183. static void pblk_end_io_read(struct nvm_rq *rqd)
  184. {
  185. struct pblk *pblk = rqd->private;
  186. struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
  187. struct bio *bio = (struct bio *)r_ctx->private;
  188. pblk_end_user_read(bio);
  189. __pblk_end_io_read(pblk, rqd, true);
  190. }
  191. static void pblk_end_partial_read(struct nvm_rq *rqd)
  192. {
  193. struct pblk *pblk = rqd->private;
  194. struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
  195. struct pblk_pr_ctx *pr_ctx = r_ctx->private;
  196. struct bio *new_bio = rqd->bio;
  197. struct bio *bio = pr_ctx->orig_bio;
  198. struct bio_vec src_bv, dst_bv;
  199. struct pblk_sec_meta *meta_list = rqd->meta_list;
  200. int bio_init_idx = pr_ctx->bio_init_idx;
  201. unsigned long *read_bitmap = pr_ctx->bitmap;
  202. int nr_secs = pr_ctx->orig_nr_secs;
  203. int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
  204. __le64 *lba_list_mem, *lba_list_media;
  205. void *src_p, *dst_p;
  206. int hole, i;
  207. if (unlikely(nr_holes == 1)) {
  208. struct ppa_addr ppa;
  209. ppa = rqd->ppa_addr;
  210. rqd->ppa_list = pr_ctx->ppa_ptr;
  211. rqd->dma_ppa_list = pr_ctx->dma_ppa_list;
  212. rqd->ppa_list[0] = ppa;
  213. }
  214. /* Re-use allocated memory for intermediate lbas */
  215. lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
  216. lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size);
  217. for (i = 0; i < nr_secs; i++) {
  218. lba_list_media[i] = meta_list[i].lba;
  219. meta_list[i].lba = lba_list_mem[i];
  220. }
  221. /* Fill the holes in the original bio */
  222. i = 0;
  223. hole = find_first_zero_bit(read_bitmap, nr_secs);
  224. do {
  225. int line_id = pblk_ppa_to_line(rqd->ppa_list[i]);
  226. struct pblk_line *line = &pblk->lines[line_id];
  227. kref_put(&line->ref, pblk_line_put);
  228. meta_list[hole].lba = lba_list_media[i];
  229. src_bv = new_bio->bi_io_vec[i++];
  230. dst_bv = bio->bi_io_vec[bio_init_idx + hole];
  231. src_p = kmap_atomic(src_bv.bv_page);
  232. dst_p = kmap_atomic(dst_bv.bv_page);
  233. memcpy(dst_p + dst_bv.bv_offset,
  234. src_p + src_bv.bv_offset,
  235. PBLK_EXPOSED_PAGE_SIZE);
  236. kunmap_atomic(src_p);
  237. kunmap_atomic(dst_p);
  238. mempool_free(src_bv.bv_page, &pblk->page_bio_pool);
  239. hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1);
  240. } while (hole < nr_secs);
  241. bio_put(new_bio);
  242. kfree(pr_ctx);
  243. /* restore original request */
  244. rqd->bio = NULL;
  245. rqd->nr_ppas = nr_secs;
  246. bio_endio(bio);
  247. __pblk_end_io_read(pblk, rqd, false);
  248. }
  249. static int pblk_setup_partial_read(struct pblk *pblk, struct nvm_rq *rqd,
  250. unsigned int bio_init_idx,
  251. unsigned long *read_bitmap,
  252. int nr_holes)
  253. {
  254. struct pblk_sec_meta *meta_list = rqd->meta_list;
  255. struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
  256. struct pblk_pr_ctx *pr_ctx;
  257. struct bio *new_bio, *bio = r_ctx->private;
  258. __le64 *lba_list_mem;
  259. int nr_secs = rqd->nr_ppas;
  260. int i;
  261. /* Re-use allocated memory for intermediate lbas */
  262. lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
  263. new_bio = bio_alloc(GFP_KERNEL, nr_holes);
  264. if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
  265. goto fail_bio_put;
  266. if (nr_holes != new_bio->bi_vcnt) {
  267. WARN_ONCE(1, "pblk: malformed bio\n");
  268. goto fail_free_pages;
  269. }
  270. pr_ctx = kmalloc(sizeof(struct pblk_pr_ctx), GFP_KERNEL);
  271. if (!pr_ctx)
  272. goto fail_free_pages;
  273. for (i = 0; i < nr_secs; i++)
  274. lba_list_mem[i] = meta_list[i].lba;
  275. new_bio->bi_iter.bi_sector = 0; /* internal bio */
  276. bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
  277. rqd->bio = new_bio;
  278. rqd->nr_ppas = nr_holes;
  279. rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
  280. pr_ctx->ppa_ptr = NULL;
  281. pr_ctx->orig_bio = bio;
  282. bitmap_copy(pr_ctx->bitmap, read_bitmap, NVM_MAX_VLBA);
  283. pr_ctx->bio_init_idx = bio_init_idx;
  284. pr_ctx->orig_nr_secs = nr_secs;
  285. r_ctx->private = pr_ctx;
  286. if (unlikely(nr_holes == 1)) {
  287. pr_ctx->ppa_ptr = rqd->ppa_list;
  288. pr_ctx->dma_ppa_list = rqd->dma_ppa_list;
  289. rqd->ppa_addr = rqd->ppa_list[0];
  290. }
  291. return 0;
  292. fail_free_pages:
  293. pblk_bio_free_pages(pblk, new_bio, 0, new_bio->bi_vcnt);
  294. fail_bio_put:
  295. bio_put(new_bio);
  296. return -ENOMEM;
  297. }
  298. static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
  299. unsigned int bio_init_idx,
  300. unsigned long *read_bitmap, int nr_secs)
  301. {
  302. int nr_holes;
  303. int ret;
  304. nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
  305. if (pblk_setup_partial_read(pblk, rqd, bio_init_idx, read_bitmap,
  306. nr_holes))
  307. return NVM_IO_ERR;
  308. rqd->end_io = pblk_end_partial_read;
  309. ret = pblk_submit_io(pblk, rqd);
  310. if (ret) {
  311. bio_put(rqd->bio);
  312. pblk_err(pblk, "partial read IO submission failed\n");
  313. goto err;
  314. }
  315. return NVM_IO_OK;
  316. err:
  317. pblk_err(pblk, "failed to perform partial read\n");
  318. /* Free allocated pages in new bio */
  319. pblk_bio_free_pages(pblk, rqd->bio, 0, rqd->bio->bi_vcnt);
  320. __pblk_end_io_read(pblk, rqd, false);
  321. return NVM_IO_ERR;
  322. }
  323. static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio,
  324. sector_t lba, unsigned long *read_bitmap)
  325. {
  326. struct pblk_sec_meta *meta_list = rqd->meta_list;
  327. struct ppa_addr ppa;
  328. pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
  329. #ifdef CONFIG_NVM_PBLK_DEBUG
  330. atomic_long_inc(&pblk->inflight_reads);
  331. #endif
  332. retry:
  333. if (pblk_ppa_empty(ppa)) {
  334. WARN_ON(test_and_set_bit(0, read_bitmap));
  335. meta_list[0].lba = cpu_to_le64(ADDR_EMPTY);
  336. return;
  337. }
  338. /* Try to read from write buffer. The address is later checked on the
  339. * write buffer to prevent retrieving overwritten data.
  340. */
  341. if (pblk_addr_in_cache(ppa)) {
  342. if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
  343. pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
  344. goto retry;
  345. }
  346. WARN_ON(test_and_set_bit(0, read_bitmap));
  347. meta_list[0].lba = cpu_to_le64(lba);
  348. #ifdef CONFIG_NVM_PBLK_DEBUG
  349. atomic_long_inc(&pblk->cache_reads);
  350. #endif
  351. } else {
  352. rqd->ppa_addr = ppa;
  353. }
  354. rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
  355. }
  356. int pblk_submit_read(struct pblk *pblk, struct bio *bio)
  357. {
  358. struct nvm_tgt_dev *dev = pblk->dev;
  359. struct request_queue *q = dev->q;
  360. sector_t blba = pblk_get_lba(bio);
  361. unsigned int nr_secs = pblk_get_secs(bio);
  362. struct pblk_g_ctx *r_ctx;
  363. struct nvm_rq *rqd;
  364. unsigned int bio_init_idx;
  365. DECLARE_BITMAP(read_bitmap, NVM_MAX_VLBA);
  366. int ret = NVM_IO_ERR;
  367. /* logic error: lba out-of-bounds. Ignore read request */
  368. if (blba >= pblk->rl.nr_secs || nr_secs > PBLK_MAX_REQ_ADDRS) {
  369. WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n",
  370. (unsigned long long)blba, nr_secs);
  371. return NVM_IO_ERR;
  372. }
  373. generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio),
  374. &pblk->disk->part0);
  375. bitmap_zero(read_bitmap, nr_secs);
  376. rqd = pblk_alloc_rqd(pblk, PBLK_READ);
  377. rqd->opcode = NVM_OP_PREAD;
  378. rqd->nr_ppas = nr_secs;
  379. rqd->bio = NULL; /* cloned bio if needed */
  380. rqd->private = pblk;
  381. rqd->end_io = pblk_end_io_read;
  382. r_ctx = nvm_rq_to_pdu(rqd);
  383. r_ctx->start_time = jiffies;
  384. r_ctx->lba = blba;
  385. r_ctx->private = bio; /* original bio */
  386. /* Save the index for this bio's start. This is needed in case
  387. * we need to fill a partial read.
  388. */
  389. bio_init_idx = pblk_get_bi_idx(bio);
  390. rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  391. &rqd->dma_meta_list);
  392. if (!rqd->meta_list) {
  393. pblk_err(pblk, "not able to allocate ppa list\n");
  394. goto fail_rqd_free;
  395. }
  396. if (nr_secs > 1) {
  397. rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
  398. rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
  399. pblk_read_ppalist_rq(pblk, rqd, bio, blba, read_bitmap);
  400. } else {
  401. pblk_read_rq(pblk, rqd, bio, blba, read_bitmap);
  402. }
  403. if (bitmap_full(read_bitmap, nr_secs)) {
  404. atomic_inc(&pblk->inflight_io);
  405. __pblk_end_io_read(pblk, rqd, false);
  406. return NVM_IO_DONE;
  407. }
  408. /* All sectors are to be read from the device */
  409. if (bitmap_empty(read_bitmap, rqd->nr_ppas)) {
  410. struct bio *int_bio = NULL;
  411. /* Clone read bio to deal with read errors internally */
  412. int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set);
  413. if (!int_bio) {
  414. pblk_err(pblk, "could not clone read bio\n");
  415. goto fail_end_io;
  416. }
  417. rqd->bio = int_bio;
  418. if (pblk_submit_io(pblk, rqd)) {
  419. pblk_err(pblk, "read IO submission failed\n");
  420. ret = NVM_IO_ERR;
  421. goto fail_end_io;
  422. }
  423. return NVM_IO_OK;
  424. }
  425. /* The read bio request could be partially filled by the write buffer,
  426. * but there are some holes that need to be read from the drive.
  427. */
  428. ret = pblk_partial_read_bio(pblk, rqd, bio_init_idx, read_bitmap,
  429. nr_secs);
  430. if (ret)
  431. goto fail_meta_free;
  432. return NVM_IO_OK;
  433. fail_meta_free:
  434. nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
  435. fail_rqd_free:
  436. pblk_free_rqd(pblk, rqd, PBLK_READ);
  437. return ret;
  438. fail_end_io:
  439. __pblk_end_io_read(pblk, rqd, false);
  440. return ret;
  441. }
  442. static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
  443. struct pblk_line *line, u64 *lba_list,
  444. u64 *paddr_list_gc, unsigned int nr_secs)
  445. {
  446. struct ppa_addr ppa_list_l2p[PBLK_MAX_REQ_ADDRS];
  447. struct ppa_addr ppa_gc;
  448. int valid_secs = 0;
  449. int i;
  450. pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
  451. for (i = 0; i < nr_secs; i++) {
  452. if (lba_list[i] == ADDR_EMPTY)
  453. continue;
  454. ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
  455. if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
  456. paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
  457. continue;
  458. }
  459. rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
  460. }
  461. #ifdef CONFIG_NVM_PBLK_DEBUG
  462. atomic_long_add(valid_secs, &pblk->inflight_reads);
  463. #endif
  464. return valid_secs;
  465. }
  466. static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
  467. struct pblk_line *line, sector_t lba,
  468. u64 paddr_gc)
  469. {
  470. struct ppa_addr ppa_l2p, ppa_gc;
  471. int valid_secs = 0;
  472. if (lba == ADDR_EMPTY)
  473. goto out;
  474. /* logic error: lba out-of-bounds */
  475. if (lba >= pblk->rl.nr_secs) {
  476. WARN(1, "pblk: read lba out of bounds\n");
  477. goto out;
  478. }
  479. spin_lock(&pblk->trans_lock);
  480. ppa_l2p = pblk_trans_map_get(pblk, lba);
  481. spin_unlock(&pblk->trans_lock);
  482. ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
  483. if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
  484. goto out;
  485. rqd->ppa_addr = ppa_l2p;
  486. valid_secs = 1;
  487. #ifdef CONFIG_NVM_PBLK_DEBUG
  488. atomic_long_inc(&pblk->inflight_reads);
  489. #endif
  490. out:
  491. return valid_secs;
  492. }
  493. int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
  494. {
  495. struct nvm_tgt_dev *dev = pblk->dev;
  496. struct nvm_geo *geo = &dev->geo;
  497. struct bio *bio;
  498. struct nvm_rq rqd;
  499. int data_len;
  500. int ret = NVM_IO_OK;
  501. memset(&rqd, 0, sizeof(struct nvm_rq));
  502. rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  503. &rqd.dma_meta_list);
  504. if (!rqd.meta_list)
  505. return -ENOMEM;
  506. if (gc_rq->nr_secs > 1) {
  507. rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
  508. rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
  509. gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
  510. gc_rq->lba_list,
  511. gc_rq->paddr_list,
  512. gc_rq->nr_secs);
  513. if (gc_rq->secs_to_gc == 1)
  514. rqd.ppa_addr = rqd.ppa_list[0];
  515. } else {
  516. gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
  517. gc_rq->lba_list[0],
  518. gc_rq->paddr_list[0]);
  519. }
  520. if (!(gc_rq->secs_to_gc))
  521. goto out;
  522. data_len = (gc_rq->secs_to_gc) * geo->csecs;
  523. bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
  524. PBLK_VMALLOC_META, GFP_KERNEL);
  525. if (IS_ERR(bio)) {
  526. pblk_err(pblk, "could not allocate GC bio (%lu)\n",
  527. PTR_ERR(bio));
  528. goto err_free_dma;
  529. }
  530. bio->bi_iter.bi_sector = 0; /* internal bio */
  531. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  532. rqd.opcode = NVM_OP_PREAD;
  533. rqd.nr_ppas = gc_rq->secs_to_gc;
  534. rqd.flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
  535. rqd.bio = bio;
  536. if (pblk_submit_io_sync(pblk, &rqd)) {
  537. ret = -EIO;
  538. pblk_err(pblk, "GC read request failed\n");
  539. goto err_free_bio;
  540. }
  541. pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs);
  542. atomic_dec(&pblk->inflight_io);
  543. if (rqd.error) {
  544. atomic_long_inc(&pblk->read_failed_gc);
  545. #ifdef CONFIG_NVM_PBLK_DEBUG
  546. pblk_print_failed_rqd(pblk, &rqd, rqd.error);
  547. #endif
  548. }
  549. #ifdef CONFIG_NVM_PBLK_DEBUG
  550. atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
  551. atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
  552. atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
  553. #endif
  554. out:
  555. nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
  556. return ret;
  557. err_free_bio:
  558. bio_put(bio);
  559. err_free_dma:
  560. nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
  561. return ret;
  562. }