pblk-core.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. /*
  2. * Copyright (C) 2016 CNEX Labs
  3. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  4. * Matias Bjorling <matias@cnexlabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * pblk-core.c - pblk's core functionality
  16. *
  17. */
  18. #include "pblk.h"
  19. static void pblk_line_mark_bb(struct work_struct *work)
  20. {
  21. struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
  22. ws);
  23. struct pblk *pblk = line_ws->pblk;
  24. struct nvm_tgt_dev *dev = pblk->dev;
  25. struct ppa_addr *ppa = line_ws->priv;
  26. int ret;
  27. ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
  28. if (ret) {
  29. struct pblk_line *line;
  30. int pos;
  31. line = &pblk->lines[pblk_dev_ppa_to_line(*ppa)];
  32. pos = pblk_dev_ppa_to_pos(&dev->geo, *ppa);
  33. pr_err("pblk: failed to mark bb, line:%d, pos:%d\n",
  34. line->id, pos);
  35. }
  36. kfree(ppa);
  37. mempool_free(line_ws, pblk->gen_ws_pool);
  38. }
  39. static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
  40. struct ppa_addr *ppa)
  41. {
  42. struct nvm_tgt_dev *dev = pblk->dev;
  43. struct nvm_geo *geo = &dev->geo;
  44. int pos = pblk_dev_ppa_to_pos(geo, *ppa);
  45. pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos);
  46. atomic_long_inc(&pblk->erase_failed);
  47. atomic_dec(&line->blk_in_line);
  48. if (test_and_set_bit(pos, line->blk_bitmap))
  49. pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n",
  50. line->id, pos);
  51. pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
  52. GFP_ATOMIC, pblk->bb_wq);
  53. }
  54. static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
  55. {
  56. struct pblk_line *line;
  57. line = &pblk->lines[pblk_dev_ppa_to_line(rqd->ppa_addr)];
  58. atomic_dec(&line->left_seblks);
  59. if (rqd->error) {
  60. struct ppa_addr *ppa;
  61. ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
  62. if (!ppa)
  63. return;
  64. *ppa = rqd->ppa_addr;
  65. pblk_mark_bb(pblk, line, ppa);
  66. }
  67. atomic_dec(&pblk->inflight_io);
  68. }
  69. /* Erase completion assumes that only one block is erased at the time */
  70. static void pblk_end_io_erase(struct nvm_rq *rqd)
  71. {
  72. struct pblk *pblk = rqd->private;
  73. __pblk_end_io_erase(pblk, rqd);
  74. mempool_free(rqd, pblk->e_rq_pool);
  75. }
  76. void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
  77. u64 paddr)
  78. {
  79. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  80. struct list_head *move_list = NULL;
  81. /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
  82. * table is modified with reclaimed sectors, a check is done to endure
  83. * that newer updates are not overwritten.
  84. */
  85. spin_lock(&line->lock);
  86. WARN_ON(line->state == PBLK_LINESTATE_FREE);
  87. if (test_and_set_bit(paddr, line->invalid_bitmap)) {
  88. WARN_ONCE(1, "pblk: double invalidate\n");
  89. spin_unlock(&line->lock);
  90. return;
  91. }
  92. le32_add_cpu(line->vsc, -1);
  93. if (line->state == PBLK_LINESTATE_CLOSED)
  94. move_list = pblk_line_gc_list(pblk, line);
  95. spin_unlock(&line->lock);
  96. if (move_list) {
  97. spin_lock(&l_mg->gc_lock);
  98. spin_lock(&line->lock);
  99. /* Prevent moving a line that has just been chosen for GC */
  100. if (line->state == PBLK_LINESTATE_GC) {
  101. spin_unlock(&line->lock);
  102. spin_unlock(&l_mg->gc_lock);
  103. return;
  104. }
  105. spin_unlock(&line->lock);
  106. list_move_tail(&line->list, move_list);
  107. spin_unlock(&l_mg->gc_lock);
  108. }
  109. }
  110. void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
  111. {
  112. struct pblk_line *line;
  113. u64 paddr;
  114. int line_id;
  115. #ifdef CONFIG_NVM_DEBUG
  116. /* Callers must ensure that the ppa points to a device address */
  117. BUG_ON(pblk_addr_in_cache(ppa));
  118. BUG_ON(pblk_ppa_empty(ppa));
  119. #endif
  120. line_id = pblk_tgt_ppa_to_line(ppa);
  121. line = &pblk->lines[line_id];
  122. paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
  123. __pblk_map_invalidate(pblk, line, paddr);
  124. }
  125. static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
  126. unsigned int nr_secs)
  127. {
  128. sector_t lba;
  129. spin_lock(&pblk->trans_lock);
  130. for (lba = slba; lba < slba + nr_secs; lba++) {
  131. struct ppa_addr ppa;
  132. ppa = pblk_trans_map_get(pblk, lba);
  133. if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
  134. pblk_map_invalidate(pblk, ppa);
  135. pblk_ppa_set_empty(&ppa);
  136. pblk_trans_map_set(pblk, lba, ppa);
  137. }
  138. spin_unlock(&pblk->trans_lock);
  139. }
  140. /* Caller must guarantee that the request is a valid type */
  141. struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
  142. {
  143. mempool_t *pool;
  144. struct nvm_rq *rqd;
  145. int rq_size;
  146. switch (type) {
  147. case PBLK_WRITE:
  148. case PBLK_WRITE_INT:
  149. pool = pblk->w_rq_pool;
  150. rq_size = pblk_w_rq_size;
  151. break;
  152. case PBLK_READ:
  153. pool = pblk->r_rq_pool;
  154. rq_size = pblk_g_rq_size;
  155. break;
  156. default:
  157. pool = pblk->e_rq_pool;
  158. rq_size = pblk_g_rq_size;
  159. }
  160. rqd = mempool_alloc(pool, GFP_KERNEL);
  161. memset(rqd, 0, rq_size);
  162. return rqd;
  163. }
  164. /* Typically used on completion path. Cannot guarantee request consistency */
  165. void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
  166. {
  167. struct nvm_tgt_dev *dev = pblk->dev;
  168. mempool_t *pool;
  169. switch (type) {
  170. case PBLK_WRITE:
  171. kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
  172. case PBLK_WRITE_INT:
  173. pool = pblk->w_rq_pool;
  174. break;
  175. case PBLK_READ:
  176. pool = pblk->r_rq_pool;
  177. break;
  178. case PBLK_ERASE:
  179. pool = pblk->e_rq_pool;
  180. break;
  181. default:
  182. pr_err("pblk: trying to free unknown rqd type\n");
  183. return;
  184. }
  185. nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
  186. mempool_free(rqd, pool);
  187. }
  188. void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
  189. int nr_pages)
  190. {
  191. struct bio_vec bv;
  192. int i;
  193. WARN_ON(off + nr_pages != bio->bi_vcnt);
  194. for (i = off; i < nr_pages + off; i++) {
  195. bv = bio->bi_io_vec[i];
  196. mempool_free(bv.bv_page, pblk->page_bio_pool);
  197. }
  198. }
  199. int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
  200. int nr_pages)
  201. {
  202. struct request_queue *q = pblk->dev->q;
  203. struct page *page;
  204. int i, ret;
  205. for (i = 0; i < nr_pages; i++) {
  206. page = mempool_alloc(pblk->page_bio_pool, flags);
  207. ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
  208. if (ret != PBLK_EXPOSED_PAGE_SIZE) {
  209. pr_err("pblk: could not add page to bio\n");
  210. mempool_free(page, pblk->page_bio_pool);
  211. goto err;
  212. }
  213. }
  214. return 0;
  215. err:
  216. pblk_bio_free_pages(pblk, bio, 0, i - 1);
  217. return -1;
  218. }
  219. static void pblk_write_kick(struct pblk *pblk)
  220. {
  221. wake_up_process(pblk->writer_ts);
  222. mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
  223. }
  224. void pblk_write_timer_fn(unsigned long data)
  225. {
  226. struct pblk *pblk = (struct pblk *)data;
  227. /* kick the write thread every tick to flush outstanding data */
  228. pblk_write_kick(pblk);
  229. }
  230. void pblk_write_should_kick(struct pblk *pblk)
  231. {
  232. unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
  233. if (secs_avail >= pblk->min_write_pgs)
  234. pblk_write_kick(pblk);
  235. }
  236. void pblk_end_io_sync(struct nvm_rq *rqd)
  237. {
  238. struct completion *waiting = rqd->private;
  239. complete(waiting);
  240. }
  241. static void pblk_wait_for_meta(struct pblk *pblk)
  242. {
  243. do {
  244. if (!atomic_read(&pblk->inflight_io))
  245. break;
  246. schedule();
  247. } while (1);
  248. }
  249. static void pblk_flush_writer(struct pblk *pblk)
  250. {
  251. pblk_rb_flush(&pblk->rwb);
  252. do {
  253. if (!pblk_rb_sync_count(&pblk->rwb))
  254. break;
  255. pblk_write_kick(pblk);
  256. schedule();
  257. } while (1);
  258. }
  259. struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
  260. {
  261. struct pblk_line_meta *lm = &pblk->lm;
  262. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  263. struct list_head *move_list = NULL;
  264. int vsc = le32_to_cpu(*line->vsc);
  265. lockdep_assert_held(&line->lock);
  266. if (!vsc) {
  267. if (line->gc_group != PBLK_LINEGC_FULL) {
  268. line->gc_group = PBLK_LINEGC_FULL;
  269. move_list = &l_mg->gc_full_list;
  270. }
  271. } else if (vsc < lm->high_thrs) {
  272. if (line->gc_group != PBLK_LINEGC_HIGH) {
  273. line->gc_group = PBLK_LINEGC_HIGH;
  274. move_list = &l_mg->gc_high_list;
  275. }
  276. } else if (vsc < lm->mid_thrs) {
  277. if (line->gc_group != PBLK_LINEGC_MID) {
  278. line->gc_group = PBLK_LINEGC_MID;
  279. move_list = &l_mg->gc_mid_list;
  280. }
  281. } else if (vsc < line->sec_in_line) {
  282. if (line->gc_group != PBLK_LINEGC_LOW) {
  283. line->gc_group = PBLK_LINEGC_LOW;
  284. move_list = &l_mg->gc_low_list;
  285. }
  286. } else if (vsc == line->sec_in_line) {
  287. if (line->gc_group != PBLK_LINEGC_EMPTY) {
  288. line->gc_group = PBLK_LINEGC_EMPTY;
  289. move_list = &l_mg->gc_empty_list;
  290. }
  291. } else {
  292. line->state = PBLK_LINESTATE_CORRUPT;
  293. line->gc_group = PBLK_LINEGC_NONE;
  294. move_list = &l_mg->corrupt_list;
  295. pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
  296. line->id, vsc,
  297. line->sec_in_line,
  298. lm->high_thrs, lm->mid_thrs);
  299. }
  300. return move_list;
  301. }
  302. void pblk_discard(struct pblk *pblk, struct bio *bio)
  303. {
  304. sector_t slba = pblk_get_lba(bio);
  305. sector_t nr_secs = pblk_get_secs(bio);
  306. pblk_invalidate_range(pblk, slba, nr_secs);
  307. }
  308. void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
  309. {
  310. atomic_long_inc(&pblk->write_failed);
  311. #ifdef CONFIG_NVM_DEBUG
  312. pblk_print_failed_rqd(pblk, rqd, rqd->error);
  313. #endif
  314. }
  315. void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
  316. {
  317. /* Empty page read is not necessarily an error (e.g., L2P recovery) */
  318. if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
  319. atomic_long_inc(&pblk->read_empty);
  320. return;
  321. }
  322. switch (rqd->error) {
  323. case NVM_RSP_WARN_HIGHECC:
  324. atomic_long_inc(&pblk->read_high_ecc);
  325. break;
  326. case NVM_RSP_ERR_FAILECC:
  327. case NVM_RSP_ERR_FAILCRC:
  328. atomic_long_inc(&pblk->read_failed);
  329. break;
  330. default:
  331. pr_err("pblk: unknown read error:%d\n", rqd->error);
  332. }
  333. #ifdef CONFIG_NVM_DEBUG
  334. pblk_print_failed_rqd(pblk, rqd, rqd->error);
  335. #endif
  336. }
  337. void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
  338. {
  339. pblk->sec_per_write = sec_per_write;
  340. }
  341. int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
  342. {
  343. struct nvm_tgt_dev *dev = pblk->dev;
  344. #ifdef CONFIG_NVM_DEBUG
  345. int ret;
  346. ret = pblk_check_io(pblk, rqd);
  347. if (ret)
  348. return ret;
  349. #endif
  350. atomic_inc(&pblk->inflight_io);
  351. return nvm_submit_io(dev, rqd);
  352. }
  353. int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
  354. {
  355. struct nvm_tgt_dev *dev = pblk->dev;
  356. #ifdef CONFIG_NVM_DEBUG
  357. int ret;
  358. ret = pblk_check_io(pblk, rqd);
  359. if (ret)
  360. return ret;
  361. #endif
  362. atomic_inc(&pblk->inflight_io);
  363. return nvm_submit_io_sync(dev, rqd);
  364. }
  365. static void pblk_bio_map_addr_endio(struct bio *bio)
  366. {
  367. bio_put(bio);
  368. }
  369. struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
  370. unsigned int nr_secs, unsigned int len,
  371. int alloc_type, gfp_t gfp_mask)
  372. {
  373. struct nvm_tgt_dev *dev = pblk->dev;
  374. void *kaddr = data;
  375. struct page *page;
  376. struct bio *bio;
  377. int i, ret;
  378. if (alloc_type == PBLK_KMALLOC_META)
  379. return bio_map_kern(dev->q, kaddr, len, gfp_mask);
  380. bio = bio_kmalloc(gfp_mask, nr_secs);
  381. if (!bio)
  382. return ERR_PTR(-ENOMEM);
  383. for (i = 0; i < nr_secs; i++) {
  384. page = vmalloc_to_page(kaddr);
  385. if (!page) {
  386. pr_err("pblk: could not map vmalloc bio\n");
  387. bio_put(bio);
  388. bio = ERR_PTR(-ENOMEM);
  389. goto out;
  390. }
  391. ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
  392. if (ret != PAGE_SIZE) {
  393. pr_err("pblk: could not add page to bio\n");
  394. bio_put(bio);
  395. bio = ERR_PTR(-ENOMEM);
  396. goto out;
  397. }
  398. kaddr += PAGE_SIZE;
  399. }
  400. bio->bi_end_io = pblk_bio_map_addr_endio;
  401. out:
  402. return bio;
  403. }
  404. int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
  405. unsigned long secs_to_flush)
  406. {
  407. int max = pblk->sec_per_write;
  408. int min = pblk->min_write_pgs;
  409. int secs_to_sync = 0;
  410. if (secs_avail >= max)
  411. secs_to_sync = max;
  412. else if (secs_avail >= min)
  413. secs_to_sync = min * (secs_avail / min);
  414. else if (secs_to_flush)
  415. secs_to_sync = min;
  416. return secs_to_sync;
  417. }
  418. void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  419. {
  420. u64 addr;
  421. int i;
  422. spin_lock(&line->lock);
  423. addr = find_next_zero_bit(line->map_bitmap,
  424. pblk->lm.sec_per_line, line->cur_sec);
  425. line->cur_sec = addr - nr_secs;
  426. for (i = 0; i < nr_secs; i++, line->cur_sec--)
  427. WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
  428. spin_unlock(&line->lock);
  429. }
  430. u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  431. {
  432. u64 addr;
  433. int i;
  434. lockdep_assert_held(&line->lock);
  435. /* logic error: ppa out-of-bounds. Prevent generating bad address */
  436. if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
  437. WARN(1, "pblk: page allocation out of bounds\n");
  438. nr_secs = pblk->lm.sec_per_line - line->cur_sec;
  439. }
  440. line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
  441. pblk->lm.sec_per_line, line->cur_sec);
  442. for (i = 0; i < nr_secs; i++, line->cur_sec++)
  443. WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
  444. return addr;
  445. }
  446. u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  447. {
  448. u64 addr;
  449. /* Lock needed in case a write fails and a recovery needs to remap
  450. * failed write buffer entries
  451. */
  452. spin_lock(&line->lock);
  453. addr = __pblk_alloc_page(pblk, line, nr_secs);
  454. line->left_msecs -= nr_secs;
  455. WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
  456. spin_unlock(&line->lock);
  457. return addr;
  458. }
  459. u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
  460. {
  461. u64 paddr;
  462. spin_lock(&line->lock);
  463. paddr = find_next_zero_bit(line->map_bitmap,
  464. pblk->lm.sec_per_line, line->cur_sec);
  465. spin_unlock(&line->lock);
  466. return paddr;
  467. }
  468. /*
  469. * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when
  470. * taking the per LUN semaphore.
  471. */
  472. static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line,
  473. void *emeta_buf, u64 paddr, int dir)
  474. {
  475. struct nvm_tgt_dev *dev = pblk->dev;
  476. struct nvm_geo *geo = &dev->geo;
  477. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  478. struct pblk_line_meta *lm = &pblk->lm;
  479. void *ppa_list, *meta_list;
  480. struct bio *bio;
  481. struct nvm_rq rqd;
  482. dma_addr_t dma_ppa_list, dma_meta_list;
  483. int min = pblk->min_write_pgs;
  484. int left_ppas = lm->emeta_sec[0];
  485. int id = line->id;
  486. int rq_ppas, rq_len;
  487. int cmd_op, bio_op;
  488. int i, j;
  489. int ret;
  490. if (dir == PBLK_WRITE) {
  491. bio_op = REQ_OP_WRITE;
  492. cmd_op = NVM_OP_PWRITE;
  493. } else if (dir == PBLK_READ) {
  494. bio_op = REQ_OP_READ;
  495. cmd_op = NVM_OP_PREAD;
  496. } else
  497. return -EINVAL;
  498. meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  499. &dma_meta_list);
  500. if (!meta_list)
  501. return -ENOMEM;
  502. ppa_list = meta_list + pblk_dma_meta_size;
  503. dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
  504. next_rq:
  505. memset(&rqd, 0, sizeof(struct nvm_rq));
  506. rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
  507. rq_len = rq_ppas * geo->sec_size;
  508. bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
  509. l_mg->emeta_alloc_type, GFP_KERNEL);
  510. if (IS_ERR(bio)) {
  511. ret = PTR_ERR(bio);
  512. goto free_rqd_dma;
  513. }
  514. bio->bi_iter.bi_sector = 0; /* internal bio */
  515. bio_set_op_attrs(bio, bio_op, 0);
  516. rqd.bio = bio;
  517. rqd.meta_list = meta_list;
  518. rqd.ppa_list = ppa_list;
  519. rqd.dma_meta_list = dma_meta_list;
  520. rqd.dma_ppa_list = dma_ppa_list;
  521. rqd.opcode = cmd_op;
  522. rqd.nr_ppas = rq_ppas;
  523. if (dir == PBLK_WRITE) {
  524. struct pblk_sec_meta *meta_list = rqd.meta_list;
  525. rqd.flags = pblk_set_progr_mode(pblk, PBLK_WRITE);
  526. for (i = 0; i < rqd.nr_ppas; ) {
  527. spin_lock(&line->lock);
  528. paddr = __pblk_alloc_page(pblk, line, min);
  529. spin_unlock(&line->lock);
  530. for (j = 0; j < min; j++, i++, paddr++) {
  531. meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
  532. rqd.ppa_list[i] =
  533. addr_to_gen_ppa(pblk, paddr, id);
  534. }
  535. }
  536. } else {
  537. for (i = 0; i < rqd.nr_ppas; ) {
  538. struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
  539. int pos = pblk_dev_ppa_to_pos(geo, ppa);
  540. int read_type = PBLK_READ_RANDOM;
  541. if (pblk_io_aligned(pblk, rq_ppas))
  542. read_type = PBLK_READ_SEQUENTIAL;
  543. rqd.flags = pblk_set_read_mode(pblk, read_type);
  544. while (test_bit(pos, line->blk_bitmap)) {
  545. paddr += min;
  546. if (pblk_boundary_paddr_checks(pblk, paddr)) {
  547. pr_err("pblk: corrupt emeta line:%d\n",
  548. line->id);
  549. bio_put(bio);
  550. ret = -EINTR;
  551. goto free_rqd_dma;
  552. }
  553. ppa = addr_to_gen_ppa(pblk, paddr, id);
  554. pos = pblk_dev_ppa_to_pos(geo, ppa);
  555. }
  556. if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
  557. pr_err("pblk: corrupt emeta line:%d\n",
  558. line->id);
  559. bio_put(bio);
  560. ret = -EINTR;
  561. goto free_rqd_dma;
  562. }
  563. for (j = 0; j < min; j++, i++, paddr++)
  564. rqd.ppa_list[i] =
  565. addr_to_gen_ppa(pblk, paddr, line->id);
  566. }
  567. }
  568. ret = pblk_submit_io_sync(pblk, &rqd);
  569. if (ret) {
  570. pr_err("pblk: emeta I/O submission failed: %d\n", ret);
  571. bio_put(bio);
  572. goto free_rqd_dma;
  573. }
  574. atomic_dec(&pblk->inflight_io);
  575. if (rqd.error) {
  576. if (dir == PBLK_WRITE)
  577. pblk_log_write_err(pblk, &rqd);
  578. else
  579. pblk_log_read_err(pblk, &rqd);
  580. }
  581. emeta_buf += rq_len;
  582. left_ppas -= rq_ppas;
  583. if (left_ppas)
  584. goto next_rq;
  585. free_rqd_dma:
  586. nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
  587. return ret;
  588. }
  589. u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
  590. {
  591. struct nvm_tgt_dev *dev = pblk->dev;
  592. struct nvm_geo *geo = &dev->geo;
  593. struct pblk_line_meta *lm = &pblk->lm;
  594. int bit;
  595. /* This usually only happens on bad lines */
  596. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  597. if (bit >= lm->blk_per_line)
  598. return -1;
  599. return bit * geo->sec_per_pl;
  600. }
  601. static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
  602. u64 paddr, int dir)
  603. {
  604. struct nvm_tgt_dev *dev = pblk->dev;
  605. struct pblk_line_meta *lm = &pblk->lm;
  606. struct bio *bio;
  607. struct nvm_rq rqd;
  608. __le64 *lba_list = NULL;
  609. int i, ret;
  610. int cmd_op, bio_op;
  611. int flags;
  612. if (dir == PBLK_WRITE) {
  613. bio_op = REQ_OP_WRITE;
  614. cmd_op = NVM_OP_PWRITE;
  615. flags = pblk_set_progr_mode(pblk, PBLK_WRITE);
  616. lba_list = emeta_to_lbas(pblk, line->emeta->buf);
  617. } else if (dir == PBLK_READ) {
  618. bio_op = REQ_OP_READ;
  619. cmd_op = NVM_OP_PREAD;
  620. flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
  621. } else
  622. return -EINVAL;
  623. memset(&rqd, 0, sizeof(struct nvm_rq));
  624. rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  625. &rqd.dma_meta_list);
  626. if (!rqd.meta_list)
  627. return -ENOMEM;
  628. rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
  629. rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
  630. bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
  631. if (IS_ERR(bio)) {
  632. ret = PTR_ERR(bio);
  633. goto free_ppa_list;
  634. }
  635. bio->bi_iter.bi_sector = 0; /* internal bio */
  636. bio_set_op_attrs(bio, bio_op, 0);
  637. rqd.bio = bio;
  638. rqd.opcode = cmd_op;
  639. rqd.flags = flags;
  640. rqd.nr_ppas = lm->smeta_sec;
  641. for (i = 0; i < lm->smeta_sec; i++, paddr++) {
  642. struct pblk_sec_meta *meta_list = rqd.meta_list;
  643. rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
  644. if (dir == PBLK_WRITE) {
  645. __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
  646. meta_list[i].lba = lba_list[paddr] = addr_empty;
  647. }
  648. }
  649. /*
  650. * This I/O is sent by the write thread when a line is replace. Since
  651. * the write thread is the only one sending write and erase commands,
  652. * there is no need to take the LUN semaphore.
  653. */
  654. ret = pblk_submit_io_sync(pblk, &rqd);
  655. if (ret) {
  656. pr_err("pblk: smeta I/O submission failed: %d\n", ret);
  657. bio_put(bio);
  658. goto free_ppa_list;
  659. }
  660. atomic_dec(&pblk->inflight_io);
  661. if (rqd.error) {
  662. if (dir == PBLK_WRITE)
  663. pblk_log_write_err(pblk, &rqd);
  664. else
  665. pblk_log_read_err(pblk, &rqd);
  666. }
  667. free_ppa_list:
  668. nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
  669. return ret;
  670. }
  671. int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
  672. {
  673. u64 bpaddr = pblk_line_smeta_start(pblk, line);
  674. return pblk_line_submit_smeta_io(pblk, line, bpaddr, PBLK_READ);
  675. }
  676. int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
  677. void *emeta_buf)
  678. {
  679. return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
  680. line->emeta_ssec, PBLK_READ);
  681. }
  682. static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
  683. struct ppa_addr ppa)
  684. {
  685. rqd->opcode = NVM_OP_ERASE;
  686. rqd->ppa_addr = ppa;
  687. rqd->nr_ppas = 1;
  688. rqd->flags = pblk_set_progr_mode(pblk, PBLK_ERASE);
  689. rqd->bio = NULL;
  690. }
  691. static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
  692. {
  693. struct nvm_rq rqd;
  694. int ret = 0;
  695. memset(&rqd, 0, sizeof(struct nvm_rq));
  696. pblk_setup_e_rq(pblk, &rqd, ppa);
  697. /* The write thread schedules erases so that it minimizes disturbances
  698. * with writes. Thus, there is no need to take the LUN semaphore.
  699. */
  700. ret = pblk_submit_io_sync(pblk, &rqd);
  701. if (ret) {
  702. struct nvm_tgt_dev *dev = pblk->dev;
  703. struct nvm_geo *geo = &dev->geo;
  704. pr_err("pblk: could not sync erase line:%d,blk:%d\n",
  705. pblk_dev_ppa_to_line(ppa),
  706. pblk_dev_ppa_to_pos(geo, ppa));
  707. rqd.error = ret;
  708. goto out;
  709. }
  710. out:
  711. rqd.private = pblk;
  712. __pblk_end_io_erase(pblk, &rqd);
  713. return ret;
  714. }
  715. int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
  716. {
  717. struct pblk_line_meta *lm = &pblk->lm;
  718. struct ppa_addr ppa;
  719. int ret, bit = -1;
  720. /* Erase only good blocks, one at a time */
  721. do {
  722. spin_lock(&line->lock);
  723. bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
  724. bit + 1);
  725. if (bit >= lm->blk_per_line) {
  726. spin_unlock(&line->lock);
  727. break;
  728. }
  729. ppa = pblk->luns[bit].bppa; /* set ch and lun */
  730. ppa.g.blk = line->id;
  731. atomic_dec(&line->left_eblks);
  732. WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
  733. spin_unlock(&line->lock);
  734. ret = pblk_blk_erase_sync(pblk, ppa);
  735. if (ret) {
  736. pr_err("pblk: failed to erase line %d\n", line->id);
  737. return ret;
  738. }
  739. } while (1);
  740. return 0;
  741. }
  742. static void pblk_line_setup_metadata(struct pblk_line *line,
  743. struct pblk_line_mgmt *l_mg,
  744. struct pblk_line_meta *lm)
  745. {
  746. int meta_line;
  747. lockdep_assert_held(&l_mg->free_lock);
  748. retry_meta:
  749. meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
  750. if (meta_line == PBLK_DATA_LINES) {
  751. spin_unlock(&l_mg->free_lock);
  752. io_schedule();
  753. spin_lock(&l_mg->free_lock);
  754. goto retry_meta;
  755. }
  756. set_bit(meta_line, &l_mg->meta_bitmap);
  757. line->meta_line = meta_line;
  758. line->smeta = l_mg->sline_meta[meta_line];
  759. line->emeta = l_mg->eline_meta[meta_line];
  760. memset(line->smeta, 0, lm->smeta_len);
  761. memset(line->emeta->buf, 0, lm->emeta_len[0]);
  762. line->emeta->mem = 0;
  763. atomic_set(&line->emeta->sync, 0);
  764. }
  765. /* For now lines are always assumed full lines. Thus, smeta former and current
  766. * lun bitmaps are omitted.
  767. */
  768. static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
  769. struct pblk_line *cur)
  770. {
  771. struct nvm_tgt_dev *dev = pblk->dev;
  772. struct nvm_geo *geo = &dev->geo;
  773. struct pblk_line_meta *lm = &pblk->lm;
  774. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  775. struct pblk_emeta *emeta = line->emeta;
  776. struct line_emeta *emeta_buf = emeta->buf;
  777. struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
  778. int nr_blk_line;
  779. /* After erasing the line, new bad blocks might appear and we risk
  780. * having an invalid line
  781. */
  782. nr_blk_line = lm->blk_per_line -
  783. bitmap_weight(line->blk_bitmap, lm->blk_per_line);
  784. if (nr_blk_line < lm->min_blk_line) {
  785. spin_lock(&l_mg->free_lock);
  786. spin_lock(&line->lock);
  787. line->state = PBLK_LINESTATE_BAD;
  788. spin_unlock(&line->lock);
  789. list_add_tail(&line->list, &l_mg->bad_list);
  790. spin_unlock(&l_mg->free_lock);
  791. pr_debug("pblk: line %d is bad\n", line->id);
  792. return 0;
  793. }
  794. /* Run-time metadata */
  795. line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
  796. /* Mark LUNs allocated in this line (all for now) */
  797. bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
  798. smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
  799. memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
  800. smeta_buf->header.id = cpu_to_le32(line->id);
  801. smeta_buf->header.type = cpu_to_le16(line->type);
  802. smeta_buf->header.version = SMETA_VERSION;
  803. /* Start metadata */
  804. smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
  805. smeta_buf->window_wr_lun = cpu_to_le32(geo->nr_luns);
  806. /* Fill metadata among lines */
  807. if (cur) {
  808. memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
  809. smeta_buf->prev_id = cpu_to_le32(cur->id);
  810. cur->emeta->buf->next_id = cpu_to_le32(line->id);
  811. } else {
  812. smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
  813. }
  814. /* All smeta must be set at this point */
  815. smeta_buf->header.crc = cpu_to_le32(
  816. pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
  817. smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
  818. /* End metadata */
  819. memcpy(&emeta_buf->header, &smeta_buf->header,
  820. sizeof(struct line_header));
  821. emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
  822. emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
  823. emeta_buf->nr_valid_lbas = cpu_to_le64(0);
  824. emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
  825. emeta_buf->crc = cpu_to_le32(0);
  826. emeta_buf->prev_id = smeta_buf->prev_id;
  827. return 1;
  828. }
  829. /* For now lines are always assumed full lines. Thus, smeta former and current
  830. * lun bitmaps are omitted.
  831. */
  832. static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
  833. int init)
  834. {
  835. struct nvm_tgt_dev *dev = pblk->dev;
  836. struct nvm_geo *geo = &dev->geo;
  837. struct pblk_line_meta *lm = &pblk->lm;
  838. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  839. int nr_bb = 0;
  840. u64 off;
  841. int bit = -1;
  842. line->sec_in_line = lm->sec_per_line;
  843. /* Capture bad block information on line mapping bitmaps */
  844. while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
  845. bit + 1)) < lm->blk_per_line) {
  846. off = bit * geo->sec_per_pl;
  847. bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
  848. lm->sec_per_line);
  849. bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
  850. lm->sec_per_line);
  851. line->sec_in_line -= geo->sec_per_blk;
  852. if (bit >= lm->emeta_bb)
  853. nr_bb++;
  854. }
  855. /* Mark smeta metadata sectors as bad sectors */
  856. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  857. off = bit * geo->sec_per_pl;
  858. bitmap_set(line->map_bitmap, off, lm->smeta_sec);
  859. line->sec_in_line -= lm->smeta_sec;
  860. line->smeta_ssec = off;
  861. line->cur_sec = off + lm->smeta_sec;
  862. if (init && pblk_line_submit_smeta_io(pblk, line, off, PBLK_WRITE)) {
  863. pr_debug("pblk: line smeta I/O failed. Retry\n");
  864. return 1;
  865. }
  866. bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
  867. /* Mark emeta metadata sectors as bad sectors. We need to consider bad
  868. * blocks to make sure that there are enough sectors to store emeta
  869. */
  870. off = lm->sec_per_line - lm->emeta_sec[0];
  871. bitmap_set(line->invalid_bitmap, off, lm->emeta_sec[0]);
  872. while (nr_bb) {
  873. off -= geo->sec_per_pl;
  874. if (!test_bit(off, line->invalid_bitmap)) {
  875. bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl);
  876. nr_bb--;
  877. }
  878. }
  879. line->sec_in_line -= lm->emeta_sec[0];
  880. line->emeta_ssec = off;
  881. line->nr_valid_lbas = 0;
  882. line->left_msecs = line->sec_in_line;
  883. *line->vsc = cpu_to_le32(line->sec_in_line);
  884. if (lm->sec_per_line - line->sec_in_line !=
  885. bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
  886. spin_lock(&line->lock);
  887. line->state = PBLK_LINESTATE_BAD;
  888. spin_unlock(&line->lock);
  889. list_add_tail(&line->list, &l_mg->bad_list);
  890. pr_err("pblk: unexpected line %d is bad\n", line->id);
  891. return 0;
  892. }
  893. return 1;
  894. }
  895. static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
  896. {
  897. struct pblk_line_meta *lm = &pblk->lm;
  898. int blk_in_line = atomic_read(&line->blk_in_line);
  899. line->map_bitmap = kzalloc(lm->sec_bitmap_len, GFP_ATOMIC);
  900. if (!line->map_bitmap)
  901. return -ENOMEM;
  902. /* will be initialized using bb info from map_bitmap */
  903. line->invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_ATOMIC);
  904. if (!line->invalid_bitmap) {
  905. kfree(line->map_bitmap);
  906. return -ENOMEM;
  907. }
  908. spin_lock(&line->lock);
  909. if (line->state != PBLK_LINESTATE_FREE) {
  910. kfree(line->map_bitmap);
  911. kfree(line->invalid_bitmap);
  912. spin_unlock(&line->lock);
  913. WARN(1, "pblk: corrupted line %d, state %d\n",
  914. line->id, line->state);
  915. return -EAGAIN;
  916. }
  917. line->state = PBLK_LINESTATE_OPEN;
  918. atomic_set(&line->left_eblks, blk_in_line);
  919. atomic_set(&line->left_seblks, blk_in_line);
  920. line->meta_distance = lm->meta_distance;
  921. spin_unlock(&line->lock);
  922. /* Bad blocks do not need to be erased */
  923. bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
  924. kref_init(&line->ref);
  925. return 0;
  926. }
  927. int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
  928. {
  929. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  930. int ret;
  931. spin_lock(&l_mg->free_lock);
  932. l_mg->data_line = line;
  933. list_del(&line->list);
  934. ret = pblk_line_prepare(pblk, line);
  935. if (ret) {
  936. list_add(&line->list, &l_mg->free_list);
  937. spin_unlock(&l_mg->free_lock);
  938. return ret;
  939. }
  940. spin_unlock(&l_mg->free_lock);
  941. pblk_rl_free_lines_dec(&pblk->rl, line);
  942. if (!pblk_line_init_bb(pblk, line, 0)) {
  943. list_add(&line->list, &l_mg->free_list);
  944. return -EINTR;
  945. }
  946. return 0;
  947. }
  948. void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
  949. {
  950. kfree(line->map_bitmap);
  951. line->map_bitmap = NULL;
  952. line->smeta = NULL;
  953. line->emeta = NULL;
  954. }
  955. struct pblk_line *pblk_line_get(struct pblk *pblk)
  956. {
  957. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  958. struct pblk_line_meta *lm = &pblk->lm;
  959. struct pblk_line *line;
  960. int ret, bit;
  961. lockdep_assert_held(&l_mg->free_lock);
  962. retry:
  963. if (list_empty(&l_mg->free_list)) {
  964. pr_err("pblk: no free lines\n");
  965. return NULL;
  966. }
  967. line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
  968. list_del(&line->list);
  969. l_mg->nr_free_lines--;
  970. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  971. if (unlikely(bit >= lm->blk_per_line)) {
  972. spin_lock(&line->lock);
  973. line->state = PBLK_LINESTATE_BAD;
  974. spin_unlock(&line->lock);
  975. list_add_tail(&line->list, &l_mg->bad_list);
  976. pr_debug("pblk: line %d is bad\n", line->id);
  977. goto retry;
  978. }
  979. ret = pblk_line_prepare(pblk, line);
  980. if (ret) {
  981. if (ret == -EAGAIN) {
  982. list_add(&line->list, &l_mg->corrupt_list);
  983. goto retry;
  984. } else {
  985. pr_err("pblk: failed to prepare line %d\n", line->id);
  986. list_add(&line->list, &l_mg->free_list);
  987. l_mg->nr_free_lines++;
  988. return NULL;
  989. }
  990. }
  991. return line;
  992. }
  993. static struct pblk_line *pblk_line_retry(struct pblk *pblk,
  994. struct pblk_line *line)
  995. {
  996. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  997. struct pblk_line *retry_line;
  998. retry:
  999. spin_lock(&l_mg->free_lock);
  1000. retry_line = pblk_line_get(pblk);
  1001. if (!retry_line) {
  1002. l_mg->data_line = NULL;
  1003. spin_unlock(&l_mg->free_lock);
  1004. return NULL;
  1005. }
  1006. retry_line->smeta = line->smeta;
  1007. retry_line->emeta = line->emeta;
  1008. retry_line->meta_line = line->meta_line;
  1009. pblk_line_free(pblk, line);
  1010. l_mg->data_line = retry_line;
  1011. spin_unlock(&l_mg->free_lock);
  1012. pblk_rl_free_lines_dec(&pblk->rl, retry_line);
  1013. if (pblk_line_erase(pblk, retry_line))
  1014. goto retry;
  1015. return retry_line;
  1016. }
  1017. static void pblk_set_space_limit(struct pblk *pblk)
  1018. {
  1019. struct pblk_rl *rl = &pblk->rl;
  1020. atomic_set(&rl->rb_space, 0);
  1021. }
  1022. struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
  1023. {
  1024. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1025. struct pblk_line *line;
  1026. int is_next = 0;
  1027. spin_lock(&l_mg->free_lock);
  1028. line = pblk_line_get(pblk);
  1029. if (!line) {
  1030. spin_unlock(&l_mg->free_lock);
  1031. return NULL;
  1032. }
  1033. line->seq_nr = l_mg->d_seq_nr++;
  1034. line->type = PBLK_LINETYPE_DATA;
  1035. l_mg->data_line = line;
  1036. pblk_line_setup_metadata(line, l_mg, &pblk->lm);
  1037. /* Allocate next line for preparation */
  1038. l_mg->data_next = pblk_line_get(pblk);
  1039. if (!l_mg->data_next) {
  1040. /* If we cannot get a new line, we need to stop the pipeline.
  1041. * Only allow as many writes in as we can store safely and then
  1042. * fail gracefully
  1043. */
  1044. pblk_set_space_limit(pblk);
  1045. l_mg->data_next = NULL;
  1046. } else {
  1047. l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
  1048. l_mg->data_next->type = PBLK_LINETYPE_DATA;
  1049. is_next = 1;
  1050. }
  1051. spin_unlock(&l_mg->free_lock);
  1052. if (pblk_line_erase(pblk, line)) {
  1053. line = pblk_line_retry(pblk, line);
  1054. if (!line)
  1055. return NULL;
  1056. }
  1057. pblk_rl_free_lines_dec(&pblk->rl, line);
  1058. if (is_next)
  1059. pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
  1060. retry_setup:
  1061. if (!pblk_line_init_metadata(pblk, line, NULL)) {
  1062. line = pblk_line_retry(pblk, line);
  1063. if (!line)
  1064. return NULL;
  1065. goto retry_setup;
  1066. }
  1067. if (!pblk_line_init_bb(pblk, line, 1)) {
  1068. line = pblk_line_retry(pblk, line);
  1069. if (!line)
  1070. return NULL;
  1071. goto retry_setup;
  1072. }
  1073. return line;
  1074. }
  1075. static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
  1076. {
  1077. lockdep_assert_held(&pblk->l_mg.free_lock);
  1078. pblk_set_space_limit(pblk);
  1079. pblk->state = PBLK_STATE_STOPPING;
  1080. }
  1081. static void pblk_line_close_meta_sync(struct pblk *pblk)
  1082. {
  1083. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1084. struct pblk_line_meta *lm = &pblk->lm;
  1085. struct pblk_line *line, *tline;
  1086. LIST_HEAD(list);
  1087. spin_lock(&l_mg->close_lock);
  1088. if (list_empty(&l_mg->emeta_list)) {
  1089. spin_unlock(&l_mg->close_lock);
  1090. return;
  1091. }
  1092. list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
  1093. spin_unlock(&l_mg->close_lock);
  1094. list_for_each_entry_safe(line, tline, &list, list) {
  1095. struct pblk_emeta *emeta = line->emeta;
  1096. while (emeta->mem < lm->emeta_len[0]) {
  1097. int ret;
  1098. ret = pblk_submit_meta_io(pblk, line);
  1099. if (ret) {
  1100. pr_err("pblk: sync meta line %d failed (%d)\n",
  1101. line->id, ret);
  1102. return;
  1103. }
  1104. }
  1105. }
  1106. pblk_wait_for_meta(pblk);
  1107. flush_workqueue(pblk->close_wq);
  1108. }
  1109. void pblk_pipeline_stop(struct pblk *pblk)
  1110. {
  1111. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1112. int ret;
  1113. spin_lock(&l_mg->free_lock);
  1114. if (pblk->state == PBLK_STATE_RECOVERING ||
  1115. pblk->state == PBLK_STATE_STOPPED) {
  1116. spin_unlock(&l_mg->free_lock);
  1117. return;
  1118. }
  1119. pblk->state = PBLK_STATE_RECOVERING;
  1120. spin_unlock(&l_mg->free_lock);
  1121. pblk_flush_writer(pblk);
  1122. pblk_wait_for_meta(pblk);
  1123. ret = pblk_recov_pad(pblk);
  1124. if (ret) {
  1125. pr_err("pblk: could not close data on teardown(%d)\n", ret);
  1126. return;
  1127. }
  1128. flush_workqueue(pblk->bb_wq);
  1129. pblk_line_close_meta_sync(pblk);
  1130. spin_lock(&l_mg->free_lock);
  1131. pblk->state = PBLK_STATE_STOPPED;
  1132. l_mg->data_line = NULL;
  1133. l_mg->data_next = NULL;
  1134. spin_unlock(&l_mg->free_lock);
  1135. }
  1136. struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
  1137. {
  1138. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1139. struct pblk_line *cur, *new = NULL;
  1140. unsigned int left_seblks;
  1141. int is_next = 0;
  1142. cur = l_mg->data_line;
  1143. new = l_mg->data_next;
  1144. if (!new)
  1145. goto out;
  1146. l_mg->data_line = new;
  1147. spin_lock(&l_mg->free_lock);
  1148. if (pblk->state != PBLK_STATE_RUNNING) {
  1149. l_mg->data_line = NULL;
  1150. l_mg->data_next = NULL;
  1151. spin_unlock(&l_mg->free_lock);
  1152. goto out;
  1153. }
  1154. pblk_line_setup_metadata(new, l_mg, &pblk->lm);
  1155. spin_unlock(&l_mg->free_lock);
  1156. retry_erase:
  1157. left_seblks = atomic_read(&new->left_seblks);
  1158. if (left_seblks) {
  1159. /* If line is not fully erased, erase it */
  1160. if (atomic_read(&new->left_eblks)) {
  1161. if (pblk_line_erase(pblk, new))
  1162. goto out;
  1163. } else {
  1164. io_schedule();
  1165. }
  1166. goto retry_erase;
  1167. }
  1168. retry_setup:
  1169. if (!pblk_line_init_metadata(pblk, new, cur)) {
  1170. new = pblk_line_retry(pblk, new);
  1171. if (!new)
  1172. goto out;
  1173. goto retry_setup;
  1174. }
  1175. if (!pblk_line_init_bb(pblk, new, 1)) {
  1176. new = pblk_line_retry(pblk, new);
  1177. if (!new)
  1178. goto out;
  1179. goto retry_setup;
  1180. }
  1181. /* Allocate next line for preparation */
  1182. spin_lock(&l_mg->free_lock);
  1183. l_mg->data_next = pblk_line_get(pblk);
  1184. if (!l_mg->data_next) {
  1185. /* If we cannot get a new line, we need to stop the pipeline.
  1186. * Only allow as many writes in as we can store safely and then
  1187. * fail gracefully
  1188. */
  1189. pblk_stop_writes(pblk, new);
  1190. l_mg->data_next = NULL;
  1191. } else {
  1192. l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
  1193. l_mg->data_next->type = PBLK_LINETYPE_DATA;
  1194. is_next = 1;
  1195. }
  1196. spin_unlock(&l_mg->free_lock);
  1197. if (is_next)
  1198. pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
  1199. out:
  1200. return new;
  1201. }
  1202. void pblk_line_free(struct pblk *pblk, struct pblk_line *line)
  1203. {
  1204. kfree(line->map_bitmap);
  1205. kfree(line->invalid_bitmap);
  1206. *line->vsc = cpu_to_le32(EMPTY_ENTRY);
  1207. line->map_bitmap = NULL;
  1208. line->invalid_bitmap = NULL;
  1209. line->smeta = NULL;
  1210. line->emeta = NULL;
  1211. }
  1212. static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
  1213. {
  1214. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1215. struct pblk_gc *gc = &pblk->gc;
  1216. spin_lock(&line->lock);
  1217. WARN_ON(line->state != PBLK_LINESTATE_GC);
  1218. line->state = PBLK_LINESTATE_FREE;
  1219. line->gc_group = PBLK_LINEGC_NONE;
  1220. pblk_line_free(pblk, line);
  1221. spin_unlock(&line->lock);
  1222. atomic_dec(&gc->pipeline_gc);
  1223. spin_lock(&l_mg->free_lock);
  1224. list_add_tail(&line->list, &l_mg->free_list);
  1225. l_mg->nr_free_lines++;
  1226. spin_unlock(&l_mg->free_lock);
  1227. pblk_rl_free_lines_inc(&pblk->rl, line);
  1228. }
  1229. static void pblk_line_put_ws(struct work_struct *work)
  1230. {
  1231. struct pblk_line_ws *line_put_ws = container_of(work,
  1232. struct pblk_line_ws, ws);
  1233. struct pblk *pblk = line_put_ws->pblk;
  1234. struct pblk_line *line = line_put_ws->line;
  1235. __pblk_line_put(pblk, line);
  1236. mempool_free(line_put_ws, pblk->gen_ws_pool);
  1237. }
  1238. void pblk_line_put(struct kref *ref)
  1239. {
  1240. struct pblk_line *line = container_of(ref, struct pblk_line, ref);
  1241. struct pblk *pblk = line->pblk;
  1242. __pblk_line_put(pblk, line);
  1243. }
  1244. void pblk_line_put_wq(struct kref *ref)
  1245. {
  1246. struct pblk_line *line = container_of(ref, struct pblk_line, ref);
  1247. struct pblk *pblk = line->pblk;
  1248. struct pblk_line_ws *line_put_ws;
  1249. line_put_ws = mempool_alloc(pblk->gen_ws_pool, GFP_ATOMIC);
  1250. if (!line_put_ws)
  1251. return;
  1252. line_put_ws->pblk = pblk;
  1253. line_put_ws->line = line;
  1254. line_put_ws->priv = NULL;
  1255. INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
  1256. queue_work(pblk->r_end_wq, &line_put_ws->ws);
  1257. }
  1258. int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
  1259. {
  1260. struct nvm_rq *rqd;
  1261. int err;
  1262. rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
  1263. pblk_setup_e_rq(pblk, rqd, ppa);
  1264. rqd->end_io = pblk_end_io_erase;
  1265. rqd->private = pblk;
  1266. /* The write thread schedules erases so that it minimizes disturbances
  1267. * with writes. Thus, there is no need to take the LUN semaphore.
  1268. */
  1269. err = pblk_submit_io(pblk, rqd);
  1270. if (err) {
  1271. struct nvm_tgt_dev *dev = pblk->dev;
  1272. struct nvm_geo *geo = &dev->geo;
  1273. pr_err("pblk: could not async erase line:%d,blk:%d\n",
  1274. pblk_dev_ppa_to_line(ppa),
  1275. pblk_dev_ppa_to_pos(geo, ppa));
  1276. }
  1277. return err;
  1278. }
  1279. struct pblk_line *pblk_line_get_data(struct pblk *pblk)
  1280. {
  1281. return pblk->l_mg.data_line;
  1282. }
  1283. /* For now, always erase next line */
  1284. struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
  1285. {
  1286. return pblk->l_mg.data_next;
  1287. }
  1288. int pblk_line_is_full(struct pblk_line *line)
  1289. {
  1290. return (line->left_msecs == 0);
  1291. }
  1292. static void pblk_line_should_sync_meta(struct pblk *pblk)
  1293. {
  1294. if (pblk_rl_is_limit(&pblk->rl))
  1295. pblk_line_close_meta_sync(pblk);
  1296. }
  1297. void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
  1298. {
  1299. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1300. struct list_head *move_list;
  1301. #ifdef CONFIG_NVM_DEBUG
  1302. struct pblk_line_meta *lm = &pblk->lm;
  1303. WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
  1304. "pblk: corrupt closed line %d\n", line->id);
  1305. #endif
  1306. spin_lock(&l_mg->free_lock);
  1307. WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
  1308. spin_unlock(&l_mg->free_lock);
  1309. spin_lock(&l_mg->gc_lock);
  1310. spin_lock(&line->lock);
  1311. WARN_ON(line->state != PBLK_LINESTATE_OPEN);
  1312. line->state = PBLK_LINESTATE_CLOSED;
  1313. move_list = pblk_line_gc_list(pblk, line);
  1314. list_add_tail(&line->list, move_list);
  1315. kfree(line->map_bitmap);
  1316. line->map_bitmap = NULL;
  1317. line->smeta = NULL;
  1318. line->emeta = NULL;
  1319. spin_unlock(&line->lock);
  1320. spin_unlock(&l_mg->gc_lock);
  1321. }
  1322. void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
  1323. {
  1324. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1325. struct pblk_line_meta *lm = &pblk->lm;
  1326. struct pblk_emeta *emeta = line->emeta;
  1327. struct line_emeta *emeta_buf = emeta->buf;
  1328. /* No need for exact vsc value; avoid a big line lock and take aprox. */
  1329. memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
  1330. memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
  1331. emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
  1332. emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
  1333. spin_lock(&l_mg->close_lock);
  1334. spin_lock(&line->lock);
  1335. list_add_tail(&line->list, &l_mg->emeta_list);
  1336. spin_unlock(&line->lock);
  1337. spin_unlock(&l_mg->close_lock);
  1338. pblk_line_should_sync_meta(pblk);
  1339. }
  1340. void pblk_line_close_ws(struct work_struct *work)
  1341. {
  1342. struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
  1343. ws);
  1344. struct pblk *pblk = line_ws->pblk;
  1345. struct pblk_line *line = line_ws->line;
  1346. pblk_line_close(pblk, line);
  1347. mempool_free(line_ws, pblk->gen_ws_pool);
  1348. }
  1349. void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
  1350. void (*work)(struct work_struct *), gfp_t gfp_mask,
  1351. struct workqueue_struct *wq)
  1352. {
  1353. struct pblk_line_ws *line_ws;
  1354. line_ws = mempool_alloc(pblk->gen_ws_pool, gfp_mask);
  1355. line_ws->pblk = pblk;
  1356. line_ws->line = line;
  1357. line_ws->priv = priv;
  1358. INIT_WORK(&line_ws->ws, work);
  1359. queue_work(wq, &line_ws->ws);
  1360. }
  1361. static void __pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list,
  1362. int nr_ppas, int pos)
  1363. {
  1364. struct pblk_lun *rlun = &pblk->luns[pos];
  1365. int ret;
  1366. /*
  1367. * Only send one inflight I/O per LUN. Since we map at a page
  1368. * granurality, all ppas in the I/O will map to the same LUN
  1369. */
  1370. #ifdef CONFIG_NVM_DEBUG
  1371. int i;
  1372. for (i = 1; i < nr_ppas; i++)
  1373. WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
  1374. ppa_list[0].g.ch != ppa_list[i].g.ch);
  1375. #endif
  1376. ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
  1377. if (ret == -ETIME || ret == -EINTR)
  1378. pr_err("pblk: taking lun semaphore timed out: err %d\n", -ret);
  1379. }
  1380. void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
  1381. {
  1382. struct nvm_tgt_dev *dev = pblk->dev;
  1383. struct nvm_geo *geo = &dev->geo;
  1384. int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
  1385. __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
  1386. }
  1387. void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
  1388. unsigned long *lun_bitmap)
  1389. {
  1390. struct nvm_tgt_dev *dev = pblk->dev;
  1391. struct nvm_geo *geo = &dev->geo;
  1392. int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
  1393. /* If the LUN has been locked for this same request, do no attempt to
  1394. * lock it again
  1395. */
  1396. if (test_and_set_bit(pos, lun_bitmap))
  1397. return;
  1398. __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
  1399. }
  1400. void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
  1401. {
  1402. struct nvm_tgt_dev *dev = pblk->dev;
  1403. struct nvm_geo *geo = &dev->geo;
  1404. struct pblk_lun *rlun;
  1405. int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
  1406. #ifdef CONFIG_NVM_DEBUG
  1407. int i;
  1408. for (i = 1; i < nr_ppas; i++)
  1409. WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
  1410. ppa_list[0].g.ch != ppa_list[i].g.ch);
  1411. #endif
  1412. rlun = &pblk->luns[pos];
  1413. up(&rlun->wr_sem);
  1414. }
  1415. void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
  1416. unsigned long *lun_bitmap)
  1417. {
  1418. struct nvm_tgt_dev *dev = pblk->dev;
  1419. struct nvm_geo *geo = &dev->geo;
  1420. struct pblk_lun *rlun;
  1421. int nr_luns = geo->nr_luns;
  1422. int bit = -1;
  1423. while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) {
  1424. rlun = &pblk->luns[bit];
  1425. up(&rlun->wr_sem);
  1426. }
  1427. }
  1428. void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
  1429. {
  1430. struct ppa_addr ppa_l2p;
  1431. /* logic error: lba out-of-bounds. Ignore update */
  1432. if (!(lba < pblk->rl.nr_secs)) {
  1433. WARN(1, "pblk: corrupted L2P map request\n");
  1434. return;
  1435. }
  1436. spin_lock(&pblk->trans_lock);
  1437. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1438. if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
  1439. pblk_map_invalidate(pblk, ppa_l2p);
  1440. pblk_trans_map_set(pblk, lba, ppa);
  1441. spin_unlock(&pblk->trans_lock);
  1442. }
  1443. void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
  1444. {
  1445. #ifdef CONFIG_NVM_DEBUG
  1446. /* Callers must ensure that the ppa points to a cache address */
  1447. BUG_ON(!pblk_addr_in_cache(ppa));
  1448. BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
  1449. #endif
  1450. pblk_update_map(pblk, lba, ppa);
  1451. }
  1452. int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
  1453. struct pblk_line *gc_line, u64 paddr_gc)
  1454. {
  1455. struct ppa_addr ppa_l2p, ppa_gc;
  1456. int ret = 1;
  1457. #ifdef CONFIG_NVM_DEBUG
  1458. /* Callers must ensure that the ppa points to a cache address */
  1459. BUG_ON(!pblk_addr_in_cache(ppa_new));
  1460. BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
  1461. #endif
  1462. /* logic error: lba out-of-bounds. Ignore update */
  1463. if (!(lba < pblk->rl.nr_secs)) {
  1464. WARN(1, "pblk: corrupted L2P map request\n");
  1465. return 0;
  1466. }
  1467. spin_lock(&pblk->trans_lock);
  1468. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1469. ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
  1470. if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
  1471. spin_lock(&gc_line->lock);
  1472. WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
  1473. "pblk: corrupted GC update");
  1474. spin_unlock(&gc_line->lock);
  1475. ret = 0;
  1476. goto out;
  1477. }
  1478. pblk_trans_map_set(pblk, lba, ppa_new);
  1479. out:
  1480. spin_unlock(&pblk->trans_lock);
  1481. return ret;
  1482. }
  1483. void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
  1484. struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
  1485. {
  1486. struct ppa_addr ppa_l2p;
  1487. #ifdef CONFIG_NVM_DEBUG
  1488. /* Callers must ensure that the ppa points to a device address */
  1489. BUG_ON(pblk_addr_in_cache(ppa_mapped));
  1490. #endif
  1491. /* Invalidate and discard padded entries */
  1492. if (lba == ADDR_EMPTY) {
  1493. #ifdef CONFIG_NVM_DEBUG
  1494. atomic_long_inc(&pblk->padded_wb);
  1495. #endif
  1496. if (!pblk_ppa_empty(ppa_mapped))
  1497. pblk_map_invalidate(pblk, ppa_mapped);
  1498. return;
  1499. }
  1500. /* logic error: lba out-of-bounds. Ignore update */
  1501. if (!(lba < pblk->rl.nr_secs)) {
  1502. WARN(1, "pblk: corrupted L2P map request\n");
  1503. return;
  1504. }
  1505. spin_lock(&pblk->trans_lock);
  1506. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1507. /* Do not update L2P if the cacheline has been updated. In this case,
  1508. * the mapped ppa must be invalidated
  1509. */
  1510. if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
  1511. if (!pblk_ppa_empty(ppa_mapped))
  1512. pblk_map_invalidate(pblk, ppa_mapped);
  1513. goto out;
  1514. }
  1515. #ifdef CONFIG_NVM_DEBUG
  1516. WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
  1517. #endif
  1518. pblk_trans_map_set(pblk, lba, ppa_mapped);
  1519. out:
  1520. spin_unlock(&pblk->trans_lock);
  1521. }
  1522. void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
  1523. sector_t blba, int nr_secs)
  1524. {
  1525. int i;
  1526. spin_lock(&pblk->trans_lock);
  1527. for (i = 0; i < nr_secs; i++) {
  1528. struct ppa_addr ppa;
  1529. ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
  1530. /* If the L2P entry maps to a line, the reference is valid */
  1531. if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
  1532. int line_id = pblk_dev_ppa_to_line(ppa);
  1533. struct pblk_line *line = &pblk->lines[line_id];
  1534. kref_get(&line->ref);
  1535. }
  1536. }
  1537. spin_unlock(&pblk->trans_lock);
  1538. }
  1539. void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
  1540. u64 *lba_list, int nr_secs)
  1541. {
  1542. u64 lba;
  1543. int i;
  1544. spin_lock(&pblk->trans_lock);
  1545. for (i = 0; i < nr_secs; i++) {
  1546. lba = lba_list[i];
  1547. if (lba != ADDR_EMPTY) {
  1548. /* logic error: lba out-of-bounds. Ignore update */
  1549. if (!(lba < pblk->rl.nr_secs)) {
  1550. WARN(1, "pblk: corrupted L2P map request\n");
  1551. continue;
  1552. }
  1553. ppas[i] = pblk_trans_map_get(pblk, lba);
  1554. }
  1555. }
  1556. spin_unlock(&pblk->trans_lock);
  1557. }