pblk-core.c 46 KB

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