rrpc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (C) 2015 IT University of Copenhagen
  3. * Initial release: Matias Bjorling <m@bjorling.me>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version
  7. * 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * Implementation of a Round-robin page-based Hybrid FTL for Open-channel SSDs.
  15. */
  16. #ifndef RRPC_H_
  17. #define RRPC_H_
  18. #include <linux/blkdev.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/bio.h>
  21. #include <linux/module.h>
  22. #include <linux/kthread.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/lightnvm.h>
  25. /* Run only GC if less than 1/X blocks are free */
  26. #define GC_LIMIT_INVERSE 10
  27. #define GC_TIME_SECS 100
  28. #define RRPC_SECTOR (512)
  29. #define RRPC_EXPOSED_PAGE_SIZE (4096)
  30. #define NR_PHY_IN_LOG (RRPC_EXPOSED_PAGE_SIZE / RRPC_SECTOR)
  31. struct rrpc_inflight {
  32. struct list_head reqs;
  33. spinlock_t lock;
  34. };
  35. struct rrpc_inflight_rq {
  36. struct list_head list;
  37. sector_t l_start;
  38. sector_t l_end;
  39. };
  40. struct rrpc_rq {
  41. struct rrpc_inflight_rq inflight_rq;
  42. struct rrpc_addr *addr;
  43. unsigned long flags;
  44. };
  45. struct rrpc_block {
  46. struct nvm_block *parent;
  47. struct list_head prio;
  48. #define MAX_INVALID_PAGES_STORAGE 8
  49. /* Bitmap for invalid page intries */
  50. unsigned long invalid_pages[MAX_INVALID_PAGES_STORAGE];
  51. /* points to the next writable page within a block */
  52. unsigned int next_page;
  53. /* number of pages that are invalid, wrt host page size */
  54. unsigned int nr_invalid_pages;
  55. spinlock_t lock;
  56. atomic_t data_cmnt_size; /* data pages committed to stable storage */
  57. };
  58. struct rrpc_lun {
  59. struct rrpc *rrpc;
  60. struct nvm_lun *parent;
  61. struct rrpc_block *cur, *gc_cur;
  62. struct rrpc_block *blocks; /* Reference to block allocation */
  63. struct list_head prio_list; /* Blocks that may be GC'ed */
  64. struct work_struct ws_gc;
  65. spinlock_t lock;
  66. };
  67. struct rrpc {
  68. /* instance must be kept in top to resolve rrpc in unprep */
  69. struct nvm_tgt_instance instance;
  70. struct nvm_dev *dev;
  71. struct gendisk *disk;
  72. u64 poffset; /* physical page offset */
  73. int lun_offset;
  74. int nr_luns;
  75. struct rrpc_lun *luns;
  76. /* calculated values */
  77. unsigned long long nr_pages;
  78. unsigned long total_blocks;
  79. /* Write strategy variables. Move these into each for structure for each
  80. * strategy
  81. */
  82. atomic_t next_lun; /* Whenever a page is written, this is updated
  83. * to point to the next write lun
  84. */
  85. spinlock_t bio_lock;
  86. struct bio_list requeue_bios;
  87. struct work_struct ws_requeue;
  88. /* Simple translation map of logical addresses to physical addresses.
  89. * The logical addresses is known by the host system, while the physical
  90. * addresses are used when writing to the disk block device.
  91. */
  92. struct rrpc_addr *trans_map;
  93. /* also store a reverse map for garbage collection */
  94. struct rrpc_rev_addr *rev_trans_map;
  95. spinlock_t rev_lock;
  96. struct rrpc_inflight inflights;
  97. mempool_t *addr_pool;
  98. mempool_t *page_pool;
  99. mempool_t *gcb_pool;
  100. mempool_t *rq_pool;
  101. struct timer_list gc_timer;
  102. struct workqueue_struct *krqd_wq;
  103. struct workqueue_struct *kgc_wq;
  104. };
  105. struct rrpc_block_gc {
  106. struct rrpc *rrpc;
  107. struct rrpc_block *rblk;
  108. struct work_struct ws_gc;
  109. };
  110. /* Logical to physical mapping */
  111. struct rrpc_addr {
  112. u64 addr;
  113. struct rrpc_block *rblk;
  114. };
  115. /* Physical to logical mapping */
  116. struct rrpc_rev_addr {
  117. u64 addr;
  118. };
  119. static inline sector_t rrpc_get_laddr(struct bio *bio)
  120. {
  121. return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  122. }
  123. static inline unsigned int rrpc_get_pages(struct bio *bio)
  124. {
  125. return bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
  126. }
  127. static inline sector_t rrpc_get_sector(sector_t laddr)
  128. {
  129. return laddr * NR_PHY_IN_LOG;
  130. }
  131. static inline int request_intersects(struct rrpc_inflight_rq *r,
  132. sector_t laddr_start, sector_t laddr_end)
  133. {
  134. return (laddr_end >= r->l_start && laddr_end <= r->l_end) &&
  135. (laddr_start >= r->l_start && laddr_start <= r->l_end);
  136. }
  137. static int __rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  138. unsigned pages, struct rrpc_inflight_rq *r)
  139. {
  140. sector_t laddr_end = laddr + pages - 1;
  141. struct rrpc_inflight_rq *rtmp;
  142. spin_lock_irq(&rrpc->inflights.lock);
  143. list_for_each_entry(rtmp, &rrpc->inflights.reqs, list) {
  144. if (unlikely(request_intersects(rtmp, laddr, laddr_end))) {
  145. /* existing, overlapping request, come back later */
  146. spin_unlock_irq(&rrpc->inflights.lock);
  147. return 1;
  148. }
  149. }
  150. r->l_start = laddr;
  151. r->l_end = laddr_end;
  152. list_add_tail(&r->list, &rrpc->inflights.reqs);
  153. spin_unlock_irq(&rrpc->inflights.lock);
  154. return 0;
  155. }
  156. static inline int rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  157. unsigned pages,
  158. struct rrpc_inflight_rq *r)
  159. {
  160. BUG_ON((laddr + pages) > rrpc->nr_pages);
  161. return __rrpc_lock_laddr(rrpc, laddr, pages, r);
  162. }
  163. static inline struct rrpc_inflight_rq *rrpc_get_inflight_rq(struct nvm_rq *rqd)
  164. {
  165. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  166. return &rrqd->inflight_rq;
  167. }
  168. static inline int rrpc_lock_rq(struct rrpc *rrpc, struct bio *bio,
  169. struct nvm_rq *rqd)
  170. {
  171. sector_t laddr = rrpc_get_laddr(bio);
  172. unsigned int pages = rrpc_get_pages(bio);
  173. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  174. return rrpc_lock_laddr(rrpc, laddr, pages, r);
  175. }
  176. static inline void rrpc_unlock_laddr(struct rrpc *rrpc,
  177. struct rrpc_inflight_rq *r)
  178. {
  179. unsigned long flags;
  180. spin_lock_irqsave(&rrpc->inflights.lock, flags);
  181. list_del_init(&r->list);
  182. spin_unlock_irqrestore(&rrpc->inflights.lock, flags);
  183. }
  184. static inline void rrpc_unlock_rq(struct rrpc *rrpc, struct nvm_rq *rqd)
  185. {
  186. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  187. uint8_t pages = rqd->nr_pages;
  188. BUG_ON((r->l_start + pages) > rrpc->nr_pages);
  189. rrpc_unlock_laddr(rrpc, r);
  190. }
  191. #endif /* RRPC_H_ */