pblk-cache.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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-cache.c - pblk's write cache
  16. */
  17. #include "pblk.h"
  18. int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, unsigned long flags)
  19. {
  20. struct pblk_w_ctx w_ctx;
  21. sector_t lba = pblk_get_lba(bio);
  22. unsigned int bpos, pos;
  23. int nr_entries = pblk_get_secs(bio);
  24. int i, ret;
  25. /* Update the write buffer head (mem) with the entries that we can
  26. * write. The write in itself cannot fail, so there is no need to
  27. * rollback from here on.
  28. */
  29. retry:
  30. ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
  31. switch (ret) {
  32. case NVM_IO_REQUEUE:
  33. io_schedule();
  34. goto retry;
  35. case NVM_IO_ERR:
  36. pblk_pipeline_stop(pblk);
  37. goto out;
  38. }
  39. if (unlikely(!bio_has_data(bio)))
  40. goto out;
  41. pblk_ppa_set_empty(&w_ctx.ppa);
  42. w_ctx.flags = flags;
  43. if (bio->bi_opf & REQ_PREFLUSH)
  44. w_ctx.flags |= PBLK_FLUSH_ENTRY;
  45. for (i = 0; i < nr_entries; i++) {
  46. void *data = bio_data(bio);
  47. w_ctx.lba = lba + i;
  48. pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
  49. pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
  50. bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
  51. }
  52. #ifdef CONFIG_NVM_DEBUG
  53. atomic_long_add(nr_entries, &pblk->inflight_writes);
  54. atomic_long_add(nr_entries, &pblk->req_writes);
  55. #endif
  56. pblk_rl_inserted(&pblk->rl, nr_entries);
  57. out:
  58. pblk_write_should_kick(pblk);
  59. return ret;
  60. }
  61. /*
  62. * On GC the incoming lbas are not necessarily sequential. Also, some of the
  63. * lbas might not be valid entries, which are marked as empty by the GC thread
  64. */
  65. int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
  66. {
  67. struct pblk_w_ctx w_ctx;
  68. unsigned int bpos, pos;
  69. void *data = gc_rq->data;
  70. int i, valid_entries;
  71. /* Update the write buffer head (mem) with the entries that we can
  72. * write. The write in itself cannot fail, so there is no need to
  73. * rollback from here on.
  74. */
  75. retry:
  76. if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
  77. io_schedule();
  78. goto retry;
  79. }
  80. w_ctx.flags = PBLK_IOTYPE_GC;
  81. pblk_ppa_set_empty(&w_ctx.ppa);
  82. for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
  83. if (gc_rq->lba_list[i] == ADDR_EMPTY)
  84. continue;
  85. w_ctx.lba = gc_rq->lba_list[i];
  86. pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
  87. pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
  88. gc_rq->paddr_list[i], pos);
  89. data += PBLK_EXPOSED_PAGE_SIZE;
  90. valid_entries++;
  91. }
  92. WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
  93. "pblk: inconsistent GC write\n");
  94. #ifdef CONFIG_NVM_DEBUG
  95. atomic_long_add(valid_entries, &pblk->inflight_writes);
  96. atomic_long_add(valid_entries, &pblk->recov_gc_writes);
  97. #endif
  98. pblk_write_should_kick(pblk);
  99. return NVM_IO_OK;
  100. }