pblk-core.c 51 KB

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