pblk-core.c 45 KB

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