blob: cc2b9414d17caa6cd05d7688cd7d6f2683940f0d [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
36#ifdef CONFIG_NVM_DEBUG
Javier González0880a9a2017-06-26 11:57:19 +020037 atomic_long_add(c_ctx->nr_valid, &pblk->sync_writes);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020038#endif
39
40 ret = pblk_rb_sync_advance(&pblk->rwb, c_ctx->nr_valid);
41
42 if (rqd->meta_list)
43 nvm_dev_dma_free(dev->parent, rqd->meta_list,
44 rqd->dma_meta_list);
45
46 bio_put(rqd->bio);
47 pblk_free_rqd(pblk, rqd, WRITE);
48
49 return ret;
50}
51
52static unsigned long pblk_end_queued_w_bio(struct pblk *pblk,
53 struct nvm_rq *rqd,
54 struct pblk_c_ctx *c_ctx)
55{
56 list_del(&c_ctx->list);
57 return pblk_end_w_bio(pblk, rqd, c_ctx);
58}
59
60static 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
67#ifdef CONFIG_NVM_DEBUG
68 atomic_long_sub(c_ctx->nr_valid, &pblk->inflight_writes);
69#endif
70
71 pblk_up_rq(pblk, rqd->ppa_list, rqd->nr_ppas, c_ctx->lun_bitmap);
72
73 pos = pblk_rb_sync_init(&pblk->rwb, &flags);
74 if (pos == c_ctx->sentry) {
75 pos = pblk_end_w_bio(pblk, rqd, c_ctx);
76
77retry:
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
92/* When a write fails, we are not sure whether the block has grown bad or a page
93 * range is more susceptible to write errors. If a high number of pages fail, we
94 * assume that the block is bad and we mark it accordingly. In all cases, we
95 * remap and resubmit the failed entries as fast as possible; if a flush is
96 * waiting on a completion, the whole stack would stall otherwise.
97 */
98static void pblk_end_w_fail(struct pblk *pblk, struct nvm_rq *rqd)
99{
100 void *comp_bits = &rqd->ppa_status;
101 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
102 struct pblk_rec_ctx *recovery;
103 struct ppa_addr *ppa_list = rqd->ppa_list;
104 int nr_ppas = rqd->nr_ppas;
105 unsigned int c_entries;
106 int bit, ret;
107
108 if (unlikely(nr_ppas == 1))
109 ppa_list = &rqd->ppa_addr;
110
111 recovery = mempool_alloc(pblk->rec_pool, GFP_ATOMIC);
112 if (!recovery) {
113 pr_err("pblk: could not allocate recovery context\n");
114 return;
115 }
116 INIT_LIST_HEAD(&recovery->failed);
117
118 bit = -1;
119 while ((bit = find_next_bit(comp_bits, nr_ppas, bit + 1)) < nr_ppas) {
120 struct pblk_rb_entry *entry;
121 struct ppa_addr ppa;
122
123 /* Logic error */
124 if (bit > c_ctx->nr_valid) {
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200125 WARN_ONCE(1, "pblk: corrupted write request\n");
Javier González33db9fd2017-04-22 01:32:47 +0200126 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200127 goto out;
128 }
129
130 ppa = ppa_list[bit];
131 entry = pblk_rb_sync_scan_entry(&pblk->rwb, &ppa);
132 if (!entry) {
133 pr_err("pblk: could not scan entry on write failure\n");
Javier González33db9fd2017-04-22 01:32:47 +0200134 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200135 goto out;
136 }
137
138 /* The list is filled first and emptied afterwards. No need for
139 * protecting it with a lock
140 */
141 list_add_tail(&entry->index, &recovery->failed);
142 }
143
144 c_entries = find_first_bit(comp_bits, nr_ppas);
145 ret = pblk_recov_setup_rq(pblk, c_ctx, recovery, comp_bits, c_entries);
146 if (ret) {
147 pr_err("pblk: could not recover from write failure\n");
Javier González33db9fd2017-04-22 01:32:47 +0200148 mempool_free(recovery, pblk->rec_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200149 goto out;
150 }
151
152 INIT_WORK(&recovery->ws_rec, pblk_submit_rec);
Javier Gonzálezef576492017-06-26 11:57:28 +0200153 queue_work(pblk->close_wq, &recovery->ws_rec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200154
155out:
156 pblk_complete_write(pblk, rqd, c_ctx);
157}
158
159static void pblk_end_io_write(struct nvm_rq *rqd)
160{
161 struct pblk *pblk = rqd->private;
162 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
163
164 if (rqd->error) {
165 pblk_log_write_err(pblk, rqd);
166 return pblk_end_w_fail(pblk, rqd);
167 }
168#ifdef CONFIG_NVM_DEBUG
169 else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200170 WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200171#endif
172
173 pblk_complete_write(pblk, rqd, c_ctx);
Javier González588726d32017-06-26 11:57:29 +0200174 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200175}
176
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200177static void pblk_end_io_write_meta(struct nvm_rq *rqd)
178{
179 struct pblk *pblk = rqd->private;
180 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200181 struct pblk_g_ctx *m_ctx = nvm_rq_to_pdu(rqd);
182 struct pblk_line *line = m_ctx->private;
183 struct pblk_emeta *emeta = line->emeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200184 int sync;
185
Javier González3eaa11e2017-07-07 21:08:52 +0200186 pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200187
188 if (rqd->error) {
189 pblk_log_write_err(pblk, rqd);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200190 pr_err("pblk: metadata I/O failed. Line %d\n", line->id);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200191 }
192#ifdef CONFIG_NVM_DEBUG
193 else
194 WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
195#endif
196
197 sync = atomic_add_return(rqd->nr_ppas, &emeta->sync);
198 if (sync == emeta->nr_entries)
Javier Gonzálezef576492017-06-26 11:57:28 +0200199 pblk_line_run_ws(pblk, line, NULL, pblk_line_close_ws,
200 pblk->close_wq);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200201
202 bio_put(rqd->bio);
Javier González3eaa11e2017-07-07 21:08:52 +0200203 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200204 pblk_free_rqd(pblk, rqd, READ);
Javier González588726d32017-06-26 11:57:29 +0200205
206 atomic_dec(&pblk->inflight_io);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200207}
208
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209static int pblk_alloc_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200210 unsigned int nr_secs,
211 nvm_end_io_fn(*end_io))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212{
213 struct nvm_tgt_dev *dev = pblk->dev;
214
215 /* Setup write request */
216 rqd->opcode = NVM_OP_PWRITE;
217 rqd->nr_ppas = nr_secs;
218 rqd->flags = pblk_set_progr_mode(pblk, WRITE);
219 rqd->private = pblk;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200220 rqd->end_io = end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221
222 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
223 &rqd->dma_meta_list);
224 if (!rqd->meta_list)
225 return -ENOMEM;
226
227 if (unlikely(nr_secs == 1))
228 return 0;
229
230 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
231 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
232
233 return 0;
234}
235
236static int pblk_setup_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200237 struct pblk_c_ctx *c_ctx, struct ppa_addr *erase_ppa)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238{
239 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezd624f372017-06-26 11:57:15 +0200240 struct pblk_line *e_line = pblk_line_get_erase(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200241 unsigned int valid = c_ctx->nr_valid;
242 unsigned int padded = c_ctx->nr_padded;
243 unsigned int nr_secs = valid + padded;
244 unsigned long *lun_bitmap;
245 int ret = 0;
246
247 lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200248 if (!lun_bitmap)
249 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250 c_ctx->lun_bitmap = lun_bitmap;
251
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200252 ret = pblk_alloc_w_rq(pblk, rqd, nr_secs, pblk_end_io_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253 if (ret) {
254 kfree(lun_bitmap);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200255 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256 }
257
Javier González588726d32017-06-26 11:57:29 +0200258 if (likely(!e_line || !atomic_read(&e_line->left_eblks)))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259 pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, valid, 0);
260 else
261 pblk_map_erase_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200262 valid, erase_ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263
Javier Gonzálezd624f372017-06-26 11:57:15 +0200264 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265}
266
267int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
268 struct pblk_c_ctx *c_ctx)
269{
270 struct pblk_line_meta *lm = &pblk->lm;
271 unsigned long *lun_bitmap;
272 int ret;
273
274 lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
275 if (!lun_bitmap)
276 return -ENOMEM;
277
278 c_ctx->lun_bitmap = lun_bitmap;
279
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200280 ret = pblk_alloc_w_rq(pblk, rqd, rqd->nr_ppas, pblk_end_io_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200281 if (ret)
282 return ret;
283
284 pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap, c_ctx->nr_valid, 0);
285
286 rqd->ppa_status = (u64)0;
287 rqd->flags = pblk_set_progr_mode(pblk, WRITE);
288
289 return ret;
290}
291
292static int pblk_calc_secs_to_sync(struct pblk *pblk, unsigned int secs_avail,
293 unsigned int secs_to_flush)
294{
295 int secs_to_sync;
296
297 secs_to_sync = pblk_calc_secs(pblk, secs_avail, secs_to_flush);
298
299#ifdef CONFIG_NVM_DEBUG
300 if ((!secs_to_sync && secs_to_flush)
301 || (secs_to_sync < 0)
302 || (secs_to_sync > secs_avail && !secs_to_flush)) {
303 pr_err("pblk: bad sector calculation (a:%d,s:%d,f:%d)\n",
304 secs_avail, secs_to_sync, secs_to_flush);
305 }
306#endif
307
308 return secs_to_sync;
309}
310
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200311static inline int pblk_valid_meta_ppa(struct pblk *pblk,
312 struct pblk_line *meta_line,
313 struct ppa_addr *ppa_list, int nr_ppas)
314{
315 struct nvm_tgt_dev *dev = pblk->dev;
316 struct nvm_geo *geo = &dev->geo;
317 struct pblk_line *data_line;
318 struct ppa_addr ppa, ppa_opt;
319 u64 paddr;
320 int i;
321
322 data_line = &pblk->lines[pblk_dev_ppa_to_line(ppa_list[0])];
323 paddr = pblk_lookup_page(pblk, meta_line);
324 ppa = addr_to_gen_ppa(pblk, paddr, 0);
325
326 if (test_bit(pblk_ppa_to_pos(geo, ppa), data_line->blk_bitmap))
327 return 1;
328
329 /* Schedule a metadata I/O that is half the distance from the data I/O
330 * with regards to the number of LUNs forming the pblk instance. This
331 * balances LUN conflicts across every I/O.
332 *
333 * When the LUN configuration changes (e.g., due to GC), this distance
334 * can align, which would result on a LUN deadlock. In this case, modify
335 * the distance to not be optimal, but allow metadata I/Os to succeed.
336 */
337 ppa_opt = addr_to_gen_ppa(pblk, paddr + data_line->meta_distance, 0);
338 if (unlikely(ppa_opt.ppa == ppa.ppa)) {
339 data_line->meta_distance--;
340 return 0;
341 }
342
343 for (i = 0; i < nr_ppas; i += pblk->min_write_pgs)
344 if (ppa_list[i].g.ch == ppa_opt.g.ch &&
345 ppa_list[i].g.lun == ppa_opt.g.lun)
346 return 1;
347
348 if (test_bit(pblk_ppa_to_pos(geo, ppa_opt), data_line->blk_bitmap)) {
349 for (i = 0; i < nr_ppas; i += pblk->min_write_pgs)
350 if (ppa_list[i].g.ch == ppa.g.ch &&
351 ppa_list[i].g.lun == ppa.g.lun)
352 return 0;
353
354 return 1;
355 }
356
357 return 0;
358}
359
360int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line)
361{
362 struct nvm_tgt_dev *dev = pblk->dev;
363 struct nvm_geo *geo = &dev->geo;
364 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
365 struct pblk_line_meta *lm = &pblk->lm;
366 struct pblk_emeta *emeta = meta_line->emeta;
367 struct pblk_g_ctx *m_ctx;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200368 struct bio *bio;
369 struct nvm_rq *rqd;
370 void *data;
371 u64 paddr;
372 int rq_ppas = pblk->min_write_pgs;
373 int id = meta_line->id;
374 int rq_len;
375 int i, j;
376 int ret;
377
378 rqd = pblk_alloc_rqd(pblk, READ);
379 if (IS_ERR(rqd)) {
380 pr_err("pblk: cannot allocate write req.\n");
381 return PTR_ERR(rqd);
382 }
383 m_ctx = nvm_rq_to_pdu(rqd);
384 m_ctx->private = meta_line;
385
386 rq_len = rq_ppas * geo->sec_size;
387 data = ((void *)emeta->buf) + emeta->mem;
388
Javier Gonzálezde54e702017-06-30 17:56:39 +0200389 bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
390 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200391 if (IS_ERR(bio)) {
392 ret = PTR_ERR(bio);
393 goto fail_free_rqd;
394 }
395 bio->bi_iter.bi_sector = 0; /* internal bio */
396 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
397 rqd->bio = bio;
398
399 ret = pblk_alloc_w_rq(pblk, rqd, rq_ppas, pblk_end_io_write_meta);
400 if (ret)
401 goto fail_free_bio;
402
403 for (i = 0; i < rqd->nr_ppas; ) {
404 spin_lock(&meta_line->lock);
405 paddr = __pblk_alloc_page(pblk, meta_line, rq_ppas);
406 spin_unlock(&meta_line->lock);
407 for (j = 0; j < rq_ppas; j++, i++, paddr++)
408 rqd->ppa_list[i] = addr_to_gen_ppa(pblk, paddr, id);
409 }
410
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200411 emeta->mem += rq_len;
412 if (emeta->mem >= lm->emeta_len[0]) {
413 spin_lock(&l_mg->close_lock);
414 list_del(&meta_line->list);
415 WARN(!bitmap_full(meta_line->map_bitmap, lm->sec_per_line),
416 "pblk: corrupt meta line %d\n", meta_line->id);
417 spin_unlock(&l_mg->close_lock);
418 }
419
Javier González3eaa11e2017-07-07 21:08:52 +0200420 pblk_down_page(pblk, rqd->ppa_list, rqd->nr_ppas);
421
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200422 ret = pblk_submit_io(pblk, rqd);
423 if (ret) {
424 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
425 goto fail_rollback;
426 }
427
428 return NVM_IO_OK;
429
430fail_rollback:
Javier González3eaa11e2017-07-07 21:08:52 +0200431 pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200432 spin_lock(&l_mg->close_lock);
433 pblk_dealloc_page(pblk, meta_line, rq_ppas);
434 list_add(&meta_line->list, &meta_line->list);
435 spin_unlock(&l_mg->close_lock);
Javier González3eaa11e2017-07-07 21:08:52 +0200436
437 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200438fail_free_bio:
Javier Gonzálezf680f192017-06-26 11:57:21 +0200439 if (likely(l_mg->emeta_alloc_type == PBLK_VMALLOC_META))
440 bio_put(bio);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200441fail_free_rqd:
442 pblk_free_rqd(pblk, rqd, READ);
443 return ret;
444}
445
446static int pblk_sched_meta_io(struct pblk *pblk, struct ppa_addr *prev_list,
447 int prev_n)
448{
449 struct pblk_line_meta *lm = &pblk->lm;
450 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
451 struct pblk_line *meta_line;
452
453 spin_lock(&l_mg->close_lock);
454retry:
455 if (list_empty(&l_mg->emeta_list)) {
456 spin_unlock(&l_mg->close_lock);
457 return 0;
458 }
459 meta_line = list_first_entry(&l_mg->emeta_list, struct pblk_line, list);
460 if (bitmap_full(meta_line->map_bitmap, lm->sec_per_line))
461 goto retry;
462 spin_unlock(&l_mg->close_lock);
463
464 if (!pblk_valid_meta_ppa(pblk, meta_line, prev_list, prev_n))
465 return 0;
466
467 return pblk_submit_meta_io(pblk, meta_line);
468}
469
Javier Gonzálezd624f372017-06-26 11:57:15 +0200470static int pblk_submit_io_set(struct pblk *pblk, struct nvm_rq *rqd)
471{
472 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
473 struct ppa_addr erase_ppa;
474 int err;
475
476 ppa_set_empty(&erase_ppa);
477
478 /* Assign lbas to ppas and populate request structure */
479 err = pblk_setup_w_rq(pblk, rqd, c_ctx, &erase_ppa);
480 if (err) {
481 pr_err("pblk: could not setup write request: %d\n", err);
482 return NVM_IO_ERR;
483 }
484
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200485 if (likely(ppa_empty(erase_ppa))) {
486 /* Submit metadata write for previous data line */
487 err = pblk_sched_meta_io(pblk, rqd->ppa_list, rqd->nr_ppas);
488 if (err) {
489 pr_err("pblk: metadata I/O submission failed: %d", err);
490 return NVM_IO_ERR;
491 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200492
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200493 /* Submit data write for current data line */
494 err = pblk_submit_io(pblk, rqd);
495 if (err) {
496 pr_err("pblk: data I/O submission failed: %d\n", err);
497 return NVM_IO_ERR;
498 }
499 } else {
500 /* Submit data write for current data line */
501 err = pblk_submit_io(pblk, rqd);
502 if (err) {
503 pr_err("pblk: data I/O submission failed: %d\n", err);
504 return NVM_IO_ERR;
505 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200506
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200507 /* Submit available erase for next data line */
508 if (pblk_blk_erase_async(pblk, erase_ppa)) {
509 struct pblk_line *e_line = pblk_line_get_erase(pblk);
510 struct nvm_tgt_dev *dev = pblk->dev;
511 struct nvm_geo *geo = &dev->geo;
512 int bit;
513
514 atomic_inc(&e_line->left_eblks);
515 bit = pblk_ppa_to_pos(geo, erase_ppa);
516 WARN_ON(!test_and_clear_bit(bit, e_line->erase_bitmap));
517 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200518 }
519
520 return NVM_IO_OK;
521}
522
523static void pblk_free_write_rqd(struct pblk *pblk, struct nvm_rq *rqd)
524{
525 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
526 struct bio *bio = rqd->bio;
527
528 if (c_ctx->nr_padded)
529 pblk_bio_free_pages(pblk, bio, rqd->nr_ppas, c_ctx->nr_padded);
530}
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}