blob: f565a56b898ab0760d36cc7999a1ef75039d70de [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-cache.c - pblk's write cache
16 */
17
18#include "pblk.h"
19
20int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, unsigned long flags)
21{
Javier González998ba622018-01-05 14:16:20 +010022 struct request_queue *q = pblk->dev->q;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023 struct pblk_w_ctx w_ctx;
24 sector_t lba = pblk_get_lba(bio);
Javier González998ba622018-01-05 14:16:20 +010025 unsigned long start_time = jiffies;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020026 unsigned int bpos, pos;
27 int nr_entries = pblk_get_secs(bio);
28 int i, ret;
29
Michael Callahanddcf35d2018-07-18 04:47:39 -070030 generic_start_io_acct(q, REQ_OP_WRITE, bio_sectors(bio),
31 &pblk->disk->part0);
Javier González998ba622018-01-05 14:16:20 +010032
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020033 /* Update the write buffer head (mem) with the entries that we can
34 * write. The write in itself cannot fail, so there is no need to
35 * rollback from here on.
36 */
37retry:
38 ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
Javier González588726d32017-06-26 11:57:29 +020039 switch (ret) {
40 case NVM_IO_REQUEUE:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020041 io_schedule();
42 goto retry;
Javier González588726d32017-06-26 11:57:29 +020043 case NVM_IO_ERR:
44 pblk_pipeline_stop(pblk);
45 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046 }
47
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020048 pblk_ppa_set_empty(&w_ctx.ppa);
Javier González6ca2f712017-10-13 14:46:17 +020049 w_ctx.flags = flags;
Hans Holmbergcc9c9a02018-06-01 16:41:13 +020050 if (bio->bi_opf & REQ_PREFLUSH) {
Javier González6ca2f712017-10-13 14:46:17 +020051 w_ctx.flags |= PBLK_FLUSH_ENTRY;
Hans Holmbergcc9c9a02018-06-01 16:41:13 +020052 pblk_write_kick(pblk);
53 }
54
55 if (unlikely(!bio_has_data(bio)))
56 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057
58 for (i = 0; i < nr_entries; i++) {
59 void *data = bio_data(bio);
60
61 w_ctx.lba = lba + i;
62
63 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
64 pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
65
66 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
67 }
68
Hans Holmberg76758392018-03-30 00:04:52 +020069 atomic64_add(nr_entries, &pblk->user_wa);
70
Matias Bjørling880eda52018-07-13 10:48:37 +020071#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020072 atomic_long_add(nr_entries, &pblk->inflight_writes);
73 atomic_long_add(nr_entries, &pblk->req_writes);
74#endif
75
Javier González588726d32017-06-26 11:57:29 +020076 pblk_rl_inserted(&pblk->rl, nr_entries);
77
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020078out:
Michael Callahanddcf35d2018-07-18 04:47:39 -070079 generic_end_io_acct(q, REQ_OP_WRITE, &pblk->disk->part0, start_time);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020080 pblk_write_should_kick(pblk);
81 return ret;
82}
83
84/*
85 * On GC the incoming lbas are not necessarily sequential. Also, some of the
86 * lbas might not be valid entries, which are marked as empty by the GC thread
87 */
Javier Gonzálezd3401212017-10-13 14:46:14 +020088int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020089{
90 struct pblk_w_ctx w_ctx;
91 unsigned int bpos, pos;
Javier Gonzálezd3401212017-10-13 14:46:14 +020092 void *data = gc_rq->data;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093 int i, valid_entries;
94
95 /* Update the write buffer head (mem) with the entries that we can
96 * write. The write in itself cannot fail, so there is no need to
97 * rollback from here on.
98 */
99retry:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200100 if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200101 io_schedule();
102 goto retry;
103 }
104
Javier Gonzálezd3401212017-10-13 14:46:14 +0200105 w_ctx.flags = PBLK_IOTYPE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200106 pblk_ppa_set_empty(&w_ctx.ppa);
107
Javier Gonzálezd3401212017-10-13 14:46:14 +0200108 for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
109 if (gc_rq->lba_list[i] == ADDR_EMPTY)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200110 continue;
111
Javier Gonzálezd3401212017-10-13 14:46:14 +0200112 w_ctx.lba = gc_rq->lba_list[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200113
114 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200115 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
116 gc_rq->paddr_list[i], pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200117
118 data += PBLK_EXPOSED_PAGE_SIZE;
119 valid_entries++;
120 }
121
Javier Gonzálezd3401212017-10-13 14:46:14 +0200122 WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200123 "pblk: inconsistent GC write\n");
124
Hans Holmberg76758392018-03-30 00:04:52 +0200125 atomic64_add(valid_entries, &pblk->gc_wa);
126
Matias Bjørling880eda52018-07-13 10:48:37 +0200127#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200128 atomic_long_add(valid_entries, &pblk->inflight_writes);
129 atomic_long_add(valid_entries, &pblk->recov_gc_writes);
130#endif
131
132 pblk_write_should_kick(pblk);
133 return NVM_IO_OK;
134}