rrpc.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 rrpc_lun *rlun;
  48. struct list_head prio;
  49. #define MAX_INVALID_PAGES_STORAGE 8
  50. /* Bitmap for invalid page intries */
  51. unsigned long invalid_pages[MAX_INVALID_PAGES_STORAGE];
  52. /* points to the next writable page within a block */
  53. unsigned int next_page;
  54. /* number of pages that are invalid, wrt host page size */
  55. unsigned int nr_invalid_pages;
  56. spinlock_t lock;
  57. atomic_t data_cmnt_size; /* data pages committed to stable storage */
  58. };
  59. struct rrpc_lun {
  60. struct rrpc *rrpc;
  61. struct nvm_lun *parent;
  62. struct rrpc_block *cur, *gc_cur;
  63. struct rrpc_block *blocks; /* Reference to block allocation */
  64. struct list_head prio_list; /* Blocks that may be GC'ed */
  65. struct list_head wblk_list; /* Queued blocks to be written to */
  66. struct work_struct ws_gc;
  67. spinlock_t lock;
  68. };
  69. struct rrpc {
  70. /* instance must be kept in top to resolve rrpc in unprep */
  71. struct nvm_tgt_instance instance;
  72. struct nvm_dev *dev;
  73. struct gendisk *disk;
  74. sector_t soffset; /* logical sector offset */
  75. u64 poffset; /* physical page offset */
  76. int lun_offset;
  77. int nr_luns;
  78. struct rrpc_lun *luns;
  79. /* calculated values */
  80. unsigned long long nr_sects;
  81. unsigned long total_blocks;
  82. /* Write strategy variables. Move these into each for structure for each
  83. * strategy
  84. */
  85. atomic_t next_lun; /* Whenever a page is written, this is updated
  86. * to point to the next write lun
  87. */
  88. spinlock_t bio_lock;
  89. struct bio_list requeue_bios;
  90. struct work_struct ws_requeue;
  91. /* Simple translation map of logical addresses to physical addresses.
  92. * The logical addresses is known by the host system, while the physical
  93. * addresses are used when writing to the disk block device.
  94. */
  95. struct rrpc_addr *trans_map;
  96. /* also store a reverse map for garbage collection */
  97. struct rrpc_rev_addr *rev_trans_map;
  98. spinlock_t rev_lock;
  99. struct rrpc_inflight inflights;
  100. mempool_t *addr_pool;
  101. mempool_t *page_pool;
  102. mempool_t *gcb_pool;
  103. mempool_t *rq_pool;
  104. struct timer_list gc_timer;
  105. struct workqueue_struct *krqd_wq;
  106. struct workqueue_struct *kgc_wq;
  107. };
  108. struct rrpc_block_gc {
  109. struct rrpc *rrpc;
  110. struct rrpc_block *rblk;
  111. struct work_struct ws_gc;
  112. };
  113. /* Logical to physical mapping */
  114. struct rrpc_addr {
  115. u64 addr;
  116. struct rrpc_block *rblk;
  117. };
  118. /* Physical to logical mapping */
  119. struct rrpc_rev_addr {
  120. u64 addr;
  121. };
  122. static inline struct rrpc_block *rrpc_get_rblk(struct rrpc_lun *rlun,
  123. int blk_id)
  124. {
  125. struct rrpc *rrpc = rlun->rrpc;
  126. int lun_blk = blk_id % rrpc->dev->blks_per_lun;
  127. return &rlun->blocks[lun_blk];
  128. }
  129. static inline sector_t rrpc_get_laddr(struct bio *bio)
  130. {
  131. return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  132. }
  133. static inline unsigned int rrpc_get_pages(struct bio *bio)
  134. {
  135. return bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
  136. }
  137. static inline sector_t rrpc_get_sector(sector_t laddr)
  138. {
  139. return laddr * NR_PHY_IN_LOG;
  140. }
  141. static inline int request_intersects(struct rrpc_inflight_rq *r,
  142. sector_t laddr_start, sector_t laddr_end)
  143. {
  144. return (laddr_end >= r->l_start) && (laddr_start <= r->l_end);
  145. }
  146. static int __rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  147. unsigned int pages, struct rrpc_inflight_rq *r)
  148. {
  149. sector_t laddr_end = laddr + pages - 1;
  150. struct rrpc_inflight_rq *rtmp;
  151. WARN_ON(irqs_disabled());
  152. spin_lock_irq(&rrpc->inflights.lock);
  153. list_for_each_entry(rtmp, &rrpc->inflights.reqs, list) {
  154. if (unlikely(request_intersects(rtmp, laddr, laddr_end))) {
  155. /* existing, overlapping request, come back later */
  156. spin_unlock_irq(&rrpc->inflights.lock);
  157. return 1;
  158. }
  159. }
  160. r->l_start = laddr;
  161. r->l_end = laddr_end;
  162. list_add_tail(&r->list, &rrpc->inflights.reqs);
  163. spin_unlock_irq(&rrpc->inflights.lock);
  164. return 0;
  165. }
  166. static inline int rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  167. unsigned int pages,
  168. struct rrpc_inflight_rq *r)
  169. {
  170. BUG_ON((laddr + pages) > rrpc->nr_sects);
  171. return __rrpc_lock_laddr(rrpc, laddr, pages, r);
  172. }
  173. static inline struct rrpc_inflight_rq *rrpc_get_inflight_rq(struct nvm_rq *rqd)
  174. {
  175. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  176. return &rrqd->inflight_rq;
  177. }
  178. static inline int rrpc_lock_rq(struct rrpc *rrpc, struct bio *bio,
  179. struct nvm_rq *rqd)
  180. {
  181. sector_t laddr = rrpc_get_laddr(bio);
  182. unsigned int pages = rrpc_get_pages(bio);
  183. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  184. return rrpc_lock_laddr(rrpc, laddr, pages, r);
  185. }
  186. static inline void rrpc_unlock_laddr(struct rrpc *rrpc,
  187. struct rrpc_inflight_rq *r)
  188. {
  189. unsigned long flags;
  190. spin_lock_irqsave(&rrpc->inflights.lock, flags);
  191. list_del_init(&r->list);
  192. spin_unlock_irqrestore(&rrpc->inflights.lock, flags);
  193. }
  194. static inline void rrpc_unlock_rq(struct rrpc *rrpc, struct nvm_rq *rqd)
  195. {
  196. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  197. uint8_t pages = rqd->nr_ppas;
  198. BUG_ON((r->l_start + pages) > rrpc->nr_sects);
  199. rrpc_unlock_laddr(rrpc, r);
  200. }
  201. #endif /* RRPC_H_ */