pblk-cache.c 3.4 KB

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