blob: b9a2aeba95abd39e37709921a5376be4118aefef [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-write.c - pblk's write path from write buffer to media
17 */
18
19#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020020#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020021
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020022static unsigned long pblk_end_w_bio(struct pblk *pblk, struct nvm_rq *rqd,
23 struct pblk_c_ctx *c_ctx)
24{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025 struct bio *original_bio;
Hans Holmberg533657c12018-01-05 14:16:10 +010026 struct pblk_rb *rwb = &pblk->rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027 unsigned long ret;
28 int i;
29
30 for (i = 0; i < c_ctx->nr_valid; i++) {
31 struct pblk_w_ctx *w_ctx;
Hans Holmberg533657c12018-01-05 14:16:10 +010032 int pos = c_ctx->sentry + i;
33 int flags;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020034
Hans Holmberg533657c12018-01-05 14:16:10 +010035 w_ctx = pblk_rb_w_ctx(rwb, pos);
36 flags = READ_ONCE(w_ctx->flags);
37
38 if (flags & PBLK_FLUSH_ENTRY) {
39 flags &= ~PBLK_FLUSH_ENTRY;
40 /* Release flags on context. Protect from writes */
41 smp_store_release(&w_ctx->flags, flags);
42
Matias Bjørling880eda52018-07-13 10:48:37 +020043#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg533657c12018-01-05 14:16:10 +010044 atomic_dec(&rwb->inflight_flush_point);
45#endif
46 }
47
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020048 while ((original_bio = bio_list_pop(&w_ctx->bios)))
49 bio_endio(original_bio);
50 }
51
Javier Gonzálezcd8ddbf2017-10-13 14:46:03 +020052 if (c_ctx->nr_padded)
53 pblk_bio_free_pages(pblk, rqd->bio, c_ctx->nr_valid,
54 c_ctx->nr_padded);
55
Matias Bjørling880eda52018-07-13 10:48:37 +020056#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleze0e12a72017-10-13 14:46:04 +020057 atomic_long_add(rqd->nr_ppas, &pblk->sync_writes);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058#endif
59
60 ret = pblk_rb_sync_advance(&pblk->rwb, c_ctx->nr_valid);
61
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020062 bio_put(rqd->bio);
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020063 pblk_free_rqd(pblk, rqd, PBLK_WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020064
65 return ret;
66}
67
68static unsigned long pblk_end_queued_w_bio(struct pblk *pblk,
69 struct nvm_rq *rqd,
70 struct pblk_c_ctx *c_ctx)
71{
72 list_del(&c_ctx->list);
73 return pblk_end_w_bio(pblk, rqd, c_ctx);
74}
75
76static void pblk_complete_write(struct pblk *pblk, struct nvm_rq *rqd,
77 struct pblk_c_ctx *c_ctx)
78{
79 struct pblk_c_ctx *c, *r;
80 unsigned long flags;
81 unsigned long pos;
82
Matias Bjørling880eda52018-07-13 10:48:37 +020083#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020084 atomic_long_sub(c_ctx->nr_valid, &pblk->inflight_writes);
85#endif
Hans Holmberge99e8022018-10-09 13:11:48 +020086 pblk_up_rq(pblk, c_ctx->lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020087
88 pos = pblk_rb_sync_init(&pblk->rwb, &flags);
89 if (pos == c_ctx->sentry) {
90 pos = pblk_end_w_bio(pblk, rqd, c_ctx);
91
92retry:
93 list_for_each_entry_safe(c, r, &pblk->compl_list, list) {
94 rqd = nvm_rq_from_c_ctx(c);
95 if (c->sentry == pos) {
96 pos = pblk_end_queued_w_bio(pblk, rqd, c);
97 goto retry;
98 }
99 }
100 } else {
101 WARN_ON(nvm_rq_from_c_ctx(c_ctx) != rqd);
102 list_add_tail(&c_ctx->list, &pblk->compl_list);
103 }
104 pblk_rb_sync_end(&pblk->rwb, &flags);
105}
106
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200107/* Map remaining sectors in chunk, starting from ppa */
Hans Holmbergab3887b2018-12-11 20:16:11 +0100108static void pblk_map_remaining(struct pblk *pblk, struct ppa_addr *ppa,
109 int rqd_ppas)
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200110{
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200111 struct pblk_line *line;
112 struct ppa_addr map_ppa = *ppa;
Hans Holmbergab3887b2018-12-11 20:16:11 +0100113 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
114 __le64 *lba_list;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200115 u64 paddr;
116 int done = 0;
Hans Holmbergab3887b2018-12-11 20:16:11 +0100117 int n = 0;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200118
Javier Gonzálezcb216652018-10-09 13:11:42 +0200119 line = pblk_ppa_to_line(pblk, *ppa);
Hans Holmbergab3887b2018-12-11 20:16:11 +0100120 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
121
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200122 spin_lock(&line->lock);
123
124 while (!done) {
125 paddr = pblk_dev_ppa_to_line_addr(pblk, map_ppa);
126
127 if (!test_and_set_bit(paddr, line->map_bitmap))
128 line->left_msecs--;
129
Hans Holmbergab3887b2018-12-11 20:16:11 +0100130 if (n < rqd_ppas && lba_list[paddr] != addr_empty)
131 line->nr_valid_lbas--;
132
133 lba_list[paddr] = addr_empty;
134
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200135 if (!test_and_set_bit(paddr, line->invalid_bitmap))
136 le32_add_cpu(line->vsc, -1);
137
Hans Holmbergbf82fa22018-10-09 13:11:59 +0200138 done = nvm_next_ppa_in_chk(pblk->dev, &map_ppa);
Hans Holmbergab3887b2018-12-11 20:16:11 +0100139
140 n++;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200141 }
142
Hans Holmberg48b8d202018-06-01 16:41:06 +0200143 line->w_err_gc->has_write_err = 1;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200144 spin_unlock(&line->lock);
145}
146
147static void pblk_prepare_resubmit(struct pblk *pblk, unsigned int sentry,
148 unsigned int nr_entries)
149{
150 struct pblk_rb *rb = &pblk->rwb;
151 struct pblk_rb_entry *entry;
152 struct pblk_line *line;
153 struct pblk_w_ctx *w_ctx;
154 struct ppa_addr ppa_l2p;
155 int flags;
Javier González40b86572018-10-09 13:12:09 +0200156 unsigned int i;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200157
158 spin_lock(&pblk->trans_lock);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200159 for (i = 0; i < nr_entries; i++) {
Javier González40b86572018-10-09 13:12:09 +0200160 entry = &rb->entries[pblk_rb_ptr_wrap(rb, sentry, i)];
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200161 w_ctx = &entry->w_ctx;
162
163 /* Check if the lba has been overwritten */
Hans Holmbergc12fa402018-12-11 20:16:10 +0100164 if (w_ctx->lba != ADDR_EMPTY) {
165 ppa_l2p = pblk_trans_map_get(pblk, w_ctx->lba);
166 if (!pblk_ppa_comp(ppa_l2p, entry->cacheline))
167 w_ctx->lba = ADDR_EMPTY;
168 }
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200169
170 /* Mark up the entry as submittable again */
171 flags = READ_ONCE(w_ctx->flags);
172 flags |= PBLK_WRITTEN_DATA;
173 /* Release flags on write context. Protect from writes */
174 smp_store_release(&w_ctx->flags, flags);
175
Javier González2e696f92018-10-09 13:11:43 +0200176 /* Decrease the reference count to the line as we will
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200177 * re-map these entries
178 */
Javier Gonzálezcb216652018-10-09 13:11:42 +0200179 line = pblk_ppa_to_line(pblk, w_ctx->ppa);
Heiner Litz05869422019-02-11 13:25:09 +0100180 atomic_dec(&line->sec_to_update);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200181 kref_put(&line->ref, pblk_line_put);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200182 }
183 spin_unlock(&pblk->trans_lock);
184}
185
186static void pblk_queue_resubmit(struct pblk *pblk, struct pblk_c_ctx *c_ctx)
187{
188 struct pblk_c_ctx *r_ctx;
189
190 r_ctx = kzalloc(sizeof(struct pblk_c_ctx), GFP_KERNEL);
191 if (!r_ctx)
192 return;
193
194 r_ctx->lun_bitmap = NULL;
195 r_ctx->sentry = c_ctx->sentry;
196 r_ctx->nr_valid = c_ctx->nr_valid;
197 r_ctx->nr_padded = c_ctx->nr_padded;
198
199 spin_lock(&pblk->resubmit_lock);
200 list_add_tail(&r_ctx->list, &pblk->resubmit_list);
201 spin_unlock(&pblk->resubmit_lock);
202
Matias Bjørling880eda52018-07-13 10:48:37 +0200203#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200204 atomic_long_add(c_ctx->nr_valid, &pblk->recov_writes);
205#endif
206}
207
208static void pblk_submit_rec(struct work_struct *work)
209{
210 struct pblk_rec_ctx *recovery =
211 container_of(work, struct pblk_rec_ctx, ws_rec);
212 struct pblk *pblk = recovery->pblk;
213 struct nvm_rq *rqd = recovery->rqd;
214 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
Hans Holmbergd68a9342018-10-09 13:11:46 +0200215 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200216
217 pblk_log_write_err(pblk, rqd);
218
Hans Holmbergab3887b2018-12-11 20:16:11 +0100219 pblk_map_remaining(pblk, ppa_list, rqd->nr_ppas);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200220 pblk_queue_resubmit(pblk, c_ctx);
221
Hans Holmberge99e8022018-10-09 13:11:48 +0200222 pblk_up_rq(pblk, c_ctx->lun_bitmap);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200223 if (c_ctx->nr_padded)
224 pblk_bio_free_pages(pblk, rqd->bio, c_ctx->nr_valid,
225 c_ctx->nr_padded);
226 bio_put(rqd->bio);
227 pblk_free_rqd(pblk, rqd, PBLK_WRITE);
228 mempool_free(recovery, &pblk->rec_pool);
229
230 atomic_dec(&pblk->inflight_io);
Igor Konopko74a37fb2019-05-04 20:38:00 +0200231 pblk_write_kick(pblk);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200232}
233
234
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235static void pblk_end_w_fail(struct pblk *pblk, struct nvm_rq *rqd)
236{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200237 struct pblk_rec_ctx *recovery;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400239 recovery = mempool_alloc(&pblk->rec_pool, GFP_ATOMIC);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200240 if (!recovery) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200241 pblk_err(pblk, "could not allocate recovery work\n");
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200242 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200243 }
244
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200245 recovery->pblk = pblk;
246 recovery->rqd = rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200247
248 INIT_WORK(&recovery->ws_rec, pblk_submit_rec);
Javier Gonzálezef576492017-06-26 11:57:28 +0200249 queue_work(pblk->close_wq, &recovery->ws_rec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250}
251
252static void pblk_end_io_write(struct nvm_rq *rqd)
253{
254 struct pblk *pblk = rqd->private;
255 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
256
257 if (rqd->error) {
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200258 pblk_end_w_fail(pblk, rqd);
259 return;
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200260 } else {
261 if (trace_pblk_chunk_state_enabled())
262 pblk_check_chunk_state_update(pblk, rqd);
Matias Bjørling880eda52018-07-13 10:48:37 +0200263#ifdef CONFIG_NVM_PBLK_DEBUG
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200264 WARN_ONCE(rqd->bio->bi_status, "pblk: corrupted write error\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265#endif
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200266 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200267
268 pblk_complete_write(pblk, rqd, c_ctx);
Javier González588726d32017-06-26 11:57:29 +0200269 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200270}
271
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200272static void pblk_end_io_write_meta(struct nvm_rq *rqd)
273{
274 struct pblk *pblk = rqd->private;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200275 struct pblk_g_ctx *m_ctx = nvm_rq_to_pdu(rqd);
276 struct pblk_line *line = m_ctx->private;
277 struct pblk_emeta *emeta = line->emeta;
Hans Holmbergd68a9342018-10-09 13:11:46 +0200278 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200279 int sync;
280
Matias Bjørling43241cf2018-10-09 13:11:51 +0200281 pblk_up_chunk(pblk, ppa_list[0]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200282
283 if (rqd->error) {
284 pblk_log_write_err(pblk, rqd);
Matias Bjørling4e495a42018-07-13 10:48:42 +0200285 pblk_err(pblk, "metadata I/O failed. Line %d\n", line->id);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200286 line->w_err_gc->has_write_err = 1;
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200287 } else {
288 if (trace_pblk_chunk_state_enabled())
289 pblk_check_chunk_state_update(pblk, rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200290 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200291
292 sync = atomic_add_return(rqd->nr_ppas, &emeta->sync);
293 if (sync == emeta->nr_entries)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200294 pblk_gen_run_ws(pblk, line, NULL, pblk_line_close_ws,
295 GFP_ATOMIC, pblk->close_wq);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200296
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200297 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier González588726d32017-06-26 11:57:29 +0200298
299 atomic_dec(&pblk->inflight_io);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200300}
301
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302static int pblk_alloc_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier González45dcf292018-10-09 13:12:02 +0200303 unsigned int nr_secs, nvm_end_io_fn(*end_io))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305 /* Setup write request */
306 rqd->opcode = NVM_OP_PWRITE;
307 rqd->nr_ppas = nr_secs;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200308 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200309 rqd->private = pblk;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200310 rqd->end_io = end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200311
Javier González45dcf292018-10-09 13:12:02 +0200312 return pblk_alloc_rqd_meta(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313}
314
315static int pblk_setup_w_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier González1e821232017-10-13 14:46:25 +0200316 struct ppa_addr *erase_ppa)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200317{
318 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezd624f372017-06-26 11:57:15 +0200319 struct pblk_line *e_line = pblk_line_get_erase(pblk);
Javier González1e821232017-10-13 14:46:25 +0200320 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200321 unsigned int valid = c_ctx->nr_valid;
322 unsigned int padded = c_ctx->nr_padded;
323 unsigned int nr_secs = valid + padded;
324 unsigned long *lun_bitmap;
Javier González1e821232017-10-13 14:46:25 +0200325 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200326
327 lun_bitmap = kzalloc(lm->lun_bitmap_len, GFP_KERNEL);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200328 if (!lun_bitmap)
329 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 c_ctx->lun_bitmap = lun_bitmap;
331
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200332 ret = pblk_alloc_w_rq(pblk, rqd, nr_secs, pblk_end_io_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200333 if (ret) {
334 kfree(lun_bitmap);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200335 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200336 }
337
Javier González588726d32017-06-26 11:57:29 +0200338 if (likely(!e_line || !atomic_read(&e_line->left_eblks)))
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100339 ret = pblk_map_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
340 valid, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200341 else
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100342 ret = pblk_map_erase_rq(pblk, rqd, c_ctx->sentry, lun_bitmap,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200343 valid, erase_ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200344
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100345 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200346}
347
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200348static int pblk_calc_secs_to_sync(struct pblk *pblk, unsigned int secs_avail,
349 unsigned int secs_to_flush)
350{
351 int secs_to_sync;
352
Igor Konopko55d8ec32018-12-11 20:16:26 +0100353 secs_to_sync = pblk_calc_secs(pblk, secs_avail, secs_to_flush, true);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200354
Matias Bjørling880eda52018-07-13 10:48:37 +0200355#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200356 if ((!secs_to_sync && secs_to_flush)
357 || (secs_to_sync < 0)
358 || (secs_to_sync > secs_avail && !secs_to_flush)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200359 pblk_err(pblk, "bad sector calculation (a:%d,s:%d,f:%d)\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360 secs_avail, secs_to_sync, secs_to_flush);
361 }
362#endif
363
364 return secs_to_sync;
365}
366
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200367int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line)
368{
369 struct nvm_tgt_dev *dev = pblk->dev;
370 struct nvm_geo *geo = &dev->geo;
371 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
372 struct pblk_line_meta *lm = &pblk->lm;
373 struct pblk_emeta *emeta = meta_line->emeta;
Hans Holmbergd68a9342018-10-09 13:11:46 +0200374 struct ppa_addr *ppa_list;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200375 struct pblk_g_ctx *m_ctx;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200376 struct nvm_rq *rqd;
377 void *data;
378 u64 paddr;
379 int rq_ppas = pblk->min_write_pgs;
380 int id = meta_line->id;
381 int rq_len;
382 int i, j;
383 int ret;
384
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200385 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
Javier González2942f502017-10-13 14:46:10 +0200386
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200387 m_ctx = nvm_rq_to_pdu(rqd);
388 m_ctx->private = meta_line;
389
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200390 rq_len = rq_ppas * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200391 data = ((void *)emeta->buf) + emeta->mem;
392
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200393 ret = pblk_alloc_w_rq(pblk, rqd, rq_ppas, pblk_end_io_write_meta);
394 if (ret)
Hans Holmberg48e5da722019-07-31 11:41:34 +0200395 goto fail_free_rqd;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200396
Hans Holmbergd68a9342018-10-09 13:11:46 +0200397 ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200398 for (i = 0; i < rqd->nr_ppas; ) {
399 spin_lock(&meta_line->lock);
400 paddr = __pblk_alloc_page(pblk, meta_line, rq_ppas);
401 spin_unlock(&meta_line->lock);
402 for (j = 0; j < rq_ppas; j++, i++, paddr++)
Hans Holmbergd68a9342018-10-09 13:11:46 +0200403 ppa_list[i] = addr_to_gen_ppa(pblk, paddr, id);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200404 }
405
Javier Gonzálezd8adaa32018-10-09 13:11:35 +0200406 spin_lock(&l_mg->close_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200407 emeta->mem += rq_len;
Javier Gonzálezd8adaa32018-10-09 13:11:35 +0200408 if (emeta->mem >= lm->emeta_len[0])
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200409 list_del(&meta_line->list);
Javier Gonzálezd8adaa32018-10-09 13:11:35 +0200410 spin_unlock(&l_mg->close_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200411
Matias Bjørling43241cf2018-10-09 13:11:51 +0200412 pblk_down_chunk(pblk, ppa_list[0]);
Javier González3eaa11e2017-07-07 21:08:52 +0200413
Hans Holmberg48e5da722019-07-31 11:41:34 +0200414 ret = pblk_submit_io(pblk, rqd, data);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200415 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200416 pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200417 goto fail_rollback;
418 }
419
420 return NVM_IO_OK;
421
422fail_rollback:
Matias Bjørling43241cf2018-10-09 13:11:51 +0200423 pblk_up_chunk(pblk, ppa_list[0]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200424 spin_lock(&l_mg->close_lock);
425 pblk_dealloc_page(pblk, meta_line, rq_ppas);
426 list_add(&meta_line->list, &meta_line->list);
427 spin_unlock(&l_mg->close_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200428fail_free_rqd:
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200429 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200430 return ret;
431}
432
Javier González1e821232017-10-13 14:46:25 +0200433static inline bool pblk_valid_meta_ppa(struct pblk *pblk,
434 struct pblk_line *meta_line,
435 struct nvm_rq *data_rqd)
436{
437 struct nvm_tgt_dev *dev = pblk->dev;
438 struct nvm_geo *geo = &dev->geo;
439 struct pblk_c_ctx *data_c_ctx = nvm_rq_to_pdu(data_rqd);
440 struct pblk_line *data_line = pblk_line_get_data(pblk);
441 struct ppa_addr ppa, ppa_opt;
442 u64 paddr;
443 int pos_opt;
444
445 /* Schedule a metadata I/O that is half the distance from the data I/O
446 * with regards to the number of LUNs forming the pblk instance. This
447 * balances LUN conflicts across every I/O.
448 *
449 * When the LUN configuration changes (e.g., due to GC), this distance
450 * can align, which would result on metadata and data I/Os colliding. In
451 * this case, modify the distance to not be optimal, but move the
452 * optimal in the right direction.
453 */
454 paddr = pblk_lookup_page(pblk, meta_line);
455 ppa = addr_to_gen_ppa(pblk, paddr, 0);
456 ppa_opt = addr_to_gen_ppa(pblk, paddr + data_line->meta_distance, 0);
457 pos_opt = pblk_ppa_to_pos(geo, ppa_opt);
458
459 if (test_bit(pos_opt, data_c_ctx->lun_bitmap) ||
460 test_bit(pos_opt, data_line->blk_bitmap))
461 return true;
462
463 if (unlikely(pblk_ppa_comp(ppa_opt, ppa)))
464 data_line->meta_distance--;
465
466 return false;
467}
468
469static struct pblk_line *pblk_should_submit_meta_io(struct pblk *pblk,
470 struct nvm_rq *data_rqd)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200471{
472 struct pblk_line_meta *lm = &pblk->lm;
473 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
474 struct pblk_line *meta_line;
475
476 spin_lock(&l_mg->close_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200477 if (list_empty(&l_mg->emeta_list)) {
478 spin_unlock(&l_mg->close_lock);
Javier González1e821232017-10-13 14:46:25 +0200479 return NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200480 }
481 meta_line = list_first_entry(&l_mg->emeta_list, struct pblk_line, list);
Javier Gonzálezd8adaa32018-10-09 13:11:35 +0200482 if (meta_line->emeta->mem >= lm->emeta_len[0]) {
483 spin_unlock(&l_mg->close_lock);
484 return NULL;
485 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200486 spin_unlock(&l_mg->close_lock);
487
Javier González1e821232017-10-13 14:46:25 +0200488 if (!pblk_valid_meta_ppa(pblk, meta_line, data_rqd))
489 return NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200490
Javier González1e821232017-10-13 14:46:25 +0200491 return meta_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200492}
493
Javier Gonzálezd624f372017-06-26 11:57:15 +0200494static int pblk_submit_io_set(struct pblk *pblk, struct nvm_rq *rqd)
495{
Javier Gonzálezd624f372017-06-26 11:57:15 +0200496 struct ppa_addr erase_ppa;
Javier González1e821232017-10-13 14:46:25 +0200497 struct pblk_line *meta_line;
Javier Gonzálezd624f372017-06-26 11:57:15 +0200498 int err;
499
Matias Bjørling26f76dc2018-01-05 14:15:59 +0100500 pblk_ppa_set_empty(&erase_ppa);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200501
502 /* Assign lbas to ppas and populate request structure */
Javier González1e821232017-10-13 14:46:25 +0200503 err = pblk_setup_w_rq(pblk, rqd, &erase_ppa);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200504 if (err) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200505 pblk_err(pblk, "could not setup write request: %d\n", err);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200506 return NVM_IO_ERR;
507 }
508
Javier González1e821232017-10-13 14:46:25 +0200509 meta_line = pblk_should_submit_meta_io(pblk, rqd);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200510
Javier González1e821232017-10-13 14:46:25 +0200511 /* Submit data write for current data line */
Hans Holmberg48e5da722019-07-31 11:41:34 +0200512 err = pblk_submit_io(pblk, rqd, NULL);
Javier González1e821232017-10-13 14:46:25 +0200513 if (err) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200514 pblk_err(pblk, "data I/O submission failed: %d\n", err);
Javier González1e821232017-10-13 14:46:25 +0200515 return NVM_IO_ERR;
516 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200517
Matias Bjørling26f76dc2018-01-05 14:15:59 +0100518 if (!pblk_ppa_empty(erase_ppa)) {
Javier González1e821232017-10-13 14:46:25 +0200519 /* Submit erase for next data line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200520 if (pblk_blk_erase_async(pblk, erase_ppa)) {
521 struct pblk_line *e_line = pblk_line_get_erase(pblk);
522 struct nvm_tgt_dev *dev = pblk->dev;
523 struct nvm_geo *geo = &dev->geo;
524 int bit;
525
526 atomic_inc(&e_line->left_eblks);
527 bit = pblk_ppa_to_pos(geo, erase_ppa);
528 WARN_ON(!test_and_clear_bit(bit, e_line->erase_bitmap));
529 }
Javier Gonzálezd624f372017-06-26 11:57:15 +0200530 }
531
Javier González1e821232017-10-13 14:46:25 +0200532 if (meta_line) {
533 /* Submit metadata write for previous data line */
534 err = pblk_submit_meta_io(pblk, meta_line);
535 if (err) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200536 pblk_err(pblk, "metadata I/O submission failed: %d",
537 err);
Javier González1e821232017-10-13 14:46:25 +0200538 return NVM_IO_ERR;
539 }
540 }
541
Javier Gonzálezd624f372017-06-26 11:57:15 +0200542 return NVM_IO_OK;
543}
544
545static void pblk_free_write_rqd(struct pblk *pblk, struct nvm_rq *rqd)
546{
547 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
548 struct bio *bio = rqd->bio;
549
550 if (c_ctx->nr_padded)
Javier Gonzálezcd8ddbf2017-10-13 14:46:03 +0200551 pblk_bio_free_pages(pblk, bio, c_ctx->nr_valid,
552 c_ctx->nr_padded);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200553}
554
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100555static int pblk_submit_write(struct pblk *pblk, int *secs_left)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556{
557 struct bio *bio;
558 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200559 unsigned int secs_avail, secs_to_sync, secs_to_com;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100560 unsigned int secs_to_flush, packed_meta_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200561 unsigned long pos;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200562 unsigned int resubmit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100564 *secs_left = 0;
565
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200566 spin_lock(&pblk->resubmit_lock);
567 resubmit = !list_empty(&pblk->resubmit_list);
568 spin_unlock(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200570 /* Resubmit failed writes first */
571 if (resubmit) {
572 struct pblk_c_ctx *r_ctx;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200573
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200574 spin_lock(&pblk->resubmit_lock);
575 r_ctx = list_first_entry(&pblk->resubmit_list,
576 struct pblk_c_ctx, list);
577 list_del(&r_ctx->list);
578 spin_unlock(&pblk->resubmit_lock);
579
580 secs_avail = r_ctx->nr_valid;
581 pos = r_ctx->sentry;
582
583 pblk_prepare_resubmit(pblk, pos, secs_avail);
584 secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail,
585 secs_avail);
586
587 kfree(r_ctx);
588 } else {
589 /* If there are no sectors in the cache,
590 * flushes (bios without data) will be cleared on
591 * the cache threads
592 */
593 secs_avail = pblk_rb_read_count(&pblk->rwb);
594 if (!secs_avail)
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100595 return 0;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200596
597 secs_to_flush = pblk_rb_flush_point_count(&pblk->rwb);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100598 if (!secs_to_flush && secs_avail < pblk->min_write_pgs_data)
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100599 return 0;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200600
601 secs_to_sync = pblk_calc_secs_to_sync(pblk, secs_avail,
602 secs_to_flush);
603 if (secs_to_sync > pblk->max_write_pgs) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200604 pblk_err(pblk, "bad buffer sync calculation\n");
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100605 return 0;
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200606 }
607
608 secs_to_com = (secs_to_sync > secs_avail) ?
609 secs_avail : secs_to_sync;
610 pos = pblk_rb_read_commit(&pblk->rwb, secs_to_com);
Javier González875d94f2017-10-13 14:46:18 +0200611 }
612
Igor Konopko55d8ec32018-12-11 20:16:26 +0100613 packed_meta_pgs = (pblk->min_write_pgs - pblk->min_write_pgs_data);
614 bio = bio_alloc(GFP_KERNEL, secs_to_sync + packed_meta_pgs);
Javier González2942f502017-10-13 14:46:10 +0200615
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616 bio->bi_iter.bi_sector = 0; /* internal bio */
617 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Javier González2942f502017-10-13 14:46:10 +0200618
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200619 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620 rqd->bio = bio;
621
Javier González875d94f2017-10-13 14:46:18 +0200622 if (pblk_rb_read_to_bio(&pblk->rwb, rqd, pos, secs_to_sync,
Javier Gonzálezd624f372017-06-26 11:57:15 +0200623 secs_avail)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200624 pblk_err(pblk, "corrupted write bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200625 goto fail_put_bio;
626 }
627
Javier Gonzálezd624f372017-06-26 11:57:15 +0200628 if (pblk_submit_io_set(pblk, rqd))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200629 goto fail_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200630
Matias Bjørling880eda52018-07-13 10:48:37 +0200631#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200632 atomic_long_add(secs_to_sync, &pblk->sub_writes);
633#endif
634
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100635 *secs_left = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200636 return 0;
637
638fail_free_bio:
Javier Gonzálezd624f372017-06-26 11:57:15 +0200639 pblk_free_write_rqd(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200640fail_put_bio:
641 bio_put(bio);
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200642 pblk_free_rqd(pblk, rqd, PBLK_WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200643
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100644 return -EINTR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645}
646
647int pblk_write_ts(void *data)
648{
649 struct pblk *pblk = data;
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100650 int secs_left;
651 int write_failure = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200652
653 while (!kthread_should_stop()) {
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100654 if (!write_failure) {
655 write_failure = pblk_submit_write(pblk, &secs_left);
656
657 if (secs_left)
658 continue;
659 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660 set_current_state(TASK_INTERRUPTIBLE);
661 io_schedule();
662 }
663
664 return 0;
665}