pblk-write.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 void pblk_sync_line(struct pblk *pblk, struct pblk_line *line)
  19. {
  20. #ifdef CONFIG_NVM_DEBUG
  21. atomic_long_inc(&pblk->sync_writes);
  22. #endif
  23. /* Counter protected by rb sync lock */
  24. line->left_ssecs--;
  25. if (!line->left_ssecs)
  26. pblk_line_run_ws(pblk, line, NULL, pblk_line_close_ws);
  27. }
  28. static unsigned long pblk_end_w_bio(struct pblk *pblk, struct nvm_rq *rqd,
  29. struct pblk_c_ctx *c_ctx)
  30. {
  31. struct nvm_tgt_dev *dev = pblk->dev;
  32. struct bio *original_bio;
  33. unsigned long ret;
  34. int i;
  35. for (i = 0; i < c_ctx->nr_valid; i++) {
  36. struct pblk_w_ctx *w_ctx;
  37. struct ppa_addr p;
  38. struct pblk_line *line;
  39. w_ctx = pblk_rb_w_ctx(&pblk->rwb, c_ctx->sentry + i);
  40. p = rqd->ppa_list[i];
  41. line = &pblk->lines[pblk_dev_ppa_to_line(p)];
  42. pblk_sync_line(pblk, line);
  43. while ((original_bio = bio_list_pop(&w_ctx->bios)))
  44. bio_endio(original_bio);
  45. }
  46. #ifdef CONFIG_NVM_DEBUG
  47. atomic_long_add(c_ctx->nr_valid, &pblk->compl_writes);
  48. #endif
  49. ret = pblk_rb_sync_advance(&pblk->rwb, c_ctx->nr_valid);
  50. if (rqd->meta_list)
  51. nvm_dev_dma_free(dev->parent, rqd->meta_list,
  52. rqd->dma_meta_list);
  53. bio_put(rqd->bio);
  54. pblk_free_rqd(pblk, rqd, WRITE);
  55. return ret;
  56. }
  57. static unsigned long pblk_end_queued_w_bio(struct pblk *pblk,
  58. struct nvm_rq *rqd,
  59. struct pblk_c_ctx *c_ctx)
  60. {
  61. list_del(&c_ctx->list);
  62. return pblk_end_w_bio(pblk, rqd, c_ctx);
  63. }
  64. static void pblk_complete_write(struct pblk *pblk, struct nvm_rq *rqd,
  65. struct pblk_c_ctx *c_ctx)
  66. {
  67. struct pblk_c_ctx *c, *r;
  68. unsigned long flags;
  69. unsigned long pos;
  70. #ifdef CONFIG_NVM_DEBUG
  71. atomic_long_sub(c_ctx->nr_valid, &pblk->inflight_writes);
  72. #endif
  73. pblk_up_rq(pblk, rqd->ppa_list, rqd->nr_ppas, c_ctx->lun_bitmap);
  74. pos = pblk_rb_sync_init(&pblk->rwb, &flags);
  75. if (pos == c_ctx->sentry) {
  76. pos = pblk_end_w_bio(pblk, rqd, c_ctx);
  77. retry:
  78. list_for_each_entry_safe(c, r, &pblk->compl_list, list) {
  79. rqd = nvm_rq_from_c_ctx(c);
  80. if (c->sentry == pos) {
  81. pos = pblk_end_queued_w_bio(pblk, rqd, c);
  82. goto retry;
  83. }
  84. }
  85. } else {
  86. WARN_ON(nvm_rq_from_c_ctx(c_ctx) != rqd);
  87. list_add_tail(&c_ctx->list, &pblk->compl_list);
  88. }
  89. pblk_rb_sync_end(&pblk->rwb, &flags);
  90. }
  91. /* When a write fails, we are not sure whether the block has grown bad or a page
  92. * range is more susceptible to write errors. If a high number of pages fail, we
  93. * assume that the block is bad and we mark it accordingly. In all cases, we
  94. * remap and resubmit the failed entries as fast as possible; if a flush is
  95. * waiting on a completion, the whole stack would stall otherwise.
  96. */
  97. static void pblk_end_w_fail(struct pblk *pblk, struct nvm_rq *rqd)
  98. {
  99. void *comp_bits = &rqd->ppa_status;
  100. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  101. struct pblk_rec_ctx *recovery;
  102. struct ppa_addr *ppa_list = rqd->ppa_list;
  103. int nr_ppas = rqd->nr_ppas;
  104. unsigned int c_entries;
  105. int bit, ret;
  106. if (unlikely(nr_ppas == 1))
  107. ppa_list = &rqd->ppa_addr;
  108. recovery = mempool_alloc(pblk->rec_pool, GFP_ATOMIC);
  109. if (!recovery) {
  110. pr_err("pblk: could not allocate recovery context\n");
  111. return;
  112. }
  113. INIT_LIST_HEAD(&recovery->failed);
  114. bit = -1;
  115. while ((bit = find_next_bit(comp_bits, nr_ppas, bit + 1)) < nr_ppas) {
  116. struct pblk_rb_entry *entry;
  117. struct ppa_addr ppa;
  118. /* Logic error */
  119. if (bit > c_ctx->nr_valid) {
  120. WARN_ONCE(1, "pblk: corrupted write request\n");
  121. mempool_free(recovery, pblk->rec_pool);
  122. goto out;
  123. }
  124. ppa = ppa_list[bit];
  125. entry = pblk_rb_sync_scan_entry(&pblk->rwb, &ppa);
  126. if (!entry) {
  127. pr_err("pblk: could not scan entry on write failure\n");
  128. mempool_free(recovery, pblk->rec_pool);
  129. goto out;
  130. }
  131. /* The list is filled first and emptied afterwards. No need for
  132. * protecting it with a lock
  133. */
  134. list_add_tail(&entry->index, &recovery->failed);
  135. }
  136. c_entries = find_first_bit(comp_bits, nr_ppas);
  137. ret = pblk_recov_setup_rq(pblk, c_ctx, recovery, comp_bits, c_entries);
  138. if (ret) {
  139. pr_err("pblk: could not recover from write failure\n");
  140. mempool_free(recovery, pblk->rec_pool);
  141. goto out;
  142. }
  143. INIT_WORK(&recovery->ws_rec, pblk_submit_rec);
  144. queue_work(pblk->kw_wq, &recovery->ws_rec);
  145. out:
  146. pblk_complete_write(pblk, rqd, c_ctx);
  147. }
  148. static void pblk_end_io_write(struct nvm_rq *rqd)
  149. {
  150. struct pblk *pblk = rqd->private;
  151. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  152. if (rqd->error) {
  153. pblk_log_write_err(pblk, rqd);
  154. return pblk_end_w_fail(pblk, rqd);
  155. }
  156. #ifdef CONFIG_NVM_DEBUG
  157. else
  158. WARN_ONCE(rqd->bio->bi_error, "pblk: corrupted write error\n");
  159. #endif
  160. pblk_complete_write(pblk, rqd, c_ctx);
  161. }
  162. static int pblk_alloc_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
  163. unsigned int nr_secs)
  164. {
  165. struct nvm_tgt_dev *dev = pblk->dev;
  166. /* Setup write request */
  167. rqd->opcode = NVM_OP_PWRITE;
  168. rqd->nr_ppas = nr_secs;
  169. rqd->flags = pblk_set_progr_mode(pblk, WRITE);
  170. rqd->private = pblk;
  171. rqd->end_io = pblk_end_io_write;
  172. rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  173. &rqd->dma_meta_list);
  174. if (!rqd->meta_list)
  175. return -ENOMEM;
  176. if (unlikely(nr_secs == 1))
  177. return 0;
  178. rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
  179. rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
  180. return 0;
  181. }
  182. static int pblk_setup_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
  183. struct pblk_c_ctx *c_ctx)
  184. {
  185. struct pblk_line_meta *lm = &pblk->lm;
  186. struct pblk_line *e_line = pblk_line_get_data_next(pblk);
  187. struct ppa_addr erase_ppa;
  188. unsigned int valid = c_ctx->nr_valid;
  189. unsigned int padded = c_ctx->nr_padded;
  190. unsigned int nr_secs = valid + padded;
  191. unsigned long *lun_bitmap;
  192. int ret = 0;
  193. lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
  194. if (!lun_bitmap) {
  195. ret = -ENOMEM;
  196. goto out;
  197. }
  198. c_ctx->lun_bitmap = lun_bitmap;
  199. ret = pblk_alloc_w_rq(pblk, rqd, nr_secs);
  200. if (ret) {
  201. kfree(lun_bitmap);
  202. goto out;
  203. }
  204. ppa_set_empty(&erase_ppa);
  205. if (likely(!e_line || !atomic_read(&e_line->left_eblks)))
  206. pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, valid, 0);
  207. else
  208. pblk_map_erase_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
  209. valid, &erase_ppa);
  210. out:
  211. if (unlikely(e_line && !ppa_empty(erase_ppa))) {
  212. if (pblk_blk_erase_async(pblk, erase_ppa)) {
  213. struct nvm_tgt_dev *dev = pblk->dev;
  214. struct nvm_geo *geo = &dev->geo;
  215. int bit;
  216. atomic_inc(&e_line->left_eblks);
  217. bit = erase_ppa.g.lun * geo->nr_chnls + erase_ppa.g.ch;
  218. WARN_ON(!test_and_clear_bit(bit, e_line->erase_bitmap));
  219. up(&pblk->erase_sem);
  220. }
  221. }
  222. return ret;
  223. }
  224. int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
  225. struct pblk_c_ctx *c_ctx)
  226. {
  227. struct pblk_line_meta *lm = &pblk->lm;
  228. unsigned long *lun_bitmap;
  229. int ret;
  230. lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
  231. if (!lun_bitmap)
  232. return -ENOMEM;
  233. c_ctx->lun_bitmap = lun_bitmap;
  234. ret = pblk_alloc_w_rq(pblk, rqd, rqd->nr_ppas);
  235. if (ret)
  236. return ret;
  237. pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, c_ctx->nr_valid, 0);
  238. rqd->ppa_status = (u64)0;
  239. rqd->flags = pblk_set_progr_mode(pblk, WRITE);
  240. return ret;
  241. }
  242. static int pblk_calc_secs_to_sync(struct pblk *pblk, unsigned int secs_avail,
  243. unsigned int secs_to_flush)
  244. {
  245. int secs_to_sync;
  246. secs_to_sync = pblk_calc_secs(pblk, secs_avail, secs_to_flush);
  247. #ifdef CONFIG_NVM_DEBUG
  248. if ((!secs_to_sync && secs_to_flush)
  249. || (secs_to_sync < 0)
  250. || (secs_to_sync > secs_avail && !secs_to_flush)) {
  251. pr_err("pblk: bad sector calculation (a:%d,s:%d,f:%d)\n",
  252. secs_avail, secs_to_sync, secs_to_flush);
  253. }
  254. #endif
  255. return secs_to_sync;
  256. }
  257. static int pblk_submit_write(struct pblk *pblk)
  258. {
  259. struct bio *bio;
  260. struct nvm_rq *rqd;
  261. struct pblk_c_ctx *c_ctx;
  262. unsigned int pgs_read;
  263. unsigned int secs_avail, secs_to_sync, secs_to_com;
  264. unsigned int secs_to_flush;
  265. unsigned long pos;
  266. int err;
  267. /* If there are no sectors in the cache, flushes (bios without data)
  268. * will be cleared on the cache threads
  269. */
  270. secs_avail = pblk_rb_read_count(&pblk->rwb);
  271. if (!secs_avail)
  272. return 1;
  273. secs_to_flush = pblk_rb_sync_point_count(&pblk->rwb);
  274. if (!secs_to_flush && secs_avail < pblk->min_write_pgs)
  275. return 1;
  276. rqd = pblk_alloc_rqd(pblk, WRITE);
  277. if (IS_ERR(rqd)) {
  278. pr_err("pblk: cannot allocate write req.\n");
  279. return 1;
  280. }
  281. c_ctx = nvm_rq_to_pdu(rqd);
  282. bio = bio_alloc(GFP_KERNEL, pblk->max_write_pgs);
  283. if (!bio) {
  284. pr_err("pblk: cannot allocate write bio\n");
  285. goto fail_free_rqd;
  286. }
  287. bio->bi_iter.bi_sector = 0; /* internal bio */
  288. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  289. rqd->bio = bio;
  290. secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail, secs_to_flush);
  291. if (secs_to_sync > pblk->max_write_pgs) {
  292. pr_err("pblk: bad buffer sync calculation\n");
  293. goto fail_put_bio;
  294. }
  295. secs_to_com = (secs_to_sync > secs_avail) ? secs_avail : secs_to_sync;
  296. pos = pblk_rb_read_commit(&pblk->rwb, secs_to_com);
  297. pgs_read = pblk_rb_read_to_bio(&pblk->rwb, bio, c_ctx, pos,
  298. secs_to_sync, secs_avail);
  299. if (!pgs_read) {
  300. pr_err("pblk: corrupted write bio\n");
  301. goto fail_put_bio;
  302. }
  303. if (c_ctx->nr_padded)
  304. if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, c_ctx->nr_padded))
  305. goto fail_put_bio;
  306. /* Assign lbas to ppas and populate request structure */
  307. err = pblk_setup_w_rq(pblk, rqd, c_ctx);
  308. if (err) {
  309. pr_err("pblk: could not setup write request\n");
  310. goto fail_free_bio;
  311. }
  312. err = pblk_submit_io(pblk, rqd);
  313. if (err) {
  314. pr_err("pblk: I/O submission failed: %d\n", err);
  315. goto fail_free_bio;
  316. }
  317. #ifdef CONFIG_NVM_DEBUG
  318. atomic_long_add(secs_to_sync, &pblk->sub_writes);
  319. #endif
  320. return 0;
  321. fail_free_bio:
  322. if (c_ctx->nr_padded)
  323. pblk_bio_free_pages(pblk, bio, secs_to_sync, c_ctx->nr_padded);
  324. fail_put_bio:
  325. bio_put(bio);
  326. fail_free_rqd:
  327. pblk_free_rqd(pblk, rqd, WRITE);
  328. return 1;
  329. }
  330. int pblk_write_ts(void *data)
  331. {
  332. struct pblk *pblk = data;
  333. while (!kthread_should_stop()) {
  334. if (!pblk_submit_write(pblk))
  335. continue;
  336. set_current_state(TASK_INTERRUPTIBLE);
  337. io_schedule();
  338. }
  339. return 0;
  340. }