blob: 299ef47a17b22ae56747b824961c029691e6fc8a [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: Javier Gonzalez <javier@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-recovery.c - pblk's recovery path
Javier González361d8892018-12-11 20:16:19 +010016 *
17 * The L2P recovery path is single threaded as the L2P table is updated in order
18 * following the line sequence ID.
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020019 */
20
21#include "pblk.h"
Hans Holmbergf2937232018-10-09 13:11:53 +020022#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023
Hans Holmberg06bc0722018-01-05 14:16:08 +010024int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025{
26 u32 crc;
27
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020028 crc = pblk_calc_emeta_crc(pblk, emeta_buf);
29 if (le32_to_cpu(emeta_buf->crc) != crc)
Hans Holmberg06bc0722018-01-05 14:16:08 +010030 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020031
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020032 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC)
Hans Holmberg06bc0722018-01-05 14:16:08 +010033 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020034
Hans Holmberg06bc0722018-01-05 14:16:08 +010035 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020036}
37
38static int pblk_recov_l2p_from_emeta(struct pblk *pblk, struct pblk_line *line)
39{
40 struct nvm_tgt_dev *dev = pblk->dev;
41 struct nvm_geo *geo = &dev->geo;
42 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020043 struct pblk_emeta *emeta = line->emeta;
44 struct line_emeta *emeta_buf = emeta->buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045 __le64 *lba_list;
Hans Holmberg75610cd2017-10-13 14:46:38 +020046 u64 data_start, data_end;
47 u64 nr_valid_lbas, nr_lbas = 0;
48 u64 i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049
Hans Holmberg06bc0722018-01-05 14:16:08 +010050 lba_list = emeta_to_lbas(pblk, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051 if (!lba_list)
52 return 1;
53
54 data_start = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Hans Holmberg75610cd2017-10-13 14:46:38 +020055 data_end = line->emeta_ssec;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020056 nr_valid_lbas = le64_to_cpu(emeta_buf->nr_valid_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057
Hans Holmberg92957092017-10-13 14:46:35 +020058 for (i = data_start; i < data_end; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059 struct ppa_addr ppa;
60 int pos;
61
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010062 ppa = addr_to_gen_ppa(pblk, i, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020063 pos = pblk_ppa_to_pos(geo, ppa);
64
65 /* Do not update bad blocks */
66 if (test_bit(pos, line->blk_bitmap))
67 continue;
68
69 if (le64_to_cpu(lba_list[i]) == ADDR_EMPTY) {
70 spin_lock(&line->lock);
71 if (test_and_set_bit(i, line->invalid_bitmap))
Dan Carpenter2a79efd2017-04-15 20:55:52 +020072 WARN_ONCE(1, "pblk: rec. double invalidate:\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073 else
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020074 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020075 spin_unlock(&line->lock);
76
77 continue;
78 }
79
80 pblk_update_map(pblk, le64_to_cpu(lba_list[i]), ppa);
81 nr_lbas++;
82 }
83
84 if (nr_valid_lbas != nr_lbas)
Matias Bjørling4e495a42018-07-13 10:48:42 +020085 pblk_err(pblk, "line %d - inconsistent lba list(%llu/%llu)\n",
Hans Holmberg75610cd2017-10-13 14:46:38 +020086 line->id, nr_valid_lbas, nr_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020087
88 line->left_msecs = 0;
89
90 return 0;
91}
92
Javier González6ad2f612018-10-09 13:12:05 +020093static void pblk_update_line_wp(struct pblk *pblk, struct pblk_line *line,
94 u64 written_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020095{
Igor Konopko2b0ae812019-05-04 20:38:01 +020096 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González6ad2f612018-10-09 13:12:05 +020097 int i;
98
99 for (i = 0; i < written_secs; i += pblk->min_write_pgs)
Igor Konopko2b0ae812019-05-04 20:38:01 +0200100 __pblk_alloc_page(pblk, line, pblk->min_write_pgs);
101
102 spin_lock(&l_mg->free_lock);
103 if (written_secs > line->left_msecs) {
104 /*
105 * We have all data sectors written
106 * and some emeta sectors written too.
107 */
108 line->left_msecs = 0;
109 } else {
110 /* We have only some data sectors written. */
111 line->left_msecs -= written_secs;
112 }
113 spin_unlock(&l_mg->free_lock);
Javier González6ad2f612018-10-09 13:12:05 +0200114}
115
116static u64 pblk_sec_in_open_line(struct pblk *pblk, struct pblk_line *line)
117{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200118 struct pblk_line_meta *lm = &pblk->lm;
119 int nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line);
Javier González6ad2f612018-10-09 13:12:05 +0200120 u64 written_secs = 0;
121 int valid_chunks = 0;
122 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200123
Javier González6ad2f612018-10-09 13:12:05 +0200124 for (i = 0; i < lm->blk_per_line; i++) {
125 struct nvm_chk_meta *chunk = &line->chks[i];
126
127 if (chunk->state & NVM_CHK_ST_OFFLINE)
128 continue;
129
130 written_secs += chunk->wp;
131 valid_chunks++;
132 }
133
134 if (lm->blk_per_line - nr_bb != valid_chunks)
135 pblk_err(pblk, "recovery line %d is bad\n", line->id);
136
137 pblk_update_line_wp(pblk, line, written_secs - lm->smeta_sec);
138
139 return written_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200140}
141
142struct pblk_recov_alloc {
143 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100144 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200145 struct nvm_rq *rqd;
146 void *data;
147 dma_addr_t dma_ppa_list;
148 dma_addr_t dma_meta_list;
149};
150
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200151static void pblk_recov_complete(struct kref *ref)
152{
153 struct pblk_pad_rq *pad_rq = container_of(ref, struct pblk_pad_rq, ref);
154
155 complete(&pad_rq->wait);
156}
157
158static void pblk_end_io_recov(struct nvm_rq *rqd)
159{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200160 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200161 struct pblk_pad_rq *pad_rq = rqd->private;
162 struct pblk *pblk = pad_rq->pblk;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200163
Matias Bjørling43241cf2018-10-09 13:11:51 +0200164 pblk_up_chunk(pblk, ppa_list[0]);
Javier González3eaa11e2017-07-07 21:08:52 +0200165
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200166 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier González3eaa11e2017-07-07 21:08:52 +0200167
168 atomic_dec(&pblk->inflight_io);
169 kref_put(&pad_rq->ref, pblk_recov_complete);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200170}
171
Javier González6ad2f612018-10-09 13:12:05 +0200172/* pad line using line bitmap. */
173static int pblk_recov_pad_line(struct pblk *pblk, struct pblk_line *line,
174 int left_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200175{
176 struct nvm_tgt_dev *dev = pblk->dev;
177 struct nvm_geo *geo = &dev->geo;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100178 void *meta_list;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200179 struct pblk_pad_rq *pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200180 struct nvm_rq *rqd;
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200181 struct ppa_addr *ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200182 void *data;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200183 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200184 u64 w_ptr = line->cur_sec;
Hans Holmberg48e5da722019-07-31 11:41:34 +0200185 int left_line_ppas, rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186 int i, j;
187 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200188
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200189 spin_lock(&line->lock);
190 left_line_ppas = line->left_msecs;
191 spin_unlock(&line->lock);
192
193 pad_rq = kmalloc(sizeof(struct pblk_pad_rq), GFP_KERNEL);
194 if (!pad_rq)
195 return -ENOMEM;
196
Kees Cookfad953c2018-06-12 14:27:37 -0700197 data = vzalloc(array_size(pblk->max_write_pgs, geo->csecs));
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200198 if (!data) {
199 ret = -ENOMEM;
200 goto free_rq;
201 }
202
203 pad_rq->pblk = pblk;
204 init_completion(&pad_rq->wait);
205 kref_init(&pad_rq->ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206
207next_pad_rq:
Igor Konopko55d8ec32018-12-11 20:16:26 +0100208 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200209 if (rq_ppas < pblk->min_write_pgs) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200210 pblk_err(pblk, "corrupted pad line %d\n", line->id);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200211 goto fail_complete;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200212 }
213
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200214 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
Javier González2942f502017-10-13 14:46:10 +0200215
Javier González45dcf292018-10-09 13:12:02 +0200216 ret = pblk_alloc_rqd_meta(pblk, rqd);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200217 if (ret) {
218 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200219 goto fail_complete;
220 }
Javier González45dcf292018-10-09 13:12:02 +0200221
Hans Holmberg48e5da722019-07-31 11:41:34 +0200222 rqd->bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223 rqd->opcode = NVM_OP_PWRITE;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200224 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200225 rqd->nr_ppas = rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200226 rqd->end_io = pblk_end_io_recov;
227 rqd->private = pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200228
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200229 ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González45dcf292018-10-09 13:12:02 +0200230 meta_list = rqd->meta_list;
231
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200232 for (i = 0; i < rqd->nr_ppas; ) {
233 struct ppa_addr ppa;
234 int pos;
235
236 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100237 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 pos = pblk_ppa_to_pos(geo, ppa);
239
240 while (test_bit(pos, line->blk_bitmap)) {
241 w_ptr += pblk->min_write_pgs;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100242 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200243 pos = pblk_ppa_to_pos(geo, ppa);
244 }
245
246 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) {
247 struct ppa_addr dev_ppa;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100248 struct pblk_sec_meta *meta;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200249 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250
251 dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
252
253 pblk_map_invalidate(pblk, dev_ppa);
Igor Konopkofaa79f22018-12-11 20:16:23 +0100254 lba_list[w_ptr] = addr_empty;
255 meta = pblk_get_meta(pblk, meta_list, i);
256 meta->lba = addr_empty;
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200257 ppa_list[i] = dev_ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200258 }
259 }
260
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200261 kref_get(&pad_rq->ref);
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200262 pblk_down_chunk(pblk, ppa_list[0]);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200263
Hans Holmberg48e5da722019-07-31 11:41:34 +0200264 ret = pblk_submit_io(pblk, rqd, data);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200266 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200267 pblk_up_chunk(pblk, ppa_list[0]);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200268 kref_put(&pad_rq->ref, pblk_recov_complete);
269 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200270 goto fail_complete;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200271 }
272
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273 left_line_ppas -= rq_ppas;
274 left_ppas -= rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200275 if (left_ppas && left_line_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200276 goto next_pad_rq;
277
Igor Konopko1fc3b302019-05-04 20:38:03 +0200278fail_complete:
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200279 kref_put(&pad_rq->ref, pblk_recov_complete);
Igor Konopko32ac0fa32019-05-04 20:38:04 +0200280 wait_for_completion(&pad_rq->wait);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200281
Javier González3eaa11e2017-07-07 21:08:52 +0200282 if (!pblk_line_is_full(line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200283 pblk_err(pblk, "corrupted padded line: %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200284
285 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200286free_rq:
287 kfree(pad_rq);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200288 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200289}
290
Javier González63dee3a2018-10-09 13:11:57 +0200291static int pblk_pad_distance(struct pblk *pblk, struct pblk_line *line)
292{
293 struct nvm_tgt_dev *dev = pblk->dev;
294 struct nvm_geo *geo = &dev->geo;
295 int distance = geo->mw_cunits * geo->all_luns * geo->ws_opt;
296
297 return (distance > line->left_msecs) ? line->left_msecs : distance;
298}
299
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100300/* Return a chunk belonging to a line by stripe(write order) index */
301static struct nvm_chk_meta *pblk_get_stripe_chunk(struct pblk *pblk,
302 struct pblk_line *line,
303 int index)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304{
305 struct nvm_tgt_dev *dev = pblk->dev;
306 struct nvm_geo *geo = &dev->geo;
Javier González6ad2f612018-10-09 13:12:05 +0200307 struct pblk_lun *rlun;
Javier González6ad2f612018-10-09 13:12:05 +0200308 struct ppa_addr ppa;
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100309 int pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200310
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100311 rlun = &pblk->luns[index];
Javier González6ad2f612018-10-09 13:12:05 +0200312 ppa = rlun->bppa;
313 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200314
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100315 return &line->chks[pos];
316}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200317
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100318static int pblk_line_wps_are_unbalanced(struct pblk *pblk,
319 struct pblk_line *line)
320{
321 struct pblk_line_meta *lm = &pblk->lm;
322 int blk_in_line = lm->blk_per_line;
323 struct nvm_chk_meta *chunk;
324 u64 max_wp, min_wp;
325 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200326
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100327 i = find_first_zero_bit(line->blk_bitmap, blk_in_line);
328
329 /* If there is one or zero good chunks in the line,
330 * the write pointers can't be unbalanced.
331 */
332 if (i >= (blk_in_line - 1))
333 return 0;
334
335 chunk = pblk_get_stripe_chunk(pblk, line, i);
336 max_wp = chunk->wp;
337 if (max_wp > pblk->max_write_pgs)
338 min_wp = max_wp - pblk->max_write_pgs;
339 else
340 min_wp = 0;
341
342 i = find_next_zero_bit(line->blk_bitmap, blk_in_line, i + 1);
343 while (i < blk_in_line) {
344 chunk = pblk_get_stripe_chunk(pblk, line, i);
345 if (chunk->wp > max_wp || chunk->wp < min_wp)
Javier González6ad2f612018-10-09 13:12:05 +0200346 return 1;
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100347
348 i = find_next_zero_bit(line->blk_bitmap, blk_in_line, i + 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349 }
350
Javier González6ad2f612018-10-09 13:12:05 +0200351 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200352}
353
354static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200355 struct pblk_recov_alloc p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200356{
357 struct nvm_tgt_dev *dev = pblk->dev;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100358 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200359 struct nvm_geo *geo = &dev->geo;
360 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100361 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200363 void *data;
364 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200365 __le64 *lba_list;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100366 u64 paddr = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200367 bool padded = false;
YueHaibingba6f7da2019-08-07 21:18:47 +0800368 int rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369 int i, j;
Javier González6ad2f612018-10-09 13:12:05 +0200370 int ret;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100371 u64 left_ppas = pblk_sec_in_open_line(pblk, line) - lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200372
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100373 if (pblk_line_wps_are_unbalanced(pblk, line))
Javier González6ad2f612018-10-09 13:12:05 +0200374 pblk_warn(pblk, "recovering unbalanced line (%d)\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375
376 ppa_list = p.ppa_list;
377 meta_list = p.meta_list;
378 rqd = p.rqd;
379 data = p.data;
380 dma_ppa_list = p.dma_ppa_list;
381 dma_meta_list = p.dma_meta_list;
382
Javier González6ad2f612018-10-09 13:12:05 +0200383 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384
385next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600386 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387
Igor Konopko55d8ec32018-12-11 20:16:26 +0100388 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200389 if (!rq_ppas)
390 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200391
Igor Konopko55d8ec32018-12-11 20:16:26 +0100392retry_rq:
Hans Holmberg48e5da722019-07-31 11:41:34 +0200393 rqd->bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200394 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200395 rqd->meta_list = meta_list;
396 rqd->nr_ppas = rq_ppas;
397 rqd->ppa_list = ppa_list;
398 rqd->dma_ppa_list = dma_ppa_list;
399 rqd->dma_meta_list = dma_meta_list;
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200400 ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200401
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200402 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200403 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200404
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405 for (i = 0; i < rqd->nr_ppas; ) {
406 struct ppa_addr ppa;
407 int pos;
408
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200409 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100410 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200411
412 while (test_bit(pos, line->blk_bitmap)) {
413 paddr += pblk->min_write_pgs;
414 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100415 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416 }
417
Javier González6ad2f612018-10-09 13:12:05 +0200418 for (j = 0; j < pblk->min_write_pgs; j++, i++)
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200419 ppa_list[i] =
Javier González6ad2f612018-10-09 13:12:05 +0200420 addr_to_gen_ppa(pblk, paddr + j, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200421 }
422
Hans Holmberg48e5da722019-07-31 11:41:34 +0200423 ret = pblk_submit_io_sync(pblk, rqd, data);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200425 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426 return ret;
427 }
428
Javier González588726d32017-06-26 11:57:29 +0200429 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430
Javier González6ad2f612018-10-09 13:12:05 +0200431 /* If a read fails, do a best effort by padding the line and retrying */
Igor Konopkod165a7a2019-05-04 20:38:02 +0200432 if (rqd->error && rqd->error != NVM_RSP_WARN_HIGHECC) {
Javier González6ad2f612018-10-09 13:12:05 +0200433 int pad_distance, ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200434
Javier González6ad2f612018-10-09 13:12:05 +0200435 if (padded) {
436 pblk_log_read_err(pblk, rqd);
437 return -EINTR;
438 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439
Javier González6ad2f612018-10-09 13:12:05 +0200440 pad_distance = pblk_pad_distance(pblk, line);
441 ret = pblk_recov_pad_line(pblk, line, pad_distance);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100442 if (ret) {
Javier González6ad2f612018-10-09 13:12:05 +0200443 return ret;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100444 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200445
Javier González6ad2f612018-10-09 13:12:05 +0200446 padded = true;
447 goto retry_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448 }
449
Igor Konopko55d8ec32018-12-11 20:16:26 +0100450 pblk_get_packed_meta(pblk, rqd);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100451
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452 for (i = 0; i < rqd->nr_ppas; i++) {
Igor Konopkofaa79f22018-12-11 20:16:23 +0100453 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
454 u64 lba = le64_to_cpu(meta->lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455
Javier González6ad2f612018-10-09 13:12:05 +0200456 lba_list[paddr++] = cpu_to_le64(lba);
457
Igor Konopko847a3a22019-05-04 20:37:48 +0200458 if (lba == ADDR_EMPTY || lba >= pblk->capacity)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200459 continue;
460
Javier González6ad2f612018-10-09 13:12:05 +0200461 line->nr_valid_lbas++;
Igor Konopko45c5fcb2019-05-04 20:38:11 +0200462 pblk_update_map(pblk, lba, ppa_list[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463 }
464
465 left_ppas -= rq_ppas;
466 if (left_ppas > 0)
467 goto next_rq;
468
Javier González6ad2f612018-10-09 13:12:05 +0200469#ifdef CONFIG_NVM_PBLK_DEBUG
470 WARN_ON(padded && !pblk_line_is_full(line));
471#endif
472
473 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474}
475
476/* Scan line for lbas on out of bound area */
477static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line)
478{
479 struct nvm_tgt_dev *dev = pblk->dev;
480 struct nvm_geo *geo = &dev->geo;
481 struct nvm_rq *rqd;
482 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100483 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 struct pblk_recov_alloc p;
485 void *data;
486 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200487 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200489 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
Javier González2942f502017-10-13 14:46:10 +0200490 if (!meta_list)
491 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492
Igor Konopko24828d02018-12-11 20:16:24 +0100493 ppa_list = (void *)(meta_list) + pblk_dma_meta_size(pblk);
494 dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200496 data = kcalloc(pblk->max_write_pgs, geo->csecs, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497 if (!data) {
498 ret = -ENOMEM;
499 goto free_meta_list;
500 }
501
Javier González6ad2f612018-10-09 13:12:05 +0200502 rqd = mempool_alloc(&pblk->r_rq_pool, GFP_KERNEL);
503 memset(rqd, 0, pblk_g_rq_size);
Javier González2942f502017-10-13 14:46:10 +0200504
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505 p.ppa_list = ppa_list;
506 p.meta_list = meta_list;
507 p.rqd = rqd;
508 p.data = data;
509 p.dma_ppa_list = dma_ppa_list;
510 p.dma_meta_list = dma_meta_list;
511
Javier González6ad2f612018-10-09 13:12:05 +0200512 ret = pblk_recov_scan_oob(pblk, line, p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513 if (ret) {
Javier González6ad2f612018-10-09 13:12:05 +0200514 pblk_err(pblk, "could not recover L2P form OOB\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200515 goto out;
516 }
517
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518 if (pblk_line_is_full(line))
519 pblk_line_recov_close(pblk, line);
520
521out:
Javier González6ad2f612018-10-09 13:12:05 +0200522 mempool_free(rqd, &pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523 kfree(data);
524free_meta_list:
525 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200526
527 return ret;
528}
529
530/* Insert lines ordered by sequence number (seq_num) on list */
531static void pblk_recov_line_add_ordered(struct list_head *head,
532 struct pblk_line *line)
533{
534 struct pblk_line *t = NULL;
535
536 list_for_each_entry(t, head, list)
537 if (t->seq_nr > line->seq_nr)
538 break;
539
540 __list_add(&line->list, t->list.prev, &t->list);
541}
542
Hans Holmberg75610cd2017-10-13 14:46:38 +0200543static u64 pblk_line_emeta_start(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200544{
545 struct nvm_tgt_dev *dev = pblk->dev;
546 struct nvm_geo *geo = &dev->geo;
547 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg75610cd2017-10-13 14:46:38 +0200548 unsigned int emeta_secs;
549 u64 emeta_start;
550 struct ppa_addr ppa;
551 int pos;
552
553 emeta_secs = lm->emeta_sec[0];
554 emeta_start = lm->sec_per_line;
555
556 while (emeta_secs) {
557 emeta_start--;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100558 ppa = addr_to_gen_ppa(pblk, emeta_start, line->id);
Hans Holmberg75610cd2017-10-13 14:46:38 +0200559 pos = pblk_ppa_to_pos(geo, ppa);
560 if (!test_bit(pos, line->blk_bitmap))
561 emeta_secs--;
562 }
563
564 return emeta_start;
565}
566
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200567static int pblk_recov_check_line_version(struct pblk *pblk,
568 struct line_emeta *emeta)
569{
570 struct line_header *header = &emeta->header;
571
572 if (header->version_major != EMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200573 pblk_err(pblk, "line major version mismatch: %d, expected: %d\n",
574 header->version_major, EMETA_VERSION_MAJOR);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200575 return 1;
576 }
577
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200578#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200579 if (header->version_minor > EMETA_VERSION_MINOR)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200580 pblk_info(pblk, "newer line minor version found: %d\n",
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200581 header->version_minor);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200582#endif
583
584 return 0;
585}
586
Hans Holmberg76758392018-03-30 00:04:52 +0200587static void pblk_recov_wa_counters(struct pblk *pblk,
588 struct line_emeta *emeta)
589{
590 struct pblk_line_meta *lm = &pblk->lm;
591 struct line_header *header = &emeta->header;
592 struct wa_counters *wa = emeta_to_wa(lm, emeta);
593
594 /* WA counters were introduced in emeta version 0.2 */
595 if (header->version_major > 0 || header->version_minor >= 2) {
596 u64 user = le64_to_cpu(wa->user);
597 u64 pad = le64_to_cpu(wa->pad);
598 u64 gc = le64_to_cpu(wa->gc);
599
600 atomic64_set(&pblk->user_wa, user);
601 atomic64_set(&pblk->pad_wa, pad);
602 atomic64_set(&pblk->gc_wa, gc);
603
604 pblk->user_rst_wa = user;
605 pblk->pad_rst_wa = pad;
606 pblk->gc_rst_wa = gc;
607 }
608}
609
Hans Holmberg9156f362018-03-30 00:05:22 +0200610static int pblk_line_was_written(struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200611 struct pblk *pblk)
Hans Holmberg9156f362018-03-30 00:05:22 +0200612{
613
Hans Holmbergb06be282018-06-01 16:41:12 +0200614 struct pblk_line_meta *lm = &pblk->lm;
615 struct nvm_tgt_dev *dev = pblk->dev;
616 struct nvm_geo *geo = &dev->geo;
617 struct nvm_chk_meta *chunk;
618 struct ppa_addr bppa;
619 int smeta_blk;
Hans Holmberg9156f362018-03-30 00:05:22 +0200620
Hans Holmbergb06be282018-06-01 16:41:12 +0200621 if (line->state == PBLK_LINESTATE_BAD)
622 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200623
Hans Holmbergb06be282018-06-01 16:41:12 +0200624 smeta_blk = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
625 if (smeta_blk >= lm->blk_per_line)
626 return 0;
627
628 bppa = pblk->luns[smeta_blk].bppa;
629 chunk = &line->chks[pblk_ppa_to_pos(geo, bppa)];
630
Igor Konopkoa24eab52019-05-04 20:38:07 +0200631 if (chunk->state & NVM_CHK_ST_CLOSED ||
632 (chunk->state & NVM_CHK_ST_OPEN
633 && chunk->wp >= lm->smeta_sec))
634 return 1;
Hans Holmbergb06be282018-06-01 16:41:12 +0200635
Igor Konopkoa24eab52019-05-04 20:38:07 +0200636 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200637}
638
Javier González6ad2f612018-10-09 13:12:05 +0200639static bool pblk_line_is_open(struct pblk *pblk, struct pblk_line *line)
640{
641 struct pblk_line_meta *lm = &pblk->lm;
642 int i;
643
644 for (i = 0; i < lm->blk_per_line; i++)
645 if (line->chks[i].state & NVM_CHK_ST_OPEN)
646 return true;
647
648 return false;
649}
650
Hans Holmberg75610cd2017-10-13 14:46:38 +0200651struct pblk_line *pblk_recov_l2p(struct pblk *pblk)
652{
653 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200654 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
655 struct pblk_line *line, *tline, *data_line = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200656 struct pblk_smeta *smeta;
657 struct pblk_emeta *emeta;
658 struct line_smeta *smeta_buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200659 int found_lines = 0, recovered_lines = 0, open_lines = 0;
660 int is_next = 0;
661 int meta_line;
662 int i, valid_uuid = 0;
663 LIST_HEAD(recov_list);
664
665 /* TODO: Implement FTL snapshot */
666
667 /* Scan recovery - takes place when FTL snapshot fails */
668 spin_lock(&l_mg->free_lock);
669 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
670 set_bit(meta_line, &l_mg->meta_bitmap);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200671 smeta = l_mg->sline_meta[meta_line];
672 emeta = l_mg->eline_meta[meta_line];
Javier González8224cbd2017-06-30 17:56:38 +0200673 smeta_buf = (struct line_smeta *)smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200674 spin_unlock(&l_mg->free_lock);
675
676 /* Order data lines using their sequence number */
677 for (i = 0; i < l_mg->nr_lines; i++) {
678 u32 crc;
679
680 line = &pblk->lines[i];
681
682 memset(smeta, 0, lm->smeta_len);
683 line->smeta = smeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200684 line->lun_bitmap = ((void *)(smeta_buf)) +
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200685 sizeof(struct line_smeta);
686
Hans Holmbergb06be282018-06-01 16:41:12 +0200687 if (!pblk_line_was_written(line, pblk))
Hans Holmberg9156f362018-03-30 00:05:22 +0200688 continue;
689
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690 /* Lines that cannot be read are assumed as not written here */
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200691 if (pblk_line_smeta_read(pblk, line))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692 continue;
693
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200694 crc = pblk_calc_smeta_crc(pblk, smeta_buf);
695 if (le32_to_cpu(smeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200696 continue;
697
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200698 if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200699 continue;
700
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200701 if (smeta_buf->header.version_major != SMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200702 pblk_err(pblk, "found incompatible line version %u\n",
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200703 smeta_buf->header.version_major);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200704 return ERR_PTR(-EINVAL);
705 }
706
707 /* The first valid instance uuid is used for initialization */
708 if (!valid_uuid) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100709 guid_copy(&pblk->instance_uuid,
710 (guid_t *)&smeta_buf->header.uuid);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200711 valid_uuid = 1;
712 }
713
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100714 if (!guid_equal(&pblk->instance_uuid,
715 (guid_t *)&smeta_buf->header.uuid)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200716 pblk_debug(pblk, "ignore line %u due to uuid mismatch\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717 i);
718 continue;
719 }
720
721 /* Update line metadata */
722 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200723 line->id = le32_to_cpu(smeta_buf->header.id);
724 line->type = le16_to_cpu(smeta_buf->header.type);
725 line->seq_nr = le64_to_cpu(smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200726 spin_unlock(&line->lock);
727
728 /* Update general metadata */
729 spin_lock(&l_mg->free_lock);
730 if (line->seq_nr >= l_mg->d_seq_nr)
731 l_mg->d_seq_nr = line->seq_nr + 1;
732 l_mg->nr_free_lines--;
733 spin_unlock(&l_mg->free_lock);
734
735 if (pblk_line_recov_alloc(pblk, line))
736 goto out;
737
738 pblk_recov_line_add_ordered(&recov_list, line);
739 found_lines++;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200740 pblk_debug(pblk, "recovering data line %d, seq:%llu\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200741 line->id, smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200742 }
743
744 if (!found_lines) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100745 guid_gen(&pblk->instance_uuid);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746
747 spin_lock(&l_mg->free_lock);
748 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
749 &l_mg->meta_bitmap));
750 spin_unlock(&l_mg->free_lock);
751
752 goto out;
753 }
754
755 /* Verify closed blocks and recover this portion of L2P table*/
756 list_for_each_entry_safe(line, tline, &recov_list, list) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757 recovered_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200758
Hans Holmberg75610cd2017-10-13 14:46:38 +0200759 line->emeta_ssec = pblk_line_emeta_start(pblk, line);
Javier González8224cbd2017-06-30 17:56:38 +0200760 line->emeta = emeta;
761 memset(line->emeta->buf, 0, lm->emeta_len[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762
Javier González6ad2f612018-10-09 13:12:05 +0200763 if (pblk_line_is_open(pblk, line)) {
764 pblk_recov_l2p_from_oob(pblk, line);
765 goto next;
766 }
767
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200768 if (pblk_line_emeta_read(pblk, line, line->emeta->buf)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200769 pblk_recov_l2p_from_oob(pblk, line);
770 goto next;
771 }
772
Hans Holmberg06bc0722018-01-05 14:16:08 +0100773 if (pblk_recov_check_emeta(pblk, line->emeta->buf)) {
774 pblk_recov_l2p_from_oob(pblk, line);
775 goto next;
776 }
777
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200778 if (pblk_recov_check_line_version(pblk, line->emeta->buf))
779 return ERR_PTR(-EINVAL);
780
Hans Holmberg76758392018-03-30 00:04:52 +0200781 pblk_recov_wa_counters(pblk, line->emeta->buf);
782
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200783 if (pblk_recov_l2p_from_emeta(pblk, line))
784 pblk_recov_l2p_from_oob(pblk, line);
785
786next:
787 if (pblk_line_is_full(line)) {
788 struct list_head *move_list;
789
790 spin_lock(&line->lock);
791 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +0200792 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
793 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794 move_list = pblk_line_gc_list(pblk, line);
795 spin_unlock(&line->lock);
796
797 spin_lock(&l_mg->gc_lock);
798 list_move_tail(&line->list, move_list);
799 spin_unlock(&l_mg->gc_lock);
800
Hans Holmberg53d82db2018-10-09 13:11:47 +0200801 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200802 line->map_bitmap = NULL;
803 line->smeta = NULL;
804 line->emeta = NULL;
805 } else {
Javier González6ad2f612018-10-09 13:12:05 +0200806 spin_lock(&line->lock);
807 line->state = PBLK_LINESTATE_OPEN;
808 spin_unlock(&line->lock);
809
810 line->emeta->mem = 0;
811 atomic_set(&line->emeta->sync, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200812
Hans Holmbergf2937232018-10-09 13:11:53 +0200813 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
814 line->state);
815
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200816 data_line = line;
Javier González6ad2f612018-10-09 13:12:05 +0200817 line->meta_line = meta_line;
818
819 open_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200820 }
821 }
822
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200823 if (!open_lines) {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200824 spin_lock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200825 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
826 &l_mg->meta_bitmap));
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200827 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200828 } else {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200829 spin_lock(&l_mg->free_lock);
Marcin Dziegielewski4bbae692019-05-04 20:37:57 +0200830 l_mg->data_line = data_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200831 /* Allocate next line for preparation */
832 l_mg->data_next = pblk_line_get(pblk);
833 if (l_mg->data_next) {
834 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
835 l_mg->data_next->type = PBLK_LINETYPE_DATA;
836 is_next = 1;
837 }
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200838 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200839 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200840
Javier Gonzáleza7689932018-01-05 14:16:13 +0100841 if (is_next)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842 pblk_line_erase(pblk, l_mg->data_next);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200843
844out:
845 if (found_lines != recovered_lines)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200846 pblk_err(pblk, "failed to recover all found lines %d/%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847 found_lines, recovered_lines);
848
849 return data_line;
850}
851
852/*
Javier González588726d32017-06-26 11:57:29 +0200853 * Pad current line
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854 */
Javier González588726d32017-06-26 11:57:29 +0200855int pblk_recov_pad(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200856{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200857 struct pblk_line *line;
858 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +0200859 int left_msecs;
860 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200861
862 spin_lock(&l_mg->free_lock);
863 line = l_mg->data_line;
Javier González588726d32017-06-26 11:57:29 +0200864 left_msecs = line->left_msecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865 spin_unlock(&l_mg->free_lock);
866
Javier González6ad2f612018-10-09 13:12:05 +0200867 ret = pblk_recov_pad_line(pblk, line, left_msecs);
Javier González588726d32017-06-26 11:57:29 +0200868 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200869 pblk_err(pblk, "tear down padding failed (%d)\n", ret);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200870 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200871 }
872
Javier González588726d32017-06-26 11:57:29 +0200873 pblk_line_close_meta(pblk, line);
Javier González588726d32017-06-26 11:57:29 +0200874 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200875}