blob: c73b17bca06b0cb7abf03a0534223d6ec9b91892 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
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
18#include "pblk.h"
19
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020020static unsigned long pblk_end_w_bio(struct pblk *pblk, struct nvm_rq *rqd,
21 struct pblk_c_ctx *c_ctx)
22{
23 struct nvm_tgt_dev *dev = pblk->dev;
24 struct bio *original_bio;
25 unsigned long ret;
26 int i;
27
28 for (i = 0; i < c_ctx->nr_valid; i++) {
29 struct pblk_w_ctx *w_ctx;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020030
31 w_ctx = pblk_rb_w_ctx(&pblk->rwb, c_ctx->sentry + i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020032 while ((original_bio = bio_list_pop(&w_ctx->bios)))
33 bio_endio(original_bio);
34 }
35
Javier Gonzálezcd8ddbf2017-10-13 14:46:03 +020036 if (c_ctx->nr_padded)
37 pblk_bio_free_pages(pblk, rqd->bio, c_ctx->nr_valid,
38 c_ctx->nr_padded);
39
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020040#ifdef CONFIG_NVM_DEBUG
Javier Gonzáleze0e12a72017-10-13 14:46:04 +020041 atomic_long_add(rqd->nr_ppas, &pblk->sync_writes);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020042#endif
43
44 ret = pblk_rb_sync_advance(&pblk->rwb, c_ctx->nr_valid);
45
Javier González56c76412017-07-07 21:08:53 +020046 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020047
48 bio_put(rqd->bio);
49 pblk_free_rqd(pblk, rqd, WRITE);
50
51 return ret;
52}
53
54static unsigned long pblk_end_queued_w_bio(struct pblk *pblk,
55 struct nvm_rq *rqd,
56 struct pblk_c_ctx *c_ctx)
57{
58 list_del(&c_ctx->list);
59 return pblk_end_w_bio(pblk, rqd, c_ctx);
60}
61
62static void pblk_complete_write(struct pblk *pblk, struct nvm_rq *rqd,
63 struct pblk_c_ctx *c_ctx)
64{
65 struct pblk_c_ctx *c, *r;
66 unsigned long flags;
67 unsigned long pos;
68
69#ifdef CONFIG_NVM_DEBUG
70 atomic_long_sub(c_ctx->nr_valid, &pblk->inflight_writes);
71#endif
72
73 pblk_up_rq(pblk, rqd->ppa_list, rqd->nr_ppas, c_ctx->lun_bitmap);
74
75 pos = pblk_rb_sync_init(&pblk->rwb, &flags);
76 if (pos == c_ctx->sentry) {
77 pos = pblk_end_w_bio(pblk, rqd, c_ctx);
78
79retry:
80 list_for_each_entry_safe(c, r, &pblk->compl_list, list) {
81 rqd = nvm_rq_from_c_ctx(c);
82 if (c->sentry == pos) {
83 pos = pblk_end_queued_w_bio(pblk, rqd, c);
84 goto retry;
85 }
86 }
87 } else {
88 WARN_ON(nvm_rq_from_c_ctx(c_ctx) != rqd);
89 list_add_tail(&c_ctx->list, &pblk->compl_list);
90 }
91 pblk_rb_sync_end(&pblk->rwb, &flags);
92}
93
94/* When a write fails, we are not sure whether the block has grown bad or a page
95 * range is more susceptible to write errors. If a high number of pages fail, we
96 * assume that the block is bad and we mark it accordingly. In all cases, we
97 * remap and resubmit the failed entries as fast as possible; if a flush is
98 * waiting on a completion, the whole stack would stall otherwise.
99 */
100static void pblk_end_w_fail(struct pblk *pblk, struct nvm_rq *rqd)
101{
102 void *comp_bits = &rqd->ppa_status;
103 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
104 struct pblk_rec_ctx *recovery;
105 struct ppa_addr *ppa_list = rqd->ppa_list;
106 int nr_ppas = rqd->nr_ppas;
107 unsigned int c_entries;
108 int bit, ret;
109
110 if (unlikely(nr_ppas == 1))
111 ppa_list = &rqd->ppa_addr;
112
113 recovery = mempool_alloc(pblk->rec_pool, GFP_ATOMIC);
114 if (!recovery) {
115 pr_err("pblk: could not allocate recovery context\n");
116 return;
117 }
118 INIT_LIST_HEAD(&recovery->failed);
119
120 bit = -1;
121 while ((bit = find_next_bit(comp_bits, nr_ppas, bit + 1)) < nr_ppas) {
122 struct pblk_rb_entry *entry;
123 struct ppa_addr ppa;
124
125 /* Logic error */
126 if (bit > c_ctx->nr_valid) {
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200127 WARN_ONCE(1, "pblk: corrupted write request\n");
Javier González33db9fd2017-04-22 01:32:47 +0200128 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200129 goto out;
130 }
131
132 ppa = ppa_list[bit];
133 entry = pblk_rb_sync_scan_entry(&pblk->rwb, &ppa);
134 if (!entry) {
135 pr_err("pblk: could not scan entry on write failure\n");
Javier González33db9fd2017-04-22 01:32:47 +0200136 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200137 goto out;
138 }
139
140 /* The list is filled first and emptied afterwards. No need for
141 * protecting it with a lock
142 */
143 list_add_tail(&entry->index, &recovery->failed);
144 }
145
146 c_entries = find_first_bit(comp_bits, nr_ppas);
147 ret = pblk_recov_setup_rq(pblk, c_ctx, recovery, comp_bits, c_entries);
148 if (ret) {
149 pr_err("pblk: could not recover from write failure\n");
Javier González33db9fd2017-04-22 01:32:47 +0200150 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200151 goto out;
152 }
153
154 INIT_WORK(&recovery->ws_rec, pblk_submit_rec);
Javier Gonzálezef576492017-06-26 11:57:28 +0200155 queue_work(pblk->close_wq, &recovery->ws_rec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200156
157out:
158 pblk_complete_write(pblk, rqd, c_ctx);
159}
160
161static void pblk_end_io_write(struct nvm_rq *rqd)
162{
163 struct pblk *pblk = rqd->private;
164 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
165
166 if (rqd->error) {
167 pblk_log_write_err(pblk, rqd);
168 return pblk_end_w_fail(pblk, rqd);
169 }
170#ifdef CONFIG_NVM_DEBUG
171 else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200172 WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200173#endif
174
175 pblk_complete_write(pblk, rqd, c_ctx);
Javier González588726d32017-06-26 11:57:29 +0200176 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200177}
178
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200179static void pblk_end_io_write_meta(struct nvm_rq *rqd)
180{
181 struct pblk *pblk = rqd->private;
182 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200183 struct pblk_g_ctx *m_ctx = nvm_rq_to_pdu(rqd);
184 struct pblk_line *line = m_ctx->private;
185 struct pblk_emeta *emeta = line->emeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200186 int sync;
187
Javier González3eaa11e2017-07-07 21:08:52 +0200188 pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200189
190 if (rqd->error) {
191 pblk_log_write_err(pblk, rqd);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200192 pr_err("pblk: metadata I/O failed. Line %d\n", line->id);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200193 }
194#ifdef CONFIG_NVM_DEBUG
195 else
196 WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
197#endif
198
199 sync = atomic_add_return(rqd->nr_ppas, &emeta->sync);
200 if (sync == emeta->nr_entries)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200201 pblk_gen_run_ws(pblk, line, NULL, pblk_line_close_ws,
202 GFP_ATOMIC, pblk->close_wq);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200203
204 bio_put(rqd->bio);
Javier González3eaa11e2017-07-07 21:08:52 +0200205 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200206 pblk_free_rqd(pblk, rqd, READ);
Javier González588726d32017-06-26 11:57:29 +0200207
208 atomic_dec(&pblk->inflight_io);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200209}
210
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200211static int pblk_alloc_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200212 unsigned int nr_secs,
213 nvm_end_io_fn(*end_io))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214{
215 struct nvm_tgt_dev *dev = pblk->dev;
216
217 /* Setup write request */
218 rqd->opcode = NVM_OP_PWRITE;
219 rqd->nr_ppas = nr_secs;
220 rqd->flags = pblk_set_progr_mode(pblk, WRITE);
221 rqd->private = pblk;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200222 rqd->end_io = end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223
224 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
225 &rqd->dma_meta_list);
226 if (!rqd->meta_list)
227 return -ENOMEM;
228
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
230 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
231
232 return 0;
233}
234
235static int pblk_setup_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200236 struct pblk_c_ctx *c_ctx, struct ppa_addr *erase_ppa)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200237{
238 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezd624f372017-06-26 11:57:15 +0200239 struct pblk_line *e_line = pblk_line_get_erase(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200240 unsigned int valid = c_ctx->nr_valid;
241 unsigned int padded = c_ctx->nr_padded;
242 unsigned int nr_secs = valid + padded;
243 unsigned long *lun_bitmap;
244 int ret = 0;
245
246 lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200247 if (!lun_bitmap)
248 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200249 c_ctx->lun_bitmap = lun_bitmap;
250
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200251 ret = pblk_alloc_w_rq(pblk, rqd, nr_secs, pblk_end_io_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252 if (ret) {
253 kfree(lun_bitmap);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200254 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255 }
256
Javier González588726d32017-06-26 11:57:29 +0200257 if (likely(!e_line || !atomic_read(&e_line->left_eblks)))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200258 pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, valid, 0);
259 else
260 pblk_map_erase_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200261 valid, erase_ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200262
Javier Gonzálezd624f372017-06-26 11:57:15 +0200263 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200264}
265
266int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
267 struct pblk_c_ctx *c_ctx)
268{
269 struct pblk_line_meta *lm = &pblk->lm;
270 unsigned long *lun_bitmap;
271 int ret;
272
273 lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
274 if (!lun_bitmap)
275 return -ENOMEM;
276
277 c_ctx->lun_bitmap = lun_bitmap;
278
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200279 ret = pblk_alloc_w_rq(pblk, rqd, rqd->nr_ppas, pblk_end_io_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200280 if (ret)
281 return ret;
282
283 pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, c_ctx->nr_valid, 0);
284
285 rqd->ppa_status = (u64)0;
286 rqd->flags = pblk_set_progr_mode(pblk, WRITE);
287
288 return ret;
289}
290
291static int pblk_calc_secs_to_sync(struct pblk *pblk, unsigned int secs_avail,
292 unsigned int secs_to_flush)
293{
294 int secs_to_sync;
295
296 secs_to_sync = pblk_calc_secs(pblk, secs_avail, secs_to_flush);
297
298#ifdef CONFIG_NVM_DEBUG
299 if ((!secs_to_sync && secs_to_flush)
300 || (secs_to_sync < 0)
301 || (secs_to_sync > secs_avail && !secs_to_flush)) {
302 pr_err("pblk: bad sector calculation (a:%d,s:%d,f:%d)\n",
303 secs_avail, secs_to_sync, secs_to_flush);
304 }
305#endif
306
307 return secs_to_sync;
308}
309
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200310static inline int pblk_valid_meta_ppa(struct pblk *pblk,
311 struct pblk_line *meta_line,
312 struct ppa_addr *ppa_list, int nr_ppas)
313{
314 struct nvm_tgt_dev *dev = pblk->dev;
315 struct nvm_geo *geo = &dev->geo;
316 struct pblk_line *data_line;
317 struct ppa_addr ppa, ppa_opt;
318 u64 paddr;
319 int i;
320
321 data_line = &pblk->lines[pblk_dev_ppa_to_line(ppa_list[0])];
322 paddr = pblk_lookup_page(pblk, meta_line);
323 ppa = addr_to_gen_ppa(pblk, paddr, 0);
324
325 if (test_bit(pblk_ppa_to_pos(geo, ppa), data_line->blk_bitmap))
326 return 1;
327
328 /* Schedule a metadata I/O that is half the distance from the data I/O
329 * with regards to the number of LUNs forming the pblk instance. This
330 * balances LUN conflicts across every I/O.
331 *
332 * When the LUN configuration changes (e.g., due to GC), this distance
333 * can align, which would result on a LUN deadlock. In this case, modify
334 * the distance to not be optimal, but allow metadata I/Os to succeed.
335 */
336 ppa_opt = addr_to_gen_ppa(pblk, paddr + data_line->meta_distance, 0);
337 if (unlikely(ppa_opt.ppa == ppa.ppa)) {
338 data_line->meta_distance--;
339 return 0;
340 }
341
342 for (i = 0; i < nr_ppas; i += pblk->min_write_pgs)
343 if (ppa_list[i].g.ch == ppa_opt.g.ch &&
344 ppa_list[i].g.lun == ppa_opt.g.lun)
345 return 1;
346
347 if (test_bit(pblk_ppa_to_pos(geo, ppa_opt), data_line->blk_bitmap)) {
348 for (i = 0; i < nr_ppas; i += pblk->min_write_pgs)
349 if (ppa_list[i].g.ch == ppa.g.ch &&
350 ppa_list[i].g.lun == ppa.g.lun)
351 return 0;
352
353 return 1;
354 }
355
356 return 0;
357}
358
359int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line)
360{
361 struct nvm_tgt_dev *dev = pblk->dev;
362 struct nvm_geo *geo = &dev->geo;
363 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
364 struct pblk_line_meta *lm = &pblk->lm;
365 struct pblk_emeta *emeta = meta_line->emeta;
366 struct pblk_g_ctx *m_ctx;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200367 struct bio *bio;
368 struct nvm_rq *rqd;
369 void *data;
370 u64 paddr;
371 int rq_ppas = pblk->min_write_pgs;
372 int id = meta_line->id;
373 int rq_len;
374 int i, j;
375 int ret;
376
377 rqd = pblk_alloc_rqd(pblk, READ);
378 if (IS_ERR(rqd)) {
379 pr_err("pblk: cannot allocate write req.\n");
380 return PTR_ERR(rqd);
381 }
382 m_ctx = nvm_rq_to_pdu(rqd);
383 m_ctx->private = meta_line;
384
385 rq_len = rq_ppas * geo->sec_size;
386 data = ((void *)emeta->buf) + emeta->mem;
387
Javier Gonzálezde54e702017-06-30 17:56:39 +0200388 bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
389 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200390 if (IS_ERR(bio)) {
391 ret = PTR_ERR(bio);
392 goto fail_free_rqd;
393 }
394 bio->bi_iter.bi_sector = 0; /* internal bio */
395 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
396 rqd->bio = bio;
397
398 ret = pblk_alloc_w_rq(pblk, rqd, rq_ppas, pblk_end_io_write_meta);
399 if (ret)
400 goto fail_free_bio;
401
402 for (i = 0; i < rqd->nr_ppas; ) {
403 spin_lock(&meta_line->lock);
404 paddr = __pblk_alloc_page(pblk, meta_line, rq_ppas);
405 spin_unlock(&meta_line->lock);
406 for (j = 0; j < rq_ppas; j++, i++, paddr++)
407 rqd->ppa_list[i] = addr_to_gen_ppa(pblk, paddr, id);
408 }
409
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200410 emeta->mem += rq_len;
411 if (emeta->mem >= lm->emeta_len[0]) {
412 spin_lock(&l_mg->close_lock);
413 list_del(&meta_line->list);
414 WARN(!bitmap_full(meta_line->map_bitmap, lm->sec_per_line),
415 "pblk: corrupt meta line %d\n", meta_line->id);
416 spin_unlock(&l_mg->close_lock);
417 }
418
Javier González3eaa11e2017-07-07 21:08:52 +0200419 pblk_down_page(pblk, rqd->ppa_list, rqd->nr_ppas);
420
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200421 ret = pblk_submit_io(pblk, rqd);
422 if (ret) {
423 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
424 goto fail_rollback;
425 }
426
427 return NVM_IO_OK;
428
429fail_rollback:
Javier González3eaa11e2017-07-07 21:08:52 +0200430 pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200431 spin_lock(&l_mg->close_lock);
432 pblk_dealloc_page(pblk, meta_line, rq_ppas);
433 list_add(&meta_line->list, &meta_line->list);
434 spin_unlock(&l_mg->close_lock);
Javier González3eaa11e2017-07-07 21:08:52 +0200435
436 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200437fail_free_bio:
Javier Gonzálezf680f192017-06-26 11:57:21 +0200438 if (likely(l_mg->emeta_alloc_type == PBLK_VMALLOC_META))
439 bio_put(bio);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200440fail_free_rqd:
441 pblk_free_rqd(pblk, rqd, READ);
442 return ret;
443}
444
445static int pblk_sched_meta_io(struct pblk *pblk, struct ppa_addr *prev_list,
446 int prev_n)
447{
448 struct pblk_line_meta *lm = &pblk->lm;
449 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
450 struct pblk_line *meta_line;
451
452 spin_lock(&l_mg->close_lock);
453retry:
454 if (list_empty(&l_mg->emeta_list)) {
455 spin_unlock(&l_mg->close_lock);
456 return 0;
457 }
458 meta_line = list_first_entry(&l_mg->emeta_list, struct pblk_line, list);
459 if (bitmap_full(meta_line->map_bitmap, lm->sec_per_line))
460 goto retry;
461 spin_unlock(&l_mg->close_lock);
462
463 if (!pblk_valid_meta_ppa(pblk, meta_line, prev_list, prev_n))
464 return 0;
465
466 return pblk_submit_meta_io(pblk, meta_line);
467}
468
Javier Gonzálezd624f372017-06-26 11:57:15 +0200469static int pblk_submit_io_set(struct pblk *pblk, struct nvm_rq *rqd)
470{
471 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
472 struct ppa_addr erase_ppa;
473 int err;
474
475 ppa_set_empty(&erase_ppa);
476
477 /* Assign lbas to ppas and populate request structure */
478 err = pblk_setup_w_rq(pblk, rqd, c_ctx, &erase_ppa);
479 if (err) {
480 pr_err("pblk: could not setup write request: %d\n", err);
481 return NVM_IO_ERR;
482 }
483
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200484 if (likely(ppa_empty(erase_ppa))) {
485 /* Submit metadata write for previous data line */
486 err = pblk_sched_meta_io(pblk, rqd->ppa_list, rqd->nr_ppas);
487 if (err) {
488 pr_err("pblk: metadata I/O submission failed: %d", err);
489 return NVM_IO_ERR;
490 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200491
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200492 /* Submit data write for current data line */
493 err = pblk_submit_io(pblk, rqd);
494 if (err) {
495 pr_err("pblk: data I/O submission failed: %d\n", err);
496 return NVM_IO_ERR;
497 }
498 } else {
499 /* Submit data write for current data line */
500 err = pblk_submit_io(pblk, rqd);
501 if (err) {
502 pr_err("pblk: data I/O submission failed: %d\n", err);
503 return NVM_IO_ERR;
504 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200505
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200506 /* Submit available erase for next data line */
507 if (pblk_blk_erase_async(pblk, erase_ppa)) {
508 struct pblk_line *e_line = pblk_line_get_erase(pblk);
509 struct nvm_tgt_dev *dev = pblk->dev;
510 struct nvm_geo *geo = &dev->geo;
511 int bit;
512
513 atomic_inc(&e_line->left_eblks);
514 bit = pblk_ppa_to_pos(geo, erase_ppa);
515 WARN_ON(!test_and_clear_bit(bit, e_line->erase_bitmap));
516 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200517 }
518
519 return NVM_IO_OK;
520}
521
522static void pblk_free_write_rqd(struct pblk *pblk, struct nvm_rq *rqd)
523{
524 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
525 struct bio *bio = rqd->bio;
526
527 if (c_ctx->nr_padded)
Javier Gonzálezcd8ddbf2017-10-13 14:46:03 +0200528 pblk_bio_free_pages(pblk, bio, c_ctx->nr_valid,
529 c_ctx->nr_padded);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200530}
531
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200532static int pblk_submit_write(struct pblk *pblk)
533{
534 struct bio *bio;
535 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536 unsigned int secs_avail, secs_to_sync, secs_to_com;
537 unsigned int secs_to_flush;
538 unsigned long pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200539
540 /* If there are no sectors in the cache, flushes (bios without data)
541 * will be cleared on the cache threads
542 */
543 secs_avail = pblk_rb_read_count(&pblk->rwb);
544 if (!secs_avail)
545 return 1;
546
547 secs_to_flush = pblk_rb_sync_point_count(&pblk->rwb);
548 if (!secs_to_flush && secs_avail < pblk->min_write_pgs)
549 return 1;
550
551 rqd = pblk_alloc_rqd(pblk, WRITE);
552 if (IS_ERR(rqd)) {
553 pr_err("pblk: cannot allocate write req.\n");
554 return 1;
555 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556
557 bio = bio_alloc(GFP_KERNEL, pblk->max_write_pgs);
558 if (!bio) {
559 pr_err("pblk: cannot allocate write bio\n");
560 goto fail_free_rqd;
561 }
562 bio->bi_iter.bi_sector = 0; /* internal bio */
563 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
564 rqd->bio = bio;
565
566 secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail, secs_to_flush);
567 if (secs_to_sync > pblk->max_write_pgs) {
568 pr_err("pblk: bad buffer sync calculation\n");
569 goto fail_put_bio;
570 }
571
572 secs_to_com = (secs_to_sync > secs_avail) ? secs_avail : secs_to_sync;
573 pos = pblk_rb_read_commit(&pblk->rwb, secs_to_com);
574
Javier Gonzálezd624f372017-06-26 11:57:15 +0200575 if (pblk_rb_read_to_bio(&pblk->rwb, rqd, bio, pos, secs_to_sync,
576 secs_avail)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577 pr_err("pblk: corrupted write bio\n");
578 goto fail_put_bio;
579 }
580
Javier Gonzálezd624f372017-06-26 11:57:15 +0200581 if (pblk_submit_io_set(pblk, rqd))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582 goto fail_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200583
584#ifdef CONFIG_NVM_DEBUG
585 atomic_long_add(secs_to_sync, &pblk->sub_writes);
586#endif
587
588 return 0;
589
590fail_free_bio:
Javier Gonzálezd624f372017-06-26 11:57:15 +0200591 pblk_free_write_rqd(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592fail_put_bio:
593 bio_put(bio);
594fail_free_rqd:
595 pblk_free_rqd(pblk, rqd, WRITE);
596
597 return 1;
598}
599
600int pblk_write_ts(void *data)
601{
602 struct pblk *pblk = data;
603
604 while (!kthread_should_stop()) {
605 if (!pblk_submit_write(pblk))
606 continue;
607 set_current_state(TASK_INTERRUPTIBLE);
608 io_schedule();
609 }
610
611 return 0;
612}