pblk-cache.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if (ret == NVM_IO_REQUEUE) {
  32. io_schedule();
  33. goto retry;
  34. }
  35. if (unlikely(!bio_has_data(bio)))
  36. goto out;
  37. w_ctx.flags = flags;
  38. pblk_ppa_set_empty(&w_ctx.ppa);
  39. for (i = 0; i < nr_entries; i++) {
  40. void *data = bio_data(bio);
  41. w_ctx.lba = lba + i;
  42. pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
  43. pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
  44. bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
  45. }
  46. #ifdef CONFIG_NVM_DEBUG
  47. atomic_long_add(nr_entries, &pblk->inflight_writes);
  48. atomic_long_add(nr_entries, &pblk->req_writes);
  49. #endif
  50. out:
  51. pblk_write_should_kick(pblk);
  52. return ret;
  53. }
  54. /*
  55. * On GC the incoming lbas are not necessarily sequential. Also, some of the
  56. * lbas might not be valid entries, which are marked as empty by the GC thread
  57. */
  58. int pblk_write_gc_to_cache(struct pblk *pblk, void *data, u64 *lba_list,
  59. unsigned int nr_entries, unsigned int nr_rec_entries,
  60. struct pblk_line *gc_line, unsigned long flags)
  61. {
  62. struct pblk_w_ctx w_ctx;
  63. unsigned int bpos, pos;
  64. int i, valid_entries;
  65. /* Update the write buffer head (mem) with the entries that we can
  66. * write. The write in itself cannot fail, so there is no need to
  67. * rollback from here on.
  68. */
  69. retry:
  70. if (!pblk_rb_may_write_gc(&pblk->rwb, nr_rec_entries, &bpos)) {
  71. io_schedule();
  72. goto retry;
  73. }
  74. w_ctx.flags = flags;
  75. pblk_ppa_set_empty(&w_ctx.ppa);
  76. for (i = 0, valid_entries = 0; i < nr_entries; i++) {
  77. if (lba_list[i] == ADDR_EMPTY)
  78. continue;
  79. w_ctx.lba = lba_list[i];
  80. pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
  81. pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_line, pos);
  82. data += PBLK_EXPOSED_PAGE_SIZE;
  83. valid_entries++;
  84. }
  85. WARN_ONCE(nr_rec_entries != valid_entries,
  86. "pblk: inconsistent GC write\n");
  87. #ifdef CONFIG_NVM_DEBUG
  88. atomic_long_add(valid_entries, &pblk->inflight_writes);
  89. atomic_long_add(valid_entries, &pblk->recov_gc_writes);
  90. #endif
  91. pblk_write_should_kick(pblk);
  92. return NVM_IO_OK;
  93. }