rrpc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. unsigned long flags;
  43. };
  44. struct rrpc_block {
  45. int id; /* id inside of LUN */
  46. struct rrpc_lun *rlun;
  47. struct list_head prio; /* LUN CG list */
  48. struct list_head list; /* LUN free, used, bb list */
  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. int state;
  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. int id;
  63. struct ppa_addr bppa;
  64. struct rrpc_block *cur, *gc_cur;
  65. struct rrpc_block *blocks; /* Reference to block allocation */
  66. struct list_head prio_list; /* Blocks that may be GC'ed */
  67. struct list_head wblk_list; /* Queued blocks to be written to */
  68. /* lun block lists */
  69. struct list_head used_list; /* In-use blocks */
  70. struct list_head free_list; /* Not used blocks i.e. released
  71. * and ready for use
  72. */
  73. struct list_head bb_list; /* Bad blocks. Mutually exclusive with
  74. * free_list and used_list
  75. */
  76. unsigned int nr_free_blocks; /* Number of unused blocks */
  77. struct work_struct ws_gc;
  78. int reserved_blocks;
  79. spinlock_t lock;
  80. };
  81. struct rrpc {
  82. /* instance must be kept in top to resolve rrpc in unprep */
  83. struct nvm_tgt_instance instance;
  84. struct nvm_tgt_dev *dev;
  85. struct gendisk *disk;
  86. sector_t soffset; /* logical sector offset */
  87. int nr_luns;
  88. struct rrpc_lun *luns;
  89. /* calculated values */
  90. unsigned long long nr_sects;
  91. /* Write strategy variables. Move these into each for structure for each
  92. * strategy
  93. */
  94. atomic_t next_lun; /* Whenever a page is written, this is updated
  95. * to point to the next write lun
  96. */
  97. spinlock_t bio_lock;
  98. struct bio_list requeue_bios;
  99. struct work_struct ws_requeue;
  100. /* Simple translation map of logical addresses to physical addresses.
  101. * The logical addresses is known by the host system, while the physical
  102. * addresses are used when writing to the disk block device.
  103. */
  104. struct rrpc_addr *trans_map;
  105. /* also store a reverse map for garbage collection */
  106. struct rrpc_rev_addr *rev_trans_map;
  107. spinlock_t rev_lock;
  108. struct rrpc_inflight inflights;
  109. mempool_t *addr_pool;
  110. mempool_t *page_pool;
  111. mempool_t *gcb_pool;
  112. mempool_t *rq_pool;
  113. struct timer_list gc_timer;
  114. struct workqueue_struct *krqd_wq;
  115. struct workqueue_struct *kgc_wq;
  116. };
  117. struct rrpc_block_gc {
  118. struct rrpc *rrpc;
  119. struct rrpc_block *rblk;
  120. struct work_struct ws_gc;
  121. };
  122. /* Logical to physical mapping */
  123. struct rrpc_addr {
  124. u64 addr;
  125. struct rrpc_block *rblk;
  126. };
  127. /* Physical to logical mapping */
  128. struct rrpc_rev_addr {
  129. u64 addr;
  130. };
  131. static inline struct ppa_addr rrpc_linear_to_generic_addr(struct nvm_geo *geo,
  132. struct ppa_addr r)
  133. {
  134. struct ppa_addr l;
  135. int secs, pgs;
  136. sector_t ppa = r.ppa;
  137. l.ppa = 0;
  138. div_u64_rem(ppa, geo->sec_per_pg, &secs);
  139. l.g.sec = secs;
  140. sector_div(ppa, geo->sec_per_pg);
  141. div_u64_rem(ppa, geo->pgs_per_blk, &pgs);
  142. l.g.pg = pgs;
  143. return l;
  144. }
  145. static inline struct ppa_addr rrpc_recov_addr(struct nvm_tgt_dev *dev, u64 pba)
  146. {
  147. return linear_to_generic_addr(&dev->geo, pba);
  148. }
  149. static inline u64 rrpc_blk_to_ppa(struct rrpc *rrpc, struct rrpc_block *rblk)
  150. {
  151. struct nvm_tgt_dev *dev = rrpc->dev;
  152. struct nvm_geo *geo = &dev->geo;
  153. struct rrpc_lun *rlun = rblk->rlun;
  154. return (rlun->id * geo->sec_per_lun) + (rblk->id * geo->sec_per_blk);
  155. }
  156. static inline sector_t rrpc_get_laddr(struct bio *bio)
  157. {
  158. return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  159. }
  160. static inline unsigned int rrpc_get_pages(struct bio *bio)
  161. {
  162. return bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
  163. }
  164. static inline sector_t rrpc_get_sector(sector_t laddr)
  165. {
  166. return laddr * NR_PHY_IN_LOG;
  167. }
  168. static inline int request_intersects(struct rrpc_inflight_rq *r,
  169. sector_t laddr_start, sector_t laddr_end)
  170. {
  171. return (laddr_end >= r->l_start) && (laddr_start <= r->l_end);
  172. }
  173. static int __rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  174. unsigned int pages, struct rrpc_inflight_rq *r)
  175. {
  176. sector_t laddr_end = laddr + pages - 1;
  177. struct rrpc_inflight_rq *rtmp;
  178. WARN_ON(irqs_disabled());
  179. spin_lock_irq(&rrpc->inflights.lock);
  180. list_for_each_entry(rtmp, &rrpc->inflights.reqs, list) {
  181. if (unlikely(request_intersects(rtmp, laddr, laddr_end))) {
  182. /* existing, overlapping request, come back later */
  183. spin_unlock_irq(&rrpc->inflights.lock);
  184. return 1;
  185. }
  186. }
  187. r->l_start = laddr;
  188. r->l_end = laddr_end;
  189. list_add_tail(&r->list, &rrpc->inflights.reqs);
  190. spin_unlock_irq(&rrpc->inflights.lock);
  191. return 0;
  192. }
  193. static inline int rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
  194. unsigned int pages,
  195. struct rrpc_inflight_rq *r)
  196. {
  197. BUG_ON((laddr + pages) > rrpc->nr_sects);
  198. return __rrpc_lock_laddr(rrpc, laddr, pages, r);
  199. }
  200. static inline struct rrpc_inflight_rq *rrpc_get_inflight_rq(struct nvm_rq *rqd)
  201. {
  202. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  203. return &rrqd->inflight_rq;
  204. }
  205. static inline int rrpc_lock_rq(struct rrpc *rrpc, struct bio *bio,
  206. struct nvm_rq *rqd)
  207. {
  208. sector_t laddr = rrpc_get_laddr(bio);
  209. unsigned int pages = rrpc_get_pages(bio);
  210. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  211. return rrpc_lock_laddr(rrpc, laddr, pages, r);
  212. }
  213. static inline void rrpc_unlock_laddr(struct rrpc *rrpc,
  214. struct rrpc_inflight_rq *r)
  215. {
  216. unsigned long flags;
  217. spin_lock_irqsave(&rrpc->inflights.lock, flags);
  218. list_del_init(&r->list);
  219. spin_unlock_irqrestore(&rrpc->inflights.lock, flags);
  220. }
  221. static inline void rrpc_unlock_rq(struct rrpc *rrpc, struct nvm_rq *rqd)
  222. {
  223. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  224. uint8_t pages = rqd->nr_ppas;
  225. BUG_ON((r->l_start + pages) > rrpc->nr_sects);
  226. rrpc_unlock_laddr(rrpc, r);
  227. }
  228. #endif /* RRPC_H_ */