pblk-write.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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-write.c - pblk's write path from write buffer to media
  16. */
  17. #include "pblk.h"
  18. static unsigned long pblk_end_w_bio(struct pblk *pblk, struct nvm_rq *rqd,
  19. struct pblk_c_ctx *c_ctx)
  20. {
  21. struct bio *original_bio;
  22. struct pblk_rb *rwb = &pblk->rwb;
  23. unsigned long ret;
  24. int i;
  25. for (i = 0; i < c_ctx->nr_valid; i++) {
  26. struct pblk_w_ctx *w_ctx;
  27. int pos = c_ctx->sentry + i;
  28. int flags;
  29. w_ctx = pblk_rb_w_ctx(rwb, pos);
  30. flags = READ_ONCE(w_ctx->flags);
  31. if (flags & PBLK_FLUSH_ENTRY) {
  32. flags &= ~PBLK_FLUSH_ENTRY;
  33. /* Release flags on context. Protect from writes */
  34. smp_store_release(&w_ctx->flags, flags);
  35. #ifdef CONFIG_NVM_PBLK_DEBUG
  36. atomic_dec(&rwb->inflight_flush_point);
  37. #endif
  38. }
  39. while ((original_bio = bio_list_pop(&w_ctx->bios)))
  40. bio_endio(original_bio);
  41. }
  42. if (c_ctx->nr_padded)
  43. pblk_bio_free_pages(pblk, rqd->bio, c_ctx->nr_valid,
  44. c_ctx->nr_padded);
  45. #ifdef CONFIG_NVM_PBLK_DEBUG
  46. atomic_long_add(rqd->nr_ppas, &pblk->sync_writes);
  47. #endif
  48. ret = pblk_rb_sync_advance(&pblk->rwb, c_ctx->nr_valid);
  49. bio_put(rqd->bio);
  50. pblk_free_rqd(pblk, rqd, PBLK_WRITE);
  51. return ret;
  52. }
  53. static unsigned long pblk_end_queued_w_bio(struct pblk *pblk,
  54. struct nvm_rq *rqd,
  55. struct pblk_c_ctx *c_ctx)
  56. {
  57. list_del(&c_ctx->list);
  58. return pblk_end_w_bio(pblk, rqd, c_ctx);
  59. }
  60. static void pblk_complete_write(struct pblk *pblk, struct nvm_rq *rqd,
  61. struct pblk_c_ctx *c_ctx)
  62. {
  63. struct pblk_c_ctx *c, *r;
  64. unsigned long flags;
  65. unsigned long pos;
  66. #ifdef CONFIG_NVM_PBLK_DEBUG
  67. atomic_long_sub(c_ctx->nr_valid, &pblk->inflight_writes);
  68. #endif
  69. pblk_up_rq(pblk, rqd->ppa_list, rqd->nr_ppas, c_ctx->lun_bitmap);
  70. pos = pblk_rb_sync_init(&pblk->rwb, &flags);
  71. if (pos == c_ctx->sentry) {
  72. pos = pblk_end_w_bio(pblk, rqd, c_ctx);
  73. retry:
  74. list_for_each_entry_safe(c, r, &pblk->compl_list, list) {
  75. rqd = nvm_rq_from_c_ctx(c);
  76. if (c->sentry == pos) {
  77. pos = pblk_end_queued_w_bio(pblk, rqd, c);
  78. goto retry;
  79. }
  80. }
  81. } else {
  82. WARN_ON(nvm_rq_from_c_ctx(c_ctx) != rqd);
  83. list_add_tail(&c_ctx->list, &pblk->compl_list);
  84. }
  85. pblk_rb_sync_end(&pblk->rwb, &flags);
  86. }
  87. /* Map remaining sectors in chunk, starting from ppa */
  88. static void pblk_map_remaining(struct pblk *pblk, struct ppa_addr *ppa)
  89. {
  90. struct nvm_tgt_dev *dev = pblk->dev;
  91. struct nvm_geo *geo = &dev->geo;
  92. struct pblk_line *line;
  93. struct ppa_addr map_ppa = *ppa;
  94. u64 paddr;
  95. int done = 0;
  96. line = &pblk->lines[pblk_ppa_to_line(*ppa)];
  97. spin_lock(&line->lock);
  98. while (!done) {
  99. paddr = pblk_dev_ppa_to_line_addr(pblk, map_ppa);
  100. if (!test_and_set_bit(paddr, line->map_bitmap))
  101. line->left_msecs--;
  102. if (!test_and_set_bit(paddr, line->invalid_bitmap))
  103. le32_add_cpu(line->vsc, -1);
  104. if (geo->version == NVM_OCSSD_SPEC_12) {
  105. map_ppa.ppa++;
  106. if (map_ppa.g.pg == geo->num_pg)
  107. done = 1;
  108. } else {
  109. map_ppa.m.sec++;
  110. if (map_ppa.m.sec == geo->clba)
  111. done = 1;
  112. }
  113. }
  114. line->w_err_gc->has_write_err = 1;
  115. spin_unlock(&line->lock);
  116. }
  117. static void pblk_prepare_resubmit(struct pblk *pblk, unsigned int sentry,
  118. unsigned int nr_entries)
  119. {
  120. struct pblk_rb *rb = &pblk->rwb;
  121. struct pblk_rb_entry *entry;
  122. struct pblk_line *line;
  123. struct pblk_w_ctx *w_ctx;
  124. struct ppa_addr ppa_l2p;
  125. int flags;
  126. unsigned int pos, i;
  127. spin_lock(&pblk->trans_lock);
  128. pos = sentry;
  129. for (i = 0; i < nr_entries; i++) {
  130. entry = &rb->entries[pos];
  131. w_ctx = &entry->w_ctx;
  132. /* Check if the lba has been overwritten */
  133. if (w_ctx->lba != ADDR_EMPTY) {
  134. ppa_l2p = pblk_trans_map_get(pblk, w_ctx->lba);
  135. if (!pblk_ppa_comp(ppa_l2p, entry->cacheline))
  136. w_ctx->lba = ADDR_EMPTY;
  137. }
  138. /* Mark up the entry as submittable again */
  139. flags = READ_ONCE(w_ctx->flags);
  140. flags |= PBLK_WRITTEN_DATA;
  141. /* Release flags on write context. Protect from writes */
  142. smp_store_release(&w_ctx->flags, flags);
  143. /* Decrese the reference count to the line as we will
  144. * re-map these entries
  145. */
  146. line = &pblk->lines[pblk_ppa_to_line(w_ctx->ppa)];
  147. kref_put(&line->ref, pblk_line_put);
  148. pos = (pos + 1) & (rb->nr_entries - 1);
  149. }
  150. spin_unlock(&pblk->trans_lock);
  151. }
  152. static void pblk_queue_resubmit(struct pblk *pblk, struct pblk_c_ctx *c_ctx)
  153. {
  154. struct pblk_c_ctx *r_ctx;
  155. r_ctx = kzalloc(sizeof(struct pblk_c_ctx), GFP_KERNEL);
  156. if (!r_ctx)
  157. return;
  158. r_ctx->lun_bitmap = NULL;
  159. r_ctx->sentry = c_ctx->sentry;
  160. r_ctx->nr_valid = c_ctx->nr_valid;
  161. r_ctx->nr_padded = c_ctx->nr_padded;
  162. spin_lock(&pblk->resubmit_lock);
  163. list_add_tail(&r_ctx->list, &pblk->resubmit_list);
  164. spin_unlock(&pblk->resubmit_lock);
  165. #ifdef CONFIG_NVM_PBLK_DEBUG
  166. atomic_long_add(c_ctx->nr_valid, &pblk->recov_writes);
  167. #endif
  168. }
  169. static void pblk_submit_rec(struct work_struct *work)
  170. {
  171. struct pblk_rec_ctx *recovery =
  172. container_of(work, struct pblk_rec_ctx, ws_rec);
  173. struct pblk *pblk = recovery->pblk;
  174. struct nvm_rq *rqd = recovery->rqd;
  175. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  176. struct ppa_addr *ppa_list;
  177. pblk_log_write_err(pblk, rqd);
  178. if (rqd->nr_ppas == 1)
  179. ppa_list = &rqd->ppa_addr;
  180. else
  181. ppa_list = rqd->ppa_list;
  182. pblk_map_remaining(pblk, ppa_list);
  183. pblk_queue_resubmit(pblk, c_ctx);
  184. pblk_up_rq(pblk, rqd->ppa_list, rqd->nr_ppas, c_ctx->lun_bitmap);
  185. if (c_ctx->nr_padded)
  186. pblk_bio_free_pages(pblk, rqd->bio, c_ctx->nr_valid,
  187. c_ctx->nr_padded);
  188. bio_put(rqd->bio);
  189. pblk_free_rqd(pblk, rqd, PBLK_WRITE);
  190. mempool_free(recovery, &pblk->rec_pool);
  191. atomic_dec(&pblk->inflight_io);
  192. }
  193. static void pblk_end_w_fail(struct pblk *pblk, struct nvm_rq *rqd)
  194. {
  195. struct pblk_rec_ctx *recovery;
  196. recovery = mempool_alloc(&pblk->rec_pool, GFP_ATOMIC);
  197. if (!recovery) {
  198. pblk_err(pblk, "could not allocate recovery work\n");
  199. return;
  200. }
  201. recovery->pblk = pblk;
  202. recovery->rqd = rqd;
  203. INIT_WORK(&recovery->ws_rec, pblk_submit_rec);
  204. queue_work(pblk->close_wq, &recovery->ws_rec);
  205. }
  206. static void pblk_end_io_write(struct nvm_rq *rqd)
  207. {
  208. struct pblk *pblk = rqd->private;
  209. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  210. if (rqd->error) {
  211. pblk_end_w_fail(pblk, rqd);
  212. return;
  213. }
  214. #ifdef CONFIG_NVM_PBLK_DEBUG
  215. else
  216. WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
  217. #endif
  218. pblk_complete_write(pblk, rqd, c_ctx);
  219. atomic_dec(&pblk->inflight_io);
  220. }
  221. static void pblk_end_io_write_meta(struct nvm_rq *rqd)
  222. {
  223. struct pblk *pblk = rqd->private;
  224. struct pblk_g_ctx *m_ctx = nvm_rq_to_pdu(rqd);
  225. struct pblk_line *line = m_ctx->private;
  226. struct pblk_emeta *emeta = line->emeta;
  227. int sync;
  228. pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
  229. if (rqd->error) {
  230. pblk_log_write_err(pblk, rqd);
  231. pblk_err(pblk, "metadata I/O failed. Line %d\n", line->id);
  232. line->w_err_gc->has_write_err = 1;
  233. }
  234. sync = atomic_add_return(rqd->nr_ppas, &emeta->sync);
  235. if (sync == emeta->nr_entries)
  236. pblk_gen_run_ws(pblk, line, NULL, pblk_line_close_ws,
  237. GFP_ATOMIC, pblk->close_wq);
  238. pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
  239. atomic_dec(&pblk->inflight_io);
  240. }
  241. static int pblk_alloc_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
  242. unsigned int nr_secs,
  243. nvm_end_io_fn(*end_io))
  244. {
  245. struct nvm_tgt_dev *dev = pblk->dev;
  246. /* Setup write request */
  247. rqd->opcode = NVM_OP_PWRITE;
  248. rqd->nr_ppas = nr_secs;
  249. rqd->flags = pblk_set_progr_mode(pblk, PBLK_WRITE);
  250. rqd->private = pblk;
  251. rqd->end_io = end_io;
  252. rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  253. &rqd->dma_meta_list);
  254. if (!rqd->meta_list)
  255. return -ENOMEM;
  256. rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
  257. rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
  258. return 0;
  259. }
  260. static int pblk_setup_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
  261. struct ppa_addr *erase_ppa)
  262. {
  263. struct pblk_line_meta *lm = &pblk->lm;
  264. struct pblk_line *e_line = pblk_line_get_erase(pblk);
  265. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  266. unsigned int valid = c_ctx->nr_valid;
  267. unsigned int padded = c_ctx->nr_padded;
  268. unsigned int nr_secs = valid + padded;
  269. unsigned long *lun_bitmap;
  270. int ret;
  271. lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
  272. if (!lun_bitmap)
  273. return -ENOMEM;
  274. c_ctx->lun_bitmap = lun_bitmap;
  275. ret = pblk_alloc_w_rq(pblk, rqd, nr_secs, pblk_end_io_write);
  276. if (ret) {
  277. kfree(lun_bitmap);
  278. return ret;
  279. }
  280. if (likely(!e_line || !atomic_read(&e_line->left_eblks)))
  281. pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, valid, 0);
  282. else
  283. pblk_map_erase_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
  284. valid, erase_ppa);
  285. return 0;
  286. }
  287. static int pblk_calc_secs_to_sync(struct pblk *pblk, unsigned int secs_avail,
  288. unsigned int secs_to_flush)
  289. {
  290. int secs_to_sync;
  291. secs_to_sync = pblk_calc_secs(pblk, secs_avail, secs_to_flush);
  292. #ifdef CONFIG_NVM_PBLK_DEBUG
  293. if ((!secs_to_sync && secs_to_flush)
  294. || (secs_to_sync < 0)
  295. || (secs_to_sync > secs_avail && !secs_to_flush)) {
  296. pblk_err(pblk, "bad sector calculation (a:%d,s:%d,f:%d)\n",
  297. secs_avail, secs_to_sync, secs_to_flush);
  298. }
  299. #endif
  300. return secs_to_sync;
  301. }
  302. int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line)
  303. {
  304. struct nvm_tgt_dev *dev = pblk->dev;
  305. struct nvm_geo *geo = &dev->geo;
  306. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  307. struct pblk_line_meta *lm = &pblk->lm;
  308. struct pblk_emeta *emeta = meta_line->emeta;
  309. struct pblk_g_ctx *m_ctx;
  310. struct bio *bio;
  311. struct nvm_rq *rqd;
  312. void *data;
  313. u64 paddr;
  314. int rq_ppas = pblk->min_write_pgs;
  315. int id = meta_line->id;
  316. int rq_len;
  317. int i, j;
  318. int ret;
  319. rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
  320. m_ctx = nvm_rq_to_pdu(rqd);
  321. m_ctx->private = meta_line;
  322. rq_len = rq_ppas * geo->csecs;
  323. data = ((void *)emeta->buf) + emeta->mem;
  324. bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
  325. l_mg->emeta_alloc_type, GFP_KERNEL);
  326. if (IS_ERR(bio)) {
  327. pblk_err(pblk, "failed to map emeta io");
  328. ret = PTR_ERR(bio);
  329. goto fail_free_rqd;
  330. }
  331. bio->bi_iter.bi_sector = 0; /* internal bio */
  332. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  333. rqd->bio = bio;
  334. ret = pblk_alloc_w_rq(pblk, rqd, rq_ppas, pblk_end_io_write_meta);
  335. if (ret)
  336. goto fail_free_bio;
  337. for (i = 0; i < rqd->nr_ppas; ) {
  338. spin_lock(&meta_line->lock);
  339. paddr = __pblk_alloc_page(pblk, meta_line, rq_ppas);
  340. spin_unlock(&meta_line->lock);
  341. for (j = 0; j < rq_ppas; j++, i++, paddr++)
  342. rqd->ppa_list[i] = addr_to_gen_ppa(pblk, paddr, id);
  343. }
  344. spin_lock(&l_mg->close_lock);
  345. emeta->mem += rq_len;
  346. if (emeta->mem >= lm->emeta_len[0])
  347. list_del(&meta_line->list);
  348. spin_unlock(&l_mg->close_lock);
  349. pblk_down_page(pblk, rqd->ppa_list, rqd->nr_ppas);
  350. ret = pblk_submit_io(pblk, rqd);
  351. if (ret) {
  352. pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
  353. goto fail_rollback;
  354. }
  355. return NVM_IO_OK;
  356. fail_rollback:
  357. pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
  358. spin_lock(&l_mg->close_lock);
  359. pblk_dealloc_page(pblk, meta_line, rq_ppas);
  360. list_add(&meta_line->list, &meta_line->list);
  361. spin_unlock(&l_mg->close_lock);
  362. fail_free_bio:
  363. bio_put(bio);
  364. fail_free_rqd:
  365. pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
  366. return ret;
  367. }
  368. static inline bool pblk_valid_meta_ppa(struct pblk *pblk,
  369. struct pblk_line *meta_line,
  370. struct nvm_rq *data_rqd)
  371. {
  372. struct nvm_tgt_dev *dev = pblk->dev;
  373. struct nvm_geo *geo = &dev->geo;
  374. struct pblk_c_ctx *data_c_ctx = nvm_rq_to_pdu(data_rqd);
  375. struct pblk_line *data_line = pblk_line_get_data(pblk);
  376. struct ppa_addr ppa, ppa_opt;
  377. u64 paddr;
  378. int pos_opt;
  379. /* Schedule a metadata I/O that is half the distance from the data I/O
  380. * with regards to the number of LUNs forming the pblk instance. This
  381. * balances LUN conflicts across every I/O.
  382. *
  383. * When the LUN configuration changes (e.g., due to GC), this distance
  384. * can align, which would result on metadata and data I/Os colliding. In
  385. * this case, modify the distance to not be optimal, but move the
  386. * optimal in the right direction.
  387. */
  388. paddr = pblk_lookup_page(pblk, meta_line);
  389. ppa = addr_to_gen_ppa(pblk, paddr, 0);
  390. ppa_opt = addr_to_gen_ppa(pblk, paddr + data_line->meta_distance, 0);
  391. pos_opt = pblk_ppa_to_pos(geo, ppa_opt);
  392. if (test_bit(pos_opt, data_c_ctx->lun_bitmap) ||
  393. test_bit(pos_opt, data_line->blk_bitmap))
  394. return true;
  395. if (unlikely(pblk_ppa_comp(ppa_opt, ppa)))
  396. data_line->meta_distance--;
  397. return false;
  398. }
  399. static struct pblk_line *pblk_should_submit_meta_io(struct pblk *pblk,
  400. struct nvm_rq *data_rqd)
  401. {
  402. struct pblk_line_meta *lm = &pblk->lm;
  403. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  404. struct pblk_line *meta_line;
  405. spin_lock(&l_mg->close_lock);
  406. if (list_empty(&l_mg->emeta_list)) {
  407. spin_unlock(&l_mg->close_lock);
  408. return NULL;
  409. }
  410. meta_line = list_first_entry(&l_mg->emeta_list, struct pblk_line, list);
  411. if (meta_line->emeta->mem >= lm->emeta_len[0]) {
  412. spin_unlock(&l_mg->close_lock);
  413. return NULL;
  414. }
  415. spin_unlock(&l_mg->close_lock);
  416. if (!pblk_valid_meta_ppa(pblk, meta_line, data_rqd))
  417. return NULL;
  418. return meta_line;
  419. }
  420. static int pblk_submit_io_set(struct pblk *pblk, struct nvm_rq *rqd)
  421. {
  422. struct ppa_addr erase_ppa;
  423. struct pblk_line *meta_line;
  424. int err;
  425. pblk_ppa_set_empty(&erase_ppa);
  426. /* Assign lbas to ppas and populate request structure */
  427. err = pblk_setup_w_rq(pblk, rqd, &erase_ppa);
  428. if (err) {
  429. pblk_err(pblk, "could not setup write request: %d\n", err);
  430. return NVM_IO_ERR;
  431. }
  432. meta_line = pblk_should_submit_meta_io(pblk, rqd);
  433. /* Submit data write for current data line */
  434. err = pblk_submit_io(pblk, rqd);
  435. if (err) {
  436. pblk_err(pblk, "data I/O submission failed: %d\n", err);
  437. return NVM_IO_ERR;
  438. }
  439. if (!pblk_ppa_empty(erase_ppa)) {
  440. /* Submit erase for next data line */
  441. if (pblk_blk_erase_async(pblk, erase_ppa)) {
  442. struct pblk_line *e_line = pblk_line_get_erase(pblk);
  443. struct nvm_tgt_dev *dev = pblk->dev;
  444. struct nvm_geo *geo = &dev->geo;
  445. int bit;
  446. atomic_inc(&e_line->left_eblks);
  447. bit = pblk_ppa_to_pos(geo, erase_ppa);
  448. WARN_ON(!test_and_clear_bit(bit, e_line->erase_bitmap));
  449. }
  450. }
  451. if (meta_line) {
  452. /* Submit metadata write for previous data line */
  453. err = pblk_submit_meta_io(pblk, meta_line);
  454. if (err) {
  455. pblk_err(pblk, "metadata I/O submission failed: %d",
  456. err);
  457. return NVM_IO_ERR;
  458. }
  459. }
  460. return NVM_IO_OK;
  461. }
  462. static void pblk_free_write_rqd(struct pblk *pblk, struct nvm_rq *rqd)
  463. {
  464. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  465. struct bio *bio = rqd->bio;
  466. if (c_ctx->nr_padded)
  467. pblk_bio_free_pages(pblk, bio, c_ctx->nr_valid,
  468. c_ctx->nr_padded);
  469. }
  470. static int pblk_submit_write(struct pblk *pblk)
  471. {
  472. struct bio *bio;
  473. struct nvm_rq *rqd;
  474. unsigned int secs_avail, secs_to_sync, secs_to_com;
  475. unsigned int secs_to_flush;
  476. unsigned long pos;
  477. unsigned int resubmit;
  478. spin_lock(&pblk->resubmit_lock);
  479. resubmit = !list_empty(&pblk->resubmit_list);
  480. spin_unlock(&pblk->resubmit_lock);
  481. /* Resubmit failed writes first */
  482. if (resubmit) {
  483. struct pblk_c_ctx *r_ctx;
  484. spin_lock(&pblk->resubmit_lock);
  485. r_ctx = list_first_entry(&pblk->resubmit_list,
  486. struct pblk_c_ctx, list);
  487. list_del(&r_ctx->list);
  488. spin_unlock(&pblk->resubmit_lock);
  489. secs_avail = r_ctx->nr_valid;
  490. pos = r_ctx->sentry;
  491. pblk_prepare_resubmit(pblk, pos, secs_avail);
  492. secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail,
  493. secs_avail);
  494. kfree(r_ctx);
  495. } else {
  496. /* If there are no sectors in the cache,
  497. * flushes (bios without data) will be cleared on
  498. * the cache threads
  499. */
  500. secs_avail = pblk_rb_read_count(&pblk->rwb);
  501. if (!secs_avail)
  502. return 1;
  503. secs_to_flush = pblk_rb_flush_point_count(&pblk->rwb);
  504. if (!secs_to_flush && secs_avail < pblk->min_write_pgs)
  505. return 1;
  506. secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail,
  507. secs_to_flush);
  508. if (secs_to_sync > pblk->max_write_pgs) {
  509. pblk_err(pblk, "bad buffer sync calculation\n");
  510. return 1;
  511. }
  512. secs_to_com = (secs_to_sync > secs_avail) ?
  513. secs_avail : secs_to_sync;
  514. pos = pblk_rb_read_commit(&pblk->rwb, secs_to_com);
  515. }
  516. bio = bio_alloc(GFP_KERNEL, secs_to_sync);
  517. bio->bi_iter.bi_sector = 0; /* internal bio */
  518. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  519. rqd = pblk_alloc_rqd(pblk, PBLK_WRITE);
  520. rqd->bio = bio;
  521. if (pblk_rb_read_to_bio(&pblk->rwb, rqd, pos, secs_to_sync,
  522. secs_avail)) {
  523. pblk_err(pblk, "corrupted write bio\n");
  524. goto fail_put_bio;
  525. }
  526. if (pblk_submit_io_set(pblk, rqd))
  527. goto fail_free_bio;
  528. #ifdef CONFIG_NVM_PBLK_DEBUG
  529. atomic_long_add(secs_to_sync, &pblk->sub_writes);
  530. #endif
  531. return 0;
  532. fail_free_bio:
  533. pblk_free_write_rqd(pblk, rqd);
  534. fail_put_bio:
  535. bio_put(bio);
  536. pblk_free_rqd(pblk, rqd, PBLK_WRITE);
  537. return 1;
  538. }
  539. int pblk_write_ts(void *data)
  540. {
  541. struct pblk *pblk = data;
  542. while (!kthread_should_stop()) {
  543. if (!pblk_submit_write(pblk))
  544. continue;
  545. set_current_state(TASK_INTERRUPTIBLE);
  546. io_schedule();
  547. }
  548. return 0;
  549. }