blob: f185f1a00008352d25bc4de0cc115d5e427a131e [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * pblk-cache.c - pblk's write cache
17 */
18
19#include "pblk.h"
20
Igor Konopko3e03f632019-05-04 20:38:06 +020021void pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
22 unsigned long flags)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023{
24 struct pblk_w_ctx w_ctx;
25 sector_t lba = pblk_get_lba(bio);
Christoph Hellwiga8e45652020-05-27 07:24:07 +020026 unsigned long start_time;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027 unsigned int bpos, pos;
28 int nr_entries = pblk_get_secs(bio);
29 int i, ret;
30
Christoph Hellwiga8e45652020-05-27 07:24:07 +020031 start_time = bio_start_io_acct(bio);
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);
Igor Konopko3e03f632019-05-04 20:38:06 +020045 bio_io_error(bio);
Javier González588726d32017-06-26 11:57:29 +020046 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020047 }
48
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049 pblk_ppa_set_empty(&w_ctx.ppa);
Javier González6ca2f712017-10-13 14:46:17 +020050 w_ctx.flags = flags;
Hans Holmbergcc9c9a02018-06-01 16:41:13 +020051 if (bio->bi_opf & REQ_PREFLUSH) {
Javier González6ca2f712017-10-13 14:46:17 +020052 w_ctx.flags |= PBLK_FLUSH_ENTRY;
Hans Holmbergcc9c9a02018-06-01 16:41:13 +020053 pblk_write_kick(pblk);
54 }
55
56 if (unlikely(!bio_has_data(bio)))
57 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058
59 for (i = 0; i < nr_entries; i++) {
60 void *data = bio_data(bio);
61
62 w_ctx.lba = lba + i;
63
64 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
65 pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
66
67 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
68 }
69
Hans Holmberg76758392018-03-30 00:04:52 +020070 atomic64_add(nr_entries, &pblk->user_wa);
71
Matias Bjørling880eda52018-07-13 10:48:37 +020072#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073 atomic_long_add(nr_entries, &pblk->inflight_writes);
74 atomic_long_add(nr_entries, &pblk->req_writes);
75#endif
76
Javier González588726d32017-06-26 11:57:29 +020077 pblk_rl_inserted(&pblk->rl, nr_entries);
78
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020079out:
Christoph Hellwiga8e45652020-05-27 07:24:07 +020080 bio_end_io_acct(bio, start_time);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020081 pblk_write_should_kick(pblk);
Igor Konopko3e03f632019-05-04 20:38:06 +020082
83 if (ret == NVM_IO_DONE)
84 bio_endio(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020085}
86
87/*
88 * On GC the incoming lbas are not necessarily sequential. Also, some of the
89 * lbas might not be valid entries, which are marked as empty by the GC thread
90 */
Javier Gonzálezd3401212017-10-13 14:46:14 +020091int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020092{
93 struct pblk_w_ctx w_ctx;
94 unsigned int bpos, pos;
Javier Gonzálezd3401212017-10-13 14:46:14 +020095 void *data = gc_rq->data;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020096 int i, valid_entries;
97
98 /* Update the write buffer head (mem) with the entries that we can
99 * write. The write in itself cannot fail, so there is no need to
100 * rollback from here on.
101 */
102retry:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200103 if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104 io_schedule();
105 goto retry;
106 }
107
Javier Gonzálezd3401212017-10-13 14:46:14 +0200108 w_ctx.flags = PBLK_IOTYPE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200109 pblk_ppa_set_empty(&w_ctx.ppa);
110
Javier Gonzálezd3401212017-10-13 14:46:14 +0200111 for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
112 if (gc_rq->lba_list[i] == ADDR_EMPTY)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200113 continue;
114
Javier Gonzálezd3401212017-10-13 14:46:14 +0200115 w_ctx.lba = gc_rq->lba_list[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200116
117 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200118 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
119 gc_rq->paddr_list[i], pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200120
121 data += PBLK_EXPOSED_PAGE_SIZE;
122 valid_entries++;
123 }
124
Javier Gonzálezd3401212017-10-13 14:46:14 +0200125 WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200126 "pblk: inconsistent GC write\n");
127
Hans Holmberg76758392018-03-30 00:04:52 +0200128 atomic64_add(valid_entries, &pblk->gc_wa);
129
Matias Bjørling880eda52018-07-13 10:48:37 +0200130#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200131 atomic_long_add(valid_entries, &pblk->inflight_writes);
132 atomic_long_add(valid_entries, &pblk->recov_gc_writes);
133#endif
134
135 pblk_write_should_kick(pblk);
136 return NVM_IO_OK;
137}