rrpc.h 6.3 KB

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