blob: a9085b0e66115086b1b0ac58d24f3376aa2e477c [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;
181 struct bio *bio;
182 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;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200185 int left_line_ppas, rq_ppas, rq_len;
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áleze46f4e42018-03-30 00:05:10 +0200214 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200215
Javier González3eaa11e2017-07-07 21:08:52 +0200216 bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
217 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200218 if (IS_ERR(bio)) {
219 ret = PTR_ERR(bio);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200220 goto fail_complete;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200221 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200222
223 bio->bi_iter.bi_sector = 0; /* internal bio */
224 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
225
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200226 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
Javier González2942f502017-10-13 14:46:10 +0200227
Javier González45dcf292018-10-09 13:12:02 +0200228 ret = pblk_alloc_rqd_meta(pblk, rqd);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200229 if (ret) {
230 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
231 bio_put(bio);
232 goto fail_complete;
233 }
Javier González45dcf292018-10-09 13:12:02 +0200234
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235 rqd->bio = bio;
236 rqd->opcode = NVM_OP_PWRITE;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200237 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 rqd->nr_ppas = rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200239 rqd->end_io = pblk_end_io_recov;
240 rqd->private = pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200241
Javier González45dcf292018-10-09 13:12:02 +0200242 meta_list = rqd->meta_list;
243
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200244 for (i = 0; i < rqd->nr_ppas; ) {
245 struct ppa_addr ppa;
246 int pos;
247
248 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100249 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250 pos = pblk_ppa_to_pos(geo, ppa);
251
252 while (test_bit(pos, line->blk_bitmap)) {
253 w_ptr += pblk->min_write_pgs;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100254 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255 pos = pblk_ppa_to_pos(geo, ppa);
256 }
257
258 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) {
259 struct ppa_addr dev_ppa;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100260 struct pblk_sec_meta *meta;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200261 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200262
263 dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
264
265 pblk_map_invalidate(pblk, dev_ppa);
Igor Konopkofaa79f22018-12-11 20:16:23 +0100266 lba_list[w_ptr] = addr_empty;
267 meta = pblk_get_meta(pblk, meta_list, i);
268 meta->lba = addr_empty;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200269 rqd->ppa_list[i] = dev_ppa;
270 }
271 }
272
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200273 kref_get(&pad_rq->ref);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200274 pblk_down_chunk(pblk, rqd->ppa_list[0]);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200275
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200276 ret = pblk_submit_io(pblk, rqd);
277 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200278 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200279 pblk_up_chunk(pblk, rqd->ppa_list[0]);
Igor Konopko1fc3b302019-05-04 20:38:03 +0200280 kref_put(&pad_rq->ref, pblk_recov_complete);
281 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
282 bio_put(bio);
283 goto fail_complete;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200284 }
285
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200286 left_line_ppas -= rq_ppas;
287 left_ppas -= rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200288 if (left_ppas && left_line_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200289 goto next_pad_rq;
290
Igor Konopko1fc3b302019-05-04 20:38:03 +0200291fail_complete:
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200292 kref_put(&pad_rq->ref, pblk_recov_complete);
Igor Konopko32ac0fa32019-05-04 20:38:04 +0200293 wait_for_completion(&pad_rq->wait);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200294
Javier González3eaa11e2017-07-07 21:08:52 +0200295 if (!pblk_line_is_full(line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200296 pblk_err(pblk, "corrupted padded line: %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200297
298 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200299free_rq:
300 kfree(pad_rq);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200301 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302}
303
Javier González63dee3a2018-10-09 13:11:57 +0200304static int pblk_pad_distance(struct pblk *pblk, struct pblk_line *line)
305{
306 struct nvm_tgt_dev *dev = pblk->dev;
307 struct nvm_geo *geo = &dev->geo;
308 int distance = geo->mw_cunits * geo->all_luns * geo->ws_opt;
309
310 return (distance > line->left_msecs) ? line->left_msecs : distance;
311}
312
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100313/* Return a chunk belonging to a line by stripe(write order) index */
314static struct nvm_chk_meta *pblk_get_stripe_chunk(struct pblk *pblk,
315 struct pblk_line *line,
316 int index)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200317{
318 struct nvm_tgt_dev *dev = pblk->dev;
319 struct nvm_geo *geo = &dev->geo;
Javier González6ad2f612018-10-09 13:12:05 +0200320 struct pblk_lun *rlun;
Javier González6ad2f612018-10-09 13:12:05 +0200321 struct ppa_addr ppa;
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100322 int pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100324 rlun = &pblk->luns[index];
Javier González6ad2f612018-10-09 13:12:05 +0200325 ppa = rlun->bppa;
326 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200327
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100328 return &line->chks[pos];
329}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100331static int pblk_line_wps_are_unbalanced(struct pblk *pblk,
332 struct pblk_line *line)
333{
334 struct pblk_line_meta *lm = &pblk->lm;
335 int blk_in_line = lm->blk_per_line;
336 struct nvm_chk_meta *chunk;
337 u64 max_wp, min_wp;
338 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200339
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100340 i = find_first_zero_bit(line->blk_bitmap, blk_in_line);
341
342 /* If there is one or zero good chunks in the line,
343 * the write pointers can't be unbalanced.
344 */
345 if (i >= (blk_in_line - 1))
346 return 0;
347
348 chunk = pblk_get_stripe_chunk(pblk, line, i);
349 max_wp = chunk->wp;
350 if (max_wp > pblk->max_write_pgs)
351 min_wp = max_wp - pblk->max_write_pgs;
352 else
353 min_wp = 0;
354
355 i = find_next_zero_bit(line->blk_bitmap, blk_in_line, i + 1);
356 while (i < blk_in_line) {
357 chunk = pblk_get_stripe_chunk(pblk, line, i);
358 if (chunk->wp > max_wp || chunk->wp < min_wp)
Javier González6ad2f612018-10-09 13:12:05 +0200359 return 1;
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100360
361 i = find_next_zero_bit(line->blk_bitmap, blk_in_line, i + 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362 }
363
Javier González6ad2f612018-10-09 13:12:05 +0200364 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200365}
366
367static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200368 struct pblk_recov_alloc p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369{
370 struct nvm_tgt_dev *dev = pblk->dev;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100371 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200372 struct nvm_geo *geo = &dev->geo;
373 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100374 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375 struct nvm_rq *rqd;
376 struct bio *bio;
377 void *data;
378 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200379 __le64 *lba_list;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100380 u64 paddr = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200381 bool padded = false;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200382 int rq_ppas, rq_len;
383 int i, j;
Javier González6ad2f612018-10-09 13:12:05 +0200384 int ret;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100385 u64 left_ppas = pblk_sec_in_open_line(pblk, line) - lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200386
Hans Holmbergaa8759d2019-02-11 13:25:07 +0100387 if (pblk_line_wps_are_unbalanced(pblk, line))
Javier González6ad2f612018-10-09 13:12:05 +0200388 pblk_warn(pblk, "recovering unbalanced line (%d)\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200389
390 ppa_list = p.ppa_list;
391 meta_list = p.meta_list;
392 rqd = p.rqd;
393 data = p.data;
394 dma_ppa_list = p.dma_ppa_list;
395 dma_meta_list = p.dma_meta_list;
396
Javier González6ad2f612018-10-09 13:12:05 +0200397 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398
399next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600400 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200401
Igor Konopko55d8ec32018-12-11 20:16:26 +0100402 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403 if (!rq_ppas)
404 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200405 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200406
Igor Konopko55d8ec32018-12-11 20:16:26 +0100407retry_rq:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200408 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
409 if (IS_ERR(bio))
410 return PTR_ERR(bio);
411
412 bio->bi_iter.bi_sector = 0; /* internal bio */
413 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100414 bio_get(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200415
416 rqd->bio = bio;
417 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418 rqd->meta_list = meta_list;
419 rqd->nr_ppas = rq_ppas;
420 rqd->ppa_list = ppa_list;
421 rqd->dma_ppa_list = dma_ppa_list;
422 rqd->dma_meta_list = dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200423
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200424 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200425 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200426
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200427 for (i = 0; i < rqd->nr_ppas; ) {
428 struct ppa_addr ppa;
429 int pos;
430
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100432 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433
434 while (test_bit(pos, line->blk_bitmap)) {
435 paddr += pblk->min_write_pgs;
436 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100437 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 }
439
Javier González6ad2f612018-10-09 13:12:05 +0200440 for (j = 0; j < pblk->min_write_pgs; j++, i++)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200441 rqd->ppa_list[i] =
Javier González6ad2f612018-10-09 13:12:05 +0200442 addr_to_gen_ppa(pblk, paddr + j, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200443 }
444
Javier González1a94b2d2017-10-13 14:46:47 +0200445 ret = pblk_submit_io_sync(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200446 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200447 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448 bio_put(bio);
449 return ret;
450 }
451
Javier González588726d32017-06-26 11:57:29 +0200452 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453
Javier González6ad2f612018-10-09 13:12:05 +0200454 /* If a read fails, do a best effort by padding the line and retrying */
Igor Konopkod165a7a2019-05-04 20:38:02 +0200455 if (rqd->error && rqd->error != NVM_RSP_WARN_HIGHECC) {
Javier González6ad2f612018-10-09 13:12:05 +0200456 int pad_distance, ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200457
Javier González6ad2f612018-10-09 13:12:05 +0200458 if (padded) {
459 pblk_log_read_err(pblk, rqd);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100460 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200461 return -EINTR;
462 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463
Javier González6ad2f612018-10-09 13:12:05 +0200464 pad_distance = pblk_pad_distance(pblk, line);
465 ret = pblk_recov_pad_line(pblk, line, pad_distance);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100466 if (ret) {
467 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200468 return ret;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100469 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470
Javier González6ad2f612018-10-09 13:12:05 +0200471 padded = true;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100472 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200473 goto retry_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474 }
475
Igor Konopko55d8ec32018-12-11 20:16:26 +0100476 pblk_get_packed_meta(pblk, rqd);
477 bio_put(bio);
478
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200479 for (i = 0; i < rqd->nr_ppas; i++) {
Igor Konopkofaa79f22018-12-11 20:16:23 +0100480 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
481 u64 lba = le64_to_cpu(meta->lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200482
Javier González6ad2f612018-10-09 13:12:05 +0200483 lba_list[paddr++] = cpu_to_le64(lba);
484
Igor Konopko847a3a22019-05-04 20:37:48 +0200485 if (lba == ADDR_EMPTY || lba >= pblk->capacity)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200486 continue;
487
Javier González6ad2f612018-10-09 13:12:05 +0200488 line->nr_valid_lbas++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200489 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
490 }
491
492 left_ppas -= rq_ppas;
493 if (left_ppas > 0)
494 goto next_rq;
495
Javier González6ad2f612018-10-09 13:12:05 +0200496#ifdef CONFIG_NVM_PBLK_DEBUG
497 WARN_ON(padded && !pblk_line_is_full(line));
498#endif
499
500 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501}
502
503/* Scan line for lbas on out of bound area */
504static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line)
505{
506 struct nvm_tgt_dev *dev = pblk->dev;
507 struct nvm_geo *geo = &dev->geo;
508 struct nvm_rq *rqd;
509 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100510 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200511 struct pblk_recov_alloc p;
512 void *data;
513 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200514 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200515
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200516 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
Javier González2942f502017-10-13 14:46:10 +0200517 if (!meta_list)
518 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200519
Igor Konopko24828d02018-12-11 20:16:24 +0100520 ppa_list = (void *)(meta_list) + pblk_dma_meta_size(pblk);
521 dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200522
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200523 data = kcalloc(pblk->max_write_pgs, geo->csecs, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 if (!data) {
525 ret = -ENOMEM;
526 goto free_meta_list;
527 }
528
Javier González6ad2f612018-10-09 13:12:05 +0200529 rqd = mempool_alloc(&pblk->r_rq_pool, GFP_KERNEL);
530 memset(rqd, 0, pblk_g_rq_size);
Javier González2942f502017-10-13 14:46:10 +0200531
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200532 p.ppa_list = ppa_list;
533 p.meta_list = meta_list;
534 p.rqd = rqd;
535 p.data = data;
536 p.dma_ppa_list = dma_ppa_list;
537 p.dma_meta_list = dma_meta_list;
538
Javier González6ad2f612018-10-09 13:12:05 +0200539 ret = pblk_recov_scan_oob(pblk, line, p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540 if (ret) {
Javier González6ad2f612018-10-09 13:12:05 +0200541 pblk_err(pblk, "could not recover L2P form OOB\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542 goto out;
543 }
544
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545 if (pblk_line_is_full(line))
546 pblk_line_recov_close(pblk, line);
547
548out:
Javier González6ad2f612018-10-09 13:12:05 +0200549 mempool_free(rqd, &pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200550 kfree(data);
551free_meta_list:
552 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200553
554 return ret;
555}
556
557/* Insert lines ordered by sequence number (seq_num) on list */
558static void pblk_recov_line_add_ordered(struct list_head *head,
559 struct pblk_line *line)
560{
561 struct pblk_line *t = NULL;
562
563 list_for_each_entry(t, head, list)
564 if (t->seq_nr > line->seq_nr)
565 break;
566
567 __list_add(&line->list, t->list.prev, &t->list);
568}
569
Hans Holmberg75610cd2017-10-13 14:46:38 +0200570static u64 pblk_line_emeta_start(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571{
572 struct nvm_tgt_dev *dev = pblk->dev;
573 struct nvm_geo *geo = &dev->geo;
574 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg75610cd2017-10-13 14:46:38 +0200575 unsigned int emeta_secs;
576 u64 emeta_start;
577 struct ppa_addr ppa;
578 int pos;
579
580 emeta_secs = lm->emeta_sec[0];
581 emeta_start = lm->sec_per_line;
582
583 while (emeta_secs) {
584 emeta_start--;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100585 ppa = addr_to_gen_ppa(pblk, emeta_start, line->id);
Hans Holmberg75610cd2017-10-13 14:46:38 +0200586 pos = pblk_ppa_to_pos(geo, ppa);
587 if (!test_bit(pos, line->blk_bitmap))
588 emeta_secs--;
589 }
590
591 return emeta_start;
592}
593
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200594static int pblk_recov_check_line_version(struct pblk *pblk,
595 struct line_emeta *emeta)
596{
597 struct line_header *header = &emeta->header;
598
599 if (header->version_major != EMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200600 pblk_err(pblk, "line major version mismatch: %d, expected: %d\n",
601 header->version_major, EMETA_VERSION_MAJOR);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200602 return 1;
603 }
604
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200605#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200606 if (header->version_minor > EMETA_VERSION_MINOR)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200607 pblk_info(pblk, "newer line minor version found: %d\n",
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200608 header->version_minor);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200609#endif
610
611 return 0;
612}
613
Hans Holmberg76758392018-03-30 00:04:52 +0200614static void pblk_recov_wa_counters(struct pblk *pblk,
615 struct line_emeta *emeta)
616{
617 struct pblk_line_meta *lm = &pblk->lm;
618 struct line_header *header = &emeta->header;
619 struct wa_counters *wa = emeta_to_wa(lm, emeta);
620
621 /* WA counters were introduced in emeta version 0.2 */
622 if (header->version_major > 0 || header->version_minor >= 2) {
623 u64 user = le64_to_cpu(wa->user);
624 u64 pad = le64_to_cpu(wa->pad);
625 u64 gc = le64_to_cpu(wa->gc);
626
627 atomic64_set(&pblk->user_wa, user);
628 atomic64_set(&pblk->pad_wa, pad);
629 atomic64_set(&pblk->gc_wa, gc);
630
631 pblk->user_rst_wa = user;
632 pblk->pad_rst_wa = pad;
633 pblk->gc_rst_wa = gc;
634 }
635}
636
Hans Holmberg9156f362018-03-30 00:05:22 +0200637static int pblk_line_was_written(struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200638 struct pblk *pblk)
Hans Holmberg9156f362018-03-30 00:05:22 +0200639{
640
Hans Holmbergb06be282018-06-01 16:41:12 +0200641 struct pblk_line_meta *lm = &pblk->lm;
642 struct nvm_tgt_dev *dev = pblk->dev;
643 struct nvm_geo *geo = &dev->geo;
644 struct nvm_chk_meta *chunk;
645 struct ppa_addr bppa;
646 int smeta_blk;
Hans Holmberg9156f362018-03-30 00:05:22 +0200647
Hans Holmbergb06be282018-06-01 16:41:12 +0200648 if (line->state == PBLK_LINESTATE_BAD)
649 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200650
Hans Holmbergb06be282018-06-01 16:41:12 +0200651 smeta_blk = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
652 if (smeta_blk >= lm->blk_per_line)
653 return 0;
654
655 bppa = pblk->luns[smeta_blk].bppa;
656 chunk = &line->chks[pblk_ppa_to_pos(geo, bppa)];
657
Igor Konopkoa24eab52019-05-04 20:38:07 +0200658 if (chunk->state & NVM_CHK_ST_CLOSED ||
659 (chunk->state & NVM_CHK_ST_OPEN
660 && chunk->wp >= lm->smeta_sec))
661 return 1;
Hans Holmbergb06be282018-06-01 16:41:12 +0200662
Igor Konopkoa24eab52019-05-04 20:38:07 +0200663 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200664}
665
Javier González6ad2f612018-10-09 13:12:05 +0200666static bool pblk_line_is_open(struct pblk *pblk, struct pblk_line *line)
667{
668 struct pblk_line_meta *lm = &pblk->lm;
669 int i;
670
671 for (i = 0; i < lm->blk_per_line; i++)
672 if (line->chks[i].state & NVM_CHK_ST_OPEN)
673 return true;
674
675 return false;
676}
677
Hans Holmberg75610cd2017-10-13 14:46:38 +0200678struct pblk_line *pblk_recov_l2p(struct pblk *pblk)
679{
680 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200681 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
682 struct pblk_line *line, *tline, *data_line = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200683 struct pblk_smeta *smeta;
684 struct pblk_emeta *emeta;
685 struct line_smeta *smeta_buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200686 int found_lines = 0, recovered_lines = 0, open_lines = 0;
687 int is_next = 0;
688 int meta_line;
689 int i, valid_uuid = 0;
690 LIST_HEAD(recov_list);
691
692 /* TODO: Implement FTL snapshot */
693
694 /* Scan recovery - takes place when FTL snapshot fails */
695 spin_lock(&l_mg->free_lock);
696 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
697 set_bit(meta_line, &l_mg->meta_bitmap);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200698 smeta = l_mg->sline_meta[meta_line];
699 emeta = l_mg->eline_meta[meta_line];
Javier González8224cbd2017-06-30 17:56:38 +0200700 smeta_buf = (struct line_smeta *)smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200701 spin_unlock(&l_mg->free_lock);
702
703 /* Order data lines using their sequence number */
704 for (i = 0; i < l_mg->nr_lines; i++) {
705 u32 crc;
706
707 line = &pblk->lines[i];
708
709 memset(smeta, 0, lm->smeta_len);
710 line->smeta = smeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200711 line->lun_bitmap = ((void *)(smeta_buf)) +
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200712 sizeof(struct line_smeta);
713
Hans Holmbergb06be282018-06-01 16:41:12 +0200714 if (!pblk_line_was_written(line, pblk))
Hans Holmberg9156f362018-03-30 00:05:22 +0200715 continue;
716
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717 /* Lines that cannot be read are assumed as not written here */
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200718 if (pblk_line_smeta_read(pblk, line))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200719 continue;
720
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200721 crc = pblk_calc_smeta_crc(pblk, smeta_buf);
722 if (le32_to_cpu(smeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200723 continue;
724
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200725 if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200726 continue;
727
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200728 if (smeta_buf->header.version_major != SMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200729 pblk_err(pblk, "found incompatible line version %u\n",
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200730 smeta_buf->header.version_major);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200731 return ERR_PTR(-EINVAL);
732 }
733
734 /* The first valid instance uuid is used for initialization */
735 if (!valid_uuid) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100736 guid_copy(&pblk->instance_uuid,
737 (guid_t *)&smeta_buf->header.uuid);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200738 valid_uuid = 1;
739 }
740
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100741 if (!guid_equal(&pblk->instance_uuid,
742 (guid_t *)&smeta_buf->header.uuid)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200743 pblk_debug(pblk, "ignore line %u due to uuid mismatch\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200744 i);
745 continue;
746 }
747
748 /* Update line metadata */
749 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200750 line->id = le32_to_cpu(smeta_buf->header.id);
751 line->type = le16_to_cpu(smeta_buf->header.type);
752 line->seq_nr = le64_to_cpu(smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200753 spin_unlock(&line->lock);
754
755 /* Update general metadata */
756 spin_lock(&l_mg->free_lock);
757 if (line->seq_nr >= l_mg->d_seq_nr)
758 l_mg->d_seq_nr = line->seq_nr + 1;
759 l_mg->nr_free_lines--;
760 spin_unlock(&l_mg->free_lock);
761
762 if (pblk_line_recov_alloc(pblk, line))
763 goto out;
764
765 pblk_recov_line_add_ordered(&recov_list, line);
766 found_lines++;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200767 pblk_debug(pblk, "recovering data line %d, seq:%llu\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200768 line->id, smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200769 }
770
771 if (!found_lines) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100772 guid_gen(&pblk->instance_uuid);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200773
774 spin_lock(&l_mg->free_lock);
775 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
776 &l_mg->meta_bitmap));
777 spin_unlock(&l_mg->free_lock);
778
779 goto out;
780 }
781
782 /* Verify closed blocks and recover this portion of L2P table*/
783 list_for_each_entry_safe(line, tline, &recov_list, list) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200784 recovered_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200785
Hans Holmberg75610cd2017-10-13 14:46:38 +0200786 line->emeta_ssec = pblk_line_emeta_start(pblk, line);
Javier González8224cbd2017-06-30 17:56:38 +0200787 line->emeta = emeta;
788 memset(line->emeta->buf, 0, lm->emeta_len[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789
Javier González6ad2f612018-10-09 13:12:05 +0200790 if (pblk_line_is_open(pblk, line)) {
791 pblk_recov_l2p_from_oob(pblk, line);
792 goto next;
793 }
794
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200795 if (pblk_line_emeta_read(pblk, line, line->emeta->buf)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200796 pblk_recov_l2p_from_oob(pblk, line);
797 goto next;
798 }
799
Hans Holmberg06bc0722018-01-05 14:16:08 +0100800 if (pblk_recov_check_emeta(pblk, line->emeta->buf)) {
801 pblk_recov_l2p_from_oob(pblk, line);
802 goto next;
803 }
804
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200805 if (pblk_recov_check_line_version(pblk, line->emeta->buf))
806 return ERR_PTR(-EINVAL);
807
Hans Holmberg76758392018-03-30 00:04:52 +0200808 pblk_recov_wa_counters(pblk, line->emeta->buf);
809
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200810 if (pblk_recov_l2p_from_emeta(pblk, line))
811 pblk_recov_l2p_from_oob(pblk, line);
812
813next:
814 if (pblk_line_is_full(line)) {
815 struct list_head *move_list;
816
817 spin_lock(&line->lock);
818 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +0200819 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
820 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200821 move_list = pblk_line_gc_list(pblk, line);
822 spin_unlock(&line->lock);
823
824 spin_lock(&l_mg->gc_lock);
825 list_move_tail(&line->list, move_list);
826 spin_unlock(&l_mg->gc_lock);
827
Hans Holmberg53d82db2018-10-09 13:11:47 +0200828 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200829 line->map_bitmap = NULL;
830 line->smeta = NULL;
831 line->emeta = NULL;
832 } else {
Javier González6ad2f612018-10-09 13:12:05 +0200833 spin_lock(&line->lock);
834 line->state = PBLK_LINESTATE_OPEN;
835 spin_unlock(&line->lock);
836
837 line->emeta->mem = 0;
838 atomic_set(&line->emeta->sync, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200839
Hans Holmbergf2937232018-10-09 13:11:53 +0200840 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
841 line->state);
842
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200843 data_line = line;
Javier González6ad2f612018-10-09 13:12:05 +0200844 line->meta_line = meta_line;
845
846 open_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847 }
848 }
849
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200850 if (!open_lines) {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200851 spin_lock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
853 &l_mg->meta_bitmap));
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200854 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200855 } else {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200856 spin_lock(&l_mg->free_lock);
Marcin Dziegielewski4bbae692019-05-04 20:37:57 +0200857 l_mg->data_line = data_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200858 /* Allocate next line for preparation */
859 l_mg->data_next = pblk_line_get(pblk);
860 if (l_mg->data_next) {
861 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
862 l_mg->data_next->type = PBLK_LINETYPE_DATA;
863 is_next = 1;
864 }
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200865 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200867
Javier Gonzáleza7689932018-01-05 14:16:13 +0100868 if (is_next)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869 pblk_line_erase(pblk, l_mg->data_next);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200870
871out:
872 if (found_lines != recovered_lines)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200873 pblk_err(pblk, "failed to recover all found lines %d/%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200874 found_lines, recovered_lines);
875
876 return data_line;
877}
878
879/*
Javier González588726d32017-06-26 11:57:29 +0200880 * Pad current line
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200881 */
Javier González588726d32017-06-26 11:57:29 +0200882int pblk_recov_pad(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200883{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200884 struct pblk_line *line;
885 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +0200886 int left_msecs;
887 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200888
889 spin_lock(&l_mg->free_lock);
890 line = l_mg->data_line;
Javier González588726d32017-06-26 11:57:29 +0200891 left_msecs = line->left_msecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892 spin_unlock(&l_mg->free_lock);
893
Javier González6ad2f612018-10-09 13:12:05 +0200894 ret = pblk_recov_pad_line(pblk, line, left_msecs);
Javier González588726d32017-06-26 11:57:29 +0200895 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200896 pblk_err(pblk, "tear down padding failed (%d)\n", ret);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200897 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898 }
899
Javier González588726d32017-06-26 11:57:29 +0200900 pblk_line_close_meta(pblk, line);
Javier González588726d32017-06-26 11:57:29 +0200901 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902}