pblk-map.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 CNEX Labs
  4. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  5. * Matias Bjorling <matias@cnexlabs.com>
  6. *
  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.
  10. *
  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.
  15. *
  16. * pblk-map.c - pblk's lba-ppa mapping strategy
  17. *
  18. */
  19. #include "pblk.h"
  20. static int pblk_map_page_data(struct pblk *pblk, unsigned int sentry,
  21. struct ppa_addr *ppa_list,
  22. unsigned long *lun_bitmap,
  23. struct pblk_sec_meta *meta_list,
  24. unsigned int valid_secs)
  25. {
  26. struct pblk_line *line = pblk_line_get_data(pblk);
  27. struct pblk_emeta *emeta;
  28. struct pblk_w_ctx *w_ctx;
  29. __le64 *lba_list;
  30. u64 paddr;
  31. int nr_secs = pblk->min_write_pgs;
  32. int i;
  33. if (pblk_line_is_full(line)) {
  34. struct pblk_line *prev_line = line;
  35. /* If we cannot allocate a new line, make sure to store metadata
  36. * on current line and then fail
  37. */
  38. line = pblk_line_replace_data(pblk);
  39. pblk_line_close_meta(pblk, prev_line);
  40. if (!line)
  41. return -EINTR;
  42. }
  43. emeta = line->emeta;
  44. lba_list = emeta_to_lbas(pblk, emeta->buf);
  45. paddr = pblk_alloc_page(pblk, line, nr_secs);
  46. for (i = 0; i < nr_secs; i++, paddr++) {
  47. __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
  48. /* ppa to be sent to the device */
  49. ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
  50. /* Write context for target bio completion on write buffer. Note
  51. * that the write buffer is protected by the sync backpointer,
  52. * and a single writer thread have access to each specific entry
  53. * at a time. Thus, it is safe to modify the context for the
  54. * entry we are setting up for submission without taking any
  55. * lock or memory barrier.
  56. */
  57. if (i < valid_secs) {
  58. kref_get(&line->ref);
  59. w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i);
  60. w_ctx->ppa = ppa_list[i];
  61. meta_list[i].lba = cpu_to_le64(w_ctx->lba);
  62. lba_list[paddr] = cpu_to_le64(w_ctx->lba);
  63. if (lba_list[paddr] != addr_empty)
  64. line->nr_valid_lbas++;
  65. else
  66. atomic64_inc(&pblk->pad_wa);
  67. } else {
  68. lba_list[paddr] = meta_list[i].lba = addr_empty;
  69. __pblk_map_invalidate(pblk, line, paddr);
  70. }
  71. }
  72. pblk_down_rq(pblk, ppa_list[0], lun_bitmap);
  73. return 0;
  74. }
  75. void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
  76. unsigned long *lun_bitmap, unsigned int valid_secs,
  77. unsigned int off)
  78. {
  79. struct pblk_sec_meta *meta_list = rqd->meta_list;
  80. struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
  81. unsigned int map_secs;
  82. int min = pblk->min_write_pgs;
  83. int i;
  84. for (i = off; i < rqd->nr_ppas; i += min) {
  85. map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
  86. if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i],
  87. lun_bitmap, &meta_list[i], map_secs)) {
  88. bio_put(rqd->bio);
  89. pblk_free_rqd(pblk, rqd, PBLK_WRITE);
  90. pblk_pipeline_stop(pblk);
  91. }
  92. }
  93. }
  94. /* only if erase_ppa is set, acquire erase semaphore */
  95. void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
  96. unsigned int sentry, unsigned long *lun_bitmap,
  97. unsigned int valid_secs, struct ppa_addr *erase_ppa)
  98. {
  99. struct nvm_tgt_dev *dev = pblk->dev;
  100. struct nvm_geo *geo = &dev->geo;
  101. struct pblk_line_meta *lm = &pblk->lm;
  102. struct pblk_sec_meta *meta_list = rqd->meta_list;
  103. struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
  104. struct pblk_line *e_line, *d_line;
  105. unsigned int map_secs;
  106. int min = pblk->min_write_pgs;
  107. int i, erase_lun;
  108. for (i = 0; i < rqd->nr_ppas; i += min) {
  109. map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
  110. if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i],
  111. lun_bitmap, &meta_list[i], map_secs)) {
  112. bio_put(rqd->bio);
  113. pblk_free_rqd(pblk, rqd, PBLK_WRITE);
  114. pblk_pipeline_stop(pblk);
  115. }
  116. erase_lun = pblk_ppa_to_pos(geo, ppa_list[i]);
  117. /* line can change after page map. We might also be writing the
  118. * last line.
  119. */
  120. e_line = pblk_line_get_erase(pblk);
  121. if (!e_line)
  122. return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
  123. valid_secs, i + min);
  124. spin_lock(&e_line->lock);
  125. if (!test_bit(erase_lun, e_line->erase_bitmap)) {
  126. set_bit(erase_lun, e_line->erase_bitmap);
  127. atomic_dec(&e_line->left_eblks);
  128. *erase_ppa = ppa_list[i];
  129. erase_ppa->a.blk = e_line->id;
  130. spin_unlock(&e_line->lock);
  131. /* Avoid evaluating e_line->left_eblks */
  132. return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
  133. valid_secs, i + min);
  134. }
  135. spin_unlock(&e_line->lock);
  136. }
  137. d_line = pblk_line_get_data(pblk);
  138. /* line can change after page map. We might also be writing the
  139. * last line.
  140. */
  141. e_line = pblk_line_get_erase(pblk);
  142. if (!e_line)
  143. return;
  144. /* Erase blocks that are bad in this line but might not be in next */
  145. if (unlikely(pblk_ppa_empty(*erase_ppa)) &&
  146. bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) {
  147. int bit = -1;
  148. retry:
  149. bit = find_next_bit(d_line->blk_bitmap,
  150. lm->blk_per_line, bit + 1);
  151. if (bit >= lm->blk_per_line)
  152. return;
  153. spin_lock(&e_line->lock);
  154. if (test_bit(bit, e_line->erase_bitmap)) {
  155. spin_unlock(&e_line->lock);
  156. goto retry;
  157. }
  158. spin_unlock(&e_line->lock);
  159. set_bit(bit, e_line->erase_bitmap);
  160. atomic_dec(&e_line->left_eblks);
  161. *erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */
  162. erase_ppa->a.blk = e_line->id;
  163. }
  164. }