rrpc.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. sector_t soffset; /* logical sector offset */
  82. u64 poffset; /* physical page offset */
  83. int lun_offset;
  84. int nr_luns;
  85. struct rrpc_lun *luns;
  86. /* calculated values */
  87. unsigned long long nr_sects;
  88. unsigned long total_blocks;
  89. /* Write strategy variables. Move these into each for structure for each
  90. * strategy
  91. */
  92. atomic_t next_lun; /* Whenever a page is written, this is updated
  93. * to point to the next write lun
  94. */
  95. spinlock_t bio_lock;
  96. struct bio_list requeue_bios;
  97. struct work_struct ws_requeue;
  98. /* Simple translation map of logical addresses to physical addresses.
  99. * The logical addresses is known by the host system, while the physical
  100. * addresses are used when writing to the disk block device.
  101. */
  102. struct rrpc_addr *trans_map;
  103. /* also store a reverse map for garbage collection */
  104. struct rrpc_rev_addr *rev_trans_map;
  105. spinlock_t rev_lock;
  106. struct rrpc_inflight inflights;
  107. mempool_t *addr_pool;
  108. mempool_t *page_pool;
  109. mempool_t *gcb_pool;
  110. mempool_t *rq_pool;
  111. struct timer_list gc_timer;
  112. struct workqueue_struct *krqd_wq;
  113. struct workqueue_struct *kgc_wq;
  114. };
  115. struct rrpc_block_gc {
  116. struct rrpc *rrpc;
  117. struct rrpc_block *rblk;
  118. struct work_struct ws_gc;
  119. };
  120. /* Logical to physical mapping */
  121. struct rrpc_addr {
  122. u64 addr;
  123. struct rrpc_block *rblk;
  124. };
  125. /* Physical to logical mapping */
  126. struct rrpc_rev_addr {
  127. u64 addr;
  128. };
  129. static inline struct rrpc_block *rrpc_get_rblk(struct rrpc_lun *rlun,
  130. int blk_id)
  131. {
  132. struct rrpc *rrpc = rlun->rrpc;
  133. int lun_blk = blk_id % rrpc->dev->blks_per_lun;
  134. return &rlun->blocks[lun_blk];
  135. }
  136. static inline sector_t rrpc_get_laddr(struct bio *bio)
  137. {
  138. return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  139. }
  140. static inline unsigned int rrpc_get_pages(struct bio *bio)
  141. {
  142. return bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
  143. }
  144. static inline sector_t rrpc_get_sector(sector_t laddr)
  145. {
  146. return laddr * NR_PHY_IN_LOG;
  147. }
  148. static inline int request_intersects(struct rrpc_inflight_rq *r,
  149. sector_t laddr_start, sector_t laddr_end)
  150. {
  151. return (laddr_end >= r->l_start) && (laddr_start <= r->l_end);
  152. }
  153. static int __rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  154. unsigned pages, struct rrpc_inflight_rq *r)
  155. {
  156. sector_t laddr_end = laddr + pages - 1;
  157. struct rrpc_inflight_rq *rtmp;
  158. WARN_ON(irqs_disabled());
  159. spin_lock_irq(&rrpc->inflights.lock);
  160. list_for_each_entry(rtmp, &rrpc->inflights.reqs, list) {
  161. if (unlikely(request_intersects(rtmp, laddr, laddr_end))) {
  162. /* existing, overlapping request, come back later */
  163. spin_unlock_irq(&rrpc->inflights.lock);
  164. return 1;
  165. }
  166. }
  167. r->l_start = laddr;
  168. r->l_end = laddr_end;
  169. list_add_tail(&r->list, &rrpc->inflights.reqs);
  170. spin_unlock_irq(&rrpc->inflights.lock);
  171. return 0;
  172. }
  173. static inline int rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  174. unsigned pages,
  175. struct rrpc_inflight_rq *r)
  176. {
  177. BUG_ON((laddr + pages) > rrpc->nr_sects);
  178. return __rrpc_lock_laddr(rrpc, laddr, pages, r);
  179. }
  180. static inline struct rrpc_inflight_rq *rrpc_get_inflight_rq(struct nvm_rq *rqd)
  181. {
  182. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  183. return &rrqd->inflight_rq;
  184. }
  185. static inline int rrpc_lock_rq(struct rrpc *rrpc, struct bio *bio,
  186. struct nvm_rq *rqd)
  187. {
  188. sector_t laddr = rrpc_get_laddr(bio);
  189. unsigned int pages = rrpc_get_pages(bio);
  190. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  191. return rrpc_lock_laddr(rrpc, laddr, pages, r);
  192. }
  193. static inline void rrpc_unlock_laddr(struct rrpc *rrpc,
  194. struct rrpc_inflight_rq *r)
  195. {
  196. unsigned long flags;
  197. spin_lock_irqsave(&rrpc->inflights.lock, flags);
  198. list_del_init(&r->list);
  199. spin_unlock_irqrestore(&rrpc->inflights.lock, flags);
  200. }
  201. static inline void rrpc_unlock_rq(struct rrpc *rrpc, struct nvm_rq *rqd)
  202. {
  203. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  204. uint8_t pages = rqd->nr_pages;
  205. BUG_ON((r->l_start + pages) > rrpc->nr_sects);
  206. rrpc_unlock_laddr(rrpc, r);
  207. }
  208. #endif /* RRPC_H_ */